2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2011 Collabora, Ltd.
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.
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.
29 #include <linux/input.h>
33 #include <wayland-server.h>
34 #include "compositor.h"
35 #include "desktop-shell-server-protocol.h"
36 #include "../shared/config-parser.h"
41 struct wlsc_compositor *compositor;
42 struct wlsc_shell shell;
45 struct wlsc_process process;
46 struct wl_client *client;
47 struct wl_resource *desktop_shell;
51 bool prepare_event_sent;
53 struct shell_surface *lock_surface;
54 struct wl_listener lock_surface_listener;
55 struct wl_list hidden_surface_list;
57 struct wl_list backgrounds;
58 struct wl_list panels;
63 struct wl_resource *binding;
64 struct wl_list surfaces;
65 struct wlsc_process process;
69 enum shell_surface_type {
73 SHELL_SURFACE_BACKGROUND,
75 SHELL_SURFACE_SCREENSAVER,
77 SHELL_SURFACE_TOPLEVEL,
78 SHELL_SURFACE_TRANSIENT,
79 SHELL_SURFACE_FULLSCREEN
82 struct shell_surface {
83 struct wl_resource resource;
85 struct wlsc_surface *surface;
86 struct wl_listener surface_destroy_listener;
88 enum shell_surface_type type;
89 int32_t saved_x, saved_y;
91 struct wlsc_output *output;
95 struct wlsc_move_grab {
97 struct wlsc_surface *surface;
102 shell_configuration(struct wl_shell *shell)
109 struct config_key saver_keys[] = {
110 { "path", CONFIG_KEY_STRING, &path },
111 { "duration", CONFIG_KEY_INTEGER, &duration },
114 struct config_section cs[] = {
115 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
118 config_file = config_file_path("wayland-desktop-shell.ini");
119 ret = parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
122 shell->screensaver.path = path;
123 shell->screensaver.duration = duration;
126 /* FIXME: free(shell->screensaver.path) on plugin fini */
130 move_grab_motion(struct wl_grab *grab,
131 uint32_t time, int32_t x, int32_t y)
133 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
134 struct wlsc_surface *es = move->surface;
136 wlsc_surface_configure(es, x + move->dx, y + move->dy,
137 es->width, es->height);
141 move_grab_button(struct wl_grab *grab,
142 uint32_t time, int32_t button, int32_t state)
147 move_grab_end(struct wl_grab *grab, uint32_t time)
152 static const struct wl_grab_interface move_grab_interface = {
159 wlsc_surface_move(struct wlsc_surface *es,
160 struct wlsc_input_device *wd, uint32_t time)
162 struct wlsc_move_grab *move;
164 move = malloc(sizeof *move);
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;
173 if (wl_input_device_update_grab(&wd->input_device,
174 &move->grab, &es->surface, time) < 0)
177 wl_input_device_set_pointer_focus(&wd->input_device,
178 NULL, time, 0, 0, 0, 0);
184 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
185 struct wl_resource *input_resource, uint32_t time)
187 struct wlsc_input_device *wd = input_resource->data;
188 struct shell_surface *shsurf = resource->data;
190 if (wlsc_surface_move(shsurf->surface, wd, time) < 0)
191 wl_resource_post_no_memory(resource);
194 struct wlsc_resize_grab {
197 int32_t dx, dy, width, height;
198 struct shell_surface *shsurf;
202 resize_grab_motion(struct wl_grab *grab,
203 uint32_t time, int32_t x, int32_t y)
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;
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;
214 width = resize->width;
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;
222 height = resize->height;
225 wl_resource_post_event(&resize->shsurf->resource,
226 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
231 resize_grab_button(struct wl_grab *grab,
232 uint32_t time, int32_t button, int32_t state)
237 resize_grab_end(struct wl_grab *grab, uint32_t time)
242 static const struct wl_grab_interface resize_grab_interface = {
249 wlsc_surface_resize(struct shell_surface *shsurf,
250 struct wlsc_input_device *wd,
251 uint32_t time, uint32_t edges)
253 struct wlsc_resize_grab *resize;
254 struct wlsc_surface *es = shsurf->surface;
256 /* FIXME: Reject if fullscreen */
258 resize = malloc(sizeof *resize);
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;
270 if (edges == 0 || edges > 15 ||
271 (edges & 3) == 3 || (edges & 12) == 12)
274 if (wl_input_device_update_grab(&wd->input_device,
275 &resize->grab, &es->surface, time) < 0)
278 wl_input_device_set_pointer_focus(&wd->input_device,
279 NULL, time, 0, 0, 0, 0);
285 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
286 struct wl_resource *input_resource, uint32_t time,
289 struct wlsc_input_device *wd = input_resource->data;
290 struct shell_surface *shsurf = resource->data;
292 /* FIXME: Reject if fullscreen */
294 if (wlsc_surface_resize(shsurf, wd, time, edges) < 0)
295 wl_resource_post_no_memory(resource);
299 reset_shell_surface_type(struct shell_surface *surface)
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;
307 case SHELL_SURFACE_PANEL:
308 case SHELL_SURFACE_BACKGROUND:
309 wl_list_remove(&surface->link);
310 wl_list_init(&surface->link);
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");
318 case SHELL_SURFACE_NONE:
319 case SHELL_SURFACE_TOPLEVEL:
320 case SHELL_SURFACE_TRANSIENT:
324 surface->type = SHELL_SURFACE_NONE;
329 shell_surface_set_toplevel(struct wl_client *client,
330 struct wl_resource *resource)
333 struct shell_surface *surface = resource->data;
335 if (reset_shell_surface_type(surface))
338 wlsc_surface_damage(surface->surface);
339 surface->type = SHELL_SURFACE_TOPLEVEL;
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)
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;
353 if (reset_shell_surface_type(shsurf))
356 /* assign to parents output */
357 es->output = pes->output;
362 wlsc_surface_damage(es);
363 shsurf->type = SHELL_SURFACE_TRANSIENT;
366 static struct wlsc_output *
367 get_default_output(struct wlsc_compositor *compositor)
369 return container_of(compositor->output_list.next,
370 struct wlsc_output, link);
374 shell_surface_set_fullscreen(struct wl_client *client,
375 struct wl_resource *resource)
378 struct shell_surface *shsurf = resource->data;
379 struct wlsc_surface *es = shsurf->surface;
380 struct wlsc_output *output;
382 if (reset_shell_surface_type(shsurf))
385 /* FIXME: Fullscreen on first output */
386 /* FIXME: Handle output going away */
387 output = get_default_output(es->compositor);
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;
399 static const struct wl_shell_surface_interface shell_surface_implementation = {
401 shell_surface_resize,
402 shell_surface_set_toplevel,
403 shell_surface_set_transient,
404 shell_surface_set_fullscreen
408 destroy_shell_surface(struct wl_resource *resource)
410 struct shell_surface *shsurf = resource->data;
412 /* in case cleaning up a dead client destroys shell_surface first */
414 wl_list_remove(&shsurf->surface_destroy_listener.link);
416 wl_list_remove(&shsurf->link);
421 shell_handle_surface_destroy(struct wl_listener *listener,
422 struct wl_resource *resource, uint32_t time)
424 struct shell_surface *shsurf = container_of(listener,
425 struct shell_surface,
426 surface_destroy_listener);
428 shsurf->surface = NULL;
429 wl_resource_destroy(&shsurf->resource, time);
432 static struct shell_surface *
433 get_shell_surface(struct wlsc_surface *surface)
435 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
436 struct wl_listener *listener;
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);
450 shell_get_shell_surface(struct wl_client *client,
451 struct wl_resource *resource,
453 struct wl_resource *surface_resource)
455 struct wlsc_surface *surface = surface_resource->data;
456 struct shell_surface *shsurf;
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");
465 shsurf = calloc(1, sizeof *shsurf);
467 wl_resource_post_no_memory(resource);
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;
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);
483 /* init link so its safe to always remove it in destroy_shell_surface */
484 wl_list_init(&shsurf->link);
486 shsurf->type = SHELL_SURFACE_NONE;
488 wl_client_add_resource(client, &shsurf->resource);
491 static const struct wl_shell_interface shell_implementation = {
492 shell_get_shell_surface
496 handle_screensaver_sigchld(struct wlsc_process *proc, int status)
502 launch_screensaver(struct wl_shell *shell)
504 if (shell->screensaver.binding)
507 if (!shell->screensaver.path)
510 wlsc_client_launch(shell->compositor,
511 &shell->screensaver.process,
512 shell->screensaver.path,
513 handle_screensaver_sigchld);
517 terminate_screensaver(struct wl_shell *shell)
519 if (shell->screensaver.process.pid == 0)
522 kill(shell->screensaver.process.pid, SIGTERM);
526 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
528 struct wl_list *list;
530 if (shell->lock_surface)
531 list = &shell->lock_surface->surface->link;
533 list = &shell->compositor->surface_list;
535 wl_list_remove(&surface->surface->link);
536 wl_list_insert(list, &surface->surface->link);
537 wlsc_surface_configure(surface->surface,
540 surface->surface->width,
541 surface->surface->height);
542 surface->surface->output = surface->output;
546 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
548 wl_list_remove(&surface->surface->link);
549 wl_list_init(&surface->surface->link);
550 surface->surface->output = NULL;
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)
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;
564 if (reset_shell_surface_type(shsurf))
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);
576 shsurf->type = SHELL_SURFACE_BACKGROUND;
577 shsurf->output = output_resource->data;
579 wl_list_insert(&shell->backgrounds, &shsurf->link);
581 surface->x = shsurf->output->x;
582 surface->y = shsurf->output->y;
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);
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)
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;
602 if (reset_shell_surface_type(shsurf))
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);
614 shsurf->type = SHELL_SURFACE_PANEL;
615 shsurf->output = output_resource->data;
617 wl_list_insert(&shell->panels, &shsurf->link);
619 surface->x = shsurf->output->x;
620 surface->y = shsurf->output->y;
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);
630 handle_lock_surface_destroy(struct wl_listener *listener,
631 struct wl_resource *resource, uint32_t time)
633 struct wl_shell *shell =
634 container_of(listener, struct wl_shell, lock_surface_listener);
636 fprintf(stderr, "lock surface gone\n");
637 shell->lock_surface = NULL;
641 desktop_shell_set_lock_surface(struct wl_client *client,
642 struct wl_resource *resource,
643 struct wl_resource *surface_resource)
645 struct wl_shell *shell = resource->data;
646 struct shell_surface *surface = surface_resource->data;
648 if (reset_shell_surface_type(surface))
651 shell->prepare_event_sent = false;
656 shell->lock_surface = surface;
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);
662 shell->lock_surface->type = SHELL_SURFACE_LOCK;
666 resume_desktop(struct wl_shell *shell)
668 struct wlsc_surface *surface;
669 struct wl_list *list;
670 struct shell_surface *tmp;
672 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
673 hide_screensaver(shell, tmp);
675 terminate_screensaver(shell);
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);
681 if (wl_list_empty(&shell->backgrounds)) {
682 list = &shell->compositor->surface_list;
684 struct shell_surface *background;
685 background = container_of(shell->backgrounds.prev,
686 struct shell_surface, link);
687 list = background->surface->link.prev;
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);
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);
701 desktop_shell_unlock(struct wl_client *client,
702 struct wl_resource *resource)
704 struct wl_shell *shell = resource->data;
706 shell->prepare_event_sent = false;
709 resume_desktop(shell);
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,
719 static enum shell_surface_type
720 get_shell_surface_type(struct wlsc_surface *surface)
722 struct shell_surface *shsurf;
724 shsurf = get_shell_surface(surface);
726 return SHELL_SURFACE_NONE;
731 move_binding(struct wl_input_device *device, uint32_t time,
732 uint32_t key, uint32_t button, uint32_t state, void *data)
734 struct wlsc_surface *surface =
735 (struct wlsc_surface *) device->pointer_focus;
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:
750 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
754 resize_binding(struct wl_input_device *device, uint32_t time,
755 uint32_t key, uint32_t button, uint32_t state, void *data)
757 struct wlsc_surface *surface =
758 (struct wlsc_surface *) device->pointer_focus;
761 struct shell_surface *shsurf;
766 shsurf = get_shell_surface(surface);
770 switch (shsurf->type) {
771 case SHELL_SURFACE_PANEL:
772 case SHELL_SURFACE_BACKGROUND:
773 case SHELL_SURFACE_FULLSCREEN:
774 case SHELL_SURFACE_SCREENSAVER:
780 x = device->grab_x - surface->x;
781 y = device->grab_y - surface->y;
783 if (x < surface->width / 3)
784 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
785 else if (x < 2 * surface->width / 3)
788 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
790 if (y < surface->height / 3)
791 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
792 else if (y < 2 * surface->height / 3)
795 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
797 wlsc_surface_resize(shsurf, (struct wlsc_input_device *) device,
802 terminate_binding(struct wl_input_device *device, uint32_t time,
803 uint32_t key, uint32_t button, uint32_t state, void *data)
805 struct wlsc_compositor *compositor = data;
808 wl_display_terminate(compositor->wl_display);
812 activate(struct wlsc_shell *base, struct wlsc_surface *es,
813 struct wlsc_input_device *device, uint32_t time)
815 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
816 struct wlsc_compositor *compositor = shell->compositor;
818 wlsc_surface_activate(es, device, time);
821 wlsc_xserver_surface_activate(es);
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);
829 case SHELL_SURFACE_PANEL:
830 /* already put on top */
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,
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);
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)
858 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
859 struct wlsc_compositor *compositor = data;
860 struct wlsc_surface *focus;
862 focus = (struct wlsc_surface *) device->pointer_focus;
863 if (state && focus && device->grab == NULL)
864 activate(compositor->shell, focus, wd, time);
868 lock(struct wlsc_shell *base)
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;
881 shell->locked = true;
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. */
887 if (!wl_list_empty(&shell->hidden_surface_list)) {
889 "%s: Assertion failed: hidden_surface_list is not empty.\n",
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)
898 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
902 wl_list_remove(&cur->link);
903 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
906 launch_screensaver(shell);
908 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
909 show_screensaver(shell, shsurf);
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;
917 /* reset pointer foci */
918 wlsc_compositor_repick(shell->compositor);
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,
927 /* TODO: disable bindings that should not work while locked. */
929 /* All this must be undone in resume_desktop(). */
933 unlock(struct wlsc_shell *base)
935 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
937 if (!shell->locked || shell->lock_surface) {
938 wlsc_compositor_wake(shell->compositor);
942 /* If desktop-shell client has gone away, unlock immediately. */
943 if (!shell->child.desktop_shell) {
944 resume_desktop(shell);
948 if (shell->prepare_event_sent)
951 wl_resource_post_event(shell->child.desktop_shell,
952 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
953 shell->prepare_event_sent = true;
957 center_on_output(struct wlsc_surface *surface, struct wlsc_output *output)
959 struct wlsc_mode *mode = output->current;
961 surface->x = output->x + (mode->width - surface->width) / 2;
962 surface->y = output->y + (mode->height - surface->height) / 2;
966 map(struct wlsc_shell *base,
967 struct wlsc_surface *surface, int32_t width, int32_t height)
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;
976 shsurf = get_shell_surface(surface);
978 surface_type = shsurf->type;
981 list = &shell->hidden_surface_list;
984 list = &compositor->surface_list;
988 surface->width = width;
989 surface->height = height;
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;
997 case SHELL_SURFACE_SCREENSAVER:
998 case SHELL_SURFACE_FULLSCREEN:
999 center_on_output(surface, surface->fullscreen_output);
1001 case SHELL_SURFACE_LOCK:
1002 center_on_output(surface, get_default_output(compositor));
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);
1015 case SHELL_SURFACE_PANEL:
1016 /* panel always on top, hidden while locked */
1017 wl_list_insert(list, &surface->link);
1019 case SHELL_SURFACE_LOCK:
1020 /* lock surface always visible, on top */
1021 wl_list_insert(&compositor->surface_list, &surface->link);
1023 wlsc_compositor_repick(compositor);
1024 wlsc_compositor_wake(compositor);
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;
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);
1046 wl_list_insert(list, &surface->link);
1051 wlsc_surface_configure(surface,
1052 surface->x, surface->y, width, height);
1054 switch (surface_type) {
1055 case SHELL_SURFACE_TOPLEVEL:
1056 case SHELL_SURFACE_TRANSIENT:
1057 case SHELL_SURFACE_FULLSCREEN:
1059 activate(base, surface,
1060 (struct wlsc_input_device *)
1061 compositor->input_device,
1062 wlsc_compositor_get_time());
1068 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1069 wlsc_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1073 configure(struct wlsc_shell *base, struct wlsc_surface *surface,
1074 int32_t x, int32_t y, int32_t width, int32_t height)
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;
1081 shsurf = get_shell_surface(surface);
1083 surface_type = shsurf->type;
1085 surface->width = width;
1086 surface->height = height;
1088 switch (surface_type) {
1089 case SHELL_SURFACE_SCREENSAVER:
1090 do_configure = !do_configure;
1092 case SHELL_SURFACE_FULLSCREEN:
1093 center_on_output(surface, surface->fullscreen_output);
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?
1106 wlsc_surface_configure(surface, x, y, width, height);
1108 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1109 surface->output = shsurf->output;
1114 desktop_shell_sigchld(struct wlsc_process *process, int status)
1116 struct wl_shell *shell =
1117 container_of(process, struct wl_shell, child.process);
1119 shell->child.process.pid = 0;
1120 shell->child.client = NULL; /* already destroyed by wayland */
1124 launch_desktop_shell_process(struct wl_shell *shell)
1126 const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell";
1128 shell->child.client = wlsc_client_launch(shell->compositor,
1129 &shell->child.process,
1131 desktop_shell_sigchld);
1133 if (!shell->child.client)
1139 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1141 struct wl_shell *shell = data;
1143 wl_client_add_object(client, &wl_shell_interface,
1144 &shell_implementation, id, shell);
1148 unbind_desktop_shell(struct wl_resource *resource)
1150 struct wl_shell *shell = resource->data;
1153 resume_desktop(shell);
1155 shell->child.desktop_shell = NULL;
1156 shell->prepare_event_sent = false;
1161 bind_desktop_shell(struct wl_client *client,
1162 void *data, uint32_t version, uint32_t id)
1164 struct wl_shell *shell = data;
1165 struct wl_resource *resource;
1167 resource = wl_client_add_object(client, &desktop_shell_interface,
1168 &desktop_shell_implementation,
1171 if (client == shell->child.client) {
1172 resource->destroy = unbind_desktop_shell;
1173 shell->child.desktop_shell = resource;
1177 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1178 "permission to bind desktop_shell denied");
1179 wl_resource_destroy(resource, 0);
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)
1188 struct wl_shell *shell = resource->data;
1189 struct shell_surface *surface = shell_surface_resource->data;
1190 struct wlsc_output *output = output_resource->data;
1192 if (reset_shell_surface_type(surface))
1195 surface->type = SHELL_SURFACE_SCREENSAVER;
1197 surface->surface->fullscreen_output = output;
1198 surface->output = output;
1199 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1202 static const struct screensaver_interface screensaver_implementation = {
1203 screensaver_set_surface
1207 unbind_screensaver(struct wl_resource *resource)
1209 struct wl_shell *shell = resource->data;
1211 shell->screensaver.binding = NULL;
1216 bind_screensaver(struct wl_client *client,
1217 void *data, uint32_t version, uint32_t id)
1219 struct wl_shell *shell = data;
1220 struct wl_resource *resource;
1222 resource = wl_client_add_object(client, &screensaver_interface,
1223 &screensaver_implementation,
1226 if (shell->screensaver.binding == NULL) {
1227 resource->destroy = unbind_screensaver;
1228 shell->screensaver.binding = resource;
1232 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1233 "interface object already bound");
1234 wl_resource_destroy(resource, 0);
1238 shell_init(struct wlsc_compositor *ec);
1241 shell_init(struct wlsc_compositor *ec)
1243 struct wl_shell *shell;
1245 shell = malloc(sizeof *shell);
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;
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);
1261 if (shell_configuration(shell) < 0)
1264 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1265 shell, bind_shell) == NULL)
1268 if (wl_display_add_global(ec->wl_display,
1269 &desktop_shell_interface,
1270 shell, bind_desktop_shell) == NULL)
1273 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1274 shell, bind_screensaver) == NULL)
1277 if (launch_desktop_shell_process(shell) != 0)
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);
1293 ec->shell = &shell->shell;