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;
97 bool saved_position_valid;
100 struct weston_transform transform;
101 struct weston_matrix rotation;
105 struct wl_pointer_grab grab;
108 struct weston_transform parent_transform;
113 enum wl_shell_surface_fullscreen_method type;
114 struct weston_transform transform; /* matrix from x, y */
116 struct weston_surface *black_surface;
119 struct weston_output *fullscreen_output;
120 struct weston_output *output;
124 struct weston_move_grab {
125 struct wl_pointer_grab grab;
126 struct weston_surface *surface;
130 struct weston_zoom_grab {
131 struct wl_keyboard_grab grab;
135 struct wl_pointer_grab grab;
136 struct shell_surface *surface;
137 struct weston_matrix rotation;
145 center_on_output(struct weston_surface *surface,
146 struct weston_output *output);
149 shell_configuration(struct wl_shell *shell)
155 struct config_key saver_keys[] = {
156 { "path", CONFIG_KEY_STRING, &path },
157 { "duration", CONFIG_KEY_INTEGER, &duration },
160 struct config_section cs[] = {
161 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
164 config_file = config_file_path("weston-desktop-shell.ini");
165 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
168 shell->screensaver.path = path;
169 shell->screensaver.duration = duration;
173 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
174 struct wl_surface *surface, int32_t x, int32_t y)
180 move_grab_motion(struct wl_pointer_grab *grab,
181 uint32_t time, int32_t x, int32_t y)
183 struct weston_move_grab *move = (struct weston_move_grab *) grab;
184 struct wl_input_device *device = grab->input_device;
185 struct weston_surface *es = move->surface;
187 weston_surface_configure(es,
188 device->x + move->dx,
189 device->y + move->dy,
190 es->geometry.width, es->geometry.height);
194 move_grab_button(struct wl_pointer_grab *grab,
195 uint32_t time, int32_t button, int32_t state)
197 struct wl_input_device *device = grab->input_device;
199 if (device->button_count == 0 && state == 0) {
200 wl_input_device_end_pointer_grab(device, time);
205 static const struct wl_pointer_grab_interface move_grab_interface = {
212 weston_surface_move(struct weston_surface *es,
213 struct weston_input_device *wd, uint32_t time)
215 struct weston_move_grab *move;
217 move = malloc(sizeof *move);
221 move->grab.interface = &move_grab_interface;
222 move->dx = es->geometry.x - wd->input_device.grab_x;
223 move->dy = es->geometry.y - wd->input_device.grab_y;
226 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
228 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
234 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
235 struct wl_resource *input_resource, uint32_t time)
237 struct weston_input_device *wd = input_resource->data;
238 struct shell_surface *shsurf = resource->data;
240 if (wd->input_device.button_count == 0 ||
241 wd->input_device.grab_time != time ||
242 wd->input_device.pointer_focus != &shsurf->surface->surface)
245 if (weston_surface_move(shsurf->surface, wd, time) < 0)
246 wl_resource_post_no_memory(resource);
249 struct weston_resize_grab {
250 struct wl_pointer_grab grab;
252 int32_t width, height;
253 struct shell_surface *shsurf;
257 resize_grab_motion(struct wl_pointer_grab *grab,
258 uint32_t time, int32_t x, int32_t y)
260 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
261 struct wl_input_device *device = grab->input_device;
262 int32_t width, height;
263 int32_t from_x, from_y;
266 weston_surface_from_global(resize->shsurf->surface,
267 device->grab_x, device->grab_y,
269 weston_surface_from_global(resize->shsurf->surface,
270 device->x, device->y, &to_x, &to_y);
272 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
273 width = resize->width + from_x - to_x;
274 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
275 width = resize->width + to_x - from_x;
277 width = resize->width;
280 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
281 height = resize->height + from_y - to_y;
282 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
283 height = resize->height + to_y - from_y;
285 height = resize->height;
288 wl_resource_post_event(&resize->shsurf->resource,
289 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
294 resize_grab_button(struct wl_pointer_grab *grab,
295 uint32_t time, int32_t button, int32_t state)
297 struct wl_input_device *device = grab->input_device;
299 if (device->button_count == 0 && state == 0) {
300 wl_input_device_end_pointer_grab(device, time);
305 static const struct wl_pointer_grab_interface resize_grab_interface = {
312 weston_surface_resize(struct shell_surface *shsurf,
313 struct weston_input_device *wd,
314 uint32_t time, uint32_t edges)
316 struct weston_resize_grab *resize;
318 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
321 if (edges == 0 || edges > 15 ||
322 (edges & 3) == 3 || (edges & 12) == 12)
325 resize = malloc(sizeof *resize);
329 resize->grab.interface = &resize_grab_interface;
330 resize->edges = edges;
331 resize->width = shsurf->surface->geometry.width;
332 resize->height = shsurf->surface->geometry.height;
333 resize->shsurf = shsurf;
335 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
337 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
343 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
344 struct wl_resource *input_resource, uint32_t time,
347 struct weston_input_device *wd = input_resource->data;
348 struct shell_surface *shsurf = resource->data;
350 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
353 if (wd->input_device.button_count == 0 ||
354 wd->input_device.grab_time != time ||
355 wd->input_device.pointer_focus != &shsurf->surface->surface)
358 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
359 wl_resource_post_no_memory(resource);
362 static struct weston_output *
363 get_default_output(struct weston_compositor *compositor)
365 return container_of(compositor->output_list.next,
366 struct weston_output, link);
370 shell_unset_fullscreen(struct shell_surface *shsurf)
372 /* undo all fullscreen things here */
373 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
374 shsurf->fullscreen.framerate = 0;
375 wl_list_remove(&shsurf->fullscreen.transform.link);
376 wl_list_init(&shsurf->fullscreen.transform.link);
377 weston_surface_destroy(shsurf->fullscreen.black_surface);
378 shsurf->fullscreen.black_surface = NULL;
379 shsurf->fullscreen_output = NULL;
380 shsurf->surface->force_configure = 1;
381 weston_surface_set_position(shsurf->surface,
382 shsurf->saved_x, shsurf->saved_y);
386 reset_shell_surface_type(struct shell_surface *surface)
388 switch (surface->type) {
389 case SHELL_SURFACE_FULLSCREEN:
390 shell_unset_fullscreen(surface);
392 case SHELL_SURFACE_MAXIMIZED:
393 surface->output = get_default_output(surface->surface->compositor);
394 weston_surface_set_position(surface->surface,
398 case SHELL_SURFACE_PANEL:
399 case SHELL_SURFACE_BACKGROUND:
400 wl_list_remove(&surface->link);
401 wl_list_init(&surface->link);
403 case SHELL_SURFACE_SCREENSAVER:
404 case SHELL_SURFACE_LOCK:
405 wl_resource_post_error(&surface->resource,
406 WL_DISPLAY_ERROR_INVALID_METHOD,
407 "cannot reassign surface type");
409 case SHELL_SURFACE_NONE:
410 case SHELL_SURFACE_TOPLEVEL:
411 case SHELL_SURFACE_TRANSIENT:
412 case SHELL_SURFACE_POPUP:
416 surface->type = SHELL_SURFACE_NONE;
421 shell_surface_set_toplevel(struct wl_client *client,
422 struct wl_resource *resource)
425 struct shell_surface *surface = resource->data;
427 if (reset_shell_surface_type(surface))
430 surface->type = SHELL_SURFACE_TOPLEVEL;
434 shell_surface_set_transient(struct wl_client *client,
435 struct wl_resource *resource,
436 struct wl_resource *parent_resource,
437 int x, int y, uint32_t flags)
439 struct shell_surface *shsurf = resource->data;
440 struct weston_surface *es = shsurf->surface;
441 struct shell_surface *pshsurf = parent_resource->data;
442 struct weston_surface *pes = pshsurf->surface;
444 if (reset_shell_surface_type(shsurf))
447 /* assign to parents output */
448 shsurf->output = pes->output;
449 weston_surface_set_position(es, pes->geometry.x + x,
450 pes->geometry.y + y);
452 shsurf->type = SHELL_SURFACE_TRANSIENT;
455 static struct wl_shell *
456 shell_surface_get_shell(struct shell_surface *shsurf)
458 struct weston_surface *es = shsurf->surface;
459 struct weston_shell *shell = es->compositor->shell;
461 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
465 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
467 struct shell_surface *priv;
468 int panel_height = 0;
473 wl_list_for_each(priv, &wlshell->panels, link) {
474 if (priv->output == output) {
475 panel_height = priv->surface->geometry.height;
483 shell_surface_set_maximized(struct wl_client *client,
484 struct wl_resource *resource,
485 struct wl_resource *output_resource )
487 struct shell_surface *shsurf = resource->data;
488 struct weston_surface *es = shsurf->surface;
489 struct wl_shell *wlshell = NULL;
490 uint32_t edges = 0, panel_height = 0;
492 /* get the default output, if the client set it as NULL
493 check whether the ouput is available */
495 shsurf->output = output_resource->data;
497 shsurf->output = get_default_output(es->compositor);
499 if (reset_shell_surface_type(shsurf))
502 shsurf->saved_x = es->geometry.x;
503 shsurf->saved_y = es->geometry.y;
504 shsurf->saved_position_valid = true;
506 wlshell = shell_surface_get_shell(shsurf);
507 panel_height = get_output_panel_height(wlshell, es->output);
508 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
509 wl_resource_post_event(&shsurf->resource,
510 WL_SHELL_SURFACE_CONFIGURE,
511 weston_compositor_get_time(), edges,
512 es->output->current->width,
513 es->output->current->height - panel_height);
515 shsurf->type = SHELL_SURFACE_MAXIMIZED;
518 static struct weston_surface *
519 create_black_surface(struct weston_compositor *ec,
520 GLfloat x, GLfloat y, int w, int h)
522 struct weston_surface *surface = NULL;
524 surface = weston_surface_create(ec);
525 if (surface == NULL) {
526 fprintf(stderr, "no memory\n");
530 weston_surface_configure(surface, x, y, w, h);
531 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
535 /* Create black surface and append it to the associated fullscreen surface.
536 * Handle size dismatch and positioning according to the method. */
538 shell_configure_fullscreen(struct shell_surface *shsurf)
540 struct weston_output *output = shsurf->fullscreen_output;
541 struct weston_surface *surface = shsurf->surface;
542 struct weston_matrix *matrix;
545 center_on_output(surface, output);
547 if (!shsurf->fullscreen.black_surface)
548 shsurf->fullscreen.black_surface = create_black_surface(surface->compositor,
549 output->x, output->y,
550 output->current->width, output->current->height);
551 wl_list_remove(&shsurf->fullscreen.black_surface->link);
552 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
553 shsurf->fullscreen.black_surface->output = output;
555 switch (shsurf->fullscreen.type) {
556 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
558 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
559 matrix = &shsurf->fullscreen.transform.matrix;
560 weston_matrix_init(matrix);
561 scale = (float)output->current->width/(float)surface->geometry.width;
562 weston_matrix_scale(matrix, scale, scale, 1);
563 wl_list_remove(&shsurf->fullscreen.transform.link);
564 wl_list_insert(surface->geometry.transformation_list.prev,
565 &shsurf->fullscreen.transform.link);
566 weston_surface_set_position(surface, output->x, output->y);
568 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
570 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
577 /* make the fullscreen and black surface at the top */
579 shell_stack_fullscreen(struct shell_surface *shsurf)
581 struct weston_surface *surface = shsurf->surface;
582 struct wl_shell *shell = shell_surface_get_shell(shsurf);
583 struct wl_list *list;
585 wl_list_remove(&surface->link);
586 wl_list_remove(&shsurf->fullscreen.black_surface->link);
589 wl_list_insert(&shell->hidden_surface_list, &surface->link);
590 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
592 list = weston_compositor_top(surface->compositor);
593 wl_list_insert(list, &surface->link);
594 wl_list_insert(&surface->link, &shsurf->fullscreen.black_surface->link);
596 weston_surface_damage(surface);
597 weston_surface_damage(shsurf->fullscreen.black_surface);
602 shell_map_fullscreen(struct shell_surface *shsurf)
604 shell_configure_fullscreen(shsurf);
605 shell_stack_fullscreen(shsurf);
609 shell_surface_set_fullscreen(struct wl_client *client,
610 struct wl_resource *resource,
613 struct wl_resource *output_resource)
615 struct shell_surface *shsurf = resource->data;
616 struct weston_surface *es = shsurf->surface;
619 shsurf->output = output_resource->data;
621 shsurf->output = get_default_output(es->compositor);
623 if (reset_shell_surface_type(shsurf))
626 shsurf->fullscreen_output = shsurf->output;
627 shsurf->fullscreen.type = method;
628 shsurf->fullscreen.framerate = framerate;
629 shsurf->type = SHELL_SURFACE_FULLSCREEN;
631 shsurf->saved_x = es->geometry.x;
632 shsurf->saved_y = es->geometry.y;
633 shsurf->saved_position_valid = true;
636 shsurf->surface->force_configure = 1;
638 wl_resource_post_event(resource,
639 WL_SHELL_SURFACE_CONFIGURE,
640 weston_compositor_get_time(), 0,
641 shsurf->output->current->width,
642 shsurf->output->current->height);
646 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
647 struct wl_surface *surface, int32_t x, int32_t y)
649 struct wl_input_device *device = grab->input_device;
650 struct shell_surface *priv =
651 container_of(grab, struct shell_surface, popup.grab);
652 struct wl_client *client = priv->surface->surface.resource.client;
654 if (surface && surface->resource.client == client) {
655 wl_input_device_set_pointer_focus(device, surface, time, x, y);
656 grab->focus = surface;
658 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
664 popup_grab_motion(struct wl_pointer_grab *grab,
665 uint32_t time, int32_t sx, int32_t sy)
667 struct wl_resource *resource;
669 resource = grab->input_device->pointer_focus_resource;
671 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
676 popup_grab_button(struct wl_pointer_grab *grab,
677 uint32_t time, int32_t button, int32_t state)
679 struct wl_resource *resource;
680 struct shell_surface *shsurf =
681 container_of(grab, struct shell_surface, popup.grab);
683 resource = grab->input_device->pointer_focus_resource;
685 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
686 time, button, state);
687 } else if (state == 0 &&
688 (shsurf->popup.initial_up ||
689 time - shsurf->popup.time > 500)) {
690 wl_resource_post_event(&shsurf->resource,
691 WL_SHELL_SURFACE_POPUP_DONE);
692 wl_input_device_end_pointer_grab(grab->input_device, time);
693 shsurf->popup.grab.input_device = NULL;
697 shsurf->popup.initial_up = 1;
700 static const struct wl_pointer_grab_interface popup_grab_interface = {
707 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
709 struct wl_input_device *device;
710 struct weston_surface *es = shsurf->surface;
711 struct weston_surface *parent = shsurf->parent->surface;
713 es->output = parent->output;
715 shsurf->popup.grab.interface = &popup_grab_interface;
716 device = es->compositor->input_device;
718 weston_surface_update_transform(parent);
719 if (parent->transform.enabled) {
720 shsurf->popup.parent_transform.matrix =
721 parent->transform.matrix;
723 /* construct x, y translation matrix */
724 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
725 shsurf->popup.parent_transform.matrix.d[12] =
727 shsurf->popup.parent_transform.matrix.d[13] =
730 wl_list_insert(es->geometry.transformation_list.prev,
731 &shsurf->popup.parent_transform.link);
732 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
734 shsurf->popup.grab.input_device = device;
735 shsurf->popup.time = device->grab_time;
736 shsurf->popup.initial_up = 0;
738 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
739 &shsurf->popup.grab, shsurf->popup.time);
743 shell_surface_set_popup(struct wl_client *client,
744 struct wl_resource *resource,
745 struct wl_resource *input_device_resource,
747 struct wl_resource *parent_resource,
748 int32_t x, int32_t y, uint32_t flags)
750 struct shell_surface *shsurf = resource->data;
752 shsurf->type = SHELL_SURFACE_POPUP;
753 shsurf->parent = parent_resource->data;
758 static const struct wl_shell_surface_interface shell_surface_implementation = {
760 shell_surface_resize,
761 shell_surface_set_toplevel,
762 shell_surface_set_transient,
763 shell_surface_set_fullscreen,
764 shell_surface_set_popup,
765 shell_surface_set_maximized
769 destroy_shell_surface(struct wl_resource *resource)
771 struct shell_surface *shsurf = resource->data;
773 if (shsurf->popup.grab.input_device)
774 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
776 /* in case cleaning up a dead client destroys shell_surface first */
778 wl_list_remove(&shsurf->surface_destroy_listener.link);
780 wl_list_remove(&shsurf->link);
785 shell_handle_surface_destroy(struct wl_listener *listener,
786 struct wl_resource *resource, uint32_t time)
788 struct shell_surface *shsurf = container_of(listener,
789 struct shell_surface,
790 surface_destroy_listener);
792 shsurf->surface = NULL;
793 wl_resource_destroy(&shsurf->resource, time);
796 static struct shell_surface *
797 get_shell_surface(struct weston_surface *surface)
799 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
800 struct wl_listener *listener;
802 /* search the destroy listener list for our callback */
803 wl_list_for_each(listener, lst, link) {
804 if (listener->func == shell_handle_surface_destroy) {
805 return container_of(listener, struct shell_surface,
806 surface_destroy_listener);
814 shell_get_shell_surface(struct wl_client *client,
815 struct wl_resource *resource,
817 struct wl_resource *surface_resource)
819 struct weston_surface *surface = surface_resource->data;
820 struct shell_surface *shsurf;
822 if (get_shell_surface(surface)) {
823 wl_resource_post_error(surface_resource,
824 WL_DISPLAY_ERROR_INVALID_OBJECT,
825 "wl_shell::get_shell_surface already requested");
829 shsurf = calloc(1, sizeof *shsurf);
831 wl_resource_post_no_memory(resource);
835 shsurf->resource.destroy = destroy_shell_surface;
836 shsurf->resource.object.id = id;
837 shsurf->resource.object.interface = &wl_shell_surface_interface;
838 shsurf->resource.object.implementation =
839 (void (**)(void)) &shell_surface_implementation;
840 shsurf->resource.data = shsurf;
842 shsurf->saved_position_valid = false;
843 shsurf->surface = surface;
844 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
845 shsurf->fullscreen.framerate = 0;
846 shsurf->fullscreen.black_surface = NULL;
847 wl_list_init(&shsurf->fullscreen.transform.link);
849 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
850 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
851 &shsurf->surface_destroy_listener.link);
853 /* init link so its safe to always remove it in destroy_shell_surface */
854 wl_list_init(&shsurf->link);
856 /* empty when not in use */
857 wl_list_init(&shsurf->rotation.transform.link);
858 weston_matrix_init(&shsurf->rotation.rotation);
860 shsurf->type = SHELL_SURFACE_NONE;
862 wl_client_add_resource(client, &shsurf->resource);
865 static const struct wl_shell_interface shell_implementation = {
866 shell_get_shell_surface
870 handle_screensaver_sigchld(struct weston_process *proc, int status)
876 launch_screensaver(struct wl_shell *shell)
878 if (shell->screensaver.binding)
881 if (!shell->screensaver.path)
884 weston_client_launch(shell->compositor,
885 &shell->screensaver.process,
886 shell->screensaver.path,
887 handle_screensaver_sigchld);
891 terminate_screensaver(struct wl_shell *shell)
893 if (shell->screensaver.process.pid == 0)
896 kill(shell->screensaver.process.pid, SIGTERM);
900 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
902 struct wl_list *list;
904 if (shell->lock_surface)
905 list = &shell->lock_surface->surface->link;
907 list = &shell->compositor->surface_list;
909 wl_list_remove(&surface->surface->link);
910 wl_list_insert(list, &surface->surface->link);
911 surface->surface->output = surface->output;
912 weston_surface_damage(surface->surface);
916 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
918 wl_list_remove(&surface->surface->link);
919 wl_list_init(&surface->surface->link);
920 surface->surface->output = NULL;
924 desktop_shell_set_background(struct wl_client *client,
925 struct wl_resource *resource,
926 struct wl_resource *output_resource,
927 struct wl_resource *surface_resource)
929 struct wl_shell *shell = resource->data;
930 struct shell_surface *shsurf = surface_resource->data;
931 struct weston_surface *surface = shsurf->surface;
932 struct shell_surface *priv;
934 if (reset_shell_surface_type(shsurf))
937 wl_list_for_each(priv, &shell->backgrounds, link) {
938 if (priv->output == output_resource->data) {
939 priv->surface->output = NULL;
940 wl_list_remove(&priv->surface->link);
941 wl_list_remove(&priv->link);
946 shsurf->type = SHELL_SURFACE_BACKGROUND;
947 shsurf->output = output_resource->data;
949 wl_list_insert(&shell->backgrounds, &shsurf->link);
951 weston_surface_set_position(surface, shsurf->output->x,
954 wl_resource_post_event(resource,
955 DESKTOP_SHELL_CONFIGURE,
956 weston_compositor_get_time(), 0, surface_resource,
957 shsurf->output->current->width,
958 shsurf->output->current->height);
962 desktop_shell_set_panel(struct wl_client *client,
963 struct wl_resource *resource,
964 struct wl_resource *output_resource,
965 struct wl_resource *surface_resource)
967 struct wl_shell *shell = resource->data;
968 struct shell_surface *shsurf = surface_resource->data;
969 struct weston_surface *surface = shsurf->surface;
970 struct shell_surface *priv;
972 if (reset_shell_surface_type(shsurf))
975 wl_list_for_each(priv, &shell->panels, link) {
976 if (priv->output == output_resource->data) {
977 priv->surface->output = NULL;
978 wl_list_remove(&priv->surface->link);
979 wl_list_remove(&priv->link);
984 shsurf->type = SHELL_SURFACE_PANEL;
985 shsurf->output = output_resource->data;
987 wl_list_insert(&shell->panels, &shsurf->link);
989 weston_surface_set_position(surface, shsurf->output->x,
992 wl_resource_post_event(resource,
993 DESKTOP_SHELL_CONFIGURE,
994 weston_compositor_get_time(), 0, surface_resource,
995 shsurf->output->current->width,
996 shsurf->output->current->height);
1000 handle_lock_surface_destroy(struct wl_listener *listener,
1001 struct wl_resource *resource, uint32_t time)
1003 struct wl_shell *shell =
1004 container_of(listener, struct wl_shell, lock_surface_listener);
1006 fprintf(stderr, "lock surface gone\n");
1007 shell->lock_surface = NULL;
1011 desktop_shell_set_lock_surface(struct wl_client *client,
1012 struct wl_resource *resource,
1013 struct wl_resource *surface_resource)
1015 struct wl_shell *shell = resource->data;
1016 struct shell_surface *surface = surface_resource->data;
1018 if (reset_shell_surface_type(surface))
1021 shell->prepare_event_sent = false;
1026 shell->lock_surface = surface;
1028 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1029 wl_list_insert(&surface_resource->destroy_listener_list,
1030 &shell->lock_surface_listener.link);
1032 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1036 resume_desktop(struct wl_shell *shell)
1038 struct weston_surface *surface;
1039 struct wl_list *list;
1040 struct shell_surface *tmp;
1042 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1043 hide_screensaver(shell, tmp);
1045 terminate_screensaver(shell);
1047 wl_list_for_each(surface, &shell->hidden_surface_list, link)
1048 weston_surface_assign_output(surface);
1050 if (wl_list_empty(&shell->backgrounds)) {
1051 list = &shell->compositor->surface_list;
1053 struct shell_surface *background;
1054 background = container_of(shell->backgrounds.prev,
1055 struct shell_surface, link);
1056 list = background->surface->link.prev;
1059 if (!wl_list_empty(&shell->hidden_surface_list))
1060 wl_list_insert_list(list, &shell->hidden_surface_list);
1061 wl_list_init(&shell->hidden_surface_list);
1063 shell->locked = false;
1064 weston_compositor_repick(shell->compositor);
1065 shell->compositor->idle_time = shell->compositor->option_idle_time;
1066 weston_compositor_wake(shell->compositor);
1067 weston_compositor_damage_all(shell->compositor);
1071 desktop_shell_unlock(struct wl_client *client,
1072 struct wl_resource *resource)
1074 struct wl_shell *shell = resource->data;
1076 shell->prepare_event_sent = false;
1079 resume_desktop(shell);
1082 static const struct desktop_shell_interface desktop_shell_implementation = {
1083 desktop_shell_set_background,
1084 desktop_shell_set_panel,
1085 desktop_shell_set_lock_surface,
1086 desktop_shell_unlock
1089 static enum shell_surface_type
1090 get_shell_surface_type(struct weston_surface *surface)
1092 struct shell_surface *shsurf;
1094 shsurf = get_shell_surface(surface);
1096 return SHELL_SURFACE_NONE;
1097 return shsurf->type;
1101 move_binding(struct wl_input_device *device, uint32_t time,
1102 uint32_t key, uint32_t button, uint32_t state, void *data)
1104 struct weston_surface *surface =
1105 (struct weston_surface *) device->pointer_focus;
1107 if (surface == NULL)
1110 switch (get_shell_surface_type(surface)) {
1111 case SHELL_SURFACE_PANEL:
1112 case SHELL_SURFACE_BACKGROUND:
1113 case SHELL_SURFACE_FULLSCREEN:
1114 case SHELL_SURFACE_SCREENSAVER:
1120 weston_surface_move(surface, (struct weston_input_device *) device, time);
1124 resize_binding(struct wl_input_device *device, uint32_t time,
1125 uint32_t key, uint32_t button, uint32_t state, void *data)
1127 struct weston_surface *surface =
1128 (struct weston_surface *) device->pointer_focus;
1131 struct shell_surface *shsurf;
1133 if (surface == NULL)
1136 shsurf = get_shell_surface(surface);
1140 switch (shsurf->type) {
1141 case SHELL_SURFACE_PANEL:
1142 case SHELL_SURFACE_BACKGROUND:
1143 case SHELL_SURFACE_FULLSCREEN:
1144 case SHELL_SURFACE_SCREENSAVER:
1150 weston_surface_from_global(surface,
1151 device->grab_x, device->grab_y, &x, &y);
1153 if (x < surface->geometry.width / 3)
1154 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1155 else if (x < 2 * surface->geometry.width / 3)
1158 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1160 if (y < surface->geometry.height / 3)
1161 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1162 else if (y < 2 * surface->geometry.height / 3)
1165 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1167 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1172 zoom_grab_key(struct wl_keyboard_grab *grab,
1173 uint32_t time, uint32_t key, int32_t state)
1175 struct wl_input_device *device = grab->input_device;
1176 struct weston_input_device *wd = (struct weston_input_device *) device;
1177 struct weston_compositor *compositor = wd->compositor;
1178 struct weston_output *output;
1179 struct weston_zoom_grab *zoom;
1181 if (!(wd->modifier_state & MODIFIER_SUPER)) {
1182 zoom = container_of(grab, struct weston_zoom_grab, grab);
1183 wl_input_device_end_keyboard_grab(device, time);
1188 wl_list_for_each(output, &compositor->output_list, link) {
1189 if (pixman_region32_contains_point(&output->region,
1190 device->x, device->y, NULL)) {
1191 if (state && key == KEY_UP) {
1192 output->zoom.active = 1;
1193 output->zoom.level -= output->zoom.increment;
1195 if (state && key == KEY_DOWN)
1196 output->zoom.level += output->zoom.increment;
1198 if (output->zoom.level >= 1.0) {
1199 output->zoom.active = 0;
1200 output->zoom.level = 1.0;
1203 if (output->zoom.level < output->zoom.increment)
1204 output->zoom.level = output->zoom.increment;
1206 weston_output_update_zoom(output, device->x, device->y);
1211 static const struct wl_keyboard_grab_interface zoom_grab = {
1216 zoom_binding(struct wl_input_device *device, uint32_t time,
1217 uint32_t key, uint32_t button, uint32_t state, void *data)
1219 struct weston_input_device *wd = (struct weston_input_device *) device;
1220 struct weston_zoom_grab *zoom;
1222 zoom = malloc(sizeof *zoom);
1226 zoom->grab.interface = &zoom_grab;
1228 wl_input_device_start_keyboard_grab(&wd->input_device, &zoom->grab, time);
1232 terminate_binding(struct wl_input_device *device, uint32_t time,
1233 uint32_t key, uint32_t button, uint32_t state, void *data)
1235 struct weston_compositor *compositor = data;
1238 wl_display_terminate(compositor->wl_display);
1242 rotate_grab_motion(struct wl_pointer_grab *grab,
1243 uint32_t time, int32_t x, int32_t y)
1245 struct rotate_grab *rotate =
1246 container_of(grab, struct rotate_grab, grab);
1247 struct wl_input_device *device = grab->input_device;
1248 struct shell_surface *surface = rotate->surface;
1249 GLfloat cx = 0.5f * surface->surface->geometry.width;
1250 GLfloat cy = 0.5f * surface->surface->geometry.height;
1254 dx = device->x - rotate->center.x;
1255 dy = device->y - rotate->center.y;
1256 r = sqrtf(dx * dx + dy * dy);
1258 wl_list_remove(&surface->rotation.transform.link);
1259 surface->surface->geometry.dirty = 1;
1262 struct weston_matrix *matrix =
1263 &surface->rotation.transform.matrix;
1265 weston_matrix_init(&rotate->rotation);
1266 rotate->rotation.d[0] = dx / r;
1267 rotate->rotation.d[4] = -dy / r;
1268 rotate->rotation.d[1] = -rotate->rotation.d[4];
1269 rotate->rotation.d[5] = rotate->rotation.d[0];
1271 weston_matrix_init(matrix);
1272 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1273 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1274 weston_matrix_multiply(matrix, &rotate->rotation);
1275 weston_matrix_translate(matrix, cx, cy, 0.0f);
1278 &surface->surface->geometry.transformation_list,
1279 &surface->rotation.transform.link);
1281 wl_list_init(&surface->rotation.transform.link);
1282 weston_matrix_init(&surface->rotation.rotation);
1283 weston_matrix_init(&rotate->rotation);
1286 /* Repaint implies weston_surface_update_transform(), which
1287 * lazily applies the damage due to rotation update.
1289 weston_compositor_schedule_repaint(surface->surface->compositor);
1293 rotate_grab_button(struct wl_pointer_grab *grab,
1294 uint32_t time, int32_t button, int32_t state)
1296 struct rotate_grab *rotate =
1297 container_of(grab, struct rotate_grab, grab);
1298 struct wl_input_device *device = grab->input_device;
1299 struct shell_surface *surface = rotate->surface;
1301 if (device->button_count == 0 && state == 0) {
1302 weston_matrix_multiply(&surface->rotation.rotation,
1304 wl_input_device_end_pointer_grab(device, time);
1309 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1316 rotate_binding(struct wl_input_device *device, uint32_t time,
1317 uint32_t key, uint32_t button, uint32_t state, void *data)
1319 struct weston_surface *base_surface =
1320 (struct weston_surface *) device->pointer_focus;
1321 struct shell_surface *surface;
1322 struct rotate_grab *rotate;
1326 if (base_surface == NULL)
1329 surface = get_shell_surface(base_surface);
1333 switch (surface->type) {
1334 case SHELL_SURFACE_PANEL:
1335 case SHELL_SURFACE_BACKGROUND:
1336 case SHELL_SURFACE_FULLSCREEN:
1337 case SHELL_SURFACE_SCREENSAVER:
1343 rotate = malloc(sizeof *rotate);
1347 rotate->grab.interface = &rotate_grab_interface;
1348 rotate->surface = surface;
1350 weston_surface_to_global(surface->surface,
1351 surface->surface->geometry.width / 2,
1352 surface->surface->geometry.height / 2,
1353 &rotate->center.x, &rotate->center.y);
1355 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1357 dx = device->x - rotate->center.x;
1358 dy = device->y - rotate->center.y;
1359 r = sqrtf(dx * dx + dy * dy);
1361 struct weston_matrix inverse;
1363 weston_matrix_init(&inverse);
1364 inverse.d[0] = dx / r;
1365 inverse.d[4] = dy / r;
1366 inverse.d[1] = -inverse.d[4];
1367 inverse.d[5] = inverse.d[0];
1368 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1370 weston_matrix_init(&surface->rotation.rotation);
1371 weston_matrix_init(&rotate->rotation);
1374 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1378 activate(struct weston_shell *base, struct weston_surface *es,
1379 struct weston_input_device *device, uint32_t time)
1381 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1382 struct weston_compositor *compositor = shell->compositor;
1383 struct wl_list *list;
1385 weston_surface_activate(es, device, time);
1387 if (compositor->wxs)
1388 weston_xserver_surface_activate(es);
1390 switch (get_shell_surface_type(es)) {
1391 case SHELL_SURFACE_BACKGROUND:
1392 /* put background back to bottom */
1393 wl_list_remove(&es->link);
1394 wl_list_insert(compositor->surface_list.prev, &es->link);
1396 case SHELL_SURFACE_PANEL:
1397 /* already put on top */
1399 case SHELL_SURFACE_SCREENSAVER:
1400 /* always below lock surface */
1401 if (shell->lock_surface) {
1402 wl_list_remove(&es->link);
1403 wl_list_insert(&shell->lock_surface->surface->link,
1407 case SHELL_SURFACE_FULLSCREEN:
1408 /* should on top of panels */
1411 if (!shell->locked) {
1412 list = weston_compositor_top(compositor);
1414 /* bring panel back to top */
1415 struct shell_surface *panel;
1416 wl_list_for_each(panel, &shell->panels, link) {
1417 wl_list_remove(&panel->surface->link);
1418 wl_list_insert(list, &panel->surface->link);
1425 click_to_activate_binding(struct wl_input_device *device,
1426 uint32_t time, uint32_t key,
1427 uint32_t button, uint32_t state, void *data)
1429 struct weston_input_device *wd = (struct weston_input_device *) device;
1430 struct weston_compositor *compositor = data;
1431 struct weston_surface *focus;
1432 struct weston_surface *upper;
1434 focus = (struct weston_surface *) device->pointer_focus;
1435 upper = container_of(focus->link.prev, struct weston_surface, link);
1436 if (focus->link.prev != &compositor->surface_list &&
1437 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1438 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1439 shell_stack_fullscreen(get_shell_surface(upper));
1443 if (state && focus && device->pointer_grab == &device->default_pointer_grab)
1444 activate(compositor->shell, focus, wd, time);
1448 lock(struct weston_shell *base)
1450 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1451 struct wl_list *surface_list = &shell->compositor->surface_list;
1452 struct weston_surface *cur;
1453 struct weston_surface *tmp;
1454 struct weston_input_device *device;
1455 struct shell_surface *shsurf;
1456 struct weston_output *output;
1459 if (shell->locked) {
1460 wl_list_for_each(output, &shell->compositor->output_list, link)
1461 /* TODO: find a way to jump to other DPMS levels */
1462 if (output->set_dpms)
1463 output->set_dpms(output, WESTON_DPMS_STANDBY);
1467 shell->locked = true;
1469 /* Move all surfaces from compositor's list to our hidden list,
1470 * except the background. This way nothing else can show or
1471 * receive input events while we are locked. */
1473 if (!wl_list_empty(&shell->hidden_surface_list)) {
1475 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1479 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1480 /* skip input device sprites, cur->surface is uninitialised */
1481 if (cur->surface.resource.client == NULL)
1484 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
1488 wl_list_remove(&cur->link);
1489 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
1492 launch_screensaver(shell);
1494 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1495 show_screensaver(shell, shsurf);
1497 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1498 shell->compositor->idle_time = shell->screensaver.duration;
1499 weston_compositor_wake(shell->compositor);
1500 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1503 /* reset pointer foci */
1504 weston_compositor_repick(shell->compositor);
1506 /* reset keyboard foci */
1507 time = weston_compositor_get_time();
1508 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1509 wl_input_device_set_keyboard_focus(&device->input_device,
1513 /* TODO: disable bindings that should not work while locked. */
1515 /* All this must be undone in resume_desktop(). */
1519 unlock(struct weston_shell *base)
1521 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1523 if (!shell->locked || shell->lock_surface) {
1524 weston_compositor_wake(shell->compositor);
1528 /* If desktop-shell client has gone away, unlock immediately. */
1529 if (!shell->child.desktop_shell) {
1530 resume_desktop(shell);
1534 if (shell->prepare_event_sent)
1537 wl_resource_post_event(shell->child.desktop_shell,
1538 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1539 shell->prepare_event_sent = true;
1543 center_on_output(struct weston_surface *surface, struct weston_output *output)
1545 struct weston_mode *mode = output->current;
1546 GLfloat x = (mode->width - surface->geometry.width) / 2;
1547 GLfloat y = (mode->height - surface->geometry.height) / 2;
1549 weston_surface_set_position(surface, output->x + x, output->y + y);
1553 map(struct weston_shell *base, struct weston_surface *surface,
1554 int32_t width, int32_t height, int32_t sx, int32_t sy)
1556 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1557 struct weston_compositor *compositor = shell->compositor;
1558 struct wl_list *list;
1559 struct shell_surface *shsurf;
1560 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1562 int panel_height = 0;
1564 shsurf = get_shell_surface(surface);
1566 surface_type = shsurf->type;
1568 if (shell->locked) {
1569 list = &shell->hidden_surface_list;
1572 list = weston_compositor_top(compositor);
1576 surface->geometry.width = width;
1577 surface->geometry.height = height;
1578 surface->geometry.dirty = 1;
1580 weston_compositor_update_drag_surfaces(compositor);
1582 /* initial positioning, see also configure() */
1583 switch (surface_type) {
1584 case SHELL_SURFACE_TOPLEVEL:
1585 weston_surface_set_position(surface, 10 + random() % 400,
1586 10 + random() % 400);
1588 case SHELL_SURFACE_SCREENSAVER:
1589 center_on_output(surface, shsurf->fullscreen_output);
1591 case SHELL_SURFACE_FULLSCREEN:
1592 shell_map_fullscreen(shsurf);
1594 case SHELL_SURFACE_MAXIMIZED:
1595 /* use surface configure to set the geometry */
1596 panel_height = get_output_panel_height(shell,surface->output);
1597 weston_surface_set_position(surface, surface->output->x,
1598 surface->output->y + panel_height);
1600 case SHELL_SURFACE_LOCK:
1601 center_on_output(surface, get_default_output(compositor));
1603 case SHELL_SURFACE_POPUP:
1604 shell_map_popup(shsurf, shsurf->popup.time);
1605 case SHELL_SURFACE_NONE:
1606 weston_surface_set_position(surface,
1607 surface->geometry.x + sx,
1608 surface->geometry.y + sy);
1614 /* surface stacking order, see also activate() */
1615 switch (surface_type) {
1616 case SHELL_SURFACE_BACKGROUND:
1617 /* background always visible, at the bottom */
1618 wl_list_insert(compositor->surface_list.prev, &surface->link);
1621 case SHELL_SURFACE_PANEL:
1622 /* panel always on top, hidden while locked */
1623 wl_list_insert(list, &surface->link);
1625 case SHELL_SURFACE_LOCK:
1626 /* lock surface always visible, on top */
1627 wl_list_insert(&compositor->surface_list, &surface->link);
1629 weston_compositor_wake(compositor);
1632 case SHELL_SURFACE_SCREENSAVER:
1633 /* If locked, show it. */
1634 if (shell->locked) {
1635 show_screensaver(shell, shsurf);
1636 compositor->idle_time = shell->screensaver.duration;
1637 weston_compositor_wake(compositor);
1638 if (!shell->lock_surface)
1639 compositor->state = WESTON_COMPOSITOR_IDLE;
1643 case SHELL_SURFACE_FULLSCREEN:
1646 case SHELL_SURFACE_NONE:
1650 /* everything else just below the panel */
1651 if (!wl_list_empty(&shell->panels)) {
1652 struct shell_surface *panel =
1653 container_of(shell->panels.prev,
1654 struct shell_surface, link);
1655 wl_list_insert(&panel->surface->link, &surface->link);
1657 wl_list_insert(list, &surface->link);
1662 weston_surface_assign_output(surface);
1663 weston_compositor_repick(compositor);
1664 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1665 surface->output = shsurf->output;
1668 switch (surface_type) {
1669 case SHELL_SURFACE_TOPLEVEL:
1670 case SHELL_SURFACE_TRANSIENT:
1671 case SHELL_SURFACE_FULLSCREEN:
1672 case SHELL_SURFACE_MAXIMIZED:
1674 activate(base, surface,
1675 (struct weston_input_device *)
1676 compositor->input_device,
1677 weston_compositor_get_time());
1683 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1684 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1688 configure(struct weston_shell *base, struct weston_surface *surface,
1689 GLfloat x, GLfloat y, int32_t width, int32_t height)
1691 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1692 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1693 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1694 struct shell_surface *shsurf;
1696 shsurf = get_shell_surface(surface);
1698 surface_type = shsurf->type;
1700 surface->geometry.x = x;
1701 surface->geometry.y = y;
1702 surface->geometry.width = width;
1703 surface->geometry.height = height;
1704 surface->geometry.dirty = 1;
1706 switch (surface_type) {
1707 case SHELL_SURFACE_SCREENSAVER:
1708 center_on_output(surface, shsurf->fullscreen_output);
1710 case SHELL_SURFACE_FULLSCREEN:
1711 shell_configure_fullscreen(shsurf);
1712 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1713 shell_stack_fullscreen(shsurf);
1715 case SHELL_SURFACE_MAXIMIZED:
1716 /* setting x, y and using configure to change that geometry */
1717 surface->geometry.x = surface->output->x;
1718 surface->geometry.y = surface->output->y +
1719 get_output_panel_height(shell,surface->output);
1721 case SHELL_SURFACE_TOPLEVEL:
1727 /* XXX: would a fullscreen surface need the same handling? */
1728 if (surface->output) {
1729 weston_surface_assign_output(surface);
1730 weston_compositor_repick(surface->compositor);
1732 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1733 surface->output = shsurf->output;
1734 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1735 surface->output = shsurf->output;
1739 static int launch_desktop_shell_process(struct wl_shell *shell);
1742 desktop_shell_sigchld(struct weston_process *process, int status)
1745 struct wl_shell *shell =
1746 container_of(process, struct wl_shell, child.process);
1748 shell->child.process.pid = 0;
1749 shell->child.client = NULL; /* already destroyed by wayland */
1751 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1752 time = weston_compositor_get_time();
1753 if (time - shell->child.deathstamp > 30000) {
1754 shell->child.deathstamp = time;
1755 shell->child.deathcount = 0;
1758 shell->child.deathcount++;
1759 if (shell->child.deathcount > 5) {
1760 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1764 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1765 launch_desktop_shell_process(shell);
1769 launch_desktop_shell_process(struct wl_shell *shell)
1771 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1773 shell->child.client = weston_client_launch(shell->compositor,
1774 &shell->child.process,
1776 desktop_shell_sigchld);
1778 if (!shell->child.client)
1784 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1786 struct wl_shell *shell = data;
1788 wl_client_add_object(client, &wl_shell_interface,
1789 &shell_implementation, id, shell);
1793 unbind_desktop_shell(struct wl_resource *resource)
1795 struct wl_shell *shell = resource->data;
1798 resume_desktop(shell);
1800 shell->child.desktop_shell = NULL;
1801 shell->prepare_event_sent = false;
1806 bind_desktop_shell(struct wl_client *client,
1807 void *data, uint32_t version, uint32_t id)
1809 struct wl_shell *shell = data;
1810 struct wl_resource *resource;
1812 resource = wl_client_add_object(client, &desktop_shell_interface,
1813 &desktop_shell_implementation,
1816 if (client == shell->child.client) {
1817 resource->destroy = unbind_desktop_shell;
1818 shell->child.desktop_shell = resource;
1822 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1823 "permission to bind desktop_shell denied");
1824 wl_resource_destroy(resource, 0);
1828 screensaver_set_surface(struct wl_client *client,
1829 struct wl_resource *resource,
1830 struct wl_resource *shell_surface_resource,
1831 struct wl_resource *output_resource)
1833 struct wl_shell *shell = resource->data;
1834 struct shell_surface *surface = shell_surface_resource->data;
1835 struct weston_output *output = output_resource->data;
1837 if (reset_shell_surface_type(surface))
1840 surface->type = SHELL_SURFACE_SCREENSAVER;
1842 surface->fullscreen_output = output;
1843 surface->output = output;
1844 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1847 static const struct screensaver_interface screensaver_implementation = {
1848 screensaver_set_surface
1852 unbind_screensaver(struct wl_resource *resource)
1854 struct wl_shell *shell = resource->data;
1856 shell->screensaver.binding = NULL;
1861 bind_screensaver(struct wl_client *client,
1862 void *data, uint32_t version, uint32_t id)
1864 struct wl_shell *shell = data;
1865 struct wl_resource *resource;
1867 resource = wl_client_add_object(client, &screensaver_interface,
1868 &screensaver_implementation,
1871 if (shell->screensaver.binding == NULL) {
1872 resource->destroy = unbind_screensaver;
1873 shell->screensaver.binding = resource;
1877 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1878 "interface object already bound");
1879 wl_resource_destroy(resource, 0);
1883 struct weston_compositor *compositor;
1884 struct weston_surface *current;
1885 struct wl_listener listener;
1886 struct wl_keyboard_grab grab;
1890 switcher_next(struct switcher *switcher)
1892 struct weston_compositor *compositor = switcher->compositor;
1893 struct weston_surface *surface;
1894 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1896 wl_list_for_each(surface, &compositor->surface_list, link) {
1897 /* Workaround for cursor surfaces. */
1898 if (surface->surface.resource.destroy_listener_list.next == NULL)
1901 switch (get_shell_surface_type(surface)) {
1902 case SHELL_SURFACE_TOPLEVEL:
1903 case SHELL_SURFACE_FULLSCREEN:
1904 case SHELL_SURFACE_MAXIMIZED:
1907 if (prev == switcher->current)
1910 surface->alpha = 64;
1911 surface->geometry.dirty = 1;
1912 weston_surface_damage(surface);
1922 wl_list_remove(&switcher->listener.link);
1923 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1924 &switcher->listener.link);
1926 switcher->current = next;
1931 switcher_handle_surface_destroy(struct wl_listener *listener,
1932 struct wl_resource *resource, uint32_t time)
1934 struct switcher *switcher =
1935 container_of(listener, struct switcher, listener);
1937 switcher_next(switcher);
1941 switcher_destroy(struct switcher *switcher, uint32_t time)
1943 struct weston_compositor *compositor = switcher->compositor;
1944 struct weston_surface *surface;
1945 struct weston_input_device *device =
1946 (struct weston_input_device *) switcher->grab.input_device;
1948 wl_list_for_each(surface, &compositor->surface_list, link) {
1949 surface->alpha = 255;
1950 weston_surface_damage(surface);
1953 activate(compositor->shell, switcher->current, device, time);
1954 wl_list_remove(&switcher->listener.link);
1955 wl_input_device_end_keyboard_grab(&device->input_device, time);
1960 switcher_key(struct wl_keyboard_grab *grab,
1961 uint32_t time, uint32_t key, int32_t state)
1963 struct switcher *switcher = container_of(grab, struct switcher, grab);
1964 struct weston_input_device *device =
1965 (struct weston_input_device *) grab->input_device;
1967 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1968 switcher_destroy(switcher, time);
1969 } else if (key == KEY_TAB && state) {
1970 switcher_next(switcher);
1974 static const struct wl_keyboard_grab_interface switcher_grab = {
1979 switcher_binding(struct wl_input_device *device, uint32_t time,
1980 uint32_t key, uint32_t button,
1981 uint32_t state, void *data)
1983 struct weston_compositor *compositor = data;
1984 struct switcher *switcher;
1986 switcher = malloc(sizeof *switcher);
1987 switcher->compositor = compositor;
1988 switcher->current = NULL;
1989 switcher->listener.func = switcher_handle_surface_destroy;
1990 wl_list_init(&switcher->listener.link);
1992 switcher->grab.interface = &switcher_grab;
1993 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1994 switcher_next(switcher);
1998 backlight_binding(struct wl_input_device *device, uint32_t time,
1999 uint32_t key, uint32_t button, uint32_t state, void *data)
2001 struct weston_compositor *compositor = data;
2002 struct weston_output *output;
2004 /* TODO: we're limiting to simple use cases, where we assume just
2005 * control on the primary display. We'd have to extend later if we
2006 * ever get support for setting backlights on random desktop LCD
2008 output = get_default_output(compositor);
2012 if (!output->set_backlight)
2015 if ((key == KEY_F9 || key == KEY_BRIGHTNESSDOWN) &&
2016 output->backlight_current > 1)
2017 output->backlight_current--;
2018 else if ((key == KEY_F10 || key == KEY_BRIGHTNESSUP) &&
2019 output->backlight_current < 10)
2020 output->backlight_current++;
2022 output->set_backlight(output, output->backlight_current);
2026 shell_destroy(struct weston_shell *base)
2028 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2030 if (shell->child.client)
2031 wl_client_destroy(shell->child.client);
2033 free(shell->screensaver.path);
2038 shell_init(struct weston_compositor *ec);
2041 shell_init(struct weston_compositor *ec)
2043 struct wl_shell *shell;
2045 shell = malloc(sizeof *shell);
2049 memset(shell, 0, sizeof *shell);
2050 shell->compositor = ec;
2051 shell->shell.lock = lock;
2052 shell->shell.unlock = unlock;
2053 shell->shell.map = map;
2054 shell->shell.configure = configure;
2055 shell->shell.destroy = shell_destroy;
2057 wl_list_init(&shell->hidden_surface_list);
2058 wl_list_init(&shell->backgrounds);
2059 wl_list_init(&shell->panels);
2060 wl_list_init(&shell->screensaver.surfaces);
2062 shell_configuration(shell);
2064 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2065 shell, bind_shell) == NULL)
2068 if (wl_display_add_global(ec->wl_display,
2069 &desktop_shell_interface,
2070 shell, bind_desktop_shell) == NULL)
2073 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2074 shell, bind_screensaver) == NULL)
2077 shell->child.deathstamp = weston_compositor_get_time();
2078 if (launch_desktop_shell_process(shell) != 0)
2081 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
2082 move_binding, shell);
2083 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
2084 resize_binding, shell);
2085 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
2086 MODIFIER_CTRL | MODIFIER_ALT,
2087 terminate_binding, ec);
2088 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2089 click_to_activate_binding, ec);
2090 weston_compositor_add_binding(ec, KEY_UP, 0, MODIFIER_SUPER,
2091 zoom_binding, shell);
2092 weston_compositor_add_binding(ec, KEY_DOWN, 0, MODIFIER_SUPER,
2093 zoom_binding, shell);
2094 weston_compositor_add_binding(ec, 0, BTN_LEFT,
2095 MODIFIER_SUPER | MODIFIER_ALT,
2096 rotate_binding, NULL);
2097 weston_compositor_add_binding(ec, KEY_TAB, 0, MODIFIER_SUPER,
2098 switcher_binding, ec);
2101 weston_compositor_add_binding(ec, KEY_F9, 0, MODIFIER_CTRL,
2102 backlight_binding, ec);
2103 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0,
2104 backlight_binding, ec);
2105 weston_compositor_add_binding(ec, KEY_F10, 0, MODIFIER_CTRL,
2106 backlight_binding, ec);
2107 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0,
2108 backlight_binding, ec);
2110 ec->shell = &shell->shell;