blob: 99c2af5a276936b4ff0d2e0b446eac62a2f5b32e [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"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050036
Pekka Paalanen068ae942011-11-28 14:11:15 +020037struct shell_surface;
38
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040039struct wl_shell {
Kristian Høgsberg75840622011-09-06 13:48:16 -040040 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040041 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020042
43 struct {
44 struct wlsc_process process;
45 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020046 struct wl_resource *desktop_shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020047 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020048
49 bool locked;
50 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020051
Pekka Paalanen068ae942011-11-28 14:11:15 +020052 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050053 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020054 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010055
56 struct wl_list backgrounds;
57 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020058
Pekka Paalanen77346a62011-11-30 16:26:35 +020059 struct {
60 struct wl_resource *binding;
61 struct wl_list surfaces;
Pekka Paalanen18027e52011-12-02 16:31:49 +020062 struct wlsc_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020063 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040064};
65
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020067 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068
Pekka Paalanen57da4a82011-11-23 16:42:16 +020069 SHELL_SURFACE_PANEL,
70 SHELL_SURFACE_BACKGROUND,
71 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020072 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050073
74 SHELL_SURFACE_TOPLEVEL,
75 SHELL_SURFACE_TRANSIENT,
76 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020077};
78
Pekka Paalanen56cdea92011-11-23 16:14:12 +020079struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020080 struct wl_resource resource;
81
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010082 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020083 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020084
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050085 enum shell_surface_type type;
86 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010087
88 struct wlsc_output *output;
89 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020090};
91
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050092struct wlsc_move_grab {
93 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050094 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050095 int32_t dx, dy;
96};
97
Pekka Paalanen56cdea92011-11-23 16:14:12 +020098static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050099move_grab_motion(struct wl_grab *grab,
100 uint32_t time, int32_t x, int32_t y)
101{
102 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500103 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500104
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400105 wlsc_surface_configure(es, x + move->dx, y + move->dy,
106 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500107}
108
109static void
110move_grab_button(struct wl_grab *grab,
111 uint32_t time, int32_t button, int32_t state)
112{
113}
114
115static void
116move_grab_end(struct wl_grab *grab, uint32_t time)
117{
118 free(grab);
119}
120
121static const struct wl_grab_interface move_grab_interface = {
122 move_grab_motion,
123 move_grab_button,
124 move_grab_end
125};
126
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400127static int
128wlsc_surface_move(struct wlsc_surface *es,
129 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500130{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500131 struct wlsc_move_grab *move;
132
133 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400134 if (!move)
135 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136
137 move->grab.interface = &move_grab_interface;
138 move->dx = es->x - wd->input_device.grab_x;
139 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500140 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500141
142 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400143 &move->grab, &es->surface, time) < 0)
144 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500145
146 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400147 wl_input_device_set_pointer_focus(&wd->input_device,
148 NULL, time, 0, 0, 0, 0);
149
150 return 0;
151}
152
153static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200154shell_surface_move(struct wl_client *client, struct wl_resource *resource,
155 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400156{
157 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200158 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400159
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200160 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400161 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162}
163
164struct wlsc_resize_grab {
165 struct wl_grab grab;
166 uint32_t edges;
167 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200168 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500169};
170
171static void
172resize_grab_motion(struct wl_grab *grab,
173 uint32_t time, int32_t x, int32_t y)
174{
175 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
176 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177 int32_t width, height;
178
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200179 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200181 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500182 width = x - device->grab_x + resize->width;
183 } else {
184 width = resize->width;
185 }
186
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200189 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190 height = y - device->grab_y + resize->height;
191 } else {
192 height = resize->height;
193 }
194
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200195 wl_resource_post_event(&resize->shsurf->resource,
196 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
197 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500198}
199
200static void
201resize_grab_button(struct wl_grab *grab,
202 uint32_t time, int32_t button, int32_t state)
203{
204}
205
206static void
207resize_grab_end(struct wl_grab *grab, uint32_t time)
208{
209 free(grab);
210}
211
212static const struct wl_grab_interface resize_grab_interface = {
213 resize_grab_motion,
214 resize_grab_button,
215 resize_grab_end
216};
217
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400218static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400220 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200221 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500222{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200224 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500226
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500227 /* FIXME: Reject if fullscreen */
228
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500229 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400230 if (!resize)
231 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500232
233 resize->grab.interface = &resize_grab_interface;
234 resize->edges = edges;
235 resize->dx = es->x - wd->input_device.grab_x;
236 resize->dy = es->y - wd->input_device.grab_y;
237 resize->width = es->width;
238 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200239 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400240
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500241 if (edges == 0 || edges > 15 ||
242 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400243 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244
245 switch (edges) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200246 case WL_SHELL_SURFACE_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500247 pointer = WLSC_POINTER_TOP;
248 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200249 case WL_SHELL_SURFACE_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500250 pointer = WLSC_POINTER_BOTTOM;
251 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200252 case WL_SHELL_SURFACE_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253 pointer = WLSC_POINTER_LEFT;
254 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200255 case WL_SHELL_SURFACE_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256 pointer = WLSC_POINTER_TOP_LEFT;
257 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200258 case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500259 pointer = WLSC_POINTER_BOTTOM_LEFT;
260 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200261 case WL_SHELL_SURFACE_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500262 pointer = WLSC_POINTER_RIGHT;
263 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200264 case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265 pointer = WLSC_POINTER_TOP_RIGHT;
266 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200267 case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268 pointer = WLSC_POINTER_BOTTOM_RIGHT;
269 break;
270 }
271
272 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400273 &resize->grab, &es->surface, time) < 0)
274 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500275
276 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400277 wl_input_device_set_pointer_focus(&wd->input_device,
278 NULL, time, 0, 0, 0, 0);
279
280 return 0;
281}
282
283static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200284shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
285 struct wl_resource *input_resource, uint32_t time,
286 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400287{
288 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200289 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400290
291 /* FIXME: Reject if fullscreen */
292
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200293 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400294 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500295}
296
Pekka Paalanen98262232011-12-01 10:42:22 +0200297static int
298reset_shell_surface_type(struct shell_surface *surface)
299{
300 switch (surface->type) {
301 case SHELL_SURFACE_FULLSCREEN:
302 surface->surface->x = surface->saved_x;
303 surface->surface->y = surface->saved_y;
304 surface->surface->fullscreen_output = NULL;
305 break;
306 case SHELL_SURFACE_PANEL:
307 case SHELL_SURFACE_BACKGROUND:
308 wl_list_remove(&surface->link);
309 wl_list_init(&surface->link);
310 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200311 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200312 case SHELL_SURFACE_LOCK:
313 wl_resource_post_error(&surface->resource,
314 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200315 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200316 return -1;
317 case SHELL_SURFACE_NONE:
318 case SHELL_SURFACE_TOPLEVEL:
319 case SHELL_SURFACE_TRANSIENT:
320 break;
321 }
322
323 surface->type = SHELL_SURFACE_NONE;
324 return 0;
325}
326
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500327static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200328shell_surface_set_toplevel(struct wl_client *client,
329 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400330
331{
Pekka Paalanen98262232011-12-01 10:42:22 +0200332 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400333
Pekka Paalanen98262232011-12-01 10:42:22 +0200334 if (reset_shell_surface_type(surface))
335 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400336
Pekka Paalanen98262232011-12-01 10:42:22 +0200337 wlsc_surface_damage(surface->surface);
338 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400339}
340
341static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200342shell_surface_set_transient(struct wl_client *client,
343 struct wl_resource *resource,
344 struct wl_resource *parent_resource,
345 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400346{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200347 struct shell_surface *shsurf = resource->data;
348 struct wlsc_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200349 struct shell_surface *pshsurf = parent_resource->data;
350 struct wlsc_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400351
Pekka Paalanen98262232011-12-01 10:42:22 +0200352 if (reset_shell_surface_type(shsurf))
353 return;
354
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400355 /* assign to parents output */
356 es->output = pes->output;
357
358 es->x = pes->x + x;
359 es->y = pes->y + y;
360
361 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200362 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400363}
364
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200365static struct wlsc_output *
366get_default_output(struct wlsc_compositor *compositor)
367{
368 return container_of(compositor->output_list.next,
369 struct wlsc_output, link);
370}
371
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400372static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200373shell_surface_set_fullscreen(struct wl_client *client,
374 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400375
376{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200377 struct shell_surface *shsurf = resource->data;
378 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400379 struct wlsc_output *output;
380
Pekka Paalanen98262232011-12-01 10:42:22 +0200381 if (reset_shell_surface_type(shsurf))
382 return;
383
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400384 /* FIXME: Fullscreen on first output */
385 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200386 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400387 es->output = output;
388
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200389 shsurf->saved_x = es->x;
390 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400391 es->x = (output->current->width - es->width) / 2;
392 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400393 es->fullscreen_output = output;
394 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200395 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396}
397
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200398static const struct wl_shell_surface_interface shell_surface_implementation = {
399 shell_surface_move,
400 shell_surface_resize,
401 shell_surface_set_toplevel,
402 shell_surface_set_transient,
403 shell_surface_set_fullscreen
404};
405
406static void
407destroy_shell_surface(struct wl_resource *resource)
408{
409 struct shell_surface *shsurf = resource->data;
410
411 /* in case cleaning up a dead client destroys shell_surface first */
412 if (shsurf->surface)
413 wl_list_remove(&shsurf->surface_destroy_listener.link);
414
415 wl_list_remove(&shsurf->link);
416 free(shsurf);
417}
418
419static void
420shell_handle_surface_destroy(struct wl_listener *listener,
421 struct wl_resource *resource, uint32_t time)
422{
423 struct shell_surface *shsurf = container_of(listener,
424 struct shell_surface,
425 surface_destroy_listener);
426
427 shsurf->surface = NULL;
428 wl_resource_destroy(&shsurf->resource, time);
429}
430
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200431static struct shell_surface *
432get_shell_surface(struct wlsc_surface *surface)
433{
434 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
435 struct wl_listener *listener;
436
437 /* search the destroy listener list for our callback */
438 wl_list_for_each(listener, lst, link) {
439 if (listener->func == shell_handle_surface_destroy) {
440 return container_of(listener, struct shell_surface,
441 surface_destroy_listener);
442 }
443 }
444
445 return NULL;
446}
447
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200448static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200449shell_get_shell_surface(struct wl_client *client,
450 struct wl_resource *resource,
451 uint32_t id,
452 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200453{
454 struct wlsc_surface *surface = surface_resource->data;
455 struct shell_surface *shsurf;
456
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200457 if (get_shell_surface(surface)) {
458 wl_resource_post_error(surface_resource,
459 WL_DISPLAY_ERROR_INVALID_OBJECT,
460 "wl_shell::get_shell_surface already requested");
461 return;
462 }
463
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200464 shsurf = calloc(1, sizeof *shsurf);
465 if (!shsurf) {
466 wl_resource_post_no_memory(resource);
467 return;
468 }
469
470 shsurf->resource.destroy = destroy_shell_surface;
471 shsurf->resource.object.id = id;
472 shsurf->resource.object.interface = &wl_shell_surface_interface;
473 shsurf->resource.object.implementation =
474 (void (**)(void)) &shell_surface_implementation;
475 shsurf->resource.data = shsurf;
476
477 shsurf->surface = surface;
478 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
479 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
480 &shsurf->surface_destroy_listener.link);
481
482 /* init link so its safe to always remove it in destroy_shell_surface */
483 wl_list_init(&shsurf->link);
484
Pekka Paalanen98262232011-12-01 10:42:22 +0200485 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200486
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200487 wl_client_add_resource(client, &shsurf->resource);
488}
489
490static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200491 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500492};
493
Kristian Høgsberg07937562011-04-12 17:25:42 -0400494static void
Pekka Paalanen18027e52011-12-02 16:31:49 +0200495handle_screensaver_sigchld(struct wlsc_process *proc, int status)
496{
497 proc->pid = 0;
498}
499
500static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200501launch_screensaver(struct wl_shell *shell)
502{
503 if (shell->screensaver.binding)
504 return;
505
Pekka Paalanen18027e52011-12-02 16:31:49 +0200506 wlsc_client_launch(shell->compositor,
507 &shell->screensaver.process,
508 "./clients/wscreensaver",
509 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200510}
511
512static void
513terminate_screensaver(struct wl_shell *shell)
514{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200515 if (shell->screensaver.process.pid == 0)
516 return;
517
518 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200519}
520
521static void
522show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
523{
524 struct wl_list *list;
525
526 if (shell->lock_surface)
527 list = &shell->lock_surface->surface->link;
528 else
529 list = &shell->compositor->surface_list;
530
531 wl_list_remove(&surface->surface->link);
532 wl_list_insert(list, &surface->surface->link);
533 wlsc_surface_configure(surface->surface,
534 surface->surface->x,
535 surface->surface->y,
536 surface->surface->width,
537 surface->surface->height);
538 surface->surface->output = surface->output;
539}
540
541static void
542hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
543{
544 wl_list_remove(&surface->surface->link);
545 wl_list_init(&surface->surface->link);
546 surface->surface->output = NULL;
547}
548
549static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400550desktop_shell_set_background(struct wl_client *client,
551 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100552 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400553 struct wl_resource *surface_resource)
554{
555 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200556 struct shell_surface *shsurf = surface_resource->data;
557 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200558 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400559
Pekka Paalanen98262232011-12-01 10:42:22 +0200560 if (reset_shell_surface_type(shsurf))
561 return;
562
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100563 wl_list_for_each(priv, &shell->backgrounds, link) {
564 if (priv->output == output_resource->data) {
565 priv->surface->output = NULL;
566 wl_list_remove(&priv->surface->link);
567 wl_list_remove(&priv->link);
568 break;
569 }
570 }
571
Pekka Paalanen068ae942011-11-28 14:11:15 +0200572 shsurf->type = SHELL_SURFACE_BACKGROUND;
573 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100574
Pekka Paalanen068ae942011-11-28 14:11:15 +0200575 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100576
Pekka Paalanen068ae942011-11-28 14:11:15 +0200577 surface->x = shsurf->output->x;
578 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200579
Kristian Høgsberg75840622011-09-06 13:48:16 -0400580 wl_resource_post_event(resource,
581 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200582 wlsc_compositor_get_time(), 0, surface_resource,
583 shsurf->output->current->width,
584 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400585}
586
587static void
588desktop_shell_set_panel(struct wl_client *client,
589 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100590 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400591 struct wl_resource *surface_resource)
592{
593 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200594 struct shell_surface *shsurf = surface_resource->data;
595 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200596 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400597
Pekka Paalanen98262232011-12-01 10:42:22 +0200598 if (reset_shell_surface_type(shsurf))
599 return;
600
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100601 wl_list_for_each(priv, &shell->panels, link) {
602 if (priv->output == output_resource->data) {
603 priv->surface->output = NULL;
604 wl_list_remove(&priv->surface->link);
605 wl_list_remove(&priv->link);
606 break;
607 }
608 }
609
Pekka Paalanen068ae942011-11-28 14:11:15 +0200610 shsurf->type = SHELL_SURFACE_PANEL;
611 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100612
Pekka Paalanen068ae942011-11-28 14:11:15 +0200613 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100614
Pekka Paalanen068ae942011-11-28 14:11:15 +0200615 surface->x = shsurf->output->x;
616 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200617
Kristian Høgsberg75840622011-09-06 13:48:16 -0400618 wl_resource_post_event(resource,
619 DESKTOP_SHELL_CONFIGURE,
620 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200621 shsurf->output->current->width,
622 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400623}
624
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200625static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500626handle_lock_surface_destroy(struct wl_listener *listener,
627 struct wl_resource *resource, uint32_t time)
628{
629 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200630 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500631
632 fprintf(stderr, "lock surface gone\n");
633 shell->lock_surface = NULL;
634}
635
636static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200637desktop_shell_set_lock_surface(struct wl_client *client,
638 struct wl_resource *resource,
639 struct wl_resource *surface_resource)
640{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200641 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200642 struct shell_surface *surface = surface_resource->data;
643
644 if (reset_shell_surface_type(surface))
645 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200646
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200647 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200648
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200649 if (!shell->locked)
650 return;
651
Pekka Paalanen98262232011-12-01 10:42:22 +0200652 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200653
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500654 shell->lock_surface_listener.func = handle_lock_surface_destroy;
655 wl_list_insert(&surface_resource->destroy_listener_list,
656 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200657
Pekka Paalanen068ae942011-11-28 14:11:15 +0200658 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200659}
660
661static void
662resume_desktop(struct wl_shell *shell)
663{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500664 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200665 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200666 struct shell_surface *tmp;
667
668 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
669 hide_screensaver(shell, tmp);
670
671 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200672
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500673 wl_list_for_each(surface, &shell->hidden_surface_list, link)
674 wlsc_surface_configure(surface, surface->x, surface->y,
675 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200676
Pekka Paalanenfe340832011-11-25 16:07:52 +0200677 if (wl_list_empty(&shell->backgrounds)) {
678 list = &shell->compositor->surface_list;
679 } else {
680 struct shell_surface *background;
681 background = container_of(shell->backgrounds.prev,
682 struct shell_surface, link);
683 list = background->surface->link.prev;
684 }
685
686 if (!wl_list_empty(&shell->hidden_surface_list))
687 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500688 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200689
690 shell->locked = false;
691 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200692 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200693}
694
695static void
696desktop_shell_unlock(struct wl_client *client,
697 struct wl_resource *resource)
698{
699 struct wl_shell *shell = resource->data;
700
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200701 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200702
703 if (shell->locked)
704 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200705}
706
Kristian Høgsberg75840622011-09-06 13:48:16 -0400707static const struct desktop_shell_interface desktop_shell_implementation = {
708 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200709 desktop_shell_set_panel,
710 desktop_shell_set_lock_surface,
711 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400712};
713
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200714static enum shell_surface_type
715get_shell_surface_type(struct wlsc_surface *surface)
716{
717 struct shell_surface *shsurf;
718
719 shsurf = get_shell_surface(surface);
720 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200721 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200722 return shsurf->type;
723}
724
Kristian Høgsberg75840622011-09-06 13:48:16 -0400725static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400726move_binding(struct wl_input_device *device, uint32_t time,
727 uint32_t key, uint32_t button, uint32_t state, void *data)
728{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400729 struct wlsc_surface *surface =
730 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500731
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100732 if (surface == NULL)
733 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400734
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200735 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100736 case SHELL_SURFACE_PANEL:
737 case SHELL_SURFACE_BACKGROUND:
738 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200739 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100740 return;
741 default:
742 break;
743 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400744
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400745 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400746}
747
748static void
749resize_binding(struct wl_input_device *device, uint32_t time,
750 uint32_t key, uint32_t button, uint32_t state, void *data)
751{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400752 struct wlsc_surface *surface =
753 (struct wlsc_surface *) device->pointer_focus;
754 uint32_t edges = 0;
755 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200756 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500757
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100758 if (surface == NULL)
759 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200760
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200761 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200762 if (!shsurf)
763 return;
764
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200765 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100766 case SHELL_SURFACE_PANEL:
767 case SHELL_SURFACE_BACKGROUND:
768 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200769 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100770 return;
771 default:
772 break;
773 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400774
Kristian Høgsberg07937562011-04-12 17:25:42 -0400775 x = device->grab_x - surface->x;
776 y = device->grab_y - surface->y;
777
778 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200779 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400780 else if (x < 2 * surface->width / 3)
781 edges |= 0;
782 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200783 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400784
785 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200786 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400787 else if (y < 2 * surface->height / 3)
788 edges |= 0;
789 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200790 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400791
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200792 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
793 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400794}
795
796static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400797activate(struct wlsc_shell *base, struct wlsc_surface *es,
798 struct wlsc_input_device *device, uint32_t time)
799{
800 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
801 struct wlsc_compositor *compositor = shell->compositor;
802
803 wlsc_surface_activate(es, device, time);
804
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400805 if (compositor->wxs)
806 wlsc_xserver_surface_activate(es);
807
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200808 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200809 case SHELL_SURFACE_BACKGROUND:
810 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400811 wl_list_remove(&es->link);
812 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200813 break;
814 case SHELL_SURFACE_PANEL:
815 /* already put on top */
816 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200817 case SHELL_SURFACE_SCREENSAVER:
818 /* always below lock surface */
819 if (shell->lock_surface) {
820 wl_list_remove(&es->link);
821 wl_list_insert(&shell->lock_surface->surface->link,
822 &es->link);
823 }
824 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200825 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100826 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200827 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100828 struct shell_surface *panel;
829 wl_list_for_each(panel, &shell->panels, link) {
830 wl_list_remove(&panel->surface->link);
831 wl_list_insert(&compositor->surface_list,
832 &panel->surface->link);
833 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200834 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400835 }
836}
837
838static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200839lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400840{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200841 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200842 struct wl_list *surface_list = &shell->compositor->surface_list;
843 struct wlsc_surface *cur;
844 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200845 struct wlsc_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200846 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200847 uint32_t time;
848
849 if (shell->locked)
850 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200851
852 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200853
854 /* Move all surfaces from compositor's list to our hidden list,
855 * except the background. This way nothing else can show or
856 * receive input events while we are locked. */
857
858 if (!wl_list_empty(&shell->hidden_surface_list)) {
859 fprintf(stderr,
860 "%s: Assertion failed: hidden_surface_list is not empty.\n",
861 __func__);
862 }
863
864 wl_list_for_each_safe(cur, tmp, surface_list, link) {
865 /* skip input device sprites, cur->surface is uninitialised */
866 if (cur->surface.resource.client == NULL)
867 continue;
868
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200869 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200870 continue;
871
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500872 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200873 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500874 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200875 }
876
Pekka Paalanen77346a62011-11-30 16:26:35 +0200877 launch_screensaver(shell);
878
879 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
880 show_screensaver(shell, shsurf);
881
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200882 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200883 wlsc_compositor_wake(shell->compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200884 shell->compositor->state = WLSC_COMPOSITOR_IDLE;
885 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200886
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200887 /* reset pointer foci */
888 wlsc_compositor_repick(shell->compositor);
889
890 /* reset keyboard foci */
891 time = wlsc_compositor_get_time();
892 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
893 wl_input_device_set_keyboard_focus(&device->input_device,
894 NULL, time);
895 }
896
897 /* TODO: disable bindings that should not work while locked. */
898
899 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200900}
901
902static void
903unlock(struct wlsc_shell *base)
904{
905 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
906
Pekka Paalanend81c2162011-11-16 13:47:34 +0200907 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200908 wlsc_compositor_wake(shell->compositor);
909 return;
910 }
911
912 /* If desktop-shell client has gone away, unlock immediately. */
913 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200914 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200915 return;
916 }
917
918 if (shell->prepare_event_sent)
919 return;
920
921 wl_resource_post_event(shell->child.desktop_shell,
922 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
923 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400924}
925
926static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200927center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
928{
929 struct wlsc_mode *mode = output->current;
930
931 surface->x = output->x + (mode->width - surface->width) / 2;
932 surface->y = output->y + (mode->height - surface->height) / 2;
933}
934
935static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500936map(struct wlsc_shell *base,
937 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400938{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400939 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
940 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500941 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200942 struct shell_surface *shsurf;
943 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
944 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200945
Pekka Paalanen77346a62011-11-30 16:26:35 +0200946 shsurf = get_shell_surface(surface);
947 if (shsurf)
948 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500949
Pekka Paalanen77346a62011-11-30 16:26:35 +0200950 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500951 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200952 do_configure = 0;
953 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500954 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200955 do_configure = 1;
956 }
957
958 surface->width = width;
959 surface->height = height;
960
961 /* initial positioning, see also configure() */
962 switch (surface_type) {
963 case SHELL_SURFACE_TOPLEVEL:
964 surface->x = 10 + random() % 400;
965 surface->y = 10 + random() % 400;
966 break;
967 case SHELL_SURFACE_SCREENSAVER:
968 case SHELL_SURFACE_FULLSCREEN:
969 center_on_output(surface, surface->fullscreen_output);
970 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200971 case SHELL_SURFACE_LOCK:
972 center_on_output(surface, get_default_output(compositor));
973 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200974 default:
975 ;
976 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400977
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200978 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200979 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200980 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200981 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500982 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200983 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200984 break;
985 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200986 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500987 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200988 break;
989 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200990 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500991 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200992
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500993 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200994 wlsc_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200995 do_configure = 1;
996 break;
997 case SHELL_SURFACE_SCREENSAVER:
998 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200999 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001000 show_screensaver(shell, shsurf);
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001001 wlsc_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001002 if (!shell->lock_surface)
1003 compositor->state = WLSC_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001004 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001005 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001006 break;
1007 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001008 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001009 if (!wl_list_empty(&shell->panels)) {
1010 struct shell_surface *panel =
1011 container_of(shell->panels.prev,
1012 struct shell_surface, link);
1013 wl_list_insert(&panel->surface->link, &surface->link);
1014 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001015 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001016 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001017 }
1018
Pekka Paalanen77346a62011-11-30 16:26:35 +02001019 if (do_configure)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001020 wlsc_surface_configure(surface,
1021 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001022
1023 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1024 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001025}
1026
1027static void
Pekka Paalanen77346a62011-11-30 16:26:35 +02001028configure(struct wlsc_shell *base, struct wlsc_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001029 int32_t x, int32_t y, int32_t width, int32_t height)
1030{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001031 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1032 int do_configure = !shell->locked;
1033 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1034 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001035
Pekka Paalanen77346a62011-11-30 16:26:35 +02001036 shsurf = get_shell_surface(surface);
1037 if (shsurf)
1038 surface_type = shsurf->type;
1039
1040 surface->width = width;
1041 surface->height = height;
1042
1043 switch (surface_type) {
1044 case SHELL_SURFACE_SCREENSAVER:
1045 do_configure = !do_configure;
1046 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001047 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001048 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001049 break;
1050 default:
1051 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001052 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001053
Pekka Paalanen77346a62011-11-30 16:26:35 +02001054 /*
1055 * wlsc_surface_configure() will assign an output, which means
1056 * the surface is supposed to be in compositor->surface_list.
1057 * Be careful with that, and make sure we stay on the right output.
1058 * XXX: would a fullscreen surface need the same handling?
1059 */
1060 if (do_configure) {
1061 wlsc_surface_configure(surface, x, y, width, height);
1062
1063 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1064 surface->output = shsurf->output;
1065 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001066}
1067
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001068static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001069desktop_shell_sigchld(struct wlsc_process *process, int status)
1070{
1071 struct wl_shell *shell =
1072 container_of(process, struct wl_shell, child.process);
1073
1074 shell->child.process.pid = 0;
1075 shell->child.client = NULL; /* already destroyed by wayland */
1076}
1077
1078static int
1079launch_desktop_shell_process(struct wl_shell *shell)
1080{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -05001081 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001082
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001083 shell->child.client = wlsc_client_launch(shell->compositor,
1084 &shell->child.process,
1085 shell_exe,
1086 desktop_shell_sigchld);
1087
1088 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001089 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001090 return 0;
1091}
1092
1093static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001094bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1095{
1096 struct wl_shell *shell = data;
1097
1098 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001099 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001100}
1101
Kristian Høgsberg75840622011-09-06 13:48:16 -04001102static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001103unbind_desktop_shell(struct wl_resource *resource)
1104{
1105 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001106
1107 if (shell->locked)
1108 resume_desktop(shell);
1109
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001110 shell->child.desktop_shell = NULL;
1111 shell->prepare_event_sent = false;
1112 free(resource);
1113}
1114
1115static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001116bind_desktop_shell(struct wl_client *client,
1117 void *data, uint32_t version, uint32_t id)
1118{
1119 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001120 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001121
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001122 resource = wl_client_add_object(client, &desktop_shell_interface,
1123 &desktop_shell_implementation,
1124 id, shell);
1125
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001126 if (client == shell->child.client) {
1127 resource->destroy = unbind_desktop_shell;
1128 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001129 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001130 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001131
1132 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1133 "permission to bind desktop_shell denied");
1134 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001135}
1136
Pekka Paalanen6e168112011-11-24 11:34:05 +02001137static void
1138screensaver_set_surface(struct wl_client *client,
1139 struct wl_resource *resource,
1140 struct wl_resource *shell_surface_resource,
1141 struct wl_resource *output_resource)
1142{
1143 struct wl_shell *shell = resource->data;
1144 struct shell_surface *surface = shell_surface_resource->data;
1145 struct wlsc_output *output = output_resource->data;
1146
Pekka Paalanen98262232011-12-01 10:42:22 +02001147 if (reset_shell_surface_type(surface))
1148 return;
1149
Pekka Paalanen77346a62011-11-30 16:26:35 +02001150 surface->type = SHELL_SURFACE_SCREENSAVER;
1151
1152 surface->surface->fullscreen_output = output;
1153 surface->output = output;
1154 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001155}
1156
1157static const struct screensaver_interface screensaver_implementation = {
1158 screensaver_set_surface
1159};
1160
1161static void
1162unbind_screensaver(struct wl_resource *resource)
1163{
1164 struct wl_shell *shell = resource->data;
1165
Pekka Paalanen77346a62011-11-30 16:26:35 +02001166 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001167 free(resource);
1168}
1169
1170static void
1171bind_screensaver(struct wl_client *client,
1172 void *data, uint32_t version, uint32_t id)
1173{
1174 struct wl_shell *shell = data;
1175 struct wl_resource *resource;
1176
1177 resource = wl_client_add_object(client, &screensaver_interface,
1178 &screensaver_implementation,
1179 id, shell);
1180
Pekka Paalanen77346a62011-11-30 16:26:35 +02001181 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001182 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001183 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001184 return;
1185 }
1186
1187 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1188 "interface object already bound");
1189 wl_resource_destroy(resource, 0);
1190}
1191
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001192int
1193shell_init(struct wlsc_compositor *ec);
1194
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001195WL_EXPORT int
1196shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001197{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001198 struct wl_shell *shell;
1199
1200 shell = malloc(sizeof *shell);
1201 if (shell == NULL)
1202 return -1;
1203
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001204 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001205 shell->compositor = ec;
1206 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001207 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001208 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001209 shell->shell.map = map;
1210 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001211
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001212 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001213 wl_list_init(&shell->backgrounds);
1214 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001215 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001216
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001217 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1218 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001219 return -1;
1220
Kristian Høgsberg75840622011-09-06 13:48:16 -04001221 if (wl_display_add_global(ec->wl_display,
1222 &desktop_shell_interface,
1223 shell, bind_desktop_shell) == NULL)
1224 return -1;
1225
Pekka Paalanen6e168112011-11-24 11:34:05 +02001226 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1227 shell, bind_screensaver) == NULL)
1228 return -1;
1229
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001230 if (launch_desktop_shell_process(shell) != 0)
1231 return -1;
1232
Kristian Høgsberg07937562011-04-12 17:25:42 -04001233 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001234 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001235 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001236 resize_binding, shell);
1237
1238 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001239
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001240 return 0;
1241}