shell: fix move of transformed surfaces
[profile/ivi/weston.git] / src / shell.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  *
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>
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <linux/input.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <math.h>
33
34 #include <wayland-server.h>
35 #include "compositor.h"
36 #include "desktop-shell-server-protocol.h"
37 #include "../shared/config-parser.h"
38
39 struct shell_surface;
40
41 struct wl_shell {
42         struct weston_compositor *compositor;
43         struct weston_shell shell;
44
45         struct {
46                 struct weston_process process;
47                 struct wl_client *client;
48                 struct wl_resource *desktop_shell;
49
50                 unsigned deathcount;
51                 uint32_t deathstamp;
52         } child;
53
54         bool locked;
55         bool prepare_event_sent;
56
57         struct shell_surface *lock_surface;
58         struct wl_listener lock_surface_listener;
59         struct wl_list hidden_surface_list;
60
61         struct wl_list backgrounds;
62         struct wl_list panels;
63
64         struct {
65                 char *path;
66                 int duration;
67                 struct wl_resource *binding;
68                 struct wl_list surfaces;
69                 struct weston_process process;
70         } screensaver;
71 };
72
73 enum shell_surface_type {
74         SHELL_SURFACE_NONE,
75
76         SHELL_SURFACE_PANEL,
77         SHELL_SURFACE_BACKGROUND,
78         SHELL_SURFACE_LOCK,
79         SHELL_SURFACE_SCREENSAVER,
80
81         SHELL_SURFACE_TOPLEVEL,
82         SHELL_SURFACE_TRANSIENT,
83         SHELL_SURFACE_FULLSCREEN,
84         SHELL_SURFACE_POPUP
85 };
86
87 struct shell_surface {
88         struct wl_resource resource;
89
90         struct weston_surface *surface;
91         struct wl_listener surface_destroy_listener;
92         struct shell_surface *parent;
93
94         enum shell_surface_type type;
95         int32_t saved_x, saved_y;
96
97         struct {
98                 struct weston_transform transform;
99                 struct weston_matrix rotation;
100         } rotation;
101
102         struct {
103                 struct wl_grab grab;
104                 uint32_t time;
105                 int32_t x, y;
106                 int32_t initial_up;
107         } popup;
108
109         struct weston_output *output;
110         struct wl_list link;
111 };
112
113 struct weston_move_grab {
114         struct wl_grab grab;
115         struct weston_surface *surface;
116         int32_t dx, dy;
117 };
118
119 struct rotate_grab {
120         struct wl_grab grab;
121         struct shell_surface *surface;
122         struct weston_matrix rotation;
123         struct {
124                 int32_t x;
125                 int32_t y;
126         } center;
127 };
128
129 static void
130 shell_configuration(struct wl_shell *shell)
131 {
132         char *config_file;
133         char *path = NULL;
134         int duration = 60;
135
136         struct config_key saver_keys[] = {
137                 { "path",       CONFIG_KEY_STRING,  &path },
138                 { "duration",   CONFIG_KEY_INTEGER, &duration },
139         };
140
141         struct config_section cs[] = {
142                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
143         };
144
145         config_file = config_file_path("weston-desktop-shell.ini");
146         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
147         free(config_file);
148
149         shell->screensaver.path = path;
150         shell->screensaver.duration = duration;
151 }
152
153 static void
154 noop_grab_focus(struct wl_grab *grab, uint32_t time,
155                 struct wl_surface *surface, int32_t x, int32_t y)
156 {
157         grab->focus = NULL;
158 }
159
160 static void
161 move_grab_motion(struct wl_grab *grab,
162                  uint32_t time, int32_t x, int32_t y)
163 {
164         struct weston_move_grab *move = (struct weston_move_grab *) grab;
165         struct wl_input_device *device = grab->input_device;
166         struct weston_surface *es = move->surface;
167
168         weston_surface_configure(es,
169                                  device->x + move->dx,
170                                  device->y + move->dy,
171                                  es->geometry.width, es->geometry.height);
172 }
173
174 static void
175 move_grab_button(struct wl_grab *grab,
176                  uint32_t time, int32_t button, int32_t state)
177 {
178         struct wl_input_device *device = grab->input_device;
179
180         if (device->button_count == 0 && state == 0) {
181                 wl_input_device_end_grab(device, time);
182                 free(grab);
183         }
184 }
185
186 static const struct wl_grab_interface move_grab_interface = {
187         noop_grab_focus,
188         move_grab_motion,
189         move_grab_button,
190 };
191
192 static int
193 weston_surface_move(struct weston_surface *es,
194                     struct weston_input_device *wd, uint32_t time)
195 {
196         struct weston_move_grab *move;
197
198         move = malloc(sizeof *move);
199         if (!move)
200                 return -1;
201
202         move->grab.interface = &move_grab_interface;
203         move->dx = es->geometry.x - wd->input_device.grab_x;
204         move->dy = es->geometry.y - wd->input_device.grab_y;
205         move->surface = es;
206
207         wl_input_device_start_grab(&wd->input_device, &move->grab, time);
208
209         wl_input_device_set_pointer_focus(&wd->input_device,
210                                           NULL, time, 0, 0, 0, 0);
211
212         return 0;
213 }
214
215 static void
216 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
217                    struct wl_resource *input_resource, uint32_t time)
218 {
219         struct weston_input_device *wd = input_resource->data;
220         struct shell_surface *shsurf = resource->data;
221
222         if (wd->input_device.button_count == 0 ||
223             wd->input_device.grab_time != time ||
224             wd->input_device.pointer_focus != &shsurf->surface->surface)
225                 return;
226
227         if (weston_surface_move(shsurf->surface, wd, time) < 0)
228                 wl_resource_post_no_memory(resource);
229 }
230
231 struct weston_resize_grab {
232         struct wl_grab grab;
233         uint32_t edges;
234         int32_t dx, dy, width, height;
235         struct shell_surface *shsurf;
236 };
237
238 static void
239 resize_grab_motion(struct wl_grab *grab,
240                    uint32_t time, int32_t x, int32_t y)
241 {
242         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
243         struct wl_input_device *device = grab->input_device;
244         int32_t width, height;
245
246         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
247                 width = device->grab_x - device->x + resize->width;
248         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
249                 width = device->x - device->grab_x + resize->width;
250         } else {
251                 width = resize->width;
252         }
253
254         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
255                 height = device->grab_y - device->y + resize->height;
256         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
257                 height = device->y - device->grab_y + resize->height;
258         } else {
259                 height = resize->height;
260         }
261
262         wl_resource_post_event(&resize->shsurf->resource,
263                                WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
264                                width, height);
265 }
266
267 static void
268 resize_grab_button(struct wl_grab *grab,
269                    uint32_t time, int32_t button, int32_t state)
270 {
271         struct wl_input_device *device = grab->input_device;
272
273         if (device->button_count == 0 && state == 0) {
274                 wl_input_device_end_grab(device, time);
275                 free(grab);
276         }
277 }
278
279 static const struct wl_grab_interface resize_grab_interface = {
280         noop_grab_focus,
281         resize_grab_motion,
282         resize_grab_button,
283 };
284
285 static int
286 weston_surface_resize(struct shell_surface *shsurf,
287                     struct weston_input_device *wd,
288                     uint32_t time, uint32_t edges)
289 {
290         struct weston_resize_grab *resize;
291         struct weston_surface *es = shsurf->surface;
292
293         /* FIXME: Reject if fullscreen */
294
295         resize = malloc(sizeof *resize);
296         if (!resize)
297                 return -1;
298
299         resize->grab.interface = &resize_grab_interface;
300         resize->edges = edges;
301         resize->dx = es->geometry.x - wd->input_device.grab_x;
302         resize->dy = es->geometry.y - wd->input_device.grab_y;
303         resize->width = es->geometry.width;
304         resize->height = es->geometry.height;
305         resize->shsurf = shsurf;
306
307         if (edges == 0 || edges > 15 ||
308             (edges & 3) == 3 || (edges & 12) == 12)
309                 goto err_out;
310
311         wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
312
313         wl_input_device_set_pointer_focus(&wd->input_device,
314                                           NULL, time, 0, 0, 0, 0);
315
316         return 0;
317
318 err_out:
319         free(resize);
320         return 0;
321 }
322
323 static void
324 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
325                      struct wl_resource *input_resource, uint32_t time,
326                      uint32_t edges)
327 {
328         struct weston_input_device *wd = input_resource->data;
329         struct shell_surface *shsurf = resource->data;
330
331         /* FIXME: Reject if fullscreen */
332
333         if (wd->input_device.button_count == 0 ||
334             wd->input_device.grab_time != time ||
335             wd->input_device.pointer_focus != &shsurf->surface->surface)
336                 return;
337
338         if (weston_surface_resize(shsurf, wd, time, edges) < 0)
339                 wl_resource_post_no_memory(resource);
340 }
341
342 static int
343 reset_shell_surface_type(struct shell_surface *surface)
344 {
345         switch (surface->type) {
346         case SHELL_SURFACE_FULLSCREEN:
347                 surface->surface->geometry.x = surface->saved_x;
348                 surface->surface->geometry.y = surface->saved_y;
349                 surface->surface->geometry.dirty = 1;
350                 surface->surface->fullscreen_output = NULL;
351                 break;
352         case SHELL_SURFACE_PANEL:
353         case SHELL_SURFACE_BACKGROUND:
354                 wl_list_remove(&surface->link);
355                 wl_list_init(&surface->link);
356                 break;
357         case SHELL_SURFACE_SCREENSAVER:
358         case SHELL_SURFACE_LOCK:
359                 wl_resource_post_error(&surface->resource,
360                                        WL_DISPLAY_ERROR_INVALID_METHOD,
361                                        "cannot reassign surface type");
362                 return -1;
363         case SHELL_SURFACE_NONE:
364         case SHELL_SURFACE_TOPLEVEL:
365         case SHELL_SURFACE_TRANSIENT:
366         case SHELL_SURFACE_POPUP:
367                 break;
368         }
369
370         surface->type = SHELL_SURFACE_NONE;
371         return 0;
372 }
373
374 static void
375 shell_surface_set_toplevel(struct wl_client *client,
376                            struct wl_resource *resource)
377
378 {
379         struct shell_surface *surface = resource->data;
380
381         if (reset_shell_surface_type(surface))
382                 return;
383
384         weston_surface_damage(surface->surface);
385         surface->type = SHELL_SURFACE_TOPLEVEL;
386 }
387
388 static void
389 shell_surface_set_transient(struct wl_client *client,
390                             struct wl_resource *resource,
391                             struct wl_resource *parent_resource,
392                             int x, int y, uint32_t flags)
393 {
394         struct shell_surface *shsurf = resource->data;
395         struct weston_surface *es = shsurf->surface;
396         struct shell_surface *pshsurf = parent_resource->data;
397         struct weston_surface *pes = pshsurf->surface;
398
399         if (reset_shell_surface_type(shsurf))
400                 return;
401
402         /* assign to parents output  */
403         es->output = pes->output;
404  
405         es->geometry.x = pes->geometry.x + x;
406         es->geometry.y = pes->geometry.y + y;
407         es->geometry.dirty = 1;
408
409         weston_surface_damage(es);
410         shsurf->type = SHELL_SURFACE_TRANSIENT;
411 }
412
413 static struct weston_output *
414 get_default_output(struct weston_compositor *compositor)
415 {
416         return container_of(compositor->output_list.next,
417                             struct weston_output, link);
418 }
419
420 static void
421 shell_surface_set_fullscreen(struct wl_client *client,
422                              struct wl_resource *resource)
423
424 {
425         struct shell_surface *shsurf = resource->data;
426         struct weston_surface *es = shsurf->surface;
427         struct weston_output *output;
428
429         if (reset_shell_surface_type(shsurf))
430                 return;
431
432         /* FIXME: Fullscreen on first output */
433         /* FIXME: Handle output going away */
434         output = get_default_output(es->compositor);
435         es->output = output;
436
437         shsurf->saved_x = es->geometry.x;
438         shsurf->saved_y = es->geometry.y;
439         es->geometry.x = (output->current->width - es->geometry.width) / 2;
440         es->geometry.y = (output->current->height - es->geometry.height) / 2;
441         es->geometry.dirty = 1;
442         es->fullscreen_output = output;
443         weston_surface_damage(es);
444         shsurf->type = SHELL_SURFACE_FULLSCREEN;
445 }
446
447 static void
448 popup_grab_focus(struct wl_grab *grab, uint32_t time,
449                  struct wl_surface *surface, int32_t x, int32_t y)
450 {
451         struct wl_input_device *device = grab->input_device;
452         struct shell_surface *priv =
453                 container_of(grab, struct shell_surface, popup.grab);
454         struct wl_client *client = priv->surface->surface.resource.client;
455
456         if (surface && surface->resource.client == client) {
457                 wl_input_device_set_pointer_focus(device, surface, time,
458                                                   device->x, device->y, x, y);
459                 grab->focus = surface;
460         } else {
461                 wl_input_device_set_pointer_focus(device, NULL,
462                                                   time, 0, 0, 0, 0);
463                 grab->focus = NULL;
464         }
465 }
466
467 static void
468 popup_grab_motion(struct wl_grab *grab,
469                   uint32_t time, int32_t x, int32_t y)
470 {
471         struct wl_input_device *device = grab->input_device;
472         struct wl_resource *resource;
473
474         resource = grab->input_device->pointer_focus_resource;
475         if (resource)
476                 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
477                                        time, device->x, device->y, x, y);
478 }
479
480 static void
481 popup_grab_button(struct wl_grab *grab,
482                   uint32_t time, int32_t button, int32_t state)
483 {
484         struct wl_resource *resource;
485         struct shell_surface *shsurf =
486                 container_of(grab, struct shell_surface, popup.grab);
487
488         resource = grab->input_device->pointer_focus_resource;
489         if (resource) {
490                 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
491                                        time, button, state);
492         } else if (state == 0 &&
493                    (shsurf->popup.initial_up ||
494                     time - shsurf->popup.time > 500)) {
495                 wl_resource_post_event(&shsurf->resource,
496                                        WL_SHELL_SURFACE_POPUP_DONE);
497                 wl_input_device_end_grab(grab->input_device, time);
498                 shsurf->popup.grab.input_device = NULL;
499         }
500
501         if (state == 0)
502                 shsurf->popup.initial_up = 1;
503 }
504
505 static const struct wl_grab_interface popup_grab_interface = {
506         popup_grab_focus,
507         popup_grab_motion,
508         popup_grab_button,
509 };
510
511 static void
512 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
513 {
514         struct wl_input_device *device;
515         struct weston_surface *es = shsurf->surface;
516         struct weston_surface *parent = shsurf->parent->surface;
517
518         es->output = parent->output;
519
520         shsurf->popup.grab.interface = &popup_grab_interface;
521         device = es->compositor->input_device;
522
523         es->geometry.x = shsurf->parent->surface->geometry.x + shsurf->popup.x;
524         es->geometry.y = shsurf->parent->surface->geometry.y + shsurf->popup.y;
525         es->geometry.dirty = 1;
526
527         shsurf->popup.grab.input_device = device;
528         shsurf->popup.time = device->grab_time;
529         shsurf->popup.initial_up = 0;
530
531         wl_input_device_start_grab(shsurf->popup.grab.input_device,
532                                    &shsurf->popup.grab, shsurf->popup.time);
533 }
534
535 static void
536 shell_surface_set_popup(struct wl_client *client,
537                         struct wl_resource *resource,
538                         struct wl_resource *input_device_resource,
539                         uint32_t time,
540                         struct wl_resource *parent_resource,
541                         int32_t x, int32_t y, uint32_t flags)
542 {
543         struct shell_surface *shsurf = resource->data;
544         struct weston_surface *es = shsurf->surface;
545
546         weston_surface_damage(es);
547         shsurf->type = SHELL_SURFACE_POPUP;
548         shsurf->parent = parent_resource->data;
549         shsurf->popup.x = x;
550         shsurf->popup.y = y;
551 }
552
553 static const struct wl_shell_surface_interface shell_surface_implementation = {
554         shell_surface_move,
555         shell_surface_resize,
556         shell_surface_set_toplevel,
557         shell_surface_set_transient,
558         shell_surface_set_fullscreen,
559         shell_surface_set_popup
560 };
561
562 static void
563 destroy_shell_surface(struct wl_resource *resource)
564 {
565         struct shell_surface *shsurf = resource->data;
566
567         if (shsurf->popup.grab.input_device)
568                 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
569
570         /* in case cleaning up a dead client destroys shell_surface first */
571         if (shsurf->surface)
572                 wl_list_remove(&shsurf->surface_destroy_listener.link);
573
574         wl_list_remove(&shsurf->link);
575         free(shsurf);
576 }
577
578 static void
579 shell_handle_surface_destroy(struct wl_listener *listener,
580                              struct wl_resource *resource, uint32_t time)
581 {
582         struct shell_surface *shsurf = container_of(listener,
583                                                     struct shell_surface,
584                                                     surface_destroy_listener);
585
586         shsurf->surface = NULL;
587         wl_resource_destroy(&shsurf->resource, time);
588 }
589
590 static struct shell_surface *
591 get_shell_surface(struct weston_surface *surface)
592 {
593         struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
594         struct wl_listener *listener;
595
596         /* search the destroy listener list for our callback */
597         wl_list_for_each(listener, lst, link) {
598                 if (listener->func == shell_handle_surface_destroy) {
599                         return container_of(listener, struct shell_surface,
600                                             surface_destroy_listener);
601                 }
602         }
603
604         return NULL;
605 }
606
607 static void
608 shell_get_shell_surface(struct wl_client *client,
609                         struct wl_resource *resource,
610                         uint32_t id,
611                         struct wl_resource *surface_resource)
612 {
613         struct weston_surface *surface = surface_resource->data;
614         struct shell_surface *shsurf;
615
616         if (get_shell_surface(surface)) {
617                 wl_resource_post_error(surface_resource,
618                         WL_DISPLAY_ERROR_INVALID_OBJECT,
619                         "wl_shell::get_shell_surface already requested");
620                 return;
621         }
622
623         shsurf = calloc(1, sizeof *shsurf);
624         if (!shsurf) {
625                 wl_resource_post_no_memory(resource);
626                 return;
627         }
628
629         shsurf->resource.destroy = destroy_shell_surface;
630         shsurf->resource.object.id = id;
631         shsurf->resource.object.interface = &wl_shell_surface_interface;
632         shsurf->resource.object.implementation =
633                 (void (**)(void)) &shell_surface_implementation;
634         shsurf->resource.data = shsurf;
635
636         shsurf->surface = surface;
637         shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
638         wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
639                        &shsurf->surface_destroy_listener.link);
640
641         /* init link so its safe to always remove it in destroy_shell_surface */
642         wl_list_init(&shsurf->link);
643
644         /* empty when not in use */
645         wl_list_init(&shsurf->rotation.transform.link);
646         weston_matrix_init(&shsurf->rotation.rotation);
647
648         shsurf->type = SHELL_SURFACE_NONE;
649
650         wl_client_add_resource(client, &shsurf->resource);
651 }
652
653 static const struct wl_shell_interface shell_implementation = {
654         shell_get_shell_surface
655 };
656
657 static void
658 handle_screensaver_sigchld(struct weston_process *proc, int status)
659 {
660         proc->pid = 0;
661 }
662
663 static void
664 launch_screensaver(struct wl_shell *shell)
665 {
666         if (shell->screensaver.binding)
667                 return;
668
669         if (!shell->screensaver.path)
670                 return;
671
672         weston_client_launch(shell->compositor,
673                            &shell->screensaver.process,
674                            shell->screensaver.path,
675                            handle_screensaver_sigchld);
676 }
677
678 static void
679 terminate_screensaver(struct wl_shell *shell)
680 {
681         if (shell->screensaver.process.pid == 0)
682                 return;
683
684         kill(shell->screensaver.process.pid, SIGTERM);
685 }
686
687 static void
688 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
689 {
690         struct wl_list *list;
691
692         if (shell->lock_surface)
693                 list = &shell->lock_surface->surface->link;
694         else
695                 list = &shell->compositor->surface_list;
696
697         wl_list_remove(&surface->surface->link);
698         wl_list_insert(list, &surface->surface->link);
699         weston_surface_configure(surface->surface,
700                                surface->surface->geometry.x,
701                                surface->surface->geometry.y,
702                                surface->surface->geometry.width,
703                                surface->surface->geometry.height);
704         surface->surface->output = surface->output;
705 }
706
707 static void
708 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
709 {
710         wl_list_remove(&surface->surface->link);
711         wl_list_init(&surface->surface->link);
712         surface->surface->output = NULL;
713 }
714
715 static void
716 desktop_shell_set_background(struct wl_client *client,
717                              struct wl_resource *resource,
718                              struct wl_resource *output_resource,
719                              struct wl_resource *surface_resource)
720 {
721         struct wl_shell *shell = resource->data;
722         struct shell_surface *shsurf = surface_resource->data;
723         struct weston_surface *surface = shsurf->surface;
724         struct shell_surface *priv;
725
726         if (reset_shell_surface_type(shsurf))
727                 return;
728
729         wl_list_for_each(priv, &shell->backgrounds, link) {
730                 if (priv->output == output_resource->data) {
731                         priv->surface->output = NULL;
732                         wl_list_remove(&priv->surface->link);
733                         wl_list_remove(&priv->link);
734                         break;
735                 }
736         }
737
738         shsurf->type = SHELL_SURFACE_BACKGROUND;
739         shsurf->output = output_resource->data;
740
741         wl_list_insert(&shell->backgrounds, &shsurf->link);
742
743         surface->geometry.x = shsurf->output->x;
744         surface->geometry.y = shsurf->output->y;
745         surface->geometry.dirty = 1;
746
747         wl_resource_post_event(resource,
748                                DESKTOP_SHELL_CONFIGURE,
749                                weston_compositor_get_time(), 0, surface_resource,
750                                shsurf->output->current->width,
751                                shsurf->output->current->height);
752 }
753
754 static void
755 desktop_shell_set_panel(struct wl_client *client,
756                         struct wl_resource *resource,
757                         struct wl_resource *output_resource,
758                         struct wl_resource *surface_resource)
759 {
760         struct wl_shell *shell = resource->data;
761         struct shell_surface *shsurf = surface_resource->data;
762         struct weston_surface *surface = shsurf->surface;
763         struct shell_surface *priv;
764
765         if (reset_shell_surface_type(shsurf))
766                 return;
767
768         wl_list_for_each(priv, &shell->panels, link) {
769                 if (priv->output == output_resource->data) {
770                         priv->surface->output = NULL;
771                         wl_list_remove(&priv->surface->link);
772                         wl_list_remove(&priv->link);
773                         break;
774                 }
775         }
776
777         shsurf->type = SHELL_SURFACE_PANEL;
778         shsurf->output = output_resource->data;
779
780         wl_list_insert(&shell->panels, &shsurf->link);
781
782         surface->geometry.x = shsurf->output->x;
783         surface->geometry.y = shsurf->output->y;
784         surface->geometry.dirty = 1;
785
786         wl_resource_post_event(resource,
787                                DESKTOP_SHELL_CONFIGURE,
788                                weston_compositor_get_time(), 0, surface_resource,
789                                shsurf->output->current->width,
790                                shsurf->output->current->height);
791 }
792
793 static void
794 handle_lock_surface_destroy(struct wl_listener *listener,
795                             struct wl_resource *resource, uint32_t time)
796 {
797         struct wl_shell *shell =
798                 container_of(listener, struct wl_shell, lock_surface_listener);
799
800         fprintf(stderr, "lock surface gone\n");
801         shell->lock_surface = NULL;
802 }
803
804 static void
805 desktop_shell_set_lock_surface(struct wl_client *client,
806                                struct wl_resource *resource,
807                                struct wl_resource *surface_resource)
808 {
809         struct wl_shell *shell = resource->data;
810         struct shell_surface *surface = surface_resource->data;
811
812         if (reset_shell_surface_type(surface))
813                 return;
814
815         shell->prepare_event_sent = false;
816
817         if (!shell->locked)
818                 return;
819
820         shell->lock_surface = surface;
821
822         shell->lock_surface_listener.func = handle_lock_surface_destroy;
823         wl_list_insert(&surface_resource->destroy_listener_list,
824                        &shell->lock_surface_listener.link);
825
826         shell->lock_surface->type = SHELL_SURFACE_LOCK;
827 }
828
829 static void
830 resume_desktop(struct wl_shell *shell)
831 {
832         struct weston_surface *surface;
833         struct wl_list *list;
834         struct shell_surface *tmp;
835
836         wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
837                 hide_screensaver(shell, tmp);
838
839         terminate_screensaver(shell);
840
841         wl_list_for_each(surface, &shell->hidden_surface_list, link)
842                 weston_surface_configure(surface, surface->geometry.x,
843                                          surface->geometry.y,
844                                          surface->geometry.width,
845                                          surface->geometry.height);
846
847         if (wl_list_empty(&shell->backgrounds)) {
848                 list = &shell->compositor->surface_list;
849         } else {
850                 struct shell_surface *background;
851                 background = container_of(shell->backgrounds.prev,
852                                           struct shell_surface, link);
853                 list = background->surface->link.prev;
854         }
855
856         if (!wl_list_empty(&shell->hidden_surface_list))
857                 wl_list_insert_list(list, &shell->hidden_surface_list);
858         wl_list_init(&shell->hidden_surface_list);
859
860         shell->locked = false;
861         weston_compositor_repick(shell->compositor);
862         shell->compositor->idle_time = shell->compositor->option_idle_time;
863         weston_compositor_wake(shell->compositor);
864 }
865
866 static void
867 desktop_shell_unlock(struct wl_client *client,
868                      struct wl_resource *resource)
869 {
870         struct wl_shell *shell = resource->data;
871
872         shell->prepare_event_sent = false;
873
874         if (shell->locked)
875                 resume_desktop(shell);
876 }
877
878 static const struct desktop_shell_interface desktop_shell_implementation = {
879         desktop_shell_set_background,
880         desktop_shell_set_panel,
881         desktop_shell_set_lock_surface,
882         desktop_shell_unlock
883 };
884
885 static enum shell_surface_type
886 get_shell_surface_type(struct weston_surface *surface)
887 {
888         struct shell_surface *shsurf;
889
890         shsurf = get_shell_surface(surface);
891         if (!shsurf)
892                 return SHELL_SURFACE_NONE;
893         return shsurf->type;
894 }
895
896 static void
897 move_binding(struct wl_input_device *device, uint32_t time,
898              uint32_t key, uint32_t button, uint32_t state, void *data)
899 {
900         struct weston_surface *surface =
901                 (struct weston_surface *) device->pointer_focus;
902
903         if (surface == NULL)
904                 return;
905
906         switch (get_shell_surface_type(surface)) {
907                 case SHELL_SURFACE_PANEL:
908                 case SHELL_SURFACE_BACKGROUND:
909                 case SHELL_SURFACE_FULLSCREEN:
910                 case SHELL_SURFACE_SCREENSAVER:
911                         return;
912                 default:
913                         break;
914         }
915
916         weston_surface_move(surface, (struct weston_input_device *) device, time);
917 }
918
919 static void
920 resize_binding(struct wl_input_device *device, uint32_t time,
921                uint32_t key, uint32_t button, uint32_t state, void *data)
922 {
923         struct weston_surface *surface =
924                 (struct weston_surface *) device->pointer_focus;
925         uint32_t edges = 0;
926         int32_t x, y;
927         struct shell_surface *shsurf;
928
929         if (surface == NULL)
930                 return;
931
932         shsurf = get_shell_surface(surface);
933         if (!shsurf)
934                 return;
935
936         switch (shsurf->type) {
937                 case SHELL_SURFACE_PANEL:
938                 case SHELL_SURFACE_BACKGROUND:
939                 case SHELL_SURFACE_FULLSCREEN:
940                 case SHELL_SURFACE_SCREENSAVER:
941                         return;
942                 default:
943                         break;
944         }
945
946         /* FIXME: convert properly to surface coordinates */
947         x = device->grab_x - surface->geometry.x;
948         y = device->grab_y - surface->geometry.y;
949
950         if (x < surface->geometry.width / 3)
951                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
952         else if (x < 2 * surface->geometry.width / 3)
953                 edges |= 0;
954         else
955                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
956
957         if (y < surface->geometry.height / 3)
958                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
959         else if (y < 2 * surface->geometry.height / 3)
960                 edges |= 0;
961         else
962                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
963
964         weston_surface_resize(shsurf, (struct weston_input_device *) device,
965                             time, edges);
966 }
967
968 static void
969 terminate_binding(struct wl_input_device *device, uint32_t time,
970                   uint32_t key, uint32_t button, uint32_t state, void *data)
971 {
972         struct weston_compositor *compositor = data;
973
974         if (state)
975                 wl_display_terminate(compositor->wl_display);
976 }
977
978 static void
979 rotate_grab_motion(struct wl_grab *grab,
980                  uint32_t time, int32_t x, int32_t y)
981 {
982         struct rotate_grab *rotate =
983                 container_of(grab, struct rotate_grab, grab);
984         struct wl_input_device *device = grab->input_device;
985         struct shell_surface *surface = rotate->surface;
986         GLfloat cx = 0.5f * surface->surface->geometry.width;
987         GLfloat cy = 0.5f * surface->surface->geometry.height;
988         GLfloat dx, dy;
989         GLfloat r;
990
991         dx = device->x - rotate->center.x;
992         dy = device->y - rotate->center.y;
993         r = sqrtf(dx * dx + dy * dy);
994
995         wl_list_remove(&surface->rotation.transform.link);
996         surface->surface->geometry.dirty = 1;
997
998         if (r > 20.0f) {
999                 struct weston_matrix *matrix =
1000                         &surface->rotation.transform.matrix;
1001
1002                 weston_matrix_init(&rotate->rotation);
1003                 rotate->rotation.d[0] = dx / r;
1004                 rotate->rotation.d[4] = -dy / r;
1005                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1006                 rotate->rotation.d[5] = rotate->rotation.d[0];
1007
1008                 weston_matrix_init(matrix);
1009                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1010                 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1011                 weston_matrix_multiply(matrix, &rotate->rotation);
1012                 weston_matrix_translate(matrix, cx, cy, 0.0f);
1013
1014                 wl_list_insert(
1015                         &surface->surface->geometry.transformation_list,
1016                         &surface->rotation.transform.link);
1017         } else {
1018                 wl_list_init(&surface->rotation.transform.link);
1019                 weston_matrix_init(&surface->rotation.rotation);
1020                 weston_matrix_init(&rotate->rotation);
1021         }
1022
1023         weston_compositor_damage_all(surface->surface->compositor);
1024 }
1025
1026 static void
1027 rotate_grab_button(struct wl_grab *grab,
1028                  uint32_t time, int32_t button, int32_t state)
1029 {
1030         struct rotate_grab *rotate =
1031                 container_of(grab, struct rotate_grab, grab);
1032         struct wl_input_device *device = grab->input_device;
1033         struct shell_surface *surface = rotate->surface;
1034
1035         if (device->button_count == 0 && state == 0) {
1036                 weston_matrix_multiply(&surface->rotation.rotation,
1037                                        &rotate->rotation);
1038                 wl_input_device_end_grab(device, time);
1039                 free(rotate);
1040         }
1041 }
1042
1043 static const struct wl_grab_interface rotate_grab_interface = {
1044         noop_grab_focus,
1045         rotate_grab_motion,
1046         rotate_grab_button,
1047 };
1048
1049 static void
1050 rotate_binding(struct wl_input_device *device, uint32_t time,
1051                uint32_t key, uint32_t button, uint32_t state, void *data)
1052 {
1053         struct weston_surface *base_surface =
1054                 (struct weston_surface *) device->pointer_focus;
1055         struct shell_surface *surface;
1056         struct rotate_grab *rotate;
1057         GLfloat dx, dy;
1058         GLfloat r;
1059
1060         if (base_surface == NULL)
1061                 return;
1062
1063         surface = get_shell_surface(base_surface);
1064         if (!surface)
1065                 return;
1066
1067         switch (surface->type) {
1068                 case SHELL_SURFACE_PANEL:
1069                 case SHELL_SURFACE_BACKGROUND:
1070                 case SHELL_SURFACE_FULLSCREEN:
1071                 case SHELL_SURFACE_SCREENSAVER:
1072                         return;
1073                 default:
1074                         break;
1075         }
1076
1077         rotate = malloc(sizeof *rotate);
1078         if (!rotate)
1079                 return;
1080
1081         rotate->grab.interface = &rotate_grab_interface;
1082         rotate->surface = surface;
1083
1084         weston_surface_to_global(surface->surface,
1085                                  surface->surface->geometry.width / 2,
1086                                  surface->surface->geometry.height / 2,
1087                                  &rotate->center.x, &rotate->center.y);
1088
1089         wl_input_device_start_grab(device, &rotate->grab, time);
1090
1091         dx = device->x - rotate->center.x;
1092         dy = device->y - rotate->center.y;
1093         r = sqrtf(dx * dx + dy * dy);
1094         if (r > 20.0f) {
1095                 struct weston_matrix inverse;
1096
1097                 weston_matrix_init(&inverse);
1098                 inverse.d[0] = dx / r;
1099                 inverse.d[4] = dy / r;
1100                 inverse.d[1] = -inverse.d[4];
1101                 inverse.d[5] = inverse.d[0];
1102                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1103         } else {
1104                 weston_matrix_init(&surface->rotation.rotation);
1105                 weston_matrix_init(&rotate->rotation);
1106         }
1107
1108         wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1109 }
1110
1111 static void
1112 activate(struct weston_shell *base, struct weston_surface *es,
1113          struct weston_input_device *device, uint32_t time)
1114 {
1115         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1116         struct weston_compositor *compositor = shell->compositor;
1117         struct wl_list *list;
1118
1119         weston_surface_activate(es, device, time);
1120
1121         if (compositor->wxs)
1122                 weston_xserver_surface_activate(es);
1123
1124         switch (get_shell_surface_type(es)) {
1125         case SHELL_SURFACE_BACKGROUND:
1126                 /* put background back to bottom */
1127                 wl_list_remove(&es->link);
1128                 wl_list_insert(compositor->surface_list.prev, &es->link);
1129                 break;
1130         case SHELL_SURFACE_PANEL:
1131                 /* already put on top */
1132                 break;
1133         case SHELL_SURFACE_SCREENSAVER:
1134                 /* always below lock surface */
1135                 if (shell->lock_surface) {
1136                         wl_list_remove(&es->link);
1137                         wl_list_insert(&shell->lock_surface->surface->link,
1138                                        &es->link);
1139                 }
1140                 break;
1141         default:
1142                 if (!shell->locked) {
1143                         list = weston_compositor_top(compositor);
1144
1145                         /* bring panel back to top */
1146                         struct shell_surface *panel;
1147                         wl_list_for_each(panel, &shell->panels, link) {
1148                                 wl_list_remove(&panel->surface->link);
1149                                 wl_list_insert(list, &panel->surface->link);
1150                         }
1151                 }
1152         }
1153 }
1154
1155 static void
1156 click_to_activate_binding(struct wl_input_device *device,
1157                          uint32_t time, uint32_t key,
1158                           uint32_t button, uint32_t state, void *data)
1159 {
1160         struct weston_input_device *wd = (struct weston_input_device *) device;
1161         struct weston_compositor *compositor = data;
1162         struct weston_surface *focus;
1163
1164         focus = (struct weston_surface *) device->pointer_focus;
1165         if (state && focus && device->grab == &device->default_grab)
1166                 activate(compositor->shell, focus, wd, time);
1167 }
1168
1169 static void
1170 lock(struct weston_shell *base)
1171 {
1172         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1173         struct wl_list *surface_list = &shell->compositor->surface_list;
1174         struct weston_surface *cur;
1175         struct weston_surface *tmp;
1176         struct weston_input_device *device;
1177         struct shell_surface *shsurf;
1178         uint32_t time;
1179
1180         if (shell->locked)
1181                 return;
1182
1183         shell->locked = true;
1184
1185         /* Move all surfaces from compositor's list to our hidden list,
1186          * except the background. This way nothing else can show or
1187          * receive input events while we are locked. */
1188
1189         if (!wl_list_empty(&shell->hidden_surface_list)) {
1190                 fprintf(stderr,
1191                 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1192                                                                 __func__);
1193         }
1194
1195         wl_list_for_each_safe(cur, tmp, surface_list, link) {
1196                 /* skip input device sprites, cur->surface is uninitialised */
1197                 if (cur->surface.resource.client == NULL)
1198                         continue;
1199
1200                 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
1201                         continue;
1202
1203                 cur->output = NULL;
1204                 wl_list_remove(&cur->link);
1205                 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
1206         }
1207
1208         launch_screensaver(shell);
1209
1210         wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1211                 show_screensaver(shell, shsurf);
1212
1213         if (!wl_list_empty(&shell->screensaver.surfaces)) {
1214                 shell->compositor->idle_time = shell->screensaver.duration;
1215                 weston_compositor_wake(shell->compositor);
1216                 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1217         }
1218
1219         /* reset pointer foci */
1220         weston_compositor_repick(shell->compositor);
1221
1222         /* reset keyboard foci */
1223         time = weston_compositor_get_time();
1224         wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1225                 wl_input_device_set_keyboard_focus(&device->input_device,
1226                                                    NULL, time);
1227         }
1228
1229         /* TODO: disable bindings that should not work while locked. */
1230
1231         /* All this must be undone in resume_desktop(). */
1232 }
1233
1234 static void
1235 unlock(struct weston_shell *base)
1236 {
1237         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1238
1239         if (!shell->locked || shell->lock_surface) {
1240                 weston_compositor_wake(shell->compositor);
1241                 return;
1242         }
1243
1244         /* If desktop-shell client has gone away, unlock immediately. */
1245         if (!shell->child.desktop_shell) {
1246                 resume_desktop(shell);
1247                 return;
1248         }
1249
1250         if (shell->prepare_event_sent)
1251                 return;
1252
1253         wl_resource_post_event(shell->child.desktop_shell,
1254                                DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1255         shell->prepare_event_sent = true;
1256 }
1257
1258 static void
1259 center_on_output(struct weston_surface *surface, struct weston_output *output)
1260 {
1261         struct weston_mode *mode = output->current;
1262
1263         surface->geometry.x =
1264                 output->x + (mode->width - surface->geometry.width) / 2;
1265         surface->geometry.y =
1266                 output->y + (mode->height - surface->geometry.height) / 2;
1267         surface->geometry.dirty = 1;
1268 }
1269
1270 static void
1271 map(struct weston_shell *base,
1272     struct weston_surface *surface, int32_t width, int32_t height)
1273 {
1274         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1275         struct weston_compositor *compositor = shell->compositor;
1276         struct wl_list *list;
1277         struct shell_surface *shsurf;
1278         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1279         int do_configure;
1280
1281         shsurf = get_shell_surface(surface);
1282         if (shsurf)
1283                 surface_type = shsurf->type;
1284
1285         if (shell->locked) {
1286                 list = &shell->hidden_surface_list;
1287                 do_configure = 0;
1288         } else {
1289                 list = weston_compositor_top(compositor);
1290                 do_configure = 1;
1291         }
1292
1293         surface->geometry.width = width;
1294         surface->geometry.height = height;
1295         surface->geometry.dirty = 1;
1296
1297         /* initial positioning, see also configure() */
1298         switch (surface_type) {
1299         case SHELL_SURFACE_TOPLEVEL:
1300                 surface->geometry.x = 10 + random() % 400;
1301                 surface->geometry.y = 10 + random() % 400;
1302                 surface->geometry.dirty = 1;
1303                 break;
1304         case SHELL_SURFACE_SCREENSAVER:
1305         case SHELL_SURFACE_FULLSCREEN:
1306                 center_on_output(surface, surface->fullscreen_output);
1307                 break;
1308         case SHELL_SURFACE_LOCK:
1309                 center_on_output(surface, get_default_output(compositor));
1310                 break;
1311         default:
1312                 ;
1313         }
1314
1315         /* surface stacking order, see also activate() */
1316         switch (surface_type) {
1317         case SHELL_SURFACE_BACKGROUND:
1318                 /* background always visible, at the bottom */
1319                 wl_list_insert(compositor->surface_list.prev, &surface->link);
1320                 do_configure = 1;
1321                 break;
1322         case SHELL_SURFACE_PANEL:
1323                 /* panel always on top, hidden while locked */
1324                 wl_list_insert(list, &surface->link);
1325                 break;
1326         case SHELL_SURFACE_LOCK:
1327                 /* lock surface always visible, on top */
1328                 wl_list_insert(&compositor->surface_list, &surface->link);
1329
1330                 weston_compositor_repick(compositor);
1331                 weston_compositor_wake(compositor);
1332                 do_configure = 1;
1333                 break;
1334         case SHELL_SURFACE_SCREENSAVER:
1335                 /* If locked, show it. */
1336                 if (shell->locked) {
1337                         show_screensaver(shell, shsurf);
1338                         compositor->idle_time = shell->screensaver.duration;
1339                         weston_compositor_wake(compositor);
1340                         if (!shell->lock_surface)
1341                                 compositor->state = WESTON_COMPOSITOR_IDLE;
1342                 }
1343                 do_configure = 0;
1344                 break;
1345         default:
1346                 /* everything else just below the panel */
1347                 if (!wl_list_empty(&shell->panels)) {
1348                         struct shell_surface *panel =
1349                                 container_of(shell->panels.prev,
1350                                              struct shell_surface, link);
1351                         wl_list_insert(&panel->surface->link, &surface->link);
1352                 } else {
1353                         wl_list_insert(list, &surface->link);
1354                 }
1355         }
1356
1357         switch (surface_type) {
1358         case SHELL_SURFACE_TOPLEVEL:
1359                 surface->geometry.x = 10 + random() % 400;
1360                 surface->geometry.y = 10 + random() % 400;
1361                 surface->geometry.dirty = 1;
1362                 break;
1363         case SHELL_SURFACE_POPUP:
1364                 shell_map_popup(shsurf, shsurf->popup.time);
1365                 break;
1366         default:
1367                 break;
1368         }
1369
1370         surface->geometry.width = width;
1371         surface->geometry.height = height;
1372         surface->geometry.dirty = 1;
1373         if (do_configure) {
1374                 weston_surface_configure(surface, surface->geometry.x,
1375                                          surface->geometry.y,
1376                                          width, height);
1377                 weston_compositor_repick(compositor);
1378         }
1379
1380         switch (surface_type) {
1381         case SHELL_SURFACE_TOPLEVEL:
1382         case SHELL_SURFACE_TRANSIENT:
1383         case SHELL_SURFACE_FULLSCREEN:
1384                 if (!shell->locked)
1385                         activate(base, surface,
1386                                  (struct weston_input_device *)
1387                                         compositor->input_device,
1388                                  weston_compositor_get_time());
1389                 break;
1390         default:
1391                 break;
1392         }
1393
1394         if (surface_type == SHELL_SURFACE_TOPLEVEL)
1395                 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1396 }
1397
1398 static void
1399 configure(struct weston_shell *base, struct weston_surface *surface,
1400           int32_t x, int32_t y, int32_t width, int32_t height)
1401 {
1402         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1403         int do_configure = !shell->locked;
1404         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1405         struct shell_surface *shsurf;
1406
1407         shsurf = get_shell_surface(surface);
1408         if (shsurf)
1409                 surface_type = shsurf->type;
1410
1411         surface->geometry.width = width;
1412         surface->geometry.height = height;
1413         surface->geometry.dirty = 1;
1414
1415         switch (surface_type) {
1416         case SHELL_SURFACE_SCREENSAVER:
1417                 do_configure = !do_configure;
1418                 /* fall through */
1419         case SHELL_SURFACE_FULLSCREEN:
1420                 center_on_output(surface, surface->fullscreen_output);
1421                 break;
1422         default:
1423                 break;
1424         }
1425
1426         /*
1427          * weston_surface_configure() will assign an output, which means
1428          * the surface is supposed to be in compositor->surface_list.
1429          * Be careful with that, and make sure we stay on the right output.
1430          * XXX: would a fullscreen surface need the same handling?
1431          */
1432         if (do_configure) {
1433                 weston_surface_configure(surface, x, y, width, height);
1434
1435                 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1436                         surface->output = shsurf->output;
1437         }
1438 }
1439
1440 static int launch_desktop_shell_process(struct wl_shell *shell);
1441
1442 static void
1443 desktop_shell_sigchld(struct weston_process *process, int status)
1444 {
1445         uint32_t time;
1446         struct wl_shell *shell =
1447                 container_of(process, struct wl_shell, child.process);
1448
1449         shell->child.process.pid = 0;
1450         shell->child.client = NULL; /* already destroyed by wayland */
1451
1452         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1453         time = weston_compositor_get_time();
1454         if (time - shell->child.deathstamp > 30000) {
1455                 shell->child.deathstamp = time;
1456                 shell->child.deathcount = 0;
1457         }
1458
1459         shell->child.deathcount++;
1460         if (shell->child.deathcount > 5) {
1461                 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1462                 return;
1463         }
1464
1465         fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1466         launch_desktop_shell_process(shell);
1467 }
1468
1469 static int
1470 launch_desktop_shell_process(struct wl_shell *shell)
1471 {
1472         const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1473
1474         shell->child.client = weston_client_launch(shell->compositor,
1475                                                  &shell->child.process,
1476                                                  shell_exe,
1477                                                  desktop_shell_sigchld);
1478
1479         if (!shell->child.client)
1480                 return -1;
1481         return 0;
1482 }
1483
1484 static void
1485 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1486 {
1487         struct wl_shell *shell = data;
1488
1489         wl_client_add_object(client, &wl_shell_interface,
1490                              &shell_implementation, id, shell);
1491 }
1492
1493 static void
1494 unbind_desktop_shell(struct wl_resource *resource)
1495 {
1496         struct wl_shell *shell = resource->data;
1497
1498         if (shell->locked)
1499                 resume_desktop(shell);
1500
1501         shell->child.desktop_shell = NULL;
1502         shell->prepare_event_sent = false;
1503         free(resource);
1504 }
1505
1506 static void
1507 bind_desktop_shell(struct wl_client *client,
1508                    void *data, uint32_t version, uint32_t id)
1509 {
1510         struct wl_shell *shell = data;
1511         struct wl_resource *resource;
1512
1513         resource = wl_client_add_object(client, &desktop_shell_interface,
1514                                         &desktop_shell_implementation,
1515                                         id, shell);
1516
1517         if (client == shell->child.client) {
1518                 resource->destroy = unbind_desktop_shell;
1519                 shell->child.desktop_shell = resource;
1520                 return;
1521         }
1522
1523         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1524                                "permission to bind desktop_shell denied");
1525         wl_resource_destroy(resource, 0);
1526 }
1527
1528 static void
1529 screensaver_set_surface(struct wl_client *client,
1530                         struct wl_resource *resource,
1531                         struct wl_resource *shell_surface_resource,
1532                         struct wl_resource *output_resource)
1533 {
1534         struct wl_shell *shell = resource->data;
1535         struct shell_surface *surface = shell_surface_resource->data;
1536         struct weston_output *output = output_resource->data;
1537
1538         if (reset_shell_surface_type(surface))
1539                 return;
1540
1541         surface->type = SHELL_SURFACE_SCREENSAVER;
1542
1543         surface->surface->fullscreen_output = output;
1544         surface->output = output;
1545         wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1546 }
1547
1548 static const struct screensaver_interface screensaver_implementation = {
1549         screensaver_set_surface
1550 };
1551
1552 static void
1553 unbind_screensaver(struct wl_resource *resource)
1554 {
1555         struct wl_shell *shell = resource->data;
1556
1557         shell->screensaver.binding = NULL;
1558         free(resource);
1559 }
1560
1561 static void
1562 bind_screensaver(struct wl_client *client,
1563                  void *data, uint32_t version, uint32_t id)
1564 {
1565         struct wl_shell *shell = data;
1566         struct wl_resource *resource;
1567
1568         resource = wl_client_add_object(client, &screensaver_interface,
1569                                         &screensaver_implementation,
1570                                         id, shell);
1571
1572         if (shell->screensaver.binding == NULL) {
1573                 resource->destroy = unbind_screensaver;
1574                 shell->screensaver.binding = resource;
1575                 return;
1576         }
1577
1578         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1579                                "interface object already bound");
1580         wl_resource_destroy(resource, 0);
1581 }
1582
1583 static void
1584 shell_destroy(struct weston_shell *base)
1585 {
1586         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1587
1588         if (shell->child.client)
1589                 wl_client_destroy(shell->child.client);
1590
1591         free(shell->screensaver.path);
1592         free(shell);
1593 }
1594
1595 int
1596 shell_init(struct weston_compositor *ec);
1597
1598 WL_EXPORT int
1599 shell_init(struct weston_compositor *ec)
1600 {
1601         struct wl_shell *shell;
1602
1603         shell = malloc(sizeof *shell);
1604         if (shell == NULL)
1605                 return -1;
1606
1607         memset(shell, 0, sizeof *shell);
1608         shell->compositor = ec;
1609         shell->shell.lock = lock;
1610         shell->shell.unlock = unlock;
1611         shell->shell.map = map;
1612         shell->shell.configure = configure;
1613         shell->shell.destroy = shell_destroy;
1614
1615         wl_list_init(&shell->hidden_surface_list);
1616         wl_list_init(&shell->backgrounds);
1617         wl_list_init(&shell->panels);
1618         wl_list_init(&shell->screensaver.surfaces);
1619
1620         shell_configuration(shell);
1621
1622         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1623                                   shell, bind_shell) == NULL)
1624                 return -1;
1625
1626         if (wl_display_add_global(ec->wl_display,
1627                                   &desktop_shell_interface,
1628                                   shell, bind_desktop_shell) == NULL)
1629                 return -1;
1630
1631         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1632                                   shell, bind_screensaver) == NULL)
1633                 return -1;
1634
1635         shell->child.deathstamp = weston_compositor_get_time();
1636         if (launch_desktop_shell_process(shell) != 0)
1637                 return -1;
1638
1639         weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
1640                                     move_binding, shell);
1641         weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
1642                                     resize_binding, shell);
1643         weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1644                                     MODIFIER_CTRL | MODIFIER_ALT,
1645                                     terminate_binding, ec);
1646         weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
1647                                     click_to_activate_binding, ec);
1648         weston_compositor_add_binding(ec, 0, BTN_LEFT,
1649                                       MODIFIER_SUPER | MODIFIER_ALT,
1650                                       rotate_binding, NULL);
1651
1652         ec->shell = &shell->shell;
1653
1654         return 0;
1655 }