compositor: Don't set DPMS state on start up
[profile/ivi/weston.git] / src / shell.c
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <linux/input.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <math.h>
33 #include <sys/types.h>
34
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "workspaces-server-protocol.h"
39 #include "../shared/config-parser.h"
40
41 #define DEFAULT_NUM_WORKSPACES 1
42 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
43
44 enum animation_type {
45         ANIMATION_NONE,
46
47         ANIMATION_ZOOM,
48         ANIMATION_FADE
49 };
50
51 struct focus_state {
52         struct weston_seat *seat;
53         struct workspace *ws;
54         struct weston_surface *keyboard_focus;
55         struct wl_list link;
56         struct wl_listener seat_destroy_listener;
57         struct wl_listener surface_destroy_listener;
58 };
59
60 struct workspace {
61         struct weston_layer layer;
62
63         struct wl_list focus_list;
64         struct wl_listener seat_destroyed_listener;
65 };
66
67 struct input_panel_surface {
68         struct wl_list link;
69         struct weston_surface *surface;
70         struct wl_listener listener;
71 };
72
73 struct desktop_shell {
74         struct weston_compositor *compositor;
75
76         struct wl_listener lock_listener;
77         struct wl_listener unlock_listener;
78         struct wl_listener destroy_listener;
79         struct wl_listener show_input_panel_listener;
80         struct wl_listener hide_input_panel_listener;
81
82         struct weston_layer fullscreen_layer;
83         struct weston_layer panel_layer;
84         struct weston_layer background_layer;
85         struct weston_layer lock_layer;
86         struct weston_layer input_panel_layer;
87
88         struct wl_listener pointer_focus_listener;
89         struct weston_surface *grab_surface;
90
91         struct {
92                 struct weston_process process;
93                 struct wl_client *client;
94                 struct wl_resource *desktop_shell;
95
96                 unsigned deathcount;
97                 uint32_t deathstamp;
98         } child;
99
100         bool locked;
101         bool showing_input_panels;
102         bool prepare_event_sent;
103
104         struct weston_surface *lock_surface;
105         struct wl_listener lock_surface_listener;
106
107         struct {
108                 struct wl_array array;
109                 unsigned int current;
110                 unsigned int num;
111
112                 struct wl_list client_list;
113
114                 struct weston_animation animation;
115                 struct wl_list anim_sticky_list;
116                 int anim_dir;
117                 uint32_t anim_timestamp;
118                 double anim_current;
119                 struct workspace *anim_from;
120                 struct workspace *anim_to;
121         } workspaces;
122
123         struct {
124                 char *path;
125                 int duration;
126                 struct wl_resource *binding;
127                 struct wl_list surfaces;
128                 struct weston_process process;
129         } screensaver;
130
131         struct {
132                 struct wl_resource *binding;
133                 struct wl_list surfaces;
134         } input_panel;
135
136         uint32_t binding_modifier;
137         enum animation_type win_animation_type;
138         struct weston_surface *debug_repaint_surface;
139 };
140
141 enum shell_surface_type {
142         SHELL_SURFACE_NONE,
143         SHELL_SURFACE_TOPLEVEL,
144         SHELL_SURFACE_TRANSIENT,
145         SHELL_SURFACE_FULLSCREEN,
146         SHELL_SURFACE_MAXIMIZED,
147         SHELL_SURFACE_POPUP
148 };
149
150 struct ping_timer {
151         struct wl_event_source *source;
152         uint32_t serial;
153 };
154
155 struct shell_surface {
156         struct wl_resource resource;
157
158         struct weston_surface *surface;
159         struct wl_listener surface_destroy_listener;
160         struct weston_surface *parent;
161         struct desktop_shell *shell;
162
163         enum shell_surface_type type, next_type;
164         char *title, *class;
165         int32_t saved_x, saved_y;
166         bool saved_position_valid;
167         bool saved_rotation_valid;
168         int unresponsive;
169
170         struct {
171                 struct weston_transform transform;
172                 struct weston_matrix rotation;
173         } rotation;
174
175         struct {
176                 struct wl_pointer_grab grab;
177                 int32_t x, y;
178                 struct weston_transform parent_transform;
179                 int32_t initial_up;
180                 struct wl_seat *seat;
181                 uint32_t serial;
182         } popup;
183
184         struct {
185                 int32_t x, y;
186                 uint32_t flags;
187         } transient;
188
189         struct {
190                 enum wl_shell_surface_fullscreen_method type;
191                 struct weston_transform transform; /* matrix from x, y */
192                 uint32_t framerate;
193                 struct weston_surface *black_surface;
194         } fullscreen;
195
196         struct ping_timer *ping_timer;
197
198         struct weston_transform workspace_transform;
199
200         struct weston_output *fullscreen_output;
201         struct weston_output *output;
202         struct wl_list link;
203
204         const struct weston_shell_client *client;
205 };
206
207 struct shell_grab {
208         struct wl_pointer_grab grab;
209         struct shell_surface *shsurf;
210         struct wl_listener shsurf_destroy_listener;
211         struct wl_pointer *pointer;
212 };
213
214 struct weston_move_grab {
215         struct shell_grab base;
216         wl_fixed_t dx, dy;
217 };
218
219 struct rotate_grab {
220         struct shell_grab base;
221         struct weston_matrix rotation;
222         struct {
223                 float x;
224                 float y;
225         } center;
226 };
227
228 static void
229 activate(struct desktop_shell *shell, struct weston_surface *es,
230          struct weston_seat *seat);
231
232 static struct workspace *
233 get_current_workspace(struct desktop_shell *shell);
234
235 static struct shell_surface *
236 get_shell_surface(struct weston_surface *surface);
237
238 static struct desktop_shell *
239 shell_surface_get_shell(struct shell_surface *shsurf);
240
241 static void
242 surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
243
244 static bool
245 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
246 {
247         struct desktop_shell *shell;
248         struct weston_surface *top_fs_es;
249
250         shell = shell_surface_get_shell(shsurf);
251         
252         if (wl_list_empty(&shell->fullscreen_layer.surface_list))
253                 return false;
254
255         top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
256                                  struct weston_surface, 
257                                  layer_link);
258         return (shsurf == get_shell_surface(top_fs_es));
259 }
260
261 static void
262 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
263 {
264         struct shell_grab *grab;
265
266         grab = container_of(listener, struct shell_grab,
267                             shsurf_destroy_listener);
268
269         grab->shsurf = NULL;
270 }
271
272 static void
273 popup_grab_end(struct wl_pointer *pointer);
274
275 static void
276 shell_grab_start(struct shell_grab *grab,
277                  const struct wl_pointer_grab_interface *interface,
278                  struct shell_surface *shsurf,
279                  struct wl_pointer *pointer,
280                  enum desktop_shell_cursor cursor)
281 {
282         struct desktop_shell *shell = shsurf->shell;
283
284         popup_grab_end(pointer);
285
286         grab->grab.interface = interface;
287         grab->shsurf = shsurf;
288         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
289         wl_signal_add(&shsurf->resource.destroy_signal,
290                       &grab->shsurf_destroy_listener);
291
292         grab->pointer = pointer;
293         grab->grab.focus = &shsurf->surface->surface;
294
295         wl_pointer_start_grab(pointer, &grab->grab);
296         desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
297         wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
298                              wl_fixed_from_int(0), wl_fixed_from_int(0));
299 }
300
301 static void
302 shell_grab_end(struct shell_grab *grab)
303 {
304         if (grab->shsurf)
305                 wl_list_remove(&grab->shsurf_destroy_listener.link);
306
307         wl_pointer_end_grab(grab->pointer);
308 }
309
310 static void
311 center_on_output(struct weston_surface *surface,
312                  struct weston_output *output);
313
314 static enum weston_keyboard_modifier
315 get_modifier(char *modifier)
316 {
317         if (!modifier)
318                 return MODIFIER_SUPER;
319
320         if (!strcmp("ctrl", modifier))
321                 return MODIFIER_CTRL;
322         else if (!strcmp("alt", modifier))
323                 return MODIFIER_ALT;
324         else if (!strcmp("super", modifier))
325                 return MODIFIER_SUPER;
326         else
327                 return MODIFIER_SUPER;
328 }
329
330 static enum animation_type
331 get_animation_type(char *animation)
332 {
333         if (!animation)
334                 return ANIMATION_NONE;
335
336         if (!strcmp("zoom", animation))
337                 return ANIMATION_ZOOM;
338         else if (!strcmp("fade", animation))
339                 return ANIMATION_FADE;
340         else
341                 return ANIMATION_NONE;
342 }
343
344 static void
345 shell_configuration(struct desktop_shell *shell)
346 {
347         char *config_file;
348         char *path = NULL;
349         int duration = 60;
350         unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
351         char *modifier = NULL;
352         char *win_animation = NULL;
353
354         struct config_key shell_keys[] = {
355                 { "binding-modifier",   CONFIG_KEY_STRING, &modifier },
356                 { "animation",          CONFIG_KEY_STRING, &win_animation},
357                 { "num-workspaces",
358                         CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
359         };
360
361         struct config_key saver_keys[] = {
362                 { "path",       CONFIG_KEY_STRING,  &path },
363                 { "duration",   CONFIG_KEY_INTEGER, &duration },
364         };
365
366         struct config_section cs[] = {
367                 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
368                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
369         };
370
371         config_file = config_file_path("weston.ini");
372         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
373         free(config_file);
374
375         shell->screensaver.path = path;
376         shell->screensaver.duration = duration;
377         shell->binding_modifier = get_modifier(modifier);
378         shell->win_animation_type = get_animation_type(win_animation);
379         shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
380 }
381
382 static void
383 focus_state_destroy(struct focus_state *state)
384 {
385         wl_list_remove(&state->seat_destroy_listener.link);
386         wl_list_remove(&state->surface_destroy_listener.link);
387         free(state);
388 }
389
390 static void
391 focus_state_seat_destroy(struct wl_listener *listener, void *data)
392 {
393         struct focus_state *state = container_of(listener,
394                                                  struct focus_state,
395                                                  seat_destroy_listener);
396
397         wl_list_remove(&state->link);
398         focus_state_destroy(state);
399 }
400
401 static void
402 focus_state_surface_destroy(struct wl_listener *listener, void *data)
403 {
404         struct focus_state *state = container_of(listener,
405                                                  struct focus_state,
406                                                  surface_destroy_listener);
407         struct desktop_shell *shell;
408         struct weston_surface *surface, *next;
409
410         next = NULL;
411         wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
412                 if (surface == state->keyboard_focus)
413                         continue;
414
415                 next = surface;
416                 break;
417         }
418
419         if (next) {
420                 shell = state->seat->compositor->shell_interface.shell;
421                 activate(shell, next, state->seat);
422         } else {
423                 wl_list_remove(&state->link);
424                 focus_state_destroy(state);
425         }
426 }
427
428 static struct focus_state *
429 focus_state_create(struct weston_seat *seat, struct workspace *ws)
430 {
431         struct focus_state *state;
432
433         state = malloc(sizeof *state);
434         if (state == NULL)
435                 return NULL;
436
437         state->ws = ws;
438         state->seat = seat;
439         wl_list_insert(&ws->focus_list, &state->link);
440
441         state->seat_destroy_listener.notify = focus_state_seat_destroy;
442         state->surface_destroy_listener.notify = focus_state_surface_destroy;
443         wl_signal_add(&seat->seat.destroy_signal,
444                       &state->seat_destroy_listener);
445         wl_list_init(&state->surface_destroy_listener.link);
446
447         return state;
448 }
449
450 static struct focus_state *
451 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
452 {
453         struct workspace *ws = get_current_workspace(shell);
454         struct focus_state *state;
455
456         wl_list_for_each(state, &ws->focus_list, link)
457                 if (state->seat == seat)
458                         break;
459
460         if (&state->link == &ws->focus_list)
461                 state = focus_state_create(seat, ws);
462
463         return state;
464 }
465
466 static void
467 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
468 {
469         struct focus_state *state, *next;
470         struct wl_surface *surface;
471
472         wl_list_for_each_safe(state, next, &ws->focus_list, link) {
473                 surface = state->keyboard_focus ?
474                         &state->keyboard_focus->surface : NULL;
475
476                 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
477         }
478 }
479
480 static void
481 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
482                     struct weston_seat *seat)
483 {
484         struct focus_state *state;
485         struct wl_surface *surface;
486
487         wl_list_for_each(state, &ws->focus_list, link) {
488                 if (state->seat == seat) {
489                         surface = seat->seat.keyboard->focus;
490                         state->keyboard_focus =
491                                 (struct weston_surface *) surface;
492                         return;
493                 }
494         }
495 }
496
497 static void
498 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
499                  struct weston_surface *surface)
500 {
501         struct focus_state *state;
502
503         wl_list_for_each(state, &ws->focus_list, link)
504                 if (state->keyboard_focus == surface)
505                         state->keyboard_focus = NULL;
506 }
507
508 static void
509 workspace_destroy(struct workspace *ws)
510 {
511         struct focus_state *state, *next;
512
513         wl_list_for_each_safe(state, next, &ws->focus_list, link)
514                 focus_state_destroy(state);
515
516         free(ws);
517 }
518
519 static void
520 seat_destroyed(struct wl_listener *listener, void *data)
521 {
522         struct weston_seat *seat = data;
523         struct focus_state *state, *next;
524         struct workspace *ws = container_of(listener,
525                                             struct workspace,
526                                             seat_destroyed_listener);
527
528         wl_list_for_each_safe(state, next, &ws->focus_list, link)
529                 if (state->seat == seat)
530                         wl_list_remove(&state->link);
531 }
532
533 static struct workspace *
534 workspace_create(void)
535 {
536         struct workspace *ws = malloc(sizeof *ws);
537         if (ws == NULL)
538                 return NULL;
539
540         weston_layer_init(&ws->layer, NULL);
541
542         wl_list_init(&ws->focus_list);
543         wl_list_init(&ws->seat_destroyed_listener.link);
544         ws->seat_destroyed_listener.notify = seat_destroyed;
545
546         return ws;
547 }
548
549 static int
550 workspace_is_empty(struct workspace *ws)
551 {
552         return wl_list_empty(&ws->layer.surface_list);
553 }
554
555 static struct workspace *
556 get_workspace(struct desktop_shell *shell, unsigned int index)
557 {
558         struct workspace **pws = shell->workspaces.array.data;
559         assert(index < shell->workspaces.num);
560         pws += index;
561         return *pws;
562 }
563
564 static struct workspace *
565 get_current_workspace(struct desktop_shell *shell)
566 {
567         return get_workspace(shell, shell->workspaces.current);
568 }
569
570 static void
571 activate_workspace(struct desktop_shell *shell, unsigned int index)
572 {
573         struct workspace *ws;
574
575         ws = get_workspace(shell, index);
576         wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
577
578         shell->workspaces.current = index;
579 }
580
581 static unsigned int
582 get_output_height(struct weston_output *output)
583 {
584         return abs(output->region.extents.y1 - output->region.extents.y2);
585 }
586
587 static void
588 surface_translate(struct weston_surface *surface, double d)
589 {
590         struct shell_surface *shsurf = get_shell_surface(surface);
591         struct weston_transform *transform;
592
593         transform = &shsurf->workspace_transform;
594         if (wl_list_empty(&transform->link))
595                 wl_list_insert(surface->geometry.transformation_list.prev,
596                                &shsurf->workspace_transform.link);
597
598         weston_matrix_init(&shsurf->workspace_transform.matrix);
599         weston_matrix_translate(&shsurf->workspace_transform.matrix,
600                                 0.0, d, 0.0);
601         surface->geometry.dirty = 1;
602 }
603
604 static void
605 workspace_translate_out(struct workspace *ws, double fraction)
606 {
607         struct weston_surface *surface;
608         unsigned int height;
609         double d;
610
611         wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612                 height = get_output_height(surface->output);
613                 d = height * fraction;
614
615                 surface_translate(surface, d);
616         }
617 }
618
619 static void
620 workspace_translate_in(struct workspace *ws, double fraction)
621 {
622         struct weston_surface *surface;
623         unsigned int height;
624         double d;
625
626         wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
627                 height = get_output_height(surface->output);
628
629                 if (fraction > 0)
630                         d = -(height - height * fraction);
631                 else
632                         d = height + height * fraction;
633
634                 surface_translate(surface, d);
635         }
636 }
637
638 static void
639 broadcast_current_workspace_state(struct desktop_shell *shell)
640 {
641         struct wl_resource *resource;
642
643         wl_list_for_each(resource, &shell->workspaces.client_list, link)
644                 workspace_manager_send_state(resource,
645                                              shell->workspaces.current,
646                                              shell->workspaces.num);
647 }
648
649 static void
650 reverse_workspace_change_animation(struct desktop_shell *shell,
651                                    unsigned int index,
652                                    struct workspace *from,
653                                    struct workspace *to)
654 {
655         shell->workspaces.current = index;
656
657         shell->workspaces.anim_to = to;
658         shell->workspaces.anim_from = from;
659         shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
660         shell->workspaces.anim_timestamp = 0;
661
662         weston_compositor_schedule_repaint(shell->compositor);
663 }
664
665 static void
666 workspace_deactivate_transforms(struct workspace *ws)
667 {
668         struct weston_surface *surface;
669         struct shell_surface *shsurf;
670
671         wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
672                 shsurf = get_shell_surface(surface);
673                 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
674                         wl_list_remove(&shsurf->workspace_transform.link);
675                         wl_list_init(&shsurf->workspace_transform.link);
676                 }
677                 shsurf->surface->geometry.dirty = 1;
678         }
679 }
680
681 static void
682 finish_workspace_change_animation(struct desktop_shell *shell,
683                                   struct workspace *from,
684                                   struct workspace *to)
685 {
686         weston_compositor_schedule_repaint(shell->compositor);
687
688         wl_list_remove(&shell->workspaces.animation.link);
689         workspace_deactivate_transforms(from);
690         workspace_deactivate_transforms(to);
691         shell->workspaces.anim_to = NULL;
692
693         wl_list_remove(&shell->workspaces.anim_from->layer.link);
694 }
695
696 static void
697 animate_workspace_change_frame(struct weston_animation *animation,
698                                struct weston_output *output, uint32_t msecs)
699 {
700         struct desktop_shell *shell =
701                 container_of(animation, struct desktop_shell,
702                              workspaces.animation);
703         struct workspace *from = shell->workspaces.anim_from;
704         struct workspace *to = shell->workspaces.anim_to;
705         uint32_t t;
706         double x, y;
707
708         if (workspace_is_empty(from) && workspace_is_empty(to)) {
709                 finish_workspace_change_animation(shell, from, to);
710                 return;
711         }
712
713         if (shell->workspaces.anim_timestamp == 0) {
714                 if (shell->workspaces.anim_current == 0.0)
715                         shell->workspaces.anim_timestamp = msecs;
716                 else
717                         shell->workspaces.anim_timestamp =
718                                 msecs -
719                                 /* Invers of movement function 'y' below. */
720                                 (asin(1.0 - shell->workspaces.anim_current) *
721                                  DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
722                                  M_2_PI);
723         }
724
725         t = msecs - shell->workspaces.anim_timestamp;
726
727         /*
728          * x = [0, π/2]
729          * y(x) = sin(x)
730          */
731         x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
732         y = sin(x);
733
734         if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
735                 weston_compositor_schedule_repaint(shell->compositor);
736
737                 workspace_translate_out(from, shell->workspaces.anim_dir * y);
738                 workspace_translate_in(to, shell->workspaces.anim_dir * y);
739                 shell->workspaces.anim_current = y;
740
741                 weston_compositor_schedule_repaint(shell->compositor);
742         }
743         else
744                 finish_workspace_change_animation(shell, from, to);
745 }
746
747 static void
748 animate_workspace_change(struct desktop_shell *shell,
749                          unsigned int index,
750                          struct workspace *from,
751                          struct workspace *to)
752 {
753         struct weston_output *output;
754
755         int dir;
756
757         if (index > shell->workspaces.current)
758                 dir = -1;
759         else
760                 dir = 1;
761
762         shell->workspaces.current = index;
763
764         shell->workspaces.anim_dir = dir;
765         shell->workspaces.anim_from = from;
766         shell->workspaces.anim_to = to;
767         shell->workspaces.anim_current = 0.0;
768         shell->workspaces.anim_timestamp = 0;
769
770         output = container_of(shell->compositor->output_list.next,
771                               struct weston_output, link);
772         wl_list_insert(&output->animation_list,
773                        &shell->workspaces.animation.link);
774
775         wl_list_insert(from->layer.link.prev, &to->layer.link);
776
777         workspace_translate_in(to, 0);
778
779         restore_focus_state(shell, to);
780
781         weston_compositor_schedule_repaint(shell->compositor);
782 }
783
784 static void
785 update_workspace(struct desktop_shell *shell, unsigned int index,
786                  struct workspace *from, struct workspace *to)
787 {
788         shell->workspaces.current = index;
789         wl_list_insert(&from->layer.link, &to->layer.link);
790         wl_list_remove(&from->layer.link);
791 }
792
793 static void
794 change_workspace(struct desktop_shell *shell, unsigned int index)
795 {
796         struct workspace *from;
797         struct workspace *to;
798
799         if (index == shell->workspaces.current)
800                 return;
801
802         /* Don't change workspace when there is any fullscreen surfaces. */
803         if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
804                 return;
805
806         from = get_current_workspace(shell);
807         to = get_workspace(shell, index);
808
809         if (shell->workspaces.anim_from == to &&
810             shell->workspaces.anim_to == from) {
811                 restore_focus_state(shell, to);
812                 reverse_workspace_change_animation(shell, index, from, to);
813                 broadcast_current_workspace_state(shell);
814                 return;
815         }
816
817         if (shell->workspaces.anim_to != NULL)
818                 finish_workspace_change_animation(shell,
819                                                   shell->workspaces.anim_from,
820                                                   shell->workspaces.anim_to);
821
822         restore_focus_state(shell, to);
823
824         if (workspace_is_empty(to) && workspace_is_empty(from))
825                 update_workspace(shell, index, from, to);
826         else
827                 animate_workspace_change(shell, index, from, to);
828
829         broadcast_current_workspace_state(shell);
830 }
831
832 static bool
833 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
834 {
835         struct wl_list *list = &ws->layer.surface_list;
836         struct wl_list *e;
837
838         if (wl_list_empty(list))
839                 return false;
840
841         e = list->next;
842
843         if (e->next != list)
844                 return false;
845
846         return container_of(e, struct weston_surface, layer_link) == surface;
847 }
848
849 static void
850 move_surface_to_workspace(struct desktop_shell *shell,
851                           struct weston_surface *surface,
852                           uint32_t workspace)
853 {
854         struct workspace *from;
855         struct workspace *to;
856         struct weston_seat *seat;
857
858         if (workspace == shell->workspaces.current)
859                 return;
860
861         if (workspace >= shell->workspaces.num)
862                 workspace = shell->workspaces.num - 1;
863
864         from = get_current_workspace(shell);
865         to = get_workspace(shell, workspace);
866
867         wl_list_remove(&surface->layer_link);
868         wl_list_insert(&to->layer.surface_list, &surface->layer_link);
869
870         drop_focus_state(shell, from, surface);
871         wl_list_for_each(seat, &shell->compositor->seat_list, link)
872                 if (seat->has_keyboard &&
873                     seat->keyboard.focus == &surface->surface)
874                         wl_keyboard_set_focus(&seat->keyboard, NULL);
875
876         weston_surface_damage_below(surface);
877 }
878
879 static void
880 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
881                                   struct wl_seat *wl_seat,
882                                   unsigned int index)
883 {
884         struct weston_seat *seat = (struct weston_seat *) wl_seat;
885         struct weston_surface *surface =
886                 (struct weston_surface *) wl_seat->keyboard->focus;
887         struct shell_surface *shsurf;
888         struct workspace *from;
889         struct workspace *to;
890         struct focus_state *state;
891
892         if (surface == NULL ||
893             index == shell->workspaces.current)
894                 return;
895
896         from = get_current_workspace(shell);
897         to = get_workspace(shell, index);
898
899         wl_list_remove(&surface->layer_link);
900         wl_list_insert(&to->layer.surface_list, &surface->layer_link);
901
902         replace_focus_state(shell, to, seat);
903         drop_focus_state(shell, from, surface);
904
905         if (shell->workspaces.anim_from == to &&
906             shell->workspaces.anim_to == from) {
907                 wl_list_remove(&to->layer.link);
908                 wl_list_insert(from->layer.link.prev, &to->layer.link);
909
910                 reverse_workspace_change_animation(shell, index, from, to);
911                 broadcast_current_workspace_state(shell);
912
913                 return;
914         }
915
916         if (shell->workspaces.anim_to != NULL)
917                 finish_workspace_change_animation(shell,
918                                                   shell->workspaces.anim_from,
919                                                   shell->workspaces.anim_to);
920
921         if (workspace_is_empty(from) &&
922             workspace_has_only(to, surface))
923                 update_workspace(shell, index, from, to);
924         else {
925                 shsurf = get_shell_surface(surface);
926                 if (wl_list_empty(&shsurf->workspace_transform.link))
927                         wl_list_insert(&shell->workspaces.anim_sticky_list,
928                                        &shsurf->workspace_transform.link);
929
930                 animate_workspace_change(shell, index, from, to);
931         }
932
933         broadcast_current_workspace_state(shell);
934
935         state = ensure_focus_state(shell, seat);
936         if (state != NULL)
937                 state->keyboard_focus = surface;
938 }
939
940 static void
941 workspace_manager_move_surface(struct wl_client *client,
942                                struct wl_resource *resource,
943                                struct wl_resource *surface_resource,
944                                uint32_t workspace)
945 {
946         struct desktop_shell *shell = resource->data;
947         struct weston_surface *surface =
948                 (struct weston_surface *) surface_resource;
949
950         move_surface_to_workspace(shell, surface, workspace);
951 }
952
953 static const struct workspace_manager_interface workspace_manager_implementation = {
954         workspace_manager_move_surface,
955 };
956
957 static void
958 unbind_resource(struct wl_resource *resource)
959 {
960         wl_list_remove(&resource->link);
961         free(resource);
962 }
963
964 static void
965 bind_workspace_manager(struct wl_client *client,
966                        void *data, uint32_t version, uint32_t id)
967 {
968         struct desktop_shell *shell = data;
969         struct wl_resource *resource;
970
971         resource = wl_client_add_object(client, &workspace_manager_interface,
972                                         &workspace_manager_implementation,
973                                         id, shell);
974
975         if (resource == NULL) {
976                 weston_log("couldn't add workspace manager object");
977                 return;
978         }
979
980         resource->destroy = unbind_resource;
981         wl_list_insert(&shell->workspaces.client_list, &resource->link);
982
983         workspace_manager_send_state(resource,
984                                      shell->workspaces.current,
985                                      shell->workspaces.num);
986 }
987
988 static void
989 noop_grab_focus(struct wl_pointer_grab *grab,
990                 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
991 {
992         grab->focus = NULL;
993 }
994
995 static void
996 move_grab_motion(struct wl_pointer_grab *grab,
997                  uint32_t time, wl_fixed_t x, wl_fixed_t y)
998 {
999         struct weston_move_grab *move = (struct weston_move_grab *) grab;
1000         struct wl_pointer *pointer = grab->pointer;
1001         struct shell_surface *shsurf = move->base.shsurf;
1002         struct weston_surface *es;
1003         int dx = wl_fixed_to_int(pointer->x + move->dx);
1004         int dy = wl_fixed_to_int(pointer->y + move->dy);
1005
1006         if (!shsurf)
1007                 return;
1008
1009         es = shsurf->surface;
1010
1011         weston_surface_configure(es, dx, dy,
1012                                  es->geometry.width, es->geometry.height);
1013
1014         weston_compositor_schedule_repaint(es->compositor);
1015 }
1016
1017 static void
1018 move_grab_button(struct wl_pointer_grab *grab,
1019                  uint32_t time, uint32_t button, uint32_t state_w)
1020 {
1021         struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1022                                                     grab);
1023         struct wl_pointer *pointer = grab->pointer;
1024         enum wl_pointer_button_state state = state_w;
1025
1026         if (pointer->button_count == 0 &&
1027             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1028                 shell_grab_end(shell_grab);
1029                 free(grab);
1030         }
1031 }
1032
1033 static const struct wl_pointer_grab_interface move_grab_interface = {
1034         noop_grab_focus,
1035         move_grab_motion,
1036         move_grab_button,
1037 };
1038
1039 static int
1040 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1041 {
1042         struct weston_move_grab *move;
1043
1044         if (!shsurf)
1045                 return -1;
1046
1047         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1048                 return 0;
1049
1050         move = malloc(sizeof *move);
1051         if (!move)
1052                 return -1;
1053
1054         move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1055                         ws->seat.pointer->grab_x;
1056         move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1057                         ws->seat.pointer->grab_y;
1058
1059         shell_grab_start(&move->base, &move_grab_interface, shsurf,
1060                          ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1061
1062         return 0;
1063 }
1064
1065 static void
1066 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1067                    struct wl_resource *seat_resource, uint32_t serial)
1068 {
1069         struct weston_seat *ws = seat_resource->data;
1070         struct shell_surface *shsurf = resource->data;
1071
1072         if (ws->seat.pointer->button_count == 0 ||
1073             ws->seat.pointer->grab_serial != serial ||
1074             ws->seat.pointer->focus != &shsurf->surface->surface)
1075                 return;
1076
1077         if (surface_move(shsurf, ws) < 0)
1078                 wl_resource_post_no_memory(resource);
1079 }
1080
1081 struct weston_resize_grab {
1082         struct shell_grab base;
1083         uint32_t edges;
1084         int32_t width, height;
1085 };
1086
1087 static void
1088 resize_grab_motion(struct wl_pointer_grab *grab,
1089                    uint32_t time, wl_fixed_t x, wl_fixed_t y)
1090 {
1091         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1092         struct wl_pointer *pointer = grab->pointer;
1093         struct shell_surface *shsurf = resize->base.shsurf;
1094         int32_t width, height;
1095         wl_fixed_t from_x, from_y;
1096         wl_fixed_t to_x, to_y;
1097
1098         if (!shsurf)
1099                 return;
1100
1101         weston_surface_from_global_fixed(shsurf->surface,
1102                                          pointer->grab_x, pointer->grab_y,
1103                                          &from_x, &from_y);
1104         weston_surface_from_global_fixed(shsurf->surface,
1105                                          pointer->x, pointer->y, &to_x, &to_y);
1106
1107         width = resize->width;
1108         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1109                 width += wl_fixed_to_int(from_x - to_x);
1110         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1111                 width += wl_fixed_to_int(to_x - from_x);
1112         }
1113
1114         height = resize->height;
1115         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1116                 height += wl_fixed_to_int(from_y - to_y);
1117         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1118                 height += wl_fixed_to_int(to_y - from_y);
1119         }
1120
1121         shsurf->client->send_configure(shsurf->surface,
1122                                        resize->edges, width, height);
1123 }
1124
1125 static void
1126 send_configure(struct weston_surface *surface,
1127                uint32_t edges, int32_t width, int32_t height)
1128 {
1129         struct shell_surface *shsurf = get_shell_surface(surface);
1130
1131         wl_shell_surface_send_configure(&shsurf->resource,
1132                                         edges, width, height);
1133 }
1134
1135 static const struct weston_shell_client shell_client = {
1136         send_configure
1137 };
1138
1139 static void
1140 resize_grab_button(struct wl_pointer_grab *grab,
1141                    uint32_t time, uint32_t button, uint32_t state_w)
1142 {
1143         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1144         struct wl_pointer *pointer = grab->pointer;
1145         enum wl_pointer_button_state state = state_w;
1146
1147         if (pointer->button_count == 0 &&
1148             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1149                 shell_grab_end(&resize->base);
1150                 free(grab);
1151         }
1152 }
1153
1154 static const struct wl_pointer_grab_interface resize_grab_interface = {
1155         noop_grab_focus,
1156         resize_grab_motion,
1157         resize_grab_button,
1158 };
1159
1160 static int
1161 surface_resize(struct shell_surface *shsurf,
1162                struct weston_seat *ws, uint32_t edges)
1163 {
1164         struct weston_resize_grab *resize;
1165
1166         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1167                 return 0;
1168
1169         if (edges == 0 || edges > 15 ||
1170             (edges & 3) == 3 || (edges & 12) == 12)
1171                 return 0;
1172
1173         resize = malloc(sizeof *resize);
1174         if (!resize)
1175                 return -1;
1176
1177         resize->edges = edges;
1178         resize->width = shsurf->surface->geometry.width;
1179         resize->height = shsurf->surface->geometry.height;
1180
1181         shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1182                          ws->seat.pointer, edges);
1183
1184         return 0;
1185 }
1186
1187 static void
1188 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1189                      struct wl_resource *seat_resource, uint32_t serial,
1190                      uint32_t edges)
1191 {
1192         struct weston_seat *ws = seat_resource->data;
1193         struct shell_surface *shsurf = resource->data;
1194
1195         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1196                 return;
1197
1198         if (ws->seat.pointer->button_count == 0 ||
1199             ws->seat.pointer->grab_serial != serial ||
1200             ws->seat.pointer->focus != &shsurf->surface->surface)
1201                 return;
1202
1203         if (surface_resize(shsurf, ws, edges) < 0)
1204                 wl_resource_post_no_memory(resource);
1205 }
1206
1207 static void
1208 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1209                        struct wl_surface *surface, int32_t x, int32_t y)
1210 {
1211         struct shell_grab *grab = (struct shell_grab *) base;
1212
1213         if (grab->grab.focus != surface) {
1214                 shell_grab_end(grab);
1215                 free(grab);
1216         }
1217 }
1218
1219 static void
1220 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1221                         uint32_t time, int32_t x, int32_t y)
1222 {
1223 }
1224
1225 static void
1226 busy_cursor_grab_button(struct wl_pointer_grab *base,
1227                         uint32_t time, uint32_t button, uint32_t state)
1228 {
1229         struct shell_grab *grab = (struct shell_grab *) base;
1230         struct shell_surface *shsurf;
1231         struct weston_surface *surface = 
1232                 (struct weston_surface *) grab->grab.pointer->current;
1233         struct weston_seat *seat =
1234                 (struct weston_seat *) grab->grab.pointer->seat;
1235
1236         shsurf = get_shell_surface(surface);
1237         if (shsurf && button == BTN_LEFT && state) {
1238                 activate(shsurf->shell, shsurf->surface, seat);
1239                 surface_move(shsurf, seat);
1240         } else if (shsurf && button == BTN_RIGHT && state) {
1241                 activate(shsurf->shell, shsurf->surface, seat);
1242                 surface_rotate(shsurf, &seat->seat);
1243         }
1244 }
1245
1246 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1247         busy_cursor_grab_focus,
1248         busy_cursor_grab_motion,
1249         busy_cursor_grab_button,
1250 };
1251
1252 static void
1253 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1254 {
1255         struct shell_grab *grab;
1256
1257         grab = malloc(sizeof *grab);
1258         if (!grab)
1259                 return;
1260
1261         shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1262                          DESKTOP_SHELL_CURSOR_BUSY);
1263 }
1264
1265 static void
1266 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1267 {
1268         struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1269
1270         if (grab->grab.interface == &busy_cursor_grab_interface) {
1271                 shell_grab_end(grab);
1272                 free(grab);
1273         }
1274 }
1275
1276 static void
1277 ping_timer_destroy(struct shell_surface *shsurf)
1278 {
1279         if (!shsurf || !shsurf->ping_timer)
1280                 return;
1281
1282         if (shsurf->ping_timer->source)
1283                 wl_event_source_remove(shsurf->ping_timer->source);
1284
1285         free(shsurf->ping_timer);
1286         shsurf->ping_timer = NULL;
1287 }
1288
1289 static int
1290 ping_timeout_handler(void *data)
1291 {
1292         struct shell_surface *shsurf = data;
1293         struct weston_seat *seat;
1294
1295         /* Client is not responding */
1296         shsurf->unresponsive = 1;
1297
1298         wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1299                 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1300                         set_busy_cursor(shsurf, seat->seat.pointer);
1301
1302         return 1;
1303 }
1304
1305 static void
1306 ping_handler(struct weston_surface *surface, uint32_t serial)
1307 {
1308         struct shell_surface *shsurf = get_shell_surface(surface);
1309         struct wl_event_loop *loop;
1310         int ping_timeout = 200;
1311
1312         if (!shsurf)
1313                 return;
1314         if (!shsurf->resource.client)
1315                 return;
1316
1317         if (shsurf->surface == shsurf->shell->grab_surface)
1318                 return;
1319
1320         if (!shsurf->ping_timer) {
1321                 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1322                 if (!shsurf->ping_timer)
1323                         return;
1324
1325                 shsurf->ping_timer->serial = serial;
1326                 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1327                 shsurf->ping_timer->source =
1328                         wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1329                 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1330
1331                 wl_shell_surface_send_ping(&shsurf->resource, serial);
1332         }
1333 }
1334
1335 static void
1336 handle_pointer_focus(struct wl_listener *listener, void *data)
1337 {
1338         struct wl_pointer *pointer = data;
1339         struct weston_surface *surface =
1340                 (struct weston_surface *) pointer->focus;
1341         struct weston_compositor *compositor;
1342         struct shell_surface *shsurf;
1343         uint32_t serial;
1344
1345         if (!surface)
1346                 return;
1347
1348         compositor = surface->compositor;
1349         shsurf = get_shell_surface(surface);
1350
1351         if (shsurf && shsurf->unresponsive) {
1352                 set_busy_cursor(shsurf, pointer);
1353         } else {
1354                 serial = wl_display_next_serial(compositor->wl_display);
1355                 ping_handler(surface, serial);
1356         }
1357 }
1358
1359 static void
1360 create_pointer_focus_listener(struct weston_seat *seat)
1361 {
1362         struct wl_listener *listener;
1363
1364         if (!seat->seat.pointer)
1365                 return;
1366
1367         listener = malloc(sizeof *listener);
1368         listener->notify = handle_pointer_focus;
1369         wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1370 }
1371
1372 static void
1373 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1374                                                         uint32_t serial)
1375 {
1376         struct shell_surface *shsurf = resource->data;
1377         struct desktop_shell *shell = shsurf->shell;
1378         struct weston_seat *seat;
1379         struct weston_compositor *ec = shsurf->surface->compositor;
1380         struct wl_pointer *pointer;
1381         int was_unresponsive;
1382
1383         if (shsurf->ping_timer == NULL)
1384                 /* Just ignore unsolicited pong. */
1385                 return;
1386
1387         if (shsurf->ping_timer->serial == serial) {
1388                 was_unresponsive = shsurf->unresponsive;
1389                 shsurf->unresponsive = 0;
1390                 if (was_unresponsive) {
1391                         /* Received pong from previously unresponsive client */
1392                         wl_list_for_each(seat, &ec->seat_list, link) {
1393                                 pointer = seat->seat.pointer;
1394                                 if (pointer->focus ==
1395                                     &shell->grab_surface->surface &&
1396                                     pointer->current ==
1397                                     &shsurf->surface->surface)
1398                                         end_busy_cursor(shsurf, pointer);
1399                         }
1400                 }
1401                 ping_timer_destroy(shsurf);
1402         }
1403 }
1404
1405 static void
1406 shell_surface_set_title(struct wl_client *client,
1407                         struct wl_resource *resource, const char *title)
1408 {
1409         struct shell_surface *shsurf = resource->data;
1410
1411         free(shsurf->title);
1412         shsurf->title = strdup(title);
1413 }
1414
1415 static void
1416 shell_surface_set_class(struct wl_client *client,
1417                         struct wl_resource *resource, const char *class)
1418 {
1419         struct shell_surface *shsurf = resource->data;
1420
1421         free(shsurf->class);
1422         shsurf->class = strdup(class);
1423 }
1424
1425 static struct weston_output *
1426 get_default_output(struct weston_compositor *compositor)
1427 {
1428         return container_of(compositor->output_list.next,
1429                             struct weston_output, link);
1430 }
1431
1432 static void
1433 shell_unset_fullscreen(struct shell_surface *shsurf)
1434 {
1435         struct workspace *ws;
1436         /* undo all fullscreen things here */
1437         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1438             shell_surface_is_top_fullscreen(shsurf)) {
1439                 weston_output_switch_mode(shsurf->fullscreen_output,
1440                                           shsurf->fullscreen_output->origin);
1441         }
1442         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1443         shsurf->fullscreen.framerate = 0;
1444         wl_list_remove(&shsurf->fullscreen.transform.link);
1445         wl_list_init(&shsurf->fullscreen.transform.link);
1446         if (shsurf->fullscreen.black_surface)
1447                 weston_surface_destroy(shsurf->fullscreen.black_surface);
1448         shsurf->fullscreen.black_surface = NULL;
1449         shsurf->fullscreen_output = NULL;
1450         weston_surface_set_position(shsurf->surface,
1451                                     shsurf->saved_x, shsurf->saved_y);
1452         if (shsurf->saved_rotation_valid) {
1453                 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1454                                &shsurf->rotation.transform.link);
1455                 shsurf->saved_rotation_valid = false;
1456         }
1457
1458         ws = get_current_workspace(shsurf->shell);
1459         wl_list_remove(&shsurf->surface->layer_link);
1460         wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1461 }
1462
1463 static int
1464 reset_shell_surface_type(struct shell_surface *surface)
1465 {
1466         switch (surface->type) {
1467         case SHELL_SURFACE_FULLSCREEN:
1468                 shell_unset_fullscreen(surface);
1469                 break;
1470         case SHELL_SURFACE_MAXIMIZED:
1471                 surface->output = get_default_output(surface->surface->compositor);
1472                 weston_surface_set_position(surface->surface,
1473                                             surface->saved_x,
1474                                             surface->saved_y);
1475                 break;
1476         case SHELL_SURFACE_NONE:
1477         case SHELL_SURFACE_TOPLEVEL:
1478         case SHELL_SURFACE_TRANSIENT:
1479         case SHELL_SURFACE_POPUP:
1480                 break;
1481         }
1482
1483         surface->type = SHELL_SURFACE_NONE;
1484         return 0;
1485 }
1486
1487 static void
1488 set_surface_type(struct shell_surface *shsurf)
1489 {
1490         struct weston_surface *surface = shsurf->surface;
1491         struct weston_surface *pes = shsurf->parent;
1492
1493         reset_shell_surface_type(shsurf);
1494
1495         shsurf->type = shsurf->next_type;
1496         shsurf->next_type = SHELL_SURFACE_NONE;
1497
1498         switch (shsurf->type) {
1499         case SHELL_SURFACE_TOPLEVEL:
1500                 break;
1501         case SHELL_SURFACE_TRANSIENT:
1502                 weston_surface_set_position(surface,
1503                                 pes->geometry.x + shsurf->transient.x,
1504                                 pes->geometry.y + shsurf->transient.y);
1505                 break;
1506
1507         case SHELL_SURFACE_MAXIMIZED:
1508                 shsurf->saved_x = surface->geometry.x;
1509                 shsurf->saved_y = surface->geometry.y;
1510                 shsurf->saved_position_valid = true;
1511                 break;
1512
1513         case SHELL_SURFACE_FULLSCREEN:
1514                 shsurf->saved_x = surface->geometry.x;
1515                 shsurf->saved_y = surface->geometry.y;
1516                 shsurf->saved_position_valid = true;
1517
1518                 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1519                         wl_list_remove(&shsurf->rotation.transform.link);
1520                         wl_list_init(&shsurf->rotation.transform.link);
1521                         shsurf->surface->geometry.dirty = 1;
1522                         shsurf->saved_rotation_valid = true;
1523                 }
1524                 break;
1525
1526         default:
1527                 break;
1528         }
1529 }
1530
1531 static void
1532 set_toplevel(struct shell_surface *shsurf)
1533 {
1534        shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1535 }
1536
1537 static void
1538 shell_surface_set_toplevel(struct wl_client *client,
1539                            struct wl_resource *resource)
1540 {
1541         struct shell_surface *surface = resource->data;
1542
1543         set_toplevel(surface);
1544 }
1545
1546 static void
1547 set_transient(struct shell_surface *shsurf,
1548               struct weston_surface *parent, int x, int y, uint32_t flags)
1549 {
1550         /* assign to parents output */
1551         shsurf->parent = parent;
1552         shsurf->transient.x = x;
1553         shsurf->transient.y = y;
1554         shsurf->transient.flags = flags;
1555         shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1556 }
1557
1558 static void
1559 shell_surface_set_transient(struct wl_client *client,
1560                             struct wl_resource *resource,
1561                             struct wl_resource *parent_resource,
1562                             int x, int y, uint32_t flags)
1563 {
1564         struct shell_surface *shsurf = resource->data;
1565         struct weston_surface *parent = parent_resource->data;
1566
1567         set_transient(shsurf, parent, x, y, flags);
1568 }
1569
1570 static struct desktop_shell *
1571 shell_surface_get_shell(struct shell_surface *shsurf)
1572 {
1573         return shsurf->shell;
1574 }
1575
1576 static int
1577 get_output_panel_height(struct desktop_shell *shell,
1578                         struct weston_output *output)
1579 {
1580         struct weston_surface *surface;
1581         int panel_height = 0;
1582
1583         if (!output)
1584                 return 0;
1585
1586         wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1587                 if (surface->output == output) {
1588                         panel_height = surface->geometry.height;
1589                         break;
1590                 }
1591         }
1592
1593         return panel_height;
1594 }
1595
1596 static void
1597 shell_surface_set_maximized(struct wl_client *client,
1598                             struct wl_resource *resource,
1599                             struct wl_resource *output_resource )
1600 {
1601         struct shell_surface *shsurf = resource->data;
1602         struct weston_surface *es = shsurf->surface;
1603         struct desktop_shell *shell = NULL;
1604         uint32_t edges = 0, panel_height = 0;
1605
1606         /* get the default output, if the client set it as NULL
1607            check whether the ouput is available */
1608         if (output_resource)
1609                 shsurf->output = output_resource->data;
1610         else if (es->output)
1611                 shsurf->output = es->output;
1612         else
1613                 shsurf->output = get_default_output(es->compositor);
1614
1615         shell = shell_surface_get_shell(shsurf);
1616         panel_height = get_output_panel_height(shell, shsurf->output);
1617         edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1618
1619         shsurf->client->send_configure(shsurf->surface, edges,
1620                                        shsurf->output->width,
1621                                        shsurf->output->height - panel_height);
1622
1623         shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1624 }
1625
1626 static void
1627 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1628
1629 static struct weston_surface *
1630 create_black_surface(struct weston_compositor *ec,
1631                      struct weston_surface *fs_surface,
1632                      float x, float y, int w, int h)
1633 {
1634         struct weston_surface *surface = NULL;
1635
1636         surface = weston_surface_create(ec);
1637         if (surface == NULL) {
1638                 weston_log("no memory\n");
1639                 return NULL;
1640         }
1641
1642         surface->configure = black_surface_configure;
1643         surface->private = fs_surface;
1644         weston_surface_configure(surface, x, y, w, h);
1645         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1646         pixman_region32_fini(&surface->opaque);
1647         pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1648
1649         return surface;
1650 }
1651
1652 /* Create black surface and append it to the associated fullscreen surface.
1653  * Handle size dismatch and positioning according to the method. */
1654 static void
1655 shell_configure_fullscreen(struct shell_surface *shsurf)
1656 {
1657         struct weston_output *output = shsurf->fullscreen_output;
1658         struct weston_surface *surface = shsurf->surface;
1659         struct weston_matrix *matrix;
1660         float scale, output_aspect, surface_aspect, x, y;
1661
1662         if (!shsurf->fullscreen.black_surface)
1663                 shsurf->fullscreen.black_surface =
1664                         create_black_surface(surface->compositor,
1665                                              surface,
1666                                              output->x, output->y,
1667                                              output->width,
1668                                              output->height);
1669
1670         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1671         wl_list_insert(&surface->layer_link,
1672                        &shsurf->fullscreen.black_surface->layer_link);
1673         shsurf->fullscreen.black_surface->output = output;
1674
1675         switch (shsurf->fullscreen.type) {
1676         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1677                 if (surface->buffer)
1678                         center_on_output(surface, shsurf->fullscreen_output);
1679                 break;
1680         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1681                 /* 1:1 mapping between surface and output dimensions */
1682                 if (output->width == surface->geometry.width &&
1683                     output->height == surface->geometry.height) {
1684                         weston_surface_set_position(surface, output->x, output->y);
1685                         break;
1686                 }
1687
1688                 matrix = &shsurf->fullscreen.transform.matrix;
1689                 weston_matrix_init(matrix);
1690
1691                 output_aspect = (float) output->width /
1692                         (float) output->height;
1693                 surface_aspect = (float) surface->geometry.width /
1694                         (float) surface->geometry.height;
1695                 if (output_aspect < surface_aspect)
1696                         scale = (float) output->width /
1697                                 (float) surface->geometry.width;
1698                 else
1699                         scale = (float) output->height /
1700                                 (float) surface->geometry.height;
1701
1702                 weston_matrix_scale(matrix, scale, scale, 1);
1703                 wl_list_remove(&shsurf->fullscreen.transform.link);
1704                 wl_list_insert(&surface->geometry.transformation_list,
1705                                &shsurf->fullscreen.transform.link);
1706                 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1707                 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1708                 weston_surface_set_position(surface, x, y);
1709
1710                 break;
1711         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1712                 if (shell_surface_is_top_fullscreen(shsurf)) {
1713                         struct weston_mode mode = {0, 
1714                                 surface->geometry.width,
1715                                 surface->geometry.height,
1716                                 shsurf->fullscreen.framerate};
1717
1718                         if (weston_output_switch_mode(output, &mode) == 0) {
1719                                 weston_surface_configure(shsurf->fullscreen.black_surface, 
1720                                                          output->x, output->y,
1721                                                          output->width,
1722                                                          output->height);
1723                                 weston_surface_set_position(surface, output->x, output->y);
1724                                 break;
1725                         }
1726                 }
1727                 break;
1728         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1729                 break;
1730         default:
1731                 break;
1732         }
1733 }
1734
1735 /* make the fullscreen and black surface at the top */
1736 static void
1737 shell_stack_fullscreen(struct shell_surface *shsurf)
1738 {
1739         struct weston_output *output = shsurf->fullscreen_output;
1740         struct weston_surface *surface = shsurf->surface;
1741         struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1742
1743         wl_list_remove(&surface->layer_link);
1744         wl_list_insert(&shell->fullscreen_layer.surface_list,
1745                        &surface->layer_link);
1746         weston_surface_damage(surface);
1747
1748         if (!shsurf->fullscreen.black_surface)
1749                 shsurf->fullscreen.black_surface =
1750                         create_black_surface(surface->compositor,
1751                                              surface,
1752                                              output->x, output->y,
1753                                              output->width,
1754                                              output->height);
1755
1756         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1757         wl_list_insert(&surface->layer_link,
1758                        &shsurf->fullscreen.black_surface->layer_link);
1759         weston_surface_damage(shsurf->fullscreen.black_surface);
1760 }
1761
1762 static void
1763 shell_map_fullscreen(struct shell_surface *shsurf)
1764 {
1765         shell_stack_fullscreen(shsurf);
1766         shell_configure_fullscreen(shsurf);
1767 }
1768
1769 static void
1770 shell_surface_set_fullscreen(struct wl_client *client,
1771                              struct wl_resource *resource,
1772                              uint32_t method,
1773                              uint32_t framerate,
1774                              struct wl_resource *output_resource)
1775 {
1776         struct shell_surface *shsurf = resource->data;
1777         struct weston_surface *es = shsurf->surface;
1778
1779         if (output_resource)
1780                 shsurf->output = output_resource->data;
1781         else if (es->output)
1782                 shsurf->output = es->output;
1783         else
1784                 shsurf->output = get_default_output(es->compositor);
1785
1786         shsurf->fullscreen_output = shsurf->output;
1787         shsurf->fullscreen.type = method;
1788         shsurf->fullscreen.framerate = framerate;
1789         shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1790
1791         shsurf->client->send_configure(shsurf->surface, 0,
1792                                        shsurf->output->width,
1793                                        shsurf->output->height);
1794 }
1795
1796 static void
1797 popup_grab_focus(struct wl_pointer_grab *grab,
1798                  struct wl_surface *surface,
1799                  wl_fixed_t x,
1800                  wl_fixed_t y)
1801 {
1802         struct wl_pointer *pointer = grab->pointer;
1803         struct shell_surface *priv =
1804                 container_of(grab, struct shell_surface, popup.grab);
1805         struct wl_client *client = priv->surface->surface.resource.client;
1806
1807         if (surface && surface->resource.client == client) {
1808                 wl_pointer_set_focus(pointer, surface, x, y);
1809                 grab->focus = surface;
1810         } else {
1811                 wl_pointer_set_focus(pointer, NULL,
1812                                      wl_fixed_from_int(0),
1813                                      wl_fixed_from_int(0));
1814                 grab->focus = NULL;
1815         }
1816 }
1817
1818 static void
1819 popup_grab_motion(struct wl_pointer_grab *grab,
1820                   uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1821 {
1822         struct wl_resource *resource;
1823
1824         resource = grab->pointer->focus_resource;
1825         if (resource)
1826                 wl_pointer_send_motion(resource, time, sx, sy);
1827 }
1828
1829 static void
1830 popup_grab_button(struct wl_pointer_grab *grab,
1831                   uint32_t time, uint32_t button, uint32_t state_w)
1832 {
1833         struct wl_resource *resource;
1834         struct shell_surface *shsurf =
1835                 container_of(grab, struct shell_surface, popup.grab);
1836         struct wl_display *display;
1837         enum wl_pointer_button_state state = state_w;
1838         uint32_t serial;
1839
1840         resource = grab->pointer->focus_resource;
1841         if (resource) {
1842                 display = wl_client_get_display(resource->client);
1843                 serial = wl_display_get_serial(display);
1844                 wl_pointer_send_button(resource, serial, time, button, state);
1845         } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1846                    (shsurf->popup.initial_up ||
1847                     time - shsurf->popup.seat->pointer->grab_time > 500)) {
1848                 popup_grab_end(grab->pointer);
1849         }
1850
1851         if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1852                 shsurf->popup.initial_up = 1;
1853 }
1854
1855 static const struct wl_pointer_grab_interface popup_grab_interface = {
1856         popup_grab_focus,
1857         popup_grab_motion,
1858         popup_grab_button,
1859 };
1860
1861 static void
1862 popup_grab_end(struct wl_pointer *pointer)
1863 {
1864         struct wl_pointer_grab *grab = pointer->grab;
1865         struct shell_surface *shsurf =
1866                 container_of(grab, struct shell_surface, popup.grab);
1867
1868         if (pointer->grab->interface == &popup_grab_interface) {
1869                 wl_shell_surface_send_popup_done(&shsurf->resource);
1870                 wl_pointer_end_grab(grab->pointer);
1871                 shsurf->popup.grab.pointer = NULL;
1872         }
1873 }
1874
1875 static void
1876 shell_map_popup(struct shell_surface *shsurf)
1877 {
1878         struct wl_seat *seat = shsurf->popup.seat;
1879         struct weston_surface *es = shsurf->surface;
1880         struct weston_surface *parent = shsurf->parent;
1881
1882         /* Remove the old transform. We don't want to add it twice
1883          * otherwise Weston will go into an infinite loop when going
1884          * through the transforms. */
1885         if (!wl_list_empty(&shsurf->popup.parent_transform.link)) {
1886                 wl_list_remove(&shsurf->popup.parent_transform.link);
1887                 wl_list_init(&shsurf->popup.parent_transform.link);
1888         }
1889
1890         es->output = parent->output;
1891         shsurf->popup.grab.interface = &popup_grab_interface;
1892
1893         weston_surface_update_transform(parent);
1894         if (parent->transform.enabled) {
1895                 shsurf->popup.parent_transform.matrix =
1896                         parent->transform.matrix;
1897         } else {
1898                 /* construct x, y translation matrix */
1899                 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1900                 shsurf->popup.parent_transform.matrix.d[12] =
1901                         parent->geometry.x;
1902                 shsurf->popup.parent_transform.matrix.d[13] =
1903                         parent->geometry.y;
1904         }
1905         wl_list_insert(es->geometry.transformation_list.prev,
1906                        &shsurf->popup.parent_transform.link);
1907
1908         shsurf->popup.initial_up = 0;
1909         weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1910         weston_surface_update_transform(es);
1911
1912         /* We don't require the grab to still be active, but if another
1913          * grab has started in the meantime, we end the popup now. */
1914         if (seat->pointer->grab_serial == shsurf->popup.serial) {
1915                 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1916         } else {
1917                 wl_shell_surface_send_popup_done(&shsurf->resource);
1918         }
1919 }
1920
1921 static void
1922 shell_surface_set_popup(struct wl_client *client,
1923                         struct wl_resource *resource,
1924                         struct wl_resource *seat_resource,
1925                         uint32_t serial,
1926                         struct wl_resource *parent_resource,
1927                         int32_t x, int32_t y, uint32_t flags)
1928 {
1929         struct shell_surface *shsurf = resource->data;
1930
1931         shsurf->type = SHELL_SURFACE_POPUP;
1932         shsurf->parent = parent_resource->data;
1933         shsurf->popup.seat = seat_resource->data;
1934         shsurf->popup.serial = serial;
1935         shsurf->popup.x = x;
1936         shsurf->popup.y = y;
1937 }
1938
1939 static const struct wl_shell_surface_interface shell_surface_implementation = {
1940         shell_surface_pong,
1941         shell_surface_move,
1942         shell_surface_resize,
1943         shell_surface_set_toplevel,
1944         shell_surface_set_transient,
1945         shell_surface_set_fullscreen,
1946         shell_surface_set_popup,
1947         shell_surface_set_maximized,
1948         shell_surface_set_title,
1949         shell_surface_set_class
1950 };
1951
1952 static void
1953 destroy_shell_surface(struct shell_surface *shsurf)
1954 {
1955         if (shsurf->popup.grab.pointer)
1956                 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1957
1958         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1959             shell_surface_is_top_fullscreen(shsurf)) {
1960                 weston_output_switch_mode(shsurf->fullscreen_output,
1961                                           shsurf->fullscreen_output->origin);
1962         }
1963
1964         if (shsurf->fullscreen.black_surface)
1965                 weston_surface_destroy(shsurf->fullscreen.black_surface);
1966
1967         /* As destroy_resource() use wl_list_for_each_safe(),
1968          * we can always remove the listener.
1969          */
1970         wl_list_remove(&shsurf->surface_destroy_listener.link);
1971         shsurf->surface->configure = NULL;
1972         ping_timer_destroy(shsurf);
1973
1974         wl_list_remove(&shsurf->link);
1975         free(shsurf);
1976 }
1977
1978 static void
1979 shell_destroy_shell_surface(struct wl_resource *resource)
1980 {
1981         struct shell_surface *shsurf = resource->data;
1982
1983         destroy_shell_surface(shsurf);
1984 }
1985
1986 static void
1987 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1988 {
1989         struct shell_surface *shsurf = container_of(listener,
1990                                                     struct shell_surface,
1991                                                     surface_destroy_listener);
1992
1993         if (shsurf->resource.client) {
1994                 wl_resource_destroy(&shsurf->resource);
1995         } else {
1996                 wl_signal_emit(&shsurf->resource.destroy_signal,
1997                                &shsurf->resource);
1998                 destroy_shell_surface(shsurf);
1999         }
2000 }
2001
2002 static void
2003 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2004
2005 static struct shell_surface *
2006 get_shell_surface(struct weston_surface *surface)
2007 {
2008         if (surface->configure == shell_surface_configure)
2009                 return surface->private;
2010         else
2011                 return NULL;
2012 }
2013
2014 static  struct shell_surface *
2015 create_shell_surface(void *shell, struct weston_surface *surface,
2016                      const struct weston_shell_client *client)
2017 {
2018         struct shell_surface *shsurf;
2019
2020         if (surface->configure) {
2021                 weston_log("surface->configure already set\n");
2022                 return NULL;
2023         }
2024
2025         shsurf = calloc(1, sizeof *shsurf);
2026         if (!shsurf) {
2027                 weston_log("no memory to allocate shell surface\n");
2028                 return NULL;
2029         }
2030
2031         surface->configure = shell_surface_configure;
2032         surface->private = shsurf;
2033
2034         shsurf->shell = (struct desktop_shell *) shell;
2035         shsurf->unresponsive = 0;
2036         shsurf->saved_position_valid = false;
2037         shsurf->saved_rotation_valid = false;
2038         shsurf->surface = surface;
2039         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2040         shsurf->fullscreen.framerate = 0;
2041         shsurf->fullscreen.black_surface = NULL;
2042         shsurf->ping_timer = NULL;
2043         wl_list_init(&shsurf->fullscreen.transform.link);
2044
2045         wl_signal_init(&shsurf->resource.destroy_signal);
2046         shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2047         wl_signal_add(&surface->surface.resource.destroy_signal,
2048                       &shsurf->surface_destroy_listener);
2049
2050         /* init link so its safe to always remove it in destroy_shell_surface */
2051         wl_list_init(&shsurf->link);
2052
2053         /* empty when not in use */
2054         wl_list_init(&shsurf->rotation.transform.link);
2055         weston_matrix_init(&shsurf->rotation.rotation);
2056
2057         wl_list_init(&shsurf->workspace_transform.link);
2058         wl_list_init(&shsurf->popup.parent_transform.link);
2059
2060         shsurf->type = SHELL_SURFACE_NONE;
2061         shsurf->next_type = SHELL_SURFACE_NONE;
2062
2063         shsurf->client = client;
2064
2065         return shsurf;
2066 }
2067
2068 static void
2069 shell_get_shell_surface(struct wl_client *client,
2070                         struct wl_resource *resource,
2071                         uint32_t id,
2072                         struct wl_resource *surface_resource)
2073 {
2074         struct weston_surface *surface = surface_resource->data;
2075         struct desktop_shell *shell = resource->data;
2076         struct shell_surface *shsurf;
2077
2078         if (get_shell_surface(surface)) {
2079                 wl_resource_post_error(surface_resource,
2080                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2081                                        "desktop_shell::get_shell_surface already requested");
2082                 return;
2083         }
2084
2085         shsurf = create_shell_surface(shell, surface, &shell_client);
2086         if (!shsurf) {
2087                 wl_resource_post_error(surface_resource,
2088                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2089                                        "surface->configure already set");
2090                 return;
2091         }
2092
2093         shsurf->resource.destroy = shell_destroy_shell_surface;
2094         shsurf->resource.object.id = id;
2095         shsurf->resource.object.interface = &wl_shell_surface_interface;
2096         shsurf->resource.object.implementation =
2097                 (void (**)(void)) &shell_surface_implementation;
2098         shsurf->resource.data = shsurf;
2099
2100         wl_client_add_resource(client, &shsurf->resource);
2101 }
2102
2103 static const struct wl_shell_interface shell_implementation = {
2104         shell_get_shell_surface
2105 };
2106
2107 static void
2108 handle_screensaver_sigchld(struct weston_process *proc, int status)
2109 {
2110         struct desktop_shell *shell =
2111                 container_of(proc, struct desktop_shell, screensaver.process);
2112         struct weston_output *output;
2113
2114         proc->pid = 0;
2115
2116         if (shell->locked)
2117                 wl_list_for_each(output, &shell->compositor->output_list, link)
2118                         if (output->set_dpms)
2119                                 output->set_dpms(output, WESTON_DPMS_STANDBY);
2120 }
2121
2122 static void
2123 launch_screensaver(struct desktop_shell *shell)
2124 {
2125         if (shell->screensaver.binding)
2126                 return;
2127
2128         if (!shell->screensaver.path)
2129                 return;
2130
2131         if (shell->screensaver.process.pid != 0) {
2132                 weston_log("old screensaver still running\n");
2133                 return;
2134         }
2135
2136         weston_client_launch(shell->compositor,
2137                            &shell->screensaver.process,
2138                            shell->screensaver.path,
2139                            handle_screensaver_sigchld);
2140 }
2141
2142 static void
2143 terminate_screensaver(struct desktop_shell *shell)
2144 {
2145         if (shell->screensaver.process.pid == 0)
2146                 return;
2147
2148         kill(shell->screensaver.process.pid, SIGTERM);
2149 }
2150
2151 static void
2152 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
2153 {
2154         struct weston_surface *s, *next;
2155
2156         wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2157                 if (s->output == es->output && s != es) {
2158                         weston_surface_unmap(s);
2159                         s->configure = NULL;
2160                 }
2161         }
2162
2163         weston_surface_configure(es, es->output->x, es->output->y,
2164                                  es->buffer->width, es->buffer->height);
2165
2166         if (wl_list_empty(&es->layer_link)) {
2167                 wl_list_insert(&layer->surface_list, &es->layer_link);
2168                 weston_compositor_schedule_repaint(es->compositor);
2169         }
2170 }
2171
2172 static void
2173 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2174 {
2175         struct desktop_shell *shell = es->private;
2176
2177         configure_static_surface(es, &shell->background_layer);
2178 }
2179
2180 static void
2181 desktop_shell_set_background(struct wl_client *client,
2182                              struct wl_resource *resource,
2183                              struct wl_resource *output_resource,
2184                              struct wl_resource *surface_resource)
2185 {
2186         struct desktop_shell *shell = resource->data;
2187         struct weston_surface *surface = surface_resource->data;
2188
2189         if (surface->configure) {
2190                 wl_resource_post_error(surface_resource,
2191                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2192                                        "surface role already assigned");
2193                 return;
2194         }
2195
2196         surface->configure = background_configure;
2197         surface->private = shell;
2198         surface->output = output_resource->data;
2199         desktop_shell_send_configure(resource, 0,
2200                                      surface_resource,
2201                                      surface->output->width,
2202                                      surface->output->height);
2203 }
2204
2205 static void
2206 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2207 {
2208         struct desktop_shell *shell = es->private;
2209
2210         configure_static_surface(es, &shell->panel_layer);
2211 }
2212
2213 static void
2214 desktop_shell_set_panel(struct wl_client *client,
2215                         struct wl_resource *resource,
2216                         struct wl_resource *output_resource,
2217                         struct wl_resource *surface_resource)
2218 {
2219         struct desktop_shell *shell = resource->data;
2220         struct weston_surface *surface = surface_resource->data;
2221
2222         if (surface->configure) {
2223                 wl_resource_post_error(surface_resource,
2224                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
2225                                        "surface role already assigned");
2226                 return;
2227         }
2228
2229         surface->configure = panel_configure;
2230         surface->private = shell;
2231         surface->output = output_resource->data;
2232         desktop_shell_send_configure(resource, 0,
2233                                      surface_resource,
2234                                      surface->output->width,
2235                                      surface->output->height);
2236 }
2237
2238 static void
2239 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2240 {
2241         struct desktop_shell *shell = surface->private;
2242
2243         center_on_output(surface, get_default_output(shell->compositor));
2244
2245         if (!weston_surface_is_mapped(surface)) {
2246                 wl_list_insert(&shell->lock_layer.surface_list,
2247                                &surface->layer_link);
2248                 weston_surface_update_transform(surface);
2249                 weston_compositor_wake(shell->compositor);
2250         }
2251 }
2252
2253 static void
2254 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2255 {
2256         struct desktop_shell *shell =
2257             container_of(listener, struct desktop_shell, lock_surface_listener);
2258
2259         weston_log("lock surface gone\n");
2260         shell->lock_surface = NULL;
2261 }
2262
2263 static void
2264 desktop_shell_set_lock_surface(struct wl_client *client,
2265                                struct wl_resource *resource,
2266                                struct wl_resource *surface_resource)
2267 {
2268         struct desktop_shell *shell = resource->data;
2269         struct weston_surface *surface = surface_resource->data;
2270
2271         shell->prepare_event_sent = false;
2272
2273         if (!shell->locked)
2274                 return;
2275
2276         shell->lock_surface = surface;
2277
2278         shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2279         wl_signal_add(&surface_resource->destroy_signal,
2280                       &shell->lock_surface_listener);
2281
2282         surface->configure = lock_surface_configure;
2283         surface->private = shell;
2284 }
2285
2286 static void
2287 resume_desktop(struct desktop_shell *shell)
2288 {
2289         struct weston_surface *surface;
2290         struct workspace *ws = get_current_workspace(shell);
2291
2292         wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2293                 weston_surface_unmap(surface);
2294
2295         terminate_screensaver(shell);
2296
2297         wl_list_remove(&shell->lock_layer.link);
2298         wl_list_insert(&shell->compositor->cursor_layer.link,
2299                        &shell->fullscreen_layer.link);
2300         wl_list_insert(&shell->fullscreen_layer.link,
2301                        &shell->panel_layer.link);
2302         if (shell->showing_input_panels) {
2303                 wl_list_insert(&shell->panel_layer.link,
2304                                &shell->input_panel_layer.link);
2305                 wl_list_insert(&shell->input_panel_layer.link,
2306                                &ws->layer.link);
2307         } else {
2308                 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2309         }
2310
2311         restore_focus_state(shell, get_current_workspace(shell));
2312
2313         shell->locked = false;
2314         shell->compositor->idle_time = shell->compositor->option_idle_time;
2315         weston_compositor_wake(shell->compositor);
2316         weston_compositor_damage_all(shell->compositor);
2317 }
2318
2319 static void
2320 desktop_shell_unlock(struct wl_client *client,
2321                      struct wl_resource *resource)
2322 {
2323         struct desktop_shell *shell = resource->data;
2324
2325         shell->prepare_event_sent = false;
2326
2327         if (shell->locked)
2328                 resume_desktop(shell);
2329 }
2330
2331 static void
2332 desktop_shell_set_grab_surface(struct wl_client *client,
2333                                struct wl_resource *resource,
2334                                struct wl_resource *surface_resource)
2335 {
2336         struct desktop_shell *shell = resource->data;
2337
2338         shell->grab_surface = surface_resource->data;
2339 }
2340
2341 static const struct desktop_shell_interface desktop_shell_implementation = {
2342         desktop_shell_set_background,
2343         desktop_shell_set_panel,
2344         desktop_shell_set_lock_surface,
2345         desktop_shell_unlock,
2346         desktop_shell_set_grab_surface
2347 };
2348
2349 static enum shell_surface_type
2350 get_shell_surface_type(struct weston_surface *surface)
2351 {
2352         struct shell_surface *shsurf;
2353
2354         shsurf = get_shell_surface(surface);
2355         if (!shsurf)
2356                 return SHELL_SURFACE_NONE;
2357         return shsurf->type;
2358 }
2359
2360 static void
2361 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2362 {
2363         struct weston_surface *surface =
2364                 (struct weston_surface *) seat->pointer->focus;
2365         struct shell_surface *shsurf;
2366
2367         if (surface == NULL)
2368                 return;
2369
2370         shsurf = get_shell_surface(surface);
2371         if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2372                 return;
2373
2374         surface_move(shsurf, (struct weston_seat *) seat);
2375 }
2376
2377 static void
2378 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2379 {
2380         struct weston_surface *surface =
2381                 (struct weston_surface *) seat->pointer->focus;
2382         uint32_t edges = 0;
2383         int32_t x, y;
2384         struct shell_surface *shsurf;
2385
2386         if (surface == NULL)
2387                 return;
2388
2389         shsurf = get_shell_surface(surface);
2390         if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2391                 return;
2392
2393         weston_surface_from_global(surface,
2394                                    wl_fixed_to_int(seat->pointer->grab_x),
2395                                    wl_fixed_to_int(seat->pointer->grab_y),
2396                                    &x, &y);
2397
2398         if (x < surface->geometry.width / 3)
2399                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2400         else if (x < 2 * surface->geometry.width / 3)
2401                 edges |= 0;
2402         else
2403                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2404
2405         if (y < surface->geometry.height / 3)
2406                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2407         else if (y < 2 * surface->geometry.height / 3)
2408                 edges |= 0;
2409         else
2410                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2411
2412         surface_resize(shsurf, (struct weston_seat *) seat, edges);
2413 }
2414
2415 static void
2416 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2417                         wl_fixed_t value, void *data)
2418 {
2419         float step = 0.005;
2420         struct shell_surface *shsurf;
2421         struct weston_surface *surface =
2422                 (struct weston_surface *) seat->pointer->focus;
2423
2424         if (surface == NULL)
2425                 return;
2426
2427         shsurf = get_shell_surface(surface);
2428         if (!shsurf)
2429                 return;
2430
2431         surface->alpha -= wl_fixed_to_double(value) * step;
2432
2433         if (surface->alpha > 1.0)
2434                 surface->alpha = 1.0;
2435         if (surface->alpha < step)
2436                 surface->alpha = step;
2437
2438         surface->geometry.dirty = 1;
2439         weston_surface_damage(surface);
2440 }
2441
2442 static void
2443 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2444         wl_fixed_t value)
2445 {
2446         struct weston_seat *ws = (struct weston_seat *) seat;
2447         struct weston_compositor *compositor = ws->compositor;
2448         struct weston_output *output;
2449         float increment;
2450
2451         wl_list_for_each(output, &compositor->output_list, link) {
2452                 if (pixman_region32_contains_point(&output->region,
2453                                                    wl_fixed_to_double(seat->pointer->x),
2454                                                    wl_fixed_to_double(seat->pointer->y),
2455                                                    NULL)) {
2456                         if (key == KEY_PAGEUP)
2457                                 increment = output->zoom.increment;
2458                         else if (key == KEY_PAGEDOWN)
2459                                 increment = -output->zoom.increment;
2460                         else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2461                                 /* For every pixel zoom 20th of a step */
2462                                 increment = output->zoom.increment *
2463                                             -wl_fixed_to_double(value) / 20.0;
2464                         else
2465                                 increment = 0;
2466
2467                         output->zoom.level += increment;
2468
2469                         if (output->zoom.level < 0.0)
2470                                 output->zoom.level = 0.0;
2471                         else if (output->zoom.level > output->zoom.max_level)
2472                                 output->zoom.level = output->zoom.max_level;
2473                         else if (!output->zoom.active) {
2474                                 output->zoom.active = 1;
2475                                 output->disable_planes++;
2476                         }
2477
2478                         output->zoom.spring_z.target = output->zoom.level;
2479
2480                         weston_output_update_zoom(output, output->zoom.type);
2481                 }
2482         }
2483 }
2484
2485 static void
2486 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2487                   wl_fixed_t value, void *data)
2488 {
2489         do_zoom(seat, time, 0, axis, value);
2490 }
2491
2492 static void
2493 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2494                  void *data)
2495 {
2496         do_zoom(seat, time, key, 0, 0);
2497 }
2498
2499 static void
2500 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2501                   void *data)
2502 {
2503         struct weston_compositor *compositor = data;
2504
2505         wl_display_terminate(compositor->wl_display);
2506 }
2507
2508 static void
2509 rotate_grab_motion(struct wl_pointer_grab *grab,
2510                    uint32_t time, wl_fixed_t x, wl_fixed_t y)
2511 {
2512         struct rotate_grab *rotate =
2513                 container_of(grab, struct rotate_grab, base.grab);
2514         struct wl_pointer *pointer = grab->pointer;
2515         struct shell_surface *shsurf = rotate->base.shsurf;
2516         struct weston_surface *surface;
2517         float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2518
2519         if (!shsurf)
2520                 return;
2521
2522         surface = shsurf->surface;
2523
2524         cx = 0.5f * surface->geometry.width;
2525         cy = 0.5f * surface->geometry.height;
2526
2527         dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2528         dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2529         r = sqrtf(dx * dx + dy * dy);
2530
2531         wl_list_remove(&shsurf->rotation.transform.link);
2532         shsurf->surface->geometry.dirty = 1;
2533
2534         if (r > 20.0f) {
2535                 struct weston_matrix *matrix =
2536                         &shsurf->rotation.transform.matrix;
2537
2538                 weston_matrix_init(&rotate->rotation);
2539                 rotate->rotation.d[0] = dx / r;
2540                 rotate->rotation.d[4] = -dy / r;
2541                 rotate->rotation.d[1] = -rotate->rotation.d[4];
2542                 rotate->rotation.d[5] = rotate->rotation.d[0];
2543
2544                 weston_matrix_init(matrix);
2545                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2546                 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2547                 weston_matrix_multiply(matrix, &rotate->rotation);
2548                 weston_matrix_translate(matrix, cx, cy, 0.0f);
2549
2550                 wl_list_insert(
2551                         &shsurf->surface->geometry.transformation_list,
2552                         &shsurf->rotation.transform.link);
2553         } else {
2554                 wl_list_init(&shsurf->rotation.transform.link);
2555                 weston_matrix_init(&shsurf->rotation.rotation);
2556                 weston_matrix_init(&rotate->rotation);
2557         }
2558
2559         /* We need to adjust the position of the surface
2560          * in case it was resized in a rotated state before */
2561         cposx = surface->geometry.x + cx;
2562         cposy = surface->geometry.y + cy;
2563         dposx = rotate->center.x - cposx;
2564         dposy = rotate->center.y - cposy;
2565         if (dposx != 0.0f || dposy != 0.0f) {
2566                 weston_surface_set_position(surface,
2567                                             surface->geometry.x + dposx,
2568                                             surface->geometry.y + dposy);
2569         }
2570
2571         /* Repaint implies weston_surface_update_transform(), which
2572          * lazily applies the damage due to rotation update.
2573          */
2574         weston_compositor_schedule_repaint(shsurf->surface->compositor);
2575 }
2576
2577 static void
2578 rotate_grab_button(struct wl_pointer_grab *grab,
2579                  uint32_t time, uint32_t button, uint32_t state_w)
2580 {
2581         struct rotate_grab *rotate =
2582                 container_of(grab, struct rotate_grab, base.grab);
2583         struct wl_pointer *pointer = grab->pointer;
2584         struct shell_surface *shsurf = rotate->base.shsurf;
2585         enum wl_pointer_button_state state = state_w;
2586
2587         if (pointer->button_count == 0 &&
2588             state == WL_POINTER_BUTTON_STATE_RELEASED) {
2589                 if (shsurf)
2590                         weston_matrix_multiply(&shsurf->rotation.rotation,
2591                                                &rotate->rotation);
2592                 shell_grab_end(&rotate->base);
2593                 free(rotate);
2594         }
2595 }
2596
2597 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2598         noop_grab_focus,
2599         rotate_grab_motion,
2600         rotate_grab_button,
2601 };
2602
2603 static void
2604 surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
2605 {
2606         struct rotate_grab *rotate;
2607         float dx, dy;
2608         float r;
2609
2610         rotate = malloc(sizeof *rotate);
2611         if (!rotate)
2612                 return;
2613
2614         weston_surface_to_global_float(surface->surface,
2615                                        surface->surface->geometry.width / 2,
2616                                        surface->surface->geometry.height / 2,
2617                                        &rotate->center.x, &rotate->center.y);
2618
2619         dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2620         dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2621         r = sqrtf(dx * dx + dy * dy);
2622         if (r > 20.0f) {
2623                 struct weston_matrix inverse;
2624
2625                 weston_matrix_init(&inverse);
2626                 inverse.d[0] = dx / r;
2627                 inverse.d[4] = dy / r;
2628                 inverse.d[1] = -inverse.d[4];
2629                 inverse.d[5] = inverse.d[0];
2630                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2631
2632                 weston_matrix_init(&rotate->rotation);
2633                 rotate->rotation.d[0] = dx / r;
2634                 rotate->rotation.d[4] = -dy / r;
2635                 rotate->rotation.d[1] = -rotate->rotation.d[4];
2636                 rotate->rotation.d[5] = rotate->rotation.d[0];
2637         } else {
2638                 weston_matrix_init(&surface->rotation.rotation);
2639                 weston_matrix_init(&rotate->rotation);
2640         }
2641
2642         shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2643                          seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2644 }
2645
2646 static void
2647 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2648                void *data)
2649 {
2650         struct weston_surface *base_surface =
2651                 (struct weston_surface *) seat->pointer->focus;
2652         struct shell_surface *surface;
2653
2654         if (base_surface == NULL)
2655                 return;
2656
2657         surface = get_shell_surface(base_surface);
2658         if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2659                 return;
2660
2661         surface_rotate(surface, seat);
2662 }
2663
2664 static void
2665 lower_fullscreen_layer(struct desktop_shell *shell)
2666 {
2667         struct workspace *ws;
2668         struct weston_surface *surface, *prev;
2669
2670         ws = get_current_workspace(shell);
2671         wl_list_for_each_reverse_safe(surface, prev,
2672                                       &shell->fullscreen_layer.surface_list,
2673                                       layer_link)
2674                 weston_surface_restack(surface, &ws->layer.surface_list);
2675 }
2676
2677 static void
2678 activate(struct desktop_shell *shell, struct weston_surface *es,
2679          struct weston_seat *seat)
2680 {
2681         struct focus_state *state;
2682         struct workspace *ws;
2683
2684         weston_surface_activate(es, seat);
2685
2686         state = ensure_focus_state(shell, seat);
2687         if (state == NULL)
2688                 return;
2689
2690         state->keyboard_focus = es;
2691         wl_list_remove(&state->surface_destroy_listener.link);
2692         wl_signal_add(&es->surface.resource.destroy_signal,
2693                       &state->surface_destroy_listener);
2694
2695         switch (get_shell_surface_type(es)) {
2696         case SHELL_SURFACE_FULLSCREEN:
2697                 /* should on top of panels */
2698                 shell_stack_fullscreen(get_shell_surface(es));
2699                 shell_configure_fullscreen(get_shell_surface(es));
2700                 break;
2701         default:
2702                 ws = get_current_workspace(shell);
2703                 lower_fullscreen_layer(shell);
2704                 weston_surface_restack(es, &ws->layer.surface_list);
2705                 break;
2706         }
2707 }
2708
2709 /* no-op func for checking black surface */
2710 static void
2711 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2712 {
2713 }
2714
2715 static bool 
2716 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2717 {
2718         if (es->configure == black_surface_configure) {
2719                 if (fs_surface)
2720                         *fs_surface = (struct weston_surface *)es->private;
2721                 return true;
2722         }
2723         return false;
2724 }
2725
2726 static void
2727 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2728                           void *data)
2729 {
2730         struct weston_seat *ws = (struct weston_seat *) seat;
2731         struct desktop_shell *shell = data;
2732         struct weston_surface *focus;
2733         struct weston_surface *upper;
2734
2735         focus = (struct weston_surface *) seat->pointer->focus;
2736         if (!focus)
2737                 return;
2738
2739         if (is_black_surface(focus, &upper))
2740                 focus = upper;
2741
2742         if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2743                 return;
2744
2745         if (seat->pointer->grab == &seat->pointer->default_grab)
2746                 activate(shell, focus, ws);
2747 }
2748
2749 static void
2750 lock(struct wl_listener *listener, void *data)
2751 {
2752         struct desktop_shell *shell =
2753                 container_of(listener, struct desktop_shell, lock_listener);
2754         struct weston_output *output;
2755         struct workspace *ws = get_current_workspace(shell);
2756
2757         if (shell->locked) {
2758                 wl_list_for_each(output, &shell->compositor->output_list, link)
2759                         /* TODO: find a way to jump to other DPMS levels */
2760                         if (output->set_dpms)
2761                                 output->set_dpms(output, WESTON_DPMS_STANDBY);
2762                 return;
2763         }
2764
2765         shell->locked = true;
2766
2767         /* Hide all surfaces by removing the fullscreen, panel and
2768          * toplevel layers.  This way nothing else can show or receive
2769          * input events while we are locked. */
2770
2771         wl_list_remove(&shell->panel_layer.link);
2772         wl_list_remove(&shell->fullscreen_layer.link);
2773         if (shell->showing_input_panels)
2774                 wl_list_remove(&shell->input_panel_layer.link);
2775         wl_list_remove(&ws->layer.link);
2776         wl_list_insert(&shell->compositor->cursor_layer.link,
2777                        &shell->lock_layer.link);
2778
2779         launch_screensaver(shell);
2780
2781         /* TODO: disable bindings that should not work while locked. */
2782
2783         /* All this must be undone in resume_desktop(). */
2784 }
2785
2786 static void
2787 unlock(struct wl_listener *listener, void *data)
2788 {
2789         struct desktop_shell *shell =
2790                 container_of(listener, struct desktop_shell, unlock_listener);
2791
2792         if (!shell->locked || shell->lock_surface) {
2793                 weston_compositor_wake(shell->compositor);
2794                 return;
2795         }
2796
2797         /* If desktop-shell client has gone away, unlock immediately. */
2798         if (!shell->child.desktop_shell) {
2799                 resume_desktop(shell);
2800                 return;
2801         }
2802
2803         if (shell->prepare_event_sent)
2804                 return;
2805
2806         desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2807         shell->prepare_event_sent = true;
2808 }
2809
2810 static void
2811 show_input_panels(struct wl_listener *listener, void *data)
2812 {
2813         struct desktop_shell *shell =
2814                 container_of(listener, struct desktop_shell,
2815                              show_input_panel_listener);
2816         struct input_panel_surface *surface, *next;
2817         struct weston_surface *ws;
2818
2819         if (shell->showing_input_panels)
2820                 return;
2821
2822         shell->showing_input_panels = true;
2823
2824         if (!shell->locked)
2825                 wl_list_insert(&shell->panel_layer.link,
2826                                &shell->input_panel_layer.link);
2827
2828         wl_list_for_each_safe(surface, next,
2829                               &shell->input_panel.surfaces, link) {
2830                 ws = surface->surface;
2831                 wl_list_insert(&shell->input_panel_layer.surface_list,
2832                                &ws->layer_link);
2833                 ws->geometry.dirty = 1;
2834                 weston_surface_update_transform(ws);
2835                 weston_surface_damage(ws);
2836                 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2837         }
2838 }
2839
2840 static void
2841 hide_input_panels(struct wl_listener *listener, void *data)
2842 {
2843         struct desktop_shell *shell =
2844                 container_of(listener, struct desktop_shell,
2845                              hide_input_panel_listener);
2846         struct weston_surface *surface, *next;
2847
2848         shell->showing_input_panels = false;
2849
2850         if (!shell->locked)
2851                 wl_list_remove(&shell->input_panel_layer.link);
2852
2853         wl_list_for_each_safe(surface, next,
2854                               &shell->input_panel_layer.surface_list, layer_link)
2855                 weston_surface_unmap(surface);
2856 }
2857
2858 static void
2859 center_on_output(struct weston_surface *surface, struct weston_output *output)
2860 {
2861         float x = (output->width - surface->buffer->width) / 2;
2862         float y = (output->height - surface->buffer->height) / 2;
2863
2864         weston_surface_configure(surface, output->x + x, output->y + y,
2865                                  surface->buffer->width,
2866                                  surface->buffer->height);
2867 }
2868
2869 static void
2870 weston_surface_set_initial_position (struct weston_surface *surface,
2871                                      struct desktop_shell *shell)
2872 {
2873         struct weston_compositor *compositor = shell->compositor;
2874         int ix = 0, iy = 0;
2875         int range_x, range_y;
2876         int dx, dy, x, y, panel_height;
2877         struct weston_output *output, *target_output = NULL;
2878         struct weston_seat *seat;
2879
2880         /* As a heuristic place the new window on the same output as the
2881          * pointer. Falling back to the output containing 0, 0.
2882          *
2883          * TODO: Do something clever for touch too?
2884          */
2885         wl_list_for_each(seat, &compositor->seat_list, link) {
2886                 if (seat->has_pointer) {
2887                         ix = wl_fixed_to_int(seat->pointer.x);
2888                         iy = wl_fixed_to_int(seat->pointer.y);
2889                         break;
2890                 }
2891         }
2892
2893         wl_list_for_each(output, &compositor->output_list, link) {
2894                 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2895                         target_output = output;
2896                         break;
2897                 }
2898         }
2899
2900         if (!target_output) {
2901                 weston_surface_set_position(surface, 10 + random() % 400,
2902                                            10 + random() % 400);
2903                 return;
2904         }
2905
2906         /* Valid range within output where the surface will still be onscreen.
2907          * If this is negative it means that the surface is bigger than
2908          * output.
2909          */
2910         panel_height = get_output_panel_height(shell, target_output);
2911         range_x = target_output->width - surface->geometry.width;
2912         range_y = (target_output->height - panel_height) -
2913                   surface->geometry.height;
2914
2915         if (range_x > 0)
2916                 dx = random() % range_x;
2917         else
2918                 dx = 0;
2919
2920         if (range_y > 0)
2921                 dy = panel_height + random() % range_y;
2922         else
2923                 dy = panel_height;
2924
2925         x = target_output->x + dx;
2926         y = target_output->y + dy;
2927
2928         weston_surface_set_position (surface, x, y);
2929 }
2930
2931 static void
2932 map(struct desktop_shell *shell, struct weston_surface *surface,
2933     int32_t width, int32_t height, int32_t sx, int32_t sy)
2934 {
2935         struct weston_compositor *compositor = shell->compositor;
2936         struct shell_surface *shsurf = get_shell_surface(surface);
2937         enum shell_surface_type surface_type = shsurf->type;
2938         struct weston_surface *parent;
2939         struct weston_seat *seat;
2940         struct workspace *ws;
2941         int panel_height = 0;
2942
2943         surface->geometry.width = width;
2944         surface->geometry.height = height;
2945         surface->geometry.dirty = 1;
2946
2947         /* initial positioning, see also configure() */
2948         switch (surface_type) {
2949         case SHELL_SURFACE_TOPLEVEL:
2950                 weston_surface_set_initial_position(surface, shell);
2951                 break;
2952         case SHELL_SURFACE_FULLSCREEN:
2953                 center_on_output(surface, shsurf->fullscreen_output);
2954                 shell_map_fullscreen(shsurf);
2955                 break;
2956         case SHELL_SURFACE_MAXIMIZED:
2957                 /* use surface configure to set the geometry */
2958                 panel_height = get_output_panel_height(shell,surface->output);
2959                 weston_surface_set_position(surface, shsurf->output->x,
2960                                             shsurf->output->y + panel_height);
2961                 break;
2962         case SHELL_SURFACE_POPUP:
2963                 shell_map_popup(shsurf);
2964                 break;
2965         case SHELL_SURFACE_NONE:
2966                 weston_surface_set_position(surface,
2967                                             surface->geometry.x + sx,
2968                                             surface->geometry.y + sy);
2969                 break;
2970         default:
2971                 ;
2972         }
2973
2974         /* surface stacking order, see also activate() */
2975         switch (surface_type) {
2976         case SHELL_SURFACE_POPUP:
2977         case SHELL_SURFACE_TRANSIENT:
2978                 parent = shsurf->parent;
2979                 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2980                 break;
2981         case SHELL_SURFACE_FULLSCREEN:
2982         case SHELL_SURFACE_NONE:
2983                 break;
2984         default:
2985                 ws = get_current_workspace(shell);
2986                 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2987                 break;
2988         }
2989
2990         if (surface_type != SHELL_SURFACE_NONE) {
2991                 weston_surface_update_transform(surface);
2992                 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2993                         surface->output = shsurf->output;
2994         }
2995
2996         switch (surface_type) {
2997         case SHELL_SURFACE_TRANSIENT:
2998                 if (shsurf->transient.flags ==
2999                                 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3000                         break;
3001         case SHELL_SURFACE_TOPLEVEL:
3002         case SHELL_SURFACE_FULLSCREEN:
3003         case SHELL_SURFACE_MAXIMIZED:
3004                 if (!shell->locked) {
3005                         wl_list_for_each(seat, &compositor->seat_list, link)
3006                                 activate(shell, surface, seat);
3007                 }
3008                 break;
3009         default:
3010                 break;
3011         }
3012
3013         if (surface_type == SHELL_SURFACE_TOPLEVEL)
3014         {
3015                 switch (shell->win_animation_type) {
3016                 case ANIMATION_FADE:
3017                         weston_fade_run(surface, NULL, NULL);
3018                         break;
3019                 case ANIMATION_ZOOM:
3020                         weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3021                         break;
3022                 default:
3023                         break;
3024                 }
3025         }
3026 }
3027
3028 static void
3029 configure(struct desktop_shell *shell, struct weston_surface *surface,
3030           float x, float y, int32_t width, int32_t height)
3031 {
3032         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3033         struct shell_surface *shsurf;
3034
3035         shsurf = get_shell_surface(surface);
3036         if (shsurf)
3037                 surface_type = shsurf->type;
3038
3039         surface->geometry.x = x;
3040         surface->geometry.y = y;
3041         surface->geometry.width = width;
3042         surface->geometry.height = height;
3043         surface->geometry.dirty = 1;
3044
3045         switch (surface_type) {
3046         case SHELL_SURFACE_FULLSCREEN:
3047                 shell_stack_fullscreen(shsurf);
3048                 shell_configure_fullscreen(shsurf);
3049                 break;
3050         case SHELL_SURFACE_MAXIMIZED:
3051                 /* setting x, y and using configure to change that geometry */
3052                 surface->geometry.x = surface->output->x;
3053                 surface->geometry.y = surface->output->y +
3054                         get_output_panel_height(shell,surface->output);
3055                 break;
3056         case SHELL_SURFACE_TOPLEVEL:
3057                 break;
3058         default:
3059                 break;
3060         }
3061
3062         /* XXX: would a fullscreen surface need the same handling? */
3063         if (surface->output) {
3064                 weston_surface_update_transform(surface);
3065
3066                 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3067                         surface->output = shsurf->output;
3068         }
3069 }
3070
3071 static void
3072 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3073 {
3074         struct shell_surface *shsurf = get_shell_surface(es);
3075         struct desktop_shell *shell = shsurf->shell;
3076         int type_changed = 0;
3077
3078         if (shsurf->next_type != SHELL_SURFACE_NONE &&
3079             shsurf->type != shsurf->next_type) {
3080                 set_surface_type(shsurf);
3081                 type_changed = 1;
3082         }
3083
3084         if (!weston_surface_is_mapped(es)) {
3085                 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
3086         } else if (type_changed || sx != 0 || sy != 0 ||
3087                    es->geometry.width != es->buffer->width ||
3088                    es->geometry.height != es->buffer->height) {
3089                 float from_x, from_y;
3090                 float to_x, to_y;
3091
3092                 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3093                 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3094                 configure(shell, es,
3095                           es->geometry.x + to_x - from_x,
3096                           es->geometry.y + to_y - from_y,
3097                           es->buffer->width, es->buffer->height);
3098         }
3099 }
3100
3101 static void launch_desktop_shell_process(void *data);
3102
3103 static void
3104 desktop_shell_sigchld(struct weston_process *process, int status)
3105 {
3106         uint32_t time;
3107         struct desktop_shell *shell =
3108                 container_of(process, struct desktop_shell, child.process);
3109
3110         shell->child.process.pid = 0;
3111         shell->child.client = NULL; /* already destroyed by wayland */
3112
3113         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3114         time = weston_compositor_get_time();
3115         if (time - shell->child.deathstamp > 30000) {
3116                 shell->child.deathstamp = time;
3117                 shell->child.deathcount = 0;
3118         }
3119
3120         shell->child.deathcount++;
3121         if (shell->child.deathcount > 5) {
3122                 weston_log("weston-desktop-shell died, giving up.\n");
3123                 return;
3124         }
3125
3126         weston_log("weston-desktop-shell died, respawning...\n");
3127         launch_desktop_shell_process(shell);
3128 }
3129
3130 static void
3131 launch_desktop_shell_process(void *data)
3132 {
3133         struct desktop_shell *shell = data;
3134         const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3135
3136         shell->child.client = weston_client_launch(shell->compositor,
3137                                                  &shell->child.process,
3138                                                  shell_exe,
3139                                                  desktop_shell_sigchld);
3140
3141         if (!shell->child.client)
3142                 weston_log("not able to start %s\n", shell_exe);
3143 }
3144
3145 static void
3146 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3147 {
3148         struct desktop_shell *shell = data;
3149
3150         wl_client_add_object(client, &wl_shell_interface,
3151                              &shell_implementation, id, shell);
3152 }
3153
3154 static void
3155 unbind_desktop_shell(struct wl_resource *resource)
3156 {
3157         struct desktop_shell *shell = resource->data;
3158
3159         if (shell->locked)
3160                 resume_desktop(shell);
3161
3162         shell->child.desktop_shell = NULL;
3163         shell->prepare_event_sent = false;
3164         free(resource);
3165 }
3166
3167 static void
3168 bind_desktop_shell(struct wl_client *client,
3169                    void *data, uint32_t version, uint32_t id)
3170 {
3171         struct desktop_shell *shell = data;
3172         struct wl_resource *resource;
3173
3174         resource = wl_client_add_object(client, &desktop_shell_interface,
3175                                         &desktop_shell_implementation,
3176                                         id, shell);
3177
3178         if (client == shell->child.client) {
3179                 resource->destroy = unbind_desktop_shell;
3180                 shell->child.desktop_shell = resource;
3181                 return;
3182         }
3183
3184         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3185                                "permission to bind desktop_shell denied");
3186         wl_resource_destroy(resource);
3187 }
3188
3189 static void
3190 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3191 {
3192         struct desktop_shell *shell = surface->private;
3193
3194         if (!shell->locked)
3195                 return;
3196
3197         center_on_output(surface, surface->output);
3198
3199         if (wl_list_empty(&surface->layer_link)) {
3200                 wl_list_insert(shell->lock_layer.surface_list.prev,
3201                                &surface->layer_link);
3202                 weston_surface_update_transform(surface);
3203                 shell->compositor->idle_time = shell->screensaver.duration;
3204                 weston_compositor_wake(shell->compositor);
3205                 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3206         }
3207 }
3208
3209 static void
3210 screensaver_set_surface(struct wl_client *client,
3211                         struct wl_resource *resource,
3212                         struct wl_resource *surface_resource,
3213                         struct wl_resource *output_resource)
3214 {
3215         struct desktop_shell *shell = resource->data;
3216         struct weston_surface *surface = surface_resource->data;
3217         struct weston_output *output = output_resource->data;
3218
3219         surface->configure = screensaver_configure;
3220         surface->private = shell;
3221         surface->output = output;
3222 }
3223
3224 static const struct screensaver_interface screensaver_implementation = {
3225         screensaver_set_surface
3226 };
3227
3228 static void
3229 unbind_screensaver(struct wl_resource *resource)
3230 {
3231         struct desktop_shell *shell = resource->data;
3232
3233         shell->screensaver.binding = NULL;
3234         free(resource);
3235 }
3236
3237 static void
3238 bind_screensaver(struct wl_client *client,
3239                  void *data, uint32_t version, uint32_t id)
3240 {
3241         struct desktop_shell *shell = data;
3242         struct wl_resource *resource;
3243
3244         resource = wl_client_add_object(client, &screensaver_interface,
3245                                         &screensaver_implementation,
3246                                         id, shell);
3247
3248         if (shell->screensaver.binding == NULL) {
3249                 resource->destroy = unbind_screensaver;
3250                 shell->screensaver.binding = resource;
3251                 return;
3252         }
3253
3254         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3255                                "interface object already bound");
3256         wl_resource_destroy(resource);
3257 }
3258
3259 static void
3260 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3261 {
3262         struct weston_mode *mode = surface->output->current;
3263         float x = (mode->width - surface->buffer->width) / 2;
3264         float y = mode->height - surface->buffer->height;
3265
3266         /* Don't map the input panel here, wait for
3267          * show_input_panels signal. */
3268
3269         weston_surface_configure(surface,
3270                                  surface->output->x + x,
3271                                  surface->output->y + y,
3272                                  surface->buffer->width,
3273                                  surface->buffer->height);
3274 }
3275
3276 static void
3277 destroy_input_panel_surface(struct wl_listener *listener,
3278                             void *data)
3279 {
3280         struct input_panel_surface *input_panel_surface =
3281                 container_of(listener, struct input_panel_surface, listener);
3282
3283         wl_list_remove(&listener->link);
3284         wl_list_remove(&input_panel_surface->link);
3285
3286         free(input_panel_surface);
3287 }
3288
3289 static void
3290 input_panel_set_surface(struct wl_client *client,
3291                         struct wl_resource *resource,
3292                         struct wl_resource *surface_resource,
3293                         struct wl_resource *output_resource)
3294 {
3295         struct desktop_shell *shell = resource->data;
3296         struct weston_surface *surface = surface_resource->data;
3297         struct weston_output *output = output_resource->data;
3298         struct input_panel_surface *input_panel_surface;
3299
3300         surface->configure = input_panel_configure;
3301         surface->private = shell;
3302         surface->output = output;
3303
3304         input_panel_surface = malloc(sizeof *input_panel_surface);
3305         if (!input_panel_surface) {
3306                 wl_resource_post_no_memory(resource);
3307                 return;
3308         }
3309
3310         input_panel_surface->surface = surface;
3311         input_panel_surface->listener.notify = destroy_input_panel_surface;
3312
3313         wl_signal_add(&surface_resource->destroy_signal,
3314                       &input_panel_surface->listener);
3315
3316         wl_list_insert(&shell->input_panel.surfaces,
3317                        &input_panel_surface->link);
3318 }
3319
3320 static const struct input_panel_interface input_panel_implementation = {
3321         input_panel_set_surface
3322 };
3323
3324 static void
3325 unbind_input_panel(struct wl_resource *resource)
3326 {
3327         struct desktop_shell *shell = resource->data;
3328
3329         shell->input_panel.binding = NULL;
3330         free(resource);
3331 }
3332
3333 static void
3334 bind_input_panel(struct wl_client *client,
3335               void *data, uint32_t version, uint32_t id)
3336 {
3337         struct desktop_shell *shell = data;
3338         struct wl_resource *resource;
3339
3340         resource = wl_client_add_object(client, &input_panel_interface,
3341                                         &input_panel_implementation,
3342                                         id, shell);
3343
3344         if (shell->input_panel.binding == NULL) {
3345                 resource->destroy = unbind_input_panel;
3346                 shell->input_panel.binding = resource;
3347                 return;
3348         }
3349
3350         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3351                                "interface object already bound");
3352         wl_resource_destroy(resource);
3353 }
3354
3355 struct switcher {
3356         struct desktop_shell *shell;
3357         struct weston_surface *current;
3358         struct wl_listener listener;
3359         struct wl_keyboard_grab grab;
3360 };
3361
3362 static void
3363 switcher_next(struct switcher *switcher)
3364 {
3365         struct weston_surface *surface;
3366         struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3367         struct shell_surface *shsurf;
3368         struct workspace *ws = get_current_workspace(switcher->shell);
3369
3370         wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3371                 switch (get_shell_surface_type(surface)) {
3372                 case SHELL_SURFACE_TOPLEVEL:
3373                 case SHELL_SURFACE_FULLSCREEN:
3374                 case SHELL_SURFACE_MAXIMIZED:
3375                         if (first == NULL)
3376                                 first = surface;
3377                         if (prev == switcher->current)
3378                                 next = surface;
3379                         prev = surface;
3380                         surface->alpha = 0.25;
3381                         surface->geometry.dirty = 1;
3382                         weston_surface_damage(surface);
3383                         break;
3384                 default:
3385                         break;
3386                 }
3387
3388                 if (is_black_surface(surface, NULL)) {
3389                         surface->alpha = 0.25;
3390                         surface->geometry.dirty = 1;
3391                         weston_surface_damage(surface);
3392                 }
3393         }
3394
3395         if (next == NULL)
3396                 next = first;
3397
3398         if (next == NULL)
3399                 return;
3400
3401         wl_list_remove(&switcher->listener.link);
3402         wl_signal_add(&next->surface.resource.destroy_signal,
3403                       &switcher->listener);
3404
3405         switcher->current = next;
3406         next->alpha = 1.0;
3407
3408         shsurf = get_shell_surface(switcher->current);
3409         if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3410                 shsurf->fullscreen.black_surface->alpha = 1.0;
3411 }
3412
3413 static void
3414 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3415 {
3416         struct switcher *switcher =
3417                 container_of(listener, struct switcher, listener);
3418
3419         switcher_next(switcher);
3420 }
3421
3422 static void
3423 switcher_destroy(struct switcher *switcher)
3424 {
3425         struct weston_surface *surface;
3426         struct wl_keyboard *keyboard = switcher->grab.keyboard;
3427         struct workspace *ws = get_current_workspace(switcher->shell);
3428
3429         wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3430                 surface->alpha = 1.0;
3431                 weston_surface_damage(surface);
3432         }
3433
3434         if (switcher->current)
3435                 activate(switcher->shell, switcher->current,
3436                          (struct weston_seat *) keyboard->seat);
3437         wl_list_remove(&switcher->listener.link);
3438         wl_keyboard_end_grab(keyboard);
3439         free(switcher);
3440 }
3441
3442 static void
3443 switcher_key(struct wl_keyboard_grab *grab,
3444              uint32_t time, uint32_t key, uint32_t state_w)
3445 {
3446         struct switcher *switcher = container_of(grab, struct switcher, grab);
3447         enum wl_keyboard_key_state state = state_w;
3448
3449         if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3450                 switcher_next(switcher);
3451 }
3452
3453 static void
3454 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3455                   uint32_t mods_depressed, uint32_t mods_latched,
3456                   uint32_t mods_locked, uint32_t group)
3457 {
3458         struct switcher *switcher = container_of(grab, struct switcher, grab);
3459         struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3460
3461         if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3462                 switcher_destroy(switcher);
3463 }
3464
3465 static const struct wl_keyboard_grab_interface switcher_grab = {
3466         switcher_key,
3467         switcher_modifier,
3468 };
3469
3470 static void
3471 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3472                  void *data)
3473 {
3474         struct desktop_shell *shell = data;
3475         struct switcher *switcher;
3476
3477         switcher = malloc(sizeof *switcher);
3478         switcher->shell = shell;
3479         switcher->current = NULL;
3480         switcher->listener.notify = switcher_handle_surface_destroy;
3481         wl_list_init(&switcher->listener.link);
3482
3483         lower_fullscreen_layer(switcher->shell);
3484         switcher->grab.interface = &switcher_grab;
3485         wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3486         wl_keyboard_set_focus(seat->keyboard, NULL);
3487         switcher_next(switcher);
3488 }
3489
3490 static void
3491 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3492                   void *data)
3493 {
3494         struct weston_compositor *compositor = data;
3495         struct weston_output *output;
3496         long backlight_new = 0;
3497
3498         /* TODO: we're limiting to simple use cases, where we assume just
3499          * control on the primary display. We'd have to extend later if we
3500          * ever get support for setting backlights on random desktop LCD
3501          * panels though */
3502         output = get_default_output(compositor);
3503         if (!output)
3504                 return;
3505
3506         if (!output->set_backlight)
3507                 return;
3508
3509         if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3510                 backlight_new = output->backlight_current - 25;
3511         else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3512                 backlight_new = output->backlight_current + 25;
3513
3514         if (backlight_new < 5)
3515                 backlight_new = 5;
3516         if (backlight_new > 255)
3517                 backlight_new = 255;
3518
3519         output->backlight_current = backlight_new;
3520         output->set_backlight(output, output->backlight_current);
3521 }
3522
3523 static void
3524 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3525                       void *data)
3526 {
3527         struct desktop_shell *shell = data;
3528         struct weston_compositor *compositor = shell->compositor;
3529         struct weston_surface *surface;
3530         struct weston_plane plane;
3531
3532         if (shell->debug_repaint_surface) {
3533                 weston_surface_destroy(shell->debug_repaint_surface);
3534                 shell->debug_repaint_surface = NULL;
3535         } else {
3536                 surface = weston_surface_create(compositor);
3537                 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3538                 weston_surface_configure(surface, 0, 0, 8192, 8192);
3539                 wl_list_insert(&compositor->fade_layer.surface_list,
3540                                &surface->layer_link);
3541                 pixman_region32_fini(&surface->input);
3542                 pixman_region32_init(&surface->input);
3543
3544                 /* Here's the dirty little trick that makes the
3545                  * repaint debugging work: we move the surface to a
3546                  * different plane and force an update_transform to
3547                  * update dependent state and clear the
3548                  * geometry.dirty bit.  This way the call to
3549                  * damage_below() in update_transform() does not
3550                  * add damage to the primary plane.  */
3551
3552                 weston_plane_init(&plane, 0, 0);
3553                 surface->plane = &plane;
3554                 weston_surface_update_transform(surface);
3555                 shell->debug_repaint_surface = surface;
3556                 surface->plane = &compositor->primary_plane;
3557                 weston_plane_release(&plane);
3558         }
3559 }
3560
3561
3562 static void
3563 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3564                       void *data)
3565 {
3566         struct desktop_shell *shell = data;
3567         struct weston_compositor *compositor = shell->compositor;
3568         compositor->fan_debug = !compositor->fan_debug;
3569         weston_compositor_damage_all(compositor);
3570 }
3571
3572 static void
3573 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3574                    void *data)
3575 {
3576         struct wl_surface *focus_surface;
3577         struct wl_client *client;
3578         struct desktop_shell *shell = data;
3579         struct weston_compositor *compositor = shell->compositor;
3580         pid_t pid;
3581
3582         focus_surface = seat->keyboard->focus;
3583         if (!focus_surface)
3584                 return;
3585
3586         wl_signal_emit(&compositor->kill_signal, focus_surface);
3587
3588         client = focus_surface->resource.client;
3589         wl_client_get_credentials(client, &pid, NULL, NULL);
3590
3591         /* Skip clients that we launched ourselves (the credentials of
3592          * the socketpair is ours) */
3593         if (pid == getpid())
3594                 return;
3595
3596         kill(pid, SIGKILL);
3597 }
3598
3599 static void
3600 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3601                      uint32_t key, void *data)
3602 {
3603         struct desktop_shell *shell = data;
3604         unsigned int new_index = shell->workspaces.current;
3605
3606         if (shell->locked)
3607                 return;
3608         if (new_index != 0)
3609                 new_index--;
3610
3611         change_workspace(shell, new_index);
3612 }
3613
3614 static void
3615 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3616                        uint32_t key, void *data)
3617 {
3618         struct desktop_shell *shell = data;
3619         unsigned int new_index = shell->workspaces.current;
3620
3621         if (shell->locked)
3622                 return;
3623         if (new_index < shell->workspaces.num - 1)
3624                 new_index++;
3625
3626         change_workspace(shell, new_index);
3627 }
3628
3629 static void
3630 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3631                     uint32_t key, void *data)
3632 {
3633         struct desktop_shell *shell = data;
3634         unsigned int new_index;
3635
3636         if (shell->locked)
3637                 return;
3638         new_index = key - KEY_F1;
3639         if (new_index >= shell->workspaces.num)
3640                 new_index = shell->workspaces.num - 1;
3641
3642         change_workspace(shell, new_index);
3643 }
3644
3645 static void
3646 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3647                                   uint32_t key, void *data)
3648 {
3649         struct desktop_shell *shell = data;
3650         unsigned int new_index = shell->workspaces.current;
3651
3652         if (shell->locked)
3653                 return;
3654
3655         if (new_index != 0)
3656                 new_index--;
3657
3658         take_surface_to_workspace_by_seat(shell, seat, new_index);
3659 }
3660
3661 static void
3662 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3663                                     uint32_t key, void *data)
3664 {
3665         struct desktop_shell *shell = data;
3666         unsigned int new_index = shell->workspaces.current;
3667
3668         if (shell->locked)
3669                 return;
3670
3671         if (new_index < shell->workspaces.num - 1)
3672                 new_index++;
3673
3674         take_surface_to_workspace_by_seat(shell, seat, new_index);
3675 }
3676
3677 static void
3678 shell_destroy(struct wl_listener *listener, void *data)
3679 {
3680         struct desktop_shell *shell =
3681                 container_of(listener, struct desktop_shell, destroy_listener);
3682         struct workspace **ws;
3683
3684         if (shell->child.client)
3685                 wl_client_destroy(shell->child.client);
3686
3687         wl_list_remove(&shell->lock_listener.link);
3688         wl_list_remove(&shell->unlock_listener.link);
3689         wl_list_remove(&shell->show_input_panel_listener.link);
3690         wl_list_remove(&shell->hide_input_panel_listener.link);
3691
3692         wl_array_for_each(ws, &shell->workspaces.array)
3693                 workspace_destroy(*ws);
3694         wl_array_release(&shell->workspaces.array);
3695
3696         free(shell->screensaver.path);
3697         free(shell);
3698 }
3699
3700 static void
3701 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3702 {
3703         uint32_t mod;
3704         int i, num_workspace_bindings;
3705
3706         /* fixed bindings */
3707         weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3708                                           MODIFIER_CTRL | MODIFIER_ALT,
3709                                           terminate_binding, ec);
3710         weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3711                                              click_to_activate_binding,
3712                                              shell);
3713         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3714                                            MODIFIER_SUPER | MODIFIER_ALT,
3715                                            surface_opacity_binding, NULL);
3716         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3717                                            MODIFIER_SUPER, zoom_axis_binding,
3718                                            NULL);
3719
3720         /* configurable bindings */
3721         mod = shell->binding_modifier;
3722         weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3723                                           zoom_key_binding, NULL);
3724         weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3725                                           zoom_key_binding, NULL);
3726         weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3727                                              shell);
3728         weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3729                                              resize_binding, shell);
3730         weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3731                                              rotate_binding, NULL);
3732         weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3733                                           shell);
3734         weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3735                                           ec);
3736         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3737                                           backlight_binding, ec);
3738         weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3739                                           ec);
3740         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3741                                           backlight_binding, ec);
3742         weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3743                                           debug_repaint_binding, shell);
3744         weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3745                                           fan_debug_repaint_binding, shell);
3746         weston_compositor_add_key_binding(ec, KEY_K, mod,
3747                                           force_kill_binding, shell);
3748         weston_compositor_add_key_binding(ec, KEY_UP, mod,
3749                                           workspace_up_binding, shell);
3750         weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3751                                           workspace_down_binding, shell);
3752         weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3753                                           workspace_move_surface_up_binding,
3754                                           shell);
3755         weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3756                                           workspace_move_surface_down_binding,
3757                                           shell);
3758
3759         /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3760         if (shell->workspaces.num > 1) {
3761                 num_workspace_bindings = shell->workspaces.num;
3762                 if (num_workspace_bindings > 6)
3763                         num_workspace_bindings = 6;
3764                 for (i = 0; i < num_workspace_bindings; i++)
3765                         weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3766                                                           workspace_f_binding,
3767                                                           shell);
3768         }
3769 }
3770
3771 WL_EXPORT int
3772 module_init(struct weston_compositor *ec)
3773 {
3774         struct weston_seat *seat;
3775         struct desktop_shell *shell;
3776         struct workspace **pws;
3777         unsigned int i;
3778         struct wl_event_loop *loop;
3779
3780         shell = malloc(sizeof *shell);
3781         if (shell == NULL)
3782                 return -1;
3783
3784         memset(shell, 0, sizeof *shell);
3785         shell->compositor = ec;
3786
3787         shell->destroy_listener.notify = shell_destroy;
3788         wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3789         shell->lock_listener.notify = lock;
3790         wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3791         shell->unlock_listener.notify = unlock;
3792         wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3793         shell->show_input_panel_listener.notify = show_input_panels;
3794         wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3795         shell->hide_input_panel_listener.notify = hide_input_panels;
3796         wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3797         ec->ping_handler = ping_handler;
3798         ec->shell_interface.shell = shell;
3799         ec->shell_interface.create_shell_surface = create_shell_surface;
3800         ec->shell_interface.set_toplevel = set_toplevel;
3801         ec->shell_interface.set_transient = set_transient;
3802         ec->shell_interface.move = surface_move;
3803         ec->shell_interface.resize = surface_resize;
3804
3805         wl_list_init(&shell->screensaver.surfaces);
3806         wl_list_init(&shell->input_panel.surfaces);
3807
3808         weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3809         weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3810         weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3811         weston_layer_init(&shell->lock_layer, NULL);
3812         weston_layer_init(&shell->input_panel_layer, NULL);
3813
3814         wl_array_init(&shell->workspaces.array);
3815         wl_list_init(&shell->workspaces.client_list);
3816
3817         shell_configuration(shell);
3818
3819         for (i = 0; i < shell->workspaces.num; i++) {
3820                 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3821                 if (pws == NULL)
3822                         return -1;
3823
3824                 *pws = workspace_create();
3825                 if (*pws == NULL)
3826                         return -1;
3827         }
3828         activate_workspace(shell, 0);
3829
3830         wl_list_init(&shell->workspaces.anim_sticky_list);
3831         wl_list_init(&shell->workspaces.animation.link);
3832         shell->workspaces.animation.frame = animate_workspace_change_frame;
3833
3834         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3835                                   shell, bind_shell) == NULL)
3836                 return -1;
3837
3838         if (wl_display_add_global(ec->wl_display,
3839                                   &desktop_shell_interface,
3840                                   shell, bind_desktop_shell) == NULL)
3841                 return -1;
3842
3843         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3844                                   shell, bind_screensaver) == NULL)
3845                 return -1;
3846
3847         if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3848                                   shell, bind_input_panel) == NULL)
3849                 return -1;
3850
3851         if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3852                                   shell, bind_workspace_manager) == NULL)
3853                 return -1;
3854
3855         shell->child.deathstamp = weston_compositor_get_time();
3856
3857         loop = wl_display_get_event_loop(ec->wl_display);
3858         wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
3859
3860         wl_list_for_each(seat, &ec->seat_list, link)
3861                 create_pointer_focus_listener(seat);
3862
3863         shell_add_bindings(ec, shell);
3864
3865         return 0;
3866 }