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