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"
41 struct desktop_shell {
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 uint32_t binding_modifier;
81 struct weston_surface *debug_repaint_surface;
84 enum shell_surface_type {
88 SHELL_SURFACE_BACKGROUND,
90 SHELL_SURFACE_SCREENSAVER,
92 SHELL_SURFACE_TOPLEVEL,
93 SHELL_SURFACE_TRANSIENT,
94 SHELL_SURFACE_FULLSCREEN,
95 SHELL_SURFACE_MAXIMIZED,
100 struct wl_event_source *source;
104 struct shell_surface {
105 struct wl_resource resource;
107 struct weston_surface *surface;
108 struct wl_listener surface_destroy_listener;
109 struct shell_surface *parent;
110 struct desktop_shell *shell;
112 enum shell_surface_type type;
113 int32_t saved_x, saved_y;
114 bool saved_position_valid;
118 struct weston_transform transform;
119 struct weston_matrix rotation;
123 struct wl_pointer_grab grab;
125 struct weston_transform parent_transform;
127 struct wl_input_device *device;
132 enum wl_shell_surface_fullscreen_method type;
133 struct weston_transform transform; /* matrix from x, y */
135 struct weston_surface *black_surface;
138 struct ping_timer *ping_timer;
141 struct weston_animation current;
145 } unresponsive_animation;
147 struct weston_output *fullscreen_output;
148 struct weston_output *output;
155 struct wl_pointer_grab grab;
156 struct shell_surface *shsurf;
157 struct wl_listener shsurf_destroy_listener;
160 struct weston_move_grab {
161 struct shell_grab base;
166 struct shell_grab base;
167 struct weston_matrix rotation;
174 static struct shell_surface *
175 get_shell_surface(struct weston_surface *surface);
177 static struct desktop_shell *
178 shell_surface_get_shell(struct shell_surface *shsurf);
181 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
183 struct desktop_shell *shell;
184 struct weston_surface *top_fs_es;
186 shell = shell_surface_get_shell(shsurf);
188 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
191 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
192 struct weston_surface,
194 return (shsurf == get_shell_surface(top_fs_es));
198 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
200 struct shell_grab *grab;
202 grab = container_of(listener, struct shell_grab,
203 shsurf_destroy_listener);
209 shell_grab_init(struct shell_grab *grab,
210 const struct wl_pointer_grab_interface *interface,
211 struct shell_surface *shsurf)
213 grab->grab.interface = interface;
214 grab->shsurf = shsurf;
215 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
216 wl_signal_add(&shsurf->resource.destroy_signal,
217 &grab->shsurf_destroy_listener);
222 shell_grab_finish(struct shell_grab *grab)
224 wl_list_remove(&grab->shsurf_destroy_listener.link);
228 center_on_output(struct weston_surface *surface,
229 struct weston_output *output);
232 get_modifier(char *modifier)
235 return MODIFIER_SUPER;
237 if (!strcmp("ctrl", modifier))
238 return MODIFIER_CTRL;
239 else if (!strcmp("alt", modifier))
241 else if (!strcmp("super", modifier))
242 return MODIFIER_SUPER;
244 return MODIFIER_SUPER;
248 shell_configuration(struct desktop_shell *shell)
253 char *modifier = NULL;
255 struct config_key saver_keys[] = {
256 { "path", CONFIG_KEY_STRING, &path },
257 { "duration", CONFIG_KEY_INTEGER, &duration },
258 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
261 struct config_section cs[] = {
262 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
265 config_file = config_file_path("weston.ini");
266 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
269 shell->screensaver.path = path;
270 shell->screensaver.duration = duration;
271 shell->binding_modifier = get_modifier(modifier);
275 noop_grab_focus(struct wl_pointer_grab *grab,
276 struct wl_surface *surface, int32_t x, int32_t y)
282 move_grab_motion(struct wl_pointer_grab *grab,
283 uint32_t time, int32_t x, int32_t y)
285 struct weston_move_grab *move = (struct weston_move_grab *) grab;
286 struct wl_input_device *device = grab->input_device;
287 struct shell_surface *shsurf = move->base.shsurf;
288 struct weston_surface *es;
293 es = shsurf->surface;
295 weston_surface_configure(es,
296 device->x + move->dx,
297 device->y + move->dy,
298 es->geometry.width, es->geometry.height);
302 move_grab_button(struct wl_pointer_grab *grab,
303 uint32_t time, uint32_t button, int32_t state)
305 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
307 struct wl_input_device *device = grab->input_device;
309 if (device->button_count == 0 && state == 0) {
310 shell_grab_finish(shell_grab);
311 wl_input_device_end_pointer_grab(device);
316 static const struct wl_pointer_grab_interface move_grab_interface = {
323 unresponsive_surface_fade(struct shell_surface *shsurf, bool reverse)
325 shsurf->unresponsive_animation.fading_in = reverse ? 0 : 1;
327 if(!shsurf->unresponsive_animation.exists) {
328 wl_list_insert(&shsurf->surface->compositor->animation_list,
329 &shsurf->unresponsive_animation.current.link);
330 shsurf->unresponsive_animation.exists = 1;
331 shsurf->unresponsive_animation.timestamp = weston_compositor_get_time();
332 weston_surface_damage(shsurf->surface);
337 unresponsive_fade_frame(struct weston_animation *animation,
338 struct weston_output *output, uint32_t msecs)
340 struct shell_surface *shsurf =
341 container_of(animation, struct shell_surface, unresponsive_animation.current);
342 struct weston_surface *surface = shsurf->surface;
343 unsigned int step = 8;
345 if (!surface || !shsurf)
348 if (shsurf->unresponsive_animation.fading_in) {
349 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
350 if (surface->saturation > 1)
351 surface->saturation -= 5;
352 if (surface->brightness > 200)
353 surface->brightness--;
355 shsurf->unresponsive_animation.timestamp += step;
358 if (surface->saturation <= 1 && surface->brightness <= 200) {
359 wl_list_remove(&shsurf->unresponsive_animation.current.link);
360 shsurf->unresponsive_animation.exists = 0;
364 while (step < msecs - shsurf->unresponsive_animation.timestamp) {
365 if (surface->saturation < 255)
366 surface->saturation += 5;
367 if (surface->brightness < 255)
368 surface->brightness++;
370 shsurf->unresponsive_animation.timestamp += step;
373 if (surface->saturation >= 255 && surface->brightness >= 255) {
374 surface->saturation = surface->brightness = 255;
375 wl_list_remove(&shsurf->unresponsive_animation.current.link);
376 shsurf->unresponsive_animation.exists = 0;
380 surface->geometry.dirty = 1;
381 weston_surface_damage(surface);
385 ping_timer_destroy(struct shell_surface *shsurf)
387 if (!shsurf || !shsurf->ping_timer)
390 if (shsurf->ping_timer->source)
391 wl_event_source_remove(shsurf->ping_timer->source);
393 free(shsurf->ping_timer);
394 shsurf->ping_timer = NULL;
398 ping_timeout_handler(void *data)
400 struct shell_surface *shsurf = data;
402 /* Client is not responding */
403 shsurf->unresponsive = 1;
404 unresponsive_surface_fade(shsurf, false);
410 ping_handler(struct weston_surface *surface, uint32_t serial)
412 struct shell_surface *shsurf;
413 shsurf = get_shell_surface(surface);
414 struct wl_event_loop *loop;
415 int ping_timeout = 2500;
420 if (!shsurf->ping_timer) {
421 shsurf->ping_timer = malloc(sizeof shsurf->ping_timer);
422 if (!shsurf->ping_timer)
425 shsurf->ping_timer->serial = serial;
426 loop = wl_display_get_event_loop(surface->compositor->wl_display);
427 shsurf->ping_timer->source =
428 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
429 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
431 wl_shell_surface_send_ping(&shsurf->resource, serial);
436 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
439 struct shell_surface *shsurf = resource->data;
441 if (shsurf->ping_timer->serial == serial) {
442 if (shsurf->unresponsive) {
443 /* Received pong from previously unresponsive client */
444 unresponsive_surface_fade(shsurf, true);
446 shsurf->unresponsive = 0;
447 ping_timer_destroy(shsurf);
452 weston_surface_move(struct weston_surface *es,
453 struct weston_input_device *wd)
455 struct weston_move_grab *move;
456 struct shell_surface *shsurf = get_shell_surface(es);
461 move = malloc(sizeof *move);
465 shell_grab_init(&move->base, &move_grab_interface, shsurf);
467 move->dx = es->geometry.x - wd->input_device.grab_x;
468 move->dy = es->geometry.y - wd->input_device.grab_y;
470 wl_input_device_start_pointer_grab(&wd->input_device,
473 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
479 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
480 struct wl_resource *input_resource, uint32_t serial)
482 struct weston_input_device *wd = input_resource->data;
483 struct shell_surface *shsurf = resource->data;
485 if (wd->input_device.button_count == 0 ||
486 wd->input_device.grab_serial != serial ||
487 wd->input_device.pointer_focus != &shsurf->surface->surface)
490 if (weston_surface_move(shsurf->surface, wd) < 0)
491 wl_resource_post_no_memory(resource);
494 struct weston_resize_grab {
495 struct shell_grab base;
497 int32_t width, height;
501 resize_grab_motion(struct wl_pointer_grab *grab,
502 uint32_t time, int32_t x, int32_t y)
504 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
505 struct wl_input_device *device = grab->input_device;
506 int32_t width, height;
507 int32_t from_x, from_y;
510 if (!resize->base.shsurf)
513 weston_surface_from_global(resize->base.shsurf->surface,
514 device->grab_x, device->grab_y,
516 weston_surface_from_global(resize->base.shsurf->surface,
517 device->x, device->y, &to_x, &to_y);
519 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
520 width = resize->width + from_x - to_x;
521 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
522 width = resize->width + to_x - from_x;
524 width = resize->width;
527 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
528 height = resize->height + from_y - to_y;
529 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
530 height = resize->height + to_y - from_y;
532 height = resize->height;
535 wl_shell_surface_send_configure(&resize->base.shsurf->resource,
536 resize->edges, width, height);
540 resize_grab_button(struct wl_pointer_grab *grab,
541 uint32_t time, uint32_t button, int32_t state)
543 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
544 struct wl_input_device *device = grab->input_device;
546 if (device->button_count == 0 && state == 0) {
547 shell_grab_finish(&resize->base);
548 wl_input_device_end_pointer_grab(device);
553 static const struct wl_pointer_grab_interface resize_grab_interface = {
560 weston_surface_resize(struct shell_surface *shsurf,
561 struct weston_input_device *wd, uint32_t edges)
563 struct weston_resize_grab *resize;
565 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
568 if (edges == 0 || edges > 15 ||
569 (edges & 3) == 3 || (edges & 12) == 12)
572 resize = malloc(sizeof *resize);
576 shell_grab_init(&resize->base, &resize_grab_interface, shsurf);
578 resize->edges = edges;
579 resize->width = shsurf->surface->geometry.width;
580 resize->height = shsurf->surface->geometry.height;
582 wl_input_device_start_pointer_grab(&wd->input_device,
585 wl_input_device_set_pointer_focus(&wd->input_device, NULL, 0, 0);
591 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
592 struct wl_resource *input_resource, uint32_t serial,
595 struct weston_input_device *wd = input_resource->data;
596 struct shell_surface *shsurf = resource->data;
598 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
601 if (wd->input_device.button_count == 0 ||
602 wd->input_device.grab_serial != serial ||
603 wd->input_device.pointer_focus != &shsurf->surface->surface)
606 if (weston_surface_resize(shsurf, wd, edges) < 0)
607 wl_resource_post_no_memory(resource);
610 static struct weston_output *
611 get_default_output(struct weston_compositor *compositor)
613 return container_of(compositor->output_list.next,
614 struct weston_output, link);
618 shell_unset_fullscreen(struct shell_surface *shsurf)
620 /* undo all fullscreen things here */
621 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
622 shell_surface_is_top_fullscreen(shsurf)) {
623 weston_output_switch_mode(shsurf->fullscreen_output,
624 shsurf->fullscreen_output->origin);
626 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
627 shsurf->fullscreen.framerate = 0;
628 wl_list_remove(&shsurf->fullscreen.transform.link);
629 wl_list_init(&shsurf->fullscreen.transform.link);
630 if (shsurf->fullscreen.black_surface)
631 weston_surface_destroy(shsurf->fullscreen.black_surface);
632 shsurf->fullscreen.black_surface = NULL;
633 shsurf->fullscreen_output = NULL;
634 shsurf->force_configure = 1;
635 weston_surface_set_position(shsurf->surface,
636 shsurf->saved_x, shsurf->saved_y);
640 reset_shell_surface_type(struct shell_surface *surface)
642 switch (surface->type) {
643 case SHELL_SURFACE_FULLSCREEN:
644 shell_unset_fullscreen(surface);
646 case SHELL_SURFACE_MAXIMIZED:
647 surface->output = get_default_output(surface->surface->compositor);
648 weston_surface_set_position(surface->surface,
652 case SHELL_SURFACE_PANEL:
653 case SHELL_SURFACE_BACKGROUND:
654 wl_list_remove(&surface->link);
655 wl_list_init(&surface->link);
657 case SHELL_SURFACE_SCREENSAVER:
658 case SHELL_SURFACE_LOCK:
659 wl_resource_post_error(&surface->resource,
660 WL_DISPLAY_ERROR_INVALID_METHOD,
661 "cannot reassign surface type");
663 case SHELL_SURFACE_NONE:
664 case SHELL_SURFACE_TOPLEVEL:
665 case SHELL_SURFACE_TRANSIENT:
666 case SHELL_SURFACE_POPUP:
670 surface->type = SHELL_SURFACE_NONE;
675 shell_surface_set_toplevel(struct wl_client *client,
676 struct wl_resource *resource)
679 struct shell_surface *surface = resource->data;
681 if (reset_shell_surface_type(surface))
684 surface->type = SHELL_SURFACE_TOPLEVEL;
688 shell_surface_set_transient(struct wl_client *client,
689 struct wl_resource *resource,
690 struct wl_resource *parent_resource,
691 int x, int y, uint32_t flags)
693 struct shell_surface *shsurf = resource->data;
694 struct weston_surface *es = shsurf->surface;
695 struct shell_surface *pshsurf = parent_resource->data;
696 struct weston_surface *pes = pshsurf->surface;
698 if (reset_shell_surface_type(shsurf))
701 /* assign to parents output */
702 shsurf->output = pes->output;
703 weston_surface_set_position(es, pes->geometry.x + x,
704 pes->geometry.y + y);
706 shsurf->type = SHELL_SURFACE_TRANSIENT;
709 static struct desktop_shell *
710 shell_surface_get_shell(struct shell_surface *shsurf)
712 return shsurf->shell;
716 get_output_panel_height(struct desktop_shell *shell,
717 struct weston_output *output)
719 struct shell_surface *priv;
720 int panel_height = 0;
725 wl_list_for_each(priv, &shell->panels, link) {
726 if (priv->output == output) {
727 panel_height = priv->surface->geometry.height;
735 shell_surface_set_maximized(struct wl_client *client,
736 struct wl_resource *resource,
737 struct wl_resource *output_resource )
739 struct shell_surface *shsurf = resource->data;
740 struct weston_surface *es = shsurf->surface;
741 struct desktop_shell *shell = NULL;
742 uint32_t edges = 0, panel_height = 0;
744 /* get the default output, if the client set it as NULL
745 check whether the ouput is available */
747 shsurf->output = output_resource->data;
749 shsurf->output = get_default_output(es->compositor);
751 if (reset_shell_surface_type(shsurf))
754 shsurf->saved_x = es->geometry.x;
755 shsurf->saved_y = es->geometry.y;
756 shsurf->saved_position_valid = true;
758 shell = shell_surface_get_shell(shsurf);
759 panel_height = get_output_panel_height(shell, es->output);
760 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
762 wl_shell_surface_send_configure(&shsurf->resource, edges,
763 es->output->current->width,
764 es->output->current->height - panel_height);
766 shsurf->type = SHELL_SURFACE_MAXIMIZED;
770 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
772 static struct weston_surface *
773 create_black_surface(struct weston_compositor *ec,
774 struct weston_surface *fs_surface,
775 GLfloat x, GLfloat y, int w, int h)
777 struct weston_surface *surface = NULL;
779 surface = weston_surface_create(ec);
780 if (surface == NULL) {
781 fprintf(stderr, "no memory\n");
785 surface->configure = black_surface_configure;
786 surface->private = fs_surface;
787 weston_surface_configure(surface, x, y, w, h);
788 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
792 /* Create black surface and append it to the associated fullscreen surface.
793 * Handle size dismatch and positioning according to the method. */
795 shell_configure_fullscreen(struct shell_surface *shsurf)
797 struct weston_output *output = shsurf->fullscreen_output;
798 struct weston_surface *surface = shsurf->surface;
799 struct weston_matrix *matrix;
802 center_on_output(surface, output);
804 if (!shsurf->fullscreen.black_surface)
805 shsurf->fullscreen.black_surface =
806 create_black_surface(surface->compositor,
808 output->x, output->y,
809 output->current->width,
810 output->current->height);
812 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
813 wl_list_insert(&surface->layer_link,
814 &shsurf->fullscreen.black_surface->layer_link);
815 shsurf->fullscreen.black_surface->output = output;
817 switch (shsurf->fullscreen.type) {
818 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
820 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
821 matrix = &shsurf->fullscreen.transform.matrix;
822 weston_matrix_init(matrix);
823 scale = (float)output->current->width/(float)surface->geometry.width;
824 weston_matrix_scale(matrix, scale, scale, 1);
825 wl_list_remove(&shsurf->fullscreen.transform.link);
826 wl_list_insert(surface->geometry.transformation_list.prev,
827 &shsurf->fullscreen.transform.link);
828 weston_surface_set_position(surface, output->x, output->y);
830 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
831 if (shell_surface_is_top_fullscreen(shsurf)) {
832 struct weston_mode mode = {0,
833 surface->geometry.width,
834 surface->geometry.height,
835 shsurf->fullscreen.framerate};
837 if (weston_output_switch_mode(output, &mode) == 0) {
838 weston_surface_configure(shsurf->fullscreen.black_surface,
839 output->x, output->y,
840 output->current->width,
841 output->current->height);
842 weston_surface_set_position(surface, output->x, output->y);
847 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
854 /* make the fullscreen and black surface at the top */
856 shell_stack_fullscreen(struct shell_surface *shsurf)
858 struct weston_output *output = shsurf->fullscreen_output;
859 struct weston_surface *surface = shsurf->surface;
860 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
862 wl_list_remove(&surface->layer_link);
863 wl_list_insert(&shell->fullscreen_layer.surface_list,
864 &surface->layer_link);
865 weston_surface_damage(surface);
867 if (!shsurf->fullscreen.black_surface)
868 shsurf->fullscreen.black_surface =
869 create_black_surface(surface->compositor,
871 output->x, output->y,
872 output->current->width,
873 output->current->height);
875 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
876 wl_list_insert(&surface->layer_link,
877 &shsurf->fullscreen.black_surface->layer_link);
878 weston_surface_damage(shsurf->fullscreen.black_surface);
882 shell_map_fullscreen(struct shell_surface *shsurf)
884 shell_stack_fullscreen(shsurf);
885 shell_configure_fullscreen(shsurf);
889 shell_surface_set_fullscreen(struct wl_client *client,
890 struct wl_resource *resource,
893 struct wl_resource *output_resource)
895 struct shell_surface *shsurf = resource->data;
896 struct weston_surface *es = shsurf->surface;
899 shsurf->output = output_resource->data;
901 shsurf->output = get_default_output(es->compositor);
903 if (reset_shell_surface_type(shsurf))
906 shsurf->fullscreen_output = shsurf->output;
907 shsurf->fullscreen.type = method;
908 shsurf->fullscreen.framerate = framerate;
909 shsurf->type = SHELL_SURFACE_FULLSCREEN;
911 shsurf->saved_x = es->geometry.x;
912 shsurf->saved_y = es->geometry.y;
913 shsurf->saved_position_valid = true;
915 if (weston_surface_is_mapped(es))
916 shsurf->force_configure = 1;
918 wl_shell_surface_send_configure(&shsurf->resource, 0,
919 shsurf->output->current->width,
920 shsurf->output->current->height);
924 popup_grab_focus(struct wl_pointer_grab *grab,
925 struct wl_surface *surface, int32_t x, int32_t y)
927 struct wl_input_device *device = grab->input_device;
928 struct shell_surface *priv =
929 container_of(grab, struct shell_surface, popup.grab);
930 struct wl_client *client = priv->surface->surface.resource.client;
932 if (surface && surface->resource.client == client) {
933 wl_input_device_set_pointer_focus(device, surface, x, y);
934 grab->focus = surface;
936 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
942 popup_grab_motion(struct wl_pointer_grab *grab,
943 uint32_t time, int32_t sx, int32_t sy)
945 struct wl_resource *resource;
947 resource = grab->input_device->pointer_focus_resource;
949 wl_input_device_send_motion(resource, time, sx, sy);
953 popup_grab_button(struct wl_pointer_grab *grab,
954 uint32_t time, uint32_t button, int32_t state)
956 struct wl_resource *resource;
957 struct shell_surface *shsurf =
958 container_of(grab, struct shell_surface, popup.grab);
959 struct wl_display *display;
962 resource = grab->input_device->pointer_focus_resource;
964 display = wl_client_get_display(resource->client);
965 serial = wl_display_get_serial(display);
966 wl_input_device_send_button(resource, serial,
967 time, button, state);
968 } else if (state == 0 &&
969 (shsurf->popup.initial_up ||
970 time - shsurf->popup.device->grab_time > 500)) {
971 wl_shell_surface_send_popup_done(&shsurf->resource);
972 wl_input_device_end_pointer_grab(grab->input_device);
973 shsurf->popup.grab.input_device = NULL;
977 shsurf->popup.initial_up = 1;
980 static const struct wl_pointer_grab_interface popup_grab_interface = {
987 shell_map_popup(struct shell_surface *shsurf)
989 struct wl_input_device *device = shsurf->popup.device;
990 struct weston_surface *es = shsurf->surface;
991 struct weston_surface *parent = shsurf->parent->surface;
993 es->output = parent->output;
994 shsurf->popup.grab.interface = &popup_grab_interface;
996 weston_surface_update_transform(parent);
997 if (parent->transform.enabled) {
998 shsurf->popup.parent_transform.matrix =
999 parent->transform.matrix;
1001 /* construct x, y translation matrix */
1002 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1003 shsurf->popup.parent_transform.matrix.d[12] =
1005 shsurf->popup.parent_transform.matrix.d[13] =
1008 wl_list_insert(es->geometry.transformation_list.prev,
1009 &shsurf->popup.parent_transform.link);
1010 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1012 shsurf->popup.initial_up = 0;
1014 /* We don't require the grab to still be active, but if another
1015 * grab has started in the meantime, we end the popup now. */
1016 if (device->grab_serial == shsurf->popup.serial) {
1017 wl_input_device_start_pointer_grab(device,
1018 &shsurf->popup.grab);
1020 wl_shell_surface_send_popup_done(&shsurf->resource);
1025 shell_surface_set_popup(struct wl_client *client,
1026 struct wl_resource *resource,
1027 struct wl_resource *input_device_resource,
1029 struct wl_resource *parent_resource,
1030 int32_t x, int32_t y, uint32_t flags)
1032 struct shell_surface *shsurf = resource->data;
1034 shsurf->type = SHELL_SURFACE_POPUP;
1035 shsurf->parent = parent_resource->data;
1036 shsurf->popup.device = input_device_resource->data;
1037 shsurf->popup.serial = serial;
1038 shsurf->popup.x = x;
1039 shsurf->popup.y = y;
1042 static const struct wl_shell_surface_interface shell_surface_implementation = {
1045 shell_surface_resize,
1046 shell_surface_set_toplevel,
1047 shell_surface_set_transient,
1048 shell_surface_set_fullscreen,
1049 shell_surface_set_popup,
1050 shell_surface_set_maximized
1054 destroy_shell_surface(struct wl_resource *resource)
1056 struct shell_surface *shsurf = resource->data;
1058 if (shsurf->popup.grab.input_device)
1059 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device);
1061 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1062 shell_surface_is_top_fullscreen(shsurf)) {
1063 weston_output_switch_mode(shsurf->fullscreen_output,
1064 shsurf->fullscreen_output->origin);
1067 if (shsurf->fullscreen.black_surface)
1068 weston_surface_destroy(shsurf->fullscreen.black_surface);
1070 /* As destroy_resource() use wl_list_for_each_safe(),
1071 * we can always remove the listener.
1073 wl_list_remove(&shsurf->surface_destroy_listener.link);
1074 shsurf->surface->configure = NULL;
1075 ping_timer_destroy(shsurf);
1077 wl_list_remove(&shsurf->link);
1082 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1084 struct shell_surface *shsurf = container_of(listener,
1085 struct shell_surface,
1086 surface_destroy_listener);
1088 wl_resource_destroy(&shsurf->resource);
1091 static struct shell_surface *
1092 get_shell_surface(struct weston_surface *surface)
1094 struct wl_listener *listener;
1096 listener = wl_signal_get(&surface->surface.resource.destroy_signal,
1097 shell_handle_surface_destroy);
1099 return container_of(listener, struct shell_surface,
1100 surface_destroy_listener);
1106 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1109 shell_get_shell_surface(struct wl_client *client,
1110 struct wl_resource *resource,
1112 struct wl_resource *surface_resource)
1114 struct weston_surface *surface = surface_resource->data;
1115 struct shell_surface *shsurf;
1117 if (get_shell_surface(surface)) {
1118 wl_resource_post_error(surface_resource,
1119 WL_DISPLAY_ERROR_INVALID_OBJECT,
1120 "desktop_shell::get_shell_surface already requested");
1124 if (surface->configure) {
1125 wl_resource_post_error(surface_resource,
1126 WL_DISPLAY_ERROR_INVALID_OBJECT,
1127 "surface->configure already set");
1131 shsurf = calloc(1, sizeof *shsurf);
1133 wl_resource_post_no_memory(resource);
1137 surface->configure = shell_surface_configure;
1139 shsurf->unresponsive = 0;
1140 shsurf->unresponsive_animation.exists = 0;
1141 shsurf->unresponsive_animation.fading_in = 0;
1142 shsurf->unresponsive_animation.current.frame = unresponsive_fade_frame;
1144 shsurf->resource.destroy = destroy_shell_surface;
1145 shsurf->resource.object.id = id;
1146 shsurf->resource.object.interface = &wl_shell_surface_interface;
1147 shsurf->resource.object.implementation =
1148 (void (**)(void)) &shell_surface_implementation;
1149 shsurf->resource.data = shsurf;
1151 shsurf->shell = resource->data;
1152 shsurf->saved_position_valid = false;
1153 shsurf->surface = surface;
1154 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1155 shsurf->fullscreen.framerate = 0;
1156 shsurf->fullscreen.black_surface = NULL;
1157 shsurf->ping_timer = NULL;
1158 wl_list_init(&shsurf->fullscreen.transform.link);
1160 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1161 wl_signal_add(&surface->surface.resource.destroy_signal,
1162 &shsurf->surface_destroy_listener);
1164 /* init link so its safe to always remove it in destroy_shell_surface */
1165 wl_list_init(&shsurf->link);
1167 /* empty when not in use */
1168 wl_list_init(&shsurf->rotation.transform.link);
1169 weston_matrix_init(&shsurf->rotation.rotation);
1171 shsurf->type = SHELL_SURFACE_NONE;
1173 wl_client_add_resource(client, &shsurf->resource);
1176 static const struct wl_shell_interface shell_implementation = {
1177 shell_get_shell_surface
1181 handle_screensaver_sigchld(struct weston_process *proc, int status)
1187 launch_screensaver(struct desktop_shell *shell)
1189 if (shell->screensaver.binding)
1192 if (!shell->screensaver.path)
1195 if (shell->screensaver.process.pid != 0) {
1196 fprintf(stderr, "old screensaver still running\n");
1200 weston_client_launch(shell->compositor,
1201 &shell->screensaver.process,
1202 shell->screensaver.path,
1203 handle_screensaver_sigchld);
1207 terminate_screensaver(struct desktop_shell *shell)
1209 if (shell->screensaver.process.pid == 0)
1212 kill(shell->screensaver.process.pid, SIGTERM);
1216 show_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
1218 struct wl_list *list;
1220 if (shell->lock_surface)
1221 list = &shell->lock_surface->surface->layer_link;
1223 list = &shell->lock_layer.surface_list;
1225 wl_list_remove(&surface->surface->layer_link);
1226 wl_list_insert(list, &surface->surface->layer_link);
1227 surface->surface->output = surface->output;
1228 weston_surface_damage(surface->surface);
1232 hide_screensaver(struct desktop_shell *shell, struct shell_surface *surface)
1234 wl_list_remove(&surface->surface->layer_link);
1235 wl_list_init(&surface->surface->layer_link);
1236 surface->surface->output = NULL;
1240 desktop_shell_set_background(struct wl_client *client,
1241 struct wl_resource *resource,
1242 struct wl_resource *output_resource,
1243 struct wl_resource *surface_resource)
1245 struct desktop_shell *shell = resource->data;
1246 struct shell_surface *shsurf = surface_resource->data;
1247 struct weston_surface *surface = shsurf->surface;
1248 struct shell_surface *priv;
1250 if (reset_shell_surface_type(shsurf))
1253 wl_list_for_each(priv, &shell->backgrounds, link) {
1254 if (priv->output == output_resource->data) {
1255 priv->surface->output = NULL;
1256 wl_list_remove(&priv->surface->layer_link);
1257 wl_list_remove(&priv->link);
1262 shsurf->type = SHELL_SURFACE_BACKGROUND;
1263 shsurf->output = output_resource->data;
1265 wl_list_insert(&shell->backgrounds, &shsurf->link);
1267 weston_surface_set_position(surface, shsurf->output->x,
1270 desktop_shell_send_configure(resource, 0,
1272 shsurf->output->current->width,
1273 shsurf->output->current->height);
1277 desktop_shell_set_panel(struct wl_client *client,
1278 struct wl_resource *resource,
1279 struct wl_resource *output_resource,
1280 struct wl_resource *surface_resource)
1282 struct desktop_shell *shell = resource->data;
1283 struct shell_surface *shsurf = surface_resource->data;
1284 struct weston_surface *surface = shsurf->surface;
1285 struct shell_surface *priv;
1287 if (reset_shell_surface_type(shsurf))
1290 wl_list_for_each(priv, &shell->panels, link) {
1291 if (priv->output == output_resource->data) {
1292 priv->surface->output = NULL;
1293 wl_list_remove(&priv->surface->layer_link);
1294 wl_list_remove(&priv->link);
1299 shsurf->type = SHELL_SURFACE_PANEL;
1300 shsurf->output = output_resource->data;
1302 wl_list_insert(&shell->panels, &shsurf->link);
1304 weston_surface_set_position(surface, shsurf->output->x,
1307 desktop_shell_send_configure(resource, 0,
1309 shsurf->output->current->width,
1310 shsurf->output->current->height);
1314 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
1316 struct desktop_shell *shell =
1317 container_of(listener, struct desktop_shell, lock_surface_listener);
1319 fprintf(stderr, "lock surface gone\n");
1320 shell->lock_surface = NULL;
1324 desktop_shell_set_lock_surface(struct wl_client *client,
1325 struct wl_resource *resource,
1326 struct wl_resource *surface_resource)
1328 struct desktop_shell *shell = resource->data;
1329 struct shell_surface *surface = surface_resource->data;
1331 if (reset_shell_surface_type(surface))
1334 shell->prepare_event_sent = false;
1339 shell->lock_surface = surface;
1341 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1342 wl_signal_add(&surface_resource->destroy_signal,
1343 &shell->lock_surface_listener);
1345 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1349 resume_desktop(struct desktop_shell *shell)
1351 struct shell_surface *tmp;
1353 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1354 hide_screensaver(shell, tmp);
1356 terminate_screensaver(shell);
1358 wl_list_remove(&shell->lock_layer.link);
1359 wl_list_insert(&shell->compositor->cursor_layer.link,
1360 &shell->fullscreen_layer.link);
1361 wl_list_insert(&shell->fullscreen_layer.link,
1362 &shell->panel_layer.link);
1363 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1365 shell->locked = false;
1366 shell->compositor->idle_time = shell->compositor->option_idle_time;
1367 weston_compositor_wake(shell->compositor);
1368 weston_compositor_damage_all(shell->compositor);
1372 desktop_shell_unlock(struct wl_client *client,
1373 struct wl_resource *resource)
1375 struct desktop_shell *shell = resource->data;
1377 shell->prepare_event_sent = false;
1380 resume_desktop(shell);
1383 static const struct desktop_shell_interface desktop_shell_implementation = {
1384 desktop_shell_set_background,
1385 desktop_shell_set_panel,
1386 desktop_shell_set_lock_surface,
1387 desktop_shell_unlock
1390 static enum shell_surface_type
1391 get_shell_surface_type(struct weston_surface *surface)
1393 struct shell_surface *shsurf;
1395 shsurf = get_shell_surface(surface);
1397 return SHELL_SURFACE_NONE;
1398 return shsurf->type;
1402 move_binding(struct wl_input_device *device, uint32_t time,
1403 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1405 struct weston_surface *surface =
1406 (struct weston_surface *) device->pointer_focus;
1408 if (surface == NULL)
1411 switch (get_shell_surface_type(surface)) {
1412 case SHELL_SURFACE_PANEL:
1413 case SHELL_SURFACE_BACKGROUND:
1414 case SHELL_SURFACE_FULLSCREEN:
1415 case SHELL_SURFACE_SCREENSAVER:
1421 weston_surface_move(surface, (struct weston_input_device *) device);
1425 resize_binding(struct wl_input_device *device, uint32_t time,
1426 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1428 struct weston_surface *surface =
1429 (struct weston_surface *) device->pointer_focus;
1432 struct shell_surface *shsurf;
1434 if (surface == NULL)
1437 shsurf = get_shell_surface(surface);
1441 switch (shsurf->type) {
1442 case SHELL_SURFACE_PANEL:
1443 case SHELL_SURFACE_BACKGROUND:
1444 case SHELL_SURFACE_FULLSCREEN:
1445 case SHELL_SURFACE_SCREENSAVER:
1451 weston_surface_from_global(surface,
1452 device->grab_x, device->grab_y, &x, &y);
1454 if (x < surface->geometry.width / 3)
1455 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1456 else if (x < 2 * surface->geometry.width / 3)
1459 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1461 if (y < surface->geometry.height / 3)
1462 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1463 else if (y < 2 * surface->geometry.height / 3)
1466 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1468 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1473 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1474 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1477 struct shell_surface *shsurf;
1478 struct weston_surface *surface =
1479 (struct weston_surface *) device->pointer_focus;
1481 if (surface == NULL)
1484 shsurf = get_shell_surface(surface);
1488 switch (shsurf->type) {
1489 case SHELL_SURFACE_BACKGROUND:
1490 case SHELL_SURFACE_SCREENSAVER:
1496 surface->alpha += value * step;
1498 if (surface->alpha > 255)
1499 surface->alpha = 255;
1500 if (surface->alpha < step)
1501 surface->alpha = step;
1503 surface->geometry.dirty = 1;
1504 weston_surface_damage(surface);
1508 zoom_binding(struct wl_input_device *device, uint32_t time,
1509 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1511 struct weston_input_device *wd = (struct weston_input_device *) device;
1512 struct weston_compositor *compositor = wd->compositor;
1513 struct weston_output *output;
1515 wl_list_for_each(output, &compositor->output_list, link) {
1516 if (pixman_region32_contains_point(&output->region,
1517 device->x, device->y, NULL)) {
1518 output->zoom.active = 1;
1519 output->zoom.level += output->zoom.increment * -value;
1521 if (output->zoom.level >= 1.0) {
1522 output->zoom.active = 0;
1523 output->zoom.level = 1.0;
1526 if (output->zoom.level < output->zoom.increment)
1527 output->zoom.level = output->zoom.increment;
1529 weston_output_update_zoom(output, device->x, device->y);
1535 terminate_binding(struct wl_input_device *device, uint32_t time,
1536 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1538 struct weston_compositor *compositor = data;
1541 wl_display_terminate(compositor->wl_display);
1545 rotate_grab_motion(struct wl_pointer_grab *grab,
1546 uint32_t time, int32_t x, int32_t y)
1548 struct rotate_grab *rotate =
1549 container_of(grab, struct rotate_grab, base.grab);
1550 struct wl_input_device *device = grab->input_device;
1551 struct shell_surface *shsurf = rotate->base.shsurf;
1552 struct weston_surface *surface;
1553 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
1558 surface = shsurf->surface;
1560 cx = 0.5f * surface->geometry.width;
1561 cy = 0.5f * surface->geometry.height;
1563 dx = device->x - rotate->center.x;
1564 dy = device->y - rotate->center.y;
1565 r = sqrtf(dx * dx + dy * dy);
1567 wl_list_remove(&shsurf->rotation.transform.link);
1568 shsurf->surface->geometry.dirty = 1;
1571 struct weston_matrix *matrix =
1572 &shsurf->rotation.transform.matrix;
1574 weston_matrix_init(&rotate->rotation);
1575 rotate->rotation.d[0] = dx / r;
1576 rotate->rotation.d[4] = -dy / r;
1577 rotate->rotation.d[1] = -rotate->rotation.d[4];
1578 rotate->rotation.d[5] = rotate->rotation.d[0];
1580 weston_matrix_init(matrix);
1581 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1582 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
1583 weston_matrix_multiply(matrix, &rotate->rotation);
1584 weston_matrix_translate(matrix, cx, cy, 0.0f);
1587 &shsurf->surface->geometry.transformation_list,
1588 &shsurf->rotation.transform.link);
1590 wl_list_init(&shsurf->rotation.transform.link);
1591 weston_matrix_init(&shsurf->rotation.rotation);
1592 weston_matrix_init(&rotate->rotation);
1595 /* We need to adjust the position of the surface
1596 * in case it was resized in a rotated state before */
1597 cposx = surface->geometry.x + cx;
1598 cposy = surface->geometry.y + cy;
1599 dposx = rotate->center.x - cposx;
1600 dposy = rotate->center.y - cposy;
1601 if (dposx != 0.0f || dposy != 0.0f) {
1602 weston_surface_set_position(surface,
1603 surface->geometry.x + dposx,
1604 surface->geometry.y + dposy);
1607 /* Repaint implies weston_surface_update_transform(), which
1608 * lazily applies the damage due to rotation update.
1610 weston_compositor_schedule_repaint(shsurf->surface->compositor);
1614 rotate_grab_button(struct wl_pointer_grab *grab,
1615 uint32_t time, uint32_t button, int32_t state)
1617 struct rotate_grab *rotate =
1618 container_of(grab, struct rotate_grab, base.grab);
1619 struct wl_input_device *device = grab->input_device;
1620 struct shell_surface *shsurf = rotate->base.shsurf;
1622 if (device->button_count == 0 && state == 0) {
1624 weston_matrix_multiply(&shsurf->rotation.rotation,
1626 shell_grab_finish(&rotate->base);
1627 wl_input_device_end_pointer_grab(device);
1632 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1639 rotate_binding(struct wl_input_device *device, uint32_t time,
1640 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1642 struct weston_surface *base_surface =
1643 (struct weston_surface *) device->pointer_focus;
1644 struct shell_surface *surface;
1645 struct rotate_grab *rotate;
1649 if (base_surface == NULL)
1652 surface = get_shell_surface(base_surface);
1656 switch (surface->type) {
1657 case SHELL_SURFACE_PANEL:
1658 case SHELL_SURFACE_BACKGROUND:
1659 case SHELL_SURFACE_FULLSCREEN:
1660 case SHELL_SURFACE_SCREENSAVER:
1666 rotate = malloc(sizeof *rotate);
1670 shell_grab_init(&rotate->base, &rotate_grab_interface, surface);
1672 weston_surface_to_global(surface->surface,
1673 surface->surface->geometry.width / 2,
1674 surface->surface->geometry.height / 2,
1675 &rotate->center.x, &rotate->center.y);
1677 wl_input_device_start_pointer_grab(device, &rotate->base.grab);
1679 dx = device->x - rotate->center.x;
1680 dy = device->y - rotate->center.y;
1681 r = sqrtf(dx * dx + dy * dy);
1683 struct weston_matrix inverse;
1685 weston_matrix_init(&inverse);
1686 inverse.d[0] = dx / r;
1687 inverse.d[4] = dy / r;
1688 inverse.d[1] = -inverse.d[4];
1689 inverse.d[5] = inverse.d[0];
1690 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1692 weston_matrix_init(&rotate->rotation);
1693 rotate->rotation.d[0] = dx / r;
1694 rotate->rotation.d[4] = -dy / r;
1695 rotate->rotation.d[1] = -rotate->rotation.d[4];
1696 rotate->rotation.d[5] = rotate->rotation.d[0];
1698 weston_matrix_init(&surface->rotation.rotation);
1699 weston_matrix_init(&rotate->rotation);
1702 wl_input_device_set_pointer_focus(device, NULL, 0, 0);
1706 activate(struct desktop_shell *shell, struct weston_surface *es,
1707 struct weston_input_device *device)
1709 struct weston_surface *surf, *prev;
1711 weston_surface_activate(es, device);
1713 switch (get_shell_surface_type(es)) {
1714 case SHELL_SURFACE_BACKGROUND:
1715 case SHELL_SURFACE_PANEL:
1716 case SHELL_SURFACE_LOCK:
1719 case SHELL_SURFACE_SCREENSAVER:
1720 /* always below lock surface */
1721 if (shell->lock_surface)
1722 weston_surface_restack(es,
1723 &shell->lock_surface->surface->layer_link);
1725 case SHELL_SURFACE_FULLSCREEN:
1726 /* should on top of panels */
1727 shell_stack_fullscreen(get_shell_surface(es));
1728 shell_configure_fullscreen(get_shell_surface(es));
1731 /* move the fullscreen surfaces down into the toplevel layer */
1732 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1733 wl_list_for_each_reverse_safe(surf,
1735 &shell->fullscreen_layer.surface_list,
1737 weston_surface_restack(surf,
1738 &shell->toplevel_layer.surface_list);
1741 weston_surface_restack(es,
1742 &shell->toplevel_layer.surface_list);
1747 /* no-op func for checking black surface */
1749 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1754 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1756 if (es->configure == black_surface_configure) {
1758 *fs_surface = (struct weston_surface *)es->private;
1765 click_to_activate_binding(struct wl_input_device *device,
1766 uint32_t time, uint32_t key,
1767 uint32_t button, uint32_t axis, int32_t state, void *data)
1769 struct weston_input_device *wd = (struct weston_input_device *) device;
1770 struct desktop_shell *shell = data;
1771 struct weston_surface *focus;
1772 struct weston_surface *upper;
1774 focus = (struct weston_surface *) device->pointer_focus;
1778 if (is_black_surface(focus, &upper))
1781 if (state && device->pointer_grab == &device->default_pointer_grab)
1782 activate(shell, focus, wd);
1786 lock(struct wl_listener *listener, void *data)
1788 struct desktop_shell *shell =
1789 container_of(listener, struct desktop_shell, lock_listener);
1790 struct weston_input_device *device;
1791 struct shell_surface *shsurf;
1792 struct weston_output *output;
1794 if (shell->locked) {
1795 wl_list_for_each(output, &shell->compositor->output_list, link)
1796 /* TODO: find a way to jump to other DPMS levels */
1797 if (output->set_dpms)
1798 output->set_dpms(output, WESTON_DPMS_STANDBY);
1802 shell->locked = true;
1804 /* Hide all surfaces by removing the fullscreen, panel and
1805 * toplevel layers. This way nothing else can show or receive
1806 * input events while we are locked. */
1808 wl_list_remove(&shell->panel_layer.link);
1809 wl_list_remove(&shell->toplevel_layer.link);
1810 wl_list_remove(&shell->fullscreen_layer.link);
1811 wl_list_insert(&shell->compositor->cursor_layer.link,
1812 &shell->lock_layer.link);
1814 launch_screensaver(shell);
1816 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1817 show_screensaver(shell, shsurf);
1819 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1820 shell->compositor->idle_time = shell->screensaver.duration;
1821 weston_compositor_wake(shell->compositor);
1822 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1825 /* reset pointer foci */
1826 weston_compositor_schedule_repaint(shell->compositor);
1828 /* reset keyboard foci */
1829 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1830 wl_input_device_set_keyboard_focus(&device->input_device,
1834 /* TODO: disable bindings that should not work while locked. */
1836 /* All this must be undone in resume_desktop(). */
1840 unlock(struct wl_listener *listener, void *data)
1842 struct desktop_shell *shell =
1843 container_of(listener, struct desktop_shell, unlock_listener);
1845 if (!shell->locked || shell->lock_surface) {
1846 weston_compositor_wake(shell->compositor);
1850 /* If desktop-shell client has gone away, unlock immediately. */
1851 if (!shell->child.desktop_shell) {
1852 resume_desktop(shell);
1856 if (shell->prepare_event_sent)
1859 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1860 shell->prepare_event_sent = true;
1864 center_on_output(struct weston_surface *surface, struct weston_output *output)
1866 struct weston_mode *mode = output->current;
1867 GLfloat x = (mode->width - surface->geometry.width) / 2;
1868 GLfloat y = (mode->height - surface->geometry.height) / 2;
1870 weston_surface_set_position(surface, output->x + x, output->y + y);
1874 map(struct desktop_shell *shell, struct weston_surface *surface,
1875 int32_t width, int32_t height, int32_t sx, int32_t sy)
1877 struct weston_compositor *compositor = shell->compositor;
1878 struct shell_surface *shsurf;
1879 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1880 struct weston_surface *parent;
1881 int panel_height = 0;
1883 shsurf = get_shell_surface(surface);
1885 surface_type = shsurf->type;
1887 surface->geometry.width = width;
1888 surface->geometry.height = height;
1889 surface->geometry.dirty = 1;
1891 /* initial positioning, see also configure() */
1892 switch (surface_type) {
1893 case SHELL_SURFACE_TOPLEVEL:
1894 weston_surface_set_position(surface, 10 + random() % 400,
1895 10 + random() % 400);
1897 case SHELL_SURFACE_SCREENSAVER:
1898 center_on_output(surface, shsurf->fullscreen_output);
1900 case SHELL_SURFACE_FULLSCREEN:
1901 shell_map_fullscreen(shsurf);
1903 case SHELL_SURFACE_MAXIMIZED:
1904 /* use surface configure to set the geometry */
1905 panel_height = get_output_panel_height(shell,surface->output);
1906 weston_surface_set_position(surface, surface->output->x,
1907 surface->output->y + panel_height);
1909 case SHELL_SURFACE_LOCK:
1910 center_on_output(surface, get_default_output(compositor));
1912 case SHELL_SURFACE_POPUP:
1913 shell_map_popup(shsurf);
1914 case SHELL_SURFACE_NONE:
1915 weston_surface_set_position(surface,
1916 surface->geometry.x + sx,
1917 surface->geometry.y + sy);
1923 /* surface stacking order, see also activate() */
1924 switch (surface_type) {
1925 case SHELL_SURFACE_BACKGROUND:
1926 /* background always visible, at the bottom */
1927 wl_list_insert(&shell->background_layer.surface_list,
1928 &surface->layer_link);
1930 case SHELL_SURFACE_PANEL:
1931 /* panel always on top, hidden while locked */
1932 wl_list_insert(&shell->panel_layer.surface_list,
1933 &surface->layer_link);
1935 case SHELL_SURFACE_LOCK:
1936 /* lock surface always visible, on top */
1937 wl_list_insert(&shell->lock_layer.surface_list,
1938 &surface->layer_link);
1939 weston_compositor_wake(compositor);
1941 case SHELL_SURFACE_SCREENSAVER:
1942 /* If locked, show it. */
1943 if (shell->locked) {
1944 show_screensaver(shell, shsurf);
1945 compositor->idle_time = shell->screensaver.duration;
1946 weston_compositor_wake(compositor);
1947 if (!shell->lock_surface)
1948 compositor->state = WESTON_COMPOSITOR_IDLE;
1951 case SHELL_SURFACE_POPUP:
1952 case SHELL_SURFACE_TRANSIENT:
1953 parent = shsurf->parent->surface;
1954 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1956 case SHELL_SURFACE_FULLSCREEN:
1957 case SHELL_SURFACE_NONE:
1960 wl_list_insert(&shell->toplevel_layer.surface_list,
1961 &surface->layer_link);
1965 if (surface_type != SHELL_SURFACE_NONE) {
1966 weston_surface_assign_output(surface);
1967 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1968 surface->output = shsurf->output;
1971 switch (surface_type) {
1972 case SHELL_SURFACE_TOPLEVEL:
1973 case SHELL_SURFACE_TRANSIENT:
1974 case SHELL_SURFACE_FULLSCREEN:
1975 case SHELL_SURFACE_MAXIMIZED:
1977 activate(shell, surface,
1978 (struct weston_input_device *)
1979 compositor->input_device);
1985 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1986 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1990 configure(struct desktop_shell *shell, struct weston_surface *surface,
1991 GLfloat x, GLfloat y, int32_t width, int32_t height)
1993 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1994 struct shell_surface *shsurf;
1996 shsurf = get_shell_surface(surface);
1998 surface_type = shsurf->type;
2000 surface->geometry.x = x;
2001 surface->geometry.y = y;
2002 surface->geometry.width = width;
2003 surface->geometry.height = height;
2004 surface->geometry.dirty = 1;
2006 switch (surface_type) {
2007 case SHELL_SURFACE_SCREENSAVER:
2008 center_on_output(surface, shsurf->fullscreen_output);
2010 case SHELL_SURFACE_FULLSCREEN:
2011 shell_stack_fullscreen(shsurf);
2012 shell_configure_fullscreen(shsurf);
2014 case SHELL_SURFACE_MAXIMIZED:
2015 /* setting x, y and using configure to change that geometry */
2016 surface->geometry.x = surface->output->x;
2017 surface->geometry.y = surface->output->y +
2018 get_output_panel_height(shell,surface->output);
2020 case SHELL_SURFACE_TOPLEVEL:
2026 /* XXX: would a fullscreen surface need the same handling? */
2027 if (surface->output) {
2028 weston_surface_assign_output(surface);
2030 if (surface_type == SHELL_SURFACE_SCREENSAVER)
2031 surface->output = shsurf->output;
2032 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
2033 surface->output = shsurf->output;
2038 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2040 struct shell_surface *shsurf = get_shell_surface(es);
2041 struct desktop_shell *shell = shsurf->shell;
2043 if (!weston_surface_is_mapped(es)) {
2044 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
2045 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
2046 es->geometry.width != es->buffer->width ||
2047 es->geometry.height != es->buffer->height) {
2048 GLfloat from_x, from_y;
2051 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2052 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2053 configure(shell, es,
2054 es->geometry.x + to_x - from_x,
2055 es->geometry.y + to_y - from_y,
2056 es->buffer->width, es->buffer->height);
2057 shsurf->force_configure = 0;
2061 static int launch_desktop_shell_process(struct desktop_shell *shell);
2064 desktop_shell_sigchld(struct weston_process *process, int status)
2067 struct desktop_shell *shell =
2068 container_of(process, struct desktop_shell, child.process);
2070 shell->child.process.pid = 0;
2071 shell->child.client = NULL; /* already destroyed by wayland */
2073 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2074 time = weston_compositor_get_time();
2075 if (time - shell->child.deathstamp > 30000) {
2076 shell->child.deathstamp = time;
2077 shell->child.deathcount = 0;
2080 shell->child.deathcount++;
2081 if (shell->child.deathcount > 5) {
2082 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
2086 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
2087 launch_desktop_shell_process(shell);
2091 launch_desktop_shell_process(struct desktop_shell *shell)
2093 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
2095 shell->child.client = weston_client_launch(shell->compositor,
2096 &shell->child.process,
2098 desktop_shell_sigchld);
2100 if (!shell->child.client)
2106 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2108 struct desktop_shell *shell = data;
2110 wl_client_add_object(client, &wl_shell_interface,
2111 &shell_implementation, id, shell);
2115 unbind_desktop_shell(struct wl_resource *resource)
2117 struct desktop_shell *shell = resource->data;
2120 resume_desktop(shell);
2122 shell->child.desktop_shell = NULL;
2123 shell->prepare_event_sent = false;
2128 bind_desktop_shell(struct wl_client *client,
2129 void *data, uint32_t version, uint32_t id)
2131 struct desktop_shell *shell = data;
2132 struct wl_resource *resource;
2134 resource = wl_client_add_object(client, &desktop_shell_interface,
2135 &desktop_shell_implementation,
2138 if (client == shell->child.client) {
2139 resource->destroy = unbind_desktop_shell;
2140 shell->child.desktop_shell = resource;
2144 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2145 "permission to bind desktop_shell denied");
2146 wl_resource_destroy(resource);
2150 screensaver_set_surface(struct wl_client *client,
2151 struct wl_resource *resource,
2152 struct wl_resource *shell_surface_resource,
2153 struct wl_resource *output_resource)
2155 struct desktop_shell *shell = resource->data;
2156 struct shell_surface *surface = shell_surface_resource->data;
2157 struct weston_output *output = output_resource->data;
2159 if (reset_shell_surface_type(surface))
2162 surface->type = SHELL_SURFACE_SCREENSAVER;
2164 surface->fullscreen_output = output;
2165 surface->output = output;
2166 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
2169 static const struct screensaver_interface screensaver_implementation = {
2170 screensaver_set_surface
2174 unbind_screensaver(struct wl_resource *resource)
2176 struct desktop_shell *shell = resource->data;
2178 shell->screensaver.binding = NULL;
2183 bind_screensaver(struct wl_client *client,
2184 void *data, uint32_t version, uint32_t id)
2186 struct desktop_shell *shell = data;
2187 struct wl_resource *resource;
2189 resource = wl_client_add_object(client, &screensaver_interface,
2190 &screensaver_implementation,
2193 if (shell->screensaver.binding == NULL) {
2194 resource->destroy = unbind_screensaver;
2195 shell->screensaver.binding = resource;
2199 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2200 "interface object already bound");
2201 wl_resource_destroy(resource);
2205 struct desktop_shell *shell;
2206 struct weston_surface *current;
2207 struct wl_listener listener;
2208 struct wl_keyboard_grab grab;
2212 switcher_next(struct switcher *switcher)
2214 struct weston_compositor *compositor = switcher->shell->compositor;
2215 struct weston_surface *surface;
2216 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
2217 struct shell_surface *shsurf;
2219 wl_list_for_each(surface, &compositor->surface_list, link) {
2220 switch (get_shell_surface_type(surface)) {
2221 case SHELL_SURFACE_TOPLEVEL:
2222 case SHELL_SURFACE_FULLSCREEN:
2223 case SHELL_SURFACE_MAXIMIZED:
2226 if (prev == switcher->current)
2229 surface->alpha = 64;
2230 surface->geometry.dirty = 1;
2231 weston_surface_damage(surface);
2237 if (is_black_surface(surface, NULL)) {
2238 surface->alpha = 64;
2239 surface->geometry.dirty = 1;
2240 weston_surface_damage(surface);
2250 wl_list_remove(&switcher->listener.link);
2251 wl_signal_add(&next->surface.resource.destroy_signal,
2252 &switcher->listener);
2254 switcher->current = next;
2257 shsurf = get_shell_surface(switcher->current);
2258 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2259 shsurf->fullscreen.black_surface->alpha = 255;
2263 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
2265 struct switcher *switcher =
2266 container_of(listener, struct switcher, listener);
2268 switcher_next(switcher);
2272 switcher_destroy(struct switcher *switcher, uint32_t time)
2274 struct weston_compositor *compositor = switcher->shell->compositor;
2275 struct weston_surface *surface;
2276 struct weston_input_device *device =
2277 (struct weston_input_device *) switcher->grab.input_device;
2279 wl_list_for_each(surface, &compositor->surface_list, link) {
2280 surface->alpha = 255;
2281 weston_surface_damage(surface);
2284 if (switcher->current)
2285 activate(switcher->shell, switcher->current, device);
2286 wl_list_remove(&switcher->listener.link);
2287 wl_input_device_end_keyboard_grab(&device->input_device);
2292 switcher_key(struct wl_keyboard_grab *grab,
2293 uint32_t time, uint32_t key, int32_t state)
2295 struct switcher *switcher = container_of(grab, struct switcher, grab);
2296 struct weston_input_device *device =
2297 (struct weston_input_device *) grab->input_device;
2299 if ((device->modifier_state & switcher->shell->binding_modifier) == 0) {
2300 switcher_destroy(switcher, time);
2301 } else if (key == KEY_TAB && state) {
2302 switcher_next(switcher);
2306 static const struct wl_keyboard_grab_interface switcher_grab = {
2311 switcher_binding(struct wl_input_device *device, uint32_t time,
2312 uint32_t key, uint32_t button, uint32_t axis,
2313 int32_t state, void *data)
2315 struct desktop_shell *shell = data;
2316 struct switcher *switcher;
2318 switcher = malloc(sizeof *switcher);
2319 switcher->shell = shell;
2320 switcher->current = NULL;
2321 switcher->listener.notify = switcher_handle_surface_destroy;
2322 wl_list_init(&switcher->listener.link);
2324 switcher->grab.interface = &switcher_grab;
2325 wl_input_device_start_keyboard_grab(device, &switcher->grab);
2326 wl_input_device_set_keyboard_focus(device, NULL);
2327 switcher_next(switcher);
2331 backlight_binding(struct wl_input_device *device, uint32_t time,
2332 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2334 struct weston_compositor *compositor = data;
2335 struct weston_output *output;
2336 long backlight_new = 0;
2338 /* TODO: we're limiting to simple use cases, where we assume just
2339 * control on the primary display. We'd have to extend later if we
2340 * ever get support for setting backlights on random desktop LCD
2342 output = get_default_output(compositor);
2346 if (!output->set_backlight)
2349 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2350 backlight_new = output->backlight_current - 25;
2351 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2352 backlight_new = output->backlight_current + 25;
2354 if (backlight_new < 5)
2356 if (backlight_new > 255)
2357 backlight_new = 255;
2359 output->backlight_current = backlight_new;
2360 output->set_backlight(output, output->backlight_current);
2364 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2365 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2367 struct desktop_shell *shell = data;
2368 struct weston_compositor *compositor = shell->compositor;
2369 struct weston_surface *surface;
2371 if (shell->debug_repaint_surface) {
2372 weston_surface_destroy(shell->debug_repaint_surface);
2373 shell->debug_repaint_surface = NULL;
2375 surface = weston_surface_create(compositor);
2376 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2377 weston_surface_configure(surface, 0, 0, 8192, 8192);
2378 wl_list_insert(&compositor->fade_layer.surface_list,
2379 &surface->layer_link);
2380 weston_surface_assign_output(surface);
2381 pixman_region32_init(&surface->input);
2383 /* Here's the dirty little trick that makes the
2384 * repaint debugging work: we force an
2385 * update_transform first to update dependent state
2386 * and clear the geometry.dirty bit. Then we clear
2387 * the surface damage so it only gets repainted
2388 * piecewise as we repaint other things. */
2390 weston_surface_update_transform(surface);
2391 pixman_region32_fini(&surface->damage);
2392 pixman_region32_init(&surface->damage);
2393 shell->debug_repaint_surface = surface;
2398 shell_destroy(struct wl_listener *listener, void *data)
2400 struct desktop_shell *shell =
2401 container_of(listener, struct desktop_shell, destroy_listener);
2403 if (shell->child.client)
2404 wl_client_destroy(shell->child.client);
2406 free(shell->screensaver.path);
2411 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
2415 /* fixed bindings */
2416 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2417 MODIFIER_CTRL | MODIFIER_ALT,
2418 terminate_binding, ec);
2419 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2420 click_to_activate_binding, shell);
2421 weston_compositor_add_binding(ec, 0, 0,
2422 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2423 MODIFIER_SUPER | MODIFIER_ALT,
2424 surface_opacity_binding, NULL);
2425 weston_compositor_add_binding(ec, 0, 0,
2426 WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2427 MODIFIER_SUPER, zoom_binding, NULL);
2429 /* configurable bindings */
2430 mod = shell->binding_modifier;
2431 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, mod,
2432 move_binding, shell);
2433 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, mod,
2434 resize_binding, shell);
2435 weston_compositor_add_binding(ec, 0, BTN_RIGHT, 0, mod,
2436 rotate_binding, NULL);
2437 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, mod,
2438 switcher_binding, shell);
2439 weston_compositor_add_binding(ec, KEY_F9, 0, 0, mod,
2440 backlight_binding, ec);
2441 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2442 backlight_binding, ec);
2443 weston_compositor_add_binding(ec, KEY_F10, 0, 0, mod,
2444 backlight_binding, ec);
2445 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2446 backlight_binding, ec);
2447 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, mod,
2448 debug_repaint_binding, shell);
2452 shell_init(struct weston_compositor *ec);
2455 shell_init(struct weston_compositor *ec)
2457 struct desktop_shell *shell;
2459 shell = malloc(sizeof *shell);
2463 memset(shell, 0, sizeof *shell);
2464 shell->compositor = ec;
2466 shell->destroy_listener.notify = shell_destroy;
2467 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
2468 shell->lock_listener.notify = lock;
2469 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
2470 shell->unlock_listener.notify = unlock;
2471 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
2472 ec->ping_handler = ping_handler;
2474 wl_list_init(&shell->backgrounds);
2475 wl_list_init(&shell->panels);
2476 wl_list_init(&shell->screensaver.surfaces);
2478 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2479 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2480 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2481 weston_layer_init(&shell->background_layer,
2482 &shell->toplevel_layer.link);
2483 wl_list_init(&shell->lock_layer.surface_list);
2485 shell_configuration(shell);
2487 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2488 shell, bind_shell) == NULL)
2491 if (wl_display_add_global(ec->wl_display,
2492 &desktop_shell_interface,
2493 shell, bind_desktop_shell) == NULL)
2496 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2497 shell, bind_screensaver) == NULL)
2500 shell->child.deathstamp = weston_compositor_get_time();
2501 if (launch_desktop_shell_process(shell) != 0)
2504 shell_add_bindings(ec, shell);