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