blob: e32e8bcd35b022573b5c6a66a99467aa69c9b232 [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
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200363static struct wlsc_output *
364get_default_output(struct wlsc_compositor *compositor)
365{
366 return container_of(compositor->output_list.next,
367 struct wlsc_output, link);
368}
369
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400370static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200371shell_surface_set_fullscreen(struct wl_client *client,
372 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400373
374{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200375 struct shell_surface *shsurf = resource->data;
376 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400377 struct wlsc_output *output;
378
Pekka Paalanen98262232011-12-01 10:42:22 +0200379 if (reset_shell_surface_type(shsurf))
380 return;
381
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400382 /* FIXME: Fullscreen on first output */
383 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200384 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400385 es->output = output;
386
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200387 shsurf->saved_x = es->x;
388 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400389 es->x = (output->current->width - es->width) / 2;
390 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400391 es->fullscreen_output = output;
392 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200393 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400394}
395
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200396static const struct wl_shell_surface_interface shell_surface_implementation = {
397 shell_surface_move,
398 shell_surface_resize,
399 shell_surface_set_toplevel,
400 shell_surface_set_transient,
401 shell_surface_set_fullscreen
402};
403
404static void
405destroy_shell_surface(struct wl_resource *resource)
406{
407 struct shell_surface *shsurf = resource->data;
408
409 /* in case cleaning up a dead client destroys shell_surface first */
410 if (shsurf->surface)
411 wl_list_remove(&shsurf->surface_destroy_listener.link);
412
413 wl_list_remove(&shsurf->link);
414 free(shsurf);
415}
416
417static void
418shell_handle_surface_destroy(struct wl_listener *listener,
419 struct wl_resource *resource, uint32_t time)
420{
421 struct shell_surface *shsurf = container_of(listener,
422 struct shell_surface,
423 surface_destroy_listener);
424
425 shsurf->surface = NULL;
426 wl_resource_destroy(&shsurf->resource, time);
427}
428
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200429static struct shell_surface *
430get_shell_surface(struct wlsc_surface *surface)
431{
432 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
433 struct wl_listener *listener;
434
435 /* search the destroy listener list for our callback */
436 wl_list_for_each(listener, lst, link) {
437 if (listener->func == shell_handle_surface_destroy) {
438 return container_of(listener, struct shell_surface,
439 surface_destroy_listener);
440 }
441 }
442
443 return NULL;
444}
445
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200446static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200447shell_get_shell_surface(struct wl_client *client,
448 struct wl_resource *resource,
449 uint32_t id,
450 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200451{
452 struct wlsc_surface *surface = surface_resource->data;
453 struct shell_surface *shsurf;
454
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200455 if (get_shell_surface(surface)) {
456 wl_resource_post_error(surface_resource,
457 WL_DISPLAY_ERROR_INVALID_OBJECT,
458 "wl_shell::get_shell_surface already requested");
459 return;
460 }
461
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200462 shsurf = calloc(1, sizeof *shsurf);
463 if (!shsurf) {
464 wl_resource_post_no_memory(resource);
465 return;
466 }
467
468 shsurf->resource.destroy = destroy_shell_surface;
469 shsurf->resource.object.id = id;
470 shsurf->resource.object.interface = &wl_shell_surface_interface;
471 shsurf->resource.object.implementation =
472 (void (**)(void)) &shell_surface_implementation;
473 shsurf->resource.data = shsurf;
474
475 shsurf->surface = surface;
476 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
477 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
478 &shsurf->surface_destroy_listener.link);
479
480 /* init link so its safe to always remove it in destroy_shell_surface */
481 wl_list_init(&shsurf->link);
482
Pekka Paalanen98262232011-12-01 10:42:22 +0200483 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200484
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200485 wl_client_add_resource(client, &shsurf->resource);
486}
487
488static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200489 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500490};
491
Kristian Høgsberg07937562011-04-12 17:25:42 -0400492static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200493launch_screensaver(struct wl_shell *shell)
494{
495 if (shell->screensaver.binding)
496 return;
497
498 /* TODO: exec() the screensaver process */
499}
500
501static void
502terminate_screensaver(struct wl_shell *shell)
503{
504 /* TODO */
505}
506
507static void
508show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
509{
510 struct wl_list *list;
511
512 if (shell->lock_surface)
513 list = &shell->lock_surface->surface->link;
514 else
515 list = &shell->compositor->surface_list;
516
517 wl_list_remove(&surface->surface->link);
518 wl_list_insert(list, &surface->surface->link);
519 wlsc_surface_configure(surface->surface,
520 surface->surface->x,
521 surface->surface->y,
522 surface->surface->width,
523 surface->surface->height);
524 surface->surface->output = surface->output;
525}
526
527static void
528hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
529{
530 wl_list_remove(&surface->surface->link);
531 wl_list_init(&surface->surface->link);
532 surface->surface->output = NULL;
533}
534
535static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400536desktop_shell_set_background(struct wl_client *client,
537 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100538 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400539 struct wl_resource *surface_resource)
540{
541 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200542 struct shell_surface *shsurf = surface_resource->data;
543 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200544 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400545
Pekka Paalanen98262232011-12-01 10:42:22 +0200546 if (reset_shell_surface_type(shsurf))
547 return;
548
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100549 wl_list_for_each(priv, &shell->backgrounds, link) {
550 if (priv->output == output_resource->data) {
551 priv->surface->output = NULL;
552 wl_list_remove(&priv->surface->link);
553 wl_list_remove(&priv->link);
554 break;
555 }
556 }
557
Pekka Paalanen068ae942011-11-28 14:11:15 +0200558 shsurf->type = SHELL_SURFACE_BACKGROUND;
559 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100560
Pekka Paalanen068ae942011-11-28 14:11:15 +0200561 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100562
Pekka Paalanen068ae942011-11-28 14:11:15 +0200563 surface->x = shsurf->output->x;
564 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200565
Kristian Høgsberg75840622011-09-06 13:48:16 -0400566 wl_resource_post_event(resource,
567 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200568 wlsc_compositor_get_time(), 0, surface_resource,
569 shsurf->output->current->width,
570 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400571}
572
573static void
574desktop_shell_set_panel(struct wl_client *client,
575 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100576 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400577 struct wl_resource *surface_resource)
578{
579 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200580 struct shell_surface *shsurf = surface_resource->data;
581 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200582 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400583
Pekka Paalanen98262232011-12-01 10:42:22 +0200584 if (reset_shell_surface_type(shsurf))
585 return;
586
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100587 wl_list_for_each(priv, &shell->panels, link) {
588 if (priv->output == output_resource->data) {
589 priv->surface->output = NULL;
590 wl_list_remove(&priv->surface->link);
591 wl_list_remove(&priv->link);
592 break;
593 }
594 }
595
Pekka Paalanen068ae942011-11-28 14:11:15 +0200596 shsurf->type = SHELL_SURFACE_PANEL;
597 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100598
Pekka Paalanen068ae942011-11-28 14:11:15 +0200599 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100600
Pekka Paalanen068ae942011-11-28 14:11:15 +0200601 surface->x = shsurf->output->x;
602 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200603
Kristian Høgsberg75840622011-09-06 13:48:16 -0400604 wl_resource_post_event(resource,
605 DESKTOP_SHELL_CONFIGURE,
606 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200607 shsurf->output->current->width,
608 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400609}
610
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200611static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500612handle_lock_surface_destroy(struct wl_listener *listener,
613 struct wl_resource *resource, uint32_t time)
614{
615 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200616 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500617
618 fprintf(stderr, "lock surface gone\n");
619 shell->lock_surface = NULL;
620}
621
622static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200623desktop_shell_set_lock_surface(struct wl_client *client,
624 struct wl_resource *resource,
625 struct wl_resource *surface_resource)
626{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200627 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200628 struct shell_surface *surface = surface_resource->data;
629
630 if (reset_shell_surface_type(surface))
631 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200632
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200633 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200634
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200635 if (!shell->locked)
636 return;
637
Pekka Paalanen98262232011-12-01 10:42:22 +0200638 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200639
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500640 shell->lock_surface_listener.func = handle_lock_surface_destroy;
641 wl_list_insert(&surface_resource->destroy_listener_list,
642 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200643
Pekka Paalanen068ae942011-11-28 14:11:15 +0200644 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200645}
646
647static void
648resume_desktop(struct wl_shell *shell)
649{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500650 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200651 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200652 struct shell_surface *tmp;
653
654 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
655 hide_screensaver(shell, tmp);
656
657 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200658
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500659 wl_list_for_each(surface, &shell->hidden_surface_list, link)
660 wlsc_surface_configure(surface, surface->x, surface->y,
661 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200662
Pekka Paalanenfe340832011-11-25 16:07:52 +0200663 if (wl_list_empty(&shell->backgrounds)) {
664 list = &shell->compositor->surface_list;
665 } else {
666 struct shell_surface *background;
667 background = container_of(shell->backgrounds.prev,
668 struct shell_surface, link);
669 list = background->surface->link.prev;
670 }
671
672 if (!wl_list_empty(&shell->hidden_surface_list))
673 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500674 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200675
676 shell->locked = false;
677 wlsc_compositor_repick(shell->compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200678 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200679}
680
681static void
682desktop_shell_unlock(struct wl_client *client,
683 struct wl_resource *resource)
684{
685 struct wl_shell *shell = resource->data;
686
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200687 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200688
689 if (shell->locked)
690 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200691}
692
Kristian Høgsberg75840622011-09-06 13:48:16 -0400693static const struct desktop_shell_interface desktop_shell_implementation = {
694 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200695 desktop_shell_set_panel,
696 desktop_shell_set_lock_surface,
697 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400698};
699
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200700static enum shell_surface_type
701get_shell_surface_type(struct wlsc_surface *surface)
702{
703 struct shell_surface *shsurf;
704
705 shsurf = get_shell_surface(surface);
706 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200707 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200708 return shsurf->type;
709}
710
Kristian Høgsberg75840622011-09-06 13:48:16 -0400711static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400712move_binding(struct wl_input_device *device, uint32_t time,
713 uint32_t key, uint32_t button, uint32_t state, void *data)
714{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400715 struct wlsc_surface *surface =
716 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500717
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100718 if (surface == NULL)
719 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400720
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200721 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100722 case SHELL_SURFACE_PANEL:
723 case SHELL_SURFACE_BACKGROUND:
724 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200725 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100726 return;
727 default:
728 break;
729 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400730
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400731 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400732}
733
734static void
735resize_binding(struct wl_input_device *device, uint32_t time,
736 uint32_t key, uint32_t button, uint32_t state, void *data)
737{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400738 struct wlsc_surface *surface =
739 (struct wlsc_surface *) device->pointer_focus;
740 uint32_t edges = 0;
741 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200742 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500743
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100744 if (surface == NULL)
745 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200746
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200747 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200748 if (!shsurf)
749 return;
750
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200751 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100752 case SHELL_SURFACE_PANEL:
753 case SHELL_SURFACE_BACKGROUND:
754 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200755 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100756 return;
757 default:
758 break;
759 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400760
Kristian Høgsberg07937562011-04-12 17:25:42 -0400761 x = device->grab_x - surface->x;
762 y = device->grab_y - surface->y;
763
764 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200765 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400766 else if (x < 2 * surface->width / 3)
767 edges |= 0;
768 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200769 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400770
771 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200772 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400773 else if (y < 2 * surface->height / 3)
774 edges |= 0;
775 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200776 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400777
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200778 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
779 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400780}
781
782static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400783activate(struct wlsc_shell *base, struct wlsc_surface *es,
784 struct wlsc_input_device *device, uint32_t time)
785{
786 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
787 struct wlsc_compositor *compositor = shell->compositor;
788
789 wlsc_surface_activate(es, device, time);
790
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400791 if (compositor->wxs)
792 wlsc_xserver_surface_activate(es);
793
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200794 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200795 case SHELL_SURFACE_BACKGROUND:
796 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400797 wl_list_remove(&es->link);
798 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200799 break;
800 case SHELL_SURFACE_PANEL:
801 /* already put on top */
802 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200803 case SHELL_SURFACE_SCREENSAVER:
804 /* always below lock surface */
805 if (shell->lock_surface) {
806 wl_list_remove(&es->link);
807 wl_list_insert(&shell->lock_surface->surface->link,
808 &es->link);
809 }
810 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200811 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100812 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200813 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100814 struct shell_surface *panel;
815 wl_list_for_each(panel, &shell->panels, link) {
816 wl_list_remove(&panel->surface->link);
817 wl_list_insert(&compositor->surface_list,
818 &panel->surface->link);
819 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200820 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400821 }
822}
823
824static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200825lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400826{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200827 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200828 struct wl_list *surface_list = &shell->compositor->surface_list;
829 struct wlsc_surface *cur;
830 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200831 struct wlsc_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200832 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200833 uint32_t time;
834
835 if (shell->locked)
836 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200837
838 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200839
840 /* Move all surfaces from compositor's list to our hidden list,
841 * except the background. This way nothing else can show or
842 * receive input events while we are locked. */
843
844 if (!wl_list_empty(&shell->hidden_surface_list)) {
845 fprintf(stderr,
846 "%s: Assertion failed: hidden_surface_list is not empty.\n",
847 __func__);
848 }
849
850 wl_list_for_each_safe(cur, tmp, surface_list, link) {
851 /* skip input device sprites, cur->surface is uninitialised */
852 if (cur->surface.resource.client == NULL)
853 continue;
854
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200855 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200856 continue;
857
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500858 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200859 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500860 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200861 }
862
Pekka Paalanen77346a62011-11-30 16:26:35 +0200863 launch_screensaver(shell);
864
865 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
866 show_screensaver(shell, shsurf);
867
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200868 if (!wl_list_empty(&shell->screensaver.surfaces))
869 wlsc_compositor_wake(shell->compositor);
870
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200871 /* reset pointer foci */
872 wlsc_compositor_repick(shell->compositor);
873
874 /* reset keyboard foci */
875 time = wlsc_compositor_get_time();
876 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
877 wl_input_device_set_keyboard_focus(&device->input_device,
878 NULL, time);
879 }
880
881 /* TODO: disable bindings that should not work while locked. */
882
883 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200884}
885
886static void
887unlock(struct wlsc_shell *base)
888{
889 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
890
Pekka Paalanend81c2162011-11-16 13:47:34 +0200891 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200892 wlsc_compositor_wake(shell->compositor);
893 return;
894 }
895
896 /* If desktop-shell client has gone away, unlock immediately. */
897 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200898 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200899 return;
900 }
901
902 if (shell->prepare_event_sent)
903 return;
904
905 wl_resource_post_event(shell->child.desktop_shell,
906 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
907 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400908}
909
910static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200911center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
912{
913 struct wlsc_mode *mode = output->current;
914
915 surface->x = output->x + (mode->width - surface->width) / 2;
916 surface->y = output->y + (mode->height - surface->height) / 2;
917}
918
919static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500920map(struct wlsc_shell *base,
921 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400922{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400923 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
924 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500925 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200926 struct shell_surface *shsurf;
927 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
928 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200929
Pekka Paalanen77346a62011-11-30 16:26:35 +0200930 shsurf = get_shell_surface(surface);
931 if (shsurf)
932 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500933
Pekka Paalanen77346a62011-11-30 16:26:35 +0200934 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500935 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200936 do_configure = 0;
937 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500938 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200939 do_configure = 1;
940 }
941
942 surface->width = width;
943 surface->height = height;
944
945 /* initial positioning, see also configure() */
946 switch (surface_type) {
947 case SHELL_SURFACE_TOPLEVEL:
948 surface->x = 10 + random() % 400;
949 surface->y = 10 + random() % 400;
950 break;
951 case SHELL_SURFACE_SCREENSAVER:
952 case SHELL_SURFACE_FULLSCREEN:
953 center_on_output(surface, surface->fullscreen_output);
954 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200955 case SHELL_SURFACE_LOCK:
956 center_on_output(surface, get_default_output(compositor));
957 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200958 default:
959 ;
960 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400961
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200962 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200963 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200964 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200965 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500966 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200967 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200968 break;
969 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200970 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500971 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200972 break;
973 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200974 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500975 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200976
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500977 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200978 wlsc_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200979 do_configure = 1;
980 break;
981 case SHELL_SURFACE_SCREENSAVER:
982 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200983 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +0200984 show_screensaver(shell, shsurf);
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200985 wlsc_compositor_wake(compositor);
986 }
Pekka Paalanen77346a62011-11-30 16:26:35 +0200987 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200988 break;
989 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +0200990 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100991 if (!wl_list_empty(&shell->panels)) {
992 struct shell_surface *panel =
993 container_of(shell->panels.prev,
994 struct shell_surface, link);
995 wl_list_insert(&panel->surface->link, &surface->link);
996 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200997 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100998 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200999 }
1000
Pekka Paalanen77346a62011-11-30 16:26:35 +02001001 if (do_configure)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001002 wlsc_surface_configure(surface,
1003 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001004
1005 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1006 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001007}
1008
1009static void
Pekka Paalanen77346a62011-11-30 16:26:35 +02001010configure(struct wlsc_shell *base, struct wlsc_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001011 int32_t x, int32_t y, int32_t width, int32_t height)
1012{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001013 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1014 int do_configure = !shell->locked;
1015 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1016 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001017
Pekka Paalanen77346a62011-11-30 16:26:35 +02001018 shsurf = get_shell_surface(surface);
1019 if (shsurf)
1020 surface_type = shsurf->type;
1021
1022 surface->width = width;
1023 surface->height = height;
1024
1025 switch (surface_type) {
1026 case SHELL_SURFACE_SCREENSAVER:
1027 do_configure = !do_configure;
1028 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001029 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001030 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001031 break;
1032 default:
1033 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001034 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001035
Pekka Paalanen77346a62011-11-30 16:26:35 +02001036 /*
1037 * wlsc_surface_configure() will assign an output, which means
1038 * the surface is supposed to be in compositor->surface_list.
1039 * Be careful with that, and make sure we stay on the right output.
1040 * XXX: would a fullscreen surface need the same handling?
1041 */
1042 if (do_configure) {
1043 wlsc_surface_configure(surface, x, y, width, height);
1044
1045 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1046 surface->output = shsurf->output;
1047 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001048}
1049
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001050static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001051desktop_shell_sigchld(struct wlsc_process *process, int status)
1052{
1053 struct wl_shell *shell =
1054 container_of(process, struct wl_shell, child.process);
1055
1056 shell->child.process.pid = 0;
1057 shell->child.client = NULL; /* already destroyed by wayland */
1058}
1059
1060static int
1061launch_desktop_shell_process(struct wl_shell *shell)
1062{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -05001063 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001064
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001065 shell->child.client = wlsc_client_launch(shell->compositor,
1066 &shell->child.process,
1067 shell_exe,
1068 desktop_shell_sigchld);
1069
1070 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001071 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001072 return 0;
1073}
1074
1075static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001076bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1077{
1078 struct wl_shell *shell = data;
1079
1080 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001081 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001082}
1083
Kristian Høgsberg75840622011-09-06 13:48:16 -04001084static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001085unbind_desktop_shell(struct wl_resource *resource)
1086{
1087 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001088
1089 if (shell->locked)
1090 resume_desktop(shell);
1091
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001092 shell->child.desktop_shell = NULL;
1093 shell->prepare_event_sent = false;
1094 free(resource);
1095}
1096
1097static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001098bind_desktop_shell(struct wl_client *client,
1099 void *data, uint32_t version, uint32_t id)
1100{
1101 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001102 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001103
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001104 resource = wl_client_add_object(client, &desktop_shell_interface,
1105 &desktop_shell_implementation,
1106 id, shell);
1107
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001108 if (client == shell->child.client) {
1109 resource->destroy = unbind_desktop_shell;
1110 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001111 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001112 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001113
1114 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1115 "permission to bind desktop_shell denied");
1116 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001117}
1118
Pekka Paalanen6e168112011-11-24 11:34:05 +02001119static void
1120screensaver_set_surface(struct wl_client *client,
1121 struct wl_resource *resource,
1122 struct wl_resource *shell_surface_resource,
1123 struct wl_resource *output_resource)
1124{
1125 struct wl_shell *shell = resource->data;
1126 struct shell_surface *surface = shell_surface_resource->data;
1127 struct wlsc_output *output = output_resource->data;
1128
Pekka Paalanen98262232011-12-01 10:42:22 +02001129 if (reset_shell_surface_type(surface))
1130 return;
1131
Pekka Paalanen77346a62011-11-30 16:26:35 +02001132 surface->type = SHELL_SURFACE_SCREENSAVER;
1133
1134 surface->surface->fullscreen_output = output;
1135 surface->output = output;
1136 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001137}
1138
1139static const struct screensaver_interface screensaver_implementation = {
1140 screensaver_set_surface
1141};
1142
1143static void
1144unbind_screensaver(struct wl_resource *resource)
1145{
1146 struct wl_shell *shell = resource->data;
1147
Pekka Paalanen77346a62011-11-30 16:26:35 +02001148 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001149 free(resource);
1150}
1151
1152static void
1153bind_screensaver(struct wl_client *client,
1154 void *data, uint32_t version, uint32_t id)
1155{
1156 struct wl_shell *shell = data;
1157 struct wl_resource *resource;
1158
1159 resource = wl_client_add_object(client, &screensaver_interface,
1160 &screensaver_implementation,
1161 id, shell);
1162
Pekka Paalanen77346a62011-11-30 16:26:35 +02001163 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001164 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001165 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001166 return;
1167 }
1168
1169 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1170 "interface object already bound");
1171 wl_resource_destroy(resource, 0);
1172}
1173
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001174int
1175shell_init(struct wlsc_compositor *ec);
1176
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001177WL_EXPORT int
1178shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001179{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001180 struct wl_shell *shell;
1181
1182 shell = malloc(sizeof *shell);
1183 if (shell == NULL)
1184 return -1;
1185
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001186 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001187 shell->compositor = ec;
1188 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001189 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001190 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001191 shell->shell.map = map;
1192 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001193
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001194 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001195 wl_list_init(&shell->backgrounds);
1196 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001197 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001198
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001199 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1200 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001201 return -1;
1202
Kristian Høgsberg75840622011-09-06 13:48:16 -04001203 if (wl_display_add_global(ec->wl_display,
1204 &desktop_shell_interface,
1205 shell, bind_desktop_shell) == NULL)
1206 return -1;
1207
Pekka Paalanen6e168112011-11-24 11:34:05 +02001208 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1209 shell, bind_screensaver) == NULL)
1210 return -1;
1211
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001212 if (launch_desktop_shell_process(shell) != 0)
1213 return -1;
1214
Kristian Høgsberg07937562011-04-12 17:25:42 -04001215 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001216 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001217 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001218 resize_binding, shell);
1219
1220 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001221
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001222 return 0;
1223}