blob: db92b5e40a26449739e1e1079374d3a74d8bb73d [file] [log] [blame]
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001/*
2 * Copyright © 2010 Intel Corporation
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02003 * Copyright © 2011 Collabora, Ltd.
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05004 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#include <stdlib.h>
Kristian Høgsberg75840622011-09-06 13:48:16 -040025#include <stdio.h>
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020026#include <stdbool.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050027#include <string.h>
28#include <unistd.h>
Kristian Høgsberg07937562011-04-12 17:25:42 -040029#include <linux/input.h>
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020030#include <assert.h>
Pekka Paalanen18027e52011-12-02 16:31:49 +020031#include <signal.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050032
Pekka Paalanen50719bc2011-11-22 14:18:50 +020033#include <wayland-server.h>
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050034#include "compositor.h"
Kristian Høgsberg75840622011-09-06 13:48:16 -040035#include "desktop-shell-server-protocol.h"
Pekka Paalanene955f1e2011-12-07 11:49:52 +020036#include "../shared/config-parser.h"
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050037
Pekka Paalanen068ae942011-11-28 14:11:15 +020038struct shell_surface;
39
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040040struct wl_shell {
Kristian Høgsberg75840622011-09-06 13:48:16 -040041 struct wlsc_compositor *compositor;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040042 struct wlsc_shell shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020043
44 struct {
45 struct wlsc_process process;
46 struct wl_client *client;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020047 struct wl_resource *desktop_shell;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +020048 } child;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +020049
50 bool locked;
51 bool prepare_event_sent;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020052
Pekka Paalanen068ae942011-11-28 14:11:15 +020053 struct shell_surface *lock_surface;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -050054 struct wl_listener lock_surface_listener;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +020055 struct wl_list hidden_surface_list;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010056
57 struct wl_list backgrounds;
58 struct wl_list panels;
Pekka Paalanen6e168112011-11-24 11:34:05 +020059
Pekka Paalanen77346a62011-11-30 16:26:35 +020060 struct {
Pekka Paalanene955f1e2011-12-07 11:49:52 +020061 const char *path;
Pekka Paalanen7296e792011-12-07 16:22:00 +020062 int duration;
Pekka Paalanen77346a62011-11-30 16:26:35 +020063 struct wl_resource *binding;
64 struct wl_list surfaces;
Pekka Paalanen18027e52011-12-02 16:31:49 +020065 struct wlsc_process process;
Pekka Paalanen77346a62011-11-30 16:26:35 +020066 } screensaver;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040067};
68
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050069enum shell_surface_type {
Pekka Paalanen98262232011-12-01 10:42:22 +020070 SHELL_SURFACE_NONE,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050071
Pekka Paalanen57da4a82011-11-23 16:42:16 +020072 SHELL_SURFACE_PANEL,
73 SHELL_SURFACE_BACKGROUND,
74 SHELL_SURFACE_LOCK,
Pekka Paalanen77346a62011-11-30 16:26:35 +020075 SHELL_SURFACE_SCREENSAVER,
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050076
77 SHELL_SURFACE_TOPLEVEL,
78 SHELL_SURFACE_TRANSIENT,
79 SHELL_SURFACE_FULLSCREEN
Pekka Paalanen57da4a82011-11-23 16:42:16 +020080};
81
Pekka Paalanen56cdea92011-11-23 16:14:12 +020082struct shell_surface {
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020083 struct wl_resource resource;
84
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010085 struct wlsc_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020086 struct wl_listener surface_destroy_listener;
Pekka Paalanen57da4a82011-11-23 16:42:16 +020087
Kristian Høgsbergd2abb832011-11-23 10:52:40 -050088 enum shell_surface_type type;
89 int32_t saved_x, saved_y;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +010090
91 struct wlsc_output *output;
92 struct wl_list link;
Pekka Paalanen56cdea92011-11-23 16:14:12 +020093};
94
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050095struct wlsc_move_grab {
96 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050097 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050098 int32_t dx, dy;
99};
100
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200101static int
102shell_configuration(struct wl_shell *shell)
103{
104 int ret;
105 char *config_file;
Pekka Paalanen7296e792011-12-07 16:22:00 +0200106 char *path = NULL;
107 int duration = 60;
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200108
109 struct config_key saver_keys[] = {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200110 { "path", CONFIG_KEY_STRING, &path },
111 { "duration", CONFIG_KEY_INTEGER, &duration },
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200112 };
113
114 struct config_section cs[] = {
115 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
116 };
117
118 config_file = config_file_path("wayland-desktop-shell.ini");
119 ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
120 free(config_file);
121
Pekka Paalanen7296e792011-12-07 16:22:00 +0200122 shell->screensaver.path = path;
123 shell->screensaver.duration = duration;
124
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200125 return ret;
126 /* FIXME: free(shell->screensaver.path) on plugin fini */
127}
128
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200129static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500130move_grab_motion(struct wl_grab *grab,
131 uint32_t time, int32_t x, int32_t y)
132{
133 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500134 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500135
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400136 wlsc_surface_configure(es, x + move->dx, y + move->dy,
137 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500138}
139
140static void
141move_grab_button(struct wl_grab *grab,
142 uint32_t time, int32_t button, int32_t state)
143{
144}
145
146static void
147move_grab_end(struct wl_grab *grab, uint32_t time)
148{
149 free(grab);
150}
151
152static const struct wl_grab_interface move_grab_interface = {
153 move_grab_motion,
154 move_grab_button,
155 move_grab_end
156};
157
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400158static int
159wlsc_surface_move(struct wlsc_surface *es,
160 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500161{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162 struct wlsc_move_grab *move;
163
164 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400165 if (!move)
166 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500167
168 move->grab.interface = &move_grab_interface;
169 move->dx = es->x - wd->input_device.grab_x;
170 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500171 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172
173 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400174 &move->grab, &es->surface, time) < 0)
175 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500176
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400177 wl_input_device_set_pointer_focus(&wd->input_device,
178 NULL, time, 0, 0, 0, 0);
179
180 return 0;
181}
182
183static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200184shell_surface_move(struct wl_client *client, struct wl_resource *resource,
185 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400186{
187 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200188 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400189
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200190 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400191 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500192}
193
194struct wlsc_resize_grab {
195 struct wl_grab grab;
196 uint32_t edges;
197 int32_t dx, dy, width, height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200198 struct shell_surface *shsurf;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199};
200
201static void
202resize_grab_motion(struct wl_grab *grab,
203 uint32_t time, int32_t x, int32_t y)
204{
205 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
206 struct wl_input_device *device = grab->input_device;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500207 int32_t width, height;
208
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200209 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210 width = device->grab_x - x + resize->width;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200211 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 width = x - device->grab_x + resize->width;
213 } else {
214 width = resize->width;
215 }
216
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200217 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 height = device->grab_y - y + resize->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200219 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220 height = y - device->grab_y + resize->height;
221 } else {
222 height = resize->height;
223 }
224
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200225 wl_resource_post_event(&resize->shsurf->resource,
226 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
227 width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500228}
229
230static void
231resize_grab_button(struct wl_grab *grab,
232 uint32_t time, int32_t button, int32_t state)
233{
234}
235
236static void
237resize_grab_end(struct wl_grab *grab, uint32_t time)
238{
239 free(grab);
240}
241
242static const struct wl_grab_interface resize_grab_interface = {
243 resize_grab_motion,
244 resize_grab_button,
245 resize_grab_end
246};
247
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400248static int
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200249wlsc_surface_resize(struct shell_surface *shsurf,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400250 struct wlsc_input_device *wd,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200251 uint32_t time, uint32_t edges)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500252{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253 struct wlsc_resize_grab *resize;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200254 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500255
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500256 /* FIXME: Reject if fullscreen */
257
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500258 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400259 if (!resize)
260 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500261
262 resize->grab.interface = &resize_grab_interface;
263 resize->edges = edges;
264 resize->dx = es->x - wd->input_device.grab_x;
265 resize->dy = es->y - wd->input_device.grab_y;
266 resize->width = es->width;
267 resize->height = es->height;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200268 resize->shsurf = shsurf;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400269
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500270 if (edges == 0 || edges > 15 ||
271 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400272 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500273
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500274 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400275 &resize->grab, &es->surface, time) < 0)
276 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500277
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400278 wl_input_device_set_pointer_focus(&wd->input_device,
279 NULL, time, 0, 0, 0, 0);
280
281 return 0;
282}
283
284static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200285shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
286 struct wl_resource *input_resource, uint32_t time,
287 uint32_t edges)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400288{
289 struct wlsc_input_device *wd = input_resource->data;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200290 struct shell_surface *shsurf = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400291
292 /* FIXME: Reject if fullscreen */
293
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200294 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400295 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500296}
297
Pekka Paalanen98262232011-12-01 10:42:22 +0200298static int
299reset_shell_surface_type(struct shell_surface *surface)
300{
301 switch (surface->type) {
302 case SHELL_SURFACE_FULLSCREEN:
303 surface->surface->x = surface->saved_x;
304 surface->surface->y = surface->saved_y;
305 surface->surface->fullscreen_output = NULL;
306 break;
307 case SHELL_SURFACE_PANEL:
308 case SHELL_SURFACE_BACKGROUND:
309 wl_list_remove(&surface->link);
310 wl_list_init(&surface->link);
311 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200312 case SHELL_SURFACE_SCREENSAVER:
Pekka Paalanen98262232011-12-01 10:42:22 +0200313 case SHELL_SURFACE_LOCK:
314 wl_resource_post_error(&surface->resource,
315 WL_DISPLAY_ERROR_INVALID_METHOD,
Pekka Paalanen77346a62011-11-30 16:26:35 +0200316 "cannot reassign surface type");
Pekka Paalanen98262232011-12-01 10:42:22 +0200317 return -1;
318 case SHELL_SURFACE_NONE:
319 case SHELL_SURFACE_TOPLEVEL:
320 case SHELL_SURFACE_TRANSIENT:
321 break;
322 }
323
324 surface->type = SHELL_SURFACE_NONE;
325 return 0;
326}
327
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500328static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200329shell_surface_set_toplevel(struct wl_client *client,
330 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400331
332{
Pekka Paalanen98262232011-12-01 10:42:22 +0200333 struct shell_surface *surface = resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400334
Pekka Paalanen98262232011-12-01 10:42:22 +0200335 if (reset_shell_surface_type(surface))
336 return;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400337
Pekka Paalanen98262232011-12-01 10:42:22 +0200338 wlsc_surface_damage(surface->surface);
339 surface->type = SHELL_SURFACE_TOPLEVEL;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400340}
341
342static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200343shell_surface_set_transient(struct wl_client *client,
344 struct wl_resource *resource,
345 struct wl_resource *parent_resource,
346 int x, int y, uint32_t flags)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400347{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200348 struct shell_surface *shsurf = resource->data;
349 struct wlsc_surface *es = shsurf->surface;
Pekka Paalanen01e7b002011-12-08 16:42:33 +0200350 struct shell_surface *pshsurf = parent_resource->data;
351 struct wlsc_surface *pes = pshsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400352
Pekka Paalanen98262232011-12-01 10:42:22 +0200353 if (reset_shell_surface_type(shsurf))
354 return;
355
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400356 /* assign to parents output */
357 es->output = pes->output;
358
359 es->x = pes->x + x;
360 es->y = pes->y + y;
361
362 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200363 shsurf->type = SHELL_SURFACE_TRANSIENT;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400364}
365
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200366static struct wlsc_output *
367get_default_output(struct wlsc_compositor *compositor)
368{
369 return container_of(compositor->output_list.next,
370 struct wlsc_output, link);
371}
372
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400373static void
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200374shell_surface_set_fullscreen(struct wl_client *client,
375 struct wl_resource *resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400376
377{
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200378 struct shell_surface *shsurf = resource->data;
379 struct wlsc_surface *es = shsurf->surface;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400380 struct wlsc_output *output;
381
Pekka Paalanen98262232011-12-01 10:42:22 +0200382 if (reset_shell_surface_type(shsurf))
383 return;
384
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400385 /* FIXME: Fullscreen on first output */
386 /* FIXME: Handle output going away */
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +0200387 output = get_default_output(es->compositor);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400388 es->output = output;
389
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200390 shsurf->saved_x = es->x;
391 shsurf->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400392 es->x = (output->current->width - es->width) / 2;
393 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400394 es->fullscreen_output = output;
395 wlsc_surface_damage(es);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200396 shsurf->type = SHELL_SURFACE_FULLSCREEN;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400397}
398
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200399static const struct wl_shell_surface_interface shell_surface_implementation = {
400 shell_surface_move,
401 shell_surface_resize,
402 shell_surface_set_toplevel,
403 shell_surface_set_transient,
404 shell_surface_set_fullscreen
405};
406
407static void
408destroy_shell_surface(struct wl_resource *resource)
409{
410 struct shell_surface *shsurf = resource->data;
411
412 /* in case cleaning up a dead client destroys shell_surface first */
413 if (shsurf->surface)
414 wl_list_remove(&shsurf->surface_destroy_listener.link);
415
416 wl_list_remove(&shsurf->link);
417 free(shsurf);
418}
419
420static void
421shell_handle_surface_destroy(struct wl_listener *listener,
422 struct wl_resource *resource, uint32_t time)
423{
424 struct shell_surface *shsurf = container_of(listener,
425 struct shell_surface,
426 surface_destroy_listener);
427
428 shsurf->surface = NULL;
429 wl_resource_destroy(&shsurf->resource, time);
430}
431
Pekka Paalanenec2b32f2011-11-28 15:12:34 +0200432static struct shell_surface *
433get_shell_surface(struct wlsc_surface *surface)
434{
435 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
436 struct wl_listener *listener;
437
438 /* search the destroy listener list for our callback */
439 wl_list_for_each(listener, lst, link) {
440 if (listener->func == shell_handle_surface_destroy) {
441 return container_of(listener, struct shell_surface,
442 surface_destroy_listener);
443 }
444 }
445
446 return NULL;
447}
448
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200449static void
Pekka Paalanen46229672011-11-29 15:49:31 +0200450shell_get_shell_surface(struct wl_client *client,
451 struct wl_resource *resource,
452 uint32_t id,
453 struct wl_resource *surface_resource)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200454{
455 struct wlsc_surface *surface = surface_resource->data;
456 struct shell_surface *shsurf;
457
Pekka Paalanenf32f1fc2011-11-29 16:05:28 +0200458 if (get_shell_surface(surface)) {
459 wl_resource_post_error(surface_resource,
460 WL_DISPLAY_ERROR_INVALID_OBJECT,
461 "wl_shell::get_shell_surface already requested");
462 return;
463 }
464
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200465 shsurf = calloc(1, sizeof *shsurf);
466 if (!shsurf) {
467 wl_resource_post_no_memory(resource);
468 return;
469 }
470
471 shsurf->resource.destroy = destroy_shell_surface;
472 shsurf->resource.object.id = id;
473 shsurf->resource.object.interface = &wl_shell_surface_interface;
474 shsurf->resource.object.implementation =
475 (void (**)(void)) &shell_surface_implementation;
476 shsurf->resource.data = shsurf;
477
478 shsurf->surface = surface;
479 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
480 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
481 &shsurf->surface_destroy_listener.link);
482
483 /* init link so its safe to always remove it in destroy_shell_surface */
484 wl_list_init(&shsurf->link);
485
Pekka Paalanen98262232011-12-01 10:42:22 +0200486 shsurf->type = SHELL_SURFACE_NONE;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200487
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200488 wl_client_add_resource(client, &shsurf->resource);
489}
490
491static const struct wl_shell_interface shell_implementation = {
Pekka Paalanen46229672011-11-29 15:49:31 +0200492 shell_get_shell_surface
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500493};
494
Kristian Høgsberg07937562011-04-12 17:25:42 -0400495static void
Pekka Paalanen18027e52011-12-02 16:31:49 +0200496handle_screensaver_sigchld(struct wlsc_process *proc, int status)
497{
498 proc->pid = 0;
499}
500
501static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200502launch_screensaver(struct wl_shell *shell)
503{
504 if (shell->screensaver.binding)
505 return;
506
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200507 if (!shell->screensaver.path)
508 return;
509
Pekka Paalanen18027e52011-12-02 16:31:49 +0200510 wlsc_client_launch(shell->compositor,
511 &shell->screensaver.process,
Pekka Paalanene955f1e2011-12-07 11:49:52 +0200512 shell->screensaver.path,
Pekka Paalanen18027e52011-12-02 16:31:49 +0200513 handle_screensaver_sigchld);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200514}
515
516static void
517terminate_screensaver(struct wl_shell *shell)
518{
Pekka Paalanen18027e52011-12-02 16:31:49 +0200519 if (shell->screensaver.process.pid == 0)
520 return;
521
522 kill(shell->screensaver.process.pid, SIGTERM);
Pekka Paalanen77346a62011-11-30 16:26:35 +0200523}
524
525static void
526show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
527{
528 struct wl_list *list;
529
530 if (shell->lock_surface)
531 list = &shell->lock_surface->surface->link;
532 else
533 list = &shell->compositor->surface_list;
534
535 wl_list_remove(&surface->surface->link);
536 wl_list_insert(list, &surface->surface->link);
537 wlsc_surface_configure(surface->surface,
538 surface->surface->x,
539 surface->surface->y,
540 surface->surface->width,
541 surface->surface->height);
542 surface->surface->output = surface->output;
543}
544
545static void
546hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
547{
548 wl_list_remove(&surface->surface->link);
549 wl_list_init(&surface->surface->link);
550 surface->surface->output = NULL;
551}
552
553static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400554desktop_shell_set_background(struct wl_client *client,
555 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100556 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400557 struct wl_resource *surface_resource)
558{
559 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200560 struct shell_surface *shsurf = surface_resource->data;
561 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200562 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400563
Pekka Paalanen98262232011-12-01 10:42:22 +0200564 if (reset_shell_surface_type(shsurf))
565 return;
566
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100567 wl_list_for_each(priv, &shell->backgrounds, link) {
568 if (priv->output == output_resource->data) {
569 priv->surface->output = NULL;
570 wl_list_remove(&priv->surface->link);
571 wl_list_remove(&priv->link);
572 break;
573 }
574 }
575
Pekka Paalanen068ae942011-11-28 14:11:15 +0200576 shsurf->type = SHELL_SURFACE_BACKGROUND;
577 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100578
Pekka Paalanen068ae942011-11-28 14:11:15 +0200579 wl_list_insert(&shell->backgrounds, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100580
Pekka Paalanen068ae942011-11-28 14:11:15 +0200581 surface->x = shsurf->output->x;
582 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200583
Kristian Høgsberg75840622011-09-06 13:48:16 -0400584 wl_resource_post_event(resource,
585 DESKTOP_SHELL_CONFIGURE,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200586 wlsc_compositor_get_time(), 0, surface_resource,
587 shsurf->output->current->width,
588 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400589}
590
591static void
592desktop_shell_set_panel(struct wl_client *client,
593 struct wl_resource *resource,
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100594 struct wl_resource *output_resource,
Kristian Høgsberg75840622011-09-06 13:48:16 -0400595 struct wl_resource *surface_resource)
596{
597 struct wl_shell *shell = resource->data;
Pekka Paalanen068ae942011-11-28 14:11:15 +0200598 struct shell_surface *shsurf = surface_resource->data;
599 struct wlsc_surface *surface = shsurf->surface;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200600 struct shell_surface *priv;
Kristian Høgsberg75840622011-09-06 13:48:16 -0400601
Pekka Paalanen98262232011-12-01 10:42:22 +0200602 if (reset_shell_surface_type(shsurf))
603 return;
604
Benjamin Franzkef02bb642011-11-23 20:46:40 +0100605 wl_list_for_each(priv, &shell->panels, link) {
606 if (priv->output == output_resource->data) {
607 priv->surface->output = NULL;
608 wl_list_remove(&priv->surface->link);
609 wl_list_remove(&priv->link);
610 break;
611 }
612 }
613
Pekka Paalanen068ae942011-11-28 14:11:15 +0200614 shsurf->type = SHELL_SURFACE_PANEL;
615 shsurf->output = output_resource->data;
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100616
Pekka Paalanen068ae942011-11-28 14:11:15 +0200617 wl_list_insert(&shell->panels, &shsurf->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100618
Pekka Paalanen068ae942011-11-28 14:11:15 +0200619 surface->x = shsurf->output->x;
620 surface->y = shsurf->output->y;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200621
Kristian Høgsberg75840622011-09-06 13:48:16 -0400622 wl_resource_post_event(resource,
623 DESKTOP_SHELL_CONFIGURE,
624 wlsc_compositor_get_time(), 0, surface_resource,
Pekka Paalanen068ae942011-11-28 14:11:15 +0200625 shsurf->output->current->width,
626 shsurf->output->current->height);
Kristian Høgsberg75840622011-09-06 13:48:16 -0400627}
628
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200629static void
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500630handle_lock_surface_destroy(struct wl_listener *listener,
631 struct wl_resource *resource, uint32_t time)
632{
633 struct wl_shell *shell =
Pekka Paalanen2ca86302011-11-16 13:47:35 +0200634 container_of(listener, struct wl_shell, lock_surface_listener);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500635
636 fprintf(stderr, "lock surface gone\n");
637 shell->lock_surface = NULL;
638}
639
640static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200641desktop_shell_set_lock_surface(struct wl_client *client,
642 struct wl_resource *resource,
643 struct wl_resource *surface_resource)
644{
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200645 struct wl_shell *shell = resource->data;
Pekka Paalanen98262232011-12-01 10:42:22 +0200646 struct shell_surface *surface = surface_resource->data;
647
648 if (reset_shell_surface_type(surface))
649 return;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200650
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200651 shell->prepare_event_sent = false;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200652
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200653 if (!shell->locked)
654 return;
655
Pekka Paalanen98262232011-12-01 10:42:22 +0200656 shell->lock_surface = surface;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200657
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500658 shell->lock_surface_listener.func = handle_lock_surface_destroy;
659 wl_list_insert(&surface_resource->destroy_listener_list,
660 &shell->lock_surface_listener.link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200661
Pekka Paalanen068ae942011-11-28 14:11:15 +0200662 shell->lock_surface->type = SHELL_SURFACE_LOCK;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200663}
664
665static void
666resume_desktop(struct wl_shell *shell)
667{
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500668 struct wlsc_surface *surface;
Pekka Paalanenfe340832011-11-25 16:07:52 +0200669 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200670 struct shell_surface *tmp;
671
672 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
673 hide_screensaver(shell, tmp);
674
675 terminate_screensaver(shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200676
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500677 wl_list_for_each(surface, &shell->hidden_surface_list, link)
678 wlsc_surface_configure(surface, surface->x, surface->y,
679 surface->width, surface->height);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200680
Pekka Paalanenfe340832011-11-25 16:07:52 +0200681 if (wl_list_empty(&shell->backgrounds)) {
682 list = &shell->compositor->surface_list;
683 } else {
684 struct shell_surface *background;
685 background = container_of(shell->backgrounds.prev,
686 struct shell_surface, link);
687 list = background->surface->link.prev;
688 }
689
690 if (!wl_list_empty(&shell->hidden_surface_list))
691 wl_list_insert_list(list, &shell->hidden_surface_list);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500692 wl_list_init(&shell->hidden_surface_list);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200693
694 shell->locked = false;
695 wlsc_compositor_repick(shell->compositor);
Pekka Paalanen7296e792011-12-07 16:22:00 +0200696 shell->compositor->idle_time = shell->compositor->option_idle_time;
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +0200697 wlsc_compositor_wake(shell->compositor);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200698}
699
700static void
701desktop_shell_unlock(struct wl_client *client,
702 struct wl_resource *resource)
703{
704 struct wl_shell *shell = resource->data;
705
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200706 shell->prepare_event_sent = false;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200707
708 if (shell->locked)
709 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200710}
711
Kristian Høgsberg75840622011-09-06 13:48:16 -0400712static const struct desktop_shell_interface desktop_shell_implementation = {
713 desktop_shell_set_background,
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200714 desktop_shell_set_panel,
715 desktop_shell_set_lock_surface,
716 desktop_shell_unlock
Kristian Høgsberg75840622011-09-06 13:48:16 -0400717};
718
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200719static enum shell_surface_type
720get_shell_surface_type(struct wlsc_surface *surface)
721{
722 struct shell_surface *shsurf;
723
724 shsurf = get_shell_surface(surface);
725 if (!shsurf)
Pekka Paalanen98262232011-12-01 10:42:22 +0200726 return SHELL_SURFACE_NONE;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200727 return shsurf->type;
728}
729
Kristian Høgsberg75840622011-09-06 13:48:16 -0400730static void
Kristian Høgsberg07937562011-04-12 17:25:42 -0400731move_binding(struct wl_input_device *device, uint32_t time,
732 uint32_t key, uint32_t button, uint32_t state, void *data)
733{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400734 struct wlsc_surface *surface =
735 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500736
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100737 if (surface == NULL)
738 return;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400739
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200740 switch (get_shell_surface_type(surface)) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100741 case SHELL_SURFACE_PANEL:
742 case SHELL_SURFACE_BACKGROUND:
743 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200744 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100745 return;
746 default:
747 break;
748 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400749
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400750 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400751}
752
753static void
754resize_binding(struct wl_input_device *device, uint32_t time,
755 uint32_t key, uint32_t button, uint32_t state, void *data)
756{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400757 struct wlsc_surface *surface =
758 (struct wlsc_surface *) device->pointer_focus;
759 uint32_t edges = 0;
760 int32_t x, y;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200761 struct shell_surface *shsurf;
Kristian Høgsbergd2abb832011-11-23 10:52:40 -0500762
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100763 if (surface == NULL)
764 return;
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200765
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200766 shsurf = get_shell_surface(surface);
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200767 if (!shsurf)
768 return;
769
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200770 switch (shsurf->type) {
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100771 case SHELL_SURFACE_PANEL:
772 case SHELL_SURFACE_BACKGROUND:
773 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +0200774 case SHELL_SURFACE_SCREENSAVER:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100775 return;
776 default:
777 break;
778 }
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400779
Kristian Høgsberg07937562011-04-12 17:25:42 -0400780 x = device->grab_x - surface->x;
781 y = device->grab_y - surface->y;
782
783 if (x < surface->width / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200784 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400785 else if (x < 2 * surface->width / 3)
786 edges |= 0;
787 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200788 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400789
790 if (y < surface->height / 3)
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200791 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400792 else if (y < 2 * surface->height / 3)
793 edges |= 0;
794 else
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200795 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400796
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200797 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
798 time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400799}
800
801static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500802terminate_binding(struct wl_input_device *device, uint32_t time,
803 uint32_t key, uint32_t button, uint32_t state, void *data)
804{
805 struct wlsc_compositor *compositor = data;
806
807 if (state)
808 wl_display_terminate(compositor->wl_display);
809}
810
811static void
Kristian Høgsberg75840622011-09-06 13:48:16 -0400812activate(struct wlsc_shell *base, struct wlsc_surface *es,
813 struct wlsc_input_device *device, uint32_t time)
814{
815 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
816 struct wlsc_compositor *compositor = shell->compositor;
817
818 wlsc_surface_activate(es, device, time);
819
Kristian Høgsbergd6e55252011-10-11 23:41:17 -0400820 if (compositor->wxs)
821 wlsc_xserver_surface_activate(es);
822
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200823 switch (get_shell_surface_type(es)) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200824 case SHELL_SURFACE_BACKGROUND:
825 /* put background back to bottom */
Kristian Høgsberg75840622011-09-06 13:48:16 -0400826 wl_list_remove(&es->link);
827 wl_list_insert(compositor->surface_list.prev, &es->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200828 break;
829 case SHELL_SURFACE_PANEL:
830 /* already put on top */
831 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200832 case SHELL_SURFACE_SCREENSAVER:
833 /* always below lock surface */
834 if (shell->lock_surface) {
835 wl_list_remove(&es->link);
836 wl_list_insert(&shell->lock_surface->surface->link,
837 &es->link);
838 }
839 break;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200840 default:
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100841 if (!shell->locked) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200842 /* bring panel back to top */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +0100843 struct shell_surface *panel;
844 wl_list_for_each(panel, &shell->panels, link) {
845 wl_list_remove(&panel->surface->link);
846 wl_list_insert(&compositor->surface_list,
847 &panel->surface->link);
848 }
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200849 }
Kristian Høgsberg75840622011-09-06 13:48:16 -0400850 }
851}
852
853static void
Kristian Høgsberge1a850e2011-12-19 15:18:05 -0500854click_to_activate_binding(struct wl_input_device *device,
855 uint32_t time, uint32_t key,
856 uint32_t button, uint32_t state, void *data)
857{
858 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
859 struct wlsc_compositor *compositor = data;
860 struct wlsc_surface *focus;
861
862 focus = (struct wlsc_surface *) device->pointer_focus;
863 if (state && focus && device->grab == NULL)
864 compositor->shell->activate(compositor->shell,
865 focus, wd, time);
866}
867
868static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200869lock(struct wlsc_shell *base)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400870{
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200871 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200872 struct wl_list *surface_list = &shell->compositor->surface_list;
873 struct wlsc_surface *cur;
874 struct wlsc_surface *tmp;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200875 struct wlsc_input_device *device;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200876 struct shell_surface *shsurf;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200877 uint32_t time;
878
879 if (shell->locked)
880 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200881
882 shell->locked = true;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200883
884 /* Move all surfaces from compositor's list to our hidden list,
885 * except the background. This way nothing else can show or
886 * receive input events while we are locked. */
887
888 if (!wl_list_empty(&shell->hidden_surface_list)) {
889 fprintf(stderr,
890 "%s: Assertion failed: hidden_surface_list is not empty.\n",
891 __func__);
892 }
893
894 wl_list_for_each_safe(cur, tmp, surface_list, link) {
895 /* skip input device sprites, cur->surface is uninitialised */
896 if (cur->surface.resource.client == NULL)
897 continue;
898
Pekka Paalanen92a0dc42011-11-28 15:34:13 +0200899 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200900 continue;
901
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500902 cur->output = NULL;
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200903 wl_list_remove(&cur->link);
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500904 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200905 }
906
Pekka Paalanen77346a62011-11-30 16:26:35 +0200907 launch_screensaver(shell);
908
909 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
910 show_screensaver(shell, shsurf);
911
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200912 if (!wl_list_empty(&shell->screensaver.surfaces)) {
Pekka Paalanen7296e792011-12-07 16:22:00 +0200913 shell->compositor->idle_time = shell->screensaver.duration;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200914 wlsc_compositor_wake(shell->compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200915 shell->compositor->state = WLSC_COMPOSITOR_IDLE;
916 }
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +0200917
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200918 /* reset pointer foci */
919 wlsc_compositor_repick(shell->compositor);
920
921 /* reset keyboard foci */
922 time = wlsc_compositor_get_time();
923 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
924 wl_input_device_set_keyboard_focus(&device->input_device,
925 NULL, time);
926 }
927
928 /* TODO: disable bindings that should not work while locked. */
929
930 /* All this must be undone in resume_desktop(). */
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200931}
932
933static void
934unlock(struct wlsc_shell *base)
935{
936 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
937
Pekka Paalanend81c2162011-11-16 13:47:34 +0200938 if (!shell->locked || shell->lock_surface) {
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200939 wlsc_compositor_wake(shell->compositor);
940 return;
941 }
942
943 /* If desktop-shell client has gone away, unlock immediately. */
944 if (!shell->child.desktop_shell) {
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +0200945 resume_desktop(shell);
Pekka Paalanen9ef3e012011-11-15 13:34:48 +0200946 return;
947 }
948
949 if (shell->prepare_event_sent)
950 return;
951
952 wl_resource_post_event(shell->child.desktop_shell,
953 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
954 shell->prepare_event_sent = true;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400955}
956
957static void
Pekka Paalanen77346a62011-11-30 16:26:35 +0200958center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
959{
960 struct wlsc_mode *mode = output->current;
961
962 surface->x = output->x + (mode->width - surface->width) / 2;
963 surface->y = output->y + (mode->height - surface->height) / 2;
964}
965
966static void
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -0500967map(struct wlsc_shell *base,
968 struct wlsc_surface *surface, int32_t width, int32_t height)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400969{
Kristian Høgsberg75840622011-09-06 13:48:16 -0400970 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
971 struct wlsc_compositor *compositor = shell->compositor;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500972 struct wl_list *list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200973 struct shell_surface *shsurf;
974 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
975 int do_configure;
Pekka Paalanen57da4a82011-11-23 16:42:16 +0200976
Pekka Paalanen77346a62011-11-30 16:26:35 +0200977 shsurf = get_shell_surface(surface);
978 if (shsurf)
979 surface_type = shsurf->type;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500980
Pekka Paalanen77346a62011-11-30 16:26:35 +0200981 if (shell->locked) {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500982 list = &shell->hidden_surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200983 do_configure = 0;
984 } else {
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500985 list = &compositor->surface_list;
Pekka Paalanen77346a62011-11-30 16:26:35 +0200986 do_configure = 1;
987 }
988
989 surface->width = width;
990 surface->height = height;
991
992 /* initial positioning, see also configure() */
993 switch (surface_type) {
994 case SHELL_SURFACE_TOPLEVEL:
995 surface->x = 10 + random() % 400;
996 surface->y = 10 + random() % 400;
997 break;
998 case SHELL_SURFACE_SCREENSAVER:
999 case SHELL_SURFACE_FULLSCREEN:
1000 center_on_output(surface, surface->fullscreen_output);
1001 break;
Pekka Paalanenaf0e34c2011-12-02 10:59:17 +02001002 case SHELL_SURFACE_LOCK:
1003 center_on_output(surface, get_default_output(compositor));
1004 break;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001005 default:
1006 ;
1007 }
Kristian Høgsberg75840622011-09-06 13:48:16 -04001008
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001009 /* surface stacking order, see also activate() */
Pekka Paalanen92a0dc42011-11-28 15:34:13 +02001010 switch (surface_type) {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001011 case SHELL_SURFACE_BACKGROUND:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001012 /* background always visible, at the bottom */
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001013 wl_list_insert(compositor->surface_list.prev, &surface->link);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001014 do_configure = 1;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001015 break;
1016 case SHELL_SURFACE_PANEL:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001017 /* panel always on top, hidden while locked */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001018 wl_list_insert(list, &surface->link);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001019 break;
1020 case SHELL_SURFACE_LOCK:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001021 /* lock surface always visible, on top */
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001022 wl_list_insert(&compositor->surface_list, &surface->link);
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001023
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001024 wlsc_compositor_repick(compositor);
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001025 wlsc_compositor_wake(compositor);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001026 do_configure = 1;
1027 break;
1028 case SHELL_SURFACE_SCREENSAVER:
1029 /* If locked, show it. */
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001030 if (shell->locked) {
Pekka Paalanen77346a62011-11-30 16:26:35 +02001031 show_screensaver(shell, shsurf);
Pekka Paalanen7296e792011-12-07 16:22:00 +02001032 compositor->idle_time = shell->screensaver.duration;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001033 wlsc_compositor_wake(compositor);
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02001034 if (!shell->lock_surface)
1035 compositor->state = WLSC_COMPOSITOR_IDLE;
Pekka Paalanenbaeb6a12011-12-01 16:23:57 +02001036 }
Pekka Paalanen77346a62011-11-30 16:26:35 +02001037 do_configure = 0;
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001038 break;
1039 default:
Pekka Paalanend3dd6e12011-11-16 13:47:33 +02001040 /* everything else just below the panel */
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001041 if (!wl_list_empty(&shell->panels)) {
1042 struct shell_surface *panel =
1043 container_of(shell->panels.prev,
1044 struct shell_surface, link);
1045 wl_list_insert(&panel->surface->link, &surface->link);
1046 } else {
Pekka Paalanen57da4a82011-11-23 16:42:16 +02001047 wl_list_insert(list, &surface->link);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001048 }
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001049 }
1050
Pekka Paalanen77346a62011-11-30 16:26:35 +02001051 if (do_configure)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001052 wlsc_surface_configure(surface,
1053 surface->x, surface->y, width, height);
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001054
Juan Zhao7bb92f02011-12-15 11:31:51 -05001055 switch (surface_type) {
1056 case SHELL_SURFACE_TOPLEVEL:
1057 case SHELL_SURFACE_TRANSIENT:
1058 case SHELL_SURFACE_FULLSCREEN:
1059 if (!shell->locked)
1060 activate(base, surface,
1061 (struct wlsc_input_device *)
1062 compositor->input_device,
1063 wlsc_compositor_get_time());
1064 break;
1065 default:
1066 break;
1067 }
1068
Kristian Høgsberg2f88a402011-12-04 15:32:59 -05001069 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1070 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001071}
1072
1073static void
Pekka Paalanen77346a62011-11-30 16:26:35 +02001074configure(struct wlsc_shell *base, struct wlsc_surface *surface,
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001075 int32_t x, int32_t y, int32_t width, int32_t height)
1076{
Pekka Paalanen77346a62011-11-30 16:26:35 +02001077 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1078 int do_configure = !shell->locked;
1079 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1080 struct shell_surface *shsurf;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001081
Pekka Paalanen77346a62011-11-30 16:26:35 +02001082 shsurf = get_shell_surface(surface);
1083 if (shsurf)
1084 surface_type = shsurf->type;
1085
1086 surface->width = width;
1087 surface->height = height;
1088
1089 switch (surface_type) {
1090 case SHELL_SURFACE_SCREENSAVER:
1091 do_configure = !do_configure;
1092 /* fall through */
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001093 case SHELL_SURFACE_FULLSCREEN:
Pekka Paalanen77346a62011-11-30 16:26:35 +02001094 center_on_output(surface, surface->fullscreen_output);
Kristian Høgsbergd2abb832011-11-23 10:52:40 -05001095 break;
1096 default:
1097 break;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -04001098 }
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001099
Pekka Paalanen77346a62011-11-30 16:26:35 +02001100 /*
1101 * wlsc_surface_configure() will assign an output, which means
1102 * the surface is supposed to be in compositor->surface_list.
1103 * Be careful with that, and make sure we stay on the right output.
1104 * XXX: would a fullscreen surface need the same handling?
1105 */
1106 if (do_configure) {
1107 wlsc_surface_configure(surface, x, y, width, height);
1108
1109 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1110 surface->output = shsurf->output;
1111 }
Kristian Høgsberg07937562011-04-12 17:25:42 -04001112}
1113
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001114static void
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001115desktop_shell_sigchld(struct wlsc_process *process, int status)
1116{
1117 struct wl_shell *shell =
1118 container_of(process, struct wl_shell, child.process);
1119
1120 shell->child.process.pid = 0;
1121 shell->child.client = NULL; /* already destroyed by wayland */
1122}
1123
1124static int
1125launch_desktop_shell_process(struct wl_shell *shell)
1126{
Kristian Høgsbergc4693c42011-11-14 14:57:17 -05001127 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001128
Pekka Paalanen409ef0a2011-12-02 15:30:21 +02001129 shell->child.client = wlsc_client_launch(shell->compositor,
1130 &shell->child.process,
1131 shell_exe,
1132 desktop_shell_sigchld);
1133
1134 if (!shell->child.client)
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001135 return -1;
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001136 return 0;
1137}
1138
1139static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001140bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1141{
1142 struct wl_shell *shell = data;
1143
1144 wl_client_add_object(client, &wl_shell_interface,
Pekka Paalanen9d1613e2011-11-25 12:09:16 +02001145 &shell_implementation, id, shell);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001146}
1147
Kristian Høgsberg75840622011-09-06 13:48:16 -04001148static void
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001149unbind_desktop_shell(struct wl_resource *resource)
1150{
1151 struct wl_shell *shell = resource->data;
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001152
1153 if (shell->locked)
1154 resume_desktop(shell);
1155
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001156 shell->child.desktop_shell = NULL;
1157 shell->prepare_event_sent = false;
1158 free(resource);
1159}
1160
1161static void
Kristian Høgsberg75840622011-09-06 13:48:16 -04001162bind_desktop_shell(struct wl_client *client,
1163 void *data, uint32_t version, uint32_t id)
1164{
1165 struct wl_shell *shell = data;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001166 struct wl_resource *resource;
Kristian Høgsberg75840622011-09-06 13:48:16 -04001167
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001168 resource = wl_client_add_object(client, &desktop_shell_interface,
1169 &desktop_shell_implementation,
1170 id, shell);
1171
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001172 if (client == shell->child.client) {
1173 resource->destroy = unbind_desktop_shell;
1174 shell->child.desktop_shell = resource;
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001175 return;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001176 }
Pekka Paalanenbbe60522011-11-03 14:11:33 +02001177
1178 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1179 "permission to bind desktop_shell denied");
1180 wl_resource_destroy(resource, 0);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001181}
1182
Pekka Paalanen6e168112011-11-24 11:34:05 +02001183static void
1184screensaver_set_surface(struct wl_client *client,
1185 struct wl_resource *resource,
1186 struct wl_resource *shell_surface_resource,
1187 struct wl_resource *output_resource)
1188{
1189 struct wl_shell *shell = resource->data;
1190 struct shell_surface *surface = shell_surface_resource->data;
1191 struct wlsc_output *output = output_resource->data;
1192
Pekka Paalanen98262232011-12-01 10:42:22 +02001193 if (reset_shell_surface_type(surface))
1194 return;
1195
Pekka Paalanen77346a62011-11-30 16:26:35 +02001196 surface->type = SHELL_SURFACE_SCREENSAVER;
1197
1198 surface->surface->fullscreen_output = output;
1199 surface->output = output;
1200 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
Pekka Paalanen6e168112011-11-24 11:34:05 +02001201}
1202
1203static const struct screensaver_interface screensaver_implementation = {
1204 screensaver_set_surface
1205};
1206
1207static void
1208unbind_screensaver(struct wl_resource *resource)
1209{
1210 struct wl_shell *shell = resource->data;
1211
Pekka Paalanen77346a62011-11-30 16:26:35 +02001212 shell->screensaver.binding = NULL;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001213 free(resource);
1214}
1215
1216static void
1217bind_screensaver(struct wl_client *client,
1218 void *data, uint32_t version, uint32_t id)
1219{
1220 struct wl_shell *shell = data;
1221 struct wl_resource *resource;
1222
1223 resource = wl_client_add_object(client, &screensaver_interface,
1224 &screensaver_implementation,
1225 id, shell);
1226
Pekka Paalanen77346a62011-11-30 16:26:35 +02001227 if (shell->screensaver.binding == NULL) {
Pekka Paalanen6e168112011-11-24 11:34:05 +02001228 resource->destroy = unbind_screensaver;
Pekka Paalanen77346a62011-11-30 16:26:35 +02001229 shell->screensaver.binding = resource;
Pekka Paalanen6e168112011-11-24 11:34:05 +02001230 return;
1231 }
1232
1233 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1234 "interface object already bound");
1235 wl_resource_destroy(resource, 0);
1236}
1237
Kristian Høgsberg6c709a32011-05-06 14:52:41 -04001238int
1239shell_init(struct wlsc_compositor *ec);
1240
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001241WL_EXPORT int
1242shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001243{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001244 struct wl_shell *shell;
1245
1246 shell = malloc(sizeof *shell);
1247 if (shell == NULL)
1248 return -1;
1249
Kristian Høgsbergf0d91162011-10-11 22:44:23 -04001250 memset(shell, 0, sizeof *shell);
Kristian Høgsberg75840622011-09-06 13:48:16 -04001251 shell->compositor = ec;
1252 shell->shell.activate = activate;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001253 shell->shell.lock = lock;
Pekka Paalanen9ef3e012011-11-15 13:34:48 +02001254 shell->shell.unlock = unlock;
Kristian Høgsberg32e24cc2011-11-09 12:07:35 -05001255 shell->shell.map = map;
1256 shell->shell.configure = configure;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001257
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001258 wl_list_init(&shell->hidden_surface_list);
Benjamin Franzked0f79ab2011-11-22 12:43:52 +01001259 wl_list_init(&shell->backgrounds);
1260 wl_list_init(&shell->panels);
Pekka Paalanen77346a62011-11-30 16:26:35 +02001261 wl_list_init(&shell->screensaver.surfaces);
Pekka Paalanenf0fc70d2011-11-15 13:34:54 +02001262
Pekka Paalanene955f1e2011-12-07 11:49:52 +02001263 if (shell_configuration(shell) < 0)
1264 return -1;
1265
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04001266 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1267 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001268 return -1;
1269
Kristian Høgsberg75840622011-09-06 13:48:16 -04001270 if (wl_display_add_global(ec->wl_display,
1271 &desktop_shell_interface,
1272 shell, bind_desktop_shell) == NULL)
1273 return -1;
1274
Pekka Paalanen6e168112011-11-24 11:34:05 +02001275 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1276 shell, bind_screensaver) == NULL)
1277 return -1;
1278
Pekka Paalanen6cd281a2011-11-03 14:11:32 +02001279 if (launch_desktop_shell_process(shell) != 0)
1280 return -1;
1281
Kristian Høgsberg07937562011-04-12 17:25:42 -04001282 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001283 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -04001284 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001285 resize_binding, shell);
Kristian Høgsberge1a850e2011-12-19 15:18:05 -05001286 wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1287 MODIFIER_CTRL | MODIFIER_ALT,
1288 terminate_binding, ec);
1289 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, 0,
1290 click_to_activate_binding, ec);
1291
1292
1293
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001294
1295 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -04001296
Kristian Høgsberg4cca3492011-01-18 07:53:49 -05001297 return 0;
1298}