2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #include <linux/input.h>
35 #include <sys/types.h>
38 #include "desktop-shell-server-protocol.h"
39 #include "workspaces-server-protocol.h"
40 #include "../shared/config-parser.h"
41 #include "xdg-shell-server-protocol.h"
43 #define DEFAULT_NUM_WORKSPACES 1
44 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
47 #define static_assert(cond, msg)
51 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
59 enum shell_surface_type {
61 SHELL_SURFACE_TOPLEVEL,
63 SHELL_SURFACE_XWAYLAND
67 * Surface stacking and ordering.
69 * This is handled using several linked lists of surfaces, organised into
70 * ‘layers’. The layers are ordered, and each of the surfaces in one layer are
71 * above all of the surfaces in the layer below. The set of layers is static and
72 * in the following order (top-most first):
73 * • Lock layer (only ever displayed on its own)
81 * The list of layers may be manipulated to remove whole layers of surfaces from
82 * display. For example, when locking the screen, all layers except the lock
85 * A surface’s layer is modified on configuring the surface, in
86 * set_surface_type() (which is only called when the surface’s type change is
87 * _committed_). If a surface’s type changes (e.g. when making a window
88 * fullscreen) its layer changes too.
90 * In order to allow popup and transient surfaces to be correctly stacked above
91 * their parent surfaces, each surface tracks both its parent surface, and a
92 * linked list of its children. When a surface’s layer is updated, so are the
93 * layers of its children. Note that child surfaces are *not* the same as
94 * subsurfaces — child/parent surfaces are purely for maintaining stacking
97 * The children_link list of siblings of a surface (i.e. those surfaces which
98 * have the same parent) only contains weston_surfaces which have a
99 * shell_surface. Stacking is not implemented for non-shell_surface
100 * weston_surfaces. This means that the following implication does *not* hold:
101 * (shsurf->parent != NULL) ⇒ !wl_list_is_empty(shsurf->children_link)
104 struct shell_surface {
105 struct wl_resource *resource;
106 struct wl_signal destroy_signal;
108 struct weston_surface *surface;
109 struct weston_view *view;
110 int32_t last_width, last_height;
111 struct wl_listener surface_destroy_listener;
112 struct wl_listener resource_destroy_listener;
114 struct weston_surface *parent;
115 struct wl_list children_list; /* child surfaces of this one */
116 struct wl_list children_link; /* sibling surfaces of this one */
117 struct desktop_shell *shell;
119 enum shell_surface_type type;
121 int32_t saved_x, saved_y;
122 int32_t saved_width, saved_height;
123 bool saved_position_valid;
124 bool saved_size_valid;
125 bool saved_rotation_valid;
126 int unresponsive, grabbed;
127 uint32_t resize_edges;
130 struct weston_transform transform;
131 struct weston_matrix rotation;
135 struct wl_list grab_link;
137 struct shell_seat *shseat;
147 enum wl_shell_surface_fullscreen_method type;
148 struct weston_transform transform; /* matrix from x, y */
150 struct weston_view *black_view;
153 struct weston_transform workspace_transform;
155 struct weston_output *fullscreen_output;
156 struct weston_output *output;
157 struct weston_output *recommended_output;
160 const struct weston_shell_client *client;
166 } state, next_state, requested_state; /* surface states */
168 bool state_requested;
174 struct weston_pointer_grab grab;
175 struct shell_surface *shsurf;
176 struct wl_listener shsurf_destroy_listener;
179 struct shell_touch_grab {
180 struct weston_touch_grab grab;
181 struct shell_surface *shsurf;
182 struct wl_listener shsurf_destroy_listener;
183 struct weston_touch *touch;
186 struct weston_move_grab {
187 struct shell_grab base;
191 struct weston_touch_move_grab {
192 struct shell_touch_grab base;
198 struct shell_grab base;
199 struct weston_matrix rotation;
207 struct weston_seat *seat;
208 struct wl_listener seat_destroy_listener;
209 struct weston_surface *focused_surface;
212 struct weston_pointer_grab grab;
213 struct wl_list surfaces_list;
214 struct wl_client *client;
219 struct shell_client {
220 struct wl_resource *resource;
221 struct wl_client *client;
222 struct desktop_shell *shell;
223 struct wl_listener destroy_listener;
224 struct wl_event_source *ping_timer;
225 uint32_t ping_serial;
230 set_alpha_if_fullscreen(struct shell_surface *shsurf)
232 if (shsurf && shsurf->state.fullscreen)
233 shsurf->fullscreen.black_view->alpha = 0.25;
236 static struct shell_client *
237 get_shell_client(struct wl_client *client);
239 static struct desktop_shell *
240 shell_surface_get_shell(struct shell_surface *shsurf);
243 surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
246 shell_fade_startup(struct desktop_shell *shell);
248 static struct shell_seat *
249 get_shell_seat(struct weston_seat *seat);
252 shell_surface_update_child_surface_layers(struct shell_surface *shsurf);
255 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf);
258 shell_surface_is_xdg_surface(struct shell_surface *shsurf);
261 shell_surface_is_xdg_popup(struct shell_surface *shsurf);
264 shell_surface_set_parent(struct shell_surface *shsurf,
265 struct weston_surface *parent);
268 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
270 struct desktop_shell *shell;
271 struct weston_view *top_fs_ev;
273 shell = shell_surface_get_shell(shsurf);
275 if (wl_list_empty(&shell->fullscreen_layer.view_list))
278 top_fs_ev = container_of(shell->fullscreen_layer.view_list.next,
281 return (shsurf == get_shell_surface(top_fs_ev->surface));
285 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
287 struct shell_grab *grab;
289 grab = container_of(listener, struct shell_grab,
290 shsurf_destroy_listener);
296 get_default_view(struct weston_surface *surface)
298 struct shell_surface *shsurf;
299 struct weston_view *view;
301 if (!surface || wl_list_empty(&surface->views))
304 shsurf = get_shell_surface(surface);
308 wl_list_for_each(view, &surface->views, surface_link)
309 if (weston_view_is_mapped(view))
312 return container_of(surface->views.next, struct weston_view, surface_link);
316 popup_grab_end(struct weston_pointer *pointer);
319 shell_grab_start(struct shell_grab *grab,
320 const struct weston_pointer_grab_interface *interface,
321 struct shell_surface *shsurf,
322 struct weston_pointer *pointer,
323 enum desktop_shell_cursor cursor)
325 struct desktop_shell *shell = shsurf->shell;
327 popup_grab_end(pointer);
329 grab->grab.interface = interface;
330 grab->shsurf = shsurf;
331 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
332 wl_signal_add(&shsurf->destroy_signal,
333 &grab->shsurf_destroy_listener);
336 weston_pointer_start_grab(pointer, &grab->grab);
337 if (shell->child.desktop_shell) {
338 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
340 weston_pointer_set_focus(pointer,
341 get_default_view(shell->grab_surface),
342 wl_fixed_from_int(0),
343 wl_fixed_from_int(0));
348 shell_grab_end(struct shell_grab *grab)
351 wl_list_remove(&grab->shsurf_destroy_listener.link);
352 grab->shsurf->grabbed = 0;
353 grab->shsurf->resize_edges = 0;
356 weston_pointer_end_grab(grab->grab.pointer);
360 shell_touch_grab_start(struct shell_touch_grab *grab,
361 const struct weston_touch_grab_interface *interface,
362 struct shell_surface *shsurf,
363 struct weston_touch *touch)
365 struct desktop_shell *shell = shsurf->shell;
367 grab->grab.interface = interface;
368 grab->shsurf = shsurf;
369 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
370 wl_signal_add(&shsurf->destroy_signal,
371 &grab->shsurf_destroy_listener);
376 weston_touch_start_grab(touch, &grab->grab);
377 if (shell->child.desktop_shell)
378 weston_touch_set_focus(touch->seat,
379 get_default_view(shell->grab_surface));
383 shell_touch_grab_end(struct shell_touch_grab *grab)
386 wl_list_remove(&grab->shsurf_destroy_listener.link);
387 grab->shsurf->grabbed = 0;
390 weston_touch_end_grab(grab->touch);
394 center_on_output(struct weston_view *view,
395 struct weston_output *output);
397 static enum weston_keyboard_modifier
398 get_modifier(char *modifier)
401 return MODIFIER_SUPER;
403 if (!strcmp("ctrl", modifier))
404 return MODIFIER_CTRL;
405 else if (!strcmp("alt", modifier))
407 else if (!strcmp("super", modifier))
408 return MODIFIER_SUPER;
410 return MODIFIER_SUPER;
413 static enum animation_type
414 get_animation_type(char *animation)
417 return ANIMATION_NONE;
419 if (!strcmp("zoom", animation))
420 return ANIMATION_ZOOM;
421 else if (!strcmp("fade", animation))
422 return ANIMATION_FADE;
423 else if (!strcmp("dim-layer", animation))
424 return ANIMATION_DIM_LAYER;
426 return ANIMATION_NONE;
430 shell_configuration(struct desktop_shell *shell)
432 struct weston_config_section *section;
436 section = weston_config_get_section(shell->compositor->config,
437 "screensaver", NULL, NULL);
438 weston_config_section_get_string(section,
439 "path", &shell->screensaver.path, NULL);
440 weston_config_section_get_int(section, "duration", &duration, 60);
441 shell->screensaver.duration = duration * 1000;
443 section = weston_config_get_section(shell->compositor->config,
444 "shell", NULL, NULL);
445 weston_config_section_get_string(section,
446 "client", &s, LIBEXECDIR "/" WESTON_SHELL_CLIENT);
448 weston_config_section_get_string(section,
449 "binding-modifier", &s, "super");
450 shell->binding_modifier = get_modifier(s);
453 weston_config_section_get_string(section,
454 "exposay-modifier", &s, "none");
455 if (strcmp(s, "none") == 0)
456 shell->exposay_modifier = 0;
458 shell->exposay_modifier = get_modifier(s);
461 weston_config_section_get_string(section, "animation", &s, "none");
462 shell->win_animation_type = get_animation_type(s);
464 weston_config_section_get_string(section,
465 "startup-animation", &s, "fade");
466 shell->startup_animation_type = get_animation_type(s);
468 if (shell->startup_animation_type == ANIMATION_ZOOM)
469 shell->startup_animation_type = ANIMATION_NONE;
470 weston_config_section_get_string(section, "focus-animation", &s, "none");
471 shell->focus_animation_type = get_animation_type(s);
473 weston_config_section_get_uint(section, "num-workspaces",
474 &shell->workspaces.num,
475 DEFAULT_NUM_WORKSPACES);
478 struct weston_output *
479 get_default_output(struct weston_compositor *compositor)
481 return container_of(compositor->output_list.next,
482 struct weston_output, link);
486 /* no-op func for checking focus surface */
488 focus_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
492 static struct focus_surface *
493 get_focus_surface(struct weston_surface *surface)
495 if (surface->configure == focus_surface_configure)
496 return surface->configure_private;
502 is_focus_surface (struct weston_surface *es)
504 return (es->configure == focus_surface_configure);
508 is_focus_view (struct weston_view *view)
510 return is_focus_surface (view->surface);
513 static struct focus_surface *
514 create_focus_surface(struct weston_compositor *ec,
515 struct weston_output *output)
517 struct focus_surface *fsurf = NULL;
518 struct weston_surface *surface = NULL;
520 fsurf = malloc(sizeof *fsurf);
524 fsurf->surface = weston_surface_create(ec);
525 surface = fsurf->surface;
526 if (surface == NULL) {
531 surface->configure = focus_surface_configure;
532 surface->output = output;
533 surface->configure_private = fsurf;
535 fsurf->view = weston_view_create(surface);
536 if (fsurf->view == NULL) {
537 weston_surface_destroy(surface);
541 fsurf->view->output = output;
543 weston_surface_set_size(surface, output->width, output->height);
544 weston_view_set_position(fsurf->view, output->x, output->y);
545 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
546 pixman_region32_fini(&surface->opaque);
547 pixman_region32_init_rect(&surface->opaque, output->x, output->y,
548 output->width, output->height);
549 pixman_region32_fini(&surface->input);
550 pixman_region32_init(&surface->input);
552 wl_list_init(&fsurf->workspace_transform.link);
558 focus_surface_destroy(struct focus_surface *fsurf)
560 weston_surface_destroy(fsurf->surface);
565 focus_animation_done(struct weston_view_animation *animation, void *data)
567 struct workspace *ws = data;
569 ws->focus_animation = NULL;
573 focus_state_destroy(struct focus_state *state)
575 wl_list_remove(&state->seat_destroy_listener.link);
576 wl_list_remove(&state->surface_destroy_listener.link);
581 focus_state_seat_destroy(struct wl_listener *listener, void *data)
583 struct focus_state *state = container_of(listener,
585 seat_destroy_listener);
587 wl_list_remove(&state->link);
588 focus_state_destroy(state);
592 focus_state_surface_destroy(struct wl_listener *listener, void *data)
594 struct focus_state *state = container_of(listener,
596 surface_destroy_listener);
597 struct desktop_shell *shell;
598 struct weston_surface *main_surface, *next;
599 struct weston_view *view;
601 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
604 wl_list_for_each(view, &state->ws->layer.view_list, layer_link) {
605 if (view->surface == main_surface)
607 if (is_focus_view(view))
610 next = view->surface;
614 /* if the focus was a sub-surface, activate its main surface */
615 if (main_surface != state->keyboard_focus)
618 shell = state->seat->compositor->shell_interface.shell;
620 state->keyboard_focus = NULL;
621 activate(shell, next, state->seat);
623 if (shell->focus_animation_type == ANIMATION_DIM_LAYER) {
624 if (state->ws->focus_animation)
625 weston_view_animation_destroy(state->ws->focus_animation);
627 state->ws->focus_animation = weston_fade_run(
628 state->ws->fsurf_front->view,
629 state->ws->fsurf_front->view->alpha, 0.0, 300,
630 focus_animation_done, state->ws);
633 wl_list_remove(&state->link);
634 focus_state_destroy(state);
638 static struct focus_state *
639 focus_state_create(struct weston_seat *seat, struct workspace *ws)
641 struct focus_state *state;
643 state = malloc(sizeof *state);
647 state->keyboard_focus = NULL;
650 wl_list_insert(&ws->focus_list, &state->link);
652 state->seat_destroy_listener.notify = focus_state_seat_destroy;
653 state->surface_destroy_listener.notify = focus_state_surface_destroy;
654 wl_signal_add(&seat->destroy_signal,
655 &state->seat_destroy_listener);
656 wl_list_init(&state->surface_destroy_listener.link);
661 static struct focus_state *
662 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
664 struct workspace *ws = get_current_workspace(shell);
665 struct focus_state *state;
667 wl_list_for_each(state, &ws->focus_list, link)
668 if (state->seat == seat)
671 if (&state->link == &ws->focus_list)
672 state = focus_state_create(seat, ws);
678 focus_state_set_focus(struct focus_state *state,
679 struct weston_surface *surface)
681 if (state->keyboard_focus) {
682 wl_list_remove(&state->surface_destroy_listener.link);
683 wl_list_init(&state->surface_destroy_listener.link);
686 state->keyboard_focus = surface;
688 wl_signal_add(&surface->destroy_signal,
689 &state->surface_destroy_listener);
693 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
695 struct focus_state *state, *next;
696 struct weston_surface *surface;
698 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
699 if (state->seat->keyboard == NULL)
702 surface = state->keyboard_focus;
704 weston_keyboard_set_focus(state->seat->keyboard, surface);
709 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
710 struct weston_seat *seat)
712 struct focus_state *state;
714 wl_list_for_each(state, &ws->focus_list, link) {
715 if (state->seat == seat) {
716 focus_state_set_focus(state, seat->keyboard->focus);
723 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
724 struct weston_surface *surface)
726 struct focus_state *state;
728 wl_list_for_each(state, &ws->focus_list, link)
729 if (state->keyboard_focus == surface)
730 focus_state_set_focus(state, NULL);
734 animate_focus_change(struct desktop_shell *shell, struct workspace *ws,
735 struct weston_view *from, struct weston_view *to)
737 struct weston_output *output;
738 bool focus_surface_created = false;
740 /* FIXME: Only support dim animation using two layers */
741 if (from == to || shell->focus_animation_type != ANIMATION_DIM_LAYER)
744 output = get_default_output(shell->compositor);
745 if (ws->fsurf_front == NULL && (from || to)) {
746 ws->fsurf_front = create_focus_surface(shell->compositor, output);
747 if (ws->fsurf_front == NULL)
749 ws->fsurf_front->view->alpha = 0.0;
751 ws->fsurf_back = create_focus_surface(shell->compositor, output);
752 if (ws->fsurf_back == NULL) {
753 focus_surface_destroy(ws->fsurf_front);
756 ws->fsurf_back->view->alpha = 0.0;
758 focus_surface_created = true;
760 wl_list_remove(&ws->fsurf_front->view->layer_link);
761 wl_list_remove(&ws->fsurf_back->view->layer_link);
764 if (ws->focus_animation) {
765 weston_view_animation_destroy(ws->focus_animation);
766 ws->focus_animation = NULL;
770 wl_list_insert(&to->layer_link,
771 &ws->fsurf_front->view->layer_link);
773 wl_list_insert(&ws->layer.view_list,
774 &ws->fsurf_front->view->layer_link);
776 if (focus_surface_created) {
777 ws->focus_animation = weston_fade_run(
778 ws->fsurf_front->view,
779 ws->fsurf_front->view->alpha, 0.6, 300,
780 focus_animation_done, ws);
782 wl_list_insert(&from->layer_link,
783 &ws->fsurf_back->view->layer_link);
784 ws->focus_animation = weston_stable_fade_run(
785 ws->fsurf_front->view, 0.0,
786 ws->fsurf_back->view, 0.6,
787 focus_animation_done, ws);
789 wl_list_insert(&ws->layer.view_list,
790 &ws->fsurf_back->view->layer_link);
791 ws->focus_animation = weston_stable_fade_run(
792 ws->fsurf_front->view, 0.0,
793 ws->fsurf_back->view, 0.6,
794 focus_animation_done, ws);
799 workspace_destroy(struct workspace *ws)
801 struct focus_state *state, *next;
803 wl_list_for_each_safe(state, next, &ws->focus_list, link)
804 focus_state_destroy(state);
807 focus_surface_destroy(ws->fsurf_front);
809 focus_surface_destroy(ws->fsurf_back);
815 seat_destroyed(struct wl_listener *listener, void *data)
817 struct weston_seat *seat = data;
818 struct focus_state *state, *next;
819 struct workspace *ws = container_of(listener,
821 seat_destroyed_listener);
823 wl_list_for_each_safe(state, next, &ws->focus_list, link)
824 if (state->seat == seat)
825 wl_list_remove(&state->link);
828 static struct workspace *
829 workspace_create(void)
831 struct workspace *ws = malloc(sizeof *ws);
835 weston_layer_init(&ws->layer, NULL);
837 wl_list_init(&ws->focus_list);
838 wl_list_init(&ws->seat_destroyed_listener.link);
839 ws->seat_destroyed_listener.notify = seat_destroyed;
840 ws->fsurf_front = NULL;
841 ws->fsurf_back = NULL;
842 ws->focus_animation = NULL;
848 workspace_is_empty(struct workspace *ws)
850 return wl_list_empty(&ws->layer.view_list);
853 static struct workspace *
854 get_workspace(struct desktop_shell *shell, unsigned int index)
856 struct workspace **pws = shell->workspaces.array.data;
857 assert(index < shell->workspaces.num);
863 get_current_workspace(struct desktop_shell *shell)
865 return get_workspace(shell, shell->workspaces.current);
869 activate_workspace(struct desktop_shell *shell, unsigned int index)
871 struct workspace *ws;
873 ws = get_workspace(shell, index);
874 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
876 shell->workspaces.current = index;
880 get_output_height(struct weston_output *output)
882 return abs(output->region.extents.y1 - output->region.extents.y2);
886 view_translate(struct workspace *ws, struct weston_view *view, double d)
888 struct weston_transform *transform;
890 if (is_focus_view(view)) {
891 struct focus_surface *fsurf = get_focus_surface(view->surface);
892 transform = &fsurf->workspace_transform;
894 struct shell_surface *shsurf = get_shell_surface(view->surface);
895 transform = &shsurf->workspace_transform;
898 if (wl_list_empty(&transform->link))
899 wl_list_insert(view->geometry.transformation_list.prev,
902 weston_matrix_init(&transform->matrix);
903 weston_matrix_translate(&transform->matrix,
905 weston_view_geometry_dirty(view);
909 workspace_translate_out(struct workspace *ws, double fraction)
911 struct weston_view *view;
915 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
916 height = get_output_height(view->surface->output);
917 d = height * fraction;
919 view_translate(ws, view, d);
924 workspace_translate_in(struct workspace *ws, double fraction)
926 struct weston_view *view;
930 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
931 height = get_output_height(view->surface->output);
934 d = -(height - height * fraction);
936 d = height + height * fraction;
938 view_translate(ws, view, d);
943 broadcast_current_workspace_state(struct desktop_shell *shell)
945 struct wl_resource *resource;
947 wl_resource_for_each(resource, &shell->workspaces.client_list)
948 workspace_manager_send_state(resource,
949 shell->workspaces.current,
950 shell->workspaces.num);
954 reverse_workspace_change_animation(struct desktop_shell *shell,
956 struct workspace *from,
957 struct workspace *to)
959 shell->workspaces.current = index;
961 shell->workspaces.anim_to = to;
962 shell->workspaces.anim_from = from;
963 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
964 shell->workspaces.anim_timestamp = 0;
966 weston_compositor_schedule_repaint(shell->compositor);
970 workspace_deactivate_transforms(struct workspace *ws)
972 struct weston_view *view;
973 struct weston_transform *transform;
975 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
976 if (is_focus_view(view)) {
977 struct focus_surface *fsurf = get_focus_surface(view->surface);
978 transform = &fsurf->workspace_transform;
980 struct shell_surface *shsurf = get_shell_surface(view->surface);
981 transform = &shsurf->workspace_transform;
984 if (!wl_list_empty(&transform->link)) {
985 wl_list_remove(&transform->link);
986 wl_list_init(&transform->link);
988 weston_view_geometry_dirty(view);
993 finish_workspace_change_animation(struct desktop_shell *shell,
994 struct workspace *from,
995 struct workspace *to)
997 weston_compositor_schedule_repaint(shell->compositor);
999 wl_list_remove(&shell->workspaces.animation.link);
1000 workspace_deactivate_transforms(from);
1001 workspace_deactivate_transforms(to);
1002 shell->workspaces.anim_to = NULL;
1004 wl_list_remove(&shell->workspaces.anim_from->layer.link);
1008 animate_workspace_change_frame(struct weston_animation *animation,
1009 struct weston_output *output, uint32_t msecs)
1011 struct desktop_shell *shell =
1012 container_of(animation, struct desktop_shell,
1013 workspaces.animation);
1014 struct workspace *from = shell->workspaces.anim_from;
1015 struct workspace *to = shell->workspaces.anim_to;
1019 if (workspace_is_empty(from) && workspace_is_empty(to)) {
1020 finish_workspace_change_animation(shell, from, to);
1024 if (shell->workspaces.anim_timestamp == 0) {
1025 if (shell->workspaces.anim_current == 0.0)
1026 shell->workspaces.anim_timestamp = msecs;
1028 shell->workspaces.anim_timestamp =
1030 /* Invers of movement function 'y' below. */
1031 (asin(1.0 - shell->workspaces.anim_current) *
1032 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
1036 t = msecs - shell->workspaces.anim_timestamp;
1042 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
1045 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
1046 weston_compositor_schedule_repaint(shell->compositor);
1048 workspace_translate_out(from, shell->workspaces.anim_dir * y);
1049 workspace_translate_in(to, shell->workspaces.anim_dir * y);
1050 shell->workspaces.anim_current = y;
1052 weston_compositor_schedule_repaint(shell->compositor);
1055 finish_workspace_change_animation(shell, from, to);
1059 animate_workspace_change(struct desktop_shell *shell,
1061 struct workspace *from,
1062 struct workspace *to)
1064 struct weston_output *output;
1068 if (index > shell->workspaces.current)
1073 shell->workspaces.current = index;
1075 shell->workspaces.anim_dir = dir;
1076 shell->workspaces.anim_from = from;
1077 shell->workspaces.anim_to = to;
1078 shell->workspaces.anim_current = 0.0;
1079 shell->workspaces.anim_timestamp = 0;
1081 output = container_of(shell->compositor->output_list.next,
1082 struct weston_output, link);
1083 wl_list_insert(&output->animation_list,
1084 &shell->workspaces.animation.link);
1086 wl_list_insert(from->layer.link.prev, &to->layer.link);
1088 workspace_translate_in(to, 0);
1090 restore_focus_state(shell, to);
1092 weston_compositor_schedule_repaint(shell->compositor);
1096 update_workspace(struct desktop_shell *shell, unsigned int index,
1097 struct workspace *from, struct workspace *to)
1099 shell->workspaces.current = index;
1100 wl_list_insert(&from->layer.link, &to->layer.link);
1101 wl_list_remove(&from->layer.link);
1105 change_workspace(struct desktop_shell *shell, unsigned int index)
1107 struct workspace *from;
1108 struct workspace *to;
1109 struct focus_state *state;
1111 if (index == shell->workspaces.current)
1114 /* Don't change workspace when there is any fullscreen surfaces. */
1115 if (!wl_list_empty(&shell->fullscreen_layer.view_list))
1118 from = get_current_workspace(shell);
1119 to = get_workspace(shell, index);
1121 if (shell->workspaces.anim_from == to &&
1122 shell->workspaces.anim_to == from) {
1123 restore_focus_state(shell, to);
1124 reverse_workspace_change_animation(shell, index, from, to);
1125 broadcast_current_workspace_state(shell);
1129 if (shell->workspaces.anim_to != NULL)
1130 finish_workspace_change_animation(shell,
1131 shell->workspaces.anim_from,
1132 shell->workspaces.anim_to);
1134 restore_focus_state(shell, to);
1136 if (shell->focus_animation_type != ANIMATION_NONE) {
1137 wl_list_for_each(state, &from->focus_list, link)
1138 if (state->keyboard_focus)
1139 animate_focus_change(shell, from,
1140 get_default_view(state->keyboard_focus), NULL);
1142 wl_list_for_each(state, &to->focus_list, link)
1143 if (state->keyboard_focus)
1144 animate_focus_change(shell, to,
1145 NULL, get_default_view(state->keyboard_focus));
1148 if (workspace_is_empty(to) && workspace_is_empty(from))
1149 update_workspace(shell, index, from, to);
1151 animate_workspace_change(shell, index, from, to);
1153 broadcast_current_workspace_state(shell);
1157 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
1159 struct wl_list *list = &ws->layer.view_list;
1162 if (wl_list_empty(list))
1167 if (e->next != list)
1170 return container_of(e, struct weston_view, layer_link)->surface == surface;
1174 move_surface_to_workspace(struct desktop_shell *shell,
1175 struct shell_surface *shsurf,
1178 struct workspace *from;
1179 struct workspace *to;
1180 struct weston_seat *seat;
1181 struct weston_surface *focus;
1182 struct weston_view *view;
1184 if (workspace == shell->workspaces.current)
1187 view = get_default_view(shsurf->surface);
1191 assert(weston_surface_get_main_surface(view->surface) == view->surface);
1193 if (workspace >= shell->workspaces.num)
1194 workspace = shell->workspaces.num - 1;
1196 from = get_current_workspace(shell);
1197 to = get_workspace(shell, workspace);
1199 wl_list_remove(&view->layer_link);
1200 wl_list_insert(&to->layer.view_list, &view->layer_link);
1202 shell_surface_update_child_surface_layers(shsurf);
1204 drop_focus_state(shell, from, view->surface);
1205 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
1206 if (!seat->keyboard)
1209 focus = weston_surface_get_main_surface(seat->keyboard->focus);
1210 if (focus == view->surface)
1211 weston_keyboard_set_focus(seat->keyboard, NULL);
1214 weston_view_damage_below(view);
1218 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
1219 struct weston_seat *seat,
1222 struct weston_surface *surface;
1223 struct weston_view *view;
1224 struct shell_surface *shsurf;
1225 struct workspace *from;
1226 struct workspace *to;
1227 struct focus_state *state;
1229 surface = weston_surface_get_main_surface(seat->keyboard->focus);
1230 view = get_default_view(surface);
1232 index == shell->workspaces.current ||
1233 is_focus_view(view))
1236 from = get_current_workspace(shell);
1237 to = get_workspace(shell, index);
1239 wl_list_remove(&view->layer_link);
1240 wl_list_insert(&to->layer.view_list, &view->layer_link);
1242 shsurf = get_shell_surface(surface);
1244 shell_surface_update_child_surface_layers(shsurf);
1246 replace_focus_state(shell, to, seat);
1247 drop_focus_state(shell, from, surface);
1249 if (shell->workspaces.anim_from == to &&
1250 shell->workspaces.anim_to == from) {
1251 wl_list_remove(&to->layer.link);
1252 wl_list_insert(from->layer.link.prev, &to->layer.link);
1254 reverse_workspace_change_animation(shell, index, from, to);
1255 broadcast_current_workspace_state(shell);
1260 if (shell->workspaces.anim_to != NULL)
1261 finish_workspace_change_animation(shell,
1262 shell->workspaces.anim_from,
1263 shell->workspaces.anim_to);
1265 if (workspace_is_empty(from) &&
1266 workspace_has_only(to, surface))
1267 update_workspace(shell, index, from, to);
1269 if (shsurf != NULL &&
1270 wl_list_empty(&shsurf->workspace_transform.link))
1271 wl_list_insert(&shell->workspaces.anim_sticky_list,
1272 &shsurf->workspace_transform.link);
1274 animate_workspace_change(shell, index, from, to);
1277 broadcast_current_workspace_state(shell);
1279 state = ensure_focus_state(shell, seat);
1281 focus_state_set_focus(state, surface);
1285 workspace_manager_move_surface(struct wl_client *client,
1286 struct wl_resource *resource,
1287 struct wl_resource *surface_resource,
1290 struct desktop_shell *shell = wl_resource_get_user_data(resource);
1291 struct weston_surface *surface =
1292 wl_resource_get_user_data(surface_resource);
1293 struct weston_surface *main_surface;
1294 struct shell_surface *shell_surface;
1296 main_surface = weston_surface_get_main_surface(surface);
1297 shell_surface = get_shell_surface(main_surface);
1298 if (shell_surface == NULL)
1301 move_surface_to_workspace(shell, shell_surface, workspace);
1304 static const struct workspace_manager_interface workspace_manager_implementation = {
1305 workspace_manager_move_surface,
1309 unbind_resource(struct wl_resource *resource)
1311 wl_list_remove(wl_resource_get_link(resource));
1315 bind_workspace_manager(struct wl_client *client,
1316 void *data, uint32_t version, uint32_t id)
1318 struct desktop_shell *shell = data;
1319 struct wl_resource *resource;
1321 resource = wl_resource_create(client,
1322 &workspace_manager_interface, 1, id);
1324 if (resource == NULL) {
1325 weston_log("couldn't add workspace manager object");
1329 wl_resource_set_implementation(resource,
1330 &workspace_manager_implementation,
1331 shell, unbind_resource);
1332 wl_list_insert(&shell->workspaces.client_list,
1333 wl_resource_get_link(resource));
1335 workspace_manager_send_state(resource,
1336 shell->workspaces.current,
1337 shell->workspaces.num);
1341 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1342 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1347 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1349 struct weston_touch_move_grab *move =
1350 (struct weston_touch_move_grab *) container_of(
1351 grab, struct shell_touch_grab, grab);
1356 if (grab->touch->num_tp == 0) {
1357 shell_touch_grab_end(&move->base);
1363 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1364 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1366 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1367 struct shell_surface *shsurf = move->base.shsurf;
1368 struct weston_surface *es;
1369 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1370 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1372 if (!shsurf || !move->active)
1375 es = shsurf->surface;
1377 weston_view_set_position(shsurf->view, dx, dy);
1379 weston_compositor_schedule_repaint(es->compositor);
1383 touch_move_grab_cancel(struct weston_touch_grab *grab)
1385 struct weston_touch_move_grab *move =
1386 (struct weston_touch_move_grab *) container_of(
1387 grab, struct shell_touch_grab, grab);
1389 shell_touch_grab_end(&move->base);
1393 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1394 touch_move_grab_down,
1396 touch_move_grab_motion,
1397 touch_move_grab_cancel,
1401 surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1403 struct weston_touch_move_grab *move;
1408 if (shsurf->state.fullscreen)
1410 if (shsurf->grabbed)
1413 move = malloc(sizeof *move);
1418 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1419 seat->touch->grab_x;
1420 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1421 seat->touch->grab_y;
1423 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1430 noop_grab_focus(struct weston_pointer_grab *grab)
1435 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1436 wl_fixed_t x, wl_fixed_t y)
1438 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1439 struct weston_pointer *pointer = grab->pointer;
1440 struct shell_surface *shsurf = move->base.shsurf;
1443 weston_pointer_move(pointer, x, y);
1444 dx = wl_fixed_to_int(pointer->x + move->dx);
1445 dy = wl_fixed_to_int(pointer->y + move->dy);
1450 weston_view_set_position(shsurf->view, dx, dy);
1452 weston_compositor_schedule_repaint(shsurf->surface->compositor);
1456 move_grab_button(struct weston_pointer_grab *grab,
1457 uint32_t time, uint32_t button, uint32_t state_w)
1459 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1461 struct weston_pointer *pointer = grab->pointer;
1462 enum wl_pointer_button_state state = state_w;
1464 if (pointer->button_count == 0 &&
1465 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1466 shell_grab_end(shell_grab);
1472 move_grab_cancel(struct weston_pointer_grab *grab)
1474 struct shell_grab *shell_grab =
1475 container_of(grab, struct shell_grab, grab);
1477 shell_grab_end(shell_grab);
1481 static const struct weston_pointer_grab_interface move_grab_interface = {
1489 surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
1491 struct weston_move_grab *move;
1496 if (shsurf->grabbed)
1498 if (shsurf->state.fullscreen || shsurf->state.maximized)
1501 move = malloc(sizeof *move);
1505 move->dx = wl_fixed_from_double(shsurf->view->geometry.x) -
1506 seat->pointer->grab_x;
1507 move->dy = wl_fixed_from_double(shsurf->view->geometry.y) -
1508 seat->pointer->grab_y;
1510 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1511 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
1517 common_surface_move(struct wl_resource *resource,
1518 struct wl_resource *seat_resource, uint32_t serial)
1520 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1521 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1522 struct weston_surface *surface;
1524 if (seat->pointer &&
1525 seat->pointer->focus &&
1526 seat->pointer->button_count > 0 &&
1527 seat->pointer->grab_serial == serial) {
1528 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1529 if ((surface == shsurf->surface) &&
1530 (surface_move(shsurf, seat) < 0))
1531 wl_resource_post_no_memory(resource);
1532 } else if (seat->touch &&
1533 seat->touch->focus &&
1534 seat->touch->grab_serial == serial) {
1535 surface = weston_surface_get_main_surface(seat->touch->focus->surface);
1536 if ((surface == shsurf->surface) &&
1537 (surface_touch_move(shsurf, seat) < 0))
1538 wl_resource_post_no_memory(resource);
1543 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1544 struct wl_resource *seat_resource, uint32_t serial)
1546 common_surface_move(resource, seat_resource, serial);
1549 struct weston_resize_grab {
1550 struct shell_grab base;
1552 int32_t width, height;
1556 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1557 wl_fixed_t x, wl_fixed_t y)
1559 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1560 struct weston_pointer *pointer = grab->pointer;
1561 struct shell_surface *shsurf = resize->base.shsurf;
1562 int32_t width, height;
1563 wl_fixed_t from_x, from_y;
1564 wl_fixed_t to_x, to_y;
1566 weston_pointer_move(pointer, x, y);
1571 weston_view_from_global_fixed(shsurf->view,
1572 pointer->grab_x, pointer->grab_y,
1574 weston_view_from_global_fixed(shsurf->view,
1575 pointer->x, pointer->y, &to_x, &to_y);
1577 width = resize->width;
1578 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1579 width += wl_fixed_to_int(from_x - to_x);
1580 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1581 width += wl_fixed_to_int(to_x - from_x);
1584 height = resize->height;
1585 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1586 height += wl_fixed_to_int(from_y - to_y);
1587 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1588 height += wl_fixed_to_int(to_y - from_y);
1591 shsurf->client->send_configure(shsurf->surface,
1592 resize->edges, width, height);
1596 send_configure(struct weston_surface *surface,
1597 uint32_t edges, int32_t width, int32_t height)
1599 struct shell_surface *shsurf = get_shell_surface(surface);
1603 wl_shell_surface_send_configure(shsurf->resource,
1604 edges, width, height);
1607 static const struct weston_shell_client shell_client = {
1612 resize_grab_button(struct weston_pointer_grab *grab,
1613 uint32_t time, uint32_t button, uint32_t state_w)
1615 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1616 struct weston_pointer *pointer = grab->pointer;
1617 enum wl_pointer_button_state state = state_w;
1619 if (pointer->button_count == 0 &&
1620 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1621 shell_grab_end(&resize->base);
1627 resize_grab_cancel(struct weston_pointer_grab *grab)
1629 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1631 shell_grab_end(&resize->base);
1635 static const struct weston_pointer_grab_interface resize_grab_interface = {
1643 * Returns the bounding box of a surface and all its sub-surfaces,
1644 * in the surface coordinates system. */
1646 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1647 int32_t *y, int32_t *w, int32_t *h) {
1648 pixman_region32_t region;
1649 pixman_box32_t *box;
1650 struct weston_subsurface *subsurface;
1652 pixman_region32_init_rect(®ion, 0, 0,
1656 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1657 pixman_region32_union_rect(®ion, ®ion,
1658 subsurface->position.x,
1659 subsurface->position.y,
1660 subsurface->surface->width,
1661 subsurface->surface->height);
1664 box = pixman_region32_extents(®ion);
1670 *w = box->x2 - box->x1;
1672 *h = box->y2 - box->y1;
1674 pixman_region32_fini(®ion);
1678 surface_resize(struct shell_surface *shsurf,
1679 struct weston_seat *seat, uint32_t edges)
1681 struct weston_resize_grab *resize;
1683 if (shsurf->state.fullscreen || shsurf->state.maximized)
1686 if (edges == 0 || edges > 15 ||
1687 (edges & 3) == 3 || (edges & 12) == 12)
1690 resize = malloc(sizeof *resize);
1694 resize->edges = edges;
1695 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1696 &resize->width, &resize->height);
1698 shsurf->resize_edges = edges;
1699 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1700 seat->pointer, edges);
1706 common_surface_resize(struct wl_resource *resource,
1707 struct wl_resource *seat_resource, uint32_t serial,
1710 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1711 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1712 struct weston_surface *surface;
1714 if (shsurf->state.fullscreen)
1717 if (seat->pointer->button_count == 0 ||
1718 seat->pointer->grab_serial != serial ||
1719 seat->pointer->focus == NULL)
1722 surface = weston_surface_get_main_surface(seat->pointer->focus->surface);
1723 if (surface != shsurf->surface)
1726 if (surface_resize(shsurf, seat, edges) < 0)
1727 wl_resource_post_no_memory(resource);
1731 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1732 struct wl_resource *seat_resource, uint32_t serial,
1735 common_surface_resize(resource, seat_resource, serial, edges);
1739 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1741 struct shell_grab *grab = (struct shell_grab *) base;
1742 struct weston_pointer *pointer = base->pointer;
1743 struct weston_view *view;
1746 view = weston_compositor_pick_view(pointer->seat->compositor,
1747 pointer->x, pointer->y,
1750 if (!grab->shsurf || grab->shsurf->surface != view->surface) {
1751 shell_grab_end(grab);
1757 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
1758 wl_fixed_t x, wl_fixed_t y)
1760 weston_pointer_move(grab->pointer, x, y);
1764 busy_cursor_grab_button(struct weston_pointer_grab *base,
1765 uint32_t time, uint32_t button, uint32_t state)
1767 struct shell_grab *grab = (struct shell_grab *) base;
1768 struct shell_surface *shsurf = grab->shsurf;
1769 struct weston_seat *seat = grab->grab.pointer->seat;
1771 if (shsurf && button == BTN_LEFT && state) {
1772 activate(shsurf->shell, shsurf->surface, seat);
1773 surface_move(shsurf, seat);
1774 } else if (shsurf && button == BTN_RIGHT && state) {
1775 activate(shsurf->shell, shsurf->surface, seat);
1776 surface_rotate(shsurf, seat);
1781 busy_cursor_grab_cancel(struct weston_pointer_grab *base)
1783 struct shell_grab *grab = (struct shell_grab *) base;
1785 shell_grab_end(grab);
1789 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1790 busy_cursor_grab_focus,
1791 busy_cursor_grab_motion,
1792 busy_cursor_grab_button,
1793 busy_cursor_grab_cancel,
1797 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
1799 struct shell_grab *grab;
1801 if (pointer->grab->interface == &busy_cursor_grab_interface)
1804 grab = malloc(sizeof *grab);
1808 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1809 DESKTOP_SHELL_CURSOR_BUSY);
1813 end_busy_cursor(struct weston_compositor *compositor, struct wl_client *client)
1815 struct shell_grab *grab;
1816 struct weston_seat *seat;
1818 wl_list_for_each(seat, &compositor->seat_list, link) {
1819 if (seat->pointer == NULL)
1822 grab = (struct shell_grab *) seat->pointer->grab;
1823 if (grab->grab.interface == &busy_cursor_grab_interface &&
1824 wl_resource_get_client(grab->shsurf->resource) == client) {
1825 shell_grab_end(grab);
1832 handle_shell_client_destroy(struct wl_listener *listener, void *data);
1835 xdg_ping_timeout_handler(void *data)
1837 struct shell_client *sc = data;
1838 struct weston_seat *seat;
1839 struct shell_surface *shsurf;
1841 /* Client is not responding */
1842 sc->unresponsive = 1;
1843 wl_list_for_each(seat, &sc->shell->compositor->seat_list, link) {
1844 if (seat->pointer == NULL || seat->pointer->focus == NULL)
1846 if (seat->pointer->focus->surface->resource == NULL)
1849 shsurf = get_shell_surface(seat->pointer->focus->surface);
1851 wl_resource_get_client(shsurf->resource) == sc->client)
1852 set_busy_cursor(shsurf, seat->pointer);
1859 handle_xdg_ping(struct shell_surface *shsurf, uint32_t serial)
1861 struct weston_compositor *compositor = shsurf->shell->compositor;
1862 struct wl_client *client = wl_resource_get_client(shsurf->resource);
1863 struct shell_client *sc;
1864 struct wl_event_loop *loop;
1865 static const int ping_timeout = 200;
1867 sc = get_shell_client(client);
1868 if (sc->unresponsive) {
1869 xdg_ping_timeout_handler(sc);
1873 sc->ping_serial = serial;
1874 loop = wl_display_get_event_loop(compositor->wl_display);
1875 if (sc->ping_timer == NULL)
1877 wl_event_loop_add_timer(loop,
1878 xdg_ping_timeout_handler, sc);
1879 if (sc->ping_timer == NULL)
1882 wl_event_source_timer_update(sc->ping_timer, ping_timeout);
1884 if (shell_surface_is_xdg_surface(shsurf) ||
1885 shell_surface_is_xdg_popup(shsurf))
1886 xdg_shell_send_ping(sc->resource, serial);
1887 else if (shell_surface_is_wl_shell_surface(shsurf))
1888 wl_shell_surface_send_ping(shsurf->resource, serial);
1892 ping_handler(struct weston_surface *surface, uint32_t serial)
1894 struct shell_surface *shsurf = get_shell_surface(surface);
1898 if (!shsurf->resource)
1900 if (shsurf->surface == shsurf->shell->grab_surface)
1903 handle_xdg_ping(shsurf, serial);
1907 handle_pointer_focus(struct wl_listener *listener, void *data)
1909 struct weston_pointer *pointer = data;
1910 struct weston_view *view = pointer->focus;
1911 struct weston_compositor *compositor;
1917 compositor = view->surface->compositor;
1918 serial = wl_display_next_serial(compositor->wl_display);
1919 ping_handler(view->surface, serial);
1923 create_pointer_focus_listener(struct weston_seat *seat)
1925 struct wl_listener *listener;
1930 listener = malloc(sizeof *listener);
1931 listener->notify = handle_pointer_focus;
1932 wl_signal_add(&seat->pointer->focus_signal, listener);
1936 shell_surface_lose_keyboard_focus(struct shell_surface *shsurf)
1938 if (--shsurf->focus_count == 0)
1939 if (shell_surface_is_xdg_surface(shsurf))
1940 xdg_surface_send_deactivated(shsurf->resource);
1944 shell_surface_gain_keyboard_focus(struct shell_surface *shsurf)
1946 if (shsurf->focus_count++ == 0)
1947 if (shell_surface_is_xdg_surface(shsurf))
1948 xdg_surface_send_activated(shsurf->resource);
1952 handle_keyboard_focus(struct wl_listener *listener, void *data)
1954 struct weston_keyboard *keyboard = data;
1955 struct shell_seat *seat = get_shell_seat(keyboard->seat);
1957 if (seat->focused_surface) {
1958 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1960 shell_surface_lose_keyboard_focus(shsurf);
1963 seat->focused_surface = keyboard->focus;
1965 if (seat->focused_surface) {
1966 struct shell_surface *shsurf = get_shell_surface(seat->focused_surface);
1968 shell_surface_gain_keyboard_focus(shsurf);
1973 create_keyboard_focus_listener(struct weston_seat *seat)
1975 struct wl_listener *listener;
1977 if (!seat->keyboard)
1980 listener = malloc(sizeof *listener);
1981 listener->notify = handle_keyboard_focus;
1982 wl_signal_add(&seat->keyboard->focus_signal, listener);
1985 static struct shell_client *
1986 get_shell_client(struct wl_client *client)
1988 struct wl_listener *listener;
1990 listener = wl_client_get_destroy_listener(client,
1991 handle_shell_client_destroy);
1992 if (listener == NULL)
1995 return container_of(listener, struct shell_client, destroy_listener);
1999 shell_client_pong(struct shell_client *sc, uint32_t serial)
2001 if (sc->ping_serial != serial)
2004 sc->unresponsive = 0;
2005 end_busy_cursor(sc->shell->compositor, sc->client);
2007 if (sc->ping_timer) {
2008 wl_event_source_remove(sc->ping_timer);
2009 sc->ping_timer = NULL;
2015 shell_surface_pong(struct wl_client *client,
2016 struct wl_resource *resource, uint32_t serial)
2018 struct shell_client *sc;
2020 sc = get_shell_client(client);
2021 shell_client_pong(sc, serial);
2025 set_title(struct shell_surface *shsurf, const char *title)
2027 free(shsurf->title);
2028 shsurf->title = strdup(title);
2032 shell_surface_set_title(struct wl_client *client,
2033 struct wl_resource *resource, const char *title)
2035 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2037 set_title(shsurf, title);
2041 shell_surface_set_class(struct wl_client *client,
2042 struct wl_resource *resource, const char *class)
2044 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2046 free(shsurf->class);
2047 shsurf->class = strdup(class);
2051 restore_output_mode(struct weston_output *output)
2053 if (output->current_mode != output->original_mode ||
2054 (int32_t)output->current_scale != output->original_scale)
2055 weston_output_switch_mode(output,
2056 output->original_mode,
2057 output->original_scale,
2058 WESTON_MODE_SWITCH_RESTORE_NATIVE);
2062 restore_all_output_modes(struct weston_compositor *compositor)
2064 struct weston_output *output;
2066 wl_list_for_each(output, &compositor->output_list, link)
2067 restore_output_mode(output);
2071 get_output_panel_height(struct desktop_shell *shell,
2072 struct weston_output *output)
2074 struct weston_view *view;
2075 int panel_height = 0;
2080 wl_list_for_each(view, &shell->panel_layer.view_list, layer_link) {
2081 if (view->surface->output == output) {
2082 panel_height = view->surface->height;
2087 return panel_height;
2090 /* The surface will be inserted into the list immediately after the link
2091 * returned by this function (i.e. will be stacked immediately above the
2092 * returned link). */
2093 static struct wl_list *
2094 shell_surface_calculate_layer_link (struct shell_surface *shsurf)
2096 struct workspace *ws;
2097 struct weston_view *parent;
2099 switch (shsurf->type) {
2100 case SHELL_SURFACE_POPUP:
2101 case SHELL_SURFACE_TOPLEVEL:
2102 if (shsurf->state.fullscreen) {
2103 return &shsurf->shell->fullscreen_layer.view_list;
2104 } else if (shsurf->parent) {
2105 /* Move the surface to its parent layer so
2106 * that surfaces which are transient for
2107 * fullscreen surfaces don't get hidden by the
2108 * fullscreen surfaces. */
2110 /* TODO: Handle a parent with multiple views */
2111 parent = get_default_view(shsurf->parent);
2113 return parent->layer_link.prev;
2117 case SHELL_SURFACE_XWAYLAND:
2118 return &shsurf->shell->fullscreen_layer.view_list;
2120 case SHELL_SURFACE_NONE:
2122 /* Go to the fallback, below. */
2126 /* Move the surface to a normal workspace layer so that surfaces
2127 * which were previously fullscreen or transient are no longer
2128 * rendered on top. */
2129 ws = get_current_workspace(shsurf->shell);
2130 return &ws->layer.view_list;
2134 shell_surface_update_child_surface_layers (struct shell_surface *shsurf)
2136 struct shell_surface *child;
2138 /* Move the child layers to the same workspace as shsurf. They will be
2139 * stacked above shsurf. */
2140 wl_list_for_each_reverse(child, &shsurf->children_list, children_link) {
2141 if (shsurf->view->layer_link.prev != &child->view->layer_link) {
2142 weston_view_geometry_dirty(child->view);
2143 wl_list_remove(&child->view->layer_link);
2144 wl_list_insert(shsurf->view->layer_link.prev,
2145 &child->view->layer_link);
2146 weston_view_geometry_dirty(child->view);
2147 weston_surface_damage(child->surface);
2149 /* Recurse. We don’t expect this to recurse very far (if
2150 * at all) because that would imply we have transient
2151 * (or popup) children of transient surfaces, which
2152 * would be unusual. */
2153 shell_surface_update_child_surface_layers(child);
2158 /* Update the surface’s layer. Mark both the old and new views as having dirty
2159 * geometry to ensure the changes are redrawn.
2161 * If any child surfaces exist and are mapped, ensure they’re in the same layer
2162 * as this surface. */
2164 shell_surface_update_layer(struct shell_surface *shsurf)
2166 struct wl_list *new_layer_link;
2168 new_layer_link = shell_surface_calculate_layer_link(shsurf);
2170 if (new_layer_link == &shsurf->view->layer_link)
2173 weston_view_geometry_dirty(shsurf->view);
2174 wl_list_remove(&shsurf->view->layer_link);
2175 wl_list_insert(new_layer_link, &shsurf->view->layer_link);
2176 weston_view_geometry_dirty(shsurf->view);
2177 weston_surface_damage(shsurf->surface);
2179 shell_surface_update_child_surface_layers(shsurf);
2183 shell_surface_set_parent(struct shell_surface *shsurf,
2184 struct weston_surface *parent)
2186 shsurf->parent = parent;
2188 wl_list_remove(&shsurf->children_link);
2189 wl_list_init(&shsurf->children_link);
2191 /* Insert into the parent surface’s child list. */
2192 if (parent != NULL) {
2193 struct shell_surface *parent_shsurf = get_shell_surface(parent);
2194 if (parent_shsurf != NULL)
2195 wl_list_insert(&parent_shsurf->children_list,
2196 &shsurf->children_link);
2201 shell_surface_set_output(struct shell_surface *shsurf,
2202 struct weston_output *output)
2204 struct weston_surface *es = shsurf->surface;
2206 /* get the default output, if the client set it as NULL
2207 check whether the ouput is available */
2209 shsurf->output = output;
2210 else if (es->output)
2211 shsurf->output = es->output;
2213 shsurf->output = get_default_output(es->compositor);
2217 surface_clear_next_states(struct shell_surface *shsurf)
2219 shsurf->next_state.maximized = false;
2220 shsurf->next_state.fullscreen = false;
2222 if ((shsurf->next_state.maximized != shsurf->state.maximized) ||
2223 (shsurf->next_state.fullscreen != shsurf->state.fullscreen))
2224 shsurf->state_changed = true;
2228 set_toplevel(struct shell_surface *shsurf)
2230 shell_surface_set_parent(shsurf, NULL);
2231 surface_clear_next_states(shsurf);
2232 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2234 /* The layer_link is updated in set_surface_type(),
2235 * called from configure. */
2239 shell_surface_set_toplevel(struct wl_client *client,
2240 struct wl_resource *resource)
2242 struct shell_surface *surface = wl_resource_get_user_data(resource);
2244 set_toplevel(surface);
2248 set_transient(struct shell_surface *shsurf,
2249 struct weston_surface *parent, int x, int y, uint32_t flags)
2251 assert(parent != NULL);
2253 shell_surface_set_parent(shsurf, parent);
2255 surface_clear_next_states(shsurf);
2257 shsurf->transient.x = x;
2258 shsurf->transient.y = y;
2259 shsurf->transient.flags = flags;
2261 shsurf->next_state.relative = true;
2262 shsurf->state_changed = true;
2263 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2265 /* The layer_link is updated in set_surface_type(),
2266 * called from configure. */
2270 shell_surface_set_transient(struct wl_client *client,
2271 struct wl_resource *resource,
2272 struct wl_resource *parent_resource,
2273 int x, int y, uint32_t flags)
2275 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2276 struct weston_surface *parent =
2277 wl_resource_get_user_data(parent_resource);
2279 set_transient(shsurf, parent, x, y, flags);
2283 set_fullscreen(struct shell_surface *shsurf,
2286 struct weston_output *output)
2288 shell_surface_set_output(shsurf, output);
2290 shsurf->fullscreen_output = shsurf->output;
2291 shsurf->fullscreen.type = method;
2292 shsurf->fullscreen.framerate = framerate;
2294 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2296 shsurf->client->send_configure(shsurf->surface, 0,
2297 shsurf->output->width,
2298 shsurf->output->height);
2300 /* The layer_link is updated in set_surface_type(),
2301 * called from configure. */
2305 weston_view_set_initial_position(struct weston_view *view,
2306 struct desktop_shell *shell);
2309 unset_fullscreen(struct shell_surface *shsurf)
2311 /* Unset the fullscreen output, driver configuration and transforms. */
2312 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2313 shell_surface_is_top_fullscreen(shsurf)) {
2314 restore_output_mode(shsurf->fullscreen_output);
2316 shsurf->fullscreen_output = NULL;
2318 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2319 shsurf->fullscreen.framerate = 0;
2321 wl_list_remove(&shsurf->fullscreen.transform.link);
2322 wl_list_init(&shsurf->fullscreen.transform.link);
2324 if (shsurf->fullscreen.black_view)
2325 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
2326 shsurf->fullscreen.black_view = NULL;
2328 if (shsurf->saved_position_valid)
2329 weston_view_set_position(shsurf->view,
2330 shsurf->saved_x, shsurf->saved_y);
2332 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2334 if (shsurf->saved_rotation_valid) {
2335 wl_list_insert(&shsurf->view->geometry.transformation_list,
2336 &shsurf->rotation.transform.link);
2337 shsurf->saved_rotation_valid = false;
2340 /* Layer is updated in set_surface_type(). */
2344 shell_surface_set_fullscreen(struct wl_client *client,
2345 struct wl_resource *resource,
2348 struct wl_resource *output_resource)
2350 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2351 struct weston_output *output;
2353 if (output_resource)
2354 output = wl_resource_get_user_data(output_resource);
2358 shell_surface_set_parent(shsurf, NULL);
2360 surface_clear_next_states(shsurf);
2361 set_fullscreen(shsurf, method, framerate, output);
2363 shsurf->next_state.fullscreen = true;
2364 shsurf->state_changed = true;
2368 set_popup(struct shell_surface *shsurf,
2369 struct weston_surface *parent,
2370 struct weston_seat *seat,
2375 assert(parent != NULL);
2377 shsurf->popup.shseat = get_shell_seat(seat);
2378 shsurf->popup.serial = serial;
2379 shsurf->popup.x = x;
2380 shsurf->popup.y = y;
2382 shsurf->type = SHELL_SURFACE_POPUP;
2386 shell_surface_set_popup(struct wl_client *client,
2387 struct wl_resource *resource,
2388 struct wl_resource *seat_resource,
2390 struct wl_resource *parent_resource,
2391 int32_t x, int32_t y, uint32_t flags)
2393 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2394 struct weston_surface *parent =
2395 wl_resource_get_user_data(parent_resource);
2397 shell_surface_set_parent(shsurf, parent);
2399 surface_clear_next_states(shsurf);
2402 wl_resource_get_user_data(seat_resource),
2407 set_maximized(struct shell_surface *shsurf,
2408 struct weston_output *output)
2410 struct desktop_shell *shell;
2411 uint32_t edges = 0, panel_height = 0;
2413 shell_surface_set_output(shsurf, output);
2415 shell = shell_surface_get_shell(shsurf);
2416 panel_height = get_output_panel_height(shell, shsurf->output);
2417 edges = WL_SHELL_SURFACE_RESIZE_TOP | WL_SHELL_SURFACE_RESIZE_LEFT;
2419 shsurf->client->send_configure(shsurf->surface, edges,
2420 shsurf->output->width,
2421 shsurf->output->height - panel_height);
2423 shsurf->type = SHELL_SURFACE_TOPLEVEL;
2427 unset_maximized(struct shell_surface *shsurf)
2429 /* undo all maximized things here */
2430 shsurf->output = get_default_output(shsurf->surface->compositor);
2432 if (shsurf->saved_position_valid)
2433 weston_view_set_position(shsurf->view,
2434 shsurf->saved_x, shsurf->saved_y);
2436 weston_view_set_initial_position(shsurf->view, shsurf->shell);
2438 if (shsurf->saved_rotation_valid) {
2439 wl_list_insert(&shsurf->view->geometry.transformation_list,
2440 &shsurf->rotation.transform.link);
2441 shsurf->saved_rotation_valid = false;
2444 /* Layer is updated in set_surface_type(). */
2448 set_minimized(struct weston_surface *surface, uint32_t is_true)
2450 struct shell_surface *shsurf;
2451 struct workspace *current_ws;
2452 struct weston_seat *seat;
2453 struct weston_surface *focus;
2454 struct weston_view *view;
2456 view = get_default_view(surface);
2460 assert(weston_surface_get_main_surface(view->surface) == view->surface);
2462 shsurf = get_shell_surface(surface);
2463 current_ws = get_current_workspace(shsurf->shell);
2465 wl_list_remove(&view->layer_link);
2466 /* hide or show, depending on the state */
2468 wl_list_insert(&shsurf->shell->minimized_layer.view_list, &view->layer_link);
2470 drop_focus_state(shsurf->shell, current_ws, view->surface);
2471 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2472 if (!seat->keyboard)
2474 focus = weston_surface_get_main_surface(seat->keyboard->focus);
2475 if (focus == view->surface)
2476 weston_keyboard_set_focus(seat->keyboard, NULL);
2480 wl_list_insert(¤t_ws->layer.view_list, &view->layer_link);
2482 wl_list_for_each(seat, &shsurf->shell->compositor->seat_list, link) {
2483 if (!seat->keyboard)
2485 activate(shsurf->shell, view->surface, seat);
2489 shell_surface_update_child_surface_layers(shsurf);
2491 weston_view_damage_below(view);
2495 shell_surface_set_maximized(struct wl_client *client,
2496 struct wl_resource *resource,
2497 struct wl_resource *output_resource)
2499 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2500 struct weston_output *output;
2502 if (output_resource)
2503 output = wl_resource_get_user_data(output_resource);
2507 shell_surface_set_parent(shsurf, NULL);
2509 surface_clear_next_states(shsurf);
2510 set_maximized(shsurf, output);
2512 shsurf->next_state.maximized = true;
2513 shsurf->state_changed = true;
2516 /* This is only ever called from set_surface_type(), so there’s no need to
2517 * update layer_links here, since they’ll be updated when we return. */
2519 reset_surface_type(struct shell_surface *surface)
2521 if (surface->state.fullscreen)
2522 unset_fullscreen(surface);
2523 if (surface->state.maximized)
2524 unset_maximized(surface);
2530 set_full_output(struct shell_surface *shsurf)
2532 shsurf->saved_x = shsurf->view->geometry.x;
2533 shsurf->saved_y = shsurf->view->geometry.y;
2534 shsurf->saved_width = shsurf->surface->width;
2535 shsurf->saved_height = shsurf->surface->height;
2536 shsurf->saved_size_valid = true;
2537 shsurf->saved_position_valid = true;
2539 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
2540 wl_list_remove(&shsurf->rotation.transform.link);
2541 wl_list_init(&shsurf->rotation.transform.link);
2542 weston_view_geometry_dirty(shsurf->view);
2543 shsurf->saved_rotation_valid = true;
2548 set_surface_type(struct shell_surface *shsurf)
2550 struct weston_surface *pes = shsurf->parent;
2551 struct weston_view *pev = get_default_view(pes);
2553 reset_surface_type(shsurf);
2555 shsurf->state = shsurf->next_state;
2556 shsurf->state_changed = false;
2558 switch (shsurf->type) {
2559 case SHELL_SURFACE_TOPLEVEL:
2560 if (shsurf->state.maximized || shsurf->state.fullscreen) {
2561 set_full_output(shsurf);
2562 } else if (shsurf->state.relative && pev) {
2563 weston_view_set_position(shsurf->view,
2564 pev->geometry.x + shsurf->transient.x,
2565 pev->geometry.y + shsurf->transient.y);
2569 case SHELL_SURFACE_XWAYLAND:
2570 weston_view_set_position(shsurf->view, shsurf->transient.x,
2571 shsurf->transient.y);
2574 case SHELL_SURFACE_POPUP:
2575 case SHELL_SURFACE_NONE:
2580 /* Update the surface’s layer. */
2581 shell_surface_update_layer(shsurf);
2584 static struct desktop_shell *
2585 shell_surface_get_shell(struct shell_surface *shsurf)
2587 return shsurf->shell;
2591 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
2593 static struct weston_view *
2594 create_black_surface(struct weston_compositor *ec,
2595 struct weston_surface *fs_surface,
2596 float x, float y, int w, int h)
2598 struct weston_surface *surface = NULL;
2599 struct weston_view *view;
2601 surface = weston_surface_create(ec);
2602 if (surface == NULL) {
2603 weston_log("no memory\n");
2606 view = weston_view_create(surface);
2607 if (surface == NULL) {
2608 weston_log("no memory\n");
2609 weston_surface_destroy(surface);
2613 surface->configure = black_surface_configure;
2614 surface->configure_private = fs_surface;
2615 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
2616 pixman_region32_fini(&surface->opaque);
2617 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
2618 pixman_region32_fini(&surface->input);
2619 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
2621 weston_surface_set_size(surface, w, h);
2622 weston_view_set_position(view, x, y);
2628 shell_ensure_fullscreen_black_view(struct shell_surface *shsurf)
2630 struct weston_output *output = shsurf->fullscreen_output;
2632 assert(shsurf->state.fullscreen);
2634 if (!shsurf->fullscreen.black_view)
2635 shsurf->fullscreen.black_view =
2636 create_black_surface(shsurf->surface->compositor,
2638 output->x, output->y,
2642 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2643 wl_list_remove(&shsurf->fullscreen.black_view->layer_link);
2644 wl_list_insert(&shsurf->view->layer_link,
2645 &shsurf->fullscreen.black_view->layer_link);
2646 weston_view_geometry_dirty(shsurf->fullscreen.black_view);
2647 weston_surface_damage(shsurf->surface);
2650 /* Create black surface and append it to the associated fullscreen surface.
2651 * Handle size dismatch and positioning according to the method. */
2653 shell_configure_fullscreen(struct shell_surface *shsurf)
2655 struct weston_output *output = shsurf->fullscreen_output;
2656 struct weston_surface *surface = shsurf->surface;
2657 struct weston_matrix *matrix;
2658 float scale, output_aspect, surface_aspect, x, y;
2659 int32_t surf_x, surf_y, surf_width, surf_height;
2661 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
2662 restore_output_mode(output);
2664 shell_ensure_fullscreen_black_view(shsurf);
2666 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
2667 &surf_width, &surf_height);
2669 switch (shsurf->fullscreen.type) {
2670 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
2671 if (surface->buffer_ref.buffer)
2672 center_on_output(shsurf->view, shsurf->fullscreen_output);
2674 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
2675 /* 1:1 mapping between surface and output dimensions */
2676 if (output->width == surf_width &&
2677 output->height == surf_height) {
2678 weston_view_set_position(shsurf->view,
2680 output->y - surf_y);
2684 matrix = &shsurf->fullscreen.transform.matrix;
2685 weston_matrix_init(matrix);
2687 output_aspect = (float) output->width /
2688 (float) output->height;
2689 /* XXX: Use surf_width and surf_height here? */
2690 surface_aspect = (float) surface->width /
2691 (float) surface->height;
2692 if (output_aspect < surface_aspect)
2693 scale = (float) output->width /
2696 scale = (float) output->height /
2697 (float) surf_height;
2699 weston_matrix_scale(matrix, scale, scale, 1);
2700 wl_list_remove(&shsurf->fullscreen.transform.link);
2701 wl_list_insert(&shsurf->view->geometry.transformation_list,
2702 &shsurf->fullscreen.transform.link);
2703 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
2704 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
2705 weston_view_set_position(shsurf->view, x, y);
2708 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
2709 if (shell_surface_is_top_fullscreen(shsurf)) {
2710 struct weston_mode mode = {0,
2711 surf_width * surface->buffer_viewport.buffer.scale,
2712 surf_height * surface->buffer_viewport.buffer.scale,
2713 shsurf->fullscreen.framerate};
2715 if (weston_output_switch_mode(output, &mode, surface->buffer_viewport.buffer.scale,
2716 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
2717 weston_view_set_position(shsurf->view,
2719 output->y - surf_y);
2720 shsurf->fullscreen.black_view->surface->width = output->width;
2721 shsurf->fullscreen.black_view->surface->height = output->height;
2722 weston_view_set_position(shsurf->fullscreen.black_view,
2724 output->y - surf_y);
2727 restore_output_mode(output);
2728 center_on_output(shsurf->view, output);
2732 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
2733 center_on_output(shsurf->view, output);
2741 shell_map_fullscreen(struct shell_surface *shsurf)
2743 shell_configure_fullscreen(shsurf);
2747 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2749 /* XXX: using the same fields for transient type */
2750 surface_clear_next_states(shsurf);
2751 shsurf->transient.x = x;
2752 shsurf->transient.y = y;
2753 shsurf->transient.flags = flags;
2755 shell_surface_set_parent(shsurf, NULL);
2757 shsurf->type = SHELL_SURFACE_XWAYLAND;
2758 shsurf->state_changed = true;
2761 static const struct weston_pointer_grab_interface popup_grab_interface;
2764 destroy_shell_seat(struct wl_listener *listener, void *data)
2766 struct shell_seat *shseat =
2767 container_of(listener,
2768 struct shell_seat, seat_destroy_listener);
2769 struct shell_surface *shsurf, *prev = NULL;
2771 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
2772 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2773 shseat->popup_grab.client = NULL;
2775 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2776 shsurf->popup.shseat = NULL;
2778 wl_list_init(&prev->popup.grab_link);
2782 wl_list_init(&prev->popup.grab_link);
2785 wl_list_remove(&shseat->seat_destroy_listener.link);
2789 static struct shell_seat *
2790 create_shell_seat(struct weston_seat *seat)
2792 struct shell_seat *shseat;
2794 shseat = calloc(1, sizeof *shseat);
2796 weston_log("no memory to allocate shell seat\n");
2800 shseat->seat = seat;
2801 wl_list_init(&shseat->popup_grab.surfaces_list);
2803 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2804 wl_signal_add(&seat->destroy_signal,
2805 &shseat->seat_destroy_listener);
2810 static struct shell_seat *
2811 get_shell_seat(struct weston_seat *seat)
2813 struct wl_listener *listener;
2815 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2816 if (listener == NULL)
2817 return create_shell_seat(seat);
2819 return container_of(listener,
2820 struct shell_seat, seat_destroy_listener);
2824 popup_grab_focus(struct weston_pointer_grab *grab)
2826 struct weston_pointer *pointer = grab->pointer;
2827 struct weston_view *view;
2828 struct shell_seat *shseat =
2829 container_of(grab, struct shell_seat, popup_grab.grab);
2830 struct wl_client *client = shseat->popup_grab.client;
2833 view = weston_compositor_pick_view(pointer->seat->compositor,
2834 pointer->x, pointer->y,
2837 if (view && view->surface->resource &&
2838 wl_resource_get_client(view->surface->resource) == client) {
2839 weston_pointer_set_focus(pointer, view, sx, sy);
2841 weston_pointer_set_focus(pointer, NULL,
2842 wl_fixed_from_int(0),
2843 wl_fixed_from_int(0));
2848 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
2849 wl_fixed_t x, wl_fixed_t y)
2851 struct weston_pointer *pointer = grab->pointer;
2852 struct wl_resource *resource;
2855 weston_pointer_move(pointer, x, y);
2857 wl_resource_for_each(resource, &pointer->focus_resource_list) {
2858 weston_view_from_global_fixed(pointer->focus,
2859 pointer->x, pointer->y,
2861 wl_pointer_send_motion(resource, time, sx, sy);
2866 popup_grab_button(struct weston_pointer_grab *grab,
2867 uint32_t time, uint32_t button, uint32_t state_w)
2869 struct wl_resource *resource;
2870 struct shell_seat *shseat =
2871 container_of(grab, struct shell_seat, popup_grab.grab);
2872 struct wl_display *display = shseat->seat->compositor->wl_display;
2873 enum wl_pointer_button_state state = state_w;
2875 struct wl_list *resource_list;
2877 resource_list = &grab->pointer->focus_resource_list;
2878 if (!wl_list_empty(resource_list)) {
2879 serial = wl_display_get_serial(display);
2880 wl_resource_for_each(resource, resource_list) {
2881 wl_pointer_send_button(resource, serial,
2882 time, button, state);
2884 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
2885 (shseat->popup_grab.initial_up ||
2886 time - shseat->seat->pointer->grab_time > 500)) {
2887 popup_grab_end(grab->pointer);
2890 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
2891 shseat->popup_grab.initial_up = 1;
2895 popup_grab_cancel(struct weston_pointer_grab *grab)
2897 popup_grab_end(grab->pointer);
2900 static const struct weston_pointer_grab_interface popup_grab_interface = {
2908 shell_surface_send_popup_done(struct shell_surface *shsurf)
2910 if (shell_surface_is_wl_shell_surface(shsurf))
2911 wl_shell_surface_send_popup_done(shsurf->resource);
2912 else if (shell_surface_is_xdg_popup(shsurf))
2913 xdg_popup_send_popup_done(shsurf->resource,
2914 shsurf->popup.serial);
2918 popup_grab_end(struct weston_pointer *pointer)
2920 struct weston_pointer_grab *grab = pointer->grab;
2921 struct shell_seat *shseat =
2922 container_of(grab, struct shell_seat, popup_grab.grab);
2923 struct shell_surface *shsurf;
2924 struct shell_surface *prev = NULL;
2926 if (pointer->grab->interface == &popup_grab_interface) {
2927 weston_pointer_end_grab(grab->pointer);
2928 shseat->popup_grab.client = NULL;
2929 shseat->popup_grab.grab.interface = NULL;
2930 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
2931 /* Send the popup_done event to all the popups open */
2932 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2933 shell_surface_send_popup_done(shsurf);
2934 shsurf->popup.shseat = NULL;
2936 wl_list_init(&prev->popup.grab_link);
2940 wl_list_init(&prev->popup.grab_link);
2941 wl_list_init(&shseat->popup_grab.surfaces_list);
2946 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2948 struct weston_seat *seat = shseat->seat;
2950 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2951 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
2952 shseat->popup_grab.grab.interface = &popup_grab_interface;
2953 /* We must make sure here that this popup was opened after
2954 * a mouse press, and not just by moving around with other
2955 * popups already open. */
2956 if (shseat->seat->pointer->button_count > 0)
2957 shseat->popup_grab.initial_up = 0;
2959 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2960 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2962 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2967 remove_popup_grab(struct shell_surface *shsurf)
2969 struct shell_seat *shseat = shsurf->popup.shseat;
2971 wl_list_remove(&shsurf->popup.grab_link);
2972 wl_list_init(&shsurf->popup.grab_link);
2973 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2974 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2975 shseat->popup_grab.grab.interface = NULL;
2980 shell_map_popup(struct shell_surface *shsurf)
2982 struct shell_seat *shseat = shsurf->popup.shseat;
2983 struct weston_view *parent_view = get_default_view(shsurf->parent);
2985 shsurf->surface->output = parent_view->output;
2986 shsurf->view->output = parent_view->output;
2988 weston_view_set_transform_parent(shsurf->view, parent_view);
2989 weston_view_set_position(shsurf->view, shsurf->popup.x, shsurf->popup.y);
2990 weston_view_update_transform(shsurf->view);
2992 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
2993 add_popup_grab(shsurf, shseat);
2995 shell_surface_send_popup_done(shsurf);
2996 shseat->popup_grab.client = NULL;
3000 static const struct wl_shell_surface_interface shell_surface_implementation = {
3003 shell_surface_resize,
3004 shell_surface_set_toplevel,
3005 shell_surface_set_transient,
3006 shell_surface_set_fullscreen,
3007 shell_surface_set_popup,
3008 shell_surface_set_maximized,
3009 shell_surface_set_title,
3010 shell_surface_set_class
3014 destroy_shell_surface(struct shell_surface *shsurf)
3016 struct shell_surface *child, *next;
3018 wl_signal_emit(&shsurf->destroy_signal, shsurf);
3020 if (!wl_list_empty(&shsurf->popup.grab_link)) {
3021 remove_popup_grab(shsurf);
3024 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
3025 shell_surface_is_top_fullscreen(shsurf))
3026 restore_output_mode (shsurf->fullscreen_output);
3028 if (shsurf->fullscreen.black_view)
3029 weston_surface_destroy(shsurf->fullscreen.black_view->surface);
3031 /* As destroy_resource() use wl_list_for_each_safe(),
3032 * we can always remove the listener.
3034 wl_list_remove(&shsurf->surface_destroy_listener.link);
3035 shsurf->surface->configure = NULL;
3036 free(shsurf->title);
3038 weston_view_destroy(shsurf->view);
3040 wl_list_remove(&shsurf->children_link);
3041 wl_list_for_each_safe(child, next, &shsurf->children_list, children_link)
3042 shell_surface_set_parent(child, NULL);
3044 wl_list_remove(&shsurf->link);
3049 shell_destroy_shell_surface(struct wl_resource *resource)
3051 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3053 shsurf->resource = NULL;
3057 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
3059 struct shell_surface *shsurf = container_of(listener,
3060 struct shell_surface,
3061 surface_destroy_listener);
3063 if (shsurf->resource)
3064 wl_resource_destroy(shsurf->resource);
3066 destroy_shell_surface(shsurf);
3070 fade_out_done(struct weston_view_animation *animation, void *data)
3072 struct shell_surface *shsurf = data;
3074 weston_surface_destroy(shsurf->surface);
3078 handle_resource_destroy(struct wl_listener *listener, void *data)
3080 struct shell_surface *shsurf =
3081 container_of(listener, struct shell_surface,
3082 resource_destroy_listener);
3084 shsurf->surface->ref_count++;
3086 pixman_region32_fini(&shsurf->surface->pending.input);
3087 pixman_region32_init(&shsurf->surface->pending.input);
3088 pixman_region32_fini(&shsurf->surface->input);
3089 pixman_region32_init(&shsurf->surface->input);
3090 if (weston_surface_is_mapped(shsurf->surface))
3091 weston_fade_run(shsurf->view, 1.0, 0.0, 300.0,
3092 fade_out_done, shsurf);
3096 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
3098 struct shell_surface *
3099 get_shell_surface(struct weston_surface *surface)
3101 if (surface->configure == shell_surface_configure)
3102 return surface->configure_private;
3107 static struct shell_surface *
3108 create_common_surface(void *shell, struct weston_surface *surface,
3109 const struct weston_shell_client *client)
3111 struct shell_surface *shsurf;
3113 if (surface->configure) {
3114 weston_log("surface->configure already set\n");
3118 shsurf = calloc(1, sizeof *shsurf);
3120 weston_log("no memory to allocate shell surface\n");
3124 shsurf->view = weston_view_create(surface);
3125 if (!shsurf->view) {
3126 weston_log("no memory to allocate shell surface\n");
3131 surface->configure = shell_surface_configure;
3132 surface->configure_private = shsurf;
3134 shsurf->resource_destroy_listener.notify = handle_resource_destroy;
3135 wl_resource_add_destroy_listener(surface->resource,
3136 &shsurf->resource_destroy_listener);
3138 shsurf->shell = (struct desktop_shell *) shell;
3139 shsurf->unresponsive = 0;
3140 shsurf->saved_position_valid = false;
3141 shsurf->saved_size_valid = false;
3142 shsurf->saved_rotation_valid = false;
3143 shsurf->surface = surface;
3144 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3145 shsurf->fullscreen.framerate = 0;
3146 shsurf->fullscreen.black_view = NULL;
3147 wl_list_init(&shsurf->fullscreen.transform.link);
3149 wl_signal_init(&shsurf->destroy_signal);
3150 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
3151 wl_signal_add(&surface->destroy_signal,
3152 &shsurf->surface_destroy_listener);
3154 /* init link so its safe to always remove it in destroy_shell_surface */
3155 wl_list_init(&shsurf->link);
3156 wl_list_init(&shsurf->popup.grab_link);
3158 /* empty when not in use */
3159 wl_list_init(&shsurf->rotation.transform.link);
3160 weston_matrix_init(&shsurf->rotation.rotation);
3162 wl_list_init(&shsurf->workspace_transform.link);
3164 wl_list_init(&shsurf->children_link);
3165 wl_list_init(&shsurf->children_list);
3166 shsurf->parent = NULL;
3168 shsurf->type = SHELL_SURFACE_NONE;
3170 shsurf->client = client;
3175 static struct shell_surface *
3176 create_shell_surface(void *shell, struct weston_surface *surface,
3177 const struct weston_shell_client *client)
3179 return create_common_surface(shell, surface, client);
3182 static struct weston_view *
3183 get_primary_view(void *shell, struct shell_surface *shsurf)
3185 return shsurf->view;
3189 shell_get_shell_surface(struct wl_client *client,
3190 struct wl_resource *resource,
3192 struct wl_resource *surface_resource)
3194 struct weston_surface *surface =
3195 wl_resource_get_user_data(surface_resource);
3196 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3197 struct shell_surface *shsurf;
3199 if (get_shell_surface(surface)) {
3200 wl_resource_post_error(surface_resource,
3201 WL_DISPLAY_ERROR_INVALID_OBJECT,
3202 "desktop_shell::get_shell_surface already requested");
3206 shsurf = create_shell_surface(shell, surface, &shell_client);
3208 wl_resource_post_error(surface_resource,
3209 WL_DISPLAY_ERROR_INVALID_OBJECT,
3210 "surface->configure already set");
3215 wl_resource_create(client,
3216 &wl_shell_surface_interface, 1, id);
3217 wl_resource_set_implementation(shsurf->resource,
3218 &shell_surface_implementation,
3219 shsurf, shell_destroy_shell_surface);
3223 shell_surface_is_wl_shell_surface(struct shell_surface *shsurf)
3225 /* A shell surface without a resource is created from xwayland
3226 * and is considered a wl_shell surface for now. */
3228 return shsurf->resource == NULL ||
3229 wl_resource_instance_of(shsurf->resource,
3230 &wl_shell_surface_interface,
3231 &shell_surface_implementation);
3234 static const struct wl_shell_interface shell_implementation = {
3235 shell_get_shell_surface
3238 /****************************
3239 * xdg-shell implementation */
3242 xdg_surface_destroy(struct wl_client *client,
3243 struct wl_resource *resource)
3245 wl_resource_destroy(resource);
3249 xdg_surface_set_transient_for(struct wl_client *client,
3250 struct wl_resource *resource,
3251 struct wl_resource *parent_resource)
3253 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3254 struct weston_surface *parent;
3256 if (parent_resource)
3257 parent = wl_resource_get_user_data(parent_resource);
3261 shell_surface_set_parent(shsurf, parent);
3265 xdg_surface_set_margin(struct wl_client *client,
3266 struct wl_resource *resource,
3272 /* Do nothing, Weston doesn't try to constrain or place
3273 * surfaces in any special manner... */
3277 xdg_surface_set_app_id(struct wl_client *client,
3278 struct wl_resource *resource,
3281 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3283 free(shsurf->class);
3284 shsurf->class = strdup(app_id);
3288 xdg_surface_set_title(struct wl_client *client,
3289 struct wl_resource *resource, const char *title)
3291 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3293 set_title(shsurf, title);
3297 xdg_surface_move(struct wl_client *client, struct wl_resource *resource,
3298 struct wl_resource *seat_resource, uint32_t serial)
3300 common_surface_move(resource, seat_resource, serial);
3304 xdg_surface_resize(struct wl_client *client, struct wl_resource *resource,
3305 struct wl_resource *seat_resource, uint32_t serial,
3308 common_surface_resize(resource, seat_resource, serial, edges);
3312 xdg_surface_set_output(struct wl_client *client,
3313 struct wl_resource *resource,
3314 struct wl_resource *output_resource)
3316 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3317 struct weston_output *output;
3319 if (output_resource)
3320 output = wl_resource_get_user_data(output_resource);
3324 shsurf->recommended_output = output;
3328 xdg_surface_change_state(struct shell_surface *shsurf,
3329 uint32_t state, uint32_t value, uint32_t serial)
3331 xdg_surface_send_change_state(shsurf->resource, state, value, serial);
3332 shsurf->state_requested = true;
3335 case XDG_SURFACE_STATE_MAXIMIZED:
3336 shsurf->requested_state.maximized = value;
3338 set_maximized(shsurf, NULL);
3340 case XDG_SURFACE_STATE_FULLSCREEN:
3341 shsurf->requested_state.fullscreen = value;
3344 set_fullscreen(shsurf,
3345 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3346 0, shsurf->recommended_output);
3352 xdg_surface_request_change_state(struct wl_client *client,
3353 struct wl_resource *resource,
3358 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3360 /* The client can't know what the current state is, so we need
3361 to always send a state change in response. */
3363 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3367 case XDG_SURFACE_STATE_MAXIMIZED:
3368 case XDG_SURFACE_STATE_FULLSCREEN:
3371 /* send error? ignore? send change state with value 0? */
3375 xdg_surface_change_state(shsurf, state, value, serial);
3379 xdg_surface_ack_change_state(struct wl_client *client,
3380 struct wl_resource *resource,
3385 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3387 if (shsurf->state_requested) {
3388 shsurf->next_state = shsurf->requested_state;
3389 shsurf->state_changed = true;
3390 shsurf->state_requested = false;
3395 xdg_surface_set_minimized(struct wl_client *client,
3396 struct wl_resource *resource)
3398 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
3400 if (shsurf->type != SHELL_SURFACE_TOPLEVEL)
3403 /* apply compositor's own minimization logic (hide) */
3404 set_minimized(shsurf->surface, 1);
3407 static const struct xdg_surface_interface xdg_surface_implementation = {
3408 xdg_surface_destroy,
3409 xdg_surface_set_transient_for,
3410 xdg_surface_set_margin,
3411 xdg_surface_set_title,
3412 xdg_surface_set_app_id,
3415 xdg_surface_set_output,
3416 xdg_surface_request_change_state,
3417 xdg_surface_ack_change_state,
3418 xdg_surface_set_minimized
3422 xdg_send_configure(struct weston_surface *surface,
3423 uint32_t edges, int32_t width, int32_t height)
3425 struct shell_surface *shsurf = get_shell_surface(surface);
3429 xdg_surface_send_configure(shsurf->resource, width, height);
3432 static const struct weston_shell_client xdg_client = {
3437 xdg_use_unstable_version(struct wl_client *client,
3438 struct wl_resource *resource,
3442 wl_resource_post_error(resource,
3444 "xdg-shell:: version not implemented yet.");
3449 static struct shell_surface *
3450 create_xdg_surface(void *shell, struct weston_surface *surface,
3451 const struct weston_shell_client *client)
3453 struct shell_surface *shsurf;
3455 shsurf = create_common_surface(shell, surface, client);
3456 shsurf->type = SHELL_SURFACE_TOPLEVEL;
3462 xdg_get_xdg_surface(struct wl_client *client,
3463 struct wl_resource *resource,
3465 struct wl_resource *surface_resource)
3467 struct weston_surface *surface =
3468 wl_resource_get_user_data(surface_resource);
3469 struct shell_client *sc = wl_resource_get_user_data(resource);
3470 struct desktop_shell *shell = sc->shell;
3471 struct shell_surface *shsurf;
3473 if (get_shell_surface(surface)) {
3474 wl_resource_post_error(surface_resource,
3475 WL_DISPLAY_ERROR_INVALID_OBJECT,
3476 "xdg_shell::get_xdg_surface already requested");
3480 shsurf = create_xdg_surface(shell, surface, &xdg_client);
3482 wl_resource_post_error(surface_resource,
3483 WL_DISPLAY_ERROR_INVALID_OBJECT,
3484 "surface->configure already set");
3489 wl_resource_create(client,
3490 &xdg_surface_interface, 1, id);
3491 wl_resource_set_implementation(shsurf->resource,
3492 &xdg_surface_implementation,
3493 shsurf, shell_destroy_shell_surface);
3497 shell_surface_is_xdg_surface(struct shell_surface *shsurf)
3499 return shsurf->resource &&
3500 wl_resource_instance_of(shsurf->resource,
3501 &xdg_surface_interface,
3502 &xdg_surface_implementation);
3505 /* xdg-popup implementation */
3508 xdg_popup_destroy(struct wl_client *client,
3509 struct wl_resource *resource)
3511 wl_resource_destroy(resource);
3514 static const struct xdg_popup_interface xdg_popup_implementation = {
3519 xdg_popup_send_configure(struct weston_surface *surface,
3520 uint32_t edges, int32_t width, int32_t height)
3524 static const struct weston_shell_client xdg_popup_client = {
3525 xdg_popup_send_configure
3528 static struct shell_surface *
3529 create_xdg_popup(void *shell, struct weston_surface *surface,
3530 const struct weston_shell_client *client,
3531 struct weston_surface *parent,
3532 struct shell_seat *seat,
3534 int32_t x, int32_t y)
3536 struct shell_surface *shsurf;
3538 shsurf = create_common_surface(shell, surface, client);
3539 shsurf->type = SHELL_SURFACE_POPUP;
3540 shsurf->popup.shseat = seat;
3541 shsurf->popup.serial = serial;
3542 shsurf->popup.x = x;
3543 shsurf->popup.y = y;
3544 shell_surface_set_parent(shsurf, parent);
3550 xdg_get_xdg_popup(struct wl_client *client,
3551 struct wl_resource *resource,
3553 struct wl_resource *surface_resource,
3554 struct wl_resource *parent_resource,
3555 struct wl_resource *seat_resource,
3557 int32_t x, int32_t y, uint32_t flags)
3559 struct weston_surface *surface =
3560 wl_resource_get_user_data(surface_resource);
3561 struct shell_client *sc = wl_resource_get_user_data(resource);
3562 struct desktop_shell *shell = sc->shell;
3563 struct shell_surface *shsurf;
3564 struct weston_surface *parent;
3565 struct shell_seat *seat;
3567 if (get_shell_surface(surface)) {
3568 wl_resource_post_error(surface_resource,
3569 WL_DISPLAY_ERROR_INVALID_OBJECT,
3570 "xdg_shell::get_xdg_popup already requested");
3574 if (!parent_resource) {
3575 wl_resource_post_error(surface_resource,
3576 WL_DISPLAY_ERROR_INVALID_OBJECT,
3577 "xdg_shell::get_xdg_popup requires a parent shell surface");
3580 parent = wl_resource_get_user_data(parent_resource);
3581 seat = get_shell_seat(wl_resource_get_user_data(seat_resource));;
3583 shsurf = create_xdg_popup(shell, surface, &xdg_popup_client,
3584 parent, seat, serial, x, y);
3586 wl_resource_post_error(surface_resource,
3587 WL_DISPLAY_ERROR_INVALID_OBJECT,
3588 "surface->configure already set");
3593 wl_resource_create(client,
3594 &xdg_popup_interface, 1, id);
3595 wl_resource_set_implementation(shsurf->resource,
3596 &xdg_popup_implementation,
3597 shsurf, shell_destroy_shell_surface);
3601 xdg_pong(struct wl_client *client,
3602 struct wl_resource *resource, uint32_t serial)
3604 struct shell_client *sc = wl_resource_get_user_data(resource);
3606 shell_client_pong(sc, serial);
3610 shell_surface_is_xdg_popup(struct shell_surface *shsurf)
3612 return wl_resource_instance_of(shsurf->resource,
3613 &xdg_popup_interface,
3614 &xdg_popup_implementation);
3617 static const struct xdg_shell_interface xdg_implementation = {
3618 xdg_use_unstable_version,
3619 xdg_get_xdg_surface,
3625 xdg_shell_unversioned_dispatch(const void *implementation,
3626 void *_target, uint32_t opcode,
3627 const struct wl_message *message,
3628 union wl_argument *args)
3630 struct wl_resource *resource = _target;
3631 struct shell_client *sc = wl_resource_get_user_data(resource);
3634 wl_resource_post_error(resource,
3635 WL_DISPLAY_ERROR_INVALID_OBJECT,
3636 "must call use_unstable_version first");
3640 #define XDG_SERVER_VERSION 3
3642 static_assert(XDG_SERVER_VERSION == XDG_SHELL_VERSION_CURRENT,
3643 "shell implementation doesn't match protocol version");
3645 if (args[0].i != XDG_SERVER_VERSION) {
3646 wl_resource_post_error(resource,
3647 WL_DISPLAY_ERROR_INVALID_OBJECT,
3648 "incompatible version, server is %d "
3650 XDG_SERVER_VERSION, args[0].i);
3654 wl_resource_set_implementation(resource, &xdg_implementation,
3660 /* end of xdg-shell implementation */
3661 /***********************************/
3664 shell_fade(struct desktop_shell *shell, enum fade_type type);
3667 screensaver_timeout(void *data)
3669 struct desktop_shell *shell = data;
3671 shell_fade(shell, FADE_OUT);
3677 handle_screensaver_sigchld(struct weston_process *proc, int status)
3679 struct desktop_shell *shell =
3680 container_of(proc, struct desktop_shell, screensaver.process);
3685 weston_compositor_sleep(shell->compositor);
3689 launch_screensaver(struct desktop_shell *shell)
3691 if (shell->screensaver.binding)
3694 if (!shell->screensaver.path) {
3695 weston_compositor_sleep(shell->compositor);
3699 if (shell->screensaver.process.pid != 0) {
3700 weston_log("old screensaver still running\n");
3704 weston_client_launch(shell->compositor,
3705 &shell->screensaver.process,
3706 shell->screensaver.path,
3707 handle_screensaver_sigchld);
3711 terminate_screensaver(struct desktop_shell *shell)
3713 if (shell->screensaver.process.pid == 0)
3716 kill(shell->screensaver.process.pid, SIGTERM);
3720 configure_static_view(struct weston_view *ev, struct weston_layer *layer)
3722 struct weston_view *v, *next;
3724 wl_list_for_each_safe(v, next, &layer->view_list, layer_link) {
3725 if (v->output == ev->output && v != ev) {
3726 weston_view_unmap(v);
3727 v->surface->configure = NULL;
3731 weston_view_set_position(ev, ev->output->x, ev->output->y);
3733 if (wl_list_empty(&ev->layer_link)) {
3734 wl_list_insert(&layer->view_list, &ev->layer_link);
3735 weston_compositor_schedule_repaint(ev->surface->compositor);
3740 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3742 struct desktop_shell *shell = es->configure_private;
3743 struct weston_view *view;
3745 view = container_of(es->views.next, struct weston_view, surface_link);
3747 configure_static_view(view, &shell->background_layer);
3751 desktop_shell_set_background(struct wl_client *client,
3752 struct wl_resource *resource,
3753 struct wl_resource *output_resource,
3754 struct wl_resource *surface_resource)
3756 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3757 struct weston_surface *surface =
3758 wl_resource_get_user_data(surface_resource);
3759 struct weston_view *view, *next;
3761 if (surface->configure) {
3762 wl_resource_post_error(surface_resource,
3763 WL_DISPLAY_ERROR_INVALID_OBJECT,
3764 "surface role already assigned");
3768 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3769 weston_view_destroy(view);
3770 view = weston_view_create(surface);
3772 surface->configure = background_configure;
3773 surface->configure_private = shell;
3774 surface->output = wl_resource_get_user_data(output_resource);
3775 view->output = surface->output;
3776 desktop_shell_send_configure(resource, 0,
3778 surface->output->width,
3779 surface->output->height);
3783 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3785 struct desktop_shell *shell = es->configure_private;
3786 struct weston_view *view;
3788 view = container_of(es->views.next, struct weston_view, surface_link);
3790 configure_static_view(view, &shell->panel_layer);
3794 desktop_shell_set_panel(struct wl_client *client,
3795 struct wl_resource *resource,
3796 struct wl_resource *output_resource,
3797 struct wl_resource *surface_resource)
3799 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3800 struct weston_surface *surface =
3801 wl_resource_get_user_data(surface_resource);
3802 struct weston_view *view, *next;
3804 if (surface->configure) {
3805 wl_resource_post_error(surface_resource,
3806 WL_DISPLAY_ERROR_INVALID_OBJECT,
3807 "surface role already assigned");
3811 wl_list_for_each_safe(view, next, &surface->views, surface_link)
3812 weston_view_destroy(view);
3813 view = weston_view_create(surface);
3815 surface->configure = panel_configure;
3816 surface->configure_private = shell;
3817 surface->output = wl_resource_get_user_data(output_resource);
3818 view->output = surface->output;
3819 desktop_shell_send_configure(resource, 0,
3821 surface->output->width,
3822 surface->output->height);
3826 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3828 struct desktop_shell *shell = surface->configure_private;
3829 struct weston_view *view;
3831 view = container_of(surface->views.next, struct weston_view, surface_link);
3833 if (surface->width == 0)
3836 center_on_output(view, get_default_output(shell->compositor));
3838 if (!weston_surface_is_mapped(surface)) {
3839 wl_list_insert(&shell->lock_layer.view_list,
3841 weston_view_update_transform(view);
3842 shell_fade(shell, FADE_IN);
3847 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
3849 struct desktop_shell *shell =
3850 container_of(listener, struct desktop_shell, lock_surface_listener);
3852 weston_log("lock surface gone\n");
3853 shell->lock_surface = NULL;
3857 desktop_shell_set_lock_surface(struct wl_client *client,
3858 struct wl_resource *resource,
3859 struct wl_resource *surface_resource)
3861 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3862 struct weston_surface *surface =
3863 wl_resource_get_user_data(surface_resource);
3865 shell->prepare_event_sent = false;
3870 shell->lock_surface = surface;
3872 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
3873 wl_signal_add(&surface->destroy_signal,
3874 &shell->lock_surface_listener);
3876 weston_view_create(surface);
3877 surface->configure = lock_surface_configure;
3878 surface->configure_private = shell;
3882 resume_desktop(struct desktop_shell *shell)
3884 struct workspace *ws = get_current_workspace(shell);
3886 terminate_screensaver(shell);
3888 wl_list_remove(&shell->lock_layer.link);
3889 if (shell->showing_input_panels) {
3890 wl_list_insert(&shell->compositor->cursor_layer.link,
3891 &shell->input_panel_layer.link);
3892 wl_list_insert(&shell->input_panel_layer.link,
3893 &shell->fullscreen_layer.link);
3895 wl_list_insert(&shell->compositor->cursor_layer.link,
3896 &shell->fullscreen_layer.link);
3898 wl_list_insert(&shell->fullscreen_layer.link,
3899 &shell->panel_layer.link);
3900 wl_list_insert(&shell->panel_layer.link,
3903 restore_focus_state(shell, get_current_workspace(shell));
3905 shell->locked = false;
3906 shell_fade(shell, FADE_IN);
3907 weston_compositor_damage_all(shell->compositor);
3911 desktop_shell_unlock(struct wl_client *client,
3912 struct wl_resource *resource)
3914 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3916 shell->prepare_event_sent = false;
3919 resume_desktop(shell);
3923 desktop_shell_set_grab_surface(struct wl_client *client,
3924 struct wl_resource *resource,
3925 struct wl_resource *surface_resource)
3927 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3929 shell->grab_surface = wl_resource_get_user_data(surface_resource);
3930 weston_view_create(shell->grab_surface);
3934 desktop_shell_desktop_ready(struct wl_client *client,
3935 struct wl_resource *resource)
3937 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3939 shell_fade_startup(shell);
3942 static const struct desktop_shell_interface desktop_shell_implementation = {
3943 desktop_shell_set_background,
3944 desktop_shell_set_panel,
3945 desktop_shell_set_lock_surface,
3946 desktop_shell_unlock,
3947 desktop_shell_set_grab_surface,
3948 desktop_shell_desktop_ready
3951 static enum shell_surface_type
3952 get_shell_surface_type(struct weston_surface *surface)
3954 struct shell_surface *shsurf;
3956 shsurf = get_shell_surface(surface);
3958 return SHELL_SURFACE_NONE;
3959 return shsurf->type;
3963 move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3965 struct weston_surface *focus;
3966 struct weston_surface *surface;
3967 struct shell_surface *shsurf;
3969 if (seat->pointer->focus == NULL)
3972 focus = seat->pointer->focus->surface;
3974 surface = weston_surface_get_main_surface(focus);
3975 if (surface == NULL)
3978 shsurf = get_shell_surface(surface);
3979 if (shsurf == NULL || shsurf->state.fullscreen ||
3980 shsurf->state.maximized)
3983 surface_move(shsurf, (struct weston_seat *) seat);
3987 maximize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
3989 struct weston_surface *focus = seat->keyboard->focus;
3990 struct weston_surface *surface;
3991 struct shell_surface *shsurf;
3994 surface = weston_surface_get_main_surface(focus);
3995 if (surface == NULL)
3998 shsurf = get_shell_surface(surface);
4002 if (!shell_surface_is_xdg_surface(shsurf))
4005 serial = wl_display_next_serial(seat->compositor->wl_display);
4006 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_MAXIMIZED,
4007 !shsurf->state.maximized, serial);
4011 fullscreen_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4013 struct weston_surface *focus = seat->keyboard->focus;
4014 struct weston_surface *surface;
4015 struct shell_surface *shsurf;
4018 surface = weston_surface_get_main_surface(focus);
4019 if (surface == NULL)
4022 shsurf = get_shell_surface(surface);
4026 if (!shell_surface_is_xdg_surface(shsurf))
4029 serial = wl_display_next_serial(seat->compositor->wl_display);
4030 xdg_surface_change_state(shsurf, XDG_SURFACE_STATE_FULLSCREEN,
4031 !shsurf->state.fullscreen, serial);
4035 touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
4037 struct weston_surface *focus = seat->touch->focus->surface;
4038 struct weston_surface *surface;
4039 struct shell_surface *shsurf;
4041 surface = weston_surface_get_main_surface(focus);
4042 if (surface == NULL)
4045 shsurf = get_shell_surface(surface);
4046 if (shsurf == NULL || shsurf->state.fullscreen ||
4047 shsurf->state.maximized)
4050 surface_touch_move(shsurf, (struct weston_seat *) seat);
4054 resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
4056 struct weston_surface *focus;
4057 struct weston_surface *surface;
4060 struct shell_surface *shsurf;
4062 if (seat->pointer->focus == NULL)
4065 focus = seat->pointer->focus->surface;
4067 surface = weston_surface_get_main_surface(focus);
4068 if (surface == NULL)
4071 shsurf = get_shell_surface(surface);
4072 if (shsurf == NULL || shsurf->state.fullscreen ||
4073 shsurf->state.maximized)
4076 weston_view_from_global(shsurf->view,
4077 wl_fixed_to_int(seat->pointer->grab_x),
4078 wl_fixed_to_int(seat->pointer->grab_y),
4081 if (x < shsurf->surface->width / 3)
4082 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
4083 else if (x < 2 * shsurf->surface->width / 3)
4086 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
4088 if (y < shsurf->surface->height / 3)
4089 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
4090 else if (y < 2 * shsurf->surface->height / 3)
4093 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
4095 surface_resize(shsurf, (struct weston_seat *) seat, edges);
4099 surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4100 wl_fixed_t value, void *data)
4103 struct shell_surface *shsurf;
4104 struct weston_surface *focus = seat->pointer->focus->surface;
4105 struct weston_surface *surface;
4107 /* XXX: broken for windows containing sub-surfaces */
4108 surface = weston_surface_get_main_surface(focus);
4109 if (surface == NULL)
4112 shsurf = get_shell_surface(surface);
4116 shsurf->view->alpha -= wl_fixed_to_double(value) * step;
4118 if (shsurf->view->alpha > 1.0)
4119 shsurf->view->alpha = 1.0;
4120 if (shsurf->view->alpha < step)
4121 shsurf->view->alpha = step;
4123 weston_view_geometry_dirty(shsurf->view);
4124 weston_surface_damage(surface);
4128 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
4131 struct weston_seat *ws = (struct weston_seat *) seat;
4132 struct weston_compositor *compositor = ws->compositor;
4133 struct weston_output *output;
4136 wl_list_for_each(output, &compositor->output_list, link) {
4137 if (pixman_region32_contains_point(&output->region,
4138 wl_fixed_to_double(seat->pointer->x),
4139 wl_fixed_to_double(seat->pointer->y),
4141 if (key == KEY_PAGEUP)
4142 increment = output->zoom.increment;
4143 else if (key == KEY_PAGEDOWN)
4144 increment = -output->zoom.increment;
4145 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
4146 /* For every pixel zoom 20th of a step */
4147 increment = output->zoom.increment *
4148 -wl_fixed_to_double(value) / 20.0;
4152 output->zoom.level += increment;
4154 if (output->zoom.level < 0.0)
4155 output->zoom.level = 0.0;
4156 else if (output->zoom.level > output->zoom.max_level)
4157 output->zoom.level = output->zoom.max_level;
4158 else if (!output->zoom.active) {
4159 weston_output_activate_zoom(output);
4162 output->zoom.spring_z.target = output->zoom.level;
4164 weston_output_update_zoom(output);
4170 zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
4171 wl_fixed_t value, void *data)
4173 do_zoom(seat, time, 0, axis, value);
4177 zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4180 do_zoom(seat, time, key, 0, 0);
4184 terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4187 struct weston_compositor *compositor = data;
4189 wl_display_terminate(compositor->wl_display);
4193 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time,
4194 wl_fixed_t x, wl_fixed_t y)
4196 struct rotate_grab *rotate =
4197 container_of(grab, struct rotate_grab, base.grab);
4198 struct weston_pointer *pointer = grab->pointer;
4199 struct shell_surface *shsurf = rotate->base.shsurf;
4200 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
4202 weston_pointer_move(pointer, x, y);
4207 cx = 0.5f * shsurf->surface->width;
4208 cy = 0.5f * shsurf->surface->height;
4210 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
4211 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
4212 r = sqrtf(dx * dx + dy * dy);
4214 wl_list_remove(&shsurf->rotation.transform.link);
4215 weston_view_geometry_dirty(shsurf->view);
4218 struct weston_matrix *matrix =
4219 &shsurf->rotation.transform.matrix;
4221 weston_matrix_init(&rotate->rotation);
4222 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4224 weston_matrix_init(matrix);
4225 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
4226 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
4227 weston_matrix_multiply(matrix, &rotate->rotation);
4228 weston_matrix_translate(matrix, cx, cy, 0.0f);
4231 &shsurf->view->geometry.transformation_list,
4232 &shsurf->rotation.transform.link);
4234 wl_list_init(&shsurf->rotation.transform.link);
4235 weston_matrix_init(&shsurf->rotation.rotation);
4236 weston_matrix_init(&rotate->rotation);
4239 /* We need to adjust the position of the surface
4240 * in case it was resized in a rotated state before */
4241 cposx = shsurf->view->geometry.x + cx;
4242 cposy = shsurf->view->geometry.y + cy;
4243 dposx = rotate->center.x - cposx;
4244 dposy = rotate->center.y - cposy;
4245 if (dposx != 0.0f || dposy != 0.0f) {
4246 weston_view_set_position(shsurf->view,
4247 shsurf->view->geometry.x + dposx,
4248 shsurf->view->geometry.y + dposy);
4251 /* Repaint implies weston_surface_update_transform(), which
4252 * lazily applies the damage due to rotation update.
4254 weston_compositor_schedule_repaint(shsurf->surface->compositor);
4258 rotate_grab_button(struct weston_pointer_grab *grab,
4259 uint32_t time, uint32_t button, uint32_t state_w)
4261 struct rotate_grab *rotate =
4262 container_of(grab, struct rotate_grab, base.grab);
4263 struct weston_pointer *pointer = grab->pointer;
4264 struct shell_surface *shsurf = rotate->base.shsurf;
4265 enum wl_pointer_button_state state = state_w;
4267 if (pointer->button_count == 0 &&
4268 state == WL_POINTER_BUTTON_STATE_RELEASED) {
4270 weston_matrix_multiply(&shsurf->rotation.rotation,
4272 shell_grab_end(&rotate->base);
4278 rotate_grab_cancel(struct weston_pointer_grab *grab)
4280 struct rotate_grab *rotate =
4281 container_of(grab, struct rotate_grab, base.grab);
4283 shell_grab_end(&rotate->base);
4287 static const struct weston_pointer_grab_interface rotate_grab_interface = {
4295 surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
4297 struct rotate_grab *rotate;
4301 rotate = malloc(sizeof *rotate);
4305 weston_view_to_global_float(surface->view,
4306 surface->surface->width * 0.5f,
4307 surface->surface->height * 0.5f,
4308 &rotate->center.x, &rotate->center.y);
4310 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
4311 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
4312 r = sqrtf(dx * dx + dy * dy);
4314 struct weston_matrix inverse;
4316 weston_matrix_init(&inverse);
4317 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
4318 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
4320 weston_matrix_init(&rotate->rotation);
4321 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
4323 weston_matrix_init(&surface->rotation.rotation);
4324 weston_matrix_init(&rotate->rotation);
4327 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
4328 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
4332 rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4335 struct weston_surface *focus;
4336 struct weston_surface *base_surface;
4337 struct shell_surface *surface;
4339 if (seat->pointer->focus == NULL)
4342 focus = seat->pointer->focus->surface;
4344 base_surface = weston_surface_get_main_surface(focus);
4345 if (base_surface == NULL)
4348 surface = get_shell_surface(base_surface);
4349 if (surface == NULL || surface->state.fullscreen ||
4350 surface->state.maximized)
4353 surface_rotate(surface, seat);
4356 /* Move all fullscreen layers down to the current workspace in a non-reversible
4357 * manner. This should be used when implementing shell-wide overlays, such as
4358 * the alt-tab switcher, which need to de-promote fullscreen layers. */
4360 lower_fullscreen_layer(struct desktop_shell *shell)
4362 struct workspace *ws;
4363 struct weston_view *view, *prev;
4365 ws = get_current_workspace(shell);
4366 wl_list_for_each_reverse_safe(view, prev,
4367 &shell->fullscreen_layer.view_list,
4369 wl_list_remove(&view->layer_link);
4370 wl_list_insert(&ws->layer.view_list, &view->layer_link);
4371 weston_view_damage_below(view);
4372 weston_surface_damage(view->surface);
4377 activate(struct desktop_shell *shell, struct weston_surface *es,
4378 struct weston_seat *seat)
4380 struct weston_surface *main_surface;
4381 struct focus_state *state;
4382 struct workspace *ws;
4383 struct weston_surface *old_es;
4384 struct shell_surface *shsurf;
4386 main_surface = weston_surface_get_main_surface(es);
4388 weston_surface_activate(es, seat);
4390 state = ensure_focus_state(shell, seat);
4394 old_es = state->keyboard_focus;
4395 focus_state_set_focus(state, es);
4397 shsurf = get_shell_surface(main_surface);
4400 if (shsurf->state.fullscreen)
4401 shell_configure_fullscreen(shsurf);
4403 restore_all_output_modes(shell->compositor);
4405 /* Update the surface’s layer. This brings it to the top of the stacking
4406 * order as appropriate. */
4407 shell_surface_update_layer(shsurf);
4409 if (shell->focus_animation_type != ANIMATION_NONE) {
4410 ws = get_current_workspace(shell);
4411 animate_focus_change(shell, ws, get_default_view(old_es), get_default_view(es));
4415 /* no-op func for checking black surface */
4417 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4422 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
4424 if (es->configure == black_surface_configure) {
4426 *fs_surface = (struct weston_surface *)es->configure_private;
4433 activate_binding(struct weston_seat *seat,
4434 struct desktop_shell *shell,
4435 struct weston_surface *focus)
4437 struct weston_surface *main_surface;
4442 if (is_black_surface(focus, &main_surface))
4443 focus = main_surface;
4445 main_surface = weston_surface_get_main_surface(focus);
4446 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
4449 activate(shell, focus, seat);
4453 click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
4456 if (seat->pointer->grab != &seat->pointer->default_grab)
4458 if (seat->pointer->focus == NULL)
4461 activate_binding(seat, data, seat->pointer->focus->surface);
4465 touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
4467 if (seat->touch->grab != &seat->touch->default_grab)
4469 if (seat->touch->focus == NULL)
4472 activate_binding(seat, data, seat->touch->focus->surface);
4476 lock(struct desktop_shell *shell)
4478 struct workspace *ws = get_current_workspace(shell);
4480 if (shell->locked) {
4481 weston_compositor_sleep(shell->compositor);
4485 shell->locked = true;
4487 /* Hide all surfaces by removing the fullscreen, panel and
4488 * toplevel layers. This way nothing else can show or receive
4489 * input events while we are locked. */
4491 wl_list_remove(&shell->panel_layer.link);
4492 wl_list_remove(&shell->fullscreen_layer.link);
4493 if (shell->showing_input_panels)
4494 wl_list_remove(&shell->input_panel_layer.link);
4495 wl_list_remove(&ws->layer.link);
4496 wl_list_insert(&shell->compositor->cursor_layer.link,
4497 &shell->lock_layer.link);
4499 launch_screensaver(shell);
4501 /* TODO: disable bindings that should not work while locked. */
4503 /* All this must be undone in resume_desktop(). */
4507 unlock(struct desktop_shell *shell)
4509 if (!shell->locked || shell->lock_surface) {
4510 shell_fade(shell, FADE_IN);
4514 /* If desktop-shell client has gone away, unlock immediately. */
4515 if (!shell->child.desktop_shell) {
4516 resume_desktop(shell);
4520 if (shell->prepare_event_sent)
4523 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
4524 shell->prepare_event_sent = true;
4528 shell_fade_done(struct weston_view_animation *animation, void *data)
4530 struct desktop_shell *shell = data;
4532 shell->fade.animation = NULL;
4534 switch (shell->fade.type) {
4536 weston_surface_destroy(shell->fade.view->surface);
4537 shell->fade.view = NULL;
4547 static struct weston_view *
4548 shell_fade_create_surface(struct desktop_shell *shell)
4550 struct weston_compositor *compositor = shell->compositor;
4551 struct weston_surface *surface;
4552 struct weston_view *view;
4554 surface = weston_surface_create(compositor);
4558 view = weston_view_create(surface);
4560 weston_surface_destroy(surface);
4564 weston_surface_set_size(surface, 8192, 8192);
4565 weston_view_set_position(view, 0, 0);
4566 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
4567 wl_list_insert(&compositor->fade_layer.view_list,
4569 pixman_region32_init(&surface->input);
4575 shell_fade(struct desktop_shell *shell, enum fade_type type)
4587 weston_log("shell: invalid fade type\n");
4591 shell->fade.type = type;
4593 if (shell->fade.view == NULL) {
4594 shell->fade.view = shell_fade_create_surface(shell);
4595 if (!shell->fade.view)
4598 shell->fade.view->alpha = 1.0 - tint;
4599 weston_view_update_transform(shell->fade.view);
4602 if (shell->fade.view->output == NULL) {
4603 /* If the black view gets a NULL output, we lost the
4604 * last output and we'll just cancel the fade. This
4605 * happens when you close the last window under the
4606 * X11 or Wayland backends. */
4607 shell->locked = false;
4608 weston_surface_destroy(shell->fade.view->surface);
4609 shell->fade.view = NULL;
4610 } else if (shell->fade.animation) {
4611 weston_fade_update(shell->fade.animation, tint);
4613 shell->fade.animation =
4614 weston_fade_run(shell->fade.view,
4615 1.0 - tint, tint, 300.0,
4616 shell_fade_done, shell);
4621 do_shell_fade_startup(void *data)
4623 struct desktop_shell *shell = data;
4625 if (shell->startup_animation_type == ANIMATION_FADE)
4626 shell_fade(shell, FADE_IN);
4627 else if (shell->startup_animation_type == ANIMATION_NONE) {
4628 weston_surface_destroy(shell->fade.view->surface);
4629 shell->fade.view = NULL;
4634 shell_fade_startup(struct desktop_shell *shell)
4636 struct wl_event_loop *loop;
4638 if (!shell->fade.startup_timer)
4641 wl_event_source_remove(shell->fade.startup_timer);
4642 shell->fade.startup_timer = NULL;
4644 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4645 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
4649 fade_startup_timeout(void *data)
4651 struct desktop_shell *shell = data;
4653 shell_fade_startup(shell);
4658 shell_fade_init(struct desktop_shell *shell)
4660 /* Make compositor output all black, and wait for the desktop-shell
4661 * client to signal it is ready, then fade in. The timer triggers a
4662 * fade-in, in case the desktop-shell client takes too long.
4665 struct wl_event_loop *loop;
4667 if (shell->fade.view != NULL) {
4668 weston_log("%s: warning: fade surface already exists\n",
4673 shell->fade.view = shell_fade_create_surface(shell);
4674 if (!shell->fade.view)
4677 weston_view_update_transform(shell->fade.view);
4678 weston_surface_damage(shell->fade.view->surface);
4680 loop = wl_display_get_event_loop(shell->compositor->wl_display);
4681 shell->fade.startup_timer =
4682 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
4683 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
4687 idle_handler(struct wl_listener *listener, void *data)
4689 struct desktop_shell *shell =
4690 container_of(listener, struct desktop_shell, idle_listener);
4691 struct weston_seat *seat;
4693 wl_list_for_each(seat, &shell->compositor->seat_list, link)
4695 popup_grab_end(seat->pointer);
4697 shell_fade(shell, FADE_OUT);
4698 /* lock() is called from shell_fade_done() */
4702 wake_handler(struct wl_listener *listener, void *data)
4704 struct desktop_shell *shell =
4705 container_of(listener, struct desktop_shell, wake_listener);
4711 center_on_output(struct weston_view *view, struct weston_output *output)
4713 int32_t surf_x, surf_y, width, height;
4716 surface_subsurfaces_boundingbox(view->surface, &surf_x, &surf_y, &width, &height);
4718 x = output->x + (output->width - width) / 2 - surf_x / 2;
4719 y = output->y + (output->height - height) / 2 - surf_y / 2;
4721 weston_view_set_position(view, x, y);
4725 weston_view_set_initial_position(struct weston_view *view,
4726 struct desktop_shell *shell)
4728 struct weston_compositor *compositor = shell->compositor;
4730 int range_x, range_y;
4731 int dx, dy, x, y, panel_height;
4732 struct weston_output *output, *target_output = NULL;
4733 struct weston_seat *seat;
4735 /* As a heuristic place the new window on the same output as the
4736 * pointer. Falling back to the output containing 0, 0.
4738 * TODO: Do something clever for touch too?
4740 wl_list_for_each(seat, &compositor->seat_list, link) {
4741 if (seat->pointer) {
4742 ix = wl_fixed_to_int(seat->pointer->x);
4743 iy = wl_fixed_to_int(seat->pointer->y);
4748 wl_list_for_each(output, &compositor->output_list, link) {
4749 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
4750 target_output = output;
4755 if (!target_output) {
4756 weston_view_set_position(view, 10 + random() % 400,
4757 10 + random() % 400);
4761 /* Valid range within output where the surface will still be onscreen.
4762 * If this is negative it means that the surface is bigger than
4765 panel_height = get_output_panel_height(shell, target_output);
4766 range_x = target_output->width - view->surface->width;
4767 range_y = (target_output->height - panel_height) -
4768 view->surface->height;
4771 dx = random() % range_x;
4776 dy = panel_height + random() % range_y;
4780 x = target_output->x + dx;
4781 y = target_output->y + dy;
4783 weston_view_set_position(view, x, y);
4787 map(struct desktop_shell *shell, struct shell_surface *shsurf,
4788 int32_t sx, int32_t sy)
4790 struct weston_compositor *compositor = shell->compositor;
4791 struct weston_seat *seat;
4792 int panel_height = 0;
4793 int32_t surf_x, surf_y;
4795 /* initial positioning, see also configure() */
4796 switch (shsurf->type) {
4797 case SHELL_SURFACE_TOPLEVEL:
4798 if (shsurf->state.fullscreen) {
4799 center_on_output(shsurf->view, shsurf->fullscreen_output);
4800 shell_map_fullscreen(shsurf);
4801 } else if (shsurf->state.maximized) {
4802 /* use surface configure to set the geometry */
4803 panel_height = get_output_panel_height(shell, shsurf->output);
4804 surface_subsurfaces_boundingbox(shsurf->surface,
4805 &surf_x, &surf_y, NULL, NULL);
4806 weston_view_set_position(shsurf->view,
4807 shsurf->output->x - surf_x,
4809 panel_height - surf_y);
4810 } else if (!shsurf->state.relative) {
4811 weston_view_set_initial_position(shsurf->view, shell);
4814 case SHELL_SURFACE_POPUP:
4815 shell_map_popup(shsurf);
4817 case SHELL_SURFACE_NONE:
4818 weston_view_set_position(shsurf->view,
4819 shsurf->view->geometry.x + sx,
4820 shsurf->view->geometry.y + sy);
4822 case SHELL_SURFACE_XWAYLAND:
4827 /* Surface stacking order, see also activate(). */
4828 shell_surface_update_layer(shsurf);
4830 if (shsurf->type != SHELL_SURFACE_NONE) {
4831 weston_view_update_transform(shsurf->view);
4832 if (shsurf->state.maximized) {
4833 shsurf->surface->output = shsurf->output;
4834 shsurf->view->output = shsurf->output;
4838 switch (shsurf->type) {
4839 /* XXX: xwayland's using the same fields for transient type */
4840 case SHELL_SURFACE_XWAYLAND:
4841 if (shsurf->transient.flags ==
4842 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4844 case SHELL_SURFACE_TOPLEVEL:
4845 if (shsurf->state.relative &&
4846 shsurf->transient.flags == WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
4850 wl_list_for_each(seat, &compositor->seat_list, link)
4851 activate(shell, shsurf->surface, seat);
4853 case SHELL_SURFACE_POPUP:
4854 case SHELL_SURFACE_NONE:
4859 if (shsurf->type == SHELL_SURFACE_TOPLEVEL &&
4860 !shsurf->state.maximized && !shsurf->state.fullscreen)
4862 switch (shell->win_animation_type) {
4863 case ANIMATION_FADE:
4864 weston_fade_run(shsurf->view, 0.0, 1.0, 300.0, NULL, NULL);
4866 case ANIMATION_ZOOM:
4867 weston_zoom_run(shsurf->view, 0.5, 1.0, NULL, NULL);
4869 case ANIMATION_NONE:
4877 configure(struct desktop_shell *shell, struct weston_surface *surface,
4880 struct shell_surface *shsurf;
4881 struct weston_view *view;
4882 int32_t mx, my, surf_x, surf_y;
4884 shsurf = get_shell_surface(surface);
4888 if (shsurf->state.fullscreen)
4889 shell_configure_fullscreen(shsurf);
4890 else if (shsurf->state.maximized) {
4891 /* setting x, y and using configure to change that geometry */
4892 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
4894 mx = shsurf->output->x - surf_x;
4895 my = shsurf->output->y +
4896 get_output_panel_height(shell,shsurf->output) - surf_y;
4897 weston_view_set_position(shsurf->view, mx, my);
4899 weston_view_set_position(shsurf->view, x, y);
4902 /* XXX: would a fullscreen surface need the same handling? */
4903 if (surface->output) {
4904 wl_list_for_each(view, &surface->views, surface_link)
4905 weston_view_update_transform(view);
4907 if (shsurf->state.maximized)
4908 surface->output = shsurf->output;
4913 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
4915 struct shell_surface *shsurf = get_shell_surface(es);
4916 struct desktop_shell *shell;
4917 int type_changed = 0;
4921 shell = shsurf->shell;
4923 if (!weston_surface_is_mapped(es) &&
4924 !wl_list_empty(&shsurf->popup.grab_link)) {
4925 remove_popup_grab(shsurf);
4931 if (shsurf->state_changed) {
4932 set_surface_type(shsurf);
4936 if (!weston_surface_is_mapped(es)) {
4937 map(shell, shsurf, sx, sy);
4938 } else if (type_changed || sx != 0 || sy != 0 ||
4939 shsurf->last_width != es->width ||
4940 shsurf->last_height != es->height) {
4941 float from_x, from_y;
4944 if (shsurf->resize_edges) {
4949 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_LEFT)
4950 sx = shsurf->last_width - es->width;
4951 if (shsurf->resize_edges & WL_SHELL_SURFACE_RESIZE_TOP)
4952 sy = shsurf->last_height - es->height;
4954 shsurf->last_width = es->width;
4955 shsurf->last_height = es->height;
4957 weston_view_to_global_float(shsurf->view, 0, 0, &from_x, &from_y);
4958 weston_view_to_global_float(shsurf->view, sx, sy, &to_x, &to_y);
4959 configure(shell, es,
4960 shsurf->view->geometry.x + to_x - from_x,
4961 shsurf->view->geometry.y + to_y - from_y);
4965 static void launch_desktop_shell_process(void *data);
4968 desktop_shell_sigchld(struct weston_process *process, int status)
4971 struct desktop_shell *shell =
4972 container_of(process, struct desktop_shell, child.process);
4974 shell->child.process.pid = 0;
4975 shell->child.client = NULL; /* already destroyed by wayland */
4977 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
4978 time = weston_compositor_get_time();
4979 if (time - shell->child.deathstamp > 30000) {
4980 shell->child.deathstamp = time;
4981 shell->child.deathcount = 0;
4984 shell->child.deathcount++;
4985 if (shell->child.deathcount > 5) {
4986 weston_log("%s died, giving up.\n", shell->client);
4990 weston_log("%s died, respawning...\n", shell->client);
4991 launch_desktop_shell_process(shell);
4992 shell_fade_startup(shell);
4996 desktop_shell_client_destroy(struct wl_listener *listener, void *data)
4998 struct desktop_shell *shell;
5000 shell = container_of(listener, struct desktop_shell,
5001 child.client_destroy_listener);
5003 shell->child.client = NULL;
5007 launch_desktop_shell_process(void *data)
5009 struct desktop_shell *shell = data;
5011 shell->child.client = weston_client_launch(shell->compositor,
5012 &shell->child.process,
5014 desktop_shell_sigchld);
5016 if (!shell->child.client)
5017 weston_log("not able to start %s\n", shell->client);
5019 shell->child.client_destroy_listener.notify =
5020 desktop_shell_client_destroy;
5021 wl_client_add_destroy_listener(shell->child.client,
5022 &shell->child.client_destroy_listener);
5026 handle_shell_client_destroy(struct wl_listener *listener, void *data)
5028 struct shell_client *sc =
5029 container_of(listener, struct shell_client, destroy_listener);
5032 wl_event_source_remove(sc->ping_timer);
5036 static struct shell_client *
5037 shell_client_create(struct wl_client *client, struct desktop_shell *shell,
5038 const struct wl_interface *interface, uint32_t id)
5040 struct shell_client *sc;
5042 sc = zalloc(sizeof *sc);
5044 wl_client_post_no_memory(client);
5048 sc->resource = wl_resource_create(client, interface, 1, id);
5049 if (sc->resource == NULL) {
5051 wl_client_post_no_memory(client);
5055 sc->client = client;
5057 sc->destroy_listener.notify = handle_shell_client_destroy;
5058 wl_client_add_destroy_listener(client, &sc->destroy_listener);
5064 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5066 struct desktop_shell *shell = data;
5067 struct shell_client *sc;
5069 sc = shell_client_create(client, shell, &wl_shell_interface, id);
5071 wl_resource_set_implementation(sc->resource,
5072 &shell_implementation,
5077 bind_xdg_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
5079 struct desktop_shell *shell = data;
5080 struct shell_client *sc;
5082 sc = shell_client_create(client, shell, &xdg_shell_interface, id);
5084 wl_resource_set_dispatcher(sc->resource,
5085 xdg_shell_unversioned_dispatch,
5090 unbind_desktop_shell(struct wl_resource *resource)
5092 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5095 resume_desktop(shell);
5097 shell->child.desktop_shell = NULL;
5098 shell->prepare_event_sent = false;
5102 bind_desktop_shell(struct wl_client *client,
5103 void *data, uint32_t version, uint32_t id)
5105 struct desktop_shell *shell = data;
5106 struct wl_resource *resource;
5108 resource = wl_resource_create(client, &desktop_shell_interface,
5109 MIN(version, 2), id);
5111 if (client == shell->child.client) {
5112 wl_resource_set_implementation(resource,
5113 &desktop_shell_implementation,
5114 shell, unbind_desktop_shell);
5115 shell->child.desktop_shell = resource;
5118 shell_fade_startup(shell);
5123 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5124 "permission to bind desktop_shell denied");
5125 wl_resource_destroy(resource);
5129 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
5131 struct desktop_shell *shell = surface->configure_private;
5132 struct weston_view *view;
5134 if (surface->width == 0)
5137 /* XXX: starting weston-screensaver beforehand does not work */
5141 view = container_of(surface->views.next, struct weston_view, surface_link);
5142 center_on_output(view, surface->output);
5144 if (wl_list_empty(&view->layer_link)) {
5145 wl_list_insert(shell->lock_layer.view_list.prev,
5147 weston_view_update_transform(view);
5148 wl_event_source_timer_update(shell->screensaver.timer,
5149 shell->screensaver.duration);
5150 shell_fade(shell, FADE_IN);
5155 screensaver_set_surface(struct wl_client *client,
5156 struct wl_resource *resource,
5157 struct wl_resource *surface_resource,
5158 struct wl_resource *output_resource)
5160 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5161 struct weston_surface *surface =
5162 wl_resource_get_user_data(surface_resource);
5163 struct weston_output *output = wl_resource_get_user_data(output_resource);
5164 struct weston_view *view, *next;
5166 /* Make sure we only have one view */
5167 wl_list_for_each_safe(view, next, &surface->views, surface_link)
5168 weston_view_destroy(view);
5169 weston_view_create(surface);
5171 surface->configure = screensaver_configure;
5172 surface->configure_private = shell;
5173 surface->output = output;
5176 static const struct screensaver_interface screensaver_implementation = {
5177 screensaver_set_surface
5181 unbind_screensaver(struct wl_resource *resource)
5183 struct desktop_shell *shell = wl_resource_get_user_data(resource);
5185 shell->screensaver.binding = NULL;
5189 bind_screensaver(struct wl_client *client,
5190 void *data, uint32_t version, uint32_t id)
5192 struct desktop_shell *shell = data;
5193 struct wl_resource *resource;
5195 resource = wl_resource_create(client, &screensaver_interface, 1, id);
5197 if (shell->screensaver.binding == NULL) {
5198 wl_resource_set_implementation(resource,
5199 &screensaver_implementation,
5200 shell, unbind_screensaver);
5201 shell->screensaver.binding = resource;
5205 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
5206 "interface object already bound");
5207 wl_resource_destroy(resource);
5211 struct desktop_shell *shell;
5212 struct weston_surface *current;
5213 struct wl_listener listener;
5214 struct weston_keyboard_grab grab;
5215 struct wl_array minimized_array;
5219 switcher_next(struct switcher *switcher)
5221 struct weston_view *view;
5222 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
5223 struct shell_surface *shsurf;
5224 struct workspace *ws = get_current_workspace(switcher->shell);
5226 /* temporary re-display minimized surfaces */
5227 struct weston_view *tmp;
5228 struct weston_view **minimized;
5229 wl_list_for_each_safe(view, tmp, &switcher->shell->minimized_layer.view_list, layer_link) {
5230 wl_list_remove(&view->layer_link);
5231 wl_list_insert(&ws->layer.view_list, &view->layer_link);
5232 minimized = wl_array_add(&switcher->minimized_array, sizeof *minimized);
5236 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
5237 shsurf = get_shell_surface(view->surface);
5239 shsurf->type == SHELL_SURFACE_TOPLEVEL &&
5240 shsurf->parent == NULL) {
5242 first = view->surface;
5243 if (prev == switcher->current)
5244 next = view->surface;
5245 prev = view->surface;
5247 weston_view_geometry_dirty(view);
5248 weston_surface_damage(view->surface);
5251 if (is_black_surface(view->surface, NULL)) {
5253 weston_view_geometry_dirty(view);
5254 weston_surface_damage(view->surface);
5264 wl_list_remove(&switcher->listener.link);
5265 wl_signal_add(&next->destroy_signal, &switcher->listener);
5267 switcher->current = next;
5268 wl_list_for_each(view, &next->views, surface_link)
5271 shsurf = get_shell_surface(switcher->current);
5272 if (shsurf && shsurf->state.fullscreen)
5273 shsurf->fullscreen.black_view->alpha = 1.0;
5277 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
5279 struct switcher *switcher =
5280 container_of(listener, struct switcher, listener);
5282 switcher_next(switcher);
5286 switcher_destroy(struct switcher *switcher)
5288 struct weston_view *view;
5289 struct weston_keyboard *keyboard = switcher->grab.keyboard;
5290 struct workspace *ws = get_current_workspace(switcher->shell);
5292 wl_list_for_each(view, &ws->layer.view_list, layer_link) {
5293 if (is_focus_view(view))
5297 weston_surface_damage(view->surface);
5300 if (switcher->current)
5301 activate(switcher->shell, switcher->current,
5302 (struct weston_seat *) keyboard->seat);
5303 wl_list_remove(&switcher->listener.link);
5304 weston_keyboard_end_grab(keyboard);
5305 if (keyboard->input_method_resource)
5306 keyboard->grab = &keyboard->input_method_grab;
5308 /* re-hide surfaces that were temporary shown during the switch */
5309 struct weston_view **minimized;
5310 wl_array_for_each(minimized, &switcher->minimized_array) {
5311 /* with the exception of the current selected */
5312 if ((*minimized)->surface != switcher->current) {
5313 wl_list_remove(&(*minimized)->layer_link);
5314 wl_list_insert(&switcher->shell->minimized_layer.view_list, &(*minimized)->layer_link);
5315 weston_view_damage_below(*minimized);
5318 wl_array_release(&switcher->minimized_array);
5324 switcher_key(struct weston_keyboard_grab *grab,
5325 uint32_t time, uint32_t key, uint32_t state_w)
5327 struct switcher *switcher = container_of(grab, struct switcher, grab);
5328 enum wl_keyboard_key_state state = state_w;
5330 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
5331 switcher_next(switcher);
5335 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
5336 uint32_t mods_depressed, uint32_t mods_latched,
5337 uint32_t mods_locked, uint32_t group)
5339 struct switcher *switcher = container_of(grab, struct switcher, grab);
5340 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
5342 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
5343 switcher_destroy(switcher);
5347 switcher_cancel(struct weston_keyboard_grab *grab)
5349 struct switcher *switcher = container_of(grab, struct switcher, grab);
5351 switcher_destroy(switcher);
5354 static const struct weston_keyboard_grab_interface switcher_grab = {
5361 switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5364 struct desktop_shell *shell = data;
5365 struct switcher *switcher;
5367 switcher = malloc(sizeof *switcher);
5368 switcher->shell = shell;
5369 switcher->current = NULL;
5370 switcher->listener.notify = switcher_handle_surface_destroy;
5371 wl_list_init(&switcher->listener.link);
5372 wl_array_init(&switcher->minimized_array);
5374 restore_all_output_modes(shell->compositor);
5375 lower_fullscreen_layer(switcher->shell);
5376 switcher->grab.interface = &switcher_grab;
5377 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
5378 weston_keyboard_set_focus(seat->keyboard, NULL);
5379 switcher_next(switcher);
5383 backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5386 struct weston_compositor *compositor = data;
5387 struct weston_output *output;
5388 long backlight_new = 0;
5390 /* TODO: we're limiting to simple use cases, where we assume just
5391 * control on the primary display. We'd have to extend later if we
5392 * ever get support for setting backlights on random desktop LCD
5394 output = get_default_output(compositor);
5398 if (!output->set_backlight)
5401 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
5402 backlight_new = output->backlight_current - 25;
5403 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
5404 backlight_new = output->backlight_current + 25;
5406 if (backlight_new < 5)
5408 if (backlight_new > 255)
5409 backlight_new = 255;
5411 output->backlight_current = backlight_new;
5412 output->set_backlight(output, output->backlight_current);
5415 struct debug_binding_grab {
5416 struct weston_keyboard_grab grab;
5417 struct weston_seat *seat;
5419 int key_released[2];
5423 debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
5424 uint32_t key, uint32_t state)
5426 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5427 struct weston_compositor *ec = db->seat->compositor;
5428 struct wl_display *display = ec->wl_display;
5429 struct wl_resource *resource;
5431 int send = 0, terminate = 0;
5432 int check_binding = 1;
5434 struct wl_list *resource_list;
5436 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
5437 /* Do not run bindings on key releases */
5440 for (i = 0; i < 2; i++)
5441 if (key == db->key[i])
5442 db->key_released[i] = 1;
5444 if (db->key_released[0] && db->key_released[1]) {
5445 /* All key releases been swalled so end the grab */
5447 } else if (key != db->key[0] && key != db->key[1]) {
5448 /* Should not swallow release of other keys */
5451 } else if (key == db->key[0] && !db->key_released[0]) {
5452 /* Do not check bindings for the first press of the binding
5453 * key. This allows it to be used as a debug shortcut.
5454 * We still need to swallow this event. */
5456 } else if (db->key[1]) {
5457 /* If we already ran a binding don't process another one since
5458 * we can't keep track of all the binding keys that were
5459 * pressed in order to swallow the release events. */
5464 if (check_binding) {
5465 if (weston_compositor_run_debug_binding(ec, db->seat, time,
5467 /* We ran a binding so swallow the press and keep the
5468 * grab to swallow the released too. */
5473 /* Terminate the grab since the key pressed is not a
5474 * debug binding key. */
5481 serial = wl_display_next_serial(display);
5482 resource_list = &grab->keyboard->focus_resource_list;
5483 wl_resource_for_each(resource, resource_list) {
5484 wl_keyboard_send_key(resource, serial, time, key, state);
5489 weston_keyboard_end_grab(grab->keyboard);
5490 if (grab->keyboard->input_method_resource)
5491 grab->keyboard->grab = &grab->keyboard->input_method_grab;
5497 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
5498 uint32_t mods_depressed, uint32_t mods_latched,
5499 uint32_t mods_locked, uint32_t group)
5501 struct wl_resource *resource;
5502 struct wl_list *resource_list;
5504 resource_list = &grab->keyboard->focus_resource_list;
5506 wl_resource_for_each(resource, resource_list) {
5507 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
5508 mods_latched, mods_locked, group);
5513 debug_binding_cancel(struct weston_keyboard_grab *grab)
5515 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
5517 weston_keyboard_end_grab(grab->keyboard);
5521 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
5523 debug_binding_modifiers,
5524 debug_binding_cancel,
5528 debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
5530 struct debug_binding_grab *grab;
5532 grab = calloc(1, sizeof *grab);
5536 grab->seat = (struct weston_seat *) seat;
5538 grab->grab.interface = &debug_binding_keyboard_grab;
5539 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
5543 force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
5546 struct weston_surface *focus_surface;
5547 struct wl_client *client;
5548 struct desktop_shell *shell = data;
5549 struct weston_compositor *compositor = shell->compositor;
5552 focus_surface = seat->keyboard->focus;
5556 wl_signal_emit(&compositor->kill_signal, focus_surface);
5558 client = wl_resource_get_client(focus_surface->resource);
5559 wl_client_get_credentials(client, &pid, NULL, NULL);
5561 /* Skip clients that we launched ourselves (the credentials of
5562 * the socketpair is ours) */
5563 if (pid == getpid())
5570 workspace_up_binding(struct weston_seat *seat, uint32_t time,
5571 uint32_t key, void *data)
5573 struct desktop_shell *shell = data;
5574 unsigned int new_index = shell->workspaces.current;
5581 change_workspace(shell, new_index);
5585 workspace_down_binding(struct weston_seat *seat, uint32_t time,
5586 uint32_t key, void *data)
5588 struct desktop_shell *shell = data;
5589 unsigned int new_index = shell->workspaces.current;
5593 if (new_index < shell->workspaces.num - 1)
5596 change_workspace(shell, new_index);
5600 workspace_f_binding(struct weston_seat *seat, uint32_t time,
5601 uint32_t key, void *data)
5603 struct desktop_shell *shell = data;
5604 unsigned int new_index;
5608 new_index = key - KEY_F1;
5609 if (new_index >= shell->workspaces.num)
5610 new_index = shell->workspaces.num - 1;
5612 change_workspace(shell, new_index);
5616 workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
5617 uint32_t key, void *data)
5619 struct desktop_shell *shell = data;
5620 unsigned int new_index = shell->workspaces.current;
5628 take_surface_to_workspace_by_seat(shell, seat, new_index);
5632 workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
5633 uint32_t key, void *data)
5635 struct desktop_shell *shell = data;
5636 unsigned int new_index = shell->workspaces.current;
5641 if (new_index < shell->workspaces.num - 1)
5644 take_surface_to_workspace_by_seat(shell, seat, new_index);
5648 shell_reposition_view_on_output_destroy(struct weston_view *view)
5650 struct weston_output *output, *first_output;
5651 struct weston_compositor *ec = view->surface->compositor;
5652 struct shell_surface *shsurf;
5656 x = view->geometry.x;
5657 y = view->geometry.y;
5659 /* At this point the destroyed output is not in the list anymore.
5660 * If the view is still visible somewhere, we leave where it is,
5661 * otherwise, move it to the first output. */
5663 wl_list_for_each(output, &ec->output_list, link) {
5664 if (pixman_region32_contains_point(&output->region,
5672 first_output = container_of(ec->output_list.next,
5673 struct weston_output, link);
5675 x = first_output->x + first_output->width / 4;
5676 y = first_output->y + first_output->height / 4;
5679 weston_view_set_position(view, x, y);
5681 shsurf = get_shell_surface(view->surface);
5684 shsurf->saved_position_valid = false;
5685 shsurf->next_state.maximized = false;
5686 shsurf->next_state.fullscreen = false;
5687 shsurf->state_changed = true;
5692 shell_reposition_views_on_output_destroy(struct shell_output *shell_output)
5694 struct desktop_shell *shell = shell_output->shell;
5695 struct weston_output *output = shell_output->output;
5696 struct weston_layer *layer;
5697 struct weston_view *view;
5699 /* Move all views in the layers owned by the shell */
5700 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5701 wl_list_for_each(view, &layer->view_list, layer_link) {
5702 if (view->output != output)
5705 shell_reposition_view_on_output_destroy(view);
5708 /* We don't start from the beggining of the layer list, so
5709 * make sure we don't wrap around it. */
5710 if (layer == &shell->background_layer)
5716 handle_output_destroy(struct wl_listener *listener, void *data)
5718 struct shell_output *output_listener =
5719 container_of(listener, struct shell_output, destroy_listener);
5721 shell_reposition_views_on_output_destroy(output_listener);
5723 wl_list_remove(&output_listener->destroy_listener.link);
5724 wl_list_remove(&output_listener->link);
5725 free(output_listener);
5729 create_shell_output(struct desktop_shell *shell,
5730 struct weston_output *output)
5732 struct shell_output *shell_output;
5734 shell_output = zalloc(sizeof *shell_output);
5735 if (shell_output == NULL)
5738 shell_output->output = output;
5739 shell_output->shell = shell;
5740 shell_output->destroy_listener.notify = handle_output_destroy;
5741 wl_signal_add(&output->destroy_signal,
5742 &shell_output->destroy_listener);
5743 wl_list_insert(shell->output_list.prev, &shell_output->link);
5747 handle_output_create(struct wl_listener *listener, void *data)
5749 struct desktop_shell *shell =
5750 container_of(listener, struct desktop_shell, output_create_listener);
5751 struct weston_output *output = (struct weston_output *)data;
5753 create_shell_output(shell, output);
5757 handle_output_move(struct wl_listener *listener, void *data)
5759 struct desktop_shell *shell;
5760 struct weston_output *output;
5761 struct weston_layer *layer;
5762 struct weston_view *view;
5765 shell = container_of(listener, struct desktop_shell,
5766 output_move_listener);
5769 /* Move all views in the layers owned by the shell */
5770 wl_list_for_each(layer, shell->fullscreen_layer.link.prev, link) {
5771 wl_list_for_each(view, &layer->view_list, layer_link) {
5772 if (view->output != output)
5775 x = view->geometry.x + output->move_x;
5776 y = view->geometry.y + output->move_y;
5777 weston_view_set_position(view, x, y);
5780 /* We don't start from the beggining of the layer list, so
5781 * make sure we don't wrap around it. */
5782 if (layer == &shell->background_layer)
5788 setup_output_destroy_handler(struct weston_compositor *ec,
5789 struct desktop_shell *shell)
5791 struct weston_output *output;
5793 wl_list_init(&shell->output_list);
5794 wl_list_for_each(output, &ec->output_list, link)
5795 create_shell_output(shell, output);
5797 shell->output_create_listener.notify = handle_output_create;
5798 wl_signal_add(&ec->output_created_signal,
5799 &shell->output_create_listener);
5801 shell->output_move_listener.notify = handle_output_move;
5802 wl_signal_add(&ec->output_moved_signal, &shell->output_move_listener);
5806 shell_destroy(struct wl_listener *listener, void *data)
5808 struct desktop_shell *shell =
5809 container_of(listener, struct desktop_shell, destroy_listener);
5810 struct workspace **ws;
5811 struct shell_output *shell_output, *tmp;
5813 /* Force state to unlocked so we don't try to fade */
5814 shell->locked = false;
5815 if (shell->child.client)
5816 wl_client_destroy(shell->child.client);
5818 wl_list_remove(&shell->idle_listener.link);
5819 wl_list_remove(&shell->wake_listener.link);
5821 input_panel_destroy(shell);
5823 wl_list_for_each_safe(shell_output, tmp, &shell->output_list, link) {
5824 wl_list_remove(&shell_output->destroy_listener.link);
5825 wl_list_remove(&shell_output->link);
5829 wl_list_remove(&shell->output_create_listener.link);
5831 wl_array_for_each(ws, &shell->workspaces.array)
5832 workspace_destroy(*ws);
5833 wl_array_release(&shell->workspaces.array);
5835 free(shell->screensaver.path);
5836 free(shell->client);
5841 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
5844 int i, num_workspace_bindings;
5846 /* fixed bindings */
5847 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
5848 MODIFIER_CTRL | MODIFIER_ALT,
5849 terminate_binding, ec);
5850 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
5851 click_to_activate_binding,
5853 weston_compositor_add_touch_binding(ec, 0,
5854 touch_to_activate_binding,
5856 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5857 MODIFIER_SUPER | MODIFIER_ALT,
5858 surface_opacity_binding, NULL);
5859 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
5860 MODIFIER_SUPER, zoom_axis_binding,
5863 /* configurable bindings */
5864 mod = shell->binding_modifier;
5865 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
5866 zoom_key_binding, NULL);
5867 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
5868 zoom_key_binding, NULL);
5869 weston_compositor_add_key_binding(ec, KEY_M, mod | MODIFIER_SHIFT,
5870 maximize_binding, NULL);
5871 weston_compositor_add_key_binding(ec, KEY_F, mod | MODIFIER_SHIFT,
5872 fullscreen_binding, NULL);
5873 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
5875 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
5876 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
5877 resize_binding, shell);
5878 weston_compositor_add_button_binding(ec, BTN_LEFT,
5879 mod | MODIFIER_SHIFT,
5880 resize_binding, shell);
5882 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
5883 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
5884 rotate_binding, NULL);
5886 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
5888 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
5890 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
5891 backlight_binding, ec);
5892 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
5894 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
5895 backlight_binding, ec);
5896 weston_compositor_add_key_binding(ec, KEY_K, mod,
5897 force_kill_binding, shell);
5898 weston_compositor_add_key_binding(ec, KEY_UP, mod,
5899 workspace_up_binding, shell);
5900 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
5901 workspace_down_binding, shell);
5902 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
5903 workspace_move_surface_up_binding,
5905 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
5906 workspace_move_surface_down_binding,
5909 if (shell->exposay_modifier)
5910 weston_compositor_add_modifier_binding(ec, shell->exposay_modifier,
5911 exposay_binding, shell);
5913 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
5914 if (shell->workspaces.num > 1) {
5915 num_workspace_bindings = shell->workspaces.num;
5916 if (num_workspace_bindings > 6)
5917 num_workspace_bindings = 6;
5918 for (i = 0; i < num_workspace_bindings; i++)
5919 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
5920 workspace_f_binding,
5924 /* Debug bindings */
5925 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
5926 debug_binding, shell);
5930 module_init(struct weston_compositor *ec,
5931 int *argc, char *argv[])
5933 struct weston_seat *seat;
5934 struct desktop_shell *shell;
5935 struct workspace **pws;
5937 struct wl_event_loop *loop;
5939 shell = zalloc(sizeof *shell);
5943 shell->compositor = ec;
5945 shell->destroy_listener.notify = shell_destroy;
5946 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
5947 shell->idle_listener.notify = idle_handler;
5948 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
5949 shell->wake_listener.notify = wake_handler;
5950 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
5952 ec->shell_interface.shell = shell;
5953 ec->shell_interface.create_shell_surface = create_shell_surface;
5954 ec->shell_interface.get_primary_view = get_primary_view;
5955 ec->shell_interface.set_toplevel = set_toplevel;
5956 ec->shell_interface.set_transient = set_transient;
5957 ec->shell_interface.set_fullscreen = set_fullscreen;
5958 ec->shell_interface.set_xwayland = set_xwayland;
5959 ec->shell_interface.move = surface_move;
5960 ec->shell_interface.resize = surface_resize;
5961 ec->shell_interface.set_title = set_title;
5963 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
5964 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
5965 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
5966 weston_layer_init(&shell->lock_layer, NULL);
5967 weston_layer_init(&shell->input_panel_layer, NULL);
5969 wl_array_init(&shell->workspaces.array);
5970 wl_list_init(&shell->workspaces.client_list);
5972 if (input_panel_setup(shell) < 0)
5975 shell_configuration(shell);
5977 shell->exposay.state_cur = EXPOSAY_LAYOUT_INACTIVE;
5978 shell->exposay.state_target = EXPOSAY_TARGET_CANCEL;
5980 for (i = 0; i < shell->workspaces.num; i++) {
5981 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
5985 *pws = workspace_create();
5989 activate_workspace(shell, 0);
5991 weston_layer_init(&shell->minimized_layer, NULL);
5993 wl_list_init(&shell->workspaces.anim_sticky_list);
5994 wl_list_init(&shell->workspaces.animation.link);
5995 shell->workspaces.animation.frame = animate_workspace_change_frame;
5997 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
5998 shell, bind_shell) == NULL)
6001 if (wl_global_create(ec->wl_display, &xdg_shell_interface, 1,
6002 shell, bind_xdg_shell) == NULL)
6005 if (wl_global_create(ec->wl_display,
6006 &desktop_shell_interface, 2,
6007 shell, bind_desktop_shell) == NULL)
6010 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
6011 shell, bind_screensaver) == NULL)
6014 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
6015 shell, bind_workspace_manager) == NULL)
6018 shell->child.deathstamp = weston_compositor_get_time();
6020 setup_output_destroy_handler(ec, shell);
6022 loop = wl_display_get_event_loop(ec->wl_display);
6023 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
6025 shell->screensaver.timer =
6026 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
6028 wl_list_for_each(seat, &ec->seat_list, link) {
6029 create_pointer_focus_listener(seat);
6030 create_keyboard_focus_listener(seat);
6033 shell_add_bindings(ec, shell);
6035 shell_fade_init(shell);