blob: 2a6d58747fdb7a16a3b66f632448cdd812ade04c [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>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050031
Pekka Paalanen50719bc2011-11-22 14:18:50 +020032#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040034#include "desktop-shell-server-protocol.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035
Pekka Paalanen068ae942011-11-28 14:11:15 +020036struct shell_surface;
37
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040038struct wl_shell {
Kristian Høgsberg75840622011-09-06 13:48:16 -040039 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040040 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020041
42 struct {
43 struct wlsc_process process;
44 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020045 struct wl_resource *desktop_shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020046 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020047
48 bool locked;
49 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020050
Pekka Paalanen068ae942011-11-28 14:11:15 +020051 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050052 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020053 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010054
55 struct wl_list backgrounds;
56 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020057
Pekka Paalanen77346a62011-11-30 16:26:35 +020058 struct {
59 struct wl_resource *binding;
60 struct wl_list surfaces;
61 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040062};
63
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050064enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020065 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050066
Pekka Paalanen57da4a82011-11-23 16:42:16 +020067 SHELL_SURFACE_PANEL,
68 SHELL_SURFACE_BACKGROUND,
69 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020070 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050071
72 SHELL_SURFACE_TOPLEVEL,
73 SHELL_SURFACE_TRANSIENT,
74 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020075};
76
Pekka Paalanen56cdea92011-11-23 16:14:12 +020077struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020078 struct wl_resource resource;
79
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010080 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020081 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020082
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050083 enum shell_surface_type type;
84 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010085
86 struct wlsc_output *output;
87 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020088};
89
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050090struct wlsc_move_grab {
91 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050092 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050093 int32_t dx, dy;
94};
95
Pekka Paalanen56cdea92011-11-23 16:14:12 +020096static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050097move_grab_motion(struct wl_grab *grab,
98 uint32_t time, int32_t x, int32_t y)
99{
100 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500101 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500102
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400103 wlsc_surface_configure(es, x + move->dx, y + move->dy,
104 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500105}
106
107static void
108move_grab_button(struct wl_grab *grab,
109 uint32_t time, int32_t button, int32_t state)
110{
111}
112
113static void
114move_grab_end(struct wl_grab *grab, uint32_t time)
115{
116 free(grab);
117}
118
119static const struct wl_grab_interface move_grab_interface = {
120 move_grab_motion,
121 move_grab_button,
122 move_grab_end
123};
124
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400125static int
126wlsc_surface_move(struct wlsc_surface *es,
127 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500128{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500129 struct wlsc_move_grab *move;
130
131 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400132 if (!move)
133 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500134
135 move->grab.interface = &move_grab_interface;
136 move->dx = es->x - wd->input_device.grab_x;
137 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500138 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500139
140 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400141 &move->grab, &es->surface, time) < 0)
142 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500143
144 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400145 wl_input_device_set_pointer_focus(&wd->input_device,
146 NULL, time, 0, 0, 0, 0);
147
148 return 0;
149}
150
151static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200152shell_surface_move(struct wl_client *client, struct wl_resource *resource,
153 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400154{
155 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200156 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400157
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200158 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400159 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500160}
161
162struct wlsc_resize_grab {
163 struct wl_grab grab;
164 uint32_t edges;
165 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200166 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167};
168
169static void
170resize_grab_motion(struct wl_grab *grab,
171 uint32_t time, int32_t x, int32_t y)
172{
173 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
174 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500175 int32_t width, height;
176
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200177 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500178 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200179 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180 width = x - device->grab_x + resize->width;
181 } else {
182 width = resize->width;
183 }
184
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200185 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500186 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200187 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500188 height = y - device->grab_y + resize->height;
189 } else {
190 height = resize->height;
191 }
192
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200193 wl_resource_post_event(&resize->shsurf->resource,
194 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
195 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196}
197
198static void
199resize_grab_button(struct wl_grab *grab,
200 uint32_t time, int32_t button, int32_t state)
201{
202}
203
204static void
205resize_grab_end(struct wl_grab *grab, uint32_t time)
206{
207 free(grab);
208}
209
210static const struct wl_grab_interface resize_grab_interface = {
211 resize_grab_motion,
212 resize_grab_button,
213 resize_grab_end
214};
215
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400216static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200217wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400218 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200222 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500225 /* FIXME: Reject if fullscreen */
226
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400228 if (!resize)
229 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500230
231 resize->grab.interface = &resize_grab_interface;
232 resize->edges = edges;
233 resize->dx = es->x - wd->input_device.grab_x;
234 resize->dy = es->y - wd->input_device.grab_y;
235 resize->width = es->width;
236 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200237 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400238
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500239 if (edges == 0 || edges > 15 ||
240 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400241 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242
243 switch (edges) {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200244 case WL_SHELL_SURFACE_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500245 pointer = WLSC_POINTER_TOP;
246 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200247 case WL_SHELL_SURFACE_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500248 pointer = WLSC_POINTER_BOTTOM;
249 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200250 case WL_SHELL_SURFACE_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500251 pointer = WLSC_POINTER_LEFT;
252 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200253 case WL_SHELL_SURFACE_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500254 pointer = WLSC_POINTER_TOP_LEFT;
255 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200256 case WL_SHELL_SURFACE_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500257 pointer = WLSC_POINTER_BOTTOM_LEFT;
258 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200259 case WL_SHELL_SURFACE_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500260 pointer = WLSC_POINTER_RIGHT;
261 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200262 case WL_SHELL_SURFACE_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500263 pointer = WLSC_POINTER_TOP_RIGHT;
264 break;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200265 case WL_SHELL_SURFACE_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500266 pointer = WLSC_POINTER_BOTTOM_RIGHT;
267 break;
268 }
269
270 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400271 &resize->grab, &es->surface, time) < 0)
272 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500273
274 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400275 wl_input_device_set_pointer_focus(&wd->input_device,
276 NULL, time, 0, 0, 0, 0);
277
278 return 0;
279}
280
281static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200282shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
283 struct wl_resource *input_resource, uint32_t time,
284 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400285{
286 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200287 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400288
289 /* FIXME: Reject if fullscreen */
290
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200291 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400292 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500293}
294
Pekka Paalanen98262232011-12-01 10:42:22 +0200295static int
296reset_shell_surface_type(struct shell_surface *surface)
297{
298 switch (surface->type) {
299 case SHELL_SURFACE_FULLSCREEN:
300 surface->surface->x = surface->saved_x;
301 surface->surface->y = surface->saved_y;
302 surface->surface->fullscreen_output = NULL;
303 break;
304 case SHELL_SURFACE_PANEL:
305 case SHELL_SURFACE_BACKGROUND:
306 wl_list_remove(&surface->link);
307 wl_list_init(&surface->link);
308 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200309 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200310 case SHELL_SURFACE_LOCK:
311 wl_resource_post_error(&surface->resource,
312 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200313 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200314 return -1;
315 case SHELL_SURFACE_NONE:
316 case SHELL_SURFACE_TOPLEVEL:
317 case SHELL_SURFACE_TRANSIENT:
318 break;
319 }
320
321 surface->type = SHELL_SURFACE_NONE;
322 return 0;
323}
324
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500325static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200326shell_surface_set_toplevel(struct wl_client *client,
327 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400328
329{
Pekka Paalanen98262232011-12-01 10:42:22 +0200330 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400331
Pekka Paalanen98262232011-12-01 10:42:22 +0200332 if (reset_shell_surface_type(surface))
333 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400334
Pekka Paalanen98262232011-12-01 10:42:22 +0200335 wlsc_surface_damage(surface->surface);
336 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400337}
338
339static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200340shell_surface_set_transient(struct wl_client *client,
341 struct wl_resource *resource,
342 struct wl_resource *parent_resource,
343 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400344{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200345 struct shell_surface *shsurf = resource->data;
346 struct wlsc_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200347 struct shell_surface *pshsurf = parent_resource->data;
348 struct wlsc_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400349
Pekka Paalanen98262232011-12-01 10:42:22 +0200350 if (reset_shell_surface_type(shsurf))
351 return;
352
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400353 /* assign to parents output */
354 es->output = pes->output;
355
356 es->x = pes->x + x;
357 es->y = pes->y + y;
358
359 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200360 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400361}
362
363static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200364shell_surface_set_fullscreen(struct wl_client *client,
365 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400366
367{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200368 struct shell_surface *shsurf = resource->data;
369 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400370 struct wlsc_output *output;
371
Pekka Paalanen98262232011-12-01 10:42:22 +0200372 if (reset_shell_surface_type(shsurf))
373 return;
374
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400375 /* FIXME: Fullscreen on first output */
376 /* FIXME: Handle output going away */
377 output = container_of(es->compositor->output_list.next,
378 struct wlsc_output, link);
379 es->output = output;
380
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200381 shsurf->saved_x = es->x;
382 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400383 es->x = (output->current->width - es->width) / 2;
384 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400385 es->fullscreen_output = output;
386 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200387 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400388}
389
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200390static const struct wl_shell_surface_interface shell_surface_implementation = {
391 shell_surface_move,
392 shell_surface_resize,
393 shell_surface_set_toplevel,
394 shell_surface_set_transient,
395 shell_surface_set_fullscreen
396};
397
398static void
399destroy_shell_surface(struct wl_resource *resource)
400{
401 struct shell_surface *shsurf = resource->data;
402
403 /* in case cleaning up a dead client destroys shell_surface first */
404 if (shsurf->surface)
405 wl_list_remove(&shsurf->surface_destroy_listener.link);
406
407 wl_list_remove(&shsurf->link);
408 free(shsurf);
409}
410
411static void
412shell_handle_surface_destroy(struct wl_listener *listener,
413 struct wl_resource *resource, uint32_t time)
414{
415 struct shell_surface *shsurf = container_of(listener,
416 struct shell_surface,
417 surface_destroy_listener);
418
419 shsurf->surface = NULL;
420 wl_resource_destroy(&shsurf->resource, time);
421}
422
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200423static struct shell_surface *
424get_shell_surface(struct wlsc_surface *surface)
425{
426 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
427 struct wl_listener *listener;
428
429 /* search the destroy listener list for our callback */
430 wl_list_for_each(listener, lst, link) {
431 if (listener->func == shell_handle_surface_destroy) {
432 return container_of(listener, struct shell_surface,
433 surface_destroy_listener);
434 }
435 }
436
437 return NULL;
438}
439
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200440static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200441shell_get_shell_surface(struct wl_client *client,
442 struct wl_resource *resource,
443 uint32_t id,
444 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200445{
446 struct wlsc_surface *surface = surface_resource->data;
447 struct shell_surface *shsurf;
448
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200449 if (get_shell_surface(surface)) {
450 wl_resource_post_error(surface_resource,
451 WL_DISPLAY_ERROR_INVALID_OBJECT,
452 "wl_shell::get_shell_surface already requested");
453 return;
454 }
455
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200456 shsurf = calloc(1, sizeof *shsurf);
457 if (!shsurf) {
458 wl_resource_post_no_memory(resource);
459 return;
460 }
461
462 shsurf->resource.destroy = destroy_shell_surface;
463 shsurf->resource.object.id = id;
464 shsurf->resource.object.interface = &wl_shell_surface_interface;
465 shsurf->resource.object.implementation =
466 (void (**)(void)) &shell_surface_implementation;
467 shsurf->resource.data = shsurf;
468
469 shsurf->surface = surface;
470 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
471 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
472 &shsurf->surface_destroy_listener.link);
473
474 /* init link so its safe to always remove it in destroy_shell_surface */
475 wl_list_init(&shsurf->link);
476
Pekka Paalanen98262232011-12-01 10:42:22 +0200477 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200478
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200479 wl_client_add_resource(client, &shsurf->resource);
480}
481
482static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200483 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500484};
485
Kristian Høgsberg07937562011-04-12 17:25:42 -0400486static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200487launch_screensaver(struct wl_shell *shell)
488{
489 if (shell->screensaver.binding)
490 return;
491
492 /* TODO: exec() the screensaver process */
493}
494
495static void
496terminate_screensaver(struct wl_shell *shell)
497{
498 /* TODO */
499}
500
501static void
502show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
503{
504 struct wl_list *list;
505
506 if (shell->lock_surface)
507 list = &shell->lock_surface->surface->link;
508 else
509 list = &shell->compositor->surface_list;
510
511 wl_list_remove(&surface->surface->link);
512 wl_list_insert(list, &surface->surface->link);
513 wlsc_surface_configure(surface->surface,
514 surface->surface->x,
515 surface->surface->y,
516 surface->surface->width,
517 surface->surface->height);
518 surface->surface->output = surface->output;
519}
520
521static void
522hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
523{
524 wl_list_remove(&surface->surface->link);
525 wl_list_init(&surface->surface->link);
526 surface->surface->output = NULL;
527}
528
529static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400530desktop_shell_set_background(struct wl_client *client,
531 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100532 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400533 struct wl_resource *surface_resource)
534{
535 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200536 struct shell_surface *shsurf = surface_resource->data;
537 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200538 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400539
Pekka Paalanen98262232011-12-01 10:42:22 +0200540 if (reset_shell_surface_type(shsurf))
541 return;
542
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100543 wl_list_for_each(priv, &shell->backgrounds, link) {
544 if (priv->output == output_resource->data) {
545 priv->surface->output = NULL;
546 wl_list_remove(&priv->surface->link);
547 wl_list_remove(&priv->link);
548 break;
549 }
550 }
551
Pekka Paalanen068ae942011-11-28 14:11:15 +0200552 shsurf->type = SHELL_SURFACE_BACKGROUND;
553 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100554
Pekka Paalanen068ae942011-11-28 14:11:15 +0200555 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100556
Pekka Paalanen068ae942011-11-28 14:11:15 +0200557 surface->x = shsurf->output->x;
558 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200559
Kristian Høgsberg75840622011-09-06 13:48:16 -0400560 wl_resource_post_event(resource,
561 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200562 wlsc_compositor_get_time(), 0, surface_resource,
563 shsurf->output->current->width,
564 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400565}
566
567static void
568desktop_shell_set_panel(struct wl_client *client,
569 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100570 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400571 struct wl_resource *surface_resource)
572{
573 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200574 struct shell_surface *shsurf = surface_resource->data;
575 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200576 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400577
Pekka Paalanen98262232011-12-01 10:42:22 +0200578 if (reset_shell_surface_type(shsurf))
579 return;
580
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100581 wl_list_for_each(priv, &shell->panels, link) {
582 if (priv->output == output_resource->data) {
583 priv->surface->output = NULL;
584 wl_list_remove(&priv->surface->link);
585 wl_list_remove(&priv->link);
586 break;
587 }
588 }
589
Pekka Paalanen068ae942011-11-28 14:11:15 +0200590 shsurf->type = SHELL_SURFACE_PANEL;
591 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100592
Pekka Paalanen068ae942011-11-28 14:11:15 +0200593 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100594
Pekka Paalanen068ae942011-11-28 14:11:15 +0200595 surface->x = shsurf->output->x;
596 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200597
Kristian Høgsberg75840622011-09-06 13:48:16 -0400598 wl_resource_post_event(resource,
599 DESKTOP_SHELL_CONFIGURE,
600 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200601 shsurf->output->current->width,
602 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400603}
604
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200605static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500606handle_lock_surface_destroy(struct wl_listener *listener,
607 struct wl_resource *resource, uint32_t time)
608{
609 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200610 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500611
612 fprintf(stderr, "lock surface gone\n");
613 shell->lock_surface = NULL;
614}
615
616static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200617desktop_shell_set_lock_surface(struct wl_client *client,
618 struct wl_resource *resource,
619 struct wl_resource *surface_resource)
620{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200621 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200622 struct shell_surface *surface = surface_resource->data;
623
624 if (reset_shell_surface_type(surface))
625 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200626
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200627 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200628
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200629 if (!shell->locked)
630 return;
631
Pekka Paalanen98262232011-12-01 10:42:22 +0200632 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200633
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500634 shell->lock_surface_listener.func = handle_lock_surface_destroy;
635 wl_list_insert(&surface_resource->destroy_listener_list,
636 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200637
Pekka Paalanen068ae942011-11-28 14:11:15 +0200638 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200639}
640
641static void
642resume_desktop(struct wl_shell *shell)
643{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500644 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200645 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200646 struct shell_surface *tmp;
647
648 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
649 hide_screensaver(shell, tmp);
650
651 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200652
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500653 wl_list_for_each(surface, &shell->hidden_surface_list, link)
654 wlsc_surface_configure(surface, surface->x, surface->y,
655 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200656
Pekka Paalanenfe340832011-11-25 16:07:52 +0200657 if (wl_list_empty(&shell->backgrounds)) {
658 list = &shell->compositor->surface_list;
659 } else {
660 struct shell_surface *background;
661 background = container_of(shell->backgrounds.prev,
662 struct shell_surface, link);
663 list = background->surface->link.prev;
664 }
665
666 if (!wl_list_empty(&shell->hidden_surface_list))
667 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500668 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200669
670 shell->locked = false;
671 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200672 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200673}
674
675static void
676desktop_shell_unlock(struct wl_client *client,
677 struct wl_resource *resource)
678{
679 struct wl_shell *shell = resource->data;
680
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200681 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200682
683 if (shell->locked)
684 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200685}
686
Kristian Høgsberg75840622011-09-06 13:48:16 -0400687static const struct desktop_shell_interface desktop_shell_implementation = {
688 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200689 desktop_shell_set_panel,
690 desktop_shell_set_lock_surface,
691 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400692};
693
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200694static enum shell_surface_type
695get_shell_surface_type(struct wlsc_surface *surface)
696{
697 struct shell_surface *shsurf;
698
699 shsurf = get_shell_surface(surface);
700 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200701 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200702 return shsurf->type;
703}
704
Kristian Høgsberg75840622011-09-06 13:48:16 -0400705static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400706move_binding(struct wl_input_device *device, uint32_t time,
707 uint32_t key, uint32_t button, uint32_t state, void *data)
708{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400709 struct wlsc_surface *surface =
710 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500711
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100712 if (surface == NULL)
713 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400714
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200715 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100716 case SHELL_SURFACE_PANEL:
717 case SHELL_SURFACE_BACKGROUND:
718 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200719 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100720 return;
721 default:
722 break;
723 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400724
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400725 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400726}
727
728static void
729resize_binding(struct wl_input_device *device, uint32_t time,
730 uint32_t key, uint32_t button, uint32_t state, void *data)
731{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400732 struct wlsc_surface *surface =
733 (struct wlsc_surface *) device->pointer_focus;
734 uint32_t edges = 0;
735 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200736 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500737
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100738 if (surface == NULL)
739 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200740
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200741 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200742 if (!shsurf)
743 return;
744
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200745 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100746 case SHELL_SURFACE_PANEL:
747 case SHELL_SURFACE_BACKGROUND:
748 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200749 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100750 return;
751 default:
752 break;
753 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400754
Kristian Høgsberg07937562011-04-12 17:25:42 -0400755 x = device->grab_x - surface->x;
756 y = device->grab_y - surface->y;
757
758 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200759 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400760 else if (x < 2 * surface->width / 3)
761 edges |= 0;
762 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200763 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400764
765 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200766 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400767 else if (y < 2 * surface->height / 3)
768 edges |= 0;
769 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200770 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400771
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200772 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
773 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400774}
775
776static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400777activate(struct wlsc_shell *base, struct wlsc_surface *es,
778 struct wlsc_input_device *device, uint32_t time)
779{
780 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
781 struct wlsc_compositor *compositor = shell->compositor;
782
783 wlsc_surface_activate(es, device, time);
784
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400785 if (compositor->wxs)
786 wlsc_xserver_surface_activate(es);
787
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200788 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200789 case SHELL_SURFACE_BACKGROUND:
790 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400791 wl_list_remove(&es->link);
792 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200793 break;
794 case SHELL_SURFACE_PANEL:
795 /* already put on top */
796 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200797 case SHELL_SURFACE_SCREENSAVER:
798 /* always below lock surface */
799 if (shell->lock_surface) {
800 wl_list_remove(&es->link);
801 wl_list_insert(&shell->lock_surface->surface->link,
802 &es->link);
803 }
804 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200805 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100806 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200807 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100808 struct shell_surface *panel;
809 wl_list_for_each(panel, &shell->panels, link) {
810 wl_list_remove(&panel->surface->link);
811 wl_list_insert(&compositor->surface_list,
812 &panel->surface->link);
813 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200814 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400815 }
816}
817
818static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200819lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400820{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200821 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200822 struct wl_list *surface_list = &shell->compositor->surface_list;
823 struct wlsc_surface *cur;
824 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200825 struct wlsc_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200826 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200827 uint32_t time;
828
829 if (shell->locked)
830 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200831
832 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200833
834 /* Move all surfaces from compositor's list to our hidden list,
835 * except the background. This way nothing else can show or
836 * receive input events while we are locked. */
837
838 if (!wl_list_empty(&shell->hidden_surface_list)) {
839 fprintf(stderr,
840 "%s: Assertion failed: hidden_surface_list is not empty.\n",
841 __func__);
842 }
843
844 wl_list_for_each_safe(cur, tmp, surface_list, link) {
845 /* skip input device sprites, cur->surface is uninitialised */
846 if (cur->surface.resource.client == NULL)
847 continue;
848
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200849 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200850 continue;
851
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500852 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200853 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500854 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200855 }
856
Pekka Paalanen77346a62011-11-30 16:26:35 +0200857 launch_screensaver(shell);
858
859 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
860 show_screensaver(shell, shsurf);
861
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200862 /* reset pointer foci */
863 wlsc_compositor_repick(shell->compositor);
864
865 /* reset keyboard foci */
866 time = wlsc_compositor_get_time();
867 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
868 wl_input_device_set_keyboard_focus(&device->input_device,
869 NULL, time);
870 }
871
872 /* TODO: disable bindings that should not work while locked. */
873
874 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200875}
876
877static void
878unlock(struct wlsc_shell *base)
879{
880 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
881
Pekka Paalanend81c2162011-11-16 13:47:34 +0200882 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200883 wlsc_compositor_wake(shell->compositor);
884 return;
885 }
886
887 /* If desktop-shell client has gone away, unlock immediately. */
888 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200889 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200890 return;
891 }
892
893 if (shell->prepare_event_sent)
894 return;
895
896 wl_resource_post_event(shell->child.desktop_shell,
897 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
898 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400899}
900
901static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200902center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
903{
904 struct wlsc_mode *mode = output->current;
905
906 surface->x = output->x + (mode->width - surface->width) / 2;
907 surface->y = output->y + (mode->height - surface->height) / 2;
908}
909
910static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500911map(struct wlsc_shell *base,
912 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400913{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400914 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
915 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500916 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200917 struct shell_surface *shsurf;
918 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
919 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200920
Pekka Paalanen77346a62011-11-30 16:26:35 +0200921 shsurf = get_shell_surface(surface);
922 if (shsurf)
923 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500924
Pekka Paalanen77346a62011-11-30 16:26:35 +0200925 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500926 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200927 do_configure = 0;
928 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500929 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200930 do_configure = 1;
931 }
932
933 surface->width = width;
934 surface->height = height;
935
936 /* initial positioning, see also configure() */
937 switch (surface_type) {
938 case SHELL_SURFACE_TOPLEVEL:
939 surface->x = 10 + random() % 400;
940 surface->y = 10 + random() % 400;
941 break;
942 case SHELL_SURFACE_SCREENSAVER:
943 case SHELL_SURFACE_FULLSCREEN:
944 center_on_output(surface, surface->fullscreen_output);
945 break;
946 default:
947 ;
948 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400949
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200950 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200951 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200952 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200953 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500954 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200955 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200956 break;
957 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200958 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500959 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200960 break;
961 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200962 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500963 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200964
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500965 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200966 wlsc_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200967 do_configure = 1;
968 break;
969 case SHELL_SURFACE_SCREENSAVER:
970 /* If locked, show it. */
971 if (shell->locked)
972 show_screensaver(shell, shsurf);
973 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200974 break;
975 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200976 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100977 if (!wl_list_empty(&shell->panels)) {
978 struct shell_surface *panel =
979 container_of(shell->panels.prev,
980 struct shell_surface, link);
981 wl_list_insert(&panel->surface->link, &surface->link);
982 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200983 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100984 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200985 }
986
Pekka Paalanen77346a62011-11-30 16:26:35 +0200987 if (do_configure)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500988 wlsc_surface_configure(surface,
989 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -0500990
991 if (surface_type == SHELL_SURFACE_TOPLEVEL)
992 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500993}
994
995static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200996configure(struct wlsc_shell *base, struct wlsc_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500997 int32_t x, int32_t y, int32_t width, int32_t height)
998{
Pekka Paalanen77346a62011-11-30 16:26:35 +0200999 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1000 int do_configure = !shell->locked;
1001 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1002 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001003
Pekka Paalanen77346a62011-11-30 16:26:35 +02001004 shsurf = get_shell_surface(surface);
1005 if (shsurf)
1006 surface_type = shsurf->type;
1007
1008 surface->width = width;
1009 surface->height = height;
1010
1011 switch (surface_type) {
1012 case SHELL_SURFACE_SCREENSAVER:
1013 do_configure = !do_configure;
1014 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001015 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001016 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001017 break;
1018 default:
1019 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001020 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001021
Pekka Paalanen77346a62011-11-30 16:26:35 +02001022 /*
1023 * wlsc_surface_configure() will assign an output, which means
1024 * the surface is supposed to be in compositor->surface_list.
1025 * Be careful with that, and make sure we stay on the right output.
1026 * XXX: would a fullscreen surface need the same handling?
1027 */
1028 if (do_configure) {
1029 wlsc_surface_configure(surface, x, y, width, height);
1030
1031 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1032 surface->output = shsurf->output;
1033 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001034}
1035
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001036static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001037desktop_shell_sigchld(struct wlsc_process *process, int status)
1038{
1039 struct wl_shell *shell =
1040 container_of(process, struct wl_shell, child.process);
1041
1042 shell->child.process.pid = 0;
1043 shell->child.client = NULL; /* already destroyed by wayland */
1044}
1045
1046static int
1047launch_desktop_shell_process(struct wl_shell *shell)
1048{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -05001049 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001050
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001051 shell->child.client = wlsc_client_launch(shell->compositor,
1052 &shell->child.process,
1053 shell_exe,
1054 desktop_shell_sigchld);
1055
1056 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001057 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001058 return 0;
1059}
1060
1061static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001062bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1063{
1064 struct wl_shell *shell = data;
1065
1066 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001067 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001068}
1069
Kristian Høgsberg75840622011-09-06 13:48:16 -04001070static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001071unbind_desktop_shell(struct wl_resource *resource)
1072{
1073 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001074
1075 if (shell->locked)
1076 resume_desktop(shell);
1077
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001078 shell->child.desktop_shell = NULL;
1079 shell->prepare_event_sent = false;
1080 free(resource);
1081}
1082
1083static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001084bind_desktop_shell(struct wl_client *client,
1085 void *data, uint32_t version, uint32_t id)
1086{
1087 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001088 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001089
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001090 resource = wl_client_add_object(client, &desktop_shell_interface,
1091 &desktop_shell_implementation,
1092 id, shell);
1093
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001094 if (client == shell->child.client) {
1095 resource->destroy = unbind_desktop_shell;
1096 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001097 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001098 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001099
1100 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1101 "permission to bind desktop_shell denied");
1102 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001103}
1104
Pekka Paalanen6e168112011-11-24 11:34:05 +02001105static void
1106screensaver_set_surface(struct wl_client *client,
1107 struct wl_resource *resource,
1108 struct wl_resource *shell_surface_resource,
1109 struct wl_resource *output_resource)
1110{
1111 struct wl_shell *shell = resource->data;
1112 struct shell_surface *surface = shell_surface_resource->data;
1113 struct wlsc_output *output = output_resource->data;
1114
Pekka Paalanen98262232011-12-01 10:42:22 +02001115 if (reset_shell_surface_type(surface))
1116 return;
1117
Pekka Paalanen77346a62011-11-30 16:26:35 +02001118 surface->type = SHELL_SURFACE_SCREENSAVER;
1119
1120 surface->surface->fullscreen_output = output;
1121 surface->output = output;
1122 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001123}
1124
1125static const struct screensaver_interface screensaver_implementation = {
1126 screensaver_set_surface
1127};
1128
1129static void
1130unbind_screensaver(struct wl_resource *resource)
1131{
1132 struct wl_shell *shell = resource->data;
1133
Pekka Paalanen77346a62011-11-30 16:26:35 +02001134 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001135 free(resource);
1136}
1137
1138static void
1139bind_screensaver(struct wl_client *client,
1140 void *data, uint32_t version, uint32_t id)
1141{
1142 struct wl_shell *shell = data;
1143 struct wl_resource *resource;
1144
1145 resource = wl_client_add_object(client, &screensaver_interface,
1146 &screensaver_implementation,
1147 id, shell);
1148
Pekka Paalanen77346a62011-11-30 16:26:35 +02001149 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001150 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001151 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001152 return;
1153 }
1154
1155 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1156 "interface object already bound");
1157 wl_resource_destroy(resource, 0);
1158}
1159
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001160int
1161shell_init(struct wlsc_compositor *ec);
1162
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001163WL_EXPORT int
1164shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001165{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001166 struct wl_shell *shell;
1167
1168 shell = malloc(sizeof *shell);
1169 if (shell == NULL)
1170 return -1;
1171
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001172 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001173 shell->compositor = ec;
1174 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001175 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001176 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001177 shell->shell.map = map;
1178 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001179
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001180 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001181 wl_list_init(&shell->backgrounds);
1182 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001183 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001184
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001185 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1186 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001187 return -1;
1188
Kristian Høgsberg75840622011-09-06 13:48:16 -04001189 if (wl_display_add_global(ec->wl_display,
1190 &desktop_shell_interface,
1191 shell, bind_desktop_shell) == NULL)
1192 return -1;
1193
Pekka Paalanen6e168112011-11-24 11:34:05 +02001194 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1195 shell, bind_screensaver) == NULL)
1196 return -1;
1197
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001198 if (launch_desktop_shell_process(shell) != 0)
1199 return -1;
1200
Kristian Høgsberg07937562011-04-12 17:25:42 -04001201 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001202 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001203 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001204 resize_binding, shell);
1205
1206 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001207
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001208 return 0;
1209}