2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <linux/input.h>
35 #include <sys/types.h>
38 #include "desktop-shell-server-protocol.h"
39 #include "workspaces-server-protocol.h"
40 #include "../shared/config-parser.h"
41 #include "xdg-shell-server-protocol.h"
43 #define DEFAULT_NUM_WORKSPACES 1
44 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
47 #define static_assert(cond, msg)
51 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
59 enum shell_surface_type {
61 SHELL_SURFACE_TOPLEVEL,
63 SHELL_SURFACE_XWAYLAND
69 * Surface stacking and ordering.
71 * This is handled using several linked lists of surfaces, organised into
72 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
73 * above all of the surfaces in the layer below. The set of layers is static and
74 * in the following order (top-most first):
75 * • Lock layer (only ever displayed on its own)
83 * The list of layers may be manipulated to remove whole layers of surfaces from
84 * display. For example, when locking the screen, all layers except the lock
87 * A surface’s layer is modified on configuring the surface, in
88 * set_surface_type() (which is only called when the surface’s type change is
89 * _committed_). If a surface’s type changes (e.g. when making a window
90 * fullscreen) its layer changes too.
92 * In order to allow popup and transient surfaces to be correctly stacked above
93 * their parent surfaces, each surface tracks both its parent surface, and a
94 * linked list of its children. When a surface’s layer is updated, so are the
95 * layers of its children. Note that child surfaces are *not* the same as
96 * subsurfaces — child/parent surfaces are purely for maintaining stacking
99 * The children_link list of siblings of a surface (i.e. those surfaces which
100 * have the same parent) only contains weston_surfaces which have a
101 * shell_surface. Stacking is not implemented for non-shell_surface
102 * weston_surfaces. This means that the following implication does *not* hold:
103 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
106 struct shell_surface {
107 struct wl_resource *resource;
108 struct wl_signal destroy_signal;
109 struct shell_client *owner;
111 struct weston_surface *surface;
112 struct weston_view *view;
113 int32_t last_width, last_height;
114 struct wl_listener surface_destroy_listener;
115 struct wl_listener resource_destroy_listener;
117 struct weston_surface *parent;
118 struct wl_list children_list; /* child surfaces of this one */
119 struct wl_list children_link; /* sibling surfaces of this one */
120 struct desktop_shell *shell;
122 enum shell_surface_type type;
124 int32_t saved_x, saved_y;
125 int32_t saved_width, saved_height;
126 bool saved_position_valid;
127 bool saved_size_valid;
128 bool saved_rotation_valid;
129 int unresponsive, grabbed;
130 uint32_t resize_edges;
133 struct weston_transform transform;
134 struct weston_matrix rotation;
138 struct wl_list grab_link;
140 struct shell_seat *shseat;
150 enum wl_shell_surface_fullscreen_method type;
151 struct weston_transform transform; /* matrix from x, y */
153 struct weston_view *black_view;
156 struct weston_transform workspace_transform;
158 struct weston_output *fullscreen_output;
159 struct weston_output *output;
162 const struct weston_shell_client *client;
164 struct surface_state {
169 } state, next_state, requested_state; /* surface states */
171 bool state_requested;
174 int32_t x, y, width, height;
175 } geometry, next_geometry;
176 bool has_set_geometry, has_next_geometry;
182 struct weston_pointer_grab grab;
183 struct shell_surface *shsurf;
184 struct wl_listener shsurf_destroy_listener;
187 struct shell_touch_grab {
188 struct weston_touch_grab grab;
189 struct shell_surface *shsurf;
190 struct wl_listener shsurf_destroy_listener;
191 struct weston_touch *touch;
194 struct weston_move_grab {
195 struct shell_grab base;
197 int client_initiated;
200 struct weston_touch_move_grab {
201 struct shell_touch_grab base;
207 struct shell_grab base;
208 struct weston_matrix rotation;
216 struct weston_seat *seat;
217 struct wl_listener seat_destroy_listener;
218 struct weston_surface *focused_surface;
220 struct wl_listener caps_changed_listener;
221 struct wl_listener pointer_focus_listener;
222 struct wl_listener keyboard_focus_listener;
225 struct weston_pointer_grab grab;
226 struct weston_touch_grab touch_grab;
227 struct wl_list surfaces_list;
228 struct wl_client *client;
230 enum { POINTER, TOUCH } type;
234 struct shell_client {
235 struct wl_resource *resource;
236 struct wl_client *client;
237 struct desktop_shell *shell;
238 struct wl_listener destroy_listener;
239 struct wl_event_source *ping_timer;
240 uint32_t ping_serial;
244 static struct desktop_shell *
245 shell_surface_get_shell(struct shell_surface *shsurf);
248 surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
251 shell_fade_startup(struct desktop_shell *shell);
253 static struct shell_seat *
254 get_shell_seat(struct weston_seat *seat);
257 get_output_panel_size(struct desktop_shell *shell,
258 struct weston_output *output,
259 int *width, int *height);
262 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
265 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
268 shell_surface_is_xdg_surface(struct shell_surface *shsurf);
271 shell_surface_is_xdg_popup(struct shell_surface *shsurf);
274 shell_surface_set_parent(struct shell_surface *shsurf,
275 struct weston_surface *parent);
278 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
280 struct desktop_shell *shell;
281 struct weston_view *top_fs_ev;
283 shell = shell_surface_get_shell(shsurf);
285 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
288 top_fs_ev = container_of(shell->fullscreen_layer.view_list.link.next,
291 return (shsurf == get_shell_surface(top_fs_ev->surface));
295 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
297 struct shell_grab *grab;
299 grab = container_of(listener, struct shell_grab,
300 shsurf_destroy_listener);
306 get_default_view(struct weston_surface *surface)
308 struct shell_surface *shsurf;
309 struct weston_view *view;
311 if (!surface || wl_list_empty(&surface->views))
314 shsurf = get_shell_surface(surface);
318 wl_list_for_each(view, &surface->views, surface_link)
319 if (weston_view_is_mapped(view))
322 return container_of(surface->views.next, struct weston_view, surface_link);
326 popup_grab_end(struct weston_pointer *pointer);
328 touch_popup_grab_end(struct weston_touch *touch);
331 shell_grab_start(struct shell_grab *grab,
332 const struct weston_pointer_grab_interface *interface,
333 struct shell_surface *shsurf,
334 struct weston_pointer *pointer,
335 enum desktop_shell_cursor cursor)
337 struct desktop_shell *shell = shsurf->shell;
339 popup_grab_end(pointer);
340 if (pointer->seat->touch)
341 touch_popup_grab_end(pointer->seat->touch);
343 grab->grab.interface = interface;
344 grab->shsurf = shsurf;
345 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
346 wl_signal_add(&shsurf->destroy_signal,
347 &grab->shsurf_destroy_listener);
350 weston_pointer_start_grab(pointer, &grab->grab);
351 if (shell->child.desktop_shell) {
352 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
354 weston_pointer_set_focus(pointer,
355 get_default_view(shell->grab_surface),
356 wl_fixed_from_int(0),
357 wl_fixed_from_int(0));
362 get_output_panel_size(struct desktop_shell *shell,
363 struct weston_output *output,
367 struct weston_view *view;
375 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
378 if (view->surface->output != output)
381 switch (shell->panel_position) {
382 case DESKTOP_SHELL_PANEL_POSITION_TOP:
383 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
385 weston_view_to_global_float(view,
386 view->surface->width, 0,
390 *height = view->surface->height + (int) y;
393 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
394 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
395 weston_view_to_global_float(view,
396 0, view->surface->height,
399 *width = view->surface->width + (int) x;
404 /* we've already set width and height to
405 * fallback values. */
410 /* the correct view wasn't found */
414 get_output_work_area(struct desktop_shell *shell,
415 struct weston_output *output,
416 pixman_rectangle32_t *area)
418 int32_t panel_width = 0, panel_height = 0;
423 get_output_panel_size(shell, output, &panel_width, &panel_height);
425 switch (shell->panel_position) {
426 case DESKTOP_SHELL_PANEL_POSITION_TOP:
428 area->y = panel_height;
429 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
430 area->width = output->width;
431 area->height = output->height - panel_height;
433 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
434 area->x = panel_width;
435 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
436 area->width = output->width - panel_width;
437 area->height = output->height;
443 send_configure_for_surface(struct shell_surface *shsurf)
445 int32_t width, height;
446 struct surface_state *state;
448 if (shsurf->state_requested)
449 state = &shsurf->requested_state;
450 else if (shsurf->state_changed)
451 state = &shsurf->next_state;
453 state = &shsurf->state;
455 if (state->fullscreen) {
456 width = shsurf->output->width;
457 height = shsurf->output->height;
458 } else if (state->maximized) {
459 struct desktop_shell *shell;
460 pixman_rectangle32_t area;
462 shell = shell_surface_get_shell(shsurf);
463 get_output_work_area(shell, shsurf->output, &area);
466 height = area.height;
472 shsurf->client->send_configure(shsurf->surface, width, height);
476 shell_surface_state_changed(struct shell_surface *shsurf)
478 if (shell_surface_is_xdg_surface(shsurf))
479 send_configure_for_surface(shsurf);
483 shell_grab_end(struct shell_grab *grab)
486 wl_list_remove(&grab->shsurf_destroy_listener.link);
487 grab->shsurf->grabbed = 0;
489 if (grab->shsurf->resize_edges) {
490 grab->shsurf->resize_edges = 0;
491 shell_surface_state_changed(grab->shsurf);
495 weston_pointer_end_grab(grab->grab.pointer);
499 shell_touch_grab_start(struct shell_touch_grab *grab,
500 const struct weston_touch_grab_interface *interface,
501 struct shell_surface *shsurf,
502 struct weston_touch *touch)
504 struct desktop_shell *shell = shsurf->shell;
506 touch_popup_grab_end(touch);
507 if (touch->seat->pointer)
508 popup_grab_end(touch->seat->pointer);
510 grab->grab.interface = interface;
511 grab->shsurf = shsurf;
512 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
513 wl_signal_add(&shsurf->destroy_signal,
514 &grab->shsurf_destroy_listener);
519 weston_touch_start_grab(touch, &grab->grab);
520 if (shell->child.desktop_shell)
521 weston_touch_set_focus(touch->seat,
522 get_default_view(shell->grab_surface));
526 shell_touch_grab_end(struct shell_touch_grab *grab)
529 wl_list_remove(&grab->shsurf_destroy_listener.link);
530 grab->shsurf->grabbed = 0;
533 weston_touch_end_grab(grab->touch);
537 center_on_output(struct weston_view *view,
538 struct weston_output *output);
540 static enum weston_keyboard_modifier
541 get_modifier(char *modifier)
544 return MODIFIER_SUPER;
546 if (!strcmp("ctrl", modifier))
547 return MODIFIER_CTRL;
548 else if (!strcmp("alt", modifier))
550 else if (!strcmp("super", modifier))
551 return MODIFIER_SUPER;
553 return MODIFIER_SUPER;
556 static enum animation_type
557 get_animation_type(char *animation)
560 return ANIMATION_NONE;
562 if (!strcmp("zoom", animation))
563 return ANIMATION_ZOOM;
564 else if (!strcmp("fade", animation))
565 return ANIMATION_FADE;
566 else if (!strcmp("dim-layer", animation))
567 return ANIMATION_DIM_LAYER;
569 return ANIMATION_NONE;
573 shell_configuration(struct desktop_shell *shell)
575 struct weston_config_section *section;
579 section = weston_config_get_section(shell->compositor->config,
580 "screensaver", NULL, NULL);
581 weston_config_section_get_string(section,
582 "path", &shell->screensaver.path, NULL);
583 weston_config_section_get_int(section, "duration", &duration, 60);
584 shell->screensaver.duration = duration * 1000;
586 section = weston_config_get_section(shell->compositor->config,
587 "shell", NULL, NULL);
588 asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
589 WESTON_SHELL_CLIENT);
590 weston_config_section_get_string(section,
591 "client", &s, client);
594 weston_config_section_get_string(section,
595 "binding-modifier", &s, "super");
596 shell->binding_modifier = get_modifier(s);
599 weston_config_section_get_string(section,
600 "exposay-modifier", &s, "none");
601 if (strcmp(s, "none") == 0)
602 shell->exposay_modifier = 0;
604 shell->exposay_modifier = get_modifier(s);
607 weston_config_section_get_string(section, "animation", &s, "none");
608 shell->win_animation_type = get_animation_type(s);
610 weston_config_section_get_string(section, "close-animation", &s, "fade");
611 shell->win_close_animation_type = get_animation_type(s);
613 weston_config_section_get_string(section,
614 "startup-animation", &s, "fade");
615 shell->startup_animation_type = get_animation_type(s);
617 if (shell->startup_animation_type == ANIMATION_ZOOM)
618 shell->startup_animation_type = ANIMATION_NONE;
619 weston_config_section_get_string(section, "focus-animation", &s, "none");
620 shell->focus_animation_type = get_animation_type(s);
622 weston_config_section_get_uint(section, "num-workspaces",
623 &shell->workspaces.num,
624 DEFAULT_NUM_WORKSPACES);
627 struct weston_output *
628 get_default_output(struct weston_compositor *compositor)
630 return container_of(compositor->output_list.next,
631 struct weston_output, link);
635 /* no-op func for checking focus surface */
637 focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
641 static struct focus_surface *
642 get_focus_surface(struct weston_surface *surface)
644 if (surface->configure == focus_surface_configure)
645 return surface->configure_private;
651 is_focus_surface (struct weston_surface *es)
653 return (es->configure == focus_surface_configure);
657 is_focus_view (struct weston_view *view)
659 return is_focus_surface (view->surface);
662 static struct focus_surface *
663 create_focus_surface(struct weston_compositor *ec,
664 struct weston_output *output)
666 struct focus_surface *fsurf = NULL;
667 struct weston_surface *surface = NULL;
669 fsurf = malloc(sizeof *fsurf);
673 fsurf->surface = weston_surface_create(ec);
674 surface = fsurf->surface;
675 if (surface == NULL) {
680 surface->configure = focus_surface_configure;
681 surface->output = output;
682 surface->configure_private = fsurf;
684 fsurf->view = weston_view_create(surface);
685 if (fsurf->view == NULL) {
686 weston_surface_destroy(surface);
690 fsurf->view->output = output;
692 weston_surface_set_size(surface, output->width, output->height);
693 weston_view_set_position(fsurf->view, output->x, output->y);
694 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
695 pixman_region32_fini(&surface->opaque);
696 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
697 output->width, output->height);
698 pixman_region32_fini(&surface->input);
699 pixman_region32_init(&surface->input);
701 wl_list_init(&fsurf->workspace_transform.link);
707 focus_surface_destroy(struct focus_surface *fsurf)
709 weston_surface_destroy(fsurf->surface);
714 focus_animation_done(struct weston_view_animation *animation, void *data)
716 struct workspace *ws = data;
718 ws->focus_animation = NULL;
722 focus_state_destroy(struct focus_state *state)
724 wl_list_remove(&state->seat_destroy_listener.link);
725 wl_list_remove(&state->surface_destroy_listener.link);
730 focus_state_seat_destroy(struct wl_listener *listener, void *data)
732 struct focus_state *state = container_of(listener,
734 seat_destroy_listener);
736 wl_list_remove(&state->link);
737 focus_state_destroy(state);
741 focus_state_surface_destroy(struct wl_listener *listener, void *data)
743 struct focus_state *state = container_of(listener,
745 surface_destroy_listener);
746 struct desktop_shell *shell;
747 struct weston_surface *main_surface, *next;
748 struct weston_view *view;
750 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
753 wl_list_for_each(view,
754 &state->ws->layer.view_list.link, layer_link.link) {
755 if (view->surface == main_surface)
757 if (is_focus_view(view))
760 next = view->surface;
764 /* if the focus was a sub-surface, activate its main surface */
765 if (main_surface != state->keyboard_focus)
768 shell = state->seat->compositor->shell_interface.shell;
770 state->keyboard_focus = NULL;
771 activate(shell, next, state->seat, true);
773 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
774 if (state->ws->focus_animation)
775 weston_view_animation_destroy(state->ws->focus_animation);
777 state->ws->focus_animation = weston_fade_run(
778 state->ws->fsurf_front->view,
779 state->ws->fsurf_front->view->alpha, 0.0, 300,
780 focus_animation_done, state->ws);
783 wl_list_remove(&state->link);
784 focus_state_destroy(state);
788 static struct focus_state *
789 focus_state_create(struct weston_seat *seat, struct workspace *ws)
791 struct focus_state *state;
793 state = malloc(sizeof *state);
797 state->keyboard_focus = NULL;
800 wl_list_insert(&ws->focus_list, &state->link);
802 state->seat_destroy_listener.notify = focus_state_seat_destroy;
803 state->surface_destroy_listener.notify = focus_state_surface_destroy;
804 wl_signal_add(&seat->destroy_signal,
805 &state->seat_destroy_listener);
806 wl_list_init(&state->surface_destroy_listener.link);
811 static struct focus_state *
812 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
814 struct workspace *ws = get_current_workspace(shell);
815 struct focus_state *state;
817 wl_list_for_each(state, &ws->focus_list, link)
818 if (state->seat == seat)
821 if (&state->link == &ws->focus_list)
822 state = focus_state_create(seat, ws);
828 focus_state_set_focus(struct focus_state *state,
829 struct weston_surface *surface)
831 if (state->keyboard_focus) {
832 wl_list_remove(&state->surface_destroy_listener.link);
833 wl_list_init(&state->surface_destroy_listener.link);
836 state->keyboard_focus = surface;
838 wl_signal_add(&surface->destroy_signal,
839 &state->surface_destroy_listener);
843 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
845 struct focus_state *state, *next;
846 struct weston_surface *surface;
847 struct wl_list pending_seat_list;
848 struct weston_seat *seat, *next_seat;
850 /* Temporarily steal the list of seats so that we can keep
851 * track of the seats we've already processed */
852 wl_list_init(&pending_seat_list);
853 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
854 wl_list_init(&shell->compositor->seat_list);
856 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
857 wl_list_remove(&state->seat->link);
858 wl_list_insert(&shell->compositor->seat_list,
861 if (state->seat->keyboard == NULL)
864 surface = state->keyboard_focus;
866 weston_keyboard_set_focus(state->seat->keyboard, surface);
869 /* For any remaining seats that we don't have a focus state
870 * for we'll reset the keyboard focus to NULL */
871 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
872 wl_list_insert(&shell->compositor->seat_list, &seat->link);
874 if (seat->keyboard == NULL)
877 weston_keyboard_set_focus(seat->keyboard, NULL);
882 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
883 struct weston_seat *seat)
885 struct focus_state *state;
887 wl_list_for_each(state, &ws->focus_list, link) {
888 if (state->seat == seat) {
889 focus_state_set_focus(state, seat->keyboard->focus);
896 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
897 struct weston_surface *surface)
899 struct focus_state *state;
901 wl_list_for_each(state, &ws->focus_list, link)
902 if (state->keyboard_focus == surface)
903 focus_state_set_focus(state, NULL);
907 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
908 struct weston_view *from, struct weston_view *to)
910 struct weston_output *output;
911 bool focus_surface_created = false;
913 /* FIXME: Only support dim animation using two layers */
914 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
917 output = get_default_output(shell->compositor);
918 if (ws->fsurf_front == NULL && (from || to)) {
919 ws->fsurf_front = create_focus_surface(shell->compositor, output);
920 if (ws->fsurf_front == NULL)
922 ws->fsurf_front->view->alpha = 0.0;
924 ws->fsurf_back = create_focus_surface(shell->compositor, output);
925 if (ws->fsurf_back == NULL) {
926 focus_surface_destroy(ws->fsurf_front);
929 ws->fsurf_back->view->alpha = 0.0;
931 focus_surface_created = true;
933 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
934 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
937 if (ws->focus_animation) {
938 weston_view_animation_destroy(ws->focus_animation);
939 ws->focus_animation = NULL;
943 weston_layer_entry_insert(&to->layer_link,
944 &ws->fsurf_front->view->layer_link);
946 weston_layer_entry_insert(&ws->layer.view_list,
947 &ws->fsurf_front->view->layer_link);
949 if (focus_surface_created) {
950 ws->focus_animation = weston_fade_run(
951 ws->fsurf_front->view,
952 ws->fsurf_front->view->alpha, 0.4, 300,
953 focus_animation_done, ws);
955 weston_layer_entry_insert(&from->layer_link,
956 &ws->fsurf_back->view->layer_link);
957 ws->focus_animation = weston_stable_fade_run(
958 ws->fsurf_front->view, 0.0,
959 ws->fsurf_back->view, 0.4,
960 focus_animation_done, ws);
962 weston_layer_entry_insert(&ws->layer.view_list,
963 &ws->fsurf_back->view->layer_link);
964 ws->focus_animation = weston_stable_fade_run(
965 ws->fsurf_front->view, 0.0,
966 ws->fsurf_back->view, 0.4,
967 focus_animation_done, ws);
972 workspace_destroy(struct workspace *ws)
974 struct focus_state *state, *next;
976 wl_list_for_each_safe(state, next, &ws->focus_list, link)
977 focus_state_destroy(state);
980 focus_surface_destroy(ws->fsurf_front);
982 focus_surface_destroy(ws->fsurf_back);
988 seat_destroyed(struct wl_listener *listener, void *data)
990 struct weston_seat *seat = data;
991 struct focus_state *state, *next;
992 struct workspace *ws = container_of(listener,
994 seat_destroyed_listener);
996 wl_list_for_each_safe(state, next, &ws->focus_list, link)
997 if (state->seat == seat)
998 wl_list_remove(&state->link);
1001 static struct workspace *
1002 workspace_create(void)
1004 struct workspace *ws = malloc(sizeof *ws);
1008 weston_layer_init(&ws->layer, NULL);
1010 wl_list_init(&ws->focus_list);
1011 wl_list_init(&ws->seat_destroyed_listener.link);
1012 ws->seat_destroyed_listener.notify = seat_destroyed;
1013 ws->fsurf_front = NULL;
1014 ws->fsurf_back = NULL;
1015 ws->focus_animation = NULL;
1021 workspace_is_empty(struct workspace *ws)
1023 return wl_list_empty(&ws->layer.view_list.link);
1026 static struct workspace *
1027 get_workspace(struct desktop_shell *shell, unsigned int index)
1029 struct workspace **pws = shell->workspaces.array.data;
1030 assert(index < shell->workspaces.num);
1036 get_current_workspace(struct desktop_shell *shell)
1038 return get_workspace(shell, shell->workspaces.current);
1042 activate_workspace(struct desktop_shell *shell, unsigned int index)
1044 struct workspace *ws;
1046 ws = get_workspace(shell, index);
1047 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1049 shell->workspaces.current = index;
1053 get_output_height(struct weston_output *output)
1055 return abs(output->region.extents.y1 - output->region.extents.y2);
1059 view_translate(struct workspace *ws, struct weston_view *view, double d)
1061 struct weston_transform *transform;
1063 if (is_focus_view(view)) {
1064 struct focus_surface *fsurf = get_focus_surface(view->surface);
1065 transform = &fsurf->workspace_transform;
1067 struct shell_surface *shsurf = get_shell_surface(view->surface);
1068 transform = &shsurf->workspace_transform;
1071 if (wl_list_empty(&transform->link))
1072 wl_list_insert(view->geometry.transformation_list.prev,
1075 weston_matrix_init(&transform->matrix);
1076 weston_matrix_translate(&transform->matrix,
1078 weston_view_geometry_dirty(view);
1082 workspace_translate_out(struct workspace *ws, double fraction)
1084 struct weston_view *view;
1085 unsigned int height;
1088 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1089 height = get_output_height(view->surface->output);
1090 d = height * fraction;
1092 view_translate(ws, view, d);
1097 workspace_translate_in(struct workspace *ws, double fraction)
1099 struct weston_view *view;
1100 unsigned int height;
1103 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1104 height = get_output_height(view->surface->output);
1107 d = -(height - height * fraction);
1109 d = height + height * fraction;
1111 view_translate(ws, view, d);
1116 broadcast_current_workspace_state(struct desktop_shell *shell)
1118 struct wl_resource *resource;
1120 wl_resource_for_each(resource, &shell->workspaces.client_list)
1121 workspace_manager_send_state(resource,
1122 shell->workspaces.current,
1123 shell->workspaces.num);
1127 reverse_workspace_change_animation(struct desktop_shell *shell,
1129 struct workspace *from,
1130 struct workspace *to)
1132 shell->workspaces.current = index;
1134 shell->workspaces.anim_to = to;
1135 shell->workspaces.anim_from = from;
1136 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1137 shell->workspaces.anim_timestamp = 0;
1139 weston_compositor_schedule_repaint(shell->compositor);
1143 workspace_deactivate_transforms(struct workspace *ws)
1145 struct weston_view *view;
1146 struct weston_transform *transform;
1148 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1149 if (is_focus_view(view)) {
1150 struct focus_surface *fsurf = get_focus_surface(view->surface);
1151 transform = &fsurf->workspace_transform;
1153 struct shell_surface *shsurf = get_shell_surface(view->surface);
1154 transform = &shsurf->workspace_transform;
1157 if (!wl_list_empty(&transform->link)) {
1158 wl_list_remove(&transform->link);
1159 wl_list_init(&transform->link);
1161 weston_view_geometry_dirty(view);
1166 finish_workspace_change_animation(struct desktop_shell *shell,
1167 struct workspace *from,
1168 struct workspace *to)
1170 struct weston_view *view;
1172 weston_compositor_schedule_repaint(shell->compositor);
1174 /* Views that extend past the bottom of the output are still
1175 * visible after the workspace animation ends but before its layer
1176 * is hidden. In that case, we need to damage below those views so
1177 * that the screen is properly repainted. */
1178 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1179 weston_view_damage_below(view);
1181 wl_list_remove(&shell->workspaces.animation.link);
1182 workspace_deactivate_transforms(from);
1183 workspace_deactivate_transforms(to);
1184 shell->workspaces.anim_to = NULL;
1186 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1190 animate_workspace_change_frame(struct weston_animation *animation,
1191 struct weston_output *output, uint32_t msecs)
1193 struct desktop_shell *shell =
1194 container_of(animation, struct desktop_shell,
1195 workspaces.animation);
1196 struct workspace *from = shell->workspaces.anim_from;
1197 struct workspace *to = shell->workspaces.anim_to;
1201 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1202 finish_workspace_change_animation(shell, from, to);
1206 if (shell->workspaces.anim_timestamp == 0) {
1207 if (shell->workspaces.anim_current == 0.0)
1208 shell->workspaces.anim_timestamp = msecs;
1210 shell->workspaces.anim_timestamp =
1212 /* Invers of movement function 'y' below. */
1213 (asin(1.0 - shell->workspaces.anim_current) *
1214 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1218 t = msecs - shell->workspaces.anim_timestamp;
1224 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1227 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1228 weston_compositor_schedule_repaint(shell->compositor);
1230 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1231 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1232 shell->workspaces.anim_current = y;
1234 weston_compositor_schedule_repaint(shell->compositor);
1237 finish_workspace_change_animation(shell, from, to);
1241 animate_workspace_change(struct desktop_shell *shell,
1243 struct workspace *from,
1244 struct workspace *to)
1246 struct weston_output *output;
1250 if (index > shell->workspaces.current)
1255 shell->workspaces.current = index;
1257 shell->workspaces.anim_dir = dir;
1258 shell->workspaces.anim_from = from;
1259 shell->workspaces.anim_to = to;
1260 shell->workspaces.anim_current = 0.0;
1261 shell->workspaces.anim_timestamp = 0;
1263 output = container_of(shell->compositor->output_list.next,
1264 struct weston_output, link);
1265 wl_list_insert(&output->animation_list,
1266 &shell->workspaces.animation.link);
1268 wl_list_insert(from->layer.link.prev, &to->layer.link);
1270 workspace_translate_in(to, 0);
1272 restore_focus_state(shell, to);
1274 weston_compositor_schedule_repaint(shell->compositor);
1278 update_workspace(struct desktop_shell *shell, unsigned int index,
1279 struct workspace *from, struct workspace *to)
1281 shell->workspaces.current = index;
1282 wl_list_insert(&from->layer.link, &to->layer.link);
1283 wl_list_remove(&from->layer.link);
1287 change_workspace(struct desktop_shell *shell, unsigned int index)
1289 struct workspace *from;
1290 struct workspace *to;
1291 struct focus_state *state;
1293 if (index == shell->workspaces.current)
1296 /* Don't change workspace when there is any fullscreen surfaces. */
1297 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1300 from = get_current_workspace(shell);
1301 to = get_workspace(shell, index);
1303 if (shell->workspaces.anim_from == to &&
1304 shell->workspaces.anim_to == from) {
1305 restore_focus_state(shell, to);
1306 reverse_workspace_change_animation(shell, index, from, to);
1307 broadcast_current_workspace_state(shell);
1311 if (shell->workspaces.anim_to != NULL)
1312 finish_workspace_change_animation(shell,
1313 shell->workspaces.anim_from,
1314 shell->workspaces.anim_to);
1316 restore_focus_state(shell, to);
1318 if (shell->focus_animation_type != ANIMATION_NONE) {
1319 wl_list_for_each(state, &from->focus_list, link)
1320 if (state->keyboard_focus)
1321 animate_focus_change(shell, from,
1322 get_default_view(state->keyboard_focus), NULL);
1324 wl_list_for_each(state, &to->focus_list, link)
1325 if (state->keyboard_focus)
1326 animate_focus_change(shell, to,
1327 NULL, get_default_view(state->keyboard_focus));
1330 if (workspace_is_empty(to) && workspace_is_empty(from))
1331 update_workspace(shell, index, from, to);
1333 animate_workspace_change(shell, index, from, to);
1335 broadcast_current_workspace_state(shell);
1339 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1341 struct wl_list *list = &ws->layer.view_list.link;
1344 if (wl_list_empty(list))
1349 if (e->next != list)
1352 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1356 move_surface_to_workspace(struct desktop_shell *shell,
1357 struct shell_surface *shsurf,
1360 struct workspace *from;
1361 struct workspace *to;
1362 struct weston_seat *seat;
1363 struct weston_surface *focus;
1364 struct weston_view *view;
1366 if (workspace == shell->workspaces.current)
1369 view = get_default_view(shsurf->surface);
1373 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1375 if (workspace >= shell->workspaces.num)
1376 workspace = shell->workspaces.num - 1;
1378 from = get_current_workspace(shell);
1379 to = get_workspace(shell, workspace);
1381 weston_layer_entry_remove(&view->layer_link);
1382 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1384 shell_surface_update_child_surface_layers(shsurf);
1386 drop_focus_state(shell, from, view->surface);
1387 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1388 if (!seat->keyboard)
1391 focus = weston_surface_get_main_surface(seat->keyboard->focus);
1392 if (focus == view->surface)
1393 weston_keyboard_set_focus(seat->keyboard, NULL);
1396 weston_view_damage_below(view);
1400 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1401 struct weston_seat *seat,
1404 struct weston_surface *surface;
1405 struct weston_view *view;
1406 struct shell_surface *shsurf;
1407 struct workspace *from;
1408 struct workspace *to;
1409 struct focus_state *state;
1411 surface = weston_surface_get_main_surface(seat->keyboard->focus);
1412 view = get_default_view(surface);
1414 index == shell->workspaces.current ||
1415 is_focus_view(view))
1418 from = get_current_workspace(shell);
1419 to = get_workspace(shell, index);
1421 weston_layer_entry_remove(&view->layer_link);
1422 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1424 shsurf = get_shell_surface(surface);
1426 shell_surface_update_child_surface_layers(shsurf);
1428 replace_focus_state(shell, to, seat);
1429 drop_focus_state(shell, from, surface);
1431 if (shell->workspaces.anim_from == to &&
1432 shell->workspaces.anim_to == from) {
1433 wl_list_remove(&to->layer.link);
1434 wl_list_insert(from->layer.link.prev, &to->layer.link);
1436 reverse_workspace_change_animation(shell, index, from, to);
1437 broadcast_current_workspace_state(shell);
1442 if (shell->workspaces.anim_to != NULL)
1443 finish_workspace_change_animation(shell,
1444 shell->workspaces.anim_from,
1445 shell->workspaces.anim_to);
1447 if (workspace_is_empty(from) &&
1448 workspace_has_only(to, surface))
1449 update_workspace(shell, index, from, to);
1451 if (shsurf != NULL &&
1452 wl_list_empty(&shsurf->workspace_transform.link))
1453 wl_list_insert(&shell->workspaces.anim_sticky_list,
1454 &shsurf->workspace_transform.link);
1456 animate_workspace_change(shell, index, from, to);
1459 broadcast_current_workspace_state(shell);
1461 state = ensure_focus_state(shell, seat);
1463 focus_state_set_focus(state, surface);
1467 workspace_manager_move_surface(struct wl_client *client,
1468 struct wl_resource *resource,
1469 struct wl_resource *surface_resource,
1472 struct desktop_shell *shell = wl_resource_get_user_data(resource);
1473 struct weston_surface *surface =
1474 wl_resource_get_user_data(surface_resource);
1475 struct weston_surface *main_surface;
1476 struct shell_surface *shell_surface;
1478 main_surface = weston_surface_get_main_surface(surface);
1479 shell_surface = get_shell_surface(main_surface);
1480 if (shell_surface == NULL)
1483 move_surface_to_workspace(shell, shell_surface, workspace);
1486 static const struct workspace_manager_interface workspace_manager_implementation = {
1487 workspace_manager_move_surface,
1491 unbind_resource(struct wl_resource *resource)
1493 wl_list_remove(wl_resource_get_link(resource));
1497 bind_workspace_manager(struct wl_client *client,
1498 void *data, uint32_t version, uint32_t id)
1500 struct desktop_shell *shell = data;
1501 struct wl_resource *resource;
1503 resource = wl_resource_create(client,
1504 &workspace_manager_interface, 1, id);
1506 if (resource == NULL) {
1507 weston_log("couldn't add workspace manager object");
1511 wl_resource_set_implementation(resource,
1512 &workspace_manager_implementation,
1513 shell, unbind_resource);
1514 wl_list_insert(&shell->workspaces.client_list,
1515 wl_resource_get_link(resource));
1517 workspace_manager_send_state(resource,
1518 shell->workspaces.current,
1519 shell->workspaces.num);
1523 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1524 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1529 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1531 struct weston_touch_move_grab *move =
1532 (struct weston_touch_move_grab *) container_of(
1533 grab, struct shell_touch_grab, grab);
1538 if (grab->touch->num_tp == 0) {
1539 shell_touch_grab_end(&move->base);
1545 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1546 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1548 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1549 struct shell_surface *shsurf = move->base.shsurf;
1550 struct weston_surface *es;
1551 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1552 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1554 if (!shsurf || !move->active)
1557 es = shsurf->surface;
1559 weston_view_set_position(shsurf->view, dx, dy);
1561 weston_compositor_schedule_repaint(es->compositor);
1565 touch_move_grab_frame(struct weston_touch_grab *grab)
1570 touch_move_grab_cancel(struct weston_touch_grab *grab)
1572 struct weston_touch_move_grab *move =
1573 (struct weston_touch_move_grab *) container_of(
1574 grab, struct shell_touch_grab, grab);
1576 shell_touch_grab_end(&move->base);
1580 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1581 touch_move_grab_down,
1583 touch_move_grab_motion,
1584 touch_move_grab_frame,
1585 touch_move_grab_cancel,
1589 surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1591 struct weston_touch_move_grab *move;
1596 if (shsurf->state.fullscreen || shsurf->state.maximized)
1599 move = malloc(sizeof *move);
1604 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1605 seat->touch->grab_x;
1606 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1607 seat->touch->grab_y;
1609 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1616 noop_grab_focus(struct weston_pointer_grab *grab)
1621 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1623 struct shell_surface *shsurf = move->base.shsurf;
1624 struct weston_pointer *pointer = move->base.grab.pointer;
1625 int x, y, panel_width, panel_height, bottom;
1626 const int safety = 50;
1628 x = wl_fixed_to_int(pointer->x + move->dx);
1629 y = wl_fixed_to_int(pointer->y + move->dy);
1631 if (shsurf->shell->panel_position == DESKTOP_SHELL_PANEL_POSITION_TOP) {
1632 get_output_panel_size(shsurf->shell, shsurf->surface->output,
1633 &panel_width, &panel_height);
1635 bottom = y + shsurf->geometry.height;
1636 if (bottom - panel_height < safety)
1637 y = panel_height + safety -
1638 shsurf->geometry.height;
1640 if (move->client_initiated &&
1641 y + shsurf->geometry.y < panel_height)
1642 y = panel_height - shsurf->geometry.y;
1650 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1651 wl_fixed_t x, wl_fixed_t y)
1653 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1654 struct weston_pointer *pointer = grab->pointer;
1655 struct shell_surface *shsurf = move->base.shsurf;
1658 weston_pointer_move(pointer, x, y);
1662 constrain_position(move, &cx, &cy);
1664 weston_view_set_position(shsurf->view, cx, cy);
1666 weston_compositor_schedule_repaint(shsurf->surface->compositor);
1670 move_grab_button(struct weston_pointer_grab *grab,
1671 uint32_t time, uint32_t button, uint32_t state_w)
1673 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1675 struct weston_pointer *pointer = grab->pointer;
1676 enum wl_pointer_button_state state = state_w;
1678 if (pointer->button_count == 0 &&
1679 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1680 shell_grab_end(shell_grab);
1686 move_grab_cancel(struct weston_pointer_grab *grab)
1688 struct shell_grab *shell_grab =
1689 container_of(grab, struct shell_grab, grab);
1691 shell_grab_end(shell_grab);
1695 static const struct weston_pointer_grab_interface move_grab_interface = {
1703 surface_move(struct shell_surface *shsurf, struct weston_seat *seat,
1704 int client_initiated)
1706 struct weston_move_grab *move;
1711 if (shsurf->grabbed ||
1712 shsurf->state.fullscreen || shsurf->state.maximized)
1715 move = malloc(sizeof *move);
1719 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1720 seat->pointer->grab_x;
1721 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1722 seat->pointer->grab_y;
1723 move->client_initiated = client_initiated;
1725 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1726 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
1732 common_surface_move(struct wl_resource *resource,
1733 struct wl_resource *seat_resource, uint32_t serial)
1735 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1736 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1737 struct weston_surface *surface;
1739 if (seat->pointer &&
1740 seat->pointer->focus &&
1741 seat->pointer->button_count > 0 &&
1742 seat->pointer->grab_serial == serial) {
1743 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1744 if ((surface == shsurf->surface) &&
1745 (surface_move(shsurf, seat, 1) < 0))
1746 wl_resource_post_no_memory(resource);
1747 } else if (seat->touch &&
1748 seat->touch->focus &&
1749 seat->touch->grab_serial == serial) {
1750 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
1751 if ((surface == shsurf->surface) &&
1752 (surface_touch_move(shsurf, seat) < 0))
1753 wl_resource_post_no_memory(resource);
1758 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1759 struct wl_resource *seat_resource, uint32_t serial)
1761 common_surface_move(resource, seat_resource, serial);
1764 struct weston_resize_grab {
1765 struct shell_grab base;
1767 int32_t width, height;
1771 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1772 wl_fixed_t x, wl_fixed_t y)
1774 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1775 struct weston_pointer *pointer = grab->pointer;
1776 struct shell_surface *shsurf = resize->base.shsurf;
1777 int32_t width, height;
1778 wl_fixed_t from_x, from_y;
1779 wl_fixed_t to_x, to_y;
1781 weston_pointer_move(pointer, x, y);
1786 weston_view_from_global_fixed(shsurf->view,
1787 pointer->grab_x, pointer->grab_y,
1789 weston_view_from_global_fixed(shsurf->view,
1790 pointer->x, pointer->y, &to_x, &to_y);
1792 width = resize->width;
1793 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1794 width += wl_fixed_to_int(from_x - to_x);
1795 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1796 width += wl_fixed_to_int(to_x - from_x);
1799 height = resize->height;
1800 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1801 height += wl_fixed_to_int(from_y - to_y);
1802 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1803 height += wl_fixed_to_int(to_y - from_y);
1806 shsurf->client->send_configure(shsurf->surface, width, height);
1810 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
1812 struct shell_surface *shsurf = get_shell_surface(surface);
1816 if (shsurf->resource)
1817 wl_shell_surface_send_configure(shsurf->resource,
1818 shsurf->resize_edges,
1822 static const struct weston_shell_client shell_client = {
1827 resize_grab_button(struct weston_pointer_grab *grab,
1828 uint32_t time, uint32_t button, uint32_t state_w)
1830 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1831 struct weston_pointer *pointer = grab->pointer;
1832 enum wl_pointer_button_state state = state_w;
1834 if (pointer->button_count == 0 &&
1835 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1836 shell_grab_end(&resize->base);
1842 resize_grab_cancel(struct weston_pointer_grab *grab)
1844 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1846 shell_grab_end(&resize->base);
1850 static const struct weston_pointer_grab_interface resize_grab_interface = {
1858 * Returns the bounding box of a surface and all its sub-surfaces,
1859 * in the surface coordinates system. */
1861 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1862 int32_t *y, int32_t *w, int32_t *h) {
1863 pixman_region32_t region;
1864 pixman_box32_t *box;
1865 struct weston_subsurface *subsurface;
1867 pixman_region32_init_rect(®ion, 0, 0,
1871 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1872 pixman_region32_union_rect(®ion, ®ion,
1873 subsurface->position.x,
1874 subsurface->position.y,
1875 subsurface->surface->width,
1876 subsurface->surface->height);
1879 box = pixman_region32_extents(®ion);
1885 *w = box->x2 - box->x1;
1887 *h = box->y2 - box->y1;
1889 pixman_region32_fini(®ion);
1893 surface_resize(struct shell_surface *shsurf,
1894 struct weston_seat *seat, uint32_t edges)
1896 struct weston_resize_grab *resize;
1897 const unsigned resize_topbottom =
1898 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1899 const unsigned resize_leftright =
1900 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1901 const unsigned resize_any = resize_topbottom | resize_leftright;
1903 if (shsurf->grabbed ||
1904 shsurf->state.fullscreen || shsurf->state.maximized)
1907 /* Check for invalid edge combinations. */
1908 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1909 (edges & resize_topbottom) == resize_topbottom ||
1910 (edges & resize_leftright) == resize_leftright)
1913 resize = malloc(sizeof *resize);
1917 resize->edges = edges;
1919 resize->width = shsurf->geometry.width;
1920 resize->height = shsurf->geometry.height;
1922 shsurf->resize_edges = edges;
1923 shell_surface_state_changed(shsurf);
1924 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1925 seat->pointer, edges);
1931 common_surface_resize(struct wl_resource *resource,
1932 struct wl_resource *seat_resource, uint32_t serial,
1935 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1936 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1937 struct weston_surface *surface;
1939 if (seat->pointer == NULL ||
1940 seat->pointer->button_count == 0 ||
1941 seat->pointer->grab_serial != serial ||
1942 seat->pointer->focus == NULL)
1945 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1946 if (surface != shsurf->surface)
1949 if (surface_resize(shsurf, seat, edges) < 0)
1950 wl_resource_post_no_memory(resource);
1954 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1955 struct wl_resource *seat_resource, uint32_t serial,
1958 common_surface_resize(resource, seat_resource, serial, edges);
1962 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1964 struct shell_grab *grab = (struct shell_grab *) base;
1965 struct weston_pointer *pointer = base->pointer;
1966 struct weston_view *view;
1969 view = weston_compositor_pick_view(pointer->seat->compositor,
1970 pointer->x, pointer->y,
1973 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1974 shell_grab_end(grab);
1980 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1981 wl_fixed_t x, wl_fixed_t y)
1983 weston_pointer_move(grab->pointer, x, y);
1987 busy_cursor_grab_button(struct weston_pointer_grab *base,
1988 uint32_t time, uint32_t button, uint32_t state)
1990 struct shell_grab *grab = (struct shell_grab *) base;
1991 struct shell_surface *shsurf = grab->shsurf;
1992 struct weston_seat *seat = grab->grab.pointer->seat;
1994 if (shsurf && button == BTN_LEFT && state) {
1995 activate(shsurf->shell, shsurf->surface, seat, true);
1996 surface_move(shsurf, seat, 0);
1997 } else if (shsurf && button == BTN_RIGHT && state) {
1998 activate(shsurf->shell, shsurf->surface, seat, true);
1999 surface_rotate(shsurf, seat);
2004 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2006 struct shell_grab *grab = (struct shell_grab *) base;
2008 shell_grab_end(grab);
2012 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
2013 busy_cursor_grab_focus,
2014 busy_cursor_grab_motion,
2015 busy_cursor_grab_button,
2016 busy_cursor_grab_cancel,
2020 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2022 struct shell_grab *grab;
2024 if (pointer->grab->interface == &busy_cursor_grab_interface)
2027 grab = malloc(sizeof *grab);
2031 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2032 DESKTOP_SHELL_CURSOR_BUSY);
2033 /* Mark the shsurf as ungrabbed so that button binding is able
2035 shsurf->grabbed = 0;
2039 end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
2041 struct shell_grab *grab;
2042 struct weston_seat *seat;
2044 wl_list_for_each(seat, &compositor->seat_list, link) {
2045 if (seat->pointer == NULL)
2048 grab = (struct shell_grab *) seat->pointer->grab;
2049 if (grab->grab.interface == &busy_cursor_grab_interface &&
2050 wl_resource_get_client(grab->shsurf->resource) == client) {
2051 shell_grab_end(grab);
2058 handle_shell_client_destroy(struct wl_listener *listener, void *data);
2061 xdg_ping_timeout_handler(void *data)
2063 struct shell_client *sc = data;
2064 struct weston_seat *seat;
2065 struct shell_surface *shsurf;
2067 /* Client is not responding */
2068 sc->unresponsive = 1;
2069 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2070 if (seat->pointer == NULL || seat->pointer->focus == NULL)
2072 if (seat->pointer->focus->surface->resource == NULL)
2075 shsurf = get_shell_surface(seat->pointer->focus->surface);
2077 wl_resource_get_client(shsurf->resource) == sc->client)
2078 set_busy_cursor(shsurf, seat->pointer);
2085 handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2087 struct weston_compositor *compositor = shsurf->shell->compositor;
2088 struct shell_client *sc = shsurf->owner;
2089 struct wl_event_loop *loop;
2090 static const int ping_timeout = 200;
2092 if (sc->unresponsive) {
2093 xdg_ping_timeout_handler(sc);
2097 sc->ping_serial = serial;
2098 loop = wl_display_get_event_loop(compositor->wl_display);
2099 if (sc->ping_timer == NULL)
2101 wl_event_loop_add_timer(loop,
2102 xdg_ping_timeout_handler, sc);
2103 if (sc->ping_timer == NULL)
2106 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2108 if (shell_surface_is_xdg_surface(shsurf) ||
2109 shell_surface_is_xdg_popup(shsurf))
2110 xdg_shell_send_ping(sc->resource, serial);
2111 else if (shell_surface_is_wl_shell_surface(shsurf))
2112 wl_shell_surface_send_ping(shsurf->resource, serial);
2116 ping_handler(struct weston_surface *surface, uint32_t serial)
2118 struct shell_surface *shsurf = get_shell_surface(surface);
2122 if (!shsurf->resource)
2124 if (shsurf->surface == shsurf->shell->grab_surface)
2127 handle_xdg_ping(shsurf, serial);
2131 handle_pointer_focus(struct wl_listener *listener, void *data)
2133 struct weston_pointer *pointer = data;
2134 struct weston_view *view = pointer->focus;
2135 struct weston_compositor *compositor;
2141 compositor = view->surface->compositor;
2142 serial = wl_display_next_serial(compositor->wl_display);
2143 ping_handler(view->surface, serial);
2147 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2149 if (--shsurf->focus_count == 0)
2150 shell_surface_state_changed(shsurf);
2154 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2156 if (shsurf->focus_count++ == 0)
2157 shell_surface_state_changed(shsurf);
2161 handle_keyboard_focus(struct wl_listener *listener, void *data)
2163 struct weston_keyboard *keyboard = data;
2164 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2166 if (seat->focused_surface) {
2167 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2169 shell_surface_lose_keyboard_focus(shsurf);
2172 seat->focused_surface = keyboard->focus;
2174 if (seat->focused_surface) {
2175 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2177 shell_surface_gain_keyboard_focus(shsurf);
2182 shell_client_pong(struct shell_client *sc, uint32_t serial)
2184 if (sc->ping_serial != serial)
2187 sc->unresponsive = 0;
2188 end_busy_cursor(sc->shell->compositor, sc->client);
2190 if (sc->ping_timer) {
2191 wl_event_source_remove(sc->ping_timer);
2192 sc->ping_timer = NULL;
2198 shell_surface_pong(struct wl_client *client,
2199 struct wl_resource *resource, uint32_t serial)
2201 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2202 struct shell_client *sc = shsurf->owner;
2204 shell_client_pong(sc, serial);
2208 set_title(struct shell_surface *shsurf, const char *title)
2210 free(shsurf->title);
2211 shsurf->title = strdup(title);
2215 set_window_geometry(struct shell_surface *shsurf,
2216 int32_t x, int32_t y, int32_t width, int32_t height)
2218 shsurf->next_geometry.x = x;
2219 shsurf->next_geometry.y = y;
2220 shsurf->next_geometry.width = width;
2221 shsurf->next_geometry.height = height;
2222 shsurf->has_next_geometry = true;
2226 shell_surface_set_title(struct wl_client *client,
2227 struct wl_resource *resource, const char *title)
2229 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2231 set_title(shsurf, title);
2235 shell_surface_set_class(struct wl_client *client,
2236 struct wl_resource *resource, const char *class)
2238 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2240 free(shsurf->class);
2241 shsurf->class = strdup(class);
2245 restore_output_mode(struct weston_output *output)
2247 if (output->current_mode != output->original_mode ||
2248 (int32_t)output->current_scale != output->original_scale)
2249 weston_output_switch_mode(output,
2250 output->original_mode,
2251 output->original_scale,
2252 WESTON_MODE_SWITCH_RESTORE_NATIVE);
2256 restore_all_output_modes(struct weston_compositor *compositor)
2258 struct weston_output *output;
2260 wl_list_for_each(output, &compositor->output_list, link)
2261 restore_output_mode(output);
2264 /* The surface will be inserted into the list immediately after the link
2265 * returned by this function (i.e. will be stacked immediately above the
2266 * returned link). */
2267 static struct weston_layer_entry *
2268 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2270 struct workspace *ws;
2271 struct weston_view *parent;
2273 switch (shsurf->type) {
2274 case SHELL_SURFACE_XWAYLAND:
2275 return &shsurf->shell->fullscreen_layer.view_list;
2277 case SHELL_SURFACE_NONE:
2280 case SHELL_SURFACE_POPUP:
2281 case SHELL_SURFACE_TOPLEVEL:
2282 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
2283 return &shsurf->shell->fullscreen_layer.view_list;
2284 } else if (shsurf->parent) {
2285 /* Move the surface to its parent layer so
2286 * that surfaces which are transient for
2287 * fullscreen surfaces don't get hidden by the
2288 * fullscreen surfaces. */
2290 /* TODO: Handle a parent with multiple views */
2291 parent = get_default_view(shsurf->parent);
2293 return container_of(parent->layer_link.link.prev,
2294 struct weston_layer_entry, link);
2297 /* Move the surface to a normal workspace layer so that surfaces
2298 * which were previously fullscreen or transient are no longer
2299 * rendered on top. */
2300 ws = get_current_workspace(shsurf->shell);
2301 return &ws->layer.view_list;
2304 assert(0 && "Unknown shell surface type");
2308 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2310 struct shell_surface *child;
2311 struct weston_layer_entry *prev;
2313 /* Move the child layers to the same workspace as shsurf. They will be
2314 * stacked above shsurf. */
2315 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2316 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
2317 weston_view_damage_below(child->view);
2318 weston_view_geometry_dirty(child->view);
2319 prev = container_of(shsurf->view->layer_link.link.prev,
2320 struct weston_layer_entry, link);
2321 weston_layer_entry_remove(&child->view->layer_link);
2322 weston_layer_entry_insert(prev,
2323 &child->view->layer_link);
2324 weston_view_geometry_dirty(child->view);
2325 weston_surface_damage(child->surface);
2327 /* Recurse. We don’t expect this to recurse very far (if
2328 * at all) because that would imply we have transient
2329 * (or popup) children of transient surfaces, which
2330 * would be unusual. */
2331 shell_surface_update_child_surface_layers(child);
2336 /* Update the surface’s layer. Mark both the old and new views as having dirty
2337 * geometry to ensure the changes are redrawn.
2339 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2340 * as this surface. */
2342 shell_surface_update_layer(struct shell_surface *shsurf)
2344 struct weston_layer_entry *new_layer_link;
2346 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2348 if (new_layer_link == NULL)
2350 if (new_layer_link == &shsurf->view->layer_link)
2353 weston_view_geometry_dirty(shsurf->view);
2354 weston_layer_entry_remove(&shsurf->view->layer_link);
2355 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
2356 weston_view_geometry_dirty(shsurf->view);
2357 weston_surface_damage(shsurf->surface);
2359 shell_surface_update_child_surface_layers(shsurf);
2363 shell_surface_set_parent(struct shell_surface *shsurf,
2364 struct weston_surface *parent)
2366 shsurf->parent = parent;
2368 wl_list_remove(&shsurf->children_link);
2369 wl_list_init(&shsurf->children_link);
2371 /* Insert into the parent surface’s child list. */
2372 if (parent != NULL) {
2373 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2374 if (parent_shsurf != NULL)
2375 wl_list_insert(&parent_shsurf->children_list,
2376 &shsurf->children_link);
2381 shell_surface_set_output(struct shell_surface *shsurf,
2382 struct weston_output *output)
2384 struct weston_surface *es = shsurf->surface;
2386 /* get the default output, if the client set it as NULL
2387 check whether the ouput is available */
2389 shsurf->output = output;
2390 else if (es->output)
2391 shsurf->output = es->output;
2393 shsurf->output = get_default_output(es->compositor);
2397 surface_clear_next_states(struct shell_surface *shsurf)
2399 shsurf->next_state.maximized = false;
2400 shsurf->next_state.fullscreen = false;
2402 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2403 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2404 shsurf->state_changed = true;
2408 set_toplevel(struct shell_surface *shsurf)
2410 shell_surface_set_parent(shsurf, NULL);
2411 surface_clear_next_states(shsurf);
2412 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2414 /* The layer_link is updated in set_surface_type(),
2415 * called from configure. */
2419 shell_surface_set_toplevel(struct wl_client *client,
2420 struct wl_resource *resource)
2422 struct shell_surface *surface = wl_resource_get_user_data(resource);
2424 set_toplevel(surface);
2428 set_transient(struct shell_surface *shsurf,
2429 struct weston_surface *parent, int x, int y, uint32_t flags)
2431 assert(parent != NULL);
2433 shell_surface_set_parent(shsurf, parent);
2435 surface_clear_next_states(shsurf);
2437 shsurf->transient.x = x;
2438 shsurf->transient.y = y;
2439 shsurf->transient.flags = flags;
2441 shsurf->next_state.relative = true;
2442 shsurf->state_changed = true;
2443 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2445 /* The layer_link is updated in set_surface_type(),
2446 * called from configure. */
2450 shell_surface_set_transient(struct wl_client *client,
2451 struct wl_resource *resource,
2452 struct wl_resource *parent_resource,
2453 int x, int y, uint32_t flags)
2455 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2456 struct weston_surface *parent =
2457 wl_resource_get_user_data(parent_resource);
2459 set_transient(shsurf, parent, x, y, flags);
2463 set_fullscreen(struct shell_surface *shsurf,
2466 struct weston_output *output)
2468 shell_surface_set_output(shsurf, output);
2469 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2471 shsurf->fullscreen_output = shsurf->output;
2472 shsurf->fullscreen.type = method;
2473 shsurf->fullscreen.framerate = framerate;
2475 send_configure_for_surface(shsurf);
2479 weston_view_set_initial_position(struct weston_view *view,
2480 struct desktop_shell *shell);
2483 unset_fullscreen(struct shell_surface *shsurf)
2485 /* Unset the fullscreen output, driver configuration and transforms. */
2486 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2487 shell_surface_is_top_fullscreen(shsurf)) {
2488 restore_output_mode(shsurf->fullscreen_output);
2491 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2492 shsurf->fullscreen.framerate = 0;
2494 wl_list_remove(&shsurf->fullscreen.transform.link);
2495 wl_list_init(&shsurf->fullscreen.transform.link);
2497 if (shsurf->fullscreen.black_view)
2498 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2499 shsurf->fullscreen.black_view = NULL;
2501 if (shsurf->saved_position_valid)
2502 weston_view_set_position(shsurf->view,
2503 shsurf->saved_x, shsurf->saved_y);
2505 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2507 if (shsurf->saved_rotation_valid) {
2508 wl_list_insert(&shsurf->view->geometry.transformation_list,
2509 &shsurf->rotation.transform.link);
2510 shsurf->saved_rotation_valid = false;
2513 /* Layer is updated in set_surface_type(). */
2517 shell_surface_set_fullscreen(struct wl_client *client,
2518 struct wl_resource *resource,
2521 struct wl_resource *output_resource)
2523 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2524 struct weston_output *output;
2526 if (output_resource)
2527 output = wl_resource_get_user_data(output_resource);
2531 shell_surface_set_parent(shsurf, NULL);
2533 surface_clear_next_states(shsurf);
2534 shsurf->next_state.fullscreen = true;
2535 shsurf->state_changed = true;
2536 set_fullscreen(shsurf, method, framerate, output);
2540 set_popup(struct shell_surface *shsurf,
2541 struct weston_surface *parent,
2542 struct weston_seat *seat,
2547 assert(parent != NULL);
2549 shsurf->popup.shseat = get_shell_seat(seat);
2550 shsurf->popup.serial = serial;
2551 shsurf->popup.x = x;
2552 shsurf->popup.y = y;
2554 shsurf->type = SHELL_SURFACE_POPUP;
2558 shell_surface_set_popup(struct wl_client *client,
2559 struct wl_resource *resource,
2560 struct wl_resource *seat_resource,
2562 struct wl_resource *parent_resource,
2563 int32_t x, int32_t y, uint32_t flags)
2565 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2566 struct weston_surface *parent =
2567 wl_resource_get_user_data(parent_resource);
2569 shell_surface_set_parent(shsurf, parent);
2571 surface_clear_next_states(shsurf);
2574 wl_resource_get_user_data(seat_resource),
2579 unset_maximized(struct shell_surface *shsurf)
2581 /* undo all maximized things here */
2582 shsurf->output = get_default_output(shsurf->surface->compositor);
2584 if (shsurf->saved_position_valid)
2585 weston_view_set_position(shsurf->view,
2586 shsurf->saved_x, shsurf->saved_y);
2588 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2590 if (shsurf->saved_rotation_valid) {
2591 wl_list_insert(&shsurf->view->geometry.transformation_list,
2592 &shsurf->rotation.transform.link);
2593 shsurf->saved_rotation_valid = false;
2596 /* Layer is updated in set_surface_type(). */
2600 set_minimized(struct weston_surface *surface, uint32_t is_true)
2602 struct shell_surface *shsurf;
2603 struct workspace *current_ws;
2604 struct weston_seat *seat;
2605 struct weston_surface *focus;
2606 struct weston_view *view;
2608 view = get_default_view(surface);
2612 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2614 shsurf = get_shell_surface(surface);
2615 current_ws = get_current_workspace(shsurf->shell);
2617 weston_layer_entry_remove(&view->layer_link);
2618 /* hide or show, depending on the state */
2620 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2622 drop_focus_state(shsurf->shell, current_ws, view->surface);
2623 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2624 if (!seat->keyboard)
2626 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2627 if (focus == view->surface)
2628 weston_keyboard_set_focus(seat->keyboard, NULL);
2632 weston_layer_entry_insert(¤t_ws->layer.view_list, &view->layer_link);
2634 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2635 if (!seat->keyboard)
2637 activate(shsurf->shell, view->surface, seat, true);
2641 shell_surface_update_child_surface_layers(shsurf);
2643 weston_view_damage_below(view);
2647 shell_surface_set_maximized(struct wl_client *client,
2648 struct wl_resource *resource,
2649 struct wl_resource *output_resource)
2651 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2652 struct weston_output *output;
2654 surface_clear_next_states(shsurf);
2655 shsurf->next_state.maximized = true;
2656 shsurf->state_changed = true;
2658 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2659 shell_surface_set_parent(shsurf, NULL);
2661 if (output_resource)
2662 output = wl_resource_get_user_data(output_resource);
2666 shell_surface_set_output(shsurf, output);
2668 send_configure_for_surface(shsurf);
2671 /* This is only ever called from set_surface_type(), so there’s no need to
2672 * update layer_links here, since they’ll be updated when we return. */
2674 reset_surface_type(struct shell_surface *surface)
2676 if (surface->state.fullscreen)
2677 unset_fullscreen(surface);
2678 if (surface->state.maximized)
2679 unset_maximized(surface);
2685 set_full_output(struct shell_surface *shsurf)
2687 shsurf->saved_x = shsurf->view->geometry.x;
2688 shsurf->saved_y = shsurf->view->geometry.y;
2689 shsurf->saved_width = shsurf->surface->width;
2690 shsurf->saved_height = shsurf->surface->height;
2691 shsurf->saved_size_valid = true;
2692 shsurf->saved_position_valid = true;
2694 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2695 wl_list_remove(&shsurf->rotation.transform.link);
2696 wl_list_init(&shsurf->rotation.transform.link);
2697 weston_view_geometry_dirty(shsurf->view);
2698 shsurf->saved_rotation_valid = true;
2703 set_surface_type(struct shell_surface *shsurf)
2705 struct weston_surface *pes = shsurf->parent;
2706 struct weston_view *pev = get_default_view(pes);
2708 reset_surface_type(shsurf);
2710 shsurf->state = shsurf->next_state;
2711 shsurf->state_changed = false;
2713 switch (shsurf->type) {
2714 case SHELL_SURFACE_TOPLEVEL:
2715 if (shsurf->state.maximized || shsurf->state.fullscreen) {
2716 set_full_output(shsurf);
2717 } else if (shsurf->state.relative && pev) {
2718 weston_view_set_position(shsurf->view,
2719 pev->geometry.x + shsurf->transient.x,
2720 pev->geometry.y + shsurf->transient.y);
2724 case SHELL_SURFACE_XWAYLAND:
2725 weston_view_set_position(shsurf->view, shsurf->transient.x,
2726 shsurf->transient.y);
2729 case SHELL_SURFACE_POPUP:
2730 case SHELL_SURFACE_NONE:
2735 /* Update the surface’s layer. */
2736 shell_surface_update_layer(shsurf);
2739 static struct desktop_shell *
2740 shell_surface_get_shell(struct shell_surface *shsurf)
2742 return shsurf->shell;
2746 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
2748 static struct weston_view *
2749 create_black_surface(struct weston_compositor *ec,
2750 struct weston_surface *fs_surface,
2751 float x, float y, int w, int h)
2753 struct weston_surface *surface = NULL;
2754 struct weston_view *view;
2756 surface = weston_surface_create(ec);
2757 if (surface == NULL) {
2758 weston_log("no memory\n");
2761 view = weston_view_create(surface);
2762 if (surface == NULL) {
2763 weston_log("no memory\n");
2764 weston_surface_destroy(surface);
2768 surface->configure = black_surface_configure;
2769 surface->configure_private = fs_surface;
2770 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2771 pixman_region32_fini(&surface->opaque);
2772 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2773 pixman_region32_fini(&surface->input);
2774 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2776 weston_surface_set_size(surface, w, h);
2777 weston_view_set_position(view, x, y);
2783 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2785 struct weston_output *output = shsurf->fullscreen_output;
2787 assert(shsurf->state.fullscreen);
2789 if (!shsurf->fullscreen.black_view)
2790 shsurf->fullscreen.black_view =
2791 create_black_surface(shsurf->surface->compositor,
2793 output->x, output->y,
2797 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2798 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2799 weston_layer_entry_insert(&shsurf->view->layer_link,
2800 &shsurf->fullscreen.black_view->layer_link);
2801 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2802 weston_surface_damage(shsurf->surface);
2804 shsurf->state.lowered = false;
2807 /* Create black surface and append it to the associated fullscreen surface.
2808 * Handle size dismatch and positioning according to the method. */
2810 shell_configure_fullscreen(struct shell_surface *shsurf)
2812 struct weston_output *output = shsurf->fullscreen_output;
2813 struct weston_surface *surface = shsurf->surface;
2814 struct weston_matrix *matrix;
2815 float scale, output_aspect, surface_aspect, x, y;
2816 int32_t surf_x, surf_y, surf_width, surf_height;
2818 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2819 restore_output_mode(output);
2821 /* Reverse the effect of lower_fullscreen_layer() */
2822 weston_layer_entry_remove(&shsurf->view->layer_link);
2823 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2825 shell_ensure_fullscreen_black_view(shsurf);
2827 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
2828 &surf_width, &surf_height);
2830 switch (shsurf->fullscreen.type) {
2831 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
2832 if (surface->buffer_ref.buffer)
2833 center_on_output(shsurf->view, shsurf->fullscreen_output);
2835 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
2836 /* 1:1 mapping between surface and output dimensions */
2837 if (output->width == surf_width &&
2838 output->height == surf_height) {
2839 weston_view_set_position(shsurf->view,
2841 output->y - surf_y);
2845 matrix = &shsurf->fullscreen.transform.matrix;
2846 weston_matrix_init(matrix);
2848 output_aspect = (float) output->width /
2849 (float) output->height;
2850 /* XXX: Use surf_width and surf_height here? */
2851 surface_aspect = (float) surface->width /
2852 (float) surface->height;
2853 if (output_aspect < surface_aspect)
2854 scale = (float) output->width /
2857 scale = (float) output->height /
2858 (float) surf_height;
2860 weston_matrix_scale(matrix, scale, scale, 1);
2861 wl_list_remove(&shsurf->fullscreen.transform.link);
2862 wl_list_insert(&shsurf->view->geometry.transformation_list,
2863 &shsurf->fullscreen.transform.link);
2864 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2865 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
2866 weston_view_set_position(shsurf->view, x, y);
2869 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
2870 if (shell_surface_is_top_fullscreen(shsurf)) {
2871 struct weston_mode mode = {0,
2872 surf_width * surface->buffer_viewport.buffer.scale,
2873 surf_height * surface->buffer_viewport.buffer.scale,
2874 shsurf->fullscreen.framerate};
2876 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
2877 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
2878 weston_view_set_position(shsurf->view,
2880 output->y - surf_y);
2881 shsurf->fullscreen.black_view->surface->width = output->width;
2882 shsurf->fullscreen.black_view->surface->height = output->height;
2883 weston_view_set_position(shsurf->fullscreen.black_view,
2885 output->y - surf_y);
2888 restore_output_mode(output);
2889 center_on_output(shsurf->view, output);
2893 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
2894 center_on_output(shsurf->view, output);
2902 shell_map_fullscreen(struct shell_surface *shsurf)
2904 shell_configure_fullscreen(shsurf);
2908 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2910 /* XXX: using the same fields for transient type */
2911 surface_clear_next_states(shsurf);
2912 shsurf->transient.x = x;
2913 shsurf->transient.y = y;
2914 shsurf->transient.flags = flags;
2916 shell_surface_set_parent(shsurf, NULL);
2918 shsurf->type = SHELL_SURFACE_XWAYLAND;
2919 shsurf->state_changed = true;
2923 shell_interface_set_fullscreen(struct shell_surface *shsurf,
2926 struct weston_output *output)
2928 surface_clear_next_states(shsurf);
2929 set_fullscreen(shsurf, method, framerate, output);
2931 shsurf->next_state.fullscreen = true;
2932 shsurf->state_changed = true;
2936 shell_interface_move(struct shell_surface *shsurf, struct weston_seat *ws)
2938 return surface_move(shsurf, ws, 1);
2941 static const struct weston_pointer_grab_interface popup_grab_interface;
2944 destroy_shell_seat(struct wl_listener *listener, void *data)
2946 struct shell_seat *shseat =
2947 container_of(listener,
2948 struct shell_seat, seat_destroy_listener);
2949 struct shell_surface *shsurf, *prev = NULL;
2951 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
2952 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2953 shseat->popup_grab.client = NULL;
2955 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2956 shsurf->popup.shseat = NULL;
2958 wl_list_init(&prev->popup.grab_link);
2962 wl_list_init(&prev->popup.grab_link);
2965 wl_list_remove(&shseat->seat_destroy_listener.link);
2970 shell_seat_caps_changed(struct wl_listener *listener, void *data)
2972 struct shell_seat *seat;
2974 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2976 if (seat->seat->keyboard &&
2977 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2978 wl_signal_add(&seat->seat->keyboard->focus_signal,
2979 &seat->keyboard_focus_listener);
2980 } else if (!seat->seat->keyboard) {
2981 wl_list_init(&seat->keyboard_focus_listener.link);
2984 if (seat->seat->pointer &&
2985 wl_list_empty(&seat->pointer_focus_listener.link)) {
2986 wl_signal_add(&seat->seat->pointer->focus_signal,
2987 &seat->pointer_focus_listener);
2988 } else if (!seat->seat->pointer) {
2989 wl_list_init(&seat->pointer_focus_listener.link);
2993 static struct shell_seat *
2994 create_shell_seat(struct weston_seat *seat)
2996 struct shell_seat *shseat;
2998 shseat = calloc(1, sizeof *shseat);
3000 weston_log("no memory to allocate shell seat\n");
3004 shseat->seat = seat;
3005 wl_list_init(&shseat->popup_grab.surfaces_list);
3007 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3008 wl_signal_add(&seat->destroy_signal,
3009 &shseat->seat_destroy_listener);
3011 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3012 wl_list_init(&shseat->keyboard_focus_listener.link);
3014 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3015 wl_list_init(&shseat->pointer_focus_listener.link);
3017 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3018 wl_signal_add(&seat->updated_caps_signal,
3019 &shseat->caps_changed_listener);
3020 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3025 static struct shell_seat *
3026 get_shell_seat(struct weston_seat *seat)
3028 struct wl_listener *listener;
3030 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
3031 assert(listener != NULL);
3033 return container_of(listener,
3034 struct shell_seat, seat_destroy_listener);
3038 popup_grab_focus(struct weston_pointer_grab *grab)
3040 struct weston_pointer *pointer = grab->pointer;
3041 struct weston_view *view;
3042 struct shell_seat *shseat =
3043 container_of(grab, struct shell_seat, popup_grab.grab);
3044 struct wl_client *client = shseat->popup_grab.client;
3047 view = weston_compositor_pick_view(pointer->seat->compositor,
3048 pointer->x, pointer->y,
3051 if (view && view->surface->resource &&
3052 wl_resource_get_client(view->surface->resource) == client) {
3053 weston_pointer_set_focus(pointer, view, sx, sy);
3055 weston_pointer_set_focus(pointer, NULL,
3056 wl_fixed_from_int(0),
3057 wl_fixed_from_int(0));
3062 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3063 wl_fixed_t x, wl_fixed_t y)
3065 struct weston_pointer *pointer = grab->pointer;
3066 struct wl_resource *resource;
3069 if (pointer->focus) {
3070 weston_view_from_global_fixed(pointer->focus, x, y,
3071 &pointer->sx, &pointer->sy);
3074 weston_pointer_move(pointer, x, y);
3076 wl_resource_for_each(resource, &pointer->focus_resource_list) {
3077 weston_view_from_global_fixed(pointer->focus,
3078 pointer->x, pointer->y,
3080 wl_pointer_send_motion(resource, time, sx, sy);
3085 popup_grab_button(struct weston_pointer_grab *grab,
3086 uint32_t time, uint32_t button, uint32_t state_w)
3088 struct wl_resource *resource;
3089 struct shell_seat *shseat =
3090 container_of(grab, struct shell_seat, popup_grab.grab);
3091 struct wl_display *display = shseat->seat->compositor->wl_display;
3092 enum wl_pointer_button_state state = state_w;
3094 struct wl_list *resource_list;
3096 resource_list = &grab->pointer->focus_resource_list;
3097 if (!wl_list_empty(resource_list)) {
3098 serial = wl_display_get_serial(display);
3099 wl_resource_for_each(resource, resource_list) {
3100 wl_pointer_send_button(resource, serial,
3101 time, button, state);
3103 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3104 (shseat->popup_grab.initial_up ||
3105 time - shseat->seat->pointer->grab_time > 500)) {
3106 popup_grab_end(grab->pointer);
3109 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
3110 shseat->popup_grab.initial_up = 1;
3114 popup_grab_cancel(struct weston_pointer_grab *grab)
3116 popup_grab_end(grab->pointer);
3119 static const struct weston_pointer_grab_interface popup_grab_interface = {
3127 touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3128 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3130 struct wl_resource *resource;
3131 struct shell_seat *shseat =
3132 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3133 struct wl_display *display = shseat->seat->compositor->wl_display;
3135 struct wl_list *resource_list;
3137 resource_list = &grab->touch->focus_resource_list;
3138 if (!wl_list_empty(resource_list)) {
3139 serial = wl_display_get_serial(display);
3140 wl_resource_for_each(resource, resource_list) {
3141 wl_touch_send_down(resource, serial, time,
3142 grab->touch->focus->surface->resource,
3149 touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3151 struct wl_resource *resource;
3152 struct shell_seat *shseat =
3153 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3154 struct wl_display *display = shseat->seat->compositor->wl_display;
3156 struct wl_list *resource_list;
3158 resource_list = &grab->touch->focus_resource_list;
3159 if (!wl_list_empty(resource_list)) {
3160 serial = wl_display_get_serial(display);
3161 wl_resource_for_each(resource, resource_list) {
3162 wl_touch_send_up(resource, serial, time, touch_id);
3168 touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3169 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
3171 struct wl_resource *resource;
3172 struct wl_list *resource_list;
3174 resource_list = &grab->touch->focus_resource_list;
3175 if (!wl_list_empty(resource_list)) {
3176 wl_resource_for_each(resource, resource_list) {
3177 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3183 touch_popup_grab_frame(struct weston_touch_grab *grab)
3188 touch_popup_grab_cancel(struct weston_touch_grab *grab)
3190 touch_popup_grab_end(grab->touch);
3193 static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3194 touch_popup_grab_down,
3195 touch_popup_grab_up,
3196 touch_popup_grab_motion,
3197 touch_popup_grab_frame,
3198 touch_popup_grab_cancel,
3202 shell_surface_send_popup_done(struct shell_surface *shsurf)
3204 if (shell_surface_is_wl_shell_surface(shsurf))
3205 wl_shell_surface_send_popup_done(shsurf->resource);
3206 else if (shell_surface_is_xdg_popup(shsurf))
3207 xdg_popup_send_popup_done(shsurf->resource,
3208 shsurf->popup.serial);
3212 popup_grab_end(struct weston_pointer *pointer)
3214 struct weston_pointer_grab *grab = pointer->grab;
3215 struct shell_seat *shseat =
3216 container_of(grab, struct shell_seat, popup_grab.grab);
3217 struct shell_surface *shsurf;
3218 struct shell_surface *prev = NULL;
3220 if (pointer->grab->interface == &popup_grab_interface) {
3221 weston_pointer_end_grab(grab->pointer);
3222 shseat->popup_grab.client = NULL;
3223 shseat->popup_grab.grab.interface = NULL;
3224 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3225 /* Send the popup_done event to all the popups open */
3226 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3227 shell_surface_send_popup_done(shsurf);
3228 shsurf->popup.shseat = NULL;
3230 wl_list_init(&prev->popup.grab_link);
3234 wl_list_init(&prev->popup.grab_link);
3235 wl_list_init(&shseat->popup_grab.surfaces_list);
3240 touch_popup_grab_end(struct weston_touch *touch)
3242 struct weston_touch_grab *grab = touch->grab;
3243 struct shell_seat *shseat =
3244 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3245 struct shell_surface *shsurf;
3246 struct shell_surface *prev = NULL;
3248 if (touch->grab->interface == &touch_popup_grab_interface) {
3249 weston_touch_end_grab(grab->touch);
3250 shseat->popup_grab.client = NULL;
3251 shseat->popup_grab.touch_grab.interface = NULL;
3252 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3253 /* Send the popup_done event to all the popups open */
3254 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
3255 shell_surface_send_popup_done(shsurf);
3256 shsurf->popup.shseat = NULL;
3258 wl_list_init(&prev->popup.grab_link);
3262 wl_list_init(&prev->popup.grab_link);
3263 wl_list_init(&shseat->popup_grab.surfaces_list);
3268 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat, int32_t type)
3270 struct weston_seat *seat = shseat->seat;
3272 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3273 shseat->popup_grab.type = type;
3274 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
3276 if (type == POINTER) {
3277 shseat->popup_grab.grab.interface = &popup_grab_interface;
3278 /* We must make sure here that this popup was opened after
3279 * a mouse press, and not just by moving around with other
3280 * popups already open. */
3281 if (shseat->seat->pointer->button_count > 0)
3282 shseat->popup_grab.initial_up = 0;
3283 } else if (type == TOUCH) {
3284 shseat->popup_grab.touch_grab.interface = &touch_popup_grab_interface;
3287 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
3289 if (type == POINTER)
3290 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
3291 else if (type == TOUCH)
3292 weston_touch_start_grab(seat->touch, &shseat->popup_grab.touch_grab);
3294 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
3299 remove_popup_grab(struct shell_surface *shsurf)
3301 struct shell_seat *shseat = shsurf->popup.shseat;
3303 wl_list_remove(&shsurf->popup.grab_link);
3304 wl_list_init(&shsurf->popup.grab_link);
3305 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3306 if (shseat->popup_grab.type == POINTER) {
3307 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3308 shseat->popup_grab.grab.interface = NULL;
3309 } else if (shseat->popup_grab.type == TOUCH) {
3310 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3311 shseat->popup_grab.touch_grab.interface = NULL;
3317 shell_map_popup(struct shell_surface *shsurf)
3319 struct shell_seat *shseat = shsurf->popup.shseat;
3320 struct weston_view *parent_view = get_default_view(shsurf->parent);
3322 shsurf->surface->output = parent_view->output;
3323 shsurf->view->output = parent_view->output;
3325 weston_view_set_transform_parent(shsurf->view, parent_view);
3326 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3327 weston_view_update_transform(shsurf->view);
3329 if (shseat->seat->pointer &&
3330 shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
3331 add_popup_grab(shsurf, shseat, POINTER);
3332 } else if (shseat->seat->touch &&
3333 shseat->seat->touch->grab_serial == shsurf->popup.serial) {
3334 add_popup_grab(shsurf, shseat, TOUCH);
3336 shell_surface_send_popup_done(shsurf);
3337 shseat->popup_grab.client = NULL;
3341 static const struct wl_shell_surface_interface shell_surface_implementation = {
3344 shell_surface_resize,
3345 shell_surface_set_toplevel,
3346 shell_surface_set_transient,
3347 shell_surface_set_fullscreen,
3348 shell_surface_set_popup,
3349 shell_surface_set_maximized,
3350 shell_surface_set_title,
3351 shell_surface_set_class
3355 destroy_shell_surface(struct shell_surface *shsurf)
3357 struct shell_surface *child, *next;
3359 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3361 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3362 remove_popup_grab(shsurf);
3365 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
3366 shell_surface_is_top_fullscreen(shsurf))
3367 restore_output_mode (shsurf->fullscreen_output);
3369 if (shsurf->fullscreen.black_view)
3370 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
3372 /* As destroy_resource() use wl_list_for_each_safe(),
3373 * we can always remove the listener.
3375 wl_list_remove(&shsurf->surface_destroy_listener.link);
3376 shsurf->surface->configure = NULL;
3377 free(shsurf->title);
3379 weston_view_destroy(shsurf->view);
3381 wl_list_remove(&shsurf->children_link);
3382 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3383 shell_surface_set_parent(child, NULL);
3385 wl_list_remove(&shsurf->link);
3390 shell_destroy_shell_surface(struct wl_resource *resource)
3392 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3394 if (!wl_list_empty(&shsurf->popup.grab_link))
3395 remove_popup_grab(shsurf);
3396 shsurf->resource = NULL;
3400 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
3402 struct shell_surface *shsurf = container_of(listener,
3403 struct shell_surface,
3404 surface_destroy_listener);
3406 if (shsurf->resource)
3407 wl_resource_destroy(shsurf->resource);
3409 destroy_shell_surface(shsurf);
3413 fade_out_done(struct weston_view_animation *animation, void *data)
3415 struct shell_surface *shsurf = data;
3417 weston_surface_destroy(shsurf->surface);
3421 handle_resource_destroy(struct wl_listener *listener, void *data)
3423 struct shell_surface *shsurf =
3424 container_of(listener, struct shell_surface,
3425 resource_destroy_listener);
3427 if (!weston_surface_is_mapped(shsurf->surface))
3430 shsurf->surface->ref_count++;
3432 pixman_region32_fini(&shsurf->surface->pending.input);
3433 pixman_region32_init(&shsurf->surface->pending.input);
3434 pixman_region32_fini(&shsurf->surface->input);
3435 pixman_region32_init(&shsurf->surface->input);
3436 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3437 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3438 fade_out_done, shsurf);
3440 weston_surface_destroy(shsurf->surface);
3445 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
3447 struct shell_surface *
3448 get_shell_surface(struct weston_surface *surface)
3450 if (surface->configure == shell_surface_configure)
3451 return surface->configure_private;
3456 static struct shell_surface *
3457 create_common_surface(struct shell_client *owner, void *shell,
3458 struct weston_surface *surface,
3459 const struct weston_shell_client *client)
3461 struct shell_surface *shsurf;
3463 if (surface->configure) {
3464 weston_log("surface->configure already set\n");
3468 shsurf = calloc(1, sizeof *shsurf);
3470 weston_log("no memory to allocate shell surface\n");
3474 shsurf->view = weston_view_create(surface);
3475 if (!shsurf->view) {
3476 weston_log("no memory to allocate shell surface\n");
3481 surface->configure = shell_surface_configure;
3482 surface->configure_private = shsurf;
3484 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3485 wl_resource_add_destroy_listener(surface->resource,
3486 &shsurf->resource_destroy_listener);
3487 shsurf->owner = owner;
3489 shsurf->shell = (struct desktop_shell *) shell;
3490 shsurf->unresponsive = 0;
3491 shsurf->saved_position_valid = false;
3492 shsurf->saved_size_valid = false;
3493 shsurf->saved_rotation_valid = false;
3494 shsurf->surface = surface;
3495 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3496 shsurf->fullscreen.framerate = 0;
3497 shsurf->fullscreen.black_view = NULL;
3498 wl_list_init(&shsurf->fullscreen.transform.link);
3500 shsurf->output = get_default_output(shsurf->shell->compositor);
3502 wl_signal_init(&shsurf->destroy_signal);
3503 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
3504 wl_signal_add(&surface->destroy_signal,
3505 &shsurf->surface_destroy_listener);
3507 /* init link so its safe to always remove it in destroy_shell_surface */
3508 wl_list_init(&shsurf->link);
3509 wl_list_init(&shsurf->popup.grab_link);
3511 /* empty when not in use */
3512 wl_list_init(&shsurf->rotation.transform.link);
3513 weston_matrix_init(&shsurf->rotation.rotation);
3515 wl_list_init(&shsurf->workspace_transform.link);
3517 wl_list_init(&shsurf->children_link);
3518 wl_list_init(&shsurf->children_list);
3519 shsurf->parent = NULL;
3521 shsurf->type = SHELL_SURFACE_NONE;
3523 shsurf->client = client;
3528 static struct shell_surface *
3529 create_shell_surface(void *shell, struct weston_surface *surface,
3530 const struct weston_shell_client *client)
3532 return create_common_surface(NULL, shell, surface, client);
3535 static struct weston_view *
3536 get_primary_view(void *shell, struct shell_surface *shsurf)
3538 return shsurf->view;
3542 shell_get_shell_surface(struct wl_client *client,
3543 struct wl_resource *resource,
3545 struct wl_resource *surface_resource)
3547 struct weston_surface *surface =
3548 wl_resource_get_user_data(surface_resource);
3549 struct shell_client *sc = wl_resource_get_user_data(resource);
3550 struct desktop_shell *shell = sc->shell;
3551 struct shell_surface *shsurf;
3553 if (get_shell_surface(surface)) {
3554 wl_resource_post_error(surface_resource,
3555 WL_DISPLAY_ERROR_INVALID_OBJECT,
3556 "desktop_shell::get_shell_surface already requested");
3560 shsurf = create_common_surface(sc, shell, surface, &shell_client);
3562 wl_resource_post_error(surface_resource,
3563 WL_DISPLAY_ERROR_INVALID_OBJECT,
3564 "surface->configure already set");
3569 wl_resource_create(client,
3570 &wl_shell_surface_interface, 1, id);
3571 wl_resource_set_implementation(shsurf->resource,
3572 &shell_surface_implementation,
3573 shsurf, shell_destroy_shell_surface);
3577 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3579 /* A shell surface without a resource is created from xwayland
3580 * and is considered a wl_shell surface for now. */
3582 return shsurf->resource == NULL ||
3583 wl_resource_instance_of(shsurf->resource,
3584 &wl_shell_surface_interface,
3585 &shell_surface_implementation);
3588 static const struct wl_shell_interface shell_implementation = {
3589 shell_get_shell_surface
3592 /****************************
3593 * xdg-shell implementation */
3596 xdg_surface_destroy(struct wl_client *client,
3597 struct wl_resource *resource)
3599 wl_resource_destroy(resource);
3603 xdg_surface_set_parent(struct wl_client *client,
3604 struct wl_resource *resource,
3605 struct wl_resource *parent_resource)
3607 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3608 struct weston_surface *parent;
3610 if (parent_resource)
3611 parent = wl_resource_get_user_data(parent_resource);
3615 shell_surface_set_parent(shsurf, parent);
3619 xdg_surface_set_app_id(struct wl_client *client,
3620 struct wl_resource *resource,
3623 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3625 free(shsurf->class);
3626 shsurf->class = strdup(app_id);
3630 xdg_surface_show_window_menu(struct wl_client *client,
3631 struct wl_resource *surface_resource,
3632 struct wl_resource *seat_resource,
3641 xdg_surface_set_title(struct wl_client *client,
3642 struct wl_resource *resource, const char *title)
3644 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3646 set_title(shsurf, title);
3650 xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3651 struct wl_resource *seat_resource, uint32_t serial)
3653 common_surface_move(resource, seat_resource, serial);
3657 xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3658 struct wl_resource *seat_resource, uint32_t serial,
3661 common_surface_resize(resource, seat_resource, serial, edges);
3665 xdg_surface_ack_configure(struct wl_client *client,
3666 struct wl_resource *resource,
3669 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3671 if (shsurf->state_requested) {
3672 shsurf->next_state = shsurf->requested_state;
3673 shsurf->state_changed = true;
3674 shsurf->state_requested = false;
3679 xdg_surface_set_window_geometry(struct wl_client *client,
3680 struct wl_resource *resource,
3686 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3688 set_window_geometry(shsurf, x, y, width, height);
3692 xdg_surface_set_maximized(struct wl_client *client,
3693 struct wl_resource *resource)
3695 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3697 shsurf->state_requested = true;
3698 shsurf->requested_state.maximized = true;
3699 send_configure_for_surface(shsurf);
3703 xdg_surface_unset_maximized(struct wl_client *client,
3704 struct wl_resource *resource)
3706 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3708 shsurf->state_requested = true;
3709 shsurf->requested_state.maximized = false;
3710 send_configure_for_surface(shsurf);
3713 static struct weston_output *
3714 get_focused_output(struct weston_compositor *compositor)
3716 struct weston_seat *seat;
3717 struct weston_output *output = NULL;
3719 wl_list_for_each(seat, &compositor->seat_list, link) {
3720 /* Priority has touch focus, then pointer and
3721 * then keyboard focus. We should probably have
3722 * three for loops and check frist for touch,
3723 * then for pointer, etc. but unless somebody has some
3724 * objections, I think this is sufficient. */
3725 if (seat->touch && seat->touch->focus)
3726 output = seat->touch->focus->output;
3727 else if (seat->pointer && seat->pointer->focus)
3728 output = seat->pointer->focus->output;
3729 else if (seat->keyboard && seat->keyboard->focus)
3730 output = seat->keyboard->focus->output;
3740 xdg_surface_set_fullscreen(struct wl_client *client,
3741 struct wl_resource *resource,
3742 struct wl_resource *output_resource)
3744 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3745 struct weston_output *output;
3747 shsurf->state_requested = true;
3748 shsurf->requested_state.fullscreen = true;
3750 if (output_resource)
3751 output = wl_resource_get_user_data(output_resource);
3755 /* handle clients launching in fullscreen */
3756 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3757 /* Set the output to the one that has focus currently. */
3758 assert(shsurf->surface);
3759 output = get_focused_output(shsurf->surface->compositor);
3762 shell_surface_set_output(shsurf, output);
3763 shsurf->fullscreen_output = shsurf->output;
3765 send_configure_for_surface(shsurf);
3769 xdg_surface_unset_fullscreen(struct wl_client *client,
3770 struct wl_resource *resource)
3772 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3774 shsurf->state_requested = true;
3775 shsurf->requested_state.fullscreen = false;
3776 send_configure_for_surface(shsurf);
3780 xdg_surface_set_minimized(struct wl_client *client,
3781 struct wl_resource *resource)
3783 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3785 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3788 /* apply compositor's own minimization logic (hide) */
3789 set_minimized(shsurf->surface, 1);
3792 static const struct xdg_surface_interface xdg_surface_implementation = {
3793 xdg_surface_destroy,
3794 xdg_surface_set_parent,
3795 xdg_surface_set_title,
3796 xdg_surface_set_app_id,
3797 xdg_surface_show_window_menu,
3800 xdg_surface_ack_configure,
3801 xdg_surface_set_window_geometry,
3802 xdg_surface_set_maximized,
3803 xdg_surface_unset_maximized,
3804 xdg_surface_set_fullscreen,
3805 xdg_surface_unset_fullscreen,
3806 xdg_surface_set_minimized,
3810 xdg_send_configure(struct weston_surface *surface,
3811 int32_t width, int32_t height)
3813 struct shell_surface *shsurf = get_shell_surface(surface);
3815 struct wl_array states;
3820 if (!shsurf->resource)
3823 wl_array_init(&states);
3824 if (shsurf->requested_state.fullscreen) {
3825 s = wl_array_add(&states, sizeof *s);
3826 *s = XDG_SURFACE_STATE_FULLSCREEN;
3827 } else if (shsurf->requested_state.maximized) {
3828 s = wl_array_add(&states, sizeof *s);
3829 *s = XDG_SURFACE_STATE_MAXIMIZED;
3831 if (shsurf->resize_edges != 0) {
3832 s = wl_array_add(&states, sizeof *s);
3833 *s = XDG_SURFACE_STATE_RESIZING;
3835 if (shsurf->focus_count > 0) {
3836 s = wl_array_add(&states, sizeof *s);
3837 *s = XDG_SURFACE_STATE_ACTIVATED;
3840 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
3841 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
3843 wl_array_release(&states);
3846 static const struct weston_shell_client xdg_client = {
3851 xdg_use_unstable_version(struct wl_client *client,
3852 struct wl_resource *resource,
3856 wl_resource_post_error(resource,
3858 "xdg-shell:: version not implemented yet.");
3863 static struct shell_surface *
3864 create_xdg_surface(struct shell_client *owner, void *shell,
3865 struct weston_surface *surface,
3866 const struct weston_shell_client *client)
3868 struct shell_surface *shsurf;
3870 shsurf = create_common_surface(owner, shell, surface, client);
3871 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3877 xdg_get_xdg_surface(struct wl_client *client,
3878 struct wl_resource *resource,
3880 struct wl_resource *surface_resource)
3882 struct weston_surface *surface =
3883 wl_resource_get_user_data(surface_resource);
3884 struct shell_client *sc = wl_resource_get_user_data(resource);
3885 struct desktop_shell *shell = sc->shell;
3886 struct shell_surface *shsurf;
3888 if (get_shell_surface(surface)) {
3889 wl_resource_post_error(surface_resource,
3890 WL_DISPLAY_ERROR_INVALID_OBJECT,
3891 "xdg_shell::get_xdg_surface already requested");
3895 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
3897 wl_resource_post_error(surface_resource,
3898 WL_DISPLAY_ERROR_INVALID_OBJECT,
3899 "surface->configure already set");
3904 wl_resource_create(client,
3905 &xdg_surface_interface, 1, id);
3906 wl_resource_set_implementation(shsurf->resource,
3907 &xdg_surface_implementation,
3908 shsurf, shell_destroy_shell_surface);
3912 shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3914 return shsurf->resource &&
3915 wl_resource_instance_of(shsurf->resource,
3916 &xdg_surface_interface,
3917 &xdg_surface_implementation);
3920 /* xdg-popup implementation */
3923 xdg_popup_destroy(struct wl_client *client,
3924 struct wl_resource *resource)
3926 wl_resource_destroy(resource);
3929 static const struct xdg_popup_interface xdg_popup_implementation = {
3934 xdg_popup_send_configure(struct weston_surface *surface,
3935 int32_t width, int32_t height)
3939 static const struct weston_shell_client xdg_popup_client = {
3940 xdg_popup_send_configure
3943 static struct shell_surface *
3944 create_xdg_popup(struct shell_client *owner, void *shell,
3945 struct weston_surface *surface,
3946 const struct weston_shell_client *client,
3947 struct weston_surface *parent,
3948 struct shell_seat *seat,
3950 int32_t x, int32_t y)
3952 struct shell_surface *shsurf;
3954 shsurf = create_common_surface(owner, shell, surface, client);
3955 shsurf->type = SHELL_SURFACE_POPUP;
3956 shsurf->popup.shseat = seat;
3957 shsurf->popup.serial = serial;
3958 shsurf->popup.x = x;
3959 shsurf->popup.y = y;
3960 shell_surface_set_parent(shsurf, parent);
3966 xdg_get_xdg_popup(struct wl_client *client,
3967 struct wl_resource *resource,
3969 struct wl_resource *surface_resource,
3970 struct wl_resource *parent_resource,
3971 struct wl_resource *seat_resource,
3973 int32_t x, int32_t y, uint32_t flags)
3975 struct weston_surface *surface =
3976 wl_resource_get_user_data(surface_resource);
3977 struct shell_client *sc = wl_resource_get_user_data(resource);
3978 struct desktop_shell *shell = sc->shell;
3979 struct shell_surface *shsurf;
3980 struct weston_surface *parent;
3981 struct shell_seat *seat;
3983 if (get_shell_surface(surface)) {
3984 wl_resource_post_error(surface_resource,
3985 WL_DISPLAY_ERROR_INVALID_OBJECT,
3986 "xdg_shell::get_xdg_popup already requested");
3990 if (!parent_resource) {
3991 wl_resource_post_error(surface_resource,
3992 WL_DISPLAY_ERROR_INVALID_OBJECT,
3993 "xdg_shell::get_xdg_popup requires a parent shell surface");
3997 parent = wl_resource_get_user_data(parent_resource);
3998 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
4000 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
4001 parent, seat, serial, x, y);
4003 wl_resource_post_error(surface_resource,
4004 WL_DISPLAY_ERROR_INVALID_OBJECT,
4005 "surface->configure already set");
4010 wl_resource_create(client,
4011 &xdg_popup_interface, 1, id);
4012 wl_resource_set_implementation(shsurf->resource,
4013 &xdg_popup_implementation,
4014 shsurf, shell_destroy_shell_surface);
4018 xdg_pong(struct wl_client *client,
4019 struct wl_resource *resource, uint32_t serial)
4021 struct shell_client *sc = wl_resource_get_user_data(resource);
4023 shell_client_pong(sc, serial);
4027 shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4029 return wl_resource_instance_of(shsurf->resource,
4030 &xdg_popup_interface,
4031 &xdg_popup_implementation);
4034 static const struct xdg_shell_interface xdg_implementation = {
4035 xdg_use_unstable_version,
4036 xdg_get_xdg_surface,
4042 xdg_shell_unversioned_dispatch(const void *implementation,
4043 void *_target, uint32_t opcode,
4044 const struct wl_message *message,
4045 union wl_argument *args)
4047 struct wl_resource *resource = _target;
4048 struct shell_client *sc = wl_resource_get_user_data(resource);
4051 wl_resource_post_error(resource,
4052 WL_DISPLAY_ERROR_INVALID_OBJECT,
4053 "must call use_unstable_version first");
4057 #define XDG_SERVER_VERSION 4
4059 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4060 "shell implementation doesn't match protocol version");
4062 if (args[0].i != XDG_SERVER_VERSION) {
4063 wl_resource_post_error(resource,
4064 WL_DISPLAY_ERROR_INVALID_OBJECT,
4065 "incompatible version, server is %d "
4067 XDG_SERVER_VERSION, args[0].i);
4071 wl_resource_set_implementation(resource, &xdg_implementation,
4077 /* end of xdg-shell implementation */
4078 /***********************************/
4081 shell_fade(struct desktop_shell *shell, enum fade_type type);
4084 screensaver_timeout(void *data)
4086 struct desktop_shell *shell = data;
4088 shell_fade(shell, FADE_OUT);
4094 handle_screensaver_sigchld(struct weston_process *proc, int status)
4096 struct desktop_shell *shell =
4097 container_of(proc, struct desktop_shell, screensaver.process);
4102 weston_compositor_sleep(shell->compositor);
4106 launch_screensaver(struct desktop_shell *shell)
4108 if (shell->screensaver.binding)
4111 if (!shell->screensaver.path) {
4112 weston_compositor_sleep(shell->compositor);
4116 if (shell->screensaver.process.pid != 0) {
4117 weston_log("old screensaver still running\n");
4121 weston_client_launch(shell->compositor,
4122 &shell->screensaver.process,
4123 shell->screensaver.path,
4124 handle_screensaver_sigchld);
4128 terminate_screensaver(struct desktop_shell *shell)
4130 if (shell->screensaver.process.pid == 0)
4133 /* Disarm the screensaver timer, otherwise it may fire when the
4134 * compositor is not in the idle state. In that case, the screen will
4135 * be locked, but the wake_signal won't fire on user input, making the
4136 * system unresponsive. */
4137 wl_event_source_timer_update(shell->screensaver.timer, 0);
4139 kill(shell->screensaver.process.pid, SIGTERM);
4143 configure_static_view(struct weston_view *ev, struct weston_layer *layer)
4145 struct weston_view *v, *next;
4147 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
4148 if (v->output == ev->output && v != ev) {
4149 weston_view_unmap(v);
4150 v->surface->configure = NULL;
4154 weston_view_set_position(ev, ev->output->x, ev->output->y);
4156 if (wl_list_empty(&ev->layer_link.link)) {
4157 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
4158 weston_compositor_schedule_repaint(ev->surface->compositor);
4163 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4165 struct desktop_shell *shell = es->configure_private;
4166 struct weston_view *view;
4168 view = container_of(es->views.next, struct weston_view, surface_link);
4170 configure_static_view(view, &shell->background_layer);
4174 desktop_shell_set_background(struct wl_client *client,
4175 struct wl_resource *resource,
4176 struct wl_resource *output_resource,
4177 struct wl_resource *surface_resource)
4179 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4180 struct weston_surface *surface =
4181 wl_resource_get_user_data(surface_resource);
4182 struct weston_view *view, *next;
4184 if (surface->configure) {
4185 wl_resource_post_error(surface_resource,
4186 WL_DISPLAY_ERROR_INVALID_OBJECT,
4187 "surface role already assigned");
4191 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4192 weston_view_destroy(view);
4193 view = weston_view_create(surface);
4195 surface->configure = background_configure;
4196 surface->configure_private = shell;
4197 surface->output = wl_resource_get_user_data(output_resource);
4198 view->output = surface->output;
4199 desktop_shell_send_configure(resource, 0,
4201 surface->output->width,
4202 surface->output->height);
4206 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4208 struct desktop_shell *shell = es->configure_private;
4209 struct weston_view *view;
4211 view = container_of(es->views.next, struct weston_view, surface_link);
4213 configure_static_view(view, &shell->panel_layer);
4217 desktop_shell_set_panel(struct wl_client *client,
4218 struct wl_resource *resource,
4219 struct wl_resource *output_resource,
4220 struct wl_resource *surface_resource)
4222 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4223 struct weston_surface *surface =
4224 wl_resource_get_user_data(surface_resource);
4225 struct weston_view *view, *next;
4227 if (surface->configure) {
4228 wl_resource_post_error(surface_resource,
4229 WL_DISPLAY_ERROR_INVALID_OBJECT,
4230 "surface role already assigned");
4234 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4235 weston_view_destroy(view);
4236 view = weston_view_create(surface);
4238 surface->configure = panel_configure;
4239 surface->configure_private = shell;
4240 surface->output = wl_resource_get_user_data(output_resource);
4241 view->output = surface->output;
4242 desktop_shell_send_configure(resource, 0,
4244 surface->output->width,
4245 surface->output->height);
4249 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
4251 struct desktop_shell *shell = surface->configure_private;
4252 struct weston_view *view;
4254 view = container_of(surface->views.next, struct weston_view, surface_link);
4256 if (surface->width == 0)
4259 center_on_output(view, get_default_output(shell->compositor));
4261 if (!weston_surface_is_mapped(surface)) {
4262 weston_layer_entry_insert(&shell->lock_layer.view_list,
4264 weston_view_update_transform(view);
4265 shell_fade(shell, FADE_IN);
4270 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
4272 struct desktop_shell *shell =
4273 container_of(listener, struct desktop_shell, lock_surface_listener);
4275 weston_log("lock surface gone\n");
4276 shell->lock_surface = NULL;
4280 desktop_shell_set_lock_surface(struct wl_client *client,
4281 struct wl_resource *resource,
4282 struct wl_resource *surface_resource)
4284 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4285 struct weston_surface *surface =
4286 wl_resource_get_user_data(surface_resource);
4288 shell->prepare_event_sent = false;
4293 shell->lock_surface = surface;
4295 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
4296 wl_signal_add(&surface->destroy_signal,
4297 &shell->lock_surface_listener);
4299 weston_view_create(surface);
4300 surface->configure = lock_surface_configure;
4301 surface->configure_private = shell;
4305 resume_desktop(struct desktop_shell *shell)
4307 struct workspace *ws = get_current_workspace(shell);
4309 terminate_screensaver(shell);
4311 wl_list_remove(&shell->lock_layer.link);
4312 if (shell->showing_input_panels) {
4313 wl_list_insert(&shell->compositor->cursor_layer.link,
4314 &shell->input_panel_layer.link);
4315 wl_list_insert(&shell->input_panel_layer.link,
4316 &shell->fullscreen_layer.link);
4318 wl_list_insert(&shell->compositor->cursor_layer.link,
4319 &shell->fullscreen_layer.link);
4321 wl_list_insert(&shell->fullscreen_layer.link,
4322 &shell->panel_layer.link);
4323 wl_list_insert(&shell->panel_layer.link,
4326 restore_focus_state(shell, get_current_workspace(shell));
4328 shell->locked = false;
4329 shell_fade(shell, FADE_IN);
4330 weston_compositor_damage_all(shell->compositor);
4334 desktop_shell_unlock(struct wl_client *client,
4335 struct wl_resource *resource)
4337 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4339 shell->prepare_event_sent = false;
4342 resume_desktop(shell);
4346 desktop_shell_set_grab_surface(struct wl_client *client,
4347 struct wl_resource *resource,
4348 struct wl_resource *surface_resource)
4350 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4352 shell->grab_surface = wl_resource_get_user_data(surface_resource);
4353 weston_view_create(shell->grab_surface);
4357 desktop_shell_desktop_ready(struct wl_client *client,
4358 struct wl_resource *resource)
4360 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4362 shell_fade_startup(shell);
4366 desktop_shell_set_panel_position(struct wl_client *client,
4367 struct wl_resource *resource,
4370 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4372 if (position != DESKTOP_SHELL_PANEL_POSITION_TOP &&
4373 position != DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4374 position != DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4375 position != DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4376 wl_resource_post_error(resource,
4377 DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4378 "bad position argument");
4382 shell->panel_position = position;
4385 static const struct desktop_shell_interface desktop_shell_implementation = {
4386 desktop_shell_set_background,
4387 desktop_shell_set_panel,
4388 desktop_shell_set_lock_surface,
4389 desktop_shell_unlock,
4390 desktop_shell_set_grab_surface,
4391 desktop_shell_desktop_ready,
4392 desktop_shell_set_panel_position
4395 static enum shell_surface_type
4396 get_shell_surface_type(struct weston_surface *surface)
4398 struct shell_surface *shsurf;
4400 shsurf = get_shell_surface(surface);
4402 return SHELL_SURFACE_NONE;
4403 return shsurf->type;
4407 move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4409 struct weston_surface *focus;
4410 struct weston_surface *surface;
4411 struct shell_surface *shsurf;
4413 if (seat->pointer->focus == NULL)
4416 focus = seat->pointer->focus->surface;
4418 surface = weston_surface_get_main_surface(focus);
4419 if (surface == NULL)
4422 shsurf = get_shell_surface(surface);
4423 if (shsurf == NULL || shsurf->state.fullscreen ||
4424 shsurf->state.maximized)
4427 surface_move(shsurf, (struct weston_seat *) seat, 0);
4431 maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4433 struct weston_surface *focus = seat->keyboard->focus;
4434 struct weston_surface *surface;
4435 struct shell_surface *shsurf;
4437 surface = weston_surface_get_main_surface(focus);
4438 if (surface == NULL)
4441 shsurf = get_shell_surface(surface);
4445 if (!shell_surface_is_xdg_surface(shsurf))
4448 shsurf->state_requested = true;
4449 shsurf->requested_state.maximized = !shsurf->state.maximized;
4450 send_configure_for_surface(shsurf);
4454 fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4456 struct weston_surface *focus = seat->keyboard->focus;
4457 struct weston_surface *surface;
4458 struct shell_surface *shsurf;
4460 surface = weston_surface_get_main_surface(focus);
4461 if (surface == NULL)
4464 shsurf = get_shell_surface(surface);
4468 if (!shell_surface_is_xdg_surface(shsurf))
4471 shsurf->state_requested = true;
4472 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4473 shsurf->fullscreen_output = shsurf->output;
4474 send_configure_for_surface(shsurf);
4478 touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4480 struct weston_surface *focus;
4481 struct weston_surface *surface;
4482 struct shell_surface *shsurf;
4484 if (seat->touch->focus == NULL)
4487 focus = seat->touch->focus->surface;
4488 surface = weston_surface_get_main_surface(focus);
4489 if (surface == NULL)
4492 shsurf = get_shell_surface(surface);
4493 if (shsurf == NULL || shsurf->state.fullscreen ||
4494 shsurf->state.maximized)
4497 surface_touch_move(shsurf, (struct weston_seat *) seat);
4501 resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4503 struct weston_surface *focus;
4504 struct weston_surface *surface;
4507 struct shell_surface *shsurf;
4509 if (seat->pointer->focus == NULL)
4512 focus = seat->pointer->focus->surface;
4514 surface = weston_surface_get_main_surface(focus);
4515 if (surface == NULL)
4518 shsurf = get_shell_surface(surface);
4519 if (shsurf == NULL || shsurf->state.fullscreen ||
4520 shsurf->state.maximized)
4523 weston_view_from_global(shsurf->view,
4524 wl_fixed_to_int(seat->pointer->grab_x),
4525 wl_fixed_to_int(seat->pointer->grab_y),
4528 if (x < shsurf->surface->width / 3)
4529 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
4530 else if (x < 2 * shsurf->surface->width / 3)
4533 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
4535 if (y < shsurf->surface->height / 3)
4536 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
4537 else if (y < 2 * shsurf->surface->height / 3)
4540 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
4542 surface_resize(shsurf, (struct weston_seat *) seat, edges);
4546 surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4547 wl_fixed_t value, void *data)
4550 struct shell_surface *shsurf;
4551 struct weston_surface *focus = seat->pointer->focus->surface;
4552 struct weston_surface *surface;
4554 /* XXX: broken for windows containing sub-surfaces */
4555 surface = weston_surface_get_main_surface(focus);
4556 if (surface == NULL)
4559 shsurf = get_shell_surface(surface);
4563 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
4565 if (shsurf->view->alpha > 1.0)
4566 shsurf->view->alpha = 1.0;
4567 if (shsurf->view->alpha < step)
4568 shsurf->view->alpha = step;
4570 weston_view_geometry_dirty(shsurf->view);
4571 weston_surface_damage(surface);
4575 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
4578 struct weston_seat *ws = (struct weston_seat *) seat;
4579 struct weston_compositor *compositor = ws->compositor;
4580 struct weston_output *output;
4583 wl_list_for_each(output, &compositor->output_list, link) {
4584 if (pixman_region32_contains_point(&output->region,
4585 wl_fixed_to_double(seat->pointer->x),
4586 wl_fixed_to_double(seat->pointer->y),
4588 if (key == KEY_PAGEUP)
4589 increment = output->zoom.increment;
4590 else if (key == KEY_PAGEDOWN)
4591 increment = -output->zoom.increment;
4592 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
4593 /* For every pixel zoom 20th of a step */
4594 increment = output->zoom.increment *
4595 -wl_fixed_to_double(value) / 20.0;
4599 output->zoom.level += increment;
4601 if (output->zoom.level < 0.0)
4602 output->zoom.level = 0.0;
4603 else if (output->zoom.level > output->zoom.max_level)
4604 output->zoom.level = output->zoom.max_level;
4605 else if (!output->zoom.active) {
4606 weston_output_activate_zoom(output);
4609 output->zoom.spring_z.target = output->zoom.level;
4611 weston_output_update_zoom(output);
4617 zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4618 wl_fixed_t value, void *data)
4620 do_zoom(seat, time, 0, axis, value);
4624 zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4627 do_zoom(seat, time, key, 0, 0);
4631 terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4634 struct weston_compositor *compositor = data;
4636 wl_display_terminate(compositor->wl_display);
4640 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4641 wl_fixed_t x, wl_fixed_t y)
4643 struct rotate_grab *rotate =
4644 container_of(grab, struct rotate_grab, base.grab);
4645 struct weston_pointer *pointer = grab->pointer;
4646 struct shell_surface *shsurf = rotate->base.shsurf;
4647 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
4649 weston_pointer_move(pointer, x, y);
4654 cx = 0.5f * shsurf->surface->width;
4655 cy = 0.5f * shsurf->surface->height;
4657 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4658 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
4659 r = sqrtf(dx * dx + dy * dy);
4661 wl_list_remove(&shsurf->rotation.transform.link);
4662 weston_view_geometry_dirty(shsurf->view);
4665 struct weston_matrix *matrix =
4666 &shsurf->rotation.transform.matrix;
4668 weston_matrix_init(&rotate->rotation);
4669 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4671 weston_matrix_init(matrix);
4672 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
4673 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
4674 weston_matrix_multiply(matrix, &rotate->rotation);
4675 weston_matrix_translate(matrix, cx, cy, 0.0f);
4678 &shsurf->view->geometry.transformation_list,
4679 &shsurf->rotation.transform.link);
4681 wl_list_init(&shsurf->rotation.transform.link);
4682 weston_matrix_init(&shsurf->rotation.rotation);
4683 weston_matrix_init(&rotate->rotation);
4686 /* We need to adjust the position of the surface
4687 * in case it was resized in a rotated state before */
4688 cposx = shsurf->view->geometry.x + cx;
4689 cposy = shsurf->view->geometry.y + cy;
4690 dposx = rotate->center.x - cposx;
4691 dposy = rotate->center.y - cposy;
4692 if (dposx != 0.0f || dposy != 0.0f) {
4693 weston_view_set_position(shsurf->view,
4694 shsurf->view->geometry.x + dposx,
4695 shsurf->view->geometry.y + dposy);
4698 /* Repaint implies weston_surface_update_transform(), which
4699 * lazily applies the damage due to rotation update.
4701 weston_compositor_schedule_repaint(shsurf->surface->compositor);
4705 rotate_grab_button(struct weston_pointer_grab *grab,
4706 uint32_t time, uint32_t button, uint32_t state_w)
4708 struct rotate_grab *rotate =
4709 container_of(grab, struct rotate_grab, base.grab);
4710 struct weston_pointer *pointer = grab->pointer;
4711 struct shell_surface *shsurf = rotate->base.shsurf;
4712 enum wl_pointer_button_state state = state_w;
4714 if (pointer->button_count == 0 &&
4715 state == WL_POINTER_BUTTON_STATE_RELEASED) {
4717 weston_matrix_multiply(&shsurf->rotation.rotation,
4719 shell_grab_end(&rotate->base);
4725 rotate_grab_cancel(struct weston_pointer_grab *grab)
4727 struct rotate_grab *rotate =
4728 container_of(grab, struct rotate_grab, base.grab);
4730 shell_grab_end(&rotate->base);
4734 static const struct weston_pointer_grab_interface rotate_grab_interface = {
4742 surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
4744 struct rotate_grab *rotate;
4748 rotate = malloc(sizeof *rotate);
4752 weston_view_to_global_float(surface->view,
4753 surface->surface->width * 0.5f,
4754 surface->surface->height * 0.5f,
4755 &rotate->center.x, &rotate->center.y);
4757 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4758 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
4759 r = sqrtf(dx * dx + dy * dy);
4761 struct weston_matrix inverse;
4763 weston_matrix_init(&inverse);
4764 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
4765 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
4767 weston_matrix_init(&rotate->rotation);
4768 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4770 weston_matrix_init(&surface->rotation.rotation);
4771 weston_matrix_init(&rotate->rotation);
4774 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4775 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
4779 rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4782 struct weston_surface *focus;
4783 struct weston_surface *base_surface;
4784 struct shell_surface *surface;
4786 if (seat->pointer->focus == NULL)
4789 focus = seat->pointer->focus->surface;
4791 base_surface = weston_surface_get_main_surface(focus);
4792 if (base_surface == NULL)
4795 surface = get_shell_surface(base_surface);
4796 if (surface == NULL || surface->state.fullscreen ||
4797 surface->state.maximized)
4800 surface_rotate(surface, seat);
4803 /* Move all fullscreen layers down to the current workspace and hide their
4804 * black views. The surfaces' state is set to both fullscreen and lowered,
4805 * and this is reversed when such a surface is re-configured, see
4806 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
4808 * This should be used when implementing shell-wide overlays, such as
4809 * the alt-tab switcher, which need to de-promote fullscreen layers. */
4811 lower_fullscreen_layer(struct desktop_shell *shell)
4813 struct workspace *ws;
4814 struct weston_view *view, *prev;
4816 ws = get_current_workspace(shell);
4817 wl_list_for_each_reverse_safe(view, prev,
4818 &shell->fullscreen_layer.view_list.link,
4820 struct shell_surface *shsurf = get_shell_surface(view->surface);
4825 /* We can have a non-fullscreen popup for a fullscreen surface
4826 * in the fullscreen layer. */
4827 if (shsurf->state.fullscreen) {
4828 /* Hide the black view */
4829 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
4830 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
4831 weston_view_damage_below(shsurf->fullscreen.black_view);
4835 /* Lower the view to the workspace layer */
4836 weston_layer_entry_remove(&view->layer_link);
4837 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
4838 weston_view_damage_below(view);
4839 weston_surface_damage(view->surface);
4841 shsurf->state.lowered = true;
4846 activate(struct desktop_shell *shell, struct weston_surface *es,
4847 struct weston_seat *seat, bool configure)
4849 struct weston_surface *main_surface;
4850 struct focus_state *state;
4851 struct workspace *ws;
4852 struct weston_surface *old_es;
4853 struct shell_surface *shsurf;
4855 lower_fullscreen_layer(shell);
4857 main_surface = weston_surface_get_main_surface(es);
4859 weston_surface_activate(es, seat);
4861 state = ensure_focus_state(shell, seat);
4865 old_es = state->keyboard_focus;
4866 focus_state_set_focus(state, es);
4868 shsurf = get_shell_surface(main_surface);
4871 if (shsurf->state.fullscreen && configure)
4872 shell_configure_fullscreen(shsurf);
4874 restore_all_output_modes(shell->compositor);
4876 /* Update the surface’s layer. This brings it to the top of the stacking
4877 * order as appropriate. */
4878 shell_surface_update_layer(shsurf);
4880 if (shell->focus_animation_type != ANIMATION_NONE) {
4881 ws = get_current_workspace(shell);
4882 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
4886 /* no-op func for checking black surface */
4888 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4893 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4895 if (es->configure == black_surface_configure) {
4897 *fs_surface = (struct weston_surface *)es->configure_private;
4904 activate_binding(struct weston_seat *seat,
4905 struct desktop_shell *shell,
4906 struct weston_surface *focus)
4908 struct weston_surface *main_surface;
4913 if (is_black_surface(focus, &main_surface))
4914 focus = main_surface;
4916 main_surface = weston_surface_get_main_surface(focus);
4917 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
4920 activate(shell, focus, seat, true);
4924 click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4927 if (seat->pointer->grab != &seat->pointer->default_grab)
4929 if (seat->pointer->focus == NULL)
4932 activate_binding(seat, data, seat->pointer->focus->surface);
4936 touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4938 if (seat->touch->grab != &seat->touch->default_grab)
4940 if (seat->touch->focus == NULL)
4943 activate_binding(seat, data, seat->touch->focus->surface);
4947 unfocus_all_seats(struct desktop_shell *shell)
4949 struct weston_seat *seat, *next;
4951 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
4952 if (seat->keyboard == NULL)
4955 weston_keyboard_set_focus(seat->keyboard, NULL);
4960 lock(struct desktop_shell *shell)
4962 struct workspace *ws = get_current_workspace(shell);
4964 if (shell->locked) {
4965 weston_compositor_sleep(shell->compositor);
4969 shell->locked = true;
4971 /* Hide all surfaces by removing the fullscreen, panel and
4972 * toplevel layers. This way nothing else can show or receive
4973 * input events while we are locked. */
4975 wl_list_remove(&shell->panel_layer.link);
4976 wl_list_remove(&shell->fullscreen_layer.link);
4977 if (shell->showing_input_panels)
4978 wl_list_remove(&shell->input_panel_layer.link);
4979 wl_list_remove(&ws->layer.link);
4980 wl_list_insert(&shell->compositor->cursor_layer.link,
4981 &shell->lock_layer.link);
4983 launch_screensaver(shell);
4985 /* Remove the keyboard focus on all seats. This will be
4986 * restored to the workspace's saved state via
4987 * restore_focus_state when the compositor is unlocked */
4988 unfocus_all_seats(shell);
4990 /* TODO: disable bindings that should not work while locked. */
4992 /* All this must be undone in resume_desktop(). */
4996 unlock(struct desktop_shell *shell)
4998 if (!shell->locked || shell->lock_surface) {
4999 shell_fade(shell, FADE_IN);
5003 /* If desktop-shell client has gone away, unlock immediately. */
5004 if (!shell->child.desktop_shell) {
5005 resume_desktop(shell);
5009 if (shell->prepare_event_sent)
5012 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
5013 shell->prepare_event_sent = true;
5017 shell_fade_done(struct weston_view_animation *animation, void *data)
5019 struct desktop_shell *shell = data;
5021 shell->fade.animation = NULL;
5023 switch (shell->fade.type) {
5025 weston_surface_destroy(shell->fade.view->surface);
5026 shell->fade.view = NULL;
5036 static struct weston_view *
5037 shell_fade_create_surface(struct desktop_shell *shell)
5039 struct weston_compositor *compositor = shell->compositor;
5040 struct weston_surface *surface;
5041 struct weston_view *view;
5043 surface = weston_surface_create(compositor);
5047 view = weston_view_create(surface);
5049 weston_surface_destroy(surface);
5053 weston_surface_set_size(surface, 8192, 8192);
5054 weston_view_set_position(view, 0, 0);
5055 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
5056 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5058 pixman_region32_init(&surface->input);
5064 shell_fade(struct desktop_shell *shell, enum fade_type type)
5076 weston_log("shell: invalid fade type\n");
5080 shell->fade.type = type;
5082 if (shell->fade.view == NULL) {
5083 shell->fade.view = shell_fade_create_surface(shell);
5084 if (!shell->fade.view)
5087 shell->fade.view->alpha = 1.0 - tint;
5088 weston_view_update_transform(shell->fade.view);
5091 if (shell->fade.view->output == NULL) {
5092 /* If the black view gets a NULL output, we lost the
5093 * last output and we'll just cancel the fade. This
5094 * happens when you close the last window under the
5095 * X11 or Wayland backends. */
5096 shell->locked = false;
5097 weston_surface_destroy(shell->fade.view->surface);
5098 shell->fade.view = NULL;
5099 } else if (shell->fade.animation) {
5100 weston_fade_update(shell->fade.animation, tint);
5102 shell->fade.animation =
5103 weston_fade_run(shell->fade.view,
5104 1.0 - tint, tint, 300.0,
5105 shell_fade_done, shell);
5110 do_shell_fade_startup(void *data)
5112 struct desktop_shell *shell = data;
5114 if (shell->startup_animation_type == ANIMATION_FADE)
5115 shell_fade(shell, FADE_IN);
5116 else if (shell->startup_animation_type == ANIMATION_NONE) {
5117 weston_surface_destroy(shell->fade.view->surface);
5118 shell->fade.view = NULL;
5123 shell_fade_startup(struct desktop_shell *shell)
5125 struct wl_event_loop *loop;
5127 if (!shell->fade.startup_timer)
5130 wl_event_source_remove(shell->fade.startup_timer);
5131 shell->fade.startup_timer = NULL;
5133 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5134 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5138 fade_startup_timeout(void *data)
5140 struct desktop_shell *shell = data;
5142 shell_fade_startup(shell);
5147 shell_fade_init(struct desktop_shell *shell)
5149 /* Make compositor output all black, and wait for the desktop-shell
5150 * client to signal it is ready, then fade in. The timer triggers a
5151 * fade-in, in case the desktop-shell client takes too long.
5154 struct wl_event_loop *loop;
5156 if (shell->fade.view != NULL) {
5157 weston_log("%s: warning: fade surface already exists\n",
5162 shell->fade.view = shell_fade_create_surface(shell);
5163 if (!shell->fade.view)
5166 weston_view_update_transform(shell->fade.view);
5167 weston_surface_damage(shell->fade.view->surface);
5169 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5170 shell->fade.startup_timer =
5171 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5172 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5176 idle_handler(struct wl_listener *listener, void *data)
5178 struct desktop_shell *shell =
5179 container_of(listener, struct desktop_shell, idle_listener);
5180 struct weston_seat *seat;
5182 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
5184 popup_grab_end(seat->pointer);
5186 touch_popup_grab_end(seat->touch);
5189 shell_fade(shell, FADE_OUT);
5190 /* lock() is called from shell_fade_done() */
5194 wake_handler(struct wl_listener *listener, void *data)
5196 struct desktop_shell *shell =
5197 container_of(listener, struct desktop_shell, wake_listener);
5203 center_on_output(struct weston_view *view, struct weston_output *output)
5205 int32_t surf_x, surf_y, width, height;
5208 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
5210 x = output->x + (output->width - width) / 2 - surf_x / 2;
5211 y = output->y + (output->height - height) / 2 - surf_y / 2;
5213 weston_view_set_position(view, x, y);
5217 weston_view_set_initial_position(struct weston_view *view,
5218 struct desktop_shell *shell)
5220 struct weston_compositor *compositor = shell->compositor;
5222 int32_t range_x, range_y;
5223 int32_t dx, dy, x, y;
5224 struct weston_output *output, *target_output = NULL;
5225 struct weston_seat *seat;
5226 pixman_rectangle32_t area;
5228 /* As a heuristic place the new window on the same output as the
5229 * pointer. Falling back to the output containing 0, 0.
5231 * TODO: Do something clever for touch too?
5233 wl_list_for_each(seat, &compositor->seat_list, link) {
5234 if (seat->pointer) {
5235 ix = wl_fixed_to_int(seat->pointer->x);
5236 iy = wl_fixed_to_int(seat->pointer->y);
5241 wl_list_for_each(output, &compositor->output_list, link) {
5242 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5243 target_output = output;
5248 if (!target_output) {
5249 weston_view_set_position(view, 10 + random() % 400,
5250 10 + random() % 400);
5254 /* Valid range within output where the surface will still be onscreen.
5255 * If this is negative it means that the surface is bigger than
5258 get_output_work_area(shell, target_output, &area);
5262 range_x = area.width - view->surface->width;
5263 range_y = area.height - view->surface->height;
5266 dx += random() % range_x;
5269 dy += random() % range_y;
5271 x = target_output->x + dx;
5272 y = target_output->y + dy;
5274 weston_view_set_position(view, x, y);
5278 set_maximized_position(struct desktop_shell *shell,
5279 struct shell_surface *shsurf)
5281 int32_t surf_x, surf_y;
5282 pixman_rectangle32_t area;
5284 /* use surface configure to set the geometry */
5285 get_output_work_area(shell, shsurf->output, &area);
5286 surface_subsurfaces_boundingbox(shsurf->surface,
5287 &surf_x, &surf_y, NULL, NULL);
5289 weston_view_set_position(shsurf->view,
5295 map(struct desktop_shell *shell, struct shell_surface *shsurf,
5296 int32_t sx, int32_t sy)
5298 struct weston_compositor *compositor = shell->compositor;
5299 struct weston_seat *seat;
5301 /* initial positioning, see also configure() */
5302 switch (shsurf->type) {
5303 case SHELL_SURFACE_TOPLEVEL:
5304 if (shsurf->state.fullscreen) {
5305 center_on_output(shsurf->view, shsurf->fullscreen_output);
5306 shell_map_fullscreen(shsurf);
5307 } else if (shsurf->state.maximized) {
5308 set_maximized_position(shell, shsurf);
5309 } else if (!shsurf->state.relative) {
5310 weston_view_set_initial_position(shsurf->view, shell);
5313 case SHELL_SURFACE_POPUP:
5314 shell_map_popup(shsurf);
5316 case SHELL_SURFACE_NONE:
5317 weston_view_set_position(shsurf->view,
5318 shsurf->view->geometry.x + sx,
5319 shsurf->view->geometry.y + sy);
5321 case SHELL_SURFACE_XWAYLAND:
5326 /* Surface stacking order, see also activate(). */
5327 shell_surface_update_layer(shsurf);
5329 if (shsurf->type != SHELL_SURFACE_NONE) {
5330 weston_view_update_transform(shsurf->view);
5331 if (shsurf->state.maximized) {
5332 shsurf->surface->output = shsurf->output;
5333 shsurf->view->output = shsurf->output;
5337 switch (shsurf->type) {
5338 /* XXX: xwayland's using the same fields for transient type */
5339 case SHELL_SURFACE_XWAYLAND:
5340 if (shsurf->transient.flags ==
5341 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5343 case SHELL_SURFACE_TOPLEVEL:
5344 if (shsurf->state.relative &&
5345 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5349 wl_list_for_each(seat, &compositor->seat_list, link)
5350 activate(shell, shsurf->surface, seat, true);
5352 case SHELL_SURFACE_POPUP:
5353 case SHELL_SURFACE_NONE:
5358 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5359 !shsurf->state.maximized && !shsurf->state.fullscreen)
5361 switch (shell->win_animation_type) {
5362 case ANIMATION_FADE:
5363 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
5365 case ANIMATION_ZOOM:
5366 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
5368 case ANIMATION_NONE:
5376 configure(struct desktop_shell *shell, struct weston_surface *surface,
5379 struct shell_surface *shsurf;
5380 struct weston_view *view;
5382 shsurf = get_shell_surface(surface);
5386 if (shsurf->state.fullscreen)
5387 shell_configure_fullscreen(shsurf);
5388 else if (shsurf->state.maximized) {
5389 set_maximized_position(shell, shsurf);
5391 weston_view_set_position(shsurf->view, x, y);
5394 /* XXX: would a fullscreen surface need the same handling? */
5395 if (surface->output) {
5396 wl_list_for_each(view, &surface->views, surface_link)
5397 weston_view_update_transform(view);
5399 if (shsurf->state.maximized)
5400 surface->output = shsurf->output;
5405 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
5407 struct shell_surface *shsurf = get_shell_surface(es);
5408 struct desktop_shell *shell;
5409 int type_changed = 0;
5413 shell = shsurf->shell;
5415 if (!weston_surface_is_mapped(es) &&
5416 !wl_list_empty(&shsurf->popup.grab_link)) {
5417 remove_popup_grab(shsurf);
5423 if (shsurf->has_next_geometry) {
5424 shsurf->geometry = shsurf->next_geometry;
5425 shsurf->has_next_geometry = false;
5426 shsurf->has_set_geometry = true;
5427 } else if (!shsurf->has_set_geometry) {
5428 surface_subsurfaces_boundingbox(shsurf->surface,
5429 &shsurf->geometry.x,
5430 &shsurf->geometry.y,
5431 &shsurf->geometry.width,
5432 &shsurf->geometry.height);
5435 if (shsurf->state_changed) {
5436 set_surface_type(shsurf);
5440 if (!weston_surface_is_mapped(es)) {
5441 map(shell, shsurf, sx, sy);
5442 } else if (type_changed || sx != 0 || sy != 0 ||
5443 shsurf->last_width != es->width ||
5444 shsurf->last_height != es->height) {
5445 float from_x, from_y;
5448 if (shsurf->resize_edges) {
5453 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5454 sx = shsurf->last_width - es->width;
5455 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5456 sy = shsurf->last_height - es->height;
5458 shsurf->last_width = es->width;
5459 shsurf->last_height = es->height;
5461 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5462 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
5463 configure(shell, es,
5464 shsurf->view->geometry.x + to_x - from_x,
5465 shsurf->view->geometry.y + to_y - from_y);
5470 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5472 struct timespec now;
5474 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5478 * If the shell helper client dies before the session has been
5479 * up for roughly 30 seconds, better just make Weston shut down,
5480 * because the user likely has no way to interact with the desktop
5483 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5484 weston_log("Error: %s apparently cannot run at all.\n",
5486 weston_log_continue(STAMP_SPACE "Quitting...");
5487 wl_display_terminate(shell->compositor->wl_display);
5495 static void launch_desktop_shell_process(void *data);
5498 respawn_desktop_shell_process(struct desktop_shell *shell)
5502 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5503 time = weston_compositor_get_time();
5504 if (time - shell->child.deathstamp > 30000) {
5505 shell->child.deathstamp = time;
5506 shell->child.deathcount = 0;
5509 shell->child.deathcount++;
5510 if (shell->child.deathcount > 5) {
5511 weston_log("%s disconnected, giving up.\n", shell->client);
5515 weston_log("%s disconnected, respawning...\n", shell->client);
5516 launch_desktop_shell_process(shell);
5520 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5522 struct desktop_shell *shell;
5524 shell = container_of(listener, struct desktop_shell,
5525 child.client_destroy_listener);
5527 wl_list_remove(&shell->child.client_destroy_listener.link);
5528 shell->child.client = NULL;
5530 * unbind_desktop_shell() will reset shell->child.desktop_shell
5531 * before the respawned process has a chance to create a new
5532 * desktop_shell object, because we are being called from the
5533 * wl_client destructor which destroys all wl_resources before
5537 if (!check_desktop_shell_crash_too_early(shell))
5538 respawn_desktop_shell_process(shell);
5540 shell_fade_startup(shell);
5544 launch_desktop_shell_process(void *data)
5546 struct desktop_shell *shell = data;
5548 shell->child.client = weston_client_start(shell->compositor,
5551 if (!shell->child.client) {
5552 weston_log("not able to start %s\n", shell->client);
5556 shell->child.client_destroy_listener.notify =
5557 desktop_shell_client_destroy;
5558 wl_client_add_destroy_listener(shell->child.client,
5559 &shell->child.client_destroy_listener);
5563 handle_shell_client_destroy(struct wl_listener *listener, void *data)
5565 struct shell_client *sc =
5566 container_of(listener, struct shell_client, destroy_listener);
5569 wl_event_source_remove(sc->ping_timer);
5573 static struct shell_client *
5574 shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5575 const struct wl_interface *interface, uint32_t id)
5577 struct shell_client *sc;
5579 sc = zalloc(sizeof *sc);
5581 wl_client_post_no_memory(client);
5585 sc->resource = wl_resource_create(client, interface, 1, id);
5586 if (sc->resource == NULL) {
5588 wl_client_post_no_memory(client);
5592 sc->client = client;
5594 sc->destroy_listener.notify = handle_shell_client_destroy;
5595 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5601 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5603 struct desktop_shell *shell = data;
5604 struct shell_client *sc;
5606 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5608 wl_resource_set_implementation(sc->resource,
5609 &shell_implementation,
5614 bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5616 struct desktop_shell *shell = data;
5617 struct shell_client *sc;
5619 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5621 wl_resource_set_dispatcher(sc->resource,
5622 xdg_shell_unversioned_dispatch,
5627 unbind_desktop_shell(struct wl_resource *resource)
5629 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5632 resume_desktop(shell);
5634 shell->child.desktop_shell = NULL;
5635 shell->prepare_event_sent = false;
5639 bind_desktop_shell(struct wl_client *client,
5640 void *data, uint32_t version, uint32_t id)
5642 struct desktop_shell *shell = data;
5643 struct wl_resource *resource;
5645 resource = wl_resource_create(client, &desktop_shell_interface,
5646 MIN(version, 3), id);
5648 if (client == shell->child.client) {
5649 wl_resource_set_implementation(resource,
5650 &desktop_shell_implementation,
5651 shell, unbind_desktop_shell);
5652 shell->child.desktop_shell = resource;
5655 shell_fade_startup(shell);
5660 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5661 "permission to bind desktop_shell denied");
5665 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
5667 struct desktop_shell *shell = surface->configure_private;
5668 struct weston_view *view;
5669 struct weston_layer_entry *prev;
5671 if (surface->width == 0)
5674 /* XXX: starting weston-screensaver beforehand does not work */
5678 view = container_of(surface->views.next, struct weston_view, surface_link);
5679 center_on_output(view, surface->output);
5681 if (wl_list_empty(&view->layer_link.link)) {
5682 prev = container_of(shell->lock_layer.view_list.link.prev,
5683 struct weston_layer_entry, link);
5684 weston_layer_entry_insert(prev, &view->layer_link);
5685 weston_view_update_transform(view);
5686 wl_event_source_timer_update(shell->screensaver.timer,
5687 shell->screensaver.duration);
5688 shell_fade(shell, FADE_IN);
5693 screensaver_set_surface(struct wl_client *client,
5694 struct wl_resource *resource,
5695 struct wl_resource *surface_resource,
5696 struct wl_resource *output_resource)
5698 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5699 struct weston_surface *surface =
5700 wl_resource_get_user_data(surface_resource);
5701 struct weston_output *output = wl_resource_get_user_data(output_resource);
5702 struct weston_view *view, *next;
5704 /* Make sure we only have one view */
5705 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5706 weston_view_destroy(view);
5707 weston_view_create(surface);
5709 surface->configure = screensaver_configure;
5710 surface->configure_private = shell;
5711 surface->output = output;
5714 static const struct screensaver_interface screensaver_implementation = {
5715 screensaver_set_surface
5719 unbind_screensaver(struct wl_resource *resource)
5721 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5723 shell->screensaver.binding = NULL;
5727 bind_screensaver(struct wl_client *client,
5728 void *data, uint32_t version, uint32_t id)
5730 struct desktop_shell *shell = data;
5731 struct wl_resource *resource;
5733 resource = wl_resource_create(client, &screensaver_interface, 1, id);
5735 if (shell->screensaver.binding == NULL) {
5736 wl_resource_set_implementation(resource,
5737 &screensaver_implementation,
5738 shell, unbind_screensaver);
5739 shell->screensaver.binding = resource;
5743 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5744 "interface object already bound");
5748 struct desktop_shell *shell;
5749 struct weston_surface *current;
5750 struct wl_listener listener;
5751 struct weston_keyboard_grab grab;
5752 struct wl_array minimized_array;
5756 switcher_next(struct switcher *switcher)
5758 struct weston_view *view;
5759 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
5760 struct shell_surface *shsurf;
5761 struct workspace *ws = get_current_workspace(switcher->shell);
5763 /* temporary re-display minimized surfaces */
5764 struct weston_view *tmp;
5765 struct weston_view **minimized;
5766 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
5767 weston_layer_entry_remove(&view->layer_link);
5768 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
5769 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5773 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
5774 shsurf = get_shell_surface(view->surface);
5776 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5777 shsurf->parent == NULL) {
5779 first = view->surface;
5780 if (prev == switcher->current)
5781 next = view->surface;
5782 prev = view->surface;
5784 weston_view_geometry_dirty(view);
5785 weston_surface_damage(view->surface);
5788 if (is_black_surface(view->surface, NULL)) {
5790 weston_view_geometry_dirty(view);
5791 weston_surface_damage(view->surface);
5801 wl_list_remove(&switcher->listener.link);
5802 wl_signal_add(&next->destroy_signal, &switcher->listener);
5804 switcher->current = next;
5805 wl_list_for_each(view, &next->views, surface_link)
5808 shsurf = get_shell_surface(switcher->current);
5809 if (shsurf && shsurf->state.fullscreen)
5810 shsurf->fullscreen.black_view->alpha = 1.0;
5814 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
5816 struct switcher *switcher =
5817 container_of(listener, struct switcher, listener);
5819 switcher_next(switcher);
5823 switcher_destroy(struct switcher *switcher)
5825 struct weston_view *view;
5826 struct weston_keyboard *keyboard = switcher->grab.keyboard;
5827 struct workspace *ws = get_current_workspace(switcher->shell);
5829 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
5830 if (is_focus_view(view))
5834 weston_surface_damage(view->surface);
5837 if (switcher->current)
5838 activate(switcher->shell, switcher->current,
5839 (struct weston_seat *) keyboard->seat, true);
5840 wl_list_remove(&switcher->listener.link);
5841 weston_keyboard_end_grab(keyboard);
5842 if (keyboard->input_method_resource)
5843 keyboard->grab = &keyboard->input_method_grab;
5845 /* re-hide surfaces that were temporary shown during the switch */
5846 struct weston_view **minimized;
5847 wl_array_for_each(minimized, &switcher->minimized_array) {
5848 /* with the exception of the current selected */
5849 if ((*minimized)->surface != switcher->current) {
5850 weston_layer_entry_remove(&(*minimized)->layer_link);
5851 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5852 weston_view_damage_below(*minimized);
5855 wl_array_release(&switcher->minimized_array);
5861 switcher_key(struct weston_keyboard_grab *grab,
5862 uint32_t time, uint32_t key, uint32_t state_w)
5864 struct switcher *switcher = container_of(grab, struct switcher, grab);
5865 enum wl_keyboard_key_state state = state_w;
5867 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
5868 switcher_next(switcher);
5872 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5873 uint32_t mods_depressed, uint32_t mods_latched,
5874 uint32_t mods_locked, uint32_t group)
5876 struct switcher *switcher = container_of(grab, struct switcher, grab);
5877 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5879 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5880 switcher_destroy(switcher);
5884 switcher_cancel(struct weston_keyboard_grab *grab)
5886 struct switcher *switcher = container_of(grab, struct switcher, grab);
5888 switcher_destroy(switcher);
5891 static const struct weston_keyboard_grab_interface switcher_grab = {
5898 switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5901 struct desktop_shell *shell = data;
5902 struct switcher *switcher;
5904 switcher = malloc(sizeof *switcher);
5905 switcher->shell = shell;
5906 switcher->current = NULL;
5907 switcher->listener.notify = switcher_handle_surface_destroy;
5908 wl_list_init(&switcher->listener.link);
5909 wl_array_init(&switcher->minimized_array);
5911 restore_all_output_modes(shell->compositor);
5912 lower_fullscreen_layer(switcher->shell);
5913 switcher->grab.interface = &switcher_grab;
5914 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5915 weston_keyboard_set_focus(seat->keyboard, NULL);
5916 switcher_next(switcher);
5920 backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5923 struct weston_compositor *compositor = data;
5924 struct weston_output *output;
5925 long backlight_new = 0;
5927 /* TODO: we're limiting to simple use cases, where we assume just
5928 * control on the primary display. We'd have to extend later if we
5929 * ever get support for setting backlights on random desktop LCD
5931 output = get_default_output(compositor);
5935 if (!output->set_backlight)
5938 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5939 backlight_new = output->backlight_current - 25;
5940 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5941 backlight_new = output->backlight_current + 25;
5943 if (backlight_new < 5)
5945 if (backlight_new > 255)
5946 backlight_new = 255;
5948 output->backlight_current = backlight_new;
5949 output->set_backlight(output, output->backlight_current);
5952 struct debug_binding_grab {
5953 struct weston_keyboard_grab grab;
5954 struct weston_seat *seat;
5956 int key_released[2];
5960 debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
5961 uint32_t key, uint32_t state)
5963 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5964 struct weston_compositor *ec = db->seat->compositor;
5965 struct wl_display *display = ec->wl_display;
5966 struct wl_resource *resource;
5968 int send = 0, terminate = 0;
5969 int check_binding = 1;
5971 struct wl_list *resource_list;
5973 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5974 /* Do not run bindings on key releases */
5977 for (i = 0; i < 2; i++)
5978 if (key == db->key[i])
5979 db->key_released[i] = 1;
5981 if (db->key_released[0] && db->key_released[1]) {
5982 /* All key releases been swalled so end the grab */
5984 } else if (key != db->key[0] && key != db->key[1]) {
5985 /* Should not swallow release of other keys */
5988 } else if (key == db->key[0] && !db->key_released[0]) {
5989 /* Do not check bindings for the first press of the binding
5990 * key. This allows it to be used as a debug shortcut.
5991 * We still need to swallow this event. */
5993 } else if (db->key[1]) {
5994 /* If we already ran a binding don't process another one since
5995 * we can't keep track of all the binding keys that were
5996 * pressed in order to swallow the release events. */
6001 if (check_binding) {
6002 if (weston_compositor_run_debug_binding(ec, db->seat, time,
6004 /* We ran a binding so swallow the press and keep the
6005 * grab to swallow the released too. */
6010 /* Terminate the grab since the key pressed is not a
6011 * debug binding key. */
6018 serial = wl_display_next_serial(display);
6019 resource_list = &grab->keyboard->focus_resource_list;
6020 wl_resource_for_each(resource, resource_list) {
6021 wl_keyboard_send_key(resource, serial, time, key, state);
6026 weston_keyboard_end_grab(grab->keyboard);
6027 if (grab->keyboard->input_method_resource)
6028 grab->keyboard->grab = &grab->keyboard->input_method_grab;
6034 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
6035 uint32_t mods_depressed, uint32_t mods_latched,
6036 uint32_t mods_locked, uint32_t group)
6038 struct wl_resource *resource;
6039 struct wl_list *resource_list;
6041 resource_list = &grab->keyboard->focus_resource_list;
6043 wl_resource_for_each(resource, resource_list) {
6044 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
6045 mods_latched, mods_locked, group);
6050 debug_binding_cancel(struct weston_keyboard_grab *grab)
6052 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
6054 weston_keyboard_end_grab(grab->keyboard);
6058 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
6060 debug_binding_modifiers,
6061 debug_binding_cancel,
6065 debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
6067 struct debug_binding_grab *grab;
6069 grab = calloc(1, sizeof *grab);
6073 grab->seat = (struct weston_seat *) seat;
6075 grab->grab.interface = &debug_binding_keyboard_grab;
6076 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
6080 force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
6083 struct weston_surface *focus_surface;
6084 struct wl_client *client;
6085 struct desktop_shell *shell = data;
6086 struct weston_compositor *compositor = shell->compositor;
6089 focus_surface = seat->keyboard->focus;
6093 wl_signal_emit(&compositor->kill_signal, focus_surface);
6095 client = wl_resource_get_client(focus_surface->resource);
6096 wl_client_get_credentials(client, &pid, NULL, NULL);
6098 /* Skip clients that we launched ourselves (the credentials of
6099 * the socketpair is ours) */
6100 if (pid == getpid())
6107 workspace_up_binding(struct weston_seat *seat, uint32_t time,
6108 uint32_t key, void *data)
6110 struct desktop_shell *shell = data;
6111 unsigned int new_index = shell->workspaces.current;
6118 change_workspace(shell, new_index);
6122 workspace_down_binding(struct weston_seat *seat, uint32_t time,
6123 uint32_t key, void *data)
6125 struct desktop_shell *shell = data;
6126 unsigned int new_index = shell->workspaces.current;
6130 if (new_index < shell->workspaces.num - 1)
6133 change_workspace(shell, new_index);
6137 workspace_f_binding(struct weston_seat *seat, uint32_t time,
6138 uint32_t key, void *data)
6140 struct desktop_shell *shell = data;
6141 unsigned int new_index;
6145 new_index = key - KEY_F1;
6146 if (new_index >= shell->workspaces.num)
6147 new_index = shell->workspaces.num - 1;
6149 change_workspace(shell, new_index);
6153 workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
6154 uint32_t key, void *data)
6156 struct desktop_shell *shell = data;
6157 unsigned int new_index = shell->workspaces.current;
6165 take_surface_to_workspace_by_seat(shell, seat, new_index);
6169 workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
6170 uint32_t key, void *data)
6172 struct desktop_shell *shell = data;
6173 unsigned int new_index = shell->workspaces.current;
6178 if (new_index < shell->workspaces.num - 1)
6181 take_surface_to_workspace_by_seat(shell, seat, new_index);
6185 shell_reposition_view_on_output_destroy(struct weston_view *view)
6187 struct weston_output *output, *first_output;
6188 struct weston_compositor *ec = view->surface->compositor;
6189 struct shell_surface *shsurf;
6193 x = view->geometry.x;
6194 y = view->geometry.y;
6196 /* At this point the destroyed output is not in the list anymore.
6197 * If the view is still visible somewhere, we leave where it is,
6198 * otherwise, move it to the first output. */
6200 wl_list_for_each(output, &ec->output_list, link) {
6201 if (pixman_region32_contains_point(&output->region,
6209 first_output = container_of(ec->output_list.next,
6210 struct weston_output, link);
6212 x = first_output->x + first_output->width / 4;
6213 y = first_output->y + first_output->height / 4;
6215 weston_view_set_position(view, x, y);
6217 weston_view_geometry_dirty(view);
6221 shsurf = get_shell_surface(view->surface);
6224 shsurf->saved_position_valid = false;
6225 shsurf->next_state.maximized = false;
6226 shsurf->next_state.fullscreen = false;
6227 shsurf->state_changed = true;
6232 shell_for_each_layer(struct desktop_shell *shell,
6233 shell_for_each_layer_func_t func, void *data)
6235 struct workspace **ws;
6237 func(shell, &shell->fullscreen_layer, data);
6238 func(shell, &shell->panel_layer, data);
6239 func(shell, &shell->background_layer, data);
6240 func(shell, &shell->lock_layer, data);
6241 func(shell, &shell->input_panel_layer, data);
6243 wl_array_for_each(ws, &shell->workspaces.array)
6244 func(shell, &(*ws)->layer, data);
6248 shell_output_destroy_move_layer(struct desktop_shell *shell,
6249 struct weston_layer *layer,
6252 struct weston_output *output = data;
6253 struct weston_view *view;
6255 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6256 if (view->output != output)
6259 shell_reposition_view_on_output_destroy(view);
6264 handle_output_destroy(struct wl_listener *listener, void *data)
6266 struct shell_output *output_listener =
6267 container_of(listener, struct shell_output, destroy_listener);
6268 struct weston_output *output = output_listener->output;
6269 struct desktop_shell *shell = output_listener->shell;
6271 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
6273 wl_list_remove(&output_listener->destroy_listener.link);
6274 wl_list_remove(&output_listener->link);
6275 free(output_listener);
6279 create_shell_output(struct desktop_shell *shell,
6280 struct weston_output *output)
6282 struct shell_output *shell_output;
6284 shell_output = zalloc(sizeof *shell_output);
6285 if (shell_output == NULL)
6288 shell_output->output = output;
6289 shell_output->shell = shell;
6290 shell_output->destroy_listener.notify = handle_output_destroy;
6291 wl_signal_add(&output->destroy_signal,
6292 &shell_output->destroy_listener);
6293 wl_list_insert(shell->output_list.prev, &shell_output->link);
6297 handle_output_create(struct wl_listener *listener, void *data)
6299 struct desktop_shell *shell =
6300 container_of(listener, struct desktop_shell, output_create_listener);
6301 struct weston_output *output = (struct weston_output *)data;
6303 create_shell_output(shell, output);
6307 handle_output_move_layer(struct desktop_shell *shell,
6308 struct weston_layer *layer, void *data)
6310 struct weston_output *output = data;
6311 struct weston_view *view;
6314 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6315 if (view->output != output)
6318 x = view->geometry.x + output->move_x;
6319 y = view->geometry.y + output->move_y;
6320 weston_view_set_position(view, x, y);
6325 handle_output_move(struct wl_listener *listener, void *data)
6327 struct desktop_shell *shell;
6329 shell = container_of(listener, struct desktop_shell,
6330 output_move_listener);
6332 shell_for_each_layer(shell, handle_output_move_layer, data);
6336 setup_output_destroy_handler(struct weston_compositor *ec,
6337 struct desktop_shell *shell)
6339 struct weston_output *output;
6341 wl_list_init(&shell->output_list);
6342 wl_list_for_each(output, &ec->output_list, link)
6343 create_shell_output(shell, output);
6345 shell->output_create_listener.notify = handle_output_create;
6346 wl_signal_add(&ec->output_created_signal,
6347 &shell->output_create_listener);
6349 shell->output_move_listener.notify = handle_output_move;
6350 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
6354 shell_destroy(struct wl_listener *listener, void *data)
6356 struct desktop_shell *shell =
6357 container_of(listener, struct desktop_shell, destroy_listener);
6358 struct workspace **ws;
6359 struct shell_output *shell_output, *tmp;
6361 /* Force state to unlocked so we don't try to fade */
6362 shell->locked = false;
6364 if (shell->child.client) {
6365 /* disable respawn */
6366 wl_list_remove(&shell->child.client_destroy_listener.link);
6367 wl_client_destroy(shell->child.client);
6370 wl_list_remove(&shell->idle_listener.link);
6371 wl_list_remove(&shell->wake_listener.link);
6373 input_panel_destroy(shell);
6375 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6376 wl_list_remove(&shell_output->destroy_listener.link);
6377 wl_list_remove(&shell_output->link);
6381 wl_list_remove(&shell->output_create_listener.link);
6382 wl_list_remove(&shell->output_move_listener.link);
6384 wl_array_for_each(ws, &shell->workspaces.array)
6385 workspace_destroy(*ws);
6386 wl_array_release(&shell->workspaces.array);
6388 free(shell->screensaver.path);
6389 free(shell->client);
6394 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6397 int i, num_workspace_bindings;
6399 /* fixed bindings */
6400 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6401 MODIFIER_CTRL | MODIFIER_ALT,
6402 terminate_binding, ec);
6403 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6404 click_to_activate_binding,
6406 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6407 click_to_activate_binding,
6409 weston_compositor_add_touch_binding(ec, 0,
6410 touch_to_activate_binding,
6412 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6413 MODIFIER_SUPER | MODIFIER_ALT,
6414 surface_opacity_binding, NULL);
6415 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6416 MODIFIER_SUPER, zoom_axis_binding,
6419 /* configurable bindings */
6420 mod = shell->binding_modifier;
6421 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6422 zoom_key_binding, NULL);
6423 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6424 zoom_key_binding, NULL);
6425 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6426 maximize_binding, NULL);
6427 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6428 fullscreen_binding, NULL);
6429 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6431 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
6432 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6433 resize_binding, shell);
6434 weston_compositor_add_button_binding(ec, BTN_LEFT,
6435 mod | MODIFIER_SHIFT,
6436 resize_binding, shell);
6438 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6439 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6440 rotate_binding, NULL);
6442 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6444 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6446 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6447 backlight_binding, ec);
6448 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6450 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6451 backlight_binding, ec);
6452 weston_compositor_add_key_binding(ec, KEY_K, mod,
6453 force_kill_binding, shell);
6454 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6455 workspace_up_binding, shell);
6456 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6457 workspace_down_binding, shell);
6458 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6459 workspace_move_surface_up_binding,
6461 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6462 workspace_move_surface_down_binding,
6465 if (shell->exposay_modifier)
6466 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6467 exposay_binding, shell);
6469 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6470 if (shell->workspaces.num > 1) {
6471 num_workspace_bindings = shell->workspaces.num;
6472 if (num_workspace_bindings > 6)
6473 num_workspace_bindings = 6;
6474 for (i = 0; i < num_workspace_bindings; i++)
6475 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6476 workspace_f_binding,
6480 /* Debug bindings */
6481 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
6482 debug_binding, shell);
6486 handle_seat_created(struct wl_listener *listener, void *data)
6488 struct weston_seat *seat = data;
6490 create_shell_seat(seat);
6494 module_init(struct weston_compositor *ec,
6495 int *argc, char *argv[])
6497 struct weston_seat *seat;
6498 struct desktop_shell *shell;
6499 struct workspace **pws;
6501 struct wl_event_loop *loop;
6503 shell = zalloc(sizeof *shell);
6507 shell->compositor = ec;
6509 shell->destroy_listener.notify = shell_destroy;
6510 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
6511 shell->idle_listener.notify = idle_handler;
6512 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6513 shell->wake_listener.notify = wake_handler;
6514 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
6516 ec->shell_interface.shell = shell;
6517 ec->shell_interface.create_shell_surface = create_shell_surface;
6518 ec->shell_interface.get_primary_view = get_primary_view;
6519 ec->shell_interface.set_toplevel = set_toplevel;
6520 ec->shell_interface.set_transient = set_transient;
6521 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
6522 ec->shell_interface.set_xwayland = set_xwayland;
6523 ec->shell_interface.move = shell_interface_move;
6524 ec->shell_interface.resize = surface_resize;
6525 ec->shell_interface.set_title = set_title;
6526 ec->shell_interface.set_window_geometry = set_window_geometry;
6528 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6529 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
6530 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6531 weston_layer_init(&shell->lock_layer, NULL);
6532 weston_layer_init(&shell->input_panel_layer, NULL);
6534 wl_array_init(&shell->workspaces.array);
6535 wl_list_init(&shell->workspaces.client_list);
6537 if (input_panel_setup(shell) < 0)
6540 shell_configuration(shell);
6542 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6543 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6545 for (i = 0; i < shell->workspaces.num; i++) {
6546 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6550 *pws = workspace_create();
6554 activate_workspace(shell, 0);
6556 weston_layer_init(&shell->minimized_layer, NULL);
6558 wl_list_init(&shell->workspaces.anim_sticky_list);
6559 wl_list_init(&shell->workspaces.animation.link);
6560 shell->workspaces.animation.frame = animate_workspace_change_frame;
6562 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
6563 shell, bind_shell) == NULL)
6566 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6567 shell, bind_xdg_shell) == NULL)
6570 if (wl_global_create(ec->wl_display,
6571 &desktop_shell_interface, 3,
6572 shell, bind_desktop_shell) == NULL)
6575 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6576 shell, bind_screensaver) == NULL)
6579 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6580 shell, bind_workspace_manager) == NULL)
6583 shell->child.deathstamp = weston_compositor_get_time();
6585 shell->panel_position = DESKTOP_SHELL_PANEL_POSITION_TOP;
6587 setup_output_destroy_handler(ec, shell);
6589 loop = wl_display_get_event_loop(ec->wl_display);
6590 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
6592 shell->screensaver.timer =
6593 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6595 wl_list_for_each(seat, &ec->seat_list, link)
6596 handle_seat_created(NULL, seat);
6597 shell->seat_create_listener.notify = handle_seat_created;
6598 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
6600 screenshooter_create(ec);
6602 shell_add_bindings(ec, shell);
6604 shell_fade_init(shell);
6606 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);