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;
44 struct wl_listener lock_listener;
45 struct wl_listener unlock_listener;
46 struct wl_listener destroy_listener;
48 struct weston_layer fullscreen_layer;
49 struct weston_layer panel_layer;
50 struct weston_layer toplevel_layer;
51 struct weston_layer background_layer;
52 struct weston_layer lock_layer;
55 struct weston_process process;
56 struct wl_client *client;
57 struct wl_resource *desktop_shell;
64 bool prepare_event_sent;
66 struct shell_surface *lock_surface;
67 struct wl_listener lock_surface_listener;
69 struct wl_list backgrounds;
70 struct wl_list panels;
75 struct wl_resource *binding;
76 struct wl_list surfaces;
77 struct weston_process process;
80 struct weston_surface *debug_repaint_surface;
83 enum shell_surface_type {
87 SHELL_SURFACE_BACKGROUND,
89 SHELL_SURFACE_SCREENSAVER,
91 SHELL_SURFACE_TOPLEVEL,
92 SHELL_SURFACE_TRANSIENT,
93 SHELL_SURFACE_FULLSCREEN,
94 SHELL_SURFACE_MAXIMIZED,
98 struct shell_surface {
99 struct wl_resource resource;
101 struct weston_surface *surface;
102 struct wl_listener surface_destroy_listener;
103 struct shell_surface *parent;
104 struct wl_shell *shell;
106 enum shell_surface_type type;
107 int32_t saved_x, saved_y;
108 bool saved_position_valid;
111 struct weston_transform transform;
112 struct weston_matrix rotation;
116 struct wl_pointer_grab grab;
119 struct weston_transform parent_transform;
124 enum wl_shell_surface_fullscreen_method type;
125 struct weston_transform transform; /* matrix from x, y */
127 struct weston_surface *black_surface;
130 struct weston_output *fullscreen_output;
131 struct weston_output *output;
138 struct wl_pointer_grab grab;
139 struct shell_surface *shsurf;
140 struct wl_listener shsurf_destroy_listener;
143 struct weston_move_grab {
144 struct shell_grab base;
149 struct shell_grab base;
150 struct weston_matrix rotation;
158 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
160 struct shell_grab *grab;
162 grab = container_of(listener, struct shell_grab,
163 shsurf_destroy_listener);
169 shell_grab_init(struct shell_grab *grab,
170 const struct wl_pointer_grab_interface *interface,
171 struct shell_surface *shsurf)
173 grab->grab.interface = interface;
174 grab->shsurf = shsurf;
175 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
176 wl_signal_add(&shsurf->resource.destroy_signal,
177 &grab->shsurf_destroy_listener);
182 shell_grab_finish(struct shell_grab *grab)
184 wl_list_remove(&grab->shsurf_destroy_listener.link);
188 center_on_output(struct weston_surface *surface,
189 struct weston_output *output);
191 static struct shell_surface *
192 get_shell_surface(struct weston_surface *surface);
195 shell_configuration(struct wl_shell *shell)
201 struct config_key saver_keys[] = {
202 { "path", CONFIG_KEY_STRING, &path },
203 { "duration", CONFIG_KEY_INTEGER, &duration },
206 struct config_section cs[] = {
207 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
210 config_file = config_file_path("weston.ini");
211 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
214 shell->screensaver.path = path;
215 shell->screensaver.duration = duration;
219 noop_grab_focus(struct wl_pointer_grab *grab,
220 struct wl_surface *surface, int32_t x, int32_t y)
226 move_grab_motion(struct wl_pointer_grab *grab,
227 uint32_t time, int32_t x, int32_t y)
229 struct weston_move_grab *move = (struct weston_move_grab *) grab;
230 struct wl_input_device *device = grab->input_device;
231 struct shell_surface *shsurf = move->base.shsurf;
232 struct weston_surface *es;
237 es = shsurf->surface;
239 weston_surface_configure(es,
240 device->x + move->dx,
241 device->y + move->dy,
242 es->geometry.width, es->geometry.height);
246 move_grab_button(struct wl_pointer_grab *grab,
247 uint32_t time, uint32_t button, int32_t state)
249 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
251 struct wl_input_device *device = grab->input_device;
253 if (device->button_count == 0 && state == 0) {
254 shell_grab_finish(shell_grab);
255 wl_input_device_end_pointer_grab(device);
260 static const struct wl_pointer_grab_interface move_grab_interface = {
267 weston_surface_move(struct weston_surface *es,
268 struct weston_input_device *wd)
270 struct weston_move_grab *move;
271 struct shell_surface *shsurf = get_shell_surface(es);
276 move = malloc(sizeof *move);
280 shell_grab_init(&move->base, &move_grab_interface, shsurf);
282 move->dx = es->geometry.x - wd->input_device.grab_x;
283 move->dy = es->geometry.y - wd->input_device.grab_y;
285 wl_input_device_start_pointer_grab(&wd->input_device,
288 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
294 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
295 struct wl_resource *input_resource, uint32_t serial)
297 struct weston_input_device *wd = input_resource->data;
298 struct shell_surface *shsurf = resource->data;
300 if (wd->input_device.button_count == 0 ||
301 wd->input_device.grab_serial != serial ||
302 wd->input_device.pointer_focus != &shsurf->surface->surface)
305 if (weston_surface_move(shsurf->surface, wd) < 0)
306 wl_resource_post_no_memory(resource);
309 struct weston_resize_grab {
310 struct shell_grab base;
312 int32_t width, height;
316 resize_grab_motion(struct wl_pointer_grab *grab,
317 uint32_t time, int32_t x, int32_t y)
319 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
320 struct wl_input_device *device = grab->input_device;
321 int32_t width, height;
322 int32_t from_x, from_y;
325 if (!resize->base.shsurf)
328 weston_surface_from_global(resize->base.shsurf->surface,
329 device->grab_x, device->grab_y,
331 weston_surface_from_global(resize->base.shsurf->surface,
332 device->x, device->y, &to_x, &to_y);
334 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
335 width = resize->width + from_x - to_x;
336 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
337 width = resize->width + to_x - from_x;
339 width = resize->width;
342 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
343 height = resize->height + from_y - to_y;
344 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
345 height = resize->height + to_y - from_y;
347 height = resize->height;
350 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
351 resize->edges, width, height);
355 resize_grab_button(struct wl_pointer_grab *grab,
356 uint32_t time, uint32_t button, int32_t state)
358 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
359 struct wl_input_device *device = grab->input_device;
361 if (device->button_count == 0 && state == 0) {
362 shell_grab_finish(&resize->base);
363 wl_input_device_end_pointer_grab(device);
368 static const struct wl_pointer_grab_interface resize_grab_interface = {
375 weston_surface_resize(struct shell_surface *shsurf,
376 struct weston_input_device *wd, uint32_t edges)
378 struct weston_resize_grab *resize;
380 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
383 if (edges == 0 || edges > 15 ||
384 (edges & 3) == 3 || (edges & 12) == 12)
387 resize = malloc(sizeof *resize);
391 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
393 resize->edges = edges;
394 resize->width = shsurf->surface->geometry.width;
395 resize->height = shsurf->surface->geometry.height;
397 wl_input_device_start_pointer_grab(&wd->input_device,
400 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
406 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
407 struct wl_resource *input_resource, uint32_t serial,
410 struct weston_input_device *wd = input_resource->data;
411 struct shell_surface *shsurf = resource->data;
413 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
416 if (wd->input_device.button_count == 0 ||
417 wd->input_device.grab_serial != serial ||
418 wd->input_device.pointer_focus != &shsurf->surface->surface)
421 if (weston_surface_resize(shsurf, wd, edges) < 0)
422 wl_resource_post_no_memory(resource);
425 static struct weston_output *
426 get_default_output(struct weston_compositor *compositor)
428 return container_of(compositor->output_list.next,
429 struct weston_output, link);
433 shell_unset_fullscreen(struct shell_surface *shsurf)
435 /* undo all fullscreen things here */
436 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
437 shsurf->fullscreen.framerate = 0;
438 wl_list_remove(&shsurf->fullscreen.transform.link);
439 wl_list_init(&shsurf->fullscreen.transform.link);
440 weston_surface_destroy(shsurf->fullscreen.black_surface);
441 shsurf->fullscreen.black_surface = NULL;
442 shsurf->fullscreen_output = NULL;
443 shsurf->force_configure = 1;
444 weston_surface_set_position(shsurf->surface,
445 shsurf->saved_x, shsurf->saved_y);
449 reset_shell_surface_type(struct shell_surface *surface)
451 switch (surface->type) {
452 case SHELL_SURFACE_FULLSCREEN:
453 shell_unset_fullscreen(surface);
455 case SHELL_SURFACE_MAXIMIZED:
456 surface->output = get_default_output(surface->surface->compositor);
457 weston_surface_set_position(surface->surface,
461 case SHELL_SURFACE_PANEL:
462 case SHELL_SURFACE_BACKGROUND:
463 wl_list_remove(&surface->link);
464 wl_list_init(&surface->link);
466 case SHELL_SURFACE_SCREENSAVER:
467 case SHELL_SURFACE_LOCK:
468 wl_resource_post_error(&surface->resource,
469 WL_DISPLAY_ERROR_INVALID_METHOD,
470 "cannot reassign surface type");
472 case SHELL_SURFACE_NONE:
473 case SHELL_SURFACE_TOPLEVEL:
474 case SHELL_SURFACE_TRANSIENT:
475 case SHELL_SURFACE_POPUP:
479 surface->type = SHELL_SURFACE_NONE;
484 shell_surface_set_toplevel(struct wl_client *client,
485 struct wl_resource *resource)
488 struct shell_surface *surface = resource->data;
490 if (reset_shell_surface_type(surface))
493 surface->type = SHELL_SURFACE_TOPLEVEL;
497 shell_surface_set_transient(struct wl_client *client,
498 struct wl_resource *resource,
499 struct wl_resource *parent_resource,
500 int x, int y, uint32_t flags)
502 struct shell_surface *shsurf = resource->data;
503 struct weston_surface *es = shsurf->surface;
504 struct shell_surface *pshsurf = parent_resource->data;
505 struct weston_surface *pes = pshsurf->surface;
507 if (reset_shell_surface_type(shsurf))
510 /* assign to parents output */
511 shsurf->output = pes->output;
512 weston_surface_set_position(es, pes->geometry.x + x,
513 pes->geometry.y + y);
515 shsurf->type = SHELL_SURFACE_TRANSIENT;
518 static struct wl_shell *
519 shell_surface_get_shell(struct shell_surface *shsurf)
521 return shsurf->shell;
525 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
527 struct shell_surface *priv;
528 int panel_height = 0;
533 wl_list_for_each(priv, &wlshell->panels, link) {
534 if (priv->output == output) {
535 panel_height = priv->surface->geometry.height;
543 shell_surface_set_maximized(struct wl_client *client,
544 struct wl_resource *resource,
545 struct wl_resource *output_resource )
547 struct shell_surface *shsurf = resource->data;
548 struct weston_surface *es = shsurf->surface;
549 struct wl_shell *wlshell = NULL;
550 uint32_t edges = 0, panel_height = 0;
552 /* get the default output, if the client set it as NULL
553 check whether the ouput is available */
555 shsurf->output = output_resource->data;
557 shsurf->output = get_default_output(es->compositor);
559 if (reset_shell_surface_type(shsurf))
562 shsurf->saved_x = es->geometry.x;
563 shsurf->saved_y = es->geometry.y;
564 shsurf->saved_position_valid = true;
566 wlshell = shell_surface_get_shell(shsurf);
567 panel_height = get_output_panel_height(wlshell, es->output);
568 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
570 wl_shell_surface_send_configure(&shsurf->resource, edges,
571 es->output->current->width,
572 es->output->current->height - panel_height);
574 shsurf->type = SHELL_SURFACE_MAXIMIZED;
578 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
580 static struct weston_surface *
581 create_black_surface(struct weston_compositor *ec,
582 struct weston_surface *fs_surface,
583 GLfloat x, GLfloat y, int w, int h)
585 struct weston_surface *surface = NULL;
587 surface = weston_surface_create(ec);
588 if (surface == NULL) {
589 fprintf(stderr, "no memory\n");
593 surface->configure = black_surface_configure;
594 surface->private = fs_surface;
595 weston_surface_configure(surface, x, y, w, h);
596 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
600 /* Create black surface and append it to the associated fullscreen surface.
601 * Handle size dismatch and positioning according to the method. */
603 shell_configure_fullscreen(struct shell_surface *shsurf)
605 struct weston_output *output = shsurf->fullscreen_output;
606 struct weston_surface *surface = shsurf->surface;
607 struct weston_matrix *matrix;
610 center_on_output(surface, output);
612 if (!shsurf->fullscreen.black_surface)
613 shsurf->fullscreen.black_surface =
614 create_black_surface(surface->compositor,
616 output->x, output->y,
617 output->current->width,
618 output->current->height);
620 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
621 wl_list_insert(&surface->layer_link,
622 &shsurf->fullscreen.black_surface->layer_link);
623 shsurf->fullscreen.black_surface->output = output;
625 switch (shsurf->fullscreen.type) {
626 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
628 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
629 matrix = &shsurf->fullscreen.transform.matrix;
630 weston_matrix_init(matrix);
631 scale = (float)output->current->width/(float)surface->geometry.width;
632 weston_matrix_scale(matrix, scale, scale, 1);
633 wl_list_remove(&shsurf->fullscreen.transform.link);
634 wl_list_insert(surface->geometry.transformation_list.prev,
635 &shsurf->fullscreen.transform.link);
636 weston_surface_set_position(surface, output->x, output->y);
638 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
640 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
647 /* make the fullscreen and black surface at the top */
649 shell_stack_fullscreen(struct shell_surface *shsurf)
651 struct weston_surface *surface = shsurf->surface;
652 struct wl_shell *shell = shell_surface_get_shell(shsurf);
654 wl_list_remove(&surface->layer_link);
655 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
657 wl_list_insert(&shell->fullscreen_layer.surface_list,
658 &surface->layer_link);
659 wl_list_insert(&surface->layer_link,
660 &shsurf->fullscreen.black_surface->layer_link);
662 weston_surface_damage(surface);
663 weston_surface_damage(shsurf->fullscreen.black_surface);
667 shell_map_fullscreen(struct shell_surface *shsurf)
669 shell_configure_fullscreen(shsurf);
670 shell_stack_fullscreen(shsurf);
674 shell_surface_set_fullscreen(struct wl_client *client,
675 struct wl_resource *resource,
678 struct wl_resource *output_resource)
680 struct shell_surface *shsurf = resource->data;
681 struct weston_surface *es = shsurf->surface;
684 shsurf->output = output_resource->data;
686 shsurf->output = get_default_output(es->compositor);
688 if (reset_shell_surface_type(shsurf))
691 shsurf->fullscreen_output = shsurf->output;
692 shsurf->fullscreen.type = method;
693 shsurf->fullscreen.framerate = framerate;
694 shsurf->type = SHELL_SURFACE_FULLSCREEN;
696 shsurf->saved_x = es->geometry.x;
697 shsurf->saved_y = es->geometry.y;
698 shsurf->saved_position_valid = true;
700 if (weston_surface_is_mapped(es))
701 shsurf->force_configure = 1;
703 wl_shell_surface_send_configure(&shsurf->resource, 0,
704 shsurf->output->current->width,
705 shsurf->output->current->height);
709 popup_grab_focus(struct wl_pointer_grab *grab,
710 struct wl_surface *surface, int32_t x, int32_t y)
712 struct wl_input_device *device = grab->input_device;
713 struct shell_surface *priv =
714 container_of(grab, struct shell_surface, popup.grab);
715 struct wl_client *client = priv->surface->surface.resource.client;
717 if (surface && surface->resource.client == client) {
718 wl_input_device_set_pointer_focus(device, surface, x, y);
719 grab->focus = surface;
721 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
727 popup_grab_motion(struct wl_pointer_grab *grab,
728 uint32_t time, int32_t sx, int32_t sy)
730 struct wl_resource *resource;
732 resource = grab->input_device->pointer_focus_resource;
734 wl_input_device_send_motion(resource, time, sx, sy);
738 popup_grab_button(struct wl_pointer_grab *grab,
739 uint32_t time, uint32_t button, int32_t state)
741 struct wl_resource *resource;
742 struct shell_surface *shsurf =
743 container_of(grab, struct shell_surface, popup.grab);
744 struct wl_display *display;
747 resource = grab->input_device->pointer_focus_resource;
749 display = wl_client_get_display(resource->client);
750 serial = wl_display_get_serial(display);
751 wl_input_device_send_button(resource, serial,
752 time, button, state);
753 } else if (state == 0 &&
754 (shsurf->popup.initial_up ||
755 time - shsurf->popup.time > 500)) {
756 wl_shell_surface_send_popup_done(&shsurf->resource);
757 wl_input_device_end_pointer_grab(grab->input_device);
758 shsurf->popup.grab.input_device = NULL;
762 shsurf->popup.initial_up = 1;
765 static const struct wl_pointer_grab_interface popup_grab_interface = {
772 shell_map_popup(struct shell_surface *shsurf, uint32_t serial)
774 struct wl_input_device *device;
775 struct weston_surface *es = shsurf->surface;
776 struct weston_surface *parent = shsurf->parent->surface;
778 es->output = parent->output;
780 shsurf->popup.grab.interface = &popup_grab_interface;
781 device = es->compositor->input_device;
783 weston_surface_update_transform(parent);
784 if (parent->transform.enabled) {
785 shsurf->popup.parent_transform.matrix =
786 parent->transform.matrix;
788 /* construct x, y translation matrix */
789 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
790 shsurf->popup.parent_transform.matrix.d[12] =
792 shsurf->popup.parent_transform.matrix.d[13] =
795 wl_list_insert(es->geometry.transformation_list.prev,
796 &shsurf->popup.parent_transform.link);
797 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
799 shsurf->popup.grab.input_device = device;
800 shsurf->popup.time = device->grab_time;
801 shsurf->popup.initial_up = 0;
803 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
804 &shsurf->popup.grab);
808 shell_surface_set_popup(struct wl_client *client,
809 struct wl_resource *resource,
810 struct wl_resource *input_device_resource,
812 struct wl_resource *parent_resource,
813 int32_t x, int32_t y, uint32_t flags)
815 struct shell_surface *shsurf = resource->data;
817 shsurf->type = SHELL_SURFACE_POPUP;
818 shsurf->parent = parent_resource->data;
823 static const struct wl_shell_surface_interface shell_surface_implementation = {
825 shell_surface_resize,
826 shell_surface_set_toplevel,
827 shell_surface_set_transient,
828 shell_surface_set_fullscreen,
829 shell_surface_set_popup,
830 shell_surface_set_maximized
834 destroy_shell_surface(struct wl_resource *resource)
836 struct shell_surface *shsurf = resource->data;
838 if (shsurf->popup.grab.input_device)
839 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
841 /* in case cleaning up a dead client destroys shell_surface first */
842 if (shsurf->surface) {
843 wl_list_remove(&shsurf->surface_destroy_listener.link);
844 shsurf->surface->configure = NULL;
847 if (shsurf->fullscreen.black_surface)
848 weston_surface_destroy(shsurf->fullscreen.black_surface);
850 wl_list_remove(&shsurf->link);
855 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
857 struct shell_surface *shsurf = container_of(listener,
858 struct shell_surface,
859 surface_destroy_listener);
861 shsurf->surface = NULL;
862 wl_resource_destroy(&shsurf->resource);
865 static struct shell_surface *
866 get_shell_surface(struct weston_surface *surface)
868 struct wl_listener *listener;
870 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
871 shell_handle_surface_destroy);
873 return container_of(listener, struct shell_surface,
874 surface_destroy_listener);
880 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
883 shell_get_shell_surface(struct wl_client *client,
884 struct wl_resource *resource,
886 struct wl_resource *surface_resource)
888 struct weston_surface *surface = surface_resource->data;
889 struct shell_surface *shsurf;
891 if (get_shell_surface(surface)) {
892 wl_resource_post_error(surface_resource,
893 WL_DISPLAY_ERROR_INVALID_OBJECT,
894 "wl_shell::get_shell_surface already requested");
898 if (surface->configure) {
899 wl_resource_post_error(surface_resource,
900 WL_DISPLAY_ERROR_INVALID_OBJECT,
901 "surface->configure already set");
905 shsurf = calloc(1, sizeof *shsurf);
907 wl_resource_post_no_memory(resource);
911 surface->configure = shell_surface_configure;
913 shsurf->resource.destroy = destroy_shell_surface;
914 shsurf->resource.object.id = id;
915 shsurf->resource.object.interface = &wl_shell_surface_interface;
916 shsurf->resource.object.implementation =
917 (void (**)(void)) &shell_surface_implementation;
918 shsurf->resource.data = shsurf;
920 shsurf->shell = resource->data;
921 shsurf->saved_position_valid = false;
922 shsurf->surface = surface;
923 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
924 shsurf->fullscreen.framerate = 0;
925 shsurf->fullscreen.black_surface = NULL;
926 wl_list_init(&shsurf->fullscreen.transform.link);
928 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
929 wl_signal_add(&surface->surface.resource.destroy_signal,
930 &shsurf->surface_destroy_listener);
932 /* init link so its safe to always remove it in destroy_shell_surface */
933 wl_list_init(&shsurf->link);
935 /* empty when not in use */
936 wl_list_init(&shsurf->rotation.transform.link);
937 weston_matrix_init(&shsurf->rotation.rotation);
939 shsurf->type = SHELL_SURFACE_NONE;
941 wl_client_add_resource(client, &shsurf->resource);
944 static const struct wl_shell_interface shell_implementation = {
945 shell_get_shell_surface
949 handle_screensaver_sigchld(struct weston_process *proc, int status)
955 launch_screensaver(struct wl_shell *shell)
957 if (shell->screensaver.binding)
960 if (!shell->screensaver.path)
963 if (shell->screensaver.process.pid != 0) {
964 fprintf(stderr, "old screensaver still running\n");
968 weston_client_launch(shell->compositor,
969 &shell->screensaver.process,
970 shell->screensaver.path,
971 handle_screensaver_sigchld);
975 terminate_screensaver(struct wl_shell *shell)
977 if (shell->screensaver.process.pid == 0)
980 kill(shell->screensaver.process.pid, SIGTERM);
984 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
986 struct wl_list *list;
988 if (shell->lock_surface)
989 list = &shell->lock_surface->surface->layer_link;
991 list = &shell->lock_layer.surface_list;
993 wl_list_remove(&surface->surface->layer_link);
994 wl_list_insert(list, &surface->surface->layer_link);
995 surface->surface->output = surface->output;
996 weston_surface_damage(surface->surface);
1000 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
1002 wl_list_remove(&surface->surface->layer_link);
1003 wl_list_init(&surface->surface->layer_link);
1004 surface->surface->output = NULL;
1008 desktop_shell_set_background(struct wl_client *client,
1009 struct wl_resource *resource,
1010 struct wl_resource *output_resource,
1011 struct wl_resource *surface_resource)
1013 struct wl_shell *shell = resource->data;
1014 struct shell_surface *shsurf = surface_resource->data;
1015 struct weston_surface *surface = shsurf->surface;
1016 struct shell_surface *priv;
1018 if (reset_shell_surface_type(shsurf))
1021 wl_list_for_each(priv, &shell->backgrounds, link) {
1022 if (priv->output == output_resource->data) {
1023 priv->surface->output = NULL;
1024 wl_list_remove(&priv->surface->layer_link);
1025 wl_list_remove(&priv->link);
1030 shsurf->type = SHELL_SURFACE_BACKGROUND;
1031 shsurf->output = output_resource->data;
1033 wl_list_insert(&shell->backgrounds, &shsurf->link);
1035 weston_surface_set_position(surface, shsurf->output->x,
1038 desktop_shell_send_configure(resource, 0,
1040 shsurf->output->current->width,
1041 shsurf->output->current->height);
1045 desktop_shell_set_panel(struct wl_client *client,
1046 struct wl_resource *resource,
1047 struct wl_resource *output_resource,
1048 struct wl_resource *surface_resource)
1050 struct wl_shell *shell = resource->data;
1051 struct shell_surface *shsurf = surface_resource->data;
1052 struct weston_surface *surface = shsurf->surface;
1053 struct shell_surface *priv;
1055 if (reset_shell_surface_type(shsurf))
1058 wl_list_for_each(priv, &shell->panels, link) {
1059 if (priv->output == output_resource->data) {
1060 priv->surface->output = NULL;
1061 wl_list_remove(&priv->surface->layer_link);
1062 wl_list_remove(&priv->link);
1067 shsurf->type = SHELL_SURFACE_PANEL;
1068 shsurf->output = output_resource->data;
1070 wl_list_insert(&shell->panels, &shsurf->link);
1072 weston_surface_set_position(surface, shsurf->output->x,
1075 desktop_shell_send_configure(resource, 0,
1077 shsurf->output->current->width,
1078 shsurf->output->current->height);
1082 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
1084 struct wl_shell *shell =
1085 container_of(listener, struct wl_shell, lock_surface_listener);
1087 fprintf(stderr, "lock surface gone\n");
1088 shell->lock_surface = NULL;
1092 desktop_shell_set_lock_surface(struct wl_client *client,
1093 struct wl_resource *resource,
1094 struct wl_resource *surface_resource)
1096 struct wl_shell *shell = resource->data;
1097 struct shell_surface *surface = surface_resource->data;
1099 if (reset_shell_surface_type(surface))
1102 shell->prepare_event_sent = false;
1107 shell->lock_surface = surface;
1109 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1110 wl_signal_add(&surface_resource->destroy_signal,
1111 &shell->lock_surface_listener);
1113 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1117 resume_desktop(struct wl_shell *shell)
1119 struct shell_surface *tmp;
1121 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1122 hide_screensaver(shell, tmp);
1124 terminate_screensaver(shell);
1126 wl_list_remove(&shell->lock_layer.link);
1127 wl_list_insert(&shell->compositor->cursor_layer.link,
1128 &shell->fullscreen_layer.link);
1129 wl_list_insert(&shell->fullscreen_layer.link,
1130 &shell->panel_layer.link);
1131 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1133 shell->locked = false;
1134 shell->compositor->idle_time = shell->compositor->option_idle_time;
1135 weston_compositor_wake(shell->compositor);
1136 weston_compositor_damage_all(shell->compositor);
1140 desktop_shell_unlock(struct wl_client *client,
1141 struct wl_resource *resource)
1143 struct wl_shell *shell = resource->data;
1145 shell->prepare_event_sent = false;
1148 resume_desktop(shell);
1151 static const struct desktop_shell_interface desktop_shell_implementation = {
1152 desktop_shell_set_background,
1153 desktop_shell_set_panel,
1154 desktop_shell_set_lock_surface,
1155 desktop_shell_unlock
1158 static enum shell_surface_type
1159 get_shell_surface_type(struct weston_surface *surface)
1161 struct shell_surface *shsurf;
1163 shsurf = get_shell_surface(surface);
1165 return SHELL_SURFACE_NONE;
1166 return shsurf->type;
1170 move_binding(struct wl_input_device *device, uint32_t time,
1171 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1173 struct weston_surface *surface =
1174 (struct weston_surface *) device->pointer_focus;
1176 if (surface == NULL)
1179 switch (get_shell_surface_type(surface)) {
1180 case SHELL_SURFACE_PANEL:
1181 case SHELL_SURFACE_BACKGROUND:
1182 case SHELL_SURFACE_FULLSCREEN:
1183 case SHELL_SURFACE_SCREENSAVER:
1189 weston_surface_move(surface, (struct weston_input_device *) device);
1193 resize_binding(struct wl_input_device *device, uint32_t time,
1194 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1196 struct weston_surface *surface =
1197 (struct weston_surface *) device->pointer_focus;
1200 struct shell_surface *shsurf;
1202 if (surface == NULL)
1205 shsurf = get_shell_surface(surface);
1209 switch (shsurf->type) {
1210 case SHELL_SURFACE_PANEL:
1211 case SHELL_SURFACE_BACKGROUND:
1212 case SHELL_SURFACE_FULLSCREEN:
1213 case SHELL_SURFACE_SCREENSAVER:
1219 weston_surface_from_global(surface,
1220 device->grab_x, device->grab_y, &x, &y);
1222 if (x < surface->geometry.width / 3)
1223 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1224 else if (x < 2 * surface->geometry.width / 3)
1227 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1229 if (y < surface->geometry.height / 3)
1230 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1231 else if (y < 2 * surface->geometry.height / 3)
1234 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1236 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1241 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1242 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1245 struct shell_surface *shsurf;
1246 struct weston_surface *surface =
1247 (struct weston_surface *) device->pointer_focus;
1249 if (surface == NULL)
1252 shsurf = get_shell_surface(surface);
1256 switch (shsurf->type) {
1257 case SHELL_SURFACE_BACKGROUND:
1258 case SHELL_SURFACE_SCREENSAVER:
1264 surface->alpha += value * step;
1266 if (surface->alpha > 255)
1267 surface->alpha = 255;
1268 if (surface->alpha < step)
1269 surface->alpha = step;
1271 surface->geometry.dirty = 1;
1272 weston_surface_damage(surface);
1276 zoom_binding(struct wl_input_device *device, uint32_t time,
1277 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1279 struct weston_input_device *wd = (struct weston_input_device *) device;
1280 struct weston_compositor *compositor = wd->compositor;
1281 struct weston_output *output;
1283 wl_list_for_each(output, &compositor->output_list, link) {
1284 if (pixman_region32_contains_point(&output->region,
1285 device->x, device->y, NULL)) {
1286 output->zoom.active = 1;
1287 output->zoom.level += output->zoom.increment * -value;
1289 if (output->zoom.level >= 1.0) {
1290 output->zoom.active = 0;
1291 output->zoom.level = 1.0;
1294 if (output->zoom.level < output->zoom.increment)
1295 output->zoom.level = output->zoom.increment;
1297 weston_output_update_zoom(output, device->x, device->y);
1303 terminate_binding(struct wl_input_device *device, uint32_t time,
1304 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1306 struct weston_compositor *compositor = data;
1309 wl_display_terminate(compositor->wl_display);
1313 rotate_grab_motion(struct wl_pointer_grab *grab,
1314 uint32_t time, int32_t x, int32_t y)
1316 struct rotate_grab *rotate =
1317 container_of(grab, struct rotate_grab, base.grab);
1318 struct wl_input_device *device = grab->input_device;
1319 struct shell_surface *shsurf = rotate->base.shsurf;
1320 struct weston_surface *surface;
1321 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1326 surface = shsurf->surface;
1328 cx = 0.5f * surface->geometry.width;
1329 cy = 0.5f * surface->geometry.height;
1331 dx = device->x - rotate->center.x;
1332 dy = device->y - rotate->center.y;
1333 r = sqrtf(dx * dx + dy * dy);
1335 wl_list_remove(&shsurf->rotation.transform.link);
1336 shsurf->surface->geometry.dirty = 1;
1339 struct weston_matrix *matrix =
1340 &shsurf->rotation.transform.matrix;
1342 weston_matrix_init(&rotate->rotation);
1343 rotate->rotation.d[0] = dx / r;
1344 rotate->rotation.d[4] = -dy / r;
1345 rotate->rotation.d[1] = -rotate->rotation.d[4];
1346 rotate->rotation.d[5] = rotate->rotation.d[0];
1348 weston_matrix_init(matrix);
1349 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1350 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
1351 weston_matrix_multiply(matrix, &rotate->rotation);
1352 weston_matrix_translate(matrix, cx, cy, 0.0f);
1355 &shsurf->surface->geometry.transformation_list,
1356 &shsurf->rotation.transform.link);
1358 wl_list_init(&shsurf->rotation.transform.link);
1359 weston_matrix_init(&shsurf->rotation.rotation);
1360 weston_matrix_init(&rotate->rotation);
1363 /* We need to adjust the position of the surface
1364 * in case it was resized in a rotated state before */
1365 cposx = surface->geometry.x + cx;
1366 cposy = surface->geometry.y + cy;
1367 dposx = rotate->center.x - cposx;
1368 dposy = rotate->center.y - cposy;
1369 if (dposx != 0.0f || dposy != 0.0f) {
1370 weston_surface_set_position(surface,
1371 surface->geometry.x + dposx,
1372 surface->geometry.y + dposy);
1375 /* Repaint implies weston_surface_update_transform(), which
1376 * lazily applies the damage due to rotation update.
1378 weston_compositor_schedule_repaint(shsurf->surface->compositor);
1382 rotate_grab_button(struct wl_pointer_grab *grab,
1383 uint32_t time, uint32_t button, int32_t state)
1385 struct rotate_grab *rotate =
1386 container_of(grab, struct rotate_grab, base.grab);
1387 struct wl_input_device *device = grab->input_device;
1388 struct shell_surface *shsurf = rotate->base.shsurf;
1390 if (device->button_count == 0 && state == 0) {
1392 weston_matrix_multiply(&shsurf->rotation.rotation,
1394 shell_grab_finish(&rotate->base);
1395 wl_input_device_end_pointer_grab(device);
1400 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1407 rotate_binding(struct wl_input_device *device, uint32_t time,
1408 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1410 struct weston_surface *base_surface =
1411 (struct weston_surface *) device->pointer_focus;
1412 struct shell_surface *surface;
1413 struct rotate_grab *rotate;
1417 if (base_surface == NULL)
1420 surface = get_shell_surface(base_surface);
1424 switch (surface->type) {
1425 case SHELL_SURFACE_PANEL:
1426 case SHELL_SURFACE_BACKGROUND:
1427 case SHELL_SURFACE_FULLSCREEN:
1428 case SHELL_SURFACE_SCREENSAVER:
1434 rotate = malloc(sizeof *rotate);
1438 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
1440 weston_surface_to_global(surface->surface,
1441 surface->surface->geometry.width / 2,
1442 surface->surface->geometry.height / 2,
1443 &rotate->center.x, &rotate->center.y);
1445 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
1447 dx = device->x - rotate->center.x;
1448 dy = device->y - rotate->center.y;
1449 r = sqrtf(dx * dx + dy * dy);
1451 struct weston_matrix inverse;
1453 weston_matrix_init(&inverse);
1454 inverse.d[0] = dx / r;
1455 inverse.d[4] = dy / r;
1456 inverse.d[1] = -inverse.d[4];
1457 inverse.d[5] = inverse.d[0];
1458 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1460 weston_matrix_init(&rotate->rotation);
1461 rotate->rotation.d[0] = dx / r;
1462 rotate->rotation.d[4] = -dy / r;
1463 rotate->rotation.d[1] = -rotate->rotation.d[4];
1464 rotate->rotation.d[5] = rotate->rotation.d[0];
1466 weston_matrix_init(&surface->rotation.rotation);
1467 weston_matrix_init(&rotate->rotation);
1470 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
1474 activate(struct wl_shell *shell, struct weston_surface *es,
1475 struct weston_input_device *device)
1477 struct weston_surface *surf, *prev;
1479 weston_surface_activate(es, device);
1481 switch (get_shell_surface_type(es)) {
1482 case SHELL_SURFACE_BACKGROUND:
1483 case SHELL_SURFACE_PANEL:
1484 case SHELL_SURFACE_LOCK:
1487 case SHELL_SURFACE_SCREENSAVER:
1488 /* always below lock surface */
1489 if (shell->lock_surface)
1490 weston_surface_restack(es,
1491 &shell->lock_surface->surface->layer_link);
1493 case SHELL_SURFACE_FULLSCREEN:
1494 /* should on top of panels */
1495 shell_stack_fullscreen(get_shell_surface(es));
1498 /* move the fullscreen surfaces down into the toplevel layer */
1499 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1500 wl_list_for_each_reverse_safe(surf,
1502 &shell->fullscreen_layer.surface_list,
1504 weston_surface_restack(surf,
1505 &shell->toplevel_layer.surface_list);
1508 weston_surface_restack(es,
1509 &shell->toplevel_layer.surface_list);
1514 /* no-op func for checking black surface */
1516 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1521 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1523 if (es->configure == black_surface_configure) {
1525 *fs_surface = (struct weston_surface *)es->private;
1532 click_to_activate_binding(struct wl_input_device *device,
1533 uint32_t time, uint32_t key,
1534 uint32_t button, uint32_t axis, int32_t state, void *data)
1536 struct weston_input_device *wd = (struct weston_input_device *) device;
1537 struct wl_shell *shell = data;
1538 struct weston_surface *focus;
1539 struct weston_surface *upper;
1541 focus = (struct weston_surface *) device->pointer_focus;
1545 if (is_black_surface(focus, &upper))
1548 if (state && device->pointer_grab == &device->default_pointer_grab)
1549 activate(shell, focus, wd);
1553 lock(struct wl_listener *listener, void *data)
1555 struct wl_shell *shell =
1556 container_of(listener, struct wl_shell, lock_listener);
1557 struct weston_input_device *device;
1558 struct shell_surface *shsurf;
1559 struct weston_output *output;
1561 if (shell->locked) {
1562 wl_list_for_each(output, &shell->compositor->output_list, link)
1563 /* TODO: find a way to jump to other DPMS levels */
1564 if (output->set_dpms)
1565 output->set_dpms(output, WESTON_DPMS_STANDBY);
1569 shell->locked = true;
1571 /* Hide all surfaces by removing the fullscreen, panel and
1572 * toplevel layers. This way nothing else can show or receive
1573 * input events while we are locked. */
1575 wl_list_remove(&shell->panel_layer.link);
1576 wl_list_remove(&shell->toplevel_layer.link);
1577 wl_list_remove(&shell->fullscreen_layer.link);
1578 wl_list_insert(&shell->compositor->cursor_layer.link,
1579 &shell->lock_layer.link);
1581 launch_screensaver(shell);
1583 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1584 show_screensaver(shell, shsurf);
1586 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1587 shell->compositor->idle_time = shell->screensaver.duration;
1588 weston_compositor_wake(shell->compositor);
1589 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1592 /* reset pointer foci */
1593 weston_compositor_schedule_repaint(shell->compositor);
1595 /* reset keyboard foci */
1596 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1597 wl_input_device_set_keyboard_focus(&device->input_device,
1601 /* TODO: disable bindings that should not work while locked. */
1603 /* All this must be undone in resume_desktop(). */
1607 unlock(struct wl_listener *listener, void *data)
1609 struct wl_shell *shell =
1610 container_of(listener, struct wl_shell, unlock_listener);
1612 if (!shell->locked || shell->lock_surface) {
1613 weston_compositor_wake(shell->compositor);
1617 /* If desktop-shell client has gone away, unlock immediately. */
1618 if (!shell->child.desktop_shell) {
1619 resume_desktop(shell);
1623 if (shell->prepare_event_sent)
1626 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1627 shell->prepare_event_sent = true;
1631 center_on_output(struct weston_surface *surface, struct weston_output *output)
1633 struct weston_mode *mode = output->current;
1634 GLfloat x = (mode->width - surface->geometry.width) / 2;
1635 GLfloat y = (mode->height - surface->geometry.height) / 2;
1637 weston_surface_set_position(surface, output->x + x, output->y + y);
1641 map(struct wl_shell *shell, struct weston_surface *surface,
1642 int32_t width, int32_t height, int32_t sx, int32_t sy)
1644 struct weston_compositor *compositor = shell->compositor;
1645 struct shell_surface *shsurf;
1646 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1647 struct weston_surface *parent;
1648 int panel_height = 0;
1650 shsurf = get_shell_surface(surface);
1652 surface_type = shsurf->type;
1654 surface->geometry.width = width;
1655 surface->geometry.height = height;
1656 surface->geometry.dirty = 1;
1658 /* initial positioning, see also configure() */
1659 switch (surface_type) {
1660 case SHELL_SURFACE_TOPLEVEL:
1661 weston_surface_set_position(surface, 10 + random() % 400,
1662 10 + random() % 400);
1664 case SHELL_SURFACE_SCREENSAVER:
1665 center_on_output(surface, shsurf->fullscreen_output);
1667 case SHELL_SURFACE_FULLSCREEN:
1668 shell_map_fullscreen(shsurf);
1670 case SHELL_SURFACE_MAXIMIZED:
1671 /* use surface configure to set the geometry */
1672 panel_height = get_output_panel_height(shell,surface->output);
1673 weston_surface_set_position(surface, surface->output->x,
1674 surface->output->y + panel_height);
1676 case SHELL_SURFACE_LOCK:
1677 center_on_output(surface, get_default_output(compositor));
1679 case SHELL_SURFACE_POPUP:
1680 shell_map_popup(shsurf, shsurf->popup.time);
1681 case SHELL_SURFACE_NONE:
1682 weston_surface_set_position(surface,
1683 surface->geometry.x + sx,
1684 surface->geometry.y + sy);
1690 /* surface stacking order, see also activate() */
1691 switch (surface_type) {
1692 case SHELL_SURFACE_BACKGROUND:
1693 /* background always visible, at the bottom */
1694 wl_list_insert(&shell->background_layer.surface_list,
1695 &surface->layer_link);
1697 case SHELL_SURFACE_PANEL:
1698 /* panel always on top, hidden while locked */
1699 wl_list_insert(&shell->panel_layer.surface_list,
1700 &surface->layer_link);
1702 case SHELL_SURFACE_LOCK:
1703 /* lock surface always visible, on top */
1704 wl_list_insert(&shell->lock_layer.surface_list,
1705 &surface->layer_link);
1706 weston_compositor_wake(compositor);
1708 case SHELL_SURFACE_SCREENSAVER:
1709 /* If locked, show it. */
1710 if (shell->locked) {
1711 show_screensaver(shell, shsurf);
1712 compositor->idle_time = shell->screensaver.duration;
1713 weston_compositor_wake(compositor);
1714 if (!shell->lock_surface)
1715 compositor->state = WESTON_COMPOSITOR_IDLE;
1718 case SHELL_SURFACE_POPUP:
1719 case SHELL_SURFACE_TRANSIENT:
1720 parent = shsurf->parent->surface;
1721 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1723 case SHELL_SURFACE_FULLSCREEN:
1724 case SHELL_SURFACE_NONE:
1727 wl_list_insert(&shell->toplevel_layer.surface_list,
1728 &surface->layer_link);
1732 if (surface_type != SHELL_SURFACE_NONE) {
1733 weston_surface_assign_output(surface);
1734 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1735 surface->output = shsurf->output;
1738 switch (surface_type) {
1739 case SHELL_SURFACE_TOPLEVEL:
1740 case SHELL_SURFACE_TRANSIENT:
1741 case SHELL_SURFACE_FULLSCREEN:
1742 case SHELL_SURFACE_MAXIMIZED:
1744 activate(shell, surface,
1745 (struct weston_input_device *)
1746 compositor->input_device);
1752 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1753 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1757 configure(struct wl_shell *shell, struct weston_surface *surface,
1758 GLfloat x, GLfloat y, int32_t width, int32_t height)
1760 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1761 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1762 struct shell_surface *shsurf;
1764 shsurf = get_shell_surface(surface);
1766 surface_type = shsurf->type;
1768 surface->geometry.x = x;
1769 surface->geometry.y = y;
1770 surface->geometry.width = width;
1771 surface->geometry.height = height;
1772 surface->geometry.dirty = 1;
1774 switch (surface_type) {
1775 case SHELL_SURFACE_SCREENSAVER:
1776 center_on_output(surface, shsurf->fullscreen_output);
1778 case SHELL_SURFACE_FULLSCREEN:
1779 shell_configure_fullscreen(shsurf);
1780 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1781 shell_stack_fullscreen(shsurf);
1783 case SHELL_SURFACE_MAXIMIZED:
1784 /* setting x, y and using configure to change that geometry */
1785 surface->geometry.x = surface->output->x;
1786 surface->geometry.y = surface->output->y +
1787 get_output_panel_height(shell,surface->output);
1789 case SHELL_SURFACE_TOPLEVEL:
1795 /* XXX: would a fullscreen surface need the same handling? */
1796 if (surface->output) {
1797 weston_surface_assign_output(surface);
1799 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1800 surface->output = shsurf->output;
1801 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1802 surface->output = shsurf->output;
1807 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1809 struct shell_surface *shsurf = get_shell_surface(es);
1810 struct wl_shell *shell = shsurf->shell;
1812 if (!weston_surface_is_mapped(es)) {
1813 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
1814 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
1815 es->geometry.width != es->buffer->width ||
1816 es->geometry.height != es->buffer->height) {
1817 GLfloat from_x, from_y;
1820 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1821 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1822 configure(shell, es,
1823 es->geometry.x + to_x - from_x,
1824 es->geometry.y + to_y - from_y,
1825 es->buffer->width, es->buffer->height);
1826 shsurf->force_configure = 0;
1830 static int launch_desktop_shell_process(struct wl_shell *shell);
1833 desktop_shell_sigchld(struct weston_process *process, int status)
1836 struct wl_shell *shell =
1837 container_of(process, struct wl_shell, child.process);
1839 shell->child.process.pid = 0;
1840 shell->child.client = NULL; /* already destroyed by wayland */
1842 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1843 time = weston_compositor_get_time();
1844 if (time - shell->child.deathstamp > 30000) {
1845 shell->child.deathstamp = time;
1846 shell->child.deathcount = 0;
1849 shell->child.deathcount++;
1850 if (shell->child.deathcount > 5) {
1851 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1855 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1856 launch_desktop_shell_process(shell);
1860 launch_desktop_shell_process(struct wl_shell *shell)
1862 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1864 shell->child.client = weston_client_launch(shell->compositor,
1865 &shell->child.process,
1867 desktop_shell_sigchld);
1869 if (!shell->child.client)
1875 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1877 struct wl_shell *shell = data;
1879 wl_client_add_object(client, &wl_shell_interface,
1880 &shell_implementation, id, shell);
1884 unbind_desktop_shell(struct wl_resource *resource)
1886 struct wl_shell *shell = resource->data;
1889 resume_desktop(shell);
1891 shell->child.desktop_shell = NULL;
1892 shell->prepare_event_sent = false;
1897 bind_desktop_shell(struct wl_client *client,
1898 void *data, uint32_t version, uint32_t id)
1900 struct wl_shell *shell = data;
1901 struct wl_resource *resource;
1903 resource = wl_client_add_object(client, &desktop_shell_interface,
1904 &desktop_shell_implementation,
1907 if (client == shell->child.client) {
1908 resource->destroy = unbind_desktop_shell;
1909 shell->child.desktop_shell = resource;
1913 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1914 "permission to bind desktop_shell denied");
1915 wl_resource_destroy(resource);
1919 screensaver_set_surface(struct wl_client *client,
1920 struct wl_resource *resource,
1921 struct wl_resource *shell_surface_resource,
1922 struct wl_resource *output_resource)
1924 struct wl_shell *shell = resource->data;
1925 struct shell_surface *surface = shell_surface_resource->data;
1926 struct weston_output *output = output_resource->data;
1928 if (reset_shell_surface_type(surface))
1931 surface->type = SHELL_SURFACE_SCREENSAVER;
1933 surface->fullscreen_output = output;
1934 surface->output = output;
1935 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1938 static const struct screensaver_interface screensaver_implementation = {
1939 screensaver_set_surface
1943 unbind_screensaver(struct wl_resource *resource)
1945 struct wl_shell *shell = resource->data;
1947 shell->screensaver.binding = NULL;
1952 bind_screensaver(struct wl_client *client,
1953 void *data, uint32_t version, uint32_t id)
1955 struct wl_shell *shell = data;
1956 struct wl_resource *resource;
1958 resource = wl_client_add_object(client, &screensaver_interface,
1959 &screensaver_implementation,
1962 if (shell->screensaver.binding == NULL) {
1963 resource->destroy = unbind_screensaver;
1964 shell->screensaver.binding = resource;
1968 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1969 "interface object already bound");
1970 wl_resource_destroy(resource);
1974 struct wl_shell *shell;
1975 struct weston_surface *current;
1976 struct wl_listener listener;
1977 struct wl_keyboard_grab grab;
1981 switcher_next(struct switcher *switcher)
1983 struct weston_compositor *compositor = switcher->shell->compositor;
1984 struct weston_surface *surface;
1985 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1986 struct shell_surface *shsurf;
1988 wl_list_for_each(surface, &compositor->surface_list, link) {
1989 switch (get_shell_surface_type(surface)) {
1990 case SHELL_SURFACE_TOPLEVEL:
1991 case SHELL_SURFACE_FULLSCREEN:
1992 case SHELL_SURFACE_MAXIMIZED:
1995 if (prev == switcher->current)
1998 surface->alpha = 64;
1999 surface->geometry.dirty = 1;
2000 weston_surface_damage(surface);
2006 if (is_black_surface(surface, NULL)) {
2007 surface->alpha = 64;
2008 surface->geometry.dirty = 1;
2009 weston_surface_damage(surface);
2019 wl_list_remove(&switcher->listener.link);
2020 wl_signal_add(&next->surface.resource.destroy_signal,
2021 &switcher->listener);
2023 switcher->current = next;
2026 shsurf = get_shell_surface(switcher->current);
2027 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2028 shsurf->fullscreen.black_surface->alpha = 255;
2032 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
2034 struct switcher *switcher =
2035 container_of(listener, struct switcher, listener);
2037 switcher_next(switcher);
2041 switcher_destroy(struct switcher *switcher, uint32_t time)
2043 struct weston_compositor *compositor = switcher->shell->compositor;
2044 struct weston_surface *surface;
2045 struct weston_input_device *device =
2046 (struct weston_input_device *) switcher->grab.input_device;
2048 wl_list_for_each(surface, &compositor->surface_list, link) {
2049 surface->alpha = 255;
2050 weston_surface_damage(surface);
2053 if (switcher->current)
2054 activate(switcher->shell, switcher->current, device);
2055 wl_list_remove(&switcher->listener.link);
2056 wl_input_device_end_keyboard_grab(&device->input_device);
2061 switcher_key(struct wl_keyboard_grab *grab,
2062 uint32_t time, uint32_t key, int32_t state)
2064 struct switcher *switcher = container_of(grab, struct switcher, grab);
2065 struct weston_input_device *device =
2066 (struct weston_input_device *) grab->input_device;
2068 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2069 switcher_destroy(switcher, time);
2070 } else if (key == KEY_TAB && state) {
2071 switcher_next(switcher);
2075 static const struct wl_keyboard_grab_interface switcher_grab = {
2080 switcher_binding(struct wl_input_device *device, uint32_t time,
2081 uint32_t key, uint32_t button, uint32_t axis,
2082 int32_t state, void *data)
2084 struct wl_shell *shell = data;
2085 struct switcher *switcher;
2087 switcher = malloc(sizeof *switcher);
2088 switcher->shell = shell;
2089 switcher->current = NULL;
2090 switcher->listener.notify = switcher_handle_surface_destroy;
2091 wl_list_init(&switcher->listener.link);
2093 switcher->grab.interface = &switcher_grab;
2094 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2095 wl_input_device_set_keyboard_focus(device, NULL);
2096 switcher_next(switcher);
2100 backlight_binding(struct wl_input_device *device, uint32_t time,
2101 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2103 struct weston_compositor *compositor = data;
2104 struct weston_output *output;
2105 long backlight_new = 0;
2107 /* TODO: we're limiting to simple use cases, where we assume just
2108 * control on the primary display. We'd have to extend later if we
2109 * ever get support for setting backlights on random desktop LCD
2111 output = get_default_output(compositor);
2115 if (!output->set_backlight)
2118 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2119 backlight_new = output->backlight_current - 25;
2120 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2121 backlight_new = output->backlight_current + 25;
2123 if (backlight_new < 5)
2125 if (backlight_new > 255)
2126 backlight_new = 255;
2128 output->backlight_current = backlight_new;
2129 output->set_backlight(output, output->backlight_current);
2133 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2134 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2136 struct wl_shell *shell = data;
2137 struct weston_compositor *compositor = shell->compositor;
2138 struct weston_surface *surface;
2140 if (shell->debug_repaint_surface) {
2141 weston_surface_destroy(shell->debug_repaint_surface);
2142 shell->debug_repaint_surface = NULL;
2144 surface = weston_surface_create(compositor);
2145 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2146 weston_surface_configure(surface, 0, 0, 8192, 8192);
2147 wl_list_insert(&compositor->fade_layer.surface_list,
2148 &surface->layer_link);
2149 weston_surface_assign_output(surface);
2150 pixman_region32_init(&surface->input);
2152 /* Here's the dirty little trick that makes the
2153 * repaint debugging work: we force an
2154 * update_transform first to update dependent state
2155 * and clear the geometry.dirty bit. Then we clear
2156 * the surface damage so it only gets repainted
2157 * piecewise as we repaint other things. */
2159 weston_surface_update_transform(surface);
2160 pixman_region32_fini(&surface->damage);
2161 pixman_region32_init(&surface->damage);
2162 shell->debug_repaint_surface = surface;
2167 shell_destroy(struct wl_listener *listener, void *data)
2169 struct wl_shell *shell =
2170 container_of(listener, struct wl_shell, destroy_listener);
2172 if (shell->child.client)
2173 wl_client_destroy(shell->child.client);
2175 free(shell->screensaver.path);
2180 shell_init(struct weston_compositor *ec);
2183 shell_init(struct weston_compositor *ec)
2185 struct wl_shell *shell;
2187 shell = malloc(sizeof *shell);
2191 memset(shell, 0, sizeof *shell);
2192 shell->compositor = ec;
2194 shell->destroy_listener.notify = shell_destroy;
2195 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2196 shell->lock_listener.notify = lock;
2197 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2198 shell->unlock_listener.notify = unlock;
2199 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
2201 wl_list_init(&shell->backgrounds);
2202 wl_list_init(&shell->panels);
2203 wl_list_init(&shell->screensaver.surfaces);
2205 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2206 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2207 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2208 weston_layer_init(&shell->background_layer,
2209 &shell->toplevel_layer.link);
2210 wl_list_init(&shell->lock_layer.surface_list);
2212 shell_configuration(shell);
2214 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2215 shell, bind_shell) == NULL)
2218 if (wl_display_add_global(ec->wl_display,
2219 &desktop_shell_interface,
2220 shell, bind_desktop_shell) == NULL)
2223 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2224 shell, bind_screensaver) == NULL)
2227 shell->child.deathstamp = weston_compositor_get_time();
2228 if (launch_desktop_shell_process(shell) != 0)
2231 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2232 move_binding, shell);
2233 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2234 resize_binding, shell);
2235 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2236 MODIFIER_CTRL | MODIFIER_ALT,
2237 terminate_binding, ec);
2238 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2239 click_to_activate_binding, shell);
2240 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2241 MODIFIER_SUPER | MODIFIER_ALT,
2242 surface_opacity_binding, NULL);
2243 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2244 MODIFIER_SUPER, zoom_binding, NULL);
2245 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2246 MODIFIER_SUPER | MODIFIER_ALT,
2247 rotate_binding, NULL);
2248 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2249 switcher_binding, shell);
2252 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2253 backlight_binding, ec);
2254 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2255 backlight_binding, ec);
2256 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2257 backlight_binding, ec);
2258 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2259 backlight_binding, ec);
2261 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2262 debug_repaint_binding, shell);