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