Remove weston_shell map and configure function pointers
[profile/ivi/weston.git] / src / shell.c
1 /*
2  * Copyright © 2010-2012 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 weston_layer fullscreen_layer;
46         struct weston_layer panel_layer;
47         struct weston_layer toplevel_layer;
48         struct weston_layer background_layer;
49         struct weston_layer lock_layer;
50
51         struct {
52                 struct weston_process process;
53                 struct wl_client *client;
54                 struct wl_resource *desktop_shell;
55
56                 unsigned deathcount;
57                 uint32_t deathstamp;
58         } child;
59
60         bool locked;
61         bool prepare_event_sent;
62
63         struct shell_surface *lock_surface;
64         struct wl_listener lock_surface_listener;
65
66         struct wl_list backgrounds;
67         struct wl_list panels;
68
69         struct {
70                 char *path;
71                 int duration;
72                 struct wl_resource *binding;
73                 struct wl_list surfaces;
74                 struct weston_process process;
75         } screensaver;
76
77         struct weston_surface *debug_repaint_surface;
78 };
79
80 enum shell_surface_type {
81         SHELL_SURFACE_NONE,
82
83         SHELL_SURFACE_PANEL,
84         SHELL_SURFACE_BACKGROUND,
85         SHELL_SURFACE_LOCK,
86         SHELL_SURFACE_SCREENSAVER,
87
88         SHELL_SURFACE_TOPLEVEL,
89         SHELL_SURFACE_TRANSIENT,
90         SHELL_SURFACE_FULLSCREEN,
91         SHELL_SURFACE_MAXIMIZED,
92         SHELL_SURFACE_POPUP
93 };
94
95 struct shell_surface {
96         struct wl_resource resource;
97
98         struct weston_surface *surface;
99         struct wl_listener surface_destroy_listener;
100         struct shell_surface *parent;
101
102         enum shell_surface_type type;
103         int32_t saved_x, saved_y;
104         bool saved_position_valid;
105
106         struct {
107                 struct weston_transform transform;
108                 struct weston_matrix rotation;
109         } rotation;
110
111         struct {
112                 struct wl_pointer_grab grab;
113                 uint32_t time;
114                 int32_t x, y;
115                 struct weston_transform parent_transform;
116                 int32_t initial_up;
117         } popup;
118
119         struct {
120                 enum wl_shell_surface_fullscreen_method type;
121                 struct weston_transform transform; /* matrix from x, y */
122                 uint32_t framerate;
123                 struct weston_surface *black_surface;
124         } fullscreen;
125
126         struct weston_output *fullscreen_output;
127         struct weston_output *output;
128         struct wl_list link;
129
130         int force_configure;
131 };
132
133 struct weston_move_grab {
134         struct wl_pointer_grab grab;
135         struct weston_surface *surface;
136         int32_t dx, dy;
137 };
138
139 struct rotate_grab {
140         struct wl_pointer_grab grab;
141         struct shell_surface *surface;
142         struct weston_matrix rotation;
143         struct {
144                 int32_t x;
145                 int32_t y;
146         } center;
147 };
148
149 static void
150 center_on_output(struct weston_surface *surface,
151                  struct weston_output *output);
152
153 static void
154 shell_configuration(struct wl_shell *shell)
155 {
156         char *config_file;
157         char *path = NULL;
158         int duration = 60;
159
160         struct config_key saver_keys[] = {
161                 { "path",       CONFIG_KEY_STRING,  &path },
162                 { "duration",   CONFIG_KEY_INTEGER, &duration },
163         };
164
165         struct config_section cs[] = {
166                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
167         };
168
169         config_file = config_file_path("weston.ini");
170         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
171         free(config_file);
172
173         shell->screensaver.path = path;
174         shell->screensaver.duration = duration;
175 }
176
177 static void
178 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
179                 struct wl_surface *surface, int32_t x, int32_t y)
180 {
181         grab->focus = NULL;
182 }
183
184 static void
185 move_grab_motion(struct wl_pointer_grab *grab,
186                  uint32_t time, int32_t x, int32_t y)
187 {
188         struct weston_move_grab *move = (struct weston_move_grab *) grab;
189         struct wl_input_device *device = grab->input_device;
190         struct weston_surface *es = move->surface;
191
192         weston_surface_configure(es,
193                                  device->x + move->dx,
194                                  device->y + move->dy,
195                                  es->geometry.width, es->geometry.height);
196 }
197
198 static void
199 move_grab_button(struct wl_pointer_grab *grab,
200                  uint32_t time, int32_t button, int32_t state)
201 {
202         struct wl_input_device *device = grab->input_device;
203
204         if (device->button_count == 0 && state == 0) {
205                 wl_input_device_end_pointer_grab(device, time);
206                 free(grab);
207         }
208 }
209
210 static const struct wl_pointer_grab_interface move_grab_interface = {
211         noop_grab_focus,
212         move_grab_motion,
213         move_grab_button,
214 };
215
216 static int
217 weston_surface_move(struct weston_surface *es,
218                     struct weston_input_device *wd, uint32_t time)
219 {
220         struct weston_move_grab *move;
221
222         move = malloc(sizeof *move);
223         if (!move)
224                 return -1;
225
226         move->grab.interface = &move_grab_interface;
227         move->dx = es->geometry.x - wd->input_device.grab_x;
228         move->dy = es->geometry.y - wd->input_device.grab_y;
229         move->surface = es;
230
231         wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
232
233         wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
234
235         return 0;
236 }
237
238 static void
239 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
240                    struct wl_resource *input_resource, uint32_t time)
241 {
242         struct weston_input_device *wd = input_resource->data;
243         struct shell_surface *shsurf = resource->data;
244
245         if (wd->input_device.button_count == 0 ||
246             wd->input_device.grab_time != time ||
247             wd->input_device.pointer_focus != &shsurf->surface->surface)
248                 return;
249
250         if (weston_surface_move(shsurf->surface, wd, time) < 0)
251                 wl_resource_post_no_memory(resource);
252 }
253
254 struct weston_resize_grab {
255         struct wl_pointer_grab grab;
256         uint32_t edges;
257         int32_t width, height;
258         struct shell_surface *shsurf;
259 };
260
261 static void
262 resize_grab_motion(struct wl_pointer_grab *grab,
263                    uint32_t time, int32_t x, int32_t y)
264 {
265         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
266         struct wl_input_device *device = grab->input_device;
267         int32_t width, height;
268         int32_t from_x, from_y;
269         int32_t to_x, to_y;
270
271         weston_surface_from_global(resize->shsurf->surface,
272                                    device->grab_x, device->grab_y,
273                                    &from_x, &from_y);
274         weston_surface_from_global(resize->shsurf->surface,
275                                    device->x, device->y, &to_x, &to_y);
276
277         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
278                 width = resize->width + from_x - to_x;
279         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
280                 width = resize->width + to_x - from_x;
281         } else {
282                 width = resize->width;
283         }
284
285         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
286                 height = resize->height + from_y - to_y;
287         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
288                 height = resize->height + to_y - from_y;
289         } else {
290                 height = resize->height;
291         }
292
293         wl_shell_surface_send_configure(&resize->shsurf->resource,
294                                         time, resize->edges, width, height);
295 }
296
297 static void
298 resize_grab_button(struct wl_pointer_grab *grab,
299                    uint32_t time, int32_t button, int32_t state)
300 {
301         struct wl_input_device *device = grab->input_device;
302
303         if (device->button_count == 0 && state == 0) {
304                 wl_input_device_end_pointer_grab(device, time);
305                 free(grab);
306         }
307 }
308
309 static const struct wl_pointer_grab_interface resize_grab_interface = {
310         noop_grab_focus,
311         resize_grab_motion,
312         resize_grab_button,
313 };
314
315 static int
316 weston_surface_resize(struct shell_surface *shsurf,
317                     struct weston_input_device *wd,
318                     uint32_t time, uint32_t edges)
319 {
320         struct weston_resize_grab *resize;
321
322         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
323                 return 0;
324
325         if (edges == 0 || edges > 15 ||
326             (edges & 3) == 3 || (edges & 12) == 12)
327                 return 0;
328
329         resize = malloc(sizeof *resize);
330         if (!resize)
331                 return -1;
332
333         resize->grab.interface = &resize_grab_interface;
334         resize->edges = edges;
335         resize->width = shsurf->surface->geometry.width;
336         resize->height = shsurf->surface->geometry.height;
337         resize->shsurf = shsurf;
338
339         wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
340
341         wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
342
343         return 0;
344 }
345
346 static void
347 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
348                      struct wl_resource *input_resource, uint32_t time,
349                      uint32_t edges)
350 {
351         struct weston_input_device *wd = input_resource->data;
352         struct shell_surface *shsurf = resource->data;
353
354         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
355                 return;
356
357         if (wd->input_device.button_count == 0 ||
358             wd->input_device.grab_time != time ||
359             wd->input_device.pointer_focus != &shsurf->surface->surface)
360                 return;
361
362         if (weston_surface_resize(shsurf, wd, time, edges) < 0)
363                 wl_resource_post_no_memory(resource);
364 }
365
366 static struct weston_output *
367 get_default_output(struct weston_compositor *compositor)
368 {
369         return container_of(compositor->output_list.next,
370                             struct weston_output, link);
371 }
372
373 static void
374 shell_unset_fullscreen(struct shell_surface *shsurf)
375 {
376         /* undo all fullscreen things here */
377         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
378         shsurf->fullscreen.framerate = 0;
379         wl_list_remove(&shsurf->fullscreen.transform.link);
380         wl_list_init(&shsurf->fullscreen.transform.link);
381         weston_surface_destroy(shsurf->fullscreen.black_surface);
382         shsurf->fullscreen.black_surface = NULL;
383         shsurf->fullscreen_output = NULL;
384         shsurf->force_configure = 1;
385         weston_surface_set_position(shsurf->surface,
386                                     shsurf->saved_x, shsurf->saved_y);
387 }
388
389 static int
390 reset_shell_surface_type(struct shell_surface *surface)
391 {
392         switch (surface->type) {
393         case SHELL_SURFACE_FULLSCREEN:
394                 shell_unset_fullscreen(surface);
395                 break;
396         case SHELL_SURFACE_MAXIMIZED:
397                 surface->output = get_default_output(surface->surface->compositor);
398                 weston_surface_set_position(surface->surface,
399                                             surface->saved_x,
400                                             surface->saved_y);
401                 break;
402         case SHELL_SURFACE_PANEL:
403         case SHELL_SURFACE_BACKGROUND:
404                 wl_list_remove(&surface->link);
405                 wl_list_init(&surface->link);
406                 break;
407         case SHELL_SURFACE_SCREENSAVER:
408         case SHELL_SURFACE_LOCK:
409                 wl_resource_post_error(&surface->resource,
410                                        WL_DISPLAY_ERROR_INVALID_METHOD,
411                                        "cannot reassign surface type");
412                 return -1;
413         case SHELL_SURFACE_NONE:
414         case SHELL_SURFACE_TOPLEVEL:
415         case SHELL_SURFACE_TRANSIENT:
416         case SHELL_SURFACE_POPUP:
417                 break;
418         }
419
420         surface->type = SHELL_SURFACE_NONE;
421         return 0;
422 }
423
424 static void
425 shell_surface_set_toplevel(struct wl_client *client,
426                            struct wl_resource *resource)
427
428 {
429         struct shell_surface *surface = resource->data;
430
431         if (reset_shell_surface_type(surface))
432                 return;
433
434         surface->type = SHELL_SURFACE_TOPLEVEL;
435 }
436
437 static void
438 shell_surface_set_transient(struct wl_client *client,
439                             struct wl_resource *resource,
440                             struct wl_resource *parent_resource,
441                             int x, int y, uint32_t flags)
442 {
443         struct shell_surface *shsurf = resource->data;
444         struct weston_surface *es = shsurf->surface;
445         struct shell_surface *pshsurf = parent_resource->data;
446         struct weston_surface *pes = pshsurf->surface;
447
448         if (reset_shell_surface_type(shsurf))
449                 return;
450
451         /* assign to parents output */
452         shsurf->output = pes->output;
453         weston_surface_set_position(es, pes->geometry.x + x,
454                                         pes->geometry.y + y);
455
456         shsurf->type = SHELL_SURFACE_TRANSIENT;
457 }
458
459 static struct wl_shell *
460 shell_surface_get_shell(struct shell_surface *shsurf)
461 {
462         struct weston_surface *es = shsurf->surface;
463         struct weston_shell *shell = es->compositor->shell;
464
465         return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
466 }
467
468 static int
469 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
470 {
471         struct shell_surface *priv;
472         int panel_height = 0;
473
474         if (!output)
475                 return 0;
476
477         wl_list_for_each(priv, &wlshell->panels, link) {
478                 if (priv->output == output) {
479                         panel_height = priv->surface->geometry.height;
480                         break;
481                 }
482         }
483         return panel_height;
484 }
485
486 static void
487 shell_surface_set_maximized(struct wl_client *client,
488                             struct wl_resource *resource,
489                             struct wl_resource *output_resource )
490 {
491         struct shell_surface *shsurf = resource->data;
492         struct weston_surface *es = shsurf->surface;
493         struct wl_shell *wlshell = NULL;
494         uint32_t edges = 0, panel_height = 0;
495
496         /* get the default output, if the client set it as NULL
497            check whether the ouput is available */
498         if (output_resource)
499                 shsurf->output = output_resource->data;
500         else
501                 shsurf->output = get_default_output(es->compositor);
502
503         if (reset_shell_surface_type(shsurf))
504                 return;
505
506         shsurf->saved_x = es->geometry.x;
507         shsurf->saved_y = es->geometry.y;
508         shsurf->saved_position_valid = true;
509
510         wlshell = shell_surface_get_shell(shsurf);
511         panel_height = get_output_panel_height(wlshell, es->output);
512         edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
513
514         wl_shell_surface_send_configure(&shsurf->resource,
515                                         weston_compositor_get_time(), edges,
516                                         es->output->current->width,
517                                         es->output->current->height - panel_height);
518
519         shsurf->type = SHELL_SURFACE_MAXIMIZED;
520 }
521
522 static struct weston_surface *
523 create_black_surface(struct weston_compositor *ec,
524                      GLfloat x, GLfloat y, int w, int h)
525 {
526         struct weston_surface *surface = NULL;
527
528         surface = weston_surface_create(ec);
529         if (surface == NULL) {
530                 fprintf(stderr, "no memory\n");
531                 return NULL;
532         }
533
534         weston_surface_configure(surface, x, y, w, h);
535         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
536         return surface;
537 }
538
539 /* Create black surface and append it to the associated fullscreen surface.
540  * Handle size dismatch and positioning according to the method. */
541 static void
542 shell_configure_fullscreen(struct shell_surface *shsurf)
543 {
544         struct weston_output *output = shsurf->fullscreen_output;
545         struct weston_surface *surface = shsurf->surface;
546         struct weston_matrix *matrix;
547         float scale;
548
549         center_on_output(surface, output);
550
551         if (!shsurf->fullscreen.black_surface)
552                 shsurf->fullscreen.black_surface =
553                         create_black_surface(surface->compositor,
554                                              output->x, output->y,
555                                              output->current->width,
556                                              output->current->height);
557
558         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
559         wl_list_insert(&surface->layer_link,
560                        &shsurf->fullscreen.black_surface->layer_link);
561         shsurf->fullscreen.black_surface->output = output;
562
563         switch (shsurf->fullscreen.type) {
564         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
565                 break;
566         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
567                 matrix = &shsurf->fullscreen.transform.matrix;
568                 weston_matrix_init(matrix);
569                 scale = (float)output->current->width/(float)surface->geometry.width;
570                 weston_matrix_scale(matrix, scale, scale, 1);
571                 wl_list_remove(&shsurf->fullscreen.transform.link);
572                 wl_list_insert(surface->geometry.transformation_list.prev,
573                                &shsurf->fullscreen.transform.link);
574                 weston_surface_set_position(surface, output->x, output->y);
575                 break;
576         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
577                 break;
578         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
579                 break;
580         default:
581                 break;
582         }
583 }
584
585 /* make the fullscreen and black surface at the top */
586 static void
587 shell_stack_fullscreen(struct shell_surface *shsurf)
588 {
589         struct weston_surface *surface = shsurf->surface;
590         struct wl_shell *shell = shell_surface_get_shell(shsurf);
591
592         wl_list_remove(&surface->layer_link);
593         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
594
595         wl_list_insert(&shell->fullscreen_layer.surface_list,
596                        &surface->layer_link);
597         wl_list_insert(&surface->layer_link,
598                        &shsurf->fullscreen.black_surface->layer_link);
599
600         weston_surface_damage(surface);
601         weston_surface_damage(shsurf->fullscreen.black_surface);
602 }
603
604 static void
605 shell_map_fullscreen(struct shell_surface *shsurf)
606 {
607         shell_configure_fullscreen(shsurf);
608         shell_stack_fullscreen(shsurf);
609 }
610
611 static void
612 shell_surface_set_fullscreen(struct wl_client *client,
613                              struct wl_resource *resource,
614                              uint32_t method,
615                              uint32_t framerate,
616                              struct wl_resource *output_resource)
617 {
618         struct shell_surface *shsurf = resource->data;
619         struct weston_surface *es = shsurf->surface;
620
621         if (output_resource)
622                 shsurf->output = output_resource->data;
623         else
624                 shsurf->output = get_default_output(es->compositor);
625
626         if (reset_shell_surface_type(shsurf))
627                 return;
628
629         shsurf->fullscreen_output = shsurf->output;
630         shsurf->fullscreen.type = method;
631         shsurf->fullscreen.framerate = framerate;
632         shsurf->type = SHELL_SURFACE_FULLSCREEN;
633
634         shsurf->saved_x = es->geometry.x;
635         shsurf->saved_y = es->geometry.y;
636         shsurf->saved_position_valid = true;
637
638         if (weston_surface_is_mapped(es))
639                 shsurf->force_configure = 1;
640
641         wl_shell_surface_send_configure(&shsurf->resource,
642                                         weston_compositor_get_time(), 0,
643                                         shsurf->output->current->width,
644                                         shsurf->output->current->height);
645 }
646
647 static void
648 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
649                  struct wl_surface *surface, int32_t x, int32_t y)
650 {
651         struct wl_input_device *device = grab->input_device;
652         struct shell_surface *priv =
653                 container_of(grab, struct shell_surface, popup.grab);
654         struct wl_client *client = priv->surface->surface.resource.client;
655
656         if (surface && surface->resource.client == client) {
657                 wl_input_device_set_pointer_focus(device, surface, time, x, y);
658                 grab->focus = surface;
659         } else {
660                 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
661                 grab->focus = NULL;
662         }
663 }
664
665 static void
666 popup_grab_motion(struct wl_pointer_grab *grab,
667                   uint32_t time, int32_t sx, int32_t sy)
668 {
669         struct wl_resource *resource;
670
671         resource = grab->input_device->pointer_focus_resource;
672         if (resource)
673                 wl_input_device_send_motion(resource, time, sx, sy);
674 }
675
676 static void
677 popup_grab_button(struct wl_pointer_grab *grab,
678                   uint32_t time, int32_t button, int32_t state)
679 {
680         struct wl_resource *resource;
681         struct shell_surface *shsurf =
682                 container_of(grab, struct shell_surface, popup.grab);
683
684         resource = grab->input_device->pointer_focus_resource;
685         if (resource) {
686                 wl_input_device_send_button(resource, time, button, state);
687         } else if (state == 0 &&
688                    (shsurf->popup.initial_up ||
689                     time - shsurf->popup.time > 500)) {
690                 wl_shell_surface_send_popup_done(&shsurf->resource);
691                 wl_input_device_end_pointer_grab(grab->input_device, time);
692                 shsurf->popup.grab.input_device = NULL;
693         }
694
695         if (state == 0)
696                 shsurf->popup.initial_up = 1;
697 }
698
699 static const struct wl_pointer_grab_interface popup_grab_interface = {
700         popup_grab_focus,
701         popup_grab_motion,
702         popup_grab_button,
703 };
704
705 static void
706 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
707 {
708         struct wl_input_device *device;
709         struct weston_surface *es = shsurf->surface;
710         struct weston_surface *parent = shsurf->parent->surface;
711
712         es->output = parent->output;
713
714         shsurf->popup.grab.interface = &popup_grab_interface;
715         device = es->compositor->input_device;
716
717         weston_surface_update_transform(parent);
718         if (parent->transform.enabled) {
719                 shsurf->popup.parent_transform.matrix =
720                         parent->transform.matrix;
721         } else {
722                 /* construct x, y translation matrix */
723                 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
724                 shsurf->popup.parent_transform.matrix.d[12] =
725                         parent->geometry.x;
726                 shsurf->popup.parent_transform.matrix.d[13] =
727                         parent->geometry.y;
728         }
729         wl_list_insert(es->geometry.transformation_list.prev,
730                        &shsurf->popup.parent_transform.link);
731         weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
732
733         shsurf->popup.grab.input_device = device;
734         shsurf->popup.time = device->grab_time;
735         shsurf->popup.initial_up = 0;
736
737         wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
738                                    &shsurf->popup.grab, shsurf->popup.time);
739 }
740
741 static void
742 shell_surface_set_popup(struct wl_client *client,
743                         struct wl_resource *resource,
744                         struct wl_resource *input_device_resource,
745                         uint32_t time,
746                         struct wl_resource *parent_resource,
747                         int32_t x, int32_t y, uint32_t flags)
748 {
749         struct shell_surface *shsurf = resource->data;
750
751         shsurf->type = SHELL_SURFACE_POPUP;
752         shsurf->parent = parent_resource->data;
753         shsurf->popup.x = x;
754         shsurf->popup.y = y;
755 }
756
757 static const struct wl_shell_surface_interface shell_surface_implementation = {
758         shell_surface_move,
759         shell_surface_resize,
760         shell_surface_set_toplevel,
761         shell_surface_set_transient,
762         shell_surface_set_fullscreen,
763         shell_surface_set_popup,
764         shell_surface_set_maximized
765 };
766
767 static void
768 destroy_shell_surface(struct wl_resource *resource)
769 {
770         struct shell_surface *shsurf = resource->data;
771
772         if (shsurf->popup.grab.input_device)
773                 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
774
775         /* in case cleaning up a dead client destroys shell_surface first */
776         if (shsurf->surface) {
777                 wl_list_remove(&shsurf->surface_destroy_listener.link);
778                 shsurf->surface->configure = NULL;
779         }
780
781         if (shsurf->fullscreen.black_surface)
782                 weston_surface_destroy(shsurf->fullscreen.black_surface);
783
784         wl_list_remove(&shsurf->link);
785         free(shsurf);
786 }
787
788 static void
789 shell_handle_surface_destroy(struct wl_listener *listener,
790                              struct wl_resource *resource, uint32_t time)
791 {
792         struct shell_surface *shsurf = container_of(listener,
793                                                     struct shell_surface,
794                                                     surface_destroy_listener);
795
796         shsurf->surface = NULL;
797         wl_resource_destroy(&shsurf->resource, time);
798 }
799
800 static struct shell_surface *
801 get_shell_surface(struct weston_surface *surface)
802 {
803         struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
804         struct wl_listener *listener;
805
806         /* search the destroy listener list for our callback */
807         wl_list_for_each(listener, lst, link) {
808                 if (listener->func == shell_handle_surface_destroy) {
809                         return container_of(listener, struct shell_surface,
810                                             surface_destroy_listener);
811                 }
812         }
813
814         return NULL;
815 }
816
817 static void
818 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
819
820 static void
821 shell_get_shell_surface(struct wl_client *client,
822                         struct wl_resource *resource,
823                         uint32_t id,
824                         struct wl_resource *surface_resource)
825 {
826         struct weston_surface *surface = surface_resource->data;
827         struct shell_surface *shsurf;
828
829         if (get_shell_surface(surface)) {
830                 wl_resource_post_error(surface_resource,
831                         WL_DISPLAY_ERROR_INVALID_OBJECT,
832                         "wl_shell::get_shell_surface already requested");
833                 return;
834         }
835
836         if (surface->configure) {
837                 wl_resource_post_error(surface_resource,
838                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
839                                        "surface->configure already set");
840                 return;
841         }
842
843         shsurf = calloc(1, sizeof *shsurf);
844         if (!shsurf) {
845                 wl_resource_post_no_memory(resource);
846                 return;
847         }
848
849         surface->configure = shell_surface_configure;
850
851         shsurf->resource.destroy = destroy_shell_surface;
852         shsurf->resource.object.id = id;
853         shsurf->resource.object.interface = &wl_shell_surface_interface;
854         shsurf->resource.object.implementation =
855                 (void (**)(void)) &shell_surface_implementation;
856         shsurf->resource.data = shsurf;
857
858         shsurf->saved_position_valid = false;
859         shsurf->surface = surface;
860         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
861         shsurf->fullscreen.framerate = 0;
862         shsurf->fullscreen.black_surface = NULL;
863         wl_list_init(&shsurf->fullscreen.transform.link);
864
865         shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
866         wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
867                        &shsurf->surface_destroy_listener.link);
868
869         /* init link so its safe to always remove it in destroy_shell_surface */
870         wl_list_init(&shsurf->link);
871
872         /* empty when not in use */
873         wl_list_init(&shsurf->rotation.transform.link);
874         weston_matrix_init(&shsurf->rotation.rotation);
875
876         shsurf->type = SHELL_SURFACE_NONE;
877
878         wl_client_add_resource(client, &shsurf->resource);
879 }
880
881 static const struct wl_shell_interface shell_implementation = {
882         shell_get_shell_surface
883 };
884
885 static void
886 handle_screensaver_sigchld(struct weston_process *proc, int status)
887 {
888         proc->pid = 0;
889 }
890
891 static void
892 launch_screensaver(struct wl_shell *shell)
893 {
894         if (shell->screensaver.binding)
895                 return;
896
897         if (!shell->screensaver.path)
898                 return;
899
900         if (shell->screensaver.process.pid != 0) {
901                 fprintf(stderr, "old screensaver still running\n");
902                 return;
903         }
904
905         weston_client_launch(shell->compositor,
906                            &shell->screensaver.process,
907                            shell->screensaver.path,
908                            handle_screensaver_sigchld);
909 }
910
911 static void
912 terminate_screensaver(struct wl_shell *shell)
913 {
914         if (shell->screensaver.process.pid == 0)
915                 return;
916
917         kill(shell->screensaver.process.pid, SIGTERM);
918 }
919
920 static void
921 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
922 {
923         struct wl_list *list;
924
925         if (shell->lock_surface)
926                 list = &shell->lock_surface->surface->layer_link;
927         else
928                 list = &shell->lock_layer.surface_list;
929
930         wl_list_remove(&surface->surface->layer_link);
931         wl_list_insert(list, &surface->surface->layer_link);
932         surface->surface->output = surface->output;
933         weston_surface_damage(surface->surface);
934 }
935
936 static void
937 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
938 {
939         wl_list_remove(&surface->surface->layer_link);
940         wl_list_init(&surface->surface->layer_link);
941         surface->surface->output = NULL;
942 }
943
944 static void
945 desktop_shell_set_background(struct wl_client *client,
946                              struct wl_resource *resource,
947                              struct wl_resource *output_resource,
948                              struct wl_resource *surface_resource)
949 {
950         struct wl_shell *shell = resource->data;
951         struct shell_surface *shsurf = surface_resource->data;
952         struct weston_surface *surface = shsurf->surface;
953         struct shell_surface *priv;
954
955         if (reset_shell_surface_type(shsurf))
956                 return;
957
958         wl_list_for_each(priv, &shell->backgrounds, link) {
959                 if (priv->output == output_resource->data) {
960                         priv->surface->output = NULL;
961                         wl_list_remove(&priv->surface->layer_link);
962                         wl_list_remove(&priv->link);
963                         break;
964                 }
965         }
966
967         shsurf->type = SHELL_SURFACE_BACKGROUND;
968         shsurf->output = output_resource->data;
969
970         wl_list_insert(&shell->backgrounds, &shsurf->link);
971
972         weston_surface_set_position(surface, shsurf->output->x,
973                                     shsurf->output->y);
974
975         desktop_shell_send_configure(resource,
976                                      weston_compositor_get_time(), 0,
977                                      surface_resource,
978                                      shsurf->output->current->width,
979                                      shsurf->output->current->height);
980 }
981
982 static void
983 desktop_shell_set_panel(struct wl_client *client,
984                         struct wl_resource *resource,
985                         struct wl_resource *output_resource,
986                         struct wl_resource *surface_resource)
987 {
988         struct wl_shell *shell = resource->data;
989         struct shell_surface *shsurf = surface_resource->data;
990         struct weston_surface *surface = shsurf->surface;
991         struct shell_surface *priv;
992
993         if (reset_shell_surface_type(shsurf))
994                 return;
995
996         wl_list_for_each(priv, &shell->panels, link) {
997                 if (priv->output == output_resource->data) {
998                         priv->surface->output = NULL;
999                         wl_list_remove(&priv->surface->layer_link);
1000                         wl_list_remove(&priv->link);
1001                         break;
1002                 }
1003         }
1004
1005         shsurf->type = SHELL_SURFACE_PANEL;
1006         shsurf->output = output_resource->data;
1007
1008         wl_list_insert(&shell->panels, &shsurf->link);
1009
1010         weston_surface_set_position(surface, shsurf->output->x,
1011                                     shsurf->output->y);
1012
1013         desktop_shell_send_configure(resource,
1014                                      weston_compositor_get_time(), 0,
1015                                      surface_resource,
1016                                      shsurf->output->current->width,
1017                                      shsurf->output->current->height);
1018 }
1019
1020 static void
1021 handle_lock_surface_destroy(struct wl_listener *listener,
1022                             struct wl_resource *resource, uint32_t time)
1023 {
1024         struct wl_shell *shell =
1025                 container_of(listener, struct wl_shell, lock_surface_listener);
1026
1027         fprintf(stderr, "lock surface gone\n");
1028         shell->lock_surface = NULL;
1029 }
1030
1031 static void
1032 desktop_shell_set_lock_surface(struct wl_client *client,
1033                                struct wl_resource *resource,
1034                                struct wl_resource *surface_resource)
1035 {
1036         struct wl_shell *shell = resource->data;
1037         struct shell_surface *surface = surface_resource->data;
1038
1039         if (reset_shell_surface_type(surface))
1040                 return;
1041
1042         shell->prepare_event_sent = false;
1043
1044         if (!shell->locked)
1045                 return;
1046
1047         shell->lock_surface = surface;
1048
1049         shell->lock_surface_listener.func = handle_lock_surface_destroy;
1050         wl_list_insert(&surface_resource->destroy_listener_list,
1051                        &shell->lock_surface_listener.link);
1052
1053         shell->lock_surface->type = SHELL_SURFACE_LOCK;
1054 }
1055
1056 static void
1057 resume_desktop(struct wl_shell *shell)
1058 {
1059         struct shell_surface *tmp;
1060
1061         wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1062                 hide_screensaver(shell, tmp);
1063
1064         terminate_screensaver(shell);
1065
1066         wl_list_remove(&shell->lock_layer.link);
1067         wl_list_insert(&shell->compositor->cursor_layer.link,
1068                        &shell->fullscreen_layer.link);
1069         wl_list_insert(&shell->fullscreen_layer.link,
1070                        &shell->panel_layer.link);
1071         wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1072
1073         shell->locked = false;
1074         shell->compositor->idle_time = shell->compositor->option_idle_time;
1075         weston_compositor_wake(shell->compositor);
1076         weston_compositor_damage_all(shell->compositor);
1077 }
1078
1079 static void
1080 desktop_shell_unlock(struct wl_client *client,
1081                      struct wl_resource *resource)
1082 {
1083         struct wl_shell *shell = resource->data;
1084
1085         shell->prepare_event_sent = false;
1086
1087         if (shell->locked)
1088                 resume_desktop(shell);
1089 }
1090
1091 static const struct desktop_shell_interface desktop_shell_implementation = {
1092         desktop_shell_set_background,
1093         desktop_shell_set_panel,
1094         desktop_shell_set_lock_surface,
1095         desktop_shell_unlock
1096 };
1097
1098 static enum shell_surface_type
1099 get_shell_surface_type(struct weston_surface *surface)
1100 {
1101         struct shell_surface *shsurf;
1102
1103         shsurf = get_shell_surface(surface);
1104         if (!shsurf)
1105                 return SHELL_SURFACE_NONE;
1106         return shsurf->type;
1107 }
1108
1109 static void
1110 move_binding(struct wl_input_device *device, uint32_t time,
1111              uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1112 {
1113         struct weston_surface *surface =
1114                 (struct weston_surface *) device->pointer_focus;
1115
1116         if (surface == NULL)
1117                 return;
1118
1119         switch (get_shell_surface_type(surface)) {
1120                 case SHELL_SURFACE_PANEL:
1121                 case SHELL_SURFACE_BACKGROUND:
1122                 case SHELL_SURFACE_FULLSCREEN:
1123                 case SHELL_SURFACE_SCREENSAVER:
1124                         return;
1125                 default:
1126                         break;
1127         }
1128
1129         weston_surface_move(surface, (struct weston_input_device *) device, time);
1130 }
1131
1132 static void
1133 resize_binding(struct wl_input_device *device, uint32_t time,
1134                uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1135 {
1136         struct weston_surface *surface =
1137                 (struct weston_surface *) device->pointer_focus;
1138         uint32_t edges = 0;
1139         int32_t x, y;
1140         struct shell_surface *shsurf;
1141
1142         if (surface == NULL)
1143                 return;
1144
1145         shsurf = get_shell_surface(surface);
1146         if (!shsurf)
1147                 return;
1148
1149         switch (shsurf->type) {
1150                 case SHELL_SURFACE_PANEL:
1151                 case SHELL_SURFACE_BACKGROUND:
1152                 case SHELL_SURFACE_FULLSCREEN:
1153                 case SHELL_SURFACE_SCREENSAVER:
1154                         return;
1155                 default:
1156                         break;
1157         }
1158
1159         weston_surface_from_global(surface,
1160                                    device->grab_x, device->grab_y, &x, &y);
1161
1162         if (x < surface->geometry.width / 3)
1163                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1164         else if (x < 2 * surface->geometry.width / 3)
1165                 edges |= 0;
1166         else
1167                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1168
1169         if (y < surface->geometry.height / 3)
1170                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1171         else if (y < 2 * surface->geometry.height / 3)
1172                 edges |= 0;
1173         else
1174                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1175
1176         weston_surface_resize(shsurf, (struct weston_input_device *) device,
1177                             time, edges);
1178 }
1179
1180 static void
1181 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1182                uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1183 {
1184         int step = 15;
1185         struct shell_surface *shsurf;
1186         struct weston_surface *surface =
1187                 (struct weston_surface *) device->pointer_focus;
1188
1189         if (surface == NULL)
1190                 return;
1191
1192         shsurf = get_shell_surface(surface);
1193         if (!shsurf)
1194                 return;
1195
1196         switch (shsurf->type) {
1197                 case SHELL_SURFACE_BACKGROUND:
1198                 case SHELL_SURFACE_SCREENSAVER:
1199                         return;
1200                 default:
1201                         break;
1202         }
1203
1204         surface->alpha += value * step;
1205
1206         if (surface->alpha > 255)
1207                 surface->alpha = 255;
1208         if (surface->alpha < step)
1209                 surface->alpha = step;
1210
1211         surface->geometry.dirty = 1;
1212         weston_surface_damage(surface);
1213 }
1214
1215 static void
1216 zoom_binding(struct wl_input_device *device, uint32_t time,
1217                uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1218 {
1219         struct weston_input_device *wd = (struct weston_input_device *) device;
1220         struct weston_compositor *compositor = wd->compositor;
1221         struct weston_output *output;
1222
1223         wl_list_for_each(output, &compositor->output_list, link) {
1224                 if (pixman_region32_contains_point(&output->region,
1225                                                 device->x, device->y, NULL)) {
1226                         output->zoom.active = 1;
1227                         output->zoom.level += output->zoom.increment * -value;
1228
1229                         if (output->zoom.level >= 1.0) {
1230                                 output->zoom.active = 0;
1231                                 output->zoom.level = 1.0;
1232                         }
1233
1234                         if (output->zoom.level < output->zoom.increment)
1235                                 output->zoom.level = output->zoom.increment;
1236
1237                         weston_output_update_zoom(output, device->x, device->y);
1238                 }
1239         }
1240 }
1241
1242 static void
1243 terminate_binding(struct wl_input_device *device, uint32_t time,
1244                   uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1245 {
1246         struct weston_compositor *compositor = data;
1247
1248         if (state)
1249                 wl_display_terminate(compositor->wl_display);
1250 }
1251
1252 static void
1253 rotate_grab_motion(struct wl_pointer_grab *grab,
1254                  uint32_t time, int32_t x, int32_t y)
1255 {
1256         struct rotate_grab *rotate =
1257                 container_of(grab, struct rotate_grab, grab);
1258         struct wl_input_device *device = grab->input_device;
1259         struct shell_surface *surface = rotate->surface;
1260         struct weston_surface *base_surface = surface->surface;
1261         GLfloat cx = 0.5f * base_surface->geometry.width;
1262         GLfloat cy = 0.5f * base_surface->geometry.height;
1263         GLfloat dx, dy, cposx, cposy, dposx, dposy;
1264         GLfloat r;
1265
1266         dx = device->x - rotate->center.x;
1267         dy = device->y - rotate->center.y;
1268         r = sqrtf(dx * dx + dy * dy);
1269
1270         wl_list_remove(&surface->rotation.transform.link);
1271         surface->surface->geometry.dirty = 1;
1272
1273         if (r > 20.0f) {
1274                 struct weston_matrix *matrix =
1275                         &surface->rotation.transform.matrix;
1276
1277                 weston_matrix_init(&rotate->rotation);
1278                 rotate->rotation.d[0] = dx / r;
1279                 rotate->rotation.d[4] = -dy / r;
1280                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1281                 rotate->rotation.d[5] = rotate->rotation.d[0];
1282
1283                 weston_matrix_init(matrix);
1284                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1285                 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1286                 weston_matrix_multiply(matrix, &rotate->rotation);
1287                 weston_matrix_translate(matrix, cx, cy, 0.0f);
1288
1289                 wl_list_insert(
1290                         &surface->surface->geometry.transformation_list,
1291                         &surface->rotation.transform.link);
1292         } else {
1293                 wl_list_init(&surface->rotation.transform.link);
1294                 weston_matrix_init(&surface->rotation.rotation);
1295                 weston_matrix_init(&rotate->rotation);
1296         }
1297
1298         /* We need to adjust the position of the surface
1299          * in case it was resized in a rotated state before */
1300         cposx = base_surface->geometry.x + cx;
1301         cposy = base_surface->geometry.y + cy;
1302         dposx = rotate->center.x - cposx;
1303         dposy = rotate->center.y - cposy;
1304         if (dposx != 0.0f || dposy != 0.0f) {
1305                 weston_surface_set_position(base_surface,
1306                                 base_surface->geometry.x + dposx,
1307                                 base_surface->geometry.y + dposy);
1308         }
1309
1310         /* Repaint implies weston_surface_update_transform(), which
1311          * lazily applies the damage due to rotation update.
1312          */
1313         weston_compositor_schedule_repaint(surface->surface->compositor);
1314 }
1315
1316 static void
1317 rotate_grab_button(struct wl_pointer_grab *grab,
1318                  uint32_t time, int32_t button, int32_t state)
1319 {
1320         struct rotate_grab *rotate =
1321                 container_of(grab, struct rotate_grab, grab);
1322         struct wl_input_device *device = grab->input_device;
1323         struct shell_surface *surface = rotate->surface;
1324
1325         if (device->button_count == 0 && state == 0) {
1326                 weston_matrix_multiply(&surface->rotation.rotation,
1327                                        &rotate->rotation);
1328                 wl_input_device_end_pointer_grab(device, time);
1329                 free(rotate);
1330         }
1331 }
1332
1333 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1334         noop_grab_focus,
1335         rotate_grab_motion,
1336         rotate_grab_button,
1337 };
1338
1339 static void
1340 rotate_binding(struct wl_input_device *device, uint32_t time,
1341                uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1342 {
1343         struct weston_surface *base_surface =
1344                 (struct weston_surface *) device->pointer_focus;
1345         struct shell_surface *surface;
1346         struct rotate_grab *rotate;
1347         GLfloat dx, dy;
1348         GLfloat r;
1349
1350         if (base_surface == NULL)
1351                 return;
1352
1353         surface = get_shell_surface(base_surface);
1354         if (!surface)
1355                 return;
1356
1357         switch (surface->type) {
1358                 case SHELL_SURFACE_PANEL:
1359                 case SHELL_SURFACE_BACKGROUND:
1360                 case SHELL_SURFACE_FULLSCREEN:
1361                 case SHELL_SURFACE_SCREENSAVER:
1362                         return;
1363                 default:
1364                         break;
1365         }
1366
1367         rotate = malloc(sizeof *rotate);
1368         if (!rotate)
1369                 return;
1370
1371         rotate->grab.interface = &rotate_grab_interface;
1372         rotate->surface = surface;
1373
1374         weston_surface_to_global(surface->surface,
1375                                  surface->surface->geometry.width / 2,
1376                                  surface->surface->geometry.height / 2,
1377                                  &rotate->center.x, &rotate->center.y);
1378
1379         wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1380
1381         dx = device->x - rotate->center.x;
1382         dy = device->y - rotate->center.y;
1383         r = sqrtf(dx * dx + dy * dy);
1384         if (r > 20.0f) {
1385                 struct weston_matrix inverse;
1386
1387                 weston_matrix_init(&inverse);
1388                 inverse.d[0] = dx / r;
1389                 inverse.d[4] = dy / r;
1390                 inverse.d[1] = -inverse.d[4];
1391                 inverse.d[5] = inverse.d[0];
1392                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1393
1394                 weston_matrix_init(&rotate->rotation);
1395                 rotate->rotation.d[0] = dx / r;
1396                 rotate->rotation.d[4] = -dy / r;
1397                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1398                 rotate->rotation.d[5] = rotate->rotation.d[0];
1399         } else {
1400                 weston_matrix_init(&surface->rotation.rotation);
1401                 weston_matrix_init(&rotate->rotation);
1402         }
1403
1404         wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1405 }
1406
1407 static void
1408 activate(struct weston_shell *base, struct weston_surface *es,
1409          struct weston_input_device *device, uint32_t time)
1410 {
1411         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1412         struct weston_compositor *compositor = shell->compositor;
1413
1414         weston_surface_activate(es, device, time);
1415
1416         if (compositor->wxs)
1417                 weston_xserver_surface_activate(es);
1418
1419         switch (get_shell_surface_type(es)) {
1420         case SHELL_SURFACE_BACKGROUND:
1421         case SHELL_SURFACE_PANEL:
1422         case SHELL_SURFACE_LOCK:
1423                 break;
1424
1425         case SHELL_SURFACE_SCREENSAVER:
1426                 /* always below lock surface */
1427                 if (shell->lock_surface)
1428                         weston_surface_restack(es,
1429                                                &shell->lock_surface->surface->layer_link);
1430                 break;
1431         case SHELL_SURFACE_FULLSCREEN:
1432                 /* should on top of panels */
1433                 break;
1434         default:
1435                 weston_surface_restack(es,
1436                                        &shell->toplevel_layer.surface_list);
1437                 break;
1438         }
1439 }
1440
1441 static void
1442 click_to_activate_binding(struct wl_input_device *device,
1443                           uint32_t time, uint32_t key,
1444                           uint32_t button, uint32_t axis, int32_t state, void *data)
1445 {
1446         struct weston_input_device *wd = (struct weston_input_device *) device;
1447         struct weston_compositor *compositor = data;
1448         struct weston_surface *focus;
1449         struct weston_surface *upper;
1450
1451         focus = (struct weston_surface *) device->pointer_focus;
1452         if (!focus)
1453                 return;
1454
1455         upper = container_of(focus->link.prev, struct weston_surface, link);
1456         if (focus->link.prev != &compositor->surface_list &&
1457             get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1458                 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1459                 shell_stack_fullscreen(get_shell_surface(upper));
1460                 focus = upper;
1461         }
1462
1463         if (state && device->pointer_grab == &device->default_pointer_grab)
1464                 activate(compositor->shell, focus, wd, time);
1465 }
1466
1467 static void
1468 lock(struct weston_shell *base)
1469 {
1470         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1471         struct weston_input_device *device;
1472         struct shell_surface *shsurf;
1473         struct weston_output *output;
1474         uint32_t time;
1475
1476         if (shell->locked) {
1477                 wl_list_for_each(output, &shell->compositor->output_list, link)
1478                         /* TODO: find a way to jump to other DPMS levels */
1479                         if (output->set_dpms)
1480                                 output->set_dpms(output, WESTON_DPMS_STANDBY);
1481                 return;
1482         }
1483
1484         shell->locked = true;
1485
1486         /* Hide all surfaces by removing the fullscreen, panel and
1487          * toplevel layers.  This way nothing else can show or receive
1488          * input events while we are locked. */
1489
1490         wl_list_remove(&shell->panel_layer.link);
1491         wl_list_remove(&shell->toplevel_layer.link);
1492         wl_list_remove(&shell->fullscreen_layer.link);
1493         wl_list_insert(&shell->compositor->cursor_layer.link,
1494                        &shell->lock_layer.link);
1495
1496         launch_screensaver(shell);
1497
1498         wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1499                 show_screensaver(shell, shsurf);
1500
1501         if (!wl_list_empty(&shell->screensaver.surfaces)) {
1502                 shell->compositor->idle_time = shell->screensaver.duration;
1503                 weston_compositor_wake(shell->compositor);
1504                 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1505         }
1506
1507         /* reset pointer foci */
1508         weston_compositor_schedule_repaint(shell->compositor);
1509
1510         /* reset keyboard foci */
1511         time = weston_compositor_get_time();
1512         wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1513                 wl_input_device_set_keyboard_focus(&device->input_device,
1514                                                    NULL, time);
1515         }
1516
1517         /* TODO: disable bindings that should not work while locked. */
1518
1519         /* All this must be undone in resume_desktop(). */
1520 }
1521
1522 static void
1523 unlock(struct weston_shell *base)
1524 {
1525         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1526
1527         if (!shell->locked || shell->lock_surface) {
1528                 weston_compositor_wake(shell->compositor);
1529                 return;
1530         }
1531
1532         /* If desktop-shell client has gone away, unlock immediately. */
1533         if (!shell->child.desktop_shell) {
1534                 resume_desktop(shell);
1535                 return;
1536         }
1537
1538         if (shell->prepare_event_sent)
1539                 return;
1540
1541         desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1542         shell->prepare_event_sent = true;
1543 }
1544
1545 static void
1546 center_on_output(struct weston_surface *surface, struct weston_output *output)
1547 {
1548         struct weston_mode *mode = output->current;
1549         GLfloat x = (mode->width - surface->geometry.width) / 2;
1550         GLfloat y = (mode->height - surface->geometry.height) / 2;
1551
1552         weston_surface_set_position(surface, output->x + x, output->y + y);
1553 }
1554
1555 static void
1556 map(struct weston_shell *base, struct weston_surface *surface,
1557     int32_t width, int32_t height, int32_t sx, int32_t sy)
1558 {
1559         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1560         struct weston_compositor *compositor = shell->compositor;
1561         struct shell_surface *shsurf;
1562         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1563         struct weston_surface *parent;
1564         int panel_height = 0;
1565
1566         shsurf = get_shell_surface(surface);
1567         if (shsurf)
1568                 surface_type = shsurf->type;
1569
1570         surface->geometry.width = width;
1571         surface->geometry.height = height;
1572         surface->geometry.dirty = 1;
1573
1574         /* initial positioning, see also configure() */
1575         switch (surface_type) {
1576         case SHELL_SURFACE_TOPLEVEL:
1577                 weston_surface_set_position(surface, 10 + random() % 400,
1578                                             10 + random() % 400);
1579                 break;
1580         case SHELL_SURFACE_SCREENSAVER:
1581                 center_on_output(surface, shsurf->fullscreen_output);
1582                 break;
1583         case SHELL_SURFACE_FULLSCREEN:
1584                 shell_map_fullscreen(shsurf);
1585                 break;
1586         case SHELL_SURFACE_MAXIMIZED:
1587                 /* use surface configure to set the geometry */
1588                 panel_height = get_output_panel_height(shell,surface->output);
1589                 weston_surface_set_position(surface, surface->output->x,
1590                                             surface->output->y + panel_height);
1591                 break;
1592         case SHELL_SURFACE_LOCK:
1593                 center_on_output(surface, get_default_output(compositor));
1594                 break;
1595         case SHELL_SURFACE_POPUP:
1596                 shell_map_popup(shsurf, shsurf->popup.time);
1597         case SHELL_SURFACE_NONE:
1598                 weston_surface_set_position(surface,
1599                                             surface->geometry.x + sx,
1600                                             surface->geometry.y + sy);
1601                 break;
1602         default:
1603                 ;
1604         }
1605
1606         /* surface stacking order, see also activate() */
1607         switch (surface_type) {
1608         case SHELL_SURFACE_BACKGROUND:
1609                 /* background always visible, at the bottom */
1610                 wl_list_insert(&shell->background_layer.surface_list,
1611                                &surface->layer_link);
1612                 break;
1613         case SHELL_SURFACE_PANEL:
1614                 /* panel always on top, hidden while locked */
1615                 wl_list_insert(&shell->panel_layer.surface_list,
1616                                &surface->layer_link);
1617                 break;
1618         case SHELL_SURFACE_LOCK:
1619                 /* lock surface always visible, on top */
1620                 wl_list_insert(&shell->lock_layer.surface_list,
1621                                &surface->layer_link);
1622                 weston_compositor_wake(compositor);
1623                 break;
1624         case SHELL_SURFACE_SCREENSAVER:
1625                 /* If locked, show it. */
1626                 if (shell->locked) {
1627                         show_screensaver(shell, shsurf);
1628                         compositor->idle_time = shell->screensaver.duration;
1629                         weston_compositor_wake(compositor);
1630                         if (!shell->lock_surface)
1631                                 compositor->state = WESTON_COMPOSITOR_IDLE;
1632                 }
1633                 break;
1634         case SHELL_SURFACE_POPUP:
1635         case SHELL_SURFACE_TRANSIENT:
1636                 parent = shsurf->parent->surface;
1637                 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1638                 break;
1639         case SHELL_SURFACE_FULLSCREEN:
1640         case SHELL_SURFACE_NONE:
1641                 break;
1642         default:
1643                 wl_list_insert(&shell->toplevel_layer.surface_list,
1644                                &surface->layer_link); 
1645                 break;
1646         }
1647
1648         if (surface_type != SHELL_SURFACE_NONE) {
1649                 weston_surface_assign_output(surface);
1650                 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1651                         surface->output = shsurf->output;
1652         }
1653
1654         switch (surface_type) {
1655         case SHELL_SURFACE_TOPLEVEL:
1656         case SHELL_SURFACE_TRANSIENT:
1657         case SHELL_SURFACE_FULLSCREEN:
1658         case SHELL_SURFACE_MAXIMIZED:
1659                 if (!shell->locked)
1660                         activate(base, surface,
1661                                  (struct weston_input_device *)
1662                                  compositor->input_device,
1663                                  weston_compositor_get_time());
1664                 break;
1665         default:
1666                 break;
1667         }
1668
1669         if (surface_type == SHELL_SURFACE_TOPLEVEL)
1670                 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1671 }
1672
1673 static void
1674 configure(struct weston_shell *base, struct weston_surface *surface,
1675           GLfloat x, GLfloat y, int32_t width, int32_t height)
1676 {
1677         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1678         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1679         enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1680         struct shell_surface *shsurf;
1681
1682         shsurf = get_shell_surface(surface);
1683         if (shsurf)
1684                 surface_type = shsurf->type;
1685
1686         surface->geometry.x = x;
1687         surface->geometry.y = y;
1688         surface->geometry.width = width;
1689         surface->geometry.height = height;
1690         surface->geometry.dirty = 1;
1691
1692         switch (surface_type) {
1693         case SHELL_SURFACE_SCREENSAVER:
1694                 center_on_output(surface, shsurf->fullscreen_output);
1695                 break;
1696         case SHELL_SURFACE_FULLSCREEN:
1697                 shell_configure_fullscreen(shsurf);
1698                 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1699                         shell_stack_fullscreen(shsurf);
1700                 break;
1701         case SHELL_SURFACE_MAXIMIZED:
1702                 /* setting x, y and using configure to change that geometry */
1703                 surface->geometry.x = surface->output->x;
1704                 surface->geometry.y = surface->output->y +
1705                         get_output_panel_height(shell,surface->output);
1706                 break;
1707         case SHELL_SURFACE_TOPLEVEL:
1708                 break;
1709         default:
1710                 break;
1711         }
1712
1713         /* XXX: would a fullscreen surface need the same handling? */
1714         if (surface->output) {
1715                 weston_surface_assign_output(surface);
1716
1717                 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1718                         surface->output = shsurf->output;
1719                 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1720                         surface->output = shsurf->output;
1721         }
1722 }
1723
1724 static void
1725 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1726 {
1727         struct weston_shell *shell = es->compositor->shell;
1728         struct shell_surface *shsurf = get_shell_surface(es);
1729
1730         if (!weston_surface_is_mapped(es)) {
1731                 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
1732         } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
1733                    es->geometry.width != es->buffer->width ||
1734                    es->geometry.height != es->buffer->height) {
1735                 GLfloat from_x, from_y;
1736                 GLfloat to_x, to_y;
1737
1738                 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1739                 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1740                 configure(shell, es,
1741                           es->geometry.x + to_x - from_x,
1742                           es->geometry.y + to_y - from_y,
1743                           es->buffer->width, es->buffer->height);
1744                 shsurf->force_configure = 0;
1745         }
1746 }
1747
1748 static int launch_desktop_shell_process(struct wl_shell *shell);
1749
1750 static void
1751 desktop_shell_sigchld(struct weston_process *process, int status)
1752 {
1753         uint32_t time;
1754         struct wl_shell *shell =
1755                 container_of(process, struct wl_shell, child.process);
1756
1757         shell->child.process.pid = 0;
1758         shell->child.client = NULL; /* already destroyed by wayland */
1759
1760         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1761         time = weston_compositor_get_time();
1762         if (time - shell->child.deathstamp > 30000) {
1763                 shell->child.deathstamp = time;
1764                 shell->child.deathcount = 0;
1765         }
1766
1767         shell->child.deathcount++;
1768         if (shell->child.deathcount > 5) {
1769                 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1770                 return;
1771         }
1772
1773         fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1774         launch_desktop_shell_process(shell);
1775 }
1776
1777 static int
1778 launch_desktop_shell_process(struct wl_shell *shell)
1779 {
1780         const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1781
1782         shell->child.client = weston_client_launch(shell->compositor,
1783                                                  &shell->child.process,
1784                                                  shell_exe,
1785                                                  desktop_shell_sigchld);
1786
1787         if (!shell->child.client)
1788                 return -1;
1789         return 0;
1790 }
1791
1792 static void
1793 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1794 {
1795         struct wl_shell *shell = data;
1796
1797         wl_client_add_object(client, &wl_shell_interface,
1798                              &shell_implementation, id, shell);
1799 }
1800
1801 static void
1802 unbind_desktop_shell(struct wl_resource *resource)
1803 {
1804         struct wl_shell *shell = resource->data;
1805
1806         if (shell->locked)
1807                 resume_desktop(shell);
1808
1809         shell->child.desktop_shell = NULL;
1810         shell->prepare_event_sent = false;
1811         free(resource);
1812 }
1813
1814 static void
1815 bind_desktop_shell(struct wl_client *client,
1816                    void *data, uint32_t version, uint32_t id)
1817 {
1818         struct wl_shell *shell = data;
1819         struct wl_resource *resource;
1820
1821         resource = wl_client_add_object(client, &desktop_shell_interface,
1822                                         &desktop_shell_implementation,
1823                                         id, shell);
1824
1825         if (client == shell->child.client) {
1826                 resource->destroy = unbind_desktop_shell;
1827                 shell->child.desktop_shell = resource;
1828                 return;
1829         }
1830
1831         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1832                                "permission to bind desktop_shell denied");
1833         wl_resource_destroy(resource, 0);
1834 }
1835
1836 static void
1837 screensaver_set_surface(struct wl_client *client,
1838                         struct wl_resource *resource,
1839                         struct wl_resource *shell_surface_resource,
1840                         struct wl_resource *output_resource)
1841 {
1842         struct wl_shell *shell = resource->data;
1843         struct shell_surface *surface = shell_surface_resource->data;
1844         struct weston_output *output = output_resource->data;
1845
1846         if (reset_shell_surface_type(surface))
1847                 return;
1848
1849         surface->type = SHELL_SURFACE_SCREENSAVER;
1850
1851         surface->fullscreen_output = output;
1852         surface->output = output;
1853         wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1854 }
1855
1856 static const struct screensaver_interface screensaver_implementation = {
1857         screensaver_set_surface
1858 };
1859
1860 static void
1861 unbind_screensaver(struct wl_resource *resource)
1862 {
1863         struct wl_shell *shell = resource->data;
1864
1865         shell->screensaver.binding = NULL;
1866         free(resource);
1867 }
1868
1869 static void
1870 bind_screensaver(struct wl_client *client,
1871                  void *data, uint32_t version, uint32_t id)
1872 {
1873         struct wl_shell *shell = data;
1874         struct wl_resource *resource;
1875
1876         resource = wl_client_add_object(client, &screensaver_interface,
1877                                         &screensaver_implementation,
1878                                         id, shell);
1879
1880         if (shell->screensaver.binding == NULL) {
1881                 resource->destroy = unbind_screensaver;
1882                 shell->screensaver.binding = resource;
1883                 return;
1884         }
1885
1886         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1887                                "interface object already bound");
1888         wl_resource_destroy(resource, 0);
1889 }
1890
1891 struct switcher {
1892         struct weston_compositor *compositor;
1893         struct weston_surface *current;
1894         struct wl_listener listener;
1895         struct wl_keyboard_grab grab;
1896 };
1897
1898 static void
1899 switcher_next(struct switcher *switcher)
1900 {
1901         struct weston_compositor *compositor = switcher->compositor;
1902         struct weston_surface *surface;
1903         struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1904
1905         wl_list_for_each(surface, &compositor->surface_list, link) {
1906                 switch (get_shell_surface_type(surface)) {
1907                 case SHELL_SURFACE_TOPLEVEL:
1908                 case SHELL_SURFACE_FULLSCREEN:
1909                 case SHELL_SURFACE_MAXIMIZED:
1910                         if (first == NULL)
1911                                 first = surface;
1912                         if (prev == switcher->current)
1913                                 next = surface;
1914                         prev = surface;
1915                         surface->alpha = 64;
1916                         surface->geometry.dirty = 1;
1917                         weston_surface_damage(surface);
1918                         break;
1919                 default:
1920                         break;
1921                 }
1922         }
1923
1924         if (next == NULL)
1925                 next = first;
1926
1927         if (next == NULL)
1928                 return;
1929
1930         wl_list_remove(&switcher->listener.link);
1931         wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1932                        &switcher->listener.link);
1933
1934         switcher->current = next;
1935         next->alpha = 255;
1936 }
1937
1938 static void
1939 switcher_handle_surface_destroy(struct wl_listener *listener,
1940                                 struct wl_resource *resource, uint32_t time)
1941 {
1942         struct switcher *switcher =
1943                 container_of(listener, struct switcher, listener);
1944
1945         switcher_next(switcher);
1946 }
1947
1948 static void
1949 switcher_destroy(struct switcher *switcher, uint32_t time)
1950 {
1951         struct weston_compositor *compositor = switcher->compositor;
1952         struct weston_surface *surface;
1953         struct weston_input_device *device =
1954                 (struct weston_input_device *) switcher->grab.input_device;
1955
1956         wl_list_for_each(surface, &compositor->surface_list, link) {
1957                 surface->alpha = 255;
1958                 weston_surface_damage(surface);
1959         }
1960
1961         if (switcher->current)
1962                 activate(compositor->shell, switcher->current, device, time);
1963         wl_list_remove(&switcher->listener.link);
1964         wl_input_device_end_keyboard_grab(&device->input_device, time);
1965         free(switcher);
1966 }
1967
1968 static void
1969 switcher_key(struct wl_keyboard_grab *grab,
1970              uint32_t time, uint32_t key, int32_t state)
1971 {
1972         struct switcher *switcher = container_of(grab, struct switcher, grab);
1973         struct weston_input_device *device =
1974                 (struct weston_input_device *) grab->input_device;
1975
1976         if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1977                 switcher_destroy(switcher, time);
1978         } else if (key == KEY_TAB && state) {
1979                 switcher_next(switcher);
1980         }
1981 };
1982
1983 static const struct wl_keyboard_grab_interface switcher_grab = {
1984         switcher_key
1985 };
1986
1987 static void
1988 switcher_binding(struct wl_input_device *device, uint32_t time,
1989                  uint32_t key, uint32_t button, uint32_t axis,
1990                  int32_t state, void *data)
1991 {
1992         struct weston_compositor *compositor = data;
1993         struct switcher *switcher;
1994
1995         switcher = malloc(sizeof *switcher);
1996         switcher->compositor = compositor;
1997         switcher->current = NULL;
1998         switcher->listener.func = switcher_handle_surface_destroy;
1999         wl_list_init(&switcher->listener.link);
2000
2001         switcher->grab.interface = &switcher_grab;
2002         wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
2003         wl_input_device_set_keyboard_focus(device, NULL,
2004                                            weston_compositor_get_time());
2005         switcher_next(switcher);
2006 }
2007
2008 static void
2009 backlight_binding(struct wl_input_device *device, uint32_t time,
2010                   uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2011 {
2012         struct weston_compositor *compositor = data;
2013         struct weston_output *output;
2014         long backlight_new = 0;
2015
2016         /* TODO: we're limiting to simple use cases, where we assume just
2017          * control on the primary display. We'd have to extend later if we
2018          * ever get support for setting backlights on random desktop LCD
2019          * panels though */
2020         output = get_default_output(compositor);
2021         if (!output)
2022                 return;
2023
2024         if (!output->set_backlight)
2025                 return;
2026
2027         if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2028                 backlight_new = output->backlight_current - 25;
2029         else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2030                 backlight_new = output->backlight_current + 25;
2031
2032         if (backlight_new < 5)
2033                 backlight_new = 5;
2034         if (backlight_new > 255)
2035                 backlight_new = 255;
2036
2037         output->backlight_current = backlight_new;
2038         output->set_backlight(output, output->backlight_current);
2039 }
2040
2041 static void
2042 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2043                       uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2044 {
2045         struct weston_compositor *compositor = data;
2046         struct wl_shell *shell =
2047                 container_of(compositor->shell, struct wl_shell, shell);
2048         struct weston_surface *surface;
2049
2050         if (shell->debug_repaint_surface) {
2051                 weston_surface_destroy(shell->debug_repaint_surface);
2052                 shell->debug_repaint_surface = NULL;
2053         } else {
2054                 surface = weston_surface_create(compositor);
2055                 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2056                 weston_surface_configure(surface, 0, 0, 8192, 8192);
2057                 wl_list_insert(&compositor->fade_layer.surface_list,
2058                                &surface->layer_link);
2059                 weston_surface_assign_output(surface);
2060                 pixman_region32_init(&surface->input);
2061
2062                 /* Here's the dirty little trick that makes the
2063                  * repaint debugging work: we force an
2064                  * update_transform first to update dependent state
2065                  * and clear the geometry.dirty bit.  Then we clear
2066                  * the surface damage so it only gets repainted
2067                  * piecewise as we repaint other things.  */
2068
2069                 weston_surface_update_transform(surface);
2070                 pixman_region32_fini(&surface->damage);
2071                 pixman_region32_init(&surface->damage);
2072                 shell->debug_repaint_surface = surface;
2073         }
2074 }
2075
2076 static void
2077 shell_destroy(struct weston_shell *base)
2078 {
2079         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2080
2081         if (shell->child.client)
2082                 wl_client_destroy(shell->child.client);
2083
2084         free(shell->screensaver.path);
2085         free(shell);
2086 }
2087
2088 int
2089 shell_init(struct weston_compositor *ec);
2090
2091 WL_EXPORT int
2092 shell_init(struct weston_compositor *ec)
2093 {
2094         struct wl_shell *shell;
2095
2096         shell = malloc(sizeof *shell);
2097         if (shell == NULL)
2098                 return -1;
2099
2100         memset(shell, 0, sizeof *shell);
2101         shell->compositor = ec;
2102         shell->shell.lock = lock;
2103         shell->shell.unlock = unlock;
2104         shell->shell.destroy = shell_destroy;
2105
2106         wl_list_init(&shell->backgrounds);
2107         wl_list_init(&shell->panels);
2108         wl_list_init(&shell->screensaver.surfaces);
2109
2110         weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2111         weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2112         weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2113         weston_layer_init(&shell->background_layer,
2114                           &shell->toplevel_layer.link);
2115         wl_list_init(&shell->lock_layer.surface_list);
2116
2117         shell_configuration(shell);
2118
2119         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2120                                   shell, bind_shell) == NULL)
2121                 return -1;
2122
2123         if (wl_display_add_global(ec->wl_display,
2124                                   &desktop_shell_interface,
2125                                   shell, bind_desktop_shell) == NULL)
2126                 return -1;
2127
2128         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2129                                   shell, bind_screensaver) == NULL)
2130                 return -1;
2131
2132         shell->child.deathstamp = weston_compositor_get_time();
2133         if (launch_desktop_shell_process(shell) != 0)
2134                 return -1;
2135
2136         weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2137                                         move_binding, shell);
2138         weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2139                                         resize_binding, shell);
2140         weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2141                                         MODIFIER_CTRL | MODIFIER_ALT,
2142                                         terminate_binding, ec);
2143         weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2144                                         click_to_activate_binding, ec);
2145         weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2146                                         MODIFIER_SUPER | MODIFIER_ALT,
2147                                         surface_opacity_binding, NULL);
2148         weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2149                                         MODIFIER_SUPER, zoom_binding, NULL);
2150         weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2151                                         MODIFIER_SUPER | MODIFIER_ALT,
2152                                         rotate_binding, NULL);
2153         weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2154                                         switcher_binding, ec);
2155
2156         /* brightness */
2157         weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2158                                         backlight_binding, ec);
2159         weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2160                                         backlight_binding, ec);
2161         weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2162                                         backlight_binding, ec);
2163         weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2164                                         backlight_binding, ec);
2165
2166         weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2167                                         debug_repaint_binding, ec);
2168
2169         ec->shell = &shell->shell;
2170
2171         return 0;
2172 }