blob: 49b0b1090a740ff4b4f265c1b6d1d0d2bf9a67d7 [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003 * Copyright © 2011 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;
43 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020044
45 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050046 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020049
50 unsigned deathcount;
51 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020052 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020053
54 bool locked;
55 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020056
Pekka Paalanen068ae942011-11-28 14:11:15 +020057 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050058 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020059 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010060
61 struct wl_list backgrounds;
62 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020063
Pekka Paalanen77346a62011-11-30 16:26:35 +020064 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020065 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020066 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020067 struct wl_resource *binding;
68 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040071};
72
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050073enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020074 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050075
Pekka Paalanen57da4a82011-11-23 16:42:16 +020076 SHELL_SURFACE_PANEL,
77 SHELL_SURFACE_BACKGROUND,
78 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020079 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050080
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050083 SHELL_SURFACE_FULLSCREEN,
84 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020085};
86
Pekka Paalanen56cdea92011-11-23 16:14:12 +020087struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020088 struct wl_resource resource;
89
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050090 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020091 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050092 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020093
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050094 enum shell_surface_type type;
95 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010096
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050097 struct {
Pekka Paalanen460099f2012-01-20 16:48:25 +020098 struct weston_transform transform;
99 } rotation;
100
101 struct {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500102 struct wl_grab grab;
103 uint32_t time;
104 int32_t x, y;
105 int32_t initial_up;
106 } popup;
107
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500108 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100109 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200110};
111
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500112struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500113 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500114 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500115 int32_t dx, dy;
116};
117
Pekka Paalanen460099f2012-01-20 16:48:25 +0200118struct rotate_grab {
119 struct wl_grab grab;
120 struct shell_surface *surface;
121 struct {
122 int32_t x;
123 int32_t y;
124 } center;
125};
126
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500127static void
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200128shell_configuration(struct wl_shell *shell)
129{
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200130 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200131 char *path = NULL;
132 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200133
134 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200135 { "path", CONFIG_KEY_STRING, &path },
136 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200137 };
138
139 struct config_section cs[] = {
140 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
141 };
142
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500143 config_file = config_file_path("weston-desktop-shell.ini");
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -0500144 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200145 free(config_file);
146
Pekka Paalanen7296e792011-12-07 16:22:00 +0200147 shell->screensaver.path = path;
148 shell->screensaver.duration = duration;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200149}
150
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200151static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500152noop_grab_focus(struct wl_grab *grab, uint32_t time,
153 struct wl_surface *surface, int32_t x, int32_t y)
154{
155 grab->focus = NULL;
156}
157
158static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500160 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500161{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500162 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500163 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500166 weston_surface_configure(es,
167 device->x + move->dx,
168 device->y + move->dy,
169 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500170}
171
172static void
173move_grab_button(struct wl_grab *grab,
174 uint32_t time, int32_t button, int32_t state)
175{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500176 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500178 if (device->button_count == 0 && state == 0) {
179 wl_input_device_end_grab(device, time);
180 free(grab);
181 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182}
183
184static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500185 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186 move_grab_motion,
187 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188};
189
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400190static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500192 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195
196 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400197 if (!move)
198 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199
200 move->grab.interface = &move_grab_interface;
201 move->dx = es->x - wd->input_device.grab_x;
202 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500203 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500205 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400207 wl_input_device_set_pointer_focus(&wd->input_device,
208 NULL, time, 0, 0, 0, 0);
209
210 return 0;
211}
212
213static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200214shell_surface_move(struct wl_client *client, struct wl_resource *resource,
215 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500217 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200218 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400219
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500220 if (wd->input_device.button_count == 0 ||
221 wd->input_device.grab_time != time ||
222 wd->input_device.pointer_focus != &shsurf->surface->surface)
223 return;
224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400226 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227}
228
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230 struct wl_grab grab;
231 uint32_t edges;
232 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200233 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234};
235
236static void
237resize_grab_motion(struct wl_grab *grab,
238 uint32_t time, int32_t x, int32_t y)
239{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500240 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500241 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242 int32_t width, height;
243
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200244 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500245 width = device->grab_x - device->x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200246 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500247 width = device->x - device->grab_x + resize->width;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248 } else {
249 width = resize->width;
250 }
251
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200252 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500253 height = device->grab_y - device->y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200254 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500255 height = device->y - device->grab_y + resize->height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256 } else {
257 height = resize->height;
258 }
259
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200260 wl_resource_post_event(&resize->shsurf->resource,
261 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
262 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263}
264
265static void
266resize_grab_button(struct wl_grab *grab,
267 uint32_t time, int32_t button, int32_t state)
268{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500269 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500271 if (device->button_count == 0 && state == 0) {
272 wl_input_device_end_grab(device, time);
273 free(grab);
274 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275}
276
277static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500278 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279 resize_grab_motion,
280 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500281};
282
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400283static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500284weston_surface_resize(struct shell_surface *shsurf,
285 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200286 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500288 struct weston_resize_grab *resize;
289 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500290
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500291 /* FIXME: Reject if fullscreen */
292
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294 if (!resize)
295 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500296
297 resize->grab.interface = &resize_grab_interface;
298 resize->edges = edges;
299 resize->dx = es->x - wd->input_device.grab_x;
300 resize->dy = es->y - wd->input_device.grab_y;
301 resize->width = es->width;
302 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200303 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400304
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500305 if (edges == 0 || edges > 15 ||
306 (edges & 3) == 3 || (edges & 12) == 12)
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200307 goto err_out;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500308
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500309 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500310
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400311 wl_input_device_set_pointer_focus(&wd->input_device,
312 NULL, time, 0, 0, 0, 0);
313
314 return 0;
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200315
316err_out:
317 free(resize);
318 return 0;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400319}
320
321static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200322shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
323 struct wl_resource *input_resource, uint32_t time,
324 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400325{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500326 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200327 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400328
329 /* FIXME: Reject if fullscreen */
330
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500331 if (wd->input_device.button_count == 0 ||
332 wd->input_device.grab_time != time ||
333 wd->input_device.pointer_focus != &shsurf->surface->surface)
334 return;
335
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500336 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400337 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500338}
339
Pekka Paalanen98262232011-12-01 10:42:22 +0200340static int
341reset_shell_surface_type(struct shell_surface *surface)
342{
343 switch (surface->type) {
344 case SHELL_SURFACE_FULLSCREEN:
345 surface->surface->x = surface->saved_x;
346 surface->surface->y = surface->saved_y;
347 surface->surface->fullscreen_output = NULL;
348 break;
349 case SHELL_SURFACE_PANEL:
350 case SHELL_SURFACE_BACKGROUND:
351 wl_list_remove(&surface->link);
352 wl_list_init(&surface->link);
353 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200354 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200355 case SHELL_SURFACE_LOCK:
356 wl_resource_post_error(&surface->resource,
357 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200358 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200359 return -1;
360 case SHELL_SURFACE_NONE:
361 case SHELL_SURFACE_TOPLEVEL:
362 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500363 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200364 break;
365 }
366
367 surface->type = SHELL_SURFACE_NONE;
368 return 0;
369}
370
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500371static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200372shell_surface_set_toplevel(struct wl_client *client,
373 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400374
375{
Pekka Paalanen98262232011-12-01 10:42:22 +0200376 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400377
Pekka Paalanen98262232011-12-01 10:42:22 +0200378 if (reset_shell_surface_type(surface))
379 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400380
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500381 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200382 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400383}
384
385static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200386shell_surface_set_transient(struct wl_client *client,
387 struct wl_resource *resource,
388 struct wl_resource *parent_resource,
389 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400390{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200391 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500392 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200393 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500394 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400395
Pekka Paalanen98262232011-12-01 10:42:22 +0200396 if (reset_shell_surface_type(shsurf))
397 return;
398
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400399 /* assign to parents output */
400 es->output = pes->output;
401
402 es->x = pes->x + x;
403 es->y = pes->y + y;
404
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500405 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200406 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400407}
408
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500409static struct weston_output *
410get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200411{
412 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500413 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200414}
415
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400416static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200417shell_surface_set_fullscreen(struct wl_client *client,
418 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400419
420{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200421 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500422 struct weston_surface *es = shsurf->surface;
423 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400424
Pekka Paalanen98262232011-12-01 10:42:22 +0200425 if (reset_shell_surface_type(shsurf))
426 return;
427
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400428 /* FIXME: Fullscreen on first output */
429 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200430 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400431 es->output = output;
432
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200433 shsurf->saved_x = es->x;
434 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400435 es->x = (output->current->width - es->width) / 2;
436 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400437 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500438 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200439 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400440}
441
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500442static void
443popup_grab_focus(struct wl_grab *grab, uint32_t time,
444 struct wl_surface *surface, int32_t x, int32_t y)
445{
446 struct wl_input_device *device = grab->input_device;
447 struct shell_surface *priv =
448 container_of(grab, struct shell_surface, popup.grab);
449 struct wl_client *client = priv->surface->surface.resource.client;
450
Pekka Paalanencb108432012-01-19 16:25:40 +0200451 if (surface && surface->resource.client == client) {
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500452 wl_input_device_set_pointer_focus(device, surface, time,
453 device->x, device->y, x, y);
454 grab->focus = surface;
455 } else {
456 wl_input_device_set_pointer_focus(device, NULL,
457 time, 0, 0, 0, 0);
458 grab->focus = NULL;
459 }
460}
461
462static void
463popup_grab_motion(struct wl_grab *grab,
464 uint32_t time, int32_t x, int32_t y)
465{
466 struct wl_input_device *device = grab->input_device;
467 struct wl_resource *resource;
468
469 resource = grab->input_device->pointer_focus_resource;
470 if (resource)
471 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
472 time, device->x, device->y, x, y);
473}
474
475static void
476popup_grab_button(struct wl_grab *grab,
477 uint32_t time, int32_t button, int32_t state)
478{
479 struct wl_resource *resource;
480 struct shell_surface *shsurf =
481 container_of(grab, struct shell_surface, popup.grab);
482
483 resource = grab->input_device->pointer_focus_resource;
484 if (resource) {
485 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
486 time, button, state);
487 } else if (state == 0 &&
488 (shsurf->popup.initial_up ||
489 time - shsurf->popup.time > 500)) {
490 wl_resource_post_event(&shsurf->resource,
491 WL_SHELL_SURFACE_POPUP_DONE);
492 wl_input_device_end_grab(grab->input_device, time);
493 shsurf->popup.grab.input_device = NULL;
494 }
495
496 if (state == 0)
497 shsurf->popup.initial_up = 1;
498}
499
500static const struct wl_grab_interface popup_grab_interface = {
501 popup_grab_focus,
502 popup_grab_motion,
503 popup_grab_button,
504};
505
506static void
507shell_map_popup(struct shell_surface *shsurf, uint32_t time)
508{
509 struct wl_input_device *device;
510 struct weston_surface *es = shsurf->surface;
511 struct weston_surface *parent = shsurf->parent->surface;
512
513 es->output = parent->output;
514
515 shsurf->popup.grab.interface = &popup_grab_interface;
516 device = es->compositor->input_device;
517
518 es->x = shsurf->parent->surface->x + shsurf->popup.x;
519 es->y = shsurf->parent->surface->y + shsurf->popup.y;
520
521 shsurf->popup.grab.input_device = device;
522 shsurf->popup.time = device->grab_time;
523 shsurf->popup.initial_up = 0;
524
525 wl_input_device_start_grab(shsurf->popup.grab.input_device,
526 &shsurf->popup.grab, shsurf->popup.time);
527}
528
529static void
530shell_surface_set_popup(struct wl_client *client,
531 struct wl_resource *resource,
532 struct wl_resource *input_device_resource,
533 uint32_t time,
534 struct wl_resource *parent_resource,
535 int32_t x, int32_t y, uint32_t flags)
536{
537 struct shell_surface *shsurf = resource->data;
538 struct weston_surface *es = shsurf->surface;
539
540 weston_surface_damage(es);
541 shsurf->type = SHELL_SURFACE_POPUP;
542 shsurf->parent = parent_resource->data;
543 shsurf->popup.x = x;
544 shsurf->popup.y = y;
545}
546
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200547static const struct wl_shell_surface_interface shell_surface_implementation = {
548 shell_surface_move,
549 shell_surface_resize,
550 shell_surface_set_toplevel,
551 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500552 shell_surface_set_fullscreen,
553 shell_surface_set_popup
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200554};
555
556static void
557destroy_shell_surface(struct wl_resource *resource)
558{
559 struct shell_surface *shsurf = resource->data;
560
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500561 if (shsurf->popup.grab.input_device)
562 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
563
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200564 /* in case cleaning up a dead client destroys shell_surface first */
565 if (shsurf->surface)
566 wl_list_remove(&shsurf->surface_destroy_listener.link);
567
568 wl_list_remove(&shsurf->link);
569 free(shsurf);
570}
571
572static void
573shell_handle_surface_destroy(struct wl_listener *listener,
574 struct wl_resource *resource, uint32_t time)
575{
576 struct shell_surface *shsurf = container_of(listener,
577 struct shell_surface,
578 surface_destroy_listener);
579
580 shsurf->surface = NULL;
581 wl_resource_destroy(&shsurf->resource, time);
582}
583
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200584static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500585get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200586{
587 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
588 struct wl_listener *listener;
589
590 /* search the destroy listener list for our callback */
591 wl_list_for_each(listener, lst, link) {
592 if (listener->func == shell_handle_surface_destroy) {
593 return container_of(listener, struct shell_surface,
594 surface_destroy_listener);
595 }
596 }
597
598 return NULL;
599}
600
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200601static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200602shell_get_shell_surface(struct wl_client *client,
603 struct wl_resource *resource,
604 uint32_t id,
605 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200606{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500607 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200608 struct shell_surface *shsurf;
609
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200610 if (get_shell_surface(surface)) {
611 wl_resource_post_error(surface_resource,
612 WL_DISPLAY_ERROR_INVALID_OBJECT,
613 "wl_shell::get_shell_surface already requested");
614 return;
615 }
616
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200617 shsurf = calloc(1, sizeof *shsurf);
618 if (!shsurf) {
619 wl_resource_post_no_memory(resource);
620 return;
621 }
622
623 shsurf->resource.destroy = destroy_shell_surface;
624 shsurf->resource.object.id = id;
625 shsurf->resource.object.interface = &wl_shell_surface_interface;
626 shsurf->resource.object.implementation =
627 (void (**)(void)) &shell_surface_implementation;
628 shsurf->resource.data = shsurf;
629
630 shsurf->surface = surface;
631 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
632 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
633 &shsurf->surface_destroy_listener.link);
634
635 /* init link so its safe to always remove it in destroy_shell_surface */
636 wl_list_init(&shsurf->link);
637
Pekka Paalanen460099f2012-01-20 16:48:25 +0200638 /* empty when not in use */
639 wl_list_init(&shsurf->rotation.transform.link);
640
Pekka Paalanen98262232011-12-01 10:42:22 +0200641 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200642
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200643 wl_client_add_resource(client, &shsurf->resource);
644}
645
646static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200647 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500648};
649
Kristian Høgsberg07937562011-04-12 17:25:42 -0400650static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500651handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200652{
653 proc->pid = 0;
654}
655
656static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200657launch_screensaver(struct wl_shell *shell)
658{
659 if (shell->screensaver.binding)
660 return;
661
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200662 if (!shell->screensaver.path)
663 return;
664
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500665 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200666 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200667 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200668 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200669}
670
671static void
672terminate_screensaver(struct wl_shell *shell)
673{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200674 if (shell->screensaver.process.pid == 0)
675 return;
676
677 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200678}
679
680static void
681show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
682{
683 struct wl_list *list;
684
685 if (shell->lock_surface)
686 list = &shell->lock_surface->surface->link;
687 else
688 list = &shell->compositor->surface_list;
689
690 wl_list_remove(&surface->surface->link);
691 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500692 weston_surface_configure(surface->surface,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200693 surface->surface->x,
694 surface->surface->y,
695 surface->surface->width,
696 surface->surface->height);
697 surface->surface->output = surface->output;
698}
699
700static void
701hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
702{
703 wl_list_remove(&surface->surface->link);
704 wl_list_init(&surface->surface->link);
705 surface->surface->output = NULL;
706}
707
708static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400709desktop_shell_set_background(struct wl_client *client,
710 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100711 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400712 struct wl_resource *surface_resource)
713{
714 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200715 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500716 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200717 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400718
Pekka Paalanen98262232011-12-01 10:42:22 +0200719 if (reset_shell_surface_type(shsurf))
720 return;
721
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100722 wl_list_for_each(priv, &shell->backgrounds, link) {
723 if (priv->output == output_resource->data) {
724 priv->surface->output = NULL;
725 wl_list_remove(&priv->surface->link);
726 wl_list_remove(&priv->link);
727 break;
728 }
729 }
730
Pekka Paalanen068ae942011-11-28 14:11:15 +0200731 shsurf->type = SHELL_SURFACE_BACKGROUND;
732 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100733
Pekka Paalanen068ae942011-11-28 14:11:15 +0200734 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100735
Pekka Paalanen068ae942011-11-28 14:11:15 +0200736 surface->x = shsurf->output->x;
737 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200738
Kristian Høgsberg75840622011-09-06 13:48:16 -0400739 wl_resource_post_event(resource,
740 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500741 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200742 shsurf->output->current->width,
743 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400744}
745
746static void
747desktop_shell_set_panel(struct wl_client *client,
748 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100749 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400750 struct wl_resource *surface_resource)
751{
752 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200753 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500754 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200755 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400756
Pekka Paalanen98262232011-12-01 10:42:22 +0200757 if (reset_shell_surface_type(shsurf))
758 return;
759
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100760 wl_list_for_each(priv, &shell->panels, link) {
761 if (priv->output == output_resource->data) {
762 priv->surface->output = NULL;
763 wl_list_remove(&priv->surface->link);
764 wl_list_remove(&priv->link);
765 break;
766 }
767 }
768
Pekka Paalanen068ae942011-11-28 14:11:15 +0200769 shsurf->type = SHELL_SURFACE_PANEL;
770 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100771
Pekka Paalanen068ae942011-11-28 14:11:15 +0200772 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100773
Pekka Paalanen068ae942011-11-28 14:11:15 +0200774 surface->x = shsurf->output->x;
775 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200776
Kristian Høgsberg75840622011-09-06 13:48:16 -0400777 wl_resource_post_event(resource,
778 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500779 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200780 shsurf->output->current->width,
781 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400782}
783
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200784static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500785handle_lock_surface_destroy(struct wl_listener *listener,
786 struct wl_resource *resource, uint32_t time)
787{
788 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200789 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500790
791 fprintf(stderr, "lock surface gone\n");
792 shell->lock_surface = NULL;
793}
794
795static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200796desktop_shell_set_lock_surface(struct wl_client *client,
797 struct wl_resource *resource,
798 struct wl_resource *surface_resource)
799{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200800 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200801 struct shell_surface *surface = surface_resource->data;
802
803 if (reset_shell_surface_type(surface))
804 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200805
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200806 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200807
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200808 if (!shell->locked)
809 return;
810
Pekka Paalanen98262232011-12-01 10:42:22 +0200811 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200812
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500813 shell->lock_surface_listener.func = handle_lock_surface_destroy;
814 wl_list_insert(&surface_resource->destroy_listener_list,
815 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200816
Pekka Paalanen068ae942011-11-28 14:11:15 +0200817 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200818}
819
820static void
821resume_desktop(struct wl_shell *shell)
822{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500823 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200824 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200825 struct shell_surface *tmp;
826
827 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
828 hide_screensaver(shell, tmp);
829
830 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200831
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500832 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500833 weston_surface_configure(surface, surface->x, surface->y,
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500834 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200835
Pekka Paalanenfe340832011-11-25 16:07:52 +0200836 if (wl_list_empty(&shell->backgrounds)) {
837 list = &shell->compositor->surface_list;
838 } else {
839 struct shell_surface *background;
840 background = container_of(shell->backgrounds.prev,
841 struct shell_surface, link);
842 list = background->surface->link.prev;
843 }
844
845 if (!wl_list_empty(&shell->hidden_surface_list))
846 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500847 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200848
849 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500850 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200851 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500852 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200853}
854
855static void
856desktop_shell_unlock(struct wl_client *client,
857 struct wl_resource *resource)
858{
859 struct wl_shell *shell = resource->data;
860
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200861 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200862
863 if (shell->locked)
864 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200865}
866
Kristian Høgsberg75840622011-09-06 13:48:16 -0400867static const struct desktop_shell_interface desktop_shell_implementation = {
868 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200869 desktop_shell_set_panel,
870 desktop_shell_set_lock_surface,
871 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400872};
873
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200874static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500875get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200876{
877 struct shell_surface *shsurf;
878
879 shsurf = get_shell_surface(surface);
880 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200881 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200882 return shsurf->type;
883}
884
Kristian Høgsberg75840622011-09-06 13:48:16 -0400885static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400886move_binding(struct wl_input_device *device, uint32_t time,
887 uint32_t key, uint32_t button, uint32_t state, void *data)
888{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500889 struct weston_surface *surface =
890 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500891
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100892 if (surface == NULL)
893 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400894
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200895 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100896 case SHELL_SURFACE_PANEL:
897 case SHELL_SURFACE_BACKGROUND:
898 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200899 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100900 return;
901 default:
902 break;
903 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400904
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500905 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400906}
907
908static void
909resize_binding(struct wl_input_device *device, uint32_t time,
910 uint32_t key, uint32_t button, uint32_t state, void *data)
911{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500912 struct weston_surface *surface =
913 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400914 uint32_t edges = 0;
915 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200916 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500917
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100918 if (surface == NULL)
919 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200920
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200921 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200922 if (!shsurf)
923 return;
924
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200925 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100926 case SHELL_SURFACE_PANEL:
927 case SHELL_SURFACE_BACKGROUND:
928 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200929 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100930 return;
931 default:
932 break;
933 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400934
Kristian Høgsberg07937562011-04-12 17:25:42 -0400935 x = device->grab_x - surface->x;
936 y = device->grab_y - surface->y;
937
938 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200939 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400940 else if (x < 2 * surface->width / 3)
941 edges |= 0;
942 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200943 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400944
945 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200946 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400947 else if (y < 2 * surface->height / 3)
948 edges |= 0;
949 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200950 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400951
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200953 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400954}
955
956static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500957terminate_binding(struct wl_input_device *device, uint32_t time,
958 uint32_t key, uint32_t button, uint32_t state, void *data)
959{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500960 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500961
962 if (state)
963 wl_display_terminate(compositor->wl_display);
964}
965
966static void
Pekka Paalanen460099f2012-01-20 16:48:25 +0200967rotate_grab_motion(struct wl_grab *grab,
968 uint32_t time, int32_t x, int32_t y)
969{
970 struct rotate_grab *rotate =
971 container_of(grab, struct rotate_grab, grab);
972 struct wl_input_device *device = grab->input_device;
973 struct shell_surface *surface = rotate->surface;
974 GLfloat dx, dy;
975 GLfloat r;
976
977 dx = device->x - rotate->center.x;
978 dy = device->y - rotate->center.y;
979 r = sqrtf(dx * dx + dy * dy);
980
981 wl_list_remove(&surface->rotation.transform.link);
982 surface->surface->transform.dirty = 1;
983
984 if (r > 20.0f) {
985 struct weston_matrix roto;
986 struct weston_matrix *matrix =
987 &surface->rotation.transform.matrix;
988
989 weston_matrix_init(&roto);
990 roto.d[0] = dx / r;
991 roto.d[4] = -dy / r;
992 roto.d[1] = -roto.d[4];
993 roto.d[5] = roto.d[0];
994
995 weston_matrix_init(matrix);
996 weston_matrix_translate(matrix, -rotate->center.x,
997 -rotate->center.y, 0.0f);
998 weston_matrix_multiply(matrix, &roto);
999 weston_matrix_translate(matrix, rotate->center.x,
1000 rotate->center.y, 0.0f);
1001
1002 wl_list_insert(surface->surface->transform.list.prev,
1003 &surface->rotation.transform.link);
1004 } else {
1005 wl_list_init(&surface->rotation.transform.link);
1006 }
1007
1008 weston_compositor_damage_all(surface->surface->compositor);
1009}
1010
1011static void
1012rotate_grab_button(struct wl_grab *grab,
1013 uint32_t time, int32_t button, int32_t state)
1014{
1015 struct rotate_grab *rotate =
1016 container_of(grab, struct rotate_grab, grab);
1017 struct wl_input_device *device = grab->input_device;
1018
1019 if (device->button_count == 0 && state == 0) {
1020 wl_input_device_end_grab(device, time);
1021 free(rotate);
1022 }
1023}
1024
1025static const struct wl_grab_interface rotate_grab_interface = {
1026 noop_grab_focus,
1027 rotate_grab_motion,
1028 rotate_grab_button,
1029};
1030
1031static void
1032rotate_binding(struct wl_input_device *device, uint32_t time,
1033 uint32_t key, uint32_t button, uint32_t state, void *data)
1034{
1035 struct weston_surface *base_surface =
1036 (struct weston_surface *) device->pointer_focus;
1037 struct shell_surface *surface;
1038 struct rotate_grab *rotate;
1039 struct weston_vector center = { { 0.0f, 0.0f, 0.0f, 1.0f } };
1040
1041 if (base_surface == NULL)
1042 return;
1043
1044 surface = get_shell_surface(base_surface);
1045 if (!surface)
1046 return;
1047
1048 switch (surface->type) {
1049 case SHELL_SURFACE_PANEL:
1050 case SHELL_SURFACE_BACKGROUND:
1051 case SHELL_SURFACE_FULLSCREEN:
1052 case SHELL_SURFACE_SCREENSAVER:
1053 return;
1054 default:
1055 break;
1056 }
1057
1058 center.f[0] = 0.5f * surface->surface->width;
1059 center.f[1] = 0.5f * surface->surface->height;
1060 weston_surface_update_transform(surface->surface);
1061 weston_matrix_transform(&surface->surface->transform.matrix, &center);
1062 if (fabsf(center.f[3]) < 1e-6)
1063 return;
1064
1065 rotate = malloc(sizeof *rotate);
1066 if (!rotate)
1067 return;
1068
1069 rotate->grab.interface = &rotate_grab_interface;
1070 rotate->surface = surface;
1071 rotate->center.x = center.f[0] / center.f[3];
1072 rotate->center.y = center.f[1] / center.f[3];
1073
1074 wl_input_device_start_grab(device, &rotate->grab, time);
1075 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1076}
1077
1078static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001079activate(struct weston_shell *base, struct weston_surface *es,
1080 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -04001081{
1082 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001083 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001084 struct wl_list *list;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001085
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001086 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001087
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001088 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001089 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -04001090
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001091 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001092 case SHELL_SURFACE_BACKGROUND:
1093 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -04001094 wl_list_remove(&es->link);
1095 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001096 break;
1097 case SHELL_SURFACE_PANEL:
1098 /* already put on top */
1099 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001100 case SHELL_SURFACE_SCREENSAVER:
1101 /* always below lock surface */
1102 if (shell->lock_surface) {
1103 wl_list_remove(&es->link);
1104 wl_list_insert(&shell->lock_surface->surface->link,
1105 &es->link);
1106 }
1107 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001108 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001109 if (!shell->locked) {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001110 list = weston_compositor_top(compositor);
1111
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001112 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001113 struct shell_surface *panel;
1114 wl_list_for_each(panel, &shell->panels, link) {
1115 wl_list_remove(&panel->surface->link);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001116 wl_list_insert(list, &panel->surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001117 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001118 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001119 }
1120}
1121
1122static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001123click_to_activate_binding(struct wl_input_device *device,
1124 uint32_t time, uint32_t key,
1125 uint32_t button, uint32_t state, void *data)
1126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001127 struct weston_input_device *wd = (struct weston_input_device *) device;
1128 struct weston_compositor *compositor = data;
1129 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001132 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001133 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001134}
1135
1136static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001137lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001138{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001139 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001140 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001141 struct weston_surface *cur;
1142 struct weston_surface *tmp;
1143 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001144 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001145 uint32_t time;
1146
1147 if (shell->locked)
1148 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001149
1150 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001151
1152 /* Move all surfaces from compositor's list to our hidden list,
1153 * except the background. This way nothing else can show or
1154 * receive input events while we are locked. */
1155
1156 if (!wl_list_empty(&shell->hidden_surface_list)) {
1157 fprintf(stderr,
1158 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1159 __func__);
1160 }
1161
1162 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1163 /* skip input device sprites, cur->surface is uninitialised */
1164 if (cur->surface.resource.client == NULL)
1165 continue;
1166
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001167 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001168 continue;
1169
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001170 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001171 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001172 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001173 }
1174
Pekka Paalanen77346a62011-11-30 16:26:35 +02001175 launch_screensaver(shell);
1176
1177 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1178 show_screensaver(shell, shsurf);
1179
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001180 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001181 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001182 weston_compositor_wake(shell->compositor);
1183 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001184 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001185
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001186 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001187 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001188
1189 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001190 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001191 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1192 wl_input_device_set_keyboard_focus(&device->input_device,
1193 NULL, time);
1194 }
1195
1196 /* TODO: disable bindings that should not work while locked. */
1197
1198 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001199}
1200
1201static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001202unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001203{
1204 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1205
Pekka Paalanend81c2162011-11-16 13:47:34 +02001206 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001207 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001208 return;
1209 }
1210
1211 /* If desktop-shell client has gone away, unlock immediately. */
1212 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001213 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001214 return;
1215 }
1216
1217 if (shell->prepare_event_sent)
1218 return;
1219
1220 wl_resource_post_event(shell->child.desktop_shell,
1221 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1222 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001223}
1224
1225static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001226center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001228 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001229
1230 surface->x = output->x + (mode->width - surface->width) / 2;
1231 surface->y = output->y + (mode->height - surface->height) / 2;
1232}
1233
1234static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235map(struct weston_shell *base,
1236 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001237{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001238 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001239 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001240 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001241 struct shell_surface *shsurf;
1242 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1243 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001244
Pekka Paalanen77346a62011-11-30 16:26:35 +02001245 shsurf = get_shell_surface(surface);
1246 if (shsurf)
1247 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001248
Pekka Paalanen77346a62011-11-30 16:26:35 +02001249 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001250 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001251 do_configure = 0;
1252 } else {
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001253 list = weston_compositor_top(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001254 do_configure = 1;
1255 }
1256
1257 surface->width = width;
1258 surface->height = height;
1259
1260 /* initial positioning, see also configure() */
1261 switch (surface_type) {
1262 case SHELL_SURFACE_TOPLEVEL:
1263 surface->x = 10 + random() % 400;
1264 surface->y = 10 + random() % 400;
1265 break;
1266 case SHELL_SURFACE_SCREENSAVER:
1267 case SHELL_SURFACE_FULLSCREEN:
1268 center_on_output(surface, surface->fullscreen_output);
1269 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001270 case SHELL_SURFACE_LOCK:
1271 center_on_output(surface, get_default_output(compositor));
1272 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001273 default:
1274 ;
1275 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001276
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001277 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001278 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001279 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001280 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001281 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001282 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001283 break;
1284 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001285 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001286 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001287 break;
1288 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001289 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001290 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001291
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292 weston_compositor_repick(compositor);
1293 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001294 do_configure = 1;
1295 break;
1296 case SHELL_SURFACE_SCREENSAVER:
1297 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001298 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001299 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001300 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001301 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001302 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001303 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001304 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001305 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001306 break;
1307 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001308 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001309 if (!wl_list_empty(&shell->panels)) {
1310 struct shell_surface *panel =
1311 container_of(shell->panels.prev,
1312 struct shell_surface, link);
1313 wl_list_insert(&panel->surface->link, &surface->link);
1314 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001315 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001316 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001317 }
1318
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001319 switch (surface_type) {
1320 case SHELL_SURFACE_TOPLEVEL:
1321 surface->x = 10 + random() % 400;
1322 surface->y = 10 + random() % 400;
1323 break;
1324 case SHELL_SURFACE_POPUP:
1325 shell_map_popup(shsurf, shsurf->popup.time);
1326 break;
1327 default:
1328 break;
1329 }
1330
1331 surface->width = width;
1332 surface->height = height;
1333 if (do_configure) {
1334 weston_surface_configure(surface, surface->x, surface->y,
1335 width, height);
1336 weston_compositor_repick(compositor);
1337 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001338
Juan Zhao7bb92f02011-12-15 11:31:51 -05001339 switch (surface_type) {
1340 case SHELL_SURFACE_TOPLEVEL:
1341 case SHELL_SURFACE_TRANSIENT:
1342 case SHELL_SURFACE_FULLSCREEN:
1343 if (!shell->locked)
1344 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001345 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001346 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001347 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001348 break;
1349 default:
1350 break;
1351 }
1352
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001353 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001354 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001355}
1356
1357static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001358configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001359 int32_t x, int32_t y, int32_t width, int32_t height)
1360{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001361 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1362 int do_configure = !shell->locked;
1363 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1364 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001365
Pekka Paalanen77346a62011-11-30 16:26:35 +02001366 shsurf = get_shell_surface(surface);
1367 if (shsurf)
1368 surface_type = shsurf->type;
1369
1370 surface->width = width;
1371 surface->height = height;
1372
1373 switch (surface_type) {
1374 case SHELL_SURFACE_SCREENSAVER:
1375 do_configure = !do_configure;
1376 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001377 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001378 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001379 break;
1380 default:
1381 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001382 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001383
Pekka Paalanen77346a62011-11-30 16:26:35 +02001384 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001385 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001386 * the surface is supposed to be in compositor->surface_list.
1387 * Be careful with that, and make sure we stay on the right output.
1388 * XXX: would a fullscreen surface need the same handling?
1389 */
1390 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001391 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001392
1393 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1394 surface->output = shsurf->output;
1395 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001396}
1397
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001398static int launch_desktop_shell_process(struct wl_shell *shell);
1399
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001400static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001401desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001402{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001403 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001404 struct wl_shell *shell =
1405 container_of(process, struct wl_shell, child.process);
1406
1407 shell->child.process.pid = 0;
1408 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001409
1410 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1411 time = weston_compositor_get_time();
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001412 if (time - shell->child.deathstamp > 30000) {
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001413 shell->child.deathstamp = time;
1414 shell->child.deathcount = 0;
1415 }
1416
1417 shell->child.deathcount++;
1418 if (shell->child.deathcount > 5) {
1419 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1420 return;
1421 }
1422
1423 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1424 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001425}
1426
1427static int
1428launch_desktop_shell_process(struct wl_shell *shell)
1429{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001430 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001431
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001432 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001433 &shell->child.process,
1434 shell_exe,
1435 desktop_shell_sigchld);
1436
1437 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001438 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001439 return 0;
1440}
1441
1442static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001443bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1444{
1445 struct wl_shell *shell = data;
1446
1447 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001448 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001449}
1450
Kristian Høgsberg75840622011-09-06 13:48:16 -04001451static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001452unbind_desktop_shell(struct wl_resource *resource)
1453{
1454 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001455
1456 if (shell->locked)
1457 resume_desktop(shell);
1458
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001459 shell->child.desktop_shell = NULL;
1460 shell->prepare_event_sent = false;
1461 free(resource);
1462}
1463
1464static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001465bind_desktop_shell(struct wl_client *client,
1466 void *data, uint32_t version, uint32_t id)
1467{
1468 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001469 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001470
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001471 resource = wl_client_add_object(client, &desktop_shell_interface,
1472 &desktop_shell_implementation,
1473 id, shell);
1474
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001475 if (client == shell->child.client) {
1476 resource->destroy = unbind_desktop_shell;
1477 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001478 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001479 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001480
1481 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1482 "permission to bind desktop_shell denied");
1483 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001484}
1485
Pekka Paalanen6e168112011-11-24 11:34:05 +02001486static void
1487screensaver_set_surface(struct wl_client *client,
1488 struct wl_resource *resource,
1489 struct wl_resource *shell_surface_resource,
1490 struct wl_resource *output_resource)
1491{
1492 struct wl_shell *shell = resource->data;
1493 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001494 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001495
Pekka Paalanen98262232011-12-01 10:42:22 +02001496 if (reset_shell_surface_type(surface))
1497 return;
1498
Pekka Paalanen77346a62011-11-30 16:26:35 +02001499 surface->type = SHELL_SURFACE_SCREENSAVER;
1500
1501 surface->surface->fullscreen_output = output;
1502 surface->output = output;
1503 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001504}
1505
1506static const struct screensaver_interface screensaver_implementation = {
1507 screensaver_set_surface
1508};
1509
1510static void
1511unbind_screensaver(struct wl_resource *resource)
1512{
1513 struct wl_shell *shell = resource->data;
1514
Pekka Paalanen77346a62011-11-30 16:26:35 +02001515 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001516 free(resource);
1517}
1518
1519static void
1520bind_screensaver(struct wl_client *client,
1521 void *data, uint32_t version, uint32_t id)
1522{
1523 struct wl_shell *shell = data;
1524 struct wl_resource *resource;
1525
1526 resource = wl_client_add_object(client, &screensaver_interface,
1527 &screensaver_implementation,
1528 id, shell);
1529
Pekka Paalanen77346a62011-11-30 16:26:35 +02001530 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001531 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001532 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001533 return;
1534 }
1535
1536 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1537 "interface object already bound");
1538 wl_resource_destroy(resource, 0);
1539}
1540
Pekka Paalanen3c647232011-12-22 13:43:43 +02001541static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001542shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001543{
1544 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1545
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001546 if (shell->child.client)
1547 wl_client_destroy(shell->child.client);
1548
Pekka Paalanen3c647232011-12-22 13:43:43 +02001549 free(shell->screensaver.path);
1550 free(shell);
1551}
1552
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001553int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001555
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001556WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001557shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001558{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001559 struct wl_shell *shell;
1560
1561 shell = malloc(sizeof *shell);
1562 if (shell == NULL)
1563 return -1;
1564
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001565 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001566 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001567 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001568 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001569 shell->shell.map = map;
1570 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001571 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001572
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001573 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001574 wl_list_init(&shell->backgrounds);
1575 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001576 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001577
Kristian Høgsberg6af8eb92012-01-25 16:57:11 -05001578 shell_configuration(shell);
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001579
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001580 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1581 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001582 return -1;
1583
Kristian Høgsberg75840622011-09-06 13:48:16 -04001584 if (wl_display_add_global(ec->wl_display,
1585 &desktop_shell_interface,
1586 shell, bind_desktop_shell) == NULL)
1587 return -1;
1588
Pekka Paalanen6e168112011-11-24 11:34:05 +02001589 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1590 shell, bind_screensaver) == NULL)
1591 return -1;
1592
Kristian Høgsbergf03a6162012-01-17 11:07:42 -05001593 shell->child.deathstamp = weston_compositor_get_time();
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001594 if (launch_desktop_shell_process(shell) != 0)
1595 return -1;
1596
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001598 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001599 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001600 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001601 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001602 MODIFIER_CTRL | MODIFIER_ALT,
1603 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001604 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001605 click_to_activate_binding, ec);
Pekka Paalanen460099f2012-01-20 16:48:25 +02001606 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1607 MODIFIER_SUPER | MODIFIER_ALT,
1608 rotate_binding, NULL);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001609
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001610 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001611
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001612 return 0;
1613}