fix weston launch error
[platform/upstream/weston.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 is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <linux/input.h>
34 #include <assert.h>
35 #include <signal.h>
36 #include <math.h>
37 #include <sys/types.h>
38
39 #include "shell.h"
40 #include "compositor/weston.h"
41 #include "weston-desktop-shell-server-protocol.h"
42 #include "shared/config-parser.h"
43 #include "shared/helpers.h"
44 #include "shared/timespec-util.h"
45 #include "libweston-desktop/libweston-desktop.h"
46
47 #define DEFAULT_NUM_WORKSPACES 1
48 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
49
50 struct focus_state {
51         struct desktop_shell *shell;
52         struct weston_seat *seat;
53         struct workspace *ws;
54         struct weston_surface *keyboard_focus;
55         struct wl_list link;
56         struct wl_listener seat_destroy_listener;
57         struct wl_listener surface_destroy_listener;
58 };
59
60 /*
61  * Surface stacking and ordering.
62  *
63  * This is handled using several linked lists of surfaces, organised into
64  * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
65  * above all of the surfaces in the layer below. The set of layers is static and
66  * in the following order (top-most first):
67  *  • Lock layer (only ever displayed on its own)
68  *  • Cursor layer
69  *  • Input panel layer
70  *  • Fullscreen layer
71  *  • Panel layer
72  *  • Workspace layers
73  *  • Background layer
74  *
75  * The list of layers may be manipulated to remove whole layers of surfaces from
76  * display. For example, when locking the screen, all layers except the lock
77  * layer are removed.
78  *
79  * A surface’s layer is modified on configuring the surface, in
80  * set_surface_type() (which is only called when the surface’s type change is
81  * _committed_). If a surface’s type changes (e.g. when making a window
82  * fullscreen) its layer changes too.
83  *
84  * In order to allow popup and transient surfaces to be correctly stacked above
85  * their parent surfaces, each surface tracks both its parent surface, and a
86  * linked list of its children. When a surface’s layer is updated, so are the
87  * layers of its children. Note that child surfaces are *not* the same as
88  * subsurfaces — child/parent surfaces are purely for maintaining stacking
89  * order.
90  *
91  * The children_link list of siblings of a surface (i.e. those surfaces which
92  * have the same parent) only contains weston_surfaces which have a
93  * shell_surface. Stacking is not implemented for non-shell_surface
94  * weston_surfaces. This means that the following implication does *not* hold:
95  *     (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
96  */
97
98 struct shell_surface {
99         struct wl_signal destroy_signal;
100
101         struct weston_desktop_surface *desktop_surface;
102         struct weston_view *view;
103         int32_t last_width, last_height;
104
105         struct desktop_shell *shell;
106
107         int32_t saved_x, saved_y;
108         bool saved_position_valid;
109         bool saved_rotation_valid;
110         int unresponsive, grabbed;
111         uint32_t resize_edges;
112
113         struct {
114                 struct weston_transform transform;
115                 struct weston_matrix rotation;
116         } rotation;
117
118         struct {
119                 struct weston_transform transform; /* matrix from x, y */
120                 struct weston_view *black_view;
121         } fullscreen;
122
123         struct weston_transform workspace_transform;
124
125         struct weston_output *fullscreen_output;
126         struct weston_output *output;
127         struct wl_listener output_destroy_listener;
128
129         struct surface_state {
130                 bool fullscreen;
131                 bool maximized;
132                 bool lowered;
133         } state;
134
135         struct {
136                 bool is_set;
137                 int32_t x;
138                 int32_t y;
139         } xwayland;
140
141         int focus_count;
142
143         bool destroying;
144 };
145
146 struct shell_grab {
147         struct weston_pointer_grab grab;
148         struct shell_surface *shsurf;
149         struct wl_listener shsurf_destroy_listener;
150 };
151
152 struct shell_touch_grab {
153         struct weston_touch_grab grab;
154         struct shell_surface *shsurf;
155         struct wl_listener shsurf_destroy_listener;
156         struct weston_touch *touch;
157 };
158
159 struct weston_move_grab {
160         struct shell_grab base;
161         wl_fixed_t dx, dy;
162         bool client_initiated;
163 };
164
165 struct weston_touch_move_grab {
166         struct shell_touch_grab base;
167         int active;
168         wl_fixed_t dx, dy;
169 };
170
171 struct rotate_grab {
172         struct shell_grab base;
173         struct weston_matrix rotation;
174         struct {
175                 float x;
176                 float y;
177         } center;
178 };
179
180 struct shell_seat {
181         struct weston_seat *seat;
182         struct wl_listener seat_destroy_listener;
183         struct weston_surface *focused_surface;
184
185         struct wl_listener caps_changed_listener;
186         struct wl_listener pointer_focus_listener;
187         struct wl_listener keyboard_focus_listener;
188 };
189
190
191 static struct desktop_shell *
192 shell_surface_get_shell(struct shell_surface *shsurf);
193
194 static void
195 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
196
197 static void
198 surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
199
200 static void
201 shell_fade_startup(struct desktop_shell *shell);
202
203 static void
204 shell_fade(struct desktop_shell *shell, enum fade_type type);
205
206 static struct shell_seat *
207 get_shell_seat(struct weston_seat *seat);
208
209 static void
210 get_output_panel_size(struct desktop_shell *shell,
211                       struct weston_output *output,
212                       int *width, int *height);
213
214 static void
215 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
216
217 static int
218 shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
219 {
220         const char *t, *c;
221         struct weston_desktop_surface *desktop_surface =
222                 weston_surface_get_desktop_surface(surface);
223
224         t = weston_desktop_surface_get_title(desktop_surface);
225         c = weston_desktop_surface_get_app_id(desktop_surface);
226
227         return snprintf(buf, len, "%s window%s%s%s%s%s",
228                 "top-level",
229                 t ? " '" : "", t ?: "", t ? "'" : "",
230                 c ? " of " : "", c ?: "");
231 }
232
233 static void
234 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
235 {
236         struct shell_grab *grab;
237
238         grab = container_of(listener, struct shell_grab,
239                             shsurf_destroy_listener);
240
241         grab->shsurf = NULL;
242 }
243
244 struct weston_view *
245 get_default_view(struct weston_surface *surface)
246 {
247         struct shell_surface *shsurf;
248         struct weston_view *view;
249
250         if (!surface || wl_list_empty(&surface->views))
251                 return NULL;
252
253         shsurf = get_shell_surface(surface);
254         if (shsurf)
255                 return shsurf->view;
256
257         wl_list_for_each(view, &surface->views, surface_link)
258                 if (weston_view_is_mapped(view))
259                         return view;
260
261         return container_of(surface->views.next, struct weston_view, surface_link);
262 }
263
264 static void
265 shell_grab_start(struct shell_grab *grab,
266                  const struct weston_pointer_grab_interface *interface,
267                  struct shell_surface *shsurf,
268                  struct weston_pointer *pointer,
269                  enum weston_desktop_shell_cursor cursor)
270 {
271         struct desktop_shell *shell = shsurf->shell;
272
273         weston_seat_break_desktop_grabs(pointer->seat);
274
275         grab->grab.interface = interface;
276         grab->shsurf = shsurf;
277         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
278         wl_signal_add(&shsurf->destroy_signal,
279                       &grab->shsurf_destroy_listener);
280
281         shsurf->grabbed = 1;
282         weston_pointer_start_grab(pointer, &grab->grab);
283         if (shell->child.desktop_shell) {
284                 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
285                                                cursor);
286                 weston_pointer_set_focus(pointer,
287                                          get_default_view(shell->grab_surface),
288                                          wl_fixed_from_int(0),
289                                          wl_fixed_from_int(0));
290         }
291 }
292
293 static void
294 get_panel_size(struct desktop_shell *shell,
295                struct weston_view *view,
296                int *width,
297                int *height)
298 {
299         float x1, y1;
300         float x2, y2;
301         weston_view_to_global_float(view, 0, 0, &x1, &y1);
302         weston_view_to_global_float(view,
303                                     view->surface->width,
304                                     view->surface->height,
305                                     &x2, &y2);
306         *width = (int)(x2 - x1);
307         *height = (int)(y2 - y1);
308 }
309
310 static void
311 get_output_panel_size(struct desktop_shell *shell,
312                       struct weston_output *output,
313                       int *width,
314                       int *height)
315 {
316         struct weston_view *view;
317
318         *width = 0;
319         *height = 0;
320
321         if (!output)
322                 return;
323
324         wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
325                 if (view->surface->output == output) {
326                         get_panel_size(shell, view, width, height);
327                         return;
328                 }
329         }
330
331         /* the correct view wasn't found */
332 }
333
334 static void
335 get_output_work_area(struct desktop_shell *shell,
336                      struct weston_output *output,
337                      pixman_rectangle32_t *area)
338 {
339         int32_t panel_width = 0, panel_height = 0;
340
341         if (!output) {
342                 area->x = 0;
343                 area->y = 0;
344                 area->width = 0;
345                 area->height = 0;
346
347                 return;
348         }
349
350         area->x = output->x;
351         area->y = output->y;
352
353         get_output_panel_size(shell, output, &panel_width, &panel_height);
354         switch (shell->panel_position) {
355         case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
356         default:
357                 area->y += panel_height;
358                 /* fallthrough */
359         case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
360                 area->width = output->width;
361                 area->height = output->height - panel_height;
362                 break;
363         case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
364                 area->x += panel_width;
365                 /* fallthrough */
366         case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
367                 area->width = output->width - panel_width;
368                 area->height = output->height;
369                 break;
370         }
371 }
372
373 static void
374 shell_grab_end(struct shell_grab *grab)
375 {
376         if (grab->shsurf) {
377                 wl_list_remove(&grab->shsurf_destroy_listener.link);
378                 grab->shsurf->grabbed = 0;
379
380                 if (grab->shsurf->resize_edges) {
381                         grab->shsurf->resize_edges = 0;
382                 }
383         }
384
385         weston_pointer_end_grab(grab->grab.pointer);
386 }
387
388 static void
389 shell_touch_grab_start(struct shell_touch_grab *grab,
390                        const struct weston_touch_grab_interface *interface,
391                        struct shell_surface *shsurf,
392                        struct weston_touch *touch)
393 {
394         struct desktop_shell *shell = shsurf->shell;
395
396         weston_seat_break_desktop_grabs(touch->seat);
397
398         grab->grab.interface = interface;
399         grab->shsurf = shsurf;
400         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
401         wl_signal_add(&shsurf->destroy_signal,
402                       &grab->shsurf_destroy_listener);
403
404         grab->touch = touch;
405         shsurf->grabbed = 1;
406
407         weston_touch_start_grab(touch, &grab->grab);
408         if (shell->child.desktop_shell)
409                 weston_touch_set_focus(touch,
410                                        get_default_view(shell->grab_surface));
411 }
412
413 static void
414 shell_touch_grab_end(struct shell_touch_grab *grab)
415 {
416         if (grab->shsurf) {
417                 wl_list_remove(&grab->shsurf_destroy_listener.link);
418                 grab->shsurf->grabbed = 0;
419         }
420
421         weston_touch_end_grab(grab->touch);
422 }
423
424 static void
425 center_on_output(struct weston_view *view,
426                  struct weston_output *output);
427
428 static enum weston_keyboard_modifier
429 get_modifier(char *modifier)
430 {
431         if (!modifier)
432                 return MODIFIER_SUPER;
433
434         if (!strcmp("ctrl", modifier))
435                 return MODIFIER_CTRL;
436         else if (!strcmp("alt", modifier))
437                 return MODIFIER_ALT;
438         else if (!strcmp("super", modifier))
439                 return MODIFIER_SUPER;
440         else if (!strcmp("none", modifier))
441                 return 0;
442         else
443                 return MODIFIER_SUPER;
444 }
445
446 static enum animation_type
447 get_animation_type(char *animation)
448 {
449         if (!animation)
450                 return ANIMATION_NONE;
451
452         if (!strcmp("zoom", animation))
453                 return ANIMATION_ZOOM;
454         else if (!strcmp("fade", animation))
455                 return ANIMATION_FADE;
456         else if (!strcmp("dim-layer", animation))
457                 return ANIMATION_DIM_LAYER;
458         else
459                 return ANIMATION_NONE;
460 }
461
462 static void
463 shell_configuration(struct desktop_shell *shell)
464 {
465         struct weston_config_section *section;
466         char *s, *client;
467         int allow_zap;
468
469         section = weston_config_get_section(wet_get_config(shell->compositor),
470                                             "shell", NULL, NULL);
471         client = wet_get_binary_path(WESTON_SHELL_CLIENT);
472         weston_config_section_get_string(section, "client", &s, client);
473         free(client);
474         shell->client = s;
475
476         weston_config_section_get_bool(section,
477                                        "allow-zap", &allow_zap, true);
478         shell->allow_zap = allow_zap;
479
480         weston_config_section_get_string(section,
481                                          "binding-modifier", &s, "super");
482         shell->binding_modifier = get_modifier(s);
483         free(s);
484
485         weston_config_section_get_string(section,
486                                          "exposay-modifier", &s, "none");
487         shell->exposay_modifier = get_modifier(s);
488         free(s);
489
490         weston_config_section_get_string(section, "animation", &s, "none");
491         shell->win_animation_type = get_animation_type(s);
492         free(s);
493         weston_config_section_get_string(section, "close-animation", &s, "fade");
494         shell->win_close_animation_type = get_animation_type(s);
495         free(s);
496         weston_config_section_get_string(section,
497                                          "startup-animation", &s, "fade");
498         shell->startup_animation_type = get_animation_type(s);
499         free(s);
500         if (shell->startup_animation_type == ANIMATION_ZOOM)
501                 shell->startup_animation_type = ANIMATION_NONE;
502         weston_config_section_get_string(section, "focus-animation", &s, "none");
503         shell->focus_animation_type = get_animation_type(s);
504         free(s);
505         weston_config_section_get_uint(section, "num-workspaces",
506                                        &shell->workspaces.num,
507                                        DEFAULT_NUM_WORKSPACES);
508 }
509
510 struct weston_output *
511 get_default_output(struct weston_compositor *compositor)
512 {
513         if (wl_list_empty(&compositor->output_list))
514                 return NULL;
515
516         return container_of(compositor->output_list.next,
517                             struct weston_output, link);
518 }
519
520 static int
521 focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
522 {
523         return snprintf(buf, len, "focus highlight effect for output %s",
524                         surface->output->name);
525 }
526
527 /* no-op func for checking focus surface */
528 static void
529 focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
530 {
531 }
532
533 static struct focus_surface *
534 get_focus_surface(struct weston_surface *surface)
535 {
536         if (surface->committed == focus_surface_committed)
537                 return surface->committed_private;
538         else
539                 return NULL;
540 }
541
542 static bool
543 is_focus_surface (struct weston_surface *es)
544 {
545         return (es->committed == focus_surface_committed);
546 }
547
548 static bool
549 is_focus_view (struct weston_view *view)
550 {
551         return is_focus_surface (view->surface);
552 }
553
554 static struct focus_surface *
555 create_focus_surface(struct weston_compositor *ec,
556                      struct weston_output *output)
557 {
558         struct focus_surface *fsurf = NULL;
559         struct weston_surface *surface = NULL;
560
561         fsurf = malloc(sizeof *fsurf);
562         if (!fsurf)
563                 return NULL;
564
565         fsurf->surface = weston_surface_create(ec);
566         surface = fsurf->surface;
567         if (surface == NULL) {
568                 free(fsurf);
569                 return NULL;
570         }
571
572         surface->committed = focus_surface_committed;
573         surface->output = output;
574         surface->is_mapped = true;
575         surface->committed_private = fsurf;
576         weston_surface_set_label_func(surface, focus_surface_get_label);
577
578         fsurf->view = weston_view_create(surface);
579         if (fsurf->view == NULL) {
580                 weston_surface_destroy(surface);
581                 free(fsurf);
582                 return NULL;
583         }
584         weston_view_set_output(fsurf->view, output);
585         fsurf->view->is_mapped = true;
586
587         weston_surface_set_size(surface, output->width, output->height);
588         weston_view_set_position(fsurf->view, output->x, output->y);
589         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
590         pixman_region32_fini(&surface->opaque);
591         pixman_region32_init_rect(&surface->opaque, output->x, output->y,
592                                   output->width, output->height);
593         pixman_region32_fini(&surface->input);
594         pixman_region32_init(&surface->input);
595
596         wl_list_init(&fsurf->workspace_transform.link);
597
598         return fsurf;
599 }
600
601 static void
602 focus_surface_destroy(struct focus_surface *fsurf)
603 {
604         weston_surface_destroy(fsurf->surface);
605         free(fsurf);
606 }
607
608 static void
609 focus_animation_done(struct weston_view_animation *animation, void *data)
610 {
611         struct workspace *ws = data;
612
613         ws->focus_animation = NULL;
614 }
615
616 static void
617 focus_state_destroy(struct focus_state *state)
618 {
619         wl_list_remove(&state->seat_destroy_listener.link);
620         wl_list_remove(&state->surface_destroy_listener.link);
621         free(state);
622 }
623
624 static void
625 focus_state_seat_destroy(struct wl_listener *listener, void *data)
626 {
627         struct focus_state *state = container_of(listener,
628                                                  struct focus_state,
629                                                  seat_destroy_listener);
630
631         wl_list_remove(&state->link);
632         focus_state_destroy(state);
633 }
634
635 static void
636 focus_state_surface_destroy(struct wl_listener *listener, void *data)
637 {
638         struct focus_state *state = container_of(listener,
639                                                  struct focus_state,
640                                                  surface_destroy_listener);
641         struct weston_surface *main_surface;
642         struct weston_view *next;
643         struct weston_view *view;
644
645         main_surface = weston_surface_get_main_surface(state->keyboard_focus);
646
647         next = NULL;
648         wl_list_for_each(view,
649                          &state->ws->layer.view_list.link, layer_link.link) {
650                 if (view->surface == main_surface)
651                         continue;
652                 if (is_focus_view(view))
653                         continue;
654                 if (!get_shell_surface(view->surface))
655                         continue;
656
657                 next = view;
658                 break;
659         }
660
661         /* if the focus was a sub-surface, activate its main surface */
662         if (main_surface != state->keyboard_focus)
663                 next = get_default_view(main_surface);
664
665         if (next) {
666                 state->keyboard_focus = NULL;
667                 activate(state->shell, next, state->seat,
668                          WESTON_ACTIVATE_FLAG_CONFIGURE);
669         } else {
670                 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
671                         if (state->ws->focus_animation)
672                                 weston_view_animation_destroy(state->ws->focus_animation);
673
674                         state->ws->focus_animation = weston_fade_run(
675                                 state->ws->fsurf_front->view,
676                                 state->ws->fsurf_front->view->alpha, 0.0, 300,
677                                 focus_animation_done, state->ws);
678                 }
679
680                 wl_list_remove(&state->link);
681                 focus_state_destroy(state);
682         }
683 }
684
685 static struct focus_state *
686 focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
687                    struct workspace *ws)
688 {
689         struct focus_state *state;
690
691         state = malloc(sizeof *state);
692         if (state == NULL)
693                 return NULL;
694
695         state->shell = shell;
696         state->keyboard_focus = NULL;
697         state->ws = ws;
698         state->seat = seat;
699         wl_list_insert(&ws->focus_list, &state->link);
700
701         state->seat_destroy_listener.notify = focus_state_seat_destroy;
702         state->surface_destroy_listener.notify = focus_state_surface_destroy;
703         wl_signal_add(&seat->destroy_signal,
704                       &state->seat_destroy_listener);
705         wl_list_init(&state->surface_destroy_listener.link);
706
707         return state;
708 }
709
710 static struct focus_state *
711 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
712 {
713         struct workspace *ws = get_current_workspace(shell);
714         struct focus_state *state;
715
716         wl_list_for_each(state, &ws->focus_list, link)
717                 if (state->seat == seat)
718                         break;
719
720         if (&state->link == &ws->focus_list)
721                 state = focus_state_create(shell, seat, ws);
722
723         return state;
724 }
725
726 static void
727 focus_state_set_focus(struct focus_state *state,
728                       struct weston_surface *surface)
729 {
730         if (state->keyboard_focus) {
731                 wl_list_remove(&state->surface_destroy_listener.link);
732                 wl_list_init(&state->surface_destroy_listener.link);
733         }
734
735         state->keyboard_focus = surface;
736         if (surface)
737                 wl_signal_add(&surface->destroy_signal,
738                               &state->surface_destroy_listener);
739 }
740
741 static void
742 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
743 {
744         struct focus_state *state, *next;
745         struct weston_surface *surface;
746         struct wl_list pending_seat_list;
747         struct weston_seat *seat, *next_seat;
748
749         /* Temporarily steal the list of seats so that we can keep
750          * track of the seats we've already processed */
751         wl_list_init(&pending_seat_list);
752         wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
753         wl_list_init(&shell->compositor->seat_list);
754
755         wl_list_for_each_safe(state, next, &ws->focus_list, link) {
756                 struct weston_keyboard *keyboard =
757                         weston_seat_get_keyboard(state->seat);
758
759                 wl_list_remove(&state->seat->link);
760                 wl_list_insert(&shell->compositor->seat_list,
761                                &state->seat->link);
762
763                 if (!keyboard)
764                         continue;
765
766                 surface = state->keyboard_focus;
767
768                 weston_keyboard_set_focus(keyboard, surface);
769         }
770
771         /* For any remaining seats that we don't have a focus state
772          * for we'll reset the keyboard focus to NULL */
773         wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
774                 struct weston_keyboard *keyboard =
775                         weston_seat_get_keyboard(seat);
776
777                 wl_list_insert(&shell->compositor->seat_list, &seat->link);
778
779                 if (!keyboard)
780                         continue;
781
782                 weston_keyboard_set_focus(keyboard, NULL);
783         }
784 }
785
786 static void
787 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
788                     struct weston_seat *seat)
789 {
790         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
791         struct focus_state *state;
792
793         wl_list_for_each(state, &ws->focus_list, link) {
794                 if (state->seat == seat) {
795                         focus_state_set_focus(state, keyboard->focus);
796                         return;
797                 }
798         }
799 }
800
801 static void
802 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
803                  struct weston_surface *surface)
804 {
805         struct focus_state *state;
806
807         wl_list_for_each(state, &ws->focus_list, link)
808                 if (state->keyboard_focus == surface)
809                         focus_state_set_focus(state, NULL);
810 }
811
812 static void
813 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
814                      struct weston_view *from, struct weston_view *to)
815 {
816         struct weston_output *output;
817         bool focus_surface_created = false;
818
819         /* FIXME: Only support dim animation using two layers */
820         if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
821                 return;
822
823         output = get_default_output(shell->compositor);
824         if (ws->fsurf_front == NULL && (from || to)) {
825                 ws->fsurf_front = create_focus_surface(shell->compositor, output);
826                 if (ws->fsurf_front == NULL)
827                         return;
828                 ws->fsurf_front->view->alpha = 0.0;
829
830                 ws->fsurf_back = create_focus_surface(shell->compositor, output);
831                 if (ws->fsurf_back == NULL) {
832                         focus_surface_destroy(ws->fsurf_front);
833                         return;
834                 }
835                 ws->fsurf_back->view->alpha = 0.0;
836
837                 focus_surface_created = true;
838         } else {
839                 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
840                 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
841         }
842
843         if (ws->focus_animation) {
844                 weston_view_animation_destroy(ws->focus_animation);
845                 ws->focus_animation = NULL;
846         }
847
848         if (to)
849                 weston_layer_entry_insert(&to->layer_link,
850                                           &ws->fsurf_front->view->layer_link);
851         else if (from)
852                 weston_layer_entry_insert(&ws->layer.view_list,
853                                           &ws->fsurf_front->view->layer_link);
854
855         if (focus_surface_created) {
856                 ws->focus_animation = weston_fade_run(
857                         ws->fsurf_front->view,
858                         ws->fsurf_front->view->alpha, 0.4, 300,
859                         focus_animation_done, ws);
860         } else if (from) {
861                 weston_layer_entry_insert(&from->layer_link,
862                                           &ws->fsurf_back->view->layer_link);
863                 ws->focus_animation = weston_stable_fade_run(
864                         ws->fsurf_front->view, 0.0,
865                         ws->fsurf_back->view, 0.4,
866                         focus_animation_done, ws);
867         } else if (to) {
868                 weston_layer_entry_insert(&ws->layer.view_list,
869                                           &ws->fsurf_back->view->layer_link);
870                 ws->focus_animation = weston_stable_fade_run(
871                         ws->fsurf_front->view, 0.0,
872                         ws->fsurf_back->view, 0.4,
873                         focus_animation_done, ws);
874         }
875 }
876
877 static void
878 workspace_destroy(struct workspace *ws)
879 {
880         struct focus_state *state, *next;
881
882         wl_list_for_each_safe(state, next, &ws->focus_list, link)
883                 focus_state_destroy(state);
884
885         if (ws->fsurf_front)
886                 focus_surface_destroy(ws->fsurf_front);
887         if (ws->fsurf_back)
888                 focus_surface_destroy(ws->fsurf_back);
889
890         free(ws);
891 }
892
893 static void
894 seat_destroyed(struct wl_listener *listener, void *data)
895 {
896         struct weston_seat *seat = data;
897         struct focus_state *state, *next;
898         struct workspace *ws = container_of(listener,
899                                             struct workspace,
900                                             seat_destroyed_listener);
901
902         wl_list_for_each_safe(state, next, &ws->focus_list, link)
903                 if (state->seat == seat)
904                         wl_list_remove(&state->link);
905 }
906
907 static struct workspace *
908 workspace_create(struct desktop_shell *shell)
909 {
910         struct workspace *ws = malloc(sizeof *ws);
911         if (ws == NULL)
912                 return NULL;
913
914         weston_layer_init(&ws->layer, shell->compositor);
915
916         wl_list_init(&ws->focus_list);
917         wl_list_init(&ws->seat_destroyed_listener.link);
918         ws->seat_destroyed_listener.notify = seat_destroyed;
919         ws->fsurf_front = NULL;
920         ws->fsurf_back = NULL;
921         ws->focus_animation = NULL;
922
923         return ws;
924 }
925
926 static int
927 workspace_is_empty(struct workspace *ws)
928 {
929         return wl_list_empty(&ws->layer.view_list.link);
930 }
931
932 static struct workspace *
933 get_workspace(struct desktop_shell *shell, unsigned int index)
934 {
935         struct workspace **pws = shell->workspaces.array.data;
936         assert(index < shell->workspaces.num);
937         pws += index;
938         return *pws;
939 }
940
941 struct workspace *
942 get_current_workspace(struct desktop_shell *shell)
943 {
944         return get_workspace(shell, shell->workspaces.current);
945 }
946
947 static void
948 activate_workspace(struct desktop_shell *shell, unsigned int index)
949 {
950         struct workspace *ws;
951
952         ws = get_workspace(shell, index);
953         weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
954
955         shell->workspaces.current = index;
956 }
957
958 static unsigned int
959 get_output_height(struct weston_output *output)
960 {
961         return abs(output->region.extents.y1 - output->region.extents.y2);
962 }
963
964 static void
965 view_translate(struct workspace *ws, struct weston_view *view, double d)
966 {
967         struct weston_transform *transform;
968
969         if (is_focus_view(view)) {
970                 struct focus_surface *fsurf = get_focus_surface(view->surface);
971                 transform = &fsurf->workspace_transform;
972         } else {
973                 struct shell_surface *shsurf = get_shell_surface(view->surface);
974                 transform = &shsurf->workspace_transform;
975         }
976
977         if (wl_list_empty(&transform->link))
978                 wl_list_insert(view->geometry.transformation_list.prev,
979                                &transform->link);
980
981         weston_matrix_init(&transform->matrix);
982         weston_matrix_translate(&transform->matrix,
983                                 0.0, d, 0.0);
984         weston_view_geometry_dirty(view);
985 }
986
987 static void
988 workspace_translate_out(struct workspace *ws, double fraction)
989 {
990         struct weston_view *view;
991         unsigned int height;
992         double d;
993
994         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
995                 height = get_output_height(view->surface->output);
996                 d = height * fraction;
997
998                 view_translate(ws, view, d);
999         }
1000 }
1001
1002 static void
1003 workspace_translate_in(struct workspace *ws, double fraction)
1004 {
1005         struct weston_view *view;
1006         unsigned int height;
1007         double d;
1008
1009         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1010                 height = get_output_height(view->surface->output);
1011
1012                 if (fraction > 0)
1013                         d = -(height - height * fraction);
1014                 else
1015                         d = height + height * fraction;
1016
1017                 view_translate(ws, view, d);
1018         }
1019 }
1020
1021 static void
1022 reverse_workspace_change_animation(struct desktop_shell *shell,
1023                                    unsigned int index,
1024                                    struct workspace *from,
1025                                    struct workspace *to)
1026 {
1027         shell->workspaces.current = index;
1028
1029         shell->workspaces.anim_to = to;
1030         shell->workspaces.anim_from = from;
1031         shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1032         shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1033
1034         weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1035         weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1036
1037         weston_compositor_schedule_repaint(shell->compositor);
1038 }
1039
1040 static void
1041 workspace_deactivate_transforms(struct workspace *ws)
1042 {
1043         struct weston_view *view;
1044         struct weston_transform *transform;
1045
1046         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1047                 if (is_focus_view(view)) {
1048                         struct focus_surface *fsurf = get_focus_surface(view->surface);
1049                         transform = &fsurf->workspace_transform;
1050                 } else {
1051                         struct shell_surface *shsurf = get_shell_surface(view->surface);
1052                         transform = &shsurf->workspace_transform;
1053                 }
1054
1055                 if (!wl_list_empty(&transform->link)) {
1056                         wl_list_remove(&transform->link);
1057                         wl_list_init(&transform->link);
1058                 }
1059                 weston_view_geometry_dirty(view);
1060         }
1061 }
1062
1063 static void
1064 finish_workspace_change_animation(struct desktop_shell *shell,
1065                                   struct workspace *from,
1066                                   struct workspace *to)
1067 {
1068         struct weston_view *view;
1069
1070         weston_compositor_schedule_repaint(shell->compositor);
1071
1072         /* Views that extend past the bottom of the output are still
1073          * visible after the workspace animation ends but before its layer
1074          * is hidden. In that case, we need to damage below those views so
1075          * that the screen is properly repainted. */
1076         wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1077                 weston_view_damage_below(view);
1078
1079         wl_list_remove(&shell->workspaces.animation.link);
1080         workspace_deactivate_transforms(from);
1081         workspace_deactivate_transforms(to);
1082         shell->workspaces.anim_to = NULL;
1083
1084         weston_layer_unset_position(&shell->workspaces.anim_from->layer);
1085 }
1086
1087 static void
1088 animate_workspace_change_frame(struct weston_animation *animation,
1089                                struct weston_output *output,
1090                                const struct timespec *time)
1091 {
1092         struct desktop_shell *shell =
1093                 container_of(animation, struct desktop_shell,
1094                              workspaces.animation);
1095         struct workspace *from = shell->workspaces.anim_from;
1096         struct workspace *to = shell->workspaces.anim_to;
1097         int64_t t;
1098         double x, y;
1099
1100         if (workspace_is_empty(from) && workspace_is_empty(to)) {
1101                 finish_workspace_change_animation(shell, from, to);
1102                 return;
1103         }
1104
1105         if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
1106                 if (shell->workspaces.anim_current == 0.0)
1107                         shell->workspaces.anim_timestamp = *time;
1108                 else
1109                         timespec_add_msec(&shell->workspaces.anim_timestamp,
1110                                 time,
1111                                 /* Invers of movement function 'y' below. */
1112                                 -(asin(1.0 - shell->workspaces.anim_current) *
1113                                   DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1114                                   M_2_PI));
1115         }
1116
1117         t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
1118
1119         /*
1120          * x = [0, π/2]
1121          * y(x) = sin(x)
1122          */
1123         x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1124         y = sin(x);
1125
1126         if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1127                 weston_compositor_schedule_repaint(shell->compositor);
1128
1129                 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1130                 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1131                 shell->workspaces.anim_current = y;
1132
1133                 weston_compositor_schedule_repaint(shell->compositor);
1134         }
1135         else
1136                 finish_workspace_change_animation(shell, from, to);
1137 }
1138
1139 static void
1140 animate_workspace_change(struct desktop_shell *shell,
1141                          unsigned int index,
1142                          struct workspace *from,
1143                          struct workspace *to)
1144 {
1145         struct weston_output *output;
1146
1147         int dir;
1148
1149         if (index > shell->workspaces.current)
1150                 dir = -1;
1151         else
1152                 dir = 1;
1153
1154         shell->workspaces.current = index;
1155
1156         shell->workspaces.anim_dir = dir;
1157         shell->workspaces.anim_from = from;
1158         shell->workspaces.anim_to = to;
1159         shell->workspaces.anim_current = 0.0;
1160         shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1161
1162         output = container_of(shell->compositor->output_list.next,
1163                               struct weston_output, link);
1164         wl_list_insert(&output->animation_list,
1165                        &shell->workspaces.animation.link);
1166
1167         weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1168         weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1169
1170         workspace_translate_in(to, 0);
1171
1172         restore_focus_state(shell, to);
1173
1174         weston_compositor_schedule_repaint(shell->compositor);
1175 }
1176
1177 static void
1178 update_workspace(struct desktop_shell *shell, unsigned int index,
1179                  struct workspace *from, struct workspace *to)
1180 {
1181         shell->workspaces.current = index;
1182         weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1183         weston_layer_unset_position(&from->layer);
1184 }
1185
1186 static void
1187 change_workspace(struct desktop_shell *shell, unsigned int index)
1188 {
1189         struct workspace *from;
1190         struct workspace *to;
1191         struct focus_state *state;
1192
1193         if (index == shell->workspaces.current)
1194                 return;
1195
1196         /* Don't change workspace when there is any fullscreen surfaces. */
1197         if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1198                 return;
1199
1200         from = get_current_workspace(shell);
1201         to = get_workspace(shell, index);
1202
1203         if (shell->workspaces.anim_from == to &&
1204             shell->workspaces.anim_to == from) {
1205                 restore_focus_state(shell, to);
1206                 reverse_workspace_change_animation(shell, index, from, to);
1207                 return;
1208         }
1209
1210         if (shell->workspaces.anim_to != NULL)
1211                 finish_workspace_change_animation(shell,
1212                                                   shell->workspaces.anim_from,
1213                                                   shell->workspaces.anim_to);
1214
1215         restore_focus_state(shell, to);
1216
1217         if (shell->focus_animation_type != ANIMATION_NONE) {
1218                 wl_list_for_each(state, &from->focus_list, link)
1219                         if (state->keyboard_focus)
1220                                 animate_focus_change(shell, from,
1221                                                      get_default_view(state->keyboard_focus), NULL);
1222
1223                 wl_list_for_each(state, &to->focus_list, link)
1224                         if (state->keyboard_focus)
1225                                 animate_focus_change(shell, to,
1226                                                      NULL, get_default_view(state->keyboard_focus));
1227         }
1228
1229         if (workspace_is_empty(to) && workspace_is_empty(from))
1230                 update_workspace(shell, index, from, to);
1231         else
1232                 animate_workspace_change(shell, index, from, to);
1233 }
1234
1235 static bool
1236 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1237 {
1238         struct wl_list *list = &ws->layer.view_list.link;
1239         struct wl_list *e;
1240
1241         if (wl_list_empty(list))
1242                 return false;
1243
1244         e = list->next;
1245
1246         if (e->next != list)
1247                 return false;
1248
1249         return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1250 }
1251
1252 static void
1253 surface_keyboard_focus_lost(struct weston_surface *surface)
1254 {
1255         struct weston_compositor *compositor = surface->compositor;
1256         struct weston_seat *seat;
1257         struct weston_surface *focus;
1258
1259         wl_list_for_each(seat, &compositor->seat_list, link) {
1260                 struct weston_keyboard *keyboard =
1261                         weston_seat_get_keyboard(seat);
1262
1263                 if (!keyboard)
1264                         continue;
1265
1266                 focus = weston_surface_get_main_surface(keyboard->focus);
1267                 if (focus == surface)
1268                         weston_keyboard_set_focus(keyboard, NULL);
1269         }
1270 }
1271
1272 static void
1273 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1274                                   struct weston_seat *seat,
1275                                   unsigned int index)
1276 {
1277         struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1278         struct weston_surface *surface;
1279         struct weston_view *view;
1280         struct shell_surface *shsurf;
1281         struct workspace *from;
1282         struct workspace *to;
1283         struct focus_state *state;
1284
1285         surface = weston_surface_get_main_surface(keyboard->focus);
1286         view = get_default_view(surface);
1287         if (view == NULL ||
1288             index == shell->workspaces.current ||
1289             is_focus_view(view))
1290                 return;
1291
1292         from = get_current_workspace(shell);
1293         to = get_workspace(shell, index);
1294
1295         weston_layer_entry_remove(&view->layer_link);
1296         weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1297
1298         shsurf = get_shell_surface(surface);
1299         if (shsurf != NULL)
1300                 shell_surface_update_child_surface_layers(shsurf);
1301
1302         replace_focus_state(shell, to, seat);
1303         drop_focus_state(shell, from, surface);
1304
1305         if (shell->workspaces.anim_from == to &&
1306             shell->workspaces.anim_to == from) {
1307                 reverse_workspace_change_animation(shell, index, from, to);
1308
1309                 return;
1310         }
1311
1312         if (shell->workspaces.anim_to != NULL)
1313                 finish_workspace_change_animation(shell,
1314                                                   shell->workspaces.anim_from,
1315                                                   shell->workspaces.anim_to);
1316
1317         if (workspace_is_empty(from) &&
1318             workspace_has_only(to, surface))
1319                 update_workspace(shell, index, from, to);
1320         else {
1321                 if (shsurf != NULL &&
1322                     wl_list_empty(&shsurf->workspace_transform.link))
1323                         wl_list_insert(&shell->workspaces.anim_sticky_list,
1324                                        &shsurf->workspace_transform.link);
1325
1326                 animate_workspace_change(shell, index, from, to);
1327         }
1328
1329         state = ensure_focus_state(shell, seat);
1330         if (state != NULL)
1331                 focus_state_set_focus(state, surface);
1332 }
1333
1334 static void
1335 touch_move_grab_down(struct weston_touch_grab *grab,
1336                      const struct timespec *time,
1337                      int touch_id, wl_fixed_t x, wl_fixed_t y)
1338 {
1339 }
1340
1341 static void
1342 touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1343                    int touch_id)
1344 {
1345         struct weston_touch_move_grab *move =
1346                 (struct weston_touch_move_grab *) container_of(
1347                         grab, struct shell_touch_grab, grab);
1348
1349         if (touch_id == 0)
1350                 move->active = 0;
1351
1352         if (grab->touch->num_tp == 0) {
1353                 shell_touch_grab_end(&move->base);
1354                 free(move);
1355         }
1356 }
1357
1358 static void
1359 touch_move_grab_motion(struct weston_touch_grab *grab,
1360                        const struct timespec *time, int touch_id,
1361                        wl_fixed_t x, wl_fixed_t y)
1362 {
1363         struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1364         struct shell_surface *shsurf = move->base.shsurf;
1365         struct weston_surface *es;
1366         int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1367         int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1368
1369         if (!shsurf || !move->active)
1370                 return;
1371
1372         es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1373
1374         weston_view_set_position(shsurf->view, dx, dy);
1375
1376         weston_compositor_schedule_repaint(es->compositor);
1377 }
1378
1379 static void
1380 touch_move_grab_frame(struct weston_touch_grab *grab)
1381 {
1382 }
1383
1384 static void
1385 touch_move_grab_cancel(struct weston_touch_grab *grab)
1386 {
1387         struct weston_touch_move_grab *move =
1388                 (struct weston_touch_move_grab *) container_of(
1389                         grab, struct shell_touch_grab, grab);
1390
1391         shell_touch_grab_end(&move->base);
1392         free(move);
1393 }
1394
1395 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1396         touch_move_grab_down,
1397         touch_move_grab_up,
1398         touch_move_grab_motion,
1399         touch_move_grab_frame,
1400         touch_move_grab_cancel,
1401 };
1402
1403 static int
1404 surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
1405 {
1406         struct weston_touch_move_grab *move;
1407
1408         if (!shsurf)
1409                 return -1;
1410
1411         if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1412             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1413                 return 0;
1414
1415         move = malloc(sizeof *move);
1416         if (!move)
1417                 return -1;
1418
1419         move->active = 1;
1420         move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1421                    touch->grab_x;
1422         move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1423                    touch->grab_y;
1424
1425         shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1426                                touch);
1427
1428         return 0;
1429 }
1430
1431 static void
1432 noop_grab_focus(struct weston_pointer_grab *grab)
1433 {
1434 }
1435
1436 static void
1437 noop_grab_axis(struct weston_pointer_grab *grab,
1438                const struct timespec *time,
1439                struct weston_pointer_axis_event *event)
1440 {
1441 }
1442
1443 static void
1444 noop_grab_axis_source(struct weston_pointer_grab *grab,
1445                       uint32_t source)
1446 {
1447 }
1448
1449 static void
1450 noop_grab_frame(struct weston_pointer_grab *grab)
1451 {
1452 }
1453
1454 static void
1455 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1456 {
1457         struct shell_surface *shsurf = move->base.shsurf;
1458         struct weston_surface *surface =
1459                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1460         struct weston_pointer *pointer = move->base.grab.pointer;
1461         int x, y, bottom;
1462         const int safety = 50;
1463         pixman_rectangle32_t area;
1464         struct weston_geometry geometry;
1465
1466         x = wl_fixed_to_int(pointer->x + move->dx);
1467         y = wl_fixed_to_int(pointer->y + move->dy);
1468
1469         if (shsurf->shell->panel_position ==
1470             WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
1471                 get_output_work_area(shsurf->shell, surface->output, &area);
1472                 geometry =
1473                         weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1474
1475                 bottom = y + geometry.height + geometry.y;
1476                 if (bottom - safety < area.y)
1477                         y = area.y + safety - geometry.height
1478                           - geometry.y;
1479
1480                 if (move->client_initiated &&
1481                     y + geometry.y < area.y)
1482                         y = area.y - geometry.y;
1483         }
1484
1485         *cx = x;
1486         *cy = y;
1487 }
1488
1489 static void
1490 move_grab_motion(struct weston_pointer_grab *grab,
1491                  const struct timespec *time,
1492                  struct weston_pointer_motion_event *event)
1493 {
1494         struct weston_move_grab *move = (struct weston_move_grab *) grab;
1495         struct weston_pointer *pointer = grab->pointer;
1496         struct shell_surface *shsurf = move->base.shsurf;
1497         struct weston_surface *surface;
1498         int cx, cy;
1499
1500         weston_pointer_move(pointer, event);
1501         if (!shsurf)
1502                 return;
1503
1504         surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1505
1506         constrain_position(move, &cx, &cy);
1507
1508         weston_view_set_position(shsurf->view, cx, cy);
1509
1510         weston_compositor_schedule_repaint(surface->compositor);
1511 }
1512
1513 static void
1514 move_grab_button(struct weston_pointer_grab *grab,
1515                  const struct timespec *time, uint32_t button, uint32_t state_w)
1516 {
1517         struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1518                                                     grab);
1519         struct weston_pointer *pointer = grab->pointer;
1520         enum wl_pointer_button_state state = state_w;
1521
1522         if (pointer->button_count == 0 &&
1523             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1524                 shell_grab_end(shell_grab);
1525                 free(grab);
1526         }
1527 }
1528
1529 static void
1530 move_grab_cancel(struct weston_pointer_grab *grab)
1531 {
1532         struct shell_grab *shell_grab =
1533                 container_of(grab, struct shell_grab, grab);
1534
1535         shell_grab_end(shell_grab);
1536         free(grab);
1537 }
1538
1539 static const struct weston_pointer_grab_interface move_grab_interface = {
1540         noop_grab_focus,
1541         move_grab_motion,
1542         move_grab_button,
1543         noop_grab_axis,
1544         noop_grab_axis_source,
1545         noop_grab_frame,
1546         move_grab_cancel,
1547 };
1548
1549 static int
1550 surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
1551              bool client_initiated)
1552 {
1553         struct weston_move_grab *move;
1554
1555         if (!shsurf)
1556                 return -1;
1557
1558         if (shsurf->grabbed ||
1559             weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1560             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1561                 return 0;
1562
1563         move = malloc(sizeof *move);
1564         if (!move)
1565                 return -1;
1566
1567         move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1568                    pointer->grab_x;
1569         move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1570                    pointer->grab_y;
1571         move->client_initiated = client_initiated;
1572
1573         shell_grab_start(&move->base, &move_grab_interface, shsurf,
1574                          pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
1575
1576         return 0;
1577 }
1578
1579 struct weston_resize_grab {
1580         struct shell_grab base;
1581         uint32_t edges;
1582         int32_t width, height;
1583 };
1584
1585 static void
1586 resize_grab_motion(struct weston_pointer_grab *grab,
1587                    const struct timespec *time,
1588                    struct weston_pointer_motion_event *event)
1589 {
1590         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1591         struct weston_pointer *pointer = grab->pointer;
1592         struct shell_surface *shsurf = resize->base.shsurf;
1593         int32_t width, height;
1594         struct weston_size min_size, max_size;
1595         wl_fixed_t from_x, from_y;
1596         wl_fixed_t to_x, to_y;
1597
1598         weston_pointer_move(pointer, event);
1599
1600         if (!shsurf)
1601                 return;
1602
1603         weston_view_from_global_fixed(shsurf->view,
1604                                       pointer->grab_x, pointer->grab_y,
1605                                       &from_x, &from_y);
1606         weston_view_from_global_fixed(shsurf->view,
1607                                       pointer->x, pointer->y, &to_x, &to_y);
1608
1609         width = resize->width;
1610         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1611                 width += wl_fixed_to_int(from_x - to_x);
1612         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1613                 width += wl_fixed_to_int(to_x - from_x);
1614         }
1615
1616         height = resize->height;
1617         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1618                 height += wl_fixed_to_int(from_y - to_y);
1619         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1620                 height += wl_fixed_to_int(to_y - from_y);
1621         }
1622
1623         max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1624         min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1625
1626         min_size.width = MAX(1, min_size.width);
1627         min_size.height = MAX(1, min_size.height);
1628
1629         if (width < min_size.width)
1630                 width = min_size.width;
1631         else if (max_size.width > 0 && width > max_size.width)
1632                 width = max_size.width;
1633         if (height < min_size.height)
1634                 height = min_size.height;
1635         else if (max_size.width > 0 && width > max_size.width)
1636                 width = max_size.width;
1637         weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
1638 }
1639
1640 static void
1641 resize_grab_button(struct weston_pointer_grab *grab,
1642                    const struct timespec *time,
1643                    uint32_t button, uint32_t state_w)
1644 {
1645         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1646         struct weston_pointer *pointer = grab->pointer;
1647         enum wl_pointer_button_state state = state_w;
1648         struct weston_desktop_surface *desktop_surface =
1649                 resize->base.shsurf->desktop_surface;
1650
1651         if (pointer->button_count == 0 &&
1652             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1653                 weston_desktop_surface_set_resizing(desktop_surface, false);
1654                 shell_grab_end(&resize->base);
1655                 free(grab);
1656         }
1657 }
1658
1659 static void
1660 resize_grab_cancel(struct weston_pointer_grab *grab)
1661 {
1662         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1663         struct weston_desktop_surface *desktop_surface =
1664                 resize->base.shsurf->desktop_surface;
1665
1666         weston_desktop_surface_set_resizing(desktop_surface, false);
1667         shell_grab_end(&resize->base);
1668         free(grab);
1669 }
1670
1671 static const struct weston_pointer_grab_interface resize_grab_interface = {
1672         noop_grab_focus,
1673         resize_grab_motion,
1674         resize_grab_button,
1675         noop_grab_axis,
1676         noop_grab_axis_source,
1677         noop_grab_frame,
1678         resize_grab_cancel,
1679 };
1680
1681 /*
1682  * Returns the bounding box of a surface and all its sub-surfaces,
1683  * in surface-local coordinates. */
1684 static void
1685 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1686                                 int32_t *y, int32_t *w, int32_t *h) {
1687         pixman_region32_t region;
1688         pixman_box32_t *box;
1689         struct weston_subsurface *subsurface;
1690
1691         pixman_region32_init_rect(&region, 0, 0,
1692                                   surface->width,
1693                                   surface->height);
1694
1695         wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1696                 pixman_region32_union_rect(&region, &region,
1697                                            subsurface->position.x,
1698                                            subsurface->position.y,
1699                                            subsurface->surface->width,
1700                                            subsurface->surface->height);
1701         }
1702
1703         box = pixman_region32_extents(&region);
1704         if (x)
1705                 *x = box->x1;
1706         if (y)
1707                 *y = box->y1;
1708         if (w)
1709                 *w = box->x2 - box->x1;
1710         if (h)
1711                 *h = box->y2 - box->y1;
1712
1713         pixman_region32_fini(&region);
1714 }
1715
1716 static int
1717 surface_resize(struct shell_surface *shsurf,
1718                struct weston_pointer *pointer, uint32_t edges)
1719 {
1720         struct weston_resize_grab *resize;
1721         const unsigned resize_topbottom =
1722                 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1723         const unsigned resize_leftright =
1724                 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1725         const unsigned resize_any = resize_topbottom | resize_leftright;
1726         struct weston_geometry geometry;
1727
1728         if (shsurf->grabbed ||
1729             weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1730             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1731                 return 0;
1732
1733         /* Check for invalid edge combinations. */
1734         if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1735             (edges & resize_topbottom) == resize_topbottom ||
1736             (edges & resize_leftright) == resize_leftright)
1737                 return 0;
1738
1739         resize = malloc(sizeof *resize);
1740         if (!resize)
1741                 return -1;
1742
1743         resize->edges = edges;
1744
1745         geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1746         resize->width = geometry.width;
1747         resize->height = geometry.height;
1748
1749         shsurf->resize_edges = edges;
1750         weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
1751         shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1752                          pointer, edges);
1753
1754         return 0;
1755 }
1756
1757 static void
1758 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1759 {
1760         struct shell_grab *grab = (struct shell_grab *) base;
1761         struct weston_pointer *pointer = base->pointer;
1762         struct weston_desktop_surface *desktop_surface;
1763         struct weston_view *view;
1764         wl_fixed_t sx, sy;
1765
1766         view = weston_compositor_pick_view(pointer->seat->compositor,
1767                                            pointer->x, pointer->y,
1768                                            &sx, &sy);
1769         desktop_surface = weston_surface_get_desktop_surface(view->surface);
1770
1771         if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
1772                 shell_grab_end(grab);
1773                 free(grab);
1774         }
1775 }
1776
1777 static void
1778 busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1779                         const struct timespec *time,
1780                         struct weston_pointer_motion_event *event)
1781 {
1782         weston_pointer_move(grab->pointer, event);
1783 }
1784
1785 static void
1786 busy_cursor_grab_button(struct weston_pointer_grab *base,
1787                         const struct timespec *time,
1788                         uint32_t button, uint32_t state)
1789 {
1790         struct shell_grab *grab = (struct shell_grab *) base;
1791         struct shell_surface *shsurf = grab->shsurf;
1792         struct weston_pointer *pointer = grab->grab.pointer;
1793         struct weston_seat *seat = pointer->seat;
1794
1795         if (shsurf && button == BTN_LEFT && state) {
1796                 activate(shsurf->shell, shsurf->view, seat,
1797                          WESTON_ACTIVATE_FLAG_CONFIGURE);
1798                 surface_move(shsurf, pointer, false);
1799         } else if (shsurf && button == BTN_RIGHT && state) {
1800                 activate(shsurf->shell, shsurf->view, seat,
1801                          WESTON_ACTIVATE_FLAG_CONFIGURE);
1802                 surface_rotate(shsurf, pointer);
1803         }
1804 }
1805
1806 static void
1807 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1808 {
1809         struct shell_grab *grab = (struct shell_grab *) base;
1810
1811         shell_grab_end(grab);
1812         free(grab);
1813 }
1814
1815 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1816         busy_cursor_grab_focus,
1817         busy_cursor_grab_motion,
1818         busy_cursor_grab_button,
1819         noop_grab_axis,
1820         noop_grab_axis_source,
1821         noop_grab_frame,
1822         busy_cursor_grab_cancel,
1823 };
1824
1825 static void
1826 handle_pointer_focus(struct wl_listener *listener, void *data)
1827 {
1828         struct weston_pointer *pointer = data;
1829         struct weston_view *view = pointer->focus;
1830         struct shell_surface *shsurf;
1831         struct weston_desktop_client *client;
1832
1833         if (!view)
1834                 return;
1835
1836         shsurf = get_shell_surface(view->surface);
1837         if (!shsurf)
1838                 return;
1839
1840         client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1841
1842         if (shsurf->unresponsive)
1843                 set_busy_cursor(shsurf, pointer);
1844         else
1845                 weston_desktop_client_ping(client);
1846 }
1847
1848 static void
1849 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1850 {
1851         if (--shsurf->focus_count == 0)
1852                 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
1853 }
1854
1855 static void
1856 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1857 {
1858         if (shsurf->focus_count++ == 0)
1859                 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
1860 }
1861
1862 static void
1863 handle_keyboard_focus(struct wl_listener *listener, void *data)
1864 {
1865         struct weston_keyboard *keyboard = data;
1866         struct shell_seat *seat = get_shell_seat(keyboard->seat);
1867
1868         if (seat->focused_surface) {
1869                 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1870                 if (shsurf)
1871                         shell_surface_lose_keyboard_focus(shsurf);
1872         }
1873
1874         seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
1875
1876         if (seat->focused_surface) {
1877                 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1878                 if (shsurf)
1879                         shell_surface_gain_keyboard_focus(shsurf);
1880         }
1881 }
1882
1883 /* The surface will be inserted into the list immediately after the link
1884  * returned by this function (i.e. will be stacked immediately above the
1885  * returned link). */
1886 static struct weston_layer_entry *
1887 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1888 {
1889         struct workspace *ws;
1890
1891         if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1892             !shsurf->state.lowered) {
1893                 return &shsurf->shell->fullscreen_layer.view_list;
1894         }
1895
1896         /* Move the surface to a normal workspace layer so that surfaces
1897          * which were previously fullscreen or transient are no longer
1898          * rendered on top. */
1899         ws = get_current_workspace(shsurf->shell);
1900         return &ws->layer.view_list;
1901 }
1902
1903 static void
1904 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1905 {
1906         weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
1907 }
1908
1909 /* Update the surface’s layer. Mark both the old and new views as having dirty
1910  * geometry to ensure the changes are redrawn.
1911  *
1912  * If any child surfaces exist and are mapped, ensure they’re in the same layer
1913  * as this surface. */
1914 static void
1915 shell_surface_update_layer(struct shell_surface *shsurf)
1916 {
1917         struct weston_surface *surface =
1918                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1919         struct weston_layer_entry *new_layer_link;
1920
1921         new_layer_link = shell_surface_calculate_layer_link(shsurf);
1922
1923         if (new_layer_link == NULL)
1924                 return;
1925         if (new_layer_link == &shsurf->view->layer_link)
1926                 return;
1927
1928         weston_view_geometry_dirty(shsurf->view);
1929         weston_layer_entry_remove(&shsurf->view->layer_link);
1930         weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
1931         weston_view_geometry_dirty(shsurf->view);
1932         weston_surface_damage(surface);
1933
1934         shell_surface_update_child_surface_layers(shsurf);
1935 }
1936
1937 static void
1938 notify_output_destroy(struct wl_listener *listener, void *data)
1939 {
1940         struct shell_surface *shsurf =
1941                 container_of(listener,
1942                              struct shell_surface, output_destroy_listener);
1943
1944         shsurf->output = NULL;
1945         shsurf->output_destroy_listener.notify = NULL;
1946 }
1947
1948 static void
1949 shell_surface_set_output(struct shell_surface *shsurf,
1950                          struct weston_output *output)
1951 {
1952         struct weston_surface *es =
1953                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1954
1955         /* get the default output, if the client set it as NULL
1956            check whether the output is available */
1957         if (output)
1958                 shsurf->output = output;
1959         else if (es->output)
1960                 shsurf->output = es->output;
1961         else
1962                 shsurf->output = get_default_output(es->compositor);
1963
1964         if (shsurf->output_destroy_listener.notify) {
1965                 wl_list_remove(&shsurf->output_destroy_listener.link);
1966                 shsurf->output_destroy_listener.notify = NULL;
1967         }
1968
1969         if (!shsurf->output)
1970                 return;
1971
1972         shsurf->output_destroy_listener.notify = notify_output_destroy;
1973         wl_signal_add(&shsurf->output->destroy_signal,
1974                       &shsurf->output_destroy_listener);
1975 }
1976
1977 static void
1978 weston_view_set_initial_position(struct weston_view *view,
1979                                  struct desktop_shell *shell);
1980
1981 static void
1982 unset_fullscreen(struct shell_surface *shsurf)
1983 {
1984         /* Unset the fullscreen output, driver configuration and transforms. */
1985         wl_list_remove(&shsurf->fullscreen.transform.link);
1986         wl_list_init(&shsurf->fullscreen.transform.link);
1987
1988         if (shsurf->fullscreen.black_view)
1989                 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1990         shsurf->fullscreen.black_view = NULL;
1991
1992         if (shsurf->saved_position_valid)
1993                 weston_view_set_position(shsurf->view,
1994                                          shsurf->saved_x, shsurf->saved_y);
1995         else
1996                 weston_view_set_initial_position(shsurf->view, shsurf->shell);
1997         shsurf->saved_position_valid = false;
1998
1999         if (shsurf->saved_rotation_valid) {
2000                 wl_list_insert(&shsurf->view->geometry.transformation_list,
2001                                &shsurf->rotation.transform.link);
2002                 shsurf->saved_rotation_valid = false;
2003         }
2004 }
2005
2006 static void
2007 unset_maximized(struct shell_surface *shsurf)
2008 {
2009         struct weston_surface *surface =
2010                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2011
2012         /* undo all maximized things here */
2013         shell_surface_set_output(shsurf, get_default_output(surface->compositor));
2014
2015         if (shsurf->saved_position_valid)
2016                 weston_view_set_position(shsurf->view,
2017                                          shsurf->saved_x, shsurf->saved_y);
2018         else
2019                 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2020         shsurf->saved_position_valid = false;
2021
2022         if (shsurf->saved_rotation_valid) {
2023                 wl_list_insert(&shsurf->view->geometry.transformation_list,
2024                                &shsurf->rotation.transform.link);
2025                 shsurf->saved_rotation_valid = false;
2026         }
2027 }
2028
2029 static void
2030 set_minimized(struct weston_surface *surface)
2031 {
2032         struct shell_surface *shsurf;
2033         struct workspace *current_ws;
2034         struct weston_view *view;
2035
2036         view = get_default_view(surface);
2037         if (!view)
2038                 return;
2039
2040         assert(weston_surface_get_main_surface(view->surface) == view->surface);
2041
2042         shsurf = get_shell_surface(surface);
2043         current_ws = get_current_workspace(shsurf->shell);
2044
2045         weston_layer_entry_remove(&view->layer_link);
2046         weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2047
2048         drop_focus_state(shsurf->shell, current_ws, view->surface);
2049         surface_keyboard_focus_lost(surface);
2050
2051         shell_surface_update_child_surface_layers(shsurf);
2052         weston_view_damage_below(view);
2053 }
2054
2055
2056 static struct desktop_shell *
2057 shell_surface_get_shell(struct shell_surface *shsurf)
2058 {
2059         return shsurf->shell;
2060 }
2061
2062 static int
2063 black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2064 {
2065         struct weston_view *fs_view = surface->committed_private;
2066         struct weston_surface *fs_surface = fs_view->surface;
2067         int n;
2068         int rem;
2069         int ret;
2070
2071         n = snprintf(buf, len, "black background surface for ");
2072         if (n < 0)
2073                 return n;
2074
2075         rem = (int)len - n;
2076         if (rem < 0)
2077                 rem = 0;
2078
2079         if (fs_surface->get_label)
2080                 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2081         else
2082                 ret = snprintf(buf + n, rem, "<unknown>");
2083
2084         if (ret < 0)
2085                 return n;
2086
2087         return n + ret;
2088 }
2089
2090 static void
2091 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
2092
2093 static struct weston_view *
2094 create_black_surface(struct weston_compositor *ec,
2095                      struct weston_view *fs_view,
2096                      float x, float y, int w, int h)
2097 {
2098         struct weston_surface *surface = NULL;
2099         struct weston_view *view;
2100
2101         surface = weston_surface_create(ec);
2102         if (surface == NULL) {
2103                 weston_log("no memory\n");
2104                 return NULL;
2105         }
2106         view = weston_view_create(surface);
2107         if (surface == NULL) {
2108                 weston_log("no memory\n");
2109                 weston_surface_destroy(surface);
2110                 return NULL;
2111         }
2112
2113         surface->committed = black_surface_committed;
2114         surface->committed_private = fs_view;
2115         weston_surface_set_label_func(surface, black_surface_get_label);
2116         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2117         pixman_region32_fini(&surface->opaque);
2118         pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2119         pixman_region32_fini(&surface->input);
2120         pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2121
2122         weston_surface_set_size(surface, w, h);
2123         weston_view_set_position(view, x, y);
2124
2125         return view;
2126 }
2127
2128 static void
2129 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2130 {
2131         struct weston_surface *surface =
2132                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2133         struct weston_output *output = shsurf->fullscreen_output;
2134
2135         assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
2136
2137         if (!shsurf->fullscreen.black_view)
2138                 shsurf->fullscreen.black_view =
2139                         create_black_surface(surface->compositor,
2140                                              shsurf->view,
2141                                              output->x, output->y,
2142                                              output->width,
2143                                              output->height);
2144
2145         weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2146         weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2147         weston_layer_entry_insert(&shsurf->view->layer_link,
2148                                   &shsurf->fullscreen.black_view->layer_link);
2149         weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2150         weston_surface_damage(surface);
2151
2152         shsurf->fullscreen.black_view->is_mapped = true;
2153         shsurf->state.lowered = false;
2154 }
2155
2156 /* Create black surface and append it to the associated fullscreen surface.
2157  * Handle size dismatch and positioning according to the method. */
2158 static void
2159 shell_configure_fullscreen(struct shell_surface *shsurf)
2160 {
2161         struct weston_surface *surface =
2162                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2163         int32_t surf_x, surf_y, surf_width, surf_height;
2164
2165         /* Reverse the effect of lower_fullscreen_layer() */
2166         weston_layer_entry_remove(&shsurf->view->layer_link);
2167         weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2168                                   &shsurf->view->layer_link);
2169
2170         if (!shsurf->fullscreen_output) {
2171                 /* If there is no output, there's not much we can do.
2172                  * Position the window somewhere, whatever. */
2173                 weston_view_set_position(shsurf->view, 0, 0);
2174                 return;
2175         }
2176
2177         shell_ensure_fullscreen_black_view(shsurf);
2178
2179         surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
2180                                         &surf_width, &surf_height);
2181
2182         if (surface->buffer_ref.buffer)
2183                 center_on_output(shsurf->view, shsurf->fullscreen_output);
2184 }
2185
2186 static void
2187 shell_map_fullscreen(struct shell_surface *shsurf)
2188 {
2189         shell_configure_fullscreen(shsurf);
2190 }
2191
2192 static struct weston_output *
2193 get_focused_output(struct weston_compositor *compositor)
2194 {
2195         struct weston_seat *seat;
2196         struct weston_output *output = NULL;
2197
2198         wl_list_for_each(seat, &compositor->seat_list, link) {
2199                 struct weston_touch *touch = weston_seat_get_touch(seat);
2200                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2201                 struct weston_keyboard *keyboard =
2202                         weston_seat_get_keyboard(seat);
2203
2204                 /* Priority has touch focus, then pointer and
2205                  * then keyboard focus. We should probably have
2206                  * three for loops and check frist for touch,
2207                  * then for pointer, etc. but unless somebody has some
2208                  * objections, I think this is sufficient. */
2209                 if (touch && touch->focus)
2210                         output = touch->focus->output;
2211                 else if (pointer && pointer->focus)
2212                         output = pointer->focus->output;
2213                 else if (keyboard && keyboard->focus)
2214                         output = keyboard->focus->output;
2215
2216                 if (output)
2217                         break;
2218         }
2219
2220         return output;
2221 }
2222
2223 static void
2224 destroy_shell_seat(struct wl_listener *listener, void *data)
2225 {
2226         struct shell_seat *shseat =
2227                 container_of(listener,
2228                              struct shell_seat, seat_destroy_listener);
2229
2230         wl_list_remove(&shseat->seat_destroy_listener.link);
2231         free(shseat);
2232 }
2233
2234 static void
2235 shell_seat_caps_changed(struct wl_listener *listener, void *data)
2236 {
2237         struct weston_keyboard *keyboard;
2238         struct weston_pointer *pointer;
2239         struct shell_seat *seat;
2240
2241         seat = container_of(listener, struct shell_seat, caps_changed_listener);
2242         keyboard = weston_seat_get_keyboard(seat->seat);
2243         pointer = weston_seat_get_pointer(seat->seat);
2244
2245         if (keyboard &&
2246             wl_list_empty(&seat->keyboard_focus_listener.link)) {
2247                 wl_signal_add(&keyboard->focus_signal,
2248                               &seat->keyboard_focus_listener);
2249         } else if (!keyboard) {
2250                 wl_list_remove(&seat->keyboard_focus_listener.link);
2251                 wl_list_init(&seat->keyboard_focus_listener.link);
2252         }
2253
2254         if (pointer &&
2255             wl_list_empty(&seat->pointer_focus_listener.link)) {
2256                 wl_signal_add(&pointer->focus_signal,
2257                               &seat->pointer_focus_listener);
2258         } else if (!pointer) {
2259                 wl_list_remove(&seat->pointer_focus_listener.link);
2260                 wl_list_init(&seat->pointer_focus_listener.link);
2261         }
2262 }
2263
2264 static struct shell_seat *
2265 create_shell_seat(struct weston_seat *seat)
2266 {
2267         struct shell_seat *shseat;
2268
2269         shseat = calloc(1, sizeof *shseat);
2270         if (!shseat) {
2271                 weston_log("no memory to allocate shell seat\n");
2272                 return NULL;
2273         }
2274
2275         shseat->seat = seat;
2276
2277         shseat->seat_destroy_listener.notify = destroy_shell_seat;
2278         wl_signal_add(&seat->destroy_signal,
2279                       &shseat->seat_destroy_listener);
2280
2281         shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2282         wl_list_init(&shseat->keyboard_focus_listener.link);
2283
2284         shseat->pointer_focus_listener.notify = handle_pointer_focus;
2285         wl_list_init(&shseat->pointer_focus_listener.link);
2286
2287         shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2288         wl_signal_add(&seat->updated_caps_signal,
2289                       &shseat->caps_changed_listener);
2290         shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2291
2292         return shseat;
2293 }
2294
2295 static struct shell_seat *
2296 get_shell_seat(struct weston_seat *seat)
2297 {
2298         struct wl_listener *listener;
2299
2300         listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2301         assert(listener != NULL);
2302
2303         return container_of(listener,
2304                             struct shell_seat, seat_destroy_listener);
2305 }
2306
2307 static void
2308 fade_out_done_idle_cb(void *data)
2309 {
2310         struct shell_surface *shsurf = data;
2311
2312         weston_surface_destroy(shsurf->view->surface);
2313
2314         if (shsurf->output_destroy_listener.notify) {
2315                 wl_list_remove(&shsurf->output_destroy_listener.link);
2316                 shsurf->output_destroy_listener.notify = NULL;
2317         }
2318
2319         free(shsurf);
2320 }
2321
2322 static void
2323 fade_out_done(struct weston_view_animation *animation, void *data)
2324 {
2325         struct shell_surface *shsurf = data;
2326         struct wl_event_loop *loop;
2327
2328         loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
2329
2330         if (weston_view_is_mapped(shsurf->view)) {
2331                 shsurf->view->is_mapped = false;
2332                 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
2333         }
2334 }
2335
2336 struct shell_surface *
2337 get_shell_surface(struct weston_surface *surface)
2338 {
2339         if (weston_surface_is_desktop_surface(surface)) {
2340                 struct weston_desktop_surface *desktop_surface =
2341                         weston_surface_get_desktop_surface(surface);
2342                 return weston_desktop_surface_get_user_data(desktop_surface);
2343         }
2344         return NULL;
2345 }
2346
2347 /*
2348  * libweston-desktop
2349  */
2350
2351 static void
2352 desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2353                       void *shell)
2354 {
2355         struct weston_desktop_client *client =
2356                 weston_desktop_surface_get_client(desktop_surface);
2357         struct wl_client *wl_client =
2358                 weston_desktop_client_get_client(client);
2359         struct weston_view *view;
2360         struct shell_surface *shsurf;
2361         struct weston_surface *surface =
2362                 weston_desktop_surface_get_surface(desktop_surface);
2363
2364         view = weston_desktop_surface_create_view(desktop_surface);
2365         if (!view)
2366                 return;
2367
2368         shsurf = calloc(1, sizeof *shsurf);
2369         if (!shsurf) {
2370                 if (wl_client)
2371                         wl_client_post_no_memory(wl_client);
2372                 else
2373                         weston_log("no memory to allocate shell surface\n");
2374                 return;
2375         }
2376
2377         weston_surface_set_label_func(surface, shell_surface_get_label);
2378
2379         shsurf->shell = (struct desktop_shell *) shell;
2380         shsurf->unresponsive = 0;
2381         shsurf->saved_position_valid = false;
2382         shsurf->saved_rotation_valid = false;
2383         shsurf->desktop_surface = desktop_surface;
2384         shsurf->view = view;
2385         shsurf->fullscreen.black_view = NULL;
2386         wl_list_init(&shsurf->fullscreen.transform.link);
2387
2388         shell_surface_set_output(
2389                 shsurf, get_default_output(shsurf->shell->compositor));
2390
2391         wl_signal_init(&shsurf->destroy_signal);
2392
2393         /* empty when not in use */
2394         wl_list_init(&shsurf->rotation.transform.link);
2395         weston_matrix_init(&shsurf->rotation.rotation);
2396
2397         wl_list_init(&shsurf->workspace_transform.link);
2398
2399         weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2400         weston_desktop_surface_set_activated(desktop_surface,
2401                                              shsurf->focus_count > 0);
2402 }
2403
2404 static void
2405 desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2406                         void *shell)
2407 {
2408         struct shell_surface *shsurf =
2409                 weston_desktop_surface_get_user_data(desktop_surface);
2410         struct weston_surface *surface =
2411                 weston_desktop_surface_get_surface(desktop_surface);
2412
2413         if (!shsurf)
2414                 return;
2415
2416         wl_signal_emit(&shsurf->destroy_signal, shsurf);
2417
2418         if (shsurf->fullscreen.black_view)
2419                 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2420
2421         weston_surface_set_label_func(surface, NULL);
2422         weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2423         shsurf->desktop_surface = NULL;
2424
2425         weston_desktop_surface_unlink_view(shsurf->view);
2426         if (weston_surface_is_mapped(surface) &&
2427             shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2428                 pixman_region32_fini(&surface->pending.input);
2429                 pixman_region32_init(&surface->pending.input);
2430                 pixman_region32_fini(&surface->input);
2431                 pixman_region32_init(&surface->input);
2432                 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2433                                 fade_out_done, shsurf);
2434         } else {
2435                 weston_view_destroy(shsurf->view);
2436
2437                 if (shsurf->output_destroy_listener.notify) {
2438                         wl_list_remove(&shsurf->output_destroy_listener.link);
2439                         shsurf->output_destroy_listener.notify = NULL;
2440                 }
2441
2442                 free(shsurf);
2443         }
2444 }
2445
2446 static void
2447 set_maximized_position(struct desktop_shell *shell,
2448                        struct shell_surface *shsurf)
2449 {
2450         pixman_rectangle32_t area;
2451         struct weston_geometry geometry;
2452
2453         get_output_work_area(shell, shsurf->output, &area);
2454         geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2455
2456         weston_view_set_position(shsurf->view,
2457                                  area.x - geometry.x,
2458                                  area.y - geometry.y);
2459 }
2460
2461 static void
2462 set_position_from_xwayland(struct shell_surface *shsurf)
2463 {
2464         struct weston_geometry geometry;
2465         float x;
2466         float y;
2467
2468         assert(shsurf->xwayland.is_set);
2469
2470         geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2471         x = shsurf->xwayland.x - geometry.x;
2472         y = shsurf->xwayland.y - geometry.y;
2473
2474         weston_view_set_position(shsurf->view, x, y);
2475
2476 #ifdef WM_DEBUG
2477         weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2478                    __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2479                    geometry.x, geometry.y, x, y);
2480 #endif
2481 }
2482
2483 static void
2484 map(struct desktop_shell *shell, struct shell_surface *shsurf,
2485     int32_t sx, int32_t sy)
2486 {
2487         struct weston_surface *surface =
2488                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2489         struct weston_compositor *compositor = shell->compositor;
2490         struct weston_seat *seat;
2491
2492         /* initial positioning, see also configure() */
2493         if (shsurf->state.fullscreen) {
2494                 center_on_output(shsurf->view, shsurf->fullscreen_output);
2495                 shell_map_fullscreen(shsurf);
2496         } else if (shsurf->state.maximized) {
2497                 set_maximized_position(shell, shsurf);
2498         } else if (shsurf->xwayland.is_set) {
2499                 set_position_from_xwayland(shsurf);
2500         } else {
2501                 weston_view_set_initial_position(shsurf->view, shell);
2502         }
2503
2504         /* Surface stacking order, see also activate(). */
2505         shell_surface_update_layer(shsurf);
2506
2507         weston_view_update_transform(shsurf->view);
2508         shsurf->view->is_mapped = true;
2509         if (shsurf->state.maximized) {
2510                 surface->output = shsurf->output;
2511                 weston_view_set_output(shsurf->view, shsurf->output);
2512         }
2513
2514         if (!shell->locked) {
2515                 wl_list_for_each(seat, &compositor->seat_list, link)
2516                         activate(shell, shsurf->view, seat,
2517                                  WESTON_ACTIVATE_FLAG_CONFIGURE);
2518         }
2519
2520         if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
2521                 switch (shell->win_animation_type) {
2522                 case ANIMATION_FADE:
2523                         weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2524                         break;
2525                 case ANIMATION_ZOOM:
2526                         weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2527                         break;
2528                 case ANIMATION_NONE:
2529                 default:
2530                         break;
2531                 }
2532         }
2533 }
2534
2535 static void
2536 desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2537                           int32_t sx, int32_t sy, void *data)
2538 {
2539         struct shell_surface *shsurf =
2540                 weston_desktop_surface_get_user_data(desktop_surface);
2541         struct weston_surface *surface =
2542                 weston_desktop_surface_get_surface(desktop_surface);
2543         struct weston_view *view = shsurf->view;
2544         struct desktop_shell *shell = data;
2545         bool was_fullscreen;
2546         bool was_maximized;
2547
2548         if (surface->width == 0)
2549                 return;
2550
2551         was_fullscreen = shsurf->state.fullscreen;
2552         was_maximized = shsurf->state.maximized;
2553
2554         shsurf->state.fullscreen =
2555                 weston_desktop_surface_get_fullscreen(desktop_surface);
2556         shsurf->state.maximized =
2557                 weston_desktop_surface_get_maximized(desktop_surface);
2558
2559         if (!weston_surface_is_mapped(surface)) {
2560                 map(shell, shsurf, sx, sy);
2561                 surface->is_mapped = true;
2562                 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2563                         ++surface->ref_count;
2564                 return;
2565         }
2566
2567         if (sx == 0 && sy == 0 &&
2568             shsurf->last_width == surface->width &&
2569             shsurf->last_height == surface->height &&
2570             was_fullscreen == shsurf->state.fullscreen &&
2571             was_maximized == shsurf->state.maximized)
2572             return;
2573
2574         if (was_fullscreen)
2575                 unset_fullscreen(shsurf);
2576         if (was_maximized)
2577                 unset_maximized(shsurf);
2578
2579         if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2580             !shsurf->saved_position_valid) {
2581                 shsurf->saved_x = shsurf->view->geometry.x;
2582                 shsurf->saved_y = shsurf->view->geometry.y;
2583                 shsurf->saved_position_valid = true;
2584
2585                 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2586                         wl_list_remove(&shsurf->rotation.transform.link);
2587                         wl_list_init(&shsurf->rotation.transform.link);
2588                         weston_view_geometry_dirty(shsurf->view);
2589                         shsurf->saved_rotation_valid = true;
2590                 }
2591         }
2592
2593         if (shsurf->state.fullscreen) {
2594                 shell_configure_fullscreen(shsurf);
2595         } else if (shsurf->state.maximized) {
2596                 set_maximized_position(shell, shsurf);
2597                 surface->output = shsurf->output;
2598         } else {
2599                 float from_x, from_y;
2600                 float to_x, to_y;
2601                 float x, y;
2602
2603                 if (shsurf->resize_edges) {
2604                         sx = 0;
2605                         sy = 0;
2606                 }
2607
2608                 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2609                         sx = shsurf->last_width - surface->width;
2610                 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2611                         sy = shsurf->last_height - surface->height;
2612
2613                 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2614                 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2615                 x = shsurf->view->geometry.x + to_x - from_x;
2616                 y = shsurf->view->geometry.y + to_y - from_y;
2617
2618                 weston_view_set_position(shsurf->view, x, y);
2619         }
2620
2621         shsurf->last_width = surface->width;
2622         shsurf->last_height = surface->height;
2623
2624         /* XXX: would a fullscreen surface need the same handling? */
2625         if (surface->output) {
2626                 wl_list_for_each(view, &surface->views, surface_link)
2627                         weston_view_update_transform(view);
2628         }
2629 }
2630
2631 static void
2632 get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2633 {
2634         struct desktop_shell *shell;
2635         pixman_rectangle32_t area;
2636
2637         shell = shell_surface_get_shell(shsurf);
2638         get_output_work_area(shell, shsurf->output, &area);
2639
2640         *width = area.width;
2641         *height = area.height;
2642 }
2643
2644 static void
2645 set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2646                struct weston_output *output)
2647 {
2648         struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2649         struct weston_surface *surface =
2650                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2651         int32_t width = 0, height = 0;
2652
2653         if (fullscreen) {
2654                 /* handle clients launching in fullscreen */
2655                 if (output == NULL && !weston_surface_is_mapped(surface)) {
2656                         /* Set the output to the one that has focus currently. */
2657                         output = get_focused_output(surface->compositor);
2658                 }
2659
2660                 shell_surface_set_output(shsurf, output);
2661                 shsurf->fullscreen_output = shsurf->output;
2662
2663                 width = shsurf->output->width;
2664                 height = shsurf->output->height;
2665         } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2666                 get_maximized_size(shsurf, &width, &height);
2667         }
2668         weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2669         weston_desktop_surface_set_size(desktop_surface, width, height);
2670 }
2671
2672 static void
2673 desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2674                      struct weston_seat *seat, uint32_t serial, void *shell)
2675 {
2676         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2677         struct weston_touch *touch = weston_seat_get_touch(seat);
2678         struct shell_surface *shsurf =
2679                 weston_desktop_surface_get_user_data(desktop_surface);
2680         struct weston_surface *surface =
2681                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2682         struct wl_resource *resource = surface->resource;
2683         struct weston_surface *focus;
2684
2685         if (pointer &&
2686             pointer->focus &&
2687             pointer->button_count > 0 &&
2688             pointer->grab_serial == serial) {
2689                 focus = weston_surface_get_main_surface(pointer->focus->surface);
2690                 if ((focus == surface) &&
2691                     (surface_move(shsurf, pointer, true) < 0))
2692                         wl_resource_post_no_memory(resource);
2693         } else if (touch &&
2694                    touch->focus &&
2695                    touch->grab_serial == serial) {
2696                 focus = weston_surface_get_main_surface(touch->focus->surface);
2697                 if ((focus == surface) &&
2698                     (surface_touch_move(shsurf, touch) < 0))
2699                         wl_resource_post_no_memory(resource);
2700         }
2701 }
2702
2703 static void
2704 desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2705                        struct weston_seat *seat, uint32_t serial,
2706                        enum weston_desktop_surface_edge edges, void *shell)
2707 {
2708         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2709         struct shell_surface *shsurf =
2710                 weston_desktop_surface_get_user_data(desktop_surface);
2711         struct weston_surface *surface =
2712                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2713         struct wl_resource *resource = surface->resource;
2714         struct weston_surface *focus;
2715
2716         if (!pointer ||
2717             pointer->button_count == 0 ||
2718             pointer->grab_serial != serial ||
2719             pointer->focus == NULL)
2720                 return;
2721
2722         focus = weston_surface_get_main_surface(pointer->focus->surface);
2723         if (focus != surface)
2724                 return;
2725
2726         if (surface_resize(shsurf, pointer, edges) < 0)
2727                 wl_resource_post_no_memory(resource);
2728 }
2729
2730 static void
2731 desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2732                                      bool fullscreen,
2733                                      struct weston_output *output, void *shell)
2734 {
2735         struct shell_surface *shsurf =
2736                 weston_desktop_surface_get_user_data(desktop_surface);
2737
2738         set_fullscreen(shsurf, fullscreen, output);
2739 }
2740
2741 static void
2742 set_maximized(struct shell_surface *shsurf, bool maximized)
2743 {
2744         struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2745         struct weston_surface *surface =
2746                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2747         int32_t width = 0, height = 0;
2748
2749         if (maximized) {
2750                 struct weston_output *output;
2751
2752                 if (!weston_surface_is_mapped(surface))
2753                         output = get_focused_output(surface->compositor);
2754                 else
2755                         output = surface->output;
2756
2757                 shell_surface_set_output(shsurf, output);
2758
2759                 get_maximized_size(shsurf, &width, &height);
2760         }
2761         weston_desktop_surface_set_maximized(desktop_surface, maximized);
2762         weston_desktop_surface_set_size(desktop_surface, width, height);
2763 }
2764
2765 static void
2766 desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2767                                     bool maximized, void *shell)
2768 {
2769         struct shell_surface *shsurf =
2770                 weston_desktop_surface_get_user_data(desktop_surface);
2771
2772         set_maximized(shsurf, maximized);
2773 }
2774
2775 static void
2776 desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2777                                     void *shell)
2778 {
2779         struct weston_surface *surface =
2780                 weston_desktop_surface_get_surface(desktop_surface);
2781
2782          /* apply compositor's own minimization logic (hide) */
2783         set_minimized(surface);
2784 }
2785
2786 static void
2787 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2788 {
2789         struct shell_grab *grab;
2790
2791         if (pointer->grab->interface == &busy_cursor_grab_interface)
2792                 return;
2793
2794         grab = malloc(sizeof *grab);
2795         if (!grab)
2796                 return;
2797
2798         shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2799                          WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2800         /* Mark the shsurf as ungrabbed so that button binding is able
2801          * to move it. */
2802         shsurf->grabbed = 0;
2803 }
2804
2805 static void
2806 end_busy_cursor(struct weston_compositor *compositor,
2807                 struct weston_desktop_client *desktop_client)
2808 {
2809         struct shell_surface *shsurf;
2810         struct shell_grab *grab;
2811         struct weston_seat *seat;
2812
2813         wl_list_for_each(seat, &compositor->seat_list, link) {
2814                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2815                 struct weston_desktop_client *grab_client;
2816
2817                 if (!pointer)
2818                         continue;
2819
2820                 if (pointer->grab->interface != &busy_cursor_grab_interface)
2821                         continue;
2822
2823                 grab = (struct shell_grab *) pointer->grab;
2824                 shsurf = grab->shsurf;
2825                 if (!shsurf)
2826                         continue;
2827
2828                 grab_client =
2829                         weston_desktop_surface_get_client(shsurf->desktop_surface);
2830                 if (grab_client  == desktop_client) {
2831                         shell_grab_end(grab);
2832                         free(grab);
2833                 }
2834         }
2835 }
2836
2837 static void
2838 desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2839                                  void *user_data)
2840 {
2841         struct shell_surface *shsurf =
2842                 weston_desktop_surface_get_user_data(desktop_surface);
2843         bool *unresponsive = user_data;
2844
2845         shsurf->unresponsive = *unresponsive;
2846 }
2847
2848 static void
2849 desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2850                              void *shell_)
2851 {
2852         struct desktop_shell *shell = shell_;
2853         struct shell_surface *shsurf;
2854         struct weston_seat *seat;
2855         bool unresponsive = true;
2856
2857         weston_desktop_client_for_each_surface(desktop_client,
2858                                                desktop_surface_set_unresponsive,
2859                                                &unresponsive);
2860
2861
2862         wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2863                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2864                 struct weston_desktop_client *grab_client;
2865
2866                 if (!pointer || !pointer->focus)
2867                         continue;
2868
2869                 shsurf = get_shell_surface(pointer->focus->surface);
2870                 if (!shsurf)
2871                         continue;
2872
2873                 grab_client =
2874                         weston_desktop_surface_get_client(shsurf->desktop_surface);
2875                 if (grab_client == desktop_client)
2876                         set_busy_cursor(shsurf, pointer);
2877         }
2878 }
2879
2880 static void
2881 desktop_surface_pong(struct weston_desktop_client *desktop_client,
2882                      void *shell_)
2883 {
2884         struct desktop_shell *shell = shell_;
2885         bool unresponsive = false;
2886
2887         weston_desktop_client_for_each_surface(desktop_client,
2888                                                desktop_surface_set_unresponsive,
2889                                                &unresponsive);
2890         end_busy_cursor(shell->compositor, desktop_client);
2891 }
2892
2893 static void
2894 desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2895                                       int32_t x, int32_t y, void *shell_)
2896 {
2897         struct shell_surface *shsurf =
2898                 weston_desktop_surface_get_user_data(surface);
2899
2900         shsurf->xwayland.x = x;
2901         shsurf->xwayland.y = y;
2902         shsurf->xwayland.is_set = true;
2903 }
2904
2905 static const struct weston_desktop_api shell_desktop_api = {
2906         .struct_size = sizeof(struct weston_desktop_api),
2907         .surface_added = desktop_surface_added,
2908         .surface_removed = desktop_surface_removed,
2909         .committed = desktop_surface_committed,
2910         .move = desktop_surface_move,
2911         .resize = desktop_surface_resize,
2912         .fullscreen_requested = desktop_surface_fullscreen_requested,
2913         .maximized_requested = desktop_surface_maximized_requested,
2914         .minimized_requested = desktop_surface_minimized_requested,
2915         .ping_timeout = desktop_surface_ping_timeout,
2916         .pong = desktop_surface_pong,
2917         .set_xwayland_position = desktop_surface_set_xwayland_position,
2918 };
2919
2920 /* ************************ *
2921  * end of libweston-desktop *
2922  * ************************ */
2923 static void
2924 configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
2925 {
2926         struct weston_view *v, *next;
2927
2928         if (!ev->output)
2929                 return;
2930
2931         wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
2932                 if (v->output == ev->output && v != ev) {
2933                         weston_view_unmap(v);
2934                         v->surface->committed = NULL;
2935                         weston_surface_set_label_func(v->surface, NULL);
2936                 }
2937         }
2938
2939         weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
2940         ev->surface->is_mapped = true;
2941         ev->is_mapped = true;
2942
2943         if (wl_list_empty(&ev->layer_link.link)) {
2944                 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
2945                 weston_compositor_schedule_repaint(ev->surface->compositor);
2946         }
2947 }
2948
2949
2950 static struct shell_output *
2951 find_shell_output_from_weston_output(struct desktop_shell *shell,
2952                                      struct weston_output *output)
2953 {
2954         struct shell_output *shell_output;
2955
2956         wl_list_for_each(shell_output, &shell->output_list, link) {
2957                 if (shell_output->output == output)
2958                         return shell_output;
2959         }
2960
2961         return NULL;
2962 }
2963
2964 static int
2965 background_get_label(struct weston_surface *surface, char *buf, size_t len)
2966 {
2967         return snprintf(buf, len, "background for output %s",
2968                         surface->output->name);
2969 }
2970
2971 static void
2972 background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
2973 {
2974         struct desktop_shell *shell = es->committed_private;
2975         struct weston_view *view;
2976
2977         view = container_of(es->views.next, struct weston_view, surface_link);
2978
2979         configure_static_view(view, &shell->background_layer, 0, 0);
2980 }
2981
2982 static void
2983 handle_background_surface_destroy(struct wl_listener *listener, void *data)
2984 {
2985         struct shell_output *output =
2986             container_of(listener, struct shell_output, background_surface_listener);
2987
2988         weston_log("background surface gone\n");
2989         wl_list_remove(&output->background_surface_listener.link);
2990         output->background_surface = NULL;
2991 }
2992
2993 static void
2994 desktop_shell_set_background(struct wl_client *client,
2995                              struct wl_resource *resource,
2996                              struct wl_resource *output_resource,
2997                              struct wl_resource *surface_resource)
2998 {
2999         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3000         struct weston_surface *surface =
3001                 wl_resource_get_user_data(surface_resource);
3002         struct shell_output *sh_output;
3003         struct weston_view *view, *next;
3004
3005         if (surface->committed) {
3006                 wl_resource_post_error(surface_resource,
3007                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3008                                        "surface role already assigned");
3009                 return;
3010         }
3011
3012         wl_list_for_each_safe(view, next, &surface->views, surface_link)
3013                 weston_view_destroy(view);
3014         view = weston_view_create(surface);
3015
3016         surface->committed = background_committed;
3017         surface->committed_private = shell;
3018         weston_surface_set_label_func(surface, background_get_label);
3019         surface->output = weston_head_from_resource(output_resource)->output;
3020         weston_view_set_output(view, surface->output);
3021
3022         sh_output = find_shell_output_from_weston_output(shell, surface->output);
3023         if (sh_output->background_surface) {
3024                 /* The output already has a background, tell our helper
3025                  * there is no need for another one. */
3026                 weston_desktop_shell_send_configure(resource, 0,
3027                                                     surface_resource,
3028                                                     0, 0);
3029         } else {
3030                 weston_desktop_shell_send_configure(resource, 0,
3031                                                     surface_resource,
3032                                                     surface->output->width,
3033                                                     surface->output->height);
3034
3035                 sh_output->background_surface = surface;
3036
3037                 sh_output->background_surface_listener.notify =
3038                                         handle_background_surface_destroy;
3039                 wl_signal_add(&surface->destroy_signal,
3040                               &sh_output->background_surface_listener);
3041         }
3042 }
3043
3044 static int
3045 panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3046 {
3047         return snprintf(buf, len, "panel for output %s",
3048                         surface->output->name);
3049 }
3050
3051 static void
3052 panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3053 {
3054         struct desktop_shell *shell = es->committed_private;
3055         struct weston_view *view;
3056         int width, height;
3057         int x = 0, y = 0;
3058
3059         view = container_of(es->views.next, struct weston_view, surface_link);
3060
3061         get_panel_size(shell, view, &width, &height);
3062         switch (shell->panel_position) {
3063         case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3064                 break;
3065         case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3066                 y = view->output->height - height;
3067                 break;
3068         case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3069                 break;
3070         case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3071                 x = view->output->width - width;
3072                 break;
3073         }
3074
3075         configure_static_view(view, &shell->panel_layer, x, y);
3076 }
3077
3078 static void
3079 handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3080 {
3081         struct shell_output *output =
3082             container_of(listener, struct shell_output, panel_surface_listener);
3083
3084         weston_log("panel surface gone\n");
3085         wl_list_remove(&output->panel_surface_listener.link);
3086         output->panel_surface = NULL;
3087 }
3088
3089
3090 static void
3091 desktop_shell_set_panel(struct wl_client *client,
3092                         struct wl_resource *resource,
3093                         struct wl_resource *output_resource,
3094                         struct wl_resource *surface_resource)
3095 {
3096         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3097         struct weston_surface *surface =
3098                 wl_resource_get_user_data(surface_resource);
3099         struct weston_view *view, *next;
3100         struct shell_output *sh_output;
3101
3102         if (surface->committed) {
3103                 wl_resource_post_error(surface_resource,
3104                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
3105                                        "surface role already assigned");
3106                 return;
3107         }
3108
3109         wl_list_for_each_safe(view, next, &surface->views, surface_link)
3110                 weston_view_destroy(view);
3111         view = weston_view_create(surface);
3112
3113         surface->committed = panel_committed;
3114         surface->committed_private = shell;
3115         weston_surface_set_label_func(surface, panel_get_label);
3116         surface->output = weston_head_from_resource(output_resource)->output;
3117         weston_view_set_output(view, surface->output);
3118
3119         sh_output = find_shell_output_from_weston_output(shell, surface->output);
3120         if (sh_output->panel_surface) {
3121                 /* The output already has a panel, tell our helper
3122                  * there is no need for another one. */
3123                 weston_desktop_shell_send_configure(resource, 0,
3124                                                     surface_resource,
3125                                                     0, 0);
3126         } else {
3127                 weston_desktop_shell_send_configure(resource, 0,
3128                                                     surface_resource,
3129                                                     surface->output->width,
3130                                                     surface->output->height);
3131
3132                 sh_output->panel_surface = surface;
3133
3134                 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3135                 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3136         }
3137 }
3138
3139 static int
3140 lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3141 {
3142         return snprintf(buf, len, "lock window");
3143 }
3144
3145 static void
3146 lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
3147 {
3148         struct desktop_shell *shell = surface->committed_private;
3149         struct weston_view *view;
3150
3151         view = container_of(surface->views.next, struct weston_view, surface_link);
3152
3153         if (surface->width == 0)
3154                 return;
3155
3156         center_on_output(view, get_default_output(shell->compositor));
3157
3158         if (!weston_surface_is_mapped(surface)) {
3159                 weston_layer_entry_insert(&shell->lock_layer.view_list,
3160                                           &view->layer_link);
3161                 weston_view_update_transform(view);
3162                 surface->is_mapped = true;
3163                 view->is_mapped = true;
3164                 shell_fade(shell, FADE_IN);
3165         }
3166 }
3167
3168 static void
3169 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
3170 {
3171         struct desktop_shell *shell =
3172             container_of(listener, struct desktop_shell, lock_surface_listener);
3173
3174         weston_log("lock surface gone\n");
3175         shell->lock_surface = NULL;
3176 }
3177
3178 static void
3179 desktop_shell_set_lock_surface(struct wl_client *client,
3180                                struct wl_resource *resource,
3181                                struct wl_resource *surface_resource)
3182 {
3183         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3184         struct weston_surface *surface =
3185                 wl_resource_get_user_data(surface_resource);
3186
3187         shell->prepare_event_sent = false;
3188
3189         if (!shell->locked)
3190                 return;
3191
3192         shell->lock_surface = surface;
3193
3194         shell->lock_surface_listener.notify = handle_lock_surface_destroy;
3195         wl_signal_add(&surface->destroy_signal,
3196                       &shell->lock_surface_listener);
3197
3198         weston_view_create(surface);
3199         surface->committed = lock_surface_committed;
3200         surface->committed_private = shell;
3201         weston_surface_set_label_func(surface, lock_surface_get_label);
3202 }
3203
3204 static void
3205 resume_desktop(struct desktop_shell *shell)
3206 {
3207         struct workspace *ws = get_current_workspace(shell);
3208
3209         weston_layer_unset_position(&shell->lock_layer);
3210
3211         if (shell->showing_input_panels)
3212                 weston_layer_set_position(&shell->input_panel_layer,
3213                                           WESTON_LAYER_POSITION_TOP_UI);
3214         weston_layer_set_position(&shell->fullscreen_layer,
3215                                   WESTON_LAYER_POSITION_FULLSCREEN);
3216         weston_layer_set_position(&shell->panel_layer,
3217                                   WESTON_LAYER_POSITION_UI);
3218         weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
3219
3220         restore_focus_state(shell, get_current_workspace(shell));
3221
3222         shell->locked = false;
3223         shell_fade(shell, FADE_IN);
3224         weston_compositor_damage_all(shell->compositor);
3225 }
3226
3227 static void
3228 desktop_shell_unlock(struct wl_client *client,
3229                      struct wl_resource *resource)
3230 {
3231         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3232
3233         shell->prepare_event_sent = false;
3234
3235         if (shell->locked)
3236                 resume_desktop(shell);
3237 }
3238
3239 static void
3240 desktop_shell_set_grab_surface(struct wl_client *client,
3241                                struct wl_resource *resource,
3242                                struct wl_resource *surface_resource)
3243 {
3244         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3245
3246         shell->grab_surface = wl_resource_get_user_data(surface_resource);
3247         weston_view_create(shell->grab_surface);
3248 }
3249
3250 static void
3251 desktop_shell_desktop_ready(struct wl_client *client,
3252                             struct wl_resource *resource)
3253 {
3254         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3255
3256         shell_fade_startup(shell);
3257 }
3258
3259 static void
3260 desktop_shell_set_panel_position(struct wl_client *client,
3261                                  struct wl_resource *resource,
3262                                  uint32_t position)
3263 {
3264         struct desktop_shell *shell = wl_resource_get_user_data(resource);
3265
3266         if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3267             position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3268             position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3269             position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
3270                 wl_resource_post_error(resource,
3271                                        WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
3272                                        "bad position argument");
3273                 return;
3274         }
3275
3276         shell->panel_position = position;
3277 }
3278
3279 static const struct weston_desktop_shell_interface desktop_shell_implementation = {
3280         desktop_shell_set_background,
3281         desktop_shell_set_panel,
3282         desktop_shell_set_lock_surface,
3283         desktop_shell_unlock,
3284         desktop_shell_set_grab_surface,
3285         desktop_shell_desktop_ready,
3286         desktop_shell_set_panel_position
3287 };
3288
3289 static void
3290 move_binding(struct weston_pointer *pointer, const struct timespec *time,
3291              uint32_t button, void *data)
3292 {
3293         struct weston_surface *focus;
3294         struct weston_surface *surface;
3295         struct shell_surface *shsurf;
3296
3297         if (pointer->focus == NULL)
3298                 return;
3299
3300         focus = pointer->focus->surface;
3301
3302         surface = weston_surface_get_main_surface(focus);
3303         if (surface == NULL)
3304                 return;
3305
3306         shsurf = get_shell_surface(surface);
3307         if (shsurf == NULL ||
3308             weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3309             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3310                 return;
3311
3312         surface_move(shsurf, pointer, false);
3313 }
3314
3315 static void
3316 maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3317                  uint32_t button, void *data)
3318 {
3319         struct weston_surface *focus = keyboard->focus;
3320         struct weston_surface *surface;
3321         struct shell_surface *shsurf;
3322
3323         surface = weston_surface_get_main_surface(focus);
3324         if (surface == NULL)
3325                 return;
3326
3327         shsurf = get_shell_surface(surface);
3328         if (shsurf == NULL)
3329                 return;
3330
3331         set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
3332 }
3333
3334 static void
3335 fullscreen_binding(struct weston_keyboard *keyboard,
3336                    const struct timespec *time, uint32_t button, void *data)
3337 {
3338         struct weston_surface *focus = keyboard->focus;
3339         struct weston_surface *surface;
3340         struct shell_surface *shsurf;
3341         bool fullscreen;
3342
3343         surface = weston_surface_get_main_surface(focus);
3344         if (surface == NULL)
3345                 return;
3346
3347         shsurf = get_shell_surface(surface);
3348         if (shsurf == NULL)
3349                 return;
3350
3351         fullscreen =
3352                 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
3353
3354         set_fullscreen(shsurf, !fullscreen, NULL);
3355 }
3356
3357 static void
3358 touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
3359 {
3360         struct weston_surface *focus;
3361         struct weston_surface *surface;
3362         struct shell_surface *shsurf;
3363
3364         if (touch->focus == NULL)
3365                 return;
3366
3367         focus = touch->focus->surface;
3368         surface = weston_surface_get_main_surface(focus);
3369         if (surface == NULL)
3370                 return;
3371
3372         shsurf = get_shell_surface(surface);
3373         if (shsurf == NULL ||
3374             weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3375             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3376                 return;
3377
3378         surface_touch_move(shsurf, touch);
3379 }
3380
3381 static void
3382 resize_binding(struct weston_pointer *pointer, const struct timespec *time,
3383                uint32_t button, void *data)
3384 {
3385         struct weston_surface *focus;
3386         struct weston_surface *surface;
3387         uint32_t edges = 0;
3388         int32_t x, y;
3389         struct shell_surface *shsurf;
3390
3391         if (pointer->focus == NULL)
3392                 return;
3393
3394         focus = pointer->focus->surface;
3395
3396         surface = weston_surface_get_main_surface(focus);
3397         if (surface == NULL)
3398                 return;
3399
3400         shsurf = get_shell_surface(surface);
3401         if (shsurf == NULL ||
3402             weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3403             weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3404                 return;
3405
3406         weston_view_from_global(shsurf->view,
3407                                 wl_fixed_to_int(pointer->grab_x),
3408                                 wl_fixed_to_int(pointer->grab_y),
3409                                 &x, &y);
3410
3411         if (x < surface->width / 3)
3412                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
3413         else if (x < 2 * surface->width / 3)
3414                 edges |= 0;
3415         else
3416                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
3417
3418         if (y < surface->height / 3)
3419                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
3420         else if (y < 2 * surface->height / 3)
3421                 edges |= 0;
3422         else
3423                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
3424
3425         surface_resize(shsurf, pointer, edges);
3426 }
3427
3428 static void
3429 surface_opacity_binding(struct weston_pointer *pointer,
3430                         const struct timespec *time,
3431                         struct weston_pointer_axis_event *event,
3432                         void *data)
3433 {
3434         float step = 0.005;
3435         struct shell_surface *shsurf;
3436         struct weston_surface *focus = pointer->focus->surface;
3437         struct weston_surface *surface;
3438
3439         /* XXX: broken for windows containing sub-surfaces */
3440         surface = weston_surface_get_main_surface(focus);
3441         if (surface == NULL)
3442                 return;
3443
3444         shsurf = get_shell_surface(surface);
3445         if (!shsurf)
3446                 return;
3447
3448         shsurf->view->alpha -= event->value * step;
3449
3450         if (shsurf->view->alpha > 1.0)
3451                 shsurf->view->alpha = 1.0;
3452         if (shsurf->view->alpha < step)
3453                 shsurf->view->alpha = step;
3454
3455         weston_view_geometry_dirty(shsurf->view);
3456         weston_surface_damage(surface);
3457 }
3458
3459 static void
3460 do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3461         uint32_t axis, double value)
3462 {
3463         struct weston_compositor *compositor = seat->compositor;
3464         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3465         struct weston_output *output;
3466         float increment;
3467
3468         if (!pointer) {
3469                 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3470                 return;
3471         }
3472
3473         wl_list_for_each(output, &compositor->output_list, link) {
3474                 if (pixman_region32_contains_point(&output->region,
3475                                                    wl_fixed_to_double(pointer->x),
3476                                                    wl_fixed_to_double(pointer->y),
3477                                                    NULL)) {
3478                         if (key == KEY_PAGEUP)
3479                                 increment = output->zoom.increment;
3480                         else if (key == KEY_PAGEDOWN)
3481                                 increment = -output->zoom.increment;
3482                         else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
3483                                 /* For every pixel zoom 20th of a step */
3484                                 increment = output->zoom.increment *
3485                                             -value / 20.0;
3486                         else
3487                                 increment = 0;
3488
3489                         output->zoom.level += increment;
3490
3491                         if (output->zoom.level < 0.0)
3492                                 output->zoom.level = 0.0;
3493                         else if (output->zoom.level > output->zoom.max_level)
3494                                 output->zoom.level = output->zoom.max_level;
3495
3496                         if (!output->zoom.active) {
3497                                 if (output->zoom.level <= 0.0)
3498                                         continue;
3499                                 weston_output_activate_zoom(output, seat);
3500                         }
3501
3502                         output->zoom.spring_z.target = output->zoom.level;
3503
3504                         weston_output_update_zoom(output);
3505                 }
3506         }
3507 }
3508
3509 static void
3510 zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
3511                   struct weston_pointer_axis_event *event,
3512                   void *data)
3513 {
3514         do_zoom(pointer->seat, time, 0, event->axis, event->value);
3515 }
3516
3517 static void
3518 zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3519                  uint32_t key, void *data)
3520 {
3521         do_zoom(keyboard->seat, time, key, 0, 0);
3522 }
3523
3524 static void
3525 terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3526                   uint32_t key, void *data)
3527 {
3528         struct weston_compositor *compositor = data;
3529
3530         wl_display_terminate(compositor->wl_display);
3531 }
3532
3533 static void
3534 rotate_grab_motion(struct weston_pointer_grab *grab,
3535                    const struct timespec *time,
3536                    struct weston_pointer_motion_event *event)
3537 {
3538         struct rotate_grab *rotate =
3539                 container_of(grab, struct rotate_grab, base.grab);
3540         struct weston_pointer *pointer = grab->pointer;
3541         struct shell_surface *shsurf = rotate->base.shsurf;
3542         struct weston_surface *surface =
3543                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
3544         float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
3545
3546         weston_pointer_move(pointer, event);
3547
3548         if (!shsurf)
3549                 return;
3550
3551         cx = 0.5f * surface->width;
3552         cy = 0.5f * surface->height;
3553
3554         dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3555         dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3556         r = sqrtf(dx * dx + dy * dy);
3557
3558         wl_list_remove(&shsurf->rotation.transform.link);
3559         weston_view_geometry_dirty(shsurf->view);
3560
3561         if (r > 20.0f) {
3562                 struct weston_matrix *matrix =
3563                         &shsurf->rotation.transform.matrix;
3564
3565                 weston_matrix_init(&rotate->rotation);
3566                 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3567
3568                 weston_matrix_init(matrix);
3569                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
3570                 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
3571                 weston_matrix_multiply(matrix, &rotate->rotation);
3572                 weston_matrix_translate(matrix, cx, cy, 0.0f);
3573
3574                 wl_list_insert(
3575                         &shsurf->view->geometry.transformation_list,
3576                         &shsurf->rotation.transform.link);
3577         } else {
3578                 wl_list_init(&shsurf->rotation.transform.link);
3579                 weston_matrix_init(&shsurf->rotation.rotation);
3580                 weston_matrix_init(&rotate->rotation);
3581         }
3582
3583         /* We need to adjust the position of the surface
3584          * in case it was resized in a rotated state before */
3585         cposx = shsurf->view->geometry.x + cx;
3586         cposy = shsurf->view->geometry.y + cy;
3587         dposx = rotate->center.x - cposx;
3588         dposy = rotate->center.y - cposy;
3589         if (dposx != 0.0f || dposy != 0.0f) {
3590                 weston_view_set_position(shsurf->view,
3591                                          shsurf->view->geometry.x + dposx,
3592                                          shsurf->view->geometry.y + dposy);
3593         }
3594
3595         /* Repaint implies weston_view_update_transform(), which
3596          * lazily applies the damage due to rotation update.
3597          */
3598         weston_compositor_schedule_repaint(surface->compositor);
3599 }
3600
3601 static void
3602 rotate_grab_button(struct weston_pointer_grab *grab,
3603                    const struct timespec *time,
3604                    uint32_t button, uint32_t state_w)
3605 {
3606         struct rotate_grab *rotate =
3607                 container_of(grab, struct rotate_grab, base.grab);
3608         struct weston_pointer *pointer = grab->pointer;
3609         struct shell_surface *shsurf = rotate->base.shsurf;
3610         enum wl_pointer_button_state state = state_w;
3611
3612         if (pointer->button_count == 0 &&
3613             state == WL_POINTER_BUTTON_STATE_RELEASED) {
3614                 if (shsurf)
3615                         weston_matrix_multiply(&shsurf->rotation.rotation,
3616                                                &rotate->rotation);
3617                 shell_grab_end(&rotate->base);
3618                 free(rotate);
3619         }
3620 }
3621
3622 static void
3623 rotate_grab_cancel(struct weston_pointer_grab *grab)
3624 {
3625         struct rotate_grab *rotate =
3626                 container_of(grab, struct rotate_grab, base.grab);
3627
3628         shell_grab_end(&rotate->base);
3629         free(rotate);
3630 }
3631
3632 static const struct weston_pointer_grab_interface rotate_grab_interface = {
3633         noop_grab_focus,
3634         rotate_grab_motion,
3635         rotate_grab_button,
3636         noop_grab_axis,
3637         noop_grab_axis_source,
3638         noop_grab_frame,
3639         rotate_grab_cancel,
3640 };
3641
3642 static void
3643 surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
3644 {
3645         struct weston_surface *surface =
3646                 weston_desktop_surface_get_surface(shsurf->desktop_surface);
3647         struct rotate_grab *rotate;
3648         float dx, dy;
3649         float r;
3650
3651         rotate = malloc(sizeof *rotate);
3652         if (!rotate)
3653                 return;
3654
3655         weston_view_to_global_float(shsurf->view,
3656                                     surface->width * 0.5f,
3657                                     surface->height * 0.5f,
3658                                     &rotate->center.x, &rotate->center.y);
3659
3660         dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3661         dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3662         r = sqrtf(dx * dx + dy * dy);
3663         if (r > 20.0f) {
3664                 struct weston_matrix inverse;
3665
3666                 weston_matrix_init(&inverse);
3667                 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
3668                 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
3669
3670                 weston_matrix_init(&rotate->rotation);
3671                 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3672         } else {
3673                 weston_matrix_init(&shsurf->rotation.rotation);
3674                 weston_matrix_init(&rotate->rotation);
3675         }
3676
3677         shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
3678                          pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
3679 }
3680
3681 static void
3682 rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3683                uint32_t button, void *data)
3684 {
3685         struct weston_surface *focus;
3686         struct weston_surface *base_surface;
3687         struct shell_surface *surface;
3688
3689         if (pointer->focus == NULL)
3690                 return;
3691
3692         focus = pointer->focus->surface;
3693
3694         base_surface = weston_surface_get_main_surface(focus);
3695         if (base_surface == NULL)
3696                 return;
3697
3698         surface = get_shell_surface(base_surface);
3699         if (surface == NULL ||
3700             weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3701             weston_desktop_surface_get_maximized(surface->desktop_surface))
3702                 return;
3703
3704         surface_rotate(surface, pointer);
3705 }
3706
3707 /* Move all fullscreen layers down to the current workspace and hide their
3708  * black views. The surfaces' state is set to both fullscreen and lowered,
3709  * and this is reversed when such a surface is re-configured, see
3710  * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3711  *
3712  * lowering_output = NULL - Lower on all outputs, else only lower on the
3713  *                   specified output.
3714  *
3715  * This should be used when implementing shell-wide overlays, such as
3716  * the alt-tab switcher, which need to de-promote fullscreen layers. */
3717 void
3718 lower_fullscreen_layer(struct desktop_shell *shell,
3719                        struct weston_output *lowering_output)
3720 {
3721         struct workspace *ws;
3722         struct weston_view *view, *prev;
3723
3724         ws = get_current_workspace(shell);
3725         wl_list_for_each_reverse_safe(view, prev,
3726                                       &shell->fullscreen_layer.view_list.link,
3727                                       layer_link.link) {
3728                 struct shell_surface *shsurf = get_shell_surface(view->surface);
3729
3730                 if (!shsurf)
3731                         continue;
3732
3733                 /* Only lower surfaces which have lowering_output as their fullscreen
3734                  * output, unless a NULL output asks for lowering on all outputs.
3735                  */
3736                 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3737                         continue;
3738
3739                 /* We can have a non-fullscreen popup for a fullscreen surface
3740                  * in the fullscreen layer. */
3741                 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
3742                         /* Hide the black view */
3743                         weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3744                         wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
3745                         weston_view_damage_below(shsurf->fullscreen.black_view);
3746
3747                 }
3748
3749                 /* Lower the view to the workspace layer */
3750                 weston_layer_entry_remove(&view->layer_link);
3751                 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
3752                 weston_view_damage_below(view);
3753                 weston_surface_damage(view->surface);
3754
3755                 shsurf->state.lowered = true;
3756         }
3757 }
3758
3759 void
3760 activate(struct desktop_shell *shell, struct weston_view *view,
3761          struct weston_seat *seat, uint32_t flags)
3762 {
3763         struct weston_surface *es = view->surface;
3764         struct weston_surface *main_surface;
3765         struct focus_state *state;
3766         struct workspace *ws;
3767         struct weston_surface *old_es;
3768         struct shell_surface *shsurf;
3769
3770         main_surface = weston_surface_get_main_surface(es);
3771         shsurf = get_shell_surface(main_surface);
3772         assert(shsurf);
3773
3774         /* Only demote fullscreen surfaces on the output of activated shsurf.
3775          * Leave fullscreen surfaces on unrelated outputs alone. */
3776         if (shsurf->output)
3777                 lower_fullscreen_layer(shell, shsurf->output);
3778
3779         weston_view_activate(view, seat, flags);
3780
3781         state = ensure_focus_state(shell, seat);
3782         if (state == NULL)
3783                 return;
3784
3785         old_es = state->keyboard_focus;
3786         focus_state_set_focus(state, es);
3787
3788         if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3789             flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
3790                 shell_configure_fullscreen(shsurf);
3791
3792         /* Update the surface’s layer. This brings it to the top of the stacking
3793          * order as appropriate. */
3794         shell_surface_update_layer(shsurf);
3795
3796         if (shell->focus_animation_type != ANIMATION_NONE) {
3797                 ws = get_current_workspace(shell);
3798                 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
3799         }
3800 }
3801
3802 /* no-op func for checking black surface */
3803 static void
3804 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3805 {
3806 }
3807
3808 static bool
3809 is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
3810 {
3811         struct weston_surface *surface = view->surface;
3812
3813         if (surface->committed == black_surface_committed) {
3814                 if (fs_view)
3815                         *fs_view = surface->committed_private;
3816                 return true;
3817         }
3818         return false;
3819 }
3820
3821 static void
3822 activate_binding(struct weston_seat *seat,
3823                  struct desktop_shell *shell,
3824                  struct weston_view *focus_view,
3825                  uint32_t flags)
3826 {
3827         struct weston_view *main_view;
3828         struct weston_surface *main_surface;
3829
3830         if (!focus_view)
3831                 return;
3832
3833         if (is_black_surface_view(focus_view, &main_view))
3834                 focus_view = main_view;
3835
3836         main_surface = weston_surface_get_main_surface(focus_view->surface);
3837         if (!get_shell_surface(main_surface))
3838                 return;
3839
3840         activate(shell, focus_view, seat, flags);
3841 }
3842
3843 static void
3844 click_to_activate_binding(struct weston_pointer *pointer,
3845                           const struct timespec *time,
3846                           uint32_t button, void *data)
3847 {
3848         if (pointer->grab != &pointer->default_grab)
3849                 return;
3850         if (pointer->focus == NULL)
3851                 return;
3852
3853         activate_binding(pointer->seat, data, pointer->focus,
3854                          WESTON_ACTIVATE_FLAG_CLICKED |
3855                          WESTON_ACTIVATE_FLAG_CONFIGURE);
3856 }
3857
3858 static void
3859 touch_to_activate_binding(struct weston_touch *touch,
3860                           const struct timespec *time,
3861                           void *data)
3862 {
3863         if (touch->grab != &touch->default_grab)
3864                 return;
3865         if (touch->focus == NULL)
3866                 return;
3867
3868         activate_binding(touch->seat, data, touch->focus,
3869                          WESTON_ACTIVATE_FLAG_CONFIGURE);
3870 }
3871
3872 static void
3873 unfocus_all_seats(struct desktop_shell *shell)
3874 {
3875         struct weston_seat *seat, *next;
3876
3877         wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
3878                 struct weston_keyboard *keyboard =
3879                         weston_seat_get_keyboard(seat);
3880
3881                 if (!keyboard)
3882                         continue;
3883
3884                 weston_keyboard_set_focus(keyboard, NULL);
3885         }
3886 }
3887
3888 static void
3889 lock(struct desktop_shell *shell)
3890 {
3891         struct workspace *ws = get_current_workspace(shell);
3892
3893         if (shell->locked) {
3894                 weston_compositor_sleep(shell->compositor);
3895                 return;
3896         }
3897
3898         shell->locked = true;
3899
3900         /* Hide all surfaces by removing the fullscreen, panel and
3901          * toplevel layers.  This way nothing else can show or receive
3902          * input events while we are locked. */
3903
3904         weston_layer_unset_position(&shell->panel_layer);
3905         weston_layer_unset_position(&shell->fullscreen_layer);
3906         if (shell->showing_input_panels)
3907                 weston_layer_unset_position(&shell->input_panel_layer);
3908         weston_layer_unset_position(&ws->layer);
3909
3910         weston_layer_set_position(&shell->lock_layer,
3911                                   WESTON_LAYER_POSITION_LOCK);
3912
3913         weston_compositor_sleep(shell->compositor);
3914
3915         /* Remove the keyboard focus on all seats. This will be
3916          * restored to the workspace's saved state via
3917          * restore_focus_state when the compositor is unlocked */
3918         unfocus_all_seats(shell);
3919
3920         /* TODO: disable bindings that should not work while locked. */
3921
3922         /* All this must be undone in resume_desktop(). */
3923 }
3924
3925 static void
3926 unlock(struct desktop_shell *shell)
3927 {
3928         struct wl_resource *shell_resource;
3929
3930         if (!shell->locked || shell->lock_surface) {
3931                 shell_fade(shell, FADE_IN);
3932                 return;
3933         }
3934
3935         /* If desktop-shell client has gone away, unlock immediately. */
3936         if (!shell->child.desktop_shell) {
3937                 resume_desktop(shell);
3938                 return;
3939         }
3940
3941         if (shell->prepare_event_sent)
3942                 return;
3943
3944         shell_resource = shell->child.desktop_shell;
3945         weston_desktop_shell_send_prepare_lock_surface(shell_resource);
3946         shell->prepare_event_sent = true;
3947 }
3948
3949 static void
3950 shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
3951 {
3952         struct shell_output *shell_output = data;
3953         struct desktop_shell *shell = shell_output->shell;
3954
3955         shell_output->fade.animation = NULL;
3956         switch (shell_output->fade.type) {
3957         case FADE_IN:
3958                 weston_surface_destroy(shell_output->fade.view->surface);
3959                 shell_output->fade.view = NULL;
3960                 break;
3961         case FADE_OUT:
3962                 lock(shell);
3963                 break;
3964         default:
3965                 break;
3966         }
3967 }
3968
3969 static struct weston_view *
3970 shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
3971 {
3972         struct weston_compositor *compositor = shell->compositor;
3973         struct weston_surface *surface;
3974         struct weston_view *view;
3975
3976         surface = weston_surface_create(compositor);
3977         if (!surface)
3978                 return NULL;
3979
3980         view = weston_view_create(surface);
3981         if (!view) {
3982                 weston_surface_destroy(surface);
3983                 return NULL;
3984         }
3985
3986         weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3987         weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
3988         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3989         weston_layer_entry_insert(&compositor->fade_layer.view_list,
3990                                   &view->layer_link);
3991         pixman_region32_init(&surface->input);
3992         surface->is_mapped = true;
3993         view->is_mapped = true;
3994
3995         return view;
3996 }
3997
3998 static void
3999 shell_fade(struct desktop_shell *shell, enum fade_type type)
4000 {
4001         float tint;
4002         struct shell_output *shell_output;
4003
4004         switch (type) {
4005         case FADE_IN:
4006                 tint = 0.0;
4007                 break;
4008         case FADE_OUT:
4009                 tint = 1.0;
4010                 break;
4011         default:
4012                 weston_log("shell: invalid fade type\n");
4013                 return;
4014         }
4015
4016         /* Create a separate fade surface for each output */
4017         wl_list_for_each(shell_output, &shell->output_list, link) {
4018                 shell_output->fade.type = type;
4019
4020                 if (shell_output->fade.view == NULL) {
4021                         shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4022                         if (!shell_output->fade.view)
4023                                 continue;
4024
4025                         shell_output->fade.view->alpha = 1.0 - tint;
4026                         weston_view_update_transform(shell_output->fade.view);
4027                 }
4028
4029                 if (shell_output->fade.view->output == NULL) {
4030                         /* If the black view gets a NULL output, we lost the
4031                          * last output and we'll just cancel the fade.  This
4032                          * happens when you close the last window under the
4033                          * X11 or Wayland backends. */
4034                         shell->locked = false;
4035                         weston_surface_destroy(shell_output->fade.view->surface);
4036                         shell_output->fade.view = NULL;
4037                 } else if (shell_output->fade.animation) {
4038                         weston_fade_update(shell_output->fade.animation, tint);
4039                 } else {
4040                         shell_output->fade.animation =
4041                                 weston_fade_run(shell_output->fade.view,
4042                                                 1.0 - tint, tint, 300.0,
4043                                                 shell_fade_done_for_output, shell_output);
4044                 }
4045         }
4046 }
4047
4048 static void
4049 do_shell_fade_startup(void *data)
4050 {
4051         struct desktop_shell *shell = data;
4052         struct shell_output *shell_output;
4053
4054         if (shell->startup_animation_type == ANIMATION_FADE) {
4055                 shell_fade(shell, FADE_IN);
4056         } else {
4057                 weston_log("desktop shell: "
4058                            "unexpected fade-in animation type %d\n",
4059                            shell->startup_animation_type);
4060                 wl_list_for_each(shell_output, &shell->output_list, link) {
4061                         weston_surface_destroy(shell_output->fade.view->surface);
4062                         shell_output->fade.view = NULL;
4063                 }
4064         }
4065 }
4066
4067 static void
4068 shell_fade_startup(struct desktop_shell *shell)
4069 {
4070         struct wl_event_loop *loop;
4071         struct shell_output *shell_output;
4072         bool has_fade = false;
4073
4074         wl_list_for_each(shell_output, &shell->output_list, link) {
4075                 if (!shell_output->fade.startup_timer)
4076                         continue;
4077
4078                 wl_event_source_remove(shell_output->fade.startup_timer);
4079                 shell_output->fade.startup_timer = NULL;
4080                 has_fade = true;
4081         }
4082
4083         if (has_fade) {
4084                 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4085                 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4086         }
4087 }
4088
4089 static int
4090 fade_startup_timeout(void *data)
4091 {
4092         struct desktop_shell *shell = data;
4093
4094         shell_fade_startup(shell);
4095         return 0;
4096 }
4097
4098 static void
4099 shell_fade_init(struct desktop_shell *shell)
4100 {
4101         /* Make compositor output all black, and wait for the desktop-shell
4102          * client to signal it is ready, then fade in. The timer triggers a
4103          * fade-in, in case the desktop-shell client takes too long.
4104          */
4105
4106         struct wl_event_loop *loop;
4107         struct shell_output *shell_output;
4108
4109         if (shell->startup_animation_type == ANIMATION_NONE)
4110                 return;
4111
4112         wl_list_for_each(shell_output, &shell->output_list, link) {
4113                 if (shell_output->fade.view != NULL) {
4114                         weston_log("%s: warning: fade surface already exists\n",
4115                                    __func__);
4116                         continue;
4117                 }
4118
4119                 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4120                 if (!shell_output->fade.view)
4121                         continue;
4122
4123                 weston_view_update_transform(shell_output->fade.view);
4124                 weston_surface_damage(shell_output->fade.view->surface);
4125
4126                 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4127                 shell_output->fade.startup_timer =
4128                         wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4129                 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4130         }
4131 }
4132
4133 static void
4134 idle_handler(struct wl_listener *listener, void *data)
4135 {
4136         struct desktop_shell *shell =
4137                 container_of(listener, struct desktop_shell, idle_listener);
4138
4139         struct weston_seat *seat;
4140
4141         wl_list_for_each(seat, &shell->compositor->seat_list, link)
4142                 weston_seat_break_desktop_grabs(seat);
4143
4144         shell_fade(shell, FADE_OUT);
4145         /* lock() is called from shell_fade_done_for_output() */
4146 }
4147
4148 static void
4149 wake_handler(struct wl_listener *listener, void *data)
4150 {
4151         struct desktop_shell *shell =
4152                 container_of(listener, struct desktop_shell, wake_listener);
4153
4154         unlock(shell);
4155 }
4156
4157 static void
4158 transform_handler(struct wl_listener *listener, void *data)
4159 {
4160         struct weston_surface *surface = data;
4161         struct shell_surface *shsurf = get_shell_surface(surface);
4162         const struct weston_xwayland_surface_api *api;
4163         int x, y;
4164
4165         if (!shsurf)
4166                 return;
4167
4168         api = shsurf->shell->xwayland_surface_api;
4169         if (!api) {
4170                 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4171                 shsurf->shell->xwayland_surface_api = api;
4172         }
4173
4174         if (!api || !api->is_xwayland_surface(surface))
4175                 return;
4176
4177         if (!weston_view_is_mapped(shsurf->view))
4178                 return;
4179
4180         x = shsurf->view->geometry.x;
4181         y = shsurf->view->geometry.y;
4182
4183         api->send_position(surface, x, y);
4184 }
4185
4186 static void
4187 center_on_output(struct weston_view *view, struct weston_output *output)
4188 {
4189         int32_t surf_x, surf_y, width, height;
4190         float x, y;
4191
4192         if (!output) {
4193                 weston_view_set_position(view, 0, 0);
4194                 return;
4195         }
4196
4197         surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
4198
4199         x = output->x + (output->width - width) / 2 - surf_x / 2;
4200         y = output->y + (output->height - height) / 2 - surf_y / 2;
4201
4202         weston_view_set_position(view, x, y);
4203 }
4204
4205 static void
4206 weston_view_set_initial_position(struct weston_view *view,
4207                                  struct desktop_shell *shell)
4208 {
4209         struct weston_compositor *compositor = shell->compositor;
4210         int ix = 0, iy = 0;
4211         int32_t range_x, range_y;
4212         int32_t x, y;
4213         struct weston_output *output, *target_output = NULL;
4214         struct weston_seat *seat;
4215         pixman_rectangle32_t area;
4216
4217         /* As a heuristic place the new window on the same output as the
4218          * pointer. Falling back to the output containing 0, 0.
4219          *
4220          * TODO: Do something clever for touch too?
4221          */
4222         wl_list_for_each(seat, &compositor->seat_list, link) {
4223                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4224
4225                 if (pointer) {
4226                         ix = wl_fixed_to_int(pointer->x);
4227                         iy = wl_fixed_to_int(pointer->y);
4228                         break;
4229                 }
4230         }
4231
4232         wl_list_for_each(output, &compositor->output_list, link) {
4233                 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4234                         target_output = output;
4235                         break;
4236                 }
4237         }
4238
4239         if (!target_output) {
4240                 weston_view_set_position(view, 10 + random() % 400,
4241                                          10 + random() % 400);
4242                 return;
4243         }
4244
4245         /* Valid range within output where the surface will still be onscreen.
4246          * If this is negative it means that the surface is bigger than
4247          * output.
4248          */
4249         get_output_work_area(shell, target_output, &area);
4250
4251         x = area.x;
4252         y = area.y;
4253         range_x = area.width - view->surface->width;
4254         range_y = area.height - view->surface->height;
4255
4256         if (range_x > 0)
4257                 x += random() % range_x;
4258
4259         if (range_y > 0)
4260                 y += random() % range_y;
4261
4262         weston_view_set_position(view, x, y);
4263 }
4264
4265 static bool
4266 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4267 {
4268         struct timespec now;
4269
4270         if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4271                 return false;
4272
4273         /*
4274          * If the shell helper client dies before the session has been
4275          * up for roughly 30 seconds, better just make Weston shut down,
4276          * because the user likely has no way to interact with the desktop
4277          * anyway.
4278          */
4279         if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4280                 weston_log("Error: %s apparently cannot run at all.\n",
4281                            shell->client);
4282                 weston_log_continue(STAMP_SPACE "Quitting...");
4283                 wl_display_terminate(shell->compositor->wl_display);
4284
4285                 return true;
4286         }
4287
4288         return false;
4289 }
4290
4291 static void launch_desktop_shell_process(void *data);
4292
4293 static void
4294 respawn_desktop_shell_process(struct desktop_shell *shell)
4295 {
4296         struct timespec time;
4297
4298         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4299         weston_compositor_get_time(&time);
4300         if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
4301                 shell->child.deathstamp = time;
4302                 shell->child.deathcount = 0;
4303         }
4304
4305         shell->child.deathcount++;
4306         if (shell->child.deathcount > 5) {
4307                 weston_log("%s disconnected, giving up.\n", shell->client);
4308                 return;
4309         }
4310
4311         weston_log("%s disconnected, respawning...\n", shell->client);
4312         launch_desktop_shell_process(shell);
4313 }
4314
4315 static void
4316 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4317 {
4318         struct desktop_shell *shell;
4319
4320         shell = container_of(listener, struct desktop_shell,
4321                              child.client_destroy_listener);
4322
4323         wl_list_remove(&shell->child.client_destroy_listener.link);
4324         shell->child.client = NULL;
4325         /*
4326          * unbind_desktop_shell() will reset shell->child.desktop_shell
4327          * before the respawned process has a chance to create a new
4328          * desktop_shell object, because we are being called from the
4329          * wl_client destructor which destroys all wl_resources before
4330          * returning.
4331          */
4332
4333         if (!check_desktop_shell_crash_too_early(shell))
4334                 respawn_desktop_shell_process(shell);
4335
4336         shell_fade_startup(shell);
4337 }
4338
4339 static void
4340 launch_desktop_shell_process(void *data)
4341 {
4342         struct desktop_shell *shell = data;
4343
4344         shell->child.client = weston_client_start(shell->compositor,
4345                                                   shell->client);
4346
4347         if (!shell->child.client) {
4348                 weston_log("not able to start %s\n", shell->client);
4349                 return;
4350         }
4351
4352         shell->child.client_destroy_listener.notify =
4353                 desktop_shell_client_destroy;
4354         wl_client_add_destroy_listener(shell->child.client,
4355                                        &shell->child.client_destroy_listener);
4356 }
4357
4358 static void
4359 unbind_desktop_shell(struct wl_resource *resource)
4360 {
4361         struct desktop_shell *shell = wl_resource_get_user_data(resource);
4362
4363         if (shell->locked)
4364                 resume_desktop(shell);
4365
4366         shell->child.desktop_shell = NULL;
4367         shell->prepare_event_sent = false;
4368 }
4369
4370 static void
4371 bind_desktop_shell(struct wl_client *client,
4372                    void *data, uint32_t version, uint32_t id)
4373 {
4374         struct desktop_shell *shell = data;
4375         struct wl_resource *resource;
4376
4377         resource = wl_resource_create(client, &weston_desktop_shell_interface,
4378                                       1, id);
4379
4380         if (client == shell->child.client) {
4381                 wl_resource_set_implementation(resource,
4382                                                &desktop_shell_implementation,
4383                                                shell, unbind_desktop_shell);
4384                 shell->child.desktop_shell = resource;
4385                 return;
4386         }
4387
4388         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4389                                "permission to bind desktop_shell denied");
4390 }
4391
4392 struct switcher {
4393         struct desktop_shell *shell;
4394         struct weston_view *current;
4395         struct wl_listener listener;
4396         struct weston_keyboard_grab grab;
4397         struct wl_array minimized_array;
4398 };
4399
4400 static void
4401 switcher_next(struct switcher *switcher)
4402 {
4403         struct weston_view *view;
4404         struct weston_view *first = NULL, *prev = NULL, *next = NULL;
4405         struct shell_surface *shsurf;
4406         struct workspace *ws = get_current_workspace(switcher->shell);
4407
4408          /* temporary re-display minimized surfaces */
4409         struct weston_view *tmp;
4410         struct weston_view **minimized;
4411         wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4412                 weston_layer_entry_remove(&view->layer_link);
4413                 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
4414                 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4415                 *minimized = view;
4416         }
4417
4418         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4419                 shsurf = get_shell_surface(view->surface);
4420                 if (shsurf) {
4421                         if (first == NULL)
4422                                 first = view;
4423                         if (prev == switcher->current)
4424                                 next = view;
4425                         prev = view;
4426                         view->alpha = 0.25;
4427                         weston_view_geometry_dirty(view);
4428                         weston_surface_damage(view->surface);
4429                 }
4430
4431                 if (is_black_surface_view(view, NULL)) {
4432                         view->alpha = 0.25;
4433                         weston_view_geometry_dirty(view);
4434                         weston_surface_damage(view->surface);
4435                 }
4436         }
4437
4438         if (next == NULL)
4439                 next = first;
4440
4441         if (next == NULL)
4442                 return;
4443
4444         wl_list_remove(&switcher->listener.link);
4445         wl_signal_add(&next->destroy_signal, &switcher->listener);
4446
4447         switcher->current = next;
4448         wl_list_for_each(view, &next->surface->views, surface_link)
4449                 view->alpha = 1.0;
4450
4451         shsurf = get_shell_surface(switcher->current->surface);
4452         if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
4453                 shsurf->fullscreen.black_view->alpha = 1.0;
4454 }
4455
4456 static void
4457 switcher_handle_view_destroy(struct wl_listener *listener, void *data)
4458 {
4459         struct switcher *switcher =
4460                 container_of(listener, struct switcher, listener);
4461
4462         switcher_next(switcher);
4463 }
4464
4465 static void
4466 switcher_destroy(struct switcher *switcher)
4467 {
4468         struct weston_view *view;
4469         struct weston_keyboard *keyboard = switcher->grab.keyboard;
4470         struct workspace *ws = get_current_workspace(switcher->shell);
4471
4472         wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4473                 if (is_focus_view(view))
4474                         continue;
4475
4476                 view->alpha = 1.0;
4477                 weston_surface_damage(view->surface);
4478         }
4479
4480         if (switcher->current) {
4481                 activate(switcher->shell, switcher->current,
4482                          keyboard->seat,
4483                          WESTON_ACTIVATE_FLAG_CONFIGURE);
4484         }
4485
4486         wl_list_remove(&switcher->listener.link);
4487         weston_keyboard_end_grab(keyboard);
4488         if (keyboard->input_method_resource)
4489                 keyboard->grab = &keyboard->input_method_grab;
4490
4491          /* re-hide surfaces that were temporary shown during the switch */
4492         struct weston_view **minimized;
4493         wl_array_for_each(minimized, &switcher->minimized_array) {
4494                 /* with the exception of the current selected */
4495                 if ((*minimized)->surface != switcher->current->surface) {
4496                         weston_layer_entry_remove(&(*minimized)->layer_link);
4497                         weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
4498                         weston_view_damage_below(*minimized);
4499                 }
4500         }
4501         wl_array_release(&switcher->minimized_array);
4502
4503         free(switcher);
4504 }
4505
4506 static void
4507 switcher_key(struct weston_keyboard_grab *grab,
4508              const struct timespec *time, uint32_t key, uint32_t state_w)
4509 {
4510         struct switcher *switcher = container_of(grab, struct switcher, grab);
4511         enum wl_keyboard_key_state state = state_w;
4512
4513         if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4514                 switcher_next(switcher);
4515 }
4516
4517 static void
4518 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4519                   uint32_t mods_depressed, uint32_t mods_latched,
4520                   uint32_t mods_locked, uint32_t group)
4521 {
4522         struct switcher *switcher = container_of(grab, struct switcher, grab);
4523         struct weston_seat *seat = grab->keyboard->seat;
4524
4525         if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4526                 switcher_destroy(switcher);
4527 }
4528
4529 static void
4530 switcher_cancel(struct weston_keyboard_grab *grab)
4531 {
4532         struct switcher *switcher = container_of(grab, struct switcher, grab);
4533
4534         switcher_destroy(switcher);
4535 }
4536
4537 static const struct weston_keyboard_grab_interface switcher_grab = {
4538         switcher_key,
4539         switcher_modifier,
4540         switcher_cancel,
4541 };
4542
4543 static void
4544 switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4545                  uint32_t key, void *data)
4546 {
4547         struct desktop_shell *shell = data;
4548         struct switcher *switcher;
4549
4550         switcher = malloc(sizeof *switcher);
4551         switcher->shell = shell;
4552         switcher->current = NULL;
4553         switcher->listener.notify = switcher_handle_view_destroy;
4554         wl_list_init(&switcher->listener.link);
4555         wl_array_init(&switcher->minimized_array);
4556
4557         lower_fullscreen_layer(switcher->shell, NULL);
4558         switcher->grab.interface = &switcher_grab;
4559         weston_keyboard_start_grab(keyboard, &switcher->grab);
4560         weston_keyboard_set_focus(keyboard, NULL);
4561         switcher_next(switcher);
4562 }
4563
4564 static void
4565 backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4566                   uint32_t key, void *data)
4567 {
4568         struct weston_compositor *compositor = data;
4569         struct weston_output *output;
4570         long backlight_new = 0;
4571
4572         /* TODO: we're limiting to simple use cases, where we assume just
4573          * control on the primary display. We'd have to extend later if we
4574          * ever get support for setting backlights on random desktop LCD
4575          * panels though */
4576         output = get_default_output(compositor);
4577         if (!output)
4578                 return;
4579
4580         if (!output->set_backlight)
4581                 return;
4582
4583         if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4584                 backlight_new = output->backlight_current - 25;
4585         else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4586                 backlight_new = output->backlight_current + 25;
4587
4588         if (backlight_new < 5)
4589                 backlight_new = 5;
4590         if (backlight_new > 255)
4591                 backlight_new = 255;
4592
4593         output->backlight_current = backlight_new;
4594         output->set_backlight(output, output->backlight_current);
4595 }
4596
4597 static void
4598 force_kill_binding(struct weston_keyboard *keyboard,
4599                    const struct timespec *time, uint32_t key, void *data)
4600 {
4601         struct weston_surface *focus_surface;
4602         struct wl_client *client;
4603         struct desktop_shell *shell = data;
4604         struct weston_compositor *compositor = shell->compositor;
4605         pid_t pid;
4606
4607         focus_surface = keyboard->focus;
4608         if (!focus_surface)
4609                 return;
4610
4611         wl_signal_emit(&compositor->kill_signal, focus_surface);
4612
4613         client = wl_resource_get_client(focus_surface->resource);
4614         wl_client_get_credentials(client, &pid, NULL, NULL);
4615
4616         /* Skip clients that we launched ourselves (the credentials of
4617          * the socketpair is ours) */
4618         if (pid == getpid())
4619                 return;
4620
4621         kill(pid, SIGKILL);
4622 }
4623
4624 static void
4625 workspace_up_binding(struct weston_keyboard *keyboard,
4626                      const struct timespec *time, uint32_t key, void *data)
4627 {
4628         struct desktop_shell *shell = data;
4629         unsigned int new_index = shell->workspaces.current;
4630
4631         if (shell->locked)
4632                 return;
4633         if (new_index != 0)
4634                 new_index--;
4635
4636         change_workspace(shell, new_index);
4637 }
4638
4639 static void
4640 workspace_down_binding(struct weston_keyboard *keyboard,
4641                        const struct timespec *time, uint32_t key, void *data)
4642 {
4643         struct desktop_shell *shell = data;
4644         unsigned int new_index = shell->workspaces.current;
4645
4646         if (shell->locked)
4647                 return;
4648         if (new_index < shell->workspaces.num - 1)
4649                 new_index++;
4650
4651         change_workspace(shell, new_index);
4652 }
4653
4654 static void
4655 workspace_f_binding(struct weston_keyboard *keyboard,
4656                     const struct timespec *time, uint32_t key, void *data)
4657 {
4658         struct desktop_shell *shell = data;
4659         unsigned int new_index;
4660
4661         if (shell->locked)
4662                 return;
4663         new_index = key - KEY_F1;
4664         if (new_index >= shell->workspaces.num)
4665                 new_index = shell->workspaces.num - 1;
4666
4667         change_workspace(shell, new_index);
4668 }
4669
4670 static void
4671 workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4672                                   const struct timespec *time, uint32_t key,
4673                                   void *data)
4674 {
4675         struct desktop_shell *shell = data;
4676         unsigned int new_index = shell->workspaces.current;
4677
4678         if (shell->locked)
4679                 return;
4680
4681         if (new_index != 0)
4682                 new_index--;
4683
4684         take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4685 }
4686
4687 static void
4688 workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4689                                     const struct timespec *time, uint32_t key,
4690                                     void *data)
4691 {
4692         struct desktop_shell *shell = data;
4693         unsigned int new_index = shell->workspaces.current;
4694
4695         if (shell->locked)
4696                 return;
4697
4698         if (new_index < shell->workspaces.num - 1)
4699                 new_index++;
4700
4701         take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4702 }
4703
4704 static void
4705 shell_reposition_view_on_output_destroy(struct weston_view *view)
4706 {
4707         struct weston_output *output, *first_output;
4708         struct weston_compositor *ec = view->surface->compositor;
4709         struct shell_surface *shsurf;
4710         float x, y;
4711         int visible;
4712
4713         x = view->geometry.x;
4714         y = view->geometry.y;
4715
4716         /* At this point the destroyed output is not in the list anymore.
4717          * If the view is still visible somewhere, we leave where it is,
4718          * otherwise, move it to the first output. */
4719         visible = 0;
4720         wl_list_for_each(output, &ec->output_list, link) {
4721                 if (pixman_region32_contains_point(&output->region,
4722                                                    x, y, NULL)) {
4723                         visible = 1;
4724                         break;
4725                 }
4726         }
4727
4728         if (!visible) {
4729                 first_output = container_of(ec->output_list.next,
4730                                             struct weston_output, link);
4731
4732                 x = first_output->x + first_output->width / 4;
4733                 y = first_output->y + first_output->height / 4;
4734
4735                 weston_view_set_position(view, x, y);
4736         } else {
4737                 weston_view_geometry_dirty(view);
4738         }
4739
4740
4741         shsurf = get_shell_surface(view->surface);
4742         if (!shsurf)
4743                 return;
4744
4745         shsurf->saved_position_valid = false;
4746         set_maximized(shsurf, false);
4747         set_fullscreen(shsurf, false, NULL);
4748 }
4749
4750 void
4751 shell_for_each_layer(struct desktop_shell *shell,
4752                      shell_for_each_layer_func_t func, void *data)
4753 {
4754         struct workspace **ws;
4755
4756         func(shell, &shell->fullscreen_layer, data);
4757         func(shell, &shell->panel_layer, data);
4758         func(shell, &shell->background_layer, data);
4759         func(shell, &shell->lock_layer, data);
4760         func(shell, &shell->input_panel_layer, data);
4761
4762         wl_array_for_each(ws, &shell->workspaces.array)
4763                 func(shell, &(*ws)->layer, data);
4764 }
4765
4766 static void
4767 shell_output_destroy_move_layer(struct desktop_shell *shell,
4768                                 struct weston_layer *layer,
4769                                 void *data)
4770 {
4771         struct weston_view *view;
4772
4773         wl_list_for_each(view, &layer->view_list.link, layer_link.link)
4774                 shell_reposition_view_on_output_destroy(view);
4775 }
4776
4777 static void
4778 handle_output_destroy(struct wl_listener *listener, void *data)
4779 {
4780         struct shell_output *output_listener =
4781                 container_of(listener, struct shell_output, destroy_listener);
4782         struct desktop_shell *shell = output_listener->shell;
4783
4784         shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
4785
4786         if (output_listener->panel_surface)
4787                 wl_list_remove(&output_listener->panel_surface_listener.link);
4788         if (output_listener->background_surface)
4789                 wl_list_remove(&output_listener->background_surface_listener.link);
4790         wl_list_remove(&output_listener->destroy_listener.link);
4791         wl_list_remove(&output_listener->link);
4792         free(output_listener);
4793 }
4794
4795 static void
4796 shell_resize_surface_to_output(struct desktop_shell *shell,
4797                                 struct weston_surface *surface,
4798                                 const struct weston_output *output)
4799 {
4800         if (!surface)
4801                 return;
4802
4803         weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4804                                         surface->resource,
4805                                         output->width,
4806                                         output->height);
4807 }
4808
4809
4810 static void
4811 handle_output_resized(struct wl_listener *listener, void *data)
4812 {
4813         struct desktop_shell *shell =
4814                 container_of(listener, struct desktop_shell, resized_listener);
4815         struct weston_output *output = (struct weston_output *)data;
4816         struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4817
4818         shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4819         shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4820 }
4821
4822 static void
4823 create_shell_output(struct desktop_shell *shell,
4824                                         struct weston_output *output)
4825 {
4826         struct shell_output *shell_output;
4827
4828         shell_output = zalloc(sizeof *shell_output);
4829         if (shell_output == NULL)
4830                 return;
4831
4832         shell_output->output = output;
4833         shell_output->shell = shell;
4834         shell_output->destroy_listener.notify = handle_output_destroy;
4835         wl_signal_add(&output->destroy_signal,
4836                       &shell_output->destroy_listener);
4837         wl_list_insert(shell->output_list.prev, &shell_output->link);
4838 }
4839
4840 static void
4841 handle_output_create(struct wl_listener *listener, void *data)
4842 {
4843         struct desktop_shell *shell =
4844                 container_of(listener, struct desktop_shell, output_create_listener);
4845         struct weston_output *output = (struct weston_output *)data;
4846
4847         create_shell_output(shell, output);
4848 }
4849
4850 static void
4851 handle_output_move_layer(struct desktop_shell *shell,
4852                          struct weston_layer *layer, void *data)
4853 {
4854         struct weston_output *output = data;
4855         struct weston_view *view;
4856         float x, y;
4857
4858         wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
4859                 if (view->output != output)
4860                         continue;
4861
4862                 x = view->geometry.x + output->move_x;
4863                 y = view->geometry.y + output->move_y;
4864                 weston_view_set_position(view, x, y);
4865         }
4866 }
4867
4868 static void
4869 handle_output_move(struct wl_listener *listener, void *data)
4870 {
4871         struct desktop_shell *shell;
4872
4873         shell = container_of(listener, struct desktop_shell,
4874                              output_move_listener);
4875
4876         shell_for_each_layer(shell, handle_output_move_layer, data);
4877 }
4878
4879 static void
4880 setup_output_destroy_handler(struct weston_compositor *ec,
4881                                                         struct desktop_shell *shell)
4882 {
4883         struct weston_output *output;
4884
4885         wl_list_init(&shell->output_list);
4886         wl_list_for_each(output, &ec->output_list, link)
4887                 create_shell_output(shell, output);
4888
4889         shell->output_create_listener.notify = handle_output_create;
4890         wl_signal_add(&ec->output_created_signal,
4891                                 &shell->output_create_listener);
4892
4893         shell->output_move_listener.notify = handle_output_move;
4894         wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
4895 }
4896
4897 static void
4898 shell_destroy(struct wl_listener *listener, void *data)
4899 {
4900         struct desktop_shell *shell =
4901                 container_of(listener, struct desktop_shell, destroy_listener);
4902         struct workspace **ws;
4903         struct shell_output *shell_output, *tmp;
4904
4905         /* Force state to unlocked so we don't try to fade */
4906         shell->locked = false;
4907
4908         if (shell->child.client) {
4909                 /* disable respawn */
4910                 wl_list_remove(&shell->child.client_destroy_listener.link);
4911                 wl_client_destroy(shell->child.client);
4912         }
4913
4914         wl_list_remove(&shell->destroy_listener.link);
4915         wl_list_remove(&shell->idle_listener.link);
4916         wl_list_remove(&shell->wake_listener.link);
4917         wl_list_remove(&shell->transform_listener.link);
4918
4919         text_backend_destroy(shell->text_backend);
4920         input_panel_destroy(shell);
4921
4922         wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4923                 wl_list_remove(&shell_output->destroy_listener.link);
4924                 wl_list_remove(&shell_output->link);
4925                 free(shell_output);
4926         }
4927
4928         wl_list_remove(&shell->output_create_listener.link);
4929         wl_list_remove(&shell->output_move_listener.link);
4930         wl_list_remove(&shell->resized_listener.link);
4931
4932         wl_array_for_each(ws, &shell->workspaces.array)
4933                 workspace_destroy(*ws);
4934         wl_array_release(&shell->workspaces.array);
4935
4936         free(shell->client);
4937         free(shell);
4938 }
4939
4940 static void
4941 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4942 {
4943         uint32_t mod;
4944         int i, num_workspace_bindings;
4945
4946         if (shell->allow_zap)
4947                 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4948                                                   MODIFIER_CTRL | MODIFIER_ALT,
4949                                                   terminate_binding, ec);
4950
4951         /* fixed bindings */
4952         weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4953                                              click_to_activate_binding,
4954                                              shell);
4955         weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4956                                              click_to_activate_binding,
4957                                              shell);
4958         weston_compositor_add_touch_binding(ec, 0,
4959                                             touch_to_activate_binding,
4960                                             shell);
4961         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4962                                           backlight_binding, ec);
4963         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4964                                           backlight_binding, ec);
4965
4966         /* configurable bindings */
4967         if (shell->exposay_modifier)
4968                 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4969                                                        exposay_binding, shell);
4970
4971         mod = shell->binding_modifier;
4972         if (!mod)
4973                 return;
4974
4975         /* This binding is not configurable, but is only enabled if there is a
4976          * valid binding modifier. */
4977         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4978                                            MODIFIER_SUPER | MODIFIER_ALT,
4979                                            surface_opacity_binding, NULL);
4980
4981         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4982                                            mod, zoom_axis_binding,
4983                                            NULL);
4984
4985         weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4986                                           zoom_key_binding, NULL);
4987         weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4988                                           zoom_key_binding, NULL);
4989         weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4990                                           maximize_binding, NULL);
4991         weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4992                                           fullscreen_binding, NULL);
4993         weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4994                                              shell);
4995         weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
4996         weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4997                                              resize_binding, shell);
4998         weston_compositor_add_button_binding(ec, BTN_LEFT,
4999                                              mod | MODIFIER_SHIFT,
5000                                              resize_binding, shell);
5001
5002         if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5003                 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5004                                                      rotate_binding, NULL);
5005
5006         weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5007                                           shell);
5008         weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5009                                           ec);
5010         weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5011                                           ec);
5012         weston_compositor_add_key_binding(ec, KEY_K, mod,
5013                                           force_kill_binding, shell);
5014         weston_compositor_add_key_binding(ec, KEY_UP, mod,
5015                                           workspace_up_binding, shell);
5016         weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5017                                           workspace_down_binding, shell);
5018         weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5019                                           workspace_move_surface_up_binding,
5020                                           shell);
5021         weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5022                                           workspace_move_surface_down_binding,
5023                                           shell);
5024
5025         /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5026         if (shell->workspaces.num > 1) {
5027                 num_workspace_bindings = shell->workspaces.num;
5028                 if (num_workspace_bindings > 6)
5029                         num_workspace_bindings = 6;
5030                 for (i = 0; i < num_workspace_bindings; i++)
5031                         weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5032                                                           workspace_f_binding,
5033                                                           shell);
5034         }
5035
5036         weston_install_debug_key_binding(ec, mod);
5037 }
5038
5039 static void
5040 handle_seat_created(struct wl_listener *listener, void *data)
5041 {
5042         struct weston_seat *seat = data;
5043
5044         create_shell_seat(seat);
5045 }
5046
5047 WL_EXPORT int
5048 wet_shell_init(struct weston_compositor *ec,
5049                int *argc, char *argv[])
5050 {
5051         struct weston_seat *seat;
5052         struct desktop_shell *shell;
5053         struct workspace **pws;
5054         unsigned int i;
5055         struct wl_event_loop *loop;
5056
5057         shell = zalloc(sizeof *shell);
5058         if (shell == NULL)
5059                 return -1;
5060
5061         shell->compositor = ec;
5062
5063         shell->destroy_listener.notify = shell_destroy;
5064         wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
5065         shell->idle_listener.notify = idle_handler;
5066         wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5067         shell->wake_listener.notify = wake_handler;
5068         wl_signal_add(&ec->wake_signal, &shell->wake_listener);
5069         shell->transform_listener.notify = transform_handler;
5070         wl_signal_add(&ec->transform_signal, &shell->transform_listener);
5071
5072         weston_layer_init(&shell->fullscreen_layer, ec);
5073         weston_layer_init(&shell->panel_layer, ec);
5074         weston_layer_init(&shell->background_layer, ec);
5075         weston_layer_init(&shell->lock_layer, ec);
5076         weston_layer_init(&shell->input_panel_layer, ec);
5077
5078         weston_layer_set_position(&shell->fullscreen_layer,
5079                                   WESTON_LAYER_POSITION_FULLSCREEN);
5080         weston_layer_set_position(&shell->panel_layer,
5081                                   WESTON_LAYER_POSITION_UI);
5082         weston_layer_set_position(&shell->background_layer,
5083                                   WESTON_LAYER_POSITION_BACKGROUND);
5084
5085         wl_array_init(&shell->workspaces.array);
5086         wl_list_init(&shell->workspaces.client_list);
5087
5088         if (input_panel_setup(shell) < 0)
5089                 return -1;
5090
5091         shell->text_backend = text_backend_init(ec);
5092         if (!shell->text_backend)
5093                 return -1;
5094
5095         shell_configuration(shell);
5096
5097         shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5098         shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5099
5100         for (i = 0; i < shell->workspaces.num; i++) {
5101                 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5102                 if (pws == NULL)
5103                         return -1;
5104
5105                 *pws = workspace_create(shell);
5106                 if (*pws == NULL)
5107                         return -1;
5108         }
5109         activate_workspace(shell, 0);
5110
5111         weston_layer_init(&shell->minimized_layer, ec);
5112
5113         wl_list_init(&shell->workspaces.anim_sticky_list);
5114         wl_list_init(&shell->workspaces.animation.link);
5115         shell->workspaces.animation.frame = animate_workspace_change_frame;
5116
5117         shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5118         if (!shell->desktop)
5119                 return -1;
5120
5121         if (wl_global_create(ec->wl_display,
5122                              &weston_desktop_shell_interface, 1,
5123                              shell, bind_desktop_shell) == NULL)
5124                 return -1;
5125
5126         weston_compositor_get_time(&shell->child.deathstamp);
5127
5128         shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
5129
5130         setup_output_destroy_handler(ec, shell);
5131
5132         loop = wl_display_get_event_loop(ec->wl_display);
5133         wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
5134
5135         wl_list_for_each(seat, &ec->seat_list, link)
5136                 handle_seat_created(NULL, seat);
5137         shell->seat_create_listener.notify = handle_seat_created;
5138         wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
5139
5140         shell->resized_listener.notify = handle_output_resized;
5141         wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5142
5143         screenshooter_create(ec);
5144
5145         shell_add_bindings(ec, shell);
5146
5147         shell_fade_init(shell);
5148
5149         clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);
5150
5151         return 0;
5152 }