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