blob: cfce25ec47cda6c3d46c91ebd61f8cf0ff467688 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
Kristian Høgsberg07045392012-02-19 18:52:44 -05002 * Copyright © 2010-2012 Intel Corporation
Pekka Paalanend581a8f2012-01-27 16:25:16 +02003 * Copyright © 2011-2012 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Pekka Paalanen460099f2012-01-20 16:48:25 +020032#include <math.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033
Pekka Paalanen50719bc2011-11-22 14:18:50 +020034#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040036#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020037#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038
Pekka Paalanen068ae942011-11-28 14:11:15 +020039struct shell_surface;
40
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050042 struct weston_compositor *compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040043
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -050048 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
53
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020054 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050055 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020056 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020057 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020058
59 unsigned deathcount;
60 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020061 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020062
63 bool locked;
64 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020065
Pekka Paalanen068ae942011-11-28 14:11:15 +020066 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050067 struct wl_listener lock_surface_listener;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010068
69 struct wl_list backgrounds;
70 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020071
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020073 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020074 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 struct wl_resource *binding;
76 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050077 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 } screensaver;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -050079
80 struct weston_surface *debug_repaint_surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040081};
82
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050083enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020084 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050085
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086 SHELL_SURFACE_PANEL,
87 SHELL_SURFACE_BACKGROUND,
88 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020089 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050090
91 SHELL_SURFACE_TOPLEVEL,
92 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050093 SHELL_SURFACE_FULLSCREEN,
Juan Zhao96879df2012-02-07 08:45:41 +080094 SHELL_SURFACE_MAXIMIZED,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050095 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020096};
97
Pekka Paalanen56cdea92011-11-23 16:14:12 +020098struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020099 struct wl_resource resource;
100
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500101 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200102 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500103 struct shell_surface *parent;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400104 struct wl_shell *shell;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200105
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500106 enum shell_surface_type type;
107 int32_t saved_x, saved_y;
Alex Wu4539b082012-03-01 12:57:46 +0800108 bool saved_position_valid;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100109
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500110 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +0200111 struct weston_transform transform;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500112 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200113 } rotation;
114
115 struct {
Scott Moreau447013d2012-02-18 05:05:29 -0700116 struct wl_pointer_grab grab;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500117 int32_t x, y;
Pekka Paalanen938269a2012-02-07 14:19:01 +0200118 struct weston_transform parent_transform;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500119 int32_t initial_up;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400120 struct wl_input_device *device;
121 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500122 } popup;
123
Alex Wu4539b082012-03-01 12:57:46 +0800124 struct {
125 enum wl_shell_surface_fullscreen_method type;
126 struct weston_transform transform; /* matrix from x, y */
127 uint32_t framerate;
128 struct weston_surface *black_surface;
129 } fullscreen;
130
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -0500131 struct weston_output *fullscreen_output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500132 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100133 struct wl_list link;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300134
135 int force_configure;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200136};
137
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300138struct shell_grab {
Scott Moreau447013d2012-02-18 05:05:29 -0700139 struct wl_pointer_grab grab;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300140 struct shell_surface *shsurf;
141 struct wl_listener shsurf_destroy_listener;
142};
143
144struct weston_move_grab {
145 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500146 int32_t dx, dy;
147};
148
Pekka Paalanen460099f2012-01-20 16:48:25 +0200149struct rotate_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300150 struct shell_grab base;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500151 struct weston_matrix rotation;
Pekka Paalanen460099f2012-01-20 16:48:25 +0200152 struct {
153 int32_t x;
154 int32_t y;
155 } center;
156};
157
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500158static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400159destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300160{
161 struct shell_grab *grab;
162
163 grab = container_of(listener, struct shell_grab,
164 shsurf_destroy_listener);
165
166 grab->shsurf = NULL;
167}
168
169static void
170shell_grab_init(struct shell_grab *grab,
171 const struct wl_pointer_grab_interface *interface,
172 struct shell_surface *shsurf)
173{
174 grab->grab.interface = interface;
175 grab->shsurf = shsurf;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400176 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
177 wl_signal_add(&shsurf->resource.destroy_signal,
178 &grab->shsurf_destroy_listener);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300179
180}
181
182static void
183shell_grab_finish(struct shell_grab *grab)
184{
185 wl_list_remove(&grab->shsurf_destroy_listener.link);
186}
187
188static void
Alex Wu4539b082012-03-01 12:57:46 +0800189center_on_output(struct weston_surface *surface,
190 struct weston_output *output);
191
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300192static struct shell_surface *
193get_shell_surface(struct weston_surface *surface);
194
Alex Wu4539b082012-03-01 12:57:46 +0800195static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200196shell_configuration(struct wl_shell *shell)
197{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200198 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200199 char *path = NULL;
200 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200201
202 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200203 { "path", CONFIG_KEY_STRING, &path },
204 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200205 };
206
207 struct config_section cs[] = {
208 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
209 };
210
Tiago Vignatti9a206c42012-03-21 19:49:18 +0200211 config_file = config_file_path("weston.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500212 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200213 free(config_file);
214
Pekka Paalanen7296e792011-12-07 16:22:00 +0200215 shell->screensaver.path = path;
216 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200217}
218
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200219static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400220noop_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500221 struct wl_surface *surface, int32_t x, int32_t y)
222{
223 grab->focus = NULL;
224}
225
226static void
Scott Moreau447013d2012-02-18 05:05:29 -0700227move_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500228 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500229{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500230 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500231 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300232 struct shell_surface *shsurf = move->base.shsurf;
233 struct weston_surface *es;
234
235 if (!shsurf)
236 return;
237
238 es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500240 weston_surface_configure(es,
241 device->x + move->dx,
242 device->y + move->dy,
Pekka Paalanen60921e52012-01-25 15:55:43 +0200243 es->geometry.width, es->geometry.height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244}
245
246static void
Scott Moreau447013d2012-02-18 05:05:29 -0700247move_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400248 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300250 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
251 grab);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500252 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500254 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300255 shell_grab_finish(shell_grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400256 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500257 free(grab);
258 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259}
260
Scott Moreau447013d2012-02-18 05:05:29 -0700261static const struct wl_pointer_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500262 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263 move_grab_motion,
264 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265};
266
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400267static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_surface_move(struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400269 struct weston_input_device *wd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271 struct weston_move_grab *move;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300272 struct shell_surface *shsurf = get_shell_surface(es);
273
274 if (!shsurf)
275 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276
277 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400278 if (!move)
279 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500280
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300281 shell_grab_init(&move->base, &move_grab_interface, shsurf);
282
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200283 move->dx = es->geometry.x - wd->input_device.grab_x;
284 move->dy = es->geometry.y - wd->input_device.grab_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500285
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300286 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400287 &move->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500288
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400289 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400290
291 return 0;
292}
293
294static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200295shell_surface_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400296 struct wl_resource *input_resource, uint32_t serial)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400297{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500298 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200299 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400300
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500301 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400302 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500303 wd->input_device.pointer_focus != &shsurf->surface->surface)
304 return;
305
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400306 if (weston_surface_move(shsurf->surface, wd) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400307 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308}
309
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500310struct weston_resize_grab {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300311 struct shell_grab base;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500312 uint32_t edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200313 int32_t width, height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500314};
315
316static void
Scott Moreau447013d2012-02-18 05:05:29 -0700317resize_grab_motion(struct wl_pointer_grab *grab,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318 uint32_t time, int32_t x, int32_t y)
319{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500321 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500322 int32_t width, height;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200323 int32_t from_x, from_y;
324 int32_t to_x, to_y;
325
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300326 if (!resize->base.shsurf)
327 return;
328
329 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200330 device->grab_x, device->grab_y,
331 &from_x, &from_y);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300332 weston_surface_from_global(resize->base.shsurf->surface,
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200333 device->x, device->y, &to_x, &to_y);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500334
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200335 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200336 width = resize->width + from_x - to_x;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200337 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200338 width = resize->width + to_x - from_x;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500339 } else {
340 width = resize->width;
341 }
342
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200343 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200344 height = resize->height + from_y - to_y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200345 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200346 height = resize->height + to_y - from_y;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500347 } else {
348 height = resize->height;
349 }
350
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300351 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400352 resize->edges, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500353}
354
355static void
Scott Moreau447013d2012-02-18 05:05:29 -0700356resize_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400357 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500358{
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300359 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500360 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500361
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500362 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300363 shell_grab_finish(&resize->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400364 wl_input_device_end_pointer_grab(device);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500365 free(grab);
366 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500367}
368
Scott Moreau447013d2012-02-18 05:05:29 -0700369static const struct wl_pointer_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500370 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500371 resize_grab_motion,
372 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500373};
374
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400375static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500376weston_surface_resize(struct shell_surface *shsurf,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400377 struct weston_input_device *wd, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500378{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500379 struct weston_resize_grab *resize;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500380
Alex Wu4539b082012-03-01 12:57:46 +0800381 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
382 return 0;
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500383
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200384 if (edges == 0 || edges > 15 ||
385 (edges & 3) == 3 || (edges & 12) == 12)
386 return 0;
387
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500388 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400389 if (!resize)
390 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500391
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300392 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
393
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500394 resize->edges = edges;
Pekka Paalanen5c97ae72012-01-30 16:19:47 +0200395 resize->width = shsurf->surface->geometry.width;
396 resize->height = shsurf->surface->geometry.height;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400397
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +0300398 wl_input_device_start_pointer_grab(&wd->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400399 &resize->base.grab);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500400
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400401 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400402
403 return 0;
404}
405
406static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200407shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400408 struct wl_resource *input_resource, uint32_t serial,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200409 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200412 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400413
Alex Wu4539b082012-03-01 12:57:46 +0800414 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
415 return;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400416
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500417 if (wd->input_device.button_count == 0 ||
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400418 wd->input_device.grab_serial != serial ||
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500419 wd->input_device.pointer_focus != &shsurf->surface->surface)
420 return;
421
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400422 if (weston_surface_resize(shsurf, wd, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400423 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500424}
425
Juan Zhao96879df2012-02-07 08:45:41 +0800426static struct weston_output *
427get_default_output(struct weston_compositor *compositor)
428{
429 return container_of(compositor->output_list.next,
430 struct weston_output, link);
431}
432
Alex Wu4539b082012-03-01 12:57:46 +0800433static void
434shell_unset_fullscreen(struct shell_surface *shsurf)
435{
436 /* undo all fullscreen things here */
437 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
438 shsurf->fullscreen.framerate = 0;
439 wl_list_remove(&shsurf->fullscreen.transform.link);
440 wl_list_init(&shsurf->fullscreen.transform.link);
441 weston_surface_destroy(shsurf->fullscreen.black_surface);
442 shsurf->fullscreen.black_surface = NULL;
443 shsurf->fullscreen_output = NULL;
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300444 shsurf->force_configure = 1;
Alex Wu4539b082012-03-01 12:57:46 +0800445 weston_surface_set_position(shsurf->surface,
446 shsurf->saved_x, shsurf->saved_y);
447}
448
Pekka Paalanen98262232011-12-01 10:42:22 +0200449static int
450reset_shell_surface_type(struct shell_surface *surface)
451{
452 switch (surface->type) {
453 case SHELL_SURFACE_FULLSCREEN:
Alex Wu4539b082012-03-01 12:57:46 +0800454 shell_unset_fullscreen(surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200455 break;
Juan Zhao96879df2012-02-07 08:45:41 +0800456 case SHELL_SURFACE_MAXIMIZED:
457 surface->output = get_default_output(surface->surface->compositor);
458 weston_surface_set_position(surface->surface,
459 surface->saved_x,
460 surface->saved_y);
461 break;
Pekka Paalanen98262232011-12-01 10:42:22 +0200462 case SHELL_SURFACE_PANEL:
463 case SHELL_SURFACE_BACKGROUND:
464 wl_list_remove(&surface->link);
465 wl_list_init(&surface->link);
466 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200467 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200468 case SHELL_SURFACE_LOCK:
469 wl_resource_post_error(&surface->resource,
470 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200471 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200472 return -1;
473 case SHELL_SURFACE_NONE:
474 case SHELL_SURFACE_TOPLEVEL:
475 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500476 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200477 break;
478 }
479
480 surface->type = SHELL_SURFACE_NONE;
481 return 0;
482}
483
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500484static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200485shell_surface_set_toplevel(struct wl_client *client,
486 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400487
488{
Pekka Paalanen98262232011-12-01 10:42:22 +0200489 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400490
Pekka Paalanen98262232011-12-01 10:42:22 +0200491 if (reset_shell_surface_type(surface))
492 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400493
Pekka Paalanen98262232011-12-01 10:42:22 +0200494 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400495}
496
497static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200498shell_surface_set_transient(struct wl_client *client,
499 struct wl_resource *resource,
500 struct wl_resource *parent_resource,
501 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400502{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200503 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500504 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200505 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500506 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400507
Pekka Paalanen98262232011-12-01 10:42:22 +0200508 if (reset_shell_surface_type(shsurf))
509 return;
510
Alex Wu4539b082012-03-01 12:57:46 +0800511 /* assign to parents output */
Alex Wu88277d12012-02-22 14:50:46 +0800512 shsurf->output = pes->output;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200513 weston_surface_set_position(es, pes->geometry.x + x,
514 pes->geometry.y + y);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400515
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200516 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400517}
518
Juan Zhao96879df2012-02-07 08:45:41 +0800519static struct wl_shell *
520shell_surface_get_shell(struct shell_surface *shsurf)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200521{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400522 return shsurf->shell;
Juan Zhao96879df2012-02-07 08:45:41 +0800523}
524
525static int
526get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
527{
528 struct shell_surface *priv;
529 int panel_height = 0;
530
531 if (!output)
532 return 0;
533
534 wl_list_for_each(priv, &wlshell->panels, link) {
535 if (priv->output == output) {
536 panel_height = priv->surface->geometry.height;
537 break;
538 }
539 }
540 return panel_height;
541}
542
543static void
544shell_surface_set_maximized(struct wl_client *client,
545 struct wl_resource *resource,
546 struct wl_resource *output_resource )
547{
548 struct shell_surface *shsurf = resource->data;
549 struct weston_surface *es = shsurf->surface;
550 struct wl_shell *wlshell = NULL;
551 uint32_t edges = 0, panel_height = 0;
552
553 /* get the default output, if the client set it as NULL
554 check whether the ouput is available */
555 if (output_resource)
556 shsurf->output = output_resource->data;
557 else
558 shsurf->output = get_default_output(es->compositor);
559
560 if (reset_shell_surface_type(shsurf))
561 return;
562
563 shsurf->saved_x = es->geometry.x;
564 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800565 shsurf->saved_position_valid = true;
Juan Zhao96879df2012-02-07 08:45:41 +0800566
567 wlshell = shell_surface_get_shell(shsurf);
568 panel_height = get_output_panel_height(wlshell, es->output);
569 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500570
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400571 wl_shell_surface_send_configure(&shsurf->resource, edges,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500572 es->output->current->width,
573 es->output->current->height - panel_height);
Juan Zhao96879df2012-02-07 08:45:41 +0800574
575 shsurf->type = SHELL_SURFACE_MAXIMIZED;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200576}
577
Alex Wu21858432012-04-01 20:13:08 +0800578static void
579black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
580
Alex Wu4539b082012-03-01 12:57:46 +0800581static struct weston_surface *
582create_black_surface(struct weston_compositor *ec,
Alex Wu21858432012-04-01 20:13:08 +0800583 struct weston_surface *fs_surface,
Alex Wu4539b082012-03-01 12:57:46 +0800584 GLfloat x, GLfloat y, int w, int h)
585{
586 struct weston_surface *surface = NULL;
587
588 surface = weston_surface_create(ec);
589 if (surface == NULL) {
590 fprintf(stderr, "no memory\n");
591 return NULL;
592 }
593
Alex Wu21858432012-04-01 20:13:08 +0800594 surface->configure = black_surface_configure;
595 surface->private = fs_surface;
Alex Wu4539b082012-03-01 12:57:46 +0800596 weston_surface_configure(surface, x, y, w, h);
597 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
598 return surface;
599}
600
601/* Create black surface and append it to the associated fullscreen surface.
602 * Handle size dismatch and positioning according to the method. */
603static void
604shell_configure_fullscreen(struct shell_surface *shsurf)
605{
606 struct weston_output *output = shsurf->fullscreen_output;
607 struct weston_surface *surface = shsurf->surface;
608 struct weston_matrix *matrix;
609 float scale;
610
611 center_on_output(surface, output);
612
613 if (!shsurf->fullscreen.black_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500614 shsurf->fullscreen.black_surface =
615 create_black_surface(surface->compositor,
Alex Wu21858432012-04-01 20:13:08 +0800616 surface,
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500617 output->x, output->y,
618 output->current->width,
619 output->current->height);
620
621 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
622 wl_list_insert(&surface->layer_link,
623 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800624 shsurf->fullscreen.black_surface->output = output;
625
626 switch (shsurf->fullscreen.type) {
627 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
628 break;
629 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
630 matrix = &shsurf->fullscreen.transform.matrix;
631 weston_matrix_init(matrix);
632 scale = (float)output->current->width/(float)surface->geometry.width;
633 weston_matrix_scale(matrix, scale, scale, 1);
634 wl_list_remove(&shsurf->fullscreen.transform.link);
635 wl_list_insert(surface->geometry.transformation_list.prev,
636 &shsurf->fullscreen.transform.link);
637 weston_surface_set_position(surface, output->x, output->y);
638 break;
639 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
640 break;
641 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
642 break;
643 default:
644 break;
645 }
646}
647
648/* make the fullscreen and black surface at the top */
649static void
650shell_stack_fullscreen(struct shell_surface *shsurf)
651{
652 struct weston_surface *surface = shsurf->surface;
653 struct wl_shell *shell = shell_surface_get_shell(shsurf);
Alex Wu4539b082012-03-01 12:57:46 +0800654
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500655 wl_list_remove(&surface->layer_link);
656 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800657
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500658 wl_list_insert(&shell->fullscreen_layer.surface_list,
659 &surface->layer_link);
660 wl_list_insert(&surface->layer_link,
661 &shsurf->fullscreen.black_surface->layer_link);
Alex Wu4539b082012-03-01 12:57:46 +0800662
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500663 weston_surface_damage(surface);
664 weston_surface_damage(shsurf->fullscreen.black_surface);
Alex Wu4539b082012-03-01 12:57:46 +0800665}
666
667static void
668shell_map_fullscreen(struct shell_surface *shsurf)
669{
670 shell_configure_fullscreen(shsurf);
671 shell_stack_fullscreen(shsurf);
672}
673
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400674static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200675shell_surface_set_fullscreen(struct wl_client *client,
Kristian Høgsbergf856fd22012-02-16 15:58:14 -0500676 struct wl_resource *resource,
677 uint32_t method,
678 uint32_t framerate,
679 struct wl_resource *output_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400680{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200681 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500682 struct weston_surface *es = shsurf->surface;
Alex Wu4539b082012-03-01 12:57:46 +0800683
684 if (output_resource)
685 shsurf->output = output_resource->data;
686 else
687 shsurf->output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400688
Pekka Paalanen98262232011-12-01 10:42:22 +0200689 if (reset_shell_surface_type(shsurf))
690 return;
691
Alex Wu4539b082012-03-01 12:57:46 +0800692 shsurf->fullscreen_output = shsurf->output;
693 shsurf->fullscreen.type = method;
694 shsurf->fullscreen.framerate = framerate;
695 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400696
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200697 shsurf->saved_x = es->geometry.x;
698 shsurf->saved_y = es->geometry.y;
Alex Wu4539b082012-03-01 12:57:46 +0800699 shsurf->saved_position_valid = true;
700
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300701 if (weston_surface_is_mapped(es))
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +0300702 shsurf->force_configure = 1;
Kristian Høgsbergd5ae9f42012-02-16 23:38:14 -0500703
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400704 wl_shell_surface_send_configure(&shsurf->resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500705 shsurf->output->current->width,
706 shsurf->output->current->height);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400707}
708
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500709static void
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400710popup_grab_focus(struct wl_pointer_grab *grab,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500711 struct wl_surface *surface, int32_t x, int32_t y)
712{
713 struct wl_input_device *device = grab->input_device;
714 struct shell_surface *priv =
715 container_of(grab, struct shell_surface, popup.grab);
716 struct wl_client *client = priv->surface->surface.resource.client;
717
Pekka Paalanencb108432012-01-19 16:25:40 +0200718 if (surface && surface->resource.client == client) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400719 wl_input_device_set_pointer_focus(device, surface, x, y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500720 grab->focus = surface;
721 } else {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400722 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500723 grab->focus = NULL;
724 }
725}
726
727static void
Scott Moreau447013d2012-02-18 05:05:29 -0700728popup_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanenb29f4122012-02-14 14:59:18 +0200729 uint32_t time, int32_t sx, int32_t sy)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500730{
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500731 struct wl_resource *resource;
732
733 resource = grab->input_device->pointer_focus_resource;
734 if (resource)
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500735 wl_input_device_send_motion(resource, time, sx, sy);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500736}
737
738static void
Scott Moreau447013d2012-02-18 05:05:29 -0700739popup_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -0400740 uint32_t time, uint32_t button, int32_t state)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500741{
742 struct wl_resource *resource;
743 struct shell_surface *shsurf =
744 container_of(grab, struct shell_surface, popup.grab);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400745 struct wl_display *display;
746 uint32_t serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500747
748 resource = grab->input_device->pointer_focus_resource;
749 if (resource) {
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400750 display = wl_client_get_display(resource->client);
751 serial = wl_display_get_serial(display);
752 wl_input_device_send_button(resource, serial,
753 time, button, state);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500754 } else if (state == 0 &&
755 (shsurf->popup.initial_up ||
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400756 time - shsurf->popup.device->grab_time > 500)) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -0500757 wl_shell_surface_send_popup_done(&shsurf->resource);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400758 wl_input_device_end_pointer_grab(grab->input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500759 shsurf->popup.grab.input_device = NULL;
760 }
761
762 if (state == 0)
763 shsurf->popup.initial_up = 1;
764}
765
Scott Moreau447013d2012-02-18 05:05:29 -0700766static const struct wl_pointer_grab_interface popup_grab_interface = {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500767 popup_grab_focus,
768 popup_grab_motion,
769 popup_grab_button,
770};
771
772static void
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400773shell_map_popup(struct shell_surface *shsurf)
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500774{
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400775 struct wl_input_device *device = shsurf->popup.device;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500776 struct weston_surface *es = shsurf->surface;
777 struct weston_surface *parent = shsurf->parent->surface;
778
779 es->output = parent->output;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500780 shsurf->popup.grab.interface = &popup_grab_interface;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500781
Pekka Paalanen938269a2012-02-07 14:19:01 +0200782 weston_surface_update_transform(parent);
783 if (parent->transform.enabled) {
784 shsurf->popup.parent_transform.matrix =
785 parent->transform.matrix;
786 } else {
787 /* construct x, y translation matrix */
788 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
789 shsurf->popup.parent_transform.matrix.d[12] =
790 parent->geometry.x;
791 shsurf->popup.parent_transform.matrix.d[13] =
792 parent->geometry.y;
793 }
794 wl_list_insert(es->geometry.transformation_list.prev,
795 &shsurf->popup.parent_transform.link);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200796 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500797
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500798 shsurf->popup.initial_up = 0;
799
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400800 /* We don't require the grab to still be active, but if another
801 * grab has started in the meantime, we end the popup now. */
802 if (device->grab_serial == shsurf->popup.serial) {
803 wl_input_device_start_pointer_grab(device,
804 &shsurf->popup.grab);
805 } else {
806 wl_shell_surface_send_popup_done(&shsurf->resource);
807 }
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500808}
809
810static void
811shell_surface_set_popup(struct wl_client *client,
812 struct wl_resource *resource,
813 struct wl_resource *input_device_resource,
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400814 uint32_t serial,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500815 struct wl_resource *parent_resource,
816 int32_t x, int32_t y, uint32_t flags)
817{
818 struct shell_surface *shsurf = resource->data;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500819
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500820 shsurf->type = SHELL_SURFACE_POPUP;
821 shsurf->parent = parent_resource->data;
Kristian Høgsberg3730f362012-04-13 12:40:07 -0400822 shsurf->popup.device = input_device_resource->data;
823 shsurf->popup.serial = serial;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500824 shsurf->popup.x = x;
825 shsurf->popup.y = y;
826}
827
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200828static const struct wl_shell_surface_interface shell_surface_implementation = {
829 shell_surface_move,
830 shell_surface_resize,
831 shell_surface_set_toplevel,
832 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500833 shell_surface_set_fullscreen,
Juan Zhao96879df2012-02-07 08:45:41 +0800834 shell_surface_set_popup,
835 shell_surface_set_maximized
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200836};
837
838static void
839destroy_shell_surface(struct wl_resource *resource)
840{
841 struct shell_surface *shsurf = resource->data;
842
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500843 if (shsurf->popup.grab.input_device)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400844 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500845
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200846 /* in case cleaning up a dead client destroys shell_surface first */
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300847 if (shsurf->surface) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200848 wl_list_remove(&shsurf->surface_destroy_listener.link);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300849 shsurf->surface->configure = NULL;
850 }
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200851
Alex Wuaa08e2d2012-03-05 11:01:40 +0800852 if (shsurf->fullscreen.black_surface)
853 weston_surface_destroy(shsurf->fullscreen.black_surface);
854
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200855 wl_list_remove(&shsurf->link);
856 free(shsurf);
857}
858
859static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400860shell_handle_surface_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200861{
862 struct shell_surface *shsurf = container_of(listener,
863 struct shell_surface,
864 surface_destroy_listener);
865
866 shsurf->surface = NULL;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400867 wl_resource_destroy(&shsurf->resource);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200868}
869
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200870static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500871get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200872{
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200873 struct wl_listener *listener;
874
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400875 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
876 shell_handle_surface_destroy);
877 if (listener)
878 return container_of(listener, struct shell_surface,
879 surface_destroy_listener);
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200880
881 return NULL;
882}
883
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200884static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300885shell_surface_configure(struct weston_surface *, int32_t, int32_t);
886
887static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200888shell_get_shell_surface(struct wl_client *client,
889 struct wl_resource *resource,
890 uint32_t id,
891 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200892{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500893 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200894 struct shell_surface *shsurf;
895
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200896 if (get_shell_surface(surface)) {
897 wl_resource_post_error(surface_resource,
898 WL_DISPLAY_ERROR_INVALID_OBJECT,
899 "wl_shell::get_shell_surface already requested");
900 return;
901 }
902
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300903 if (surface->configure) {
904 wl_resource_post_error(surface_resource,
905 WL_DISPLAY_ERROR_INVALID_OBJECT,
906 "surface->configure already set");
907 return;
908 }
909
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200910 shsurf = calloc(1, sizeof *shsurf);
911 if (!shsurf) {
912 wl_resource_post_no_memory(resource);
913 return;
914 }
915
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +0300916 surface->configure = shell_surface_configure;
917
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200918 shsurf->resource.destroy = destroy_shell_surface;
919 shsurf->resource.object.id = id;
920 shsurf->resource.object.interface = &wl_shell_surface_interface;
921 shsurf->resource.object.implementation =
922 (void (**)(void)) &shell_surface_implementation;
923 shsurf->resource.data = shsurf;
924
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400925 shsurf->shell = resource->data;
Alex Wu4539b082012-03-01 12:57:46 +0800926 shsurf->saved_position_valid = false;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200927 shsurf->surface = surface;
Alex Wu4539b082012-03-01 12:57:46 +0800928 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
929 shsurf->fullscreen.framerate = 0;
930 shsurf->fullscreen.black_surface = NULL;
931 wl_list_init(&shsurf->fullscreen.transform.link);
932
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400933 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
934 wl_signal_add(&surface->surface.resource.destroy_signal,
935 &shsurf->surface_destroy_listener);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200936
937 /* init link so its safe to always remove it in destroy_shell_surface */
938 wl_list_init(&shsurf->link);
939
Pekka Paalanen460099f2012-01-20 16:48:25 +0200940 /* empty when not in use */
941 wl_list_init(&shsurf->rotation.transform.link);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -0500942 weston_matrix_init(&shsurf->rotation.rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +0200943
Pekka Paalanen98262232011-12-01 10:42:22 +0200944 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200945
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200946 wl_client_add_resource(client, &shsurf->resource);
947}
948
949static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200950 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500951};
952
Kristian Høgsberg07937562011-04-12 17:25:42 -0400953static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500954handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200955{
956 proc->pid = 0;
957}
958
959static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200960launch_screensaver(struct wl_shell *shell)
961{
962 if (shell->screensaver.binding)
963 return;
964
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200965 if (!shell->screensaver.path)
966 return;
967
Kristian Høgsberg32bed572012-03-01 17:11:36 -0500968 if (shell->screensaver.process.pid != 0) {
969 fprintf(stderr, "old screensaver still running\n");
970 return;
971 }
972
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500973 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200974 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200975 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200976 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200977}
978
979static void
980terminate_screensaver(struct wl_shell *shell)
981{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200982 if (shell->screensaver.process.pid == 0)
983 return;
984
985 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200986}
987
988static void
989show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
990{
991 struct wl_list *list;
992
993 if (shell->lock_surface)
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500994 list = &shell->lock_surface->surface->layer_link;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995 else
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500996 list = &shell->lock_layer.surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200997
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500998 wl_list_remove(&surface->surface->layer_link);
999 wl_list_insert(list, &surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001000 surface->surface->output = surface->output;
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001001 weston_surface_damage(surface->surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001002}
1003
1004static void
1005hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
1006{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001007 wl_list_remove(&surface->surface->layer_link);
1008 wl_list_init(&surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001009 surface->surface->output = NULL;
1010}
1011
1012static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001013desktop_shell_set_background(struct wl_client *client,
1014 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001015 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001016 struct wl_resource *surface_resource)
1017{
1018 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001019 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001020 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001021 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001022
Pekka Paalanen98262232011-12-01 10:42:22 +02001023 if (reset_shell_surface_type(shsurf))
1024 return;
1025
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001026 wl_list_for_each(priv, &shell->backgrounds, link) {
1027 if (priv->output == output_resource->data) {
1028 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001029 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001030 wl_list_remove(&priv->link);
1031 break;
1032 }
1033 }
1034
Pekka Paalanen068ae942011-11-28 14:11:15 +02001035 shsurf->type = SHELL_SURFACE_BACKGROUND;
1036 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001037
Pekka Paalanen068ae942011-11-28 14:11:15 +02001038 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001039
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001040 weston_surface_set_position(surface, shsurf->output->x,
1041 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001042
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001043 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001044 surface_resource,
1045 shsurf->output->current->width,
1046 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001047}
1048
1049static void
1050desktop_shell_set_panel(struct wl_client *client,
1051 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001052 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -04001053 struct wl_resource *surface_resource)
1054{
1055 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +02001056 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001057 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001058 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001059
Pekka Paalanen98262232011-12-01 10:42:22 +02001060 if (reset_shell_surface_type(shsurf))
1061 return;
1062
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001063 wl_list_for_each(priv, &shell->panels, link) {
1064 if (priv->output == output_resource->data) {
1065 priv->surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001066 wl_list_remove(&priv->surface->layer_link);
Benjamin Franzkef02bb642011-11-23 20:46:40 +01001067 wl_list_remove(&priv->link);
1068 break;
1069 }
1070 }
1071
Pekka Paalanen068ae942011-11-28 14:11:15 +02001072 shsurf->type = SHELL_SURFACE_PANEL;
1073 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001074
Pekka Paalanen068ae942011-11-28 14:11:15 +02001075 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001076
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001077 weston_surface_set_position(surface, shsurf->output->x,
1078 shsurf->output->y);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001079
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001080 desktop_shell_send_configure(resource, 0,
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001081 surface_resource,
1082 shsurf->output->current->width,
1083 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001084}
1085
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001086static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001087handle_lock_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001088{
1089 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +02001090 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001091
1092 fprintf(stderr, "lock surface gone\n");
1093 shell->lock_surface = NULL;
1094}
1095
1096static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001097desktop_shell_set_lock_surface(struct wl_client *client,
1098 struct wl_resource *resource,
1099 struct wl_resource *surface_resource)
1100{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001101 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +02001102 struct shell_surface *surface = surface_resource->data;
1103
1104 if (reset_shell_surface_type(surface))
1105 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001106
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001107 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001108
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001109 if (!shell->locked)
1110 return;
1111
Pekka Paalanen98262232011-12-01 10:42:22 +02001112 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001113
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001114 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1115 wl_signal_add(&surface_resource->destroy_signal,
1116 &shell->lock_surface_listener);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001117
Pekka Paalanen068ae942011-11-28 14:11:15 +02001118 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001119}
1120
1121static void
1122resume_desktop(struct wl_shell *shell)
1123{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001124 struct shell_surface *tmp;
1125
1126 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1127 hide_screensaver(shell, tmp);
1128
1129 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001130
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001131 wl_list_remove(&shell->lock_layer.link);
1132 wl_list_insert(&shell->compositor->cursor_layer.link,
1133 &shell->fullscreen_layer.link);
1134 wl_list_insert(&shell->fullscreen_layer.link,
1135 &shell->panel_layer.link);
1136 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001137
1138 shell->locked = false;
Pekka Paalanen7296e792011-12-07 16:22:00 +02001139 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001140 weston_compositor_wake(shell->compositor);
Pekka Paalanenfc6d91a2012-02-10 15:33:10 +02001141 weston_compositor_damage_all(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001142}
1143
1144static void
1145desktop_shell_unlock(struct wl_client *client,
1146 struct wl_resource *resource)
1147{
1148 struct wl_shell *shell = resource->data;
1149
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001150 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001151
1152 if (shell->locked)
1153 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001154}
1155
Kristian Høgsberg75840622011-09-06 13:48:16 -04001156static const struct desktop_shell_interface desktop_shell_implementation = {
1157 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001158 desktop_shell_set_panel,
1159 desktop_shell_set_lock_surface,
1160 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -04001161};
1162
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001163static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001164get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001165{
1166 struct shell_surface *shsurf;
1167
1168 shsurf = get_shell_surface(surface);
1169 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +02001170 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001171 return shsurf->type;
1172}
1173
Kristian Høgsberg75840622011-09-06 13:48:16 -04001174static void
Kristian Høgsberg07937562011-04-12 17:25:42 -04001175move_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001176 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001177{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001178 struct weston_surface *surface =
1179 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001180
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001181 if (surface == NULL)
1182 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001183
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001184 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001185 case SHELL_SURFACE_PANEL:
1186 case SHELL_SURFACE_BACKGROUND:
1187 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001188 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001189 return;
1190 default:
1191 break;
1192 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001193
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001194 weston_surface_move(surface, (struct weston_input_device *) device);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001195}
1196
1197static void
1198resize_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001199 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001200{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001201 struct weston_surface *surface =
1202 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001203 uint32_t edges = 0;
1204 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001205 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001206
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001207 if (surface == NULL)
1208 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001209
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001210 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001211 if (!shsurf)
1212 return;
1213
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001214 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001215 case SHELL_SURFACE_PANEL:
1216 case SHELL_SURFACE_BACKGROUND:
1217 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001218 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001219 return;
1220 default:
1221 break;
1222 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -04001223
Pekka Paalanen5c97ae72012-01-30 16:19:47 +02001224 weston_surface_from_global(surface,
1225 device->grab_x, device->grab_y, &x, &y);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001226
Pekka Paalanen60921e52012-01-25 15:55:43 +02001227 if (x < surface->geometry.width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001228 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001229 else if (x < 2 * surface->geometry.width / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001230 edges |= 0;
1231 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001232 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001233
Pekka Paalanen60921e52012-01-25 15:55:43 +02001234 if (y < surface->geometry.height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001235 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001236 else if (y < 2 * surface->geometry.height / 3)
Kristian Høgsberg07937562011-04-12 17:25:42 -04001237 edges |= 0;
1238 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001239 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001240
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001241 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001242 edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001243}
1244
1245static void
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001246surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1247 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1248{
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001249 uint32_t step = 15;
Scott Moreaua3aa9c92012-03-22 11:01:03 -06001250 struct shell_surface *shsurf;
1251 struct weston_surface *surface =
1252 (struct weston_surface *) device->pointer_focus;
1253
1254 if (surface == NULL)
1255 return;
1256
1257 shsurf = get_shell_surface(surface);
1258 if (!shsurf)
1259 return;
1260
1261 switch (shsurf->type) {
1262 case SHELL_SURFACE_BACKGROUND:
1263 case SHELL_SURFACE_SCREENSAVER:
1264 return;
1265 default:
1266 break;
1267 }
1268
1269 surface->alpha += value * step;
1270
1271 if (surface->alpha > 255)
1272 surface->alpha = 255;
1273 if (surface->alpha < step)
1274 surface->alpha = step;
1275
1276 surface->geometry.dirty = 1;
1277 weston_surface_damage(surface);
1278}
1279
1280static void
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001281zoom_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau1b45a792012-03-22 10:58:23 -06001282 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
Scott Moreauccbf29d2012-02-22 14:21:41 -07001283{
1284 struct weston_input_device *wd = (struct weston_input_device *) device;
1285 struct weston_compositor *compositor = wd->compositor;
1286 struct weston_output *output;
1287
1288 wl_list_for_each(output, &compositor->output_list, link) {
1289 if (pixman_region32_contains_point(&output->region,
1290 device->x, device->y, NULL)) {
Scott Moreau1b45a792012-03-22 10:58:23 -06001291 output->zoom.active = 1;
1292 output->zoom.level += output->zoom.increment * -value;
Scott Moreauc6d7f602012-02-23 22:28:37 -07001293
1294 if (output->zoom.level >= 1.0) {
1295 output->zoom.active = 0;
1296 output->zoom.level = 1.0;
1297 }
Scott Moreauccbf29d2012-02-22 14:21:41 -07001298
1299 if (output->zoom.level < output->zoom.increment)
1300 output->zoom.level = output->zoom.increment;
1301
1302 weston_output_update_zoom(output, device->x, device->y);
1303 }
1304 }
1305}
1306
Scott Moreauccbf29d2012-02-22 14:21:41 -07001307static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001308terminate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001309 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001310{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001311 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001312
1313 if (state)
1314 wl_display_terminate(compositor->wl_display);
1315}
1316
1317static void
Scott Moreau447013d2012-02-18 05:05:29 -07001318rotate_grab_motion(struct wl_pointer_grab *grab,
Pekka Paalanen460099f2012-01-20 16:48:25 +02001319 uint32_t time, int32_t x, int32_t y)
1320{
1321 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001322 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001323 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001324 struct shell_surface *shsurf = rotate->base.shsurf;
1325 struct weston_surface *surface;
1326 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1327
1328 if (!shsurf)
1329 return;
1330
1331 surface = shsurf->surface;
1332
1333 cx = 0.5f * surface->geometry.width;
1334 cy = 0.5f * surface->geometry.height;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001335
1336 dx = device->x - rotate->center.x;
1337 dy = device->y - rotate->center.y;
1338 r = sqrtf(dx * dx + dy * dy);
1339
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001340 wl_list_remove(&shsurf->rotation.transform.link);
1341 shsurf->surface->geometry.dirty = 1;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001342
1343 if (r > 20.0f) {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001344 struct weston_matrix *matrix =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001345 &shsurf->rotation.transform.matrix;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001346
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001347 weston_matrix_init(&rotate->rotation);
1348 rotate->rotation.d[0] = dx / r;
1349 rotate->rotation.d[4] = -dy / r;
1350 rotate->rotation.d[1] = -rotate->rotation.d[4];
1351 rotate->rotation.d[5] = rotate->rotation.d[0];
Pekka Paalanen460099f2012-01-20 16:48:25 +02001352
1353 weston_matrix_init(matrix);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001354 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001355 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001356 weston_matrix_multiply(matrix, &rotate->rotation);
Pekka Paalanen7b3bd3d2012-01-30 14:16:34 +02001357 weston_matrix_translate(matrix, cx, cy, 0.0f);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001358
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +02001359 wl_list_insert(
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001360 &shsurf->surface->geometry.transformation_list,
1361 &shsurf->rotation.transform.link);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001362 } else {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001363 wl_list_init(&shsurf->rotation.transform.link);
1364 weston_matrix_init(&shsurf->rotation.rotation);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001365 weston_matrix_init(&rotate->rotation);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001366 }
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001367
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001368 /* We need to adjust the position of the surface
1369 * in case it was resized in a rotated state before */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001370 cposx = surface->geometry.x + cx;
1371 cposy = surface->geometry.y + cy;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001372 dposx = rotate->center.x - cposx;
1373 dposy = rotate->center.y - cposy;
1374 if (dposx != 0.0f || dposy != 0.0f) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001375 weston_surface_set_position(surface,
1376 surface->geometry.x + dposx,
1377 surface->geometry.y + dposy);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001378 }
1379
Pekka Paalanenb45ac5e2012-02-09 15:58:44 +02001380 /* Repaint implies weston_surface_update_transform(), which
1381 * lazily applies the damage due to rotation update.
1382 */
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001383 weston_compositor_schedule_repaint(shsurf->surface->compositor);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001384}
1385
1386static void
Scott Moreau447013d2012-02-18 05:05:29 -07001387rotate_grab_button(struct wl_pointer_grab *grab,
Kristian Høgsberged92f792012-03-30 11:31:25 -04001388 uint32_t time, uint32_t button, int32_t state)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001389{
1390 struct rotate_grab *rotate =
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001391 container_of(grab, struct rotate_grab, base.grab);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001392 struct wl_input_device *device = grab->input_device;
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001393 struct shell_surface *shsurf = rotate->base.shsurf;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001394
1395 if (device->button_count == 0 && state == 0) {
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001396 if (shsurf)
1397 weston_matrix_multiply(&shsurf->rotation.rotation,
1398 &rotate->rotation);
1399 shell_grab_finish(&rotate->base);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001400 wl_input_device_end_pointer_grab(device);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001401 free(rotate);
1402 }
1403}
1404
Scott Moreau447013d2012-02-18 05:05:29 -07001405static const struct wl_pointer_grab_interface rotate_grab_interface = {
Pekka Paalanen460099f2012-01-20 16:48:25 +02001406 noop_grab_focus,
1407 rotate_grab_motion,
1408 rotate_grab_button,
1409};
1410
1411static void
1412rotate_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001413 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Pekka Paalanen460099f2012-01-20 16:48:25 +02001414{
1415 struct weston_surface *base_surface =
1416 (struct weston_surface *) device->pointer_focus;
1417 struct shell_surface *surface;
1418 struct rotate_grab *rotate;
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001419 GLfloat dx, dy;
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001420 GLfloat r;
Pekka Paalanen460099f2012-01-20 16:48:25 +02001421
1422 if (base_surface == NULL)
1423 return;
1424
1425 surface = get_shell_surface(base_surface);
1426 if (!surface)
1427 return;
1428
1429 switch (surface->type) {
1430 case SHELL_SURFACE_PANEL:
1431 case SHELL_SURFACE_BACKGROUND:
1432 case SHELL_SURFACE_FULLSCREEN:
1433 case SHELL_SURFACE_SCREENSAVER:
1434 return;
1435 default:
1436 break;
1437 }
1438
Pekka Paalanen460099f2012-01-20 16:48:25 +02001439 rotate = malloc(sizeof *rotate);
1440 if (!rotate)
1441 return;
1442
Ander Conselvan de Oliveirafe0444a2012-04-04 17:48:05 +03001443 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001444
1445 weston_surface_to_global(surface->surface,
Pekka Paalanen60921e52012-01-25 15:55:43 +02001446 surface->surface->geometry.width / 2,
1447 surface->surface->geometry.height / 2,
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001448 &rotate->center.x, &rotate->center.y);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001449
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001450 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001451
1452 dx = device->x - rotate->center.x;
1453 dy = device->y - rotate->center.y;
1454 r = sqrtf(dx * dx + dy * dy);
1455 if (r > 20.0f) {
1456 struct weston_matrix inverse;
1457
1458 weston_matrix_init(&inverse);
1459 inverse.d[0] = dx / r;
1460 inverse.d[4] = dy / r;
1461 inverse.d[1] = -inverse.d[4];
1462 inverse.d[5] = inverse.d[0];
1463 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
Rafal Mielniczuk2d7ab822012-03-22 22:22:04 +01001464
1465 weston_matrix_init(&rotate->rotation);
1466 rotate->rotation.d[0] = dx / r;
1467 rotate->rotation.d[4] = -dy / r;
1468 rotate->rotation.d[1] = -rotate->rotation.d[4];
1469 rotate->rotation.d[5] = rotate->rotation.d[0];
Kristian Høgsberg765e27b2012-01-27 13:36:13 -05001470 } else {
1471 weston_matrix_init(&surface->rotation.rotation);
1472 weston_matrix_init(&rotate->rotation);
1473 }
1474
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001475 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001476}
1477
1478static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001479activate(struct wl_shell *shell, struct weston_surface *es,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001480 struct weston_input_device *device)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001481{
Alex Wu21858432012-04-01 20:13:08 +08001482 struct weston_surface *surf, *prev;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001483
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001484 weston_surface_activate(es, device);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001485
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001486 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001487 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001488 case SHELL_SURFACE_PANEL:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001489 case SHELL_SURFACE_LOCK:
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001490 break;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001491
Pekka Paalanen77346a62011-11-30 16:26:35 +02001492 case SHELL_SURFACE_SCREENSAVER:
1493 /* always below lock surface */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001494 if (shell->lock_surface)
1495 weston_surface_restack(es,
1496 &shell->lock_surface->surface->layer_link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001497 break;
Alex Wu4539b082012-03-01 12:57:46 +08001498 case SHELL_SURFACE_FULLSCREEN:
1499 /* should on top of panels */
Alex Wu21858432012-04-01 20:13:08 +08001500 shell_stack_fullscreen(get_shell_surface(es));
Alex Wu4539b082012-03-01 12:57:46 +08001501 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001502 default:
Alex Wu21858432012-04-01 20:13:08 +08001503 /* move the fullscreen surfaces down into the toplevel layer */
1504 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1505 wl_list_for_each_reverse_safe(surf,
1506 prev,
1507 &shell->fullscreen_layer.surface_list,
1508 layer_link)
1509 weston_surface_restack(surf,
1510 &shell->toplevel_layer.surface_list);
1511 }
1512
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001513 weston_surface_restack(es,
1514 &shell->toplevel_layer.surface_list);
1515 break;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001516 }
1517}
1518
Alex Wu21858432012-04-01 20:13:08 +08001519/* no-op func for checking black surface */
1520static void
1521black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1522{
1523}
1524
1525static bool
1526is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1527{
1528 if (es->configure == black_surface_configure) {
1529 if (fs_surface)
1530 *fs_surface = (struct weston_surface *)es->private;
1531 return true;
1532 }
1533 return false;
1534}
1535
Kristian Høgsberg75840622011-09-06 13:48:16 -04001536static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001537click_to_activate_binding(struct wl_input_device *device,
Alex Wu4539b082012-03-01 12:57:46 +08001538 uint32_t time, uint32_t key,
Scott Moreau6a3633d2012-03-20 08:47:59 -06001539 uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001540{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001541 struct weston_input_device *wd = (struct weston_input_device *) device;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001542 struct wl_shell *shell = data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543 struct weston_surface *focus;
Alex Wu4539b082012-03-01 12:57:46 +08001544 struct weston_surface *upper;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001545
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 focus = (struct weston_surface *) device->pointer_focus;
Alex Wu9c35e6b2012-03-05 14:13:13 +08001547 if (!focus)
1548 return;
1549
Alex Wu21858432012-04-01 20:13:08 +08001550 if (is_black_surface(focus, &upper))
Alex Wu4539b082012-03-01 12:57:46 +08001551 focus = upper;
Alex Wu4539b082012-03-01 12:57:46 +08001552
Alex Wu9c35e6b2012-03-05 14:13:13 +08001553 if (state && device->pointer_grab == &device->default_pointer_grab)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001554 activate(shell, focus, wd);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001555}
1556
1557static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001558lock(struct wl_listener *listener, void *data)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001559{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001560 struct wl_shell *shell =
1561 container_of(listener, struct wl_shell, lock_listener);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001562 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001563 struct shell_surface *shsurf;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001564 struct weston_output *output;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001565
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001566 if (shell->locked) {
1567 wl_list_for_each(output, &shell->compositor->output_list, link)
1568 /* TODO: find a way to jump to other DPMS levels */
1569 if (output->set_dpms)
1570 output->set_dpms(output, WESTON_DPMS_STANDBY);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001571 return;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001572 }
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001573
1574 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001575
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001576 /* Hide all surfaces by removing the fullscreen, panel and
1577 * toplevel layers. This way nothing else can show or receive
1578 * input events while we are locked. */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001579
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001580 wl_list_remove(&shell->panel_layer.link);
1581 wl_list_remove(&shell->toplevel_layer.link);
1582 wl_list_remove(&shell->fullscreen_layer.link);
1583 wl_list_insert(&shell->compositor->cursor_layer.link,
1584 &shell->lock_layer.link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001585
Pekka Paalanen77346a62011-11-30 16:26:35 +02001586 launch_screensaver(shell);
1587
1588 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1589 show_screensaver(shell, shsurf);
1590
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001591 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001592 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001593 weston_compositor_wake(shell->compositor);
1594 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001595 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001596
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001597 /* reset pointer foci */
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001598 weston_compositor_schedule_repaint(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001599
1600 /* reset keyboard foci */
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001601 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1602 wl_input_device_set_keyboard_focus(&device->input_device,
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001603 NULL);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001604 }
1605
1606 /* TODO: disable bindings that should not work while locked. */
1607
1608 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001609}
1610
1611static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001612unlock(struct wl_listener *listener, void *data)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001613{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001614 struct wl_shell *shell =
1615 container_of(listener, struct wl_shell, unlock_listener);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001616
Pekka Paalanend81c2162011-11-16 13:47:34 +02001617 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001618 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001619 return;
1620 }
1621
1622 /* If desktop-shell client has gone away, unlock immediately. */
1623 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001624 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001625 return;
1626 }
1627
1628 if (shell->prepare_event_sent)
1629 return;
1630
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001631 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001632 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001633}
1634
1635static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001636center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001637{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001638 struct weston_mode *mode = output->current;
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001639 GLfloat x = (mode->width - surface->geometry.width) / 2;
1640 GLfloat y = (mode->height - surface->geometry.height) / 2;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001641
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001642 weston_surface_set_position(surface, output->x + x, output->y + y);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001643}
1644
1645static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001646map(struct wl_shell *shell, struct weston_surface *surface,
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001647 int32_t width, int32_t height, int32_t sx, int32_t sy)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001648{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001649 struct weston_compositor *compositor = shell->compositor;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001650 struct shell_surface *shsurf;
1651 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001652 struct weston_surface *parent;
Juan Zhao96879df2012-02-07 08:45:41 +08001653 int panel_height = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001654
Pekka Paalanen77346a62011-11-30 16:26:35 +02001655 shsurf = get_shell_surface(surface);
1656 if (shsurf)
1657 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001658
Pekka Paalanen60921e52012-01-25 15:55:43 +02001659 surface->geometry.width = width;
1660 surface->geometry.height = height;
1661 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001662
1663 /* initial positioning, see also configure() */
1664 switch (surface_type) {
1665 case SHELL_SURFACE_TOPLEVEL:
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001666 weston_surface_set_position(surface, 10 + random() % 400,
1667 10 + random() % 400);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001668 break;
1669 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001670 center_on_output(surface, shsurf->fullscreen_output);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001671 break;
Alex Wu4539b082012-03-01 12:57:46 +08001672 case SHELL_SURFACE_FULLSCREEN:
1673 shell_map_fullscreen(shsurf);
1674 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001675 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001676 /* use surface configure to set the geometry */
Juan Zhao96879df2012-02-07 08:45:41 +08001677 panel_height = get_output_panel_height(shell,surface->output);
1678 weston_surface_set_position(surface, surface->output->x,
1679 surface->output->y + panel_height);
1680 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001681 case SHELL_SURFACE_LOCK:
1682 center_on_output(surface, get_default_output(compositor));
1683 break;
Tiago Vignatti0f997012012-02-10 16:17:23 +02001684 case SHELL_SURFACE_POPUP:
Kristian Høgsberg3730f362012-04-13 12:40:07 -04001685 shell_map_popup(shsurf);
Ander Conselvan de Oliveirae9e05152012-02-15 17:02:56 +02001686 case SHELL_SURFACE_NONE:
1687 weston_surface_set_position(surface,
1688 surface->geometry.x + sx,
1689 surface->geometry.y + sy);
Tiago Vignatti0f997012012-02-10 16:17:23 +02001690 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001691 default:
1692 ;
1693 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001694
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001695 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001696 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001697 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001698 /* background always visible, at the bottom */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001699 wl_list_insert(&shell->background_layer.surface_list,
1700 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001701 break;
1702 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001703 /* panel always on top, hidden while locked */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704 wl_list_insert(&shell->panel_layer.surface_list,
1705 &surface->layer_link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001706 break;
1707 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001708 /* lock surface always visible, on top */
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001709 wl_list_insert(&shell->lock_layer.surface_list,
1710 &surface->layer_link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001711 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001712 break;
1713 case SHELL_SURFACE_SCREENSAVER:
1714 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001715 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001716 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001717 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001718 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001719 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001720 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001721 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001722 break;
Kristian Høgsberg60c49542012-03-05 20:51:34 -05001723 case SHELL_SURFACE_POPUP:
1724 case SHELL_SURFACE_TRANSIENT:
1725 parent = shsurf->parent->surface;
1726 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1727 break;
Alex Wu4539b082012-03-01 12:57:46 +08001728 case SHELL_SURFACE_FULLSCREEN:
Ander Conselvan de Oliveiraa1ff53b2012-02-15 17:02:54 +02001729 case SHELL_SURFACE_NONE:
1730 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001731 default:
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001732 wl_list_insert(&shell->toplevel_layer.surface_list,
1733 &surface->layer_link);
1734 break;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001735 }
1736
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001737 if (surface_type != SHELL_SURFACE_NONE) {
1738 weston_surface_assign_output(surface);
Ander Conselvan de Oliveirade56c312012-03-05 15:39:23 +02001739 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1740 surface->output = shsurf->output;
1741 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001742
Juan Zhao7bb92f02011-12-15 11:31:51 -05001743 switch (surface_type) {
1744 case SHELL_SURFACE_TOPLEVEL:
1745 case SHELL_SURFACE_TRANSIENT:
1746 case SHELL_SURFACE_FULLSCREEN:
Juan Zhao96879df2012-02-07 08:45:41 +08001747 case SHELL_SURFACE_MAXIMIZED:
Juan Zhao7bb92f02011-12-15 11:31:51 -05001748 if (!shell->locked)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001749 activate(shell, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001750 (struct weston_input_device *)
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001751 compositor->input_device);
Juan Zhao7bb92f02011-12-15 11:31:51 -05001752 break;
1753 default:
1754 break;
1755 }
1756
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001757 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001758 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001759}
1760
1761static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001762configure(struct wl_shell *shell, struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001763 GLfloat x, GLfloat y, int32_t width, int32_t height)
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001764{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001765 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
Alex Wu4539b082012-03-01 12:57:46 +08001766 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001767 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001768
Pekka Paalanen77346a62011-11-30 16:26:35 +02001769 shsurf = get_shell_surface(surface);
1770 if (shsurf)
1771 surface_type = shsurf->type;
1772
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001773 surface->geometry.x = x;
1774 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +02001775 surface->geometry.width = width;
1776 surface->geometry.height = height;
1777 surface->geometry.dirty = 1;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001778
1779 switch (surface_type) {
1780 case SHELL_SURFACE_SCREENSAVER:
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001781 center_on_output(surface, shsurf->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001782 break;
Alex Wu4539b082012-03-01 12:57:46 +08001783 case SHELL_SURFACE_FULLSCREEN:
1784 shell_configure_fullscreen(shsurf);
1785 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1786 shell_stack_fullscreen(shsurf);
1787 break;
Juan Zhao96879df2012-02-07 08:45:41 +08001788 case SHELL_SURFACE_MAXIMIZED:
Alex Wu4539b082012-03-01 12:57:46 +08001789 /* setting x, y and using configure to change that geometry */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001790 surface->geometry.x = surface->output->x;
1791 surface->geometry.y = surface->output->y +
1792 get_output_panel_height(shell,surface->output);
Juan Zhao96879df2012-02-07 08:45:41 +08001793 break;
Alex Wu4539b082012-03-01 12:57:46 +08001794 case SHELL_SURFACE_TOPLEVEL:
1795 break;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001796 default:
1797 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001798 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001799
Alex Wu4539b082012-03-01 12:57:46 +08001800 /* XXX: would a fullscreen surface need the same handling? */
Kristian Høgsberg6a8b5532012-02-16 23:43:59 -05001801 if (surface->output) {
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001802 weston_surface_assign_output(surface);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001803
1804 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1805 surface->output = shsurf->output;
Juan Zhao96879df2012-02-07 08:45:41 +08001806 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1807 surface->output = shsurf->output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001808 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001809}
1810
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001811static void
1812shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1813{
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001814 struct shell_surface *shsurf = get_shell_surface(es);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001815 struct wl_shell *shell = shsurf->shell;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001816
1817 if (!weston_surface_is_mapped(es)) {
1818 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001819 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001820 es->geometry.width != es->buffer->width ||
1821 es->geometry.height != es->buffer->height) {
1822 GLfloat from_x, from_y;
1823 GLfloat to_x, to_y;
1824
1825 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1826 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1827 configure(shell, es,
1828 es->geometry.x + to_x - from_x,
1829 es->geometry.y + to_y - from_y,
1830 es->buffer->width, es->buffer->height);
Ander Conselvan de Oliveira7fb9f952012-03-27 17:36:42 +03001831 shsurf->force_configure = 0;
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001832 }
1833}
1834
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001835static int launch_desktop_shell_process(struct wl_shell *shell);
1836
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001837static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001838desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001839{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001840 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001841 struct wl_shell *shell =
1842 container_of(process, struct wl_shell, child.process);
1843
1844 shell->child.process.pid = 0;
1845 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001846
1847 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1848 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001849 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001850 shell->child.deathstamp = time;
1851 shell->child.deathcount = 0;
1852 }
1853
1854 shell->child.deathcount++;
1855 if (shell->child.deathcount > 5) {
1856 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1857 return;
1858 }
1859
1860 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1861 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001862}
1863
1864static int
1865launch_desktop_shell_process(struct wl_shell *shell)
1866{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001867 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001868
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001869 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001870 &shell->child.process,
1871 shell_exe,
1872 desktop_shell_sigchld);
1873
1874 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001875 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001876 return 0;
1877}
1878
1879static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001880bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1881{
1882 struct wl_shell *shell = data;
1883
1884 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001885 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001886}
1887
Kristian Høgsberg75840622011-09-06 13:48:16 -04001888static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001889unbind_desktop_shell(struct wl_resource *resource)
1890{
1891 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001892
1893 if (shell->locked)
1894 resume_desktop(shell);
1895
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001896 shell->child.desktop_shell = NULL;
1897 shell->prepare_event_sent = false;
1898 free(resource);
1899}
1900
1901static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001902bind_desktop_shell(struct wl_client *client,
1903 void *data, uint32_t version, uint32_t id)
1904{
1905 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001906 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001907
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001908 resource = wl_client_add_object(client, &desktop_shell_interface,
1909 &desktop_shell_implementation,
1910 id, shell);
1911
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001912 if (client == shell->child.client) {
1913 resource->destroy = unbind_desktop_shell;
1914 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001915 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001916 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001917
1918 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1919 "permission to bind desktop_shell denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001920 wl_resource_destroy(resource);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001921}
1922
Pekka Paalanen6e168112011-11-24 11:34:05 +02001923static void
1924screensaver_set_surface(struct wl_client *client,
1925 struct wl_resource *resource,
1926 struct wl_resource *shell_surface_resource,
1927 struct wl_resource *output_resource)
1928{
1929 struct wl_shell *shell = resource->data;
1930 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001931 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001932
Pekka Paalanen98262232011-12-01 10:42:22 +02001933 if (reset_shell_surface_type(surface))
1934 return;
1935
Pekka Paalanen77346a62011-11-30 16:26:35 +02001936 surface->type = SHELL_SURFACE_SCREENSAVER;
1937
Kristian Høgsberg1cbf3262012-02-17 23:49:07 -05001938 surface->fullscreen_output = output;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001939 surface->output = output;
1940 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001941}
1942
1943static const struct screensaver_interface screensaver_implementation = {
1944 screensaver_set_surface
1945};
1946
1947static void
1948unbind_screensaver(struct wl_resource *resource)
1949{
1950 struct wl_shell *shell = resource->data;
1951
Pekka Paalanen77346a62011-11-30 16:26:35 +02001952 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001953 free(resource);
1954}
1955
1956static void
1957bind_screensaver(struct wl_client *client,
1958 void *data, uint32_t version, uint32_t id)
1959{
1960 struct wl_shell *shell = data;
1961 struct wl_resource *resource;
1962
1963 resource = wl_client_add_object(client, &screensaver_interface,
1964 &screensaver_implementation,
1965 id, shell);
1966
Pekka Paalanen77346a62011-11-30 16:26:35 +02001967 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001968 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001969 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001970 return;
1971 }
1972
1973 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1974 "interface object already bound");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001975 wl_resource_destroy(resource);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001976}
1977
Kristian Høgsberg07045392012-02-19 18:52:44 -05001978struct switcher {
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001979 struct wl_shell *shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001980 struct weston_surface *current;
1981 struct wl_listener listener;
1982 struct wl_keyboard_grab grab;
1983};
1984
1985static void
1986switcher_next(struct switcher *switcher)
1987{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001988 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001989 struct weston_surface *surface;
1990 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
Kristian Høgsberg32e56862012-04-02 22:18:58 -04001991 struct shell_surface *shsurf;
Kristian Høgsberg07045392012-02-19 18:52:44 -05001992
1993 wl_list_for_each(surface, &compositor->surface_list, link) {
Kristian Høgsberg07045392012-02-19 18:52:44 -05001994 switch (get_shell_surface_type(surface)) {
1995 case SHELL_SURFACE_TOPLEVEL:
1996 case SHELL_SURFACE_FULLSCREEN:
1997 case SHELL_SURFACE_MAXIMIZED:
1998 if (first == NULL)
1999 first = surface;
2000 if (prev == switcher->current)
2001 next = surface;
2002 prev = surface;
2003 surface->alpha = 64;
Kristian Høgsbergcacb7cd2012-02-28 09:20:21 -05002004 surface->geometry.dirty = 1;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002005 weston_surface_damage(surface);
2006 break;
2007 default:
2008 break;
2009 }
Alex Wu1659daa2012-04-01 20:13:09 +08002010
2011 if (is_black_surface(surface, NULL)) {
2012 surface->alpha = 64;
2013 surface->geometry.dirty = 1;
2014 weston_surface_damage(surface);
2015 }
Kristian Høgsberg07045392012-02-19 18:52:44 -05002016 }
2017
2018 if (next == NULL)
2019 next = first;
2020
Alex Wu07b26062012-03-12 16:06:01 +08002021 if (next == NULL)
2022 return;
2023
Kristian Høgsberg07045392012-02-19 18:52:44 -05002024 wl_list_remove(&switcher->listener.link);
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002025 wl_signal_add(&next->surface.resource.destroy_signal,
2026 &switcher->listener);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002027
2028 switcher->current = next;
2029 next->alpha = 255;
Alex Wu1659daa2012-04-01 20:13:09 +08002030
Kristian Høgsberg32e56862012-04-02 22:18:58 -04002031 shsurf = get_shell_surface(switcher->current);
2032 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2033 shsurf->fullscreen.black_surface->alpha = 255;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002034}
2035
2036static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002037switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002038{
2039 struct switcher *switcher =
2040 container_of(listener, struct switcher, listener);
2041
2042 switcher_next(switcher);
2043}
2044
2045static void
2046switcher_destroy(struct switcher *switcher, uint32_t time)
2047{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002048 struct weston_compositor *compositor = switcher->shell->compositor;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002049 struct weston_surface *surface;
2050 struct weston_input_device *device =
2051 (struct weston_input_device *) switcher->grab.input_device;
2052
2053 wl_list_for_each(surface, &compositor->surface_list, link) {
2054 surface->alpha = 255;
2055 weston_surface_damage(surface);
2056 }
2057
Alex Wu07b26062012-03-12 16:06:01 +08002058 if (switcher->current)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002059 activate(switcher->shell, switcher->current, device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002060 wl_list_remove(&switcher->listener.link);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002061 wl_input_device_end_keyboard_grab(&device->input_device);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002062 free(switcher);
2063}
2064
2065static void
2066switcher_key(struct wl_keyboard_grab *grab,
2067 uint32_t time, uint32_t key, int32_t state)
2068{
2069 struct switcher *switcher = container_of(grab, struct switcher, grab);
2070 struct weston_input_device *device =
2071 (struct weston_input_device *) grab->input_device;
2072
2073 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2074 switcher_destroy(switcher, time);
2075 } else if (key == KEY_TAB && state) {
2076 switcher_next(switcher);
2077 }
2078};
2079
2080static const struct wl_keyboard_grab_interface switcher_grab = {
2081 switcher_key
2082};
2083
2084static void
2085switcher_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002086 uint32_t key, uint32_t button, uint32_t axis,
2087 int32_t state, void *data)
Kristian Høgsberg07045392012-02-19 18:52:44 -05002088{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002089 struct wl_shell *shell = data;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002090 struct switcher *switcher;
2091
2092 switcher = malloc(sizeof *switcher);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002093 switcher->shell = shell;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002094 switcher->current = NULL;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002095 switcher->listener.notify = switcher_handle_surface_destroy;
Kristian Høgsberg07045392012-02-19 18:52:44 -05002096 wl_list_init(&switcher->listener.link);
2097
2098 switcher->grab.interface = &switcher_grab;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002099 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2100 wl_input_device_set_keyboard_focus(device, NULL);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002101 switcher_next(switcher);
2102}
2103
Pekka Paalanen3c647232011-12-22 13:43:43 +02002104static void
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002105backlight_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002106 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002107{
2108 struct weston_compositor *compositor = data;
2109 struct weston_output *output;
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002110 long backlight_new = 0;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002111
2112 /* TODO: we're limiting to simple use cases, where we assume just
2113 * control on the primary display. We'd have to extend later if we
2114 * ever get support for setting backlights on random desktop LCD
2115 * panels though */
2116 output = get_default_output(compositor);
2117 if (!output)
2118 return;
2119
2120 if (!output->set_backlight)
2121 return;
2122
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002123 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2124 backlight_new = output->backlight_current - 25;
2125 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2126 backlight_new = output->backlight_current + 25;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002127
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002128 if (backlight_new < 5)
2129 backlight_new = 5;
2130 if (backlight_new > 255)
2131 backlight_new = 255;
2132
2133 output->backlight_current = backlight_new;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002134 output->set_backlight(output, output->backlight_current);
2135}
2136
2137static void
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002138debug_repaint_binding(struct wl_input_device *device, uint32_t time,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002139 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002140{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002141 struct wl_shell *shell = data;
2142 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsbergb41c0812012-03-04 14:53:40 -05002143 struct weston_surface *surface;
2144
2145 if (shell->debug_repaint_surface) {
2146 weston_surface_destroy(shell->debug_repaint_surface);
2147 shell->debug_repaint_surface = NULL;
2148 } else {
2149 surface = weston_surface_create(compositor);
2150 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2151 weston_surface_configure(surface, 0, 0, 8192, 8192);
2152 wl_list_insert(&compositor->fade_layer.surface_list,
2153 &surface->layer_link);
2154 weston_surface_assign_output(surface);
2155 pixman_region32_init(&surface->input);
2156
2157 /* Here's the dirty little trick that makes the
2158 * repaint debugging work: we force an
2159 * update_transform first to update dependent state
2160 * and clear the geometry.dirty bit. Then we clear
2161 * the surface damage so it only gets repainted
2162 * piecewise as we repaint other things. */
2163
2164 weston_surface_update_transform(surface);
2165 pixman_region32_fini(&surface->damage);
2166 pixman_region32_init(&surface->damage);
2167 shell->debug_repaint_surface = surface;
2168 }
2169}
2170
2171static void
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002172shell_destroy(struct wl_listener *listener, void *data)
Pekka Paalanen3c647232011-12-22 13:43:43 +02002173{
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002174 struct wl_shell *shell =
2175 container_of(listener, struct wl_shell, destroy_listener);
Pekka Paalanen3c647232011-12-22 13:43:43 +02002176
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02002177 if (shell->child.client)
2178 wl_client_destroy(shell->child.client);
2179
Pekka Paalanen3c647232011-12-22 13:43:43 +02002180 free(shell->screensaver.path);
2181 free(shell);
2182}
2183
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002184int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002185shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04002186
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002187WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002188shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002189{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002190 struct wl_shell *shell;
2191
2192 shell = malloc(sizeof *shell);
2193 if (shell == NULL)
2194 return -1;
2195
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04002196 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04002197 shell->compositor = ec;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002198
2199 shell->destroy_listener.notify = shell_destroy;
2200 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2201 shell->lock_listener.notify = lock;
2202 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2203 shell->unlock_listener.notify = unlock;
2204 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002205
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01002206 wl_list_init(&shell->backgrounds);
2207 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02002208 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02002209
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002210 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2211 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2212 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2213 weston_layer_init(&shell->background_layer,
2214 &shell->toplevel_layer.link);
2215 wl_list_init(&shell->lock_layer.surface_list);
2216
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05002217 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02002218
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002219 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2220 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002221 return -1;
2222
Kristian Høgsberg75840622011-09-06 13:48:16 -04002223 if (wl_display_add_global(ec->wl_display,
2224 &desktop_shell_interface,
2225 shell, bind_desktop_shell) == NULL)
2226 return -1;
2227
Pekka Paalanen6e168112011-11-24 11:34:05 +02002228 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2229 shell, bind_screensaver) == NULL)
2230 return -1;
2231
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05002232 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02002233 if (launch_desktop_shell_process(shell) != 0)
2234 return -1;
2235
Scott Moreau6a3633d2012-03-20 08:47:59 -06002236 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2237 move_binding, shell);
2238 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2239 resize_binding, shell);
2240 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2241 MODIFIER_CTRL | MODIFIER_ALT,
2242 terminate_binding, ec);
2243 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002244 click_to_activate_binding, shell);
Scott Moreau1b45a792012-03-22 10:58:23 -06002245 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreaua3aa9c92012-03-22 11:01:03 -06002246 MODIFIER_SUPER | MODIFIER_ALT,
2247 surface_opacity_binding, NULL);
2248 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
Scott Moreau1b45a792012-03-22 10:58:23 -06002249 MODIFIER_SUPER, zoom_binding, NULL);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002250 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Scott Moreau6a3633d2012-03-20 08:47:59 -06002251 MODIFIER_SUPER | MODIFIER_ALT,
2252 rotate_binding, NULL);
2253 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002254 switcher_binding, shell);
Kristian Høgsberg07045392012-02-19 18:52:44 -05002255
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002256 /* brightness */
Scott Moreau6a3633d2012-03-20 08:47:59 -06002257 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2258 backlight_binding, ec);
2259 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2260 backlight_binding, ec);
2261 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2262 backlight_binding, ec);
2263 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2264 backlight_binding, ec);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002265
Scott Moreau6a3633d2012-03-20 08:47:59 -06002266 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002267 debug_repaint_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04002268
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05002269 return 0;
2270}