2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
33 #include <linux/input.h>
37 #include <sys/types.h>
40 #include "compositor/weston.h"
41 #include "weston-desktop-shell-server-protocol.h"
42 #include "shared/config-parser.h"
43 #include "shared/helpers.h"
44 #include "xdg-shell-unstable-v5-server-protocol.h"
46 #define DEFAULT_NUM_WORKSPACES 1
47 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
50 #define static_assert(cond, msg)
54 struct weston_seat *seat;
56 struct weston_surface *keyboard_focus;
58 struct wl_listener seat_destroy_listener;
59 struct wl_listener surface_destroy_listener;
62 enum shell_surface_type {
64 SHELL_SURFACE_TOPLEVEL,
66 SHELL_SURFACE_XWAYLAND
72 * Surface stacking and ordering.
74 * This is handled using several linked lists of surfaces, organised into
75 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
76 * above all of the surfaces in the layer below. The set of layers is static and
77 * in the following order (top-most first):
78 * • Lock layer (only ever displayed on its own)
86 * The list of layers may be manipulated to remove whole layers of surfaces from
87 * display. For example, when locking the screen, all layers except the lock
90 * A surface’s layer is modified on configuring the surface, in
91 * set_surface_type() (which is only called when the surface’s type change is
92 * _committed_). If a surface’s type changes (e.g. when making a window
93 * fullscreen) its layer changes too.
95 * In order to allow popup and transient surfaces to be correctly stacked above
96 * their parent surfaces, each surface tracks both its parent surface, and a
97 * linked list of its children. When a surface’s layer is updated, so are the
98 * layers of its children. Note that child surfaces are *not* the same as
99 * subsurfaces — child/parent surfaces are purely for maintaining stacking
102 * The children_link list of siblings of a surface (i.e. those surfaces which
103 * have the same parent) only contains weston_surfaces which have a
104 * shell_surface. Stacking is not implemented for non-shell_surface
105 * weston_surfaces. This means that the following implication does *not* hold:
106 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
109 struct shell_surface {
110 struct wl_resource *resource;
111 struct wl_signal destroy_signal;
112 struct shell_client *owner;
113 struct wl_resource *owner_resource;
115 struct weston_surface *surface;
116 struct weston_view *view;
117 int32_t last_width, last_height;
118 struct wl_listener surface_destroy_listener;
119 struct wl_listener resource_destroy_listener;
121 struct weston_surface *parent;
122 struct wl_list children_list; /* child surfaces of this one */
123 struct wl_list children_link; /* sibling surfaces of this one */
124 struct desktop_shell *shell;
126 enum shell_surface_type type;
128 int32_t saved_x, saved_y;
129 bool saved_position_valid;
130 bool saved_rotation_valid;
131 int unresponsive, grabbed;
132 uint32_t resize_edges;
135 struct weston_transform transform;
136 struct weston_matrix rotation;
140 struct wl_list grab_link;
142 struct shell_seat *shseat;
152 enum wl_shell_surface_fullscreen_method type;
153 struct weston_transform transform; /* matrix from x, y */
155 struct weston_view *black_view;
158 struct weston_transform workspace_transform;
160 struct weston_output *fullscreen_output;
161 struct weston_output *output;
164 const struct weston_shell_client *client;
166 struct surface_state {
171 } state, next_state, requested_state; /* surface states */
173 bool state_requested;
176 int32_t x, y, width, height;
177 } geometry, next_geometry;
178 bool has_set_geometry, has_next_geometry;
186 struct weston_pointer_grab grab;
187 struct shell_surface *shsurf;
188 struct wl_listener shsurf_destroy_listener;
191 struct shell_touch_grab {
192 struct weston_touch_grab grab;
193 struct shell_surface *shsurf;
194 struct wl_listener shsurf_destroy_listener;
195 struct weston_touch *touch;
198 struct weston_move_grab {
199 struct shell_grab base;
201 bool client_initiated;
204 struct weston_touch_move_grab {
205 struct shell_touch_grab base;
211 struct shell_grab base;
212 struct weston_matrix rotation;
220 struct weston_seat *seat;
221 struct wl_listener seat_destroy_listener;
222 struct weston_surface *focused_surface;
224 struct wl_listener caps_changed_listener;
225 struct wl_listener pointer_focus_listener;
226 struct wl_listener keyboard_focus_listener;
229 struct weston_pointer_grab grab;
230 struct weston_touch_grab touch_grab;
231 struct wl_list surfaces_list;
232 struct wl_client *client;
234 enum { POINTER, TOUCH } type;
238 struct shell_client {
239 struct wl_resource *resource;
240 struct wl_client *client;
241 struct desktop_shell *shell;
242 struct wl_listener destroy_listener;
243 struct wl_event_source *ping_timer;
244 uint32_t ping_serial;
246 struct wl_list surface_list;
249 static struct desktop_shell *
250 shell_surface_get_shell(struct shell_surface *shsurf);
253 surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
256 shell_fade_startup(struct desktop_shell *shell);
258 static struct shell_seat *
259 get_shell_seat(struct weston_seat *seat);
262 get_output_panel_size(struct desktop_shell *shell,
263 struct weston_output *output,
264 int *width, int *height);
267 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
270 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
273 shell_surface_is_xdg_surface(struct shell_surface *shsurf);
276 shell_surface_is_xdg_popup(struct shell_surface *shsurf);
279 shell_surface_set_parent(struct shell_surface *shsurf,
280 struct weston_surface *parent);
283 shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
285 struct shell_surface *shsurf;
286 const char *typestr[] = {
287 [SHELL_SURFACE_NONE] = "unidentified",
288 [SHELL_SURFACE_TOPLEVEL] = "top-level",
289 [SHELL_SURFACE_POPUP] = "popup",
290 [SHELL_SURFACE_XWAYLAND] = "Xwayland",
294 shsurf = get_shell_surface(surface);
296 return snprintf(buf, len, "unidentified window");
301 return snprintf(buf, len, "%s window%s%s%s%s%s",
302 typestr[shsurf->type],
303 t ? " '" : "", t ?: "", t ? "'" : "",
304 c ? " of " : "", c ?: "");
308 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
310 struct desktop_shell *shell;
311 struct weston_view *view;
312 struct shell_surface *top_fs_shsurf = NULL;
314 shell = shell_surface_get_shell(shsurf);
316 if (wl_list_empty(&shell->fullscreen_layer.view_list.link))
319 /* Find topmost shsurf on the same fullscreen output on which shsurf
320 * is displaying. We don't care about other outputs.
322 wl_list_for_each(view, &shell->fullscreen_layer.view_list.link,
324 struct shell_surface *cand_shsurf = get_shell_surface(view->surface);
327 (cand_shsurf->fullscreen_output == shsurf->fullscreen_output)) {
328 top_fs_shsurf = cand_shsurf;
333 return (shsurf == top_fs_shsurf);
337 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
339 struct shell_grab *grab;
341 grab = container_of(listener, struct shell_grab,
342 shsurf_destroy_listener);
348 get_default_view(struct weston_surface *surface)
350 struct shell_surface *shsurf;
351 struct weston_view *view;
353 if (!surface || wl_list_empty(&surface->views))
356 shsurf = get_shell_surface(surface);
360 wl_list_for_each(view, &surface->views, surface_link)
361 if (weston_view_is_mapped(view))
364 return container_of(surface->views.next, struct weston_view, surface_link);
368 popup_grab_end(struct weston_pointer *pointer);
370 touch_popup_grab_end(struct weston_touch *touch);
373 shell_grab_start(struct shell_grab *grab,
374 const struct weston_pointer_grab_interface *interface,
375 struct shell_surface *shsurf,
376 struct weston_pointer *pointer,
377 enum weston_desktop_shell_cursor cursor)
379 struct desktop_shell *shell = shsurf->shell;
380 struct weston_touch *touch = weston_seat_get_touch(pointer->seat);
382 popup_grab_end(pointer);
384 touch_popup_grab_end(touch);
386 grab->grab.interface = interface;
387 grab->shsurf = shsurf;
388 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
389 wl_signal_add(&shsurf->destroy_signal,
390 &grab->shsurf_destroy_listener);
393 weston_pointer_start_grab(pointer, &grab->grab);
394 if (shell->child.desktop_shell) {
395 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
397 weston_pointer_set_focus(pointer,
398 get_default_view(shell->grab_surface),
399 wl_fixed_from_int(0),
400 wl_fixed_from_int(0));
405 get_output_panel_size(struct desktop_shell *shell,
406 struct weston_output *output,
410 struct weston_view *view;
418 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
421 if (view->surface->output != output)
424 switch (shell->panel_position) {
425 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
426 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
427 weston_view_to_global_float(view,
428 view->surface->width, 0,
431 *width = (int)x - output->x;
432 *height = view->surface->height + (int) y - output->y;
435 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
436 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
437 weston_view_to_global_float(view,
438 0, view->surface->height,
441 *width = view->surface->width + (int)x - output->x;
442 *height = (int)y - output->y;
446 /* we've already set width and height to
447 * fallback values. */
452 /* the correct view wasn't found */
456 get_output_work_area(void *data,
457 struct weston_output *output,
458 pixman_rectangle32_t *area)
460 struct desktop_shell *shell = data;
461 int32_t panel_width = 0, panel_height = 0;
466 get_output_panel_size(shell, output, &panel_width, &panel_height);
467 switch (shell->panel_position) {
468 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
470 area->y += panel_height;
471 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
472 area->width = output->width;
473 area->height = output->height - panel_height;
475 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
476 area->x += panel_width;
477 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
478 area->width = output->width - panel_width;
479 area->height = output->height;
484 static struct shell_surface *
485 find_toplevel_surface(struct shell_surface *in_surface)
487 struct shell_surface *surface = in_surface;
490 if (surface->type == SHELL_SURFACE_TOPLEVEL)
492 surface = get_shell_surface(surface->parent);
495 /* If no top level surface was found, just use whatever surface was
496 originally provided. */
501 send_configure_for_surface(struct shell_surface *shsurf)
503 int32_t width, height;
504 struct surface_state *state;
506 if (shsurf->state_requested)
507 state = &shsurf->requested_state;
508 else if (shsurf->state_changed)
509 state = &shsurf->next_state;
511 state = &shsurf->state;
513 if (state->fullscreen) {
514 width = shsurf->output->width;
515 height = shsurf->output->height;
516 } else if (state->maximized) {
517 struct desktop_shell *shell;
518 pixman_rectangle32_t area;
520 shell = shell_surface_get_shell(shsurf);
521 get_output_work_area(shell, shsurf->output, &area);
524 height = area.height;
525 } else if (shsurf->resize_edges) {
526 width = shsurf->geometry.width;
527 height = shsurf->geometry.height;
533 shsurf->client->send_configure(shsurf->surface, width, height);
537 shell_surface_state_changed(struct shell_surface *shsurf)
539 if (shell_surface_is_xdg_surface(shsurf))
540 send_configure_for_surface(shsurf);
544 shell_grab_end(struct shell_grab *grab)
547 wl_list_remove(&grab->shsurf_destroy_listener.link);
548 grab->shsurf->grabbed = 0;
550 if (grab->shsurf->resize_edges) {
551 grab->shsurf->resize_edges = 0;
552 shell_surface_state_changed(grab->shsurf);
556 weston_pointer_end_grab(grab->grab.pointer);
560 shell_touch_grab_start(struct shell_touch_grab *grab,
561 const struct weston_touch_grab_interface *interface,
562 struct shell_surface *shsurf,
563 struct weston_touch *touch)
565 struct desktop_shell *shell = shsurf->shell;
566 struct weston_pointer *pointer = weston_seat_get_pointer(touch->seat);
568 touch_popup_grab_end(touch);
570 popup_grab_end(pointer);
572 grab->grab.interface = interface;
573 grab->shsurf = shsurf;
574 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
575 wl_signal_add(&shsurf->destroy_signal,
576 &grab->shsurf_destroy_listener);
581 weston_touch_start_grab(touch, &grab->grab);
582 if (shell->child.desktop_shell)
583 weston_touch_set_focus(touch,
584 get_default_view(shell->grab_surface));
588 shell_touch_grab_end(struct shell_touch_grab *grab)
591 wl_list_remove(&grab->shsurf_destroy_listener.link);
592 grab->shsurf->grabbed = 0;
595 weston_touch_end_grab(grab->touch);
599 center_on_output(struct weston_view *view,
600 struct weston_output *output);
602 static enum weston_keyboard_modifier
603 get_modifier(char *modifier)
606 return MODIFIER_SUPER;
608 if (!strcmp("ctrl", modifier))
609 return MODIFIER_CTRL;
610 else if (!strcmp("alt", modifier))
612 else if (!strcmp("super", modifier))
613 return MODIFIER_SUPER;
614 else if (!strcmp("none", modifier))
617 return MODIFIER_SUPER;
620 static enum animation_type
621 get_animation_type(char *animation)
624 return ANIMATION_NONE;
626 if (!strcmp("zoom", animation))
627 return ANIMATION_ZOOM;
628 else if (!strcmp("fade", animation))
629 return ANIMATION_FADE;
630 else if (!strcmp("dim-layer", animation))
631 return ANIMATION_DIM_LAYER;
633 return ANIMATION_NONE;
637 shell_configuration(struct desktop_shell *shell)
639 struct weston_config_section *section;
644 section = weston_config_get_section(wet_get_config(shell->compositor),
645 "shell", NULL, NULL);
646 ret = asprintf(&client, "%s/%s", weston_config_get_libexec_dir(),
647 WESTON_SHELL_CLIENT);
650 weston_config_section_get_string(section,
651 "client", &s, client);
655 weston_config_section_get_bool(section,
656 "allow-zap", &allow_zap, true);
657 shell->allow_zap = allow_zap;
659 weston_config_section_get_string(section,
660 "binding-modifier", &s, "super");
661 shell->binding_modifier = get_modifier(s);
664 weston_config_section_get_string(section,
665 "exposay-modifier", &s, "none");
666 shell->exposay_modifier = get_modifier(s);
669 weston_config_section_get_string(section, "animation", &s, "none");
670 shell->win_animation_type = get_animation_type(s);
672 weston_config_section_get_string(section, "close-animation", &s, "fade");
673 shell->win_close_animation_type = get_animation_type(s);
675 weston_config_section_get_string(section,
676 "startup-animation", &s, "fade");
677 shell->startup_animation_type = get_animation_type(s);
679 if (shell->startup_animation_type == ANIMATION_ZOOM)
680 shell->startup_animation_type = ANIMATION_NONE;
681 weston_config_section_get_string(section, "focus-animation", &s, "none");
682 shell->focus_animation_type = get_animation_type(s);
684 weston_config_section_get_uint(section, "num-workspaces",
685 &shell->workspaces.num,
686 DEFAULT_NUM_WORKSPACES);
689 struct weston_output *
690 get_default_output(struct weston_compositor *compositor)
692 if (wl_list_empty(&compositor->output_list))
695 return container_of(compositor->output_list.next,
696 struct weston_output, link);
700 focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
702 return snprintf(buf, len, "focus highlight effect for output %s",
703 surface->output->name);
706 /* no-op func for checking focus surface */
708 focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
712 static struct focus_surface *
713 get_focus_surface(struct weston_surface *surface)
715 if (surface->committed == focus_surface_committed)
716 return surface->committed_private;
722 is_focus_surface (struct weston_surface *es)
724 return (es->committed == focus_surface_committed);
728 is_focus_view (struct weston_view *view)
730 return is_focus_surface (view->surface);
733 static struct focus_surface *
734 create_focus_surface(struct weston_compositor *ec,
735 struct weston_output *output)
737 struct focus_surface *fsurf = NULL;
738 struct weston_surface *surface = NULL;
740 fsurf = malloc(sizeof *fsurf);
744 fsurf->surface = weston_surface_create(ec);
745 surface = fsurf->surface;
746 if (surface == NULL) {
751 surface->committed = focus_surface_committed;
752 surface->output = output;
753 surface->is_mapped = true;
754 surface->committed_private = fsurf;
755 weston_surface_set_label_func(surface, focus_surface_get_label);
757 fsurf->view = weston_view_create(surface);
758 if (fsurf->view == NULL) {
759 weston_surface_destroy(surface);
763 fsurf->view->output = output;
764 fsurf->view->is_mapped = true;
766 weston_surface_set_size(surface, output->width, output->height);
767 weston_view_set_position(fsurf->view, output->x, output->y);
768 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
769 pixman_region32_fini(&surface->opaque);
770 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
771 output->width, output->height);
772 pixman_region32_fini(&surface->input);
773 pixman_region32_init(&surface->input);
775 wl_list_init(&fsurf->workspace_transform.link);
781 focus_surface_destroy(struct focus_surface *fsurf)
783 weston_surface_destroy(fsurf->surface);
788 focus_animation_done(struct weston_view_animation *animation, void *data)
790 struct workspace *ws = data;
792 ws->focus_animation = NULL;
796 focus_state_destroy(struct focus_state *state)
798 wl_list_remove(&state->seat_destroy_listener.link);
799 wl_list_remove(&state->surface_destroy_listener.link);
804 focus_state_seat_destroy(struct wl_listener *listener, void *data)
806 struct focus_state *state = container_of(listener,
808 seat_destroy_listener);
810 wl_list_remove(&state->link);
811 focus_state_destroy(state);
815 focus_state_surface_destroy(struct wl_listener *listener, void *data)
817 struct focus_state *state = container_of(listener,
819 surface_destroy_listener);
820 struct desktop_shell *shell;
821 struct weston_surface *main_surface;
822 struct weston_view *next;
823 struct weston_view *view;
825 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
828 wl_list_for_each(view,
829 &state->ws->layer.view_list.link, layer_link.link) {
830 if (view->surface == main_surface)
832 if (is_focus_view(view))
839 /* if the focus was a sub-surface, activate its main surface */
840 if (main_surface != state->keyboard_focus)
841 next = get_default_view(main_surface);
843 shell = state->seat->compositor->shell_interface.shell;
845 state->keyboard_focus = NULL;
846 activate(shell, next, state->seat,
847 WESTON_ACTIVATE_FLAG_CONFIGURE);
849 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
850 if (state->ws->focus_animation)
851 weston_view_animation_destroy(state->ws->focus_animation);
853 state->ws->focus_animation = weston_fade_run(
854 state->ws->fsurf_front->view,
855 state->ws->fsurf_front->view->alpha, 0.0, 300,
856 focus_animation_done, state->ws);
859 wl_list_remove(&state->link);
860 focus_state_destroy(state);
864 static struct focus_state *
865 focus_state_create(struct weston_seat *seat, struct workspace *ws)
867 struct focus_state *state;
869 state = malloc(sizeof *state);
873 state->keyboard_focus = NULL;
876 wl_list_insert(&ws->focus_list, &state->link);
878 state->seat_destroy_listener.notify = focus_state_seat_destroy;
879 state->surface_destroy_listener.notify = focus_state_surface_destroy;
880 wl_signal_add(&seat->destroy_signal,
881 &state->seat_destroy_listener);
882 wl_list_init(&state->surface_destroy_listener.link);
887 static struct focus_state *
888 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
890 struct workspace *ws = get_current_workspace(shell);
891 struct focus_state *state;
893 wl_list_for_each(state, &ws->focus_list, link)
894 if (state->seat == seat)
897 if (&state->link == &ws->focus_list)
898 state = focus_state_create(seat, ws);
904 focus_state_set_focus(struct focus_state *state,
905 struct weston_surface *surface)
907 if (state->keyboard_focus) {
908 wl_list_remove(&state->surface_destroy_listener.link);
909 wl_list_init(&state->surface_destroy_listener.link);
912 state->keyboard_focus = surface;
914 wl_signal_add(&surface->destroy_signal,
915 &state->surface_destroy_listener);
919 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
921 struct focus_state *state, *next;
922 struct weston_surface *surface;
923 struct wl_list pending_seat_list;
924 struct weston_seat *seat, *next_seat;
926 /* Temporarily steal the list of seats so that we can keep
927 * track of the seats we've already processed */
928 wl_list_init(&pending_seat_list);
929 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
930 wl_list_init(&shell->compositor->seat_list);
932 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
933 struct weston_keyboard *keyboard =
934 weston_seat_get_keyboard(state->seat);
936 wl_list_remove(&state->seat->link);
937 wl_list_insert(&shell->compositor->seat_list,
943 surface = state->keyboard_focus;
945 weston_keyboard_set_focus(keyboard, surface);
948 /* For any remaining seats that we don't have a focus state
949 * for we'll reset the keyboard focus to NULL */
950 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
951 struct weston_keyboard *keyboard =
952 weston_seat_get_keyboard(seat);
954 wl_list_insert(&shell->compositor->seat_list, &seat->link);
959 weston_keyboard_set_focus(keyboard, NULL);
964 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
965 struct weston_seat *seat)
967 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
968 struct focus_state *state;
970 wl_list_for_each(state, &ws->focus_list, link) {
971 if (state->seat == seat) {
972 focus_state_set_focus(state, keyboard->focus);
979 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
980 struct weston_surface *surface)
982 struct focus_state *state;
984 wl_list_for_each(state, &ws->focus_list, link)
985 if (state->keyboard_focus == surface)
986 focus_state_set_focus(state, NULL);
990 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
991 struct weston_view *from, struct weston_view *to)
993 struct weston_output *output;
994 bool focus_surface_created = false;
996 /* FIXME: Only support dim animation using two layers */
997 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
1000 output = get_default_output(shell->compositor);
1001 if (ws->fsurf_front == NULL && (from || to)) {
1002 ws->fsurf_front = create_focus_surface(shell->compositor, output);
1003 if (ws->fsurf_front == NULL)
1005 ws->fsurf_front->view->alpha = 0.0;
1007 ws->fsurf_back = create_focus_surface(shell->compositor, output);
1008 if (ws->fsurf_back == NULL) {
1009 focus_surface_destroy(ws->fsurf_front);
1012 ws->fsurf_back->view->alpha = 0.0;
1014 focus_surface_created = true;
1016 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
1017 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
1020 if (ws->focus_animation) {
1021 weston_view_animation_destroy(ws->focus_animation);
1022 ws->focus_animation = NULL;
1026 weston_layer_entry_insert(&to->layer_link,
1027 &ws->fsurf_front->view->layer_link);
1029 weston_layer_entry_insert(&ws->layer.view_list,
1030 &ws->fsurf_front->view->layer_link);
1032 if (focus_surface_created) {
1033 ws->focus_animation = weston_fade_run(
1034 ws->fsurf_front->view,
1035 ws->fsurf_front->view->alpha, 0.4, 300,
1036 focus_animation_done, ws);
1038 weston_layer_entry_insert(&from->layer_link,
1039 &ws->fsurf_back->view->layer_link);
1040 ws->focus_animation = weston_stable_fade_run(
1041 ws->fsurf_front->view, 0.0,
1042 ws->fsurf_back->view, 0.4,
1043 focus_animation_done, ws);
1045 weston_layer_entry_insert(&ws->layer.view_list,
1046 &ws->fsurf_back->view->layer_link);
1047 ws->focus_animation = weston_stable_fade_run(
1048 ws->fsurf_front->view, 0.0,
1049 ws->fsurf_back->view, 0.4,
1050 focus_animation_done, ws);
1055 workspace_destroy(struct workspace *ws)
1057 struct focus_state *state, *next;
1059 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1060 focus_state_destroy(state);
1062 if (ws->fsurf_front)
1063 focus_surface_destroy(ws->fsurf_front);
1065 focus_surface_destroy(ws->fsurf_back);
1071 seat_destroyed(struct wl_listener *listener, void *data)
1073 struct weston_seat *seat = data;
1074 struct focus_state *state, *next;
1075 struct workspace *ws = container_of(listener,
1077 seat_destroyed_listener);
1079 wl_list_for_each_safe(state, next, &ws->focus_list, link)
1080 if (state->seat == seat)
1081 wl_list_remove(&state->link);
1084 static struct workspace *
1085 workspace_create(void)
1087 struct workspace *ws = malloc(sizeof *ws);
1091 weston_layer_init(&ws->layer, NULL);
1093 wl_list_init(&ws->focus_list);
1094 wl_list_init(&ws->seat_destroyed_listener.link);
1095 ws->seat_destroyed_listener.notify = seat_destroyed;
1096 ws->fsurf_front = NULL;
1097 ws->fsurf_back = NULL;
1098 ws->focus_animation = NULL;
1104 workspace_is_empty(struct workspace *ws)
1106 return wl_list_empty(&ws->layer.view_list.link);
1109 static struct workspace *
1110 get_workspace(struct desktop_shell *shell, unsigned int index)
1112 struct workspace **pws = shell->workspaces.array.data;
1113 assert(index < shell->workspaces.num);
1119 get_current_workspace(struct desktop_shell *shell)
1121 return get_workspace(shell, shell->workspaces.current);
1125 activate_workspace(struct desktop_shell *shell, unsigned int index)
1127 struct workspace *ws;
1129 ws = get_workspace(shell, index);
1130 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1132 shell->workspaces.current = index;
1136 get_output_height(struct weston_output *output)
1138 return abs(output->region.extents.y1 - output->region.extents.y2);
1142 view_translate(struct workspace *ws, struct weston_view *view, double d)
1144 struct weston_transform *transform;
1146 if (is_focus_view(view)) {
1147 struct focus_surface *fsurf = get_focus_surface(view->surface);
1148 transform = &fsurf->workspace_transform;
1150 struct shell_surface *shsurf = get_shell_surface(view->surface);
1151 transform = &shsurf->workspace_transform;
1154 if (wl_list_empty(&transform->link))
1155 wl_list_insert(view->geometry.transformation_list.prev,
1158 weston_matrix_init(&transform->matrix);
1159 weston_matrix_translate(&transform->matrix,
1161 weston_view_geometry_dirty(view);
1165 workspace_translate_out(struct workspace *ws, double fraction)
1167 struct weston_view *view;
1168 unsigned int height;
1171 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1172 height = get_output_height(view->surface->output);
1173 d = height * fraction;
1175 view_translate(ws, view, d);
1180 workspace_translate_in(struct workspace *ws, double fraction)
1182 struct weston_view *view;
1183 unsigned int height;
1186 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1187 height = get_output_height(view->surface->output);
1190 d = -(height - height * fraction);
1192 d = height + height * fraction;
1194 view_translate(ws, view, d);
1199 reverse_workspace_change_animation(struct desktop_shell *shell,
1201 struct workspace *from,
1202 struct workspace *to)
1204 shell->workspaces.current = index;
1206 shell->workspaces.anim_to = to;
1207 shell->workspaces.anim_from = from;
1208 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1209 shell->workspaces.anim_timestamp = 0;
1211 weston_compositor_schedule_repaint(shell->compositor);
1215 workspace_deactivate_transforms(struct workspace *ws)
1217 struct weston_view *view;
1218 struct weston_transform *transform;
1220 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1221 if (is_focus_view(view)) {
1222 struct focus_surface *fsurf = get_focus_surface(view->surface);
1223 transform = &fsurf->workspace_transform;
1225 struct shell_surface *shsurf = get_shell_surface(view->surface);
1226 transform = &shsurf->workspace_transform;
1229 if (!wl_list_empty(&transform->link)) {
1230 wl_list_remove(&transform->link);
1231 wl_list_init(&transform->link);
1233 weston_view_geometry_dirty(view);
1238 finish_workspace_change_animation(struct desktop_shell *shell,
1239 struct workspace *from,
1240 struct workspace *to)
1242 struct weston_view *view;
1244 weston_compositor_schedule_repaint(shell->compositor);
1246 /* Views that extend past the bottom of the output are still
1247 * visible after the workspace animation ends but before its layer
1248 * is hidden. In that case, we need to damage below those views so
1249 * that the screen is properly repainted. */
1250 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1251 weston_view_damage_below(view);
1253 wl_list_remove(&shell->workspaces.animation.link);
1254 workspace_deactivate_transforms(from);
1255 workspace_deactivate_transforms(to);
1256 shell->workspaces.anim_to = NULL;
1258 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1262 animate_workspace_change_frame(struct weston_animation *animation,
1263 struct weston_output *output, uint32_t msecs)
1265 struct desktop_shell *shell =
1266 container_of(animation, struct desktop_shell,
1267 workspaces.animation);
1268 struct workspace *from = shell->workspaces.anim_from;
1269 struct workspace *to = shell->workspaces.anim_to;
1273 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1274 finish_workspace_change_animation(shell, from, to);
1278 if (shell->workspaces.anim_timestamp == 0) {
1279 if (shell->workspaces.anim_current == 0.0)
1280 shell->workspaces.anim_timestamp = msecs;
1282 shell->workspaces.anim_timestamp =
1284 /* Invers of movement function 'y' below. */
1285 (asin(1.0 - shell->workspaces.anim_current) *
1286 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1290 t = msecs - shell->workspaces.anim_timestamp;
1296 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1299 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1300 weston_compositor_schedule_repaint(shell->compositor);
1302 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1303 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1304 shell->workspaces.anim_current = y;
1306 weston_compositor_schedule_repaint(shell->compositor);
1309 finish_workspace_change_animation(shell, from, to);
1313 animate_workspace_change(struct desktop_shell *shell,
1315 struct workspace *from,
1316 struct workspace *to)
1318 struct weston_output *output;
1322 if (index > shell->workspaces.current)
1327 shell->workspaces.current = index;
1329 shell->workspaces.anim_dir = dir;
1330 shell->workspaces.anim_from = from;
1331 shell->workspaces.anim_to = to;
1332 shell->workspaces.anim_current = 0.0;
1333 shell->workspaces.anim_timestamp = 0;
1335 output = container_of(shell->compositor->output_list.next,
1336 struct weston_output, link);
1337 wl_list_insert(&output->animation_list,
1338 &shell->workspaces.animation.link);
1340 wl_list_insert(from->layer.link.prev, &to->layer.link);
1342 workspace_translate_in(to, 0);
1344 restore_focus_state(shell, to);
1346 weston_compositor_schedule_repaint(shell->compositor);
1350 update_workspace(struct desktop_shell *shell, unsigned int index,
1351 struct workspace *from, struct workspace *to)
1353 shell->workspaces.current = index;
1354 wl_list_insert(&from->layer.link, &to->layer.link);
1355 wl_list_remove(&from->layer.link);
1359 change_workspace(struct desktop_shell *shell, unsigned int index)
1361 struct workspace *from;
1362 struct workspace *to;
1363 struct focus_state *state;
1365 if (index == shell->workspaces.current)
1368 /* Don't change workspace when there is any fullscreen surfaces. */
1369 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1372 from = get_current_workspace(shell);
1373 to = get_workspace(shell, index);
1375 if (shell->workspaces.anim_from == to &&
1376 shell->workspaces.anim_to == from) {
1377 restore_focus_state(shell, to);
1378 reverse_workspace_change_animation(shell, index, from, to);
1382 if (shell->workspaces.anim_to != NULL)
1383 finish_workspace_change_animation(shell,
1384 shell->workspaces.anim_from,
1385 shell->workspaces.anim_to);
1387 restore_focus_state(shell, to);
1389 if (shell->focus_animation_type != ANIMATION_NONE) {
1390 wl_list_for_each(state, &from->focus_list, link)
1391 if (state->keyboard_focus)
1392 animate_focus_change(shell, from,
1393 get_default_view(state->keyboard_focus), NULL);
1395 wl_list_for_each(state, &to->focus_list, link)
1396 if (state->keyboard_focus)
1397 animate_focus_change(shell, to,
1398 NULL, get_default_view(state->keyboard_focus));
1401 if (workspace_is_empty(to) && workspace_is_empty(from))
1402 update_workspace(shell, index, from, to);
1404 animate_workspace_change(shell, index, from, to);
1408 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1410 struct wl_list *list = &ws->layer.view_list.link;
1413 if (wl_list_empty(list))
1418 if (e->next != list)
1421 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1425 surface_keyboard_focus_lost(struct weston_surface *surface)
1427 struct weston_compositor *compositor = surface->compositor;
1428 struct weston_seat *seat;
1429 struct weston_surface *focus;
1431 wl_list_for_each(seat, &compositor->seat_list, link) {
1432 struct weston_keyboard *keyboard =
1433 weston_seat_get_keyboard(seat);
1438 focus = weston_surface_get_main_surface(keyboard->focus);
1439 if (focus == surface)
1440 weston_keyboard_set_focus(keyboard, NULL);
1445 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1446 struct weston_seat *seat,
1449 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1450 struct weston_surface *surface;
1451 struct weston_view *view;
1452 struct shell_surface *shsurf;
1453 struct workspace *from;
1454 struct workspace *to;
1455 struct focus_state *state;
1457 surface = weston_surface_get_main_surface(keyboard->focus);
1458 view = get_default_view(surface);
1460 index == shell->workspaces.current ||
1461 is_focus_view(view))
1464 from = get_current_workspace(shell);
1465 to = get_workspace(shell, index);
1467 weston_layer_entry_remove(&view->layer_link);
1468 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1470 shsurf = get_shell_surface(surface);
1472 shell_surface_update_child_surface_layers(shsurf);
1474 replace_focus_state(shell, to, seat);
1475 drop_focus_state(shell, from, surface);
1477 if (shell->workspaces.anim_from == to &&
1478 shell->workspaces.anim_to == from) {
1479 wl_list_remove(&to->layer.link);
1480 wl_list_insert(from->layer.link.prev, &to->layer.link);
1482 reverse_workspace_change_animation(shell, index, from, to);
1487 if (shell->workspaces.anim_to != NULL)
1488 finish_workspace_change_animation(shell,
1489 shell->workspaces.anim_from,
1490 shell->workspaces.anim_to);
1492 if (workspace_is_empty(from) &&
1493 workspace_has_only(to, surface))
1494 update_workspace(shell, index, from, to);
1496 if (shsurf != NULL &&
1497 wl_list_empty(&shsurf->workspace_transform.link))
1498 wl_list_insert(&shell->workspaces.anim_sticky_list,
1499 &shsurf->workspace_transform.link);
1501 animate_workspace_change(shell, index, from, to);
1504 state = ensure_focus_state(shell, seat);
1506 focus_state_set_focus(state, surface);
1510 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1511 int touch_id, wl_fixed_t x, wl_fixed_t y)
1516 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1518 struct weston_touch_move_grab *move =
1519 (struct weston_touch_move_grab *) container_of(
1520 grab, struct shell_touch_grab, grab);
1525 if (grab->touch->num_tp == 0) {
1526 shell_touch_grab_end(&move->base);
1532 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1533 int touch_id, wl_fixed_t x, wl_fixed_t y)
1535 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1536 struct shell_surface *shsurf = move->base.shsurf;
1537 struct weston_surface *es;
1538 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1539 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1541 if (!shsurf || !move->active)
1544 es = shsurf->surface;
1546 weston_view_set_position(shsurf->view, dx, dy);
1548 weston_compositor_schedule_repaint(es->compositor);
1552 touch_move_grab_frame(struct weston_touch_grab *grab)
1557 touch_move_grab_cancel(struct weston_touch_grab *grab)
1559 struct weston_touch_move_grab *move =
1560 (struct weston_touch_move_grab *) container_of(
1561 grab, struct shell_touch_grab, grab);
1563 shell_touch_grab_end(&move->base);
1567 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1568 touch_move_grab_down,
1570 touch_move_grab_motion,
1571 touch_move_grab_frame,
1572 touch_move_grab_cancel,
1576 surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
1578 struct weston_touch_move_grab *move;
1583 if (shsurf->state.fullscreen || shsurf->state.maximized)
1586 move = malloc(sizeof *move);
1591 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1593 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1596 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1603 noop_grab_focus(struct weston_pointer_grab *grab)
1608 noop_grab_axis(struct weston_pointer_grab *grab,
1609 uint32_t time, struct weston_pointer_axis_event *event)
1614 noop_grab_axis_source(struct weston_pointer_grab *grab,
1620 noop_grab_frame(struct weston_pointer_grab *grab)
1625 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1627 struct shell_surface *shsurf = move->base.shsurf;
1628 struct weston_pointer *pointer = move->base.grab.pointer;
1630 const int safety = 50;
1631 pixman_rectangle32_t area;
1633 x = wl_fixed_to_int(pointer->x + move->dx);
1634 y = wl_fixed_to_int(pointer->y + move->dy);
1636 if (shsurf->shell->panel_position ==
1637 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
1638 get_output_work_area(shsurf->shell,
1639 shsurf->surface->output,
1642 bottom = y + shsurf->geometry.height + shsurf->geometry.y;
1643 if (bottom - safety < area.y)
1644 y = area.y + safety - shsurf->geometry.height
1645 - shsurf->geometry.y;
1647 if (move->client_initiated &&
1648 y + shsurf->geometry.y < area.y)
1649 y = area.y - shsurf->geometry.y;
1658 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1659 struct weston_pointer_motion_event *event)
1661 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1662 struct weston_pointer *pointer = grab->pointer;
1663 struct shell_surface *shsurf = move->base.shsurf;
1666 weston_pointer_move(pointer, event);
1670 constrain_position(move, &cx, &cy);
1672 weston_view_set_position(shsurf->view, cx, cy);
1674 weston_compositor_schedule_repaint(shsurf->surface->compositor);
1678 move_grab_button(struct weston_pointer_grab *grab,
1679 uint32_t time, uint32_t button, uint32_t state_w)
1681 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1683 struct weston_pointer *pointer = grab->pointer;
1684 enum wl_pointer_button_state state = state_w;
1686 if (pointer->button_count == 0 &&
1687 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1688 shell_grab_end(shell_grab);
1694 move_grab_cancel(struct weston_pointer_grab *grab)
1696 struct shell_grab *shell_grab =
1697 container_of(grab, struct shell_grab, grab);
1699 shell_grab_end(shell_grab);
1703 static const struct weston_pointer_grab_interface move_grab_interface = {
1708 noop_grab_axis_source,
1714 surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
1715 bool client_initiated)
1717 struct weston_move_grab *move;
1722 shsurf = find_toplevel_surface(shsurf);
1724 if (shsurf->grabbed ||
1725 shsurf->state.fullscreen || shsurf->state.maximized)
1728 move = malloc(sizeof *move);
1732 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1734 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1736 move->client_initiated = client_initiated;
1738 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1739 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
1745 common_surface_move(struct wl_resource *resource,
1746 struct wl_resource *seat_resource, uint32_t serial)
1748 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1749 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1750 struct weston_touch *touch = weston_seat_get_touch(seat);
1751 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1752 struct weston_surface *surface;
1756 pointer->button_count > 0 &&
1757 pointer->grab_serial == serial) {
1758 surface = weston_surface_get_main_surface(pointer->focus->surface);
1759 if ((surface == shsurf->surface) &&
1760 (surface_move(shsurf, pointer, true) < 0))
1761 wl_resource_post_no_memory(resource);
1764 touch->grab_serial == serial) {
1765 surface = weston_surface_get_main_surface(touch->focus->surface);
1766 if ((surface == shsurf->surface) &&
1767 (surface_touch_move(shsurf, touch) < 0))
1768 wl_resource_post_no_memory(resource);
1773 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1774 struct wl_resource *seat_resource, uint32_t serial)
1776 common_surface_move(resource, seat_resource, serial);
1779 struct weston_resize_grab {
1780 struct shell_grab base;
1782 int32_t width, height;
1786 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1787 struct weston_pointer_motion_event *event)
1789 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1790 struct weston_pointer *pointer = grab->pointer;
1791 struct shell_surface *shsurf = resize->base.shsurf;
1792 int32_t width, height;
1793 wl_fixed_t from_x, from_y;
1794 wl_fixed_t to_x, to_y;
1796 weston_pointer_move(pointer, event);
1801 weston_view_from_global_fixed(shsurf->view,
1802 pointer->grab_x, pointer->grab_y,
1804 weston_view_from_global_fixed(shsurf->view,
1805 pointer->x, pointer->y, &to_x, &to_y);
1807 width = resize->width;
1808 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1809 width += wl_fixed_to_int(from_x - to_x);
1810 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1811 width += wl_fixed_to_int(to_x - from_x);
1814 height = resize->height;
1815 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1816 height += wl_fixed_to_int(from_y - to_y);
1817 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1818 height += wl_fixed_to_int(to_y - from_y);
1825 shsurf->client->send_configure(shsurf->surface, width, height);
1829 send_configure(struct weston_surface *surface, int32_t width, int32_t height)
1831 struct shell_surface *shsurf = get_shell_surface(surface);
1835 if (shsurf->resource)
1836 wl_shell_surface_send_configure(shsurf->resource,
1837 shsurf->resize_edges,
1841 static const struct weston_shell_client shell_client = {
1847 resize_grab_button(struct weston_pointer_grab *grab,
1848 uint32_t time, uint32_t button, uint32_t state_w)
1850 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1851 struct weston_pointer *pointer = grab->pointer;
1852 enum wl_pointer_button_state state = state_w;
1854 if (pointer->button_count == 0 &&
1855 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1856 shell_grab_end(&resize->base);
1862 resize_grab_cancel(struct weston_pointer_grab *grab)
1864 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1866 shell_grab_end(&resize->base);
1870 static const struct weston_pointer_grab_interface resize_grab_interface = {
1875 noop_grab_axis_source,
1881 * Returns the bounding box of a surface and all its sub-surfaces,
1882 * in surface-local coordinates. */
1884 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1885 int32_t *y, int32_t *w, int32_t *h) {
1886 pixman_region32_t region;
1887 pixman_box32_t *box;
1888 struct weston_subsurface *subsurface;
1890 pixman_region32_init_rect(®ion, 0, 0,
1894 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1895 pixman_region32_union_rect(®ion, ®ion,
1896 subsurface->position.x,
1897 subsurface->position.y,
1898 subsurface->surface->width,
1899 subsurface->surface->height);
1902 box = pixman_region32_extents(®ion);
1908 *w = box->x2 - box->x1;
1910 *h = box->y2 - box->y1;
1912 pixman_region32_fini(®ion);
1916 surface_resize(struct shell_surface *shsurf,
1917 struct weston_pointer *pointer, uint32_t edges)
1919 struct weston_resize_grab *resize;
1920 const unsigned resize_topbottom =
1921 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1922 const unsigned resize_leftright =
1923 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1924 const unsigned resize_any = resize_topbottom | resize_leftright;
1926 if (shsurf->grabbed ||
1927 shsurf->state.fullscreen || shsurf->state.maximized)
1930 /* Check for invalid edge combinations. */
1931 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1932 (edges & resize_topbottom) == resize_topbottom ||
1933 (edges & resize_leftright) == resize_leftright)
1936 resize = malloc(sizeof *resize);
1940 resize->edges = edges;
1942 resize->width = shsurf->geometry.width;
1943 resize->height = shsurf->geometry.height;
1945 shsurf->resize_edges = edges;
1946 shell_surface_state_changed(shsurf);
1947 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1954 common_surface_resize(struct wl_resource *resource,
1955 struct wl_resource *seat_resource, uint32_t serial,
1958 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1959 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
1960 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1961 struct weston_surface *surface;
1964 pointer->button_count == 0 ||
1965 pointer->grab_serial != serial ||
1966 pointer->focus == NULL)
1969 surface = weston_surface_get_main_surface(pointer->focus->surface);
1970 if (surface != shsurf->surface)
1973 if (surface_resize(shsurf, pointer, edges) < 0)
1974 wl_resource_post_no_memory(resource);
1978 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1979 struct wl_resource *seat_resource, uint32_t serial,
1982 common_surface_resize(resource, seat_resource, serial, edges);
1986 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1988 struct shell_grab *grab = (struct shell_grab *) base;
1989 struct weston_pointer *pointer = base->pointer;
1990 struct weston_view *view;
1993 view = weston_compositor_pick_view(pointer->seat->compositor,
1994 pointer->x, pointer->y,
1997 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1998 shell_grab_end(grab);
2004 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2005 struct weston_pointer_motion_event *event)
2007 weston_pointer_move(grab->pointer, event);
2011 busy_cursor_grab_button(struct weston_pointer_grab *base,
2012 uint32_t time, uint32_t button, uint32_t state)
2014 struct shell_grab *grab = (struct shell_grab *) base;
2015 struct shell_surface *shsurf = grab->shsurf;
2016 struct weston_pointer *pointer = grab->grab.pointer;
2017 struct weston_seat *seat = pointer->seat;
2019 if (shsurf && button == BTN_LEFT && state) {
2020 activate(shsurf->shell, shsurf->view, seat,
2021 WESTON_ACTIVATE_FLAG_CONFIGURE);
2022 surface_move(shsurf, pointer, false);
2023 } else if (shsurf && button == BTN_RIGHT && state) {
2024 activate(shsurf->shell, shsurf->view, seat,
2025 WESTON_ACTIVATE_FLAG_CONFIGURE);
2026 surface_rotate(shsurf, pointer);
2031 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
2033 struct shell_grab *grab = (struct shell_grab *) base;
2035 shell_grab_end(grab);
2039 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
2040 busy_cursor_grab_focus,
2041 busy_cursor_grab_motion,
2042 busy_cursor_grab_button,
2044 noop_grab_axis_source,
2046 busy_cursor_grab_cancel,
2050 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2052 struct shell_grab *grab;
2054 if (pointer->grab->interface == &busy_cursor_grab_interface)
2057 grab = malloc(sizeof *grab);
2061 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2062 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2063 /* Mark the shsurf as ungrabbed so that button binding is able
2065 shsurf->grabbed = 0;
2069 end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
2071 struct shell_grab *grab;
2072 struct weston_seat *seat;
2074 wl_list_for_each(seat, &compositor->seat_list, link) {
2075 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2080 grab = (struct shell_grab *) pointer->grab;
2081 if (grab->grab.interface == &busy_cursor_grab_interface &&
2082 grab->shsurf->resource &&
2083 wl_resource_get_client(grab->shsurf->resource) == client) {
2084 shell_grab_end(grab);
2091 handle_shell_client_destroy(struct wl_listener *listener, void *data);
2094 xdg_ping_timeout_handler(void *data)
2096 struct shell_client *sc = data;
2097 struct weston_seat *seat;
2098 struct shell_surface *shsurf;
2100 /* Client is not responding */
2101 sc->unresponsive = 1;
2102 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
2103 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2107 !pointer->focus->surface->resource)
2110 shsurf = get_shell_surface(pointer->focus->surface);
2111 if (shsurf && shsurf->resource &&
2112 wl_resource_get_client(shsurf->resource) == sc->client)
2113 set_busy_cursor(shsurf, pointer);
2120 handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
2122 struct weston_compositor *compositor = shsurf->shell->compositor;
2123 struct shell_client *sc = shsurf->owner;
2124 struct wl_event_loop *loop;
2125 static const int ping_timeout = 200;
2127 if (sc->unresponsive) {
2128 xdg_ping_timeout_handler(sc);
2132 sc->ping_serial = serial;
2133 loop = wl_display_get_event_loop(compositor->wl_display);
2134 if (sc->ping_timer == NULL)
2136 wl_event_loop_add_timer(loop,
2137 xdg_ping_timeout_handler, sc);
2138 if (sc->ping_timer == NULL)
2141 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
2143 if (shell_surface_is_xdg_surface(shsurf) ||
2144 shell_surface_is_xdg_popup(shsurf))
2145 xdg_shell_send_ping(sc->resource, serial);
2146 else if (shell_surface_is_wl_shell_surface(shsurf)
2147 && shsurf->resource)
2148 wl_shell_surface_send_ping(shsurf->resource, serial);
2152 ping_handler(struct weston_surface *surface, uint32_t serial)
2154 struct shell_surface *shsurf = get_shell_surface(surface);
2158 if (!shsurf->resource)
2160 if (shsurf->surface == shsurf->shell->grab_surface)
2165 handle_xdg_ping(shsurf, serial);
2169 handle_pointer_focus(struct wl_listener *listener, void *data)
2171 struct weston_pointer *pointer = data;
2172 struct weston_view *view = pointer->focus;
2173 struct weston_compositor *compositor;
2179 compositor = view->surface->compositor;
2180 serial = wl_display_next_serial(compositor->wl_display);
2181 ping_handler(view->surface, serial);
2185 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
2187 if (--shsurf->focus_count == 0)
2188 shell_surface_state_changed(shsurf);
2192 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
2194 if (shsurf->focus_count++ == 0)
2195 shell_surface_state_changed(shsurf);
2199 handle_keyboard_focus(struct wl_listener *listener, void *data)
2201 struct weston_keyboard *keyboard = data;
2202 struct shell_seat *seat = get_shell_seat(keyboard->seat);
2204 if (seat->focused_surface) {
2205 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2207 shell_surface_lose_keyboard_focus(shsurf);
2210 seat->focused_surface = keyboard->focus;
2212 if (seat->focused_surface) {
2213 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
2215 shell_surface_gain_keyboard_focus(shsurf);
2220 shell_client_pong(struct shell_client *sc, uint32_t serial)
2222 if (sc->ping_serial != serial)
2225 sc->unresponsive = 0;
2226 end_busy_cursor(sc->shell->compositor, sc->client);
2228 if (sc->ping_timer) {
2229 wl_event_source_remove(sc->ping_timer);
2230 sc->ping_timer = NULL;
2236 shell_surface_pong(struct wl_client *client,
2237 struct wl_resource *resource, uint32_t serial)
2239 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2240 struct shell_client *sc = shsurf->owner;
2242 shell_client_pong(sc, serial);
2246 set_title(struct shell_surface *shsurf, const char *title)
2248 free(shsurf->title);
2249 shsurf->title = strdup(title);
2250 shsurf->surface->timeline.force_refresh = 1;
2254 set_pid(struct shell_surface *shsurf, pid_t pid)
2256 /* We have no use for it */
2260 set_type(struct shell_surface *shsurf, enum shell_surface_type t)
2263 shsurf->surface->timeline.force_refresh = 1;
2267 set_window_geometry(struct shell_surface *shsurf,
2268 int32_t x, int32_t y, int32_t width, int32_t height)
2270 shsurf->next_geometry.x = x;
2271 shsurf->next_geometry.y = y;
2272 shsurf->next_geometry.width = width;
2273 shsurf->next_geometry.height = height;
2274 shsurf->has_next_geometry = true;
2278 shell_surface_set_title(struct wl_client *client,
2279 struct wl_resource *resource, const char *title)
2281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2283 set_title(shsurf, title);
2287 shell_surface_set_class(struct wl_client *client,
2288 struct wl_resource *resource, const char *class)
2290 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2292 free(shsurf->class);
2293 shsurf->class = strdup(class);
2294 shsurf->surface->timeline.force_refresh = 1;
2298 restore_output_mode(struct weston_output *output)
2300 if (output && output->original_mode)
2301 weston_output_mode_switch_to_native(output);
2305 restore_all_output_modes(struct weston_compositor *compositor)
2307 struct weston_output *output;
2309 wl_list_for_each(output, &compositor->output_list, link)
2310 restore_output_mode(output);
2313 /* The surface will be inserted into the list immediately after the link
2314 * returned by this function (i.e. will be stacked immediately above the
2315 * returned link). */
2316 static struct weston_layer_entry *
2317 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2319 struct workspace *ws;
2320 struct weston_view *parent;
2322 switch (shsurf->type) {
2323 case SHELL_SURFACE_XWAYLAND:
2324 return &shsurf->shell->fullscreen_layer.view_list;
2326 case SHELL_SURFACE_NONE:
2329 case SHELL_SURFACE_POPUP:
2330 case SHELL_SURFACE_TOPLEVEL:
2331 if (shsurf->state.fullscreen && !shsurf->state.lowered) {
2332 return &shsurf->shell->fullscreen_layer.view_list;
2333 } else if (shsurf->parent) {
2334 /* Move the surface to its parent layer so
2335 * that surfaces which are transient for
2336 * fullscreen surfaces don't get hidden by the
2337 * fullscreen surfaces. */
2339 /* TODO: Handle a parent with multiple views */
2340 parent = get_default_view(shsurf->parent);
2342 return container_of(parent->layer_link.link.prev,
2343 struct weston_layer_entry, link);
2346 /* Move the surface to a normal workspace layer so that surfaces
2347 * which were previously fullscreen or transient are no longer
2348 * rendered on top. */
2349 ws = get_current_workspace(shsurf->shell);
2350 return &ws->layer.view_list;
2353 assert(0 && "Unknown shell surface type");
2357 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2359 struct shell_surface *child;
2360 struct weston_layer_entry *prev;
2362 /* Move the child layers to the same workspace as shsurf. They will be
2363 * stacked above shsurf. */
2364 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2365 if (shsurf->view->layer_link.link.prev != &child->view->layer_link.link) {
2366 weston_view_damage_below(child->view);
2367 weston_view_geometry_dirty(child->view);
2368 prev = container_of(shsurf->view->layer_link.link.prev,
2369 struct weston_layer_entry, link);
2370 weston_layer_entry_remove(&child->view->layer_link);
2371 weston_layer_entry_insert(prev,
2372 &child->view->layer_link);
2373 weston_view_geometry_dirty(child->view);
2374 weston_surface_damage(child->surface);
2376 /* Recurse. We don’t expect this to recurse very far (if
2377 * at all) because that would imply we have transient
2378 * (or popup) children of transient surfaces, which
2379 * would be unusual. */
2380 shell_surface_update_child_surface_layers(child);
2385 /* Update the surface’s layer. Mark both the old and new views as having dirty
2386 * geometry to ensure the changes are redrawn.
2388 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2389 * as this surface. */
2391 shell_surface_update_layer(struct shell_surface *shsurf)
2393 struct weston_layer_entry *new_layer_link;
2395 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2397 if (new_layer_link == NULL)
2399 if (new_layer_link == &shsurf->view->layer_link)
2402 weston_view_geometry_dirty(shsurf->view);
2403 weston_layer_entry_remove(&shsurf->view->layer_link);
2404 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
2405 weston_view_geometry_dirty(shsurf->view);
2406 weston_surface_damage(shsurf->surface);
2408 shell_surface_update_child_surface_layers(shsurf);
2412 shell_surface_set_parent(struct shell_surface *shsurf,
2413 struct weston_surface *parent)
2415 shsurf->parent = parent;
2417 wl_list_remove(&shsurf->children_link);
2418 wl_list_init(&shsurf->children_link);
2420 /* Insert into the parent surface’s child list. */
2421 if (parent != NULL) {
2422 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2423 if (parent_shsurf != NULL)
2424 wl_list_insert(&parent_shsurf->children_list,
2425 &shsurf->children_link);
2430 shell_surface_set_output(struct shell_surface *shsurf,
2431 struct weston_output *output)
2433 struct weston_surface *es = shsurf->surface;
2435 /* get the default output, if the client set it as NULL
2436 check whether the ouput is available */
2438 shsurf->output = output;
2439 else if (es->output)
2440 shsurf->output = es->output;
2442 shsurf->output = get_default_output(es->compositor);
2446 surface_clear_next_states(struct shell_surface *shsurf)
2448 shsurf->next_state.maximized = false;
2449 shsurf->next_state.fullscreen = false;
2451 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2452 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2453 shsurf->state_changed = true;
2457 set_toplevel(struct shell_surface *shsurf)
2459 shell_surface_set_parent(shsurf, NULL);
2460 surface_clear_next_states(shsurf);
2461 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
2463 /* The layer_link is updated in set_surface_type(),
2464 * called from configure. */
2468 shell_surface_set_toplevel(struct wl_client *client,
2469 struct wl_resource *resource)
2471 struct shell_surface *surface = wl_resource_get_user_data(resource);
2473 set_toplevel(surface);
2477 set_transient(struct shell_surface *shsurf,
2478 struct weston_surface *parent, int x, int y, uint32_t flags)
2480 assert(parent != NULL);
2482 shell_surface_set_parent(shsurf, parent);
2484 surface_clear_next_states(shsurf);
2486 shsurf->transient.x = x;
2487 shsurf->transient.y = y;
2488 shsurf->transient.flags = flags;
2490 shsurf->next_state.relative = true;
2491 shsurf->state_changed = true;
2492 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
2494 /* The layer_link is updated in set_surface_type(),
2495 * called from configure. */
2499 shell_surface_set_transient(struct wl_client *client,
2500 struct wl_resource *resource,
2501 struct wl_resource *parent_resource,
2502 int x, int y, uint32_t flags)
2504 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2505 struct weston_surface *parent =
2506 wl_resource_get_user_data(parent_resource);
2508 set_transient(shsurf, parent, x, y, flags);
2512 set_fullscreen(struct shell_surface *shsurf,
2515 struct weston_output *output)
2517 shell_surface_set_output(shsurf, output);
2518 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
2520 shsurf->fullscreen_output = shsurf->output;
2521 shsurf->fullscreen.type = method;
2522 shsurf->fullscreen.framerate = framerate;
2524 send_configure_for_surface(shsurf);
2528 weston_view_set_initial_position(struct weston_view *view,
2529 struct desktop_shell *shell);
2532 unset_fullscreen(struct shell_surface *shsurf)
2534 /* Unset the fullscreen output, driver configuration and transforms. */
2535 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2536 shell_surface_is_top_fullscreen(shsurf)) {
2537 restore_output_mode(shsurf->fullscreen_output);
2540 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2541 shsurf->fullscreen.framerate = 0;
2543 wl_list_remove(&shsurf->fullscreen.transform.link);
2544 wl_list_init(&shsurf->fullscreen.transform.link);
2546 if (shsurf->fullscreen.black_view)
2547 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2548 shsurf->fullscreen.black_view = NULL;
2550 if (shsurf->saved_position_valid)
2551 weston_view_set_position(shsurf->view,
2552 shsurf->saved_x, shsurf->saved_y);
2554 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2556 if (shsurf->saved_rotation_valid) {
2557 wl_list_insert(&shsurf->view->geometry.transformation_list,
2558 &shsurf->rotation.transform.link);
2559 shsurf->saved_rotation_valid = false;
2562 /* Layer is updated in set_surface_type(). */
2566 shell_surface_set_fullscreen(struct wl_client *client,
2567 struct wl_resource *resource,
2570 struct wl_resource *output_resource)
2572 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2573 struct weston_output *output;
2575 if (shsurf->fullscreen_output == shsurf->output &&
2576 shsurf->fullscreen.type == method &&
2577 shsurf->fullscreen.framerate == framerate) {
2578 send_configure_for_surface(shsurf);
2582 if (output_resource)
2583 output = wl_resource_get_user_data(output_resource);
2587 shell_surface_set_parent(shsurf, NULL);
2589 surface_clear_next_states(shsurf);
2590 shsurf->next_state.fullscreen = true;
2591 shsurf->state_changed = true;
2592 set_fullscreen(shsurf, method, framerate, output);
2596 set_popup(struct shell_surface *shsurf,
2597 struct weston_surface *parent,
2598 struct weston_seat *seat,
2603 assert(parent != NULL);
2605 shsurf->popup.shseat = get_shell_seat(seat);
2606 shsurf->popup.serial = serial;
2607 shsurf->popup.x = x;
2608 shsurf->popup.y = y;
2610 set_type(shsurf, SHELL_SURFACE_POPUP);
2614 shell_surface_set_popup(struct wl_client *client,
2615 struct wl_resource *resource,
2616 struct wl_resource *seat_resource,
2618 struct wl_resource *parent_resource,
2619 int32_t x, int32_t y, uint32_t flags)
2621 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2622 struct weston_surface *parent =
2623 wl_resource_get_user_data(parent_resource);
2625 shell_surface_set_parent(shsurf, parent);
2627 surface_clear_next_states(shsurf);
2630 wl_resource_get_user_data(seat_resource),
2635 unset_maximized(struct shell_surface *shsurf)
2637 /* undo all maximized things here */
2638 shsurf->output = get_default_output(shsurf->surface->compositor);
2640 if (shsurf->saved_position_valid)
2641 weston_view_set_position(shsurf->view,
2642 shsurf->saved_x, shsurf->saved_y);
2644 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2646 if (shsurf->saved_rotation_valid) {
2647 wl_list_insert(&shsurf->view->geometry.transformation_list,
2648 &shsurf->rotation.transform.link);
2649 shsurf->saved_rotation_valid = false;
2652 /* Layer is updated in set_surface_type(). */
2656 set_minimized(struct weston_surface *surface)
2658 struct shell_surface *shsurf;
2659 struct workspace *current_ws;
2660 struct weston_view *view;
2662 view = get_default_view(surface);
2666 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2668 shsurf = get_shell_surface(surface);
2669 current_ws = get_current_workspace(shsurf->shell);
2671 weston_layer_entry_remove(&view->layer_link);
2672 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2674 drop_focus_state(shsurf->shell, current_ws, view->surface);
2675 surface_keyboard_focus_lost(surface);
2677 shell_surface_update_child_surface_layers(shsurf);
2678 weston_view_damage_below(view);
2682 shell_surface_set_maximized(struct wl_client *client,
2683 struct wl_resource *resource,
2684 struct wl_resource *output_resource)
2686 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2687 struct weston_output *output;
2689 surface_clear_next_states(shsurf);
2690 shsurf->next_state.maximized = true;
2691 shsurf->state_changed = true;
2693 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
2694 shell_surface_set_parent(shsurf, NULL);
2696 if (output_resource)
2697 output = wl_resource_get_user_data(output_resource);
2701 shell_surface_set_output(shsurf, output);
2703 send_configure_for_surface(shsurf);
2706 /* This is only ever called from set_surface_type(), so there’s no need to
2707 * update layer_links here, since they’ll be updated when we return. */
2709 reset_surface_type(struct shell_surface *surface)
2711 if (surface->state.fullscreen)
2712 unset_fullscreen(surface);
2713 if (surface->state.maximized)
2714 unset_maximized(surface);
2720 set_full_output(struct shell_surface *shsurf)
2722 shsurf->saved_x = shsurf->view->geometry.x;
2723 shsurf->saved_y = shsurf->view->geometry.y;
2724 shsurf->saved_position_valid = true;
2726 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2727 wl_list_remove(&shsurf->rotation.transform.link);
2728 wl_list_init(&shsurf->rotation.transform.link);
2729 weston_view_geometry_dirty(shsurf->view);
2730 shsurf->saved_rotation_valid = true;
2735 set_surface_type(struct shell_surface *shsurf)
2737 struct weston_surface *pes = shsurf->parent;
2738 struct weston_view *pev = get_default_view(pes);
2740 reset_surface_type(shsurf);
2742 shsurf->state = shsurf->next_state;
2743 shsurf->state_changed = false;
2745 switch (shsurf->type) {
2746 case SHELL_SURFACE_TOPLEVEL:
2747 if (shsurf->state.maximized || shsurf->state.fullscreen) {
2748 set_full_output(shsurf);
2749 } else if (shsurf->state.relative && pev) {
2750 weston_view_set_position(shsurf->view,
2751 pev->geometry.x + shsurf->transient.x,
2752 pev->geometry.y + shsurf->transient.y);
2756 case SHELL_SURFACE_XWAYLAND:
2757 weston_view_set_position(shsurf->view, shsurf->transient.x,
2758 shsurf->transient.y);
2761 case SHELL_SURFACE_POPUP:
2762 case SHELL_SURFACE_NONE:
2767 /* Update the surface’s layer. */
2768 shell_surface_update_layer(shsurf);
2771 static struct desktop_shell *
2772 shell_surface_get_shell(struct shell_surface *shsurf)
2774 return shsurf->shell;
2778 black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2780 struct weston_view *fs_view = surface->committed_private;
2781 struct weston_surface *fs_surface = fs_view->surface;
2786 n = snprintf(buf, len, "black background surface for ");
2794 if (fs_surface->get_label)
2795 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2797 ret = snprintf(buf + n, rem, "<unknown>");
2806 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
2808 static struct weston_view *
2809 create_black_surface(struct weston_compositor *ec,
2810 struct weston_view *fs_view,
2811 float x, float y, int w, int h)
2813 struct weston_surface *surface = NULL;
2814 struct weston_view *view;
2816 surface = weston_surface_create(ec);
2817 if (surface == NULL) {
2818 weston_log("no memory\n");
2821 view = weston_view_create(surface);
2822 if (surface == NULL) {
2823 weston_log("no memory\n");
2824 weston_surface_destroy(surface);
2828 surface->committed = black_surface_committed;
2829 surface->committed_private = fs_view;
2830 weston_surface_set_label_func(surface, black_surface_get_label);
2831 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2832 pixman_region32_fini(&surface->opaque);
2833 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2834 pixman_region32_fini(&surface->input);
2835 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2837 weston_surface_set_size(surface, w, h);
2838 weston_view_set_position(view, x, y);
2844 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2846 struct weston_output *output = shsurf->fullscreen_output;
2848 assert(shsurf->state.fullscreen);
2850 if (!shsurf->fullscreen.black_view)
2851 shsurf->fullscreen.black_view =
2852 create_black_surface(shsurf->surface->compositor,
2854 output->x, output->y,
2858 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2859 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2860 weston_layer_entry_insert(&shsurf->view->layer_link,
2861 &shsurf->fullscreen.black_view->layer_link);
2862 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2863 weston_surface_damage(shsurf->surface);
2865 shsurf->fullscreen.black_view->is_mapped = true;
2866 shsurf->state.lowered = false;
2869 /* Create black surface and append it to the associated fullscreen surface.
2870 * Handle size dismatch and positioning according to the method. */
2872 shell_configure_fullscreen(struct shell_surface *shsurf)
2874 struct weston_output *output = shsurf->fullscreen_output;
2875 struct weston_surface *surface = shsurf->surface;
2876 struct weston_matrix *matrix;
2877 float scale, output_aspect, surface_aspect, x, y;
2878 int32_t surf_x, surf_y, surf_width, surf_height;
2880 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2881 restore_output_mode(output);
2883 /* Reverse the effect of lower_fullscreen_layer() */
2884 weston_layer_entry_remove(&shsurf->view->layer_link);
2885 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list, &shsurf->view->layer_link);
2887 shell_ensure_fullscreen_black_view(shsurf);
2889 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
2890 &surf_width, &surf_height);
2892 switch (shsurf->fullscreen.type) {
2893 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
2894 if (surface->buffer_ref.buffer)
2895 center_on_output(shsurf->view, shsurf->fullscreen_output);
2897 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
2898 /* 1:1 mapping between surface and output dimensions */
2899 if (output->width == surf_width &&
2900 output->height == surf_height) {
2901 weston_view_set_position(shsurf->view,
2903 output->y - surf_y);
2907 matrix = &shsurf->fullscreen.transform.matrix;
2908 weston_matrix_init(matrix);
2910 output_aspect = (float) output->width /
2911 (float) output->height;
2912 /* XXX: Use surf_width and surf_height here? */
2913 surface_aspect = (float) surface->width /
2914 (float) surface->height;
2915 if (output_aspect < surface_aspect)
2916 scale = (float) output->width /
2919 scale = (float) output->height /
2920 (float) surf_height;
2922 weston_matrix_scale(matrix, scale, scale, 1);
2923 wl_list_remove(&shsurf->fullscreen.transform.link);
2924 wl_list_insert(&shsurf->view->geometry.transformation_list,
2925 &shsurf->fullscreen.transform.link);
2926 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2927 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
2928 weston_view_set_position(shsurf->view, x, y);
2931 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
2932 if (shell_surface_is_top_fullscreen(shsurf)) {
2933 struct weston_mode mode = {0,
2934 surf_width * surface->buffer_viewport.buffer.scale,
2935 surf_height * surface->buffer_viewport.buffer.scale,
2936 shsurf->fullscreen.framerate};
2938 if (weston_output_mode_switch_to_temporary(output, &mode,
2939 surface->buffer_viewport.buffer.scale) == 0) {
2940 weston_view_set_position(shsurf->view,
2942 output->y - surf_y);
2943 shsurf->fullscreen.black_view->surface->width = output->width;
2944 shsurf->fullscreen.black_view->surface->height = output->height;
2945 weston_view_set_position(shsurf->fullscreen.black_view,
2947 output->y - surf_y);
2950 weston_log("shell: Can't switch to temporary mode.\n");
2951 restore_output_mode(output);
2952 center_on_output(shsurf->view, output);
2956 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
2957 center_on_output(shsurf->view, output);
2965 shell_map_fullscreen(struct shell_surface *shsurf)
2967 shell_configure_fullscreen(shsurf);
2971 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2973 /* XXX: using the same fields for transient type */
2974 surface_clear_next_states(shsurf);
2975 shsurf->transient.x = x;
2976 shsurf->transient.y = y;
2977 shsurf->transient.flags = flags;
2979 shell_surface_set_parent(shsurf, NULL);
2981 set_type(shsurf, SHELL_SURFACE_XWAYLAND);
2982 shsurf->state_changed = true;
2986 shell_interface_set_fullscreen(struct shell_surface *shsurf,
2989 struct weston_output *output)
2991 surface_clear_next_states(shsurf);
2992 shsurf->next_state.fullscreen = true;
2993 shsurf->state_changed = true;
2995 set_fullscreen(shsurf, method, framerate, output);
2998 static struct weston_output *
2999 get_focused_output(struct weston_compositor *compositor)
3001 struct weston_seat *seat;
3002 struct weston_output *output = NULL;
3004 wl_list_for_each(seat, &compositor->seat_list, link) {
3005 struct weston_touch *touch = weston_seat_get_touch(seat);
3006 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3007 struct weston_keyboard *keyboard =
3008 weston_seat_get_keyboard(seat);
3010 /* Priority has touch focus, then pointer and
3011 * then keyboard focus. We should probably have
3012 * three for loops and check frist for touch,
3013 * then for pointer, etc. but unless somebody has some
3014 * objections, I think this is sufficient. */
3015 if (touch && touch->focus)
3016 output = touch->focus->output;
3017 else if (pointer && pointer->focus)
3018 output = pointer->focus->output;
3019 else if (keyboard && keyboard->focus)
3020 output = keyboard->focus->output;
3030 shell_interface_set_maximized(struct shell_surface *shsurf)
3032 struct weston_output *output;
3034 surface_clear_next_states(shsurf);
3035 shsurf->next_state.maximized = true;
3036 shsurf->state_changed = true;
3037 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3039 if (!weston_surface_is_mapped(shsurf->surface))
3040 output = get_focused_output(shsurf->surface->compositor);
3042 output = shsurf->surface->output;
3044 shell_surface_set_output(shsurf, output);
3045 send_configure_for_surface(shsurf);
3049 shell_interface_move(struct shell_surface *shsurf, struct weston_pointer *pointer)
3051 return surface_move(shsurf, pointer, true);
3055 shell_interface_resize(struct shell_surface *shsurf,
3056 struct weston_pointer *pointer,
3059 return surface_resize(shsurf, pointer, edges);
3062 static const struct weston_pointer_grab_interface popup_grab_interface;
3065 destroy_shell_seat(struct wl_listener *listener, void *data)
3067 struct shell_seat *shseat =
3068 container_of(listener,
3069 struct shell_seat, seat_destroy_listener);
3070 struct shell_surface *shsurf, *next;
3072 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
3073 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3074 shseat->popup_grab.client = NULL;
3076 wl_list_for_each_safe(shsurf, next,
3077 &shseat->popup_grab.surfaces_list,
3079 shsurf->popup.shseat = NULL;
3080 wl_list_init(&shsurf->popup.grab_link);
3084 wl_list_remove(&shseat->seat_destroy_listener.link);
3089 shell_seat_caps_changed(struct wl_listener *listener, void *data)
3091 struct weston_keyboard *keyboard;
3092 struct weston_pointer *pointer;
3093 struct shell_seat *seat;
3095 seat = container_of(listener, struct shell_seat, caps_changed_listener);
3096 keyboard = weston_seat_get_keyboard(seat->seat);
3097 pointer = weston_seat_get_pointer(seat->seat);
3100 wl_list_empty(&seat->keyboard_focus_listener.link)) {
3101 wl_signal_add(&keyboard->focus_signal,
3102 &seat->keyboard_focus_listener);
3103 } else if (!keyboard) {
3104 wl_list_remove(&seat->keyboard_focus_listener.link);
3105 wl_list_init(&seat->keyboard_focus_listener.link);
3109 wl_list_empty(&seat->pointer_focus_listener.link)) {
3110 wl_signal_add(&pointer->focus_signal,
3111 &seat->pointer_focus_listener);
3112 } else if (!pointer) {
3113 wl_list_remove(&seat->pointer_focus_listener.link);
3114 wl_list_init(&seat->pointer_focus_listener.link);
3118 static struct shell_seat *
3119 create_shell_seat(struct weston_seat *seat)
3121 struct shell_seat *shseat;
3123 shseat = calloc(1, sizeof *shseat);
3125 weston_log("no memory to allocate shell seat\n");
3129 shseat->seat = seat;
3130 wl_list_init(&shseat->popup_grab.surfaces_list);
3132 shseat->seat_destroy_listener.notify = destroy_shell_seat;
3133 wl_signal_add(&seat->destroy_signal,
3134 &shseat->seat_destroy_listener);
3136 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
3137 wl_list_init(&shseat->keyboard_focus_listener.link);
3139 shseat->pointer_focus_listener.notify = handle_pointer_focus;
3140 wl_list_init(&shseat->pointer_focus_listener.link);
3142 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
3143 wl_signal_add(&seat->updated_caps_signal,
3144 &shseat->caps_changed_listener);
3145 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
3150 static struct shell_seat *
3151 get_shell_seat(struct weston_seat *seat)
3153 struct wl_listener *listener;
3155 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
3156 assert(listener != NULL);
3158 return container_of(listener,
3159 struct shell_seat, seat_destroy_listener);
3163 popup_grab_focus(struct weston_pointer_grab *grab)
3165 struct weston_pointer *pointer = grab->pointer;
3166 struct weston_view *view;
3167 struct shell_seat *shseat =
3168 container_of(grab, struct shell_seat, popup_grab.grab);
3169 struct wl_client *client = shseat->popup_grab.client;
3172 view = weston_compositor_pick_view(pointer->seat->compositor,
3173 pointer->x, pointer->y,
3176 if (view && view->surface->resource &&
3177 wl_resource_get_client(view->surface->resource) == client) {
3178 weston_pointer_set_focus(pointer, view, sx, sy);
3180 weston_pointer_clear_focus(pointer);
3185 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
3186 struct weston_pointer_motion_event *event)
3188 struct weston_pointer *pointer = grab->pointer;
3189 struct wl_resource *resource;
3190 struct wl_list *resource_list;
3194 if (pointer->focus) {
3195 weston_pointer_motion_to_abs(pointer, event, &x, &y);
3196 weston_view_from_global_fixed(pointer->focus, x, y,
3197 &pointer->sx, &pointer->sy);
3200 weston_pointer_move(pointer, event);
3202 if (!pointer->focus_client)
3205 resource_list = &pointer->focus_client->pointer_resources;
3206 wl_resource_for_each(resource, resource_list) {
3207 weston_view_from_global_fixed(pointer->focus,
3208 pointer->x, pointer->y,
3210 wl_pointer_send_motion(resource, time, sx, sy);
3215 popup_grab_button(struct weston_pointer_grab *grab,
3216 uint32_t time, uint32_t button, uint32_t state_w)
3218 struct wl_resource *resource;
3219 struct shell_seat *shseat =
3220 container_of(grab, struct shell_seat, popup_grab.grab);
3221 struct wl_display *display = shseat->seat->compositor->wl_display;
3222 enum wl_pointer_button_state state = state_w;
3224 struct wl_list *resource_list = NULL;
3226 if (grab->pointer->focus_client)
3227 resource_list = &grab->pointer->focus_client->pointer_resources;
3228 if (resource_list && !wl_list_empty(resource_list)) {
3229 serial = wl_display_get_serial(display);
3230 wl_resource_for_each(resource, resource_list) {
3231 wl_pointer_send_button(resource, serial,
3232 time, button, state);
3234 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3235 (shseat->popup_grab.initial_up ||
3236 time - grab->pointer->grab_time > 500)) {
3237 popup_grab_end(grab->pointer);
3240 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
3241 shseat->popup_grab.initial_up = 1;
3245 popup_grab_axis(struct weston_pointer_grab *grab,
3247 struct weston_pointer_axis_event *event)
3249 weston_pointer_send_axis(grab->pointer, time, event);
3253 popup_grab_axis_source(struct weston_pointer_grab *grab, uint32_t source)
3255 weston_pointer_send_axis_source(grab->pointer, source);
3259 popup_grab_frame(struct weston_pointer_grab *grab)
3261 weston_pointer_send_frame(grab->pointer);
3265 popup_grab_cancel(struct weston_pointer_grab *grab)
3267 popup_grab_end(grab->pointer);
3270 static const struct weston_pointer_grab_interface popup_grab_interface = {
3275 popup_grab_axis_source,
3281 touch_popup_grab_down(struct weston_touch_grab *grab, uint32_t time,
3282 int touch_id, wl_fixed_t x, wl_fixed_t y)
3284 struct wl_resource *resource;
3285 struct shell_seat *shseat =
3286 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3287 struct wl_display *display = shseat->seat->compositor->wl_display;
3289 struct wl_list *resource_list;
3292 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
3294 resource_list = &grab->touch->focus_resource_list;
3295 if (!wl_list_empty(resource_list)) {
3296 serial = wl_display_get_serial(display);
3297 wl_resource_for_each(resource, resource_list) {
3298 wl_touch_send_down(resource, serial, time,
3299 grab->touch->focus->surface->resource,
3306 touch_popup_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
3308 struct wl_resource *resource;
3309 struct shell_seat *shseat =
3310 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3311 struct wl_display *display = shseat->seat->compositor->wl_display;
3313 struct wl_list *resource_list;
3315 resource_list = &grab->touch->focus_resource_list;
3316 if (!wl_list_empty(resource_list)) {
3317 serial = wl_display_get_serial(display);
3318 wl_resource_for_each(resource, resource_list) {
3319 wl_touch_send_up(resource, serial, time, touch_id);
3325 touch_popup_grab_motion(struct weston_touch_grab *grab, uint32_t time,
3326 int touch_id, wl_fixed_t x, wl_fixed_t y)
3328 struct wl_resource *resource;
3329 struct wl_list *resource_list;
3332 weston_view_from_global_fixed(grab->touch->focus, x, y, &sx, &sy);
3334 resource_list = &grab->touch->focus_resource_list;
3335 if (!wl_list_empty(resource_list)) {
3336 wl_resource_for_each(resource, resource_list) {
3337 wl_touch_send_motion(resource, time, touch_id, sx, sy);
3343 touch_popup_grab_frame(struct weston_touch_grab *grab)
3348 touch_popup_grab_cancel(struct weston_touch_grab *grab)
3350 touch_popup_grab_end(grab->touch);
3353 static const struct weston_touch_grab_interface touch_popup_grab_interface = {
3354 touch_popup_grab_down,
3355 touch_popup_grab_up,
3356 touch_popup_grab_motion,
3357 touch_popup_grab_frame,
3358 touch_popup_grab_cancel,
3362 shell_surface_send_popup_done(struct shell_surface *shsurf)
3364 if (shsurf->resource == NULL)
3366 else if (shell_surface_is_wl_shell_surface(shsurf))
3367 wl_shell_surface_send_popup_done(shsurf->resource);
3368 else if (shell_surface_is_xdg_popup(shsurf))
3369 xdg_popup_send_popup_done(shsurf->resource);
3373 popup_grab_end(struct weston_pointer *pointer)
3375 struct weston_pointer_grab *grab = pointer->grab;
3376 struct shell_seat *shseat =
3377 container_of(grab, struct shell_seat, popup_grab.grab);
3378 struct shell_surface *shsurf;
3379 struct shell_surface *next;
3381 if (pointer->grab->interface == &popup_grab_interface) {
3382 weston_pointer_end_grab(grab->pointer);
3383 shseat->popup_grab.client = NULL;
3384 shseat->popup_grab.grab.interface = NULL;
3385 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3386 /* Send the popup_done event to all the popups open */
3387 wl_list_for_each_safe(shsurf, next,
3388 &shseat->popup_grab.surfaces_list,
3390 shell_surface_send_popup_done(shsurf);
3391 shsurf->popup.shseat = NULL;
3392 wl_list_init(&shsurf->popup.grab_link);
3394 wl_list_init(&shseat->popup_grab.surfaces_list);
3399 touch_popup_grab_end(struct weston_touch *touch)
3401 struct weston_touch_grab *grab = touch->grab;
3402 struct shell_seat *shseat =
3403 container_of(grab, struct shell_seat, popup_grab.touch_grab);
3404 struct shell_surface *shsurf;
3405 struct shell_surface *next;
3407 if (touch->grab->interface == &touch_popup_grab_interface) {
3408 weston_touch_end_grab(grab->touch);
3409 shseat->popup_grab.client = NULL;
3410 shseat->popup_grab.touch_grab.interface = NULL;
3411 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
3412 /* Send the popup_done event to all the popups open */
3413 wl_list_for_each_safe(shsurf, next,
3414 &shseat->popup_grab.surfaces_list,
3416 shell_surface_send_popup_done(shsurf);
3417 shsurf->popup.shseat = NULL;
3418 wl_list_init(&shsurf->popup.grab_link);
3420 wl_list_init(&shseat->popup_grab.surfaces_list);
3424 static struct shell_surface *
3425 get_top_popup(struct shell_seat *shseat)
3427 struct shell_surface *shsurf;
3429 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3432 shsurf = container_of(shseat->popup_grab.surfaces_list.next,
3433 struct shell_surface,
3440 add_popup_grab(struct shell_surface *shsurf,
3441 struct shell_seat *shseat,
3444 struct weston_seat *seat = shseat->seat;
3445 struct shell_surface *parent, *top_surface;
3446 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3447 struct weston_touch *touch = weston_seat_get_touch(seat);
3449 parent = get_shell_surface(shsurf->parent);
3450 top_surface = get_top_popup(shseat);
3451 if (shell_surface_is_xdg_popup(shsurf) &&
3453 (top_surface == NULL && !shell_surface_is_xdg_surface(parent)) ||
3454 (top_surface != NULL && parent != top_surface))) {
3455 wl_resource_post_error(shsurf->owner_resource,
3456 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
3457 "xdg_popup was not created on the "
3462 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3463 shseat->popup_grab.type = type;
3464 shseat->popup_grab.client =
3465 wl_resource_get_client(shsurf->resource);
3467 if (type == POINTER) {
3468 shseat->popup_grab.grab.interface =
3469 &popup_grab_interface;
3471 /* We must make sure here that this popup was opened
3472 * after a mouse press, and not just by moving around
3473 * with other popups already open. */
3474 if (pointer->button_count > 0)
3475 shseat->popup_grab.initial_up = 0;
3476 } else if (type == TOUCH) {
3477 shseat->popup_grab.touch_grab.interface =
3478 &touch_popup_grab_interface;
3481 wl_list_insert(&shseat->popup_grab.surfaces_list,
3482 &shsurf->popup.grab_link);
3484 if (type == POINTER) {
3485 weston_pointer_start_grab(pointer,
3486 &shseat->popup_grab.grab);
3487 } else if (type == TOUCH) {
3488 weston_touch_start_grab(touch,
3489 &shseat->popup_grab.touch_grab);
3492 wl_list_insert(&shseat->popup_grab.surfaces_list,
3493 &shsurf->popup.grab_link);
3500 remove_popup_grab(struct shell_surface *shsurf)
3502 struct shell_seat *shseat = shsurf->popup.shseat;
3504 if (shell_surface_is_xdg_popup(shsurf) &&
3505 get_top_popup(shseat) != shsurf) {
3506 wl_resource_post_error(shsurf->owner_resource,
3507 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
3508 "xdg_popup was destroyed while it was "
3509 "not the topmost popup.");
3513 wl_list_remove(&shsurf->popup.grab_link);
3514 wl_list_init(&shsurf->popup.grab_link);
3515 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
3516 if (shseat->popup_grab.type == POINTER) {
3517 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
3518 shseat->popup_grab.grab.interface = NULL;
3519 } else if (shseat->popup_grab.type == TOUCH) {
3520 weston_touch_end_grab(shseat->popup_grab.touch_grab.touch);
3521 shseat->popup_grab.touch_grab.interface = NULL;
3527 shell_map_popup(struct shell_surface *shsurf)
3529 struct shell_seat *shseat = shsurf->popup.shseat;
3530 struct weston_view *parent_view = get_default_view(shsurf->parent);
3531 struct weston_pointer *pointer = weston_seat_get_pointer(shseat->seat);
3532 struct weston_touch *touch = weston_seat_get_touch(shseat->seat);
3534 shsurf->surface->output = parent_view->output;
3535 shsurf->surface->is_mapped = true;
3536 shsurf->view->output = parent_view->output;
3538 weston_view_set_transform_parent(shsurf->view, parent_view);
3539 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
3540 weston_view_update_transform(shsurf->view);
3541 shsurf->view->is_mapped = true;
3544 pointer->grab_serial == shsurf->popup.serial) {
3545 if (add_popup_grab(shsurf, shseat, POINTER) != 0)
3548 touch->grab_serial == shsurf->popup.serial) {
3549 if (add_popup_grab(shsurf, shseat, TOUCH) != 0)
3552 shell_surface_send_popup_done(shsurf);
3553 shseat->popup_grab.client = NULL;
3559 static const struct wl_shell_surface_interface shell_surface_implementation = {
3562 shell_surface_resize,
3563 shell_surface_set_toplevel,
3564 shell_surface_set_transient,
3565 shell_surface_set_fullscreen,
3566 shell_surface_set_popup,
3567 shell_surface_set_maximized,
3568 shell_surface_set_title,
3569 shell_surface_set_class
3573 destroy_shell_surface(struct shell_surface *shsurf)
3575 struct shell_surface *child, *next;
3577 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3579 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3580 remove_popup_grab(shsurf);
3583 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
3584 shell_surface_is_top_fullscreen(shsurf))
3585 restore_output_mode (shsurf->fullscreen_output);
3587 if (shsurf->fullscreen.black_view)
3588 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
3590 /* As destroy_resource() use wl_list_for_each_safe(),
3591 * we can always remove the listener.
3593 wl_list_remove(&shsurf->surface_destroy_listener.link);
3594 shsurf->surface->committed = NULL;
3595 weston_surface_set_label_func(shsurf->surface, NULL);
3596 free(shsurf->title);
3598 weston_view_destroy(shsurf->view);
3600 wl_list_remove(&shsurf->children_link);
3601 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3602 shell_surface_set_parent(child, NULL);
3604 wl_list_remove(&shsurf->link);
3609 shell_destroy_shell_surface(struct wl_resource *resource)
3611 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3613 if (!wl_list_empty(&shsurf->popup.grab_link))
3614 remove_popup_grab(shsurf);
3615 if (shsurf->resource)
3616 wl_list_remove(wl_resource_get_link(shsurf->resource));
3617 shsurf->resource = NULL;
3621 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
3623 struct shell_surface *shsurf = container_of(listener,
3624 struct shell_surface,
3625 surface_destroy_listener);
3627 if (shsurf->resource)
3628 wl_resource_destroy(shsurf->resource);
3630 destroy_shell_surface(shsurf);
3634 fade_out_done_idle_cb(void *data)
3636 struct shell_surface *shsurf = data;
3638 weston_surface_destroy(shsurf->surface);
3642 fade_out_done(struct weston_view_animation *animation, void *data)
3644 struct shell_surface *shsurf = data;
3645 struct wl_event_loop *loop;
3647 loop = wl_display_get_event_loop(
3648 shsurf->surface->compositor->wl_display);
3650 if (!shsurf->destroying) {
3651 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
3652 shsurf->destroying = true;
3657 handle_resource_destroy(struct wl_listener *listener, void *data)
3659 struct shell_surface *shsurf =
3660 container_of(listener, struct shell_surface,
3661 resource_destroy_listener);
3663 if (!weston_surface_is_mapped(shsurf->surface))
3666 shsurf->surface->ref_count++;
3668 pixman_region32_fini(&shsurf->surface->pending.input);
3669 pixman_region32_init(&shsurf->surface->pending.input);
3670 pixman_region32_fini(&shsurf->surface->input);
3671 pixman_region32_init(&shsurf->surface->input);
3672 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
3673 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3674 fade_out_done, shsurf);
3676 weston_surface_destroy(shsurf->surface);
3681 shell_surface_committed(struct weston_surface *, int32_t, int32_t);
3683 struct shell_surface *
3684 get_shell_surface(struct weston_surface *surface)
3686 if (surface->committed == shell_surface_committed)
3687 return surface->committed_private;
3692 static struct shell_surface *
3693 create_common_surface(struct shell_client *owner, void *shell,
3694 struct weston_surface *surface,
3695 const struct weston_shell_client *client)
3697 struct shell_surface *shsurf;
3699 assert(surface->committed == NULL);
3701 shsurf = calloc(1, sizeof *shsurf);
3703 weston_log("no memory to allocate shell surface\n");
3707 shsurf->view = weston_view_create(surface);
3708 if (!shsurf->view) {
3709 weston_log("no memory to allocate shell surface\n");
3714 surface->committed = shell_surface_committed;
3715 surface->committed_private = shsurf;
3716 weston_surface_set_label_func(surface, shell_surface_get_label);
3718 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3719 wl_resource_add_destroy_listener(surface->resource,
3720 &shsurf->resource_destroy_listener);
3721 shsurf->owner = owner;
3723 shsurf->shell = (struct desktop_shell *) shell;
3724 shsurf->unresponsive = 0;
3725 shsurf->saved_position_valid = false;
3726 shsurf->saved_rotation_valid = false;
3727 shsurf->surface = surface;
3728 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3729 shsurf->fullscreen.framerate = 0;
3730 shsurf->fullscreen.black_view = NULL;
3731 wl_list_init(&shsurf->fullscreen.transform.link);
3733 shsurf->output = get_default_output(shsurf->shell->compositor);
3735 wl_signal_init(&shsurf->destroy_signal);
3736 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
3737 wl_signal_add(&surface->destroy_signal,
3738 &shsurf->surface_destroy_listener);
3740 /* init link so its safe to always remove it in destroy_shell_surface */
3741 wl_list_init(&shsurf->link);
3742 wl_list_init(&shsurf->popup.grab_link);
3744 /* empty when not in use */
3745 wl_list_init(&shsurf->rotation.transform.link);
3746 weston_matrix_init(&shsurf->rotation.rotation);
3748 wl_list_init(&shsurf->workspace_transform.link);
3750 wl_list_init(&shsurf->children_link);
3751 wl_list_init(&shsurf->children_list);
3752 shsurf->parent = NULL;
3754 set_type(shsurf, SHELL_SURFACE_NONE);
3756 shsurf->client = client;
3761 static struct shell_surface *
3762 create_shell_surface(void *shell, struct weston_surface *surface,
3763 const struct weston_shell_client *client)
3765 return create_common_surface(NULL, shell, surface, client);
3769 shell_get_shell_surface(struct wl_client *client,
3770 struct wl_resource *resource,
3772 struct wl_resource *surface_resource)
3774 struct weston_surface *surface =
3775 wl_resource_get_user_data(surface_resource);
3776 struct shell_client *sc = wl_resource_get_user_data(resource);
3777 struct desktop_shell *shell = sc->shell;
3778 struct shell_surface *shsurf;
3780 if (weston_surface_set_role(surface, "wl_shell_surface",
3781 resource, WL_SHELL_ERROR_ROLE) < 0)
3784 shsurf = create_common_surface(sc, shell, surface, &shell_client);
3786 wl_resource_post_no_memory(surface_resource);
3791 wl_resource_create(client,
3792 &wl_shell_surface_interface, 1, id);
3793 if (!shsurf->resource) {
3794 wl_resource_post_no_memory(surface_resource);
3797 wl_resource_set_implementation(shsurf->resource,
3798 &shell_surface_implementation,
3799 shsurf, shell_destroy_shell_surface);
3800 wl_list_insert(&sc->surface_list,
3801 wl_resource_get_link(shsurf->resource));
3805 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3807 /* A shell surface without a resource is created from xwayland
3808 * and is considered a wl_shell surface for now. */
3810 return shsurf->resource == NULL ||
3811 wl_resource_instance_of(shsurf->resource,
3812 &wl_shell_surface_interface,
3813 &shell_surface_implementation);
3816 static const struct wl_shell_interface shell_implementation = {
3817 shell_get_shell_surface
3820 /****************************
3821 * xdg-shell implementation */
3824 xdg_surface_destroy(struct wl_client *client,
3825 struct wl_resource *resource)
3827 wl_resource_destroy(resource);
3831 xdg_surface_set_parent(struct wl_client *client,
3832 struct wl_resource *resource,
3833 struct wl_resource *parent_resource)
3835 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3836 struct shell_surface *parent;
3838 if (parent_resource) {
3839 parent = wl_resource_get_user_data(parent_resource);
3840 shell_surface_set_parent(shsurf, parent->surface);
3842 shell_surface_set_parent(shsurf, NULL);
3847 xdg_surface_set_app_id(struct wl_client *client,
3848 struct wl_resource *resource,
3851 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3853 free(shsurf->class);
3854 shsurf->class = strdup(app_id);
3855 shsurf->surface->timeline.force_refresh = 1;
3859 xdg_surface_show_window_menu(struct wl_client *client,
3860 struct wl_resource *surface_resource,
3861 struct wl_resource *seat_resource,
3870 xdg_surface_set_title(struct wl_client *client,
3871 struct wl_resource *resource, const char *title)
3873 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3875 set_title(shsurf, title);
3879 xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3880 struct wl_resource *seat_resource, uint32_t serial)
3882 common_surface_move(resource, seat_resource, serial);
3886 xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3887 struct wl_resource *seat_resource, uint32_t serial,
3890 common_surface_resize(resource, seat_resource, serial, edges);
3894 xdg_surface_ack_configure(struct wl_client *client,
3895 struct wl_resource *resource,
3898 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3900 if (shsurf->state_requested) {
3901 shsurf->next_state = shsurf->requested_state;
3902 shsurf->state_changed = true;
3903 shsurf->state_requested = false;
3908 xdg_surface_set_window_geometry(struct wl_client *client,
3909 struct wl_resource *resource,
3915 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3917 set_window_geometry(shsurf, x, y, width, height);
3921 xdg_surface_set_maximized(struct wl_client *client,
3922 struct wl_resource *resource)
3924 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3925 struct weston_output *output;
3927 shsurf->state_requested = true;
3928 shsurf->requested_state.maximized = true;
3930 if (!weston_surface_is_mapped(shsurf->surface))
3931 output = get_focused_output(shsurf->surface->compositor);
3933 output = shsurf->surface->output;
3935 shell_surface_set_output(shsurf, output);
3936 send_configure_for_surface(shsurf);
3940 xdg_surface_unset_maximized(struct wl_client *client,
3941 struct wl_resource *resource)
3943 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3945 shsurf->state_requested = true;
3946 shsurf->requested_state.maximized = false;
3947 send_configure_for_surface(shsurf);
3951 xdg_surface_set_fullscreen(struct wl_client *client,
3952 struct wl_resource *resource,
3953 struct wl_resource *output_resource)
3955 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3956 struct weston_output *output;
3958 shsurf->state_requested = true;
3959 shsurf->requested_state.fullscreen = true;
3961 if (output_resource)
3962 output = wl_resource_get_user_data(output_resource);
3966 /* handle clients launching in fullscreen */
3967 if (output == NULL && !weston_surface_is_mapped(shsurf->surface)) {
3968 /* Set the output to the one that has focus currently. */
3969 assert(shsurf->surface);
3970 output = get_focused_output(shsurf->surface->compositor);
3973 shell_surface_set_output(shsurf, output);
3974 shsurf->fullscreen_output = shsurf->output;
3976 send_configure_for_surface(shsurf);
3980 xdg_surface_unset_fullscreen(struct wl_client *client,
3981 struct wl_resource *resource)
3983 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3985 shsurf->state_requested = true;
3986 shsurf->requested_state.fullscreen = false;
3987 send_configure_for_surface(shsurf);
3991 xdg_surface_set_minimized(struct wl_client *client,
3992 struct wl_resource *resource)
3994 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3996 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3999 /* apply compositor's own minimization logic (hide) */
4000 set_minimized(shsurf->surface);
4003 static const struct xdg_surface_interface xdg_surface_implementation = {
4004 xdg_surface_destroy,
4005 xdg_surface_set_parent,
4006 xdg_surface_set_title,
4007 xdg_surface_set_app_id,
4008 xdg_surface_show_window_menu,
4011 xdg_surface_ack_configure,
4012 xdg_surface_set_window_geometry,
4013 xdg_surface_set_maximized,
4014 xdg_surface_unset_maximized,
4015 xdg_surface_set_fullscreen,
4016 xdg_surface_unset_fullscreen,
4017 xdg_surface_set_minimized,
4021 xdg_send_configure(struct weston_surface *surface,
4022 int32_t width, int32_t height)
4024 struct shell_surface *shsurf = get_shell_surface(surface);
4026 struct wl_array states;
4031 if (!shsurf->resource)
4034 wl_array_init(&states);
4035 if (shsurf->requested_state.fullscreen) {
4036 s = wl_array_add(&states, sizeof *s);
4037 *s = XDG_SURFACE_STATE_FULLSCREEN;
4038 } else if (shsurf->requested_state.maximized) {
4039 s = wl_array_add(&states, sizeof *s);
4040 *s = XDG_SURFACE_STATE_MAXIMIZED;
4042 if (shsurf->resize_edges != 0) {
4043 s = wl_array_add(&states, sizeof *s);
4044 *s = XDG_SURFACE_STATE_RESIZING;
4046 if (shsurf->focus_count > 0) {
4047 s = wl_array_add(&states, sizeof *s);
4048 *s = XDG_SURFACE_STATE_ACTIVATED;
4051 serial = wl_display_next_serial(shsurf->surface->compositor->wl_display);
4052 xdg_surface_send_configure(shsurf->resource, width, height, &states, serial);
4054 wl_array_release(&states);
4057 static const struct weston_shell_client xdg_client = {
4063 xdg_shell_destroy(struct wl_client *client,
4064 struct wl_resource *resource)
4066 struct shell_client *sc = wl_resource_get_user_data(resource);
4067 struct wl_resource *shsurf_resource;
4068 struct shell_surface *shsurf;
4070 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
4071 shsurf = wl_resource_get_user_data(shsurf_resource);
4072 if (shsurf->owner_resource == resource) {
4073 wl_resource_post_error(
4075 XDG_SHELL_ERROR_DEFUNCT_SURFACES,
4076 "not all child surface objects destroyed");
4081 wl_resource_destroy(resource);
4085 xdg_use_unstable_version(struct wl_client *client,
4086 struct wl_resource *resource,
4090 wl_resource_post_error(resource,
4092 "xdg-shell:: version not implemented yet.");
4097 static struct shell_surface *
4098 create_xdg_surface(struct shell_client *owner, void *shell,
4099 struct weston_surface *surface,
4100 const struct weston_shell_client *client)
4102 struct shell_surface *shsurf;
4104 shsurf = create_common_surface(owner, shell, surface, client);
4108 set_type(shsurf, SHELL_SURFACE_TOPLEVEL);
4114 xdg_get_xdg_surface(struct wl_client *client,
4115 struct wl_resource *resource,
4117 struct wl_resource *surface_resource)
4119 struct weston_surface *surface =
4120 wl_resource_get_user_data(surface_resource);
4121 struct shell_client *sc = wl_resource_get_user_data(resource);
4122 struct desktop_shell *shell = sc->shell;
4123 struct shell_surface *shsurf;
4125 shsurf = get_shell_surface(surface);
4126 if (shsurf && shell_surface_is_xdg_surface(shsurf)) {
4127 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4128 "This wl_surface is already an "
4133 if (weston_surface_set_role(surface, "xdg_surface",
4134 resource, XDG_SHELL_ERROR_ROLE) < 0)
4137 shsurf = create_xdg_surface(sc, shell, surface, &xdg_client);
4139 wl_resource_post_no_memory(surface_resource);
4144 wl_resource_create(client,
4145 &xdg_surface_interface, 1, id);
4146 if (!shsurf->resource) {
4147 wl_resource_post_no_memory(surface_resource);
4150 wl_resource_set_implementation(shsurf->resource,
4151 &xdg_surface_implementation,
4152 shsurf, shell_destroy_shell_surface);
4153 shsurf->owner_resource = resource;
4154 wl_list_insert(&sc->surface_list,
4155 wl_resource_get_link(shsurf->resource));
4159 shell_surface_is_xdg_surface(struct shell_surface *shsurf)
4161 return shsurf->resource &&
4162 wl_resource_instance_of(shsurf->resource,
4163 &xdg_surface_interface,
4164 &xdg_surface_implementation);
4167 /* xdg-popup implementation */
4170 xdg_popup_destroy(struct wl_client *client,
4171 struct wl_resource *resource)
4173 wl_resource_destroy(resource);
4176 static const struct xdg_popup_interface xdg_popup_implementation = {
4181 xdg_popup_send_configure(struct weston_surface *surface,
4182 int32_t width, int32_t height)
4186 static const struct weston_shell_client xdg_popup_client = {
4187 xdg_popup_send_configure,
4191 static struct shell_surface *
4192 create_xdg_popup(struct shell_client *owner, void *shell,
4193 struct weston_surface *surface,
4194 const struct weston_shell_client *client,
4195 struct weston_surface *parent,
4196 struct shell_seat *seat,
4198 int32_t x, int32_t y)
4200 struct shell_surface *shsurf;
4202 shsurf = create_common_surface(owner, shell, surface, client);
4206 set_type(shsurf, SHELL_SURFACE_POPUP);
4207 shsurf->popup.shseat = seat;
4208 shsurf->popup.serial = serial;
4209 shsurf->popup.x = x;
4210 shsurf->popup.y = y;
4211 shell_surface_set_parent(shsurf, parent);
4217 xdg_get_xdg_popup(struct wl_client *client,
4218 struct wl_resource *resource,
4220 struct wl_resource *surface_resource,
4221 struct wl_resource *parent_resource,
4222 struct wl_resource *seat_resource,
4224 int32_t x, int32_t y)
4226 struct weston_surface *surface =
4227 wl_resource_get_user_data(surface_resource);
4228 struct shell_client *sc = wl_resource_get_user_data(resource);
4229 struct desktop_shell *shell = sc->shell;
4230 struct shell_surface *shsurf;
4231 struct shell_surface *parent_shsurf;
4232 struct weston_surface *parent;
4233 struct shell_seat *seat;
4235 shsurf = get_shell_surface(surface);
4236 if (shsurf && shell_surface_is_xdg_popup(shsurf)) {
4237 wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE,
4238 "This wl_surface is already an "
4243 if (weston_surface_set_role(surface, "xdg_popup",
4244 resource, XDG_SHELL_ERROR_ROLE) < 0)
4247 if (!parent_resource) {
4248 wl_resource_post_error(surface_resource,
4249 WL_DISPLAY_ERROR_INVALID_OBJECT,
4250 "xdg_shell::get_xdg_popup requires a parent shell surface");
4254 parent = wl_resource_get_user_data(parent_resource);
4255 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));
4257 /* Verify that we are creating the top most popup when mapping,
4258 * as it's not until then we know whether it was mapped as most
4259 * top level or not. */
4261 parent_shsurf = get_shell_surface(parent);
4262 if (!parent_shsurf ||
4263 (!shell_surface_is_xdg_popup(parent_shsurf) &&
4264 !shell_surface_is_xdg_surface(parent_shsurf))) {
4265 wl_resource_post_error(resource,
4266 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
4267 "xdg_popup parent was invalid");
4271 shsurf = create_xdg_popup(sc, shell, surface, &xdg_popup_client,
4272 parent, seat, serial, x, y);
4274 wl_resource_post_no_memory(surface_resource);
4279 wl_resource_create(client,
4280 &xdg_popup_interface, 1, id);
4281 if (!shsurf->resource) {
4282 wl_resource_post_no_memory(surface_resource);
4285 wl_resource_set_implementation(shsurf->resource,
4286 &xdg_popup_implementation,
4287 shsurf, shell_destroy_shell_surface);
4288 shsurf->owner_resource = resource;
4289 wl_list_insert(&sc->surface_list,
4290 wl_resource_get_link(shsurf->resource));
4294 xdg_pong(struct wl_client *client,
4295 struct wl_resource *resource, uint32_t serial)
4297 struct shell_client *sc = wl_resource_get_user_data(resource);
4299 shell_client_pong(sc, serial);
4303 shell_surface_is_xdg_popup(struct shell_surface *shsurf)
4305 return (shsurf->resource &&
4306 wl_resource_instance_of(shsurf->resource,
4307 &xdg_popup_interface,
4308 &xdg_popup_implementation));
4311 static const struct xdg_shell_interface xdg_implementation = {
4313 xdg_use_unstable_version,
4314 xdg_get_xdg_surface,
4320 xdg_shell_unversioned_dispatch(const void *implementation,
4321 void *_target, uint32_t opcode,
4322 const struct wl_message *message,
4323 union wl_argument *args)
4325 struct wl_resource *resource = _target;
4326 struct shell_client *sc = wl_resource_get_user_data(resource);
4328 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
4329 wl_resource_post_error(resource,
4330 WL_DISPLAY_ERROR_INVALID_OBJECT,
4331 "must call use_unstable_version first");
4335 #define XDG_SERVER_VERSION 5
4337 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
4338 "shell implementation doesn't match protocol version");
4340 if (args[0].i != XDG_SERVER_VERSION) {
4341 wl_resource_post_error(resource,
4342 WL_DISPLAY_ERROR_INVALID_OBJECT,
4343 "incompatible version, server is %d "
4345 XDG_SERVER_VERSION, args[0].i);
4349 wl_resource_set_implementation(resource, &xdg_implementation,
4355 /* end of xdg-shell implementation */
4356 /***********************************/
4359 shell_fade(struct desktop_shell *shell, enum fade_type type);
4362 configure_static_view(struct weston_view *ev, struct weston_layer *layer)
4364 struct weston_view *v, *next;
4366 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
4367 if (v->output == ev->output && v != ev) {
4368 weston_view_unmap(v);
4369 v->surface->committed = NULL;
4370 weston_surface_set_label_func(v->surface, NULL);
4374 weston_view_set_position(ev, ev->output->x, ev->output->y);
4375 ev->surface->is_mapped = true;
4376 ev->is_mapped = true;
4378 if (wl_list_empty(&ev->layer_link.link)) {
4379 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
4380 weston_compositor_schedule_repaint(ev->surface->compositor);
4385 static struct shell_output *
4386 find_shell_output_from_weston_output(struct desktop_shell *shell, struct weston_output *output)
4388 struct shell_output *shell_output;
4390 wl_list_for_each(shell_output, &shell->output_list, link) {
4391 if (shell_output->output == output)
4392 return shell_output;
4399 background_get_label(struct weston_surface *surface, char *buf, size_t len)
4401 return snprintf(buf, len, "background for output %s",
4402 surface->output->name);
4406 background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
4408 struct desktop_shell *shell = es->committed_private;
4409 struct weston_view *view;
4411 view = container_of(es->views.next, struct weston_view, surface_link);
4413 configure_static_view(view, &shell->background_layer);
4417 handle_background_surface_destroy(struct wl_listener *listener, void *data)
4419 struct shell_output *output =
4420 container_of(listener, struct shell_output, background_surface_listener);
4422 weston_log("background surface gone\n");
4423 output->background_surface = NULL;
4427 desktop_shell_set_background(struct wl_client *client,
4428 struct wl_resource *resource,
4429 struct wl_resource *output_resource,
4430 struct wl_resource *surface_resource)
4432 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4433 struct weston_surface *surface =
4434 wl_resource_get_user_data(surface_resource);
4435 struct shell_output *sh_output;
4436 struct weston_view *view, *next;
4438 if (surface->committed) {
4439 wl_resource_post_error(surface_resource,
4440 WL_DISPLAY_ERROR_INVALID_OBJECT,
4441 "surface role already assigned");
4445 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4446 weston_view_destroy(view);
4447 view = weston_view_create(surface);
4449 surface->committed = background_committed;
4450 surface->committed_private = shell;
4451 weston_surface_set_label_func(surface, background_get_label);
4452 surface->output = wl_resource_get_user_data(output_resource);
4453 view->output = surface->output;
4454 weston_desktop_shell_send_configure(resource, 0,
4456 surface->output->width,
4457 surface->output->height);
4459 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4460 sh_output->background_surface = surface;
4462 sh_output->background_surface_listener.notify = handle_background_surface_destroy;
4463 wl_signal_add(&surface->destroy_signal, &sh_output->background_surface_listener);
4467 panel_get_label(struct weston_surface *surface, char *buf, size_t len)
4469 return snprintf(buf, len, "panel for output %s",
4470 surface->output->name);
4474 panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
4476 struct desktop_shell *shell = es->committed_private;
4477 struct weston_view *view;
4479 view = container_of(es->views.next, struct weston_view, surface_link);
4481 configure_static_view(view, &shell->panel_layer);
4485 handle_panel_surface_destroy(struct wl_listener *listener, void *data)
4487 struct shell_output *output =
4488 container_of(listener, struct shell_output, panel_surface_listener);
4490 weston_log("panel surface gone\n");
4491 output->panel_surface = NULL;
4496 desktop_shell_set_panel(struct wl_client *client,
4497 struct wl_resource *resource,
4498 struct wl_resource *output_resource,
4499 struct wl_resource *surface_resource)
4501 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4502 struct weston_surface *surface =
4503 wl_resource_get_user_data(surface_resource);
4504 struct weston_view *view, *next;
4505 struct shell_output *sh_output;
4507 if (surface->committed) {
4508 wl_resource_post_error(surface_resource,
4509 WL_DISPLAY_ERROR_INVALID_OBJECT,
4510 "surface role already assigned");
4514 wl_list_for_each_safe(view, next, &surface->views, surface_link)
4515 weston_view_destroy(view);
4516 view = weston_view_create(surface);
4518 surface->committed = panel_committed;
4519 surface->committed_private = shell;
4520 weston_surface_set_label_func(surface, panel_get_label);
4521 surface->output = wl_resource_get_user_data(output_resource);
4522 view->output = surface->output;
4523 weston_desktop_shell_send_configure(resource, 0,
4525 surface->output->width,
4526 surface->output->height);
4528 sh_output = find_shell_output_from_weston_output(shell, surface->output);
4529 sh_output->panel_surface = surface;
4531 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
4532 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
4536 lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
4538 return snprintf(buf, len, "lock window");
4542 lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
4544 struct desktop_shell *shell = surface->committed_private;
4545 struct weston_view *view;
4547 view = container_of(surface->views.next, struct weston_view, surface_link);
4549 if (surface->width == 0)
4552 center_on_output(view, get_default_output(shell->compositor));
4554 if (!weston_surface_is_mapped(surface)) {
4555 weston_layer_entry_insert(&shell->lock_layer.view_list,
4557 weston_view_update_transform(view);
4558 surface->is_mapped = true;
4559 view->is_mapped = true;
4560 shell_fade(shell, FADE_IN);
4565 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
4567 struct desktop_shell *shell =
4568 container_of(listener, struct desktop_shell, lock_surface_listener);
4570 weston_log("lock surface gone\n");
4571 shell->lock_surface = NULL;
4575 desktop_shell_set_lock_surface(struct wl_client *client,
4576 struct wl_resource *resource,
4577 struct wl_resource *surface_resource)
4579 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4580 struct weston_surface *surface =
4581 wl_resource_get_user_data(surface_resource);
4583 shell->prepare_event_sent = false;
4588 shell->lock_surface = surface;
4590 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
4591 wl_signal_add(&surface->destroy_signal,
4592 &shell->lock_surface_listener);
4594 weston_view_create(surface);
4595 surface->committed = lock_surface_committed;
4596 surface->committed_private = shell;
4597 weston_surface_set_label_func(surface, lock_surface_get_label);
4601 resume_desktop(struct desktop_shell *shell)
4603 struct workspace *ws = get_current_workspace(shell);
4605 wl_list_remove(&shell->lock_layer.link);
4606 if (shell->showing_input_panels) {
4607 wl_list_insert(&shell->compositor->cursor_layer.link,
4608 &shell->input_panel_layer.link);
4609 wl_list_insert(&shell->input_panel_layer.link,
4610 &shell->fullscreen_layer.link);
4612 wl_list_insert(&shell->compositor->cursor_layer.link,
4613 &shell->fullscreen_layer.link);
4615 wl_list_insert(&shell->fullscreen_layer.link,
4616 &shell->panel_layer.link);
4617 wl_list_insert(&shell->panel_layer.link,
4620 restore_focus_state(shell, get_current_workspace(shell));
4622 shell->locked = false;
4623 shell_fade(shell, FADE_IN);
4624 weston_compositor_damage_all(shell->compositor);
4628 desktop_shell_unlock(struct wl_client *client,
4629 struct wl_resource *resource)
4631 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4633 shell->prepare_event_sent = false;
4636 resume_desktop(shell);
4640 desktop_shell_set_grab_surface(struct wl_client *client,
4641 struct wl_resource *resource,
4642 struct wl_resource *surface_resource)
4644 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4646 shell->grab_surface = wl_resource_get_user_data(surface_resource);
4647 weston_view_create(shell->grab_surface);
4651 desktop_shell_desktop_ready(struct wl_client *client,
4652 struct wl_resource *resource)
4654 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4656 shell_fade_startup(shell);
4660 desktop_shell_set_panel_position(struct wl_client *client,
4661 struct wl_resource *resource,
4664 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4666 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
4667 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
4668 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
4669 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
4670 wl_resource_post_error(resource,
4671 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
4672 "bad position argument");
4676 shell->panel_position = position;
4679 static const struct weston_desktop_shell_interface desktop_shell_implementation = {
4680 desktop_shell_set_background,
4681 desktop_shell_set_panel,
4682 desktop_shell_set_lock_surface,
4683 desktop_shell_unlock,
4684 desktop_shell_set_grab_surface,
4685 desktop_shell_desktop_ready,
4686 desktop_shell_set_panel_position
4689 static enum shell_surface_type
4690 get_shell_surface_type(struct weston_surface *surface)
4692 struct shell_surface *shsurf;
4694 shsurf = get_shell_surface(surface);
4696 return SHELL_SURFACE_NONE;
4697 return shsurf->type;
4701 move_binding(struct weston_pointer *pointer, uint32_t time,
4702 uint32_t button, void *data)
4704 struct weston_surface *focus;
4705 struct weston_surface *surface;
4706 struct shell_surface *shsurf;
4708 if (pointer->focus == NULL)
4711 focus = pointer->focus->surface;
4713 surface = weston_surface_get_main_surface(focus);
4714 if (surface == NULL)
4717 shsurf = get_shell_surface(surface);
4718 if (shsurf == NULL || shsurf->state.fullscreen ||
4719 shsurf->state.maximized)
4722 surface_move(shsurf, pointer, false);
4726 maximize_binding(struct weston_keyboard *keyboard, uint32_t time,
4727 uint32_t button, void *data)
4729 struct weston_surface *focus = keyboard->focus;
4730 struct weston_surface *surface;
4731 struct shell_surface *shsurf;
4733 surface = weston_surface_get_main_surface(focus);
4734 if (surface == NULL)
4737 shsurf = get_shell_surface(surface);
4741 if (!shell_surface_is_xdg_surface(shsurf))
4744 shsurf->state_requested = true;
4745 shsurf->requested_state.maximized = !shsurf->state.maximized;
4746 send_configure_for_surface(shsurf);
4750 fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
4751 uint32_t button, void *data)
4753 struct weston_surface *focus = keyboard->focus;
4754 struct weston_surface *surface;
4755 struct shell_surface *shsurf;
4757 surface = weston_surface_get_main_surface(focus);
4758 if (surface == NULL)
4761 shsurf = get_shell_surface(surface);
4765 if (!shell_surface_is_xdg_surface(shsurf))
4768 shsurf->state_requested = true;
4769 shsurf->requested_state.fullscreen = !shsurf->state.fullscreen;
4770 shsurf->fullscreen_output = shsurf->output;
4771 send_configure_for_surface(shsurf);
4775 touch_move_binding(struct weston_touch *touch, uint32_t time, void *data)
4777 struct weston_surface *focus;
4778 struct weston_surface *surface;
4779 struct shell_surface *shsurf;
4781 if (touch->focus == NULL)
4784 focus = touch->focus->surface;
4785 surface = weston_surface_get_main_surface(focus);
4786 if (surface == NULL)
4789 shsurf = get_shell_surface(surface);
4790 if (shsurf == NULL || shsurf->state.fullscreen ||
4791 shsurf->state.maximized)
4794 surface_touch_move(shsurf, touch);
4798 resize_binding(struct weston_pointer *pointer, uint32_t time,
4799 uint32_t button, void *data)
4801 struct weston_surface *focus;
4802 struct weston_surface *surface;
4805 struct shell_surface *shsurf;
4807 if (pointer->focus == NULL)
4810 focus = pointer->focus->surface;
4812 surface = weston_surface_get_main_surface(focus);
4813 if (surface == NULL)
4816 shsurf = get_shell_surface(surface);
4817 if (shsurf == NULL || shsurf->state.fullscreen ||
4818 shsurf->state.maximized)
4821 weston_view_from_global(shsurf->view,
4822 wl_fixed_to_int(pointer->grab_x),
4823 wl_fixed_to_int(pointer->grab_y),
4826 if (x < shsurf->surface->width / 3)
4827 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
4828 else if (x < 2 * shsurf->surface->width / 3)
4831 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
4833 if (y < shsurf->surface->height / 3)
4834 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
4835 else if (y < 2 * shsurf->surface->height / 3)
4838 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
4840 surface_resize(shsurf, pointer, edges);
4844 surface_opacity_binding(struct weston_pointer *pointer, uint32_t time,
4845 struct weston_pointer_axis_event *event,
4849 struct shell_surface *shsurf;
4850 struct weston_surface *focus = pointer->focus->surface;
4851 struct weston_surface *surface;
4853 /* XXX: broken for windows containing sub-surfaces */
4854 surface = weston_surface_get_main_surface(focus);
4855 if (surface == NULL)
4858 shsurf = get_shell_surface(surface);
4862 shsurf->view->alpha -= event->value * step;
4864 if (shsurf->view->alpha > 1.0)
4865 shsurf->view->alpha = 1.0;
4866 if (shsurf->view->alpha < step)
4867 shsurf->view->alpha = step;
4869 weston_view_geometry_dirty(shsurf->view);
4870 weston_surface_damage(surface);
4874 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
4877 struct weston_compositor *compositor = seat->compositor;
4878 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4879 struct weston_output *output;
4883 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
4887 wl_list_for_each(output, &compositor->output_list, link) {
4888 if (pixman_region32_contains_point(&output->region,
4889 wl_fixed_to_double(pointer->x),
4890 wl_fixed_to_double(pointer->y),
4892 if (key == KEY_PAGEUP)
4893 increment = output->zoom.increment;
4894 else if (key == KEY_PAGEDOWN)
4895 increment = -output->zoom.increment;
4896 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
4897 /* For every pixel zoom 20th of a step */
4898 increment = output->zoom.increment *
4903 output->zoom.level += increment;
4905 if (output->zoom.level < 0.0)
4906 output->zoom.level = 0.0;
4907 else if (output->zoom.level > output->zoom.max_level)
4908 output->zoom.level = output->zoom.max_level;
4910 if (!output->zoom.active) {
4911 if (output->zoom.level <= 0.0)
4913 weston_output_activate_zoom(output, seat);
4916 output->zoom.spring_z.target = output->zoom.level;
4918 weston_output_update_zoom(output);
4924 zoom_axis_binding(struct weston_pointer *pointer, uint32_t time,
4925 struct weston_pointer_axis_event *event,
4928 do_zoom(pointer->seat, time, 0, event->axis, event->value);
4932 zoom_key_binding(struct weston_keyboard *keyboard, uint32_t time,
4933 uint32_t key, void *data)
4935 do_zoom(keyboard->seat, time, key, 0, 0);
4939 terminate_binding(struct weston_keyboard *keyboard, uint32_t time,
4940 uint32_t key, void *data)
4942 struct weston_compositor *compositor = data;
4944 wl_display_terminate(compositor->wl_display);
4948 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4949 struct weston_pointer_motion_event *event)
4951 struct rotate_grab *rotate =
4952 container_of(grab, struct rotate_grab, base.grab);
4953 struct weston_pointer *pointer = grab->pointer;
4954 struct shell_surface *shsurf = rotate->base.shsurf;
4955 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
4957 weston_pointer_move(pointer, event);
4962 cx = 0.5f * shsurf->surface->width;
4963 cy = 0.5f * shsurf->surface->height;
4965 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4966 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
4967 r = sqrtf(dx * dx + dy * dy);
4969 wl_list_remove(&shsurf->rotation.transform.link);
4970 weston_view_geometry_dirty(shsurf->view);
4973 struct weston_matrix *matrix =
4974 &shsurf->rotation.transform.matrix;
4976 weston_matrix_init(&rotate->rotation);
4977 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4979 weston_matrix_init(matrix);
4980 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
4981 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
4982 weston_matrix_multiply(matrix, &rotate->rotation);
4983 weston_matrix_translate(matrix, cx, cy, 0.0f);
4986 &shsurf->view->geometry.transformation_list,
4987 &shsurf->rotation.transform.link);
4989 wl_list_init(&shsurf->rotation.transform.link);
4990 weston_matrix_init(&shsurf->rotation.rotation);
4991 weston_matrix_init(&rotate->rotation);
4994 /* We need to adjust the position of the surface
4995 * in case it was resized in a rotated state before */
4996 cposx = shsurf->view->geometry.x + cx;
4997 cposy = shsurf->view->geometry.y + cy;
4998 dposx = rotate->center.x - cposx;
4999 dposy = rotate->center.y - cposy;
5000 if (dposx != 0.0f || dposy != 0.0f) {
5001 weston_view_set_position(shsurf->view,
5002 shsurf->view->geometry.x + dposx,
5003 shsurf->view->geometry.y + dposy);
5006 /* Repaint implies weston_view_update_transform(), which
5007 * lazily applies the damage due to rotation update.
5009 weston_compositor_schedule_repaint(shsurf->surface->compositor);
5013 rotate_grab_button(struct weston_pointer_grab *grab,
5014 uint32_t time, uint32_t button, uint32_t state_w)
5016 struct rotate_grab *rotate =
5017 container_of(grab, struct rotate_grab, base.grab);
5018 struct weston_pointer *pointer = grab->pointer;
5019 struct shell_surface *shsurf = rotate->base.shsurf;
5020 enum wl_pointer_button_state state = state_w;
5022 if (pointer->button_count == 0 &&
5023 state == WL_POINTER_BUTTON_STATE_RELEASED) {
5025 weston_matrix_multiply(&shsurf->rotation.rotation,
5027 shell_grab_end(&rotate->base);
5033 rotate_grab_cancel(struct weston_pointer_grab *grab)
5035 struct rotate_grab *rotate =
5036 container_of(grab, struct rotate_grab, base.grab);
5038 shell_grab_end(&rotate->base);
5042 static const struct weston_pointer_grab_interface rotate_grab_interface = {
5047 noop_grab_axis_source,
5053 surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer)
5055 struct rotate_grab *rotate;
5059 surface = find_toplevel_surface(surface);
5061 rotate = malloc(sizeof *rotate);
5065 weston_view_to_global_float(surface->view,
5066 surface->surface->width * 0.5f,
5067 surface->surface->height * 0.5f,
5068 &rotate->center.x, &rotate->center.y);
5070 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
5071 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
5072 r = sqrtf(dx * dx + dy * dy);
5074 struct weston_matrix inverse;
5076 weston_matrix_init(&inverse);
5077 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
5078 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
5080 weston_matrix_init(&rotate->rotation);
5081 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
5083 weston_matrix_init(&surface->rotation.rotation);
5084 weston_matrix_init(&rotate->rotation);
5087 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
5088 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
5092 rotate_binding(struct weston_pointer *pointer, uint32_t time, uint32_t button,
5095 struct weston_surface *focus;
5096 struct weston_surface *base_surface;
5097 struct shell_surface *surface;
5099 if (pointer->focus == NULL)
5102 focus = pointer->focus->surface;
5104 base_surface = weston_surface_get_main_surface(focus);
5105 if (base_surface == NULL)
5108 surface = get_shell_surface(base_surface);
5109 if (surface == NULL || surface->state.fullscreen ||
5110 surface->state.maximized)
5113 surface_rotate(surface, pointer);
5116 /* Move all fullscreen layers down to the current workspace and hide their
5117 * black views. The surfaces' state is set to both fullscreen and lowered,
5118 * and this is reversed when such a surface is re-configured, see
5119 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
5121 * lowering_output = NULL - Lower on all outputs, else only lower on the
5124 * This should be used when implementing shell-wide overlays, such as
5125 * the alt-tab switcher, which need to de-promote fullscreen layers. */
5127 lower_fullscreen_layer(struct desktop_shell *shell,
5128 struct weston_output *lowering_output)
5130 struct workspace *ws;
5131 struct weston_view *view, *prev;
5133 ws = get_current_workspace(shell);
5134 wl_list_for_each_reverse_safe(view, prev,
5135 &shell->fullscreen_layer.view_list.link,
5137 struct shell_surface *shsurf = get_shell_surface(view->surface);
5142 /* Only lower surfaces which have lowering_output as their fullscreen
5143 * output, unless a NULL output asks for lowering on all outputs.
5145 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
5148 /* We can have a non-fullscreen popup for a fullscreen surface
5149 * in the fullscreen layer. */
5150 if (shsurf->state.fullscreen) {
5151 /* Hide the black view */
5152 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
5153 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
5154 weston_view_damage_below(shsurf->fullscreen.black_view);
5158 /* Lower the view to the workspace layer */
5159 weston_layer_entry_remove(&view->layer_link);
5160 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
5161 weston_view_damage_below(view);
5162 weston_surface_damage(view->surface);
5164 shsurf->state.lowered = true;
5169 activate(struct desktop_shell *shell, struct weston_view *view,
5170 struct weston_seat *seat, uint32_t flags)
5172 struct weston_surface *es = view->surface;
5173 struct weston_surface *main_surface;
5174 struct focus_state *state;
5175 struct workspace *ws;
5176 struct weston_surface *old_es;
5177 struct shell_surface *shsurf;
5179 main_surface = weston_surface_get_main_surface(es);
5180 shsurf = get_shell_surface(main_surface);
5183 /* Only demote fullscreen surfaces on the output of activated shsurf.
5184 * Leave fullscreen surfaces on unrelated outputs alone. */
5185 lower_fullscreen_layer(shell, shsurf->output);
5187 weston_view_activate(view, seat, flags);
5189 state = ensure_focus_state(shell, seat);
5193 old_es = state->keyboard_focus;
5194 focus_state_set_focus(state, es);
5196 if (shsurf->state.fullscreen && flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
5197 shell_configure_fullscreen(shsurf);
5199 restore_output_mode(shsurf->output);
5201 /* Update the surface’s layer. This brings it to the top of the stacking
5202 * order as appropriate. */
5203 shell_surface_update_layer(shsurf);
5205 if (shell->focus_animation_type != ANIMATION_NONE) {
5206 ws = get_current_workspace(shell);
5207 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
5211 /* no-op func for checking black surface */
5213 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
5218 is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
5220 struct weston_surface *surface = view->surface;
5222 if (surface->committed == black_surface_committed) {
5224 *fs_view = surface->committed_private;
5231 activate_binding(struct weston_seat *seat,
5232 struct desktop_shell *shell,
5233 struct weston_view *focus_view,
5236 struct weston_view *main_view;
5237 struct weston_surface *main_surface;
5242 if (is_black_surface_view(focus_view, &main_view))
5243 focus_view = main_view;
5245 main_surface = weston_surface_get_main_surface(focus_view->surface);
5246 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
5249 activate(shell, focus_view, seat, flags);
5253 click_to_activate_binding(struct weston_pointer *pointer, uint32_t time,
5254 uint32_t button, void *data)
5256 if (pointer->grab != &pointer->default_grab)
5258 if (pointer->focus == NULL)
5261 activate_binding(pointer->seat, data, pointer->focus,
5262 WESTON_ACTIVATE_FLAG_CLICKED |
5263 WESTON_ACTIVATE_FLAG_CONFIGURE);
5267 touch_to_activate_binding(struct weston_touch *touch, uint32_t time,
5270 if (touch->grab != &touch->default_grab)
5272 if (touch->focus == NULL)
5275 activate_binding(touch->seat, data, touch->focus,
5276 WESTON_ACTIVATE_FLAG_CONFIGURE);
5280 unfocus_all_seats(struct desktop_shell *shell)
5282 struct weston_seat *seat, *next;
5284 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
5285 struct weston_keyboard *keyboard =
5286 weston_seat_get_keyboard(seat);
5291 weston_keyboard_set_focus(keyboard, NULL);
5296 lock(struct desktop_shell *shell)
5298 struct workspace *ws = get_current_workspace(shell);
5300 if (shell->locked) {
5301 weston_compositor_sleep(shell->compositor);
5305 shell->locked = true;
5307 /* Hide all surfaces by removing the fullscreen, panel and
5308 * toplevel layers. This way nothing else can show or receive
5309 * input events while we are locked. */
5311 wl_list_remove(&shell->panel_layer.link);
5312 wl_list_remove(&shell->fullscreen_layer.link);
5313 if (shell->showing_input_panels)
5314 wl_list_remove(&shell->input_panel_layer.link);
5315 wl_list_remove(&ws->layer.link);
5316 wl_list_insert(&shell->compositor->cursor_layer.link,
5317 &shell->lock_layer.link);
5319 weston_compositor_sleep(shell->compositor);
5321 /* Remove the keyboard focus on all seats. This will be
5322 * restored to the workspace's saved state via
5323 * restore_focus_state when the compositor is unlocked */
5324 unfocus_all_seats(shell);
5326 /* TODO: disable bindings that should not work while locked. */
5328 /* All this must be undone in resume_desktop(). */
5332 unlock(struct desktop_shell *shell)
5334 struct wl_resource *shell_resource;
5336 if (!shell->locked || shell->lock_surface) {
5337 shell_fade(shell, FADE_IN);
5341 /* If desktop-shell client has gone away, unlock immediately. */
5342 if (!shell->child.desktop_shell) {
5343 resume_desktop(shell);
5347 if (shell->prepare_event_sent)
5350 shell_resource = shell->child.desktop_shell;
5351 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
5352 shell->prepare_event_sent = true;
5356 shell_fade_done(struct weston_view_animation *animation, void *data)
5358 struct desktop_shell *shell = data;
5360 shell->fade.animation = NULL;
5362 switch (shell->fade.type) {
5364 weston_surface_destroy(shell->fade.view->surface);
5365 shell->fade.view = NULL;
5375 static struct weston_view *
5376 shell_fade_create_surface(struct desktop_shell *shell)
5378 struct weston_compositor *compositor = shell->compositor;
5379 struct weston_surface *surface;
5380 struct weston_view *view;
5382 surface = weston_surface_create(compositor);
5386 view = weston_view_create(surface);
5388 weston_surface_destroy(surface);
5392 weston_surface_set_size(surface, 8192, 8192);
5393 weston_view_set_position(view, 0, 0);
5394 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
5395 weston_layer_entry_insert(&compositor->fade_layer.view_list,
5397 pixman_region32_init(&surface->input);
5398 surface->is_mapped = true;
5399 view->is_mapped = true;
5405 shell_fade(struct desktop_shell *shell, enum fade_type type)
5417 weston_log("shell: invalid fade type\n");
5421 shell->fade.type = type;
5423 if (shell->fade.view == NULL) {
5424 shell->fade.view = shell_fade_create_surface(shell);
5425 if (!shell->fade.view)
5428 shell->fade.view->alpha = 1.0 - tint;
5429 weston_view_update_transform(shell->fade.view);
5432 if (shell->fade.view->output == NULL) {
5433 /* If the black view gets a NULL output, we lost the
5434 * last output and we'll just cancel the fade. This
5435 * happens when you close the last window under the
5436 * X11 or Wayland backends. */
5437 shell->locked = false;
5438 weston_surface_destroy(shell->fade.view->surface);
5439 shell->fade.view = NULL;
5440 } else if (shell->fade.animation) {
5441 weston_fade_update(shell->fade.animation, tint);
5443 shell->fade.animation =
5444 weston_fade_run(shell->fade.view,
5445 1.0 - tint, tint, 300.0,
5446 shell_fade_done, shell);
5451 do_shell_fade_startup(void *data)
5453 struct desktop_shell *shell = data;
5455 if (shell->startup_animation_type == ANIMATION_FADE) {
5456 shell_fade(shell, FADE_IN);
5458 weston_log("desktop shell: "
5459 "unexpected fade-in animation type %d\n",
5460 shell->startup_animation_type);
5461 weston_surface_destroy(shell->fade.view->surface);
5462 shell->fade.view = NULL;
5467 shell_fade_startup(struct desktop_shell *shell)
5469 struct wl_event_loop *loop;
5471 if (!shell->fade.startup_timer)
5474 wl_event_source_remove(shell->fade.startup_timer);
5475 shell->fade.startup_timer = NULL;
5477 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5478 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
5482 fade_startup_timeout(void *data)
5484 struct desktop_shell *shell = data;
5486 shell_fade_startup(shell);
5491 shell_fade_init(struct desktop_shell *shell)
5493 /* Make compositor output all black, and wait for the desktop-shell
5494 * client to signal it is ready, then fade in. The timer triggers a
5495 * fade-in, in case the desktop-shell client takes too long.
5498 struct wl_event_loop *loop;
5500 if (shell->fade.view != NULL) {
5501 weston_log("%s: warning: fade surface already exists\n",
5506 if (shell->startup_animation_type == ANIMATION_NONE)
5509 shell->fade.view = shell_fade_create_surface(shell);
5510 if (!shell->fade.view)
5513 weston_view_update_transform(shell->fade.view);
5514 weston_surface_damage(shell->fade.view->surface);
5516 loop = wl_display_get_event_loop(shell->compositor->wl_display);
5517 shell->fade.startup_timer =
5518 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
5519 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
5523 idle_handler(struct wl_listener *listener, void *data)
5525 struct desktop_shell *shell =
5526 container_of(listener, struct desktop_shell, idle_listener);
5527 struct weston_seat *seat;
5529 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
5530 struct weston_touch *touch = weston_seat_get_touch(seat);
5531 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5534 popup_grab_end(pointer);
5536 touch_popup_grab_end(touch);
5539 shell_fade(shell, FADE_OUT);
5540 /* lock() is called from shell_fade_done() */
5544 wake_handler(struct wl_listener *listener, void *data)
5546 struct desktop_shell *shell =
5547 container_of(listener, struct desktop_shell, wake_listener);
5553 transform_handler(struct wl_listener *listener, void *data)
5555 struct weston_surface *surface = data;
5556 struct shell_surface *shsurf = get_shell_surface(surface);
5557 struct weston_view *view;;
5560 if (!shsurf || !shsurf->client->send_position)
5563 view = shsurf->view;
5564 if (!view || !weston_view_is_mapped(view))
5567 x = view->geometry.x;
5568 y = view->geometry.y;
5570 shsurf->client->send_position(surface, x, y);
5574 center_on_output(struct weston_view *view, struct weston_output *output)
5576 int32_t surf_x, surf_y, width, height;
5579 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
5581 x = output->x + (output->width - width) / 2 - surf_x / 2;
5582 y = output->y + (output->height - height) / 2 - surf_y / 2;
5584 weston_view_set_position(view, x, y);
5588 weston_view_set_initial_position(struct weston_view *view,
5589 struct desktop_shell *shell)
5591 struct weston_compositor *compositor = shell->compositor;
5593 int32_t range_x, range_y;
5595 struct weston_output *output, *target_output = NULL;
5596 struct weston_seat *seat;
5597 pixman_rectangle32_t area;
5599 /* As a heuristic place the new window on the same output as the
5600 * pointer. Falling back to the output containing 0, 0.
5602 * TODO: Do something clever for touch too?
5604 wl_list_for_each(seat, &compositor->seat_list, link) {
5605 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
5608 ix = wl_fixed_to_int(pointer->x);
5609 iy = wl_fixed_to_int(pointer->y);
5614 wl_list_for_each(output, &compositor->output_list, link) {
5615 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
5616 target_output = output;
5621 if (!target_output) {
5622 weston_view_set_position(view, 10 + random() % 400,
5623 10 + random() % 400);
5627 /* Valid range within output where the surface will still be onscreen.
5628 * If this is negative it means that the surface is bigger than
5631 get_output_work_area(shell, target_output, &area);
5635 range_x = area.width - view->surface->width;
5636 range_y = area.height - view->surface->height;
5639 x += random() % range_x;
5642 y += random() % range_y;
5644 weston_view_set_position(view, x, y);
5648 set_maximized_position(struct desktop_shell *shell,
5649 struct shell_surface *shsurf)
5651 int32_t surf_x, surf_y;
5652 pixman_rectangle32_t area;
5654 get_output_work_area(shell, shsurf->output, &area);
5655 if (shsurf->has_set_geometry) {
5656 surf_x = shsurf->geometry.x;
5657 surf_y = shsurf->geometry.y;
5659 surface_subsurfaces_boundingbox(shsurf->surface,
5660 &surf_x, &surf_y, NULL, NULL);
5663 weston_view_set_position(shsurf->view,
5669 map(struct desktop_shell *shell, struct shell_surface *shsurf,
5670 int32_t sx, int32_t sy)
5672 struct weston_compositor *compositor = shell->compositor;
5673 struct weston_seat *seat;
5675 /* initial positioning, see also configure() */
5676 switch (shsurf->type) {
5677 case SHELL_SURFACE_TOPLEVEL:
5678 if (shsurf->state.fullscreen) {
5679 center_on_output(shsurf->view, shsurf->fullscreen_output);
5680 shell_map_fullscreen(shsurf);
5681 } else if (shsurf->state.maximized) {
5682 set_maximized_position(shell, shsurf);
5683 } else if (!shsurf->state.relative) {
5684 weston_view_set_initial_position(shsurf->view, shell);
5687 case SHELL_SURFACE_POPUP:
5688 if (shell_map_popup(shsurf) != 0)
5691 case SHELL_SURFACE_NONE:
5692 weston_view_set_position(shsurf->view,
5693 shsurf->view->geometry.x + sx,
5694 shsurf->view->geometry.y + sy);
5696 case SHELL_SURFACE_XWAYLAND:
5701 /* Surface stacking order, see also activate(). */
5702 shell_surface_update_layer(shsurf);
5704 if (shsurf->type != SHELL_SURFACE_NONE) {
5705 weston_view_update_transform(shsurf->view);
5706 shsurf->view->is_mapped = true;
5707 if (shsurf->state.maximized) {
5708 shsurf->surface->output = shsurf->output;
5709 shsurf->view->output = shsurf->output;
5713 switch (shsurf->type) {
5714 /* XXX: xwayland's using the same fields for transient type */
5715 case SHELL_SURFACE_XWAYLAND:
5716 if (shsurf->transient.flags ==
5717 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5719 case SHELL_SURFACE_TOPLEVEL:
5720 if (shsurf->state.relative &&
5721 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
5725 wl_list_for_each(seat, &compositor->seat_list, link)
5726 activate(shell, shsurf->view, seat,
5727 WESTON_ACTIVATE_FLAG_CONFIGURE);
5729 case SHELL_SURFACE_POPUP:
5730 case SHELL_SURFACE_NONE:
5735 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5736 !shsurf->state.maximized && !shsurf->state.fullscreen)
5738 switch (shell->win_animation_type) {
5739 case ANIMATION_FADE:
5740 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
5742 case ANIMATION_ZOOM:
5743 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
5745 case ANIMATION_NONE:
5753 configure(struct desktop_shell *shell, struct weston_surface *surface,
5756 struct shell_surface *shsurf;
5757 struct weston_view *view;
5759 shsurf = get_shell_surface(surface);
5763 if (shsurf->state.fullscreen)
5764 shell_configure_fullscreen(shsurf);
5765 else if (shsurf->state.maximized) {
5766 set_maximized_position(shell, shsurf);
5768 weston_view_set_position(shsurf->view, x, y);
5771 /* XXX: would a fullscreen surface need the same handling? */
5772 if (surface->output) {
5773 wl_list_for_each(view, &surface->views, surface_link)
5774 weston_view_update_transform(view);
5776 if (shsurf->state.maximized)
5777 surface->output = shsurf->output;
5782 shell_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
5784 struct shell_surface *shsurf = get_shell_surface(es);
5785 struct desktop_shell *shell;
5786 int type_changed = 0;
5790 shell = shsurf->shell;
5792 if (!weston_surface_is_mapped(es) &&
5793 !wl_list_empty(&shsurf->popup.grab_link)) {
5794 remove_popup_grab(shsurf);
5800 if (shsurf->has_next_geometry) {
5801 shsurf->geometry = shsurf->next_geometry;
5802 shsurf->has_next_geometry = false;
5803 shsurf->has_set_geometry = true;
5804 } else if (!shsurf->has_set_geometry) {
5805 surface_subsurfaces_boundingbox(shsurf->surface,
5806 &shsurf->geometry.x,
5807 &shsurf->geometry.y,
5808 &shsurf->geometry.width,
5809 &shsurf->geometry.height);
5812 if (shsurf->state_changed) {
5813 set_surface_type(shsurf);
5817 if (!weston_surface_is_mapped(es)) {
5818 map(shell, shsurf, sx, sy);
5819 es->is_mapped = true;
5820 } else if (type_changed || sx != 0 || sy != 0 ||
5821 shsurf->last_width != es->width ||
5822 shsurf->last_height != es->height) {
5823 float from_x, from_y;
5826 if (shsurf->resize_edges) {
5831 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
5832 sx = shsurf->last_width - es->width;
5833 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
5834 sy = shsurf->last_height - es->height;
5836 shsurf->last_width = es->width;
5837 shsurf->last_height = es->height;
5839 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
5840 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
5841 configure(shell, es,
5842 shsurf->view->geometry.x + to_x - from_x,
5843 shsurf->view->geometry.y + to_y - from_y);
5848 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
5850 struct timespec now;
5852 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
5856 * If the shell helper client dies before the session has been
5857 * up for roughly 30 seconds, better just make Weston shut down,
5858 * because the user likely has no way to interact with the desktop
5861 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
5862 weston_log("Error: %s apparently cannot run at all.\n",
5864 weston_log_continue(STAMP_SPACE "Quitting...");
5865 wl_display_terminate(shell->compositor->wl_display);
5873 static void launch_desktop_shell_process(void *data);
5876 respawn_desktop_shell_process(struct desktop_shell *shell)
5880 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
5881 time = weston_compositor_get_time();
5882 if (time - shell->child.deathstamp > 30000) {
5883 shell->child.deathstamp = time;
5884 shell->child.deathcount = 0;
5887 shell->child.deathcount++;
5888 if (shell->child.deathcount > 5) {
5889 weston_log("%s disconnected, giving up.\n", shell->client);
5893 weston_log("%s disconnected, respawning...\n", shell->client);
5894 launch_desktop_shell_process(shell);
5898 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
5900 struct desktop_shell *shell;
5902 shell = container_of(listener, struct desktop_shell,
5903 child.client_destroy_listener);
5905 wl_list_remove(&shell->child.client_destroy_listener.link);
5906 shell->child.client = NULL;
5908 * unbind_desktop_shell() will reset shell->child.desktop_shell
5909 * before the respawned process has a chance to create a new
5910 * desktop_shell object, because we are being called from the
5911 * wl_client destructor which destroys all wl_resources before
5915 if (!check_desktop_shell_crash_too_early(shell))
5916 respawn_desktop_shell_process(shell);
5918 shell_fade_startup(shell);
5922 launch_desktop_shell_process(void *data)
5924 struct desktop_shell *shell = data;
5926 shell->child.client = weston_client_start(shell->compositor,
5929 if (!shell->child.client) {
5930 weston_log("not able to start %s\n", shell->client);
5934 shell->child.client_destroy_listener.notify =
5935 desktop_shell_client_destroy;
5936 wl_client_add_destroy_listener(shell->child.client,
5937 &shell->child.client_destroy_listener);
5941 handle_shell_client_destroy(struct wl_listener *listener, void *data)
5943 struct shell_client *sc =
5944 container_of(listener, struct shell_client, destroy_listener);
5945 struct wl_resource *shsurf_resource;
5946 struct shell_surface *shsurf;
5949 wl_event_source_remove(sc->ping_timer);
5951 /* Since we're about to free shell_client, we remove it from the
5952 * head of the surface list so we don't use that freed list node
5953 * during surface clean up later on.
5955 wl_resource_for_each(shsurf_resource, &sc->surface_list) {
5956 shsurf = wl_resource_get_user_data(shsurf_resource);
5957 shsurf->owner = NULL;
5959 wl_list_remove(&sc->surface_list);
5964 static struct shell_client *
5965 shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5966 const struct wl_interface *interface, uint32_t id)
5968 struct shell_client *sc;
5970 sc = zalloc(sizeof *sc);
5972 wl_client_post_no_memory(client);
5976 sc->resource = wl_resource_create(client, interface, 1, id);
5977 if (sc->resource == NULL) {
5979 wl_client_post_no_memory(client);
5983 sc->client = client;
5985 sc->destroy_listener.notify = handle_shell_client_destroy;
5986 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5987 wl_list_init(&sc->surface_list);
5993 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5995 struct desktop_shell *shell = data;
5996 struct shell_client *sc;
5998 sc = shell_client_create(client, shell, &wl_shell_interface, id);
6000 wl_resource_set_implementation(sc->resource,
6001 &shell_implementation,
6006 bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
6008 struct desktop_shell *shell = data;
6009 struct shell_client *sc;
6011 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
6013 wl_resource_set_dispatcher(sc->resource,
6014 xdg_shell_unversioned_dispatch,
6019 unbind_desktop_shell(struct wl_resource *resource)
6021 struct desktop_shell *shell = wl_resource_get_user_data(resource);
6024 resume_desktop(shell);
6026 shell->child.desktop_shell = NULL;
6027 shell->prepare_event_sent = false;
6031 bind_desktop_shell(struct wl_client *client,
6032 void *data, uint32_t version, uint32_t id)
6034 struct desktop_shell *shell = data;
6035 struct wl_resource *resource;
6037 resource = wl_resource_create(client, &weston_desktop_shell_interface,
6040 if (client == shell->child.client) {
6041 wl_resource_set_implementation(resource,
6042 &desktop_shell_implementation,
6043 shell, unbind_desktop_shell);
6044 shell->child.desktop_shell = resource;
6048 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
6049 "permission to bind desktop_shell denied");
6053 struct desktop_shell *shell;
6054 struct weston_view *current;
6055 struct wl_listener listener;
6056 struct weston_keyboard_grab grab;
6057 struct wl_array minimized_array;
6061 switcher_next(struct switcher *switcher)
6063 struct weston_view *view;
6064 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
6065 struct shell_surface *shsurf;
6066 struct workspace *ws = get_current_workspace(switcher->shell);
6068 /* temporary re-display minimized surfaces */
6069 struct weston_view *tmp;
6070 struct weston_view **minimized;
6071 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
6072 weston_layer_entry_remove(&view->layer_link);
6073 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
6074 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
6078 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
6079 shsurf = get_shell_surface(view->surface);
6081 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
6082 shsurf->parent == NULL) {
6085 if (prev == switcher->current)
6089 weston_view_geometry_dirty(view);
6090 weston_surface_damage(view->surface);
6093 if (is_black_surface_view(view, NULL)) {
6095 weston_view_geometry_dirty(view);
6096 weston_surface_damage(view->surface);
6106 wl_list_remove(&switcher->listener.link);
6107 wl_signal_add(&next->destroy_signal, &switcher->listener);
6109 switcher->current = next;
6110 wl_list_for_each(view, &next->surface->views, surface_link)
6113 shsurf = get_shell_surface(switcher->current->surface);
6114 if (shsurf && shsurf->state.fullscreen)
6115 shsurf->fullscreen.black_view->alpha = 1.0;
6119 switcher_handle_view_destroy(struct wl_listener *listener, void *data)
6121 struct switcher *switcher =
6122 container_of(listener, struct switcher, listener);
6124 switcher_next(switcher);
6128 switcher_destroy(struct switcher *switcher)
6130 struct weston_view *view;
6131 struct weston_keyboard *keyboard = switcher->grab.keyboard;
6132 struct workspace *ws = get_current_workspace(switcher->shell);
6134 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
6135 if (is_focus_view(view))
6139 weston_surface_damage(view->surface);
6142 if (switcher->current) {
6143 activate(switcher->shell, switcher->current,
6145 WESTON_ACTIVATE_FLAG_CONFIGURE);
6148 wl_list_remove(&switcher->listener.link);
6149 weston_keyboard_end_grab(keyboard);
6150 if (keyboard->input_method_resource)
6151 keyboard->grab = &keyboard->input_method_grab;
6153 /* re-hide surfaces that were temporary shown during the switch */
6154 struct weston_view **minimized;
6155 wl_array_for_each(minimized, &switcher->minimized_array) {
6156 /* with the exception of the current selected */
6157 if ((*minimized)->surface != switcher->current->surface) {
6158 weston_layer_entry_remove(&(*minimized)->layer_link);
6159 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
6160 weston_view_damage_below(*minimized);
6163 wl_array_release(&switcher->minimized_array);
6169 switcher_key(struct weston_keyboard_grab *grab,
6170 uint32_t time, uint32_t key, uint32_t state_w)
6172 struct switcher *switcher = container_of(grab, struct switcher, grab);
6173 enum wl_keyboard_key_state state = state_w;
6175 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
6176 switcher_next(switcher);
6180 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
6181 uint32_t mods_depressed, uint32_t mods_latched,
6182 uint32_t mods_locked, uint32_t group)
6184 struct switcher *switcher = container_of(grab, struct switcher, grab);
6185 struct weston_seat *seat = grab->keyboard->seat;
6187 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
6188 switcher_destroy(switcher);
6192 switcher_cancel(struct weston_keyboard_grab *grab)
6194 struct switcher *switcher = container_of(grab, struct switcher, grab);
6196 switcher_destroy(switcher);
6199 static const struct weston_keyboard_grab_interface switcher_grab = {
6206 switcher_binding(struct weston_keyboard *keyboard, uint32_t time,
6207 uint32_t key, void *data)
6209 struct desktop_shell *shell = data;
6210 struct switcher *switcher;
6212 switcher = malloc(sizeof *switcher);
6213 switcher->shell = shell;
6214 switcher->current = NULL;
6215 switcher->listener.notify = switcher_handle_view_destroy;
6216 wl_list_init(&switcher->listener.link);
6217 wl_array_init(&switcher->minimized_array);
6219 restore_all_output_modes(shell->compositor);
6220 lower_fullscreen_layer(switcher->shell, NULL);
6221 switcher->grab.interface = &switcher_grab;
6222 weston_keyboard_start_grab(keyboard, &switcher->grab);
6223 weston_keyboard_set_focus(keyboard, NULL);
6224 switcher_next(switcher);
6228 backlight_binding(struct weston_keyboard *keyboard, uint32_t time,
6229 uint32_t key, void *data)
6231 struct weston_compositor *compositor = data;
6232 struct weston_output *output;
6233 long backlight_new = 0;
6235 /* TODO: we're limiting to simple use cases, where we assume just
6236 * control on the primary display. We'd have to extend later if we
6237 * ever get support for setting backlights on random desktop LCD
6239 output = get_default_output(compositor);
6243 if (!output->set_backlight)
6246 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
6247 backlight_new = output->backlight_current - 25;
6248 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
6249 backlight_new = output->backlight_current + 25;
6251 if (backlight_new < 5)
6253 if (backlight_new > 255)
6254 backlight_new = 255;
6256 output->backlight_current = backlight_new;
6257 output->set_backlight(output, output->backlight_current);
6261 force_kill_binding(struct weston_keyboard *keyboard, uint32_t time,
6262 uint32_t key, void *data)
6264 struct weston_surface *focus_surface;
6265 struct wl_client *client;
6266 struct desktop_shell *shell = data;
6267 struct weston_compositor *compositor = shell->compositor;
6270 focus_surface = keyboard->focus;
6274 wl_signal_emit(&compositor->kill_signal, focus_surface);
6276 client = wl_resource_get_client(focus_surface->resource);
6277 wl_client_get_credentials(client, &pid, NULL, NULL);
6279 /* Skip clients that we launched ourselves (the credentials of
6280 * the socketpair is ours) */
6281 if (pid == getpid())
6288 workspace_up_binding(struct weston_keyboard *keyboard, uint32_t time,
6289 uint32_t key, void *data)
6291 struct desktop_shell *shell = data;
6292 unsigned int new_index = shell->workspaces.current;
6299 change_workspace(shell, new_index);
6303 workspace_down_binding(struct weston_keyboard *keyboard, uint32_t time,
6304 uint32_t key, void *data)
6306 struct desktop_shell *shell = data;
6307 unsigned int new_index = shell->workspaces.current;
6311 if (new_index < shell->workspaces.num - 1)
6314 change_workspace(shell, new_index);
6318 workspace_f_binding(struct weston_keyboard *keyboard, uint32_t time,
6319 uint32_t key, void *data)
6321 struct desktop_shell *shell = data;
6322 unsigned int new_index;
6326 new_index = key - KEY_F1;
6327 if (new_index >= shell->workspaces.num)
6328 new_index = shell->workspaces.num - 1;
6330 change_workspace(shell, new_index);
6334 workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
6335 uint32_t time, uint32_t key, void *data)
6337 struct desktop_shell *shell = data;
6338 unsigned int new_index = shell->workspaces.current;
6346 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
6350 workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
6351 uint32_t time, uint32_t key, void *data)
6353 struct desktop_shell *shell = data;
6354 unsigned int new_index = shell->workspaces.current;
6359 if (new_index < shell->workspaces.num - 1)
6362 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
6366 shell_reposition_view_on_output_destroy(struct weston_view *view)
6368 struct weston_output *output, *first_output;
6369 struct weston_compositor *ec = view->surface->compositor;
6370 struct shell_surface *shsurf;
6374 x = view->geometry.x;
6375 y = view->geometry.y;
6377 /* At this point the destroyed output is not in the list anymore.
6378 * If the view is still visible somewhere, we leave where it is,
6379 * otherwise, move it to the first output. */
6381 wl_list_for_each(output, &ec->output_list, link) {
6382 if (pixman_region32_contains_point(&output->region,
6390 first_output = container_of(ec->output_list.next,
6391 struct weston_output, link);
6393 x = first_output->x + first_output->width / 4;
6394 y = first_output->y + first_output->height / 4;
6396 weston_view_set_position(view, x, y);
6398 weston_view_geometry_dirty(view);
6402 shsurf = get_shell_surface(view->surface);
6405 shsurf->saved_position_valid = false;
6406 shsurf->next_state.maximized = false;
6407 shsurf->next_state.fullscreen = false;
6408 shsurf->state_changed = true;
6413 shell_for_each_layer(struct desktop_shell *shell,
6414 shell_for_each_layer_func_t func, void *data)
6416 struct workspace **ws;
6418 func(shell, &shell->fullscreen_layer, data);
6419 func(shell, &shell->panel_layer, data);
6420 func(shell, &shell->background_layer, data);
6421 func(shell, &shell->lock_layer, data);
6422 func(shell, &shell->input_panel_layer, data);
6424 wl_array_for_each(ws, &shell->workspaces.array)
6425 func(shell, &(*ws)->layer, data);
6429 shell_output_destroy_move_layer(struct desktop_shell *shell,
6430 struct weston_layer *layer,
6433 struct weston_output *output = data;
6434 struct weston_view *view;
6436 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6437 if (view->output != output)
6440 shell_reposition_view_on_output_destroy(view);
6445 handle_output_destroy(struct wl_listener *listener, void *data)
6447 struct shell_output *output_listener =
6448 container_of(listener, struct shell_output, destroy_listener);
6449 struct weston_output *output = output_listener->output;
6450 struct desktop_shell *shell = output_listener->shell;
6452 shell_for_each_layer(shell, shell_output_destroy_move_layer, output);
6454 wl_list_remove(&output_listener->destroy_listener.link);
6455 wl_list_remove(&output_listener->link);
6456 free(output_listener);
6460 shell_resize_surface_to_output(struct desktop_shell *shell,
6461 struct weston_surface *surface,
6462 const struct weston_output *output)
6467 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
6475 handle_output_resized(struct wl_listener *listener, void *data)
6477 struct desktop_shell *shell =
6478 container_of(listener, struct desktop_shell, resized_listener);
6479 struct weston_output *output = (struct weston_output *)data;
6480 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
6482 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
6483 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
6487 create_shell_output(struct desktop_shell *shell,
6488 struct weston_output *output)
6490 struct shell_output *shell_output;
6492 shell_output = zalloc(sizeof *shell_output);
6493 if (shell_output == NULL)
6496 shell_output->output = output;
6497 shell_output->shell = shell;
6498 shell_output->destroy_listener.notify = handle_output_destroy;
6499 wl_signal_add(&output->destroy_signal,
6500 &shell_output->destroy_listener);
6501 wl_list_insert(shell->output_list.prev, &shell_output->link);
6505 handle_output_create(struct wl_listener *listener, void *data)
6507 struct desktop_shell *shell =
6508 container_of(listener, struct desktop_shell, output_create_listener);
6509 struct weston_output *output = (struct weston_output *)data;
6511 create_shell_output(shell, output);
6515 handle_output_move_layer(struct desktop_shell *shell,
6516 struct weston_layer *layer, void *data)
6518 struct weston_output *output = data;
6519 struct weston_view *view;
6522 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
6523 if (view->output != output)
6526 x = view->geometry.x + output->move_x;
6527 y = view->geometry.y + output->move_y;
6528 weston_view_set_position(view, x, y);
6533 handle_output_move(struct wl_listener *listener, void *data)
6535 struct desktop_shell *shell;
6537 shell = container_of(listener, struct desktop_shell,
6538 output_move_listener);
6540 shell_for_each_layer(shell, handle_output_move_layer, data);
6544 setup_output_destroy_handler(struct weston_compositor *ec,
6545 struct desktop_shell *shell)
6547 struct weston_output *output;
6549 wl_list_init(&shell->output_list);
6550 wl_list_for_each(output, &ec->output_list, link)
6551 create_shell_output(shell, output);
6553 shell->output_create_listener.notify = handle_output_create;
6554 wl_signal_add(&ec->output_created_signal,
6555 &shell->output_create_listener);
6557 shell->output_move_listener.notify = handle_output_move;
6558 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
6562 shell_destroy(struct wl_listener *listener, void *data)
6564 struct desktop_shell *shell =
6565 container_of(listener, struct desktop_shell, destroy_listener);
6566 struct workspace **ws;
6567 struct shell_output *shell_output, *tmp;
6569 /* Force state to unlocked so we don't try to fade */
6570 shell->locked = false;
6572 if (shell->child.client) {
6573 /* disable respawn */
6574 wl_list_remove(&shell->child.client_destroy_listener.link);
6575 wl_client_destroy(shell->child.client);
6578 wl_list_remove(&shell->idle_listener.link);
6579 wl_list_remove(&shell->wake_listener.link);
6580 wl_list_remove(&shell->transform_listener.link);
6582 text_backend_destroy(shell->text_backend);
6583 input_panel_destroy(shell);
6585 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
6586 wl_list_remove(&shell_output->destroy_listener.link);
6587 wl_list_remove(&shell_output->link);
6591 wl_list_remove(&shell->output_create_listener.link);
6592 wl_list_remove(&shell->output_move_listener.link);
6593 wl_list_remove(&shell->resized_listener.link);
6595 wl_array_for_each(ws, &shell->workspaces.array)
6596 workspace_destroy(*ws);
6597 wl_array_release(&shell->workspaces.array);
6599 free(shell->client);
6604 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
6607 int i, num_workspace_bindings;
6609 if (shell->allow_zap)
6610 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
6611 MODIFIER_CTRL | MODIFIER_ALT,
6612 terminate_binding, ec);
6614 /* fixed bindings */
6615 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
6616 click_to_activate_binding,
6618 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
6619 click_to_activate_binding,
6621 weston_compositor_add_touch_binding(ec, 0,
6622 touch_to_activate_binding,
6624 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6625 MODIFIER_SUPER | MODIFIER_ALT,
6626 surface_opacity_binding, NULL);
6627 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
6628 MODIFIER_SUPER, zoom_axis_binding,
6630 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
6631 backlight_binding, ec);
6632 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
6633 backlight_binding, ec);
6635 /* configurable bindings */
6636 if (shell->exposay_modifier)
6637 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
6638 exposay_binding, shell);
6640 mod = shell->binding_modifier;
6644 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
6645 zoom_key_binding, NULL);
6646 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
6647 zoom_key_binding, NULL);
6648 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
6649 maximize_binding, NULL);
6650 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
6651 fullscreen_binding, NULL);
6652 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
6654 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
6655 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
6656 resize_binding, shell);
6657 weston_compositor_add_button_binding(ec, BTN_LEFT,
6658 mod | MODIFIER_SHIFT,
6659 resize_binding, shell);
6661 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
6662 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
6663 rotate_binding, NULL);
6665 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
6667 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
6669 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
6671 weston_compositor_add_key_binding(ec, KEY_K, mod,
6672 force_kill_binding, shell);
6673 weston_compositor_add_key_binding(ec, KEY_UP, mod,
6674 workspace_up_binding, shell);
6675 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
6676 workspace_down_binding, shell);
6677 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
6678 workspace_move_surface_up_binding,
6680 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
6681 workspace_move_surface_down_binding,
6684 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
6685 if (shell->workspaces.num > 1) {
6686 num_workspace_bindings = shell->workspaces.num;
6687 if (num_workspace_bindings > 6)
6688 num_workspace_bindings = 6;
6689 for (i = 0; i < num_workspace_bindings; i++)
6690 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
6691 workspace_f_binding,
6695 weston_install_debug_key_binding(ec, mod);
6699 handle_seat_created(struct wl_listener *listener, void *data)
6701 struct weston_seat *seat = data;
6703 create_shell_seat(seat);
6707 module_init(struct weston_compositor *ec,
6708 int *argc, char *argv[])
6710 struct weston_seat *seat;
6711 struct desktop_shell *shell;
6712 struct workspace **pws;
6714 struct wl_event_loop *loop;
6716 shell = zalloc(sizeof *shell);
6720 shell->compositor = ec;
6722 shell->destroy_listener.notify = shell_destroy;
6723 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
6724 shell->idle_listener.notify = idle_handler;
6725 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
6726 shell->wake_listener.notify = wake_handler;
6727 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
6728 shell->transform_listener.notify = transform_handler;
6729 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
6731 ec->shell_interface.shell = shell;
6732 ec->shell_interface.create_shell_surface = create_shell_surface;
6733 ec->shell_interface.set_toplevel = set_toplevel;
6734 ec->shell_interface.set_transient = set_transient;
6735 ec->shell_interface.set_fullscreen = shell_interface_set_fullscreen;
6736 ec->shell_interface.set_xwayland = set_xwayland;
6737 ec->shell_interface.move = shell_interface_move;
6738 ec->shell_interface.resize = shell_interface_resize;
6739 ec->shell_interface.set_title = set_title;
6740 ec->shell_interface.set_window_geometry = set_window_geometry;
6741 ec->shell_interface.set_maximized = shell_interface_set_maximized;
6742 ec->shell_interface.set_pid = set_pid;
6743 ec->shell_interface.get_output_work_area = get_output_work_area;
6745 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
6746 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
6747 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
6748 weston_layer_init(&shell->lock_layer, NULL);
6749 weston_layer_init(&shell->input_panel_layer, NULL);
6751 wl_array_init(&shell->workspaces.array);
6752 wl_list_init(&shell->workspaces.client_list);
6754 if (input_panel_setup(shell) < 0)
6757 shell->text_backend = text_backend_init(ec);
6758 if (!shell->text_backend)
6761 shell_configuration(shell);
6763 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
6764 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
6766 for (i = 0; i < shell->workspaces.num; i++) {
6767 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
6771 *pws = workspace_create();
6775 activate_workspace(shell, 0);
6777 weston_layer_init(&shell->minimized_layer, NULL);
6779 wl_list_init(&shell->workspaces.anim_sticky_list);
6780 wl_list_init(&shell->workspaces.animation.link);
6781 shell->workspaces.animation.frame = animate_workspace_change_frame;
6783 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
6784 shell, bind_shell) == NULL)
6787 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6788 shell, bind_xdg_shell) == NULL)
6791 if (wl_global_create(ec->wl_display,
6792 &weston_desktop_shell_interface, 1,
6793 shell, bind_desktop_shell) == NULL)
6796 shell->child.deathstamp = weston_compositor_get_time();
6798 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
6800 setup_output_destroy_handler(ec, shell);
6802 loop = wl_display_get_event_loop(ec->wl_display);
6803 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
6805 wl_list_for_each(seat, &ec->seat_list, link)
6806 handle_seat_created(NULL, seat);
6807 shell->seat_create_listener.notify = handle_seat_created;
6808 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
6810 shell->resized_listener.notify = handle_output_resized;
6811 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
6813 screenshooter_create(ec);
6815 shell_add_bindings(ec, shell);
6817 shell_fade_init(shell);
6819 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);