Split bindings into separate key/button/axis bindings
[profile/ivi/weston.git] / src / shell.c
1 /*
2  * Copyright © 2010-2012 Intel Corporation
3  * Copyright © 2011-2012 Collabora, Ltd.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <linux/input.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <math.h>
33 #include <sys/types.h>
34
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "../shared/config-parser.h"
39
40 enum animation_type {
41         ANIMATION_NONE,
42
43         ANIMATION_ZOOM,
44         ANIMATION_FADE
45 };
46
47 struct desktop_shell {
48         struct weston_compositor *compositor;
49
50         struct wl_listener lock_listener;
51         struct wl_listener unlock_listener;
52         struct wl_listener destroy_listener;
53
54         struct weston_layer fullscreen_layer;
55         struct weston_layer panel_layer;
56         struct weston_layer toplevel_layer;
57         struct weston_layer background_layer;
58         struct weston_layer lock_layer;
59
60         struct {
61                 struct weston_process process;
62                 struct wl_client *client;
63                 struct wl_resource *desktop_shell;
64
65                 unsigned deathcount;
66                 uint32_t deathstamp;
67         } child;
68
69         bool locked;
70         bool prepare_event_sent;
71
72         struct shell_surface *lock_surface;
73         struct wl_listener lock_surface_listener;
74
75         struct wl_list backgrounds;
76         struct wl_list panels;
77
78         struct {
79                 char *path;
80                 int duration;
81                 struct wl_resource *binding;
82                 struct wl_list surfaces;
83                 struct weston_process process;
84         } screensaver;
85
86         uint32_t binding_modifier;
87         enum animation_type win_animation_type;
88         struct weston_surface *debug_repaint_surface;
89 };
90
91 enum shell_surface_type {
92         SHELL_SURFACE_NONE,
93
94         SHELL_SURFACE_PANEL,
95         SHELL_SURFACE_BACKGROUND,
96         SHELL_SURFACE_LOCK,
97         SHELL_SURFACE_SCREENSAVER,
98
99         SHELL_SURFACE_TOPLEVEL,
100         SHELL_SURFACE_TRANSIENT,
101         SHELL_SURFACE_FULLSCREEN,
102         SHELL_SURFACE_MAXIMIZED,
103         SHELL_SURFACE_POPUP
104 };
105
106 struct ping_timer {
107         struct wl_event_source *source;
108         uint32_t serial;
109 };
110
111 struct shell_surface {
112         struct wl_resource resource;
113
114         struct weston_surface *surface;
115         struct wl_listener surface_destroy_listener;
116         struct shell_surface *parent;
117         struct desktop_shell *shell;
118
119         enum shell_surface_type type, next_type;
120         char *title, *class;
121         int32_t saved_x, saved_y;
122         bool saved_position_valid;
123         bool saved_rotation_valid;
124         int unresponsive;
125
126         struct {
127                 struct weston_transform transform;
128                 struct weston_matrix rotation;
129         } rotation;
130
131         struct {
132                 struct wl_pointer_grab grab;
133                 int32_t x, y;
134                 struct weston_transform parent_transform;
135                 int32_t initial_up;
136                 struct wl_seat *seat;
137                 uint32_t serial;
138         } popup;
139
140         struct {
141                 int32_t x, y;
142                 uint32_t flags;
143         } transient;
144
145         struct {
146                 enum wl_shell_surface_fullscreen_method type;
147                 struct weston_transform transform; /* matrix from x, y */
148                 uint32_t framerate;
149                 struct weston_surface *black_surface;
150         } fullscreen;
151
152         struct ping_timer *ping_timer;
153
154         struct {
155                 struct weston_animation current;
156                 int exists;
157                 int fading_in;
158                 uint32_t timestamp;
159         } unresponsive_animation;
160
161         struct weston_output *fullscreen_output;
162         struct weston_output *output;
163         struct wl_list link;
164
165         const struct weston_shell_client *client;
166 };
167
168 struct shell_grab {
169         struct wl_pointer_grab grab;
170         struct shell_surface *shsurf;
171         struct wl_listener shsurf_destroy_listener;
172 };
173
174 struct weston_move_grab {
175         struct shell_grab base;
176         wl_fixed_t dx, dy;
177 };
178
179 struct rotate_grab {
180         struct shell_grab base;
181         struct weston_matrix rotation;
182         struct {
183                 int32_t x;
184                 int32_t y;
185         } center;
186 };
187
188 static struct shell_surface *
189 get_shell_surface(struct weston_surface *surface);
190
191 static struct desktop_shell *
192 shell_surface_get_shell(struct shell_surface *shsurf);
193
194 static bool
195 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
196 {
197         struct desktop_shell *shell;
198         struct weston_surface *top_fs_es;
199
200         shell = shell_surface_get_shell(shsurf);
201         
202         if (wl_list_empty(&shell->fullscreen_layer.surface_list))
203                 return false;
204
205         top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
206                                  struct weston_surface, 
207                                  layer_link);
208         return (shsurf == get_shell_surface(top_fs_es));
209 }
210
211 static void
212 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
213 {
214         struct shell_grab *grab;
215
216         grab = container_of(listener, struct shell_grab,
217                             shsurf_destroy_listener);
218
219         grab->shsurf = NULL;
220 }
221
222 static void
223 shell_grab_init(struct shell_grab *grab,
224                 const struct wl_pointer_grab_interface *interface,
225                 struct shell_surface *shsurf)
226 {
227         grab->grab.interface = interface;
228         grab->shsurf = shsurf;
229         grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
230         wl_signal_add(&shsurf->resource.destroy_signal,
231                       &grab->shsurf_destroy_listener);
232
233 }
234
235 static void
236 shell_grab_finish(struct shell_grab *grab)
237 {
238         wl_list_remove(&grab->shsurf_destroy_listener.link);
239 }
240
241 static void
242 center_on_output(struct weston_surface *surface,
243                  struct weston_output *output);
244
245 static enum weston_keyboard_modifier
246 get_modifier(char *modifier)
247 {
248         if (!modifier)
249                 return MODIFIER_SUPER;
250
251         if (!strcmp("ctrl", modifier))
252                 return MODIFIER_CTRL;
253         else if (!strcmp("alt", modifier))
254                 return MODIFIER_ALT;
255         else if (!strcmp("super", modifier))
256                 return MODIFIER_SUPER;
257         else
258                 return MODIFIER_SUPER;
259 }
260
261 static enum animation_type
262 get_animation_type(char *animation)
263 {
264         if (!animation)
265                 return ANIMATION_NONE;
266
267         if (!strcmp("zoom", animation))
268                 return ANIMATION_ZOOM;
269         else if (!strcmp("fade", animation))
270                 return ANIMATION_FADE;
271         else
272                 return ANIMATION_NONE;
273 }
274
275 static void
276 shell_configuration(struct desktop_shell *shell)
277 {
278         char *config_file;
279         char *path = NULL;
280         int duration = 60;
281         char *modifier = NULL;
282         char *win_animation = NULL;
283
284         struct config_key shell_keys[] = {
285                 { "binding-modifier",   CONFIG_KEY_STRING, &modifier },
286                 { "animation",          CONFIG_KEY_STRING, &win_animation},
287         };
288
289         struct config_key saver_keys[] = {
290                 { "path",       CONFIG_KEY_STRING,  &path },
291                 { "duration",   CONFIG_KEY_INTEGER, &duration },
292         };
293
294         struct config_section cs[] = {
295                 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
296                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
297         };
298
299         config_file = config_file_path("weston.ini");
300         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
301         free(config_file);
302
303         shell->screensaver.path = path;
304         shell->screensaver.duration = duration;
305         shell->binding_modifier = get_modifier(modifier);
306         shell->win_animation_type = get_animation_type(win_animation);
307 }
308
309 static void
310 noop_grab_focus(struct wl_pointer_grab *grab,
311                 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
312 {
313         grab->focus = NULL;
314 }
315
316 static void
317 move_grab_motion(struct wl_pointer_grab *grab,
318                  uint32_t time, wl_fixed_t x, wl_fixed_t y)
319 {
320         struct weston_move_grab *move = (struct weston_move_grab *) grab;
321         struct wl_pointer *pointer = grab->pointer;
322         struct shell_surface *shsurf = move->base.shsurf;
323         struct weston_surface *es;
324         int dx = wl_fixed_to_int(pointer->x + move->dx);
325         int dy = wl_fixed_to_int(pointer->y + move->dy);
326
327         if (!shsurf)
328                 return;
329
330         es = shsurf->surface;
331
332         weston_surface_configure(es, dx, dy,
333                                  es->geometry.width, es->geometry.height);
334 }
335
336 static void
337 move_grab_button(struct wl_pointer_grab *grab,
338                  uint32_t time, uint32_t button, uint32_t state_w)
339 {
340         struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
341                                                     grab);
342         struct wl_pointer *pointer = grab->pointer;
343         enum wl_pointer_button_state state = state_w;
344
345         if (pointer->button_count == 0 &&
346             state == WL_POINTER_BUTTON_STATE_RELEASED) {
347                 shell_grab_finish(shell_grab);
348                 wl_pointer_end_grab(pointer);
349                 free(grab);
350         }
351 }
352
353 static const struct wl_pointer_grab_interface move_grab_interface = {
354         noop_grab_focus,
355         move_grab_motion,
356         move_grab_button,
357 };
358
359 static void
360 unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
361 {
362         shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
363
364         if(!shsurf->unresponsive_animation.exists) {
365                 wl_list_insert(&shsurf->surface->compositor->animation_list,
366                        &shsurf->unresponsive_animation.current.link);
367                 shsurf->unresponsive_animation.exists = 1;
368                 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
369                 weston_surface_damage(shsurf->surface);
370         }
371 }
372
373 static void
374 unresponsive_fade_frame(struct weston_animation *animation,
375                 struct weston_output *output, uint32_t msecs)
376 {
377         struct shell_surface *shsurf =
378                 container_of(animation, struct shell_surface, unresponsive_animation.current);
379         struct weston_surface *surface = shsurf->surface;
380         unsigned int step = 8;
381
382         if (!surface || !shsurf)
383                 return;
384
385         if (shsurf->unresponsive_animation.fading_in) {
386                 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
387                         if (surface->saturation > 1)
388                                 surface->saturation -= 5;
389                         if (surface->brightness > 200)
390                                 surface->brightness--;
391
392                         shsurf->unresponsive_animation.timestamp += step;
393                 }
394
395                 if (surface->saturation <= 1 && surface->brightness <= 200) {
396                         wl_list_remove(&shsurf->unresponsive_animation.current.link);
397                         shsurf->unresponsive_animation.exists = 0;
398                 }
399         }
400         else {
401                 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
402                         if (surface->saturation < 255)
403                                 surface->saturation += 5;
404                         if (surface->brightness < 255)
405                                 surface->brightness++;
406
407                         shsurf->unresponsive_animation.timestamp += step;
408                 }
409
410                 if (surface->saturation >= 255 && surface->brightness >= 255) {
411                         surface->saturation = surface->brightness = 255;
412                         wl_list_remove(&shsurf->unresponsive_animation.current.link);
413                         shsurf->unresponsive_animation.exists = 0;
414                 }
415         }
416
417         surface->geometry.dirty = 1;
418         weston_surface_damage(surface);
419 }
420
421 static void
422 ping_timer_destroy(struct shell_surface *shsurf)
423 {
424         if (!shsurf || !shsurf->ping_timer)
425                 return;
426
427         if (shsurf->ping_timer->source)
428                 wl_event_source_remove(shsurf->ping_timer->source);
429
430         free(shsurf->ping_timer);
431         shsurf->ping_timer = NULL;
432 }
433
434 static int
435 ping_timeout_handler(void *data)
436 {
437         struct shell_surface *shsurf = data;
438
439         /* Client is not responding */
440         shsurf->unresponsive = 1;
441         unresponsive_surface_fade(shsurf, false);
442
443         return 1;
444 }
445
446 static void
447 ping_handler(struct weston_surface *surface, uint32_t serial)
448 {
449         struct shell_surface *shsurf = get_shell_surface(surface);
450         struct wl_event_loop *loop;
451         int ping_timeout = 2500;
452
453         if (!shsurf)
454                 return;
455         if (!shsurf->resource.client)
456                 return;
457
458         if (!shsurf->ping_timer) {
459                 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
460                 if (!shsurf->ping_timer)
461                         return;
462
463                 shsurf->ping_timer->serial = serial;
464                 loop = wl_display_get_event_loop(surface->compositor->wl_display);
465                 shsurf->ping_timer->source =
466                         wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
467                 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
468
469                 wl_shell_surface_send_ping(&shsurf->resource, serial);
470         }
471 }
472
473 static void
474 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
475                                                         uint32_t serial)
476 {
477         struct shell_surface *shsurf = resource->data;
478
479         if (shsurf->ping_timer->serial == serial) {
480                 if (shsurf->unresponsive) {
481                         /* Received pong from previously unresponsive client */
482                         unresponsive_surface_fade(shsurf, true);
483                 }
484                 shsurf->unresponsive = 0;
485                 ping_timer_destroy(shsurf);
486         }
487 }
488
489 static void
490 shell_surface_set_title(struct wl_client *client,
491                         struct wl_resource *resource, const char *title)
492 {
493         struct shell_surface *shsurf = resource->data;
494
495         free(shsurf->title);
496         shsurf->title = strdup(title);
497 }
498
499 static void
500 shell_surface_set_class(struct wl_client *client,
501                         struct wl_resource *resource, const char *class)
502 {
503         struct shell_surface *shsurf = resource->data;
504
505         free(shsurf->class);
506         shsurf->class = strdup(class);
507 }
508
509 static int
510 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
511 {
512         struct weston_move_grab *move;
513
514         if (!shsurf)
515                 return -1;
516
517         move = malloc(sizeof *move);
518         if (!move)
519                 return -1;
520
521         shell_grab_init(&move->base, &move_grab_interface, shsurf);
522
523         move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
524                         ws->seat.pointer->grab_x;
525         move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
526                         ws->seat.pointer->grab_y;
527
528         wl_pointer_start_grab(ws->seat.pointer, &move->base.grab);
529
530         wl_pointer_set_focus(ws->seat.pointer, NULL,
531                              wl_fixed_from_int(0),
532                              wl_fixed_from_int(0));
533
534         return 0;
535 }
536
537 static void
538 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
539                    struct wl_resource *seat_resource, uint32_t serial)
540 {
541         struct weston_seat *ws = seat_resource->data;
542         struct shell_surface *shsurf = resource->data;
543
544         if (ws->seat.pointer->button_count == 0 ||
545             ws->seat.pointer->grab_serial != serial ||
546             ws->seat.pointer->focus != &shsurf->surface->surface)
547                 return;
548
549         if (surface_move(shsurf, ws) < 0)
550                 wl_resource_post_no_memory(resource);
551 }
552
553 struct weston_resize_grab {
554         struct shell_grab base;
555         uint32_t edges;
556         int32_t width, height;
557 };
558
559 static void
560 resize_grab_motion(struct wl_pointer_grab *grab,
561                    uint32_t time, wl_fixed_t x, wl_fixed_t y)
562 {
563         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
564         struct wl_pointer *pointer = grab->pointer;
565         struct shell_surface *shsurf = resize->base.shsurf;
566         int32_t width, height;
567         wl_fixed_t from_x, from_y;
568         wl_fixed_t to_x, to_y;
569
570         if (!shsurf)
571                 return;
572
573         weston_surface_from_global_fixed(shsurf->surface,
574                                          pointer->grab_x, pointer->grab_y,
575                                          &from_x, &from_y);
576         weston_surface_from_global_fixed(shsurf->surface,
577                                          pointer->x, pointer->y, &to_x, &to_y);
578
579         width = resize->width;
580         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
581                 width += wl_fixed_to_int(from_x - to_x);
582         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
583                 width += wl_fixed_to_int(to_x - from_x);
584         }
585
586         height = resize->height;
587         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
588                 height += wl_fixed_to_int(from_y - to_y);
589         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
590                 height += wl_fixed_to_int(to_y - from_y);
591         }
592
593         shsurf->client->send_configure(shsurf->surface,
594                                        resize->edges, width, height);
595 }
596
597 static void
598 send_configure(struct weston_surface *surface,
599                uint32_t edges, int32_t width, int32_t height)
600 {
601         struct shell_surface *shsurf = get_shell_surface(surface);
602
603         wl_shell_surface_send_configure(&shsurf->resource,
604                                         edges, width, height);
605 }
606
607 static const struct weston_shell_client shell_client = {
608         send_configure
609 };
610
611 static void
612 resize_grab_button(struct wl_pointer_grab *grab,
613                    uint32_t time, uint32_t button, uint32_t state_w)
614 {
615         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
616         struct wl_pointer *pointer = grab->pointer;
617         enum wl_pointer_button_state state = state_w;
618
619         if (pointer->button_count == 0 &&
620             state == WL_POINTER_BUTTON_STATE_RELEASED) {
621                 shell_grab_finish(&resize->base);
622                 wl_pointer_end_grab(pointer);
623                 free(grab);
624         }
625 }
626
627 static const struct wl_pointer_grab_interface resize_grab_interface = {
628         noop_grab_focus,
629         resize_grab_motion,
630         resize_grab_button,
631 };
632
633 static int
634 surface_resize(struct shell_surface *shsurf,
635                struct weston_seat *ws, uint32_t edges)
636 {
637         struct weston_resize_grab *resize;
638
639         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
640                 return 0;
641
642         if (edges == 0 || edges > 15 ||
643             (edges & 3) == 3 || (edges & 12) == 12)
644                 return 0;
645
646         resize = malloc(sizeof *resize);
647         if (!resize)
648                 return -1;
649
650         shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
651
652         resize->edges = edges;
653         resize->width = shsurf->surface->geometry.width;
654         resize->height = shsurf->surface->geometry.height;
655
656         wl_pointer_start_grab(ws->seat.pointer, &resize->base.grab);
657
658         wl_pointer_set_focus(ws->seat.pointer, NULL,
659                              wl_fixed_from_int(0),
660                              wl_fixed_from_int(0));
661
662         return 0;
663 }
664
665 static void
666 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
667                      struct wl_resource *seat_resource, uint32_t serial,
668                      uint32_t edges)
669 {
670         struct weston_seat *ws = seat_resource->data;
671         struct shell_surface *shsurf = resource->data;
672
673         if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
674                 return;
675
676         if (ws->seat.pointer->button_count == 0 ||
677             ws->seat.pointer->grab_serial != serial ||
678             ws->seat.pointer->focus != &shsurf->surface->surface)
679                 return;
680
681         if (surface_resize(shsurf, ws, edges) < 0)
682                 wl_resource_post_no_memory(resource);
683 }
684
685 static struct weston_output *
686 get_default_output(struct weston_compositor *compositor)
687 {
688         return container_of(compositor->output_list.next,
689                             struct weston_output, link);
690 }
691
692 static void
693 shell_unset_fullscreen(struct shell_surface *shsurf)
694 {
695         /* undo all fullscreen things here */
696         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
697             shell_surface_is_top_fullscreen(shsurf)) {
698                 weston_output_switch_mode(shsurf->fullscreen_output,
699                                           shsurf->fullscreen_output->origin);
700         }
701         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
702         shsurf->fullscreen.framerate = 0;
703         wl_list_remove(&shsurf->fullscreen.transform.link);
704         wl_list_init(&shsurf->fullscreen.transform.link);
705         if (shsurf->fullscreen.black_surface)
706                 weston_surface_destroy(shsurf->fullscreen.black_surface);
707         shsurf->fullscreen.black_surface = NULL;
708         shsurf->fullscreen_output = NULL;
709         weston_surface_set_position(shsurf->surface,
710                                     shsurf->saved_x, shsurf->saved_y);
711         if (shsurf->saved_rotation_valid) {
712                 wl_list_insert(&shsurf->surface->geometry.transformation_list,
713                                &shsurf->rotation.transform.link);
714                 shsurf->saved_rotation_valid = false;
715         }
716 }
717
718 static int
719 reset_shell_surface_type(struct shell_surface *surface)
720 {
721         switch (surface->type) {
722         case SHELL_SURFACE_FULLSCREEN:
723                 shell_unset_fullscreen(surface);
724                 break;
725         case SHELL_SURFACE_MAXIMIZED:
726                 surface->output = get_default_output(surface->surface->compositor);
727                 weston_surface_set_position(surface->surface,
728                                             surface->saved_x,
729                                             surface->saved_y);
730                 break;
731         case SHELL_SURFACE_PANEL:
732         case SHELL_SURFACE_BACKGROUND:
733                 wl_list_remove(&surface->link);
734                 wl_list_init(&surface->link);
735                 break;
736         case SHELL_SURFACE_SCREENSAVER:
737         case SHELL_SURFACE_LOCK:
738                 wl_resource_post_error(&surface->resource,
739                                        WL_DISPLAY_ERROR_INVALID_METHOD,
740                                        "cannot reassign surface type");
741                 return -1;
742         case SHELL_SURFACE_NONE:
743         case SHELL_SURFACE_TOPLEVEL:
744         case SHELL_SURFACE_TRANSIENT:
745         case SHELL_SURFACE_POPUP:
746                 break;
747         }
748
749         surface->type = SHELL_SURFACE_NONE;
750         return 0;
751 }
752
753 static void
754 set_surface_type(struct shell_surface *shsurf)
755 {
756         struct weston_surface *surface = shsurf->surface;
757         struct shell_surface *pshsurf = shsurf->parent;
758         struct weston_surface *pes;
759         struct shell_surface *priv;
760         struct desktop_shell *shell = shsurf->shell;
761
762         reset_shell_surface_type(shsurf);
763
764         shsurf->type = shsurf->next_type;
765         shsurf->next_type = SHELL_SURFACE_NONE;
766
767         switch (shsurf->type) {
768         case SHELL_SURFACE_TOPLEVEL:
769                 break;
770         case SHELL_SURFACE_TRANSIENT:
771                 pes = pshsurf->surface;
772                 weston_surface_set_position(surface,
773                                 pes->geometry.x + shsurf->transient.x,
774                                 pes->geometry.y + shsurf->transient.y);
775                 break;
776
777         case SHELL_SURFACE_MAXIMIZED:
778                 shsurf->saved_x = surface->geometry.x;
779                 shsurf->saved_y = surface->geometry.y;
780                 shsurf->saved_position_valid = true;
781                 break;
782
783         case SHELL_SURFACE_FULLSCREEN:
784                 shsurf->saved_x = surface->geometry.x;
785                 shsurf->saved_y = surface->geometry.y;
786                 shsurf->saved_position_valid = true;
787
788                 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
789                         wl_list_remove(&shsurf->rotation.transform.link);
790                         wl_list_init(&shsurf->rotation.transform.link);
791                         shsurf->surface->geometry.dirty = 1;
792                         shsurf->saved_rotation_valid = true;
793                 }
794                 break;
795
796         case SHELL_SURFACE_BACKGROUND:
797                 wl_list_for_each(priv, &shell->backgrounds, link) {
798                         if (priv->output == shsurf->output) {
799                                 priv->surface->output = NULL;
800                                 wl_list_remove(&priv->surface->layer_link);
801                                 wl_list_remove(&priv->link);
802                                 break;
803                         }
804                 }
805
806                 wl_list_insert(&shell->backgrounds, &shsurf->link);
807
808                 weston_surface_set_position(surface, shsurf->output->x,
809                                             shsurf->output->y);
810                 break;
811
812         case SHELL_SURFACE_PANEL:
813                 wl_list_for_each(priv, &shell->panels, link) {
814                         if (priv->output == shsurf->output) {
815                                 priv->surface->output = NULL;
816                                 wl_list_remove(&priv->surface->layer_link);
817                                 wl_list_remove(&priv->link);
818                                 break;
819                         }
820                 }
821
822                 wl_list_insert(&shell->panels, &shsurf->link);
823
824                 weston_surface_set_position(surface, shsurf->output->x,
825                                             shsurf->output->y);
826                 break;
827
828         default:
829                 break;
830         }
831 }
832
833 static void
834 set_toplevel(struct shell_surface *shsurf)
835 {
836        shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
837 }
838
839 static void
840 shell_surface_set_toplevel(struct wl_client *client,
841                            struct wl_resource *resource)
842 {
843         struct shell_surface *surface = resource->data;
844
845         set_toplevel(surface);
846 }
847
848 static void
849 set_transient(struct shell_surface *shsurf,
850               struct shell_surface *pshsurf, int x, int y, uint32_t flags)
851 {
852         /* assign to parents output */
853         shsurf->parent = pshsurf;
854         shsurf->transient.x = x;
855         shsurf->transient.y = y;
856         shsurf->transient.flags = flags;
857         shsurf->next_type = SHELL_SURFACE_TRANSIENT;
858 }
859
860 static void
861 shell_surface_set_transient(struct wl_client *client,
862                             struct wl_resource *resource,
863                             struct wl_resource *parent_resource,
864                             int x, int y, uint32_t flags)
865 {
866         struct shell_surface *shsurf = resource->data;
867         struct shell_surface *pshsurf = parent_resource->data;
868
869         set_transient(shsurf, pshsurf, x, y, flags);
870 }
871
872 static struct desktop_shell *
873 shell_surface_get_shell(struct shell_surface *shsurf)
874 {
875         return shsurf->shell;
876 }
877
878 static int
879 get_output_panel_height(struct desktop_shell *shell,
880                         struct weston_output *output)
881 {
882         struct shell_surface *priv;
883         int panel_height = 0;
884
885         if (!output)
886                 return 0;
887
888         wl_list_for_each(priv, &shell->panels, link) {
889                 if (priv->output == output) {
890                         panel_height = priv->surface->geometry.height;
891                         break;
892                 }
893         }
894         return panel_height;
895 }
896
897 static void
898 shell_surface_set_maximized(struct wl_client *client,
899                             struct wl_resource *resource,
900                             struct wl_resource *output_resource )
901 {
902         struct shell_surface *shsurf = resource->data;
903         struct weston_surface *es = shsurf->surface;
904         struct desktop_shell *shell = NULL;
905         uint32_t edges = 0, panel_height = 0;
906
907         /* get the default output, if the client set it as NULL
908            check whether the ouput is available */
909         if (output_resource)
910                 shsurf->output = output_resource->data;
911         else
912                 shsurf->output = get_default_output(es->compositor);
913
914         shell = shell_surface_get_shell(shsurf);
915         panel_height = get_output_panel_height(shell, es->output);
916         edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
917
918         shsurf->client->send_configure(shsurf->surface, edges,
919                                        es->output->current->width,
920                                        es->output->current->height - panel_height);
921
922         shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
923 }
924
925 static void
926 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
927
928 static struct weston_surface *
929 create_black_surface(struct weston_compositor *ec,
930                      struct weston_surface *fs_surface,
931                      GLfloat x, GLfloat y, int w, int h)
932 {
933         struct weston_surface *surface = NULL;
934
935         surface = weston_surface_create(ec);
936         if (surface == NULL) {
937                 fprintf(stderr, "no memory\n");
938                 return NULL;
939         }
940
941         surface->configure = black_surface_configure;
942         surface->private = fs_surface;
943         weston_surface_configure(surface, x, y, w, h);
944         weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
945         return surface;
946 }
947
948 /* Create black surface and append it to the associated fullscreen surface.
949  * Handle size dismatch and positioning according to the method. */
950 static void
951 shell_configure_fullscreen(struct shell_surface *shsurf)
952 {
953         struct weston_output *output = shsurf->fullscreen_output;
954         struct weston_surface *surface = shsurf->surface;
955         struct weston_matrix *matrix;
956         float scale;
957
958         center_on_output(surface, output);
959
960         if (!shsurf->fullscreen.black_surface)
961                 shsurf->fullscreen.black_surface =
962                         create_black_surface(surface->compositor,
963                                              surface,
964                                              output->x, output->y,
965                                              output->current->width,
966                                              output->current->height);
967
968         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
969         wl_list_insert(&surface->layer_link,
970                        &shsurf->fullscreen.black_surface->layer_link);
971         shsurf->fullscreen.black_surface->output = output;
972
973         switch (shsurf->fullscreen.type) {
974         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
975                 break;
976         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
977                 matrix = &shsurf->fullscreen.transform.matrix;
978                 weston_matrix_init(matrix);
979                 scale = (float)output->current->width/(float)surface->geometry.width;
980                 weston_matrix_scale(matrix, scale, scale, 1);
981                 wl_list_remove(&shsurf->fullscreen.transform.link);
982                 wl_list_insert(surface->geometry.transformation_list.prev,
983                                &shsurf->fullscreen.transform.link);
984                 weston_surface_set_position(surface, output->x, output->y);
985                 break;
986         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
987                 if (shell_surface_is_top_fullscreen(shsurf)) {
988                         struct weston_mode mode = {0, 
989                                 surface->geometry.width,
990                                 surface->geometry.height,
991                                 shsurf->fullscreen.framerate};
992
993                         if (weston_output_switch_mode(output, &mode) == 0) {
994                                 weston_surface_configure(shsurf->fullscreen.black_surface, 
995                                                          output->x, output->y,
996                                                          output->current->width,
997                                                          output->current->height);
998                                 weston_surface_set_position(surface, output->x, output->y);
999                                 break;
1000                         }
1001                 }
1002                 break;
1003         case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1004                 break;
1005         default:
1006                 break;
1007         }
1008 }
1009
1010 /* make the fullscreen and black surface at the top */
1011 static void
1012 shell_stack_fullscreen(struct shell_surface *shsurf)
1013 {
1014         struct weston_output *output = shsurf->fullscreen_output;
1015         struct weston_surface *surface = shsurf->surface;
1016         struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1017
1018         wl_list_remove(&surface->layer_link);
1019         wl_list_insert(&shell->fullscreen_layer.surface_list,
1020                        &surface->layer_link);
1021         weston_surface_damage(surface);
1022
1023         if (!shsurf->fullscreen.black_surface)
1024                 shsurf->fullscreen.black_surface =
1025                         create_black_surface(surface->compositor,
1026                                              surface,
1027                                              output->x, output->y,
1028                                              output->current->width,
1029                                              output->current->height);
1030
1031         wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1032         wl_list_insert(&surface->layer_link,
1033                        &shsurf->fullscreen.black_surface->layer_link);
1034         weston_surface_damage(shsurf->fullscreen.black_surface);
1035 }
1036
1037 static void
1038 shell_map_fullscreen(struct shell_surface *shsurf)
1039 {
1040         shell_stack_fullscreen(shsurf);
1041         shell_configure_fullscreen(shsurf);
1042 }
1043
1044 static void
1045 shell_surface_set_fullscreen(struct wl_client *client,
1046                              struct wl_resource *resource,
1047                              uint32_t method,
1048                              uint32_t framerate,
1049                              struct wl_resource *output_resource)
1050 {
1051         struct shell_surface *shsurf = resource->data;
1052         struct weston_surface *es = shsurf->surface;
1053
1054         if (output_resource)
1055                 shsurf->output = output_resource->data;
1056         else
1057                 shsurf->output = get_default_output(es->compositor);
1058
1059         shsurf->fullscreen_output = shsurf->output;
1060         shsurf->fullscreen.type = method;
1061         shsurf->fullscreen.framerate = framerate;
1062         shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1063
1064         shsurf->client->send_configure(shsurf->surface, 0,
1065                                        shsurf->output->current->width,
1066                                        shsurf->output->current->height);
1067 }
1068
1069 static void
1070 popup_grab_focus(struct wl_pointer_grab *grab,
1071                  struct wl_surface *surface,
1072                  wl_fixed_t x,
1073                  wl_fixed_t y)
1074 {
1075         struct wl_pointer *pointer = grab->pointer;
1076         struct shell_surface *priv =
1077                 container_of(grab, struct shell_surface, popup.grab);
1078         struct wl_client *client = priv->surface->surface.resource.client;
1079
1080         if (surface && surface->resource.client == client) {
1081                 wl_pointer_set_focus(pointer, surface, x, y);
1082                 grab->focus = surface;
1083         } else {
1084                 wl_pointer_set_focus(pointer, NULL,
1085                                      wl_fixed_from_int(0),
1086                                      wl_fixed_from_int(0));
1087                 grab->focus = NULL;
1088         }
1089 }
1090
1091 static void
1092 popup_grab_motion(struct wl_pointer_grab *grab,
1093                   uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1094 {
1095         struct wl_resource *resource;
1096
1097         resource = grab->pointer->focus_resource;
1098         if (resource)
1099                 wl_pointer_send_motion(resource, time, sx, sy);
1100 }
1101
1102 static void
1103 popup_grab_button(struct wl_pointer_grab *grab,
1104                   uint32_t time, uint32_t button, uint32_t state_w)
1105 {
1106         struct wl_resource *resource;
1107         struct shell_surface *shsurf =
1108                 container_of(grab, struct shell_surface, popup.grab);
1109         struct wl_display *display;
1110         enum wl_pointer_button_state state = state_w;
1111         uint32_t serial;
1112
1113         resource = grab->pointer->focus_resource;
1114         if (resource) {
1115                 display = wl_client_get_display(resource->client);
1116                 serial = wl_display_get_serial(display);
1117                 wl_pointer_send_button(resource, serial, time, button, state);
1118         } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1119                    (shsurf->popup.initial_up ||
1120                     time - shsurf->popup.seat->pointer->grab_time > 500)) {
1121                 wl_shell_surface_send_popup_done(&shsurf->resource);
1122                 wl_pointer_end_grab(grab->pointer);
1123                 shsurf->popup.grab.pointer = NULL;
1124         }
1125
1126         if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1127                 shsurf->popup.initial_up = 1;
1128 }
1129
1130 static const struct wl_pointer_grab_interface popup_grab_interface = {
1131         popup_grab_focus,
1132         popup_grab_motion,
1133         popup_grab_button,
1134 };
1135
1136 static void
1137 shell_map_popup(struct shell_surface *shsurf)
1138 {
1139         struct wl_seat *seat = shsurf->popup.seat;
1140         struct weston_surface *es = shsurf->surface;
1141         struct weston_surface *parent = shsurf->parent->surface;
1142
1143         es->output = parent->output;
1144         shsurf->popup.grab.interface = &popup_grab_interface;
1145
1146         weston_surface_update_transform(parent);
1147         if (parent->transform.enabled) {
1148                 shsurf->popup.parent_transform.matrix =
1149                         parent->transform.matrix;
1150         } else {
1151                 /* construct x, y translation matrix */
1152                 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1153                 shsurf->popup.parent_transform.matrix.d[12] =
1154                         parent->geometry.x;
1155                 shsurf->popup.parent_transform.matrix.d[13] =
1156                         parent->geometry.y;
1157         }
1158         wl_list_insert(es->geometry.transformation_list.prev,
1159                        &shsurf->popup.parent_transform.link);
1160         weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1161
1162         shsurf->popup.initial_up = 0;
1163
1164         /* We don't require the grab to still be active, but if another
1165          * grab has started in the meantime, we end the popup now. */
1166         if (seat->pointer->grab_serial == shsurf->popup.serial) {
1167                 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1168         } else {
1169                 wl_shell_surface_send_popup_done(&shsurf->resource);
1170         }
1171 }
1172
1173 static void
1174 shell_surface_set_popup(struct wl_client *client,
1175                         struct wl_resource *resource,
1176                         struct wl_resource *seat_resource,
1177                         uint32_t serial,
1178                         struct wl_resource *parent_resource,
1179                         int32_t x, int32_t y, uint32_t flags)
1180 {
1181         struct shell_surface *shsurf = resource->data;
1182
1183         shsurf->type = SHELL_SURFACE_POPUP;
1184         shsurf->parent = parent_resource->data;
1185         shsurf->popup.seat = seat_resource->data;
1186         shsurf->popup.serial = serial;
1187         shsurf->popup.x = x;
1188         shsurf->popup.y = y;
1189 }
1190
1191 static const struct wl_shell_surface_interface shell_surface_implementation = {
1192         shell_surface_pong,
1193         shell_surface_move,
1194         shell_surface_resize,
1195         shell_surface_set_toplevel,
1196         shell_surface_set_transient,
1197         shell_surface_set_fullscreen,
1198         shell_surface_set_popup,
1199         shell_surface_set_maximized,
1200         shell_surface_set_title,
1201         shell_surface_set_class
1202 };
1203
1204 static void
1205 destroy_shell_surface(struct shell_surface *shsurf)
1206 {
1207         if (shsurf->popup.grab.pointer)
1208                 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1209
1210         if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1211             shell_surface_is_top_fullscreen(shsurf)) {
1212                 weston_output_switch_mode(shsurf->fullscreen_output,
1213                                           shsurf->fullscreen_output->origin);
1214         }
1215
1216         if (shsurf->fullscreen.black_surface)
1217                 weston_surface_destroy(shsurf->fullscreen.black_surface);
1218
1219         /* As destroy_resource() use wl_list_for_each_safe(),
1220          * we can always remove the listener.
1221          */
1222         wl_list_remove(&shsurf->surface_destroy_listener.link);
1223         shsurf->surface->configure = NULL;
1224         ping_timer_destroy(shsurf);
1225
1226         if (shsurf->unresponsive_animation.exists)
1227                 wl_list_remove(&shsurf->unresponsive_animation.current.link);
1228
1229         wl_list_remove(&shsurf->link);
1230         free(shsurf);
1231 }
1232
1233 static void
1234 shell_destroy_shell_surface(struct wl_resource *resource)
1235 {
1236         struct shell_surface *shsurf = resource->data;
1237
1238         destroy_shell_surface(shsurf);
1239 }
1240
1241 static void
1242 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1243 {
1244         struct shell_surface *shsurf = container_of(listener,
1245                                                     struct shell_surface,
1246                                                     surface_destroy_listener);
1247
1248         /* tricky way to check if resource was in fact created */
1249         if (shsurf->resource.object.implementation != 0)
1250                 wl_resource_destroy(&shsurf->resource);
1251         else
1252                 destroy_shell_surface(shsurf);
1253 }
1254
1255 static struct shell_surface *
1256 get_shell_surface(struct weston_surface *surface)
1257 {
1258         struct wl_listener *listener;
1259
1260         listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1261                                  shell_handle_surface_destroy);
1262         if (listener)
1263                 return container_of(listener, struct shell_surface,
1264                                     surface_destroy_listener);
1265
1266         return NULL;
1267 }
1268
1269 static void
1270 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1271
1272 static  struct shell_surface *
1273 create_shell_surface(void *shell, struct weston_surface *surface,
1274                      const struct weston_shell_client *client)
1275 {
1276         struct shell_surface *shsurf;
1277
1278         if (surface->configure) {
1279                 fprintf(stderr, "surface->configure already set\n");
1280                 return NULL;
1281         }
1282
1283         shsurf = calloc(1, sizeof *shsurf);
1284         if (!shsurf) {
1285                 fprintf(stderr, "no memory to allocate shell surface\n");
1286                 return NULL;
1287         }
1288
1289         surface->configure = shell_surface_configure;
1290         surface->compositor->shell_interface.shell = shell;
1291
1292         shsurf->shell = (struct desktop_shell *) shell;
1293         shsurf->unresponsive = 0;
1294         shsurf->unresponsive_animation.exists = 0;
1295         shsurf->unresponsive_animation.fading_in = 0;
1296         shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
1297         shsurf->saved_position_valid = false;
1298         shsurf->saved_rotation_valid = false;
1299         shsurf->surface = surface;
1300         shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1301         shsurf->fullscreen.framerate = 0;
1302         shsurf->fullscreen.black_surface = NULL;
1303         shsurf->ping_timer = NULL;
1304         wl_list_init(&shsurf->fullscreen.transform.link);
1305
1306         wl_signal_init(&shsurf->resource.destroy_signal);
1307         shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1308         wl_signal_add(&surface->surface.resource.destroy_signal,
1309                       &shsurf->surface_destroy_listener);
1310
1311         /* init link so its safe to always remove it in destroy_shell_surface */
1312         wl_list_init(&shsurf->link);
1313
1314         /* empty when not in use */
1315         wl_list_init(&shsurf->rotation.transform.link);
1316         weston_matrix_init(&shsurf->rotation.rotation);
1317
1318         shsurf->type = SHELL_SURFACE_NONE;
1319         shsurf->next_type = SHELL_SURFACE_NONE;
1320
1321         shsurf->client = client;
1322
1323         return shsurf;
1324 }
1325
1326 static void
1327 shell_get_shell_surface(struct wl_client *client,
1328                         struct wl_resource *resource,
1329                         uint32_t id,
1330                         struct wl_resource *surface_resource)
1331 {
1332         struct weston_surface *surface = surface_resource->data;
1333         struct desktop_shell *shell = resource->data;
1334         struct shell_surface *shsurf;
1335
1336         if (get_shell_surface(surface)) {
1337                 wl_resource_post_error(surface_resource,
1338                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1339                                        "desktop_shell::get_shell_surface already requested");
1340                 return;
1341         }
1342
1343         shsurf = create_shell_surface(shell, surface, &shell_client);
1344         if (!shsurf) {
1345                 wl_resource_post_error(surface_resource,
1346                                        WL_DISPLAY_ERROR_INVALID_OBJECT,
1347                                        "surface->configure already set");
1348                 return;
1349         }
1350
1351         shsurf->resource.destroy = shell_destroy_shell_surface;
1352         shsurf->resource.object.id = id;
1353         shsurf->resource.object.interface = &wl_shell_surface_interface;
1354         shsurf->resource.object.implementation =
1355                 (void (**)(void)) &shell_surface_implementation;
1356         shsurf->resource.data = shsurf;
1357
1358         wl_client_add_resource(client, &shsurf->resource);
1359 }
1360
1361 static const struct wl_shell_interface shell_implementation = {
1362         shell_get_shell_surface
1363 };
1364
1365 static void
1366 handle_screensaver_sigchld(struct weston_process *proc, int status)
1367 {
1368         proc->pid = 0;
1369 }
1370
1371 static void
1372 launch_screensaver(struct desktop_shell *shell)
1373 {
1374         if (shell->screensaver.binding)
1375                 return;
1376
1377         if (!shell->screensaver.path)
1378                 return;
1379
1380         if (shell->screensaver.process.pid != 0) {
1381                 fprintf(stderr, "old screensaver still running\n");
1382                 return;
1383         }
1384
1385         weston_client_launch(shell->compositor,
1386                            &shell->screensaver.process,
1387                            shell->screensaver.path,
1388                            handle_screensaver_sigchld);
1389 }
1390
1391 static void
1392 terminate_screensaver(struct desktop_shell *shell)
1393 {
1394         if (shell->screensaver.process.pid == 0)
1395                 return;
1396
1397         kill(shell->screensaver.process.pid, SIGTERM);
1398 }
1399
1400 static void
1401 show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
1402 {
1403         struct wl_list *list;
1404
1405         if (shell->lock_surface)
1406                 list = &shell->lock_surface->surface->layer_link;
1407         else
1408                 list = &shell->lock_layer.surface_list;
1409
1410         wl_list_remove(&surface->surface->layer_link);
1411         wl_list_insert(list, &surface->surface->layer_link);
1412         surface->surface->output = surface->output;
1413         weston_surface_damage(surface->surface);
1414 }
1415
1416 static void
1417 hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
1418 {
1419         wl_list_remove(&surface->surface->layer_link);
1420         wl_list_init(&surface->surface->layer_link);
1421         surface->surface->output = NULL;
1422 }
1423
1424 static void
1425 desktop_shell_set_background(struct wl_client *client,
1426                              struct wl_resource *resource,
1427                              struct wl_resource *output_resource,
1428                              struct wl_resource *surface_resource)
1429 {
1430         struct shell_surface *shsurf = surface_resource->data;
1431
1432         shsurf->next_type = SHELL_SURFACE_BACKGROUND;
1433         shsurf->output = output_resource->data;
1434
1435         desktop_shell_send_configure(resource, 0,
1436                                      surface_resource,
1437                                      shsurf->output->current->width,
1438                                      shsurf->output->current->height);
1439 }
1440
1441 static void
1442 desktop_shell_set_panel(struct wl_client *client,
1443                         struct wl_resource *resource,
1444                         struct wl_resource *output_resource,
1445                         struct wl_resource *surface_resource)
1446 {
1447         struct shell_surface *shsurf = surface_resource->data;
1448
1449         shsurf->next_type = SHELL_SURFACE_PANEL;
1450         shsurf->output = output_resource->data;
1451
1452         desktop_shell_send_configure(resource, 0,
1453                                      surface_resource,
1454                                      shsurf->output->current->width,
1455                                      shsurf->output->current->height);
1456 }
1457
1458 static void
1459 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
1460 {
1461         struct desktop_shell *shell =
1462             container_of(listener, struct desktop_shell, lock_surface_listener);
1463
1464         fprintf(stderr, "lock surface gone\n");
1465         shell->lock_surface = NULL;
1466 }
1467
1468 static void
1469 desktop_shell_set_lock_surface(struct wl_client *client,
1470                                struct wl_resource *resource,
1471                                struct wl_resource *surface_resource)
1472 {
1473         struct desktop_shell *shell = resource->data;
1474         struct shell_surface *surface = surface_resource->data;
1475
1476         shell->prepare_event_sent = false;
1477
1478         if (!shell->locked)
1479                 return;
1480
1481         shell->lock_surface = surface;
1482
1483         shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1484         wl_signal_add(&surface_resource->destroy_signal,
1485                       &shell->lock_surface_listener);
1486
1487         shell->lock_surface->next_type = SHELL_SURFACE_LOCK;
1488 }
1489
1490 static void
1491 resume_desktop(struct desktop_shell *shell)
1492 {
1493         struct shell_surface *tmp;
1494
1495         wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1496                 hide_screensaver(shell, tmp);
1497
1498         terminate_screensaver(shell);
1499
1500         wl_list_remove(&shell->lock_layer.link);
1501         wl_list_insert(&shell->compositor->cursor_layer.link,
1502                        &shell->fullscreen_layer.link);
1503         wl_list_insert(&shell->fullscreen_layer.link,
1504                        &shell->panel_layer.link);
1505         wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1506
1507         shell->locked = false;
1508         shell->compositor->idle_time = shell->compositor->option_idle_time;
1509         weston_compositor_wake(shell->compositor);
1510         weston_compositor_damage_all(shell->compositor);
1511 }
1512
1513 static void
1514 desktop_shell_unlock(struct wl_client *client,
1515                      struct wl_resource *resource)
1516 {
1517         struct desktop_shell *shell = resource->data;
1518
1519         shell->prepare_event_sent = false;
1520
1521         if (shell->locked)
1522                 resume_desktop(shell);
1523 }
1524
1525 static const struct desktop_shell_interface desktop_shell_implementation = {
1526         desktop_shell_set_background,
1527         desktop_shell_set_panel,
1528         desktop_shell_set_lock_surface,
1529         desktop_shell_unlock
1530 };
1531
1532 static enum shell_surface_type
1533 get_shell_surface_type(struct weston_surface *surface)
1534 {
1535         struct shell_surface *shsurf;
1536
1537         shsurf = get_shell_surface(surface);
1538         if (!shsurf)
1539                 return SHELL_SURFACE_NONE;
1540         return shsurf->type;
1541 }
1542
1543 static void
1544 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
1545 {
1546         struct weston_surface *surface =
1547                 (struct weston_surface *) seat->pointer->focus;
1548         struct shell_surface *shsurf;
1549
1550         if (surface == NULL)
1551                 return;
1552
1553         shsurf = get_shell_surface(surface);
1554         if (shsurf == NULL)
1555                 return;
1556
1557         switch (shsurf->type) {
1558                 case SHELL_SURFACE_PANEL:
1559                 case SHELL_SURFACE_BACKGROUND:
1560                 case SHELL_SURFACE_FULLSCREEN:
1561                 case SHELL_SURFACE_SCREENSAVER:
1562                         return;
1563                 default:
1564                         break;
1565         }
1566
1567         surface_move(shsurf, (struct weston_seat *) seat);
1568 }
1569
1570 static void
1571 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
1572 {
1573         struct weston_surface *surface =
1574                 (struct weston_surface *) seat->pointer->focus;
1575         uint32_t edges = 0;
1576         int32_t x, y;
1577         struct shell_surface *shsurf;
1578
1579         if (surface == NULL)
1580                 return;
1581
1582         shsurf = get_shell_surface(surface);
1583         if (!shsurf)
1584                 return;
1585
1586         switch (shsurf->type) {
1587                 case SHELL_SURFACE_PANEL:
1588                 case SHELL_SURFACE_BACKGROUND:
1589                 case SHELL_SURFACE_FULLSCREEN:
1590                 case SHELL_SURFACE_SCREENSAVER:
1591                         return;
1592                 default:
1593                         break;
1594         }
1595
1596         weston_surface_from_global(surface,
1597                                    wl_fixed_to_int(seat->pointer->grab_x),
1598                                    wl_fixed_to_int(seat->pointer->grab_y),
1599                                    &x, &y);
1600
1601         if (x < surface->geometry.width / 3)
1602                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1603         else if (x < 2 * surface->geometry.width / 3)
1604                 edges |= 0;
1605         else
1606                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1607
1608         if (y < surface->geometry.height / 3)
1609                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1610         else if (y < 2 * surface->geometry.height / 3)
1611                 edges |= 0;
1612         else
1613                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1614
1615         surface_resize(shsurf, (struct weston_seat *) seat, edges);
1616 }
1617
1618 static void
1619 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
1620                         int32_t value, void *data)
1621 {
1622         float step = 0.05;
1623         struct shell_surface *shsurf;
1624         struct weston_surface *surface =
1625                 (struct weston_surface *) seat->pointer->focus;
1626
1627         if (surface == NULL)
1628                 return;
1629
1630         shsurf = get_shell_surface(surface);
1631         if (!shsurf)
1632                 return;
1633
1634         switch (shsurf->type) {
1635                 case SHELL_SURFACE_BACKGROUND:
1636                 case SHELL_SURFACE_SCREENSAVER:
1637                         return;
1638                 default:
1639                         break;
1640         }
1641
1642         surface->alpha += value * step;
1643
1644         if (surface->alpha > 1.0)
1645                 surface->alpha = 1.0;
1646         if (surface->alpha < step)
1647                 surface->alpha = step;
1648
1649         surface->geometry.dirty = 1;
1650         weston_surface_damage(surface);
1651 }
1652
1653 static void
1654 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
1655         int32_t value)
1656 {
1657         struct weston_seat *ws = (struct weston_seat *) seat;
1658         struct weston_compositor *compositor = ws->compositor;
1659         struct weston_output *output;
1660         float maximum_level, increment;
1661
1662         wl_list_for_each(output, &compositor->output_list, link) {
1663                 if (pixman_region32_contains_point(&output->region,
1664                                                    wl_fixed_to_double(seat->pointer->x),
1665                                                    wl_fixed_to_double(seat->pointer->y),
1666                                                    NULL)) {
1667                         if (key == KEY_PAGEUP)
1668                                 increment = output->zoom.increment;
1669                         else if (key == KEY_PAGEDOWN)
1670                                 increment = -output->zoom.increment;
1671                         else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
1672                                 increment = output->zoom.increment * value;
1673                         else
1674                                 increment = 0;
1675
1676                         output->zoom.active = 1;
1677                         output->zoom.level += increment;
1678
1679                         if (output->zoom.level <= 0.0) {
1680                                 output->zoom.active = 0;
1681                                 output->zoom.level = 0.0;
1682                         }
1683
1684                         maximum_level = 1 - output->zoom.increment;
1685
1686                         if (output->zoom.level > maximum_level)
1687                                 output->zoom.level = maximum_level;
1688
1689                         weston_output_update_zoom(output,
1690                                                   seat->pointer->x,
1691                                                   seat->pointer->y,
1692                                                   ZOOM_POINTER);
1693                 }
1694         }
1695 }
1696
1697 static void
1698 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
1699                   int32_t value, void *data)
1700 {
1701         do_zoom(seat, time, 0, axis, value);
1702 }
1703
1704 static void
1705 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1706                  void *data)
1707 {
1708         do_zoom(seat, time, key, 0, 0);
1709 }
1710
1711 static void
1712 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
1713                   void *data)
1714 {
1715         struct weston_compositor *compositor = data;
1716
1717         wl_display_terminate(compositor->wl_display);
1718 }
1719
1720 static void
1721 rotate_grab_motion(struct wl_pointer_grab *grab,
1722                    uint32_t time, wl_fixed_t x, wl_fixed_t y)
1723 {
1724         struct rotate_grab *rotate =
1725                 container_of(grab, struct rotate_grab, base.grab);
1726         struct wl_pointer *pointer = grab->pointer;
1727         struct shell_surface *shsurf = rotate->base.shsurf;
1728         struct weston_surface *surface;
1729         GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1730
1731         if (!shsurf)
1732                 return;
1733
1734         surface = shsurf->surface;
1735
1736         cx = 0.5f * surface->geometry.width;
1737         cy = 0.5f * surface->geometry.height;
1738
1739         dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
1740         dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
1741         r = sqrtf(dx * dx + dy * dy);
1742
1743         wl_list_remove(&shsurf->rotation.transform.link);
1744         shsurf->surface->geometry.dirty = 1;
1745
1746         if (r > 20.0f) {
1747                 struct weston_matrix *matrix =
1748                         &shsurf->rotation.transform.matrix;
1749
1750                 weston_matrix_init(&rotate->rotation);
1751                 rotate->rotation.d[0] = dx / r;
1752                 rotate->rotation.d[4] = -dy / r;
1753                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1754                 rotate->rotation.d[5] = rotate->rotation.d[0];
1755
1756                 weston_matrix_init(matrix);
1757                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1758                 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
1759                 weston_matrix_multiply(matrix, &rotate->rotation);
1760                 weston_matrix_translate(matrix, cx, cy, 0.0f);
1761
1762                 wl_list_insert(
1763                         &shsurf->surface->geometry.transformation_list,
1764                         &shsurf->rotation.transform.link);
1765         } else {
1766                 wl_list_init(&shsurf->rotation.transform.link);
1767                 weston_matrix_init(&shsurf->rotation.rotation);
1768                 weston_matrix_init(&rotate->rotation);
1769         }
1770
1771         /* We need to adjust the position of the surface
1772          * in case it was resized in a rotated state before */
1773         cposx = surface->geometry.x + cx;
1774         cposy = surface->geometry.y + cy;
1775         dposx = rotate->center.x - cposx;
1776         dposy = rotate->center.y - cposy;
1777         if (dposx != 0.0f || dposy != 0.0f) {
1778                 weston_surface_set_position(surface,
1779                                             surface->geometry.x + dposx,
1780                                             surface->geometry.y + dposy);
1781         }
1782
1783         /* Repaint implies weston_surface_update_transform(), which
1784          * lazily applies the damage due to rotation update.
1785          */
1786         weston_compositor_schedule_repaint(shsurf->surface->compositor);
1787 }
1788
1789 static void
1790 rotate_grab_button(struct wl_pointer_grab *grab,
1791                  uint32_t time, uint32_t button, uint32_t state_w)
1792 {
1793         struct rotate_grab *rotate =
1794                 container_of(grab, struct rotate_grab, base.grab);
1795         struct wl_pointer *pointer = grab->pointer;
1796         struct shell_surface *shsurf = rotate->base.shsurf;
1797         enum wl_pointer_button_state state = state_w;
1798
1799         if (pointer->button_count == 0 &&
1800             state == WL_POINTER_BUTTON_STATE_RELEASED) {
1801                 if (shsurf)
1802                         weston_matrix_multiply(&shsurf->rotation.rotation,
1803                                                &rotate->rotation);
1804                 shell_grab_finish(&rotate->base);
1805                 wl_pointer_end_grab(pointer);
1806                 free(rotate);
1807         }
1808 }
1809
1810 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1811         noop_grab_focus,
1812         rotate_grab_motion,
1813         rotate_grab_button,
1814 };
1815
1816 static void
1817 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
1818                void *data)
1819 {
1820         struct weston_surface *base_surface =
1821                 (struct weston_surface *) seat->pointer->focus;
1822         struct shell_surface *surface;
1823         struct rotate_grab *rotate;
1824         GLfloat dx, dy;
1825         GLfloat r;
1826
1827         if (base_surface == NULL)
1828                 return;
1829
1830         surface = get_shell_surface(base_surface);
1831         if (!surface)
1832                 return;
1833
1834         switch (surface->type) {
1835                 case SHELL_SURFACE_PANEL:
1836                 case SHELL_SURFACE_BACKGROUND:
1837                 case SHELL_SURFACE_FULLSCREEN:
1838                 case SHELL_SURFACE_SCREENSAVER:
1839                         return;
1840                 default:
1841                         break;
1842         }
1843
1844         rotate = malloc(sizeof *rotate);
1845         if (!rotate)
1846                 return;
1847
1848         shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
1849
1850         weston_surface_to_global(surface->surface,
1851                                  surface->surface->geometry.width / 2,
1852                                  surface->surface->geometry.height / 2,
1853                                  &rotate->center.x, &rotate->center.y);
1854
1855         wl_pointer_start_grab(seat->pointer, &rotate->base.grab);
1856
1857         dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
1858         dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
1859         r = sqrtf(dx * dx + dy * dy);
1860         if (r > 20.0f) {
1861                 struct weston_matrix inverse;
1862
1863                 weston_matrix_init(&inverse);
1864                 inverse.d[0] = dx / r;
1865                 inverse.d[4] = dy / r;
1866                 inverse.d[1] = -inverse.d[4];
1867                 inverse.d[5] = inverse.d[0];
1868                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1869
1870                 weston_matrix_init(&rotate->rotation);
1871                 rotate->rotation.d[0] = dx / r;
1872                 rotate->rotation.d[4] = -dy / r;
1873                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1874                 rotate->rotation.d[5] = rotate->rotation.d[0];
1875         } else {
1876                 weston_matrix_init(&surface->rotation.rotation);
1877                 weston_matrix_init(&rotate->rotation);
1878         }
1879
1880         wl_pointer_set_focus(seat->pointer, NULL,
1881                              wl_fixed_from_int(0),
1882                              wl_fixed_from_int(0));
1883 }
1884
1885 static void
1886 activate(struct desktop_shell *shell, struct weston_surface *es,
1887          struct weston_seat *seat)
1888 {
1889         struct weston_surface *surf, *prev;
1890
1891         weston_surface_activate(es, seat);
1892
1893         switch (get_shell_surface_type(es)) {
1894         case SHELL_SURFACE_BACKGROUND:
1895         case SHELL_SURFACE_PANEL:
1896         case SHELL_SURFACE_LOCK:
1897                 break;
1898
1899         case SHELL_SURFACE_SCREENSAVER:
1900                 /* always below lock surface */
1901                 if (shell->lock_surface)
1902                         weston_surface_restack(es,
1903                                                &shell->lock_surface->surface->layer_link);
1904                 break;
1905         case SHELL_SURFACE_FULLSCREEN:
1906                 /* should on top of panels */
1907                 shell_stack_fullscreen(get_shell_surface(es));
1908                 shell_configure_fullscreen(get_shell_surface(es));
1909                 break;
1910         default:
1911                 /* move the fullscreen surfaces down into the toplevel layer */
1912                 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1913                         wl_list_for_each_reverse_safe(surf,
1914                                                       prev, 
1915                                                       &shell->fullscreen_layer.surface_list, 
1916                                                       layer_link)
1917                                 weston_surface_restack(surf,
1918                                                        &shell->toplevel_layer.surface_list); 
1919                 }
1920
1921                 weston_surface_restack(es,
1922                                        &shell->toplevel_layer.surface_list);
1923                 break;
1924         }
1925 }
1926
1927 /* no-op func for checking black surface */
1928 static void
1929 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1930 {
1931 }
1932
1933 static bool 
1934 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1935 {
1936         if (es->configure == black_surface_configure) {
1937                 if (fs_surface)
1938                         *fs_surface = (struct weston_surface *)es->private;
1939                 return true;
1940         }
1941         return false;
1942 }
1943
1944 static void
1945 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
1946                           void *data)
1947 {
1948         struct weston_seat *ws = (struct weston_seat *) seat;
1949         struct desktop_shell *shell = data;
1950         struct weston_surface *focus;
1951         struct weston_surface *upper;
1952
1953         focus = (struct weston_surface *) seat->pointer->focus;
1954         if (!focus)
1955                 return;
1956
1957         if (is_black_surface(focus, &upper))
1958                 focus = upper;
1959
1960         if (seat->pointer->grab == &seat->pointer->default_grab)
1961                 activate(shell, focus, ws);
1962 }
1963
1964 static void
1965 lock(struct wl_listener *listener, void *data)
1966 {
1967         struct desktop_shell *shell =
1968                 container_of(listener, struct desktop_shell, lock_listener);
1969         struct weston_seat *seat;
1970         struct shell_surface *shsurf;
1971         struct weston_output *output;
1972
1973         if (shell->locked) {
1974                 wl_list_for_each(output, &shell->compositor->output_list, link)
1975                         /* TODO: find a way to jump to other DPMS levels */
1976                         if (output->set_dpms)
1977                                 output->set_dpms(output, WESTON_DPMS_STANDBY);
1978                 return;
1979         }
1980
1981         shell->locked = true;
1982
1983         /* Hide all surfaces by removing the fullscreen, panel and
1984          * toplevel layers.  This way nothing else can show or receive
1985          * input events while we are locked. */
1986
1987         wl_list_remove(&shell->panel_layer.link);
1988         wl_list_remove(&shell->toplevel_layer.link);
1989         wl_list_remove(&shell->fullscreen_layer.link);
1990         wl_list_insert(&shell->compositor->cursor_layer.link,
1991                        &shell->lock_layer.link);
1992
1993         launch_screensaver(shell);
1994
1995         wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1996                 show_screensaver(shell, shsurf);
1997
1998         if (!wl_list_empty(&shell->screensaver.surfaces)) {
1999                 shell->compositor->idle_time = shell->screensaver.duration;
2000                 weston_compositor_wake(shell->compositor);
2001                 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2002         }
2003
2004         /* reset pointer foci */
2005         weston_compositor_schedule_repaint(shell->compositor);
2006
2007         /* reset keyboard foci */
2008         wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2009                 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
2010         }
2011
2012         /* TODO: disable bindings that should not work while locked. */
2013
2014         /* All this must be undone in resume_desktop(). */
2015 }
2016
2017 static void
2018 unlock(struct wl_listener *listener, void *data)
2019 {
2020         struct desktop_shell *shell =
2021                 container_of(listener, struct desktop_shell, unlock_listener);
2022
2023         if (!shell->locked || shell->lock_surface) {
2024                 weston_compositor_wake(shell->compositor);
2025                 return;
2026         }
2027
2028         /* If desktop-shell client has gone away, unlock immediately. */
2029         if (!shell->child.desktop_shell) {
2030                 resume_desktop(shell);
2031                 return;
2032         }
2033
2034         if (shell->prepare_event_sent)
2035                 return;
2036
2037         desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2038         shell->prepare_event_sent = true;
2039 }
2040
2041 static void
2042 center_on_output(struct weston_surface *surface, struct weston_output *output)
2043 {
2044         struct weston_mode *mode = output->current;
2045         GLfloat x = (mode->width - surface->geometry.width) / 2;
2046         GLfloat y = (mode->height - surface->geometry.height) / 2;
2047
2048         weston_surface_set_position(surface, output->x + x, output->y + y);
2049 }
2050
2051 static void
2052 map(struct desktop_shell *shell, struct weston_surface *surface,
2053     int32_t width, int32_t height, int32_t sx, int32_t sy)
2054 {
2055         struct weston_compositor *compositor = shell->compositor;
2056         struct shell_surface *shsurf;
2057         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2058         struct weston_surface *parent;
2059         struct weston_seat *seat;
2060         int panel_height = 0;
2061
2062         shsurf = get_shell_surface(surface);
2063         if (shsurf)
2064                 surface_type = shsurf->type;
2065
2066         surface->geometry.width = width;
2067         surface->geometry.height = height;
2068         surface->geometry.dirty = 1;
2069
2070         /* initial positioning, see also configure() */
2071         switch (surface_type) {
2072         case SHELL_SURFACE_TOPLEVEL:
2073                 weston_surface_set_position(surface, 10 + random() % 400,
2074                                             10 + random() % 400);
2075                 break;
2076         case SHELL_SURFACE_SCREENSAVER:
2077                 center_on_output(surface, shsurf->fullscreen_output);
2078                 break;
2079         case SHELL_SURFACE_FULLSCREEN:
2080                 shell_map_fullscreen(shsurf);
2081                 break;
2082         case SHELL_SURFACE_MAXIMIZED:
2083                 /* use surface configure to set the geometry */
2084                 panel_height = get_output_panel_height(shell,surface->output);
2085                 weston_surface_set_position(surface, surface->output->x,
2086                                             surface->output->y + panel_height);
2087                 break;
2088         case SHELL_SURFACE_LOCK:
2089                 center_on_output(surface, get_default_output(compositor));
2090                 break;
2091         case SHELL_SURFACE_POPUP:
2092                 shell_map_popup(shsurf);
2093         case SHELL_SURFACE_NONE:
2094                 weston_surface_set_position(surface,
2095                                             surface->geometry.x + sx,
2096                                             surface->geometry.y + sy);
2097                 break;
2098         default:
2099                 ;
2100         }
2101
2102         /* surface stacking order, see also activate() */
2103         switch (surface_type) {
2104         case SHELL_SURFACE_BACKGROUND:
2105                 /* background always visible, at the bottom */
2106                 wl_list_insert(&shell->background_layer.surface_list,
2107                                &surface->layer_link);
2108                 break;
2109         case SHELL_SURFACE_PANEL:
2110                 /* panel always on top, hidden while locked */
2111                 wl_list_insert(&shell->panel_layer.surface_list,
2112                                &surface->layer_link);
2113                 break;
2114         case SHELL_SURFACE_LOCK:
2115                 /* lock surface always visible, on top */
2116                 wl_list_insert(&shell->lock_layer.surface_list,
2117                                &surface->layer_link);
2118                 weston_compositor_wake(compositor);
2119                 break;
2120         case SHELL_SURFACE_SCREENSAVER:
2121                 /* If locked, show it. */
2122                 if (shell->locked) {
2123                         show_screensaver(shell, shsurf);
2124                         compositor->idle_time = shell->screensaver.duration;
2125                         weston_compositor_wake(compositor);
2126                         if (!shell->lock_surface)
2127                                 compositor->state = WESTON_COMPOSITOR_IDLE;
2128                 }
2129                 break;
2130         case SHELL_SURFACE_POPUP:
2131         case SHELL_SURFACE_TRANSIENT:
2132                 parent = shsurf->parent->surface;
2133                 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2134                 break;
2135         case SHELL_SURFACE_FULLSCREEN:
2136         case SHELL_SURFACE_NONE:
2137                 break;
2138         default:
2139                 wl_list_insert(&shell->toplevel_layer.surface_list,
2140                                &surface->layer_link); 
2141                 break;
2142         }
2143
2144         if (surface_type != SHELL_SURFACE_NONE) {
2145                 weston_surface_assign_output(surface);
2146                 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2147                         surface->output = shsurf->output;
2148         }
2149
2150         switch (surface_type) {
2151         case SHELL_SURFACE_TRANSIENT:
2152                 if (shsurf->transient.flags ==
2153                                 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2154                         break;
2155         case SHELL_SURFACE_TOPLEVEL:
2156         case SHELL_SURFACE_FULLSCREEN:
2157         case SHELL_SURFACE_MAXIMIZED:
2158                 if (!shell->locked) {
2159                         wl_list_for_each(seat, &compositor->seat_list, link)
2160                                 activate(shell, surface, seat);
2161                 }
2162                 break;
2163         default:
2164                 break;
2165         }
2166
2167         if (surface_type == SHELL_SURFACE_TOPLEVEL)
2168         {
2169                 switch (shell->win_animation_type) {
2170                 case ANIMATION_FADE:
2171                         weston_fade_run(surface, NULL, NULL);
2172                         break;
2173                 case ANIMATION_ZOOM:
2174                         weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2175                         break;
2176                 default:
2177                         break;
2178                 }
2179         }
2180 }
2181
2182 static void
2183 configure(struct desktop_shell *shell, struct weston_surface *surface,
2184           GLfloat x, GLfloat y, int32_t width, int32_t height)
2185 {
2186         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2187         struct shell_surface *shsurf;
2188
2189         shsurf = get_shell_surface(surface);
2190         if (shsurf)
2191                 surface_type = shsurf->type;
2192
2193         surface->geometry.x = x;
2194         surface->geometry.y = y;
2195         surface->geometry.width = width;
2196         surface->geometry.height = height;
2197         surface->geometry.dirty = 1;
2198
2199         switch (surface_type) {
2200         case SHELL_SURFACE_SCREENSAVER:
2201                 center_on_output(surface, shsurf->fullscreen_output);
2202                 break;
2203         case SHELL_SURFACE_FULLSCREEN:
2204                 shell_stack_fullscreen(shsurf);
2205                 shell_configure_fullscreen(shsurf);
2206                 break;
2207         case SHELL_SURFACE_MAXIMIZED:
2208                 /* setting x, y and using configure to change that geometry */
2209                 surface->geometry.x = surface->output->x;
2210                 surface->geometry.y = surface->output->y +
2211                         get_output_panel_height(shell,surface->output);
2212                 break;
2213         case SHELL_SURFACE_TOPLEVEL:
2214                 break;
2215         default:
2216                 break;
2217         }
2218
2219         /* XXX: would a fullscreen surface need the same handling? */
2220         if (surface->output) {
2221                 weston_surface_assign_output(surface);
2222
2223                 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2224                         surface->output = shsurf->output;
2225                 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2226                         surface->output = shsurf->output;
2227         }
2228 }
2229
2230 static void
2231 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2232 {
2233         struct shell_surface *shsurf = get_shell_surface(es);
2234         struct desktop_shell *shell = shsurf->shell;
2235         int type_changed = 0;
2236
2237         if (shsurf->next_type != SHELL_SURFACE_NONE &&
2238             shsurf->type != shsurf->next_type) {
2239                 set_surface_type(shsurf);
2240                 type_changed = 1;
2241         }
2242
2243         if (!weston_surface_is_mapped(es)) {
2244                 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
2245         } else if (type_changed || sx != 0 || sy != 0 ||
2246                    es->geometry.width != es->buffer->width ||
2247                    es->geometry.height != es->buffer->height) {
2248                 GLfloat from_x, from_y;
2249                 GLfloat to_x, to_y;
2250
2251                 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2252                 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2253                 configure(shell, es,
2254                           es->geometry.x + to_x - from_x,
2255                           es->geometry.y + to_y - from_y,
2256                           es->buffer->width, es->buffer->height);
2257         }
2258 }
2259
2260 static int launch_desktop_shell_process(struct desktop_shell *shell);
2261
2262 static void
2263 desktop_shell_sigchld(struct weston_process *process, int status)
2264 {
2265         uint32_t time;
2266         struct desktop_shell *shell =
2267                 container_of(process, struct desktop_shell, child.process);
2268
2269         shell->child.process.pid = 0;
2270         shell->child.client = NULL; /* already destroyed by wayland */
2271
2272         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2273         time = weston_compositor_get_time();
2274         if (time - shell->child.deathstamp > 30000) {
2275                 shell->child.deathstamp = time;
2276                 shell->child.deathcount = 0;
2277         }
2278
2279         shell->child.deathcount++;
2280         if (shell->child.deathcount > 5) {
2281                 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2282                 return;
2283         }
2284
2285         fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2286         launch_desktop_shell_process(shell);
2287 }
2288
2289 static int
2290 launch_desktop_shell_process(struct desktop_shell *shell)
2291 {
2292         const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
2293
2294         shell->child.client = weston_client_launch(shell->compositor,
2295                                                  &shell->child.process,
2296                                                  shell_exe,
2297                                                  desktop_shell_sigchld);
2298
2299         if (!shell->child.client)
2300                 return -1;
2301         return 0;
2302 }
2303
2304 static void
2305 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2306 {
2307         struct desktop_shell *shell = data;
2308
2309         wl_client_add_object(client, &wl_shell_interface,
2310                              &shell_implementation, id, shell);
2311 }
2312
2313 static void
2314 unbind_desktop_shell(struct wl_resource *resource)
2315 {
2316         struct desktop_shell *shell = resource->data;
2317
2318         if (shell->locked)
2319                 resume_desktop(shell);
2320
2321         shell->child.desktop_shell = NULL;
2322         shell->prepare_event_sent = false;
2323         free(resource);
2324 }
2325
2326 static void
2327 bind_desktop_shell(struct wl_client *client,
2328                    void *data, uint32_t version, uint32_t id)
2329 {
2330         struct desktop_shell *shell = data;
2331         struct wl_resource *resource;
2332
2333         resource = wl_client_add_object(client, &desktop_shell_interface,
2334                                         &desktop_shell_implementation,
2335                                         id, shell);
2336
2337         if (client == shell->child.client) {
2338                 resource->destroy = unbind_desktop_shell;
2339                 shell->child.desktop_shell = resource;
2340                 return;
2341         }
2342
2343         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2344                                "permission to bind desktop_shell denied");
2345         wl_resource_destroy(resource);
2346 }
2347
2348 static void
2349 screensaver_set_surface(struct wl_client *client,
2350                         struct wl_resource *resource,
2351                         struct wl_resource *shell_surface_resource,
2352                         struct wl_resource *output_resource)
2353 {
2354         struct desktop_shell *shell = resource->data;
2355         struct shell_surface *surface = shell_surface_resource->data;
2356         struct weston_output *output = output_resource->data;
2357
2358         surface->next_type = SHELL_SURFACE_SCREENSAVER;
2359
2360         surface->fullscreen_output = output;
2361         surface->output = output;
2362         wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
2363 }
2364
2365 static const struct screensaver_interface screensaver_implementation = {
2366         screensaver_set_surface
2367 };
2368
2369 static void
2370 unbind_screensaver(struct wl_resource *resource)
2371 {
2372         struct desktop_shell *shell = resource->data;
2373
2374         shell->screensaver.binding = NULL;
2375         free(resource);
2376 }
2377
2378 static void
2379 bind_screensaver(struct wl_client *client,
2380                  void *data, uint32_t version, uint32_t id)
2381 {
2382         struct desktop_shell *shell = data;
2383         struct wl_resource *resource;
2384
2385         resource = wl_client_add_object(client, &screensaver_interface,
2386                                         &screensaver_implementation,
2387                                         id, shell);
2388
2389         if (shell->screensaver.binding == NULL) {
2390                 resource->destroy = unbind_screensaver;
2391                 shell->screensaver.binding = resource;
2392                 return;
2393         }
2394
2395         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2396                                "interface object already bound");
2397         wl_resource_destroy(resource);
2398 }
2399
2400 struct switcher {
2401         struct desktop_shell *shell;
2402         struct weston_surface *current;
2403         struct wl_listener listener;
2404         struct wl_keyboard_grab grab;
2405 };
2406
2407 static void
2408 switcher_next(struct switcher *switcher)
2409 {
2410         struct weston_compositor *compositor = switcher->shell->compositor;
2411         struct weston_surface *surface;
2412         struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
2413         struct shell_surface *shsurf;
2414
2415         wl_list_for_each(surface, &compositor->surface_list, link) {
2416                 switch (get_shell_surface_type(surface)) {
2417                 case SHELL_SURFACE_TOPLEVEL:
2418                 case SHELL_SURFACE_FULLSCREEN:
2419                 case SHELL_SURFACE_MAXIMIZED:
2420                         if (first == NULL)
2421                                 first = surface;
2422                         if (prev == switcher->current)
2423                                 next = surface;
2424                         prev = surface;
2425                         surface->alpha = 0.25;
2426                         surface->geometry.dirty = 1;
2427                         weston_surface_damage(surface);
2428                         break;
2429                 default:
2430                         break;
2431                 }
2432
2433                 if (is_black_surface(surface, NULL)) {
2434                         surface->alpha = 0.25;
2435                         surface->geometry.dirty = 1;
2436                         weston_surface_damage(surface);
2437                 }
2438         }
2439
2440         if (next == NULL)
2441                 next = first;
2442
2443         if (next == NULL)
2444                 return;
2445
2446         wl_list_remove(&switcher->listener.link);
2447         wl_signal_add(&next->surface.resource.destroy_signal,
2448                       &switcher->listener);
2449
2450         switcher->current = next;
2451         next->alpha = 1.0;
2452
2453         shsurf = get_shell_surface(switcher->current);
2454         if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2455                 shsurf->fullscreen.black_surface->alpha = 1.0;
2456 }
2457
2458 static void
2459 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
2460 {
2461         struct switcher *switcher =
2462                 container_of(listener, struct switcher, listener);
2463
2464         switcher_next(switcher);
2465 }
2466
2467 static void
2468 switcher_destroy(struct switcher *switcher)
2469 {
2470         struct weston_compositor *compositor = switcher->shell->compositor;
2471         struct weston_surface *surface;
2472         struct wl_keyboard *keyboard = switcher->grab.keyboard;
2473
2474         wl_list_for_each(surface, &compositor->surface_list, link) {
2475                 surface->alpha = 1.0;
2476                 weston_surface_damage(surface);
2477         }
2478
2479         if (switcher->current)
2480                 activate(switcher->shell, switcher->current,
2481                          (struct weston_seat *) keyboard->seat);
2482         wl_list_remove(&switcher->listener.link);
2483         wl_keyboard_end_grab(keyboard);
2484         free(switcher);
2485 }
2486
2487 static void
2488 switcher_key(struct wl_keyboard_grab *grab,
2489              uint32_t time, uint32_t key, uint32_t state_w)
2490 {
2491         struct switcher *switcher = container_of(grab, struct switcher, grab);
2492         enum wl_keyboard_key_state state = state_w;
2493
2494         if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
2495                 switcher_next(switcher);
2496 }
2497
2498 static void
2499 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
2500                   uint32_t mods_depressed, uint32_t mods_latched,
2501                   uint32_t mods_locked, uint32_t group)
2502 {
2503         struct switcher *switcher = container_of(grab, struct switcher, grab);
2504         struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
2505
2506         if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
2507                 switcher_destroy(switcher);
2508 }
2509
2510 static const struct wl_keyboard_grab_interface switcher_grab = {
2511         switcher_key,
2512         switcher_modifier,
2513 };
2514
2515 static void
2516 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2517                  void *data)
2518 {
2519         struct desktop_shell *shell = data;
2520         struct switcher *switcher;
2521
2522         switcher = malloc(sizeof *switcher);
2523         switcher->shell = shell;
2524         switcher->current = NULL;
2525         switcher->listener.notify = switcher_handle_surface_destroy;
2526         wl_list_init(&switcher->listener.link);
2527
2528         switcher->grab.interface = &switcher_grab;
2529         wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
2530         wl_keyboard_set_focus(seat->keyboard, NULL);
2531         switcher_next(switcher);
2532 }
2533
2534 static void
2535 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2536                   void *data)
2537 {
2538         struct weston_compositor *compositor = data;
2539         struct weston_output *output;
2540         long backlight_new = 0;
2541
2542         /* TODO: we're limiting to simple use cases, where we assume just
2543          * control on the primary display. We'd have to extend later if we
2544          * ever get support for setting backlights on random desktop LCD
2545          * panels though */
2546         output = get_default_output(compositor);
2547         if (!output)
2548                 return;
2549
2550         if (!output->set_backlight)
2551                 return;
2552
2553         if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2554                 backlight_new = output->backlight_current - 25;
2555         else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2556                 backlight_new = output->backlight_current + 25;
2557
2558         if (backlight_new < 5)
2559                 backlight_new = 5;
2560         if (backlight_new > 255)
2561                 backlight_new = 255;
2562
2563         output->backlight_current = backlight_new;
2564         output->set_backlight(output, output->backlight_current);
2565 }
2566
2567 static void
2568 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2569                       void *data)
2570 {
2571         struct desktop_shell *shell = data;
2572         struct weston_compositor *compositor = shell->compositor;
2573         struct weston_surface *surface;
2574
2575         if (shell->debug_repaint_surface) {
2576                 weston_surface_destroy(shell->debug_repaint_surface);
2577                 shell->debug_repaint_surface = NULL;
2578         } else {
2579                 surface = weston_surface_create(compositor);
2580                 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2581                 weston_surface_configure(surface, 0, 0, 8192, 8192);
2582                 wl_list_insert(&compositor->fade_layer.surface_list,
2583                                &surface->layer_link);
2584                 weston_surface_assign_output(surface);
2585                 pixman_region32_init(&surface->input);
2586
2587                 /* Here's the dirty little trick that makes the
2588                  * repaint debugging work: we force an
2589                  * update_transform first to update dependent state
2590                  * and clear the geometry.dirty bit.  Then we clear
2591                  * the surface damage so it only gets repainted
2592                  * piecewise as we repaint other things.  */
2593
2594                 weston_surface_update_transform(surface);
2595                 pixman_region32_fini(&surface->damage);
2596                 pixman_region32_init(&surface->damage);
2597                 shell->debug_repaint_surface = surface;
2598         }
2599 }
2600
2601 static void
2602 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2603                    void *data)
2604 {
2605         struct wl_client *client;
2606         pid_t pid;
2607         uid_t uid;
2608         gid_t gid;
2609
2610         client = seat->keyboard->focus->resource.client;
2611         wl_client_get_credentials(client, &pid, &uid, &gid);
2612
2613         kill(pid, SIGKILL);
2614 }
2615
2616 static void
2617 shell_destroy(struct wl_listener *listener, void *data)
2618 {
2619         struct desktop_shell *shell =
2620                 container_of(listener, struct desktop_shell, destroy_listener);
2621
2622         if (shell->child.client)
2623                 wl_client_destroy(shell->child.client);
2624
2625         wl_list_remove(&shell->lock_listener.link);
2626         wl_list_remove(&shell->unlock_listener.link);
2627
2628         free(shell->screensaver.path);
2629         free(shell);
2630 }
2631
2632 static void
2633 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2634 {
2635         uint32_t mod;
2636
2637         /* fixed bindings */
2638         weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
2639                                           MODIFIER_CTRL | MODIFIER_ALT,
2640                                           terminate_binding, ec);
2641         weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
2642                                              click_to_activate_binding,
2643                                              shell);
2644         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2645                                            MODIFIER_SUPER | MODIFIER_ALT,
2646                                            surface_opacity_binding, NULL);
2647         weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
2648                                            MODIFIER_SUPER, zoom_axis_binding,
2649                                            NULL);
2650
2651         /* configurable bindings */
2652         mod = shell->binding_modifier;
2653         weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
2654                                           zoom_key_binding, NULL);
2655         weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
2656                                           zoom_key_binding, NULL);
2657         weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
2658                                              shell);
2659         weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
2660                                              resize_binding, shell);
2661         weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
2662                                              rotate_binding, NULL);
2663         weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
2664                                           shell);
2665         weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
2666                                           ec);
2667         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
2668                                           backlight_binding, ec);
2669         weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
2670                                           ec);
2671         weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
2672                                           backlight_binding, ec);
2673         weston_compositor_add_key_binding(ec, KEY_SPACE, mod,
2674                                           debug_repaint_binding, shell);
2675         weston_compositor_add_key_binding(ec, KEY_K, mod,
2676                                           force_kill_binding, shell);
2677 }
2678
2679 int
2680 shell_init(struct weston_compositor *ec);
2681
2682 WL_EXPORT int
2683 shell_init(struct weston_compositor *ec)
2684 {
2685         struct desktop_shell *shell;
2686
2687         shell = malloc(sizeof *shell);
2688         if (shell == NULL)
2689                 return -1;
2690
2691         memset(shell, 0, sizeof *shell);
2692         shell->compositor = ec;
2693
2694         shell->destroy_listener.notify = shell_destroy;
2695         wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2696         shell->lock_listener.notify = lock;
2697         wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2698         shell->unlock_listener.notify = unlock;
2699         wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
2700         ec->ping_handler = ping_handler;
2701         ec->shell_interface.create_shell_surface = create_shell_surface;
2702         ec->shell_interface.set_toplevel = set_toplevel;
2703         ec->shell_interface.set_transient = set_transient;
2704         ec->shell_interface.move = surface_move;
2705         ec->shell_interface.resize = surface_resize;
2706
2707         wl_list_init(&shell->backgrounds);
2708         wl_list_init(&shell->panels);
2709         wl_list_init(&shell->screensaver.surfaces);
2710
2711         weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2712         weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2713         weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2714         weston_layer_init(&shell->background_layer,
2715                           &shell->toplevel_layer.link);
2716         wl_list_init(&shell->lock_layer.surface_list);
2717
2718         shell_configuration(shell);
2719
2720         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2721                                   shell, bind_shell) == NULL)
2722                 return -1;
2723
2724         if (wl_display_add_global(ec->wl_display,
2725                                   &desktop_shell_interface,
2726                                   shell, bind_desktop_shell) == NULL)
2727                 return -1;
2728
2729         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2730                                   shell, bind_screensaver) == NULL)
2731                 return -1;
2732
2733         shell->child.deathstamp = weston_compositor_get_time();
2734         if (launch_desktop_shell_process(shell) != 0)
2735                 return -1;
2736
2737         shell_add_bindings(ec, shell);
2738
2739         return 0;
2740 }