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