ad6750d32753945b60c42a493950832e63524a5e
[profile/ivi/weston-ivi-shell.git] / desktop-shell / shell.c
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  * Copyright © 2013 Raspberry Pi Foundation
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <linux/input.h>
32 #include <assert.h>
33 #include <signal.h>
34 #include <math.h>
35 #include <sys/types.h>
36
37 #include "shell.h"
38 #include "desktop-shell-server-protocol.h"
39 #include "workspaces-server-protocol.h"
40 #include "../shared/config-parser.h"
41 #include "xdg-shell-server-protocol.h"
42
43 #define DEFAULT_NUM_WORKSPACES 1
44 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
45
46 #ifndef static_assert
47 #define static_assert(cond, msg)
48 #endif
49
50 struct focus_state {
51         struct weston_seat *seat;
52         struct workspace *ws;
53         struct weston_surface *keyboard_focus;
54         struct wl_list link;
55         struct wl_listener seat_destroy_listener;
56         struct wl_listener surface_destroy_listener;
57 };
58
59 enum shell_surface_type {
60         SHELL_SURFACE_NONE,
61         SHELL_SURFACE_TOPLEVEL,
62         SHELL_SURFACE_POPUP,
63         SHELL_SURFACE_XWAYLAND
64 };
65
66 struct shell_client;
67
68 /*
69  * Surface stacking and ordering.
70  *
71  * This is handled using several linked lists of surfaces, organised into
72  * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73  * above all of the surfaces in the layer below. The set of layers is static and
74  * in the following order (top-most first):
75  *  • Lock layer (only ever displayed on its own)
76  *  • Cursor layer
77  *  • Input panel layer
78  *  • Fullscreen layer
79  *  • Panel layer
80  *  • Workspace layers
81  *  • Background layer
82  *
83  * The list of layers may be manipulated to remove whole layers of surfaces from
84  * display. For example, when locking the screen, all layers except the lock
85  * layer are removed.
86  *
87  * A surface’s layer is modified on configuring the surface, in
88  * set_surface_type() (which is only called when the surface’s type change is
89  * _committed_). If a surface’s type changes (e.g. when making a window
90  * fullscreen) its layer changes too.
91  *
92  * In order to allow popup and transient surfaces to be correctly stacked above
93  * their parent surfaces, each surface tracks both its parent surface, and a
94  * linked list of its children. When a surface’s layer is updated, so are the
95  * layers of its children. Note that child surfaces are *not* the same as
96  * subsurfaces — child/parent surfaces are purely for maintaining stacking
97  * order.
98  *
99  * The children_link list of siblings of a surface (i.e. those surfaces which
100  * have the same parent) only contains weston_surfaces which have a
101  * shell_surface. Stacking is not implemented for non-shell_surface
102  * weston_surfaces. This means that the following implication does *not* hold:
103  *     (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104  */
105
106 struct shell_surface {
107         struct wl_resource *resource;
108         struct wl_signal destroy_signal;
109         struct shell_client *owner;
110
111         struct weston_surface *surface;
112         struct weston_view *view;
113         int32_t last_width, last_height;
114         struct wl_listener surface_destroy_listener;
115         struct wl_listener resource_destroy_listener;
116
117         struct weston_surface *parent;
118         struct wl_list children_list;  /* child surfaces of this one */
119         struct wl_list children_link;  /* sibling surfaces of this one */
120         struct desktop_shell *shell;
121
122         enum shell_surface_type type;
123         char *title, *class;
124         int32_t saved_x, saved_y;
125         int32_t saved_width, saved_height;
126         bool saved_position_valid;
127         bool saved_size_valid;
128         bool saved_rotation_valid;
129         int unresponsive, grabbed;
130         uint32_t resize_edges;
131
132         struct {
133                 struct weston_transform transform;
134                 struct weston_matrix rotation;
135         } rotation;
136
137         struct {
138                 struct wl_list grab_link;
139                 int32_t x, y;
140                 struct shell_seat *shseat;
141                 uint32_t serial;
142         } popup;
143
144         struct {
145                 int32_t x, y;
146                 uint32_t flags;
147         } transient;
148
149         struct {
150                 enum wl_shell_surface_fullscreen_method type;
151                 struct weston_transform transform; /* matrix from x, y */
152                 uint32_t framerate;
153                 struct weston_view *black_view;
154         } fullscreen;
155
156         struct weston_transform workspace_transform;
157
158         struct weston_output *fullscreen_output;
159         struct weston_output *output;
160         struct wl_list link;
161
162         const struct weston_shell_client *client;
163
164         struct surface_state {
165                 bool maximized;
166                 bool fullscreen;
167                 bool relative;
168                 bool lowered;
169         } state, next_state, requested_state; /* surface states */
170         bool state_changed;
171         bool state_requested;
172
173         struct {
174                 int32_t x, y, width, height;
175         } geometry, next_geometry;
176         bool has_set_geometry, has_next_geometry;
177
178         int focus_count;
179 };
180
181 struct shell_grab {
182         struct weston_pointer_grab grab;
183         struct shell_surface *shsurf;
184         struct wl_listener shsurf_destroy_listener;
185 };
186
187 struct shell_touch_grab {
188         struct weston_touch_grab grab;
189         struct shell_surface *shsurf;
190         struct wl_listener shsurf_destroy_listener;
191         struct weston_touch *touch;
192 };
193
194 struct weston_move_grab {
195         struct shell_grab base;
196         wl_fixed_t dx, dy;
197         int client_initiated;
198 };
199
200 struct weston_touch_move_grab {
201         struct shell_touch_grab base;
202         int active;
203         wl_fixed_t dx, dy;
204 };
205
206 struct rotate_grab {
207         struct shell_grab base;
208         struct weston_matrix rotation;
209         struct {
210                 float x;
211                 float y;
212         } center;
213 };
214
215 struct shell_seat {
216         struct weston_seat *seat;
217         struct wl_listener seat_destroy_listener;
218         struct weston_surface *focused_surface;
219
220         struct wl_listener caps_changed_listener;
221         struct wl_listener pointer_focus_listener;
222         struct wl_listener keyboard_focus_listener;
223
224         struct {
225                 struct weston_pointer_grab grab;
226                 struct weston_touch_grab touch_grab;
227                 struct wl_list surfaces_list;
228                 struct wl_client *client;
229                 int32_t initial_up;
230                 enum { POINTER, TOUCH } type;
231         } popup_grab;
232 };
233
234 struct shell_client {
235         struct wl_resource *resource;
236         struct wl_client *client;
237         struct desktop_shell *shell;
238         struct wl_listener destroy_listener;
239         struct wl_event_source *ping_timer;
240         uint32_t ping_serial;
241         int unresponsive;
242 };
243
244 static struct desktop_shell *
245 shell_surface_get_shell(struct shell_surface *shsurf);
246
247 static void
248 surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
249
250 static void
251 shell_fade_startup(struct desktop_shell *shell);
252
253 static struct shell_seat *
254 get_shell_seat(struct weston_seat *seat);
255
256 static void
257 get_output_panel_size(struct desktop_shell *shell,
258                       struct weston_output *output,
259                       int *width, int *height);
260
261 static void
262 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
263
264 static bool
265 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
266
267 static bool
268 shell_surface_is_xdg_surface(struct shell_surface *shsurf);
269
270 static bool
271 shell_surface_is_xdg_popup(struct shell_surface *shsurf);
272
273 static void
274 shell_surface_set_parent(struct shell_surface *shsurf,
275                          struct weston_surface *parent);
276
277 static bool
278 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
279 {
280         struct desktop_shell *shell;
281         struct weston_view *top_fs_ev;
282
283         shell = shell_surface_get_shell(shsurf);
284
285         if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
286                 return false;
287
288         top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
289                                  struct weston_view,
290                                  layer_link.link);
291         return (shsurf == get_shell_surface(top_fs_ev->surface));
292 }
293
294 static void
295 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
296 {
297         struct shell_grab *grab;
298
299         grab = container_of(listener, struct shell_grab,
300                             shsurf_destroy_listener);
301
302         grab->shsurf = NULL;
303 }
304
305 struct weston_view *
306 get_default_view(struct weston_surface *surface)
307 {
308         struct shell_surface *shsurf;
309         struct weston_view *view;
310
311         if (!surface || wl_list_empty(&surface->views))
312                 return NULL;
313
314         shsurf = get_shell_surface(surface);
315         if (shsurf)
316                 return shsurf->view;
317
318         wl_list_for_each(view, &surface->views, surface_link)
319                 if (weston_view_is_mapped(view))
320                         return view;
321
322         return container_of(surface->views.next, struct weston_view, surface_link);
323 }
324
325 static void
326 popup_grab_end(struct weston_pointer *pointer);
327 static void
328 touch_popup_grab_end(struct weston_touch *touch);
329
330 static void
331 shell_grab_start(struct shell_grab *grab,
332                  const struct weston_pointer_grab_interface *interface,
333                  struct shell_surface *shsurf,
334                  struct weston_pointer *pointer,
335                  enum desktop_shell_cursor cursor)
336 {
337         struct desktop_shell *shell = shsurf->shell;
338
339         popup_grab_end(pointer);
340         if (pointer->seat->touch)
341                 touch_popup_grab_end(pointer->seat->touch);
342
343         grab->grab.interface = interface;
344         grab->shsurf = shsurf;
345         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
346         wl_signal_add(&shsurf->destroy_signal,
347                       &grab->shsurf_destroy_listener);
348
349         shsurf->grabbed = 1;
350         weston_pointer_start_grab(pointer, &grab->grab);
351         if (shell->child.desktop_shell) {
352                 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
353                                                cursor);
354                 weston_pointer_set_focus(pointer,
355                                          get_default_view(shell->grab_surface),
356                                          wl_fixed_from_int(0),
357                                          wl_fixed_from_int(0));
358         }
359 }
360
361 static void
362 get_output_panel_size(struct desktop_shell *shell,
363                       struct weston_output *output,
364                       int *width,
365                       int *height)
366 {
367         struct weston_view *view;
368
369         *width = 0;
370         *height = 0;
371
372         if (!output)
373                 return;
374
375         wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
376                 float x, y;
377
378                 if (view->surface->output != output)
379                         continue;
380
381                 switch (shell->panel_position) {
382                 case DESKTOP_SHELL_PANEL_POSITION_TOP:
383                 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
384
385                         weston_view_to_global_float(view,
386                                                     view->surface->width, 0,
387                                                     &x, &y);
388
389                         *width = (int) x;
390                         *height = view->surface->height + (int) y;
391                         return;
392
393                 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
394                 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
395                         weston_view_to_global_float(view,
396                                                     0, view->surface->height,
397                                                     &x, &y);
398
399                         *width = view->surface->width + (int) x;
400                         *height = (int) y;
401                         return;
402
403                 default:
404                         /* we've already set width and height to
405                          * fallback values. */
406                         break;
407                 }
408         }
409
410         /* the correct view wasn't found */
411 }
412
413 static void
414 get_output_work_area(struct desktop_shell *shell,
415                      struct weston_output *output,
416                      pixman_rectangle32_t *area)
417 {
418         int32_t panel_width = 0, panel_height = 0;
419
420         area->x = 0;
421         area->y = 0;
422
423         get_output_panel_size(shell, output, &panel_width, &panel_height);
424
425         switch (shell->panel_position) {
426         case DESKTOP_SHELL_PANEL_POSITION_TOP:
427         default:
428                 area->y = panel_height;
429         case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
430                 area->width = output->width;
431                 area->height = output->height - panel_height;
432                 break;
433         case DESKTOP_SHELL_PANEL_POSITION_LEFT:
434                 area->x = panel_width;
435         case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
436                 area->width = output->width - panel_width;
437                 area->height = output->height;
438                 break;
439         }
440 }
441
442 static void
443 send_configure_for_surface(struct shell_surface *shsurf)
444 {
445         int32_t width, height;
446         struct surface_state *state;
447
448         if (shsurf->state_requested)
449                 state = &shsurf->requested_state;
450         else if (shsurf->state_changed)
451                 state = &shsurf->next_state;
452         else
453                 state = &shsurf->state;
454
455         if (state->fullscreen) {
456                 width = shsurf->output->width;
457                 height = shsurf->output->height;
458         } else if (state->maximized) {
459                 struct desktop_shell *shell;
460                 pixman_rectangle32_t area;
461
462                 shell = shell_surface_get_shell(shsurf);
463                 get_output_work_area(shell, shsurf->output, &area);
464
465                 width = area.width;
466                 height = area.height;
467         } else {
468                 width = 0;
469                 height = 0;
470         }
471
472         shsurf->client->send_configure(shsurf->surface, width, height);
473 }
474
475 static void
476 shell_surface_state_changed(struct shell_surface *shsurf)
477 {
478         if (shell_surface_is_xdg_surface(shsurf))
479                 send_configure_for_surface(shsurf);
480 }
481
482 static void
483 shell_grab_end(struct shell_grab *grab)
484 {
485         if (grab->shsurf) {
486                 wl_list_remove(&grab->shsurf_destroy_listener.link);
487                 grab->shsurf->grabbed = 0;
488
489                 if (grab->shsurf->resize_edges) {
490                         grab->shsurf->resize_edges = 0;
491                         shell_surface_state_changed(grab->shsurf);
492                 }
493         }
494
495         weston_pointer_end_grab(grab->grab.pointer);
496 }
497
498 static void
499 shell_touch_grab_start(struct shell_touch_grab *grab,
500                        const struct weston_touch_grab_interface *interface,
501                        struct shell_surface *shsurf,
502                        struct weston_touch *touch)
503 {
504         struct desktop_shell *shell = shsurf->shell;
505
506         touch_popup_grab_end(touch);
507         if (touch->seat->pointer)
508                 popup_grab_end(touch->seat->pointer);
509
510         grab->grab.interface = interface;
511         grab->shsurf = shsurf;
512         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
513         wl_signal_add(&shsurf->destroy_signal,
514                       &grab->shsurf_destroy_listener);
515
516         grab->touch = touch;
517         shsurf->grabbed = 1;
518
519         weston_touch_start_grab(touch, &grab->grab);
520         if (shell->child.desktop_shell)
521                 weston_touch_set_focus(touch->seat,
522                                        get_default_view(shell->grab_surface));
523 }
524
525 static void
526 shell_touch_grab_end(struct shell_touch_grab *grab)
527 {
528         if (grab->shsurf) {
529                 wl_list_remove(&grab->shsurf_destroy_listener.link);
530                 grab->shsurf->grabbed = 0;
531         }
532
533         weston_touch_end_grab(grab->touch);
534 }
535
536 static void
537 center_on_output(struct weston_view *view,
538                  struct weston_output *output);
539
540 static enum weston_keyboard_modifier
541 get_modifier(char *modifier)
542 {
543         if (!modifier)
544                 return MODIFIER_SUPER;
545
546         if (!strcmp("ctrl", modifier))
547                 return MODIFIER_CTRL;
548         else if (!strcmp("alt", modifier))
549                 return MODIFIER_ALT;
550         else if (!strcmp("super", modifier))
551                 return MODIFIER_SUPER;
552         else
553                 return MODIFIER_SUPER;
554 }
555
556 static enum animation_type
557 get_animation_type(char *animation)
558 {
559         if (!animation)
560                 return ANIMATION_NONE;
561
562         if (!strcmp("zoom", animation))
563                 return ANIMATION_ZOOM;
564         else if (!strcmp("fade", animation))
565                 return ANIMATION_FADE;
566         else if (!strcmp("dim-layer", animation))
567                 return ANIMATION_DIM_LAYER;
568         else
569                 return ANIMATION_NONE;
570 }
571
572 static void
573 shell_configuration(struct desktop_shell *shell)
574 {
575         struct weston_config_section *section;
576         int duration;
577         char *s, *client;
578
579         section = weston_config_get_section(shell->compositor->config,
580                                             "screensaver", NULL, NULL);
581         weston_config_section_get_string(section,
582                                          "path", &shell->screensaver.path, NULL);
583         weston_config_section_get_int(section, "duration", &duration, 60);
584         shell->screensaver.duration = duration * 1000;
585
586         section = weston_config_get_section(shell->compositor->config,
587                                             "shell", NULL, NULL);
588         asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
589                                    WESTON_SHELL_CLIENT);
590         weston_config_section_get_string(section,
591                                          "client", &s, client);
592         free(client);
593         shell->client = s;
594         weston_config_section_get_string(section,
595                                          "binding-modifier", &s, "super");
596         shell->binding_modifier = get_modifier(s);
597         free(s);
598
599         weston_config_section_get_string(section,
600                                          "exposay-modifier", &s, "none");
601         if (strcmp(s, "none") == 0)
602                 shell->exposay_modifier = 0;
603         else
604                 shell->exposay_modifier = get_modifier(s);
605         free(s);
606
607         weston_config_section_get_string(section, "animation", &s, "none");
608         shell->win_animation_type = get_animation_type(s);
609         free(s);
610         weston_config_section_get_string(section, "close-animation", &s, "fade");
611         shell->win_close_animation_type = get_animation_type(s);
612         free(s);
613         weston_config_section_get_string(section,
614                                          "startup-animation", &s, "fade");
615         shell->startup_animation_type = get_animation_type(s);
616         free(s);
617         if (shell->startup_animation_type == ANIMATION_ZOOM)
618                 shell->startup_animation_type = ANIMATION_NONE;
619         weston_config_section_get_string(section, "focus-animation", &s, "none");
620         shell->focus_animation_type = get_animation_type(s);
621         free(s);
622         weston_config_section_get_uint(section, "num-workspaces",
623                                        &shell->workspaces.num,
624                                        DEFAULT_NUM_WORKSPACES);
625 }
626
627 struct weston_output *
628 get_default_output(struct weston_compositor *compositor)
629 {
630         return container_of(compositor->output_list.next,
631                             struct weston_output, link);
632 }
633
634
635 /* no-op func for checking focus surface */
636 static void
637 focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
638 {
639 }
640
641 static struct focus_surface *
642 get_focus_surface(struct weston_surface *surface)
643 {
644         if (surface->configure == focus_surface_configure)
645                 return surface->configure_private;
646         else
647                 return NULL;
648 }
649
650 static bool
651 is_focus_surface (struct weston_surface *es)
652 {
653         return (es->configure == focus_surface_configure);
654 }
655
656 static bool
657 is_focus_view (struct weston_view *view)
658 {
659         return is_focus_surface (view->surface);
660 }
661
662 static struct focus_surface *
663 create_focus_surface(struct weston_compositor *ec,
664                      struct weston_output *output)
665 {
666         struct focus_surface *fsurf = NULL;
667         struct weston_surface *surface = NULL;
668
669         fsurf = malloc(sizeof *fsurf);
670         if (!fsurf)
671                 return NULL;
672
673         fsurf->surface = weston_surface_create(ec);
674         surface = fsurf->surface;
675         if (surface == NULL) {
676                 free(fsurf);
677                 return NULL;
678         }
679
680         surface->configure = focus_surface_configure;
681         surface->output = output;
682         surface->configure_private = fsurf;
683
684         fsurf->view = weston_view_create(surface);
685         if (fsurf->view == NULL) {
686                 weston_surface_destroy(surface);
687                 free(fsurf);
688                 return NULL;
689         }
690         fsurf->view->output = output;
691
692         weston_surface_set_size(surface, output->width, output->height);
693         weston_view_set_position(fsurf->view, output->x, output->y);
694         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
695         pixman_region32_fini(&surface->opaque);
696         pixman_region32_init_rect(&surface->opaque, output->x, output->y,
697                                   output->width, output->height);
698         pixman_region32_fini(&surface->input);
699         pixman_region32_init(&surface->input);
700
701         wl_list_init(&fsurf->workspace_transform.link);
702
703         return fsurf;
704 }
705
706 static void
707 focus_surface_destroy(struct focus_surface *fsurf)
708 {
709         weston_surface_destroy(fsurf->surface);
710         free(fsurf);
711 }
712
713 static void
714 focus_animation_done(struct weston_view_animation *animation, void *data)
715 {
716         struct workspace *ws = data;
717
718         ws->focus_animation = NULL;
719 }
720
721 static void
722 focus_state_destroy(struct focus_state *state)
723 {
724         wl_list_remove(&state->seat_destroy_listener.link);
725         wl_list_remove(&state->surface_destroy_listener.link);
726         free(state);
727 }
728
729 static void
730 focus_state_seat_destroy(struct wl_listener *listener, void *data)
731 {
732         struct focus_state *state = container_of(listener,
733                                                  struct focus_state,
734                                                  seat_destroy_listener);
735
736         wl_list_remove(&state->link);
737         focus_state_destroy(state);
738 }
739
740 static void
741 focus_state_surface_destroy(struct wl_listener *listener, void *data)
742 {
743         struct focus_state *state = container_of(listener,
744                                                  struct focus_state,
745                                                  surface_destroy_listener);
746         struct desktop_shell *shell;
747         struct weston_surface *main_surface, *next;
748         struct weston_view *view;
749
750         main_surface = weston_surface_get_main_surface(state->keyboard_focus);
751
752         next = NULL;
753         wl_list_for_each(view,
754                          &state->ws->layer.view_list.link, layer_link.link) {
755                 if (view->surface == main_surface)
756                         continue;
757                 if (is_focus_view(view))
758                         continue;
759
760                 next = view->surface;
761                 break;
762         }
763
764         /* if the focus was a sub-surface, activate its main surface */
765         if (main_surface != state->keyboard_focus)
766                 next = main_surface;
767
768         shell = state->seat->compositor->shell_interface.shell;
769         if (next) {
770                 state->keyboard_focus = NULL;
771                 activate(shell, next, state->seat, true);
772         } else {
773                 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
774                         if (state->ws->focus_animation)
775                                 weston_view_animation_destroy(state->ws->focus_animation);
776
777                         state->ws->focus_animation = weston_fade_run(
778                                 state->ws->fsurf_front->view,
779                                 state->ws->fsurf_front->view->alpha, 0.0, 300,
780                                 focus_animation_done, state->ws);
781                 }
782
783                 wl_list_remove(&state->link);
784                 focus_state_destroy(state);
785         }
786 }
787
788 static struct focus_state *
789 focus_state_create(struct weston_seat *seat, struct workspace *ws)
790 {
791         struct focus_state *state;
792
793         state = malloc(sizeof *state);
794         if (state == NULL)
795                 return NULL;
796
797         state->keyboard_focus = NULL;
798         state->ws = ws;
799         state->seat = seat;
800         wl_list_insert(&ws->focus_list, &state->link);
801
802         state->seat_destroy_listener.notify = focus_state_seat_destroy;
803         state->surface_destroy_listener.notify = focus_state_surface_destroy;
804         wl_signal_add(&seat->destroy_signal,
805                       &state->seat_destroy_listener);
806         wl_list_init(&state->surface_destroy_listener.link);
807
808         return state;
809 }
810
811 static struct focus_state *
812 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
813 {
814         struct workspace *ws = get_current_workspace(shell);
815         struct focus_state *state;
816
817         wl_list_for_each(state, &ws->focus_list, link)
818                 if (state->seat == seat)
819                         break;
820
821         if (&state->link == &ws->focus_list)
822                 state = focus_state_create(seat, ws);
823
824         return state;
825 }
826
827 static void
828 focus_state_set_focus(struct focus_state *state,
829                       struct weston_surface *surface)
830 {
831         if (state->keyboard_focus) {
832                 wl_list_remove(&state->surface_destroy_listener.link);
833                 wl_list_init(&state->surface_destroy_listener.link);
834         }
835
836         state->keyboard_focus = surface;
837         if (surface)
838                 wl_signal_add(&surface->destroy_signal,
839                               &state->surface_destroy_listener);
840 }
841
842 static void
843 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
844 {
845         struct focus_state *state, *next;
846         struct weston_surface *surface;
847         struct wl_list pending_seat_list;
848         struct weston_seat *seat, *next_seat;
849
850         /* Temporarily steal the list of seats so that we can keep
851          * track of the seats we've already processed */
852         wl_list_init(&pending_seat_list);
853         wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
854         wl_list_init(&shell->compositor->seat_list);
855
856         wl_list_for_each_safe(state, next, &ws->focus_list, link) {
857                 wl_list_remove(&state->seat->link);
858                 wl_list_insert(&shell->compositor->seat_list,
859                                &state->seat->link);
860
861                 if (state->seat->keyboard == NULL)
862                         continue;
863
864                 surface = state->keyboard_focus;
865
866                 weston_keyboard_set_focus(state->seat->keyboard, surface);
867         }
868
869         /* For any remaining seats that we don't have a focus state
870          * for we'll reset the keyboard focus to NULL */
871         wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
872                 wl_list_insert(&shell->compositor->seat_list, &seat->link);
873
874                 if (seat->keyboard == NULL)
875                         continue;
876
877                 weston_keyboard_set_focus(seat->keyboard, NULL);
878         }
879 }
880
881 static void
882 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
883                     struct weston_seat *seat)
884 {
885         struct focus_state *state;
886
887         wl_list_for_each(state, &ws->focus_list, link) {
888                 if (state->seat == seat) {
889                         focus_state_set_focus(state, seat->keyboard->focus);
890                         return;
891                 }
892         }
893 }
894
895 static void
896 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
897                  struct weston_surface *surface)
898 {
899         struct focus_state *state;
900
901         wl_list_for_each(state, &ws->focus_list, link)
902                 if (state->keyboard_focus == surface)
903                         focus_state_set_focus(state, NULL);
904 }
905
906 static void
907 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
908                      struct weston_view *from, struct weston_view *to)
909 {
910         struct weston_output *output;
911         bool focus_surface_created = false;
912
913         /* FIXME: Only support dim animation using two layers */
914         if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
915                 return;
916
917         output = get_default_output(shell->compositor);
918         if (ws->fsurf_front == NULL && (from || to)) {
919                 ws->fsurf_front = create_focus_surface(shell->compositor, output);
920                 if (ws->fsurf_front == NULL)
921                         return;
922                 ws->fsurf_front->view->alpha = 0.0;
923
924                 ws->fsurf_back = create_focus_surface(shell->compositor, output);
925                 if (ws->fsurf_back == NULL) {
926                         focus_surface_destroy(ws->fsurf_front);
927                         return;
928                 }
929                 ws->fsurf_back->view->alpha = 0.0;
930
931                 focus_surface_created = true;
932         } else {
933                 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
934                 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
935         }
936
937         if (ws->focus_animation) {
938                 weston_view_animation_destroy(ws->focus_animation);
939                 ws->focus_animation = NULL;
940         }
941
942         if (to)
943                 weston_layer_entry_insert(&to->layer_link,
944                                           &ws->fsurf_front->view->layer_link);
945         else if (from)
946                 weston_layer_entry_insert(&ws->layer.view_list,
947                                           &ws->fsurf_front->view->layer_link);
948
949         if (focus_surface_created) {
950                 ws->focus_animation = weston_fade_run(
951                         ws->fsurf_front->view,
952                         ws->fsurf_front->view->alpha, 0.4, 300,
953                         focus_animation_done, ws);
954         } else if (from) {
955                 weston_layer_entry_insert(&from->layer_link,
956                                           &ws->fsurf_back->view->layer_link);
957                 ws->focus_animation = weston_stable_fade_run(
958                         ws->fsurf_front->view, 0.0,
959                         ws->fsurf_back->view, 0.4,
960                         focus_animation_done, ws);
961         } else if (to) {
962                 weston_layer_entry_insert(&ws->layer.view_list,
963                                           &ws->fsurf_back->view->layer_link);
964                 ws->focus_animation = weston_stable_fade_run(
965                         ws->fsurf_front->view, 0.0,
966                         ws->fsurf_back->view, 0.4,
967                         focus_animation_done, ws);
968         }
969 }
970
971 static void
972 workspace_destroy(struct workspace *ws)
973 {
974         struct focus_state *state, *next;
975
976         wl_list_for_each_safe(state, next, &ws->focus_list, link)
977                 focus_state_destroy(state);
978
979         if (ws->fsurf_front)
980                 focus_surface_destroy(ws->fsurf_front);
981         if (ws->fsurf_back)
982                 focus_surface_destroy(ws->fsurf_back);
983
984         free(ws);
985 }
986
987 static void
988 seat_destroyed(struct wl_listener *listener, void *data)
989 {
990         struct weston_seat *seat = data;
991         struct focus_state *state, *next;
992         struct workspace *ws = container_of(listener,
993                                             struct workspace,
994                                             seat_destroyed_listener);
995
996         wl_list_for_each_safe(state, next, &ws->focus_list, link)
997                 if (state->seat == seat)
998                         wl_list_remove(&state->link);
999 }
1000
1001 static struct workspace *
1002 workspace_create(void)
1003 {
1004         struct workspace *ws = malloc(sizeof *ws);
1005         if (ws == NULL)
1006                 return NULL;
1007
1008         weston_layer_init(&ws->layer, NULL);
1009
1010         wl_list_init(&ws->focus_list);
1011         wl_list_init(&ws->seat_destroyed_listener.link);
1012         ws->seat_destroyed_listener.notify = seat_destroyed;
1013         ws->fsurf_front = NULL;
1014         ws->fsurf_back = NULL;
1015         ws->focus_animation = NULL;
1016
1017         return ws;
1018 }
1019
1020 static int
1021 workspace_is_empty(struct workspace *ws)
1022 {
1023         return wl_list_empty(&ws->layer.view_list.link);
1024 }
1025
1026 static struct workspace *
1027 get_workspace(struct desktop_shell *shell, unsigned int index)
1028 {
1029         struct workspace **pws = shell->workspaces.array.data;
1030         assert(index < shell->workspaces.num);
1031         pws += index;
1032         return *pws;
1033 }
1034
1035 struct workspace *
1036 get_current_workspace(struct desktop_shell *shell)
1037 {
1038         return get_workspace(shell, shell->workspaces.current);
1039 }
1040
1041 static void
1042 activate_workspace(struct desktop_shell *shell, unsigned int index)
1043 {
1044         struct workspace *ws;
1045
1046         ws = get_workspace(shell, index);
1047         wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1048
1049         shell->workspaces.current = index;
1050 }
1051
1052 static unsigned int
1053 get_output_height(struct weston_output *output)
1054 {
1055         return abs(output->region.extents.y1 - output->region.extents.y2);
1056 }
1057
1058 static void
1059 view_translate(struct workspace *ws, struct weston_view *view, double d)
1060 {
1061         struct weston_transform *transform;
1062
1063         if (is_focus_view(view)) {
1064                 struct focus_surface *fsurf = get_focus_surface(view->surface);
1065                 transform = &fsurf->workspace_transform;
1066         } else {
1067                 struct shell_surface *shsurf = get_shell_surface(view->surface);
1068                 transform = &shsurf->workspace_transform;
1069         }
1070
1071         if (wl_list_empty(&transform->link))
1072                 wl_list_insert(view->geometry.transformation_list.prev,
1073                                &transform->link);
1074
1075         weston_matrix_init(&transform->matrix);
1076         weston_matrix_translate(&transform->matrix,
1077                                 0.0, d, 0.0);
1078         weston_view_geometry_dirty(view);
1079 }
1080
1081 static void
1082 workspace_translate_out(struct workspace *ws, double fraction)
1083 {
1084         struct weston_view *view;
1085         unsigned int height;
1086         double d;
1087
1088         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1089                 height = get_output_height(view->surface->output);
1090                 d = height * fraction;
1091
1092                 view_translate(ws, view, d);
1093         }
1094 }
1095
1096 static void
1097 workspace_translate_in(struct workspace *ws, double fraction)
1098 {
1099         struct weston_view *view;
1100         unsigned int height;
1101         double d;
1102
1103         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1104                 height = get_output_height(view->surface->output);
1105
1106                 if (fraction > 0)
1107                         d = -(height - height * fraction);
1108                 else
1109                         d = height + height * fraction;
1110
1111                 view_translate(ws, view, d);
1112         }
1113 }
1114
1115 static void
1116 broadcast_current_workspace_state(struct desktop_shell *shell)
1117 {
1118         struct wl_resource *resource;
1119
1120         wl_resource_for_each(resource, &shell->workspaces.client_list)
1121                 workspace_manager_send_state(resource,
1122                                              shell->workspaces.current,
1123                                              shell->workspaces.num);
1124 }
1125
1126 static void
1127 reverse_workspace_change_animation(struct desktop_shell *shell,
1128                                    unsigned int index,
1129                                    struct workspace *from,
1130                                    struct workspace *to)
1131 {
1132         shell->workspaces.current = index;
1133
1134         shell->workspaces.anim_to = to;
1135         shell->workspaces.anim_from = from;
1136         shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1137         shell->workspaces.anim_timestamp = 0;
1138
1139         weston_compositor_schedule_repaint(shell->compositor);
1140 }
1141
1142 static void
1143 workspace_deactivate_transforms(struct workspace *ws)
1144 {
1145         struct weston_view *view;
1146         struct weston_transform *transform;
1147
1148         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1149                 if (is_focus_view(view)) {
1150                         struct focus_surface *fsurf = get_focus_surface(view->surface);
1151                         transform = &fsurf->workspace_transform;
1152                 } else {
1153                         struct shell_surface *shsurf = get_shell_surface(view->surface);
1154                         transform = &shsurf->workspace_transform;
1155                 }
1156
1157                 if (!wl_list_empty(&transform->link)) {
1158                         wl_list_remove(&transform->link);
1159                         wl_list_init(&transform->link);
1160                 }
1161                 weston_view_geometry_dirty(view);
1162         }
1163 }
1164
1165 static void
1166 finish_workspace_change_animation(struct desktop_shell *shell,
1167                                   struct workspace *from,
1168                                   struct workspace *to)
1169 {
1170         struct weston_view *view;
1171
1172         weston_compositor_schedule_repaint(shell->compositor);
1173
1174         /* Views that extend past the bottom of the output are still
1175          * visible after the workspace animation ends but before its layer
1176          * is hidden. In that case, we need to damage below those views so
1177          * that the screen is properly repainted. */
1178         wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1179                 weston_view_damage_below(view);
1180
1181         wl_list_remove(&shell->workspaces.animation.link);
1182         workspace_deactivate_transforms(from);
1183         workspace_deactivate_transforms(to);
1184         shell->workspaces.anim_to = NULL;
1185
1186         wl_list_remove(&shell->workspaces.anim_from->layer.link);
1187 }
1188
1189 static void
1190 animate_workspace_change_frame(struct weston_animation *animation,
1191                                struct weston_output *output, uint32_t msecs)
1192 {
1193         struct desktop_shell *shell =
1194                 container_of(animation, struct desktop_shell,
1195                              workspaces.animation);
1196         struct workspace *from = shell->workspaces.anim_from;
1197         struct workspace *to = shell->workspaces.anim_to;
1198         uint32_t t;
1199         double x, y;
1200
1201         if (workspace_is_empty(from) && workspace_is_empty(to)) {
1202                 finish_workspace_change_animation(shell, from, to);
1203                 return;
1204         }
1205
1206         if (shell->workspaces.anim_timestamp == 0) {
1207                 if (shell->workspaces.anim_current == 0.0)
1208                         shell->workspaces.anim_timestamp = msecs;
1209                 else
1210                         shell->workspaces.anim_timestamp =
1211                                 msecs -
1212                                 /* Invers of movement function 'y' below. */
1213                                 (asin(1.0 - shell->workspaces.anim_current) *
1214                                  DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1215                                  M_2_PI);
1216         }
1217
1218         t = msecs - shell->workspaces.anim_timestamp;
1219
1220         /*
1221          * x = [0, π/2]
1222          * y(x) = sin(x)
1223          */
1224         x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1225         y = sin(x);
1226
1227         if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1228                 weston_compositor_schedule_repaint(shell->compositor);
1229
1230                 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1231                 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1232                 shell->workspaces.anim_current = y;
1233
1234                 weston_compositor_schedule_repaint(shell->compositor);
1235         }
1236         else
1237                 finish_workspace_change_animation(shell, from, to);
1238 }
1239
1240 static void
1241 animate_workspace_change(struct desktop_shell *shell,
1242                          unsigned int index,
1243                          struct workspace *from,
1244                          struct workspace *to)
1245 {
1246         struct weston_output *output;
1247
1248         int dir;
1249
1250         if (index > shell->workspaces.current)
1251                 dir = -1;
1252         else
1253                 dir = 1;
1254
1255         shell->workspaces.current = index;
1256
1257         shell->workspaces.anim_dir = dir;
1258         shell->workspaces.anim_from = from;
1259         shell->workspaces.anim_to = to;
1260         shell->workspaces.anim_current = 0.0;
1261         shell->workspaces.anim_timestamp = 0;
1262
1263         output = container_of(shell->compositor->output_list.next,
1264                               struct weston_output, link);
1265         wl_list_insert(&output->animation_list,
1266                        &shell->workspaces.animation.link);
1267
1268         wl_list_insert(from->layer.link.prev, &to->layer.link);
1269
1270         workspace_translate_in(to, 0);
1271
1272         restore_focus_state(shell, to);
1273
1274         weston_compositor_schedule_repaint(shell->compositor);
1275 }
1276
1277 static void
1278 update_workspace(struct desktop_shell *shell, unsigned int index,
1279                  struct workspace *from, struct workspace *to)
1280 {
1281         shell->workspaces.current = index;
1282         wl_list_insert(&from->layer.link, &to->layer.link);
1283         wl_list_remove(&from->layer.link);
1284 }
1285
1286 static void
1287 change_workspace(struct desktop_shell *shell, unsigned int index)
1288 {
1289         struct workspace *from;
1290         struct workspace *to;
1291         struct focus_state *state;
1292
1293         if (index == shell->workspaces.current)
1294                 return;
1295
1296         /* Don't change workspace when there is any fullscreen surfaces. */
1297         if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1298                 return;
1299
1300         from = get_current_workspace(shell);
1301         to = get_workspace(shell, index);
1302
1303         if (shell->workspaces.anim_from == to &&
1304             shell->workspaces.anim_to == from) {
1305                 restore_focus_state(shell, to);
1306                 reverse_workspace_change_animation(shell, index, from, to);
1307                 broadcast_current_workspace_state(shell);
1308                 return;
1309         }
1310
1311         if (shell->workspaces.anim_to != NULL)
1312                 finish_workspace_change_animation(shell,
1313                                                   shell->workspaces.anim_from,
1314                                                   shell->workspaces.anim_to);
1315
1316         restore_focus_state(shell, to);
1317
1318         if (shell->focus_animation_type != ANIMATION_NONE) {
1319                 wl_list_for_each(state, &from->focus_list, link)
1320                         if (state->keyboard_focus)
1321                                 animate_focus_change(shell, from,
1322                                                      get_default_view(state->keyboard_focus), NULL);
1323
1324                 wl_list_for_each(state, &to->focus_list, link)
1325                         if (state->keyboard_focus)
1326                                 animate_focus_change(shell, to,
1327                                                      NULL, get_default_view(state->keyboard_focus));
1328         }
1329
1330         if (workspace_is_empty(to) && workspace_is_empty(from))
1331                 update_workspace(shell, index, from, to);
1332         else
1333                 animate_workspace_change(shell, index, from, to);
1334
1335         broadcast_current_workspace_state(shell);
1336 }
1337
1338 static bool
1339 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1340 {
1341         struct wl_list *list = &ws->layer.view_list.link;
1342         struct wl_list *e;
1343
1344         if (wl_list_empty(list))
1345                 return false;
1346
1347         e = list->next;
1348
1349         if (e->next != list)
1350                 return false;
1351
1352         return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1353 }
1354
1355 static void
1356 move_surface_to_workspace(struct desktop_shell *shell,
1357                           struct shell_surface *shsurf,
1358                           uint32_t workspace)
1359 {
1360         struct workspace *from;
1361         struct workspace *to;
1362         struct weston_seat *seat;
1363         struct weston_surface *focus;
1364         struct weston_view *view;
1365
1366         if (workspace == shell->workspaces.current)
1367                 return;
1368
1369         view = get_default_view(shsurf->surface);
1370         if (!view)
1371                 return;
1372
1373         assert(weston_surface_get_main_surface(view->surface) == view->surface);
1374
1375         if (workspace >= shell->workspaces.num)
1376                 workspace = shell->workspaces.num - 1;
1377
1378         from = get_current_workspace(shell);
1379         to = get_workspace(shell, workspace);
1380
1381         weston_layer_entry_remove(&view->layer_link);
1382         weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1383
1384         shell_surface_update_child_surface_layers(shsurf);
1385
1386         drop_focus_state(shell, from, view->surface);
1387         wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1388                 if (!seat->keyboard)
1389                         continue;
1390
1391                 focus = weston_surface_get_main_surface(seat->keyboard->focus);
1392                 if (focus == view->surface)
1393                         weston_keyboard_set_focus(seat->keyboard, NULL);
1394         }
1395
1396         weston_view_damage_below(view);
1397 }
1398
1399 static void
1400 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1401                                   struct weston_seat *seat,
1402                                   unsigned int index)
1403 {
1404         struct weston_surface *surface;
1405         struct weston_view *view;
1406         struct shell_surface *shsurf;
1407         struct workspace *from;
1408         struct workspace *to;
1409         struct focus_state *state;
1410
1411         surface = weston_surface_get_main_surface(seat->keyboard->focus);
1412         view = get_default_view(surface);
1413         if (view == NULL ||
1414             index == shell->workspaces.current ||
1415             is_focus_view(view))
1416                 return;
1417
1418         from = get_current_workspace(shell);
1419         to = get_workspace(shell, index);
1420
1421         weston_layer_entry_remove(&view->layer_link);
1422         weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1423
1424         shsurf = get_shell_surface(surface);
1425         if (shsurf != NULL)
1426                 shell_surface_update_child_surface_layers(shsurf);
1427
1428         replace_focus_state(shell, to, seat);
1429         drop_focus_state(shell, from, surface);
1430
1431         if (shell->workspaces.anim_from == to &&
1432             shell->workspaces.anim_to == from) {
1433                 wl_list_remove(&to->layer.link);
1434                 wl_list_insert(from->layer.link.prev, &to->layer.link);
1435
1436                 reverse_workspace_change_animation(shell, index, from, to);
1437                 broadcast_current_workspace_state(shell);
1438
1439                 return;
1440         }
1441
1442         if (shell->workspaces.anim_to != NULL)
1443                 finish_workspace_change_animation(shell,
1444                                                   shell->workspaces.anim_from,
1445                                                   shell->workspaces.anim_to);
1446
1447         if (workspace_is_empty(from) &&
1448             workspace_has_only(to, surface))
1449                 update_workspace(shell, index, from, to);
1450         else {
1451                 if (shsurf != NULL &&
1452                     wl_list_empty(&shsurf->workspace_transform.link))
1453                         wl_list_insert(&shell->workspaces.anim_sticky_list,
1454                                        &shsurf->workspace_transform.link);
1455
1456                 animate_workspace_change(shell, index, from, to);
1457         }
1458
1459         broadcast_current_workspace_state(shell);
1460
1461         state = ensure_focus_state(shell, seat);
1462         if (state != NULL)
1463                 focus_state_set_focus(state, surface);
1464 }
1465
1466 static void
1467 workspace_manager_move_surface(struct wl_client *client,
1468                                struct wl_resource *resource,
1469                                struct wl_resource *surface_resource,
1470                                uint32_t workspace)
1471 {
1472         struct desktop_shell *shell = wl_resource_get_user_data(resource);
1473         struct weston_surface *surface =
1474                 wl_resource_get_user_data(surface_resource);
1475         struct weston_surface *main_surface;
1476         struct shell_surface *shell_surface;
1477
1478         main_surface = weston_surface_get_main_surface(surface);
1479         shell_surface = get_shell_surface(main_surface);
1480         if (shell_surface == NULL)
1481                 return;
1482
1483         move_surface_to_workspace(shell, shell_surface, workspace);
1484 }
1485
1486 static const struct workspace_manager_interface workspace_manager_implementation = {
1487         workspace_manager_move_surface,
1488 };
1489
1490 static void
1491 unbind_resource(struct wl_resource *resource)
1492 {
1493         wl_list_remove(wl_resource_get_link(resource));
1494 }
1495
1496 static void
1497 bind_workspace_manager(struct wl_client *client,
1498                        void *data, uint32_t version, uint32_t id)
1499 {
1500         struct desktop_shell *shell = data;
1501         struct wl_resource *resource;
1502
1503         resource = wl_resource_create(client,
1504                                       &workspace_manager_interface, 1, id);
1505
1506         if (resource == NULL) {
1507                 weston_log("couldn't add workspace manager object");
1508                 return;
1509         }
1510
1511         wl_resource_set_implementation(resource,
1512                                        &workspace_manager_implementation,
1513                                        shell, unbind_resource);
1514         wl_list_insert(&shell->workspaces.client_list,
1515                        wl_resource_get_link(resource));
1516
1517         workspace_manager_send_state(resource,
1518                                      shell->workspaces.current,
1519                                      shell->workspaces.num);
1520 }
1521
1522 static void
1523 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1524                      int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1525 {
1526 }
1527
1528 static void
1529 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1530 {
1531         struct weston_touch_move_grab *move =
1532                 (struct weston_touch_move_grab *) container_of(
1533                         grab, struct shell_touch_grab, grab);
1534
1535         if (touch_id == 0)
1536                 move->active = 0;
1537
1538         if (grab->touch->num_tp == 0) {
1539                 shell_touch_grab_end(&move->base);
1540                 free(move);
1541         }
1542 }
1543
1544 static void
1545 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1546                        int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1547 {
1548         struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1549         struct shell_surface *shsurf = move->base.shsurf;
1550         struct weston_surface *es;
1551         int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1552         int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1553
1554         if (!shsurf || !move->active)
1555                 return;
1556
1557         es = shsurf->surface;
1558
1559         weston_view_set_position(shsurf->view, dx, dy);
1560
1561         weston_compositor_schedule_repaint(es->compositor);
1562 }
1563
1564 static void
1565 touch_move_grab_frame(struct weston_touch_grab *grab)
1566 {
1567 }
1568
1569 static void
1570 touch_move_grab_cancel(struct weston_touch_grab *grab)
1571 {
1572         struct weston_touch_move_grab *move =
1573                 (struct weston_touch_move_grab *) container_of(
1574                         grab, struct shell_touch_grab, grab);
1575
1576         shell_touch_grab_end(&move->base);
1577         free(move);
1578 }
1579
1580 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1581         touch_move_grab_down,
1582         touch_move_grab_up,
1583         touch_move_grab_motion,
1584         touch_move_grab_frame,
1585         touch_move_grab_cancel,
1586 };
1587
1588 static int
1589 surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1590 {
1591         struct weston_touch_move_grab *move;
1592
1593         if (!shsurf)
1594                 return -1;
1595
1596         if (shsurf->state.fullscreen || shsurf->state.maximized)
1597                 return 0;
1598
1599         move = malloc(sizeof *move);
1600         if (!move)
1601                 return -1;
1602
1603         move->active = 1;
1604         move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1605                         seat->touch->grab_x;
1606         move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1607                         seat->touch->grab_y;
1608
1609         shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1610                                seat->touch);
1611
1612         return 0;
1613 }
1614
1615 static void
1616 noop_grab_focus(struct weston_pointer_grab *grab)
1617 {
1618 }
1619
1620 static void
1621 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1622 {
1623         struct shell_surface *shsurf = move->base.shsurf;
1624         struct weston_pointer *pointer = move->base.grab.pointer;
1625         int x, y, panel_width, panel_height, bottom;
1626         const int safety = 50;
1627
1628         x = wl_fixed_to_int(pointer->x + move->dx);
1629         y = wl_fixed_to_int(pointer->y + move->dy);
1630
1631         if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1632                 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1633                                       &panel_width, &panel_height);
1634
1635                 bottom = y + shsurf->geometry.height;
1636                 if (bottom - panel_height < safety)
1637                         y = panel_height + safety -
1638                                 shsurf->geometry.height;
1639
1640                 if (move->client_initiated &&
1641                     y + shsurf->geometry.y < panel_height)
1642                         y = panel_height - shsurf->geometry.y;
1643         }
1644
1645         *cx = x;
1646         *cy = y;
1647 }
1648
1649 static void
1650 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1651                  wl_fixed_t x, wl_fixed_t y)
1652 {
1653         struct weston_move_grab *move = (struct weston_move_grab *) grab;
1654         struct weston_pointer *pointer = grab->pointer;
1655         struct shell_surface *shsurf = move->base.shsurf;
1656         int cx, cy;
1657
1658         weston_pointer_move(pointer, x, y);
1659         if (!shsurf)
1660                 return;
1661
1662         constrain_position(move, &cx, &cy);
1663
1664         weston_view_set_position(shsurf->view, cx, cy);
1665
1666         weston_compositor_schedule_repaint(shsurf->surface->compositor);
1667 }
1668
1669 static void
1670 move_grab_button(struct weston_pointer_grab *grab,
1671                  uint32_t time, uint32_t button, uint32_t state_w)
1672 {
1673         struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1674                                                     grab);
1675         struct weston_pointer *pointer = grab->pointer;
1676         enum wl_pointer_button_state state = state_w;
1677
1678         if (pointer->button_count == 0 &&
1679             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1680                 shell_grab_end(shell_grab);
1681                 free(grab);
1682         }
1683 }
1684
1685 static void
1686 move_grab_cancel(struct weston_pointer_grab *grab)
1687 {
1688         struct shell_grab *shell_grab =
1689                 container_of(grab, struct shell_grab, grab);
1690
1691         shell_grab_end(shell_grab);
1692         free(grab);
1693 }
1694
1695 static const struct weston_pointer_grab_interface move_grab_interface = {
1696         noop_grab_focus,
1697         move_grab_motion,
1698         move_grab_button,
1699         move_grab_cancel,
1700 };
1701
1702 static int
1703 surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1704              int client_initiated)
1705 {
1706         struct weston_move_grab *move;
1707
1708         if (!shsurf)
1709                 return -1;
1710
1711         if (shsurf->grabbed ||
1712             shsurf->state.fullscreen || shsurf->state.maximized)
1713                 return 0;
1714
1715         move = malloc(sizeof *move);
1716         if (!move)
1717                 return -1;
1718
1719         move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1720                         seat->pointer->grab_x;
1721         move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1722                         seat->pointer->grab_y;
1723         move->client_initiated = client_initiated;
1724
1725         shell_grab_start(&move->base, &move_grab_interface, shsurf,
1726                          seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
1727
1728         return 0;
1729 }
1730
1731 static void
1732 common_surface_move(struct wl_resource *resource,
1733                     struct wl_resource *seat_resource, uint32_t serial)
1734 {
1735         struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1736         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1737         struct weston_surface *surface;
1738
1739         if (seat->pointer &&
1740             seat->pointer->focus &&
1741             seat->pointer->button_count > 0 &&
1742             seat->pointer->grab_serial == serial) {
1743                 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1744                 if ((surface == shsurf->surface) &&
1745                     (surface_move(shsurf, seat, 1) < 0))
1746                         wl_resource_post_no_memory(resource);
1747         } else if (seat->touch &&
1748                    seat->touch->focus &&
1749                    seat->touch->grab_serial == serial) {
1750                 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
1751                 if ((surface == shsurf->surface) &&
1752                     (surface_touch_move(shsurf, seat) < 0))
1753                         wl_resource_post_no_memory(resource);
1754         }
1755 }
1756
1757 static void
1758 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1759                    struct wl_resource *seat_resource, uint32_t serial)
1760 {
1761         common_surface_move(resource, seat_resource, serial);
1762 }
1763
1764 struct weston_resize_grab {
1765         struct shell_grab base;
1766         uint32_t edges;
1767         int32_t width, height;
1768 };
1769
1770 static void
1771 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1772                    wl_fixed_t x, wl_fixed_t y)
1773 {
1774         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1775         struct weston_pointer *pointer = grab->pointer;
1776         struct shell_surface *shsurf = resize->base.shsurf;
1777         int32_t width, height;
1778         wl_fixed_t from_x, from_y;
1779         wl_fixed_t to_x, to_y;
1780
1781         weston_pointer_move(pointer, x, y);
1782
1783         if (!shsurf)
1784                 return;
1785
1786         weston_view_from_global_fixed(shsurf->view,
1787                                       pointer->grab_x, pointer->grab_y,
1788                                       &from_x, &from_y);
1789         weston_view_from_global_fixed(shsurf->view,
1790                                       pointer->x, pointer->y, &to_x, &to_y);
1791
1792         width = resize->width;
1793         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1794                 width += wl_fixed_to_int(from_x - to_x);
1795         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1796                 width += wl_fixed_to_int(to_x - from_x);
1797         }
1798
1799         height = resize->height;
1800         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1801                 height += wl_fixed_to_int(from_y - to_y);
1802         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1803                 height += wl_fixed_to_int(to_y - from_y);
1804         }
1805
1806         shsurf->client->send_configure(shsurf->surface, width, height);
1807 }
1808
1809 static void
1810 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
1811 {
1812         struct shell_surface *shsurf = get_shell_surface(surface);
1813
1814         assert(shsurf);
1815
1816         if (shsurf->resource)
1817                 wl_shell_surface_send_configure(shsurf->resource,
1818                                                 shsurf->resize_edges,
1819                                                 width, height);
1820 }
1821
1822 static const struct weston_shell_client shell_client = {
1823         send_configure
1824 };
1825
1826 static void
1827 resize_grab_button(struct weston_pointer_grab *grab,
1828                    uint32_t time, uint32_t button, uint32_t state_w)
1829 {
1830         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1831         struct weston_pointer *pointer = grab->pointer;
1832         enum wl_pointer_button_state state = state_w;
1833
1834         if (pointer->button_count == 0 &&
1835             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1836                 shell_grab_end(&resize->base);
1837                 free(grab);
1838         }
1839 }
1840
1841 static void
1842 resize_grab_cancel(struct weston_pointer_grab *grab)
1843 {
1844         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1845
1846         shell_grab_end(&resize->base);
1847         free(grab);
1848 }
1849
1850 static const struct weston_pointer_grab_interface resize_grab_interface = {
1851         noop_grab_focus,
1852         resize_grab_motion,
1853         resize_grab_button,
1854         resize_grab_cancel,
1855 };
1856
1857 /*
1858  * Returns the bounding box of a surface and all its sub-surfaces,
1859  * in the surface coordinates system. */
1860 static void
1861 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1862                                 int32_t *y, int32_t *w, int32_t *h) {
1863         pixman_region32_t region;
1864         pixman_box32_t *box;
1865         struct weston_subsurface *subsurface;
1866
1867         pixman_region32_init_rect(&region, 0, 0,
1868                                   surface->width,
1869                                   surface->height);
1870
1871         wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1872                 pixman_region32_union_rect(&region, &region,
1873                                            subsurface->position.x,
1874                                            subsurface->position.y,
1875                                            subsurface->surface->width,
1876                                            subsurface->surface->height);
1877         }
1878
1879         box = pixman_region32_extents(&region);
1880         if (x)
1881                 *x = box->x1;
1882         if (y)
1883                 *y = box->y1;
1884         if (w)
1885                 *w = box->x2 - box->x1;
1886         if (h)
1887                 *h = box->y2 - box->y1;
1888
1889         pixman_region32_fini(&region);
1890 }
1891
1892 static int
1893 surface_resize(struct shell_surface *shsurf,
1894                struct weston_seat *seat, uint32_t edges)
1895 {
1896         struct weston_resize_grab *resize;
1897         const unsigned resize_topbottom =
1898                 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1899         const unsigned resize_leftright =
1900                 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1901         const unsigned resize_any = resize_topbottom | resize_leftright;
1902
1903         if (shsurf->grabbed ||
1904             shsurf->state.fullscreen || shsurf->state.maximized)
1905                 return 0;
1906
1907         /* Check for invalid edge combinations. */
1908         if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1909             (edges & resize_topbottom) == resize_topbottom ||
1910             (edges & resize_leftright) == resize_leftright)
1911                 return 0;
1912
1913         resize = malloc(sizeof *resize);
1914         if (!resize)
1915                 return -1;
1916
1917         resize->edges = edges;
1918
1919         resize->width = shsurf->geometry.width;
1920         resize->height = shsurf->geometry.height;
1921
1922         shsurf->resize_edges = edges;
1923         shell_surface_state_changed(shsurf);
1924         shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1925                          seat->pointer, edges);
1926
1927         return 0;
1928 }
1929
1930 static void
1931 common_surface_resize(struct wl_resource *resource,
1932                       struct wl_resource *seat_resource, uint32_t serial,
1933                       uint32_t edges)
1934 {
1935         struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1936         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1937         struct weston_surface *surface;
1938
1939         if (seat->pointer == NULL ||
1940             seat->pointer->button_count == 0 ||
1941             seat->pointer->grab_serial != serial ||
1942             seat->pointer->focus == NULL)
1943                 return;
1944
1945         surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1946         if (surface != shsurf->surface)
1947                 return;
1948
1949         if (surface_resize(shsurf, seat, edges) < 0)
1950                 wl_resource_post_no_memory(resource);
1951 }
1952
1953 static void
1954 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1955                      struct wl_resource *seat_resource, uint32_t serial,
1956                      uint32_t edges)
1957 {
1958         common_surface_resize(resource, seat_resource, serial, edges);
1959 }
1960
1961 static void
1962 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1963 {
1964         struct shell_grab *grab = (struct shell_grab *) base;
1965         struct weston_pointer *pointer = base->pointer;
1966         struct weston_view *view;
1967         wl_fixed_t sx, sy;
1968
1969         view = weston_compositor_pick_view(pointer->seat->compositor,
1970                                            pointer->x, pointer->y,
1971                                            &sx, &sy);
1972
1973         if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1974                 shell_grab_end(grab);
1975                 free(grab);
1976         }
1977 }
1978
1979 static void
1980 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1981                         wl_fixed_t x, wl_fixed_t y)
1982 {
1983         weston_pointer_move(grab->pointer, x, y);
1984 }
1985
1986 static void
1987 busy_cursor_grab_button(struct weston_pointer_grab *base,
1988                         uint32_t time, uint32_t button, uint32_t state)
1989 {
1990         struct shell_grab *grab = (struct shell_grab *) base;
1991         struct shell_surface *shsurf = grab->shsurf;
1992         struct weston_seat *seat = grab->grab.pointer->seat;
1993
1994         if (shsurf && button == BTN_LEFT && state) {
1995                 activate(shsurf->shell, shsurf->surface, seat, true);
1996                 surface_move(shsurf, seat, 0);
1997         } else if (shsurf && button == BTN_RIGHT && state) {
1998                 activate(shsurf->shell, shsurf->surface, seat, true);
1999                 surface_rotate(shsurf, seat);
2000         }
2001 }
2002
2003 static void
2004 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2005 {
2006         struct shell_grab *grab = (struct shell_grab *) base;
2007
2008         shell_grab_end(grab);
2009         free(grab);
2010 }
2011
2012 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
2013         busy_cursor_grab_focus,
2014         busy_cursor_grab_motion,
2015         busy_cursor_grab_button,
2016         busy_cursor_grab_cancel,
2017 };
2018
2019 static void
2020 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2021 {
2022         struct shell_grab *grab;
2023
2024         if (pointer->grab->interface == &busy_cursor_grab_interface)
2025                 return;
2026
2027         grab = malloc(sizeof *grab);
2028         if (!grab)
2029                 return;
2030
2031         shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2032                          DESKTOP_SHELL_CURSOR_BUSY);
2033         /* Mark the shsurf as ungrabbed so that button binding is able
2034          * to move it. */
2035         shsurf->grabbed = 0;
2036 }
2037
2038 static void
2039 end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
2040 {
2041         struct shell_grab *grab;
2042         struct weston_seat *seat;
2043
2044         wl_list_for_each(seat, &compositor->seat_list, link) {
2045                 if (seat->pointer == NULL)
2046                         continue;
2047
2048                 grab = (struct shell_grab *) seat->pointer->grab;
2049                 if (grab->grab.interface == &busy_cursor_grab_interface &&
2050                     wl_resource_get_client(grab->shsurf->resource) == client) {
2051                         shell_grab_end(grab);
2052                         free(grab);
2053                 }
2054         }
2055 }
2056
2057 static void
2058 handle_shell_client_destroy(struct wl_listener *listener, void *data);
2059
2060 static int
2061 xdg_ping_timeout_handler(void *data)
2062 {
2063         struct shell_client *sc = data;
2064         struct weston_seat *seat;
2065         struct shell_surface *shsurf;
2066
2067         /* Client is not responding */
2068         sc->unresponsive = 1;
2069         wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2070                 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2071                         continue;
2072                 if (seat->pointer->focus->surface->resource == NULL)
2073                         continue;
2074                 
2075                 shsurf = get_shell_surface(seat->pointer->focus->surface);
2076                 if (shsurf &&
2077                     wl_resource_get_client(shsurf->resource) == sc->client)
2078                         set_busy_cursor(shsurf, seat->pointer);
2079         }
2080
2081         return 1;
2082 }
2083
2084 static void
2085 handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2086 {
2087         struct weston_compositor *compositor = shsurf->shell->compositor;
2088         struct shell_client *sc = shsurf->owner;
2089         struct wl_event_loop *loop;
2090         static const int ping_timeout = 200;
2091
2092         if (sc->unresponsive) {
2093                 xdg_ping_timeout_handler(sc);
2094                 return;
2095         }
2096
2097         sc->ping_serial = serial;
2098         loop = wl_display_get_event_loop(compositor->wl_display);
2099         if (sc->ping_timer == NULL)
2100                 sc->ping_timer =
2101                         wl_event_loop_add_timer(loop,
2102                                                 xdg_ping_timeout_handler, sc);
2103         if (sc->ping_timer == NULL)
2104                 return;
2105
2106         wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2107
2108         if (shell_surface_is_xdg_surface(shsurf) ||
2109             shell_surface_is_xdg_popup(shsurf))
2110                 xdg_shell_send_ping(sc->resource, serial);
2111         else if (shell_surface_is_wl_shell_surface(shsurf))
2112                 wl_shell_surface_send_ping(shsurf->resource, serial);
2113 }
2114
2115 static void
2116 ping_handler(struct weston_surface *surface, uint32_t serial)
2117 {
2118         struct shell_surface *shsurf = get_shell_surface(surface);
2119
2120         if (!shsurf)
2121                 return;
2122         if (!shsurf->resource)
2123                 return;
2124         if (shsurf->surface == shsurf->shell->grab_surface)
2125                 return;
2126
2127         handle_xdg_ping(shsurf, serial);
2128 }
2129
2130 static void
2131 handle_pointer_focus(struct wl_listener *listener, void *data)
2132 {
2133         struct weston_pointer *pointer = data;
2134         struct weston_view *view = pointer->focus;
2135         struct weston_compositor *compositor;
2136         uint32_t serial;
2137
2138         if (!view)
2139                 return;
2140
2141         compositor = view->surface->compositor;
2142         serial = wl_display_next_serial(compositor->wl_display);
2143         ping_handler(view->surface, serial);
2144 }
2145
2146 static void
2147 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2148 {
2149         if (--shsurf->focus_count == 0)
2150                 shell_surface_state_changed(shsurf);
2151 }
2152
2153 static void
2154 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2155 {
2156         if (shsurf->focus_count++ == 0)
2157                 shell_surface_state_changed(shsurf);
2158 }
2159
2160 static void
2161 handle_keyboard_focus(struct wl_listener *listener, void *data)
2162 {
2163         struct weston_keyboard *keyboard = data;
2164         struct shell_seat *seat = get_shell_seat(keyboard->seat);
2165
2166         if (seat->focused_surface) {
2167                 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2168                 if (shsurf)
2169                         shell_surface_lose_keyboard_focus(shsurf);
2170         }
2171
2172         seat->focused_surface = keyboard->focus;
2173
2174         if (seat->focused_surface) {
2175                 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2176                 if (shsurf)
2177                         shell_surface_gain_keyboard_focus(shsurf);
2178         }
2179 }
2180
2181 static void
2182 shell_client_pong(struct shell_client *sc, uint32_t serial)
2183 {
2184         if (sc->ping_serial != serial)
2185                 return;
2186
2187         sc->unresponsive = 0;
2188         end_busy_cursor(sc->shell->compositor, sc->client);
2189
2190         if (sc->ping_timer) {
2191                 wl_event_source_remove(sc->ping_timer);
2192                 sc->ping_timer = NULL;
2193         }
2194
2195 }
2196
2197 static void
2198 shell_surface_pong(struct wl_client *client,
2199                    struct wl_resource *resource, uint32_t serial)
2200 {
2201         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2202         struct shell_client *sc = shsurf->owner;
2203
2204         shell_client_pong(sc, serial);
2205 }
2206
2207 static void
2208 set_title(struct shell_surface *shsurf, const char *title)
2209 {
2210         free(shsurf->title);
2211         shsurf->title = strdup(title);
2212 }
2213
2214 static void
2215 set_window_geometry(struct shell_surface *shsurf,
2216                     int32_t x, int32_t y, int32_t width, int32_t height)
2217 {
2218         shsurf->next_geometry.x = x;
2219         shsurf->next_geometry.y = y;
2220         shsurf->next_geometry.width = width;
2221         shsurf->next_geometry.height = height;
2222         shsurf->has_next_geometry = true;
2223 }
2224
2225 static void
2226 shell_surface_set_title(struct wl_client *client,
2227                         struct wl_resource *resource, const char *title)
2228 {
2229         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2230
2231         set_title(shsurf, title);
2232 }
2233
2234 static void
2235 shell_surface_set_class(struct wl_client *client,
2236                         struct wl_resource *resource, const char *class)
2237 {
2238         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2239
2240         free(shsurf->class);
2241         shsurf->class = strdup(class);
2242 }
2243
2244 static void
2245 restore_output_mode(struct weston_output *output)
2246 {
2247         if (output->current_mode != output->original_mode ||
2248             (int32_t)output->current_scale != output->original_scale)
2249                 weston_output_switch_mode(output,
2250                                           output->original_mode,
2251                                           output->original_scale,
2252                                           WESTON_MODE_SWITCH_RESTORE_NATIVE);
2253 }
2254
2255 static void
2256 restore_all_output_modes(struct weston_compositor *compositor)
2257 {
2258         struct weston_output *output;
2259
2260         wl_list_for_each(output, &compositor->output_list, link)
2261                 restore_output_mode(output);
2262 }
2263
2264 /* The surface will be inserted into the list immediately after the link
2265  * returned by this function (i.e. will be stacked immediately above the
2266  * returned link). */
2267 static struct weston_layer_entry *
2268 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2269 {
2270         struct workspace *ws;
2271         struct weston_view *parent;
2272
2273         switch (shsurf->type) {
2274         case SHELL_SURFACE_XWAYLAND:
2275                 return &shsurf->shell->fullscreen_layer.view_list;
2276
2277         case SHELL_SURFACE_NONE:
2278                 return NULL;
2279
2280         case SHELL_SURFACE_POPUP:
2281         case SHELL_SURFACE_TOPLEVEL:
2282                 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
2283                         return &shsurf->shell->fullscreen_layer.view_list;
2284                 } else if (shsurf->parent) {
2285                         /* Move the surface to its parent layer so
2286                          * that surfaces which are transient for
2287                          * fullscreen surfaces don't get hidden by the
2288                          * fullscreen surfaces. */
2289
2290                         /* TODO: Handle a parent with multiple views */
2291                         parent = get_default_view(shsurf->parent);
2292                         if (parent)
2293                                 return container_of(parent->layer_link.link.prev,
2294                                                     struct weston_layer_entry, link);
2295                 }
2296
2297                 /* Move the surface to a normal workspace layer so that surfaces
2298                  * which were previously fullscreen or transient are no longer
2299                  * rendered on top. */
2300                 ws = get_current_workspace(shsurf->shell);
2301                 return &ws->layer.view_list;
2302         }
2303
2304         assert(0 && "Unknown shell surface type");
2305 }
2306
2307 static void
2308 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2309 {
2310         struct shell_surface *child;
2311         struct weston_layer_entry *prev;
2312
2313         /* Move the child layers to the same workspace as shsurf. They will be
2314          * stacked above shsurf. */
2315         wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2316                 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
2317                         weston_view_damage_below(child->view);
2318                         weston_view_geometry_dirty(child->view);
2319                         prev = container_of(shsurf->view->layer_link.link.prev,
2320                                             struct weston_layer_entry, link);
2321                         weston_layer_entry_remove(&child->view->layer_link);
2322                         weston_layer_entry_insert(prev,
2323                                                   &child->view->layer_link);
2324                         weston_view_geometry_dirty(child->view);
2325                         weston_surface_damage(child->surface);
2326
2327                         /* Recurse. We don’t expect this to recurse very far (if
2328                          * at all) because that would imply we have transient
2329                          * (or popup) children of transient surfaces, which
2330                          * would be unusual. */
2331                         shell_surface_update_child_surface_layers(child);
2332                 }
2333         }
2334 }
2335
2336 /* Update the surface’s layer. Mark both the old and new views as having dirty
2337  * geometry to ensure the changes are redrawn.
2338  *
2339  * If any child surfaces exist and are mapped, ensure they’re in the same layer
2340  * as this surface. */
2341 static void
2342 shell_surface_update_layer(struct shell_surface *shsurf)
2343 {
2344         struct weston_layer_entry *new_layer_link;
2345
2346         new_layer_link = shell_surface_calculate_layer_link(shsurf);
2347
2348         if (new_layer_link == NULL)
2349                 return;
2350         if (new_layer_link == &shsurf->view->layer_link)
2351                 return;
2352
2353         weston_view_geometry_dirty(shsurf->view);
2354         weston_layer_entry_remove(&shsurf->view->layer_link);
2355         weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
2356         weston_view_geometry_dirty(shsurf->view);
2357         weston_surface_damage(shsurf->surface);
2358
2359         shell_surface_update_child_surface_layers(shsurf);
2360 }
2361
2362 static void
2363 shell_surface_set_parent(struct shell_surface *shsurf,
2364                          struct weston_surface *parent)
2365 {
2366         shsurf->parent = parent;
2367
2368         wl_list_remove(&shsurf->children_link);
2369         wl_list_init(&shsurf->children_link);
2370
2371         /* Insert into the parent surface’s child list. */
2372         if (parent != NULL) {
2373                 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2374                 if (parent_shsurf != NULL)
2375                         wl_list_insert(&parent_shsurf->children_list,
2376                                        &shsurf->children_link);
2377         }
2378 }
2379
2380 static void
2381 shell_surface_set_output(struct shell_surface *shsurf,
2382                          struct weston_output *output)
2383 {
2384         struct weston_surface *es = shsurf->surface;
2385
2386         /* get the default output, if the client set it as NULL
2387            check whether the ouput is available */
2388         if (output)
2389                 shsurf->output = output;
2390         else if (es->output)
2391                 shsurf->output = es->output;
2392         else
2393                 shsurf->output = get_default_output(es->compositor);
2394 }
2395
2396 static void
2397 surface_clear_next_states(struct shell_surface *shsurf)
2398 {
2399         shsurf->next_state.maximized = false;
2400         shsurf->next_state.fullscreen = false;
2401
2402         if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2403             (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2404                 shsurf->state_changed = true;
2405 }
2406
2407 static void
2408 set_toplevel(struct shell_surface *shsurf)
2409 {
2410         shell_surface_set_parent(shsurf, NULL);
2411         surface_clear_next_states(shsurf);
2412         shsurf->type = SHELL_SURFACE_TOPLEVEL;
2413
2414         /* The layer_link is updated in set_surface_type(),
2415          * called from configure. */
2416 }
2417
2418 static void
2419 shell_surface_set_toplevel(struct wl_client *client,
2420                            struct wl_resource *resource)
2421 {
2422         struct shell_surface *surface = wl_resource_get_user_data(resource);
2423
2424         set_toplevel(surface);
2425 }
2426
2427 static void
2428 set_transient(struct shell_surface *shsurf,
2429               struct weston_surface *parent, int x, int y, uint32_t flags)
2430 {
2431         assert(parent != NULL);
2432
2433         shell_surface_set_parent(shsurf, parent);
2434
2435         surface_clear_next_states(shsurf);
2436
2437         shsurf->transient.x = x;
2438         shsurf->transient.y = y;
2439         shsurf->transient.flags = flags;
2440
2441         shsurf->next_state.relative = true;
2442         shsurf->state_changed = true;
2443         shsurf->type = SHELL_SURFACE_TOPLEVEL;
2444
2445         /* The layer_link is updated in set_surface_type(),
2446          * called from configure. */
2447 }
2448
2449 static void
2450 shell_surface_set_transient(struct wl_client *client,
2451                             struct wl_resource *resource,
2452                             struct wl_resource *parent_resource,
2453                             int x, int y, uint32_t flags)
2454 {
2455         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2456         struct weston_surface *parent =
2457                 wl_resource_get_user_data(parent_resource);
2458
2459         set_transient(shsurf, parent, x, y, flags);
2460 }
2461
2462 static void
2463 set_fullscreen(struct shell_surface *shsurf,
2464                uint32_t method,
2465                uint32_t framerate,
2466                struct weston_output *output)
2467 {
2468         shell_surface_set_output(shsurf, output);
2469         shsurf->type = SHELL_SURFACE_TOPLEVEL;
2470
2471         shsurf->fullscreen_output = shsurf->output;
2472         shsurf->fullscreen.type = method;
2473         shsurf->fullscreen.framerate = framerate;
2474
2475         send_configure_for_surface(shsurf);
2476 }
2477
2478 static void
2479 weston_view_set_initial_position(struct weston_view *view,
2480                                  struct desktop_shell *shell);
2481
2482 static void
2483 unset_fullscreen(struct shell_surface *shsurf)
2484 {
2485         /* Unset the fullscreen output, driver configuration and transforms. */
2486         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2487             shell_surface_is_top_fullscreen(shsurf)) {
2488                 restore_output_mode(shsurf->fullscreen_output);
2489         }
2490
2491         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2492         shsurf->fullscreen.framerate = 0;
2493
2494         wl_list_remove(&shsurf->fullscreen.transform.link);
2495         wl_list_init(&shsurf->fullscreen.transform.link);
2496
2497         if (shsurf->fullscreen.black_view)
2498                 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2499         shsurf->fullscreen.black_view = NULL;
2500
2501         if (shsurf->saved_position_valid)
2502                 weston_view_set_position(shsurf->view,
2503                                          shsurf->saved_x, shsurf->saved_y);
2504         else
2505                 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2506
2507         if (shsurf->saved_rotation_valid) {
2508                 wl_list_insert(&shsurf->view->geometry.transformation_list,
2509                                &shsurf->rotation.transform.link);
2510                 shsurf->saved_rotation_valid = false;
2511         }
2512
2513         /* Layer is updated in set_surface_type(). */
2514 }
2515
2516 static void
2517 shell_surface_set_fullscreen(struct wl_client *client,
2518                              struct wl_resource *resource,
2519                              uint32_t method,
2520                              uint32_t framerate,
2521                              struct wl_resource *output_resource)
2522 {
2523         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2524         struct weston_output *output;
2525
2526         if (output_resource)
2527                 output = wl_resource_get_user_data(output_resource);
2528         else
2529                 output = NULL;
2530
2531         shell_surface_set_parent(shsurf, NULL);
2532
2533         surface_clear_next_states(shsurf);
2534         shsurf->next_state.fullscreen = true;
2535         shsurf->state_changed = true;
2536         set_fullscreen(shsurf, method, framerate, output);
2537 }
2538
2539 static void
2540 set_popup(struct shell_surface *shsurf,
2541           struct weston_surface *parent,
2542           struct weston_seat *seat,
2543           uint32_t serial,
2544           int32_t x,
2545           int32_t y)
2546 {
2547         assert(parent != NULL);
2548
2549         shsurf->popup.shseat = get_shell_seat(seat);
2550         shsurf->popup.serial = serial;
2551         shsurf->popup.x = x;
2552         shsurf->popup.y = y;
2553
2554         shsurf->type = SHELL_SURFACE_POPUP;
2555 }
2556
2557 static void
2558 shell_surface_set_popup(struct wl_client *client,
2559                         struct wl_resource *resource,
2560                         struct wl_resource *seat_resource,
2561                         uint32_t serial,
2562                         struct wl_resource *parent_resource,
2563                         int32_t x, int32_t y, uint32_t flags)
2564 {
2565         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2566         struct weston_surface *parent =
2567                 wl_resource_get_user_data(parent_resource);
2568
2569         shell_surface_set_parent(shsurf, parent);
2570
2571         surface_clear_next_states(shsurf);
2572         set_popup(shsurf,
2573                   parent,
2574                   wl_resource_get_user_data(seat_resource),
2575                   serial, x, y);
2576 }
2577
2578 static void
2579 unset_maximized(struct shell_surface *shsurf)
2580 {
2581         /* undo all maximized things here */
2582         shsurf->output = get_default_output(shsurf->surface->compositor);
2583
2584         if (shsurf->saved_position_valid)
2585                 weston_view_set_position(shsurf->view,
2586                                          shsurf->saved_x, shsurf->saved_y);
2587         else
2588                 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2589
2590         if (shsurf->saved_rotation_valid) {
2591                 wl_list_insert(&shsurf->view->geometry.transformation_list,
2592                                &shsurf->rotation.transform.link);
2593                 shsurf->saved_rotation_valid = false;
2594         }
2595
2596         /* Layer is updated in set_surface_type(). */
2597 }
2598
2599 static void
2600 set_minimized(struct weston_surface *surface, uint32_t is_true)
2601 {
2602         struct shell_surface *shsurf;
2603         struct workspace *current_ws;
2604         struct weston_seat *seat;
2605         struct weston_surface *focus;
2606         struct weston_view *view;
2607
2608         view = get_default_view(surface);
2609         if (!view)
2610                 return;
2611
2612         assert(weston_surface_get_main_surface(view->surface) == view->surface);
2613
2614         shsurf = get_shell_surface(surface);
2615         current_ws = get_current_workspace(shsurf->shell);
2616
2617         weston_layer_entry_remove(&view->layer_link);
2618          /* hide or show, depending on the state */
2619         if (is_true) {
2620                 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2621
2622                 drop_focus_state(shsurf->shell, current_ws, view->surface);
2623                 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2624                         if (!seat->keyboard)
2625                                 continue;
2626                         focus = weston_surface_get_main_surface(seat->keyboard->focus);
2627                         if (focus == view->surface)
2628                                 weston_keyboard_set_focus(seat->keyboard, NULL);
2629                 }
2630         }
2631         else {
2632                 weston_layer_entry_insert(&current_ws->layer.view_list, &view->layer_link);
2633
2634                 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2635                         if (!seat->keyboard)
2636                                 continue;
2637                         activate(shsurf->shell, view->surface, seat, true);
2638                 }
2639         }
2640
2641         shell_surface_update_child_surface_layers(shsurf);
2642
2643         weston_view_damage_below(view);
2644 }
2645
2646 static void
2647 shell_surface_set_maximized(struct wl_client *client,
2648                             struct wl_resource *resource,
2649                             struct wl_resource *output_resource)
2650 {
2651         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2652         struct weston_output *output;
2653
2654         surface_clear_next_states(shsurf);
2655         shsurf->next_state.maximized = true;
2656         shsurf->state_changed = true;
2657
2658         shsurf->type = SHELL_SURFACE_TOPLEVEL;
2659         shell_surface_set_parent(shsurf, NULL);
2660
2661         if (output_resource)
2662                 output = wl_resource_get_user_data(output_resource);
2663         else
2664                 output = NULL;
2665
2666         shell_surface_set_output(shsurf, output);
2667
2668         send_configure_for_surface(shsurf);
2669 }
2670
2671 /* This is only ever called from set_surface_type(), so there’s no need to
2672  * update layer_links here, since they’ll be updated when we return. */
2673 static int
2674 reset_surface_type(struct shell_surface *surface)
2675 {
2676         if (surface->state.fullscreen)
2677                 unset_fullscreen(surface);
2678         if (surface->state.maximized)
2679                 unset_maximized(surface);
2680
2681         return 0;
2682 }
2683
2684 static void
2685 set_full_output(struct shell_surface *shsurf)
2686 {
2687         shsurf->saved_x = shsurf->view->geometry.x;
2688         shsurf->saved_y = shsurf->view->geometry.y;
2689         shsurf->saved_width = shsurf->surface->width;
2690         shsurf->saved_height = shsurf->surface->height;
2691         shsurf->saved_size_valid = true;
2692         shsurf->saved_position_valid = true;
2693
2694         if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2695                 wl_list_remove(&shsurf->rotation.transform.link);
2696                 wl_list_init(&shsurf->rotation.transform.link);
2697                 weston_view_geometry_dirty(shsurf->view);
2698                 shsurf->saved_rotation_valid = true;
2699         }
2700 }
2701
2702 static void
2703 set_surface_type(struct shell_surface *shsurf)
2704 {
2705         struct weston_surface *pes = shsurf->parent;
2706         struct weston_view *pev = get_default_view(pes);
2707
2708         reset_surface_type(shsurf);
2709
2710         shsurf->state = shsurf->next_state;
2711         shsurf->state_changed = false;
2712
2713         switch (shsurf->type) {
2714         case SHELL_SURFACE_TOPLEVEL:
2715                 if (shsurf->state.maximized || shsurf->state.fullscreen) {
2716                         set_full_output(shsurf);
2717                 } else if (shsurf->state.relative && pev) {
2718                         weston_view_set_position(shsurf->view,
2719                                                  pev->geometry.x + shsurf->transient.x,
2720                                                  pev->geometry.y + shsurf->transient.y);
2721                 }
2722                 break;
2723
2724         case SHELL_SURFACE_XWAYLAND:
2725                 weston_view_set_position(shsurf->view, shsurf->transient.x,
2726                                          shsurf->transient.y);
2727                 break;
2728
2729         case SHELL_SURFACE_POPUP:
2730         case SHELL_SURFACE_NONE:
2731         default:
2732                 break;
2733         }
2734
2735         /* Update the surface’s layer. */
2736         shell_surface_update_layer(shsurf);
2737 }
2738
2739 static struct desktop_shell *
2740 shell_surface_get_shell(struct shell_surface *shsurf)
2741 {
2742         return shsurf->shell;
2743 }
2744
2745 static void
2746 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
2747
2748 static struct weston_view *
2749 create_black_surface(struct weston_compositor *ec,
2750                      struct weston_surface *fs_surface,
2751                      float x, float y, int w, int h)
2752 {
2753         struct weston_surface *surface = NULL;
2754         struct weston_view *view;
2755
2756         surface = weston_surface_create(ec);
2757         if (surface == NULL) {
2758                 weston_log("no memory\n");
2759                 return NULL;
2760         }
2761         view = weston_view_create(surface);
2762         if (surface == NULL) {
2763                 weston_log("no memory\n");
2764                 weston_surface_destroy(surface);
2765                 return NULL;
2766         }
2767
2768         surface->configure = black_surface_configure;
2769         surface->configure_private = fs_surface;
2770         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2771         pixman_region32_fini(&surface->opaque);
2772         pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2773         pixman_region32_fini(&surface->input);
2774         pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2775
2776         weston_surface_set_size(surface, w, h);
2777         weston_view_set_position(view, x, y);
2778
2779         return view;
2780 }
2781
2782 static void
2783 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2784 {
2785         struct weston_output *output = shsurf->fullscreen_output;
2786
2787         assert(shsurf->state.fullscreen);
2788
2789         if (!shsurf->fullscreen.black_view)
2790                 shsurf->fullscreen.black_view =
2791                         create_black_surface(shsurf->surface->compositor,
2792                                              shsurf->surface,
2793                                              output->x, output->y,
2794                                              output->width,
2795                                              output->height);
2796
2797         weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2798         weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2799         weston_layer_entry_insert(&shsurf->view->layer_link,
2800                                   &shsurf->fullscreen.black_view->layer_link);
2801         weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2802         weston_surface_damage(shsurf->surface);
2803
2804         shsurf->state.lowered = false;
2805 }
2806
2807 /* Create black surface and append it to the associated fullscreen surface.
2808  * Handle size dismatch and positioning according to the method. */
2809 static void
2810 shell_configure_fullscreen(struct shell_surface *shsurf)
2811 {
2812         struct weston_output *output = shsurf->fullscreen_output;
2813         struct weston_surface *surface = shsurf->surface;
2814         struct weston_matrix *matrix;
2815         float scale, output_aspect, surface_aspect, x, y;
2816         int32_t surf_x, surf_y, surf_width, surf_height;
2817
2818         if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2819                 restore_output_mode(output);
2820
2821         /* Reverse the effect of lower_fullscreen_layer() */
2822         weston_layer_entry_remove(&shsurf->view->layer_link);
2823         weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2824
2825         shell_ensure_fullscreen_black_view(shsurf);
2826
2827         surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
2828                                         &surf_width, &surf_height);
2829
2830         switch (shsurf->fullscreen.type) {
2831         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
2832                 if (surface->buffer_ref.buffer)
2833                         center_on_output(shsurf->view, shsurf->fullscreen_output);
2834                 break;
2835         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
2836                 /* 1:1 mapping between surface and output dimensions */
2837                 if (output->width == surf_width &&
2838                         output->height == surf_height) {
2839                         weston_view_set_position(shsurf->view,
2840                                                  output->x - surf_x,
2841                                                  output->y - surf_y);
2842                         break;
2843                 }
2844
2845                 matrix = &shsurf->fullscreen.transform.matrix;
2846                 weston_matrix_init(matrix);
2847
2848                 output_aspect = (float) output->width /
2849                         (float) output->height;
2850                 /* XXX: Use surf_width and surf_height here? */
2851                 surface_aspect = (float) surface->width /
2852                         (float) surface->height;
2853                 if (output_aspect < surface_aspect)
2854                         scale = (float) output->width /
2855                                 (float) surf_width;
2856                 else
2857                         scale = (float) output->height /
2858                                 (float) surf_height;
2859
2860                 weston_matrix_scale(matrix, scale, scale, 1);
2861                 wl_list_remove(&shsurf->fullscreen.transform.link);
2862                 wl_list_insert(&shsurf->view->geometry.transformation_list,
2863                                &shsurf->fullscreen.transform.link);
2864                 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2865                 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
2866                 weston_view_set_position(shsurf->view, x, y);
2867
2868                 break;
2869         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
2870                 if (shell_surface_is_top_fullscreen(shsurf)) {
2871                         struct weston_mode mode = {0,
2872                                 surf_width * surface->buffer_viewport.buffer.scale,
2873                                 surf_height * surface->buffer_viewport.buffer.scale,
2874                                 shsurf->fullscreen.framerate};
2875
2876                         if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
2877                                         WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
2878                                 weston_view_set_position(shsurf->view,
2879                                                          output->x - surf_x,
2880                                                          output->y - surf_y);
2881                                 shsurf->fullscreen.black_view->surface->width = output->width;
2882                                 shsurf->fullscreen.black_view->surface->height = output->height;
2883                                 weston_view_set_position(shsurf->fullscreen.black_view,
2884                                                          output->x - surf_x,
2885                                                          output->y - surf_y);
2886                                 break;
2887                         } else {
2888                                 restore_output_mode(output);
2889                                 center_on_output(shsurf->view, output);
2890                         }
2891                 }
2892                 break;
2893         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
2894                 center_on_output(shsurf->view, output);
2895                 break;
2896         default:
2897                 break;
2898         }
2899 }
2900
2901 static void
2902 shell_map_fullscreen(struct shell_surface *shsurf)
2903 {
2904         shell_configure_fullscreen(shsurf);
2905 }
2906
2907 static void
2908 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2909 {
2910         /* XXX: using the same fields for transient type */
2911         surface_clear_next_states(shsurf);
2912         shsurf->transient.x = x;
2913         shsurf->transient.y = y;
2914         shsurf->transient.flags = flags;
2915
2916         shell_surface_set_parent(shsurf, NULL);
2917
2918         shsurf->type = SHELL_SURFACE_XWAYLAND;
2919         shsurf->state_changed = true;
2920 }
2921
2922 static void
2923 shell_interface_set_fullscreen(struct shell_surface *shsurf,
2924                                uint32_t method,
2925                                uint32_t framerate,
2926                                struct weston_output *output)
2927 {
2928         surface_clear_next_states(shsurf);
2929         set_fullscreen(shsurf, method, framerate, output);
2930
2931         shsurf->next_state.fullscreen = true;
2932         shsurf->state_changed = true;
2933 }
2934
2935 static int
2936 shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2937 {
2938         return surface_move(shsurf, ws, 1);
2939 }
2940
2941 static const struct weston_pointer_grab_interface popup_grab_interface;
2942
2943 static void
2944 destroy_shell_seat(struct wl_listener *listener, void *data)
2945 {
2946         struct shell_seat *shseat =
2947                 container_of(listener,
2948                              struct shell_seat, seat_destroy_listener);
2949         struct shell_surface *shsurf, *prev = NULL;
2950
2951         if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
2952                 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2953                 shseat->popup_grab.client = NULL;
2954
2955                 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2956                         shsurf->popup.shseat = NULL;
2957                         if (prev) {
2958                                 wl_list_init(&prev->popup.grab_link);
2959                         }
2960                         prev = shsurf;
2961                 }
2962                 wl_list_init(&prev->popup.grab_link);
2963         }
2964
2965         wl_list_remove(&shseat->seat_destroy_listener.link);
2966         free(shseat);
2967 }
2968
2969 static void
2970 shell_seat_caps_changed(struct wl_listener *listener, void *data)
2971 {
2972         struct shell_seat *seat;
2973
2974         seat = container_of(listener, struct shell_seat, caps_changed_listener);
2975
2976         if (seat->seat->keyboard &&
2977             wl_list_empty(&seat->keyboard_focus_listener.link)) {
2978                 wl_signal_add(&seat->seat->keyboard->focus_signal,
2979                               &seat->keyboard_focus_listener);
2980         } else if (!seat->seat->keyboard) {
2981                 wl_list_init(&seat->keyboard_focus_listener.link);
2982         }
2983
2984         if (seat->seat->pointer &&
2985             wl_list_empty(&seat->pointer_focus_listener.link)) {
2986                 wl_signal_add(&seat->seat->pointer->focus_signal,
2987                               &seat->pointer_focus_listener);
2988         } else if (!seat->seat->pointer) {
2989                 wl_list_init(&seat->pointer_focus_listener.link);
2990         }
2991 }
2992
2993 static struct shell_seat *
2994 create_shell_seat(struct weston_seat *seat)
2995 {
2996         struct shell_seat *shseat;
2997
2998         shseat = calloc(1, sizeof *shseat);
2999         if (!shseat) {
3000                 weston_log("no memory to allocate shell seat\n");
3001                 return NULL;
3002         }
3003
3004         shseat->seat = seat;
3005         wl_list_init(&shseat->popup_grab.surfaces_list);
3006
3007         shseat->seat_destroy_listener.notify = destroy_shell_seat;
3008         wl_signal_add(&seat->destroy_signal,
3009                       &shseat->seat_destroy_listener);
3010
3011         shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3012         wl_list_init(&shseat->keyboard_focus_listener.link);
3013
3014         shseat->pointer_focus_listener.notify = handle_pointer_focus;
3015         wl_list_init(&shseat->pointer_focus_listener.link);
3016
3017         shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3018         wl_signal_add(&seat->updated_caps_signal,
3019                       &shseat->caps_changed_listener);
3020         shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3021
3022         return shseat;
3023 }
3024
3025 static struct shell_seat *
3026 get_shell_seat(struct weston_seat *seat)
3027 {
3028         struct wl_listener *listener;
3029
3030         listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
3031         assert(listener != NULL);
3032
3033         return container_of(listener,
3034                             struct shell_seat, seat_destroy_listener);
3035 }
3036
3037 static void
3038 popup_grab_focus(struct weston_pointer_grab *grab)
3039 {
3040         struct weston_pointer *pointer = grab->pointer;
3041         struct weston_view *view;
3042         struct shell_seat *shseat =
3043             container_of(grab, struct shell_seat, popup_grab.grab);
3044         struct wl_client *client = shseat->popup_grab.client;
3045         wl_fixed_t sx, sy;
3046
3047         view = weston_compositor_pick_view(pointer->seat->compositor,
3048                                            pointer->x, pointer->y,
3049                                            &sx, &sy);
3050
3051         if (view && view->surface->resource &&
3052             wl_resource_get_client(view->surface->resource) == client) {
3053                 weston_pointer_set_focus(pointer, view, sx, sy);
3054         } else {
3055                 weston_pointer_set_focus(pointer, NULL,
3056                                          wl_fixed_from_int(0),
3057                                          wl_fixed_from_int(0));
3058         }
3059 }
3060
3061 static void
3062 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3063                   wl_fixed_t x, wl_fixed_t y)
3064 {
3065         struct weston_pointer *pointer = grab->pointer;
3066         struct wl_resource *resource;
3067         wl_fixed_t sx, sy;
3068
3069         if (pointer->focus) {
3070                 weston_view_from_global_fixed(pointer->focus, x, y,
3071                                               &pointer->sx, &pointer->sy);
3072         }
3073
3074         weston_pointer_move(pointer, x, y);
3075
3076         wl_resource_for_each(resource, &pointer->focus_resource_list) {
3077                 weston_view_from_global_fixed(pointer->focus,
3078                                               pointer->x, pointer->y,
3079                                               &sx, &sy);
3080                 wl_pointer_send_motion(resource, time, sx, sy);
3081         }
3082 }
3083
3084 static void
3085 popup_grab_button(struct weston_pointer_grab *grab,
3086                   uint32_t time, uint32_t button, uint32_t state_w)
3087 {
3088         struct wl_resource *resource;
3089         struct shell_seat *shseat =
3090             container_of(grab, struct shell_seat, popup_grab.grab);
3091         struct wl_display *display = shseat->seat->compositor->wl_display;
3092         enum wl_pointer_button_state state = state_w;
3093         uint32_t serial;
3094         struct wl_list *resource_list;
3095
3096         resource_list = &grab->pointer->focus_resource_list;
3097         if (!wl_list_empty(resource_list)) {
3098                 serial = wl_display_get_serial(display);
3099                 wl_resource_for_each(resource, resource_list) {
3100                         wl_pointer_send_button(resource, serial,
3101                                                time, button, state);
3102                 }
3103         } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3104                    (shseat->popup_grab.initial_up ||
3105                     time - shseat->seat->pointer->grab_time > 500)) {
3106                 popup_grab_end(grab->pointer);
3107         }
3108
3109         if (state == WL_POINTER_BUTTON_STATE_RELEASED)
3110                 shseat->popup_grab.initial_up = 1;
3111 }
3112
3113 static void
3114 popup_grab_cancel(struct weston_pointer_grab *grab)
3115 {
3116         popup_grab_end(grab->pointer);
3117 }
3118
3119 static const struct weston_pointer_grab_interface popup_grab_interface = {
3120         popup_grab_focus,
3121         popup_grab_motion,
3122         popup_grab_button,
3123         popup_grab_cancel,
3124 };
3125
3126 static void
3127 touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3128                       int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3129 {
3130         struct wl_resource *resource;
3131         struct shell_seat *shseat =
3132             container_of(grab, struct shell_seat, popup_grab.touch_grab);
3133         struct wl_display *display = shseat->seat->compositor->wl_display;
3134         uint32_t serial;
3135         struct wl_list *resource_list;
3136
3137         resource_list = &grab->touch->focus_resource_list;
3138         if (!wl_list_empty(resource_list)) {
3139                 serial = wl_display_get_serial(display);
3140                 wl_resource_for_each(resource, resource_list) {
3141                         wl_touch_send_down(resource, serial, time,
3142                                 grab->touch->focus->surface->resource,
3143                                 touch_id, sx, sy);
3144                 }
3145         }
3146 }
3147
3148 static void
3149 touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3150 {
3151         struct wl_resource *resource;
3152         struct shell_seat *shseat =
3153             container_of(grab, struct shell_seat, popup_grab.touch_grab);
3154         struct wl_display *display = shseat->seat->compositor->wl_display;
3155         uint32_t serial;
3156         struct wl_list *resource_list;
3157
3158         resource_list = &grab->touch->focus_resource_list;
3159         if (!wl_list_empty(resource_list)) {
3160                 serial = wl_display_get_serial(display);
3161                 wl_resource_for_each(resource, resource_list) {
3162                         wl_touch_send_up(resource, serial, time, touch_id);
3163                 }
3164         }
3165 }
3166
3167 static void
3168 touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3169                         int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3170 {
3171         struct wl_resource *resource;
3172         struct wl_list *resource_list;
3173
3174         resource_list = &grab->touch->focus_resource_list;
3175         if (!wl_list_empty(resource_list)) {
3176                 wl_resource_for_each(resource, resource_list) {
3177                         wl_touch_send_motion(resource, time, touch_id, sx, sy);
3178                 }
3179         }
3180 }
3181
3182 static void
3183 touch_popup_grab_frame(struct weston_touch_grab *grab)
3184 {
3185 }
3186
3187 static void
3188 touch_popup_grab_cancel(struct weston_touch_grab *grab)
3189 {
3190         touch_popup_grab_end(grab->touch);
3191 }
3192
3193 static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3194         touch_popup_grab_down,
3195         touch_popup_grab_up,
3196         touch_popup_grab_motion,
3197         touch_popup_grab_frame,
3198         touch_popup_grab_cancel,
3199 };
3200
3201 static void
3202 shell_surface_send_popup_done(struct shell_surface *shsurf)
3203 {
3204         if (shell_surface_is_wl_shell_surface(shsurf))
3205                 wl_shell_surface_send_popup_done(shsurf->resource);
3206         else if (shell_surface_is_xdg_popup(shsurf))
3207                 xdg_popup_send_popup_done(shsurf->resource,
3208                                           shsurf->popup.serial);
3209 }
3210
3211 static void
3212 popup_grab_end(struct weston_pointer *pointer)
3213 {
3214         struct weston_pointer_grab *grab = pointer->grab;
3215         struct shell_seat *shseat =
3216             container_of(grab, struct shell_seat, popup_grab.grab);
3217         struct shell_surface *shsurf;
3218         struct shell_surface *prev = NULL;
3219
3220         if (pointer->grab->interface == &popup_grab_interface) {
3221                 weston_pointer_end_grab(grab->pointer);
3222                 shseat->popup_grab.client = NULL;
3223                 shseat->popup_grab.grab.interface = NULL;
3224                 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3225                 /* Send the popup_done event to all the popups open */
3226                 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3227                         shell_surface_send_popup_done(shsurf);
3228                         shsurf->popup.shseat = NULL;
3229                         if (prev) {
3230                                 wl_list_init(&prev->popup.grab_link);
3231                         }
3232                         prev = shsurf;
3233                 }
3234                 wl_list_init(&prev->popup.grab_link);
3235                 wl_list_init(&shseat->popup_grab.surfaces_list);
3236         }
3237 }
3238
3239 static void
3240 touch_popup_grab_end(struct weston_touch *touch)
3241 {
3242         struct weston_touch_grab *grab = touch->grab;
3243         struct shell_seat *shseat =
3244             container_of(grab, struct shell_seat, popup_grab.touch_grab);
3245         struct shell_surface *shsurf;
3246         struct shell_surface *prev = NULL;
3247
3248         if (touch->grab->interface == &touch_popup_grab_interface) {
3249                 weston_touch_end_grab(grab->touch);
3250                 shseat->popup_grab.client = NULL;
3251                 shseat->popup_grab.touch_grab.interface = NULL;
3252                 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3253                 /* Send the popup_done event to all the popups open */
3254                 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3255                         shell_surface_send_popup_done(shsurf);
3256                         shsurf->popup.shseat = NULL;
3257                         if (prev) {
3258                                 wl_list_init(&prev->popup.grab_link);
3259                         }
3260                         prev = shsurf;
3261                 }
3262                 wl_list_init(&prev->popup.grab_link);
3263                 wl_list_init(&shseat->popup_grab.surfaces_list);
3264         }
3265 }
3266
3267 static void
3268 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat, int32_t type)
3269 {
3270         struct weston_seat *seat = shseat->seat;
3271
3272         if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3273                 shseat->popup_grab.type = type;
3274                 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
3275
3276                 if (type == POINTER) {
3277                         shseat->popup_grab.grab.interface = &popup_grab_interface;
3278                         /* We must make sure here that this popup was opened after
3279                          * a mouse press, and not just by moving around with other
3280                          * popups already open. */
3281                         if (shseat->seat->pointer->button_count > 0)
3282                                 shseat->popup_grab.initial_up = 0;
3283                 } else if (type == TOUCH) {
3284                         shseat->popup_grab.touch_grab.interface = &touch_popup_grab_interface;
3285                 }
3286
3287                 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
3288
3289                 if (type == POINTER)
3290                         weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
3291                 else if (type == TOUCH)
3292                         weston_touch_start_grab(seat->touch, &shseat->popup_grab.touch_grab);
3293         } else {
3294                 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
3295         }
3296 }
3297
3298 static void
3299 remove_popup_grab(struct shell_surface *shsurf)
3300 {
3301         struct shell_seat *shseat = shsurf->popup.shseat;
3302
3303         wl_list_remove(&shsurf->popup.grab_link);
3304         wl_list_init(&shsurf->popup.grab_link);
3305         if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3306                 if (shseat->popup_grab.type == POINTER) {
3307                         weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3308                         shseat->popup_grab.grab.interface = NULL;
3309                 } else if (shseat->popup_grab.type == TOUCH) {
3310                         weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3311                         shseat->popup_grab.touch_grab.interface = NULL;
3312                 }
3313         }
3314 }
3315
3316 static void
3317 shell_map_popup(struct shell_surface *shsurf)
3318 {
3319         struct shell_seat *shseat = shsurf->popup.shseat;
3320         struct weston_view *parent_view = get_default_view(shsurf->parent);
3321
3322         shsurf->surface->output = parent_view->output;
3323         shsurf->view->output = parent_view->output;
3324
3325         weston_view_set_transform_parent(shsurf->view, parent_view);
3326         weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3327         weston_view_update_transform(shsurf->view);
3328
3329         if (shseat->seat->pointer &&
3330             shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
3331                 add_popup_grab(shsurf, shseat, POINTER);
3332         } else if (shseat->seat->touch &&
3333                    shseat->seat->touch->grab_serial == shsurf->popup.serial) {
3334                 add_popup_grab(shsurf, shseat, TOUCH);
3335         } else {
3336                 shell_surface_send_popup_done(shsurf);
3337                 shseat->popup_grab.client = NULL;
3338         }
3339 }
3340
3341 static const struct wl_shell_surface_interface shell_surface_implementation = {
3342         shell_surface_pong,
3343         shell_surface_move,
3344         shell_surface_resize,
3345         shell_surface_set_toplevel,
3346         shell_surface_set_transient,
3347         shell_surface_set_fullscreen,
3348         shell_surface_set_popup,
3349         shell_surface_set_maximized,
3350         shell_surface_set_title,
3351         shell_surface_set_class
3352 };
3353
3354 static void
3355 destroy_shell_surface(struct shell_surface *shsurf)
3356 {
3357         struct shell_surface *child, *next;
3358
3359         wl_signal_emit(&shsurf->destroy_signal, shsurf);
3360
3361         if (!wl_list_empty(&shsurf->popup.grab_link)) {
3362                 remove_popup_grab(shsurf);
3363         }
3364
3365         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
3366             shell_surface_is_top_fullscreen(shsurf))
3367                 restore_output_mode (shsurf->fullscreen_output);
3368
3369         if (shsurf->fullscreen.black_view)
3370                 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
3371
3372         /* As destroy_resource() use wl_list_for_each_safe(),
3373          * we can always remove the listener.
3374          */
3375         wl_list_remove(&shsurf->surface_destroy_listener.link);
3376         shsurf->surface->configure = NULL;
3377         free(shsurf->title);
3378
3379         weston_view_destroy(shsurf->view);
3380
3381         wl_list_remove(&shsurf->children_link);
3382         wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3383                 shell_surface_set_parent(child, NULL);
3384
3385         wl_list_remove(&shsurf->link);
3386         free(shsurf);
3387 }
3388
3389 static void
3390 shell_destroy_shell_surface(struct wl_resource *resource)
3391 {
3392         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3393
3394         if (!wl_list_empty(&shsurf->popup.grab_link))
3395                 remove_popup_grab(shsurf);
3396         shsurf->resource = NULL;
3397 }
3398
3399 static void
3400 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
3401 {
3402         struct shell_surface *shsurf = container_of(listener,
3403                                                     struct shell_surface,
3404                                                     surface_destroy_listener);
3405
3406         if (shsurf->resource)
3407                 wl_resource_destroy(shsurf->resource);
3408
3409         destroy_shell_surface(shsurf);
3410 }
3411
3412 static void
3413 fade_out_done(struct weston_view_animation *animation, void *data)
3414 {
3415         struct shell_surface *shsurf = data;
3416
3417         weston_surface_destroy(shsurf->surface);
3418 }
3419
3420 static void
3421 handle_resource_destroy(struct wl_listener *listener, void *data)
3422 {
3423         struct shell_surface *shsurf =
3424                 container_of(listener, struct shell_surface,
3425                              resource_destroy_listener);
3426
3427         if (!weston_surface_is_mapped(shsurf->surface))
3428                 return;
3429
3430         shsurf->surface->ref_count++;
3431
3432         pixman_region32_fini(&shsurf->surface->pending.input);
3433         pixman_region32_init(&shsurf->surface->pending.input);
3434         pixman_region32_fini(&shsurf->surface->input);
3435         pixman_region32_init(&shsurf->surface->input);
3436         if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3437                 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3438                                 fade_out_done, shsurf);
3439         } else {
3440                 weston_surface_destroy(shsurf->surface);
3441         }
3442 }
3443
3444 static void
3445 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
3446
3447 struct shell_surface *
3448 get_shell_surface(struct weston_surface *surface)
3449 {
3450         if (surface->configure == shell_surface_configure)
3451                 return surface->configure_private;
3452         else
3453                 return NULL;
3454 }
3455
3456 static struct shell_surface *
3457 create_common_surface(struct shell_client *owner, void *shell,
3458                       struct weston_surface *surface,
3459                       const struct weston_shell_client *client)
3460 {
3461         struct shell_surface *shsurf;
3462
3463         if (surface->configure) {
3464                 weston_log("surface->configure already set\n");
3465                 return NULL;
3466         }
3467
3468         shsurf = calloc(1, sizeof *shsurf);
3469         if (!shsurf) {
3470                 weston_log("no memory to allocate shell surface\n");
3471                 return NULL;
3472         }
3473
3474         shsurf->view = weston_view_create(surface);
3475         if (!shsurf->view) {
3476                 weston_log("no memory to allocate shell surface\n");
3477                 free(shsurf);
3478                 return NULL;
3479         }
3480
3481         surface->configure = shell_surface_configure;
3482         surface->configure_private = shsurf;
3483
3484         shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3485         wl_resource_add_destroy_listener(surface->resource,
3486                                          &shsurf->resource_destroy_listener);
3487         shsurf->owner = owner;
3488
3489         shsurf->shell = (struct desktop_shell *) shell;
3490         shsurf->unresponsive = 0;
3491         shsurf->saved_position_valid = false;
3492         shsurf->saved_size_valid = false;
3493         shsurf->saved_rotation_valid = false;
3494         shsurf->surface = surface;
3495         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3496         shsurf->fullscreen.framerate = 0;
3497         shsurf->fullscreen.black_view = NULL;
3498         wl_list_init(&shsurf->fullscreen.transform.link);
3499
3500         shsurf->output = get_default_output(shsurf->shell->compositor);
3501
3502         wl_signal_init(&shsurf->destroy_signal);
3503         shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
3504         wl_signal_add(&surface->destroy_signal,
3505                       &shsurf->surface_destroy_listener);
3506
3507         /* init link so its safe to always remove it in destroy_shell_surface */
3508         wl_list_init(&shsurf->link);
3509         wl_list_init(&shsurf->popup.grab_link);
3510
3511         /* empty when not in use */
3512         wl_list_init(&shsurf->rotation.transform.link);
3513         weston_matrix_init(&shsurf->rotation.rotation);
3514
3515         wl_list_init(&shsurf->workspace_transform.link);
3516
3517         wl_list_init(&shsurf->children_link);
3518         wl_list_init(&shsurf->children_list);
3519         shsurf->parent = NULL;
3520
3521         shsurf->type = SHELL_SURFACE_NONE;
3522
3523         shsurf->client = client;
3524
3525         return shsurf;
3526 }
3527
3528 static struct shell_surface *
3529 create_shell_surface(void *shell, struct weston_surface *surface,
3530                      const struct weston_shell_client *client)
3531 {
3532         return create_common_surface(NULL, shell, surface, client);
3533 }
3534
3535 static struct weston_view *
3536 get_primary_view(void *shell, struct shell_surface *shsurf)
3537 {
3538         return shsurf->view;
3539 }
3540
3541 static void
3542 shell_get_shell_surface(struct wl_client *client,
3543                         struct wl_resource *resource,
3544                         uint32_t id,
3545                         struct wl_resource *surface_resource)
3546 {
3547         struct weston_surface *surface =
3548                 wl_resource_get_user_data(surface_resource);
3549         struct shell_client *sc = wl_resource_get_user_data(resource);
3550         struct desktop_shell *shell = sc->shell;
3551         struct shell_surface *shsurf;
3552
3553         if (get_shell_surface(surface)) {
3554                 wl_resource_post_error(surface_resource,
3555                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3556                                        "desktop_shell::get_shell_surface already requested");
3557                 return;
3558         }
3559
3560         shsurf = create_common_surface(sc, shell, surface, &shell_client);
3561         if (!shsurf) {
3562                 wl_resource_post_error(surface_resource,
3563                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3564                                        "surface->configure already set");
3565                 return;
3566         }
3567
3568         shsurf->resource =
3569                 wl_resource_create(client,
3570                                    &wl_shell_surface_interface, 1, id);
3571         wl_resource_set_implementation(shsurf->resource,
3572                                        &shell_surface_implementation,
3573                                        shsurf, shell_destroy_shell_surface);
3574 }
3575
3576 static bool
3577 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3578 {
3579         /* A shell surface without a resource is created from xwayland
3580          * and is considered a wl_shell surface for now. */
3581
3582         return shsurf->resource == NULL ||
3583                 wl_resource_instance_of(shsurf->resource,
3584                                         &wl_shell_surface_interface,
3585                                         &shell_surface_implementation);
3586 }
3587
3588 static const struct wl_shell_interface shell_implementation = {
3589         shell_get_shell_surface
3590 };
3591
3592 /****************************
3593  * xdg-shell implementation */
3594
3595 static void
3596 xdg_surface_destroy(struct wl_client *client,
3597                     struct wl_resource *resource)
3598 {
3599         wl_resource_destroy(resource);
3600 }
3601
3602 static void
3603 xdg_surface_set_parent(struct wl_client *client,
3604                        struct wl_resource *resource,
3605                        struct wl_resource *parent_resource)
3606 {
3607         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3608         struct weston_surface *parent;
3609
3610         if (parent_resource)
3611                 parent = wl_resource_get_user_data(parent_resource);
3612         else
3613                 parent = NULL;
3614
3615         shell_surface_set_parent(shsurf, parent);
3616 }
3617
3618 static void
3619 xdg_surface_set_app_id(struct wl_client *client,
3620                        struct wl_resource *resource,
3621                        const char *app_id)
3622 {
3623         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3624
3625         free(shsurf->class);
3626         shsurf->class = strdup(app_id);
3627 }
3628
3629 static void
3630 xdg_surface_show_window_menu(struct wl_client *client,
3631                              struct wl_resource *surface_resource,
3632                              struct wl_resource *seat_resource,
3633                              uint32_t serial,
3634                              int32_t x,
3635                              int32_t y)
3636 {
3637         /* TODO */
3638 }
3639
3640 static void
3641 xdg_surface_set_title(struct wl_client *client,
3642                         struct wl_resource *resource, const char *title)
3643 {
3644         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3645
3646         set_title(shsurf, title);
3647 }
3648
3649 static void
3650 xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3651                  struct wl_resource *seat_resource, uint32_t serial)
3652 {
3653         common_surface_move(resource, seat_resource, serial);
3654 }
3655
3656 static void
3657 xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3658                    struct wl_resource *seat_resource, uint32_t serial,
3659                    uint32_t edges)
3660 {
3661         common_surface_resize(resource, seat_resource, serial, edges);
3662 }
3663
3664 static void
3665 xdg_surface_ack_configure(struct wl_client *client,
3666                           struct wl_resource *resource,
3667                           uint32_t serial)
3668 {
3669         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3670
3671         if (shsurf->state_requested) {
3672                 shsurf->next_state = shsurf->requested_state;
3673                 shsurf->state_changed = true;
3674                 shsurf->state_requested = false;
3675         }
3676 }
3677
3678 static void
3679 xdg_surface_set_window_geometry(struct wl_client *client,
3680                                 struct wl_resource *resource,
3681                                 int32_t x,
3682                                 int32_t y,
3683                                 int32_t width,
3684                                 int32_t height)
3685 {
3686         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3687
3688         set_window_geometry(shsurf, x, y, width, height);
3689 }
3690
3691 static void
3692 xdg_surface_set_maximized(struct wl_client *client,
3693                           struct wl_resource *resource)
3694 {
3695         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3696
3697         shsurf->state_requested = true;
3698         shsurf->requested_state.maximized = true;
3699         send_configure_for_surface(shsurf);
3700 }
3701
3702 static void
3703 xdg_surface_unset_maximized(struct wl_client *client,
3704                             struct wl_resource *resource)
3705 {
3706         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3707
3708         shsurf->state_requested = true;
3709         shsurf->requested_state.maximized = false;
3710         send_configure_for_surface(shsurf);
3711 }
3712
3713 static void
3714 xdg_surface_set_fullscreen(struct wl_client *client,
3715                            struct wl_resource *resource,
3716                            struct wl_resource *output_resource)
3717 {
3718         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3719         struct weston_output *output;
3720
3721         shsurf->state_requested = true;
3722         shsurf->requested_state.fullscreen = true;
3723
3724         if (output_resource)
3725                 output = wl_resource_get_user_data(output_resource);
3726         else
3727                 output = NULL;
3728
3729         shell_surface_set_output(shsurf, output);
3730         shsurf->fullscreen_output = shsurf->output;
3731
3732         send_configure_for_surface(shsurf);
3733 }
3734
3735 static void
3736 xdg_surface_unset_fullscreen(struct wl_client *client,
3737                              struct wl_resource *resource)
3738 {
3739         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3740
3741         shsurf->state_requested = true;
3742         shsurf->requested_state.fullscreen = false;
3743         send_configure_for_surface(shsurf);
3744 }
3745
3746 static void
3747 xdg_surface_set_minimized(struct wl_client *client,
3748                             struct wl_resource *resource)
3749 {
3750         struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3751
3752         if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3753                 return;
3754
3755          /* apply compositor's own minimization logic (hide) */
3756         set_minimized(shsurf->surface, 1);
3757 }
3758
3759 static const struct xdg_surface_interface xdg_surface_implementation = {
3760         xdg_surface_destroy,
3761         xdg_surface_set_parent,
3762         xdg_surface_set_title,
3763         xdg_surface_set_app_id,
3764         xdg_surface_show_window_menu,
3765         xdg_surface_move,
3766         xdg_surface_resize,
3767         xdg_surface_ack_configure,
3768         xdg_surface_set_window_geometry,
3769         xdg_surface_set_maximized,
3770         xdg_surface_unset_maximized,
3771         xdg_surface_set_fullscreen,
3772         xdg_surface_unset_fullscreen,
3773         xdg_surface_set_minimized,
3774 };
3775
3776 static void
3777 xdg_send_configure(struct weston_surface *surface,
3778                    int32_t width, int32_t height)
3779 {
3780         struct shell_surface *shsurf = get_shell_surface(surface);
3781         uint32_t *s;
3782         struct wl_array states;
3783         uint32_t serial;
3784
3785         assert(shsurf);
3786
3787         if (!shsurf->resource)
3788                 return;
3789
3790         wl_array_init(&states);
3791         if (shsurf->requested_state.fullscreen) {
3792                 s = wl_array_add(&states, sizeof *s);
3793                 *s = XDG_SURFACE_STATE_FULLSCREEN;
3794         } else if (shsurf->requested_state.maximized) {
3795                 s = wl_array_add(&states, sizeof *s);
3796                 *s = XDG_SURFACE_STATE_MAXIMIZED;
3797         }
3798         if (shsurf->resize_edges != 0) {
3799                 s = wl_array_add(&states, sizeof *s);
3800                 *s = XDG_SURFACE_STATE_RESIZING;
3801         }
3802         if (shsurf->focus_count > 0) {
3803                 s = wl_array_add(&states, sizeof *s);
3804                 *s = XDG_SURFACE_STATE_ACTIVATED;
3805         }
3806
3807         serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3808         xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3809
3810         wl_array_release(&states);
3811 }
3812
3813 static const struct weston_shell_client xdg_client = {
3814         xdg_send_configure
3815 };
3816
3817 static void
3818 xdg_use_unstable_version(struct wl_client *client,
3819                          struct wl_resource *resource,
3820                          int32_t version)
3821 {
3822         if (version > 1) {
3823                 wl_resource_post_error(resource,
3824                                        1,
3825                                        "xdg-shell:: version not implemented yet.");
3826                 return;
3827         }
3828 }
3829
3830 static struct shell_surface *
3831 create_xdg_surface(struct shell_client *owner, void *shell,
3832                    struct weston_surface *surface,
3833                    const struct weston_shell_client *client)
3834 {
3835         struct shell_surface *shsurf;
3836
3837         shsurf = create_common_surface(owner, shell, surface, client);
3838         shsurf->type = SHELL_SURFACE_TOPLEVEL;
3839
3840         return shsurf;
3841 }
3842
3843 static void
3844 xdg_get_xdg_surface(struct wl_client *client,
3845                     struct wl_resource *resource,
3846                     uint32_t id,
3847                     struct wl_resource *surface_resource)
3848 {
3849         struct weston_surface *surface =
3850                 wl_resource_get_user_data(surface_resource);
3851         struct shell_client *sc = wl_resource_get_user_data(resource);
3852         struct desktop_shell *shell = sc->shell;
3853         struct shell_surface *shsurf;
3854
3855         if (get_shell_surface(surface)) {
3856                 wl_resource_post_error(surface_resource,
3857                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3858                                        "xdg_shell::get_xdg_surface already requested");
3859                 return;
3860         }
3861
3862         shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
3863         if (!shsurf) {
3864                 wl_resource_post_error(surface_resource,
3865                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3866                                        "surface->configure already set");
3867                 return;
3868         }
3869
3870         shsurf->resource =
3871                 wl_resource_create(client,
3872                                    &xdg_surface_interface, 1, id);
3873         wl_resource_set_implementation(shsurf->resource,
3874                                        &xdg_surface_implementation,
3875                                        shsurf, shell_destroy_shell_surface);
3876 }
3877
3878 static bool
3879 shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3880 {
3881         return shsurf->resource &&
3882                 wl_resource_instance_of(shsurf->resource,
3883                                         &xdg_surface_interface,
3884                                         &xdg_surface_implementation);
3885 }
3886
3887 /* xdg-popup implementation */
3888
3889 static void
3890 xdg_popup_destroy(struct wl_client *client,
3891                   struct wl_resource *resource)
3892 {
3893         wl_resource_destroy(resource);
3894 }
3895
3896 static const struct xdg_popup_interface xdg_popup_implementation = {
3897         xdg_popup_destroy,
3898 };
3899
3900 static void
3901 xdg_popup_send_configure(struct weston_surface *surface,
3902                          int32_t width, int32_t height)
3903 {
3904 }
3905
3906 static const struct weston_shell_client xdg_popup_client = {
3907         xdg_popup_send_configure
3908 };
3909
3910 static struct shell_surface *
3911 create_xdg_popup(struct shell_client *owner, void *shell,
3912                  struct weston_surface *surface,
3913                  const struct weston_shell_client *client,
3914                  struct weston_surface *parent,
3915                  struct shell_seat *seat,
3916                  uint32_t serial,
3917                  int32_t x, int32_t y)
3918 {
3919         struct shell_surface *shsurf;
3920
3921         shsurf = create_common_surface(owner, shell, surface, client);
3922         shsurf->type = SHELL_SURFACE_POPUP;
3923         shsurf->popup.shseat = seat;
3924         shsurf->popup.serial = serial;
3925         shsurf->popup.x = x;
3926         shsurf->popup.y = y;
3927         shell_surface_set_parent(shsurf, parent);
3928
3929         return shsurf;
3930 }
3931
3932 static void
3933 xdg_get_xdg_popup(struct wl_client *client,
3934                   struct wl_resource *resource,
3935                   uint32_t id,
3936                   struct wl_resource *surface_resource,
3937                   struct wl_resource *parent_resource,
3938                   struct wl_resource *seat_resource,
3939                   uint32_t serial,
3940                   int32_t x, int32_t y, uint32_t flags)
3941 {
3942         struct weston_surface *surface =
3943                 wl_resource_get_user_data(surface_resource);
3944         struct shell_client *sc = wl_resource_get_user_data(resource);
3945         struct desktop_shell *shell = sc->shell;
3946         struct shell_surface *shsurf;
3947         struct weston_surface *parent;
3948         struct shell_seat *seat;
3949
3950         if (get_shell_surface(surface)) {
3951                 wl_resource_post_error(surface_resource,
3952                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3953                                        "xdg_shell::get_xdg_popup already requested");
3954                 return;
3955         }
3956
3957         if (!parent_resource) {
3958                 wl_resource_post_error(surface_resource,
3959                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3960                                        "xdg_shell::get_xdg_popup requires a parent shell surface");
3961                 return;
3962         }
3963
3964         parent = wl_resource_get_user_data(parent_resource);
3965         seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3966
3967         shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
3968                                   parent, seat, serial, x, y);
3969         if (!shsurf) {
3970                 wl_resource_post_error(surface_resource,
3971                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3972                                        "surface->configure already set");
3973                 return;
3974         }
3975
3976         shsurf->resource =
3977                 wl_resource_create(client,
3978                                    &xdg_popup_interface, 1, id);
3979         wl_resource_set_implementation(shsurf->resource,
3980                                        &xdg_popup_implementation,
3981                                        shsurf, shell_destroy_shell_surface);
3982 }
3983
3984 static void
3985 xdg_pong(struct wl_client *client,
3986          struct wl_resource *resource, uint32_t serial)
3987 {
3988         struct shell_client *sc = wl_resource_get_user_data(resource);
3989
3990         shell_client_pong(sc, serial);
3991 }
3992
3993 static bool
3994 shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3995 {
3996         return wl_resource_instance_of(shsurf->resource,
3997                                        &xdg_popup_interface,
3998                                        &xdg_popup_implementation);
3999 }
4000
4001 static const struct xdg_shell_interface xdg_implementation = {
4002         xdg_use_unstable_version,
4003         xdg_get_xdg_surface,
4004         xdg_get_xdg_popup,
4005         xdg_pong
4006 };
4007
4008 static int
4009 xdg_shell_unversioned_dispatch(const void *implementation,
4010                                void *_target, uint32_t opcode,
4011                                const struct wl_message *message,
4012                                union wl_argument *args)
4013 {
4014         struct wl_resource *resource = _target;
4015         struct shell_client *sc = wl_resource_get_user_data(resource);
4016
4017         if (opcode != 0) {
4018                 wl_resource_post_error(resource,
4019                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4020                                        "must call use_unstable_version first");
4021                 return 0;
4022         }
4023
4024 #define XDG_SERVER_VERSION 4
4025
4026         static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4027                       "shell implementation doesn't match protocol version");
4028
4029         if (args[0].i != XDG_SERVER_VERSION) {
4030                 wl_resource_post_error(resource,
4031                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4032                                        "incompatible version, server is %d "
4033                                        "client wants %d",
4034                                        XDG_SERVER_VERSION, args[0].i);
4035                 return 0;
4036         }
4037
4038         wl_resource_set_implementation(resource, &xdg_implementation,
4039                                        sc, NULL);
4040
4041         return 1;
4042 }
4043
4044 /* end of xdg-shell implementation */
4045 /***********************************/
4046
4047 static void
4048 shell_fade(struct desktop_shell *shell, enum fade_type type);
4049
4050 static int
4051 screensaver_timeout(void *data)
4052 {
4053         struct desktop_shell *shell = data;
4054
4055         shell_fade(shell, FADE_OUT);
4056
4057         return 1;
4058 }
4059
4060 static void
4061 handle_screensaver_sigchld(struct weston_process *proc, int status)
4062 {
4063         struct desktop_shell *shell =
4064                 container_of(proc, struct desktop_shell, screensaver.process);
4065
4066         proc->pid = 0;
4067
4068         if (shell->locked)
4069                 weston_compositor_sleep(shell->compositor);
4070 }
4071
4072 static void
4073 launch_screensaver(struct desktop_shell *shell)
4074 {
4075         if (shell->screensaver.binding)
4076                 return;
4077
4078         if (!shell->screensaver.path) {
4079                 weston_compositor_sleep(shell->compositor);
4080                 return;
4081         }
4082
4083         if (shell->screensaver.process.pid != 0) {
4084                 weston_log("old screensaver still running\n");
4085                 return;
4086         }
4087
4088         weston_client_launch(shell->compositor,
4089                            &shell->screensaver.process,
4090                            shell->screensaver.path,
4091                            handle_screensaver_sigchld);
4092 }
4093
4094 static void
4095 terminate_screensaver(struct desktop_shell *shell)
4096 {
4097         if (shell->screensaver.process.pid == 0)
4098                 return;
4099
4100         /* Disarm the screensaver timer, otherwise it may fire when the
4101          * compositor is not in the idle state. In that case, the screen will
4102          * be locked, but the wake_signal won't fire on user input, making the
4103          * system unresponsive. */
4104         wl_event_source_timer_update(shell->screensaver.timer, 0);
4105
4106         kill(shell->screensaver.process.pid, SIGTERM);
4107 }
4108
4109 static void
4110 configure_static_view(struct weston_view *ev, struct weston_layer *layer)
4111 {
4112         struct weston_view *v, *next;
4113
4114         wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
4115                 if (v->output == ev->output && v != ev) {
4116                         weston_view_unmap(v);
4117                         v->surface->configure = NULL;
4118                 }
4119         }
4120
4121         weston_view_set_position(ev, ev->output->x, ev->output->y);
4122
4123         if (wl_list_empty(&ev->layer_link.link)) {
4124                 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
4125                 weston_compositor_schedule_repaint(ev->surface->compositor);
4126         }
4127 }
4128
4129 static void
4130 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4131 {
4132         struct desktop_shell *shell = es->configure_private;
4133         struct weston_view *view;
4134
4135         view = container_of(es->views.next, struct weston_view, surface_link);
4136
4137         configure_static_view(view, &shell->background_layer);
4138 }
4139
4140 static void
4141 desktop_shell_set_background(struct wl_client *client,
4142                              struct wl_resource *resource,
4143                              struct wl_resource *output_resource,
4144                              struct wl_resource *surface_resource)
4145 {
4146         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4147         struct weston_surface *surface =
4148                 wl_resource_get_user_data(surface_resource);
4149         struct weston_view *view, *next;
4150
4151         if (surface->configure) {
4152                 wl_resource_post_error(surface_resource,
4153                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4154                                        "surface role already assigned");
4155                 return;
4156         }
4157
4158         wl_list_for_each_safe(view, next, &surface->views, surface_link)
4159                 weston_view_destroy(view);
4160         view = weston_view_create(surface);
4161
4162         surface->configure = background_configure;
4163         surface->configure_private = shell;
4164         surface->output = wl_resource_get_user_data(output_resource);
4165         view->output = surface->output;
4166         desktop_shell_send_configure(resource, 0,
4167                                      surface_resource,
4168                                      surface->output->width,
4169                                      surface->output->height);
4170 }
4171
4172 static void
4173 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4174 {
4175         struct desktop_shell *shell = es->configure_private;
4176         struct weston_view *view;
4177
4178         view = container_of(es->views.next, struct weston_view, surface_link);
4179
4180         configure_static_view(view, &shell->panel_layer);
4181 }
4182
4183 static void
4184 desktop_shell_set_panel(struct wl_client *client,
4185                         struct wl_resource *resource,
4186                         struct wl_resource *output_resource,
4187                         struct wl_resource *surface_resource)
4188 {
4189         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4190         struct weston_surface *surface =
4191                 wl_resource_get_user_data(surface_resource);
4192         struct weston_view *view, *next;
4193
4194         if (surface->configure) {
4195                 wl_resource_post_error(surface_resource,
4196                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
4197                                        "surface role already assigned");
4198                 return;
4199         }
4200
4201         wl_list_for_each_safe(view, next, &surface->views, surface_link)
4202                 weston_view_destroy(view);
4203         view = weston_view_create(surface);
4204
4205         surface->configure = panel_configure;
4206         surface->configure_private = shell;
4207         surface->output = wl_resource_get_user_data(output_resource);
4208         view->output = surface->output;
4209         desktop_shell_send_configure(resource, 0,
4210                                      surface_resource,
4211                                      surface->output->width,
4212                                      surface->output->height);
4213 }
4214
4215 static void
4216 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
4217 {
4218         struct desktop_shell *shell = surface->configure_private;
4219         struct weston_view *view;
4220
4221         view = container_of(surface->views.next, struct weston_view, surface_link);
4222
4223         if (surface->width == 0)
4224                 return;
4225
4226         center_on_output(view, get_default_output(shell->compositor));
4227
4228         if (!weston_surface_is_mapped(surface)) {
4229                 weston_layer_entry_insert(&shell->lock_layer.view_list,
4230                                           &view->layer_link);
4231                 weston_view_update_transform(view);
4232                 shell_fade(shell, FADE_IN);
4233         }
4234 }
4235
4236 static void
4237 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
4238 {
4239         struct desktop_shell *shell =
4240             container_of(listener, struct desktop_shell, lock_surface_listener);
4241
4242         weston_log("lock surface gone\n");
4243         shell->lock_surface = NULL;
4244 }
4245
4246 static void
4247 desktop_shell_set_lock_surface(struct wl_client *client,
4248                                struct wl_resource *resource,
4249                                struct wl_resource *surface_resource)
4250 {
4251         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4252         struct weston_surface *surface =
4253                 wl_resource_get_user_data(surface_resource);
4254
4255         shell->prepare_event_sent = false;
4256
4257         if (!shell->locked)
4258                 return;
4259
4260         shell->lock_surface = surface;
4261
4262         shell->lock_surface_listener.notify = handle_lock_surface_destroy;
4263         wl_signal_add(&surface->destroy_signal,
4264                       &shell->lock_surface_listener);
4265
4266         weston_view_create(surface);
4267         surface->configure = lock_surface_configure;
4268         surface->configure_private = shell;
4269 }
4270
4271 static void
4272 resume_desktop(struct desktop_shell *shell)
4273 {
4274         struct workspace *ws = get_current_workspace(shell);
4275
4276         terminate_screensaver(shell);
4277
4278         wl_list_remove(&shell->lock_layer.link);
4279         if (shell->showing_input_panels) {
4280                 wl_list_insert(&shell->compositor->cursor_layer.link,
4281                                &shell->input_panel_layer.link);
4282                 wl_list_insert(&shell->input_panel_layer.link,
4283                                &shell->fullscreen_layer.link);
4284         } else {
4285                 wl_list_insert(&shell->compositor->cursor_layer.link,
4286                                &shell->fullscreen_layer.link);
4287         }
4288         wl_list_insert(&shell->fullscreen_layer.link,
4289                        &shell->panel_layer.link);
4290         wl_list_insert(&shell->panel_layer.link,
4291                        &ws->layer.link),
4292
4293         restore_focus_state(shell, get_current_workspace(shell));
4294
4295         shell->locked = false;
4296         shell_fade(shell, FADE_IN);
4297         weston_compositor_damage_all(shell->compositor);
4298 }
4299
4300 static void
4301 desktop_shell_unlock(struct wl_client *client,
4302                      struct wl_resource *resource)
4303 {
4304         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4305
4306         shell->prepare_event_sent = false;
4307
4308         if (shell->locked)
4309                 resume_desktop(shell);
4310 }
4311
4312 static void
4313 desktop_shell_set_grab_surface(struct wl_client *client,
4314                                struct wl_resource *resource,
4315                                struct wl_resource *surface_resource)
4316 {
4317         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4318
4319         shell->grab_surface = wl_resource_get_user_data(surface_resource);
4320         weston_view_create(shell->grab_surface);
4321 }
4322
4323 static void
4324 desktop_shell_desktop_ready(struct wl_client *client,
4325                             struct wl_resource *resource)
4326 {
4327         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4328
4329         shell_fade_startup(shell);
4330 }
4331
4332 static void
4333 desktop_shell_set_panel_position(struct wl_client *client,
4334                                  struct wl_resource *resource,
4335                                  uint32_t position)
4336 {
4337         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4338
4339         if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4340             position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4341             position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4342             position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4343                 wl_resource_post_error(resource,
4344                                        DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4345                                        "bad position argument");
4346                 return;
4347         }
4348
4349         shell->panel_position = position;
4350 }
4351
4352 static const struct desktop_shell_interface desktop_shell_implementation = {
4353         desktop_shell_set_background,
4354         desktop_shell_set_panel,
4355         desktop_shell_set_lock_surface,
4356         desktop_shell_unlock,
4357         desktop_shell_set_grab_surface,
4358         desktop_shell_desktop_ready,
4359         desktop_shell_set_panel_position
4360 };
4361
4362 static enum shell_surface_type
4363 get_shell_surface_type(struct weston_surface *surface)
4364 {
4365         struct shell_surface *shsurf;
4366
4367         shsurf = get_shell_surface(surface);
4368         if (!shsurf)
4369                 return SHELL_SURFACE_NONE;
4370         return shsurf->type;
4371 }
4372
4373 static void
4374 move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4375 {
4376         struct weston_surface *focus;
4377         struct weston_surface *surface;
4378         struct shell_surface *shsurf;
4379
4380         if (seat->pointer->focus == NULL)
4381                 return;
4382
4383         focus = seat->pointer->focus->surface;
4384
4385         surface = weston_surface_get_main_surface(focus);
4386         if (surface == NULL)
4387                 return;
4388
4389         shsurf = get_shell_surface(surface);
4390         if (shsurf == NULL || shsurf->state.fullscreen ||
4391             shsurf->state.maximized)
4392                 return;
4393
4394         surface_move(shsurf, (struct weston_seat *) seat, 0);
4395 }
4396
4397 static void
4398 maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4399 {
4400         struct weston_surface *focus = seat->keyboard->focus;
4401         struct weston_surface *surface;
4402         struct shell_surface *shsurf;
4403
4404         surface = weston_surface_get_main_surface(focus);
4405         if (surface == NULL)
4406                 return;
4407
4408         shsurf = get_shell_surface(surface);
4409         if (shsurf == NULL)
4410                 return;
4411
4412         if (!shell_surface_is_xdg_surface(shsurf))
4413                 return;
4414
4415         shsurf->state_requested = true;
4416         shsurf->requested_state.maximized = !shsurf->state.maximized;
4417         send_configure_for_surface(shsurf);
4418 }
4419
4420 static void
4421 fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4422 {
4423         struct weston_surface *focus = seat->keyboard->focus;
4424         struct weston_surface *surface;
4425         struct shell_surface *shsurf;
4426
4427         surface = weston_surface_get_main_surface(focus);
4428         if (surface == NULL)
4429                 return;
4430
4431         shsurf = get_shell_surface(surface);
4432         if (shsurf == NULL)
4433                 return;
4434
4435         if (!shell_surface_is_xdg_surface(shsurf))
4436                 return;
4437
4438         shsurf->state_requested = true;
4439         shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4440         shsurf->fullscreen_output = shsurf->output;
4441         send_configure_for_surface(shsurf);
4442 }
4443
4444 static void
4445 touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4446 {
4447         struct weston_surface *focus;
4448         struct weston_surface *surface;
4449         struct shell_surface *shsurf;
4450
4451         if (seat->touch->focus == NULL)
4452                 return;
4453
4454         focus = seat->touch->focus->surface;
4455         surface = weston_surface_get_main_surface(focus);
4456         if (surface == NULL)
4457                 return;
4458
4459         shsurf = get_shell_surface(surface);
4460         if (shsurf == NULL || shsurf->state.fullscreen ||
4461             shsurf->state.maximized)
4462                 return;
4463
4464         surface_touch_move(shsurf, (struct weston_seat *) seat);
4465 }
4466
4467 static void
4468 resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4469 {
4470         struct weston_surface *focus;
4471         struct weston_surface *surface;
4472         uint32_t edges = 0;
4473         int32_t x, y;
4474         struct shell_surface *shsurf;
4475
4476         if (seat->pointer->focus == NULL)
4477                 return;
4478
4479         focus = seat->pointer->focus->surface;
4480
4481         surface = weston_surface_get_main_surface(focus);
4482         if (surface == NULL)
4483                 return;
4484
4485         shsurf = get_shell_surface(surface);
4486         if (shsurf == NULL || shsurf->state.fullscreen ||
4487             shsurf->state.maximized)
4488                 return;
4489
4490         weston_view_from_global(shsurf->view,
4491                                 wl_fixed_to_int(seat->pointer->grab_x),
4492                                 wl_fixed_to_int(seat->pointer->grab_y),
4493                                 &x, &y);
4494
4495         if (x < shsurf->surface->width / 3)
4496                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
4497         else if (x < 2 * shsurf->surface->width / 3)
4498                 edges |= 0;
4499         else
4500                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
4501
4502         if (y < shsurf->surface->height / 3)
4503                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
4504         else if (y < 2 * shsurf->surface->height / 3)
4505                 edges |= 0;
4506         else
4507                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
4508
4509         surface_resize(shsurf, (struct weston_seat *) seat, edges);
4510 }
4511
4512 static void
4513 surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4514                         wl_fixed_t value, void *data)
4515 {
4516         float step = 0.005;
4517         struct shell_surface *shsurf;
4518         struct weston_surface *focus = seat->pointer->focus->surface;
4519         struct weston_surface *surface;
4520
4521         /* XXX: broken for windows containing sub-surfaces */
4522         surface = weston_surface_get_main_surface(focus);
4523         if (surface == NULL)
4524                 return;
4525
4526         shsurf = get_shell_surface(surface);
4527         if (!shsurf)
4528                 return;
4529
4530         shsurf->view->alpha -= wl_fixed_to_double(value) * step;
4531
4532         if (shsurf->view->alpha > 1.0)
4533                 shsurf->view->alpha = 1.0;
4534         if (shsurf->view->alpha < step)
4535                 shsurf->view->alpha = step;
4536
4537         weston_view_geometry_dirty(shsurf->view);
4538         weston_surface_damage(surface);
4539 }
4540
4541 static void
4542 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
4543         wl_fixed_t value)
4544 {
4545         struct weston_seat *ws = (struct weston_seat *) seat;
4546         struct weston_compositor *compositor = ws->compositor;
4547         struct weston_output *output;
4548         float increment;
4549
4550         wl_list_for_each(output, &compositor->output_list, link) {
4551                 if (pixman_region32_contains_point(&output->region,
4552                                                    wl_fixed_to_double(seat->pointer->x),
4553                                                    wl_fixed_to_double(seat->pointer->y),
4554                                                    NULL)) {
4555                         if (key == KEY_PAGEUP)
4556                                 increment = output->zoom.increment;
4557                         else if (key == KEY_PAGEDOWN)
4558                                 increment = -output->zoom.increment;
4559                         else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
4560                                 /* For every pixel zoom 20th of a step */
4561                                 increment = output->zoom.increment *
4562                                             -wl_fixed_to_double(value) / 20.0;
4563                         else
4564                                 increment = 0;
4565
4566                         output->zoom.level += increment;
4567
4568                         if (output->zoom.level < 0.0)
4569                                 output->zoom.level = 0.0;
4570                         else if (output->zoom.level > output->zoom.max_level)
4571                                 output->zoom.level = output->zoom.max_level;
4572                         else if (!output->zoom.active) {
4573                                 weston_output_activate_zoom(output);
4574                         }
4575
4576                         output->zoom.spring_z.target = output->zoom.level;
4577
4578                         weston_output_update_zoom(output);
4579                 }
4580         }
4581 }
4582
4583 static void
4584 zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4585                   wl_fixed_t value, void *data)
4586 {
4587         do_zoom(seat, time, 0, axis, value);
4588 }
4589
4590 static void
4591 zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4592                  void *data)
4593 {
4594         do_zoom(seat, time, key, 0, 0);
4595 }
4596
4597 static void
4598 terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4599                   void *data)
4600 {
4601         struct weston_compositor *compositor = data;
4602
4603         wl_display_terminate(compositor->wl_display);
4604 }
4605
4606 static void
4607 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4608                    wl_fixed_t x, wl_fixed_t y)
4609 {
4610         struct rotate_grab *rotate =
4611                 container_of(grab, struct rotate_grab, base.grab);
4612         struct weston_pointer *pointer = grab->pointer;
4613         struct shell_surface *shsurf = rotate->base.shsurf;
4614         float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
4615
4616         weston_pointer_move(pointer, x, y);
4617
4618         if (!shsurf)
4619                 return;
4620
4621         cx = 0.5f * shsurf->surface->width;
4622         cy = 0.5f * shsurf->surface->height;
4623
4624         dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4625         dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
4626         r = sqrtf(dx * dx + dy * dy);
4627
4628         wl_list_remove(&shsurf->rotation.transform.link);
4629         weston_view_geometry_dirty(shsurf->view);
4630
4631         if (r > 20.0f) {
4632                 struct weston_matrix *matrix =
4633                         &shsurf->rotation.transform.matrix;
4634
4635                 weston_matrix_init(&rotate->rotation);
4636                 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4637
4638                 weston_matrix_init(matrix);
4639                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
4640                 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
4641                 weston_matrix_multiply(matrix, &rotate->rotation);
4642                 weston_matrix_translate(matrix, cx, cy, 0.0f);
4643
4644                 wl_list_insert(
4645                         &shsurf->view->geometry.transformation_list,
4646                         &shsurf->rotation.transform.link);
4647         } else {
4648                 wl_list_init(&shsurf->rotation.transform.link);
4649                 weston_matrix_init(&shsurf->rotation.rotation);
4650                 weston_matrix_init(&rotate->rotation);
4651         }
4652
4653         /* We need to adjust the position of the surface
4654          * in case it was resized in a rotated state before */
4655         cposx = shsurf->view->geometry.x + cx;
4656         cposy = shsurf->view->geometry.y + cy;
4657         dposx = rotate->center.x - cposx;
4658         dposy = rotate->center.y - cposy;
4659         if (dposx != 0.0f || dposy != 0.0f) {
4660                 weston_view_set_position(shsurf->view,
4661                                          shsurf->view->geometry.x + dposx,
4662                                          shsurf->view->geometry.y + dposy);
4663         }
4664
4665         /* Repaint implies weston_surface_update_transform(), which
4666          * lazily applies the damage due to rotation update.
4667          */
4668         weston_compositor_schedule_repaint(shsurf->surface->compositor);
4669 }
4670
4671 static void
4672 rotate_grab_button(struct weston_pointer_grab *grab,
4673                    uint32_t time, uint32_t button, uint32_t state_w)
4674 {
4675         struct rotate_grab *rotate =
4676                 container_of(grab, struct rotate_grab, base.grab);
4677         struct weston_pointer *pointer = grab->pointer;
4678         struct shell_surface *shsurf = rotate->base.shsurf;
4679         enum wl_pointer_button_state state = state_w;
4680
4681         if (pointer->button_count == 0 &&
4682             state == WL_POINTER_BUTTON_STATE_RELEASED) {
4683                 if (shsurf)
4684                         weston_matrix_multiply(&shsurf->rotation.rotation,
4685                                                &rotate->rotation);
4686                 shell_grab_end(&rotate->base);
4687                 free(rotate);
4688         }
4689 }
4690
4691 static void
4692 rotate_grab_cancel(struct weston_pointer_grab *grab)
4693 {
4694         struct rotate_grab *rotate =
4695                 container_of(grab, struct rotate_grab, base.grab);
4696
4697         shell_grab_end(&rotate->base);
4698         free(rotate);
4699 }
4700
4701 static const struct weston_pointer_grab_interface rotate_grab_interface = {
4702         noop_grab_focus,
4703         rotate_grab_motion,
4704         rotate_grab_button,
4705         rotate_grab_cancel,
4706 };
4707
4708 static void
4709 surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
4710 {
4711         struct rotate_grab *rotate;
4712         float dx, dy;
4713         float r;
4714
4715         rotate = malloc(sizeof *rotate);
4716         if (!rotate)
4717                 return;
4718
4719         weston_view_to_global_float(surface->view,
4720                                     surface->surface->width * 0.5f,
4721                                     surface->surface->height * 0.5f,
4722                                     &rotate->center.x, &rotate->center.y);
4723
4724         dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4725         dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
4726         r = sqrtf(dx * dx + dy * dy);
4727         if (r > 20.0f) {
4728                 struct weston_matrix inverse;
4729
4730                 weston_matrix_init(&inverse);
4731                 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
4732                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
4733
4734                 weston_matrix_init(&rotate->rotation);
4735                 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4736         } else {
4737                 weston_matrix_init(&surface->rotation.rotation);
4738                 weston_matrix_init(&rotate->rotation);
4739         }
4740
4741         shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4742                          seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
4743 }
4744
4745 static void
4746 rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4747                void *data)
4748 {
4749         struct weston_surface *focus;
4750         struct weston_surface *base_surface;
4751         struct shell_surface *surface;
4752
4753         if (seat->pointer->focus == NULL)
4754                 return;
4755
4756         focus = seat->pointer->focus->surface;
4757
4758         base_surface = weston_surface_get_main_surface(focus);
4759         if (base_surface == NULL)
4760                 return;
4761
4762         surface = get_shell_surface(base_surface);
4763         if (surface == NULL || surface->state.fullscreen ||
4764             surface->state.maximized)
4765                 return;
4766
4767         surface_rotate(surface, seat);
4768 }
4769
4770 /* Move all fullscreen layers down to the current workspace and hide their
4771  * black views. The surfaces' state is set to both fullscreen and lowered,
4772  * and this is reversed when such a surface is re-configured, see
4773  * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4774  *
4775  * This should be used when implementing shell-wide overlays, such as
4776  * the alt-tab switcher, which need to de-promote fullscreen layers. */
4777 void
4778 lower_fullscreen_layer(struct desktop_shell *shell)
4779 {
4780         struct workspace *ws;
4781         struct weston_view *view, *prev;
4782
4783         ws = get_current_workspace(shell);
4784         wl_list_for_each_reverse_safe(view, prev,
4785                                       &shell->fullscreen_layer.view_list.link,
4786                                       layer_link.link) {
4787                 struct shell_surface *shsurf = get_shell_surface(view->surface);
4788
4789                 if (!shsurf)
4790                         continue;
4791
4792                 /* We can have a non-fullscreen popup for a fullscreen surface
4793                  * in the fullscreen layer. */
4794                 if (shsurf->state.fullscreen) {
4795                         /* Hide the black view */
4796                         weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4797                         wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
4798                         weston_view_damage_below(shsurf->fullscreen.black_view);
4799
4800                 }
4801
4802                 /* Lower the view to the workspace layer */
4803                 weston_layer_entry_remove(&view->layer_link);
4804                 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
4805                 weston_view_damage_below(view);
4806                 weston_surface_damage(view->surface);
4807
4808                 shsurf->state.lowered = true;
4809         }
4810 }
4811
4812 void
4813 activate(struct desktop_shell *shell, struct weston_surface *es,
4814          struct weston_seat *seat, bool configure)
4815 {
4816         struct weston_surface *main_surface;
4817         struct focus_state *state;
4818         struct workspace *ws;
4819         struct weston_surface *old_es;
4820         struct shell_surface *shsurf;
4821
4822         lower_fullscreen_layer(shell);
4823
4824         main_surface = weston_surface_get_main_surface(es);
4825
4826         weston_surface_activate(es, seat);
4827
4828         state = ensure_focus_state(shell, seat);
4829         if (state == NULL)
4830                 return;
4831
4832         old_es = state->keyboard_focus;
4833         focus_state_set_focus(state, es);
4834
4835         shsurf = get_shell_surface(main_surface);
4836         assert(shsurf);
4837
4838         if (shsurf->state.fullscreen && configure)
4839                 shell_configure_fullscreen(shsurf);
4840         else
4841                 restore_all_output_modes(shell->compositor);
4842
4843         /* Update the surface’s layer. This brings it to the top of the stacking
4844          * order as appropriate. */
4845         shell_surface_update_layer(shsurf);
4846
4847         if (shell->focus_animation_type != ANIMATION_NONE) {
4848                 ws = get_current_workspace(shell);
4849                 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
4850         }
4851 }
4852
4853 /* no-op func for checking black surface */
4854 static void
4855 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4856 {
4857 }
4858
4859 static bool
4860 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4861 {
4862         if (es->configure == black_surface_configure) {
4863                 if (fs_surface)
4864                         *fs_surface = (struct weston_surface *)es->configure_private;
4865                 return true;
4866         }
4867         return false;
4868 }
4869
4870 static void
4871 activate_binding(struct weston_seat *seat,
4872                  struct desktop_shell *shell,
4873                  struct weston_surface *focus)
4874 {
4875         struct weston_surface *main_surface;
4876
4877         if (!focus)
4878                 return;
4879
4880         if (is_black_surface(focus, &main_surface))
4881                 focus = main_surface;
4882
4883         main_surface = weston_surface_get_main_surface(focus);
4884         if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
4885                 return;
4886
4887         activate(shell, focus, seat, true);
4888 }
4889
4890 static void
4891 click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4892                           void *data)
4893 {
4894         if (seat->pointer->grab != &seat->pointer->default_grab)
4895                 return;
4896         if (seat->pointer->focus == NULL)
4897                 return;
4898
4899         activate_binding(seat, data, seat->pointer->focus->surface);
4900 }
4901
4902 static void
4903 touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4904 {
4905         if (seat->touch->grab != &seat->touch->default_grab)
4906                 return;
4907         if (seat->touch->focus == NULL)
4908                 return;
4909
4910         activate_binding(seat, data, seat->touch->focus->surface);
4911 }
4912
4913 static void
4914 unfocus_all_seats(struct desktop_shell *shell)
4915 {
4916         struct weston_seat *seat, *next;
4917
4918         wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4919                 if (seat->keyboard == NULL)
4920                         continue;
4921
4922                 weston_keyboard_set_focus(seat->keyboard, NULL);
4923         }
4924 }
4925
4926 static void
4927 lock(struct desktop_shell *shell)
4928 {
4929         struct workspace *ws = get_current_workspace(shell);
4930
4931         if (shell->locked) {
4932                 weston_compositor_sleep(shell->compositor);
4933                 return;
4934         }
4935
4936         shell->locked = true;
4937
4938         /* Hide all surfaces by removing the fullscreen, panel and
4939          * toplevel layers.  This way nothing else can show or receive
4940          * input events while we are locked. */
4941
4942         wl_list_remove(&shell->panel_layer.link);
4943         wl_list_remove(&shell->fullscreen_layer.link);
4944         if (shell->showing_input_panels)
4945                 wl_list_remove(&shell->input_panel_layer.link);
4946         wl_list_remove(&ws->layer.link);
4947         wl_list_insert(&shell->compositor->cursor_layer.link,
4948                        &shell->lock_layer.link);
4949
4950         launch_screensaver(shell);
4951
4952         /* Remove the keyboard focus on all seats. This will be
4953          * restored to the workspace's saved state via
4954          * restore_focus_state when the compositor is unlocked */
4955         unfocus_all_seats(shell);
4956
4957         /* TODO: disable bindings that should not work while locked. */
4958
4959         /* All this must be undone in resume_desktop(). */
4960 }
4961
4962 static void
4963 unlock(struct desktop_shell *shell)
4964 {
4965         if (!shell->locked || shell->lock_surface) {
4966                 shell_fade(shell, FADE_IN);
4967                 return;
4968         }
4969
4970         /* If desktop-shell client has gone away, unlock immediately. */
4971         if (!shell->child.desktop_shell) {
4972                 resume_desktop(shell);
4973                 return;
4974         }
4975
4976         if (shell->prepare_event_sent)
4977                 return;
4978
4979         desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
4980         shell->prepare_event_sent = true;
4981 }
4982
4983 static void
4984 shell_fade_done(struct weston_view_animation *animation, void *data)
4985 {
4986         struct desktop_shell *shell = data;
4987
4988         shell->fade.animation = NULL;
4989
4990         switch (shell->fade.type) {
4991         case FADE_IN:
4992                 weston_surface_destroy(shell->fade.view->surface);
4993                 shell->fade.view = NULL;
4994                 break;
4995         case FADE_OUT:
4996                 lock(shell);
4997                 break;
4998         default:
4999                 break;
5000         }
5001 }
5002
5003 static struct weston_view *
5004 shell_fade_create_surface(struct desktop_shell *shell)
5005 {
5006         struct weston_compositor *compositor = shell->compositor;
5007         struct weston_surface *surface;
5008         struct weston_view *view;
5009
5010         surface = weston_surface_create(compositor);
5011         if (!surface)
5012                 return NULL;
5013
5014         view = weston_view_create(surface);
5015         if (!view) {
5016                 weston_surface_destroy(surface);
5017                 return NULL;
5018         }
5019
5020         weston_surface_set_size(surface, 8192, 8192);
5021         weston_view_set_position(view, 0, 0);
5022         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
5023         weston_layer_entry_insert(&compositor->fade_layer.view_list,
5024                                   &view->layer_link);
5025         pixman_region32_init(&surface->input);
5026
5027         return view;
5028 }
5029
5030 static void
5031 shell_fade(struct desktop_shell *shell, enum fade_type type)
5032 {
5033         float tint;
5034
5035         switch (type) {
5036         case FADE_IN:
5037                 tint = 0.0;
5038                 break;
5039         case FADE_OUT:
5040                 tint = 1.0;
5041                 break;
5042         default:
5043                 weston_log("shell: invalid fade type\n");
5044                 return;
5045         }
5046
5047         shell->fade.type = type;
5048
5049         if (shell->fade.view == NULL) {
5050                 shell->fade.view = shell_fade_create_surface(shell);
5051                 if (!shell->fade.view)
5052                         return;
5053
5054                 shell->fade.view->alpha = 1.0 - tint;
5055                 weston_view_update_transform(shell->fade.view);
5056         }
5057
5058         if (shell->fade.view->output == NULL) {
5059                 /* If the black view gets a NULL output, we lost the
5060                  * last output and we'll just cancel the fade.  This
5061                  * happens when you close the last window under the
5062                  * X11 or Wayland backends. */
5063                 shell->locked = false;
5064                 weston_surface_destroy(shell->fade.view->surface);
5065                 shell->fade.view = NULL;
5066         } else if (shell->fade.animation) {
5067                 weston_fade_update(shell->fade.animation, tint);
5068         } else {
5069                 shell->fade.animation =
5070                         weston_fade_run(shell->fade.view,
5071                                         1.0 - tint, tint, 300.0,
5072                                         shell_fade_done, shell);
5073         }
5074 }
5075
5076 static void
5077 do_shell_fade_startup(void *data)
5078 {
5079         struct desktop_shell *shell = data;
5080
5081         if (shell->startup_animation_type == ANIMATION_FADE)
5082                 shell_fade(shell, FADE_IN);
5083         else if (shell->startup_animation_type == ANIMATION_NONE) {
5084                 weston_surface_destroy(shell->fade.view->surface);
5085                 shell->fade.view = NULL;
5086         }
5087 }
5088
5089 static void
5090 shell_fade_startup(struct desktop_shell *shell)
5091 {
5092         struct wl_event_loop *loop;
5093
5094         if (!shell->fade.startup_timer)
5095                 return;
5096
5097         wl_event_source_remove(shell->fade.startup_timer);
5098         shell->fade.startup_timer = NULL;
5099
5100         loop = wl_display_get_event_loop(shell->compositor->wl_display);
5101         wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5102 }
5103
5104 static int
5105 fade_startup_timeout(void *data)
5106 {
5107         struct desktop_shell *shell = data;
5108
5109         shell_fade_startup(shell);
5110         return 0;
5111 }
5112
5113 static void
5114 shell_fade_init(struct desktop_shell *shell)
5115 {
5116         /* Make compositor output all black, and wait for the desktop-shell
5117          * client to signal it is ready, then fade in. The timer triggers a
5118          * fade-in, in case the desktop-shell client takes too long.
5119          */
5120
5121         struct wl_event_loop *loop;
5122
5123         if (shell->fade.view != NULL) {
5124                 weston_log("%s: warning: fade surface already exists\n",
5125                            __func__);
5126                 return;
5127         }
5128
5129         shell->fade.view = shell_fade_create_surface(shell);
5130         if (!shell->fade.view)
5131                 return;
5132
5133         weston_view_update_transform(shell->fade.view);
5134         weston_surface_damage(shell->fade.view->surface);
5135
5136         loop = wl_display_get_event_loop(shell->compositor->wl_display);
5137         shell->fade.startup_timer =
5138                 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5139         wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5140 }
5141
5142 static void
5143 idle_handler(struct wl_listener *listener, void *data)
5144 {
5145         struct desktop_shell *shell =
5146                 container_of(listener, struct desktop_shell, idle_listener);
5147         struct weston_seat *seat;
5148
5149         wl_list_for_each(seat, &shell->compositor->seat_list, link) {
5150                 if (seat->pointer)
5151                         popup_grab_end(seat->pointer);
5152                 if (seat->touch)
5153                         touch_popup_grab_end(seat->touch);
5154         }
5155
5156         shell_fade(shell, FADE_OUT);
5157         /* lock() is called from shell_fade_done() */
5158 }
5159
5160 static void
5161 wake_handler(struct wl_listener *listener, void *data)
5162 {
5163         struct desktop_shell *shell =
5164                 container_of(listener, struct desktop_shell, wake_listener);
5165
5166         unlock(shell);
5167 }
5168
5169 static void
5170 center_on_output(struct weston_view *view, struct weston_output *output)
5171 {
5172         int32_t surf_x, surf_y, width, height;
5173         float x, y;
5174
5175         surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
5176
5177         x = output->x + (output->width - width) / 2 - surf_x / 2;
5178         y = output->y + (output->height - height) / 2 - surf_y / 2;
5179
5180         weston_view_set_position(view, x, y);
5181 }
5182
5183 static void
5184 weston_view_set_initial_position(struct weston_view *view,
5185                                  struct desktop_shell *shell)
5186 {
5187         struct weston_compositor *compositor = shell->compositor;
5188         int ix = 0, iy = 0;
5189         int32_t range_x, range_y;
5190         int32_t dx, dy, x, y;
5191         struct weston_output *output, *target_output = NULL;
5192         struct weston_seat *seat;
5193         pixman_rectangle32_t area;
5194
5195         /* As a heuristic place the new window on the same output as the
5196          * pointer. Falling back to the output containing 0, 0.
5197          *
5198          * TODO: Do something clever for touch too?
5199          */
5200         wl_list_for_each(seat, &compositor->seat_list, link) {
5201                 if (seat->pointer) {
5202                         ix = wl_fixed_to_int(seat->pointer->x);
5203                         iy = wl_fixed_to_int(seat->pointer->y);
5204                         break;
5205                 }
5206         }
5207
5208         wl_list_for_each(output, &compositor->output_list, link) {
5209                 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5210                         target_output = output;
5211                         break;
5212                 }
5213         }
5214
5215         if (!target_output) {
5216                 weston_view_set_position(view, 10 + random() % 400,
5217                                          10 + random() % 400);
5218                 return;
5219         }
5220
5221         /* Valid range within output where the surface will still be onscreen.
5222          * If this is negative it means that the surface is bigger than
5223          * output.
5224          */
5225         get_output_work_area(shell, target_output, &area);
5226
5227         dx = area.x;
5228         dy = area.y;
5229         range_x = area.width - view->surface->width;
5230         range_y = area.height - view->surface->height;
5231
5232         if (range_x > 0)
5233                 dx += random() % range_x;
5234
5235         if (range_y > 0)
5236                 dy += random() % range_y;
5237
5238         x = target_output->x + dx;
5239         y = target_output->y + dy;
5240
5241         weston_view_set_position(view, x, y);
5242 }
5243
5244 static void
5245 set_maximized_position(struct desktop_shell *shell,
5246                        struct shell_surface *shsurf)
5247 {
5248         int32_t surf_x, surf_y;
5249         pixman_rectangle32_t area;
5250
5251         /* use surface configure to set the geometry */
5252         get_output_work_area(shell, shsurf->output, &area);
5253         surface_subsurfaces_boundingbox(shsurf->surface,
5254                                         &surf_x, &surf_y, NULL, NULL);
5255
5256         weston_view_set_position(shsurf->view,
5257                                  area.x - surf_x,
5258                                  area.y - surf_y);
5259 }
5260
5261 static void
5262 map(struct desktop_shell *shell, struct shell_surface *shsurf,
5263     int32_t sx, int32_t sy)
5264 {
5265         struct weston_compositor *compositor = shell->compositor;
5266         struct weston_seat *seat;
5267
5268         /* initial positioning, see also configure() */
5269         switch (shsurf->type) {
5270         case SHELL_SURFACE_TOPLEVEL:
5271                 if (shsurf->state.fullscreen) {
5272                         center_on_output(shsurf->view, shsurf->fullscreen_output);
5273                         shell_map_fullscreen(shsurf);
5274                 } else if (shsurf->state.maximized) {
5275                         set_maximized_position(shell, shsurf);
5276                 } else if (!shsurf->state.relative) {
5277                         weston_view_set_initial_position(shsurf->view, shell);
5278                 }
5279                 break;
5280         case SHELL_SURFACE_POPUP:
5281                 shell_map_popup(shsurf);
5282                 break;
5283         case SHELL_SURFACE_NONE:
5284                 weston_view_set_position(shsurf->view,
5285                                          shsurf->view->geometry.x + sx,
5286                                          shsurf->view->geometry.y + sy);
5287                 break;
5288         case SHELL_SURFACE_XWAYLAND:
5289         default:
5290                 ;
5291         }
5292
5293         /* Surface stacking order, see also activate(). */
5294         shell_surface_update_layer(shsurf);
5295
5296         if (shsurf->type != SHELL_SURFACE_NONE) {
5297                 weston_view_update_transform(shsurf->view);
5298                 if (shsurf->state.maximized) {
5299                         shsurf->surface->output = shsurf->output;
5300                         shsurf->view->output = shsurf->output;
5301                 }
5302         }
5303
5304         switch (shsurf->type) {
5305         /* XXX: xwayland's using the same fields for transient type */
5306         case SHELL_SURFACE_XWAYLAND:
5307                 if (shsurf->transient.flags ==
5308                                 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5309                         break;
5310         case SHELL_SURFACE_TOPLEVEL:
5311                 if (shsurf->state.relative &&
5312                     shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5313                         break;
5314                 if (shell->locked)
5315                         break;
5316                 wl_list_for_each(seat, &compositor->seat_list, link)
5317                         activate(shell, shsurf->surface, seat, true);
5318                 break;
5319         case SHELL_SURFACE_POPUP:
5320         case SHELL_SURFACE_NONE:
5321         default:
5322                 break;
5323         }
5324
5325         if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5326             !shsurf->state.maximized && !shsurf->state.fullscreen)
5327         {
5328                 switch (shell->win_animation_type) {
5329                 case ANIMATION_FADE:
5330                         weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
5331                         break;
5332                 case ANIMATION_ZOOM:
5333                         weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
5334                         break;
5335                 case ANIMATION_NONE:
5336                 default:
5337                         break;
5338                 }
5339         }
5340 }
5341
5342 static void
5343 configure(struct desktop_shell *shell, struct weston_surface *surface,
5344           float x, float y)
5345 {
5346         struct shell_surface *shsurf;
5347         struct weston_view *view;
5348
5349         shsurf = get_shell_surface(surface);
5350
5351         assert(shsurf);
5352
5353         if (shsurf->state.fullscreen)
5354                 shell_configure_fullscreen(shsurf);
5355         else if (shsurf->state.maximized) {
5356                 set_maximized_position(shell, shsurf);
5357         } else {
5358                 weston_view_set_position(shsurf->view, x, y);
5359         }
5360
5361         /* XXX: would a fullscreen surface need the same handling? */
5362         if (surface->output) {
5363                 wl_list_for_each(view, &surface->views, surface_link)
5364                         weston_view_update_transform(view);
5365
5366                 if (shsurf->state.maximized)
5367                         surface->output = shsurf->output;
5368         }
5369 }
5370
5371 static void
5372 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
5373 {
5374         struct shell_surface *shsurf = get_shell_surface(es);
5375         struct desktop_shell *shell;
5376         int type_changed = 0;
5377
5378         assert(shsurf);
5379
5380         shell = shsurf->shell;
5381
5382         if (!weston_surface_is_mapped(es) &&
5383             !wl_list_empty(&shsurf->popup.grab_link)) {
5384                 remove_popup_grab(shsurf);
5385         }
5386
5387         if (es->width == 0)
5388                 return;
5389
5390         if (shsurf->has_next_geometry) {
5391                 shsurf->geometry = shsurf->next_geometry;
5392                 shsurf->has_next_geometry = false;
5393                 shsurf->has_set_geometry = true;
5394         } else if (!shsurf->has_set_geometry) {
5395                 surface_subsurfaces_boundingbox(shsurf->surface,
5396                                                 &shsurf->geometry.x,
5397                                                 &shsurf->geometry.y,
5398                                                 &shsurf->geometry.width,
5399                                                 &shsurf->geometry.height);
5400         }
5401
5402         if (shsurf->state_changed) {
5403                 set_surface_type(shsurf);
5404                 type_changed = 1;
5405         }
5406
5407         if (!weston_surface_is_mapped(es)) {
5408                 map(shell, shsurf, sx, sy);
5409         } else if (type_changed || sx != 0 || sy != 0 ||
5410                    shsurf->last_width != es->width ||
5411                    shsurf->last_height != es->height) {
5412                 float from_x, from_y;
5413                 float to_x, to_y;
5414
5415                 if (shsurf->resize_edges) {
5416                         sx = 0;
5417                         sy = 0;
5418                 }
5419
5420                 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5421                         sx = shsurf->last_width - es->width;
5422                 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5423                         sy = shsurf->last_height - es->height;
5424
5425                 shsurf->last_width = es->width;
5426                 shsurf->last_height = es->height;
5427
5428                 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5429                 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
5430                 configure(shell, es,
5431                           shsurf->view->geometry.x + to_x - from_x,
5432                           shsurf->view->geometry.y + to_y - from_y);
5433         }
5434 }
5435
5436 static bool
5437 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5438 {
5439         struct timespec now;
5440
5441         if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5442                 return false;
5443
5444         /*
5445          * If the shell helper client dies before the session has been
5446          * up for roughly 30 seconds, better just make Weston shut down,
5447          * because the user likely has no way to interact with the desktop
5448          * anyway.
5449          */
5450         if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5451                 weston_log("Error: %s apparently cannot run at all.\n",
5452                            shell->client);
5453                 weston_log_continue(STAMP_SPACE "Quitting...");
5454                 wl_display_terminate(shell->compositor->wl_display);
5455
5456                 return true;
5457         }
5458
5459         return false;
5460 }
5461
5462 static void launch_desktop_shell_process(void *data);
5463
5464 static void
5465 respawn_desktop_shell_process(struct desktop_shell *shell)
5466 {
5467         uint32_t time;
5468
5469         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5470         time = weston_compositor_get_time();
5471         if (time - shell->child.deathstamp > 30000) {
5472                 shell->child.deathstamp = time;
5473                 shell->child.deathcount = 0;
5474         }
5475
5476         shell->child.deathcount++;
5477         if (shell->child.deathcount > 5) {
5478                 weston_log("%s disconnected, giving up.\n", shell->client);
5479                 return;
5480         }
5481
5482         weston_log("%s disconnected, respawning...\n", shell->client);
5483         launch_desktop_shell_process(shell);
5484 }
5485
5486 static void
5487 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5488 {
5489         struct desktop_shell *shell;
5490
5491         shell = container_of(listener, struct desktop_shell,
5492                              child.client_destroy_listener);
5493
5494         wl_list_remove(&shell->child.client_destroy_listener.link);
5495         shell->child.client = NULL;
5496         /*
5497          * unbind_desktop_shell() will reset shell->child.desktop_shell
5498          * before the respawned process has a chance to create a new
5499          * desktop_shell object, because we are being called from the
5500          * wl_client destructor which destroys all wl_resources before
5501          * returning.
5502          */
5503
5504         if (!check_desktop_shell_crash_too_early(shell))
5505                 respawn_desktop_shell_process(shell);
5506
5507         shell_fade_startup(shell);
5508 }
5509
5510 static void
5511 launch_desktop_shell_process(void *data)
5512 {
5513         struct desktop_shell *shell = data;
5514
5515         shell->child.client = weston_client_start(shell->compositor,
5516                                                   shell->client);
5517
5518         if (!shell->child.client) {
5519                 weston_log("not able to start %s\n", shell->client);
5520                 return;
5521         }
5522
5523         shell->child.client_destroy_listener.notify =
5524                 desktop_shell_client_destroy;
5525         wl_client_add_destroy_listener(shell->child.client,
5526                                        &shell->child.client_destroy_listener);
5527 }
5528
5529 static void
5530 handle_shell_client_destroy(struct wl_listener *listener, void *data)
5531 {
5532         struct shell_client *sc =
5533                 container_of(listener, struct shell_client, destroy_listener);
5534
5535         if (sc->ping_timer)
5536                 wl_event_source_remove(sc->ping_timer);
5537         free(sc);
5538 }
5539
5540 static struct shell_client *
5541 shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5542                     const struct wl_interface *interface, uint32_t id)
5543 {
5544         struct shell_client *sc;
5545
5546         sc = zalloc(sizeof *sc);
5547         if (sc == NULL) {
5548                 wl_client_post_no_memory(client);
5549                 return NULL;
5550         }
5551
5552         sc->resource = wl_resource_create(client, interface, 1, id);
5553         if (sc->resource == NULL) {
5554                 free(sc);
5555                 wl_client_post_no_memory(client);
5556                 return NULL;
5557         }
5558
5559         sc->client = client;
5560         sc->shell = shell;
5561         sc->destroy_listener.notify = handle_shell_client_destroy;
5562         wl_client_add_destroy_listener(client, &sc->destroy_listener);
5563
5564         return sc;
5565 }
5566
5567 static void
5568 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5569 {
5570         struct desktop_shell *shell = data;
5571         struct shell_client *sc;
5572
5573         sc = shell_client_create(client, shell, &wl_shell_interface, id);
5574         if (sc)
5575                 wl_resource_set_implementation(sc->resource,
5576                                                &shell_implementation,
5577                                                sc, NULL);
5578 }
5579
5580 static void
5581 bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5582 {
5583         struct desktop_shell *shell = data;
5584         struct shell_client *sc;
5585
5586         sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5587         if (sc)
5588                 wl_resource_set_dispatcher(sc->resource,
5589                                            xdg_shell_unversioned_dispatch,
5590                                            NULL, sc, NULL);
5591 }
5592
5593 static void
5594 unbind_desktop_shell(struct wl_resource *resource)
5595 {
5596         struct desktop_shell *shell = wl_resource_get_user_data(resource);
5597
5598         if (shell->locked)
5599                 resume_desktop(shell);
5600
5601         shell->child.desktop_shell = NULL;
5602         shell->prepare_event_sent = false;
5603 }
5604
5605 static void
5606 bind_desktop_shell(struct wl_client *client,
5607                    void *data, uint32_t version, uint32_t id)
5608 {
5609         struct desktop_shell *shell = data;
5610         struct wl_resource *resource;
5611
5612         resource = wl_resource_create(client, &desktop_shell_interface,
5613                                       MIN(version, 3), id);
5614
5615         if (client == shell->child.client) {
5616                 wl_resource_set_implementation(resource,
5617                                                &desktop_shell_implementation,
5618                                                shell, unbind_desktop_shell);
5619                 shell->child.desktop_shell = resource;
5620
5621                 if (version < 2)
5622                         shell_fade_startup(shell);
5623
5624                 return;
5625         }
5626
5627         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5628                                "permission to bind desktop_shell denied");
5629 }
5630
5631 static void
5632 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
5633 {
5634         struct desktop_shell *shell = surface->configure_private;
5635         struct weston_view *view;
5636         struct weston_layer_entry *prev;
5637
5638         if (surface->width == 0)
5639                 return;
5640
5641         /* XXX: starting weston-screensaver beforehand does not work */
5642         if (!shell->locked)
5643                 return;
5644
5645         view = container_of(surface->views.next, struct weston_view, surface_link);
5646         center_on_output(view, surface->output);
5647
5648         if (wl_list_empty(&view->layer_link.link)) {
5649                 prev = container_of(shell->lock_layer.view_list.link.prev,
5650                                     struct weston_layer_entry, link);
5651                 weston_layer_entry_insert(prev, &view->layer_link);
5652                 weston_view_update_transform(view);
5653                 wl_event_source_timer_update(shell->screensaver.timer,
5654                                              shell->screensaver.duration);
5655                 shell_fade(shell, FADE_IN);
5656         }
5657 }
5658
5659 static void
5660 screensaver_set_surface(struct wl_client *client,
5661                         struct wl_resource *resource,
5662                         struct wl_resource *surface_resource,
5663                         struct wl_resource *output_resource)
5664 {
5665         struct desktop_shell *shell = wl_resource_get_user_data(resource);
5666         struct weston_surface *surface =
5667                 wl_resource_get_user_data(surface_resource);
5668         struct weston_output *output = wl_resource_get_user_data(output_resource);
5669         struct weston_view *view, *next;
5670
5671         /* Make sure we only have one view */
5672         wl_list_for_each_safe(view, next, &surface->views, surface_link)
5673                 weston_view_destroy(view);
5674         weston_view_create(surface);
5675
5676         surface->configure = screensaver_configure;
5677         surface->configure_private = shell;
5678         surface->output = output;
5679 }
5680
5681 static const struct screensaver_interface screensaver_implementation = {
5682         screensaver_set_surface
5683 };
5684
5685 static void
5686 unbind_screensaver(struct wl_resource *resource)
5687 {
5688         struct desktop_shell *shell = wl_resource_get_user_data(resource);
5689
5690         shell->screensaver.binding = NULL;
5691 }
5692
5693 static void
5694 bind_screensaver(struct wl_client *client,
5695                  void *data, uint32_t version, uint32_t id)
5696 {
5697         struct desktop_shell *shell = data;
5698         struct wl_resource *resource;
5699
5700         resource = wl_resource_create(client, &screensaver_interface, 1, id);
5701
5702         if (shell->screensaver.binding == NULL) {
5703                 wl_resource_set_implementation(resource,
5704                                                &screensaver_implementation,
5705                                                shell, unbind_screensaver);
5706                 shell->screensaver.binding = resource;
5707                 return;
5708         }
5709
5710         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5711                                "interface object already bound");
5712 }
5713
5714 struct switcher {
5715         struct desktop_shell *shell;
5716         struct weston_surface *current;
5717         struct wl_listener listener;
5718         struct weston_keyboard_grab grab;
5719         struct wl_array minimized_array;
5720 };
5721
5722 static void
5723 switcher_next(struct switcher *switcher)
5724 {
5725         struct weston_view *view;
5726         struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
5727         struct shell_surface *shsurf;
5728         struct workspace *ws = get_current_workspace(switcher->shell);
5729
5730          /* temporary re-display minimized surfaces */
5731         struct weston_view *tmp;
5732         struct weston_view **minimized;
5733         wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5734                 weston_layer_entry_remove(&view->layer_link);
5735                 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
5736                 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5737                 *minimized = view;
5738         }
5739
5740         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
5741                 shsurf = get_shell_surface(view->surface);
5742                 if (shsurf &&
5743                     shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5744                     shsurf->parent == NULL) {
5745                         if (first == NULL)
5746                                 first = view->surface;
5747                         if (prev == switcher->current)
5748                                 next = view->surface;
5749                         prev = view->surface;
5750                         view->alpha = 0.25;
5751                         weston_view_geometry_dirty(view);
5752                         weston_surface_damage(view->surface);
5753                 }
5754
5755                 if (is_black_surface(view->surface, NULL)) {
5756                         view->alpha = 0.25;
5757                         weston_view_geometry_dirty(view);
5758                         weston_surface_damage(view->surface);
5759                 }
5760         }
5761
5762         if (next == NULL)
5763                 next = first;
5764
5765         if (next == NULL)
5766                 return;
5767
5768         wl_list_remove(&switcher->listener.link);
5769         wl_signal_add(&next->destroy_signal, &switcher->listener);
5770
5771         switcher->current = next;
5772         wl_list_for_each(view, &next->views, surface_link)
5773                 view->alpha = 1.0;
5774
5775         shsurf = get_shell_surface(switcher->current);
5776         if (shsurf && shsurf->state.fullscreen)
5777                 shsurf->fullscreen.black_view->alpha = 1.0;
5778 }
5779
5780 static void
5781 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
5782 {
5783         struct switcher *switcher =
5784                 container_of(listener, struct switcher, listener);
5785
5786         switcher_next(switcher);
5787 }
5788
5789 static void
5790 switcher_destroy(struct switcher *switcher)
5791 {
5792         struct weston_view *view;
5793         struct weston_keyboard *keyboard = switcher->grab.keyboard;
5794         struct workspace *ws = get_current_workspace(switcher->shell);
5795
5796         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
5797                 if (is_focus_view(view))
5798                         continue;
5799
5800                 view->alpha = 1.0;
5801                 weston_surface_damage(view->surface);
5802         }
5803
5804         if (switcher->current)
5805                 activate(switcher->shell, switcher->current,
5806                          (struct weston_seat *) keyboard->seat, true);
5807         wl_list_remove(&switcher->listener.link);
5808         weston_keyboard_end_grab(keyboard);
5809         if (keyboard->input_method_resource)
5810                 keyboard->grab = &keyboard->input_method_grab;
5811
5812          /* re-hide surfaces that were temporary shown during the switch */
5813         struct weston_view **minimized;
5814         wl_array_for_each(minimized, &switcher->minimized_array) {
5815                 /* with the exception of the current selected */
5816                 if ((*minimized)->surface != switcher->current) {
5817                         weston_layer_entry_remove(&(*minimized)->layer_link);
5818                         weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5819                         weston_view_damage_below(*minimized);
5820                 }
5821         }
5822         wl_array_release(&switcher->minimized_array);
5823
5824         free(switcher);
5825 }
5826
5827 static void
5828 switcher_key(struct weston_keyboard_grab *grab,
5829              uint32_t time, uint32_t key, uint32_t state_w)
5830 {
5831         struct switcher *switcher = container_of(grab, struct switcher, grab);
5832         enum wl_keyboard_key_state state = state_w;
5833
5834         if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
5835                 switcher_next(switcher);
5836 }
5837
5838 static void
5839 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5840                   uint32_t mods_depressed, uint32_t mods_latched,
5841                   uint32_t mods_locked, uint32_t group)
5842 {
5843         struct switcher *switcher = container_of(grab, struct switcher, grab);
5844         struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5845
5846         if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5847                 switcher_destroy(switcher);
5848 }
5849
5850 static void
5851 switcher_cancel(struct weston_keyboard_grab *grab)
5852 {
5853         struct switcher *switcher = container_of(grab, struct switcher, grab);
5854
5855         switcher_destroy(switcher);
5856 }
5857
5858 static const struct weston_keyboard_grab_interface switcher_grab = {
5859         switcher_key,
5860         switcher_modifier,
5861         switcher_cancel,
5862 };
5863
5864 static void
5865 switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5866                  void *data)
5867 {
5868         struct desktop_shell *shell = data;
5869         struct switcher *switcher;
5870
5871         switcher = malloc(sizeof *switcher);
5872         switcher->shell = shell;
5873         switcher->current = NULL;
5874         switcher->listener.notify = switcher_handle_surface_destroy;
5875         wl_list_init(&switcher->listener.link);
5876         wl_array_init(&switcher->minimized_array);
5877
5878         restore_all_output_modes(shell->compositor);
5879         lower_fullscreen_layer(switcher->shell);
5880         switcher->grab.interface = &switcher_grab;
5881         weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5882         weston_keyboard_set_focus(seat->keyboard, NULL);
5883         switcher_next(switcher);
5884 }
5885
5886 static void
5887 backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5888                   void *data)
5889 {
5890         struct weston_compositor *compositor = data;
5891         struct weston_output *output;
5892         long backlight_new = 0;
5893
5894         /* TODO: we're limiting to simple use cases, where we assume just
5895          * control on the primary display. We'd have to extend later if we
5896          * ever get support for setting backlights on random desktop LCD
5897          * panels though */
5898         output = get_default_output(compositor);
5899         if (!output)
5900                 return;
5901
5902         if (!output->set_backlight)
5903                 return;
5904
5905         if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5906                 backlight_new = output->backlight_current - 25;
5907         else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5908                 backlight_new = output->backlight_current + 25;
5909
5910         if (backlight_new < 5)
5911                 backlight_new = 5;
5912         if (backlight_new > 255)
5913                 backlight_new = 255;
5914
5915         output->backlight_current = backlight_new;
5916         output->set_backlight(output, output->backlight_current);
5917 }
5918
5919 struct debug_binding_grab {
5920         struct weston_keyboard_grab grab;
5921         struct weston_seat *seat;
5922         uint32_t key[2];
5923         int key_released[2];
5924 };
5925
5926 static void
5927 debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
5928                   uint32_t key, uint32_t state)
5929 {
5930         struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5931         struct weston_compositor *ec = db->seat->compositor;
5932         struct wl_display *display = ec->wl_display;
5933         struct wl_resource *resource;
5934         uint32_t serial;
5935         int send = 0, terminate = 0;
5936         int check_binding = 1;
5937         int i;
5938         struct wl_list *resource_list;
5939
5940         if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5941                 /* Do not run bindings on key releases */
5942                 check_binding = 0;
5943
5944                 for (i = 0; i < 2; i++)
5945                         if (key == db->key[i])
5946                                 db->key_released[i] = 1;
5947
5948                 if (db->key_released[0] && db->key_released[1]) {
5949                         /* All key releases been swalled so end the grab */
5950                         terminate = 1;
5951                 } else if (key != db->key[0] && key != db->key[1]) {
5952                         /* Should not swallow release of other keys */
5953                         send = 1;
5954                 }
5955         } else if (key == db->key[0] && !db->key_released[0]) {
5956                 /* Do not check bindings for the first press of the binding
5957                  * key. This allows it to be used as a debug shortcut.
5958                  * We still need to swallow this event. */
5959                 check_binding = 0;
5960         } else if (db->key[1]) {
5961                 /* If we already ran a binding don't process another one since
5962                  * we can't keep track of all the binding keys that were
5963                  * pressed in order to swallow the release events. */
5964                 send = 1;
5965                 check_binding = 0;
5966         }
5967
5968         if (check_binding) {
5969                 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5970                                                         key, state)) {
5971                         /* We ran a binding so swallow the press and keep the
5972                          * grab to swallow the released too. */
5973                         send = 0;
5974                         terminate = 0;
5975                         db->key[1] = key;
5976                 } else {
5977                         /* Terminate the grab since the key pressed is not a
5978                          * debug binding key. */
5979                         send = 1;
5980                         terminate = 1;
5981                 }
5982         }
5983
5984         if (send) {
5985                 serial = wl_display_next_serial(display);
5986                 resource_list = &grab->keyboard->focus_resource_list;
5987                 wl_resource_for_each(resource, resource_list) {
5988                         wl_keyboard_send_key(resource, serial, time, key, state);
5989                 }
5990         }
5991
5992         if (terminate) {
5993                 weston_keyboard_end_grab(grab->keyboard);
5994                 if (grab->keyboard->input_method_resource)
5995                         grab->keyboard->grab = &grab->keyboard->input_method_grab;
5996                 free(db);
5997         }
5998 }
5999
6000 static void
6001 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
6002                         uint32_t mods_depressed, uint32_t mods_latched,
6003                         uint32_t mods_locked, uint32_t group)
6004 {
6005         struct wl_resource *resource;
6006         struct wl_list *resource_list;
6007
6008         resource_list = &grab->keyboard->focus_resource_list;
6009
6010         wl_resource_for_each(resource, resource_list) {
6011                 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6012                                            mods_latched, mods_locked, group);
6013         }
6014 }
6015
6016 static void
6017 debug_binding_cancel(struct weston_keyboard_grab *grab)
6018 {
6019         struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6020
6021         weston_keyboard_end_grab(grab->keyboard);
6022         free(db);
6023 }
6024
6025 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
6026         debug_binding_key,
6027         debug_binding_modifiers,
6028         debug_binding_cancel,
6029 };
6030
6031 static void
6032 debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
6033 {
6034         struct debug_binding_grab *grab;
6035
6036         grab = calloc(1, sizeof *grab);
6037         if (!grab)
6038                 return;
6039
6040         grab->seat = (struct weston_seat *) seat;
6041         grab->key[0] = key;
6042         grab->grab.interface = &debug_binding_keyboard_grab;
6043         weston_keyboard_start_grab(seat->keyboard, &grab->grab);
6044 }
6045
6046 static void
6047 force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
6048                    void *data)
6049 {
6050         struct weston_surface *focus_surface;
6051         struct wl_client *client;
6052         struct desktop_shell *shell = data;
6053         struct weston_compositor *compositor = shell->compositor;
6054         pid_t pid;
6055
6056         focus_surface = seat->keyboard->focus;
6057         if (!focus_surface)
6058                 return;
6059
6060         wl_signal_emit(&compositor->kill_signal, focus_surface);
6061
6062         client = wl_resource_get_client(focus_surface->resource);
6063         wl_client_get_credentials(client, &pid, NULL, NULL);
6064
6065         /* Skip clients that we launched ourselves (the credentials of
6066          * the socketpair is ours) */
6067         if (pid == getpid())
6068                 return;
6069
6070         kill(pid, SIGKILL);
6071 }
6072
6073 static void
6074 workspace_up_binding(struct weston_seat *seat, uint32_t time,
6075                      uint32_t key, void *data)
6076 {
6077         struct desktop_shell *shell = data;
6078         unsigned int new_index = shell->workspaces.current;
6079
6080         if (shell->locked)
6081                 return;
6082         if (new_index != 0)
6083                 new_index--;
6084
6085         change_workspace(shell, new_index);
6086 }
6087
6088 static void
6089 workspace_down_binding(struct weston_seat *seat, uint32_t time,
6090                        uint32_t key, void *data)
6091 {
6092         struct desktop_shell *shell = data;
6093         unsigned int new_index = shell->workspaces.current;
6094
6095         if (shell->locked)
6096                 return;
6097         if (new_index < shell->workspaces.num - 1)
6098                 new_index++;
6099
6100         change_workspace(shell, new_index);
6101 }
6102
6103 static void
6104 workspace_f_binding(struct weston_seat *seat, uint32_t time,
6105                     uint32_t key, void *data)
6106 {
6107         struct desktop_shell *shell = data;
6108         unsigned int new_index;
6109
6110         if (shell->locked)
6111                 return;
6112         new_index = key - KEY_F1;
6113         if (new_index >= shell->workspaces.num)
6114                 new_index = shell->workspaces.num - 1;
6115
6116         change_workspace(shell, new_index);
6117 }
6118
6119 static void
6120 workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
6121                                   uint32_t key, void *data)
6122 {
6123         struct desktop_shell *shell = data;
6124         unsigned int new_index = shell->workspaces.current;
6125
6126         if (shell->locked)
6127                 return;
6128
6129         if (new_index != 0)
6130                 new_index--;
6131
6132         take_surface_to_workspace_by_seat(shell, seat, new_index);
6133 }
6134
6135 static void
6136 workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
6137                                     uint32_t key, void *data)
6138 {
6139         struct desktop_shell *shell = data;
6140         unsigned int new_index = shell->workspaces.current;
6141
6142         if (shell->locked)
6143                 return;
6144
6145         if (new_index < shell->workspaces.num - 1)
6146                 new_index++;
6147
6148         take_surface_to_workspace_by_seat(shell, seat, new_index);
6149 }
6150
6151 static void
6152 shell_reposition_view_on_output_destroy(struct weston_view *view)
6153 {
6154         struct weston_output *output, *first_output;
6155         struct weston_compositor *ec = view->surface->compositor;
6156         struct shell_surface *shsurf;
6157         float x, y;
6158         int visible;
6159
6160         x = view->geometry.x;
6161         y = view->geometry.y;
6162
6163         /* At this point the destroyed output is not in the list anymore.
6164          * If the view is still visible somewhere, we leave where it is,
6165          * otherwise, move it to the first output. */
6166         visible = 0;
6167         wl_list_for_each(output, &ec->output_list, link) {
6168                 if (pixman_region32_contains_point(&output->region,
6169                                                    x, y, NULL)) {
6170                         visible = 1;
6171                         break;
6172                 }
6173         }
6174
6175         if (!visible) {
6176                 first_output = container_of(ec->output_list.next,
6177                                             struct weston_output, link);
6178
6179                 x = first_output->x + first_output->width / 4;
6180                 y = first_output->y + first_output->height / 4;
6181
6182                 weston_view_set_position(view, x, y);
6183         } else {
6184                 weston_view_geometry_dirty(view);
6185         }
6186
6187
6188         shsurf = get_shell_surface(view->surface);
6189
6190         if (shsurf) {
6191                 shsurf->saved_position_valid = false;
6192                 shsurf->next_state.maximized = false;
6193                 shsurf->next_state.fullscreen = false;
6194                 shsurf->state_changed = true;
6195         }
6196 }
6197
6198 void
6199 shell_for_each_layer(struct desktop_shell *shell,
6200                      shell_for_each_layer_func_t func, void *data)
6201 {
6202         struct workspace **ws;
6203
6204         func(shell, &shell->fullscreen_layer, data);
6205         func(shell, &shell->panel_layer, data);
6206         func(shell, &shell->background_layer, data);
6207         func(shell, &shell->lock_layer, data);
6208         func(shell, &shell->input_panel_layer, data);
6209
6210         wl_array_for_each(ws, &shell->workspaces.array)
6211                 func(shell, &(*ws)->layer, data);
6212 }
6213
6214 static void
6215 shell_output_destroy_move_layer(struct desktop_shell *shell,
6216                                 struct weston_layer *layer,
6217                                 void *data)
6218 {
6219         struct weston_output *output = data;
6220         struct weston_view *view;
6221
6222         wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6223                 if (view->output != output)
6224                         continue;
6225
6226                 shell_reposition_view_on_output_destroy(view);
6227         }
6228 }
6229
6230 static void
6231 handle_output_destroy(struct wl_listener *listener, void *data)
6232 {
6233         struct shell_output *output_listener =
6234                 container_of(listener, struct shell_output, destroy_listener);
6235         struct weston_output *output = output_listener->output;
6236         struct desktop_shell *shell = output_listener->shell;
6237
6238         shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
6239
6240         wl_list_remove(&output_listener->destroy_listener.link);
6241         wl_list_remove(&output_listener->link);
6242         free(output_listener);
6243 }
6244
6245 static void
6246 create_shell_output(struct desktop_shell *shell,
6247                                         struct weston_output *output)
6248 {
6249         struct shell_output *shell_output;
6250
6251         shell_output = zalloc(sizeof *shell_output);
6252         if (shell_output == NULL)
6253                 return;
6254
6255         shell_output->output = output;
6256         shell_output->shell = shell;
6257         shell_output->destroy_listener.notify = handle_output_destroy;
6258         wl_signal_add(&output->destroy_signal,
6259                       &shell_output->destroy_listener);
6260         wl_list_insert(shell->output_list.prev, &shell_output->link);
6261 }
6262
6263 static void
6264 handle_output_create(struct wl_listener *listener, void *data)
6265 {
6266         struct desktop_shell *shell =
6267                 container_of(listener, struct desktop_shell, output_create_listener);
6268         struct weston_output *output = (struct weston_output *)data;
6269
6270         create_shell_output(shell, output);
6271 }
6272
6273 static void
6274 handle_output_move_layer(struct desktop_shell *shell,
6275                          struct weston_layer *layer, void *data)
6276 {
6277         struct weston_output *output = data;
6278         struct weston_view *view;
6279         float x, y;
6280
6281         wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6282                 if (view->output != output)
6283                         continue;
6284
6285                 x = view->geometry.x + output->move_x;
6286                 y = view->geometry.y + output->move_y;
6287                 weston_view_set_position(view, x, y);
6288         }
6289 }
6290
6291 static void
6292 handle_output_move(struct wl_listener *listener, void *data)
6293 {
6294         struct desktop_shell *shell;
6295
6296         shell = container_of(listener, struct desktop_shell,
6297                              output_move_listener);
6298
6299         shell_for_each_layer(shell, handle_output_move_layer, data);
6300 }
6301
6302 static void
6303 setup_output_destroy_handler(struct weston_compositor *ec,
6304                                                         struct desktop_shell *shell)
6305 {
6306         struct weston_output *output;
6307
6308         wl_list_init(&shell->output_list);
6309         wl_list_for_each(output, &ec->output_list, link)
6310                 create_shell_output(shell, output);
6311
6312         shell->output_create_listener.notify = handle_output_create;
6313         wl_signal_add(&ec->output_created_signal,
6314                                 &shell->output_create_listener);
6315
6316         shell->output_move_listener.notify = handle_output_move;
6317         wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
6318 }
6319
6320 static void
6321 shell_destroy(struct wl_listener *listener, void *data)
6322 {
6323         struct desktop_shell *shell =
6324                 container_of(listener, struct desktop_shell, destroy_listener);
6325         struct workspace **ws;
6326         struct shell_output *shell_output, *tmp;
6327
6328         /* Force state to unlocked so we don't try to fade */
6329         shell->locked = false;
6330
6331         if (shell->child.client) {
6332                 /* disable respawn */
6333                 wl_list_remove(&shell->child.client_destroy_listener.link);
6334                 wl_client_destroy(shell->child.client);
6335         }
6336
6337         wl_list_remove(&shell->idle_listener.link);
6338         wl_list_remove(&shell->wake_listener.link);
6339
6340         input_panel_destroy(shell);
6341
6342         wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6343                 wl_list_remove(&shell_output->destroy_listener.link);
6344                 wl_list_remove(&shell_output->link);
6345                 free(shell_output);
6346         }
6347
6348         wl_list_remove(&shell->output_create_listener.link);
6349         wl_list_remove(&shell->output_move_listener.link);
6350
6351         wl_array_for_each(ws, &shell->workspaces.array)
6352                 workspace_destroy(*ws);
6353         wl_array_release(&shell->workspaces.array);
6354
6355         free(shell->screensaver.path);
6356         free(shell->client);
6357         free(shell);
6358 }
6359
6360 static void
6361 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6362 {
6363         uint32_t mod;
6364         int i, num_workspace_bindings;
6365
6366         /* fixed bindings */
6367         weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6368                                           MODIFIER_CTRL | MODIFIER_ALT,
6369                                           terminate_binding, ec);
6370         weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6371                                              click_to_activate_binding,
6372                                              shell);
6373         weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6374                                              click_to_activate_binding,
6375                                              shell);
6376         weston_compositor_add_touch_binding(ec, 0,
6377                                             touch_to_activate_binding,
6378                                             shell);
6379         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6380                                            MODIFIER_SUPER | MODIFIER_ALT,
6381                                            surface_opacity_binding, NULL);
6382         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6383                                            MODIFIER_SUPER, zoom_axis_binding,
6384                                            NULL);
6385
6386         /* configurable bindings */
6387         mod = shell->binding_modifier;
6388         weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6389                                           zoom_key_binding, NULL);
6390         weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6391                                           zoom_key_binding, NULL);
6392         weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6393                                           maximize_binding, NULL);
6394         weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6395                                           fullscreen_binding, NULL);
6396         weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6397                                              shell);
6398         weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
6399         weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6400                                              resize_binding, shell);
6401         weston_compositor_add_button_binding(ec, BTN_LEFT,
6402                                              mod | MODIFIER_SHIFT,
6403                                              resize_binding, shell);
6404
6405         if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6406                 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6407                                                      rotate_binding, NULL);
6408
6409         weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6410                                           shell);
6411         weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6412                                           ec);
6413         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6414                                           backlight_binding, ec);
6415         weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6416                                           ec);
6417         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6418                                           backlight_binding, ec);
6419         weston_compositor_add_key_binding(ec, KEY_K, mod,
6420                                           force_kill_binding, shell);
6421         weston_compositor_add_key_binding(ec, KEY_UP, mod,
6422                                           workspace_up_binding, shell);
6423         weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6424                                           workspace_down_binding, shell);
6425         weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6426                                           workspace_move_surface_up_binding,
6427                                           shell);
6428         weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6429                                           workspace_move_surface_down_binding,
6430                                           shell);
6431
6432         if (shell->exposay_modifier)
6433                 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6434                                                        exposay_binding, shell);
6435
6436         /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6437         if (shell->workspaces.num > 1) {
6438                 num_workspace_bindings = shell->workspaces.num;
6439                 if (num_workspace_bindings > 6)
6440                         num_workspace_bindings = 6;
6441                 for (i = 0; i < num_workspace_bindings; i++)
6442                         weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6443                                                           workspace_f_binding,
6444                                                           shell);
6445         }
6446
6447         /* Debug bindings */
6448         weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6449                                           debug_binding, shell);
6450 }
6451
6452 static void
6453 handle_seat_created(struct wl_listener *listener, void *data)
6454 {
6455         struct weston_seat *seat = data;
6456
6457         create_shell_seat(seat);
6458 }
6459
6460 WL_EXPORT int
6461 module_init(struct weston_compositor *ec,
6462             int *argc, char *argv[])
6463 {
6464         struct weston_seat *seat;
6465         struct desktop_shell *shell;
6466         struct workspace **pws;
6467         unsigned int i;
6468         struct wl_event_loop *loop;
6469
6470         shell = zalloc(sizeof *shell);
6471         if (shell == NULL)
6472                 return -1;
6473
6474         shell->compositor = ec;
6475
6476         shell->destroy_listener.notify = shell_destroy;
6477         wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
6478         shell->idle_listener.notify = idle_handler;
6479         wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6480         shell->wake_listener.notify = wake_handler;
6481         wl_signal_add(&ec->wake_signal, &shell->wake_listener);
6482
6483         ec->shell_interface.shell = shell;
6484         ec->shell_interface.create_shell_surface = create_shell_surface;
6485         ec->shell_interface.get_primary_view = get_primary_view;
6486         ec->shell_interface.set_toplevel = set_toplevel;
6487         ec->shell_interface.set_transient = set_transient;
6488         ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
6489         ec->shell_interface.set_xwayland = set_xwayland;
6490         ec->shell_interface.move = shell_interface_move;
6491         ec->shell_interface.resize = surface_resize;
6492         ec->shell_interface.set_title = set_title;
6493         ec->shell_interface.set_window_geometry = set_window_geometry;
6494
6495         weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6496         weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
6497         weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6498         weston_layer_init(&shell->lock_layer, NULL);
6499         weston_layer_init(&shell->input_panel_layer, NULL);
6500
6501         wl_array_init(&shell->workspaces.array);
6502         wl_list_init(&shell->workspaces.client_list);
6503
6504         if (input_panel_setup(shell) < 0)
6505                 return -1;
6506
6507         shell_configuration(shell);
6508
6509         shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6510         shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6511
6512         for (i = 0; i < shell->workspaces.num; i++) {
6513                 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6514                 if (pws == NULL)
6515                         return -1;
6516
6517                 *pws = workspace_create();
6518                 if (*pws == NULL)
6519                         return -1;
6520         }
6521         activate_workspace(shell, 0);
6522
6523         weston_layer_init(&shell->minimized_layer, NULL);
6524
6525         wl_list_init(&shell->workspaces.anim_sticky_list);
6526         wl_list_init(&shell->workspaces.animation.link);
6527         shell->workspaces.animation.frame = animate_workspace_change_frame;
6528
6529         if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
6530                                   shell, bind_shell) == NULL)
6531                 return -1;
6532
6533         if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6534                                   shell, bind_xdg_shell) == NULL)
6535                 return -1;
6536
6537         if (wl_global_create(ec->wl_display,
6538                              &desktop_shell_interface, 3,
6539                              shell, bind_desktop_shell) == NULL)
6540                 return -1;
6541
6542         if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6543                              shell, bind_screensaver) == NULL)
6544                 return -1;
6545
6546         if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6547                              shell, bind_workspace_manager) == NULL)
6548                 return -1;
6549
6550         shell->child.deathstamp = weston_compositor_get_time();
6551
6552         shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6553
6554         setup_output_destroy_handler(ec, shell);
6555
6556         loop = wl_display_get_event_loop(ec->wl_display);
6557         wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
6558
6559         shell->screensaver.timer =
6560                 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6561
6562         wl_list_for_each(seat, &ec->seat_list, link)
6563                 handle_seat_created(NULL, seat);
6564         shell->seat_create_listener.notify = handle_seat_created;
6565         wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
6566
6567         screenshooter_create(ec);
6568
6569         shell_add_bindings(ec, shell);
6570
6571         shell_fade_init(shell);
6572
6573         clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
6574
6575         return 0;
6576 }