x11: Remove destroy listener when X11 window is destroyed
[platform/upstream/weston.git] / compositor / shell.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2011 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
33 #include <wayland-server.h>
34 #include "compositor.h"
35 #include "desktop-shell-server-protocol.h"
36 #include "../shared/config-parser.h"
37
38 struct shell_surface;
39
40 struct wl_shell {
41         struct wlsc_compositor *compositor;
42         struct wlsc_shell shell;
43
44         struct {
45                 struct wlsc_process process;
46                 struct wl_client *client;
47                 struct wl_resource *desktop_shell;
48         } child;
49
50         bool locked;
51         bool prepare_event_sent;
52
53         struct shell_surface *lock_surface;
54         struct wl_listener lock_surface_listener;
55         struct wl_list hidden_surface_list;
56
57         struct wl_list backgrounds;
58         struct wl_list panels;
59
60         struct {
61                 const char *path;
62                 int duration;
63                 struct wl_resource *binding;
64                 struct wl_list surfaces;
65                 struct wlsc_process process;
66         } screensaver;
67 };
68
69 enum shell_surface_type {
70         SHELL_SURFACE_NONE,
71
72         SHELL_SURFACE_PANEL,
73         SHELL_SURFACE_BACKGROUND,
74         SHELL_SURFACE_LOCK,
75         SHELL_SURFACE_SCREENSAVER,
76
77         SHELL_SURFACE_TOPLEVEL,
78         SHELL_SURFACE_TRANSIENT,
79         SHELL_SURFACE_FULLSCREEN
80 };
81
82 struct shell_surface {
83         struct wl_resource resource;
84
85         struct wlsc_surface *surface;
86         struct wl_listener surface_destroy_listener;
87
88         enum shell_surface_type type;
89         int32_t saved_x, saved_y;
90
91         struct wlsc_output *output;
92         struct wl_list link;
93 };
94
95 struct wlsc_move_grab {
96         struct wl_grab grab;
97         struct wlsc_surface *surface;
98         int32_t dx, dy;
99 };
100
101 static int
102 shell_configuration(struct wl_shell *shell)
103 {
104         int ret;
105         char *config_file;
106         char *path = NULL;
107         int duration = 60;
108
109         struct config_key saver_keys[] = {
110                 { "path",       CONFIG_KEY_STRING,  &path },
111                 { "duration",   CONFIG_KEY_INTEGER, &duration },
112         };
113
114         struct config_section cs[] = {
115                 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
116         };
117
118         config_file = config_file_path("wayland-desktop-shell.ini");
119         ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
120         free(config_file);
121
122         shell->screensaver.path = path;
123         shell->screensaver.duration = duration;
124
125         return ret;
126         /* FIXME: free(shell->screensaver.path) on plugin fini */
127 }
128
129 static void
130 move_grab_motion(struct wl_grab *grab,
131                    uint32_t time, int32_t x, int32_t y)
132 {
133         struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
134         struct wlsc_surface *es = move->surface;
135
136         wlsc_surface_configure(es, x + move->dx, y + move->dy,
137                                es->width, es->height);
138 }
139
140 static void
141 move_grab_button(struct wl_grab *grab,
142                  uint32_t time, int32_t button, int32_t state)
143 {
144 }
145
146 static void
147 move_grab_end(struct wl_grab *grab, uint32_t time)
148 {
149         free(grab);
150 }
151
152 static const struct wl_grab_interface move_grab_interface = {
153         move_grab_motion,
154         move_grab_button,
155         move_grab_end
156 };
157
158 static int
159 wlsc_surface_move(struct wlsc_surface *es,
160                   struct wlsc_input_device *wd, uint32_t time)
161 {
162         struct wlsc_move_grab *move;
163
164         move = malloc(sizeof *move);
165         if (!move)
166                 return -1;
167
168         move->grab.interface = &move_grab_interface;
169         move->dx = es->x - wd->input_device.grab_x;
170         move->dy = es->y - wd->input_device.grab_y;
171         move->surface = es;
172
173         if (wl_input_device_update_grab(&wd->input_device,
174                                         &move->grab, &es->surface, time) < 0)
175                 return 0;
176
177         wl_input_device_set_pointer_focus(&wd->input_device,
178                                           NULL, time, 0, 0, 0, 0);
179
180         return 0;
181 }
182
183 static void
184 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
185                    struct wl_resource *input_resource, uint32_t time)
186 {
187         struct wlsc_input_device *wd = input_resource->data;
188         struct shell_surface *shsurf = resource->data;
189
190         if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
191                 wl_resource_post_no_memory(resource);
192 }
193
194 struct wlsc_resize_grab {
195         struct wl_grab grab;
196         uint32_t edges;
197         int32_t dx, dy, width, height;
198         struct shell_surface *shsurf;
199 };
200
201 static void
202 resize_grab_motion(struct wl_grab *grab,
203                    uint32_t time, int32_t x, int32_t y)
204 {
205         struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
206         struct wl_input_device *device = grab->input_device;
207         int32_t width, height;
208
209         if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
210                 width = device->grab_x - x + resize->width;
211         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
212                 width = x - device->grab_x + resize->width;
213         } else {
214                 width = resize->width;
215         }
216
217         if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
218                 height = device->grab_y - y + resize->height;
219         } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
220                 height = y - device->grab_y + resize->height;
221         } else {
222                 height = resize->height;
223         }
224
225         wl_resource_post_event(&resize->shsurf->resource,
226                                WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
227                                width, height);
228 }
229
230 static void
231 resize_grab_button(struct wl_grab *grab,
232                    uint32_t time, int32_t button, int32_t state)
233 {
234 }
235
236 static void
237 resize_grab_end(struct wl_grab *grab, uint32_t time)
238 {
239         free(grab);
240 }
241
242 static const struct wl_grab_interface resize_grab_interface = {
243         resize_grab_motion,
244         resize_grab_button,
245         resize_grab_end
246 };
247
248 static int
249 wlsc_surface_resize(struct shell_surface *shsurf,
250                     struct wlsc_input_device *wd,
251                     uint32_t time, uint32_t edges)
252 {
253         struct wlsc_resize_grab *resize;
254         struct wlsc_surface *es = shsurf->surface;
255
256         /* FIXME: Reject if fullscreen */
257
258         resize = malloc(sizeof *resize);
259         if (!resize)
260                 return -1;
261
262         resize->grab.interface = &resize_grab_interface;
263         resize->edges = edges;
264         resize->dx = es->x - wd->input_device.grab_x;
265         resize->dy = es->y - wd->input_device.grab_y;
266         resize->width = es->width;
267         resize->height = es->height;
268         resize->shsurf = shsurf;
269
270         if (edges == 0 || edges > 15 ||
271             (edges & 3) == 3 || (edges & 12) == 12)
272                 return 0;
273
274         if (wl_input_device_update_grab(&wd->input_device,
275                                         &resize->grab, &es->surface, time) < 0)
276                 return 0;
277
278         wl_input_device_set_pointer_focus(&wd->input_device,
279                                           NULL, time, 0, 0, 0, 0);
280
281         return 0;
282 }
283
284 static void
285 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
286                      struct wl_resource *input_resource, uint32_t time,
287                      uint32_t edges)
288 {
289         struct wlsc_input_device *wd = input_resource->data;
290         struct shell_surface *shsurf = resource->data;
291
292         /* FIXME: Reject if fullscreen */
293
294         if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
295                 wl_resource_post_no_memory(resource);
296 }
297
298 static int
299 reset_shell_surface_type(struct shell_surface *surface)
300 {
301         switch (surface->type) {
302         case SHELL_SURFACE_FULLSCREEN:
303                 surface->surface->x = surface->saved_x;
304                 surface->surface->y = surface->saved_y;
305                 surface->surface->fullscreen_output = NULL;
306                 break;
307         case SHELL_SURFACE_PANEL:
308         case SHELL_SURFACE_BACKGROUND:
309                 wl_list_remove(&surface->link);
310                 wl_list_init(&surface->link);
311                 break;
312         case SHELL_SURFACE_SCREENSAVER:
313         case SHELL_SURFACE_LOCK:
314                 wl_resource_post_error(&surface->resource,
315                                        WL_DISPLAY_ERROR_INVALID_METHOD,
316                                        "cannot reassign surface type");
317                 return -1;
318         case SHELL_SURFACE_NONE:
319         case SHELL_SURFACE_TOPLEVEL:
320         case SHELL_SURFACE_TRANSIENT:
321                 break;
322         }
323
324         surface->type = SHELL_SURFACE_NONE;
325         return 0;
326 }
327
328 static void
329 shell_surface_set_toplevel(struct wl_client *client,
330                            struct wl_resource *resource)
331
332 {
333         struct shell_surface *surface = resource->data;
334
335         if (reset_shell_surface_type(surface))
336                 return;
337
338         wlsc_surface_damage(surface->surface);
339         surface->type = SHELL_SURFACE_TOPLEVEL;
340 }
341
342 static void
343 shell_surface_set_transient(struct wl_client *client,
344                             struct wl_resource *resource,
345                             struct wl_resource *parent_resource,
346                             int x, int y, uint32_t flags)
347 {
348         struct shell_surface *shsurf = resource->data;
349         struct wlsc_surface *es = shsurf->surface;
350         struct shell_surface *pshsurf = parent_resource->data;
351         struct wlsc_surface *pes = pshsurf->surface;
352
353         if (reset_shell_surface_type(shsurf))
354                 return;
355
356         /* assign to parents output  */
357         es->output = pes->output;
358  
359         es->x = pes->x + x;
360         es->y = pes->y + y;
361
362         wlsc_surface_damage(es);
363         shsurf->type = SHELL_SURFACE_TRANSIENT;
364 }
365
366 static struct wlsc_output *
367 get_default_output(struct wlsc_compositor *compositor)
368 {
369         return container_of(compositor->output_list.next,
370                             struct wlsc_output, link);
371 }
372
373 static void
374 shell_surface_set_fullscreen(struct wl_client *client,
375                              struct wl_resource *resource)
376
377 {
378         struct shell_surface *shsurf = resource->data;
379         struct wlsc_surface *es = shsurf->surface;
380         struct wlsc_output *output;
381
382         if (reset_shell_surface_type(shsurf))
383                 return;
384
385         /* FIXME: Fullscreen on first output */
386         /* FIXME: Handle output going away */
387         output = get_default_output(es->compositor);
388         es->output = output;
389
390         shsurf->saved_x = es->x;
391         shsurf->saved_y = es->y;
392         es->x = (output->current->width - es->width) / 2;
393         es->y = (output->current->height - es->height) / 2;
394         es->fullscreen_output = output;
395         wlsc_surface_damage(es);
396         shsurf->type = SHELL_SURFACE_FULLSCREEN;
397 }
398
399 static const struct wl_shell_surface_interface shell_surface_implementation = {
400         shell_surface_move,
401         shell_surface_resize,
402         shell_surface_set_toplevel,
403         shell_surface_set_transient,
404         shell_surface_set_fullscreen
405 };
406
407 static void
408 destroy_shell_surface(struct wl_resource *resource)
409 {
410         struct shell_surface *shsurf = resource->data;
411
412         /* in case cleaning up a dead client destroys shell_surface first */
413         if (shsurf->surface)
414                 wl_list_remove(&shsurf->surface_destroy_listener.link);
415
416         wl_list_remove(&shsurf->link);
417         free(shsurf);
418 }
419
420 static void
421 shell_handle_surface_destroy(struct wl_listener *listener,
422                              struct wl_resource *resource, uint32_t time)
423 {
424         struct shell_surface *shsurf = container_of(listener,
425                                                     struct shell_surface,
426                                                     surface_destroy_listener);
427
428         shsurf->surface = NULL;
429         wl_resource_destroy(&shsurf->resource, time);
430 }
431
432 static struct shell_surface *
433 get_shell_surface(struct wlsc_surface *surface)
434 {
435         struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
436         struct wl_listener *listener;
437
438         /* search the destroy listener list for our callback */
439         wl_list_for_each(listener, lst, link) {
440                 if (listener->func == shell_handle_surface_destroy) {
441                         return container_of(listener, struct shell_surface,
442                                             surface_destroy_listener);
443                 }
444         }
445
446         return NULL;
447 }
448
449 static void
450 shell_get_shell_surface(struct wl_client *client,
451                         struct wl_resource *resource,
452                         uint32_t id,
453                         struct wl_resource *surface_resource)
454 {
455         struct wlsc_surface *surface = surface_resource->data;
456         struct shell_surface *shsurf;
457
458         if (get_shell_surface(surface)) {
459                 wl_resource_post_error(surface_resource,
460                         WL_DISPLAY_ERROR_INVALID_OBJECT,
461                         "wl_shell::get_shell_surface already requested");
462                 return;
463         }
464
465         shsurf = calloc(1, sizeof *shsurf);
466         if (!shsurf) {
467                 wl_resource_post_no_memory(resource);
468                 return;
469         }
470
471         shsurf->resource.destroy = destroy_shell_surface;
472         shsurf->resource.object.id = id;
473         shsurf->resource.object.interface = &wl_shell_surface_interface;
474         shsurf->resource.object.implementation =
475                 (void (**)(void)) &shell_surface_implementation;
476         shsurf->resource.data = shsurf;
477
478         shsurf->surface = surface;
479         shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
480         wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
481                        &shsurf->surface_destroy_listener.link);
482
483         /* init link so its safe to always remove it in destroy_shell_surface */
484         wl_list_init(&shsurf->link);
485
486         shsurf->type = SHELL_SURFACE_NONE;
487
488         wl_client_add_resource(client, &shsurf->resource);
489 }
490
491 static const struct wl_shell_interface shell_implementation = {
492         shell_get_shell_surface
493 };
494
495 static void
496 handle_screensaver_sigchld(struct wlsc_process *proc, int status)
497 {
498         proc->pid = 0;
499 }
500
501 static void
502 launch_screensaver(struct wl_shell *shell)
503 {
504         if (shell->screensaver.binding)
505                 return;
506
507         if (!shell->screensaver.path)
508                 return;
509
510         wlsc_client_launch(shell->compositor,
511                            &shell->screensaver.process,
512                            shell->screensaver.path,
513                            handle_screensaver_sigchld);
514 }
515
516 static void
517 terminate_screensaver(struct wl_shell *shell)
518 {
519         if (shell->screensaver.process.pid == 0)
520                 return;
521
522         kill(shell->screensaver.process.pid, SIGTERM);
523 }
524
525 static void
526 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
527 {
528         struct wl_list *list;
529
530         if (shell->lock_surface)
531                 list = &shell->lock_surface->surface->link;
532         else
533                 list = &shell->compositor->surface_list;
534
535         wl_list_remove(&surface->surface->link);
536         wl_list_insert(list, &surface->surface->link);
537         wlsc_surface_configure(surface->surface,
538                                surface->surface->x,
539                                surface->surface->y,
540                                surface->surface->width,
541                                surface->surface->height);
542         surface->surface->output = surface->output;
543 }
544
545 static void
546 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
547 {
548         wl_list_remove(&surface->surface->link);
549         wl_list_init(&surface->surface->link);
550         surface->surface->output = NULL;
551 }
552
553 static void
554 desktop_shell_set_background(struct wl_client *client,
555                              struct wl_resource *resource,
556                              struct wl_resource *output_resource,
557                              struct wl_resource *surface_resource)
558 {
559         struct wl_shell *shell = resource->data;
560         struct shell_surface *shsurf = surface_resource->data;
561         struct wlsc_surface *surface = shsurf->surface;
562         struct shell_surface *priv;
563
564         if (reset_shell_surface_type(shsurf))
565                 return;
566
567         wl_list_for_each(priv, &shell->backgrounds, link) {
568                 if (priv->output == output_resource->data) {
569                         priv->surface->output = NULL;
570                         wl_list_remove(&priv->surface->link);
571                         wl_list_remove(&priv->link);
572                         break;
573                 }
574         }
575
576         shsurf->type = SHELL_SURFACE_BACKGROUND;
577         shsurf->output = output_resource->data;
578
579         wl_list_insert(&shell->backgrounds, &shsurf->link);
580
581         surface->x = shsurf->output->x;
582         surface->y = shsurf->output->y;
583
584         wl_resource_post_event(resource,
585                                DESKTOP_SHELL_CONFIGURE,
586                                wlsc_compositor_get_time(), 0, surface_resource,
587                                shsurf->output->current->width,
588                                shsurf->output->current->height);
589 }
590
591 static void
592 desktop_shell_set_panel(struct wl_client *client,
593                         struct wl_resource *resource,
594                         struct wl_resource *output_resource,
595                         struct wl_resource *surface_resource)
596 {
597         struct wl_shell *shell = resource->data;
598         struct shell_surface *shsurf = surface_resource->data;
599         struct wlsc_surface *surface = shsurf->surface;
600         struct shell_surface *priv;
601
602         if (reset_shell_surface_type(shsurf))
603                 return;
604
605         wl_list_for_each(priv, &shell->panels, link) {
606                 if (priv->output == output_resource->data) {
607                         priv->surface->output = NULL;
608                         wl_list_remove(&priv->surface->link);
609                         wl_list_remove(&priv->link);
610                         break;
611                 }
612         }
613
614         shsurf->type = SHELL_SURFACE_PANEL;
615         shsurf->output = output_resource->data;
616
617         wl_list_insert(&shell->panels, &shsurf->link);
618
619         surface->x = shsurf->output->x;
620         surface->y = shsurf->output->y;
621
622         wl_resource_post_event(resource,
623                                DESKTOP_SHELL_CONFIGURE,
624                                wlsc_compositor_get_time(), 0, surface_resource,
625                                shsurf->output->current->width,
626                                shsurf->output->current->height);
627 }
628
629 static void
630 handle_lock_surface_destroy(struct wl_listener *listener,
631                             struct wl_resource *resource, uint32_t time)
632 {
633         struct wl_shell *shell =
634                 container_of(listener, struct wl_shell, lock_surface_listener);
635
636         fprintf(stderr, "lock surface gone\n");
637         shell->lock_surface = NULL;
638 }
639
640 static void
641 desktop_shell_set_lock_surface(struct wl_client *client,
642                                struct wl_resource *resource,
643                                struct wl_resource *surface_resource)
644 {
645         struct wl_shell *shell = resource->data;
646         struct shell_surface *surface = surface_resource->data;
647
648         if (reset_shell_surface_type(surface))
649                 return;
650
651         shell->prepare_event_sent = false;
652
653         if (!shell->locked)
654                 return;
655
656         shell->lock_surface = surface;
657
658         shell->lock_surface_listener.func = handle_lock_surface_destroy;
659         wl_list_insert(&surface_resource->destroy_listener_list,
660                        &shell->lock_surface_listener.link);
661
662         shell->lock_surface->type = SHELL_SURFACE_LOCK;
663 }
664
665 static void
666 resume_desktop(struct wl_shell *shell)
667 {
668         struct wlsc_surface *surface;
669         struct wl_list *list;
670         struct shell_surface *tmp;
671
672         wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
673                 hide_screensaver(shell, tmp);
674
675         terminate_screensaver(shell);
676
677         wl_list_for_each(surface, &shell->hidden_surface_list, link)
678                 wlsc_surface_configure(surface, surface->x, surface->y,
679                                        surface->width, surface->height);
680
681         if (wl_list_empty(&shell->backgrounds)) {
682                 list = &shell->compositor->surface_list;
683         } else {
684                 struct shell_surface *background;
685                 background = container_of(shell->backgrounds.prev,
686                                           struct shell_surface, link);
687                 list = background->surface->link.prev;
688         }
689
690         if (!wl_list_empty(&shell->hidden_surface_list))
691                 wl_list_insert_list(list, &shell->hidden_surface_list);
692         wl_list_init(&shell->hidden_surface_list);
693
694         shell->locked = false;
695         wlsc_compositor_repick(shell->compositor);
696         shell->compositor->idle_time = shell->compositor->option_idle_time;
697         wlsc_compositor_wake(shell->compositor);
698 }
699
700 static void
701 desktop_shell_unlock(struct wl_client *client,
702                      struct wl_resource *resource)
703 {
704         struct wl_shell *shell = resource->data;
705
706         shell->prepare_event_sent = false;
707
708         if (shell->locked)
709                 resume_desktop(shell);
710 }
711
712 static const struct desktop_shell_interface desktop_shell_implementation = {
713         desktop_shell_set_background,
714         desktop_shell_set_panel,
715         desktop_shell_set_lock_surface,
716         desktop_shell_unlock
717 };
718
719 static enum shell_surface_type
720 get_shell_surface_type(struct wlsc_surface *surface)
721 {
722         struct shell_surface *shsurf;
723
724         shsurf = get_shell_surface(surface);
725         if (!shsurf)
726                 return SHELL_SURFACE_NONE;
727         return shsurf->type;
728 }
729
730 static void
731 move_binding(struct wl_input_device *device, uint32_t time,
732              uint32_t key, uint32_t button, uint32_t state, void *data)
733 {
734         struct wlsc_surface *surface =
735                 (struct wlsc_surface *) device->pointer_focus;
736
737         if (surface == NULL)
738                 return;
739
740         switch (get_shell_surface_type(surface)) {
741                 case SHELL_SURFACE_PANEL:
742                 case SHELL_SURFACE_BACKGROUND:
743                 case SHELL_SURFACE_FULLSCREEN:
744                 case SHELL_SURFACE_SCREENSAVER:
745                         return;
746                 default:
747                         break;
748         }
749
750         wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
751 }
752
753 static void
754 resize_binding(struct wl_input_device *device, uint32_t time,
755                uint32_t key, uint32_t button, uint32_t state, void *data)
756 {
757         struct wlsc_surface *surface =
758                 (struct wlsc_surface *) device->pointer_focus;
759         uint32_t edges = 0;
760         int32_t x, y;
761         struct shell_surface *shsurf;
762
763         if (surface == NULL)
764                 return;
765
766         shsurf = get_shell_surface(surface);
767         if (!shsurf)
768                 return;
769
770         switch (shsurf->type) {
771                 case SHELL_SURFACE_PANEL:
772                 case SHELL_SURFACE_BACKGROUND:
773                 case SHELL_SURFACE_FULLSCREEN:
774                 case SHELL_SURFACE_SCREENSAVER:
775                         return;
776                 default:
777                         break;
778         }
779
780         x = device->grab_x - surface->x;
781         y = device->grab_y - surface->y;
782
783         if (x < surface->width / 3)
784                 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
785         else if (x < 2 * surface->width / 3)
786                 edges |= 0;
787         else
788                 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
789
790         if (y < surface->height / 3)
791                 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
792         else if (y < 2 * surface->height / 3)
793                 edges |= 0;
794         else
795                 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
796
797         wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
798                             time, edges);
799 }
800
801 static void
802 terminate_binding(struct wl_input_device *device, uint32_t time,
803                   uint32_t key, uint32_t button, uint32_t state, void *data)
804 {
805         struct wlsc_compositor *compositor = data;
806
807         if (state)
808                 wl_display_terminate(compositor->wl_display);
809 }
810
811 static void
812 activate(struct wlsc_shell *base, struct wlsc_surface *es,
813          struct wlsc_input_device *device, uint32_t time)
814 {
815         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
816         struct wlsc_compositor *compositor = shell->compositor;
817
818         wlsc_surface_activate(es, device, time);
819
820         if (compositor->wxs)
821                 wlsc_xserver_surface_activate(es);
822
823         switch (get_shell_surface_type(es)) {
824         case SHELL_SURFACE_BACKGROUND:
825                 /* put background back to bottom */
826                 wl_list_remove(&es->link);
827                 wl_list_insert(compositor->surface_list.prev, &es->link);
828                 break;
829         case SHELL_SURFACE_PANEL:
830                 /* already put on top */
831                 break;
832         case SHELL_SURFACE_SCREENSAVER:
833                 /* always below lock surface */
834                 if (shell->lock_surface) {
835                         wl_list_remove(&es->link);
836                         wl_list_insert(&shell->lock_surface->surface->link,
837                                        &es->link);
838                 }
839                 break;
840         default:
841                 if (!shell->locked) {
842                         /* bring panel back to top */
843                         struct shell_surface *panel;
844                         wl_list_for_each(panel, &shell->panels, link) {
845                                 wl_list_remove(&panel->surface->link);
846                                 wl_list_insert(&compositor->surface_list,
847                                                &panel->surface->link);
848                         }
849                 }
850         }
851 }
852
853 static void
854 click_to_activate_binding(struct wl_input_device *device,
855                          uint32_t time, uint32_t key,
856                           uint32_t button, uint32_t state, void *data)
857 {
858         struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
859         struct wlsc_compositor *compositor = data;
860         struct wlsc_surface *focus;
861
862         focus = (struct wlsc_surface *) device->pointer_focus;
863         if (state && focus && device->grab == NULL)
864                 activate(compositor->shell, focus, wd, time);
865 }
866
867 static void
868 lock(struct wlsc_shell *base)
869 {
870         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
871         struct wl_list *surface_list = &shell->compositor->surface_list;
872         struct wlsc_surface *cur;
873         struct wlsc_surface *tmp;
874         struct wlsc_input_device *device;
875         struct shell_surface *shsurf;
876         uint32_t time;
877
878         if (shell->locked)
879                 return;
880
881         shell->locked = true;
882
883         /* Move all surfaces from compositor's list to our hidden list,
884          * except the background. This way nothing else can show or
885          * receive input events while we are locked. */
886
887         if (!wl_list_empty(&shell->hidden_surface_list)) {
888                 fprintf(stderr,
889                 "%s: Assertion failed: hidden_surface_list is not empty.\n",
890                                                                 __func__);
891         }
892
893         wl_list_for_each_safe(cur, tmp, surface_list, link) {
894                 /* skip input device sprites, cur->surface is uninitialised */
895                 if (cur->surface.resource.client == NULL)
896                         continue;
897
898                 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
899                         continue;
900
901                 cur->output = NULL;
902                 wl_list_remove(&cur->link);
903                 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
904         }
905
906         launch_screensaver(shell);
907
908         wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
909                 show_screensaver(shell, shsurf);
910
911         if (!wl_list_empty(&shell->screensaver.surfaces)) {
912                 shell->compositor->idle_time = shell->screensaver.duration;
913                 wlsc_compositor_wake(shell->compositor);
914                 shell->compositor->state = WLSC_COMPOSITOR_IDLE;
915         }
916
917         /* reset pointer foci */
918         wlsc_compositor_repick(shell->compositor);
919
920         /* reset keyboard foci */
921         time = wlsc_compositor_get_time();
922         wl_list_for_each(device, &shell->compositor->input_device_list, link) {
923                 wl_input_device_set_keyboard_focus(&device->input_device,
924                                                    NULL, time);
925         }
926
927         /* TODO: disable bindings that should not work while locked. */
928
929         /* All this must be undone in resume_desktop(). */
930 }
931
932 static void
933 unlock(struct wlsc_shell *base)
934 {
935         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
936
937         if (!shell->locked || shell->lock_surface) {
938                 wlsc_compositor_wake(shell->compositor);
939                 return;
940         }
941
942         /* If desktop-shell client has gone away, unlock immediately. */
943         if (!shell->child.desktop_shell) {
944                 resume_desktop(shell);
945                 return;
946         }
947
948         if (shell->prepare_event_sent)
949                 return;
950
951         wl_resource_post_event(shell->child.desktop_shell,
952                                DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
953         shell->prepare_event_sent = true;
954 }
955
956 static void
957 center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
958 {
959         struct wlsc_mode *mode = output->current;
960
961         surface->x = output->x + (mode->width - surface->width) / 2;
962         surface->y = output->y + (mode->height - surface->height) / 2;
963 }
964
965 static void
966 map(struct wlsc_shell *base,
967     struct wlsc_surface *surface, int32_t width, int32_t height)
968 {
969         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
970         struct wlsc_compositor *compositor = shell->compositor;
971         struct wl_list *list;
972         struct shell_surface *shsurf;
973         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
974         int do_configure;
975
976         shsurf = get_shell_surface(surface);
977         if (shsurf)
978                 surface_type = shsurf->type;
979
980         if (shell->locked) {
981                 list = &shell->hidden_surface_list;
982                 do_configure = 0;
983         } else {
984                 list = &compositor->surface_list;
985                 do_configure = 1;
986         }
987
988         surface->width = width;
989         surface->height = height;
990
991         /* initial positioning, see also configure() */
992         switch (surface_type) {
993         case SHELL_SURFACE_TOPLEVEL:
994                 surface->x = 10 + random() % 400;
995                 surface->y = 10 + random() % 400;
996                 break;
997         case SHELL_SURFACE_SCREENSAVER:
998         case SHELL_SURFACE_FULLSCREEN:
999                 center_on_output(surface, surface->fullscreen_output);
1000                 break;
1001         case SHELL_SURFACE_LOCK:
1002                 center_on_output(surface, get_default_output(compositor));
1003                 break;
1004         default:
1005                 ;
1006         }
1007
1008         /* surface stacking order, see also activate() */
1009         switch (surface_type) {
1010         case SHELL_SURFACE_BACKGROUND:
1011                 /* background always visible, at the bottom */
1012                 wl_list_insert(compositor->surface_list.prev, &surface->link);
1013                 do_configure = 1;
1014                 break;
1015         case SHELL_SURFACE_PANEL:
1016                 /* panel always on top, hidden while locked */
1017                 wl_list_insert(list, &surface->link);
1018                 break;
1019         case SHELL_SURFACE_LOCK:
1020                 /* lock surface always visible, on top */
1021                 wl_list_insert(&compositor->surface_list, &surface->link);
1022
1023                 wlsc_compositor_repick(compositor);
1024                 wlsc_compositor_wake(compositor);
1025                 do_configure = 1;
1026                 break;
1027         case SHELL_SURFACE_SCREENSAVER:
1028                 /* If locked, show it. */
1029                 if (shell->locked) {
1030                         show_screensaver(shell, shsurf);
1031                         compositor->idle_time = shell->screensaver.duration;
1032                         wlsc_compositor_wake(compositor);
1033                         if (!shell->lock_surface)
1034                                 compositor->state = WLSC_COMPOSITOR_IDLE;
1035                 }
1036                 do_configure = 0;
1037                 break;
1038         default:
1039                 /* everything else just below the panel */
1040                 if (!wl_list_empty(&shell->panels)) {
1041                         struct shell_surface *panel =
1042                                 container_of(shell->panels.prev,
1043                                              struct shell_surface, link);
1044                         wl_list_insert(&panel->surface->link, &surface->link);
1045                 } else {
1046                         wl_list_insert(list, &surface->link);
1047                 }
1048         }
1049
1050         if (do_configure)
1051                 wlsc_surface_configure(surface,
1052                                        surface->x, surface->y, width, height);
1053
1054         switch (surface_type) {
1055         case SHELL_SURFACE_TOPLEVEL:
1056         case SHELL_SURFACE_TRANSIENT:
1057         case SHELL_SURFACE_FULLSCREEN:
1058                 if (!shell->locked)
1059                         activate(base, surface,
1060                                  (struct wlsc_input_device *)
1061                                         compositor->input_device,
1062                                  wlsc_compositor_get_time());
1063                 break;
1064         default:
1065                 break;
1066         }
1067
1068         if (surface_type == SHELL_SURFACE_TOPLEVEL)
1069                 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1070 }
1071
1072 static void
1073 configure(struct wlsc_shell *base, struct wlsc_surface *surface,
1074           int32_t x, int32_t y, int32_t width, int32_t height)
1075 {
1076         struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1077         int do_configure = !shell->locked;
1078         enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1079         struct shell_surface *shsurf;
1080
1081         shsurf = get_shell_surface(surface);
1082         if (shsurf)
1083                 surface_type = shsurf->type;
1084
1085         surface->width = width;
1086         surface->height = height;
1087
1088         switch (surface_type) {
1089         case SHELL_SURFACE_SCREENSAVER:
1090                 do_configure = !do_configure;
1091                 /* fall through */
1092         case SHELL_SURFACE_FULLSCREEN:
1093                 center_on_output(surface, surface->fullscreen_output);
1094                 break;
1095         default:
1096                 break;
1097         }
1098
1099         /*
1100          * wlsc_surface_configure() will assign an output, which means
1101          * the surface is supposed to be in compositor->surface_list.
1102          * Be careful with that, and make sure we stay on the right output.
1103          * XXX: would a fullscreen surface need the same handling?
1104          */
1105         if (do_configure) {
1106                 wlsc_surface_configure(surface, x, y, width, height);
1107
1108                 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1109                         surface->output = shsurf->output;
1110         }
1111 }
1112
1113 static void
1114 desktop_shell_sigchld(struct wlsc_process *process, int status)
1115 {
1116         struct wl_shell *shell =
1117                 container_of(process, struct wl_shell, child.process);
1118
1119         shell->child.process.pid = 0;
1120         shell->child.client = NULL; /* already destroyed by wayland */
1121 }
1122
1123 static int
1124 launch_desktop_shell_process(struct wl_shell *shell)
1125 {
1126         const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
1127
1128         shell->child.client = wlsc_client_launch(shell->compositor,
1129                                                  &shell->child.process,
1130                                                  shell_exe,
1131                                                  desktop_shell_sigchld);
1132
1133         if (!shell->child.client)
1134                 return -1;
1135         return 0;
1136 }
1137
1138 static void
1139 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1140 {
1141         struct wl_shell *shell = data;
1142
1143         wl_client_add_object(client, &wl_shell_interface,
1144                              &shell_implementation, id, shell);
1145 }
1146
1147 static void
1148 unbind_desktop_shell(struct wl_resource *resource)
1149 {
1150         struct wl_shell *shell = resource->data;
1151
1152         if (shell->locked)
1153                 resume_desktop(shell);
1154
1155         shell->child.desktop_shell = NULL;
1156         shell->prepare_event_sent = false;
1157         free(resource);
1158 }
1159
1160 static void
1161 bind_desktop_shell(struct wl_client *client,
1162                    void *data, uint32_t version, uint32_t id)
1163 {
1164         struct wl_shell *shell = data;
1165         struct wl_resource *resource;
1166
1167         resource = wl_client_add_object(client, &desktop_shell_interface,
1168                                         &desktop_shell_implementation,
1169                                         id, shell);
1170
1171         if (client == shell->child.client) {
1172                 resource->destroy = unbind_desktop_shell;
1173                 shell->child.desktop_shell = resource;
1174                 return;
1175         }
1176
1177         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1178                                "permission to bind desktop_shell denied");
1179         wl_resource_destroy(resource, 0);
1180 }
1181
1182 static void
1183 screensaver_set_surface(struct wl_client *client,
1184                         struct wl_resource *resource,
1185                         struct wl_resource *shell_surface_resource,
1186                         struct wl_resource *output_resource)
1187 {
1188         struct wl_shell *shell = resource->data;
1189         struct shell_surface *surface = shell_surface_resource->data;
1190         struct wlsc_output *output = output_resource->data;
1191
1192         if (reset_shell_surface_type(surface))
1193                 return;
1194
1195         surface->type = SHELL_SURFACE_SCREENSAVER;
1196
1197         surface->surface->fullscreen_output = output;
1198         surface->output = output;
1199         wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1200 }
1201
1202 static const struct screensaver_interface screensaver_implementation = {
1203         screensaver_set_surface
1204 };
1205
1206 static void
1207 unbind_screensaver(struct wl_resource *resource)
1208 {
1209         struct wl_shell *shell = resource->data;
1210
1211         shell->screensaver.binding = NULL;
1212         free(resource);
1213 }
1214
1215 static void
1216 bind_screensaver(struct wl_client *client,
1217                  void *data, uint32_t version, uint32_t id)
1218 {
1219         struct wl_shell *shell = data;
1220         struct wl_resource *resource;
1221
1222         resource = wl_client_add_object(client, &screensaver_interface,
1223                                         &screensaver_implementation,
1224                                         id, shell);
1225
1226         if (shell->screensaver.binding == NULL) {
1227                 resource->destroy = unbind_screensaver;
1228                 shell->screensaver.binding = resource;
1229                 return;
1230         }
1231
1232         wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1233                                "interface object already bound");
1234         wl_resource_destroy(resource, 0);
1235 }
1236
1237 int
1238 shell_init(struct wlsc_compositor *ec);
1239
1240 WL_EXPORT int
1241 shell_init(struct wlsc_compositor *ec)
1242 {
1243         struct wl_shell *shell;
1244
1245         shell = malloc(sizeof *shell);
1246         if (shell == NULL)
1247                 return -1;
1248
1249         memset(shell, 0, sizeof *shell);
1250         shell->compositor = ec;
1251         shell->shell.lock = lock;
1252         shell->shell.unlock = unlock;
1253         shell->shell.map = map;
1254         shell->shell.configure = configure;
1255
1256         wl_list_init(&shell->hidden_surface_list);
1257         wl_list_init(&shell->backgrounds);
1258         wl_list_init(&shell->panels);
1259         wl_list_init(&shell->screensaver.surfaces);
1260
1261         if (shell_configuration(shell) < 0)
1262                 return -1;
1263
1264         if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1265                                   shell, bind_shell) == NULL)
1266                 return -1;
1267
1268         if (wl_display_add_global(ec->wl_display,
1269                                   &desktop_shell_interface,
1270                                   shell, bind_desktop_shell) == NULL)
1271                 return -1;
1272
1273         if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1274                                   shell, bind_screensaver) == NULL)
1275                 return -1;
1276
1277         if (launch_desktop_shell_process(shell) != 0)
1278                 return -1;
1279
1280         wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
1281                                     move_binding, shell);
1282         wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
1283                                     resize_binding, shell);
1284         wlsc_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1285                                     MODIFIER_CTRL | MODIFIER_ALT,
1286                                     terminate_binding, ec);
1287         wlsc_compositor_add_binding(ec, 0, BTN_LEFT, 0,
1288                                     click_to_activate_binding, ec);
1289
1290
1291
1292
1293         ec->shell = &shell->shell;
1294
1295         return 0;
1296 }