blob: 6cabcfcb2f5e19b3fbee728529d1b891c6f85784 [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øgsberg75840622011-09-06 13:48:16 -040041 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040042 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020043
44 struct {
45 struct wlsc_process process;
46 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 Paalanene955f1e2011-12-07 11:49:52 +020061 const char *path;
Pekka Paalanen77346a62011-11-30 16:26:35 +020062 struct wl_resource *binding;
63 struct wl_list surfaces;
Pekka Paalanen18027e52011-12-02 16:31:49 +020064 struct wlsc_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020065 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040066};
67
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050068enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020069 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050070
Pekka Paalanen57da4a82011-11-23 16:42:16 +020071 SHELL_SURFACE_PANEL,
72 SHELL_SURFACE_BACKGROUND,
73 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020074 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050075
76 SHELL_SURFACE_TOPLEVEL,
77 SHELL_SURFACE_TRANSIENT,
78 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020079};
80
Pekka Paalanen56cdea92011-11-23 16:14:12 +020081struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020082 struct wl_resource resource;
83
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010084 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020085 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020086
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050087 enum shell_surface_type type;
88 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010089
90 struct wlsc_output *output;
91 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020092};
93
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050094struct wlsc_move_grab {
95 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050096 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050097 int32_t dx, dy;
98};
99
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200100static int
101shell_configuration(struct wl_shell *shell)
102{
103 int ret;
104 char *config_file;
105
106 struct config_key saver_keys[] = {
107 { "path", CONFIG_KEY_STRING, &shell->screensaver.path },
108 };
109
110 struct config_section cs[] = {
111 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
112 };
113
114 config_file = config_file_path("wayland-desktop-shell.ini");
115 ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
116 free(config_file);
117
118 return ret;
119 /* FIXME: free(shell->screensaver.path) on plugin fini */
120}
121
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200122static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500123move_grab_motion(struct wl_grab *grab,
124 uint32_t time, int32_t x, int32_t y)
125{
126 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500127 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500128
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400129 wlsc_surface_configure(es, x + move->dx, y + move->dy,
130 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500131}
132
133static void
134move_grab_button(struct wl_grab *grab,
135 uint32_t time, int32_t button, int32_t state)
136{
137}
138
139static void
140move_grab_end(struct wl_grab *grab, uint32_t time)
141{
142 free(grab);
143}
144
145static const struct wl_grab_interface move_grab_interface = {
146 move_grab_motion,
147 move_grab_button,
148 move_grab_end
149};
150
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400151static int
152wlsc_surface_move(struct wlsc_surface *es,
153 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500154{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500155 struct wlsc_move_grab *move;
156
157 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400158 if (!move)
159 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500160
161 move->grab.interface = &move_grab_interface;
162 move->dx = es->x - wd->input_device.grab_x;
163 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500164 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165
166 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400167 &move->grab, &es->surface, time) < 0)
168 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500169
170 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400171 wl_input_device_set_pointer_focus(&wd->input_device,
172 NULL, time, 0, 0, 0, 0);
173
174 return 0;
175}
176
177static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200178shell_surface_move(struct wl_client *client, struct wl_resource *resource,
179 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400180{
181 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200182 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400183
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400185 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186}
187
188struct wlsc_resize_grab {
189 struct wl_grab grab;
190 uint32_t edges;
191 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200192 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193};
194
195static void
196resize_grab_motion(struct wl_grab *grab,
197 uint32_t time, int32_t x, int32_t y)
198{
199 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
200 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201 int32_t width, height;
202
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200203 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500204 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200205 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206 width = x - device->grab_x + resize->width;
207 } else {
208 width = resize->width;
209 }
210
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200211 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200213 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214 height = y - device->grab_y + resize->height;
215 } else {
216 height = resize->height;
217 }
218
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219 wl_resource_post_event(&resize->shsurf->resource,
220 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
221 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500222}
223
224static void
225resize_grab_button(struct wl_grab *grab,
226 uint32_t time, int32_t button, int32_t state)
227{
228}
229
230static void
231resize_grab_end(struct wl_grab *grab, uint32_t time)
232{
233 free(grab);
234}
235
236static const struct wl_grab_interface resize_grab_interface = {
237 resize_grab_motion,
238 resize_grab_button,
239 resize_grab_end
240};
241
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400242static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200243wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400244 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200245 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500246{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500247 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200248 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500249 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500250
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500251 /* FIXME: Reject if fullscreen */
252
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400254 if (!resize)
255 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500256
257 resize->grab.interface = &resize_grab_interface;
258 resize->edges = edges;
259 resize->dx = es->x - wd->input_device.grab_x;
260 resize->dy = es->y - wd->input_device.grab_y;
261 resize->width = es->width;
262 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200263 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400264
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500265 if (edges == 0 || edges > 15 ||
266 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400267 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500268
269 switch (edges) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200270 case WL_SHELL_SURFACE_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500271 pointer = WLSC_POINTER_TOP;
272 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200273 case WL_SHELL_SURFACE_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500274 pointer = WLSC_POINTER_BOTTOM;
275 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200276 case WL_SHELL_SURFACE_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500277 pointer = WLSC_POINTER_LEFT;
278 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200279 case WL_SHELL_SURFACE_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500280 pointer = WLSC_POINTER_TOP_LEFT;
281 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200282 case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500283 pointer = WLSC_POINTER_BOTTOM_LEFT;
284 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285 case WL_SHELL_SURFACE_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500286 pointer = WLSC_POINTER_RIGHT;
287 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200288 case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500289 pointer = WLSC_POINTER_TOP_RIGHT;
290 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200291 case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500292 pointer = WLSC_POINTER_BOTTOM_RIGHT;
293 break;
294 }
295
296 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400297 &resize->grab, &es->surface, time) < 0)
298 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500299
300 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400301 wl_input_device_set_pointer_focus(&wd->input_device,
302 NULL, time, 0, 0, 0, 0);
303
304 return 0;
305}
306
307static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200308shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
309 struct wl_resource *input_resource, uint32_t time,
310 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400311{
312 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200313 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400314
315 /* FIXME: Reject if fullscreen */
316
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200317 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400318 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500319}
320
Pekka Paalanen98262232011-12-01 10:42:22 +0200321static int
322reset_shell_surface_type(struct shell_surface *surface)
323{
324 switch (surface->type) {
325 case SHELL_SURFACE_FULLSCREEN:
326 surface->surface->x = surface->saved_x;
327 surface->surface->y = surface->saved_y;
328 surface->surface->fullscreen_output = NULL;
329 break;
330 case SHELL_SURFACE_PANEL:
331 case SHELL_SURFACE_BACKGROUND:
332 wl_list_remove(&surface->link);
333 wl_list_init(&surface->link);
334 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200335 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200336 case SHELL_SURFACE_LOCK:
337 wl_resource_post_error(&surface->resource,
338 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200339 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200340 return -1;
341 case SHELL_SURFACE_NONE:
342 case SHELL_SURFACE_TOPLEVEL:
343 case SHELL_SURFACE_TRANSIENT:
344 break;
345 }
346
347 surface->type = SHELL_SURFACE_NONE;
348 return 0;
349}
350
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500351static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200352shell_surface_set_toplevel(struct wl_client *client,
353 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400354
355{
Pekka Paalanen98262232011-12-01 10:42:22 +0200356 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400357
Pekka Paalanen98262232011-12-01 10:42:22 +0200358 if (reset_shell_surface_type(surface))
359 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400360
Pekka Paalanen98262232011-12-01 10:42:22 +0200361 wlsc_surface_damage(surface->surface);
362 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400363}
364
365static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200366shell_surface_set_transient(struct wl_client *client,
367 struct wl_resource *resource,
368 struct wl_resource *parent_resource,
369 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400370{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200371 struct shell_surface *shsurf = resource->data;
372 struct wlsc_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200373 struct shell_surface *pshsurf = parent_resource->data;
374 struct wlsc_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400375
Pekka Paalanen98262232011-12-01 10:42:22 +0200376 if (reset_shell_surface_type(shsurf))
377 return;
378
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400379 /* assign to parents output */
380 es->output = pes->output;
381
382 es->x = pes->x + x;
383 es->y = pes->y + y;
384
385 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200386 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400387}
388
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200389static struct wlsc_output *
390get_default_output(struct wlsc_compositor *compositor)
391{
392 return container_of(compositor->output_list.next,
393 struct wlsc_output, link);
394}
395
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400396static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200397shell_surface_set_fullscreen(struct wl_client *client,
398 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400399
400{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200401 struct shell_surface *shsurf = resource->data;
402 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400403 struct wlsc_output *output;
404
Pekka Paalanen98262232011-12-01 10:42:22 +0200405 if (reset_shell_surface_type(shsurf))
406 return;
407
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400408 /* FIXME: Fullscreen on first output */
409 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200410 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400411 es->output = output;
412
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200413 shsurf->saved_x = es->x;
414 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400415 es->x = (output->current->width - es->width) / 2;
416 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400417 es->fullscreen_output = output;
418 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200419 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400420}
421
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200422static const struct wl_shell_surface_interface shell_surface_implementation = {
423 shell_surface_move,
424 shell_surface_resize,
425 shell_surface_set_toplevel,
426 shell_surface_set_transient,
427 shell_surface_set_fullscreen
428};
429
430static void
431destroy_shell_surface(struct wl_resource *resource)
432{
433 struct shell_surface *shsurf = resource->data;
434
435 /* in case cleaning up a dead client destroys shell_surface first */
436 if (shsurf->surface)
437 wl_list_remove(&shsurf->surface_destroy_listener.link);
438
439 wl_list_remove(&shsurf->link);
440 free(shsurf);
441}
442
443static void
444shell_handle_surface_destroy(struct wl_listener *listener,
445 struct wl_resource *resource, uint32_t time)
446{
447 struct shell_surface *shsurf = container_of(listener,
448 struct shell_surface,
449 surface_destroy_listener);
450
451 shsurf->surface = NULL;
452 wl_resource_destroy(&shsurf->resource, time);
453}
454
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200455static struct shell_surface *
456get_shell_surface(struct wlsc_surface *surface)
457{
458 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
459 struct wl_listener *listener;
460
461 /* search the destroy listener list for our callback */
462 wl_list_for_each(listener, lst, link) {
463 if (listener->func == shell_handle_surface_destroy) {
464 return container_of(listener, struct shell_surface,
465 surface_destroy_listener);
466 }
467 }
468
469 return NULL;
470}
471
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200472static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200473shell_get_shell_surface(struct wl_client *client,
474 struct wl_resource *resource,
475 uint32_t id,
476 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200477{
478 struct wlsc_surface *surface = surface_resource->data;
479 struct shell_surface *shsurf;
480
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200481 if (get_shell_surface(surface)) {
482 wl_resource_post_error(surface_resource,
483 WL_DISPLAY_ERROR_INVALID_OBJECT,
484 "wl_shell::get_shell_surface already requested");
485 return;
486 }
487
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200488 shsurf = calloc(1, sizeof *shsurf);
489 if (!shsurf) {
490 wl_resource_post_no_memory(resource);
491 return;
492 }
493
494 shsurf->resource.destroy = destroy_shell_surface;
495 shsurf->resource.object.id = id;
496 shsurf->resource.object.interface = &wl_shell_surface_interface;
497 shsurf->resource.object.implementation =
498 (void (**)(void)) &shell_surface_implementation;
499 shsurf->resource.data = shsurf;
500
501 shsurf->surface = surface;
502 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
503 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
504 &shsurf->surface_destroy_listener.link);
505
506 /* init link so its safe to always remove it in destroy_shell_surface */
507 wl_list_init(&shsurf->link);
508
Pekka Paalanen98262232011-12-01 10:42:22 +0200509 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200510
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200511 wl_client_add_resource(client, &shsurf->resource);
512}
513
514static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200515 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500516};
517
Kristian Høgsberg07937562011-04-12 17:25:42 -0400518static void
Pekka Paalanen18027e52011-12-02 16:31:49 +0200519handle_screensaver_sigchld(struct wlsc_process *proc, int status)
520{
521 proc->pid = 0;
522}
523
524static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200525launch_screensaver(struct wl_shell *shell)
526{
527 if (shell->screensaver.binding)
528 return;
529
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200530 if (!shell->screensaver.path)
531 return;
532
Pekka Paalanen18027e52011-12-02 16:31:49 +0200533 wlsc_client_launch(shell->compositor,
534 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200535 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200536 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200537}
538
539static void
540terminate_screensaver(struct wl_shell *shell)
541{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200542 if (shell->screensaver.process.pid == 0)
543 return;
544
545 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200546}
547
548static void
549show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
550{
551 struct wl_list *list;
552
553 if (shell->lock_surface)
554 list = &shell->lock_surface->surface->link;
555 else
556 list = &shell->compositor->surface_list;
557
558 wl_list_remove(&surface->surface->link);
559 wl_list_insert(list, &surface->surface->link);
560 wlsc_surface_configure(surface->surface,
561 surface->surface->x,
562 surface->surface->y,
563 surface->surface->width,
564 surface->surface->height);
565 surface->surface->output = surface->output;
566}
567
568static void
569hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
570{
571 wl_list_remove(&surface->surface->link);
572 wl_list_init(&surface->surface->link);
573 surface->surface->output = NULL;
574}
575
576static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400577desktop_shell_set_background(struct wl_client *client,
578 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100579 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400580 struct wl_resource *surface_resource)
581{
582 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200583 struct shell_surface *shsurf = surface_resource->data;
584 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200585 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400586
Pekka Paalanen98262232011-12-01 10:42:22 +0200587 if (reset_shell_surface_type(shsurf))
588 return;
589
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100590 wl_list_for_each(priv, &shell->backgrounds, link) {
591 if (priv->output == output_resource->data) {
592 priv->surface->output = NULL;
593 wl_list_remove(&priv->surface->link);
594 wl_list_remove(&priv->link);
595 break;
596 }
597 }
598
Pekka Paalanen068ae942011-11-28 14:11:15 +0200599 shsurf->type = SHELL_SURFACE_BACKGROUND;
600 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100601
Pekka Paalanen068ae942011-11-28 14:11:15 +0200602 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100603
Pekka Paalanen068ae942011-11-28 14:11:15 +0200604 surface->x = shsurf->output->x;
605 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200606
Kristian Høgsberg75840622011-09-06 13:48:16 -0400607 wl_resource_post_event(resource,
608 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200609 wlsc_compositor_get_time(), 0, surface_resource,
610 shsurf->output->current->width,
611 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400612}
613
614static void
615desktop_shell_set_panel(struct wl_client *client,
616 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100617 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400618 struct wl_resource *surface_resource)
619{
620 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200621 struct shell_surface *shsurf = surface_resource->data;
622 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200623 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400624
Pekka Paalanen98262232011-12-01 10:42:22 +0200625 if (reset_shell_surface_type(shsurf))
626 return;
627
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100628 wl_list_for_each(priv, &shell->panels, link) {
629 if (priv->output == output_resource->data) {
630 priv->surface->output = NULL;
631 wl_list_remove(&priv->surface->link);
632 wl_list_remove(&priv->link);
633 break;
634 }
635 }
636
Pekka Paalanen068ae942011-11-28 14:11:15 +0200637 shsurf->type = SHELL_SURFACE_PANEL;
638 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100639
Pekka Paalanen068ae942011-11-28 14:11:15 +0200640 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100641
Pekka Paalanen068ae942011-11-28 14:11:15 +0200642 surface->x = shsurf->output->x;
643 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200644
Kristian Høgsberg75840622011-09-06 13:48:16 -0400645 wl_resource_post_event(resource,
646 DESKTOP_SHELL_CONFIGURE,
647 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200648 shsurf->output->current->width,
649 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400650}
651
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200652static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500653handle_lock_surface_destroy(struct wl_listener *listener,
654 struct wl_resource *resource, uint32_t time)
655{
656 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200657 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500658
659 fprintf(stderr, "lock surface gone\n");
660 shell->lock_surface = NULL;
661}
662
663static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200664desktop_shell_set_lock_surface(struct wl_client *client,
665 struct wl_resource *resource,
666 struct wl_resource *surface_resource)
667{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200668 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200669 struct shell_surface *surface = surface_resource->data;
670
671 if (reset_shell_surface_type(surface))
672 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200673
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200674 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200675
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200676 if (!shell->locked)
677 return;
678
Pekka Paalanen98262232011-12-01 10:42:22 +0200679 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200680
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500681 shell->lock_surface_listener.func = handle_lock_surface_destroy;
682 wl_list_insert(&surface_resource->destroy_listener_list,
683 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200684
Pekka Paalanen068ae942011-11-28 14:11:15 +0200685 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200686}
687
688static void
689resume_desktop(struct wl_shell *shell)
690{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500691 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200692 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200693 struct shell_surface *tmp;
694
695 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
696 hide_screensaver(shell, tmp);
697
698 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200699
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500700 wl_list_for_each(surface, &shell->hidden_surface_list, link)
701 wlsc_surface_configure(surface, surface->x, surface->y,
702 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200703
Pekka Paalanenfe340832011-11-25 16:07:52 +0200704 if (wl_list_empty(&shell->backgrounds)) {
705 list = &shell->compositor->surface_list;
706 } else {
707 struct shell_surface *background;
708 background = container_of(shell->backgrounds.prev,
709 struct shell_surface, link);
710 list = background->surface->link.prev;
711 }
712
713 if (!wl_list_empty(&shell->hidden_surface_list))
714 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500715 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200716
717 shell->locked = false;
718 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200719 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200720}
721
722static void
723desktop_shell_unlock(struct wl_client *client,
724 struct wl_resource *resource)
725{
726 struct wl_shell *shell = resource->data;
727
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200728 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200729
730 if (shell->locked)
731 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200732}
733
Kristian Høgsberg75840622011-09-06 13:48:16 -0400734static const struct desktop_shell_interface desktop_shell_implementation = {
735 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200736 desktop_shell_set_panel,
737 desktop_shell_set_lock_surface,
738 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400739};
740
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200741static enum shell_surface_type
742get_shell_surface_type(struct wlsc_surface *surface)
743{
744 struct shell_surface *shsurf;
745
746 shsurf = get_shell_surface(surface);
747 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200748 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200749 return shsurf->type;
750}
751
Kristian Høgsberg75840622011-09-06 13:48:16 -0400752static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400753move_binding(struct wl_input_device *device, uint32_t time,
754 uint32_t key, uint32_t button, uint32_t state, void *data)
755{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400756 struct wlsc_surface *surface =
757 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500758
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100759 if (surface == NULL)
760 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400761
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200762 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100763 case SHELL_SURFACE_PANEL:
764 case SHELL_SURFACE_BACKGROUND:
765 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200766 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100767 return;
768 default:
769 break;
770 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400771
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400772 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400773}
774
775static void
776resize_binding(struct wl_input_device *device, uint32_t time,
777 uint32_t key, uint32_t button, uint32_t state, void *data)
778{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400779 struct wlsc_surface *surface =
780 (struct wlsc_surface *) device->pointer_focus;
781 uint32_t edges = 0;
782 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200783 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500784
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100785 if (surface == NULL)
786 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200787
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200788 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200789 if (!shsurf)
790 return;
791
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200792 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100793 case SHELL_SURFACE_PANEL:
794 case SHELL_SURFACE_BACKGROUND:
795 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200796 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100797 return;
798 default:
799 break;
800 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400801
Kristian Høgsberg07937562011-04-12 17:25:42 -0400802 x = device->grab_x - surface->x;
803 y = device->grab_y - surface->y;
804
805 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200806 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400807 else if (x < 2 * surface->width / 3)
808 edges |= 0;
809 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200810 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400811
812 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200813 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400814 else if (y < 2 * surface->height / 3)
815 edges |= 0;
816 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200817 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400818
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200819 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
820 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400821}
822
823static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400824activate(struct wlsc_shell *base, struct wlsc_surface *es,
825 struct wlsc_input_device *device, uint32_t time)
826{
827 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
828 struct wlsc_compositor *compositor = shell->compositor;
829
830 wlsc_surface_activate(es, device, time);
831
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400832 if (compositor->wxs)
833 wlsc_xserver_surface_activate(es);
834
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200835 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200836 case SHELL_SURFACE_BACKGROUND:
837 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400838 wl_list_remove(&es->link);
839 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200840 break;
841 case SHELL_SURFACE_PANEL:
842 /* already put on top */
843 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200844 case SHELL_SURFACE_SCREENSAVER:
845 /* always below lock surface */
846 if (shell->lock_surface) {
847 wl_list_remove(&es->link);
848 wl_list_insert(&shell->lock_surface->surface->link,
849 &es->link);
850 }
851 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200852 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100853 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200854 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100855 struct shell_surface *panel;
856 wl_list_for_each(panel, &shell->panels, link) {
857 wl_list_remove(&panel->surface->link);
858 wl_list_insert(&compositor->surface_list,
859 &panel->surface->link);
860 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200861 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400862 }
863}
864
865static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200866lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400867{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200868 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200869 struct wl_list *surface_list = &shell->compositor->surface_list;
870 struct wlsc_surface *cur;
871 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200872 struct wlsc_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200873 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200874 uint32_t time;
875
876 if (shell->locked)
877 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200878
879 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200880
881 /* Move all surfaces from compositor's list to our hidden list,
882 * except the background. This way nothing else can show or
883 * receive input events while we are locked. */
884
885 if (!wl_list_empty(&shell->hidden_surface_list)) {
886 fprintf(stderr,
887 "%s: Assertion failed: hidden_surface_list is not empty.\n",
888 __func__);
889 }
890
891 wl_list_for_each_safe(cur, tmp, surface_list, link) {
892 /* skip input device sprites, cur->surface is uninitialised */
893 if (cur->surface.resource.client == NULL)
894 continue;
895
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200896 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200897 continue;
898
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500899 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200900 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500901 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200902 }
903
Pekka Paalanen77346a62011-11-30 16:26:35 +0200904 launch_screensaver(shell);
905
906 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
907 show_screensaver(shell, shsurf);
908
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200909 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200910 wlsc_compositor_wake(shell->compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200911 shell->compositor->state = WLSC_COMPOSITOR_IDLE;
912 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200913
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200914 /* reset pointer foci */
915 wlsc_compositor_repick(shell->compositor);
916
917 /* reset keyboard foci */
918 time = wlsc_compositor_get_time();
919 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
920 wl_input_device_set_keyboard_focus(&device->input_device,
921 NULL, time);
922 }
923
924 /* TODO: disable bindings that should not work while locked. */
925
926 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200927}
928
929static void
930unlock(struct wlsc_shell *base)
931{
932 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
933
Pekka Paalanend81c2162011-11-16 13:47:34 +0200934 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200935 wlsc_compositor_wake(shell->compositor);
936 return;
937 }
938
939 /* If desktop-shell client has gone away, unlock immediately. */
940 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200941 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200942 return;
943 }
944
945 if (shell->prepare_event_sent)
946 return;
947
948 wl_resource_post_event(shell->child.desktop_shell,
949 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
950 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400951}
952
953static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200954center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
955{
956 struct wlsc_mode *mode = output->current;
957
958 surface->x = output->x + (mode->width - surface->width) / 2;
959 surface->y = output->y + (mode->height - surface->height) / 2;
960}
961
962static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500963map(struct wlsc_shell *base,
964 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400965{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400966 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
967 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500968 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200969 struct shell_surface *shsurf;
970 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
971 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200972
Pekka Paalanen77346a62011-11-30 16:26:35 +0200973 shsurf = get_shell_surface(surface);
974 if (shsurf)
975 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500976
Pekka Paalanen77346a62011-11-30 16:26:35 +0200977 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500978 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200979 do_configure = 0;
980 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500981 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200982 do_configure = 1;
983 }
984
985 surface->width = width;
986 surface->height = height;
987
988 /* initial positioning, see also configure() */
989 switch (surface_type) {
990 case SHELL_SURFACE_TOPLEVEL:
991 surface->x = 10 + random() % 400;
992 surface->y = 10 + random() % 400;
993 break;
994 case SHELL_SURFACE_SCREENSAVER:
995 case SHELL_SURFACE_FULLSCREEN:
996 center_on_output(surface, surface->fullscreen_output);
997 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200998 case SHELL_SURFACE_LOCK:
999 center_on_output(surface, get_default_output(compositor));
1000 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001001 default:
1002 ;
1003 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001004
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001005 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001006 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001007 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001008 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001009 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001010 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001011 break;
1012 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001013 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001014 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001015 break;
1016 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001017 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001018 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001019
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001020 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001021 wlsc_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001022 do_configure = 1;
1023 break;
1024 case SHELL_SURFACE_SCREENSAVER:
1025 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001026 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001027 show_screensaver(shell, shsurf);
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001028 wlsc_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001029 if (!shell->lock_surface)
1030 compositor->state = WLSC_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001031 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001032 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001033 break;
1034 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001035 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001036 if (!wl_list_empty(&shell->panels)) {
1037 struct shell_surface *panel =
1038 container_of(shell->panels.prev,
1039 struct shell_surface, link);
1040 wl_list_insert(&panel->surface->link, &surface->link);
1041 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001042 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001043 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001044 }
1045
Pekka Paalanen77346a62011-11-30 16:26:35 +02001046 if (do_configure)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001047 wlsc_surface_configure(surface,
1048 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001049
1050 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1051 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001052}
1053
1054static void
Pekka Paalanen77346a62011-11-30 16:26:35 +02001055configure(struct wlsc_shell *base, struct wlsc_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001056 int32_t x, int32_t y, int32_t width, int32_t height)
1057{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001058 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1059 int do_configure = !shell->locked;
1060 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1061 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001062
Pekka Paalanen77346a62011-11-30 16:26:35 +02001063 shsurf = get_shell_surface(surface);
1064 if (shsurf)
1065 surface_type = shsurf->type;
1066
1067 surface->width = width;
1068 surface->height = height;
1069
1070 switch (surface_type) {
1071 case SHELL_SURFACE_SCREENSAVER:
1072 do_configure = !do_configure;
1073 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001074 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001075 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001076 break;
1077 default:
1078 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001079 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001080
Pekka Paalanen77346a62011-11-30 16:26:35 +02001081 /*
1082 * wlsc_surface_configure() will assign an output, which means
1083 * the surface is supposed to be in compositor->surface_list.
1084 * Be careful with that, and make sure we stay on the right output.
1085 * XXX: would a fullscreen surface need the same handling?
1086 */
1087 if (do_configure) {
1088 wlsc_surface_configure(surface, x, y, width, height);
1089
1090 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1091 surface->output = shsurf->output;
1092 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001093}
1094
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001095static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001096desktop_shell_sigchld(struct wlsc_process *process, int status)
1097{
1098 struct wl_shell *shell =
1099 container_of(process, struct wl_shell, child.process);
1100
1101 shell->child.process.pid = 0;
1102 shell->child.client = NULL; /* already destroyed by wayland */
1103}
1104
1105static int
1106launch_desktop_shell_process(struct wl_shell *shell)
1107{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -05001108 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001109
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001110 shell->child.client = wlsc_client_launch(shell->compositor,
1111 &shell->child.process,
1112 shell_exe,
1113 desktop_shell_sigchld);
1114
1115 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001116 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001117 return 0;
1118}
1119
1120static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001121bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1122{
1123 struct wl_shell *shell = data;
1124
1125 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001126 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001127}
1128
Kristian Høgsberg75840622011-09-06 13:48:16 -04001129static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001130unbind_desktop_shell(struct wl_resource *resource)
1131{
1132 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001133
1134 if (shell->locked)
1135 resume_desktop(shell);
1136
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001137 shell->child.desktop_shell = NULL;
1138 shell->prepare_event_sent = false;
1139 free(resource);
1140}
1141
1142static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001143bind_desktop_shell(struct wl_client *client,
1144 void *data, uint32_t version, uint32_t id)
1145{
1146 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001147 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001148
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001149 resource = wl_client_add_object(client, &desktop_shell_interface,
1150 &desktop_shell_implementation,
1151 id, shell);
1152
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001153 if (client == shell->child.client) {
1154 resource->destroy = unbind_desktop_shell;
1155 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001156 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001157 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001158
1159 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1160 "permission to bind desktop_shell denied");
1161 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001162}
1163
Pekka Paalanen6e168112011-11-24 11:34:05 +02001164static void
1165screensaver_set_surface(struct wl_client *client,
1166 struct wl_resource *resource,
1167 struct wl_resource *shell_surface_resource,
1168 struct wl_resource *output_resource)
1169{
1170 struct wl_shell *shell = resource->data;
1171 struct shell_surface *surface = shell_surface_resource->data;
1172 struct wlsc_output *output = output_resource->data;
1173
Pekka Paalanen98262232011-12-01 10:42:22 +02001174 if (reset_shell_surface_type(surface))
1175 return;
1176
Pekka Paalanen77346a62011-11-30 16:26:35 +02001177 surface->type = SHELL_SURFACE_SCREENSAVER;
1178
1179 surface->surface->fullscreen_output = output;
1180 surface->output = output;
1181 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001182}
1183
1184static const struct screensaver_interface screensaver_implementation = {
1185 screensaver_set_surface
1186};
1187
1188static void
1189unbind_screensaver(struct wl_resource *resource)
1190{
1191 struct wl_shell *shell = resource->data;
1192
Pekka Paalanen77346a62011-11-30 16:26:35 +02001193 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001194 free(resource);
1195}
1196
1197static void
1198bind_screensaver(struct wl_client *client,
1199 void *data, uint32_t version, uint32_t id)
1200{
1201 struct wl_shell *shell = data;
1202 struct wl_resource *resource;
1203
1204 resource = wl_client_add_object(client, &screensaver_interface,
1205 &screensaver_implementation,
1206 id, shell);
1207
Pekka Paalanen77346a62011-11-30 16:26:35 +02001208 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001209 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001210 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001211 return;
1212 }
1213
1214 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1215 "interface object already bound");
1216 wl_resource_destroy(resource, 0);
1217}
1218
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001219int
1220shell_init(struct wlsc_compositor *ec);
1221
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001222WL_EXPORT int
1223shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001224{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001225 struct wl_shell *shell;
1226
1227 shell = malloc(sizeof *shell);
1228 if (shell == NULL)
1229 return -1;
1230
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001231 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001232 shell->compositor = ec;
1233 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001234 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001235 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001236 shell->shell.map = map;
1237 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001238
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001239 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001240 wl_list_init(&shell->backgrounds);
1241 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001242 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001243
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001244 if (shell_configuration(shell) < 0)
1245 return -1;
1246
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001247 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1248 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001249 return -1;
1250
Kristian Høgsberg75840622011-09-06 13:48:16 -04001251 if (wl_display_add_global(ec->wl_display,
1252 &desktop_shell_interface,
1253 shell, bind_desktop_shell) == NULL)
1254 return -1;
1255
Pekka Paalanen6e168112011-11-24 11:34:05 +02001256 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1257 shell, bind_screensaver) == NULL)
1258 return -1;
1259
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001260 if (launch_desktop_shell_process(shell) != 0)
1261 return -1;
1262
Kristian Høgsberg07937562011-04-12 17:25:42 -04001263 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001264 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001265 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001266 resize_binding, shell);
1267
1268 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001269
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001270 return 0;
1271}