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