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