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