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;
45 struct weston_layer fullscreen_layer;
46 struct weston_layer panel_layer;
47 struct weston_layer toplevel_layer;
48 struct weston_layer background_layer;
49 struct weston_layer lock_layer;
52 struct weston_process process;
53 struct wl_client *client;
54 struct wl_resource *desktop_shell;
61 bool prepare_event_sent;
63 struct shell_surface *lock_surface;
64 struct wl_listener lock_surface_listener;
66 struct wl_list backgrounds;
67 struct wl_list panels;
72 struct wl_resource *binding;
73 struct wl_list surfaces;
74 struct weston_process process;
77 struct weston_surface *debug_repaint_surface;
80 enum shell_surface_type {
84 SHELL_SURFACE_BACKGROUND,
86 SHELL_SURFACE_SCREENSAVER,
88 SHELL_SURFACE_TOPLEVEL,
89 SHELL_SURFACE_TRANSIENT,
90 SHELL_SURFACE_FULLSCREEN,
91 SHELL_SURFACE_MAXIMIZED,
95 struct shell_surface {
96 struct wl_resource resource;
98 struct weston_surface *surface;
99 struct wl_listener surface_destroy_listener;
100 struct shell_surface *parent;
102 enum shell_surface_type type;
103 int32_t saved_x, saved_y;
104 bool saved_position_valid;
107 struct weston_transform transform;
108 struct weston_matrix rotation;
112 struct wl_pointer_grab grab;
115 struct weston_transform parent_transform;
120 enum wl_shell_surface_fullscreen_method type;
121 struct weston_transform transform; /* matrix from x, y */
123 struct weston_surface *black_surface;
126 struct weston_output *fullscreen_output;
127 struct weston_output *output;
131 struct weston_move_grab {
132 struct wl_pointer_grab grab;
133 struct weston_surface *surface;
138 struct wl_pointer_grab grab;
139 struct shell_surface *surface;
140 struct weston_matrix rotation;
148 center_on_output(struct weston_surface *surface,
149 struct weston_output *output);
152 shell_configuration(struct wl_shell *shell)
158 struct config_key saver_keys[] = {
159 { "path", CONFIG_KEY_STRING, &path },
160 { "duration", CONFIG_KEY_INTEGER, &duration },
163 struct config_section cs[] = {
164 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
167 config_file = config_file_path("weston.ini");
168 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
171 shell->screensaver.path = path;
172 shell->screensaver.duration = duration;
176 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
177 struct wl_surface *surface, int32_t x, int32_t y)
183 move_grab_motion(struct wl_pointer_grab *grab,
184 uint32_t time, int32_t x, int32_t y)
186 struct weston_move_grab *move = (struct weston_move_grab *) grab;
187 struct wl_input_device *device = grab->input_device;
188 struct weston_surface *es = move->surface;
190 weston_surface_configure(es,
191 device->x + move->dx,
192 device->y + move->dy,
193 es->geometry.width, es->geometry.height);
197 move_grab_button(struct wl_pointer_grab *grab,
198 uint32_t time, int32_t button, int32_t state)
200 struct wl_input_device *device = grab->input_device;
202 if (device->button_count == 0 && state == 0) {
203 wl_input_device_end_pointer_grab(device, time);
208 static const struct wl_pointer_grab_interface move_grab_interface = {
215 weston_surface_move(struct weston_surface *es,
216 struct weston_input_device *wd, uint32_t time)
218 struct weston_move_grab *move;
220 move = malloc(sizeof *move);
224 move->grab.interface = &move_grab_interface;
225 move->dx = es->geometry.x - wd->input_device.grab_x;
226 move->dy = es->geometry.y - wd->input_device.grab_y;
229 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
231 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
237 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
238 struct wl_resource *input_resource, uint32_t time)
240 struct weston_input_device *wd = input_resource->data;
241 struct shell_surface *shsurf = resource->data;
243 if (wd->input_device.button_count == 0 ||
244 wd->input_device.grab_time != time ||
245 wd->input_device.pointer_focus != &shsurf->surface->surface)
248 if (weston_surface_move(shsurf->surface, wd, time) < 0)
249 wl_resource_post_no_memory(resource);
252 struct weston_resize_grab {
253 struct wl_pointer_grab grab;
255 int32_t width, height;
256 struct shell_surface *shsurf;
260 resize_grab_motion(struct wl_pointer_grab *grab,
261 uint32_t time, int32_t x, int32_t y)
263 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
264 struct wl_input_device *device = grab->input_device;
265 int32_t width, height;
266 int32_t from_x, from_y;
269 weston_surface_from_global(resize->shsurf->surface,
270 device->grab_x, device->grab_y,
272 weston_surface_from_global(resize->shsurf->surface,
273 device->x, device->y, &to_x, &to_y);
275 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
276 width = resize->width + from_x - to_x;
277 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
278 width = resize->width + to_x - from_x;
280 width = resize->width;
283 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
284 height = resize->height + from_y - to_y;
285 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
286 height = resize->height + to_y - from_y;
288 height = resize->height;
291 wl_shell_surface_send_configure(&resize->shsurf->resource,
292 time, resize->edges, width, height);
296 resize_grab_button(struct wl_pointer_grab *grab,
297 uint32_t time, int32_t button, int32_t state)
299 struct wl_input_device *device = grab->input_device;
301 if (device->button_count == 0 && state == 0) {
302 wl_input_device_end_pointer_grab(device, time);
307 static const struct wl_pointer_grab_interface resize_grab_interface = {
314 weston_surface_resize(struct shell_surface *shsurf,
315 struct weston_input_device *wd,
316 uint32_t time, uint32_t edges)
318 struct weston_resize_grab *resize;
320 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
323 if (edges == 0 || edges > 15 ||
324 (edges & 3) == 3 || (edges & 12) == 12)
327 resize = malloc(sizeof *resize);
331 resize->grab.interface = &resize_grab_interface;
332 resize->edges = edges;
333 resize->width = shsurf->surface->geometry.width;
334 resize->height = shsurf->surface->geometry.height;
335 resize->shsurf = shsurf;
337 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
339 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
345 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
346 struct wl_resource *input_resource, uint32_t time,
349 struct weston_input_device *wd = input_resource->data;
350 struct shell_surface *shsurf = resource->data;
352 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
355 if (wd->input_device.button_count == 0 ||
356 wd->input_device.grab_time != time ||
357 wd->input_device.pointer_focus != &shsurf->surface->surface)
360 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
361 wl_resource_post_no_memory(resource);
364 static struct weston_output *
365 get_default_output(struct weston_compositor *compositor)
367 return container_of(compositor->output_list.next,
368 struct weston_output, link);
372 shell_unset_fullscreen(struct shell_surface *shsurf)
374 /* undo all fullscreen things here */
375 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
376 shsurf->fullscreen.framerate = 0;
377 wl_list_remove(&shsurf->fullscreen.transform.link);
378 wl_list_init(&shsurf->fullscreen.transform.link);
379 weston_surface_destroy(shsurf->fullscreen.black_surface);
380 shsurf->fullscreen.black_surface = NULL;
381 shsurf->fullscreen_output = NULL;
382 shsurf->surface->force_configure = 1;
383 weston_surface_set_position(shsurf->surface,
384 shsurf->saved_x, shsurf->saved_y);
388 reset_shell_surface_type(struct shell_surface *surface)
390 switch (surface->type) {
391 case SHELL_SURFACE_FULLSCREEN:
392 shell_unset_fullscreen(surface);
394 case SHELL_SURFACE_MAXIMIZED:
395 surface->output = get_default_output(surface->surface->compositor);
396 weston_surface_set_position(surface->surface,
400 case SHELL_SURFACE_PANEL:
401 case SHELL_SURFACE_BACKGROUND:
402 wl_list_remove(&surface->link);
403 wl_list_init(&surface->link);
405 case SHELL_SURFACE_SCREENSAVER:
406 case SHELL_SURFACE_LOCK:
407 wl_resource_post_error(&surface->resource,
408 WL_DISPLAY_ERROR_INVALID_METHOD,
409 "cannot reassign surface type");
411 case SHELL_SURFACE_NONE:
412 case SHELL_SURFACE_TOPLEVEL:
413 case SHELL_SURFACE_TRANSIENT:
414 case SHELL_SURFACE_POPUP:
418 surface->type = SHELL_SURFACE_NONE;
423 shell_surface_set_toplevel(struct wl_client *client,
424 struct wl_resource *resource)
427 struct shell_surface *surface = resource->data;
429 if (reset_shell_surface_type(surface))
432 surface->type = SHELL_SURFACE_TOPLEVEL;
436 shell_surface_set_transient(struct wl_client *client,
437 struct wl_resource *resource,
438 struct wl_resource *parent_resource,
439 int x, int y, uint32_t flags)
441 struct shell_surface *shsurf = resource->data;
442 struct weston_surface *es = shsurf->surface;
443 struct shell_surface *pshsurf = parent_resource->data;
444 struct weston_surface *pes = pshsurf->surface;
446 if (reset_shell_surface_type(shsurf))
449 /* assign to parents output */
450 shsurf->output = pes->output;
451 weston_surface_set_position(es, pes->geometry.x + x,
452 pes->geometry.y + y);
454 shsurf->type = SHELL_SURFACE_TRANSIENT;
457 static struct wl_shell *
458 shell_surface_get_shell(struct shell_surface *shsurf)
460 struct weston_surface *es = shsurf->surface;
461 struct weston_shell *shell = es->compositor->shell;
463 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
467 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
469 struct shell_surface *priv;
470 int panel_height = 0;
475 wl_list_for_each(priv, &wlshell->panels, link) {
476 if (priv->output == output) {
477 panel_height = priv->surface->geometry.height;
485 shell_surface_set_maximized(struct wl_client *client,
486 struct wl_resource *resource,
487 struct wl_resource *output_resource )
489 struct shell_surface *shsurf = resource->data;
490 struct weston_surface *es = shsurf->surface;
491 struct wl_shell *wlshell = NULL;
492 uint32_t edges = 0, panel_height = 0;
494 /* get the default output, if the client set it as NULL
495 check whether the ouput is available */
497 shsurf->output = output_resource->data;
499 shsurf->output = get_default_output(es->compositor);
501 if (reset_shell_surface_type(shsurf))
504 shsurf->saved_x = es->geometry.x;
505 shsurf->saved_y = es->geometry.y;
506 shsurf->saved_position_valid = true;
508 wlshell = shell_surface_get_shell(shsurf);
509 panel_height = get_output_panel_height(wlshell, es->output);
510 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
512 wl_shell_surface_send_configure(&shsurf->resource,
513 weston_compositor_get_time(), edges,
514 es->output->current->width,
515 es->output->current->height - panel_height);
517 shsurf->type = SHELL_SURFACE_MAXIMIZED;
520 static struct weston_surface *
521 create_black_surface(struct weston_compositor *ec,
522 GLfloat x, GLfloat y, int w, int h)
524 struct weston_surface *surface = NULL;
526 surface = weston_surface_create(ec);
527 if (surface == NULL) {
528 fprintf(stderr, "no memory\n");
532 weston_surface_configure(surface, x, y, w, h);
533 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
537 /* Create black surface and append it to the associated fullscreen surface.
538 * Handle size dismatch and positioning according to the method. */
540 shell_configure_fullscreen(struct shell_surface *shsurf)
542 struct weston_output *output = shsurf->fullscreen_output;
543 struct weston_surface *surface = shsurf->surface;
544 struct weston_matrix *matrix;
547 center_on_output(surface, output);
549 if (!shsurf->fullscreen.black_surface)
550 shsurf->fullscreen.black_surface =
551 create_black_surface(surface->compositor,
552 output->x, output->y,
553 output->current->width,
554 output->current->height);
556 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
557 wl_list_insert(&surface->layer_link,
558 &shsurf->fullscreen.black_surface->layer_link);
559 shsurf->fullscreen.black_surface->output = output;
561 switch (shsurf->fullscreen.type) {
562 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
564 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
565 matrix = &shsurf->fullscreen.transform.matrix;
566 weston_matrix_init(matrix);
567 scale = (float)output->current->width/(float)surface->geometry.width;
568 weston_matrix_scale(matrix, scale, scale, 1);
569 wl_list_remove(&shsurf->fullscreen.transform.link);
570 wl_list_insert(surface->geometry.transformation_list.prev,
571 &shsurf->fullscreen.transform.link);
572 weston_surface_set_position(surface, output->x, output->y);
574 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
576 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
583 /* make the fullscreen and black surface at the top */
585 shell_stack_fullscreen(struct shell_surface *shsurf)
587 struct weston_surface *surface = shsurf->surface;
588 struct wl_shell *shell = shell_surface_get_shell(shsurf);
590 wl_list_remove(&surface->layer_link);
591 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
593 wl_list_insert(&shell->fullscreen_layer.surface_list,
594 &surface->layer_link);
595 wl_list_insert(&surface->layer_link,
596 &shsurf->fullscreen.black_surface->layer_link);
598 weston_surface_damage(surface);
599 weston_surface_damage(shsurf->fullscreen.black_surface);
603 shell_map_fullscreen(struct shell_surface *shsurf)
605 shell_configure_fullscreen(shsurf);
606 shell_stack_fullscreen(shsurf);
610 shell_surface_set_fullscreen(struct wl_client *client,
611 struct wl_resource *resource,
614 struct wl_resource *output_resource)
616 struct shell_surface *shsurf = resource->data;
617 struct weston_surface *es = shsurf->surface;
620 shsurf->output = output_resource->data;
622 shsurf->output = get_default_output(es->compositor);
624 if (reset_shell_surface_type(shsurf))
627 shsurf->fullscreen_output = shsurf->output;
628 shsurf->fullscreen.type = method;
629 shsurf->fullscreen.framerate = framerate;
630 shsurf->type = SHELL_SURFACE_FULLSCREEN;
632 shsurf->saved_x = es->geometry.x;
633 shsurf->saved_y = es->geometry.y;
634 shsurf->saved_position_valid = true;
637 shsurf->surface->force_configure = 1;
639 wl_shell_surface_send_configure(&shsurf->resource,
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_input_device_send_motion(resource, time, sx, sy);
675 popup_grab_button(struct wl_pointer_grab *grab,
676 uint32_t time, int32_t button, int32_t state)
678 struct wl_resource *resource;
679 struct shell_surface *shsurf =
680 container_of(grab, struct shell_surface, popup.grab);
682 resource = grab->input_device->pointer_focus_resource;
684 wl_input_device_send_button(resource, time, button, state);
685 } else if (state == 0 &&
686 (shsurf->popup.initial_up ||
687 time - shsurf->popup.time > 500)) {
688 wl_shell_surface_send_popup_done(&shsurf->resource);
689 wl_input_device_end_pointer_grab(grab->input_device, time);
690 shsurf->popup.grab.input_device = NULL;
694 shsurf->popup.initial_up = 1;
697 static const struct wl_pointer_grab_interface popup_grab_interface = {
704 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
706 struct wl_input_device *device;
707 struct weston_surface *es = shsurf->surface;
708 struct weston_surface *parent = shsurf->parent->surface;
710 es->output = parent->output;
712 shsurf->popup.grab.interface = &popup_grab_interface;
713 device = es->compositor->input_device;
715 weston_surface_update_transform(parent);
716 if (parent->transform.enabled) {
717 shsurf->popup.parent_transform.matrix =
718 parent->transform.matrix;
720 /* construct x, y translation matrix */
721 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
722 shsurf->popup.parent_transform.matrix.d[12] =
724 shsurf->popup.parent_transform.matrix.d[13] =
727 wl_list_insert(es->geometry.transformation_list.prev,
728 &shsurf->popup.parent_transform.link);
729 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
731 shsurf->popup.grab.input_device = device;
732 shsurf->popup.time = device->grab_time;
733 shsurf->popup.initial_up = 0;
735 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
736 &shsurf->popup.grab, shsurf->popup.time);
740 shell_surface_set_popup(struct wl_client *client,
741 struct wl_resource *resource,
742 struct wl_resource *input_device_resource,
744 struct wl_resource *parent_resource,
745 int32_t x, int32_t y, uint32_t flags)
747 struct shell_surface *shsurf = resource->data;
749 shsurf->type = SHELL_SURFACE_POPUP;
750 shsurf->parent = parent_resource->data;
755 static const struct wl_shell_surface_interface shell_surface_implementation = {
757 shell_surface_resize,
758 shell_surface_set_toplevel,
759 shell_surface_set_transient,
760 shell_surface_set_fullscreen,
761 shell_surface_set_popup,
762 shell_surface_set_maximized
766 destroy_shell_surface(struct wl_resource *resource)
768 struct shell_surface *shsurf = resource->data;
770 if (shsurf->popup.grab.input_device)
771 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
773 /* in case cleaning up a dead client destroys shell_surface first */
775 wl_list_remove(&shsurf->surface_destroy_listener.link);
777 if (shsurf->fullscreen.black_surface)
778 weston_surface_destroy(shsurf->fullscreen.black_surface);
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 if (shell->screensaver.process.pid != 0) {
885 fprintf(stderr, "old screensaver still running\n");
889 weston_client_launch(shell->compositor,
890 &shell->screensaver.process,
891 shell->screensaver.path,
892 handle_screensaver_sigchld);
896 terminate_screensaver(struct wl_shell *shell)
898 if (shell->screensaver.process.pid == 0)
901 kill(shell->screensaver.process.pid, SIGTERM);
905 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
907 struct wl_list *list;
909 if (shell->lock_surface)
910 list = &shell->lock_surface->surface->layer_link;
912 list = &shell->lock_layer.surface_list;
914 wl_list_remove(&surface->surface->layer_link);
915 wl_list_insert(list, &surface->surface->layer_link);
916 surface->surface->output = surface->output;
917 weston_surface_damage(surface->surface);
921 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
923 wl_list_remove(&surface->surface->layer_link);
924 wl_list_init(&surface->surface->layer_link);
925 surface->surface->output = NULL;
929 desktop_shell_set_background(struct wl_client *client,
930 struct wl_resource *resource,
931 struct wl_resource *output_resource,
932 struct wl_resource *surface_resource)
934 struct wl_shell *shell = resource->data;
935 struct shell_surface *shsurf = surface_resource->data;
936 struct weston_surface *surface = shsurf->surface;
937 struct shell_surface *priv;
939 if (reset_shell_surface_type(shsurf))
942 wl_list_for_each(priv, &shell->backgrounds, link) {
943 if (priv->output == output_resource->data) {
944 priv->surface->output = NULL;
945 wl_list_remove(&priv->surface->layer_link);
946 wl_list_remove(&priv->link);
951 shsurf->type = SHELL_SURFACE_BACKGROUND;
952 shsurf->output = output_resource->data;
954 wl_list_insert(&shell->backgrounds, &shsurf->link);
956 weston_surface_set_position(surface, shsurf->output->x,
959 desktop_shell_send_configure(resource,
960 weston_compositor_get_time(), 0,
962 shsurf->output->current->width,
963 shsurf->output->current->height);
967 desktop_shell_set_panel(struct wl_client *client,
968 struct wl_resource *resource,
969 struct wl_resource *output_resource,
970 struct wl_resource *surface_resource)
972 struct wl_shell *shell = resource->data;
973 struct shell_surface *shsurf = surface_resource->data;
974 struct weston_surface *surface = shsurf->surface;
975 struct shell_surface *priv;
977 if (reset_shell_surface_type(shsurf))
980 wl_list_for_each(priv, &shell->panels, link) {
981 if (priv->output == output_resource->data) {
982 priv->surface->output = NULL;
983 wl_list_remove(&priv->surface->layer_link);
984 wl_list_remove(&priv->link);
989 shsurf->type = SHELL_SURFACE_PANEL;
990 shsurf->output = output_resource->data;
992 wl_list_insert(&shell->panels, &shsurf->link);
994 weston_surface_set_position(surface, shsurf->output->x,
997 desktop_shell_send_configure(resource,
998 weston_compositor_get_time(), 0,
1000 shsurf->output->current->width,
1001 shsurf->output->current->height);
1005 handle_lock_surface_destroy(struct wl_listener *listener,
1006 struct wl_resource *resource, uint32_t time)
1008 struct wl_shell *shell =
1009 container_of(listener, struct wl_shell, lock_surface_listener);
1011 fprintf(stderr, "lock surface gone\n");
1012 shell->lock_surface = NULL;
1016 desktop_shell_set_lock_surface(struct wl_client *client,
1017 struct wl_resource *resource,
1018 struct wl_resource *surface_resource)
1020 struct wl_shell *shell = resource->data;
1021 struct shell_surface *surface = surface_resource->data;
1023 if (reset_shell_surface_type(surface))
1026 shell->prepare_event_sent = false;
1031 shell->lock_surface = surface;
1033 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1034 wl_list_insert(&surface_resource->destroy_listener_list,
1035 &shell->lock_surface_listener.link);
1037 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1041 resume_desktop(struct wl_shell *shell)
1043 struct shell_surface *tmp;
1045 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1046 hide_screensaver(shell, tmp);
1048 terminate_screensaver(shell);
1050 wl_list_remove(&shell->lock_layer.link);
1051 wl_list_insert(&shell->compositor->cursor_layer.link,
1052 &shell->fullscreen_layer.link);
1053 wl_list_insert(&shell->fullscreen_layer.link,
1054 &shell->panel_layer.link);
1055 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1057 shell->locked = false;
1058 shell->compositor->idle_time = shell->compositor->option_idle_time;
1059 weston_compositor_wake(shell->compositor);
1060 weston_compositor_damage_all(shell->compositor);
1064 desktop_shell_unlock(struct wl_client *client,
1065 struct wl_resource *resource)
1067 struct wl_shell *shell = resource->data;
1069 shell->prepare_event_sent = false;
1072 resume_desktop(shell);
1075 static const struct desktop_shell_interface desktop_shell_implementation = {
1076 desktop_shell_set_background,
1077 desktop_shell_set_panel,
1078 desktop_shell_set_lock_surface,
1079 desktop_shell_unlock
1082 static enum shell_surface_type
1083 get_shell_surface_type(struct weston_surface *surface)
1085 struct shell_surface *shsurf;
1087 shsurf = get_shell_surface(surface);
1089 return SHELL_SURFACE_NONE;
1090 return shsurf->type;
1094 move_binding(struct wl_input_device *device, uint32_t time,
1095 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1097 struct weston_surface *surface =
1098 (struct weston_surface *) device->pointer_focus;
1100 if (surface == NULL)
1103 switch (get_shell_surface_type(surface)) {
1104 case SHELL_SURFACE_PANEL:
1105 case SHELL_SURFACE_BACKGROUND:
1106 case SHELL_SURFACE_FULLSCREEN:
1107 case SHELL_SURFACE_SCREENSAVER:
1113 weston_surface_move(surface, (struct weston_input_device *) device, time);
1117 resize_binding(struct wl_input_device *device, uint32_t time,
1118 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1120 struct weston_surface *surface =
1121 (struct weston_surface *) device->pointer_focus;
1124 struct shell_surface *shsurf;
1126 if (surface == NULL)
1129 shsurf = get_shell_surface(surface);
1133 switch (shsurf->type) {
1134 case SHELL_SURFACE_PANEL:
1135 case SHELL_SURFACE_BACKGROUND:
1136 case SHELL_SURFACE_FULLSCREEN:
1137 case SHELL_SURFACE_SCREENSAVER:
1143 weston_surface_from_global(surface,
1144 device->grab_x, device->grab_y, &x, &y);
1146 if (x < surface->geometry.width / 3)
1147 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1148 else if (x < 2 * surface->geometry.width / 3)
1151 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1153 if (y < surface->geometry.height / 3)
1154 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1155 else if (y < 2 * surface->geometry.height / 3)
1158 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1160 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1165 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1166 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1169 struct shell_surface *shsurf;
1170 struct weston_surface *surface =
1171 (struct weston_surface *) device->pointer_focus;
1173 if (surface == NULL)
1176 shsurf = get_shell_surface(surface);
1180 switch (shsurf->type) {
1181 case SHELL_SURFACE_BACKGROUND:
1182 case SHELL_SURFACE_SCREENSAVER:
1188 surface->alpha += value * step;
1190 if (surface->alpha > 255)
1191 surface->alpha = 255;
1192 if (surface->alpha < step)
1193 surface->alpha = step;
1195 surface->geometry.dirty = 1;
1196 weston_surface_damage(surface);
1200 zoom_binding(struct wl_input_device *device, uint32_t time,
1201 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1203 struct weston_input_device *wd = (struct weston_input_device *) device;
1204 struct weston_compositor *compositor = wd->compositor;
1205 struct weston_output *output;
1207 wl_list_for_each(output, &compositor->output_list, link) {
1208 if (pixman_region32_contains_point(&output->region,
1209 device->x, device->y, NULL)) {
1210 output->zoom.active = 1;
1211 output->zoom.level += output->zoom.increment * -value;
1213 if (output->zoom.level >= 1.0) {
1214 output->zoom.active = 0;
1215 output->zoom.level = 1.0;
1218 if (output->zoom.level < output->zoom.increment)
1219 output->zoom.level = output->zoom.increment;
1221 weston_output_update_zoom(output, device->x, device->y);
1227 terminate_binding(struct wl_input_device *device, uint32_t time,
1228 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1230 struct weston_compositor *compositor = data;
1233 wl_display_terminate(compositor->wl_display);
1237 rotate_grab_motion(struct wl_pointer_grab *grab,
1238 uint32_t time, int32_t x, int32_t y)
1240 struct rotate_grab *rotate =
1241 container_of(grab, struct rotate_grab, grab);
1242 struct wl_input_device *device = grab->input_device;
1243 struct shell_surface *surface = rotate->surface;
1244 struct weston_surface *base_surface = surface->surface;
1245 GLfloat cx = 0.5f * base_surface->geometry.width;
1246 GLfloat cy = 0.5f * base_surface->geometry.height;
1247 GLfloat dx, dy, cposx, cposy, dposx, dposy;
1250 dx = device->x - rotate->center.x;
1251 dy = device->y - rotate->center.y;
1252 r = sqrtf(dx * dx + dy * dy);
1254 wl_list_remove(&surface->rotation.transform.link);
1255 surface->surface->geometry.dirty = 1;
1258 struct weston_matrix *matrix =
1259 &surface->rotation.transform.matrix;
1261 weston_matrix_init(&rotate->rotation);
1262 rotate->rotation.d[0] = dx / r;
1263 rotate->rotation.d[4] = -dy / r;
1264 rotate->rotation.d[1] = -rotate->rotation.d[4];
1265 rotate->rotation.d[5] = rotate->rotation.d[0];
1267 weston_matrix_init(matrix);
1268 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1269 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1270 weston_matrix_multiply(matrix, &rotate->rotation);
1271 weston_matrix_translate(matrix, cx, cy, 0.0f);
1274 &surface->surface->geometry.transformation_list,
1275 &surface->rotation.transform.link);
1277 wl_list_init(&surface->rotation.transform.link);
1278 weston_matrix_init(&surface->rotation.rotation);
1279 weston_matrix_init(&rotate->rotation);
1282 /* We need to adjust the position of the surface
1283 * in case it was resized in a rotated state before */
1284 cposx = base_surface->geometry.x + cx;
1285 cposy = base_surface->geometry.y + cy;
1286 dposx = rotate->center.x - cposx;
1287 dposy = rotate->center.y - cposy;
1288 if (dposx != 0.0f || dposy != 0.0f) {
1289 weston_surface_set_position(base_surface,
1290 base_surface->geometry.x + dposx,
1291 base_surface->geometry.y + dposy);
1294 /* Repaint implies weston_surface_update_transform(), which
1295 * lazily applies the damage due to rotation update.
1297 weston_compositor_schedule_repaint(surface->surface->compositor);
1301 rotate_grab_button(struct wl_pointer_grab *grab,
1302 uint32_t time, int32_t button, int32_t state)
1304 struct rotate_grab *rotate =
1305 container_of(grab, struct rotate_grab, grab);
1306 struct wl_input_device *device = grab->input_device;
1307 struct shell_surface *surface = rotate->surface;
1309 if (device->button_count == 0 && state == 0) {
1310 weston_matrix_multiply(&surface->rotation.rotation,
1312 wl_input_device_end_pointer_grab(device, time);
1317 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1324 rotate_binding(struct wl_input_device *device, uint32_t time,
1325 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1327 struct weston_surface *base_surface =
1328 (struct weston_surface *) device->pointer_focus;
1329 struct shell_surface *surface;
1330 struct rotate_grab *rotate;
1334 if (base_surface == NULL)
1337 surface = get_shell_surface(base_surface);
1341 switch (surface->type) {
1342 case SHELL_SURFACE_PANEL:
1343 case SHELL_SURFACE_BACKGROUND:
1344 case SHELL_SURFACE_FULLSCREEN:
1345 case SHELL_SURFACE_SCREENSAVER:
1351 rotate = malloc(sizeof *rotate);
1355 rotate->grab.interface = &rotate_grab_interface;
1356 rotate->surface = surface;
1358 weston_surface_to_global(surface->surface,
1359 surface->surface->geometry.width / 2,
1360 surface->surface->geometry.height / 2,
1361 &rotate->center.x, &rotate->center.y);
1363 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1365 dx = device->x - rotate->center.x;
1366 dy = device->y - rotate->center.y;
1367 r = sqrtf(dx * dx + dy * dy);
1369 struct weston_matrix inverse;
1371 weston_matrix_init(&inverse);
1372 inverse.d[0] = dx / r;
1373 inverse.d[4] = dy / r;
1374 inverse.d[1] = -inverse.d[4];
1375 inverse.d[5] = inverse.d[0];
1376 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1378 weston_matrix_init(&rotate->rotation);
1379 rotate->rotation.d[0] = dx / r;
1380 rotate->rotation.d[4] = -dy / r;
1381 rotate->rotation.d[1] = -rotate->rotation.d[4];
1382 rotate->rotation.d[5] = rotate->rotation.d[0];
1384 weston_matrix_init(&surface->rotation.rotation);
1385 weston_matrix_init(&rotate->rotation);
1388 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1392 activate(struct weston_shell *base, struct weston_surface *es,
1393 struct weston_input_device *device, uint32_t time)
1395 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1396 struct weston_compositor *compositor = shell->compositor;
1398 weston_surface_activate(es, device, time);
1400 if (compositor->wxs)
1401 weston_xserver_surface_activate(es);
1403 switch (get_shell_surface_type(es)) {
1404 case SHELL_SURFACE_BACKGROUND:
1405 case SHELL_SURFACE_PANEL:
1406 case SHELL_SURFACE_LOCK:
1409 case SHELL_SURFACE_SCREENSAVER:
1410 /* always below lock surface */
1411 if (shell->lock_surface)
1412 weston_surface_restack(es,
1413 &shell->lock_surface->surface->layer_link);
1415 case SHELL_SURFACE_FULLSCREEN:
1416 /* should on top of panels */
1419 weston_surface_restack(es,
1420 &shell->toplevel_layer.surface_list);
1426 click_to_activate_binding(struct wl_input_device *device,
1427 uint32_t time, uint32_t key,
1428 uint32_t button, uint32_t axis, int32_t state, void *data)
1430 struct weston_input_device *wd = (struct weston_input_device *) device;
1431 struct weston_compositor *compositor = data;
1432 struct weston_surface *focus;
1433 struct weston_surface *upper;
1435 focus = (struct weston_surface *) device->pointer_focus;
1439 upper = container_of(focus->link.prev, struct weston_surface, link);
1440 if (focus->link.prev != &compositor->surface_list &&
1441 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1442 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1443 shell_stack_fullscreen(get_shell_surface(upper));
1447 if (state && device->pointer_grab == &device->default_pointer_grab)
1448 activate(compositor->shell, focus, wd, time);
1452 lock(struct weston_shell *base)
1454 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1455 struct weston_input_device *device;
1456 struct shell_surface *shsurf;
1457 struct weston_output *output;
1460 if (shell->locked) {
1461 wl_list_for_each(output, &shell->compositor->output_list, link)
1462 /* TODO: find a way to jump to other DPMS levels */
1463 if (output->set_dpms)
1464 output->set_dpms(output, WESTON_DPMS_STANDBY);
1468 shell->locked = true;
1470 /* Hide all surfaces by removing the fullscreen, panel and
1471 * toplevel layers. This way nothing else can show or receive
1472 * input events while we are locked. */
1474 wl_list_remove(&shell->panel_layer.link);
1475 wl_list_remove(&shell->toplevel_layer.link);
1476 wl_list_remove(&shell->fullscreen_layer.link);
1477 wl_list_insert(&shell->compositor->cursor_layer.link,
1478 &shell->lock_layer.link);
1480 launch_screensaver(shell);
1482 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1483 show_screensaver(shell, shsurf);
1485 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1486 shell->compositor->idle_time = shell->screensaver.duration;
1487 weston_compositor_wake(shell->compositor);
1488 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1491 /* reset pointer foci */
1492 weston_compositor_schedule_repaint(shell->compositor);
1494 /* reset keyboard foci */
1495 time = weston_compositor_get_time();
1496 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1497 wl_input_device_set_keyboard_focus(&device->input_device,
1501 /* TODO: disable bindings that should not work while locked. */
1503 /* All this must be undone in resume_desktop(). */
1507 unlock(struct weston_shell *base)
1509 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1511 if (!shell->locked || shell->lock_surface) {
1512 weston_compositor_wake(shell->compositor);
1516 /* If desktop-shell client has gone away, unlock immediately. */
1517 if (!shell->child.desktop_shell) {
1518 resume_desktop(shell);
1522 if (shell->prepare_event_sent)
1525 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1526 shell->prepare_event_sent = true;
1530 center_on_output(struct weston_surface *surface, struct weston_output *output)
1532 struct weston_mode *mode = output->current;
1533 GLfloat x = (mode->width - surface->geometry.width) / 2;
1534 GLfloat y = (mode->height - surface->geometry.height) / 2;
1536 weston_surface_set_position(surface, output->x + x, output->y + y);
1540 map(struct weston_shell *base, struct weston_surface *surface,
1541 int32_t width, int32_t height, int32_t sx, int32_t sy)
1543 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1544 struct weston_compositor *compositor = shell->compositor;
1545 struct shell_surface *shsurf;
1546 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1547 struct weston_surface *parent;
1548 int panel_height = 0;
1550 shsurf = get_shell_surface(surface);
1552 surface_type = shsurf->type;
1554 surface->geometry.width = width;
1555 surface->geometry.height = height;
1556 surface->geometry.dirty = 1;
1558 weston_compositor_update_drag_surfaces(compositor);
1560 /* initial positioning, see also configure() */
1561 switch (surface_type) {
1562 case SHELL_SURFACE_TOPLEVEL:
1563 weston_surface_set_position(surface, 10 + random() % 400,
1564 10 + random() % 400);
1566 case SHELL_SURFACE_SCREENSAVER:
1567 center_on_output(surface, shsurf->fullscreen_output);
1569 case SHELL_SURFACE_FULLSCREEN:
1570 shell_map_fullscreen(shsurf);
1572 case SHELL_SURFACE_MAXIMIZED:
1573 /* use surface configure to set the geometry */
1574 panel_height = get_output_panel_height(shell,surface->output);
1575 weston_surface_set_position(surface, surface->output->x,
1576 surface->output->y + panel_height);
1578 case SHELL_SURFACE_LOCK:
1579 center_on_output(surface, get_default_output(compositor));
1581 case SHELL_SURFACE_POPUP:
1582 shell_map_popup(shsurf, shsurf->popup.time);
1583 case SHELL_SURFACE_NONE:
1584 weston_surface_set_position(surface,
1585 surface->geometry.x + sx,
1586 surface->geometry.y + sy);
1592 /* surface stacking order, see also activate() */
1593 switch (surface_type) {
1594 case SHELL_SURFACE_BACKGROUND:
1595 /* background always visible, at the bottom */
1596 wl_list_insert(&shell->background_layer.surface_list,
1597 &surface->layer_link);
1599 case SHELL_SURFACE_PANEL:
1600 /* panel always on top, hidden while locked */
1601 wl_list_insert(&shell->panel_layer.surface_list,
1602 &surface->layer_link);
1604 case SHELL_SURFACE_LOCK:
1605 /* lock surface always visible, on top */
1606 wl_list_insert(&shell->lock_layer.surface_list,
1607 &surface->layer_link);
1608 weston_compositor_wake(compositor);
1610 case SHELL_SURFACE_SCREENSAVER:
1611 /* If locked, show it. */
1612 if (shell->locked) {
1613 show_screensaver(shell, shsurf);
1614 compositor->idle_time = shell->screensaver.duration;
1615 weston_compositor_wake(compositor);
1616 if (!shell->lock_surface)
1617 compositor->state = WESTON_COMPOSITOR_IDLE;
1620 case SHELL_SURFACE_POPUP:
1621 case SHELL_SURFACE_TRANSIENT:
1622 parent = shsurf->parent->surface;
1623 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1625 case SHELL_SURFACE_FULLSCREEN:
1626 case SHELL_SURFACE_NONE:
1629 wl_list_insert(&shell->toplevel_layer.surface_list,
1630 &surface->layer_link);
1634 if (surface_type != SHELL_SURFACE_NONE) {
1635 weston_surface_assign_output(surface);
1636 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1637 surface->output = shsurf->output;
1640 switch (surface_type) {
1641 case SHELL_SURFACE_TOPLEVEL:
1642 case SHELL_SURFACE_TRANSIENT:
1643 case SHELL_SURFACE_FULLSCREEN:
1644 case SHELL_SURFACE_MAXIMIZED:
1646 activate(base, surface,
1647 (struct weston_input_device *)
1648 compositor->input_device,
1649 weston_compositor_get_time());
1655 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1656 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1660 configure(struct weston_shell *base, struct weston_surface *surface,
1661 GLfloat x, GLfloat y, int32_t width, int32_t height)
1663 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1664 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1665 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1666 struct shell_surface *shsurf;
1668 shsurf = get_shell_surface(surface);
1670 surface_type = shsurf->type;
1672 surface->geometry.x = x;
1673 surface->geometry.y = y;
1674 surface->geometry.width = width;
1675 surface->geometry.height = height;
1676 surface->geometry.dirty = 1;
1678 switch (surface_type) {
1679 case SHELL_SURFACE_SCREENSAVER:
1680 center_on_output(surface, shsurf->fullscreen_output);
1682 case SHELL_SURFACE_FULLSCREEN:
1683 shell_configure_fullscreen(shsurf);
1684 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1685 shell_stack_fullscreen(shsurf);
1687 case SHELL_SURFACE_MAXIMIZED:
1688 /* setting x, y and using configure to change that geometry */
1689 surface->geometry.x = surface->output->x;
1690 surface->geometry.y = surface->output->y +
1691 get_output_panel_height(shell,surface->output);
1693 case SHELL_SURFACE_TOPLEVEL:
1699 /* XXX: would a fullscreen surface need the same handling? */
1700 if (surface->output) {
1701 weston_surface_assign_output(surface);
1703 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1704 surface->output = shsurf->output;
1705 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1706 surface->output = shsurf->output;
1710 static int launch_desktop_shell_process(struct wl_shell *shell);
1713 desktop_shell_sigchld(struct weston_process *process, int status)
1716 struct wl_shell *shell =
1717 container_of(process, struct wl_shell, child.process);
1719 shell->child.process.pid = 0;
1720 shell->child.client = NULL; /* already destroyed by wayland */
1722 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1723 time = weston_compositor_get_time();
1724 if (time - shell->child.deathstamp > 30000) {
1725 shell->child.deathstamp = time;
1726 shell->child.deathcount = 0;
1729 shell->child.deathcount++;
1730 if (shell->child.deathcount > 5) {
1731 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1735 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1736 launch_desktop_shell_process(shell);
1740 launch_desktop_shell_process(struct wl_shell *shell)
1742 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1744 shell->child.client = weston_client_launch(shell->compositor,
1745 &shell->child.process,
1747 desktop_shell_sigchld);
1749 if (!shell->child.client)
1755 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1757 struct wl_shell *shell = data;
1759 wl_client_add_object(client, &wl_shell_interface,
1760 &shell_implementation, id, shell);
1764 unbind_desktop_shell(struct wl_resource *resource)
1766 struct wl_shell *shell = resource->data;
1769 resume_desktop(shell);
1771 shell->child.desktop_shell = NULL;
1772 shell->prepare_event_sent = false;
1777 bind_desktop_shell(struct wl_client *client,
1778 void *data, uint32_t version, uint32_t id)
1780 struct wl_shell *shell = data;
1781 struct wl_resource *resource;
1783 resource = wl_client_add_object(client, &desktop_shell_interface,
1784 &desktop_shell_implementation,
1787 if (client == shell->child.client) {
1788 resource->destroy = unbind_desktop_shell;
1789 shell->child.desktop_shell = resource;
1793 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1794 "permission to bind desktop_shell denied");
1795 wl_resource_destroy(resource, 0);
1799 screensaver_set_surface(struct wl_client *client,
1800 struct wl_resource *resource,
1801 struct wl_resource *shell_surface_resource,
1802 struct wl_resource *output_resource)
1804 struct wl_shell *shell = resource->data;
1805 struct shell_surface *surface = shell_surface_resource->data;
1806 struct weston_output *output = output_resource->data;
1808 if (reset_shell_surface_type(surface))
1811 surface->type = SHELL_SURFACE_SCREENSAVER;
1813 surface->fullscreen_output = output;
1814 surface->output = output;
1815 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1818 static const struct screensaver_interface screensaver_implementation = {
1819 screensaver_set_surface
1823 unbind_screensaver(struct wl_resource *resource)
1825 struct wl_shell *shell = resource->data;
1827 shell->screensaver.binding = NULL;
1832 bind_screensaver(struct wl_client *client,
1833 void *data, uint32_t version, uint32_t id)
1835 struct wl_shell *shell = data;
1836 struct wl_resource *resource;
1838 resource = wl_client_add_object(client, &screensaver_interface,
1839 &screensaver_implementation,
1842 if (shell->screensaver.binding == NULL) {
1843 resource->destroy = unbind_screensaver;
1844 shell->screensaver.binding = resource;
1848 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1849 "interface object already bound");
1850 wl_resource_destroy(resource, 0);
1854 struct weston_compositor *compositor;
1855 struct weston_surface *current;
1856 struct wl_listener listener;
1857 struct wl_keyboard_grab grab;
1861 switcher_next(struct switcher *switcher)
1863 struct weston_compositor *compositor = switcher->compositor;
1864 struct weston_surface *surface;
1865 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1867 wl_list_for_each(surface, &compositor->surface_list, link) {
1868 /* Workaround for cursor surfaces. */
1869 if (surface->surface.resource.destroy_listener_list.next == NULL)
1872 switch (get_shell_surface_type(surface)) {
1873 case SHELL_SURFACE_TOPLEVEL:
1874 case SHELL_SURFACE_FULLSCREEN:
1875 case SHELL_SURFACE_MAXIMIZED:
1878 if (prev == switcher->current)
1881 surface->alpha = 64;
1882 surface->geometry.dirty = 1;
1883 weston_surface_damage(surface);
1896 wl_list_remove(&switcher->listener.link);
1897 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1898 &switcher->listener.link);
1900 switcher->current = next;
1905 switcher_handle_surface_destroy(struct wl_listener *listener,
1906 struct wl_resource *resource, uint32_t time)
1908 struct switcher *switcher =
1909 container_of(listener, struct switcher, listener);
1911 switcher_next(switcher);
1915 switcher_destroy(struct switcher *switcher, uint32_t time)
1917 struct weston_compositor *compositor = switcher->compositor;
1918 struct weston_surface *surface;
1919 struct weston_input_device *device =
1920 (struct weston_input_device *) switcher->grab.input_device;
1922 wl_list_for_each(surface, &compositor->surface_list, link) {
1923 surface->alpha = 255;
1924 weston_surface_damage(surface);
1927 if (switcher->current)
1928 activate(compositor->shell, switcher->current, device, time);
1929 wl_list_remove(&switcher->listener.link);
1930 wl_input_device_end_keyboard_grab(&device->input_device, time);
1935 switcher_key(struct wl_keyboard_grab *grab,
1936 uint32_t time, uint32_t key, int32_t state)
1938 struct switcher *switcher = container_of(grab, struct switcher, grab);
1939 struct weston_input_device *device =
1940 (struct weston_input_device *) grab->input_device;
1942 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1943 switcher_destroy(switcher, time);
1944 } else if (key == KEY_TAB && state) {
1945 switcher_next(switcher);
1949 static const struct wl_keyboard_grab_interface switcher_grab = {
1954 switcher_binding(struct wl_input_device *device, uint32_t time,
1955 uint32_t key, uint32_t button, uint32_t axis,
1956 int32_t state, void *data)
1958 struct weston_compositor *compositor = data;
1959 struct switcher *switcher;
1961 switcher = malloc(sizeof *switcher);
1962 switcher->compositor = compositor;
1963 switcher->current = NULL;
1964 switcher->listener.func = switcher_handle_surface_destroy;
1965 wl_list_init(&switcher->listener.link);
1967 switcher->grab.interface = &switcher_grab;
1968 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
1969 wl_input_device_set_keyboard_focus(device, NULL,
1970 weston_compositor_get_time());
1971 switcher_next(switcher);
1975 backlight_binding(struct wl_input_device *device, uint32_t time,
1976 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1978 struct weston_compositor *compositor = data;
1979 struct weston_output *output;
1980 long backlight_new = 0;
1982 /* TODO: we're limiting to simple use cases, where we assume just
1983 * control on the primary display. We'd have to extend later if we
1984 * ever get support for setting backlights on random desktop LCD
1986 output = get_default_output(compositor);
1990 if (!output->set_backlight)
1993 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
1994 backlight_new = output->backlight_current - 25;
1995 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
1996 backlight_new = output->backlight_current + 25;
1998 if (backlight_new < 5)
2000 if (backlight_new > 255)
2001 backlight_new = 255;
2003 output->backlight_current = backlight_new;
2004 output->set_backlight(output, output->backlight_current);
2008 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2009 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2011 struct weston_compositor *compositor = data;
2012 struct wl_shell *shell =
2013 container_of(compositor->shell, struct wl_shell, shell);
2014 struct weston_surface *surface;
2016 if (shell->debug_repaint_surface) {
2017 weston_surface_destroy(shell->debug_repaint_surface);
2018 shell->debug_repaint_surface = NULL;
2020 surface = weston_surface_create(compositor);
2021 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2022 weston_surface_configure(surface, 0, 0, 8192, 8192);
2023 wl_list_insert(&compositor->fade_layer.surface_list,
2024 &surface->layer_link);
2025 weston_surface_assign_output(surface);
2026 pixman_region32_init(&surface->input);
2028 /* Here's the dirty little trick that makes the
2029 * repaint debugging work: we force an
2030 * update_transform first to update dependent state
2031 * and clear the geometry.dirty bit. Then we clear
2032 * the surface damage so it only gets repainted
2033 * piecewise as we repaint other things. */
2035 weston_surface_update_transform(surface);
2036 pixman_region32_fini(&surface->damage);
2037 pixman_region32_init(&surface->damage);
2038 shell->debug_repaint_surface = surface;
2043 shell_destroy(struct weston_shell *base)
2045 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2047 if (shell->child.client)
2048 wl_client_destroy(shell->child.client);
2050 free(shell->screensaver.path);
2055 shell_init(struct weston_compositor *ec);
2058 shell_init(struct weston_compositor *ec)
2060 struct wl_shell *shell;
2062 shell = malloc(sizeof *shell);
2066 memset(shell, 0, sizeof *shell);
2067 shell->compositor = ec;
2068 shell->shell.lock = lock;
2069 shell->shell.unlock = unlock;
2070 shell->shell.map = map;
2071 shell->shell.configure = configure;
2072 shell->shell.destroy = shell_destroy;
2074 wl_list_init(&shell->backgrounds);
2075 wl_list_init(&shell->panels);
2076 wl_list_init(&shell->screensaver.surfaces);
2078 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2079 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2080 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2081 weston_layer_init(&shell->background_layer,
2082 &shell->toplevel_layer.link);
2083 wl_list_init(&shell->lock_layer.surface_list);
2085 shell_configuration(shell);
2087 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2088 shell, bind_shell) == NULL)
2091 if (wl_display_add_global(ec->wl_display,
2092 &desktop_shell_interface,
2093 shell, bind_desktop_shell) == NULL)
2096 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2097 shell, bind_screensaver) == NULL)
2100 shell->child.deathstamp = weston_compositor_get_time();
2101 if (launch_desktop_shell_process(shell) != 0)
2104 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2105 move_binding, shell);
2106 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2107 resize_binding, shell);
2108 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2109 MODIFIER_CTRL | MODIFIER_ALT,
2110 terminate_binding, ec);
2111 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2112 click_to_activate_binding, ec);
2113 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2114 MODIFIER_SUPER | MODIFIER_ALT,
2115 surface_opacity_binding, NULL);
2116 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2117 MODIFIER_SUPER, zoom_binding, NULL);
2118 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2119 MODIFIER_SUPER | MODIFIER_ALT,
2120 rotate_binding, NULL);
2121 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2122 switcher_binding, ec);
2125 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2126 backlight_binding, ec);
2127 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2128 backlight_binding, ec);
2129 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2130 backlight_binding, ec);
2131 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2132 backlight_binding, ec);
2134 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2135 debug_repaint_binding, ec);
2137 ec->shell = &shell->shell;