blob: c0d8b8d03c25f16add9df91ab4019116a9a24c0f [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>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050032
Pekka Paalanen50719bc2011-11-22 14:18:50 +020033#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040035#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020036#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Pekka Paalanen068ae942011-11-28 14:11:15 +020038struct shell_surface;
39
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040040struct wl_shell {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050041 struct weston_compositor *compositor;
42 struct weston_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020043
44 struct {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050045 struct weston_process process;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020046 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020047 struct wl_resource *desktop_shell;
Pekka Paalanen4d733ee2012-01-17 14:36:27 +020048
49 unsigned deathcount;
50 uint32_t deathstamp;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020051 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020052
53 bool locked;
54 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020055
Pekka Paalanen068ae942011-11-28 14:11:15 +020056 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050057 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020058 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010059
60 struct wl_list backgrounds;
61 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020062
Pekka Paalanen77346a62011-11-30 16:26:35 +020063 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020064 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020065 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020066 struct wl_resource *binding;
67 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020069 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040070};
71
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050072enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020073 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050074
Pekka Paalanen57da4a82011-11-23 16:42:16 +020075 SHELL_SURFACE_PANEL,
76 SHELL_SURFACE_BACKGROUND,
77 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020078 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050079
80 SHELL_SURFACE_TOPLEVEL,
81 SHELL_SURFACE_TRANSIENT,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050082 SHELL_SURFACE_FULLSCREEN,
83 SHELL_SURFACE_POPUP
Pekka Paalanen57da4a82011-11-23 16:42:16 +020084};
85
Pekka Paalanen56cdea92011-11-23 16:14:12 +020086struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020087 struct wl_resource resource;
88
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050089 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020090 struct wl_listener surface_destroy_listener;
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050091 struct shell_surface *parent;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020092
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050093 enum shell_surface_type type;
94 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010095
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -050096 struct {
97 struct wl_grab grab;
98 uint32_t time;
99 int32_t x, y;
100 int32_t initial_up;
101 } popup;
102
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500103 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100104 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200105};
106
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500107struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500108 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500110 int32_t dx, dy;
111};
112
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200113static int
114shell_configuration(struct wl_shell *shell)
115{
116 int ret;
117 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200118 char *path = NULL;
119 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200120
121 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200122 { "path", CONFIG_KEY_STRING, &path },
123 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200124 };
125
126 struct config_section cs[] = {
127 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
128 };
129
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500130 config_file = config_file_path("weston-desktop-shell.ini");
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200131 ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
132 free(config_file);
133
Pekka Paalanen7296e792011-12-07 16:22:00 +0200134 shell->screensaver.path = path;
135 shell->screensaver.duration = duration;
136
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200137 return ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200138}
139
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200140static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500141noop_grab_focus(struct wl_grab *grab, uint32_t time,
142 struct wl_surface *surface, int32_t x, int32_t y)
143{
144 grab->focus = NULL;
145}
146
147static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500148move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500149 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500150{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500151 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500152 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500153 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500154
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500155 weston_surface_configure(es,
156 device->x + move->dx,
157 device->y + move->dy,
158 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159}
160
161static void
162move_grab_button(struct wl_grab *grab,
163 uint32_t time, int32_t button, int32_t state)
164{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500165 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500166
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500167 if (device->button_count == 0 && state == 0) {
168 wl_input_device_end_grab(device, time);
169 free(grab);
170 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500171}
172
173static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500174 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500175 move_grab_motion,
176 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177};
178
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400179static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500180weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500181 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500183 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500184
185 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400186 if (!move)
187 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188
189 move->grab.interface = &move_grab_interface;
190 move->dx = es->x - wd->input_device.grab_x;
191 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500192 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500194 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500195
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400196 wl_input_device_set_pointer_focus(&wd->input_device,
197 NULL, time, 0, 0, 0, 0);
198
199 return 0;
200}
201
202static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200203shell_surface_move(struct wl_client *client, struct wl_resource *resource,
204 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400205{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500206 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200207 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400208
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500209 if (wd->input_device.button_count == 0 ||
210 wd->input_device.grab_time != time ||
211 wd->input_device.pointer_focus != &shsurf->surface->surface)
212 return;
213
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500214 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400215 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216}
217
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500218struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500219 struct wl_grab grab;
220 uint32_t edges;
221 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200222 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223};
224
225static void
226resize_grab_motion(struct wl_grab *grab,
227 uint32_t time, int32_t x, int32_t y)
228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500229 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500231 int32_t width, height;
232
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200233 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500234 width = device->grab_x - device->x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200235 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500236 width = device->x - device->grab_x + resize->width;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500237 } else {
238 width = resize->width;
239 }
240
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200241 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500242 height = device->grab_y - device->y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200243 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500244 height = device->y - device->grab_y + resize->height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 } else {
246 height = resize->height;
247 }
248
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200249 wl_resource_post_event(&resize->shsurf->resource,
250 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
251 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252}
253
254static void
255resize_grab_button(struct wl_grab *grab,
256 uint32_t time, int32_t button, int32_t state)
257{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500258 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500260 if (device->button_count == 0 && state == 0) {
261 wl_input_device_end_grab(device, time);
262 free(grab);
263 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264}
265
266static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500267 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268 resize_grab_motion,
269 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270};
271
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400272static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_surface_resize(struct shell_surface *shsurf,
274 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200275 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500276{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500277 struct weston_resize_grab *resize;
278 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500279
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500280 /* FIXME: Reject if fullscreen */
281
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400283 if (!resize)
284 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500285
286 resize->grab.interface = &resize_grab_interface;
287 resize->edges = edges;
288 resize->dx = es->x - wd->input_device.grab_x;
289 resize->dy = es->y - wd->input_device.grab_y;
290 resize->width = es->width;
291 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200292 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400293
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500294 if (edges == 0 || edges > 15 ||
295 (edges & 3) == 3 || (edges & 12) == 12)
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200296 goto err_out;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500297
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500298 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400300 wl_input_device_set_pointer_focus(&wd->input_device,
301 NULL, time, 0, 0, 0, 0);
302
303 return 0;
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200304
305err_out:
306 free(resize);
307 return 0;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400308}
309
310static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200311shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
312 struct wl_resource *input_resource, uint32_t time,
313 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200316 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400317
318 /* FIXME: Reject if fullscreen */
319
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500320 if (wd->input_device.button_count == 0 ||
321 wd->input_device.grab_time != time ||
322 wd->input_device.pointer_focus != &shsurf->surface->surface)
323 return;
324
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500325 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400326 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327}
328
Pekka Paalanen98262232011-12-01 10:42:22 +0200329static int
330reset_shell_surface_type(struct shell_surface *surface)
331{
332 switch (surface->type) {
333 case SHELL_SURFACE_FULLSCREEN:
334 surface->surface->x = surface->saved_x;
335 surface->surface->y = surface->saved_y;
336 surface->surface->fullscreen_output = NULL;
337 break;
338 case SHELL_SURFACE_PANEL:
339 case SHELL_SURFACE_BACKGROUND:
340 wl_list_remove(&surface->link);
341 wl_list_init(&surface->link);
342 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200343 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200344 case SHELL_SURFACE_LOCK:
345 wl_resource_post_error(&surface->resource,
346 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200347 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200348 return -1;
349 case SHELL_SURFACE_NONE:
350 case SHELL_SURFACE_TOPLEVEL:
351 case SHELL_SURFACE_TRANSIENT:
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500352 case SHELL_SURFACE_POPUP:
Pekka Paalanen98262232011-12-01 10:42:22 +0200353 break;
354 }
355
356 surface->type = SHELL_SURFACE_NONE;
357 return 0;
358}
359
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500360static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200361shell_surface_set_toplevel(struct wl_client *client,
362 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400363
364{
Pekka Paalanen98262232011-12-01 10:42:22 +0200365 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400366
Pekka Paalanen98262232011-12-01 10:42:22 +0200367 if (reset_shell_surface_type(surface))
368 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400369
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500370 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200371 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400372}
373
374static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200375shell_surface_set_transient(struct wl_client *client,
376 struct wl_resource *resource,
377 struct wl_resource *parent_resource,
378 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400379{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200380 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500381 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200382 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500383 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400384
Pekka Paalanen98262232011-12-01 10:42:22 +0200385 if (reset_shell_surface_type(shsurf))
386 return;
387
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400388 /* assign to parents output */
389 es->output = pes->output;
390
391 es->x = pes->x + x;
392 es->y = pes->y + y;
393
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500394 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200395 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396}
397
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500398static struct weston_output *
399get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200400{
401 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500402 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200403}
404
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400405static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200406shell_surface_set_fullscreen(struct wl_client *client,
407 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400408
409{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200410 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 struct weston_surface *es = shsurf->surface;
412 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400413
Pekka Paalanen98262232011-12-01 10:42:22 +0200414 if (reset_shell_surface_type(shsurf))
415 return;
416
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400417 /* FIXME: Fullscreen on first output */
418 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200419 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400420 es->output = output;
421
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200422 shsurf->saved_x = es->x;
423 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400424 es->x = (output->current->width - es->width) / 2;
425 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400426 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500427 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200428 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400429}
430
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500431static void
432popup_grab_focus(struct wl_grab *grab, uint32_t time,
433 struct wl_surface *surface, int32_t x, int32_t y)
434{
435 struct wl_input_device *device = grab->input_device;
436 struct shell_surface *priv =
437 container_of(grab, struct shell_surface, popup.grab);
438 struct wl_client *client = priv->surface->surface.resource.client;
439
440 if (surface->resource.client == client) {
441 wl_input_device_set_pointer_focus(device, surface, time,
442 device->x, device->y, x, y);
443 grab->focus = surface;
444 } else {
445 wl_input_device_set_pointer_focus(device, NULL,
446 time, 0, 0, 0, 0);
447 grab->focus = NULL;
448 }
449}
450
451static void
452popup_grab_motion(struct wl_grab *grab,
453 uint32_t time, int32_t x, int32_t y)
454{
455 struct wl_input_device *device = grab->input_device;
456 struct wl_resource *resource;
457
458 resource = grab->input_device->pointer_focus_resource;
459 if (resource)
460 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
461 time, device->x, device->y, x, y);
462}
463
464static void
465popup_grab_button(struct wl_grab *grab,
466 uint32_t time, int32_t button, int32_t state)
467{
468 struct wl_resource *resource;
469 struct shell_surface *shsurf =
470 container_of(grab, struct shell_surface, popup.grab);
471
472 resource = grab->input_device->pointer_focus_resource;
473 if (resource) {
474 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
475 time, button, state);
476 } else if (state == 0 &&
477 (shsurf->popup.initial_up ||
478 time - shsurf->popup.time > 500)) {
479 wl_resource_post_event(&shsurf->resource,
480 WL_SHELL_SURFACE_POPUP_DONE);
481 wl_input_device_end_grab(grab->input_device, time);
482 shsurf->popup.grab.input_device = NULL;
483 }
484
485 if (state == 0)
486 shsurf->popup.initial_up = 1;
487}
488
489static const struct wl_grab_interface popup_grab_interface = {
490 popup_grab_focus,
491 popup_grab_motion,
492 popup_grab_button,
493};
494
495static void
496shell_map_popup(struct shell_surface *shsurf, uint32_t time)
497{
498 struct wl_input_device *device;
499 struct weston_surface *es = shsurf->surface;
500 struct weston_surface *parent = shsurf->parent->surface;
501
502 es->output = parent->output;
503
504 shsurf->popup.grab.interface = &popup_grab_interface;
505 device = es->compositor->input_device;
506
507 es->x = shsurf->parent->surface->x + shsurf->popup.x;
508 es->y = shsurf->parent->surface->y + shsurf->popup.y;
509
510 shsurf->popup.grab.input_device = device;
511 shsurf->popup.time = device->grab_time;
512 shsurf->popup.initial_up = 0;
513
514 wl_input_device_start_grab(shsurf->popup.grab.input_device,
515 &shsurf->popup.grab, shsurf->popup.time);
516}
517
518static void
519shell_surface_set_popup(struct wl_client *client,
520 struct wl_resource *resource,
521 struct wl_resource *input_device_resource,
522 uint32_t time,
523 struct wl_resource *parent_resource,
524 int32_t x, int32_t y, uint32_t flags)
525{
526 struct shell_surface *shsurf = resource->data;
527 struct weston_surface *es = shsurf->surface;
528
529 weston_surface_damage(es);
530 shsurf->type = SHELL_SURFACE_POPUP;
531 shsurf->parent = parent_resource->data;
532 shsurf->popup.x = x;
533 shsurf->popup.y = y;
534}
535
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200536static const struct wl_shell_surface_interface shell_surface_implementation = {
537 shell_surface_move,
538 shell_surface_resize,
539 shell_surface_set_toplevel,
540 shell_surface_set_transient,
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500541 shell_surface_set_fullscreen,
542 shell_surface_set_popup
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200543};
544
545static void
546destroy_shell_surface(struct wl_resource *resource)
547{
548 struct shell_surface *shsurf = resource->data;
549
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -0500550 if (shsurf->popup.grab.input_device)
551 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
552
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200553 /* in case cleaning up a dead client destroys shell_surface first */
554 if (shsurf->surface)
555 wl_list_remove(&shsurf->surface_destroy_listener.link);
556
557 wl_list_remove(&shsurf->link);
558 free(shsurf);
559}
560
561static void
562shell_handle_surface_destroy(struct wl_listener *listener,
563 struct wl_resource *resource, uint32_t time)
564{
565 struct shell_surface *shsurf = container_of(listener,
566 struct shell_surface,
567 surface_destroy_listener);
568
569 shsurf->surface = NULL;
570 wl_resource_destroy(&shsurf->resource, time);
571}
572
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200573static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500574get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200575{
576 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
577 struct wl_listener *listener;
578
579 /* search the destroy listener list for our callback */
580 wl_list_for_each(listener, lst, link) {
581 if (listener->func == shell_handle_surface_destroy) {
582 return container_of(listener, struct shell_surface,
583 surface_destroy_listener);
584 }
585 }
586
587 return NULL;
588}
589
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200590static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200591shell_get_shell_surface(struct wl_client *client,
592 struct wl_resource *resource,
593 uint32_t id,
594 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200595{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500596 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200597 struct shell_surface *shsurf;
598
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200599 if (get_shell_surface(surface)) {
600 wl_resource_post_error(surface_resource,
601 WL_DISPLAY_ERROR_INVALID_OBJECT,
602 "wl_shell::get_shell_surface already requested");
603 return;
604 }
605
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200606 shsurf = calloc(1, sizeof *shsurf);
607 if (!shsurf) {
608 wl_resource_post_no_memory(resource);
609 return;
610 }
611
612 shsurf->resource.destroy = destroy_shell_surface;
613 shsurf->resource.object.id = id;
614 shsurf->resource.object.interface = &wl_shell_surface_interface;
615 shsurf->resource.object.implementation =
616 (void (**)(void)) &shell_surface_implementation;
617 shsurf->resource.data = shsurf;
618
619 shsurf->surface = surface;
620 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
621 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
622 &shsurf->surface_destroy_listener.link);
623
624 /* init link so its safe to always remove it in destroy_shell_surface */
625 wl_list_init(&shsurf->link);
626
Pekka Paalanen98262232011-12-01 10:42:22 +0200627 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200628
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200629 wl_client_add_resource(client, &shsurf->resource);
630}
631
632static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200633 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500634};
635
Kristian Høgsberg07937562011-04-12 17:25:42 -0400636static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200638{
639 proc->pid = 0;
640}
641
642static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200643launch_screensaver(struct wl_shell *shell)
644{
645 if (shell->screensaver.binding)
646 return;
647
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200648 if (!shell->screensaver.path)
649 return;
650
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500651 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200652 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200653 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200654 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200655}
656
657static void
658terminate_screensaver(struct wl_shell *shell)
659{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200660 if (shell->screensaver.process.pid == 0)
661 return;
662
663 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200664}
665
666static void
667show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
668{
669 struct wl_list *list;
670
671 if (shell->lock_surface)
672 list = &shell->lock_surface->surface->link;
673 else
674 list = &shell->compositor->surface_list;
675
676 wl_list_remove(&surface->surface->link);
677 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500678 weston_surface_configure(surface->surface,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200679 surface->surface->x,
680 surface->surface->y,
681 surface->surface->width,
682 surface->surface->height);
683 surface->surface->output = surface->output;
684}
685
686static void
687hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
688{
689 wl_list_remove(&surface->surface->link);
690 wl_list_init(&surface->surface->link);
691 surface->surface->output = NULL;
692}
693
694static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400695desktop_shell_set_background(struct wl_client *client,
696 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100697 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400698 struct wl_resource *surface_resource)
699{
700 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200701 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500702 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200703 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400704
Pekka Paalanen98262232011-12-01 10:42:22 +0200705 if (reset_shell_surface_type(shsurf))
706 return;
707
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100708 wl_list_for_each(priv, &shell->backgrounds, link) {
709 if (priv->output == output_resource->data) {
710 priv->surface->output = NULL;
711 wl_list_remove(&priv->surface->link);
712 wl_list_remove(&priv->link);
713 break;
714 }
715 }
716
Pekka Paalanen068ae942011-11-28 14:11:15 +0200717 shsurf->type = SHELL_SURFACE_BACKGROUND;
718 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100719
Pekka Paalanen068ae942011-11-28 14:11:15 +0200720 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100721
Pekka Paalanen068ae942011-11-28 14:11:15 +0200722 surface->x = shsurf->output->x;
723 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200724
Kristian Høgsberg75840622011-09-06 13:48:16 -0400725 wl_resource_post_event(resource,
726 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500727 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200728 shsurf->output->current->width,
729 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400730}
731
732static void
733desktop_shell_set_panel(struct wl_client *client,
734 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100735 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400736 struct wl_resource *surface_resource)
737{
738 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200739 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500740 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200741 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400742
Pekka Paalanen98262232011-12-01 10:42:22 +0200743 if (reset_shell_surface_type(shsurf))
744 return;
745
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100746 wl_list_for_each(priv, &shell->panels, link) {
747 if (priv->output == output_resource->data) {
748 priv->surface->output = NULL;
749 wl_list_remove(&priv->surface->link);
750 wl_list_remove(&priv->link);
751 break;
752 }
753 }
754
Pekka Paalanen068ae942011-11-28 14:11:15 +0200755 shsurf->type = SHELL_SURFACE_PANEL;
756 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100757
Pekka Paalanen068ae942011-11-28 14:11:15 +0200758 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100759
Pekka Paalanen068ae942011-11-28 14:11:15 +0200760 surface->x = shsurf->output->x;
761 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200762
Kristian Høgsberg75840622011-09-06 13:48:16 -0400763 wl_resource_post_event(resource,
764 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500765 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200766 shsurf->output->current->width,
767 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400768}
769
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200770static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500771handle_lock_surface_destroy(struct wl_listener *listener,
772 struct wl_resource *resource, uint32_t time)
773{
774 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200775 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500776
777 fprintf(stderr, "lock surface gone\n");
778 shell->lock_surface = NULL;
779}
780
781static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200782desktop_shell_set_lock_surface(struct wl_client *client,
783 struct wl_resource *resource,
784 struct wl_resource *surface_resource)
785{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200786 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200787 struct shell_surface *surface = surface_resource->data;
788
789 if (reset_shell_surface_type(surface))
790 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200791
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200792 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200793
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200794 if (!shell->locked)
795 return;
796
Pekka Paalanen98262232011-12-01 10:42:22 +0200797 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200798
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500799 shell->lock_surface_listener.func = handle_lock_surface_destroy;
800 wl_list_insert(&surface_resource->destroy_listener_list,
801 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200802
Pekka Paalanen068ae942011-11-28 14:11:15 +0200803 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200804}
805
806static void
807resume_desktop(struct wl_shell *shell)
808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500809 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200810 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200811 struct shell_surface *tmp;
812
813 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
814 hide_screensaver(shell, tmp);
815
816 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200817
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500818 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500819 weston_surface_configure(surface, surface->x, surface->y,
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500820 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200821
Pekka Paalanenfe340832011-11-25 16:07:52 +0200822 if (wl_list_empty(&shell->backgrounds)) {
823 list = &shell->compositor->surface_list;
824 } else {
825 struct shell_surface *background;
826 background = container_of(shell->backgrounds.prev,
827 struct shell_surface, link);
828 list = background->surface->link.prev;
829 }
830
831 if (!wl_list_empty(&shell->hidden_surface_list))
832 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500833 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200834
835 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500836 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200837 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500838 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200839}
840
841static void
842desktop_shell_unlock(struct wl_client *client,
843 struct wl_resource *resource)
844{
845 struct wl_shell *shell = resource->data;
846
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200847 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200848
849 if (shell->locked)
850 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200851}
852
Kristian Høgsberg75840622011-09-06 13:48:16 -0400853static const struct desktop_shell_interface desktop_shell_implementation = {
854 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200855 desktop_shell_set_panel,
856 desktop_shell_set_lock_surface,
857 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400858};
859
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200860static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500861get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200862{
863 struct shell_surface *shsurf;
864
865 shsurf = get_shell_surface(surface);
866 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200867 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200868 return shsurf->type;
869}
870
Kristian Høgsberg75840622011-09-06 13:48:16 -0400871static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400872move_binding(struct wl_input_device *device, uint32_t time,
873 uint32_t key, uint32_t button, uint32_t state, void *data)
874{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500875 struct weston_surface *surface =
876 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500877
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100878 if (surface == NULL)
879 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400880
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200881 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100882 case SHELL_SURFACE_PANEL:
883 case SHELL_SURFACE_BACKGROUND:
884 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200885 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100886 return;
887 default:
888 break;
889 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400890
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500891 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400892}
893
894static void
895resize_binding(struct wl_input_device *device, uint32_t time,
896 uint32_t key, uint32_t button, uint32_t state, void *data)
897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500898 struct weston_surface *surface =
899 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400900 uint32_t edges = 0;
901 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200902 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500903
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100904 if (surface == NULL)
905 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200906
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200907 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200908 if (!shsurf)
909 return;
910
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200911 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100912 case SHELL_SURFACE_PANEL:
913 case SHELL_SURFACE_BACKGROUND:
914 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200915 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100916 return;
917 default:
918 break;
919 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400920
Kristian Høgsberg07937562011-04-12 17:25:42 -0400921 x = device->grab_x - surface->x;
922 y = device->grab_y - surface->y;
923
924 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200925 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400926 else if (x < 2 * surface->width / 3)
927 edges |= 0;
928 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200929 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400930
931 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200932 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400933 else if (y < 2 * surface->height / 3)
934 edges |= 0;
935 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200936 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400937
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500938 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200939 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400940}
941
942static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500943terminate_binding(struct wl_input_device *device, uint32_t time,
944 uint32_t key, uint32_t button, uint32_t state, void *data)
945{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500946 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500947
948 if (state)
949 wl_display_terminate(compositor->wl_display);
950}
951
952static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500953activate(struct weston_shell *base, struct weston_surface *es,
954 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -0400955{
956 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400958
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400960
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400961 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500962 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400963
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200964 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200965 case SHELL_SURFACE_BACKGROUND:
966 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400967 wl_list_remove(&es->link);
968 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200969 break;
970 case SHELL_SURFACE_PANEL:
971 /* already put on top */
972 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200973 case SHELL_SURFACE_SCREENSAVER:
974 /* always below lock surface */
975 if (shell->lock_surface) {
976 wl_list_remove(&es->link);
977 wl_list_insert(&shell->lock_surface->surface->link,
978 &es->link);
979 }
980 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200981 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100982 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200983 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100984 struct shell_surface *panel;
985 wl_list_for_each(panel, &shell->panels, link) {
986 wl_list_remove(&panel->surface->link);
987 wl_list_insert(&compositor->surface_list,
988 &panel->surface->link);
989 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200990 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400991 }
992}
993
994static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500995click_to_activate_binding(struct wl_input_device *device,
996 uint32_t time, uint32_t key,
997 uint32_t button, uint32_t state, void *data)
998{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500999 struct weston_input_device *wd = (struct weston_input_device *) device;
1000 struct weston_compositor *compositor = data;
1001 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001002
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001003 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001004 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -05001005 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001006}
1007
1008static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001009lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001010{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001011 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001012 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001013 struct weston_surface *cur;
1014 struct weston_surface *tmp;
1015 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001016 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001017 uint32_t time;
1018
1019 if (shell->locked)
1020 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001021
1022 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001023
1024 /* Move all surfaces from compositor's list to our hidden list,
1025 * except the background. This way nothing else can show or
1026 * receive input events while we are locked. */
1027
1028 if (!wl_list_empty(&shell->hidden_surface_list)) {
1029 fprintf(stderr,
1030 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1031 __func__);
1032 }
1033
1034 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1035 /* skip input device sprites, cur->surface is uninitialised */
1036 if (cur->surface.resource.client == NULL)
1037 continue;
1038
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001039 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001040 continue;
1041
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001042 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001043 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001044 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001045 }
1046
Pekka Paalanen77346a62011-11-30 16:26:35 +02001047 launch_screensaver(shell);
1048
1049 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1050 show_screensaver(shell, shsurf);
1051
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001052 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +02001053 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001054 weston_compositor_wake(shell->compositor);
1055 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001056 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001057
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001058 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001059 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001060
1061 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001062 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001063 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1064 wl_input_device_set_keyboard_focus(&device->input_device,
1065 NULL, time);
1066 }
1067
1068 /* TODO: disable bindings that should not work while locked. */
1069
1070 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001071}
1072
1073static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001074unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001075{
1076 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1077
Pekka Paalanend81c2162011-11-16 13:47:34 +02001078 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001079 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001080 return;
1081 }
1082
1083 /* If desktop-shell client has gone away, unlock immediately. */
1084 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001085 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001086 return;
1087 }
1088
1089 if (shell->prepare_event_sent)
1090 return;
1091
1092 wl_resource_post_event(shell->child.desktop_shell,
1093 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1094 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001095}
1096
1097static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001098center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +02001099{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001100 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001101
1102 surface->x = output->x + (mode->width - surface->width) / 2;
1103 surface->y = output->y + (mode->height - surface->height) / 2;
1104}
1105
1106static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001107map(struct weston_shell *base,
1108 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001109{
Kristian Høgsberg75840622011-09-06 13:48:16 -04001110 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001111 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001112 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001113 struct shell_surface *shsurf;
1114 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1115 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001116
Pekka Paalanen77346a62011-11-30 16:26:35 +02001117 shsurf = get_shell_surface(surface);
1118 if (shsurf)
1119 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001120
Pekka Paalanen77346a62011-11-30 16:26:35 +02001121 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001122 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001123 do_configure = 0;
1124 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001125 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001126 do_configure = 1;
1127 }
1128
1129 surface->width = width;
1130 surface->height = height;
1131
1132 /* initial positioning, see also configure() */
1133 switch (surface_type) {
1134 case SHELL_SURFACE_TOPLEVEL:
1135 surface->x = 10 + random() % 400;
1136 surface->y = 10 + random() % 400;
1137 break;
1138 case SHELL_SURFACE_SCREENSAVER:
1139 case SHELL_SURFACE_FULLSCREEN:
1140 center_on_output(surface, surface->fullscreen_output);
1141 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001142 case SHELL_SURFACE_LOCK:
1143 center_on_output(surface, get_default_output(compositor));
1144 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001145 default:
1146 ;
1147 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001148
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001149 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001150 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001151 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001152 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001153 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001154 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001155 break;
1156 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001157 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001158 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001159 break;
1160 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001161 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001162 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001163
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001164 weston_compositor_repick(compositor);
1165 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001166 do_configure = 1;
1167 break;
1168 case SHELL_SURFACE_SCREENSAVER:
1169 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001170 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001171 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001172 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001173 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001174 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001175 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001176 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001177 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001178 break;
1179 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001180 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001181 if (!wl_list_empty(&shell->panels)) {
1182 struct shell_surface *panel =
1183 container_of(shell->panels.prev,
1184 struct shell_surface, link);
1185 wl_list_insert(&panel->surface->link, &surface->link);
1186 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001187 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001188 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001189 }
1190
Kristian Høgsbergb3cca0a2012-01-04 22:19:14 -05001191 switch (surface_type) {
1192 case SHELL_SURFACE_TOPLEVEL:
1193 surface->x = 10 + random() % 400;
1194 surface->y = 10 + random() % 400;
1195 break;
1196 case SHELL_SURFACE_POPUP:
1197 shell_map_popup(shsurf, shsurf->popup.time);
1198 break;
1199 default:
1200 break;
1201 }
1202
1203 surface->width = width;
1204 surface->height = height;
1205 if (do_configure) {
1206 weston_surface_configure(surface, surface->x, surface->y,
1207 width, height);
1208 weston_compositor_repick(compositor);
1209 }
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001210
Juan Zhao7bb92f02011-12-15 11:31:51 -05001211 switch (surface_type) {
1212 case SHELL_SURFACE_TOPLEVEL:
1213 case SHELL_SURFACE_TRANSIENT:
1214 case SHELL_SURFACE_FULLSCREEN:
1215 if (!shell->locked)
1216 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001217 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001218 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001219 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001220 break;
1221 default:
1222 break;
1223 }
1224
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001225 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001226 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001227}
1228
1229static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001230configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001231 int32_t x, int32_t y, int32_t width, int32_t height)
1232{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001233 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1234 int do_configure = !shell->locked;
1235 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1236 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001237
Pekka Paalanen77346a62011-11-30 16:26:35 +02001238 shsurf = get_shell_surface(surface);
1239 if (shsurf)
1240 surface_type = shsurf->type;
1241
1242 surface->width = width;
1243 surface->height = height;
1244
1245 switch (surface_type) {
1246 case SHELL_SURFACE_SCREENSAVER:
1247 do_configure = !do_configure;
1248 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001249 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001250 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001251 break;
1252 default:
1253 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001254 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001255
Pekka Paalanen77346a62011-11-30 16:26:35 +02001256 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001257 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001258 * the surface is supposed to be in compositor->surface_list.
1259 * Be careful with that, and make sure we stay on the right output.
1260 * XXX: would a fullscreen surface need the same handling?
1261 */
1262 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001263 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001264
1265 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1266 surface->output = shsurf->output;
1267 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001268}
1269
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001270static int launch_desktop_shell_process(struct wl_shell *shell);
1271
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001272static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001273desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001274{
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001275 uint32_t time;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001276 struct wl_shell *shell =
1277 container_of(process, struct wl_shell, child.process);
1278
1279 shell->child.process.pid = 0;
1280 shell->child.client = NULL; /* already destroyed by wayland */
Pekka Paalanen4d733ee2012-01-17 14:36:27 +02001281
1282 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1283 time = weston_compositor_get_time();
1284 if (shell->child.deathstamp == 0 ||
1285 time - shell->child.deathstamp > 30000) {
1286 shell->child.deathstamp = time;
1287 shell->child.deathcount = 0;
1288 }
1289
1290 shell->child.deathcount++;
1291 if (shell->child.deathcount > 5) {
1292 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1293 return;
1294 }
1295
1296 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1297 launch_desktop_shell_process(shell);
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001298}
1299
1300static int
1301launch_desktop_shell_process(struct wl_shell *shell)
1302{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001303 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001304
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001305 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001306 &shell->child.process,
1307 shell_exe,
1308 desktop_shell_sigchld);
1309
1310 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001311 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001312 return 0;
1313}
1314
1315static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001316bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1317{
1318 struct wl_shell *shell = data;
1319
1320 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001321 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001322}
1323
Kristian Høgsberg75840622011-09-06 13:48:16 -04001324static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001325unbind_desktop_shell(struct wl_resource *resource)
1326{
1327 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001328
1329 if (shell->locked)
1330 resume_desktop(shell);
1331
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001332 shell->child.desktop_shell = NULL;
1333 shell->prepare_event_sent = false;
1334 free(resource);
1335}
1336
1337static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001338bind_desktop_shell(struct wl_client *client,
1339 void *data, uint32_t version, uint32_t id)
1340{
1341 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001342 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001343
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001344 resource = wl_client_add_object(client, &desktop_shell_interface,
1345 &desktop_shell_implementation,
1346 id, shell);
1347
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001348 if (client == shell->child.client) {
1349 resource->destroy = unbind_desktop_shell;
1350 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001351 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001352 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001353
1354 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1355 "permission to bind desktop_shell denied");
1356 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001357}
1358
Pekka Paalanen6e168112011-11-24 11:34:05 +02001359static void
1360screensaver_set_surface(struct wl_client *client,
1361 struct wl_resource *resource,
1362 struct wl_resource *shell_surface_resource,
1363 struct wl_resource *output_resource)
1364{
1365 struct wl_shell *shell = resource->data;
1366 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001368
Pekka Paalanen98262232011-12-01 10:42:22 +02001369 if (reset_shell_surface_type(surface))
1370 return;
1371
Pekka Paalanen77346a62011-11-30 16:26:35 +02001372 surface->type = SHELL_SURFACE_SCREENSAVER;
1373
1374 surface->surface->fullscreen_output = output;
1375 surface->output = output;
1376 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001377}
1378
1379static const struct screensaver_interface screensaver_implementation = {
1380 screensaver_set_surface
1381};
1382
1383static void
1384unbind_screensaver(struct wl_resource *resource)
1385{
1386 struct wl_shell *shell = resource->data;
1387
Pekka Paalanen77346a62011-11-30 16:26:35 +02001388 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001389 free(resource);
1390}
1391
1392static void
1393bind_screensaver(struct wl_client *client,
1394 void *data, uint32_t version, uint32_t id)
1395{
1396 struct wl_shell *shell = data;
1397 struct wl_resource *resource;
1398
1399 resource = wl_client_add_object(client, &screensaver_interface,
1400 &screensaver_implementation,
1401 id, shell);
1402
Pekka Paalanen77346a62011-11-30 16:26:35 +02001403 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001404 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001405 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001406 return;
1407 }
1408
1409 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1410 "interface object already bound");
1411 wl_resource_destroy(resource, 0);
1412}
1413
Pekka Paalanen3c647232011-12-22 13:43:43 +02001414static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001415shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001416{
1417 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1418
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001419 if (shell->child.client)
1420 wl_client_destroy(shell->child.client);
1421
Pekka Paalanen3c647232011-12-22 13:43:43 +02001422 free(shell->screensaver.path);
1423 free(shell);
1424}
1425
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001426int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001427shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001428
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001429WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001430shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001431{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001432 struct wl_shell *shell;
1433
1434 shell = malloc(sizeof *shell);
1435 if (shell == NULL)
1436 return -1;
1437
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001438 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001439 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001440 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001441 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001442 shell->shell.map = map;
1443 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001444 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001445
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001446 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001447 wl_list_init(&shell->backgrounds);
1448 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001449 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001450
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001451 if (shell_configuration(shell) < 0)
1452 return -1;
1453
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001454 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1455 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001456 return -1;
1457
Kristian Høgsberg75840622011-09-06 13:48:16 -04001458 if (wl_display_add_global(ec->wl_display,
1459 &desktop_shell_interface,
1460 shell, bind_desktop_shell) == NULL)
1461 return -1;
1462
Pekka Paalanen6e168112011-11-24 11:34:05 +02001463 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1464 shell, bind_screensaver) == NULL)
1465 return -1;
1466
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001467 if (launch_desktop_shell_process(shell) != 0)
1468 return -1;
1469
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001471 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001472 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001473 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001474 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001475 MODIFIER_CTRL | MODIFIER_ALT,
1476 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001477 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001478 click_to_activate_binding, ec);
1479
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001480 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001481
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001482 return 0;
1483}