2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 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>
34 #include <wayland-server.h>
35 #include "compositor.h"
36 #include "desktop-shell-server-protocol.h"
37 #include "../shared/config-parser.h"
42 struct weston_compositor *compositor;
43 struct weston_shell shell;
46 struct weston_process process;
47 struct wl_client *client;
48 struct wl_resource *desktop_shell;
55 bool prepare_event_sent;
57 struct shell_surface *lock_surface;
58 struct wl_listener lock_surface_listener;
59 struct wl_list hidden_surface_list;
61 struct wl_list backgrounds;
62 struct wl_list panels;
67 struct wl_resource *binding;
68 struct wl_list surfaces;
69 struct weston_process process;
73 enum shell_surface_type {
77 SHELL_SURFACE_BACKGROUND,
79 SHELL_SURFACE_SCREENSAVER,
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
83 SHELL_SURFACE_FULLSCREEN,
84 SHELL_SURFACE_MAXIMIZED,
88 struct shell_surface {
89 struct wl_resource resource;
91 struct weston_surface *surface;
92 struct wl_listener surface_destroy_listener;
93 struct shell_surface *parent;
95 enum shell_surface_type type;
96 int32_t saved_x, saved_y;
99 struct weston_transform transform;
100 struct weston_matrix rotation;
104 struct wl_pointer_grab grab;
107 struct weston_transform parent_transform;
111 struct weston_output *fullscreen_output;
112 struct weston_output *output;
116 struct weston_move_grab {
117 struct wl_pointer_grab grab;
118 struct weston_surface *surface;
122 struct weston_zoom_grab {
123 struct wl_keyboard_grab grab;
127 struct wl_pointer_grab grab;
128 struct shell_surface *surface;
129 struct weston_matrix rotation;
137 shell_configuration(struct wl_shell *shell)
143 struct config_key saver_keys[] = {
144 { "path", CONFIG_KEY_STRING, &path },
145 { "duration", CONFIG_KEY_INTEGER, &duration },
148 struct config_section cs[] = {
149 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
152 config_file = config_file_path("weston-desktop-shell.ini");
153 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
156 shell->screensaver.path = path;
157 shell->screensaver.duration = duration;
161 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
162 struct wl_surface *surface, int32_t x, int32_t y)
168 move_grab_motion(struct wl_pointer_grab *grab,
169 uint32_t time, int32_t x, int32_t y)
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;
175 weston_surface_configure(es,
176 device->x + move->dx,
177 device->y + move->dy,
178 es->geometry.width, es->geometry.height);
182 move_grab_button(struct wl_pointer_grab *grab,
183 uint32_t time, int32_t button, int32_t state)
185 struct wl_input_device *device = grab->input_device;
187 if (device->button_count == 0 && state == 0) {
188 wl_input_device_end_pointer_grab(device, time);
193 static const struct wl_pointer_grab_interface move_grab_interface = {
200 weston_surface_move(struct weston_surface *es,
201 struct weston_input_device *wd, uint32_t time)
203 struct weston_move_grab *move;
205 move = malloc(sizeof *move);
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;
214 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
216 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
222 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
223 struct wl_resource *input_resource, uint32_t time)
225 struct weston_input_device *wd = input_resource->data;
226 struct shell_surface *shsurf = resource->data;
228 if (wd->input_device.button_count == 0 ||
229 wd->input_device.grab_time != time ||
230 wd->input_device.pointer_focus != &shsurf->surface->surface)
233 if (weston_surface_move(shsurf->surface, wd, time) < 0)
234 wl_resource_post_no_memory(resource);
237 struct weston_resize_grab {
238 struct wl_pointer_grab grab;
240 int32_t width, height;
241 struct shell_surface *shsurf;
245 resize_grab_motion(struct wl_pointer_grab *grab,
246 uint32_t time, int32_t x, int32_t y)
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;
254 weston_surface_from_global(resize->shsurf->surface,
255 device->grab_x, device->grab_y,
257 weston_surface_from_global(resize->shsurf->surface,
258 device->x, device->y, &to_x, &to_y);
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;
265 width = resize->width;
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;
273 height = resize->height;
276 wl_resource_post_event(&resize->shsurf->resource,
277 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
282 resize_grab_button(struct wl_pointer_grab *grab,
283 uint32_t time, int32_t button, int32_t state)
285 struct wl_input_device *device = grab->input_device;
287 if (device->button_count == 0 && state == 0) {
288 wl_input_device_end_pointer_grab(device, time);
293 static const struct wl_pointer_grab_interface resize_grab_interface = {
300 weston_surface_resize(struct shell_surface *shsurf,
301 struct weston_input_device *wd,
302 uint32_t time, uint32_t edges)
304 struct weston_resize_grab *resize;
306 /* FIXME: Reject if fullscreen */
308 if (edges == 0 || edges > 15 ||
309 (edges & 3) == 3 || (edges & 12) == 12)
312 resize = malloc(sizeof *resize);
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;
322 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
324 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
330 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
331 struct wl_resource *input_resource, uint32_t time,
334 struct weston_input_device *wd = input_resource->data;
335 struct shell_surface *shsurf = resource->data;
337 /* FIXME: Reject if fullscreen */
339 if (wd->input_device.button_count == 0 ||
340 wd->input_device.grab_time != time ||
341 wd->input_device.pointer_focus != &shsurf->surface->surface)
344 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
345 wl_resource_post_no_memory(resource);
348 static struct weston_output *
349 get_default_output(struct weston_compositor *compositor)
351 return container_of(compositor->output_list.next,
352 struct weston_output, link);
356 reset_shell_surface_type(struct shell_surface *surface)
358 switch (surface->type) {
359 case SHELL_SURFACE_FULLSCREEN:
360 weston_surface_set_position(surface->surface,
363 surface->fullscreen_output = NULL;
365 case SHELL_SURFACE_MAXIMIZED:
366 surface->output = get_default_output(surface->surface->compositor);
367 weston_surface_set_position(surface->surface,
371 case SHELL_SURFACE_PANEL:
372 case SHELL_SURFACE_BACKGROUND:
373 wl_list_remove(&surface->link);
374 wl_list_init(&surface->link);
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");
382 case SHELL_SURFACE_NONE:
383 case SHELL_SURFACE_TOPLEVEL:
384 case SHELL_SURFACE_TRANSIENT:
385 case SHELL_SURFACE_POPUP:
389 surface->type = SHELL_SURFACE_NONE;
394 shell_surface_set_toplevel(struct wl_client *client,
395 struct wl_resource *resource)
398 struct shell_surface *surface = resource->data;
400 if (reset_shell_surface_type(surface))
403 surface->type = SHELL_SURFACE_TOPLEVEL;
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)
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;
417 if (reset_shell_surface_type(shsurf))
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);
425 shsurf->type = SHELL_SURFACE_TRANSIENT;
428 static struct wl_shell *
429 shell_surface_get_shell(struct shell_surface *shsurf)
431 struct weston_surface *es = shsurf->surface;
432 struct weston_shell *shell = es->compositor->shell;
434 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
438 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
440 struct shell_surface *priv;
441 int panel_height = 0;
446 wl_list_for_each(priv, &wlshell->panels, link) {
447 if (priv->output == output) {
448 panel_height = priv->surface->geometry.height;
456 shell_surface_set_maximized(struct wl_client *client,
457 struct wl_resource *resource,
458 struct wl_resource *output_resource )
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;
465 /* get the default output, if the client set it as NULL
466 check whether the ouput is available */
468 shsurf->output = output_resource->data;
470 shsurf->output = get_default_output(es->compositor);
472 if (reset_shell_surface_type(shsurf))
475 shsurf->saved_x = es->geometry.x;
476 shsurf->saved_y = es->geometry.y;
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);
487 shsurf->type = SHELL_SURFACE_MAXIMIZED;
491 shell_surface_set_fullscreen(struct wl_client *client,
492 struct wl_resource *resource,
495 struct wl_resource *output_resource)
497 struct shell_surface *shsurf = resource->data;
498 struct weston_surface *es = shsurf->surface;
499 struct weston_output *output;
501 if (reset_shell_surface_type(shsurf))
504 /* FIXME: Fullscreen on first output */
505 /* FIXME: Handle output going away */
506 output = get_default_output(es->compositor);
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;
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);
522 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
523 struct wl_surface *surface, int32_t x, int32_t y)
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;
530 if (surface && surface->resource.client == client) {
531 wl_input_device_set_pointer_focus(device, surface, time, x, y);
532 grab->focus = surface;
534 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
540 popup_grab_motion(struct wl_pointer_grab *grab,
541 uint32_t time, int32_t sx, int32_t sy)
543 struct wl_resource *resource;
545 resource = grab->input_device->pointer_focus_resource;
547 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
552 popup_grab_button(struct wl_pointer_grab *grab,
553 uint32_t time, int32_t button, int32_t state)
555 struct wl_resource *resource;
556 struct shell_surface *shsurf =
557 container_of(grab, struct shell_surface, popup.grab);
559 resource = grab->input_device->pointer_focus_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;
573 shsurf->popup.initial_up = 1;
576 static const struct wl_pointer_grab_interface popup_grab_interface = {
583 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
585 struct wl_input_device *device;
586 struct weston_surface *es = shsurf->surface;
587 struct weston_surface *parent = shsurf->parent->surface;
589 es->output = parent->output;
591 shsurf->popup.grab.interface = &popup_grab_interface;
592 device = es->compositor->input_device;
594 weston_surface_update_transform(parent);
595 if (parent->transform.enabled) {
596 shsurf->popup.parent_transform.matrix =
597 parent->transform.matrix;
599 /* construct x, y translation matrix */
600 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
601 shsurf->popup.parent_transform.matrix.d[12] =
603 shsurf->popup.parent_transform.matrix.d[13] =
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);
610 shsurf->popup.grab.input_device = device;
611 shsurf->popup.time = device->grab_time;
612 shsurf->popup.initial_up = 0;
614 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
615 &shsurf->popup.grab, shsurf->popup.time);
619 shell_surface_set_popup(struct wl_client *client,
620 struct wl_resource *resource,
621 struct wl_resource *input_device_resource,
623 struct wl_resource *parent_resource,
624 int32_t x, int32_t y, uint32_t flags)
626 struct shell_surface *shsurf = resource->data;
628 shsurf->type = SHELL_SURFACE_POPUP;
629 shsurf->parent = parent_resource->data;
634 static const struct wl_shell_surface_interface shell_surface_implementation = {
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
645 destroy_shell_surface(struct wl_resource *resource)
647 struct shell_surface *shsurf = resource->data;
649 if (shsurf->popup.grab.input_device)
650 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
652 /* in case cleaning up a dead client destroys shell_surface first */
654 wl_list_remove(&shsurf->surface_destroy_listener.link);
656 wl_list_remove(&shsurf->link);
661 shell_handle_surface_destroy(struct wl_listener *listener,
662 struct wl_resource *resource, uint32_t time)
664 struct shell_surface *shsurf = container_of(listener,
665 struct shell_surface,
666 surface_destroy_listener);
668 shsurf->surface = NULL;
669 wl_resource_destroy(&shsurf->resource, time);
672 static struct shell_surface *
673 get_shell_surface(struct weston_surface *surface)
675 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
676 struct wl_listener *listener;
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);
690 shell_get_shell_surface(struct wl_client *client,
691 struct wl_resource *resource,
693 struct wl_resource *surface_resource)
695 struct weston_surface *surface = surface_resource->data;
696 struct shell_surface *shsurf;
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");
705 shsurf = calloc(1, sizeof *shsurf);
707 wl_resource_post_no_memory(resource);
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;
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);
723 /* init link so its safe to always remove it in destroy_shell_surface */
724 wl_list_init(&shsurf->link);
726 /* empty when not in use */
727 wl_list_init(&shsurf->rotation.transform.link);
728 weston_matrix_init(&shsurf->rotation.rotation);
730 shsurf->type = SHELL_SURFACE_NONE;
732 wl_client_add_resource(client, &shsurf->resource);
735 static const struct wl_shell_interface shell_implementation = {
736 shell_get_shell_surface
740 handle_screensaver_sigchld(struct weston_process *proc, int status)
746 launch_screensaver(struct wl_shell *shell)
748 if (shell->screensaver.binding)
751 if (!shell->screensaver.path)
754 weston_client_launch(shell->compositor,
755 &shell->screensaver.process,
756 shell->screensaver.path,
757 handle_screensaver_sigchld);
761 terminate_screensaver(struct wl_shell *shell)
763 if (shell->screensaver.process.pid == 0)
766 kill(shell->screensaver.process.pid, SIGTERM);
770 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
772 struct wl_list *list;
774 if (shell->lock_surface)
775 list = &shell->lock_surface->surface->link;
777 list = &shell->compositor->surface_list;
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);
786 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
788 wl_list_remove(&surface->surface->link);
789 wl_list_init(&surface->surface->link);
790 surface->surface->output = NULL;
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)
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;
804 if (reset_shell_surface_type(shsurf))
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);
816 shsurf->type = SHELL_SURFACE_BACKGROUND;
817 shsurf->output = output_resource->data;
819 wl_list_insert(&shell->backgrounds, &shsurf->link);
821 weston_surface_set_position(surface, shsurf->output->x,
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);
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)
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;
842 if (reset_shell_surface_type(shsurf))
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);
854 shsurf->type = SHELL_SURFACE_PANEL;
855 shsurf->output = output_resource->data;
857 wl_list_insert(&shell->panels, &shsurf->link);
859 weston_surface_set_position(surface, shsurf->output->x,
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);
870 handle_lock_surface_destroy(struct wl_listener *listener,
871 struct wl_resource *resource, uint32_t time)
873 struct wl_shell *shell =
874 container_of(listener, struct wl_shell, lock_surface_listener);
876 fprintf(stderr, "lock surface gone\n");
877 shell->lock_surface = NULL;
881 desktop_shell_set_lock_surface(struct wl_client *client,
882 struct wl_resource *resource,
883 struct wl_resource *surface_resource)
885 struct wl_shell *shell = resource->data;
886 struct shell_surface *surface = surface_resource->data;
888 if (reset_shell_surface_type(surface))
891 shell->prepare_event_sent = false;
896 shell->lock_surface = surface;
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);
902 shell->lock_surface->type = SHELL_SURFACE_LOCK;
906 resume_desktop(struct wl_shell *shell)
908 struct weston_surface *surface;
909 struct wl_list *list;
910 struct shell_surface *tmp;
912 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
913 hide_screensaver(shell, tmp);
915 terminate_screensaver(shell);
917 wl_list_for_each(surface, &shell->hidden_surface_list, link)
918 weston_surface_assign_output(surface);
920 if (wl_list_empty(&shell->backgrounds)) {
921 list = &shell->compositor->surface_list;
923 struct shell_surface *background;
924 background = container_of(shell->backgrounds.prev,
925 struct shell_surface, link);
926 list = background->surface->link.prev;
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);
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);
941 desktop_shell_unlock(struct wl_client *client,
942 struct wl_resource *resource)
944 struct wl_shell *shell = resource->data;
946 shell->prepare_event_sent = false;
949 resume_desktop(shell);
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,
959 static enum shell_surface_type
960 get_shell_surface_type(struct weston_surface *surface)
962 struct shell_surface *shsurf;
964 shsurf = get_shell_surface(surface);
966 return SHELL_SURFACE_NONE;
971 move_binding(struct wl_input_device *device, uint32_t time,
972 uint32_t key, uint32_t button, uint32_t state, void *data)
974 struct weston_surface *surface =
975 (struct weston_surface *) device->pointer_focus;
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:
990 weston_surface_move(surface, (struct weston_input_device *) device, time);
994 resize_binding(struct wl_input_device *device, uint32_t time,
995 uint32_t key, uint32_t button, uint32_t state, void *data)
997 struct weston_surface *surface =
998 (struct weston_surface *) device->pointer_focus;
1001 struct shell_surface *shsurf;
1003 if (surface == NULL)
1006 shsurf = get_shell_surface(surface);
1010 switch (shsurf->type) {
1011 case SHELL_SURFACE_PANEL:
1012 case SHELL_SURFACE_BACKGROUND:
1013 case SHELL_SURFACE_FULLSCREEN:
1014 case SHELL_SURFACE_SCREENSAVER:
1020 weston_surface_from_global(surface,
1021 device->grab_x, device->grab_y, &x, &y);
1023 if (x < surface->geometry.width / 3)
1024 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1025 else if (x < 2 * surface->geometry.width / 3)
1028 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1030 if (y < surface->geometry.height / 3)
1031 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1032 else if (y < 2 * surface->geometry.height / 3)
1035 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1037 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1042 zoom_grab_key(struct wl_keyboard_grab *grab,
1043 uint32_t time, uint32_t key, int32_t state)
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;
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);
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;
1065 if (state && key == KEY_DOWN)
1066 output->zoom.level += output->zoom.increment;
1068 if (output->zoom.level >= 1.0) {
1069 output->zoom.active = 0;
1070 output->zoom.level = 1.0;
1073 if (output->zoom.level < output->zoom.increment)
1074 output->zoom.level = output->zoom.increment;
1076 weston_output_update_zoom(output, device->x, device->y);
1081 static const struct wl_keyboard_grab_interface zoom_grab = {
1086 zoom_binding(struct wl_input_device *device, uint32_t time,
1087 uint32_t key, uint32_t button, uint32_t state, void *data)
1089 struct weston_input_device *wd = (struct weston_input_device *) device;
1090 struct weston_zoom_grab *zoom;
1092 zoom = malloc(sizeof *zoom);
1096 zoom->grab.interface = &zoom_grab;
1098 wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
1102 terminate_binding(struct wl_input_device *device, uint32_t time,
1103 uint32_t key, uint32_t button, uint32_t state, void *data)
1105 struct weston_compositor *compositor = data;
1108 wl_display_terminate(compositor->wl_display);
1112 rotate_grab_motion(struct wl_pointer_grab *grab,
1113 uint32_t time, int32_t x, int32_t y)
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;
1124 dx = device->x - rotate->center.x;
1125 dy = device->y - rotate->center.y;
1126 r = sqrtf(dx * dx + dy * dy);
1128 wl_list_remove(&surface->rotation.transform.link);
1129 surface->surface->geometry.dirty = 1;
1132 struct weston_matrix *matrix =
1133 &surface->rotation.transform.matrix;
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];
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);
1148 &surface->surface->geometry.transformation_list,
1149 &surface->rotation.transform.link);
1151 wl_list_init(&surface->rotation.transform.link);
1152 weston_matrix_init(&surface->rotation.rotation);
1153 weston_matrix_init(&rotate->rotation);
1156 /* Repaint implies weston_surface_update_transform(), which
1157 * lazily applies the damage due to rotation update.
1159 weston_compositor_schedule_repaint(surface->surface->compositor);
1163 rotate_grab_button(struct wl_pointer_grab *grab,
1164 uint32_t time, int32_t button, int32_t state)
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;
1171 if (device->button_count == 0 && state == 0) {
1172 weston_matrix_multiply(&surface->rotation.rotation,
1174 wl_input_device_end_pointer_grab(device, time);
1179 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1186 rotate_binding(struct wl_input_device *device, uint32_t time,
1187 uint32_t key, uint32_t button, uint32_t state, void *data)
1189 struct weston_surface *base_surface =
1190 (struct weston_surface *) device->pointer_focus;
1191 struct shell_surface *surface;
1192 struct rotate_grab *rotate;
1196 if (base_surface == NULL)
1199 surface = get_shell_surface(base_surface);
1203 switch (surface->type) {
1204 case SHELL_SURFACE_PANEL:
1205 case SHELL_SURFACE_BACKGROUND:
1206 case SHELL_SURFACE_FULLSCREEN:
1207 case SHELL_SURFACE_SCREENSAVER:
1213 rotate = malloc(sizeof *rotate);
1217 rotate->grab.interface = &rotate_grab_interface;
1218 rotate->surface = surface;
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);
1225 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1227 dx = device->x - rotate->center.x;
1228 dy = device->y - rotate->center.y;
1229 r = sqrtf(dx * dx + dy * dy);
1231 struct weston_matrix inverse;
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);
1240 weston_matrix_init(&surface->rotation.rotation);
1241 weston_matrix_init(&rotate->rotation);
1244 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1248 activate(struct weston_shell *base, struct weston_surface *es,
1249 struct weston_input_device *device, uint32_t time)
1251 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1252 struct weston_compositor *compositor = shell->compositor;
1253 struct wl_list *list;
1255 weston_surface_activate(es, device, time);
1257 if (compositor->wxs)
1258 weston_xserver_surface_activate(es);
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);
1266 case SHELL_SURFACE_PANEL:
1267 /* already put on top */
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,
1278 if (!shell->locked) {
1279 list = weston_compositor_top(compositor);
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);
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)
1296 struct weston_input_device *wd = (struct weston_input_device *) device;
1297 struct weston_compositor *compositor = data;
1298 struct weston_surface *focus;
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);
1306 lock(struct weston_shell *base)
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;
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);
1325 shell->locked = true;
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. */
1331 if (!wl_list_empty(&shell->hidden_surface_list)) {
1333 "%s: Assertion failed: hidden_surface_list is not empty.\n",
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)
1342 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
1346 wl_list_remove(&cur->link);
1347 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
1350 launch_screensaver(shell);
1352 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1353 show_screensaver(shell, shsurf);
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;
1361 /* reset pointer foci */
1362 weston_compositor_repick(shell->compositor);
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,
1371 /* TODO: disable bindings that should not work while locked. */
1373 /* All this must be undone in resume_desktop(). */
1377 unlock(struct weston_shell *base)
1379 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1381 if (!shell->locked || shell->lock_surface) {
1382 weston_compositor_wake(shell->compositor);
1386 /* If desktop-shell client has gone away, unlock immediately. */
1387 if (!shell->child.desktop_shell) {
1388 resume_desktop(shell);
1392 if (shell->prepare_event_sent)
1395 wl_resource_post_event(shell->child.desktop_shell,
1396 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1397 shell->prepare_event_sent = true;
1401 center_on_output(struct weston_surface *surface, struct weston_output *output)
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;
1407 weston_surface_set_position(surface, output->x + x, output->y + y);
1411 map(struct weston_shell *base, struct weston_surface *surface,
1412 int32_t width, int32_t height, int32_t sx, int32_t sy)
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;
1420 int panel_height = 0;
1422 shsurf = get_shell_surface(surface);
1424 surface_type = shsurf->type;
1426 if (shell->locked) {
1427 list = &shell->hidden_surface_list;
1430 list = weston_compositor_top(compositor);
1434 surface->geometry.width = width;
1435 surface->geometry.height = height;
1436 surface->geometry.dirty = 1;
1438 weston_compositor_update_drag_surfaces(compositor);
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);
1446 case SHELL_SURFACE_SCREENSAVER:
1447 case SHELL_SURFACE_FULLSCREEN:
1448 center_on_output(surface, shsurf->fullscreen_output);
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);
1456 case SHELL_SURFACE_LOCK:
1457 center_on_output(surface, get_default_output(compositor));
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);
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);
1477 case SHELL_SURFACE_PANEL:
1478 /* panel always on top, hidden while locked */
1479 wl_list_insert(list, &surface->link);
1481 case SHELL_SURFACE_LOCK:
1482 /* lock surface always visible, on top */
1483 wl_list_insert(&compositor->surface_list, &surface->link);
1485 weston_compositor_wake(compositor);
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;
1499 case SHELL_SURFACE_NONE:
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);
1510 wl_list_insert(list, &surface->link);
1515 weston_surface_assign_output(surface);
1516 weston_compositor_repick(compositor);
1517 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1518 surface->output = shsurf->output;
1521 switch (surface_type) {
1522 case SHELL_SURFACE_TOPLEVEL:
1523 case SHELL_SURFACE_TRANSIENT:
1524 case SHELL_SURFACE_FULLSCREEN:
1525 case SHELL_SURFACE_MAXIMIZED:
1527 activate(base, surface,
1528 (struct weston_input_device *)
1529 compositor->input_device,
1530 weston_compositor_get_time());
1536 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1537 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1541 configure(struct weston_shell *base, struct weston_surface *surface,
1542 GLfloat x, GLfloat y, int32_t width, int32_t height)
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;
1548 shsurf = get_shell_surface(surface);
1550 surface_type = shsurf->type;
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;
1558 switch (surface_type) {
1559 case SHELL_SURFACE_SCREENSAVER:
1560 case SHELL_SURFACE_FULLSCREEN:
1561 center_on_output(surface, shsurf->fullscreen_output);
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);
1573 /* XXX: would a fullscreen surface need the same handling? */
1574 if (surface->output) {
1575 weston_surface_assign_output(surface);
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;
1584 static int launch_desktop_shell_process(struct wl_shell *shell);
1587 desktop_shell_sigchld(struct weston_process *process, int status)
1590 struct wl_shell *shell =
1591 container_of(process, struct wl_shell, child.process);
1593 shell->child.process.pid = 0;
1594 shell->child.client = NULL; /* already destroyed by wayland */
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;
1603 shell->child.deathcount++;
1604 if (shell->child.deathcount > 5) {
1605 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1609 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1610 launch_desktop_shell_process(shell);
1614 launch_desktop_shell_process(struct wl_shell *shell)
1616 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1618 shell->child.client = weston_client_launch(shell->compositor,
1619 &shell->child.process,
1621 desktop_shell_sigchld);
1623 if (!shell->child.client)
1629 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1631 struct wl_shell *shell = data;
1633 wl_client_add_object(client, &wl_shell_interface,
1634 &shell_implementation, id, shell);
1638 unbind_desktop_shell(struct wl_resource *resource)
1640 struct wl_shell *shell = resource->data;
1643 resume_desktop(shell);
1645 shell->child.desktop_shell = NULL;
1646 shell->prepare_event_sent = false;
1651 bind_desktop_shell(struct wl_client *client,
1652 void *data, uint32_t version, uint32_t id)
1654 struct wl_shell *shell = data;
1655 struct wl_resource *resource;
1657 resource = wl_client_add_object(client, &desktop_shell_interface,
1658 &desktop_shell_implementation,
1661 if (client == shell->child.client) {
1662 resource->destroy = unbind_desktop_shell;
1663 shell->child.desktop_shell = resource;
1667 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1668 "permission to bind desktop_shell denied");
1669 wl_resource_destroy(resource, 0);
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)
1678 struct wl_shell *shell = resource->data;
1679 struct shell_surface *surface = shell_surface_resource->data;
1680 struct weston_output *output = output_resource->data;
1682 if (reset_shell_surface_type(surface))
1685 surface->type = SHELL_SURFACE_SCREENSAVER;
1687 surface->fullscreen_output = output;
1688 surface->output = output;
1689 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1692 static const struct screensaver_interface screensaver_implementation = {
1693 screensaver_set_surface
1697 unbind_screensaver(struct wl_resource *resource)
1699 struct wl_shell *shell = resource->data;
1701 shell->screensaver.binding = NULL;
1706 bind_screensaver(struct wl_client *client,
1707 void *data, uint32_t version, uint32_t id)
1709 struct wl_shell *shell = data;
1710 struct wl_resource *resource;
1712 resource = wl_client_add_object(client, &screensaver_interface,
1713 &screensaver_implementation,
1716 if (shell->screensaver.binding == NULL) {
1717 resource->destroy = unbind_screensaver;
1718 shell->screensaver.binding = resource;
1722 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1723 "interface object already bound");
1724 wl_resource_destroy(resource, 0);
1728 struct weston_compositor *compositor;
1729 struct weston_surface *current;
1730 struct wl_listener listener;
1731 struct wl_keyboard_grab grab;
1735 switcher_next(struct switcher *switcher)
1737 struct weston_compositor *compositor = switcher->compositor;
1738 struct weston_surface *surface;
1739 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
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)
1746 switch (get_shell_surface_type(surface)) {
1747 case SHELL_SURFACE_TOPLEVEL:
1748 case SHELL_SURFACE_FULLSCREEN:
1749 case SHELL_SURFACE_MAXIMIZED:
1752 if (prev == switcher->current)
1755 surface->alpha = 64;
1756 surface->geometry.dirty = 1;
1757 weston_surface_damage(surface);
1767 wl_list_remove(&switcher->listener.link);
1768 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1769 &switcher->listener.link);
1771 switcher->current = next;
1776 switcher_handle_surface_destroy(struct wl_listener *listener,
1777 struct wl_resource *resource, uint32_t time)
1779 struct switcher *switcher =
1780 container_of(listener, struct switcher, listener);
1782 switcher_next(switcher);
1786 switcher_destroy(struct switcher *switcher, uint32_t time)
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;
1793 wl_list_for_each(surface, &compositor->surface_list, link) {
1794 surface->alpha = 255;
1795 weston_surface_damage(surface);
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);
1805 switcher_key(struct wl_keyboard_grab *grab,
1806 uint32_t time, uint32_t key, int32_t state)
1808 struct switcher *switcher = container_of(grab, struct switcher, grab);
1809 struct weston_input_device *device =
1810 (struct weston_input_device *) grab->input_device;
1812 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1813 switcher_destroy(switcher, time);
1814 } else if (key == KEY_TAB && state) {
1815 switcher_next(switcher);
1819 static const struct wl_keyboard_grab_interface switcher_grab = {
1824 switcher_binding(struct wl_input_device *device, uint32_t time,
1825 uint32_t key, uint32_t button,
1826 uint32_t state, void *data)
1828 struct weston_compositor *compositor = data;
1829 struct switcher *switcher;
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);
1837 switcher->grab.interface = &switcher_grab;
1838 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1839 switcher_next(switcher);
1843 backlight_binding(struct wl_input_device *device, uint32_t time,
1844 uint32_t key, uint32_t button, uint32_t state, void *data)
1846 struct weston_compositor *compositor = data;
1847 struct weston_output *output;
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
1853 output = get_default_output(compositor);
1857 if (!output->set_backlight)
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++;
1867 output->set_backlight(output, output->backlight_current);
1871 shell_destroy(struct weston_shell *base)
1873 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1875 if (shell->child.client)
1876 wl_client_destroy(shell->child.client);
1878 free(shell->screensaver.path);
1883 shell_init(struct weston_compositor *ec);
1886 shell_init(struct weston_compositor *ec)
1888 struct wl_shell *shell;
1890 shell = malloc(sizeof *shell);
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;
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);
1907 shell_configuration(shell);
1909 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1910 shell, bind_shell) == NULL)
1913 if (wl_display_add_global(ec->wl_display,
1914 &desktop_shell_interface,
1915 shell, bind_desktop_shell) == NULL)
1918 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1919 shell, bind_screensaver) == NULL)
1922 shell->child.deathstamp = weston_compositor_get_time();
1923 if (launch_desktop_shell_process(shell) != 0)
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);
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);
1955 ec->shell = &shell->shell;