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