compositor: add dpms and backlight support
[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
34 #include <wayland-server.h>
35 #include "compositor.h"
36 #include "desktop-shell-server-protocol.h"
37 #include "../shared/config-parser.h"
38
39 struct shell_surface;
40
41 struct wl_shell {
42         struct weston_compositor *compositor;
43         struct weston_shell shell;
44
45         struct {
46                 struct weston_process process;
47                 struct wl_client *client;
48                 struct wl_resource *desktop_shell;
49
50                 unsigned deathcount;
51                 uint32_t deathstamp;
52         } child;
53
54         bool locked;
55         bool prepare_event_sent;
56
57         struct shell_surface *lock_surface;
58         struct wl_listener lock_surface_listener;
59         struct wl_list hidden_surface_list;
60
61         struct wl_list backgrounds;
62         struct wl_list panels;
63
64         struct {
65                 char *path;
66                 int duration;
67                 struct wl_resource *binding;
68                 struct wl_list surfaces;
69                 struct weston_process process;
70         } screensaver;
71 };
72
73 enum shell_surface_type {
74         SHELL_SURFACE_NONE,
75
76         SHELL_SURFACE_PANEL,
77         SHELL_SURFACE_BACKGROUND,
78         SHELL_SURFACE_LOCK,
79         SHELL_SURFACE_SCREENSAVER,
80
81         SHELL_SURFACE_TOPLEVEL,
82         SHELL_SURFACE_TRANSIENT,
83         SHELL_SURFACE_FULLSCREEN,
84         SHELL_SURFACE_MAXIMIZED,
85         SHELL_SURFACE_POPUP
86 };
87
88 struct shell_surface {
89         struct wl_resource resource;
90
91         struct weston_surface *surface;
92         struct wl_listener surface_destroy_listener;
93         struct shell_surface *parent;
94
95         enum shell_surface_type type;
96         int32_t saved_x, saved_y;
97
98         struct {
99                 struct weston_transform transform;
100                 struct weston_matrix rotation;
101         } rotation;
102
103         struct {
104                 struct wl_pointer_grab grab;
105                 uint32_t time;
106                 int32_t x, y;
107                 struct weston_transform parent_transform;
108                 int32_t initial_up;
109         } popup;
110
111         struct weston_output *fullscreen_output;
112         struct weston_output *output;
113         struct wl_list link;
114 };
115
116 struct weston_move_grab {
117         struct wl_pointer_grab grab;
118         struct weston_surface *surface;
119         int32_t dx, dy;
120 };
121
122 struct weston_zoom_grab {
123         struct wl_keyboard_grab grab;
124 };
125
126 struct rotate_grab {
127         struct wl_pointer_grab grab;
128         struct shell_surface *surface;
129         struct weston_matrix rotation;
130         struct {
131                 int32_t x;
132                 int32_t y;
133         } center;
134 };
135
136 static void
137 shell_configuration(struct wl_shell *shell)
138 {
139         char *config_file;
140         char *path = NULL;
141         int duration = 60;
142
143         struct config_key saver_keys[] = {
144                 { "path",       CONFIG_KEY_STRING,  &path },
145                 { "duration",   CONFIG_KEY_INTEGER, &duration },
146         };
147
148         struct config_section cs[] = {
149                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
150         };
151
152         config_file = config_file_path("weston-desktop-shell.ini");
153         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
154         free(config_file);
155
156         shell->screensaver.path = path;
157         shell->screensaver.duration = duration;
158 }
159
160 static void
161 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
162                 struct wl_surface *surface, int32_t x, int32_t y)
163 {
164         grab->focus = NULL;
165 }
166
167 static void
168 move_grab_motion(struct wl_pointer_grab *grab,
169                  uint32_t time, int32_t x, int32_t y)
170 {
171         struct weston_move_grab *move = (struct weston_move_grab *) grab;
172         struct wl_input_device *device = grab->input_device;
173         struct weston_surface *es = move->surface;
174
175         weston_surface_configure(es,
176                                  device->x + move->dx,
177                                  device->y + move->dy,
178                                  es->geometry.width, es->geometry.height);
179 }
180
181 static void
182 move_grab_button(struct wl_pointer_grab *grab,
183                  uint32_t time, int32_t button, int32_t state)
184 {
185         struct wl_input_device *device = grab->input_device;
186
187         if (device->button_count == 0 && state == 0) {
188                 wl_input_device_end_pointer_grab(device, time);
189                 free(grab);
190         }
191 }
192
193 static const struct wl_pointer_grab_interface move_grab_interface = {
194         noop_grab_focus,
195         move_grab_motion,
196         move_grab_button,
197 };
198
199 static int
200 weston_surface_move(struct weston_surface *es,
201                     struct weston_input_device *wd, uint32_t time)
202 {
203         struct weston_move_grab *move;
204
205         move = malloc(sizeof *move);
206         if (!move)
207                 return -1;
208
209         move->grab.interface = &move_grab_interface;
210         move->dx = es->geometry.x - wd->input_device.grab_x;
211         move->dy = es->geometry.y - wd->input_device.grab_y;
212         move->surface = es;
213
214         wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
215
216         wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
217
218         return 0;
219 }
220
221 static void
222 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
223                    struct wl_resource *input_resource, uint32_t time)
224 {
225         struct weston_input_device *wd = input_resource->data;
226         struct shell_surface *shsurf = resource->data;
227
228         if (wd->input_device.button_count == 0 ||
229             wd->input_device.grab_time != time ||
230             wd->input_device.pointer_focus != &shsurf->surface->surface)
231                 return;
232
233         if (weston_surface_move(shsurf->surface, wd, time) < 0)
234                 wl_resource_post_no_memory(resource);
235 }
236
237 struct weston_resize_grab {
238         struct wl_pointer_grab grab;
239         uint32_t edges;
240         int32_t width, height;
241         struct shell_surface *shsurf;
242 };
243
244 static void
245 resize_grab_motion(struct wl_pointer_grab *grab,
246                    uint32_t time, int32_t x, int32_t y)
247 {
248         struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
249         struct wl_input_device *device = grab->input_device;
250         int32_t width, height;
251         int32_t from_x, from_y;
252         int32_t to_x, to_y;
253
254         weston_surface_from_global(resize->shsurf->surface,
255                                    device->grab_x, device->grab_y,
256                                    &from_x, &from_y);
257         weston_surface_from_global(resize->shsurf->surface,
258                                    device->x, device->y, &to_x, &to_y);
259
260         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
261                 width = resize->width + from_x - to_x;
262         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
263                 width = resize->width + to_x - from_x;
264         } else {
265                 width = resize->width;
266         }
267
268         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
269                 height = resize->height + from_y - to_y;
270         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
271                 height = resize->height + to_y - from_y;
272         } else {
273                 height = resize->height;
274         }
275
276         wl_resource_post_event(&resize->shsurf->resource,
277                                WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
278                                width, height);
279 }
280
281 static void
282 resize_grab_button(struct wl_pointer_grab *grab,
283                    uint32_t time, int32_t button, int32_t state)
284 {
285         struct wl_input_device *device = grab->input_device;
286
287         if (device->button_count == 0 && state == 0) {
288                 wl_input_device_end_pointer_grab(device, time);
289                 free(grab);
290         }
291 }
292
293 static const struct wl_pointer_grab_interface resize_grab_interface = {
294         noop_grab_focus,
295         resize_grab_motion,
296         resize_grab_button,
297 };
298
299 static int
300 weston_surface_resize(struct shell_surface *shsurf,
301                     struct weston_input_device *wd,
302                     uint32_t time, uint32_t edges)
303 {
304         struct weston_resize_grab *resize;
305
306         /* FIXME: Reject if fullscreen */
307
308         if (edges == 0 || edges > 15 ||
309             (edges & 3) == 3 || (edges & 12) == 12)
310                 return 0;
311
312         resize = malloc(sizeof *resize);
313         if (!resize)
314                 return -1;
315
316         resize->grab.interface = &resize_grab_interface;
317         resize->edges = edges;
318         resize->width = shsurf->surface->geometry.width;
319         resize->height = shsurf->surface->geometry.height;
320         resize->shsurf = shsurf;
321
322         wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
323
324         wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
325
326         return 0;
327 }
328
329 static void
330 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
331                      struct wl_resource *input_resource, uint32_t time,
332                      uint32_t edges)
333 {
334         struct weston_input_device *wd = input_resource->data;
335         struct shell_surface *shsurf = resource->data;
336
337         /* FIXME: Reject if fullscreen */
338
339         if (wd->input_device.button_count == 0 ||
340             wd->input_device.grab_time != time ||
341             wd->input_device.pointer_focus != &shsurf->surface->surface)
342                 return;
343
344         if (weston_surface_resize(shsurf, wd, time, edges) < 0)
345                 wl_resource_post_no_memory(resource);
346 }
347
348 static struct weston_output *
349 get_default_output(struct weston_compositor *compositor)
350 {
351         return container_of(compositor->output_list.next,
352                             struct weston_output, link);
353 }
354
355 static int
356 reset_shell_surface_type(struct shell_surface *surface)
357 {
358         switch (surface->type) {
359         case SHELL_SURFACE_FULLSCREEN:
360                 weston_surface_set_position(surface->surface,
361                                             surface->saved_x,
362                                             surface->saved_y);
363                 surface->fullscreen_output = NULL;
364                 break;
365         case SHELL_SURFACE_MAXIMIZED:
366                 surface->output = get_default_output(surface->surface->compositor);
367                 weston_surface_set_position(surface->surface,
368                                             surface->saved_x,
369                                             surface->saved_y);
370                 break;
371         case SHELL_SURFACE_PANEL:
372         case SHELL_SURFACE_BACKGROUND:
373                 wl_list_remove(&surface->link);
374                 wl_list_init(&surface->link);
375                 break;
376         case SHELL_SURFACE_SCREENSAVER:
377         case SHELL_SURFACE_LOCK:
378                 wl_resource_post_error(&surface->resource,
379                                        WL_DISPLAY_ERROR_INVALID_METHOD,
380                                        "cannot reassign surface type");
381                 return -1;
382         case SHELL_SURFACE_NONE:
383         case SHELL_SURFACE_TOPLEVEL:
384         case SHELL_SURFACE_TRANSIENT:
385         case SHELL_SURFACE_POPUP:
386                 break;
387         }
388
389         surface->type = SHELL_SURFACE_NONE;
390         return 0;
391 }
392
393 static void
394 shell_surface_set_toplevel(struct wl_client *client,
395                            struct wl_resource *resource)
396
397 {
398         struct shell_surface *surface = resource->data;
399
400         if (reset_shell_surface_type(surface))
401                 return;
402
403         surface->type = SHELL_SURFACE_TOPLEVEL;
404 }
405
406 static void
407 shell_surface_set_transient(struct wl_client *client,
408                             struct wl_resource *resource,
409                             struct wl_resource *parent_resource,
410                             int x, int y, uint32_t flags)
411 {
412         struct shell_surface *shsurf = resource->data;
413         struct weston_surface *es = shsurf->surface;
414         struct shell_surface *pshsurf = parent_resource->data;
415         struct weston_surface *pes = pshsurf->surface;
416
417         if (reset_shell_surface_type(shsurf))
418                 return;
419
420         /* assign to parents output  */
421         shsurf->output = pes->output;
422         weston_surface_set_position(es, pes->geometry.x + x,
423                                         pes->geometry.y + y);
424
425         shsurf->type = SHELL_SURFACE_TRANSIENT;
426 }
427
428 static struct wl_shell *
429 shell_surface_get_shell(struct shell_surface *shsurf)
430 {
431         struct weston_surface *es = shsurf->surface;
432         struct weston_shell *shell = es->compositor->shell;
433
434         return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
435 }
436
437 static int
438 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
439 {
440         struct shell_surface *priv;
441         int panel_height = 0;
442
443         if (!output)
444                 return 0;
445
446         wl_list_for_each(priv, &wlshell->panels, link) {
447                 if (priv->output == output) {
448                         panel_height = priv->surface->geometry.height;
449                         break;
450                 }
451         }
452         return panel_height;
453 }
454
455 static void
456 shell_surface_set_maximized(struct wl_client *client,
457                             struct wl_resource *resource,
458                             struct wl_resource *output_resource )
459 {
460         struct shell_surface *shsurf = resource->data;
461         struct weston_surface *es = shsurf->surface;
462         struct wl_shell *wlshell = NULL;
463         uint32_t edges = 0, panel_height = 0;
464
465         /* get the default output, if the client set it as NULL
466            check whether the ouput is available */
467         if (output_resource)
468                 shsurf->output = output_resource->data;
469         else
470                 shsurf->output = get_default_output(es->compositor);
471
472         if (reset_shell_surface_type(shsurf))
473                 return;
474
475         shsurf->saved_x = es->geometry.x;
476         shsurf->saved_y = es->geometry.y;
477
478         wlshell = shell_surface_get_shell(shsurf);
479         panel_height = get_output_panel_height(wlshell, es->output);
480         edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
481         wl_resource_post_event(&shsurf->resource,
482                                WL_SHELL_SURFACE_CONFIGURE,
483                                weston_compositor_get_time(), edges,
484                                es->output->current->width,
485                                es->output->current->height - panel_height);
486
487         shsurf->type = SHELL_SURFACE_MAXIMIZED;
488 }
489
490 static void
491 shell_surface_set_fullscreen(struct wl_client *client,
492                              struct wl_resource *resource,
493                              uint32_t method,
494                              uint32_t framerate,
495                              struct wl_resource *output_resource)
496 {
497         struct shell_surface *shsurf = resource->data;
498         struct weston_surface *es = shsurf->surface;
499         struct weston_output *output;
500
501         if (reset_shell_surface_type(shsurf))
502                 return;
503
504         /* FIXME: Fullscreen on first output */
505         /* FIXME: Handle output going away */
506         output = get_default_output(es->compositor);
507
508         shsurf->saved_x = es->geometry.x;
509         shsurf->saved_y = es->geometry.y;
510         shsurf->output = output;
511         shsurf->fullscreen_output = output;
512         shsurf->type = SHELL_SURFACE_FULLSCREEN;
513
514         wl_resource_post_event(resource,
515                                WL_SHELL_SURFACE_CONFIGURE,
516                                weston_compositor_get_time(), 0,
517                                shsurf->output->current->width,
518                                shsurf->output->current->height);
519 }
520
521 static void
522 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
523                  struct wl_surface *surface, int32_t x, int32_t y)
524 {
525         struct wl_input_device *device = grab->input_device;
526         struct shell_surface *priv =
527                 container_of(grab, struct shell_surface, popup.grab);
528         struct wl_client *client = priv->surface->surface.resource.client;
529
530         if (surface && surface->resource.client == client) {
531                 wl_input_device_set_pointer_focus(device, surface, time, x, y);
532                 grab->focus = surface;
533         } else {
534                 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
535                 grab->focus = NULL;
536         }
537 }
538
539 static void
540 popup_grab_motion(struct wl_pointer_grab *grab,
541                   uint32_t time, int32_t sx, int32_t sy)
542 {
543         struct wl_resource *resource;
544
545         resource = grab->input_device->pointer_focus_resource;
546         if (resource)
547                 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
548                                        time, sx, sy);
549 }
550
551 static void
552 popup_grab_button(struct wl_pointer_grab *grab,
553                   uint32_t time, int32_t button, int32_t state)
554 {
555         struct wl_resource *resource;
556         struct shell_surface *shsurf =
557                 container_of(grab, struct shell_surface, popup.grab);
558
559         resource = grab->input_device->pointer_focus_resource;
560         if (resource) {
561                 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
562                                        time, button, state);
563         } else if (state == 0 &&
564                    (shsurf->popup.initial_up ||
565                     time - shsurf->popup.time > 500)) {
566                 wl_resource_post_event(&shsurf->resource,
567                                        WL_SHELL_SURFACE_POPUP_DONE);
568                 wl_input_device_end_pointer_grab(grab->input_device, time);
569                 shsurf->popup.grab.input_device = NULL;
570         }
571
572         if (state == 0)
573                 shsurf->popup.initial_up = 1;
574 }
575
576 static const struct wl_pointer_grab_interface popup_grab_interface = {
577         popup_grab_focus,
578         popup_grab_motion,
579         popup_grab_button,
580 };
581
582 static void
583 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
584 {
585         struct wl_input_device *device;
586         struct weston_surface *es = shsurf->surface;
587         struct weston_surface *parent = shsurf->parent->surface;
588
589         es->output = parent->output;
590
591         shsurf->popup.grab.interface = &popup_grab_interface;
592         device = es->compositor->input_device;
593
594         weston_surface_update_transform(parent);
595         if (parent->transform.enabled) {
596                 shsurf->popup.parent_transform.matrix =
597                         parent->transform.matrix;
598         } else {
599                 /* construct x, y translation matrix */
600                 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
601                 shsurf->popup.parent_transform.matrix.d[12] =
602                         parent->geometry.x;
603                 shsurf->popup.parent_transform.matrix.d[13] =
604                         parent->geometry.y;
605         }
606         wl_list_insert(es->geometry.transformation_list.prev,
607                        &shsurf->popup.parent_transform.link);
608         weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
609
610         shsurf->popup.grab.input_device = device;
611         shsurf->popup.time = device->grab_time;
612         shsurf->popup.initial_up = 0;
613
614         wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
615                                    &shsurf->popup.grab, shsurf->popup.time);
616 }
617
618 static void
619 shell_surface_set_popup(struct wl_client *client,
620                         struct wl_resource *resource,
621                         struct wl_resource *input_device_resource,
622                         uint32_t time,
623                         struct wl_resource *parent_resource,
624                         int32_t x, int32_t y, uint32_t flags)
625 {
626         struct shell_surface *shsurf = resource->data;
627
628         shsurf->type = SHELL_SURFACE_POPUP;
629         shsurf->parent = parent_resource->data;
630         shsurf->popup.x = x;
631         shsurf->popup.y = y;
632 }
633
634 static const struct wl_shell_surface_interface shell_surface_implementation = {
635         shell_surface_move,
636         shell_surface_resize,
637         shell_surface_set_toplevel,
638         shell_surface_set_transient,
639         shell_surface_set_fullscreen,
640         shell_surface_set_popup,
641         shell_surface_set_maximized
642 };
643
644 static void
645 destroy_shell_surface(struct wl_resource *resource)
646 {
647         struct shell_surface *shsurf = resource->data;
648
649         if (shsurf->popup.grab.input_device)
650                 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
651
652         /* in case cleaning up a dead client destroys shell_surface first */
653         if (shsurf->surface)
654                 wl_list_remove(&shsurf->surface_destroy_listener.link);
655
656         wl_list_remove(&shsurf->link);
657         free(shsurf);
658 }
659
660 static void
661 shell_handle_surface_destroy(struct wl_listener *listener,
662                              struct wl_resource *resource, uint32_t time)
663 {
664         struct shell_surface *shsurf = container_of(listener,
665                                                     struct shell_surface,
666                                                     surface_destroy_listener);
667
668         shsurf->surface = NULL;
669         wl_resource_destroy(&shsurf->resource, time);
670 }
671
672 static struct shell_surface *
673 get_shell_surface(struct weston_surface *surface)
674 {
675         struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
676         struct wl_listener *listener;
677
678         /* search the destroy listener list for our callback */
679         wl_list_for_each(listener, lst, link) {
680                 if (listener->func == shell_handle_surface_destroy) {
681                         return container_of(listener, struct shell_surface,
682                                             surface_destroy_listener);
683                 }
684         }
685
686         return NULL;
687 }
688
689 static void
690 shell_get_shell_surface(struct wl_client *client,
691                         struct wl_resource *resource,
692                         uint32_t id,
693                         struct wl_resource *surface_resource)
694 {
695         struct weston_surface *surface = surface_resource->data;
696         struct shell_surface *shsurf;
697
698         if (get_shell_surface(surface)) {
699                 wl_resource_post_error(surface_resource,
700                         WL_DISPLAY_ERROR_INVALID_OBJECT,
701                         "wl_shell::get_shell_surface already requested");
702                 return;
703         }
704
705         shsurf = calloc(1, sizeof *shsurf);
706         if (!shsurf) {
707                 wl_resource_post_no_memory(resource);
708                 return;
709         }
710
711         shsurf->resource.destroy = destroy_shell_surface;
712         shsurf->resource.object.id = id;
713         shsurf->resource.object.interface = &wl_shell_surface_interface;
714         shsurf->resource.object.implementation =
715                 (void (**)(void)) &shell_surface_implementation;
716         shsurf->resource.data = shsurf;
717
718         shsurf->surface = surface;
719         shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
720         wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
721                        &shsurf->surface_destroy_listener.link);
722
723         /* init link so its safe to always remove it in destroy_shell_surface */
724         wl_list_init(&shsurf->link);
725
726         /* empty when not in use */
727         wl_list_init(&shsurf->rotation.transform.link);
728         weston_matrix_init(&shsurf->rotation.rotation);
729
730         shsurf->type = SHELL_SURFACE_NONE;
731
732         wl_client_add_resource(client, &shsurf->resource);
733 }
734
735 static const struct wl_shell_interface shell_implementation = {
736         shell_get_shell_surface
737 };
738
739 static void
740 handle_screensaver_sigchld(struct weston_process *proc, int status)
741 {
742         proc->pid = 0;
743 }
744
745 static void
746 launch_screensaver(struct wl_shell *shell)
747 {
748         if (shell->screensaver.binding)
749                 return;
750
751         if (!shell->screensaver.path)
752                 return;
753
754         weston_client_launch(shell->compositor,
755                            &shell->screensaver.process,
756                            shell->screensaver.path,
757                            handle_screensaver_sigchld);
758 }
759
760 static void
761 terminate_screensaver(struct wl_shell *shell)
762 {
763         if (shell->screensaver.process.pid == 0)
764                 return;
765
766         kill(shell->screensaver.process.pid, SIGTERM);
767 }
768
769 static void
770 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
771 {
772         struct wl_list *list;
773
774         if (shell->lock_surface)
775                 list = &shell->lock_surface->surface->link;
776         else
777                 list = &shell->compositor->surface_list;
778
779         wl_list_remove(&surface->surface->link);
780         wl_list_insert(list, &surface->surface->link);
781         surface->surface->output = surface->output;
782         weston_surface_damage(surface->surface);
783 }
784
785 static void
786 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
787 {
788         wl_list_remove(&surface->surface->link);
789         wl_list_init(&surface->surface->link);
790         surface->surface->output = NULL;
791 }
792
793 static void
794 desktop_shell_set_background(struct wl_client *client,
795                              struct wl_resource *resource,
796                              struct wl_resource *output_resource,
797                              struct wl_resource *surface_resource)
798 {
799         struct wl_shell *shell = resource->data;
800         struct shell_surface *shsurf = surface_resource->data;
801         struct weston_surface *surface = shsurf->surface;
802         struct shell_surface *priv;
803
804         if (reset_shell_surface_type(shsurf))
805                 return;
806
807         wl_list_for_each(priv, &shell->backgrounds, link) {
808                 if (priv->output == output_resource->data) {
809                         priv->surface->output = NULL;
810                         wl_list_remove(&priv->surface->link);
811                         wl_list_remove(&priv->link);
812                         break;
813                 }
814         }
815
816         shsurf->type = SHELL_SURFACE_BACKGROUND;
817         shsurf->output = output_resource->data;
818
819         wl_list_insert(&shell->backgrounds, &shsurf->link);
820
821         weston_surface_set_position(surface, shsurf->output->x,
822                                     shsurf->output->y);
823
824         wl_resource_post_event(resource,
825                                DESKTOP_SHELL_CONFIGURE,
826                                weston_compositor_get_time(), 0, surface_resource,
827                                shsurf->output->current->width,
828                                shsurf->output->current->height);
829 }
830
831 static void
832 desktop_shell_set_panel(struct wl_client *client,
833                         struct wl_resource *resource,
834                         struct wl_resource *output_resource,
835                         struct wl_resource *surface_resource)
836 {
837         struct wl_shell *shell = resource->data;
838         struct shell_surface *shsurf = surface_resource->data;
839         struct weston_surface *surface = shsurf->surface;
840         struct shell_surface *priv;
841
842         if (reset_shell_surface_type(shsurf))
843                 return;
844
845         wl_list_for_each(priv, &shell->panels, link) {
846                 if (priv->output == output_resource->data) {
847                         priv->surface->output = NULL;
848                         wl_list_remove(&priv->surface->link);
849                         wl_list_remove(&priv->link);
850                         break;
851                 }
852         }
853
854         shsurf->type = SHELL_SURFACE_PANEL;
855         shsurf->output = output_resource->data;
856
857         wl_list_insert(&shell->panels, &shsurf->link);
858
859         weston_surface_set_position(surface, shsurf->output->x,
860                                     shsurf->output->y);
861
862         wl_resource_post_event(resource,
863                                DESKTOP_SHELL_CONFIGURE,
864                                weston_compositor_get_time(), 0, surface_resource,
865                                shsurf->output->current->width,
866                                shsurf->output->current->height);
867 }
868
869 static void
870 handle_lock_surface_destroy(struct wl_listener *listener,
871                             struct wl_resource *resource, uint32_t time)
872 {
873         struct wl_shell *shell =
874                 container_of(listener, struct wl_shell, lock_surface_listener);
875
876         fprintf(stderr, "lock surface gone\n");
877         shell->lock_surface = NULL;
878 }
879
880 static void
881 desktop_shell_set_lock_surface(struct wl_client *client,
882                                struct wl_resource *resource,
883                                struct wl_resource *surface_resource)
884 {
885         struct wl_shell *shell = resource->data;
886         struct shell_surface *surface = surface_resource->data;
887
888         if (reset_shell_surface_type(surface))
889                 return;
890
891         shell->prepare_event_sent = false;
892
893         if (!shell->locked)
894                 return;
895
896         shell->lock_surface = surface;
897
898         shell->lock_surface_listener.func = handle_lock_surface_destroy;
899         wl_list_insert(&surface_resource->destroy_listener_list,
900                        &shell->lock_surface_listener.link);
901
902         shell->lock_surface->type = SHELL_SURFACE_LOCK;
903 }
904
905 static void
906 resume_desktop(struct wl_shell *shell)
907 {
908         struct weston_surface *surface;
909         struct wl_list *list;
910         struct shell_surface *tmp;
911
912         wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
913                 hide_screensaver(shell, tmp);
914
915         terminate_screensaver(shell);
916
917         wl_list_for_each(surface, &shell->hidden_surface_list, link)
918                 weston_surface_assign_output(surface);
919
920         if (wl_list_empty(&shell->backgrounds)) {
921                 list = &shell->compositor->surface_list;
922         } else {
923                 struct shell_surface *background;
924                 background = container_of(shell->backgrounds.prev,
925                                           struct shell_surface, link);
926                 list = background->surface->link.prev;
927         }
928
929         if (!wl_list_empty(&shell->hidden_surface_list))
930                 wl_list_insert_list(list, &shell->hidden_surface_list);
931         wl_list_init(&shell->hidden_surface_list);
932
933         shell->locked = false;
934         weston_compositor_repick(shell->compositor);
935         shell->compositor->idle_time = shell->compositor->option_idle_time;
936         weston_compositor_wake(shell->compositor);
937         weston_compositor_damage_all(shell->compositor);
938 }
939
940 static void
941 desktop_shell_unlock(struct wl_client *client,
942                      struct wl_resource *resource)
943 {
944         struct wl_shell *shell = resource->data;
945
946         shell->prepare_event_sent = false;
947
948         if (shell->locked)
949                 resume_desktop(shell);
950 }
951
952 static const struct desktop_shell_interface desktop_shell_implementation = {
953         desktop_shell_set_background,
954         desktop_shell_set_panel,
955         desktop_shell_set_lock_surface,
956         desktop_shell_unlock
957 };
958
959 static enum shell_surface_type
960 get_shell_surface_type(struct weston_surface *surface)
961 {
962         struct shell_surface *shsurf;
963
964         shsurf = get_shell_surface(surface);
965         if (!shsurf)
966                 return SHELL_SURFACE_NONE;
967         return shsurf->type;
968 }
969
970 static void
971 move_binding(struct wl_input_device *device, uint32_t time,
972              uint32_t key, uint32_t button, uint32_t state, void *data)
973 {
974         struct weston_surface *surface =
975                 (struct weston_surface *) device->pointer_focus;
976
977         if (surface == NULL)
978                 return;
979
980         switch (get_shell_surface_type(surface)) {
981                 case SHELL_SURFACE_PANEL:
982                 case SHELL_SURFACE_BACKGROUND:
983                 case SHELL_SURFACE_FULLSCREEN:
984                 case SHELL_SURFACE_SCREENSAVER:
985                         return;
986                 default:
987                         break;
988         }
989
990         weston_surface_move(surface, (struct weston_input_device *) device, time);
991 }
992
993 static void
994 resize_binding(struct wl_input_device *device, uint32_t time,
995                uint32_t key, uint32_t button, uint32_t state, void *data)
996 {
997         struct weston_surface *surface =
998                 (struct weston_surface *) device->pointer_focus;
999         uint32_t edges = 0;
1000         int32_t x, y;
1001         struct shell_surface *shsurf;
1002
1003         if (surface == NULL)
1004                 return;
1005
1006         shsurf = get_shell_surface(surface);
1007         if (!shsurf)
1008                 return;
1009
1010         switch (shsurf->type) {
1011                 case SHELL_SURFACE_PANEL:
1012                 case SHELL_SURFACE_BACKGROUND:
1013                 case SHELL_SURFACE_FULLSCREEN:
1014                 case SHELL_SURFACE_SCREENSAVER:
1015                         return;
1016                 default:
1017                         break;
1018         }
1019
1020         weston_surface_from_global(surface,
1021                                    device->grab_x, device->grab_y, &x, &y);
1022
1023         if (x < surface->geometry.width / 3)
1024                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1025         else if (x < 2 * surface->geometry.width / 3)
1026                 edges |= 0;
1027         else
1028                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1029
1030         if (y < surface->geometry.height / 3)
1031                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1032         else if (y < 2 * surface->geometry.height / 3)
1033                 edges |= 0;
1034         else
1035                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1036
1037         weston_surface_resize(shsurf, (struct weston_input_device *) device,
1038                             time, edges);
1039 }
1040
1041 static void
1042 zoom_grab_key(struct wl_keyboard_grab *grab,
1043                 uint32_t time, uint32_t key, int32_t state)
1044 {
1045         struct wl_input_device *device = grab->input_device;
1046         struct weston_input_device *wd = (struct weston_input_device *) device;
1047         struct weston_compositor *compositor = wd->compositor;
1048         struct weston_output *output;
1049         struct weston_zoom_grab *zoom;
1050
1051         if (!(wd->modifier_state & MODIFIER_SUPER)) {
1052                 zoom = container_of(grab, struct weston_zoom_grab, grab);
1053                 wl_input_device_end_keyboard_grab(device, time);
1054                 free(zoom);
1055                 return;
1056         }
1057
1058         wl_list_for_each(output, &compositor->output_list, link) {
1059                 if (pixman_region32_contains_point(&output->region,
1060                                                 device->x, device->y, NULL)) {
1061                         if (state && key == KEY_UP) {
1062                                 output->zoom.active = 1;
1063                                 output->zoom.level -= output->zoom.increment;
1064                         }
1065                         if (state && key == KEY_DOWN)
1066                                 output->zoom.level += output->zoom.increment;
1067
1068                         if (output->zoom.level >= 1.0) {
1069                                 output->zoom.active = 0;
1070                                 output->zoom.level = 1.0;
1071                         }
1072
1073                         if (output->zoom.level < output->zoom.increment)
1074                                 output->zoom.level = output->zoom.increment;
1075
1076                         weston_output_update_zoom(output, device->x, device->y);
1077                 }
1078         }
1079 }
1080
1081 static const struct wl_keyboard_grab_interface zoom_grab = {
1082         zoom_grab_key,
1083 };
1084
1085 static void
1086 zoom_binding(struct wl_input_device *device, uint32_t time,
1087                uint32_t key, uint32_t button, uint32_t state, void *data)
1088 {
1089         struct weston_input_device *wd = (struct weston_input_device *) device;
1090         struct weston_zoom_grab *zoom;
1091
1092         zoom = malloc(sizeof *zoom);
1093         if (!zoom)
1094                 return;
1095
1096         zoom->grab.interface = &zoom_grab;
1097
1098         wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
1099 }
1100
1101 static void
1102 terminate_binding(struct wl_input_device *device, uint32_t time,
1103                   uint32_t key, uint32_t button, uint32_t state, void *data)
1104 {
1105         struct weston_compositor *compositor = data;
1106
1107         if (state)
1108                 wl_display_terminate(compositor->wl_display);
1109 }
1110
1111 static void
1112 rotate_grab_motion(struct wl_pointer_grab *grab,
1113                  uint32_t time, int32_t x, int32_t y)
1114 {
1115         struct rotate_grab *rotate =
1116                 container_of(grab, struct rotate_grab, grab);
1117         struct wl_input_device *device = grab->input_device;
1118         struct shell_surface *surface = rotate->surface;
1119         GLfloat cx = 0.5f * surface->surface->geometry.width;
1120         GLfloat cy = 0.5f * surface->surface->geometry.height;
1121         GLfloat dx, dy;
1122         GLfloat r;
1123
1124         dx = device->x - rotate->center.x;
1125         dy = device->y - rotate->center.y;
1126         r = sqrtf(dx * dx + dy * dy);
1127
1128         wl_list_remove(&surface->rotation.transform.link);
1129         surface->surface->geometry.dirty = 1;
1130
1131         if (r > 20.0f) {
1132                 struct weston_matrix *matrix =
1133                         &surface->rotation.transform.matrix;
1134
1135                 weston_matrix_init(&rotate->rotation);
1136                 rotate->rotation.d[0] = dx / r;
1137                 rotate->rotation.d[4] = -dy / r;
1138                 rotate->rotation.d[1] = -rotate->rotation.d[4];
1139                 rotate->rotation.d[5] = rotate->rotation.d[0];
1140
1141                 weston_matrix_init(matrix);
1142                 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1143                 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1144                 weston_matrix_multiply(matrix, &rotate->rotation);
1145                 weston_matrix_translate(matrix, cx, cy, 0.0f);
1146
1147                 wl_list_insert(
1148                         &surface->surface->geometry.transformation_list,
1149                         &surface->rotation.transform.link);
1150         } else {
1151                 wl_list_init(&surface->rotation.transform.link);
1152                 weston_matrix_init(&surface->rotation.rotation);
1153                 weston_matrix_init(&rotate->rotation);
1154         }
1155
1156         /* Repaint implies weston_surface_update_transform(), which
1157          * lazily applies the damage due to rotation update.
1158          */
1159         weston_compositor_schedule_repaint(surface->surface->compositor);
1160 }
1161
1162 static void
1163 rotate_grab_button(struct wl_pointer_grab *grab,
1164                  uint32_t time, int32_t button, int32_t state)
1165 {
1166         struct rotate_grab *rotate =
1167                 container_of(grab, struct rotate_grab, grab);
1168         struct wl_input_device *device = grab->input_device;
1169         struct shell_surface *surface = rotate->surface;
1170
1171         if (device->button_count == 0 && state == 0) {
1172                 weston_matrix_multiply(&surface->rotation.rotation,
1173                                        &rotate->rotation);
1174                 wl_input_device_end_pointer_grab(device, time);
1175                 free(rotate);
1176         }
1177 }
1178
1179 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1180         noop_grab_focus,
1181         rotate_grab_motion,
1182         rotate_grab_button,
1183 };
1184
1185 static void
1186 rotate_binding(struct wl_input_device *device, uint32_t time,
1187                uint32_t key, uint32_t button, uint32_t state, void *data)
1188 {
1189         struct weston_surface *base_surface =
1190                 (struct weston_surface *) device->pointer_focus;
1191         struct shell_surface *surface;
1192         struct rotate_grab *rotate;
1193         GLfloat dx, dy;
1194         GLfloat r;
1195
1196         if (base_surface == NULL)
1197                 return;
1198
1199         surface = get_shell_surface(base_surface);
1200         if (!surface)
1201                 return;
1202
1203         switch (surface->type) {
1204                 case SHELL_SURFACE_PANEL:
1205                 case SHELL_SURFACE_BACKGROUND:
1206                 case SHELL_SURFACE_FULLSCREEN:
1207                 case SHELL_SURFACE_SCREENSAVER:
1208                         return;
1209                 default:
1210                         break;
1211         }
1212
1213         rotate = malloc(sizeof *rotate);
1214         if (!rotate)
1215                 return;
1216
1217         rotate->grab.interface = &rotate_grab_interface;
1218         rotate->surface = surface;
1219
1220         weston_surface_to_global(surface->surface,
1221                                  surface->surface->geometry.width / 2,
1222                                  surface->surface->geometry.height / 2,
1223                                  &rotate->center.x, &rotate->center.y);
1224
1225         wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1226
1227         dx = device->x - rotate->center.x;
1228         dy = device->y - rotate->center.y;
1229         r = sqrtf(dx * dx + dy * dy);
1230         if (r > 20.0f) {
1231                 struct weston_matrix inverse;
1232
1233                 weston_matrix_init(&inverse);
1234                 inverse.d[0] = dx / r;
1235                 inverse.d[4] = dy / r;
1236                 inverse.d[1] = -inverse.d[4];
1237                 inverse.d[5] = inverse.d[0];
1238                 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1239         } else {
1240                 weston_matrix_init(&surface->rotation.rotation);
1241                 weston_matrix_init(&rotate->rotation);
1242         }
1243
1244         wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1245 }
1246
1247 static void
1248 activate(struct weston_shell *base, struct weston_surface *es,
1249          struct weston_input_device *device, uint32_t time)
1250 {
1251         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1252         struct weston_compositor *compositor = shell->compositor;
1253         struct wl_list *list;
1254
1255         weston_surface_activate(es, device, time);
1256
1257         if (compositor->wxs)
1258                 weston_xserver_surface_activate(es);
1259
1260         switch (get_shell_surface_type(es)) {
1261         case SHELL_SURFACE_BACKGROUND:
1262                 /* put background back to bottom */
1263                 wl_list_remove(&es->link);
1264                 wl_list_insert(compositor->surface_list.prev, &es->link);
1265                 break;
1266         case SHELL_SURFACE_PANEL:
1267                 /* already put on top */
1268                 break;
1269         case SHELL_SURFACE_SCREENSAVER:
1270                 /* always below lock surface */
1271                 if (shell->lock_surface) {
1272                         wl_list_remove(&es->link);
1273                         wl_list_insert(&shell->lock_surface->surface->link,
1274                                        &es->link);
1275                 }
1276                 break;
1277         default:
1278                 if (!shell->locked) {
1279                         list = weston_compositor_top(compositor);
1280
1281                         /* bring panel back to top */
1282                         struct shell_surface *panel;
1283                         wl_list_for_each(panel, &shell->panels, link) {
1284                                 wl_list_remove(&panel->surface->link);
1285                                 wl_list_insert(list, &panel->surface->link);
1286                         }
1287                 }
1288         }
1289 }
1290
1291 static void
1292 click_to_activate_binding(struct wl_input_device *device,
1293                          uint32_t time, uint32_t key,
1294                           uint32_t button, uint32_t state, void *data)
1295 {
1296         struct weston_input_device *wd = (struct weston_input_device *) device;
1297         struct weston_compositor *compositor = data;
1298         struct weston_surface *focus;
1299
1300         focus = (struct weston_surface *) device->pointer_focus;
1301         if (state && focus && device->pointer_grab == &device->default_pointer_grab)
1302                 activate(compositor->shell, focus, wd, time);
1303 }
1304
1305 static void
1306 lock(struct weston_shell *base)
1307 {
1308         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1309         struct wl_list *surface_list = &shell->compositor->surface_list;
1310         struct weston_surface *cur;
1311         struct weston_surface *tmp;
1312         struct weston_input_device *device;
1313         struct shell_surface *shsurf;
1314         struct weston_output *output;
1315         uint32_t time;
1316
1317         if (shell->locked) {
1318                 wl_list_for_each(output, &shell->compositor->output_list, link)
1319                         /* TODO: find a way to jump to other DPMS levels */
1320                         if (output->set_dpms)
1321                                 output->set_dpms(output, WESTON_DPMS_STANDBY);
1322                 return;
1323         }
1324
1325         shell->locked = true;
1326
1327         /* Move all surfaces from compositor's list to our hidden list,
1328          * except the background. This way nothing else can show or
1329          * receive input events while we are locked. */
1330
1331         if (!wl_list_empty(&shell->hidden_surface_list)) {
1332                 fprintf(stderr,
1333                 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1334                                                                 __func__);
1335         }
1336
1337         wl_list_for_each_safe(cur, tmp, surface_list, link) {
1338                 /* skip input device sprites, cur->surface is uninitialised */
1339                 if (cur->surface.resource.client == NULL)
1340                         continue;
1341
1342                 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
1343                         continue;
1344
1345                 cur->output = NULL;
1346                 wl_list_remove(&cur->link);
1347                 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
1348         }
1349
1350         launch_screensaver(shell);
1351
1352         wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1353                 show_screensaver(shell, shsurf);
1354
1355         if (!wl_list_empty(&shell->screensaver.surfaces)) {
1356                 shell->compositor->idle_time = shell->screensaver.duration;
1357                 weston_compositor_wake(shell->compositor);
1358                 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1359         }
1360
1361         /* reset pointer foci */
1362         weston_compositor_repick(shell->compositor);
1363
1364         /* reset keyboard foci */
1365         time = weston_compositor_get_time();
1366         wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1367                 wl_input_device_set_keyboard_focus(&device->input_device,
1368                                                    NULL, time);
1369         }
1370
1371         /* TODO: disable bindings that should not work while locked. */
1372
1373         /* All this must be undone in resume_desktop(). */
1374 }
1375
1376 static void
1377 unlock(struct weston_shell *base)
1378 {
1379         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1380
1381         if (!shell->locked || shell->lock_surface) {
1382                 weston_compositor_wake(shell->compositor);
1383                 return;
1384         }
1385
1386         /* If desktop-shell client has gone away, unlock immediately. */
1387         if (!shell->child.desktop_shell) {
1388                 resume_desktop(shell);
1389                 return;
1390         }
1391
1392         if (shell->prepare_event_sent)
1393                 return;
1394
1395         wl_resource_post_event(shell->child.desktop_shell,
1396                                DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1397         shell->prepare_event_sent = true;
1398 }
1399
1400 static void
1401 center_on_output(struct weston_surface *surface, struct weston_output *output)
1402 {
1403         struct weston_mode *mode = output->current;
1404         GLfloat x = (mode->width - surface->geometry.width) / 2;
1405         GLfloat y = (mode->height - surface->geometry.height) / 2;
1406
1407         weston_surface_set_position(surface, output->x + x, output->y + y);
1408 }
1409
1410 static void
1411 map(struct weston_shell *base, struct weston_surface *surface,
1412     int32_t width, int32_t height, int32_t sx, int32_t sy)
1413 {
1414         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1415         struct weston_compositor *compositor = shell->compositor;
1416         struct wl_list *list;
1417         struct shell_surface *shsurf;
1418         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1419         int do_configure;
1420         int panel_height = 0;
1421
1422         shsurf = get_shell_surface(surface);
1423         if (shsurf)
1424                 surface_type = shsurf->type;
1425
1426         if (shell->locked) {
1427                 list = &shell->hidden_surface_list;
1428                 do_configure = 0;
1429         } else {
1430                 list = weston_compositor_top(compositor);
1431                 do_configure = 1;
1432         }
1433
1434         surface->geometry.width = width;
1435         surface->geometry.height = height;
1436         surface->geometry.dirty = 1;
1437
1438         weston_compositor_update_drag_surfaces(compositor);
1439
1440         /* initial positioning, see also configure() */
1441         switch (surface_type) {
1442         case SHELL_SURFACE_TOPLEVEL:
1443                 weston_surface_set_position(surface, 10 + random() % 400,
1444                                             10 + random() % 400);
1445                 break;
1446         case SHELL_SURFACE_SCREENSAVER:
1447         case SHELL_SURFACE_FULLSCREEN:
1448                 center_on_output(surface, shsurf->fullscreen_output);
1449                 break;
1450         case SHELL_SURFACE_MAXIMIZED:
1451                 /*use surface configure to set the geometry*/
1452                 panel_height = get_output_panel_height(shell,surface->output);
1453                 weston_surface_set_position(surface, surface->output->x,
1454                                             surface->output->y + panel_height);
1455                 break;
1456         case SHELL_SURFACE_LOCK:
1457                 center_on_output(surface, get_default_output(compositor));
1458                 break;
1459         case SHELL_SURFACE_POPUP:
1460                 shell_map_popup(shsurf, shsurf->popup.time);
1461         case SHELL_SURFACE_NONE:
1462                 weston_surface_set_position(surface,
1463                                             surface->geometry.x + sx,
1464                                             surface->geometry.y + sy);
1465                 break;
1466         default:
1467                 ;
1468         }
1469
1470         /* surface stacking order, see also activate() */
1471         switch (surface_type) {
1472         case SHELL_SURFACE_BACKGROUND:
1473                 /* background always visible, at the bottom */
1474                 wl_list_insert(compositor->surface_list.prev, &surface->link);
1475                 do_configure = 1;
1476                 break;
1477         case SHELL_SURFACE_PANEL:
1478                 /* panel always on top, hidden while locked */
1479                 wl_list_insert(list, &surface->link);
1480                 break;
1481         case SHELL_SURFACE_LOCK:
1482                 /* lock surface always visible, on top */
1483                 wl_list_insert(&compositor->surface_list, &surface->link);
1484
1485                 weston_compositor_wake(compositor);
1486                 do_configure = 1;
1487                 break;
1488         case SHELL_SURFACE_SCREENSAVER:
1489                 /* If locked, show it. */
1490                 if (shell->locked) {
1491                         show_screensaver(shell, shsurf);
1492                         compositor->idle_time = shell->screensaver.duration;
1493                         weston_compositor_wake(compositor);
1494                         if (!shell->lock_surface)
1495                                 compositor->state = WESTON_COMPOSITOR_IDLE;
1496                 }
1497                 do_configure = 0;
1498                 break;
1499         case SHELL_SURFACE_NONE:
1500                 do_configure = 0;
1501                 break;
1502         default:
1503                 /* everything else just below the panel */
1504                 if (!wl_list_empty(&shell->panels)) {
1505                         struct shell_surface *panel =
1506                                 container_of(shell->panels.prev,
1507                                              struct shell_surface, link);
1508                         wl_list_insert(&panel->surface->link, &surface->link);
1509                 } else {
1510                         wl_list_insert(list, &surface->link);
1511                 }
1512         }
1513
1514         if (do_configure) {
1515                 weston_surface_assign_output(surface);
1516                 weston_compositor_repick(compositor);
1517                 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1518                         surface->output = shsurf->output;
1519         }
1520
1521         switch (surface_type) {
1522         case SHELL_SURFACE_TOPLEVEL:
1523         case SHELL_SURFACE_TRANSIENT:
1524         case SHELL_SURFACE_FULLSCREEN:
1525         case SHELL_SURFACE_MAXIMIZED:
1526                 if (!shell->locked)
1527                         activate(base, surface,
1528                                  (struct weston_input_device *)
1529                                         compositor->input_device,
1530                                  weston_compositor_get_time());
1531                 break;
1532         default:
1533                 break;
1534         }
1535
1536         if (surface_type == SHELL_SURFACE_TOPLEVEL)
1537                 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1538 }
1539
1540 static void
1541 configure(struct weston_shell *base, struct weston_surface *surface,
1542           GLfloat x, GLfloat y, int32_t width, int32_t height)
1543 {
1544         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1545         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1546         struct shell_surface *shsurf;
1547
1548         shsurf = get_shell_surface(surface);
1549         if (shsurf)
1550                 surface_type = shsurf->type;
1551
1552         surface->geometry.x = x;
1553         surface->geometry.y = y;
1554         surface->geometry.width = width;
1555         surface->geometry.height = height;
1556         surface->geometry.dirty = 1;
1557
1558         switch (surface_type) {
1559         case SHELL_SURFACE_SCREENSAVER:
1560         case SHELL_SURFACE_FULLSCREEN:
1561                 center_on_output(surface, shsurf->fullscreen_output);
1562                 break;
1563         case SHELL_SURFACE_MAXIMIZED:
1564                 /*setting x, y and using configure to change that geometry*/
1565                 surface->geometry.x = surface->output->x;
1566                 surface->geometry.y = surface->output->y +
1567                         get_output_panel_height(shell,surface->output);
1568                 break;
1569         default:
1570                 break;
1571         }
1572
1573         /*  XXX: would a fullscreen surface need the same handling? */
1574         if (surface->output) {
1575                 weston_surface_assign_output(surface);
1576
1577                 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1578                         surface->output = shsurf->output;
1579                 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1580                         surface->output = shsurf->output;
1581         }
1582 }
1583
1584 static int launch_desktop_shell_process(struct wl_shell *shell);
1585
1586 static void
1587 desktop_shell_sigchld(struct weston_process *process, int status)
1588 {
1589         uint32_t time;
1590         struct wl_shell *shell =
1591                 container_of(process, struct wl_shell, child.process);
1592
1593         shell->child.process.pid = 0;
1594         shell->child.client = NULL; /* already destroyed by wayland */
1595
1596         /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1597         time = weston_compositor_get_time();
1598         if (time - shell->child.deathstamp > 30000) {
1599                 shell->child.deathstamp = time;
1600                 shell->child.deathcount = 0;
1601         }
1602
1603         shell->child.deathcount++;
1604         if (shell->child.deathcount > 5) {
1605                 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1606                 return;
1607         }
1608
1609         fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1610         launch_desktop_shell_process(shell);
1611 }
1612
1613 static int
1614 launch_desktop_shell_process(struct wl_shell *shell)
1615 {
1616         const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1617
1618         shell->child.client = weston_client_launch(shell->compositor,
1619                                                  &shell->child.process,
1620                                                  shell_exe,
1621                                                  desktop_shell_sigchld);
1622
1623         if (!shell->child.client)
1624                 return -1;
1625         return 0;
1626 }
1627
1628 static void
1629 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1630 {
1631         struct wl_shell *shell = data;
1632
1633         wl_client_add_object(client, &wl_shell_interface,
1634                              &shell_implementation, id, shell);
1635 }
1636
1637 static void
1638 unbind_desktop_shell(struct wl_resource *resource)
1639 {
1640         struct wl_shell *shell = resource->data;
1641
1642         if (shell->locked)
1643                 resume_desktop(shell);
1644
1645         shell->child.desktop_shell = NULL;
1646         shell->prepare_event_sent = false;
1647         free(resource);
1648 }
1649
1650 static void
1651 bind_desktop_shell(struct wl_client *client,
1652                    void *data, uint32_t version, uint32_t id)
1653 {
1654         struct wl_shell *shell = data;
1655         struct wl_resource *resource;
1656
1657         resource = wl_client_add_object(client, &desktop_shell_interface,
1658                                         &desktop_shell_implementation,
1659                                         id, shell);
1660
1661         if (client == shell->child.client) {
1662                 resource->destroy = unbind_desktop_shell;
1663                 shell->child.desktop_shell = resource;
1664                 return;
1665         }
1666
1667         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1668                                "permission to bind desktop_shell denied");
1669         wl_resource_destroy(resource, 0);
1670 }
1671
1672 static void
1673 screensaver_set_surface(struct wl_client *client,
1674                         struct wl_resource *resource,
1675                         struct wl_resource *shell_surface_resource,
1676                         struct wl_resource *output_resource)
1677 {
1678         struct wl_shell *shell = resource->data;
1679         struct shell_surface *surface = shell_surface_resource->data;
1680         struct weston_output *output = output_resource->data;
1681
1682         if (reset_shell_surface_type(surface))
1683                 return;
1684
1685         surface->type = SHELL_SURFACE_SCREENSAVER;
1686
1687         surface->fullscreen_output = output;
1688         surface->output = output;
1689         wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1690 }
1691
1692 static const struct screensaver_interface screensaver_implementation = {
1693         screensaver_set_surface
1694 };
1695
1696 static void
1697 unbind_screensaver(struct wl_resource *resource)
1698 {
1699         struct wl_shell *shell = resource->data;
1700
1701         shell->screensaver.binding = NULL;
1702         free(resource);
1703 }
1704
1705 static void
1706 bind_screensaver(struct wl_client *client,
1707                  void *data, uint32_t version, uint32_t id)
1708 {
1709         struct wl_shell *shell = data;
1710         struct wl_resource *resource;
1711
1712         resource = wl_client_add_object(client, &screensaver_interface,
1713                                         &screensaver_implementation,
1714                                         id, shell);
1715
1716         if (shell->screensaver.binding == NULL) {
1717                 resource->destroy = unbind_screensaver;
1718                 shell->screensaver.binding = resource;
1719                 return;
1720         }
1721
1722         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1723                                "interface object already bound");
1724         wl_resource_destroy(resource, 0);
1725 }
1726
1727 struct switcher {
1728         struct weston_compositor *compositor;
1729         struct weston_surface *current;
1730         struct wl_listener listener;
1731         struct wl_keyboard_grab grab;
1732 };
1733
1734 static void
1735 switcher_next(struct switcher *switcher)
1736 {
1737         struct weston_compositor *compositor = switcher->compositor;
1738         struct weston_surface *surface;
1739         struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1740
1741         wl_list_for_each(surface, &compositor->surface_list, link) {
1742                 /* Workaround for cursor surfaces. */
1743                 if (surface->surface.resource.destroy_listener_list.next == NULL)
1744                         continue;
1745
1746                 switch (get_shell_surface_type(surface)) {
1747                 case SHELL_SURFACE_TOPLEVEL:
1748                 case SHELL_SURFACE_FULLSCREEN:
1749                 case SHELL_SURFACE_MAXIMIZED:
1750                         if (first == NULL)
1751                                 first = surface;
1752                         if (prev == switcher->current)
1753                                 next = surface;
1754                         prev = surface;
1755                         surface->alpha = 64;
1756                         surface->geometry.dirty = 1;
1757                         weston_surface_damage(surface);
1758                         break;
1759                 default:
1760                         break;
1761                 }
1762         }
1763
1764         if (next == NULL)
1765                 next = first;
1766
1767         wl_list_remove(&switcher->listener.link);
1768         wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1769                        &switcher->listener.link);
1770
1771         switcher->current = next;
1772         next->alpha = 255;
1773 }
1774
1775 static void
1776 switcher_handle_surface_destroy(struct wl_listener *listener,
1777                                 struct wl_resource *resource, uint32_t time)
1778 {
1779         struct switcher *switcher =
1780                 container_of(listener, struct switcher, listener);
1781
1782         switcher_next(switcher);
1783 }
1784
1785 static void
1786 switcher_destroy(struct switcher *switcher, uint32_t time)
1787 {
1788         struct weston_compositor *compositor = switcher->compositor;
1789         struct weston_surface *surface;
1790         struct weston_input_device *device =
1791                 (struct weston_input_device *) switcher->grab.input_device;
1792
1793         wl_list_for_each(surface, &compositor->surface_list, link) {
1794                 surface->alpha = 255;
1795                 weston_surface_damage(surface);
1796         }
1797
1798         activate(compositor->shell, switcher->current, device, time);
1799         wl_list_remove(&switcher->listener.link);
1800         wl_input_device_end_keyboard_grab(&device->input_device, time);
1801         free(switcher);
1802 }
1803
1804 static void
1805 switcher_key(struct wl_keyboard_grab *grab,
1806              uint32_t time, uint32_t key, int32_t state)
1807 {
1808         struct switcher *switcher = container_of(grab, struct switcher, grab);
1809         struct weston_input_device *device =
1810                 (struct weston_input_device *) grab->input_device;
1811
1812         if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1813                 switcher_destroy(switcher, time);
1814         } else if (key == KEY_TAB && state) {
1815                 switcher_next(switcher);
1816         }
1817 };
1818
1819 static const struct wl_keyboard_grab_interface switcher_grab = {
1820         switcher_key
1821 };
1822
1823 static void
1824 switcher_binding(struct wl_input_device *device, uint32_t time,
1825                  uint32_t key, uint32_t button,
1826                  uint32_t state, void *data)
1827 {
1828         struct weston_compositor *compositor = data;
1829         struct switcher *switcher;
1830
1831         switcher = malloc(sizeof *switcher);
1832         switcher->compositor = compositor;
1833         switcher->current = NULL;
1834         switcher->listener.func = switcher_handle_surface_destroy;
1835         wl_list_init(&switcher->listener.link);
1836
1837         switcher->grab.interface = &switcher_grab;
1838         wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1839         switcher_next(switcher);
1840 }
1841
1842 static void
1843 backlight_binding(struct wl_input_device *device, uint32_t time,
1844                   uint32_t key, uint32_t button, uint32_t state, void *data)
1845 {
1846         struct weston_compositor *compositor = data;
1847         struct weston_output *output;
1848
1849         /* TODO: we're limiting to simple use cases, where we assume just
1850          * control on the primary display. We'd have to extend later if we
1851          * ever get support for setting backlights on random desktop LCD
1852          * panels though */
1853         output = get_default_output(compositor);
1854         if (!output)
1855                 return;
1856
1857         if (!output->set_backlight)
1858                 return;
1859
1860         if ((key == KEY_F9 || key == KEY_BRIGHTNESSDOWN) &&
1861             output->backlight_current > 1)
1862                 output->backlight_current--;
1863         else if ((key == KEY_F10 || key == KEY_BRIGHTNESSUP) &&
1864             output->backlight_current < 10)
1865                 output->backlight_current++;
1866
1867         output->set_backlight(output, output->backlight_current);
1868 }
1869
1870 static void
1871 shell_destroy(struct weston_shell *base)
1872 {
1873         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1874
1875         if (shell->child.client)
1876                 wl_client_destroy(shell->child.client);
1877
1878         free(shell->screensaver.path);
1879         free(shell);
1880 }
1881
1882 int
1883 shell_init(struct weston_compositor *ec);
1884
1885 WL_EXPORT int
1886 shell_init(struct weston_compositor *ec)
1887 {
1888         struct wl_shell *shell;
1889
1890         shell = malloc(sizeof *shell);
1891         if (shell == NULL)
1892                 return -1;
1893
1894         memset(shell, 0, sizeof *shell);
1895         shell->compositor = ec;
1896         shell->shell.lock = lock;
1897         shell->shell.unlock = unlock;
1898         shell->shell.map = map;
1899         shell->shell.configure = configure;
1900         shell->shell.destroy = shell_destroy;
1901
1902         wl_list_init(&shell->hidden_surface_list);
1903         wl_list_init(&shell->backgrounds);
1904         wl_list_init(&shell->panels);
1905         wl_list_init(&shell->screensaver.surfaces);
1906
1907         shell_configuration(shell);
1908
1909         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1910                                   shell, bind_shell) == NULL)
1911                 return -1;
1912
1913         if (wl_display_add_global(ec->wl_display,
1914                                   &desktop_shell_interface,
1915                                   shell, bind_desktop_shell) == NULL)
1916                 return -1;
1917
1918         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1919                                   shell, bind_screensaver) == NULL)
1920                 return -1;
1921
1922         shell->child.deathstamp = weston_compositor_get_time();
1923         if (launch_desktop_shell_process(shell) != 0)
1924                 return -1;
1925
1926         weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
1927                                     move_binding, shell);
1928         weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
1929                                     resize_binding, shell);
1930         weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1931                                     MODIFIER_CTRL | MODIFIER_ALT,
1932                                     terminate_binding, ec);
1933         weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
1934                                     click_to_activate_binding, ec);
1935         weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
1936                                     zoom_binding, shell);
1937         weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
1938                                     zoom_binding, shell);
1939         weston_compositor_add_binding(ec, 0, BTN_LEFT,
1940                                       MODIFIER_SUPER | MODIFIER_ALT,
1941                                       rotate_binding, NULL);
1942         weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
1943                                       switcher_binding, ec);
1944
1945         /* brightness */
1946         weston_compositor_add_binding(ec, KEY_F9, 0, MODIFIER_CTRL,
1947                                       backlight_binding, ec);
1948         weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0,
1949                                       backlight_binding, ec);
1950         weston_compositor_add_binding(ec, KEY_F10, 0, MODIFIER_CTRL,
1951                                       backlight_binding, ec);
1952         weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0,
1953                                       backlight_binding, ec);
1954
1955         ec->shell = &shell->shell;
1956
1957         return 0;
1958 }