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 "shared/timespec-util.h"
45 #include "libweston-desktop/libweston-desktop.h"
47 #define DEFAULT_NUM_WORKSPACES 1
48 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
51 struct desktop_shell *shell;
52 struct weston_seat *seat;
54 struct weston_surface *keyboard_focus;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
61 * Surface stacking and ordering.
63 * This is handled using several linked lists of surfaces, organised into
64 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
65 * above all of the surfaces in the layer below. The set of layers is static and
66 * in the following order (top-most first):
67 * • Lock layer (only ever displayed on its own)
75 * The list of layers may be manipulated to remove whole layers of surfaces from
76 * display. For example, when locking the screen, all layers except the lock
79 * A surface’s layer is modified on configuring the surface, in
80 * set_surface_type() (which is only called when the surface’s type change is
81 * _committed_). If a surface’s type changes (e.g. when making a window
82 * fullscreen) its layer changes too.
84 * In order to allow popup and transient surfaces to be correctly stacked above
85 * their parent surfaces, each surface tracks both its parent surface, and a
86 * linked list of its children. When a surface’s layer is updated, so are the
87 * layers of its children. Note that child surfaces are *not* the same as
88 * subsurfaces — child/parent surfaces are purely for maintaining stacking
91 * The children_link list of siblings of a surface (i.e. those surfaces which
92 * have the same parent) only contains weston_surfaces which have a
93 * shell_surface. Stacking is not implemented for non-shell_surface
94 * weston_surfaces. This means that the following implication does *not* hold:
95 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
98 struct shell_surface {
99 struct wl_signal destroy_signal;
101 struct weston_desktop_surface *desktop_surface;
102 struct weston_view *view;
103 int32_t last_width, last_height;
105 struct desktop_shell *shell;
107 int32_t saved_x, saved_y;
108 bool saved_position_valid;
109 bool saved_rotation_valid;
110 int unresponsive, grabbed;
111 uint32_t resize_edges;
114 struct weston_transform transform;
115 struct weston_matrix rotation;
119 struct weston_transform transform; /* matrix from x, y */
120 struct weston_view *black_view;
123 struct weston_transform workspace_transform;
125 struct weston_output *fullscreen_output;
126 struct weston_output *output;
127 struct wl_listener output_destroy_listener;
129 struct surface_state {
147 struct weston_pointer_grab grab;
148 struct shell_surface *shsurf;
149 struct wl_listener shsurf_destroy_listener;
152 struct shell_touch_grab {
153 struct weston_touch_grab grab;
154 struct shell_surface *shsurf;
155 struct wl_listener shsurf_destroy_listener;
156 struct weston_touch *touch;
159 struct weston_move_grab {
160 struct shell_grab base;
162 bool client_initiated;
165 struct weston_touch_move_grab {
166 struct shell_touch_grab base;
172 struct shell_grab base;
173 struct weston_matrix rotation;
181 struct weston_seat *seat;
182 struct wl_listener seat_destroy_listener;
183 struct weston_surface *focused_surface;
185 struct wl_listener caps_changed_listener;
186 struct wl_listener pointer_focus_listener;
187 struct wl_listener keyboard_focus_listener;
191 static struct desktop_shell *
192 shell_surface_get_shell(struct shell_surface *shsurf);
195 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
198 surface_rotate(struct shell_surface *surface, struct weston_pointer *pointer);
201 shell_fade_startup(struct desktop_shell *shell);
204 shell_fade(struct desktop_shell *shell, enum fade_type type);
206 static struct shell_seat *
207 get_shell_seat(struct weston_seat *seat);
210 get_output_panel_size(struct desktop_shell *shell,
211 struct weston_output *output,
212 int *width, int *height);
215 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
218 shell_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
221 struct weston_desktop_surface *desktop_surface =
222 weston_surface_get_desktop_surface(surface);
224 t = weston_desktop_surface_get_title(desktop_surface);
225 c = weston_desktop_surface_get_app_id(desktop_surface);
227 return snprintf(buf, len, "%s window%s%s%s%s%s",
229 t ? " '" : "", t ?: "", t ? "'" : "",
230 c ? " of " : "", c ?: "");
234 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
236 struct shell_grab *grab;
238 grab = container_of(listener, struct shell_grab,
239 shsurf_destroy_listener);
245 get_default_view(struct weston_surface *surface)
247 struct shell_surface *shsurf;
248 struct weston_view *view;
250 if (!surface || wl_list_empty(&surface->views))
253 shsurf = get_shell_surface(surface);
257 wl_list_for_each(view, &surface->views, surface_link)
258 if (weston_view_is_mapped(view))
261 return container_of(surface->views.next, struct weston_view, surface_link);
265 shell_grab_start(struct shell_grab *grab,
266 const struct weston_pointer_grab_interface *interface,
267 struct shell_surface *shsurf,
268 struct weston_pointer *pointer,
269 enum weston_desktop_shell_cursor cursor)
271 struct desktop_shell *shell = shsurf->shell;
273 weston_seat_break_desktop_grabs(pointer->seat);
275 grab->grab.interface = interface;
276 grab->shsurf = shsurf;
277 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
278 wl_signal_add(&shsurf->destroy_signal,
279 &grab->shsurf_destroy_listener);
282 weston_pointer_start_grab(pointer, &grab->grab);
283 if (shell->child.desktop_shell) {
284 weston_desktop_shell_send_grab_cursor(shell->child.desktop_shell,
286 weston_pointer_set_focus(pointer,
287 get_default_view(shell->grab_surface),
288 wl_fixed_from_int(0),
289 wl_fixed_from_int(0));
294 get_panel_size(struct desktop_shell *shell,
295 struct weston_view *view,
301 weston_view_to_global_float(view, 0, 0, &x1, &y1);
302 weston_view_to_global_float(view,
303 view->surface->width,
304 view->surface->height,
306 *width = (int)(x2 - x1);
307 *height = (int)(y2 - y1);
311 get_output_panel_size(struct desktop_shell *shell,
312 struct weston_output *output,
316 struct weston_view *view;
324 wl_list_for_each(view, &shell->panel_layer.view_list.link, layer_link.link) {
325 if (view->surface->output == output) {
326 get_panel_size(shell, view, width, height);
331 /* the correct view wasn't found */
335 get_output_work_area(struct desktop_shell *shell,
336 struct weston_output *output,
337 pixman_rectangle32_t *area)
339 int32_t panel_width = 0, panel_height = 0;
353 get_output_panel_size(shell, output, &panel_width, &panel_height);
354 switch (shell->panel_position) {
355 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
357 area->y += panel_height;
359 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
360 area->width = output->width;
361 area->height = output->height - panel_height;
363 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
364 area->x += panel_width;
366 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
367 area->width = output->width - panel_width;
368 area->height = output->height;
374 shell_grab_end(struct shell_grab *grab)
377 wl_list_remove(&grab->shsurf_destroy_listener.link);
378 grab->shsurf->grabbed = 0;
380 if (grab->shsurf->resize_edges) {
381 grab->shsurf->resize_edges = 0;
385 weston_pointer_end_grab(grab->grab.pointer);
389 shell_touch_grab_start(struct shell_touch_grab *grab,
390 const struct weston_touch_grab_interface *interface,
391 struct shell_surface *shsurf,
392 struct weston_touch *touch)
394 struct desktop_shell *shell = shsurf->shell;
396 weston_seat_break_desktop_grabs(touch->seat);
398 grab->grab.interface = interface;
399 grab->shsurf = shsurf;
400 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
401 wl_signal_add(&shsurf->destroy_signal,
402 &grab->shsurf_destroy_listener);
407 weston_touch_start_grab(touch, &grab->grab);
408 if (shell->child.desktop_shell)
409 weston_touch_set_focus(touch,
410 get_default_view(shell->grab_surface));
414 shell_touch_grab_end(struct shell_touch_grab *grab)
417 wl_list_remove(&grab->shsurf_destroy_listener.link);
418 grab->shsurf->grabbed = 0;
421 weston_touch_end_grab(grab->touch);
425 center_on_output(struct weston_view *view,
426 struct weston_output *output);
428 static enum weston_keyboard_modifier
429 get_modifier(char *modifier)
432 return MODIFIER_SUPER;
434 if (!strcmp("ctrl", modifier))
435 return MODIFIER_CTRL;
436 else if (!strcmp("alt", modifier))
438 else if (!strcmp("super", modifier))
439 return MODIFIER_SUPER;
440 else if (!strcmp("none", modifier))
443 return MODIFIER_SUPER;
446 static enum animation_type
447 get_animation_type(char *animation)
450 return ANIMATION_NONE;
452 if (!strcmp("zoom", animation))
453 return ANIMATION_ZOOM;
454 else if (!strcmp("fade", animation))
455 return ANIMATION_FADE;
456 else if (!strcmp("dim-layer", animation))
457 return ANIMATION_DIM_LAYER;
459 return ANIMATION_NONE;
463 shell_configuration(struct desktop_shell *shell)
465 struct weston_config_section *section;
469 section = weston_config_get_section(wet_get_config(shell->compositor),
470 "shell", NULL, NULL);
471 client = wet_get_binary_path(WESTON_SHELL_CLIENT);
472 weston_config_section_get_string(section, "client", &s, client);
476 weston_config_section_get_bool(section,
477 "allow-zap", &allow_zap, true);
478 shell->allow_zap = allow_zap;
480 weston_config_section_get_string(section,
481 "binding-modifier", &s, "super");
482 shell->binding_modifier = get_modifier(s);
485 weston_config_section_get_string(section,
486 "exposay-modifier", &s, "none");
487 shell->exposay_modifier = get_modifier(s);
490 weston_config_section_get_string(section, "animation", &s, "none");
491 shell->win_animation_type = get_animation_type(s);
493 weston_config_section_get_string(section, "close-animation", &s, "fade");
494 shell->win_close_animation_type = get_animation_type(s);
496 weston_config_section_get_string(section,
497 "startup-animation", &s, "fade");
498 shell->startup_animation_type = get_animation_type(s);
500 if (shell->startup_animation_type == ANIMATION_ZOOM)
501 shell->startup_animation_type = ANIMATION_NONE;
502 weston_config_section_get_string(section, "focus-animation", &s, "none");
503 shell->focus_animation_type = get_animation_type(s);
505 weston_config_section_get_uint(section, "num-workspaces",
506 &shell->workspaces.num,
507 DEFAULT_NUM_WORKSPACES);
510 struct weston_output *
511 get_default_output(struct weston_compositor *compositor)
513 if (wl_list_empty(&compositor->output_list))
516 return container_of(compositor->output_list.next,
517 struct weston_output, link);
521 focus_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
523 return snprintf(buf, len, "focus highlight effect for output %s",
524 surface->output->name);
527 /* no-op func for checking focus surface */
529 focus_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
533 static struct focus_surface *
534 get_focus_surface(struct weston_surface *surface)
536 if (surface->committed == focus_surface_committed)
537 return surface->committed_private;
543 is_focus_surface (struct weston_surface *es)
545 return (es->committed == focus_surface_committed);
549 is_focus_view (struct weston_view *view)
551 return is_focus_surface (view->surface);
554 static struct focus_surface *
555 create_focus_surface(struct weston_compositor *ec,
556 struct weston_output *output)
558 struct focus_surface *fsurf = NULL;
559 struct weston_surface *surface = NULL;
561 fsurf = malloc(sizeof *fsurf);
565 fsurf->surface = weston_surface_create(ec);
566 surface = fsurf->surface;
567 if (surface == NULL) {
572 surface->committed = focus_surface_committed;
573 surface->output = output;
574 surface->is_mapped = true;
575 surface->committed_private = fsurf;
576 weston_surface_set_label_func(surface, focus_surface_get_label);
578 fsurf->view = weston_view_create(surface);
579 if (fsurf->view == NULL) {
580 weston_surface_destroy(surface);
584 weston_view_set_output(fsurf->view, output);
585 fsurf->view->is_mapped = true;
587 weston_surface_set_size(surface, output->width, output->height);
588 weston_view_set_position(fsurf->view, output->x, output->y);
589 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
590 pixman_region32_fini(&surface->opaque);
591 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
592 output->width, output->height);
593 pixman_region32_fini(&surface->input);
594 pixman_region32_init(&surface->input);
596 wl_list_init(&fsurf->workspace_transform.link);
602 focus_surface_destroy(struct focus_surface *fsurf)
604 weston_surface_destroy(fsurf->surface);
609 focus_animation_done(struct weston_view_animation *animation, void *data)
611 struct workspace *ws = data;
613 ws->focus_animation = NULL;
617 focus_state_destroy(struct focus_state *state)
619 wl_list_remove(&state->seat_destroy_listener.link);
620 wl_list_remove(&state->surface_destroy_listener.link);
625 focus_state_seat_destroy(struct wl_listener *listener, void *data)
627 struct focus_state *state = container_of(listener,
629 seat_destroy_listener);
631 wl_list_remove(&state->link);
632 focus_state_destroy(state);
636 focus_state_surface_destroy(struct wl_listener *listener, void *data)
638 struct focus_state *state = container_of(listener,
640 surface_destroy_listener);
641 struct weston_surface *main_surface;
642 struct weston_view *next;
643 struct weston_view *view;
645 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
648 wl_list_for_each(view,
649 &state->ws->layer.view_list.link, layer_link.link) {
650 if (view->surface == main_surface)
652 if (is_focus_view(view))
654 if (!get_shell_surface(view->surface))
661 /* if the focus was a sub-surface, activate its main surface */
662 if (main_surface != state->keyboard_focus)
663 next = get_default_view(main_surface);
666 state->keyboard_focus = NULL;
667 activate(state->shell, next, state->seat,
668 WESTON_ACTIVATE_FLAG_CONFIGURE);
670 if (state->shell->focus_animation_type == ANIMATION_DIM_LAYER) {
671 if (state->ws->focus_animation)
672 weston_view_animation_destroy(state->ws->focus_animation);
674 state->ws->focus_animation = weston_fade_run(
675 state->ws->fsurf_front->view,
676 state->ws->fsurf_front->view->alpha, 0.0, 300,
677 focus_animation_done, state->ws);
680 wl_list_remove(&state->link);
681 focus_state_destroy(state);
685 static struct focus_state *
686 focus_state_create(struct desktop_shell *shell, struct weston_seat *seat,
687 struct workspace *ws)
689 struct focus_state *state;
691 state = malloc(sizeof *state);
695 state->shell = shell;
696 state->keyboard_focus = NULL;
699 wl_list_insert(&ws->focus_list, &state->link);
701 state->seat_destroy_listener.notify = focus_state_seat_destroy;
702 state->surface_destroy_listener.notify = focus_state_surface_destroy;
703 wl_signal_add(&seat->destroy_signal,
704 &state->seat_destroy_listener);
705 wl_list_init(&state->surface_destroy_listener.link);
710 static struct focus_state *
711 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
713 struct workspace *ws = get_current_workspace(shell);
714 struct focus_state *state;
716 wl_list_for_each(state, &ws->focus_list, link)
717 if (state->seat == seat)
720 if (&state->link == &ws->focus_list)
721 state = focus_state_create(shell, seat, ws);
727 focus_state_set_focus(struct focus_state *state,
728 struct weston_surface *surface)
730 if (state->keyboard_focus) {
731 wl_list_remove(&state->surface_destroy_listener.link);
732 wl_list_init(&state->surface_destroy_listener.link);
735 state->keyboard_focus = surface;
737 wl_signal_add(&surface->destroy_signal,
738 &state->surface_destroy_listener);
742 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
744 struct focus_state *state, *next;
745 struct weston_surface *surface;
746 struct wl_list pending_seat_list;
747 struct weston_seat *seat, *next_seat;
749 /* Temporarily steal the list of seats so that we can keep
750 * track of the seats we've already processed */
751 wl_list_init(&pending_seat_list);
752 wl_list_insert_list(&pending_seat_list, &shell->compositor->seat_list);
753 wl_list_init(&shell->compositor->seat_list);
755 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
756 struct weston_keyboard *keyboard =
757 weston_seat_get_keyboard(state->seat);
759 wl_list_remove(&state->seat->link);
760 wl_list_insert(&shell->compositor->seat_list,
766 surface = state->keyboard_focus;
768 weston_keyboard_set_focus(keyboard, surface);
771 /* For any remaining seats that we don't have a focus state
772 * for we'll reset the keyboard focus to NULL */
773 wl_list_for_each_safe(seat, next_seat, &pending_seat_list, link) {
774 struct weston_keyboard *keyboard =
775 weston_seat_get_keyboard(seat);
777 wl_list_insert(&shell->compositor->seat_list, &seat->link);
782 weston_keyboard_set_focus(keyboard, NULL);
787 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
788 struct weston_seat *seat)
790 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
791 struct focus_state *state;
793 wl_list_for_each(state, &ws->focus_list, link) {
794 if (state->seat == seat) {
795 focus_state_set_focus(state, keyboard->focus);
802 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
803 struct weston_surface *surface)
805 struct focus_state *state;
807 wl_list_for_each(state, &ws->focus_list, link)
808 if (state->keyboard_focus == surface)
809 focus_state_set_focus(state, NULL);
813 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
814 struct weston_view *from, struct weston_view *to)
816 struct weston_output *output;
817 bool focus_surface_created = false;
819 /* FIXME: Only support dim animation using two layers */
820 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
823 output = get_default_output(shell->compositor);
824 if (ws->fsurf_front == NULL && (from || to)) {
825 ws->fsurf_front = create_focus_surface(shell->compositor, output);
826 if (ws->fsurf_front == NULL)
828 ws->fsurf_front->view->alpha = 0.0;
830 ws->fsurf_back = create_focus_surface(shell->compositor, output);
831 if (ws->fsurf_back == NULL) {
832 focus_surface_destroy(ws->fsurf_front);
835 ws->fsurf_back->view->alpha = 0.0;
837 focus_surface_created = true;
839 weston_layer_entry_remove(&ws->fsurf_front->view->layer_link);
840 weston_layer_entry_remove(&ws->fsurf_back->view->layer_link);
843 if (ws->focus_animation) {
844 weston_view_animation_destroy(ws->focus_animation);
845 ws->focus_animation = NULL;
849 weston_layer_entry_insert(&to->layer_link,
850 &ws->fsurf_front->view->layer_link);
852 weston_layer_entry_insert(&ws->layer.view_list,
853 &ws->fsurf_front->view->layer_link);
855 if (focus_surface_created) {
856 ws->focus_animation = weston_fade_run(
857 ws->fsurf_front->view,
858 ws->fsurf_front->view->alpha, 0.4, 300,
859 focus_animation_done, ws);
861 weston_layer_entry_insert(&from->layer_link,
862 &ws->fsurf_back->view->layer_link);
863 ws->focus_animation = weston_stable_fade_run(
864 ws->fsurf_front->view, 0.0,
865 ws->fsurf_back->view, 0.4,
866 focus_animation_done, ws);
868 weston_layer_entry_insert(&ws->layer.view_list,
869 &ws->fsurf_back->view->layer_link);
870 ws->focus_animation = weston_stable_fade_run(
871 ws->fsurf_front->view, 0.0,
872 ws->fsurf_back->view, 0.4,
873 focus_animation_done, ws);
878 workspace_destroy(struct workspace *ws)
880 struct focus_state *state, *next;
882 wl_list_for_each_safe(state, next, &ws->focus_list, link)
883 focus_state_destroy(state);
886 focus_surface_destroy(ws->fsurf_front);
888 focus_surface_destroy(ws->fsurf_back);
894 seat_destroyed(struct wl_listener *listener, void *data)
896 struct weston_seat *seat = data;
897 struct focus_state *state, *next;
898 struct workspace *ws = container_of(listener,
900 seat_destroyed_listener);
902 wl_list_for_each_safe(state, next, &ws->focus_list, link)
903 if (state->seat == seat)
904 wl_list_remove(&state->link);
907 static struct workspace *
908 workspace_create(struct desktop_shell *shell)
910 struct workspace *ws = malloc(sizeof *ws);
914 weston_layer_init(&ws->layer, shell->compositor);
916 wl_list_init(&ws->focus_list);
917 wl_list_init(&ws->seat_destroyed_listener.link);
918 ws->seat_destroyed_listener.notify = seat_destroyed;
919 ws->fsurf_front = NULL;
920 ws->fsurf_back = NULL;
921 ws->focus_animation = NULL;
927 workspace_is_empty(struct workspace *ws)
929 return wl_list_empty(&ws->layer.view_list.link);
932 static struct workspace *
933 get_workspace(struct desktop_shell *shell, unsigned int index)
935 struct workspace **pws = shell->workspaces.array.data;
936 assert(index < shell->workspaces.num);
942 get_current_workspace(struct desktop_shell *shell)
944 return get_workspace(shell, shell->workspaces.current);
948 activate_workspace(struct desktop_shell *shell, unsigned int index)
950 struct workspace *ws;
952 ws = get_workspace(shell, index);
953 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
955 shell->workspaces.current = index;
959 get_output_height(struct weston_output *output)
961 return abs(output->region.extents.y1 - output->region.extents.y2);
965 view_translate(struct workspace *ws, struct weston_view *view, double d)
967 struct weston_transform *transform;
969 if (is_focus_view(view)) {
970 struct focus_surface *fsurf = get_focus_surface(view->surface);
971 transform = &fsurf->workspace_transform;
973 struct shell_surface *shsurf = get_shell_surface(view->surface);
974 transform = &shsurf->workspace_transform;
977 if (wl_list_empty(&transform->link))
978 wl_list_insert(view->geometry.transformation_list.prev,
981 weston_matrix_init(&transform->matrix);
982 weston_matrix_translate(&transform->matrix,
984 weston_view_geometry_dirty(view);
988 workspace_translate_out(struct workspace *ws, double fraction)
990 struct weston_view *view;
994 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
995 height = get_output_height(view->surface->output);
996 d = height * fraction;
998 view_translate(ws, view, d);
1003 workspace_translate_in(struct workspace *ws, double fraction)
1005 struct weston_view *view;
1006 unsigned int height;
1009 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1010 height = get_output_height(view->surface->output);
1013 d = -(height - height * fraction);
1015 d = height + height * fraction;
1017 view_translate(ws, view, d);
1022 reverse_workspace_change_animation(struct desktop_shell *shell,
1024 struct workspace *from,
1025 struct workspace *to)
1027 shell->workspaces.current = index;
1029 shell->workspaces.anim_to = to;
1030 shell->workspaces.anim_from = from;
1031 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
1032 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1034 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1035 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1037 weston_compositor_schedule_repaint(shell->compositor);
1041 workspace_deactivate_transforms(struct workspace *ws)
1043 struct weston_view *view;
1044 struct weston_transform *transform;
1046 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
1047 if (is_focus_view(view)) {
1048 struct focus_surface *fsurf = get_focus_surface(view->surface);
1049 transform = &fsurf->workspace_transform;
1051 struct shell_surface *shsurf = get_shell_surface(view->surface);
1052 transform = &shsurf->workspace_transform;
1055 if (!wl_list_empty(&transform->link)) {
1056 wl_list_remove(&transform->link);
1057 wl_list_init(&transform->link);
1059 weston_view_geometry_dirty(view);
1064 finish_workspace_change_animation(struct desktop_shell *shell,
1065 struct workspace *from,
1066 struct workspace *to)
1068 struct weston_view *view;
1070 weston_compositor_schedule_repaint(shell->compositor);
1072 /* Views that extend past the bottom of the output are still
1073 * visible after the workspace animation ends but before its layer
1074 * is hidden. In that case, we need to damage below those views so
1075 * that the screen is properly repainted. */
1076 wl_list_for_each(view, &from->layer.view_list.link, layer_link.link)
1077 weston_view_damage_below(view);
1079 wl_list_remove(&shell->workspaces.animation.link);
1080 workspace_deactivate_transforms(from);
1081 workspace_deactivate_transforms(to);
1082 shell->workspaces.anim_to = NULL;
1084 weston_layer_unset_position(&shell->workspaces.anim_from->layer);
1088 animate_workspace_change_frame(struct weston_animation *animation,
1089 struct weston_output *output,
1090 const struct timespec *time)
1092 struct desktop_shell *shell =
1093 container_of(animation, struct desktop_shell,
1094 workspaces.animation);
1095 struct workspace *from = shell->workspaces.anim_from;
1096 struct workspace *to = shell->workspaces.anim_to;
1100 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1101 finish_workspace_change_animation(shell, from, to);
1105 if (timespec_is_zero(&shell->workspaces.anim_timestamp)) {
1106 if (shell->workspaces.anim_current == 0.0)
1107 shell->workspaces.anim_timestamp = *time;
1109 timespec_add_msec(&shell->workspaces.anim_timestamp,
1111 /* Invers of movement function 'y' below. */
1112 -(asin(1.0 - shell->workspaces.anim_current) *
1113 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1117 t = timespec_sub_to_msec(time, &shell->workspaces.anim_timestamp);
1123 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1126 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1127 weston_compositor_schedule_repaint(shell->compositor);
1129 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1130 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1131 shell->workspaces.anim_current = y;
1133 weston_compositor_schedule_repaint(shell->compositor);
1136 finish_workspace_change_animation(shell, from, to);
1140 animate_workspace_change(struct desktop_shell *shell,
1142 struct workspace *from,
1143 struct workspace *to)
1145 struct weston_output *output;
1149 if (index > shell->workspaces.current)
1154 shell->workspaces.current = index;
1156 shell->workspaces.anim_dir = dir;
1157 shell->workspaces.anim_from = from;
1158 shell->workspaces.anim_to = to;
1159 shell->workspaces.anim_current = 0.0;
1160 shell->workspaces.anim_timestamp = (struct timespec) { 0 };
1162 output = container_of(shell->compositor->output_list.next,
1163 struct weston_output, link);
1164 wl_list_insert(&output->animation_list,
1165 &shell->workspaces.animation.link);
1167 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1168 weston_layer_set_position(&from->layer, WESTON_LAYER_POSITION_NORMAL - 1);
1170 workspace_translate_in(to, 0);
1172 restore_focus_state(shell, to);
1174 weston_compositor_schedule_repaint(shell->compositor);
1178 update_workspace(struct desktop_shell *shell, unsigned int index,
1179 struct workspace *from, struct workspace *to)
1181 shell->workspaces.current = index;
1182 weston_layer_set_position(&to->layer, WESTON_LAYER_POSITION_NORMAL);
1183 weston_layer_unset_position(&from->layer);
1187 change_workspace(struct desktop_shell *shell, unsigned int index)
1189 struct workspace *from;
1190 struct workspace *to;
1191 struct focus_state *state;
1193 if (index == shell->workspaces.current)
1196 /* Don't change workspace when there is any fullscreen surfaces. */
1197 if (!wl_list_empty(&shell->fullscreen_layer.view_list.link))
1200 from = get_current_workspace(shell);
1201 to = get_workspace(shell, index);
1203 if (shell->workspaces.anim_from == to &&
1204 shell->workspaces.anim_to == from) {
1205 restore_focus_state(shell, to);
1206 reverse_workspace_change_animation(shell, index, from, to);
1210 if (shell->workspaces.anim_to != NULL)
1211 finish_workspace_change_animation(shell,
1212 shell->workspaces.anim_from,
1213 shell->workspaces.anim_to);
1215 restore_focus_state(shell, to);
1217 if (shell->focus_animation_type != ANIMATION_NONE) {
1218 wl_list_for_each(state, &from->focus_list, link)
1219 if (state->keyboard_focus)
1220 animate_focus_change(shell, from,
1221 get_default_view(state->keyboard_focus), NULL);
1223 wl_list_for_each(state, &to->focus_list, link)
1224 if (state->keyboard_focus)
1225 animate_focus_change(shell, to,
1226 NULL, get_default_view(state->keyboard_focus));
1229 if (workspace_is_empty(to) && workspace_is_empty(from))
1230 update_workspace(shell, index, from, to);
1232 animate_workspace_change(shell, index, from, to);
1236 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1238 struct wl_list *list = &ws->layer.view_list.link;
1241 if (wl_list_empty(list))
1246 if (e->next != list)
1249 return container_of(e, struct weston_view, layer_link.link)->surface == surface;
1253 surface_keyboard_focus_lost(struct weston_surface *surface)
1255 struct weston_compositor *compositor = surface->compositor;
1256 struct weston_seat *seat;
1257 struct weston_surface *focus;
1259 wl_list_for_each(seat, &compositor->seat_list, link) {
1260 struct weston_keyboard *keyboard =
1261 weston_seat_get_keyboard(seat);
1266 focus = weston_surface_get_main_surface(keyboard->focus);
1267 if (focus == surface)
1268 weston_keyboard_set_focus(keyboard, NULL);
1273 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1274 struct weston_seat *seat,
1277 struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
1278 struct weston_surface *surface;
1279 struct weston_view *view;
1280 struct shell_surface *shsurf;
1281 struct workspace *from;
1282 struct workspace *to;
1283 struct focus_state *state;
1285 surface = weston_surface_get_main_surface(keyboard->focus);
1286 view = get_default_view(surface);
1288 index == shell->workspaces.current ||
1289 is_focus_view(view))
1292 from = get_current_workspace(shell);
1293 to = get_workspace(shell, index);
1295 weston_layer_entry_remove(&view->layer_link);
1296 weston_layer_entry_insert(&to->layer.view_list, &view->layer_link);
1298 shsurf = get_shell_surface(surface);
1300 shell_surface_update_child_surface_layers(shsurf);
1302 replace_focus_state(shell, to, seat);
1303 drop_focus_state(shell, from, surface);
1305 if (shell->workspaces.anim_from == to &&
1306 shell->workspaces.anim_to == from) {
1307 reverse_workspace_change_animation(shell, index, from, to);
1312 if (shell->workspaces.anim_to != NULL)
1313 finish_workspace_change_animation(shell,
1314 shell->workspaces.anim_from,
1315 shell->workspaces.anim_to);
1317 if (workspace_is_empty(from) &&
1318 workspace_has_only(to, surface))
1319 update_workspace(shell, index, from, to);
1321 if (shsurf != NULL &&
1322 wl_list_empty(&shsurf->workspace_transform.link))
1323 wl_list_insert(&shell->workspaces.anim_sticky_list,
1324 &shsurf->workspace_transform.link);
1326 animate_workspace_change(shell, index, from, to);
1329 state = ensure_focus_state(shell, seat);
1331 focus_state_set_focus(state, surface);
1335 touch_move_grab_down(struct weston_touch_grab *grab,
1336 const struct timespec *time,
1337 int touch_id, wl_fixed_t x, wl_fixed_t y)
1342 touch_move_grab_up(struct weston_touch_grab *grab, const struct timespec *time,
1345 struct weston_touch_move_grab *move =
1346 (struct weston_touch_move_grab *) container_of(
1347 grab, struct shell_touch_grab, grab);
1352 if (grab->touch->num_tp == 0) {
1353 shell_touch_grab_end(&move->base);
1359 touch_move_grab_motion(struct weston_touch_grab *grab,
1360 const struct timespec *time, int touch_id,
1361 wl_fixed_t x, wl_fixed_t y)
1363 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1364 struct shell_surface *shsurf = move->base.shsurf;
1365 struct weston_surface *es;
1366 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1367 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1369 if (!shsurf || !move->active)
1372 es = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1374 weston_view_set_position(shsurf->view, dx, dy);
1376 weston_compositor_schedule_repaint(es->compositor);
1380 touch_move_grab_frame(struct weston_touch_grab *grab)
1385 touch_move_grab_cancel(struct weston_touch_grab *grab)
1387 struct weston_touch_move_grab *move =
1388 (struct weston_touch_move_grab *) container_of(
1389 grab, struct shell_touch_grab, grab);
1391 shell_touch_grab_end(&move->base);
1395 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1396 touch_move_grab_down,
1398 touch_move_grab_motion,
1399 touch_move_grab_frame,
1400 touch_move_grab_cancel,
1404 surface_touch_move(struct shell_surface *shsurf, struct weston_touch *touch)
1406 struct weston_touch_move_grab *move;
1411 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1412 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1415 move = malloc(sizeof *move);
1420 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1422 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1425 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1432 noop_grab_focus(struct weston_pointer_grab *grab)
1437 noop_grab_axis(struct weston_pointer_grab *grab,
1438 const struct timespec *time,
1439 struct weston_pointer_axis_event *event)
1444 noop_grab_axis_source(struct weston_pointer_grab *grab,
1450 noop_grab_frame(struct weston_pointer_grab *grab)
1455 constrain_position(struct weston_move_grab *move, int *cx, int *cy)
1457 struct shell_surface *shsurf = move->base.shsurf;
1458 struct weston_surface *surface =
1459 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1460 struct weston_pointer *pointer = move->base.grab.pointer;
1462 const int safety = 50;
1463 pixman_rectangle32_t area;
1464 struct weston_geometry geometry;
1466 x = wl_fixed_to_int(pointer->x + move->dx);
1467 y = wl_fixed_to_int(pointer->y + move->dy);
1469 if (shsurf->shell->panel_position ==
1470 WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP) {
1471 get_output_work_area(shsurf->shell, surface->output, &area);
1473 weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1475 bottom = y + geometry.height + geometry.y;
1476 if (bottom - safety < area.y)
1477 y = area.y + safety - geometry.height
1480 if (move->client_initiated &&
1481 y + geometry.y < area.y)
1482 y = area.y - geometry.y;
1490 move_grab_motion(struct weston_pointer_grab *grab,
1491 const struct timespec *time,
1492 struct weston_pointer_motion_event *event)
1494 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1495 struct weston_pointer *pointer = grab->pointer;
1496 struct shell_surface *shsurf = move->base.shsurf;
1497 struct weston_surface *surface;
1500 weston_pointer_move(pointer, event);
1504 surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);
1506 constrain_position(move, &cx, &cy);
1508 weston_view_set_position(shsurf->view, cx, cy);
1510 weston_compositor_schedule_repaint(surface->compositor);
1514 move_grab_button(struct weston_pointer_grab *grab,
1515 const struct timespec *time, uint32_t button, uint32_t state_w)
1517 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1519 struct weston_pointer *pointer = grab->pointer;
1520 enum wl_pointer_button_state state = state_w;
1522 if (pointer->button_count == 0 &&
1523 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1524 shell_grab_end(shell_grab);
1530 move_grab_cancel(struct weston_pointer_grab *grab)
1532 struct shell_grab *shell_grab =
1533 container_of(grab, struct shell_grab, grab);
1535 shell_grab_end(shell_grab);
1539 static const struct weston_pointer_grab_interface move_grab_interface = {
1544 noop_grab_axis_source,
1550 surface_move(struct shell_surface *shsurf, struct weston_pointer *pointer,
1551 bool client_initiated)
1553 struct weston_move_grab *move;
1558 if (shsurf->grabbed ||
1559 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1560 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1563 move = malloc(sizeof *move);
1567 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1569 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1571 move->client_initiated = client_initiated;
1573 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1574 pointer, WESTON_DESKTOP_SHELL_CURSOR_MOVE);
1579 struct weston_resize_grab {
1580 struct shell_grab base;
1582 int32_t width, height;
1586 resize_grab_motion(struct weston_pointer_grab *grab,
1587 const struct timespec *time,
1588 struct weston_pointer_motion_event *event)
1590 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1591 struct weston_pointer *pointer = grab->pointer;
1592 struct shell_surface *shsurf = resize->base.shsurf;
1593 int32_t width, height;
1594 struct weston_size min_size, max_size;
1595 wl_fixed_t from_x, from_y;
1596 wl_fixed_t to_x, to_y;
1598 weston_pointer_move(pointer, event);
1603 weston_view_from_global_fixed(shsurf->view,
1604 pointer->grab_x, pointer->grab_y,
1606 weston_view_from_global_fixed(shsurf->view,
1607 pointer->x, pointer->y, &to_x, &to_y);
1609 width = resize->width;
1610 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1611 width += wl_fixed_to_int(from_x - to_x);
1612 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1613 width += wl_fixed_to_int(to_x - from_x);
1616 height = resize->height;
1617 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1618 height += wl_fixed_to_int(from_y - to_y);
1619 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1620 height += wl_fixed_to_int(to_y - from_y);
1623 max_size = weston_desktop_surface_get_max_size(shsurf->desktop_surface);
1624 min_size = weston_desktop_surface_get_min_size(shsurf->desktop_surface);
1626 min_size.width = MAX(1, min_size.width);
1627 min_size.height = MAX(1, min_size.height);
1629 if (width < min_size.width)
1630 width = min_size.width;
1631 else if (max_size.width > 0 && width > max_size.width)
1632 width = max_size.width;
1633 if (height < min_size.height)
1634 height = min_size.height;
1635 else if (max_size.width > 0 && width > max_size.width)
1636 width = max_size.width;
1637 weston_desktop_surface_set_size(shsurf->desktop_surface, width, height);
1641 resize_grab_button(struct weston_pointer_grab *grab,
1642 const struct timespec *time,
1643 uint32_t button, uint32_t state_w)
1645 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1646 struct weston_pointer *pointer = grab->pointer;
1647 enum wl_pointer_button_state state = state_w;
1648 struct weston_desktop_surface *desktop_surface =
1649 resize->base.shsurf->desktop_surface;
1651 if (pointer->button_count == 0 &&
1652 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1653 weston_desktop_surface_set_resizing(desktop_surface, false);
1654 shell_grab_end(&resize->base);
1660 resize_grab_cancel(struct weston_pointer_grab *grab)
1662 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1663 struct weston_desktop_surface *desktop_surface =
1664 resize->base.shsurf->desktop_surface;
1666 weston_desktop_surface_set_resizing(desktop_surface, false);
1667 shell_grab_end(&resize->base);
1671 static const struct weston_pointer_grab_interface resize_grab_interface = {
1676 noop_grab_axis_source,
1682 * Returns the bounding box of a surface and all its sub-surfaces,
1683 * in surface-local coordinates. */
1685 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1686 int32_t *y, int32_t *w, int32_t *h) {
1687 pixman_region32_t region;
1688 pixman_box32_t *box;
1689 struct weston_subsurface *subsurface;
1691 pixman_region32_init_rect(®ion, 0, 0,
1695 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1696 pixman_region32_union_rect(®ion, ®ion,
1697 subsurface->position.x,
1698 subsurface->position.y,
1699 subsurface->surface->width,
1700 subsurface->surface->height);
1703 box = pixman_region32_extents(®ion);
1709 *w = box->x2 - box->x1;
1711 *h = box->y2 - box->y1;
1713 pixman_region32_fini(®ion);
1717 surface_resize(struct shell_surface *shsurf,
1718 struct weston_pointer *pointer, uint32_t edges)
1720 struct weston_resize_grab *resize;
1721 const unsigned resize_topbottom =
1722 WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_BOTTOM;
1723 const unsigned resize_leftright =
1724 WL_SHELL_SURFACE_RESIZE_LEFT | WL_SHELL_SURFACE_RESIZE_RIGHT;
1725 const unsigned resize_any = resize_topbottom | resize_leftright;
1726 struct weston_geometry geometry;
1728 if (shsurf->grabbed ||
1729 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
1730 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
1733 /* Check for invalid edge combinations. */
1734 if (edges == WL_SHELL_SURFACE_RESIZE_NONE || edges > resize_any ||
1735 (edges & resize_topbottom) == resize_topbottom ||
1736 (edges & resize_leftright) == resize_leftright)
1739 resize = malloc(sizeof *resize);
1743 resize->edges = edges;
1745 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
1746 resize->width = geometry.width;
1747 resize->height = geometry.height;
1749 shsurf->resize_edges = edges;
1750 weston_desktop_surface_set_resizing(shsurf->desktop_surface, true);
1751 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1758 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1760 struct shell_grab *grab = (struct shell_grab *) base;
1761 struct weston_pointer *pointer = base->pointer;
1762 struct weston_desktop_surface *desktop_surface;
1763 struct weston_view *view;
1766 view = weston_compositor_pick_view(pointer->seat->compositor,
1767 pointer->x, pointer->y,
1769 desktop_surface = weston_surface_get_desktop_surface(view->surface);
1771 if (!grab->shsurf || grab->shsurf->desktop_surface != desktop_surface) {
1772 shell_grab_end(grab);
1778 busy_cursor_grab_motion(struct weston_pointer_grab *grab,
1779 const struct timespec *time,
1780 struct weston_pointer_motion_event *event)
1782 weston_pointer_move(grab->pointer, event);
1786 busy_cursor_grab_button(struct weston_pointer_grab *base,
1787 const struct timespec *time,
1788 uint32_t button, uint32_t state)
1790 struct shell_grab *grab = (struct shell_grab *) base;
1791 struct shell_surface *shsurf = grab->shsurf;
1792 struct weston_pointer *pointer = grab->grab.pointer;
1793 struct weston_seat *seat = pointer->seat;
1795 if (shsurf && button == BTN_LEFT && state) {
1796 activate(shsurf->shell, shsurf->view, seat,
1797 WESTON_ACTIVATE_FLAG_CONFIGURE);
1798 surface_move(shsurf, pointer, false);
1799 } else if (shsurf && button == BTN_RIGHT && state) {
1800 activate(shsurf->shell, shsurf->view, seat,
1801 WESTON_ACTIVATE_FLAG_CONFIGURE);
1802 surface_rotate(shsurf, pointer);
1807 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1809 struct shell_grab *grab = (struct shell_grab *) base;
1811 shell_grab_end(grab);
1815 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1816 busy_cursor_grab_focus,
1817 busy_cursor_grab_motion,
1818 busy_cursor_grab_button,
1820 noop_grab_axis_source,
1822 busy_cursor_grab_cancel,
1826 handle_pointer_focus(struct wl_listener *listener, void *data)
1828 struct weston_pointer *pointer = data;
1829 struct weston_view *view = pointer->focus;
1830 struct shell_surface *shsurf;
1831 struct weston_desktop_client *client;
1836 shsurf = get_shell_surface(view->surface);
1840 client = weston_desktop_surface_get_client(shsurf->desktop_surface);
1842 if (shsurf->unresponsive)
1843 set_busy_cursor(shsurf, pointer);
1845 weston_desktop_client_ping(client);
1849 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1851 if (--shsurf->focus_count == 0)
1852 weston_desktop_surface_set_activated(shsurf->desktop_surface, false);
1856 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1858 if (shsurf->focus_count++ == 0)
1859 weston_desktop_surface_set_activated(shsurf->desktop_surface, true);
1863 handle_keyboard_focus(struct wl_listener *listener, void *data)
1865 struct weston_keyboard *keyboard = data;
1866 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1868 if (seat->focused_surface) {
1869 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1871 shell_surface_lose_keyboard_focus(shsurf);
1874 seat->focused_surface = weston_surface_get_main_surface(keyboard->focus);
1876 if (seat->focused_surface) {
1877 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1879 shell_surface_gain_keyboard_focus(shsurf);
1883 /* The surface will be inserted into the list immediately after the link
1884 * returned by this function (i.e. will be stacked immediately above the
1885 * returned link). */
1886 static struct weston_layer_entry *
1887 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
1889 struct workspace *ws;
1891 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
1892 !shsurf->state.lowered) {
1893 return &shsurf->shell->fullscreen_layer.view_list;
1896 /* Move the surface to a normal workspace layer so that surfaces
1897 * which were previously fullscreen or transient are no longer
1898 * rendered on top. */
1899 ws = get_current_workspace(shsurf->shell);
1900 return &ws->layer.view_list;
1904 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
1906 weston_desktop_surface_propagate_layer(shsurf->desktop_surface);
1909 /* Update the surface’s layer. Mark both the old and new views as having dirty
1910 * geometry to ensure the changes are redrawn.
1912 * If any child surfaces exist and are mapped, ensure they’re in the same layer
1913 * as this surface. */
1915 shell_surface_update_layer(struct shell_surface *shsurf)
1917 struct weston_surface *surface =
1918 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1919 struct weston_layer_entry *new_layer_link;
1921 new_layer_link = shell_surface_calculate_layer_link(shsurf);
1923 if (new_layer_link == NULL)
1925 if (new_layer_link == &shsurf->view->layer_link)
1928 weston_view_geometry_dirty(shsurf->view);
1929 weston_layer_entry_remove(&shsurf->view->layer_link);
1930 weston_layer_entry_insert(new_layer_link, &shsurf->view->layer_link);
1931 weston_view_geometry_dirty(shsurf->view);
1932 weston_surface_damage(surface);
1934 shell_surface_update_child_surface_layers(shsurf);
1938 notify_output_destroy(struct wl_listener *listener, void *data)
1940 struct shell_surface *shsurf =
1941 container_of(listener,
1942 struct shell_surface, output_destroy_listener);
1944 shsurf->output = NULL;
1945 shsurf->output_destroy_listener.notify = NULL;
1949 shell_surface_set_output(struct shell_surface *shsurf,
1950 struct weston_output *output)
1952 struct weston_surface *es =
1953 weston_desktop_surface_get_surface(shsurf->desktop_surface);
1955 /* get the default output, if the client set it as NULL
1956 check whether the output is available */
1958 shsurf->output = output;
1959 else if (es->output)
1960 shsurf->output = es->output;
1962 shsurf->output = get_default_output(es->compositor);
1964 if (shsurf->output_destroy_listener.notify) {
1965 wl_list_remove(&shsurf->output_destroy_listener.link);
1966 shsurf->output_destroy_listener.notify = NULL;
1969 if (!shsurf->output)
1972 shsurf->output_destroy_listener.notify = notify_output_destroy;
1973 wl_signal_add(&shsurf->output->destroy_signal,
1974 &shsurf->output_destroy_listener);
1978 weston_view_set_initial_position(struct weston_view *view,
1979 struct desktop_shell *shell);
1982 unset_fullscreen(struct shell_surface *shsurf)
1984 /* Unset the fullscreen output, driver configuration and transforms. */
1985 wl_list_remove(&shsurf->fullscreen.transform.link);
1986 wl_list_init(&shsurf->fullscreen.transform.link);
1988 if (shsurf->fullscreen.black_view)
1989 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
1990 shsurf->fullscreen.black_view = NULL;
1992 if (shsurf->saved_position_valid)
1993 weston_view_set_position(shsurf->view,
1994 shsurf->saved_x, shsurf->saved_y);
1996 weston_view_set_initial_position(shsurf->view, shsurf->shell);
1997 shsurf->saved_position_valid = false;
1999 if (shsurf->saved_rotation_valid) {
2000 wl_list_insert(&shsurf->view->geometry.transformation_list,
2001 &shsurf->rotation.transform.link);
2002 shsurf->saved_rotation_valid = false;
2007 unset_maximized(struct shell_surface *shsurf)
2009 struct weston_surface *surface =
2010 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2012 /* undo all maximized things here */
2013 shell_surface_set_output(shsurf, get_default_output(surface->compositor));
2015 if (shsurf->saved_position_valid)
2016 weston_view_set_position(shsurf->view,
2017 shsurf->saved_x, shsurf->saved_y);
2019 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2020 shsurf->saved_position_valid = false;
2022 if (shsurf->saved_rotation_valid) {
2023 wl_list_insert(&shsurf->view->geometry.transformation_list,
2024 &shsurf->rotation.transform.link);
2025 shsurf->saved_rotation_valid = false;
2030 set_minimized(struct weston_surface *surface)
2032 struct shell_surface *shsurf;
2033 struct workspace *current_ws;
2034 struct weston_view *view;
2036 view = get_default_view(surface);
2040 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2042 shsurf = get_shell_surface(surface);
2043 current_ws = get_current_workspace(shsurf->shell);
2045 weston_layer_entry_remove(&view->layer_link);
2046 weston_layer_entry_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2048 drop_focus_state(shsurf->shell, current_ws, view->surface);
2049 surface_keyboard_focus_lost(surface);
2051 shell_surface_update_child_surface_layers(shsurf);
2052 weston_view_damage_below(view);
2056 static struct desktop_shell *
2057 shell_surface_get_shell(struct shell_surface *shsurf)
2059 return shsurf->shell;
2063 black_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
2065 struct weston_view *fs_view = surface->committed_private;
2066 struct weston_surface *fs_surface = fs_view->surface;
2071 n = snprintf(buf, len, "black background surface for ");
2079 if (fs_surface->get_label)
2080 ret = fs_surface->get_label(fs_surface, buf + n, rem);
2082 ret = snprintf(buf + n, rem, "<unknown>");
2091 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy);
2093 static struct weston_view *
2094 create_black_surface(struct weston_compositor *ec,
2095 struct weston_view *fs_view,
2096 float x, float y, int w, int h)
2098 struct weston_surface *surface = NULL;
2099 struct weston_view *view;
2101 surface = weston_surface_create(ec);
2102 if (surface == NULL) {
2103 weston_log("no memory\n");
2106 view = weston_view_create(surface);
2107 if (surface == NULL) {
2108 weston_log("no memory\n");
2109 weston_surface_destroy(surface);
2113 surface->committed = black_surface_committed;
2114 surface->committed_private = fs_view;
2115 weston_surface_set_label_func(surface, black_surface_get_label);
2116 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2117 pixman_region32_fini(&surface->opaque);
2118 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2119 pixman_region32_fini(&surface->input);
2120 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2122 weston_surface_set_size(surface, w, h);
2123 weston_view_set_position(view, x, y);
2129 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2131 struct weston_surface *surface =
2132 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2133 struct weston_output *output = shsurf->fullscreen_output;
2135 assert(weston_desktop_surface_get_fullscreen(shsurf->desktop_surface));
2137 if (!shsurf->fullscreen.black_view)
2138 shsurf->fullscreen.black_view =
2139 create_black_surface(surface->compositor,
2141 output->x, output->y,
2145 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2146 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
2147 weston_layer_entry_insert(&shsurf->view->layer_link,
2148 &shsurf->fullscreen.black_view->layer_link);
2149 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2150 weston_surface_damage(surface);
2152 shsurf->fullscreen.black_view->is_mapped = true;
2153 shsurf->state.lowered = false;
2156 /* Create black surface and append it to the associated fullscreen surface.
2157 * Handle size dismatch and positioning according to the method. */
2159 shell_configure_fullscreen(struct shell_surface *shsurf)
2161 struct weston_surface *surface =
2162 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2163 int32_t surf_x, surf_y, surf_width, surf_height;
2165 /* Reverse the effect of lower_fullscreen_layer() */
2166 weston_layer_entry_remove(&shsurf->view->layer_link);
2167 weston_layer_entry_insert(&shsurf->shell->fullscreen_layer.view_list,
2168 &shsurf->view->layer_link);
2170 if (!shsurf->fullscreen_output) {
2171 /* If there is no output, there's not much we can do.
2172 * Position the window somewhere, whatever. */
2173 weston_view_set_position(shsurf->view, 0, 0);
2177 shell_ensure_fullscreen_black_view(shsurf);
2179 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
2180 &surf_width, &surf_height);
2182 if (surface->buffer_ref.buffer)
2183 center_on_output(shsurf->view, shsurf->fullscreen_output);
2187 shell_map_fullscreen(struct shell_surface *shsurf)
2189 shell_configure_fullscreen(shsurf);
2192 static struct weston_output *
2193 get_focused_output(struct weston_compositor *compositor)
2195 struct weston_seat *seat;
2196 struct weston_output *output = NULL;
2198 wl_list_for_each(seat, &compositor->seat_list, link) {
2199 struct weston_touch *touch = weston_seat_get_touch(seat);
2200 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2201 struct weston_keyboard *keyboard =
2202 weston_seat_get_keyboard(seat);
2204 /* Priority has touch focus, then pointer and
2205 * then keyboard focus. We should probably have
2206 * three for loops and check frist for touch,
2207 * then for pointer, etc. but unless somebody has some
2208 * objections, I think this is sufficient. */
2209 if (touch && touch->focus)
2210 output = touch->focus->output;
2211 else if (pointer && pointer->focus)
2212 output = pointer->focus->output;
2213 else if (keyboard && keyboard->focus)
2214 output = keyboard->focus->output;
2224 destroy_shell_seat(struct wl_listener *listener, void *data)
2226 struct shell_seat *shseat =
2227 container_of(listener,
2228 struct shell_seat, seat_destroy_listener);
2230 wl_list_remove(&shseat->seat_destroy_listener.link);
2235 shell_seat_caps_changed(struct wl_listener *listener, void *data)
2237 struct weston_keyboard *keyboard;
2238 struct weston_pointer *pointer;
2239 struct shell_seat *seat;
2241 seat = container_of(listener, struct shell_seat, caps_changed_listener);
2242 keyboard = weston_seat_get_keyboard(seat->seat);
2243 pointer = weston_seat_get_pointer(seat->seat);
2246 wl_list_empty(&seat->keyboard_focus_listener.link)) {
2247 wl_signal_add(&keyboard->focus_signal,
2248 &seat->keyboard_focus_listener);
2249 } else if (!keyboard) {
2250 wl_list_remove(&seat->keyboard_focus_listener.link);
2251 wl_list_init(&seat->keyboard_focus_listener.link);
2255 wl_list_empty(&seat->pointer_focus_listener.link)) {
2256 wl_signal_add(&pointer->focus_signal,
2257 &seat->pointer_focus_listener);
2258 } else if (!pointer) {
2259 wl_list_remove(&seat->pointer_focus_listener.link);
2260 wl_list_init(&seat->pointer_focus_listener.link);
2264 static struct shell_seat *
2265 create_shell_seat(struct weston_seat *seat)
2267 struct shell_seat *shseat;
2269 shseat = calloc(1, sizeof *shseat);
2271 weston_log("no memory to allocate shell seat\n");
2275 shseat->seat = seat;
2277 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2278 wl_signal_add(&seat->destroy_signal,
2279 &shseat->seat_destroy_listener);
2281 shseat->keyboard_focus_listener.notify = handle_keyboard_focus;
2282 wl_list_init(&shseat->keyboard_focus_listener.link);
2284 shseat->pointer_focus_listener.notify = handle_pointer_focus;
2285 wl_list_init(&shseat->pointer_focus_listener.link);
2287 shseat->caps_changed_listener.notify = shell_seat_caps_changed;
2288 wl_signal_add(&seat->updated_caps_signal,
2289 &shseat->caps_changed_listener);
2290 shell_seat_caps_changed(&shseat->caps_changed_listener, NULL);
2295 static struct shell_seat *
2296 get_shell_seat(struct weston_seat *seat)
2298 struct wl_listener *listener;
2300 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2301 assert(listener != NULL);
2303 return container_of(listener,
2304 struct shell_seat, seat_destroy_listener);
2308 fade_out_done_idle_cb(void *data)
2310 struct shell_surface *shsurf = data;
2312 weston_surface_destroy(shsurf->view->surface);
2314 if (shsurf->output_destroy_listener.notify) {
2315 wl_list_remove(&shsurf->output_destroy_listener.link);
2316 shsurf->output_destroy_listener.notify = NULL;
2323 fade_out_done(struct weston_view_animation *animation, void *data)
2325 struct shell_surface *shsurf = data;
2326 struct wl_event_loop *loop;
2328 loop = wl_display_get_event_loop(shsurf->shell->compositor->wl_display);
2330 if (weston_view_is_mapped(shsurf->view)) {
2331 shsurf->view->is_mapped = false;
2332 wl_event_loop_add_idle(loop, fade_out_done_idle_cb, shsurf);
2336 struct shell_surface *
2337 get_shell_surface(struct weston_surface *surface)
2339 if (weston_surface_is_desktop_surface(surface)) {
2340 struct weston_desktop_surface *desktop_surface =
2341 weston_surface_get_desktop_surface(surface);
2342 return weston_desktop_surface_get_user_data(desktop_surface);
2352 desktop_surface_added(struct weston_desktop_surface *desktop_surface,
2355 struct weston_desktop_client *client =
2356 weston_desktop_surface_get_client(desktop_surface);
2357 struct wl_client *wl_client =
2358 weston_desktop_client_get_client(client);
2359 struct weston_view *view;
2360 struct shell_surface *shsurf;
2361 struct weston_surface *surface =
2362 weston_desktop_surface_get_surface(desktop_surface);
2364 view = weston_desktop_surface_create_view(desktop_surface);
2368 shsurf = calloc(1, sizeof *shsurf);
2371 wl_client_post_no_memory(wl_client);
2373 weston_log("no memory to allocate shell surface\n");
2377 weston_surface_set_label_func(surface, shell_surface_get_label);
2379 shsurf->shell = (struct desktop_shell *) shell;
2380 shsurf->unresponsive = 0;
2381 shsurf->saved_position_valid = false;
2382 shsurf->saved_rotation_valid = false;
2383 shsurf->desktop_surface = desktop_surface;
2384 shsurf->view = view;
2385 shsurf->fullscreen.black_view = NULL;
2386 wl_list_init(&shsurf->fullscreen.transform.link);
2388 shell_surface_set_output(
2389 shsurf, get_default_output(shsurf->shell->compositor));
2391 wl_signal_init(&shsurf->destroy_signal);
2393 /* empty when not in use */
2394 wl_list_init(&shsurf->rotation.transform.link);
2395 weston_matrix_init(&shsurf->rotation.rotation);
2397 wl_list_init(&shsurf->workspace_transform.link);
2399 weston_desktop_surface_set_user_data(desktop_surface, shsurf);
2400 weston_desktop_surface_set_activated(desktop_surface,
2401 shsurf->focus_count > 0);
2405 desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
2408 struct shell_surface *shsurf =
2409 weston_desktop_surface_get_user_data(desktop_surface);
2410 struct weston_surface *surface =
2411 weston_desktop_surface_get_surface(desktop_surface);
2416 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2418 if (shsurf->fullscreen.black_view)
2419 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2421 weston_surface_set_label_func(surface, NULL);
2422 weston_desktop_surface_set_user_data(shsurf->desktop_surface, NULL);
2423 shsurf->desktop_surface = NULL;
2425 weston_desktop_surface_unlink_view(shsurf->view);
2426 if (weston_surface_is_mapped(surface) &&
2427 shsurf->shell->win_close_animation_type == ANIMATION_FADE) {
2428 pixman_region32_fini(&surface->pending.input);
2429 pixman_region32_init(&surface->pending.input);
2430 pixman_region32_fini(&surface->input);
2431 pixman_region32_init(&surface->input);
2432 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
2433 fade_out_done, shsurf);
2435 weston_view_destroy(shsurf->view);
2437 if (shsurf->output_destroy_listener.notify) {
2438 wl_list_remove(&shsurf->output_destroy_listener.link);
2439 shsurf->output_destroy_listener.notify = NULL;
2447 set_maximized_position(struct desktop_shell *shell,
2448 struct shell_surface *shsurf)
2450 pixman_rectangle32_t area;
2451 struct weston_geometry geometry;
2453 get_output_work_area(shell, shsurf->output, &area);
2454 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2456 weston_view_set_position(shsurf->view,
2457 area.x - geometry.x,
2458 area.y - geometry.y);
2462 set_position_from_xwayland(struct shell_surface *shsurf)
2464 struct weston_geometry geometry;
2468 assert(shsurf->xwayland.is_set);
2470 geometry = weston_desktop_surface_get_geometry(shsurf->desktop_surface);
2471 x = shsurf->xwayland.x - geometry.x;
2472 y = shsurf->xwayland.y - geometry.y;
2474 weston_view_set_position(shsurf->view, x, y);
2477 weston_log("%s: XWM %d, %d; geometry %d, %d; view %f, %f\n",
2478 __func__, shsurf->xwayland.x, shsurf->xwayland.y,
2479 geometry.x, geometry.y, x, y);
2484 map(struct desktop_shell *shell, struct shell_surface *shsurf,
2485 int32_t sx, int32_t sy)
2487 struct weston_surface *surface =
2488 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2489 struct weston_compositor *compositor = shell->compositor;
2490 struct weston_seat *seat;
2492 /* initial positioning, see also configure() */
2493 if (shsurf->state.fullscreen) {
2494 center_on_output(shsurf->view, shsurf->fullscreen_output);
2495 shell_map_fullscreen(shsurf);
2496 } else if (shsurf->state.maximized) {
2497 set_maximized_position(shell, shsurf);
2498 } else if (shsurf->xwayland.is_set) {
2499 set_position_from_xwayland(shsurf);
2501 weston_view_set_initial_position(shsurf->view, shell);
2504 /* Surface stacking order, see also activate(). */
2505 shell_surface_update_layer(shsurf);
2507 weston_view_update_transform(shsurf->view);
2508 shsurf->view->is_mapped = true;
2509 if (shsurf->state.maximized) {
2510 surface->output = shsurf->output;
2511 weston_view_set_output(shsurf->view, shsurf->output);
2514 if (!shell->locked) {
2515 wl_list_for_each(seat, &compositor->seat_list, link)
2516 activate(shell, shsurf->view, seat,
2517 WESTON_ACTIVATE_FLAG_CONFIGURE);
2520 if (!shsurf->state.fullscreen && !shsurf->state.maximized) {
2521 switch (shell->win_animation_type) {
2522 case ANIMATION_FADE:
2523 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
2525 case ANIMATION_ZOOM:
2526 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
2528 case ANIMATION_NONE:
2536 desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
2537 int32_t sx, int32_t sy, void *data)
2539 struct shell_surface *shsurf =
2540 weston_desktop_surface_get_user_data(desktop_surface);
2541 struct weston_surface *surface =
2542 weston_desktop_surface_get_surface(desktop_surface);
2543 struct weston_view *view = shsurf->view;
2544 struct desktop_shell *shell = data;
2545 bool was_fullscreen;
2548 if (surface->width == 0)
2551 was_fullscreen = shsurf->state.fullscreen;
2552 was_maximized = shsurf->state.maximized;
2554 shsurf->state.fullscreen =
2555 weston_desktop_surface_get_fullscreen(desktop_surface);
2556 shsurf->state.maximized =
2557 weston_desktop_surface_get_maximized(desktop_surface);
2559 if (!weston_surface_is_mapped(surface)) {
2560 map(shell, shsurf, sx, sy);
2561 surface->is_mapped = true;
2562 if (shsurf->shell->win_close_animation_type == ANIMATION_FADE)
2563 ++surface->ref_count;
2567 if (sx == 0 && sy == 0 &&
2568 shsurf->last_width == surface->width &&
2569 shsurf->last_height == surface->height &&
2570 was_fullscreen == shsurf->state.fullscreen &&
2571 was_maximized == shsurf->state.maximized)
2575 unset_fullscreen(shsurf);
2577 unset_maximized(shsurf);
2579 if ((shsurf->state.fullscreen || shsurf->state.maximized) &&
2580 !shsurf->saved_position_valid) {
2581 shsurf->saved_x = shsurf->view->geometry.x;
2582 shsurf->saved_y = shsurf->view->geometry.y;
2583 shsurf->saved_position_valid = true;
2585 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2586 wl_list_remove(&shsurf->rotation.transform.link);
2587 wl_list_init(&shsurf->rotation.transform.link);
2588 weston_view_geometry_dirty(shsurf->view);
2589 shsurf->saved_rotation_valid = true;
2593 if (shsurf->state.fullscreen) {
2594 shell_configure_fullscreen(shsurf);
2595 } else if (shsurf->state.maximized) {
2596 set_maximized_position(shell, shsurf);
2597 surface->output = shsurf->output;
2599 float from_x, from_y;
2603 if (shsurf->resize_edges) {
2608 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
2609 sx = shsurf->last_width - surface->width;
2610 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
2611 sy = shsurf->last_height - surface->height;
2613 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
2614 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
2615 x = shsurf->view->geometry.x + to_x - from_x;
2616 y = shsurf->view->geometry.y + to_y - from_y;
2618 weston_view_set_position(shsurf->view, x, y);
2621 shsurf->last_width = surface->width;
2622 shsurf->last_height = surface->height;
2624 /* XXX: would a fullscreen surface need the same handling? */
2625 if (surface->output) {
2626 wl_list_for_each(view, &surface->views, surface_link)
2627 weston_view_update_transform(view);
2632 get_maximized_size(struct shell_surface *shsurf, int32_t *width, int32_t *height)
2634 struct desktop_shell *shell;
2635 pixman_rectangle32_t area;
2637 shell = shell_surface_get_shell(shsurf);
2638 get_output_work_area(shell, shsurf->output, &area);
2640 *width = area.width;
2641 *height = area.height;
2645 set_fullscreen(struct shell_surface *shsurf, bool fullscreen,
2646 struct weston_output *output)
2648 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2649 struct weston_surface *surface =
2650 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2651 int32_t width = 0, height = 0;
2654 /* handle clients launching in fullscreen */
2655 if (output == NULL && !weston_surface_is_mapped(surface)) {
2656 /* Set the output to the one that has focus currently. */
2657 output = get_focused_output(surface->compositor);
2660 shell_surface_set_output(shsurf, output);
2661 shsurf->fullscreen_output = shsurf->output;
2663 width = shsurf->output->width;
2664 height = shsurf->output->height;
2665 } else if (weston_desktop_surface_get_maximized(desktop_surface)) {
2666 get_maximized_size(shsurf, &width, &height);
2668 weston_desktop_surface_set_fullscreen(desktop_surface, fullscreen);
2669 weston_desktop_surface_set_size(desktop_surface, width, height);
2673 desktop_surface_move(struct weston_desktop_surface *desktop_surface,
2674 struct weston_seat *seat, uint32_t serial, void *shell)
2676 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2677 struct weston_touch *touch = weston_seat_get_touch(seat);
2678 struct shell_surface *shsurf =
2679 weston_desktop_surface_get_user_data(desktop_surface);
2680 struct weston_surface *surface =
2681 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2682 struct wl_resource *resource = surface->resource;
2683 struct weston_surface *focus;
2687 pointer->button_count > 0 &&
2688 pointer->grab_serial == serial) {
2689 focus = weston_surface_get_main_surface(pointer->focus->surface);
2690 if ((focus == surface) &&
2691 (surface_move(shsurf, pointer, true) < 0))
2692 wl_resource_post_no_memory(resource);
2695 touch->grab_serial == serial) {
2696 focus = weston_surface_get_main_surface(touch->focus->surface);
2697 if ((focus == surface) &&
2698 (surface_touch_move(shsurf, touch) < 0))
2699 wl_resource_post_no_memory(resource);
2704 desktop_surface_resize(struct weston_desktop_surface *desktop_surface,
2705 struct weston_seat *seat, uint32_t serial,
2706 enum weston_desktop_surface_edge edges, void *shell)
2708 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2709 struct shell_surface *shsurf =
2710 weston_desktop_surface_get_user_data(desktop_surface);
2711 struct weston_surface *surface =
2712 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2713 struct wl_resource *resource = surface->resource;
2714 struct weston_surface *focus;
2717 pointer->button_count == 0 ||
2718 pointer->grab_serial != serial ||
2719 pointer->focus == NULL)
2722 focus = weston_surface_get_main_surface(pointer->focus->surface);
2723 if (focus != surface)
2726 if (surface_resize(shsurf, pointer, edges) < 0)
2727 wl_resource_post_no_memory(resource);
2731 desktop_surface_fullscreen_requested(struct weston_desktop_surface *desktop_surface,
2733 struct weston_output *output, void *shell)
2735 struct shell_surface *shsurf =
2736 weston_desktop_surface_get_user_data(desktop_surface);
2738 set_fullscreen(shsurf, fullscreen, output);
2742 set_maximized(struct shell_surface *shsurf, bool maximized)
2744 struct weston_desktop_surface *desktop_surface = shsurf->desktop_surface;
2745 struct weston_surface *surface =
2746 weston_desktop_surface_get_surface(shsurf->desktop_surface);
2747 int32_t width = 0, height = 0;
2750 struct weston_output *output;
2752 if (!weston_surface_is_mapped(surface))
2753 output = get_focused_output(surface->compositor);
2755 output = surface->output;
2757 shell_surface_set_output(shsurf, output);
2759 get_maximized_size(shsurf, &width, &height);
2761 weston_desktop_surface_set_maximized(desktop_surface, maximized);
2762 weston_desktop_surface_set_size(desktop_surface, width, height);
2766 desktop_surface_maximized_requested(struct weston_desktop_surface *desktop_surface,
2767 bool maximized, void *shell)
2769 struct shell_surface *shsurf =
2770 weston_desktop_surface_get_user_data(desktop_surface);
2772 set_maximized(shsurf, maximized);
2776 desktop_surface_minimized_requested(struct weston_desktop_surface *desktop_surface,
2779 struct weston_surface *surface =
2780 weston_desktop_surface_get_surface(desktop_surface);
2782 /* apply compositor's own minimization logic (hide) */
2783 set_minimized(surface);
2787 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
2789 struct shell_grab *grab;
2791 if (pointer->grab->interface == &busy_cursor_grab_interface)
2794 grab = malloc(sizeof *grab);
2798 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
2799 WESTON_DESKTOP_SHELL_CURSOR_BUSY);
2800 /* Mark the shsurf as ungrabbed so that button binding is able
2802 shsurf->grabbed = 0;
2806 end_busy_cursor(struct weston_compositor *compositor,
2807 struct weston_desktop_client *desktop_client)
2809 struct shell_surface *shsurf;
2810 struct shell_grab *grab;
2811 struct weston_seat *seat;
2813 wl_list_for_each(seat, &compositor->seat_list, link) {
2814 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2815 struct weston_desktop_client *grab_client;
2820 if (pointer->grab->interface != &busy_cursor_grab_interface)
2823 grab = (struct shell_grab *) pointer->grab;
2824 shsurf = grab->shsurf;
2829 weston_desktop_surface_get_client(shsurf->desktop_surface);
2830 if (grab_client == desktop_client) {
2831 shell_grab_end(grab);
2838 desktop_surface_set_unresponsive(struct weston_desktop_surface *desktop_surface,
2841 struct shell_surface *shsurf =
2842 weston_desktop_surface_get_user_data(desktop_surface);
2843 bool *unresponsive = user_data;
2845 shsurf->unresponsive = *unresponsive;
2849 desktop_surface_ping_timeout(struct weston_desktop_client *desktop_client,
2852 struct desktop_shell *shell = shell_;
2853 struct shell_surface *shsurf;
2854 struct weston_seat *seat;
2855 bool unresponsive = true;
2857 weston_desktop_client_for_each_surface(desktop_client,
2858 desktop_surface_set_unresponsive,
2862 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
2863 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
2864 struct weston_desktop_client *grab_client;
2866 if (!pointer || !pointer->focus)
2869 shsurf = get_shell_surface(pointer->focus->surface);
2874 weston_desktop_surface_get_client(shsurf->desktop_surface);
2875 if (grab_client == desktop_client)
2876 set_busy_cursor(shsurf, pointer);
2881 desktop_surface_pong(struct weston_desktop_client *desktop_client,
2884 struct desktop_shell *shell = shell_;
2885 bool unresponsive = false;
2887 weston_desktop_client_for_each_surface(desktop_client,
2888 desktop_surface_set_unresponsive,
2890 end_busy_cursor(shell->compositor, desktop_client);
2894 desktop_surface_set_xwayland_position(struct weston_desktop_surface *surface,
2895 int32_t x, int32_t y, void *shell_)
2897 struct shell_surface *shsurf =
2898 weston_desktop_surface_get_user_data(surface);
2900 shsurf->xwayland.x = x;
2901 shsurf->xwayland.y = y;
2902 shsurf->xwayland.is_set = true;
2905 static const struct weston_desktop_api shell_desktop_api = {
2906 .struct_size = sizeof(struct weston_desktop_api),
2907 .surface_added = desktop_surface_added,
2908 .surface_removed = desktop_surface_removed,
2909 .committed = desktop_surface_committed,
2910 .move = desktop_surface_move,
2911 .resize = desktop_surface_resize,
2912 .fullscreen_requested = desktop_surface_fullscreen_requested,
2913 .maximized_requested = desktop_surface_maximized_requested,
2914 .minimized_requested = desktop_surface_minimized_requested,
2915 .ping_timeout = desktop_surface_ping_timeout,
2916 .pong = desktop_surface_pong,
2917 .set_xwayland_position = desktop_surface_set_xwayland_position,
2920 /* ************************ *
2921 * end of libweston-desktop *
2922 * ************************ */
2924 configure_static_view(struct weston_view *ev, struct weston_layer *layer, int x, int y)
2926 struct weston_view *v, *next;
2931 wl_list_for_each_safe(v, next, &layer->view_list.link, layer_link.link) {
2932 if (v->output == ev->output && v != ev) {
2933 weston_view_unmap(v);
2934 v->surface->committed = NULL;
2935 weston_surface_set_label_func(v->surface, NULL);
2939 weston_view_set_position(ev, ev->output->x + x, ev->output->y + y);
2940 ev->surface->is_mapped = true;
2941 ev->is_mapped = true;
2943 if (wl_list_empty(&ev->layer_link.link)) {
2944 weston_layer_entry_insert(&layer->view_list, &ev->layer_link);
2945 weston_compositor_schedule_repaint(ev->surface->compositor);
2950 static struct shell_output *
2951 find_shell_output_from_weston_output(struct desktop_shell *shell,
2952 struct weston_output *output)
2954 struct shell_output *shell_output;
2956 wl_list_for_each(shell_output, &shell->output_list, link) {
2957 if (shell_output->output == output)
2958 return shell_output;
2965 background_get_label(struct weston_surface *surface, char *buf, size_t len)
2967 return snprintf(buf, len, "background for output %s",
2968 surface->output->name);
2972 background_committed(struct weston_surface *es, int32_t sx, int32_t sy)
2974 struct desktop_shell *shell = es->committed_private;
2975 struct weston_view *view;
2977 view = container_of(es->views.next, struct weston_view, surface_link);
2979 configure_static_view(view, &shell->background_layer, 0, 0);
2983 handle_background_surface_destroy(struct wl_listener *listener, void *data)
2985 struct shell_output *output =
2986 container_of(listener, struct shell_output, background_surface_listener);
2988 weston_log("background surface gone\n");
2989 wl_list_remove(&output->background_surface_listener.link);
2990 output->background_surface = NULL;
2994 desktop_shell_set_background(struct wl_client *client,
2995 struct wl_resource *resource,
2996 struct wl_resource *output_resource,
2997 struct wl_resource *surface_resource)
2999 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3000 struct weston_surface *surface =
3001 wl_resource_get_user_data(surface_resource);
3002 struct shell_output *sh_output;
3003 struct weston_view *view, *next;
3005 if (surface->committed) {
3006 wl_resource_post_error(surface_resource,
3007 WL_DISPLAY_ERROR_INVALID_OBJECT,
3008 "surface role already assigned");
3012 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3013 weston_view_destroy(view);
3014 view = weston_view_create(surface);
3016 surface->committed = background_committed;
3017 surface->committed_private = shell;
3018 weston_surface_set_label_func(surface, background_get_label);
3019 surface->output = weston_head_from_resource(output_resource)->output;
3020 weston_view_set_output(view, surface->output);
3022 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3023 if (sh_output->background_surface) {
3024 /* The output already has a background, tell our helper
3025 * there is no need for another one. */
3026 weston_desktop_shell_send_configure(resource, 0,
3030 weston_desktop_shell_send_configure(resource, 0,
3032 surface->output->width,
3033 surface->output->height);
3035 sh_output->background_surface = surface;
3037 sh_output->background_surface_listener.notify =
3038 handle_background_surface_destroy;
3039 wl_signal_add(&surface->destroy_signal,
3040 &sh_output->background_surface_listener);
3045 panel_get_label(struct weston_surface *surface, char *buf, size_t len)
3047 return snprintf(buf, len, "panel for output %s",
3048 surface->output->name);
3052 panel_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3054 struct desktop_shell *shell = es->committed_private;
3055 struct weston_view *view;
3059 view = container_of(es->views.next, struct weston_view, surface_link);
3061 get_panel_size(shell, view, &width, &height);
3062 switch (shell->panel_position) {
3063 case WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP:
3065 case WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
3066 y = view->output->height - height;
3068 case WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT:
3070 case WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT:
3071 x = view->output->width - width;
3075 configure_static_view(view, &shell->panel_layer, x, y);
3079 handle_panel_surface_destroy(struct wl_listener *listener, void *data)
3081 struct shell_output *output =
3082 container_of(listener, struct shell_output, panel_surface_listener);
3084 weston_log("panel surface gone\n");
3085 wl_list_remove(&output->panel_surface_listener.link);
3086 output->panel_surface = NULL;
3091 desktop_shell_set_panel(struct wl_client *client,
3092 struct wl_resource *resource,
3093 struct wl_resource *output_resource,
3094 struct wl_resource *surface_resource)
3096 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3097 struct weston_surface *surface =
3098 wl_resource_get_user_data(surface_resource);
3099 struct weston_view *view, *next;
3100 struct shell_output *sh_output;
3102 if (surface->committed) {
3103 wl_resource_post_error(surface_resource,
3104 WL_DISPLAY_ERROR_INVALID_OBJECT,
3105 "surface role already assigned");
3109 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3110 weston_view_destroy(view);
3111 view = weston_view_create(surface);
3113 surface->committed = panel_committed;
3114 surface->committed_private = shell;
3115 weston_surface_set_label_func(surface, panel_get_label);
3116 surface->output = weston_head_from_resource(output_resource)->output;
3117 weston_view_set_output(view, surface->output);
3119 sh_output = find_shell_output_from_weston_output(shell, surface->output);
3120 if (sh_output->panel_surface) {
3121 /* The output already has a panel, tell our helper
3122 * there is no need for another one. */
3123 weston_desktop_shell_send_configure(resource, 0,
3127 weston_desktop_shell_send_configure(resource, 0,
3129 surface->output->width,
3130 surface->output->height);
3132 sh_output->panel_surface = surface;
3134 sh_output->panel_surface_listener.notify = handle_panel_surface_destroy;
3135 wl_signal_add(&surface->destroy_signal, &sh_output->panel_surface_listener);
3140 lock_surface_get_label(struct weston_surface *surface, char *buf, size_t len)
3142 return snprintf(buf, len, "lock window");
3146 lock_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
3148 struct desktop_shell *shell = surface->committed_private;
3149 struct weston_view *view;
3151 view = container_of(surface->views.next, struct weston_view, surface_link);
3153 if (surface->width == 0)
3156 center_on_output(view, get_default_output(shell->compositor));
3158 if (!weston_surface_is_mapped(surface)) {
3159 weston_layer_entry_insert(&shell->lock_layer.view_list,
3161 weston_view_update_transform(view);
3162 surface->is_mapped = true;
3163 view->is_mapped = true;
3164 shell_fade(shell, FADE_IN);
3169 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
3171 struct desktop_shell *shell =
3172 container_of(listener, struct desktop_shell, lock_surface_listener);
3174 weston_log("lock surface gone\n");
3175 shell->lock_surface = NULL;
3179 desktop_shell_set_lock_surface(struct wl_client *client,
3180 struct wl_resource *resource,
3181 struct wl_resource *surface_resource)
3183 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3184 struct weston_surface *surface =
3185 wl_resource_get_user_data(surface_resource);
3187 shell->prepare_event_sent = false;
3192 shell->lock_surface = surface;
3194 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
3195 wl_signal_add(&surface->destroy_signal,
3196 &shell->lock_surface_listener);
3198 weston_view_create(surface);
3199 surface->committed = lock_surface_committed;
3200 surface->committed_private = shell;
3201 weston_surface_set_label_func(surface, lock_surface_get_label);
3205 resume_desktop(struct desktop_shell *shell)
3207 struct workspace *ws = get_current_workspace(shell);
3209 weston_layer_unset_position(&shell->lock_layer);
3211 if (shell->showing_input_panels)
3212 weston_layer_set_position(&shell->input_panel_layer,
3213 WESTON_LAYER_POSITION_TOP_UI);
3214 weston_layer_set_position(&shell->fullscreen_layer,
3215 WESTON_LAYER_POSITION_FULLSCREEN);
3216 weston_layer_set_position(&shell->panel_layer,
3217 WESTON_LAYER_POSITION_UI);
3218 weston_layer_set_position(&ws->layer, WESTON_LAYER_POSITION_NORMAL);
3220 restore_focus_state(shell, get_current_workspace(shell));
3222 shell->locked = false;
3223 shell_fade(shell, FADE_IN);
3224 weston_compositor_damage_all(shell->compositor);
3228 desktop_shell_unlock(struct wl_client *client,
3229 struct wl_resource *resource)
3231 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3233 shell->prepare_event_sent = false;
3236 resume_desktop(shell);
3240 desktop_shell_set_grab_surface(struct wl_client *client,
3241 struct wl_resource *resource,
3242 struct wl_resource *surface_resource)
3244 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3246 shell->grab_surface = wl_resource_get_user_data(surface_resource);
3247 weston_view_create(shell->grab_surface);
3251 desktop_shell_desktop_ready(struct wl_client *client,
3252 struct wl_resource *resource)
3254 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3256 shell_fade_startup(shell);
3260 desktop_shell_set_panel_position(struct wl_client *client,
3261 struct wl_resource *resource,
3264 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3266 if (position != WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP &&
3267 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_BOTTOM &&
3268 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_LEFT &&
3269 position != WESTON_DESKTOP_SHELL_PANEL_POSITION_RIGHT) {
3270 wl_resource_post_error(resource,
3271 WESTON_DESKTOP_SHELL_ERROR_INVALID_ARGUMENT,
3272 "bad position argument");
3276 shell->panel_position = position;
3279 static const struct weston_desktop_shell_interface desktop_shell_implementation = {
3280 desktop_shell_set_background,
3281 desktop_shell_set_panel,
3282 desktop_shell_set_lock_surface,
3283 desktop_shell_unlock,
3284 desktop_shell_set_grab_surface,
3285 desktop_shell_desktop_ready,
3286 desktop_shell_set_panel_position
3290 move_binding(struct weston_pointer *pointer, const struct timespec *time,
3291 uint32_t button, void *data)
3293 struct weston_surface *focus;
3294 struct weston_surface *surface;
3295 struct shell_surface *shsurf;
3297 if (pointer->focus == NULL)
3300 focus = pointer->focus->surface;
3302 surface = weston_surface_get_main_surface(focus);
3303 if (surface == NULL)
3306 shsurf = get_shell_surface(surface);
3307 if (shsurf == NULL ||
3308 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3309 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3312 surface_move(shsurf, pointer, false);
3316 maximize_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3317 uint32_t button, void *data)
3319 struct weston_surface *focus = keyboard->focus;
3320 struct weston_surface *surface;
3321 struct shell_surface *shsurf;
3323 surface = weston_surface_get_main_surface(focus);
3324 if (surface == NULL)
3327 shsurf = get_shell_surface(surface);
3331 set_maximized(shsurf, !weston_desktop_surface_get_maximized(shsurf->desktop_surface));
3335 fullscreen_binding(struct weston_keyboard *keyboard,
3336 const struct timespec *time, uint32_t button, void *data)
3338 struct weston_surface *focus = keyboard->focus;
3339 struct weston_surface *surface;
3340 struct shell_surface *shsurf;
3343 surface = weston_surface_get_main_surface(focus);
3344 if (surface == NULL)
3347 shsurf = get_shell_surface(surface);
3352 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface);
3354 set_fullscreen(shsurf, !fullscreen, NULL);
3358 touch_move_binding(struct weston_touch *touch, const struct timespec *time, void *data)
3360 struct weston_surface *focus;
3361 struct weston_surface *surface;
3362 struct shell_surface *shsurf;
3364 if (touch->focus == NULL)
3367 focus = touch->focus->surface;
3368 surface = weston_surface_get_main_surface(focus);
3369 if (surface == NULL)
3372 shsurf = get_shell_surface(surface);
3373 if (shsurf == NULL ||
3374 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3375 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3378 surface_touch_move(shsurf, touch);
3382 resize_binding(struct weston_pointer *pointer, const struct timespec *time,
3383 uint32_t button, void *data)
3385 struct weston_surface *focus;
3386 struct weston_surface *surface;
3389 struct shell_surface *shsurf;
3391 if (pointer->focus == NULL)
3394 focus = pointer->focus->surface;
3396 surface = weston_surface_get_main_surface(focus);
3397 if (surface == NULL)
3400 shsurf = get_shell_surface(surface);
3401 if (shsurf == NULL ||
3402 weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
3403 weston_desktop_surface_get_maximized(shsurf->desktop_surface))
3406 weston_view_from_global(shsurf->view,
3407 wl_fixed_to_int(pointer->grab_x),
3408 wl_fixed_to_int(pointer->grab_y),
3411 if (x < surface->width / 3)
3412 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
3413 else if (x < 2 * surface->width / 3)
3416 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
3418 if (y < surface->height / 3)
3419 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
3420 else if (y < 2 * surface->height / 3)
3423 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
3425 surface_resize(shsurf, pointer, edges);
3429 surface_opacity_binding(struct weston_pointer *pointer,
3430 const struct timespec *time,
3431 struct weston_pointer_axis_event *event,
3435 struct shell_surface *shsurf;
3436 struct weston_surface *focus = pointer->focus->surface;
3437 struct weston_surface *surface;
3439 /* XXX: broken for windows containing sub-surfaces */
3440 surface = weston_surface_get_main_surface(focus);
3441 if (surface == NULL)
3444 shsurf = get_shell_surface(surface);
3448 shsurf->view->alpha -= event->value * step;
3450 if (shsurf->view->alpha > 1.0)
3451 shsurf->view->alpha = 1.0;
3452 if (shsurf->view->alpha < step)
3453 shsurf->view->alpha = step;
3455 weston_view_geometry_dirty(shsurf->view);
3456 weston_surface_damage(surface);
3460 do_zoom(struct weston_seat *seat, const struct timespec *time, uint32_t key,
3461 uint32_t axis, double value)
3463 struct weston_compositor *compositor = seat->compositor;
3464 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
3465 struct weston_output *output;
3469 weston_log("Zoom hotkey pressed but seat '%s' contains no pointer.\n", seat->seat_name);
3473 wl_list_for_each(output, &compositor->output_list, link) {
3474 if (pixman_region32_contains_point(&output->region,
3475 wl_fixed_to_double(pointer->x),
3476 wl_fixed_to_double(pointer->y),
3478 if (key == KEY_PAGEUP)
3479 increment = output->zoom.increment;
3480 else if (key == KEY_PAGEDOWN)
3481 increment = -output->zoom.increment;
3482 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
3483 /* For every pixel zoom 20th of a step */
3484 increment = output->zoom.increment *
3489 output->zoom.level += increment;
3491 if (output->zoom.level < 0.0)
3492 output->zoom.level = 0.0;
3493 else if (output->zoom.level > output->zoom.max_level)
3494 output->zoom.level = output->zoom.max_level;
3496 if (!output->zoom.active) {
3497 if (output->zoom.level <= 0.0)
3499 weston_output_activate_zoom(output, seat);
3502 output->zoom.spring_z.target = output->zoom.level;
3504 weston_output_update_zoom(output);
3510 zoom_axis_binding(struct weston_pointer *pointer, const struct timespec *time,
3511 struct weston_pointer_axis_event *event,
3514 do_zoom(pointer->seat, time, 0, event->axis, event->value);
3518 zoom_key_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3519 uint32_t key, void *data)
3521 do_zoom(keyboard->seat, time, key, 0, 0);
3525 terminate_binding(struct weston_keyboard *keyboard, const struct timespec *time,
3526 uint32_t key, void *data)
3528 struct weston_compositor *compositor = data;
3530 wl_display_terminate(compositor->wl_display);
3534 rotate_grab_motion(struct weston_pointer_grab *grab,
3535 const struct timespec *time,
3536 struct weston_pointer_motion_event *event)
3538 struct rotate_grab *rotate =
3539 container_of(grab, struct rotate_grab, base.grab);
3540 struct weston_pointer *pointer = grab->pointer;
3541 struct shell_surface *shsurf = rotate->base.shsurf;
3542 struct weston_surface *surface =
3543 weston_desktop_surface_get_surface(shsurf->desktop_surface);
3544 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
3546 weston_pointer_move(pointer, event);
3551 cx = 0.5f * surface->width;
3552 cy = 0.5f * surface->height;
3554 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3555 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3556 r = sqrtf(dx * dx + dy * dy);
3558 wl_list_remove(&shsurf->rotation.transform.link);
3559 weston_view_geometry_dirty(shsurf->view);
3562 struct weston_matrix *matrix =
3563 &shsurf->rotation.transform.matrix;
3565 weston_matrix_init(&rotate->rotation);
3566 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3568 weston_matrix_init(matrix);
3569 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
3570 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
3571 weston_matrix_multiply(matrix, &rotate->rotation);
3572 weston_matrix_translate(matrix, cx, cy, 0.0f);
3575 &shsurf->view->geometry.transformation_list,
3576 &shsurf->rotation.transform.link);
3578 wl_list_init(&shsurf->rotation.transform.link);
3579 weston_matrix_init(&shsurf->rotation.rotation);
3580 weston_matrix_init(&rotate->rotation);
3583 /* We need to adjust the position of the surface
3584 * in case it was resized in a rotated state before */
3585 cposx = shsurf->view->geometry.x + cx;
3586 cposy = shsurf->view->geometry.y + cy;
3587 dposx = rotate->center.x - cposx;
3588 dposy = rotate->center.y - cposy;
3589 if (dposx != 0.0f || dposy != 0.0f) {
3590 weston_view_set_position(shsurf->view,
3591 shsurf->view->geometry.x + dposx,
3592 shsurf->view->geometry.y + dposy);
3595 /* Repaint implies weston_view_update_transform(), which
3596 * lazily applies the damage due to rotation update.
3598 weston_compositor_schedule_repaint(surface->compositor);
3602 rotate_grab_button(struct weston_pointer_grab *grab,
3603 const struct timespec *time,
3604 uint32_t button, uint32_t state_w)
3606 struct rotate_grab *rotate =
3607 container_of(grab, struct rotate_grab, base.grab);
3608 struct weston_pointer *pointer = grab->pointer;
3609 struct shell_surface *shsurf = rotate->base.shsurf;
3610 enum wl_pointer_button_state state = state_w;
3612 if (pointer->button_count == 0 &&
3613 state == WL_POINTER_BUTTON_STATE_RELEASED) {
3615 weston_matrix_multiply(&shsurf->rotation.rotation,
3617 shell_grab_end(&rotate->base);
3623 rotate_grab_cancel(struct weston_pointer_grab *grab)
3625 struct rotate_grab *rotate =
3626 container_of(grab, struct rotate_grab, base.grab);
3628 shell_grab_end(&rotate->base);
3632 static const struct weston_pointer_grab_interface rotate_grab_interface = {
3637 noop_grab_axis_source,
3643 surface_rotate(struct shell_surface *shsurf, struct weston_pointer *pointer)
3645 struct weston_surface *surface =
3646 weston_desktop_surface_get_surface(shsurf->desktop_surface);
3647 struct rotate_grab *rotate;
3651 rotate = malloc(sizeof *rotate);
3655 weston_view_to_global_float(shsurf->view,
3656 surface->width * 0.5f,
3657 surface->height * 0.5f,
3658 &rotate->center.x, &rotate->center.y);
3660 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
3661 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
3662 r = sqrtf(dx * dx + dy * dy);
3664 struct weston_matrix inverse;
3666 weston_matrix_init(&inverse);
3667 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
3668 weston_matrix_multiply(&shsurf->rotation.rotation, &inverse);
3670 weston_matrix_init(&rotate->rotation);
3671 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3673 weston_matrix_init(&shsurf->rotation.rotation);
3674 weston_matrix_init(&rotate->rotation);
3677 shell_grab_start(&rotate->base, &rotate_grab_interface, shsurf,
3678 pointer, WESTON_DESKTOP_SHELL_CURSOR_ARROW);
3682 rotate_binding(struct weston_pointer *pointer, const struct timespec *time,
3683 uint32_t button, void *data)
3685 struct weston_surface *focus;
3686 struct weston_surface *base_surface;
3687 struct shell_surface *surface;
3689 if (pointer->focus == NULL)
3692 focus = pointer->focus->surface;
3694 base_surface = weston_surface_get_main_surface(focus);
3695 if (base_surface == NULL)
3698 surface = get_shell_surface(base_surface);
3699 if (surface == NULL ||
3700 weston_desktop_surface_get_fullscreen(surface->desktop_surface) ||
3701 weston_desktop_surface_get_maximized(surface->desktop_surface))
3704 surface_rotate(surface, pointer);
3707 /* Move all fullscreen layers down to the current workspace and hide their
3708 * black views. The surfaces' state is set to both fullscreen and lowered,
3709 * and this is reversed when such a surface is re-configured, see
3710 * shell_configure_fullscreen() and shell_ensure_fullscreen_black_view().
3712 * lowering_output = NULL - Lower on all outputs, else only lower on the
3715 * This should be used when implementing shell-wide overlays, such as
3716 * the alt-tab switcher, which need to de-promote fullscreen layers. */
3718 lower_fullscreen_layer(struct desktop_shell *shell,
3719 struct weston_output *lowering_output)
3721 struct workspace *ws;
3722 struct weston_view *view, *prev;
3724 ws = get_current_workspace(shell);
3725 wl_list_for_each_reverse_safe(view, prev,
3726 &shell->fullscreen_layer.view_list.link,
3728 struct shell_surface *shsurf = get_shell_surface(view->surface);
3733 /* Only lower surfaces which have lowering_output as their fullscreen
3734 * output, unless a NULL output asks for lowering on all outputs.
3736 if (lowering_output && (shsurf->fullscreen_output != lowering_output))
3739 /* We can have a non-fullscreen popup for a fullscreen surface
3740 * in the fullscreen layer. */
3741 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface)) {
3742 /* Hide the black view */
3743 weston_layer_entry_remove(&shsurf->fullscreen.black_view->layer_link);
3744 wl_list_init(&shsurf->fullscreen.black_view->layer_link.link);
3745 weston_view_damage_below(shsurf->fullscreen.black_view);
3749 /* Lower the view to the workspace layer */
3750 weston_layer_entry_remove(&view->layer_link);
3751 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
3752 weston_view_damage_below(view);
3753 weston_surface_damage(view->surface);
3755 shsurf->state.lowered = true;
3760 activate(struct desktop_shell *shell, struct weston_view *view,
3761 struct weston_seat *seat, uint32_t flags)
3763 struct weston_surface *es = view->surface;
3764 struct weston_surface *main_surface;
3765 struct focus_state *state;
3766 struct workspace *ws;
3767 struct weston_surface *old_es;
3768 struct shell_surface *shsurf;
3770 main_surface = weston_surface_get_main_surface(es);
3771 shsurf = get_shell_surface(main_surface);
3774 /* Only demote fullscreen surfaces on the output of activated shsurf.
3775 * Leave fullscreen surfaces on unrelated outputs alone. */
3777 lower_fullscreen_layer(shell, shsurf->output);
3779 weston_view_activate(view, seat, flags);
3781 state = ensure_focus_state(shell, seat);
3785 old_es = state->keyboard_focus;
3786 focus_state_set_focus(state, es);
3788 if (weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) &&
3789 flags & WESTON_ACTIVATE_FLAG_CONFIGURE)
3790 shell_configure_fullscreen(shsurf);
3792 /* Update the surface’s layer. This brings it to the top of the stacking
3793 * order as appropriate. */
3794 shell_surface_update_layer(shsurf);
3796 if (shell->focus_animation_type != ANIMATION_NONE) {
3797 ws = get_current_workspace(shell);
3798 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
3802 /* no-op func for checking black surface */
3804 black_surface_committed(struct weston_surface *es, int32_t sx, int32_t sy)
3809 is_black_surface_view(struct weston_view *view, struct weston_view **fs_view)
3811 struct weston_surface *surface = view->surface;
3813 if (surface->committed == black_surface_committed) {
3815 *fs_view = surface->committed_private;
3822 activate_binding(struct weston_seat *seat,
3823 struct desktop_shell *shell,
3824 struct weston_view *focus_view,
3827 struct weston_view *main_view;
3828 struct weston_surface *main_surface;
3833 if (is_black_surface_view(focus_view, &main_view))
3834 focus_view = main_view;
3836 main_surface = weston_surface_get_main_surface(focus_view->surface);
3837 if (!get_shell_surface(main_surface))
3840 activate(shell, focus_view, seat, flags);
3844 click_to_activate_binding(struct weston_pointer *pointer,
3845 const struct timespec *time,
3846 uint32_t button, void *data)
3848 if (pointer->grab != &pointer->default_grab)
3850 if (pointer->focus == NULL)
3853 activate_binding(pointer->seat, data, pointer->focus,
3854 WESTON_ACTIVATE_FLAG_CLICKED |
3855 WESTON_ACTIVATE_FLAG_CONFIGURE);
3859 touch_to_activate_binding(struct weston_touch *touch,
3860 const struct timespec *time,
3863 if (touch->grab != &touch->default_grab)
3865 if (touch->focus == NULL)
3868 activate_binding(touch->seat, data, touch->focus,
3869 WESTON_ACTIVATE_FLAG_CONFIGURE);
3873 unfocus_all_seats(struct desktop_shell *shell)
3875 struct weston_seat *seat, *next;
3877 wl_list_for_each_safe(seat, next, &shell->compositor->seat_list, link) {
3878 struct weston_keyboard *keyboard =
3879 weston_seat_get_keyboard(seat);
3884 weston_keyboard_set_focus(keyboard, NULL);
3889 lock(struct desktop_shell *shell)
3891 struct workspace *ws = get_current_workspace(shell);
3893 if (shell->locked) {
3894 weston_compositor_sleep(shell->compositor);
3898 shell->locked = true;
3900 /* Hide all surfaces by removing the fullscreen, panel and
3901 * toplevel layers. This way nothing else can show or receive
3902 * input events while we are locked. */
3904 weston_layer_unset_position(&shell->panel_layer);
3905 weston_layer_unset_position(&shell->fullscreen_layer);
3906 if (shell->showing_input_panels)
3907 weston_layer_unset_position(&shell->input_panel_layer);
3908 weston_layer_unset_position(&ws->layer);
3910 weston_layer_set_position(&shell->lock_layer,
3911 WESTON_LAYER_POSITION_LOCK);
3913 weston_compositor_sleep(shell->compositor);
3915 /* Remove the keyboard focus on all seats. This will be
3916 * restored to the workspace's saved state via
3917 * restore_focus_state when the compositor is unlocked */
3918 unfocus_all_seats(shell);
3920 /* TODO: disable bindings that should not work while locked. */
3922 /* All this must be undone in resume_desktop(). */
3926 unlock(struct desktop_shell *shell)
3928 struct wl_resource *shell_resource;
3930 if (!shell->locked || shell->lock_surface) {
3931 shell_fade(shell, FADE_IN);
3935 /* If desktop-shell client has gone away, unlock immediately. */
3936 if (!shell->child.desktop_shell) {
3937 resume_desktop(shell);
3941 if (shell->prepare_event_sent)
3944 shell_resource = shell->child.desktop_shell;
3945 weston_desktop_shell_send_prepare_lock_surface(shell_resource);
3946 shell->prepare_event_sent = true;
3950 shell_fade_done_for_output(struct weston_view_animation *animation, void *data)
3952 struct shell_output *shell_output = data;
3953 struct desktop_shell *shell = shell_output->shell;
3955 shell_output->fade.animation = NULL;
3956 switch (shell_output->fade.type) {
3958 weston_surface_destroy(shell_output->fade.view->surface);
3959 shell_output->fade.view = NULL;
3969 static struct weston_view *
3970 shell_fade_create_surface_for_output(struct desktop_shell *shell, struct shell_output *shell_output)
3972 struct weston_compositor *compositor = shell->compositor;
3973 struct weston_surface *surface;
3974 struct weston_view *view;
3976 surface = weston_surface_create(compositor);
3980 view = weston_view_create(surface);
3982 weston_surface_destroy(surface);
3986 weston_surface_set_size(surface, shell_output->output->width, shell_output->output->height);
3987 weston_view_set_position(view, shell_output->output->x, shell_output->output->y);
3988 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3989 weston_layer_entry_insert(&compositor->fade_layer.view_list,
3991 pixman_region32_init(&surface->input);
3992 surface->is_mapped = true;
3993 view->is_mapped = true;
3999 shell_fade(struct desktop_shell *shell, enum fade_type type)
4002 struct shell_output *shell_output;
4012 weston_log("shell: invalid fade type\n");
4016 /* Create a separate fade surface for each output */
4017 wl_list_for_each(shell_output, &shell->output_list, link) {
4018 shell_output->fade.type = type;
4020 if (shell_output->fade.view == NULL) {
4021 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4022 if (!shell_output->fade.view)
4025 shell_output->fade.view->alpha = 1.0 - tint;
4026 weston_view_update_transform(shell_output->fade.view);
4029 if (shell_output->fade.view->output == NULL) {
4030 /* If the black view gets a NULL output, we lost the
4031 * last output and we'll just cancel the fade. This
4032 * happens when you close the last window under the
4033 * X11 or Wayland backends. */
4034 shell->locked = false;
4035 weston_surface_destroy(shell_output->fade.view->surface);
4036 shell_output->fade.view = NULL;
4037 } else if (shell_output->fade.animation) {
4038 weston_fade_update(shell_output->fade.animation, tint);
4040 shell_output->fade.animation =
4041 weston_fade_run(shell_output->fade.view,
4042 1.0 - tint, tint, 300.0,
4043 shell_fade_done_for_output, shell_output);
4049 do_shell_fade_startup(void *data)
4051 struct desktop_shell *shell = data;
4052 struct shell_output *shell_output;
4054 if (shell->startup_animation_type == ANIMATION_FADE) {
4055 shell_fade(shell, FADE_IN);
4057 weston_log("desktop shell: "
4058 "unexpected fade-in animation type %d\n",
4059 shell->startup_animation_type);
4060 wl_list_for_each(shell_output, &shell->output_list, link) {
4061 weston_surface_destroy(shell_output->fade.view->surface);
4062 shell_output->fade.view = NULL;
4068 shell_fade_startup(struct desktop_shell *shell)
4070 struct wl_event_loop *loop;
4071 struct shell_output *shell_output;
4072 bool has_fade = false;
4074 wl_list_for_each(shell_output, &shell->output_list, link) {
4075 if (!shell_output->fade.startup_timer)
4078 wl_event_source_remove(shell_output->fade.startup_timer);
4079 shell_output->fade.startup_timer = NULL;
4084 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4085 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4090 fade_startup_timeout(void *data)
4092 struct desktop_shell *shell = data;
4094 shell_fade_startup(shell);
4099 shell_fade_init(struct desktop_shell *shell)
4101 /* Make compositor output all black, and wait for the desktop-shell
4102 * client to signal it is ready, then fade in. The timer triggers a
4103 * fade-in, in case the desktop-shell client takes too long.
4106 struct wl_event_loop *loop;
4107 struct shell_output *shell_output;
4109 if (shell->startup_animation_type == ANIMATION_NONE)
4112 wl_list_for_each(shell_output, &shell->output_list, link) {
4113 if (shell_output->fade.view != NULL) {
4114 weston_log("%s: warning: fade surface already exists\n",
4119 shell_output->fade.view = shell_fade_create_surface_for_output(shell, shell_output);
4120 if (!shell_output->fade.view)
4123 weston_view_update_transform(shell_output->fade.view);
4124 weston_surface_damage(shell_output->fade.view->surface);
4126 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4127 shell_output->fade.startup_timer =
4128 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4129 wl_event_source_timer_update(shell_output->fade.startup_timer, 15000);
4134 idle_handler(struct wl_listener *listener, void *data)
4136 struct desktop_shell *shell =
4137 container_of(listener, struct desktop_shell, idle_listener);
4139 struct weston_seat *seat;
4141 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4142 weston_seat_break_desktop_grabs(seat);
4144 shell_fade(shell, FADE_OUT);
4145 /* lock() is called from shell_fade_done_for_output() */
4149 wake_handler(struct wl_listener *listener, void *data)
4151 struct desktop_shell *shell =
4152 container_of(listener, struct desktop_shell, wake_listener);
4158 transform_handler(struct wl_listener *listener, void *data)
4160 struct weston_surface *surface = data;
4161 struct shell_surface *shsurf = get_shell_surface(surface);
4162 const struct weston_xwayland_surface_api *api;
4168 api = shsurf->shell->xwayland_surface_api;
4170 api = weston_xwayland_surface_get_api(shsurf->shell->compositor);
4171 shsurf->shell->xwayland_surface_api = api;
4174 if (!api || !api->is_xwayland_surface(surface))
4177 if (!weston_view_is_mapped(shsurf->view))
4180 x = shsurf->view->geometry.x;
4181 y = shsurf->view->geometry.y;
4183 api->send_position(surface, x, y);
4187 center_on_output(struct weston_view *view, struct weston_output *output)
4189 int32_t surf_x, surf_y, width, height;
4193 weston_view_set_position(view, 0, 0);
4197 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
4199 x = output->x + (output->width - width) / 2 - surf_x / 2;
4200 y = output->y + (output->height - height) / 2 - surf_y / 2;
4202 weston_view_set_position(view, x, y);
4206 weston_view_set_initial_position(struct weston_view *view,
4207 struct desktop_shell *shell)
4209 struct weston_compositor *compositor = shell->compositor;
4211 int32_t range_x, range_y;
4213 struct weston_output *output, *target_output = NULL;
4214 struct weston_seat *seat;
4215 pixman_rectangle32_t area;
4217 /* As a heuristic place the new window on the same output as the
4218 * pointer. Falling back to the output containing 0, 0.
4220 * TODO: Do something clever for touch too?
4222 wl_list_for_each(seat, &compositor->seat_list, link) {
4223 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
4226 ix = wl_fixed_to_int(pointer->x);
4227 iy = wl_fixed_to_int(pointer->y);
4232 wl_list_for_each(output, &compositor->output_list, link) {
4233 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4234 target_output = output;
4239 if (!target_output) {
4240 weston_view_set_position(view, 10 + random() % 400,
4241 10 + random() % 400);
4245 /* Valid range within output where the surface will still be onscreen.
4246 * If this is negative it means that the surface is bigger than
4249 get_output_work_area(shell, target_output, &area);
4253 range_x = area.width - view->surface->width;
4254 range_y = area.height - view->surface->height;
4257 x += random() % range_x;
4260 y += random() % range_y;
4262 weston_view_set_position(view, x, y);
4266 check_desktop_shell_crash_too_early(struct desktop_shell *shell)
4268 struct timespec now;
4270 if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
4274 * If the shell helper client dies before the session has been
4275 * up for roughly 30 seconds, better just make Weston shut down,
4276 * because the user likely has no way to interact with the desktop
4279 if (now.tv_sec - shell->startup_time.tv_sec < 30) {
4280 weston_log("Error: %s apparently cannot run at all.\n",
4282 weston_log_continue(STAMP_SPACE "Quitting...");
4283 wl_display_terminate(shell->compositor->wl_display);
4291 static void launch_desktop_shell_process(void *data);
4294 respawn_desktop_shell_process(struct desktop_shell *shell)
4296 struct timespec time;
4298 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4299 weston_compositor_get_time(&time);
4300 if (timespec_sub_to_msec(&time, &shell->child.deathstamp) > 30000) {
4301 shell->child.deathstamp = time;
4302 shell->child.deathcount = 0;
4305 shell->child.deathcount++;
4306 if (shell->child.deathcount > 5) {
4307 weston_log("%s disconnected, giving up.\n", shell->client);
4311 weston_log("%s disconnected, respawning...\n", shell->client);
4312 launch_desktop_shell_process(shell);
4316 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4318 struct desktop_shell *shell;
4320 shell = container_of(listener, struct desktop_shell,
4321 child.client_destroy_listener);
4323 wl_list_remove(&shell->child.client_destroy_listener.link);
4324 shell->child.client = NULL;
4326 * unbind_desktop_shell() will reset shell->child.desktop_shell
4327 * before the respawned process has a chance to create a new
4328 * desktop_shell object, because we are being called from the
4329 * wl_client destructor which destroys all wl_resources before
4333 if (!check_desktop_shell_crash_too_early(shell))
4334 respawn_desktop_shell_process(shell);
4336 shell_fade_startup(shell);
4340 launch_desktop_shell_process(void *data)
4342 struct desktop_shell *shell = data;
4344 shell->child.client = weston_client_start(shell->compositor,
4347 if (!shell->child.client) {
4348 weston_log("not able to start %s\n", shell->client);
4352 shell->child.client_destroy_listener.notify =
4353 desktop_shell_client_destroy;
4354 wl_client_add_destroy_listener(shell->child.client,
4355 &shell->child.client_destroy_listener);
4359 unbind_desktop_shell(struct wl_resource *resource)
4361 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4364 resume_desktop(shell);
4366 shell->child.desktop_shell = NULL;
4367 shell->prepare_event_sent = false;
4371 bind_desktop_shell(struct wl_client *client,
4372 void *data, uint32_t version, uint32_t id)
4374 struct desktop_shell *shell = data;
4375 struct wl_resource *resource;
4377 resource = wl_resource_create(client, &weston_desktop_shell_interface,
4380 if (client == shell->child.client) {
4381 wl_resource_set_implementation(resource,
4382 &desktop_shell_implementation,
4383 shell, unbind_desktop_shell);
4384 shell->child.desktop_shell = resource;
4388 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4389 "permission to bind desktop_shell denied");
4393 struct desktop_shell *shell;
4394 struct weston_view *current;
4395 struct wl_listener listener;
4396 struct weston_keyboard_grab grab;
4397 struct wl_array minimized_array;
4401 switcher_next(struct switcher *switcher)
4403 struct weston_view *view;
4404 struct weston_view *first = NULL, *prev = NULL, *next = NULL;
4405 struct shell_surface *shsurf;
4406 struct workspace *ws = get_current_workspace(switcher->shell);
4408 /* temporary re-display minimized surfaces */
4409 struct weston_view *tmp;
4410 struct weston_view **minimized;
4411 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list.link, layer_link.link) {
4412 weston_layer_entry_remove(&view->layer_link);
4413 weston_layer_entry_insert(&ws->layer.view_list, &view->layer_link);
4414 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
4418 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4419 shsurf = get_shell_surface(view->surface);
4423 if (prev == switcher->current)
4427 weston_view_geometry_dirty(view);
4428 weston_surface_damage(view->surface);
4431 if (is_black_surface_view(view, NULL)) {
4433 weston_view_geometry_dirty(view);
4434 weston_surface_damage(view->surface);
4444 wl_list_remove(&switcher->listener.link);
4445 wl_signal_add(&next->destroy_signal, &switcher->listener);
4447 switcher->current = next;
4448 wl_list_for_each(view, &next->surface->views, surface_link)
4451 shsurf = get_shell_surface(switcher->current->surface);
4452 if (shsurf && weston_desktop_surface_get_fullscreen(shsurf->desktop_surface))
4453 shsurf->fullscreen.black_view->alpha = 1.0;
4457 switcher_handle_view_destroy(struct wl_listener *listener, void *data)
4459 struct switcher *switcher =
4460 container_of(listener, struct switcher, listener);
4462 switcher_next(switcher);
4466 switcher_destroy(struct switcher *switcher)
4468 struct weston_view *view;
4469 struct weston_keyboard *keyboard = switcher->grab.keyboard;
4470 struct workspace *ws = get_current_workspace(switcher->shell);
4472 wl_list_for_each(view, &ws->layer.view_list.link, layer_link.link) {
4473 if (is_focus_view(view))
4477 weston_surface_damage(view->surface);
4480 if (switcher->current) {
4481 activate(switcher->shell, switcher->current,
4483 WESTON_ACTIVATE_FLAG_CONFIGURE);
4486 wl_list_remove(&switcher->listener.link);
4487 weston_keyboard_end_grab(keyboard);
4488 if (keyboard->input_method_resource)
4489 keyboard->grab = &keyboard->input_method_grab;
4491 /* re-hide surfaces that were temporary shown during the switch */
4492 struct weston_view **minimized;
4493 wl_array_for_each(minimized, &switcher->minimized_array) {
4494 /* with the exception of the current selected */
4495 if ((*minimized)->surface != switcher->current->surface) {
4496 weston_layer_entry_remove(&(*minimized)->layer_link);
4497 weston_layer_entry_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
4498 weston_view_damage_below(*minimized);
4501 wl_array_release(&switcher->minimized_array);
4507 switcher_key(struct weston_keyboard_grab *grab,
4508 const struct timespec *time, uint32_t key, uint32_t state_w)
4510 struct switcher *switcher = container_of(grab, struct switcher, grab);
4511 enum wl_keyboard_key_state state = state_w;
4513 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4514 switcher_next(switcher);
4518 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4519 uint32_t mods_depressed, uint32_t mods_latched,
4520 uint32_t mods_locked, uint32_t group)
4522 struct switcher *switcher = container_of(grab, struct switcher, grab);
4523 struct weston_seat *seat = grab->keyboard->seat;
4525 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4526 switcher_destroy(switcher);
4530 switcher_cancel(struct weston_keyboard_grab *grab)
4532 struct switcher *switcher = container_of(grab, struct switcher, grab);
4534 switcher_destroy(switcher);
4537 static const struct weston_keyboard_grab_interface switcher_grab = {
4544 switcher_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4545 uint32_t key, void *data)
4547 struct desktop_shell *shell = data;
4548 struct switcher *switcher;
4550 switcher = malloc(sizeof *switcher);
4551 switcher->shell = shell;
4552 switcher->current = NULL;
4553 switcher->listener.notify = switcher_handle_view_destroy;
4554 wl_list_init(&switcher->listener.link);
4555 wl_array_init(&switcher->minimized_array);
4557 lower_fullscreen_layer(switcher->shell, NULL);
4558 switcher->grab.interface = &switcher_grab;
4559 weston_keyboard_start_grab(keyboard, &switcher->grab);
4560 weston_keyboard_set_focus(keyboard, NULL);
4561 switcher_next(switcher);
4565 backlight_binding(struct weston_keyboard *keyboard, const struct timespec *time,
4566 uint32_t key, void *data)
4568 struct weston_compositor *compositor = data;
4569 struct weston_output *output;
4570 long backlight_new = 0;
4572 /* TODO: we're limiting to simple use cases, where we assume just
4573 * control on the primary display. We'd have to extend later if we
4574 * ever get support for setting backlights on random desktop LCD
4576 output = get_default_output(compositor);
4580 if (!output->set_backlight)
4583 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4584 backlight_new = output->backlight_current - 25;
4585 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4586 backlight_new = output->backlight_current + 25;
4588 if (backlight_new < 5)
4590 if (backlight_new > 255)
4591 backlight_new = 255;
4593 output->backlight_current = backlight_new;
4594 output->set_backlight(output, output->backlight_current);
4598 force_kill_binding(struct weston_keyboard *keyboard,
4599 const struct timespec *time, uint32_t key, void *data)
4601 struct weston_surface *focus_surface;
4602 struct wl_client *client;
4603 struct desktop_shell *shell = data;
4604 struct weston_compositor *compositor = shell->compositor;
4607 focus_surface = keyboard->focus;
4611 wl_signal_emit(&compositor->kill_signal, focus_surface);
4613 client = wl_resource_get_client(focus_surface->resource);
4614 wl_client_get_credentials(client, &pid, NULL, NULL);
4616 /* Skip clients that we launched ourselves (the credentials of
4617 * the socketpair is ours) */
4618 if (pid == getpid())
4625 workspace_up_binding(struct weston_keyboard *keyboard,
4626 const struct timespec *time, uint32_t key, void *data)
4628 struct desktop_shell *shell = data;
4629 unsigned int new_index = shell->workspaces.current;
4636 change_workspace(shell, new_index);
4640 workspace_down_binding(struct weston_keyboard *keyboard,
4641 const struct timespec *time, uint32_t key, void *data)
4643 struct desktop_shell *shell = data;
4644 unsigned int new_index = shell->workspaces.current;
4648 if (new_index < shell->workspaces.num - 1)
4651 change_workspace(shell, new_index);
4655 workspace_f_binding(struct weston_keyboard *keyboard,
4656 const struct timespec *time, uint32_t key, void *data)
4658 struct desktop_shell *shell = data;
4659 unsigned int new_index;
4663 new_index = key - KEY_F1;
4664 if (new_index >= shell->workspaces.num)
4665 new_index = shell->workspaces.num - 1;
4667 change_workspace(shell, new_index);
4671 workspace_move_surface_up_binding(struct weston_keyboard *keyboard,
4672 const struct timespec *time, uint32_t key,
4675 struct desktop_shell *shell = data;
4676 unsigned int new_index = shell->workspaces.current;
4684 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4688 workspace_move_surface_down_binding(struct weston_keyboard *keyboard,
4689 const struct timespec *time, uint32_t key,
4692 struct desktop_shell *shell = data;
4693 unsigned int new_index = shell->workspaces.current;
4698 if (new_index < shell->workspaces.num - 1)
4701 take_surface_to_workspace_by_seat(shell, keyboard->seat, new_index);
4705 shell_reposition_view_on_output_destroy(struct weston_view *view)
4707 struct weston_output *output, *first_output;
4708 struct weston_compositor *ec = view->surface->compositor;
4709 struct shell_surface *shsurf;
4713 x = view->geometry.x;
4714 y = view->geometry.y;
4716 /* At this point the destroyed output is not in the list anymore.
4717 * If the view is still visible somewhere, we leave where it is,
4718 * otherwise, move it to the first output. */
4720 wl_list_for_each(output, &ec->output_list, link) {
4721 if (pixman_region32_contains_point(&output->region,
4729 first_output = container_of(ec->output_list.next,
4730 struct weston_output, link);
4732 x = first_output->x + first_output->width / 4;
4733 y = first_output->y + first_output->height / 4;
4735 weston_view_set_position(view, x, y);
4737 weston_view_geometry_dirty(view);
4741 shsurf = get_shell_surface(view->surface);
4745 shsurf->saved_position_valid = false;
4746 set_maximized(shsurf, false);
4747 set_fullscreen(shsurf, false, NULL);
4751 shell_for_each_layer(struct desktop_shell *shell,
4752 shell_for_each_layer_func_t func, void *data)
4754 struct workspace **ws;
4756 func(shell, &shell->fullscreen_layer, data);
4757 func(shell, &shell->panel_layer, data);
4758 func(shell, &shell->background_layer, data);
4759 func(shell, &shell->lock_layer, data);
4760 func(shell, &shell->input_panel_layer, data);
4762 wl_array_for_each(ws, &shell->workspaces.array)
4763 func(shell, &(*ws)->layer, data);
4767 shell_output_destroy_move_layer(struct desktop_shell *shell,
4768 struct weston_layer *layer,
4771 struct weston_view *view;
4773 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
4774 shell_reposition_view_on_output_destroy(view);
4778 handle_output_destroy(struct wl_listener *listener, void *data)
4780 struct shell_output *output_listener =
4781 container_of(listener, struct shell_output, destroy_listener);
4782 struct desktop_shell *shell = output_listener->shell;
4784 shell_for_each_layer(shell, shell_output_destroy_move_layer, NULL);
4786 if (output_listener->panel_surface)
4787 wl_list_remove(&output_listener->panel_surface_listener.link);
4788 if (output_listener->background_surface)
4789 wl_list_remove(&output_listener->background_surface_listener.link);
4790 wl_list_remove(&output_listener->destroy_listener.link);
4791 wl_list_remove(&output_listener->link);
4792 free(output_listener);
4796 shell_resize_surface_to_output(struct desktop_shell *shell,
4797 struct weston_surface *surface,
4798 const struct weston_output *output)
4803 weston_desktop_shell_send_configure(shell->child.desktop_shell, 0,
4811 handle_output_resized(struct wl_listener *listener, void *data)
4813 struct desktop_shell *shell =
4814 container_of(listener, struct desktop_shell, resized_listener);
4815 struct weston_output *output = (struct weston_output *)data;
4816 struct shell_output *sh_output = find_shell_output_from_weston_output(shell, output);
4818 shell_resize_surface_to_output(shell, sh_output->background_surface, output);
4819 shell_resize_surface_to_output(shell, sh_output->panel_surface, output);
4823 create_shell_output(struct desktop_shell *shell,
4824 struct weston_output *output)
4826 struct shell_output *shell_output;
4828 shell_output = zalloc(sizeof *shell_output);
4829 if (shell_output == NULL)
4832 shell_output->output = output;
4833 shell_output->shell = shell;
4834 shell_output->destroy_listener.notify = handle_output_destroy;
4835 wl_signal_add(&output->destroy_signal,
4836 &shell_output->destroy_listener);
4837 wl_list_insert(shell->output_list.prev, &shell_output->link);
4841 handle_output_create(struct wl_listener *listener, void *data)
4843 struct desktop_shell *shell =
4844 container_of(listener, struct desktop_shell, output_create_listener);
4845 struct weston_output *output = (struct weston_output *)data;
4847 create_shell_output(shell, output);
4851 handle_output_move_layer(struct desktop_shell *shell,
4852 struct weston_layer *layer, void *data)
4854 struct weston_output *output = data;
4855 struct weston_view *view;
4858 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
4859 if (view->output != output)
4862 x = view->geometry.x + output->move_x;
4863 y = view->geometry.y + output->move_y;
4864 weston_view_set_position(view, x, y);
4869 handle_output_move(struct wl_listener *listener, void *data)
4871 struct desktop_shell *shell;
4873 shell = container_of(listener, struct desktop_shell,
4874 output_move_listener);
4876 shell_for_each_layer(shell, handle_output_move_layer, data);
4880 setup_output_destroy_handler(struct weston_compositor *ec,
4881 struct desktop_shell *shell)
4883 struct weston_output *output;
4885 wl_list_init(&shell->output_list);
4886 wl_list_for_each(output, &ec->output_list, link)
4887 create_shell_output(shell, output);
4889 shell->output_create_listener.notify = handle_output_create;
4890 wl_signal_add(&ec->output_created_signal,
4891 &shell->output_create_listener);
4893 shell->output_move_listener.notify = handle_output_move;
4894 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
4898 shell_destroy(struct wl_listener *listener, void *data)
4900 struct desktop_shell *shell =
4901 container_of(listener, struct desktop_shell, destroy_listener);
4902 struct workspace **ws;
4903 struct shell_output *shell_output, *tmp;
4905 /* Force state to unlocked so we don't try to fade */
4906 shell->locked = false;
4908 if (shell->child.client) {
4909 /* disable respawn */
4910 wl_list_remove(&shell->child.client_destroy_listener.link);
4911 wl_client_destroy(shell->child.client);
4914 wl_list_remove(&shell->destroy_listener.link);
4915 wl_list_remove(&shell->idle_listener.link);
4916 wl_list_remove(&shell->wake_listener.link);
4917 wl_list_remove(&shell->transform_listener.link);
4919 text_backend_destroy(shell->text_backend);
4920 input_panel_destroy(shell);
4922 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
4923 wl_list_remove(&shell_output->destroy_listener.link);
4924 wl_list_remove(&shell_output->link);
4928 wl_list_remove(&shell->output_create_listener.link);
4929 wl_list_remove(&shell->output_move_listener.link);
4930 wl_list_remove(&shell->resized_listener.link);
4932 wl_array_for_each(ws, &shell->workspaces.array)
4933 workspace_destroy(*ws);
4934 wl_array_release(&shell->workspaces.array);
4936 free(shell->client);
4941 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4944 int i, num_workspace_bindings;
4946 if (shell->allow_zap)
4947 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4948 MODIFIER_CTRL | MODIFIER_ALT,
4949 terminate_binding, ec);
4951 /* fixed bindings */
4952 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4953 click_to_activate_binding,
4955 weston_compositor_add_button_binding(ec, BTN_RIGHT, 0,
4956 click_to_activate_binding,
4958 weston_compositor_add_touch_binding(ec, 0,
4959 touch_to_activate_binding,
4961 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4962 backlight_binding, ec);
4963 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4964 backlight_binding, ec);
4966 /* configurable bindings */
4967 if (shell->exposay_modifier)
4968 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
4969 exposay_binding, shell);
4971 mod = shell->binding_modifier;
4975 /* This binding is not configurable, but is only enabled if there is a
4976 * valid binding modifier. */
4977 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4978 MODIFIER_SUPER | MODIFIER_ALT,
4979 surface_opacity_binding, NULL);
4981 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4982 mod, zoom_axis_binding,
4985 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4986 zoom_key_binding, NULL);
4987 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4988 zoom_key_binding, NULL);
4989 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
4990 maximize_binding, NULL);
4991 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
4992 fullscreen_binding, NULL);
4993 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4995 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
4996 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4997 resize_binding, shell);
4998 weston_compositor_add_button_binding(ec, BTN_LEFT,
4999 mod | MODIFIER_SHIFT,
5000 resize_binding, shell);
5002 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5003 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5004 rotate_binding, NULL);
5006 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5008 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5010 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5012 weston_compositor_add_key_binding(ec, KEY_K, mod,
5013 force_kill_binding, shell);
5014 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5015 workspace_up_binding, shell);
5016 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5017 workspace_down_binding, shell);
5018 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5019 workspace_move_surface_up_binding,
5021 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5022 workspace_move_surface_down_binding,
5025 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5026 if (shell->workspaces.num > 1) {
5027 num_workspace_bindings = shell->workspaces.num;
5028 if (num_workspace_bindings > 6)
5029 num_workspace_bindings = 6;
5030 for (i = 0; i < num_workspace_bindings; i++)
5031 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5032 workspace_f_binding,
5036 weston_install_debug_key_binding(ec, mod);
5040 handle_seat_created(struct wl_listener *listener, void *data)
5042 struct weston_seat *seat = data;
5044 create_shell_seat(seat);
5048 wet_shell_init(struct weston_compositor *ec,
5049 int *argc, char *argv[])
5051 struct weston_seat *seat;
5052 struct desktop_shell *shell;
5053 struct workspace **pws;
5055 struct wl_event_loop *loop;
5057 shell = zalloc(sizeof *shell);
5061 shell->compositor = ec;
5063 shell->destroy_listener.notify = shell_destroy;
5064 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
5065 shell->idle_listener.notify = idle_handler;
5066 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5067 shell->wake_listener.notify = wake_handler;
5068 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
5069 shell->transform_listener.notify = transform_handler;
5070 wl_signal_add(&ec->transform_signal, &shell->transform_listener);
5072 weston_layer_init(&shell->fullscreen_layer, ec);
5073 weston_layer_init(&shell->panel_layer, ec);
5074 weston_layer_init(&shell->background_layer, ec);
5075 weston_layer_init(&shell->lock_layer, ec);
5076 weston_layer_init(&shell->input_panel_layer, ec);
5078 weston_layer_set_position(&shell->fullscreen_layer,
5079 WESTON_LAYER_POSITION_FULLSCREEN);
5080 weston_layer_set_position(&shell->panel_layer,
5081 WESTON_LAYER_POSITION_UI);
5082 weston_layer_set_position(&shell->background_layer,
5083 WESTON_LAYER_POSITION_BACKGROUND);
5085 wl_array_init(&shell->workspaces.array);
5086 wl_list_init(&shell->workspaces.client_list);
5088 if (input_panel_setup(shell) < 0)
5091 shell->text_backend = text_backend_init(ec);
5092 if (!shell->text_backend)
5095 shell_configuration(shell);
5097 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5098 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5100 for (i = 0; i < shell->workspaces.num; i++) {
5101 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5105 *pws = workspace_create(shell);
5109 activate_workspace(shell, 0);
5111 weston_layer_init(&shell->minimized_layer, ec);
5113 wl_list_init(&shell->workspaces.anim_sticky_list);
5114 wl_list_init(&shell->workspaces.animation.link);
5115 shell->workspaces.animation.frame = animate_workspace_change_frame;
5117 shell->desktop = weston_desktop_create(ec, &shell_desktop_api, shell);
5118 if (!shell->desktop)
5121 if (wl_global_create(ec->wl_display,
5122 &weston_desktop_shell_interface, 1,
5123 shell, bind_desktop_shell) == NULL)
5126 weston_compositor_get_time(&shell->child.deathstamp);
5128 shell->panel_position = WESTON_DESKTOP_SHELL_PANEL_POSITION_TOP;
5130 setup_output_destroy_handler(ec, shell);
5132 loop = wl_display_get_event_loop(ec->wl_display);
5133 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
5135 wl_list_for_each(seat, &ec->seat_list, link)
5136 handle_seat_created(NULL, seat);
5137 shell->seat_create_listener.notify = handle_seat_created;
5138 wl_signal_add(&ec->seat_created_signal, &shell->seat_create_listener);
5140 shell->resized_listener.notify = handle_output_resized;
5141 wl_signal_add(&ec->output_resized_signal, &shell->resized_listener);
5143 screenshooter_create(ec);
5145 shell_add_bindings(ec, shell);
5147 shell_fade_init(shell);
5149 clock_gettime(CLOCK_MONOTONIC, &shell->startup_time);