blob: b27067dccc00625698f2f84f7a45c0c3704559c6 [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 Paalanen6cd281a2011-11-03 14:11:32 +020048 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020049
50 bool locked;
51 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020052
Pekka Paalanen068ae942011-11-28 14:11:15 +020053 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050054 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020055 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010056
57 struct wl_list backgrounds;
58 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020059
Pekka Paalanen77346a62011-11-30 16:26:35 +020060 struct {
Pekka Paalanen3c647232011-12-22 13:43:43 +020061 char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020062 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020063 struct wl_resource *binding;
64 struct wl_list surfaces;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050065 struct weston_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020066 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040067};
68
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050069enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020070 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050071
Pekka Paalanen57da4a82011-11-23 16:42:16 +020072 SHELL_SURFACE_PANEL,
73 SHELL_SURFACE_BACKGROUND,
74 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050076
77 SHELL_SURFACE_TOPLEVEL,
78 SHELL_SURFACE_TRANSIENT,
79 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020080};
81
Pekka Paalanen56cdea92011-11-23 16:14:12 +020082struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020083 struct wl_resource resource;
84
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050085 struct weston_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020086 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020087
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050088 enum shell_surface_type type;
89 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010090
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050091 struct weston_output *output;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010092 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020093};
94
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050095struct weston_move_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050096 struct wl_grab grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097 struct weston_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050098 int32_t dx, dy;
99};
100
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200101static int
102shell_configuration(struct wl_shell *shell)
103{
104 int ret;
105 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200106 char *path = NULL;
107 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200108
109 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200110 { "path", CONFIG_KEY_STRING, &path },
111 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200112 };
113
114 struct config_section cs[] = {
115 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
116 };
117
Kristian Høgsberg9724b512012-01-03 14:35:49 -0500118 config_file = config_file_path("weston-desktop-shell.ini");
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200119 ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
120 free(config_file);
121
Pekka Paalanen7296e792011-12-07 16:22:00 +0200122 shell->screensaver.path = path;
123 shell->screensaver.duration = duration;
124
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200125 return ret;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200126}
127
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200128static void
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500129noop_grab_focus(struct wl_grab *grab, uint32_t time,
130 struct wl_surface *surface, int32_t x, int32_t y)
131{
132 grab->focus = NULL;
133}
134
135static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136move_grab_motion(struct wl_grab *grab,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500137 uint32_t time, int32_t x, int32_t y)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500138{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500139 struct weston_move_grab *move = (struct weston_move_grab *) grab;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500140 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500141 struct weston_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500142
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500143 weston_surface_configure(es,
144 device->x + move->dx,
145 device->y + move->dy,
146 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500147}
148
149static void
150move_grab_button(struct wl_grab *grab,
151 uint32_t time, int32_t button, int32_t state)
152{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500153 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500154
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500155 if (device->button_count == 0 && state == 0) {
156 wl_input_device_end_grab(device, time);
157 free(grab);
158 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500159}
160
161static const struct wl_grab_interface move_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500162 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500163 move_grab_motion,
164 move_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165};
166
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400167static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500168weston_surface_move(struct weston_surface *es,
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500169 struct weston_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500170{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500171 struct weston_move_grab *move;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172
173 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400174 if (!move)
175 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500176
177 move->grab.interface = &move_grab_interface;
178 move->dx = es->x - wd->input_device.grab_x;
179 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500180 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500181
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500182 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500183
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400184 wl_input_device_set_pointer_focus(&wd->input_device,
185 NULL, time, 0, 0, 0, 0);
186
187 return 0;
188}
189
190static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200191shell_surface_move(struct wl_client *client, struct wl_resource *resource,
192 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400193{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500194 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200195 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400196
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500197 if (wd->input_device.button_count == 0 ||
198 wd->input_device.grab_time != time ||
199 wd->input_device.pointer_focus != &shsurf->surface->surface)
200 return;
201
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500202 if (weston_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400203 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204}
205
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500206struct weston_resize_grab {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207 struct wl_grab grab;
208 uint32_t edges;
209 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200210 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500211};
212
213static void
214resize_grab_motion(struct wl_grab *grab,
215 uint32_t time, int32_t x, int32_t y)
216{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500217 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500219 int32_t width, height;
220
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200221 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500222 width = device->grab_x - device->x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200223 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500224 width = device->x - device->grab_x + resize->width;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225 } else {
226 width = resize->width;
227 }
228
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200229 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500230 height = device->grab_y - device->y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200231 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500232 height = device->y - device->grab_y + resize->height;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500233 } else {
234 height = resize->height;
235 }
236
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200237 wl_resource_post_event(&resize->shsurf->resource,
238 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
239 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500240}
241
242static void
243resize_grab_button(struct wl_grab *grab,
244 uint32_t time, int32_t button, int32_t state)
245{
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500246 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500247
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500248 if (device->button_count == 0 && state == 0) {
249 wl_input_device_end_grab(device, time);
250 free(grab);
251 }
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252}
253
254static const struct wl_grab_interface resize_grab_interface = {
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500255 noop_grab_focus,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256 resize_grab_motion,
257 resize_grab_button,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258};
259
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400260static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500261weston_surface_resize(struct shell_surface *shsurf,
262 struct weston_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200263 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500264{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500265 struct weston_resize_grab *resize;
266 struct weston_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500267
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500268 /* FIXME: Reject if fullscreen */
269
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400271 if (!resize)
272 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500273
274 resize->grab.interface = &resize_grab_interface;
275 resize->edges = edges;
276 resize->dx = es->x - wd->input_device.grab_x;
277 resize->dy = es->y - wd->input_device.grab_y;
278 resize->width = es->width;
279 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200280 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400281
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500282 if (edges == 0 || edges > 15 ||
283 (edges & 3) == 3 || (edges & 12) == 12)
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200284 goto err_out;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500285
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500286 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500287
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400288 wl_input_device_set_pointer_focus(&wd->input_device,
289 NULL, time, 0, 0, 0, 0);
290
291 return 0;
Pekka Paalanen61b5c672012-01-04 14:09:05 +0200292
293err_out:
294 free(resize);
295 return 0;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400296}
297
298static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200299shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
300 struct wl_resource *input_resource, uint32_t time,
301 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400302{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500303 struct weston_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200304 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400305
306 /* FIXME: Reject if fullscreen */
307
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500308 if (wd->input_device.button_count == 0 ||
309 wd->input_device.grab_time != time ||
310 wd->input_device.pointer_focus != &shsurf->surface->surface)
311 return;
312
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500313 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400314 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315}
316
Pekka Paalanen98262232011-12-01 10:42:22 +0200317static int
318reset_shell_surface_type(struct shell_surface *surface)
319{
320 switch (surface->type) {
321 case SHELL_SURFACE_FULLSCREEN:
322 surface->surface->x = surface->saved_x;
323 surface->surface->y = surface->saved_y;
324 surface->surface->fullscreen_output = NULL;
325 break;
326 case SHELL_SURFACE_PANEL:
327 case SHELL_SURFACE_BACKGROUND:
328 wl_list_remove(&surface->link);
329 wl_list_init(&surface->link);
330 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200331 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200332 case SHELL_SURFACE_LOCK:
333 wl_resource_post_error(&surface->resource,
334 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200335 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200336 return -1;
337 case SHELL_SURFACE_NONE:
338 case SHELL_SURFACE_TOPLEVEL:
339 case SHELL_SURFACE_TRANSIENT:
340 break;
341 }
342
343 surface->type = SHELL_SURFACE_NONE;
344 return 0;
345}
346
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500347static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200348shell_surface_set_toplevel(struct wl_client *client,
349 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400350
351{
Pekka Paalanen98262232011-12-01 10:42:22 +0200352 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400353
Pekka Paalanen98262232011-12-01 10:42:22 +0200354 if (reset_shell_surface_type(surface))
355 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400356
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500357 weston_surface_damage(surface->surface);
Pekka Paalanen98262232011-12-01 10:42:22 +0200358 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400359}
360
361static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200362shell_surface_set_transient(struct wl_client *client,
363 struct wl_resource *resource,
364 struct wl_resource *parent_resource,
365 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400366{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200367 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500368 struct weston_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200369 struct shell_surface *pshsurf = parent_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500370 struct weston_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400371
Pekka Paalanen98262232011-12-01 10:42:22 +0200372 if (reset_shell_surface_type(shsurf))
373 return;
374
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400375 /* assign to parents output */
376 es->output = pes->output;
377
378 es->x = pes->x + x;
379 es->y = pes->y + y;
380
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500381 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200382 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400383}
384
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500385static struct weston_output *
386get_default_output(struct weston_compositor *compositor)
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200387{
388 return container_of(compositor->output_list.next,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500389 struct weston_output, link);
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200390}
391
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400392static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200393shell_surface_set_fullscreen(struct wl_client *client,
394 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400395
396{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200397 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500398 struct weston_surface *es = shsurf->surface;
399 struct weston_output *output;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400400
Pekka Paalanen98262232011-12-01 10:42:22 +0200401 if (reset_shell_surface_type(shsurf))
402 return;
403
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400404 /* FIXME: Fullscreen on first output */
405 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200406 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400407 es->output = output;
408
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200409 shsurf->saved_x = es->x;
410 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400411 es->x = (output->current->width - es->width) / 2;
412 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400413 es->fullscreen_output = output;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500414 weston_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200415 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400416}
417
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200418static const struct wl_shell_surface_interface shell_surface_implementation = {
419 shell_surface_move,
420 shell_surface_resize,
421 shell_surface_set_toplevel,
422 shell_surface_set_transient,
423 shell_surface_set_fullscreen
424};
425
426static void
427destroy_shell_surface(struct wl_resource *resource)
428{
429 struct shell_surface *shsurf = resource->data;
430
431 /* in case cleaning up a dead client destroys shell_surface first */
432 if (shsurf->surface)
433 wl_list_remove(&shsurf->surface_destroy_listener.link);
434
435 wl_list_remove(&shsurf->link);
436 free(shsurf);
437}
438
439static void
440shell_handle_surface_destroy(struct wl_listener *listener,
441 struct wl_resource *resource, uint32_t time)
442{
443 struct shell_surface *shsurf = container_of(listener,
444 struct shell_surface,
445 surface_destroy_listener);
446
447 shsurf->surface = NULL;
448 wl_resource_destroy(&shsurf->resource, time);
449}
450
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200451static struct shell_surface *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500452get_shell_surface(struct weston_surface *surface)
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200453{
454 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
455 struct wl_listener *listener;
456
457 /* search the destroy listener list for our callback */
458 wl_list_for_each(listener, lst, link) {
459 if (listener->func == shell_handle_surface_destroy) {
460 return container_of(listener, struct shell_surface,
461 surface_destroy_listener);
462 }
463 }
464
465 return NULL;
466}
467
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200468static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200469shell_get_shell_surface(struct wl_client *client,
470 struct wl_resource *resource,
471 uint32_t id,
472 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200473{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500474 struct weston_surface *surface = surface_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200475 struct shell_surface *shsurf;
476
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200477 if (get_shell_surface(surface)) {
478 wl_resource_post_error(surface_resource,
479 WL_DISPLAY_ERROR_INVALID_OBJECT,
480 "wl_shell::get_shell_surface already requested");
481 return;
482 }
483
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200484 shsurf = calloc(1, sizeof *shsurf);
485 if (!shsurf) {
486 wl_resource_post_no_memory(resource);
487 return;
488 }
489
490 shsurf->resource.destroy = destroy_shell_surface;
491 shsurf->resource.object.id = id;
492 shsurf->resource.object.interface = &wl_shell_surface_interface;
493 shsurf->resource.object.implementation =
494 (void (**)(void)) &shell_surface_implementation;
495 shsurf->resource.data = shsurf;
496
497 shsurf->surface = surface;
498 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
499 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
500 &shsurf->surface_destroy_listener.link);
501
502 /* init link so its safe to always remove it in destroy_shell_surface */
503 wl_list_init(&shsurf->link);
504
Pekka Paalanen98262232011-12-01 10:42:22 +0200505 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200506
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200507 wl_client_add_resource(client, &shsurf->resource);
508}
509
510static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200511 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500512};
513
Kristian Høgsberg07937562011-04-12 17:25:42 -0400514static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500515handle_screensaver_sigchld(struct weston_process *proc, int status)
Pekka Paalanen18027e52011-12-02 16:31:49 +0200516{
517 proc->pid = 0;
518}
519
520static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200521launch_screensaver(struct wl_shell *shell)
522{
523 if (shell->screensaver.binding)
524 return;
525
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200526 if (!shell->screensaver.path)
527 return;
528
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500529 weston_client_launch(shell->compositor,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200530 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200531 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200532 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200533}
534
535static void
536terminate_screensaver(struct wl_shell *shell)
537{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200538 if (shell->screensaver.process.pid == 0)
539 return;
540
541 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200542}
543
544static void
545show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
546{
547 struct wl_list *list;
548
549 if (shell->lock_surface)
550 list = &shell->lock_surface->surface->link;
551 else
552 list = &shell->compositor->surface_list;
553
554 wl_list_remove(&surface->surface->link);
555 wl_list_insert(list, &surface->surface->link);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500556 weston_surface_configure(surface->surface,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200557 surface->surface->x,
558 surface->surface->y,
559 surface->surface->width,
560 surface->surface->height);
561 surface->surface->output = surface->output;
562}
563
564static void
565hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
566{
567 wl_list_remove(&surface->surface->link);
568 wl_list_init(&surface->surface->link);
569 surface->surface->output = NULL;
570}
571
572static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400573desktop_shell_set_background(struct wl_client *client,
574 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100575 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400576 struct wl_resource *surface_resource)
577{
578 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200579 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500580 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200581 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400582
Pekka Paalanen98262232011-12-01 10:42:22 +0200583 if (reset_shell_surface_type(shsurf))
584 return;
585
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100586 wl_list_for_each(priv, &shell->backgrounds, link) {
587 if (priv->output == output_resource->data) {
588 priv->surface->output = NULL;
589 wl_list_remove(&priv->surface->link);
590 wl_list_remove(&priv->link);
591 break;
592 }
593 }
594
Pekka Paalanen068ae942011-11-28 14:11:15 +0200595 shsurf->type = SHELL_SURFACE_BACKGROUND;
596 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100597
Pekka Paalanen068ae942011-11-28 14:11:15 +0200598 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100599
Pekka Paalanen068ae942011-11-28 14:11:15 +0200600 surface->x = shsurf->output->x;
601 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200602
Kristian Høgsberg75840622011-09-06 13:48:16 -0400603 wl_resource_post_event(resource,
604 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500605 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200606 shsurf->output->current->width,
607 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400608}
609
610static void
611desktop_shell_set_panel(struct wl_client *client,
612 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100613 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400614 struct wl_resource *surface_resource)
615{
616 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200617 struct shell_surface *shsurf = surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500618 struct weston_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200619 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400620
Pekka Paalanen98262232011-12-01 10:42:22 +0200621 if (reset_shell_surface_type(shsurf))
622 return;
623
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100624 wl_list_for_each(priv, &shell->panels, link) {
625 if (priv->output == output_resource->data) {
626 priv->surface->output = NULL;
627 wl_list_remove(&priv->surface->link);
628 wl_list_remove(&priv->link);
629 break;
630 }
631 }
632
Pekka Paalanen068ae942011-11-28 14:11:15 +0200633 shsurf->type = SHELL_SURFACE_PANEL;
634 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100635
Pekka Paalanen068ae942011-11-28 14:11:15 +0200636 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100637
Pekka Paalanen068ae942011-11-28 14:11:15 +0200638 surface->x = shsurf->output->x;
639 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200640
Kristian Høgsberg75840622011-09-06 13:48:16 -0400641 wl_resource_post_event(resource,
642 DESKTOP_SHELL_CONFIGURE,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500643 weston_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200644 shsurf->output->current->width,
645 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400646}
647
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200648static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500649handle_lock_surface_destroy(struct wl_listener *listener,
650 struct wl_resource *resource, uint32_t time)
651{
652 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200653 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500654
655 fprintf(stderr, "lock surface gone\n");
656 shell->lock_surface = NULL;
657}
658
659static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200660desktop_shell_set_lock_surface(struct wl_client *client,
661 struct wl_resource *resource,
662 struct wl_resource *surface_resource)
663{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200664 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200665 struct shell_surface *surface = surface_resource->data;
666
667 if (reset_shell_surface_type(surface))
668 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200669
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200670 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200671
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200672 if (!shell->locked)
673 return;
674
Pekka Paalanen98262232011-12-01 10:42:22 +0200675 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200676
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500677 shell->lock_surface_listener.func = handle_lock_surface_destroy;
678 wl_list_insert(&surface_resource->destroy_listener_list,
679 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200680
Pekka Paalanen068ae942011-11-28 14:11:15 +0200681 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200682}
683
684static void
685resume_desktop(struct wl_shell *shell)
686{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500687 struct weston_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200688 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200689 struct shell_surface *tmp;
690
691 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
692 hide_screensaver(shell, tmp);
693
694 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200695
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500696 wl_list_for_each(surface, &shell->hidden_surface_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500697 weston_surface_configure(surface, surface->x, surface->y,
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500698 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200699
Pekka Paalanenfe340832011-11-25 16:07:52 +0200700 if (wl_list_empty(&shell->backgrounds)) {
701 list = &shell->compositor->surface_list;
702 } else {
703 struct shell_surface *background;
704 background = container_of(shell->backgrounds.prev,
705 struct shell_surface, link);
706 list = background->surface->link.prev;
707 }
708
709 if (!wl_list_empty(&shell->hidden_surface_list))
710 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500711 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200712
713 shell->locked = false;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500714 weston_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200715 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500716 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200717}
718
719static void
720desktop_shell_unlock(struct wl_client *client,
721 struct wl_resource *resource)
722{
723 struct wl_shell *shell = resource->data;
724
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200725 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200726
727 if (shell->locked)
728 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200729}
730
Kristian Høgsberg75840622011-09-06 13:48:16 -0400731static const struct desktop_shell_interface desktop_shell_implementation = {
732 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200733 desktop_shell_set_panel,
734 desktop_shell_set_lock_surface,
735 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400736};
737
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200738static enum shell_surface_type
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500739get_shell_surface_type(struct weston_surface *surface)
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200740{
741 struct shell_surface *shsurf;
742
743 shsurf = get_shell_surface(surface);
744 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200745 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200746 return shsurf->type;
747}
748
Kristian Høgsberg75840622011-09-06 13:48:16 -0400749static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400750move_binding(struct wl_input_device *device, uint32_t time,
751 uint32_t key, uint32_t button, uint32_t state, void *data)
752{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500753 struct weston_surface *surface =
754 (struct weston_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500755
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100756 if (surface == NULL)
757 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400758
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200759 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100760 case SHELL_SURFACE_PANEL:
761 case SHELL_SURFACE_BACKGROUND:
762 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200763 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100764 return;
765 default:
766 break;
767 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400768
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500769 weston_surface_move(surface, (struct weston_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400770}
771
772static void
773resize_binding(struct wl_input_device *device, uint32_t time,
774 uint32_t key, uint32_t button, uint32_t state, void *data)
775{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500776 struct weston_surface *surface =
777 (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400778 uint32_t edges = 0;
779 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200780 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500781
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100782 if (surface == NULL)
783 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200784
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200785 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200786 if (!shsurf)
787 return;
788
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200789 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100790 case SHELL_SURFACE_PANEL:
791 case SHELL_SURFACE_BACKGROUND:
792 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200793 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100794 return;
795 default:
796 break;
797 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400798
Kristian Høgsberg07937562011-04-12 17:25:42 -0400799 x = device->grab_x - surface->x;
800 y = device->grab_y - surface->y;
801
802 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200803 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400804 else if (x < 2 * surface->width / 3)
805 edges |= 0;
806 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200807 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400808
809 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200810 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400811 else if (y < 2 * surface->height / 3)
812 edges |= 0;
813 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200814 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400815
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500816 weston_surface_resize(shsurf, (struct weston_input_device *) device,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200817 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400818}
819
820static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500821terminate_binding(struct wl_input_device *device, uint32_t time,
822 uint32_t key, uint32_t button, uint32_t state, void *data)
823{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500824 struct weston_compositor *compositor = data;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500825
826 if (state)
827 wl_display_terminate(compositor->wl_display);
828}
829
830static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500831activate(struct weston_shell *base, struct weston_surface *es,
832 struct weston_input_device *device, uint32_t time)
Kristian Høgsberg75840622011-09-06 13:48:16 -0400833{
834 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400836
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500837 weston_surface_activate(es, device, time);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400838
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400839 if (compositor->wxs)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500840 weston_xserver_surface_activate(es);
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400841
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200842 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200843 case SHELL_SURFACE_BACKGROUND:
844 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400845 wl_list_remove(&es->link);
846 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200847 break;
848 case SHELL_SURFACE_PANEL:
849 /* already put on top */
850 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200851 case SHELL_SURFACE_SCREENSAVER:
852 /* always below lock surface */
853 if (shell->lock_surface) {
854 wl_list_remove(&es->link);
855 wl_list_insert(&shell->lock_surface->surface->link,
856 &es->link);
857 }
858 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200859 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100860 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200861 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100862 struct shell_surface *panel;
863 wl_list_for_each(panel, &shell->panels, link) {
864 wl_list_remove(&panel->surface->link);
865 wl_list_insert(&compositor->surface_list,
866 &panel->surface->link);
867 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200868 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400869 }
870}
871
872static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500873click_to_activate_binding(struct wl_input_device *device,
874 uint32_t time, uint32_t key,
875 uint32_t button, uint32_t state, void *data)
876{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500877 struct weston_input_device *wd = (struct weston_input_device *) device;
878 struct weston_compositor *compositor = data;
879 struct weston_surface *focus;
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500880
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500881 focus = (struct weston_surface *) device->pointer_focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500882 if (state && focus && device->grab == &device->default_grab)
Kristian Høgsberg2a25cd42011-12-19 15:19:54 -0500883 activate(compositor->shell, focus, wd, time);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500884}
885
886static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500887lock(struct weston_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400888{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200889 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200890 struct wl_list *surface_list = &shell->compositor->surface_list;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500891 struct weston_surface *cur;
892 struct weston_surface *tmp;
893 struct weston_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200894 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200895 uint32_t time;
896
897 if (shell->locked)
898 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200899
900 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200901
902 /* Move all surfaces from compositor's list to our hidden list,
903 * except the background. This way nothing else can show or
904 * receive input events while we are locked. */
905
906 if (!wl_list_empty(&shell->hidden_surface_list)) {
907 fprintf(stderr,
908 "%s: Assertion failed: hidden_surface_list is not empty.\n",
909 __func__);
910 }
911
912 wl_list_for_each_safe(cur, tmp, surface_list, link) {
913 /* skip input device sprites, cur->surface is uninitialised */
914 if (cur->surface.resource.client == NULL)
915 continue;
916
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200917 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200918 continue;
919
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500920 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200921 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500922 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200923 }
924
Pekka Paalanen77346a62011-11-30 16:26:35 +0200925 launch_screensaver(shell);
926
927 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
928 show_screensaver(shell, shsurf);
929
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200930 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200931 shell->compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500932 weston_compositor_wake(shell->compositor);
933 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200934 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200935
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200936 /* reset pointer foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 weston_compositor_repick(shell->compositor);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200938
939 /* reset keyboard foci */
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500940 time = weston_compositor_get_time();
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200941 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
942 wl_input_device_set_keyboard_focus(&device->input_device,
943 NULL, time);
944 }
945
946 /* TODO: disable bindings that should not work while locked. */
947
948 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200949}
950
951static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952unlock(struct weston_shell *base)
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200953{
954 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
955
Pekka Paalanend81c2162011-11-16 13:47:34 +0200956 if (!shell->locked || shell->lock_surface) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 weston_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200958 return;
959 }
960
961 /* If desktop-shell client has gone away, unlock immediately. */
962 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200963 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200964 return;
965 }
966
967 if (shell->prepare_event_sent)
968 return;
969
970 wl_resource_post_event(shell->child.desktop_shell,
971 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
972 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400973}
974
975static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500976center_on_output(struct weston_surface *surface, struct weston_output *output)
Pekka Paalanen77346a62011-11-30 16:26:35 +0200977{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500978 struct weston_mode *mode = output->current;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200979
980 surface->x = output->x + (mode->width - surface->width) / 2;
981 surface->y = output->y + (mode->height - surface->height) / 2;
982}
983
984static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500985map(struct weston_shell *base,
986 struct weston_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400987{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400988 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500989 struct weston_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500990 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200991 struct shell_surface *shsurf;
992 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
993 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200994
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995 shsurf = get_shell_surface(surface);
996 if (shsurf)
997 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500998
Pekka Paalanen77346a62011-11-30 16:26:35 +0200999 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001000 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001001 do_configure = 0;
1002 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001003 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001004 do_configure = 1;
1005 }
1006
1007 surface->width = width;
1008 surface->height = height;
1009
1010 /* initial positioning, see also configure() */
1011 switch (surface_type) {
1012 case SHELL_SURFACE_TOPLEVEL:
1013 surface->x = 10 + random() % 400;
1014 surface->y = 10 + random() % 400;
1015 break;
1016 case SHELL_SURFACE_SCREENSAVER:
1017 case SHELL_SURFACE_FULLSCREEN:
1018 center_on_output(surface, surface->fullscreen_output);
1019 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001020 case SHELL_SURFACE_LOCK:
1021 center_on_output(surface, get_default_output(compositor));
1022 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001023 default:
1024 ;
1025 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001026
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001027 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001028 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001029 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001030 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001031 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001032 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001033 break;
1034 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001035 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001036 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001037 break;
1038 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001039 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001040 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001041
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001042 weston_compositor_repick(compositor);
1043 weston_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001044 do_configure = 1;
1045 break;
1046 case SHELL_SURFACE_SCREENSAVER:
1047 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001048 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001049 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001050 compositor->idle_time = shell->screensaver.duration;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051 weston_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001052 if (!shell->lock_surface)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001053 compositor->state = WESTON_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001054 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001055 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001056 break;
1057 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001058 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001059 if (!wl_list_empty(&shell->panels)) {
1060 struct shell_surface *panel =
1061 container_of(shell->panels.prev,
1062 struct shell_surface, link);
1063 wl_list_insert(&panel->surface->link, &surface->link);
1064 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001065 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001066 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001067 }
1068
Pekka Paalanen77346a62011-11-30 16:26:35 +02001069 if (do_configure)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001070 weston_surface_configure(surface,
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001071 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001072
Juan Zhao7bb92f02011-12-15 11:31:51 -05001073 switch (surface_type) {
1074 case SHELL_SURFACE_TOPLEVEL:
1075 case SHELL_SURFACE_TRANSIENT:
1076 case SHELL_SURFACE_FULLSCREEN:
1077 if (!shell->locked)
1078 activate(base, surface,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001079 (struct weston_input_device *)
Juan Zhao7bb92f02011-12-15 11:31:51 -05001080 compositor->input_device,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001081 weston_compositor_get_time());
Juan Zhao7bb92f02011-12-15 11:31:51 -05001082 break;
1083 default:
1084 break;
1085 }
1086
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001087 if (surface_type == SHELL_SURFACE_TOPLEVEL)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001088 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001089}
1090
1091static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001092configure(struct weston_shell *base, struct weston_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001093 int32_t x, int32_t y, int32_t width, int32_t height)
1094{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001095 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1096 int do_configure = !shell->locked;
1097 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1098 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001099
Pekka Paalanen77346a62011-11-30 16:26:35 +02001100 shsurf = get_shell_surface(surface);
1101 if (shsurf)
1102 surface_type = shsurf->type;
1103
1104 surface->width = width;
1105 surface->height = height;
1106
1107 switch (surface_type) {
1108 case SHELL_SURFACE_SCREENSAVER:
1109 do_configure = !do_configure;
1110 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001111 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001112 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001113 break;
1114 default:
1115 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001116 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001117
Pekka Paalanen77346a62011-11-30 16:26:35 +02001118 /*
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001119 * weston_surface_configure() will assign an output, which means
Pekka Paalanen77346a62011-11-30 16:26:35 +02001120 * the surface is supposed to be in compositor->surface_list.
1121 * Be careful with that, and make sure we stay on the right output.
1122 * XXX: would a fullscreen surface need the same handling?
1123 */
1124 if (do_configure) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001125 weston_surface_configure(surface, x, y, width, height);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001126
1127 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1128 surface->output = shsurf->output;
1129 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001130}
1131
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001132static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001133desktop_shell_sigchld(struct weston_process *process, int status)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001134{
1135 struct wl_shell *shell =
1136 container_of(process, struct wl_shell, child.process);
1137
1138 shell->child.process.pid = 0;
1139 shell->child.client = NULL; /* already destroyed by wayland */
1140}
1141
1142static int
1143launch_desktop_shell_process(struct wl_shell *shell)
1144{
Kristian Høgsberg9724b512012-01-03 14:35:49 -05001145 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001146
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001147 shell->child.client = weston_client_launch(shell->compositor,
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001148 &shell->child.process,
1149 shell_exe,
1150 desktop_shell_sigchld);
1151
1152 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001153 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001154 return 0;
1155}
1156
1157static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001158bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1159{
1160 struct wl_shell *shell = data;
1161
1162 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001163 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001164}
1165
Kristian Høgsberg75840622011-09-06 13:48:16 -04001166static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001167unbind_desktop_shell(struct wl_resource *resource)
1168{
1169 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001170
1171 if (shell->locked)
1172 resume_desktop(shell);
1173
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001174 shell->child.desktop_shell = NULL;
1175 shell->prepare_event_sent = false;
1176 free(resource);
1177}
1178
1179static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001180bind_desktop_shell(struct wl_client *client,
1181 void *data, uint32_t version, uint32_t id)
1182{
1183 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001184 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001185
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001186 resource = wl_client_add_object(client, &desktop_shell_interface,
1187 &desktop_shell_implementation,
1188 id, shell);
1189
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001190 if (client == shell->child.client) {
1191 resource->destroy = unbind_desktop_shell;
1192 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001193 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001194 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001195
1196 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1197 "permission to bind desktop_shell denied");
1198 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001199}
1200
Pekka Paalanen6e168112011-11-24 11:34:05 +02001201static void
1202screensaver_set_surface(struct wl_client *client,
1203 struct wl_resource *resource,
1204 struct wl_resource *shell_surface_resource,
1205 struct wl_resource *output_resource)
1206{
1207 struct wl_shell *shell = resource->data;
1208 struct shell_surface *surface = shell_surface_resource->data;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001209 struct weston_output *output = output_resource->data;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001210
Pekka Paalanen98262232011-12-01 10:42:22 +02001211 if (reset_shell_surface_type(surface))
1212 return;
1213
Pekka Paalanen77346a62011-11-30 16:26:35 +02001214 surface->type = SHELL_SURFACE_SCREENSAVER;
1215
1216 surface->surface->fullscreen_output = output;
1217 surface->output = output;
1218 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001219}
1220
1221static const struct screensaver_interface screensaver_implementation = {
1222 screensaver_set_surface
1223};
1224
1225static void
1226unbind_screensaver(struct wl_resource *resource)
1227{
1228 struct wl_shell *shell = resource->data;
1229
Pekka Paalanen77346a62011-11-30 16:26:35 +02001230 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001231 free(resource);
1232}
1233
1234static void
1235bind_screensaver(struct wl_client *client,
1236 void *data, uint32_t version, uint32_t id)
1237{
1238 struct wl_shell *shell = data;
1239 struct wl_resource *resource;
1240
1241 resource = wl_client_add_object(client, &screensaver_interface,
1242 &screensaver_implementation,
1243 id, shell);
1244
Pekka Paalanen77346a62011-11-30 16:26:35 +02001245 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001246 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001247 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001248 return;
1249 }
1250
1251 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1252 "interface object already bound");
1253 wl_resource_destroy(resource, 0);
1254}
1255
Pekka Paalanen3c647232011-12-22 13:43:43 +02001256static void
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001257shell_destroy(struct weston_shell *base)
Pekka Paalanen3c647232011-12-22 13:43:43 +02001258{
1259 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1260
Pekka Paalanen9cf5cc82012-01-02 16:00:24 +02001261 if (shell->child.client)
1262 wl_client_destroy(shell->child.client);
1263
Pekka Paalanen3c647232011-12-22 13:43:43 +02001264 free(shell->screensaver.path);
1265 free(shell);
1266}
1267
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001268int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001269shell_init(struct weston_compositor *ec);
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001270
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001271WL_EXPORT int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001272shell_init(struct weston_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001273{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001274 struct wl_shell *shell;
1275
1276 shell = malloc(sizeof *shell);
1277 if (shell == NULL)
1278 return -1;
1279
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001280 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001281 shell->compositor = ec;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001282 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001283 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001284 shell->shell.map = map;
1285 shell->shell.configure = configure;
Pekka Paalanen3c647232011-12-22 13:43:43 +02001286 shell->shell.destroy = shell_destroy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001287
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001288 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001289 wl_list_init(&shell->backgrounds);
1290 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001291 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001292
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001293 if (shell_configuration(shell) < 0)
1294 return -1;
1295
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001296 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1297 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001298 return -1;
1299
Kristian Høgsberg75840622011-09-06 13:48:16 -04001300 if (wl_display_add_global(ec->wl_display,
1301 &desktop_shell_interface,
1302 shell, bind_desktop_shell) == NULL)
1303 return -1;
1304
Pekka Paalanen6e168112011-11-24 11:34:05 +02001305 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1306 shell, bind_screensaver) == NULL)
1307 return -1;
1308
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001309 if (launch_desktop_shell_process(shell) != 0)
1310 return -1;
1311
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001312 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001313 move_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001314 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001315 resize_binding, shell);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001316 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001317 MODIFIER_CTRL | MODIFIER_ALT,
1318 terminate_binding, ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001320 click_to_activate_binding, ec);
1321
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001322 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001323
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001324 return 0;
1325}