2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
33 #include <sys/types.h>
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "input-method-server-protocol.h"
39 #include "workspaces-server-protocol.h"
40 #include "../shared/config-parser.h"
42 #define DEFAULT_NUM_WORKSPACES 1
43 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
58 struct weston_seat *seat;
60 struct weston_surface *keyboard_focus;
62 struct wl_listener seat_destroy_listener;
63 struct wl_listener surface_destroy_listener;
67 struct weston_layer layer;
69 struct wl_list focus_list;
70 struct wl_listener seat_destroyed_listener;
73 struct input_panel_surface {
74 struct wl_resource resource;
76 struct desktop_shell *shell;
79 struct weston_surface *surface;
80 struct wl_listener surface_destroy_listener;
83 struct desktop_shell {
84 struct weston_compositor *compositor;
86 struct wl_listener idle_listener;
87 struct wl_listener wake_listener;
88 struct wl_listener destroy_listener;
89 struct wl_listener show_input_panel_listener;
90 struct wl_listener hide_input_panel_listener;
92 struct weston_layer fullscreen_layer;
93 struct weston_layer panel_layer;
94 struct weston_layer background_layer;
95 struct weston_layer lock_layer;
96 struct weston_layer input_panel_layer;
98 struct wl_listener pointer_focus_listener;
99 struct weston_surface *grab_surface;
102 struct weston_process process;
103 struct wl_client *client;
104 struct wl_resource *desktop_shell;
111 bool showing_input_panels;
112 bool prepare_event_sent;
114 struct weston_surface *lock_surface;
115 struct wl_listener lock_surface_listener;
118 struct wl_array array;
119 unsigned int current;
122 struct wl_list client_list;
124 struct weston_animation animation;
125 struct wl_list anim_sticky_list;
127 uint32_t anim_timestamp;
129 struct workspace *anim_from;
130 struct workspace *anim_to;
136 struct wl_resource *binding;
137 struct weston_process process;
138 struct wl_event_source *timer;
142 struct wl_resource *binding;
143 struct wl_list surfaces;
147 struct weston_surface *surface;
148 struct weston_surface_animation *animation;
152 uint32_t binding_modifier;
153 enum animation_type win_animation_type;
156 enum shell_surface_type {
158 SHELL_SURFACE_TOPLEVEL,
159 SHELL_SURFACE_TRANSIENT,
160 SHELL_SURFACE_FULLSCREEN,
161 SHELL_SURFACE_MAXIMIZED,
166 struct wl_event_source *source;
170 struct shell_surface {
171 struct wl_resource resource;
173 struct weston_surface *surface;
174 struct wl_listener surface_destroy_listener;
175 struct weston_surface *parent;
176 struct desktop_shell *shell;
178 enum shell_surface_type type, next_type;
180 int32_t saved_x, saved_y;
181 bool saved_position_valid;
182 bool saved_rotation_valid;
186 struct weston_transform transform;
187 struct weston_matrix rotation;
191 struct wl_pointer_grab grab;
194 struct wl_seat *seat;
204 enum wl_shell_surface_fullscreen_method type;
205 struct weston_transform transform; /* matrix from x, y */
207 struct weston_surface *black_surface;
210 struct ping_timer *ping_timer;
212 struct weston_transform workspace_transform;
214 struct weston_output *fullscreen_output;
215 struct weston_output *output;
218 const struct weston_shell_client *client;
222 struct wl_pointer_grab grab;
223 struct shell_surface *shsurf;
224 struct wl_listener shsurf_destroy_listener;
225 struct wl_pointer *pointer;
228 struct weston_move_grab {
229 struct shell_grab base;
234 struct shell_grab base;
235 struct weston_matrix rotation;
243 activate(struct desktop_shell *shell, struct weston_surface *es,
244 struct weston_seat *seat);
246 static struct workspace *
247 get_current_workspace(struct desktop_shell *shell);
249 static struct shell_surface *
250 get_shell_surface(struct weston_surface *surface);
252 static struct desktop_shell *
253 shell_surface_get_shell(struct shell_surface *shsurf);
256 surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
259 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
261 struct desktop_shell *shell;
262 struct weston_surface *top_fs_es;
264 shell = shell_surface_get_shell(shsurf);
266 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
269 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
270 struct weston_surface,
272 return (shsurf == get_shell_surface(top_fs_es));
276 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
278 struct shell_grab *grab;
280 grab = container_of(listener, struct shell_grab,
281 shsurf_destroy_listener);
287 popup_grab_end(struct wl_pointer *pointer);
290 shell_grab_start(struct shell_grab *grab,
291 const struct wl_pointer_grab_interface *interface,
292 struct shell_surface *shsurf,
293 struct wl_pointer *pointer,
294 enum desktop_shell_cursor cursor)
296 struct desktop_shell *shell = shsurf->shell;
298 popup_grab_end(pointer);
300 grab->grab.interface = interface;
301 grab->shsurf = shsurf;
302 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
303 wl_signal_add(&shsurf->resource.destroy_signal,
304 &grab->shsurf_destroy_listener);
306 grab->pointer = pointer;
307 grab->grab.focus = &shsurf->surface->surface;
309 wl_pointer_start_grab(pointer, &grab->grab);
310 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
311 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
312 wl_fixed_from_int(0), wl_fixed_from_int(0));
316 shell_grab_end(struct shell_grab *grab)
319 wl_list_remove(&grab->shsurf_destroy_listener.link);
321 wl_pointer_end_grab(grab->pointer);
325 center_on_output(struct weston_surface *surface,
326 struct weston_output *output);
328 static enum weston_keyboard_modifier
329 get_modifier(char *modifier)
332 return MODIFIER_SUPER;
334 if (!strcmp("ctrl", modifier))
335 return MODIFIER_CTRL;
336 else if (!strcmp("alt", modifier))
338 else if (!strcmp("super", modifier))
339 return MODIFIER_SUPER;
341 return MODIFIER_SUPER;
344 static enum animation_type
345 get_animation_type(char *animation)
348 return ANIMATION_NONE;
350 if (!strcmp("zoom", animation))
351 return ANIMATION_ZOOM;
352 else if (!strcmp("fade", animation))
353 return ANIMATION_FADE;
355 return ANIMATION_NONE;
359 shell_configuration(struct desktop_shell *shell, const char *config_file)
363 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
364 char *modifier = NULL;
365 char *win_animation = NULL;
367 struct config_key shell_keys[] = {
368 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
369 { "animation", CONFIG_KEY_STRING, &win_animation},
371 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
374 struct config_key saver_keys[] = {
375 { "path", CONFIG_KEY_STRING, &path },
376 { "duration", CONFIG_KEY_INTEGER, &duration },
379 struct config_section cs[] = {
380 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
381 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
384 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
386 shell->screensaver.path = path;
387 shell->screensaver.duration = duration * 1000;
388 shell->binding_modifier = get_modifier(modifier);
389 shell->win_animation_type = get_animation_type(win_animation);
390 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
394 focus_state_destroy(struct focus_state *state)
396 wl_list_remove(&state->seat_destroy_listener.link);
397 wl_list_remove(&state->surface_destroy_listener.link);
402 focus_state_seat_destroy(struct wl_listener *listener, void *data)
404 struct focus_state *state = container_of(listener,
406 seat_destroy_listener);
408 wl_list_remove(&state->link);
409 focus_state_destroy(state);
413 focus_state_surface_destroy(struct wl_listener *listener, void *data)
415 struct focus_state *state = container_of(listener,
417 surface_destroy_listener);
418 struct desktop_shell *shell;
419 struct weston_surface *surface, *next;
422 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
423 if (surface == state->keyboard_focus)
431 shell = state->seat->compositor->shell_interface.shell;
432 activate(shell, next, state->seat);
434 wl_list_remove(&state->link);
435 focus_state_destroy(state);
439 static struct focus_state *
440 focus_state_create(struct weston_seat *seat, struct workspace *ws)
442 struct focus_state *state;
444 state = malloc(sizeof *state);
450 wl_list_insert(&ws->focus_list, &state->link);
452 state->seat_destroy_listener.notify = focus_state_seat_destroy;
453 state->surface_destroy_listener.notify = focus_state_surface_destroy;
454 wl_signal_add(&seat->seat.destroy_signal,
455 &state->seat_destroy_listener);
456 wl_list_init(&state->surface_destroy_listener.link);
461 static struct focus_state *
462 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
464 struct workspace *ws = get_current_workspace(shell);
465 struct focus_state *state;
467 wl_list_for_each(state, &ws->focus_list, link)
468 if (state->seat == seat)
471 if (&state->link == &ws->focus_list)
472 state = focus_state_create(seat, ws);
478 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
480 struct focus_state *state, *next;
481 struct wl_surface *surface;
483 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
484 surface = state->keyboard_focus ?
485 &state->keyboard_focus->surface : NULL;
487 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
492 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
493 struct weston_seat *seat)
495 struct focus_state *state;
496 struct wl_surface *surface;
498 wl_list_for_each(state, &ws->focus_list, link) {
499 if (state->seat == seat) {
500 surface = seat->seat.keyboard->focus;
501 state->keyboard_focus =
502 (struct weston_surface *) surface;
509 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
510 struct weston_surface *surface)
512 struct focus_state *state;
514 wl_list_for_each(state, &ws->focus_list, link)
515 if (state->keyboard_focus == surface)
516 state->keyboard_focus = NULL;
520 workspace_destroy(struct workspace *ws)
522 struct focus_state *state, *next;
524 wl_list_for_each_safe(state, next, &ws->focus_list, link)
525 focus_state_destroy(state);
531 seat_destroyed(struct wl_listener *listener, void *data)
533 struct weston_seat *seat = data;
534 struct focus_state *state, *next;
535 struct workspace *ws = container_of(listener,
537 seat_destroyed_listener);
539 wl_list_for_each_safe(state, next, &ws->focus_list, link)
540 if (state->seat == seat)
541 wl_list_remove(&state->link);
544 static struct workspace *
545 workspace_create(void)
547 struct workspace *ws = malloc(sizeof *ws);
551 weston_layer_init(&ws->layer, NULL);
553 wl_list_init(&ws->focus_list);
554 wl_list_init(&ws->seat_destroyed_listener.link);
555 ws->seat_destroyed_listener.notify = seat_destroyed;
561 workspace_is_empty(struct workspace *ws)
563 return wl_list_empty(&ws->layer.surface_list);
566 static struct workspace *
567 get_workspace(struct desktop_shell *shell, unsigned int index)
569 struct workspace **pws = shell->workspaces.array.data;
570 assert(index < shell->workspaces.num);
575 static struct workspace *
576 get_current_workspace(struct desktop_shell *shell)
578 return get_workspace(shell, shell->workspaces.current);
582 activate_workspace(struct desktop_shell *shell, unsigned int index)
584 struct workspace *ws;
586 ws = get_workspace(shell, index);
587 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
589 shell->workspaces.current = index;
593 get_output_height(struct weston_output *output)
595 return abs(output->region.extents.y1 - output->region.extents.y2);
599 surface_translate(struct weston_surface *surface, double d)
601 struct shell_surface *shsurf = get_shell_surface(surface);
602 struct weston_transform *transform;
604 transform = &shsurf->workspace_transform;
605 if (wl_list_empty(&transform->link))
606 wl_list_insert(surface->geometry.transformation_list.prev,
607 &shsurf->workspace_transform.link);
609 weston_matrix_init(&shsurf->workspace_transform.matrix);
610 weston_matrix_translate(&shsurf->workspace_transform.matrix,
612 weston_surface_geometry_dirty(surface);
616 workspace_translate_out(struct workspace *ws, double fraction)
618 struct weston_surface *surface;
622 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
623 height = get_output_height(surface->output);
624 d = height * fraction;
626 surface_translate(surface, d);
631 workspace_translate_in(struct workspace *ws, double fraction)
633 struct weston_surface *surface;
637 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
638 height = get_output_height(surface->output);
641 d = -(height - height * fraction);
643 d = height + height * fraction;
645 surface_translate(surface, d);
650 broadcast_current_workspace_state(struct desktop_shell *shell)
652 struct wl_resource *resource;
654 wl_list_for_each(resource, &shell->workspaces.client_list, link)
655 workspace_manager_send_state(resource,
656 shell->workspaces.current,
657 shell->workspaces.num);
661 reverse_workspace_change_animation(struct desktop_shell *shell,
663 struct workspace *from,
664 struct workspace *to)
666 shell->workspaces.current = index;
668 shell->workspaces.anim_to = to;
669 shell->workspaces.anim_from = from;
670 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
671 shell->workspaces.anim_timestamp = 0;
673 weston_compositor_schedule_repaint(shell->compositor);
677 workspace_deactivate_transforms(struct workspace *ws)
679 struct weston_surface *surface;
680 struct shell_surface *shsurf;
682 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
683 shsurf = get_shell_surface(surface);
684 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
685 wl_list_remove(&shsurf->workspace_transform.link);
686 wl_list_init(&shsurf->workspace_transform.link);
688 weston_surface_geometry_dirty(surface);
693 finish_workspace_change_animation(struct desktop_shell *shell,
694 struct workspace *from,
695 struct workspace *to)
697 weston_compositor_schedule_repaint(shell->compositor);
699 wl_list_remove(&shell->workspaces.animation.link);
700 workspace_deactivate_transforms(from);
701 workspace_deactivate_transforms(to);
702 shell->workspaces.anim_to = NULL;
704 wl_list_remove(&shell->workspaces.anim_from->layer.link);
708 animate_workspace_change_frame(struct weston_animation *animation,
709 struct weston_output *output, uint32_t msecs)
711 struct desktop_shell *shell =
712 container_of(animation, struct desktop_shell,
713 workspaces.animation);
714 struct workspace *from = shell->workspaces.anim_from;
715 struct workspace *to = shell->workspaces.anim_to;
719 if (workspace_is_empty(from) && workspace_is_empty(to)) {
720 finish_workspace_change_animation(shell, from, to);
724 if (shell->workspaces.anim_timestamp == 0) {
725 if (shell->workspaces.anim_current == 0.0)
726 shell->workspaces.anim_timestamp = msecs;
728 shell->workspaces.anim_timestamp =
730 /* Invers of movement function 'y' below. */
731 (asin(1.0 - shell->workspaces.anim_current) *
732 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
736 t = msecs - shell->workspaces.anim_timestamp;
742 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
745 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
746 weston_compositor_schedule_repaint(shell->compositor);
748 workspace_translate_out(from, shell->workspaces.anim_dir * y);
749 workspace_translate_in(to, shell->workspaces.anim_dir * y);
750 shell->workspaces.anim_current = y;
752 weston_compositor_schedule_repaint(shell->compositor);
755 finish_workspace_change_animation(shell, from, to);
759 animate_workspace_change(struct desktop_shell *shell,
761 struct workspace *from,
762 struct workspace *to)
764 struct weston_output *output;
768 if (index > shell->workspaces.current)
773 shell->workspaces.current = index;
775 shell->workspaces.anim_dir = dir;
776 shell->workspaces.anim_from = from;
777 shell->workspaces.anim_to = to;
778 shell->workspaces.anim_current = 0.0;
779 shell->workspaces.anim_timestamp = 0;
781 output = container_of(shell->compositor->output_list.next,
782 struct weston_output, link);
783 wl_list_insert(&output->animation_list,
784 &shell->workspaces.animation.link);
786 wl_list_insert(from->layer.link.prev, &to->layer.link);
788 workspace_translate_in(to, 0);
790 restore_focus_state(shell, to);
792 weston_compositor_schedule_repaint(shell->compositor);
796 update_workspace(struct desktop_shell *shell, unsigned int index,
797 struct workspace *from, struct workspace *to)
799 shell->workspaces.current = index;
800 wl_list_insert(&from->layer.link, &to->layer.link);
801 wl_list_remove(&from->layer.link);
805 change_workspace(struct desktop_shell *shell, unsigned int index)
807 struct workspace *from;
808 struct workspace *to;
810 if (index == shell->workspaces.current)
813 /* Don't change workspace when there is any fullscreen surfaces. */
814 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
817 from = get_current_workspace(shell);
818 to = get_workspace(shell, index);
820 if (shell->workspaces.anim_from == to &&
821 shell->workspaces.anim_to == from) {
822 restore_focus_state(shell, to);
823 reverse_workspace_change_animation(shell, index, from, to);
824 broadcast_current_workspace_state(shell);
828 if (shell->workspaces.anim_to != NULL)
829 finish_workspace_change_animation(shell,
830 shell->workspaces.anim_from,
831 shell->workspaces.anim_to);
833 restore_focus_state(shell, to);
835 if (workspace_is_empty(to) && workspace_is_empty(from))
836 update_workspace(shell, index, from, to);
838 animate_workspace_change(shell, index, from, to);
840 broadcast_current_workspace_state(shell);
844 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
846 struct wl_list *list = &ws->layer.surface_list;
849 if (wl_list_empty(list))
857 return container_of(e, struct weston_surface, layer_link) == surface;
861 move_surface_to_workspace(struct desktop_shell *shell,
862 struct weston_surface *surface,
865 struct workspace *from;
866 struct workspace *to;
867 struct weston_seat *seat;
869 if (workspace == shell->workspaces.current)
872 if (workspace >= shell->workspaces.num)
873 workspace = shell->workspaces.num - 1;
875 from = get_current_workspace(shell);
876 to = get_workspace(shell, workspace);
878 wl_list_remove(&surface->layer_link);
879 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
881 drop_focus_state(shell, from, surface);
882 wl_list_for_each(seat, &shell->compositor->seat_list, link)
883 if (seat->has_keyboard &&
884 seat->keyboard.keyboard.focus == &surface->surface)
885 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
887 weston_surface_damage_below(surface);
891 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
892 struct wl_seat *wl_seat,
895 struct weston_seat *seat = (struct weston_seat *) wl_seat;
896 struct weston_surface *surface =
897 (struct weston_surface *) wl_seat->keyboard->focus;
898 struct shell_surface *shsurf;
899 struct workspace *from;
900 struct workspace *to;
901 struct focus_state *state;
903 if (surface == NULL ||
904 index == shell->workspaces.current)
907 from = get_current_workspace(shell);
908 to = get_workspace(shell, index);
910 wl_list_remove(&surface->layer_link);
911 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
913 replace_focus_state(shell, to, seat);
914 drop_focus_state(shell, from, surface);
916 if (shell->workspaces.anim_from == to &&
917 shell->workspaces.anim_to == from) {
918 wl_list_remove(&to->layer.link);
919 wl_list_insert(from->layer.link.prev, &to->layer.link);
921 reverse_workspace_change_animation(shell, index, from, to);
922 broadcast_current_workspace_state(shell);
927 if (shell->workspaces.anim_to != NULL)
928 finish_workspace_change_animation(shell,
929 shell->workspaces.anim_from,
930 shell->workspaces.anim_to);
932 if (workspace_is_empty(from) &&
933 workspace_has_only(to, surface))
934 update_workspace(shell, index, from, to);
936 shsurf = get_shell_surface(surface);
937 if (wl_list_empty(&shsurf->workspace_transform.link))
938 wl_list_insert(&shell->workspaces.anim_sticky_list,
939 &shsurf->workspace_transform.link);
941 animate_workspace_change(shell, index, from, to);
944 broadcast_current_workspace_state(shell);
946 state = ensure_focus_state(shell, seat);
948 state->keyboard_focus = surface;
952 workspace_manager_move_surface(struct wl_client *client,
953 struct wl_resource *resource,
954 struct wl_resource *surface_resource,
957 struct desktop_shell *shell = resource->data;
958 struct weston_surface *surface =
959 (struct weston_surface *) surface_resource;
961 move_surface_to_workspace(shell, surface, workspace);
964 static const struct workspace_manager_interface workspace_manager_implementation = {
965 workspace_manager_move_surface,
969 unbind_resource(struct wl_resource *resource)
971 wl_list_remove(&resource->link);
976 bind_workspace_manager(struct wl_client *client,
977 void *data, uint32_t version, uint32_t id)
979 struct desktop_shell *shell = data;
980 struct wl_resource *resource;
982 resource = wl_client_add_object(client, &workspace_manager_interface,
983 &workspace_manager_implementation,
986 if (resource == NULL) {
987 weston_log("couldn't add workspace manager object");
991 resource->destroy = unbind_resource;
992 wl_list_insert(&shell->workspaces.client_list, &resource->link);
994 workspace_manager_send_state(resource,
995 shell->workspaces.current,
996 shell->workspaces.num);
1000 noop_grab_focus(struct wl_pointer_grab *grab,
1001 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
1007 move_grab_motion(struct wl_pointer_grab *grab,
1008 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1010 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1011 struct wl_pointer *pointer = grab->pointer;
1012 struct shell_surface *shsurf = move->base.shsurf;
1013 struct weston_surface *es;
1014 int dx = wl_fixed_to_int(pointer->x + move->dx);
1015 int dy = wl_fixed_to_int(pointer->y + move->dy);
1020 es = shsurf->surface;
1022 weston_surface_configure(es, dx, dy,
1023 es->geometry.width, es->geometry.height);
1025 weston_compositor_schedule_repaint(es->compositor);
1029 move_grab_button(struct wl_pointer_grab *grab,
1030 uint32_t time, uint32_t button, uint32_t state_w)
1032 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1034 struct wl_pointer *pointer = grab->pointer;
1035 enum wl_pointer_button_state state = state_w;
1037 if (pointer->button_count == 0 &&
1038 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1039 shell_grab_end(shell_grab);
1044 static const struct wl_pointer_grab_interface move_grab_interface = {
1051 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1053 struct weston_move_grab *move;
1058 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1061 move = malloc(sizeof *move);
1065 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1066 ws->seat.pointer->grab_x;
1067 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1068 ws->seat.pointer->grab_y;
1070 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1071 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1077 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1078 struct wl_resource *seat_resource, uint32_t serial)
1080 struct weston_seat *ws = seat_resource->data;
1081 struct shell_surface *shsurf = resource->data;
1083 if (ws->seat.pointer->button_count == 0 ||
1084 ws->seat.pointer->grab_serial != serial ||
1085 ws->seat.pointer->focus != &shsurf->surface->surface)
1088 if (surface_move(shsurf, ws) < 0)
1089 wl_resource_post_no_memory(resource);
1092 struct weston_resize_grab {
1093 struct shell_grab base;
1095 int32_t width, height;
1099 resize_grab_motion(struct wl_pointer_grab *grab,
1100 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1102 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1103 struct wl_pointer *pointer = grab->pointer;
1104 struct shell_surface *shsurf = resize->base.shsurf;
1105 int32_t width, height;
1106 wl_fixed_t from_x, from_y;
1107 wl_fixed_t to_x, to_y;
1112 weston_surface_from_global_fixed(shsurf->surface,
1113 pointer->grab_x, pointer->grab_y,
1115 weston_surface_from_global_fixed(shsurf->surface,
1116 pointer->x, pointer->y, &to_x, &to_y);
1118 width = resize->width;
1119 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1120 width += wl_fixed_to_int(from_x - to_x);
1121 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1122 width += wl_fixed_to_int(to_x - from_x);
1125 height = resize->height;
1126 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1127 height += wl_fixed_to_int(from_y - to_y);
1128 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1129 height += wl_fixed_to_int(to_y - from_y);
1132 shsurf->client->send_configure(shsurf->surface,
1133 resize->edges, width, height);
1137 send_configure(struct weston_surface *surface,
1138 uint32_t edges, int32_t width, int32_t height)
1140 struct shell_surface *shsurf = get_shell_surface(surface);
1142 wl_shell_surface_send_configure(&shsurf->resource,
1143 edges, width, height);
1146 static const struct weston_shell_client shell_client = {
1151 resize_grab_button(struct wl_pointer_grab *grab,
1152 uint32_t time, uint32_t button, uint32_t state_w)
1154 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1155 struct wl_pointer *pointer = grab->pointer;
1156 enum wl_pointer_button_state state = state_w;
1158 if (pointer->button_count == 0 &&
1159 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1160 shell_grab_end(&resize->base);
1165 static const struct wl_pointer_grab_interface resize_grab_interface = {
1172 surface_resize(struct shell_surface *shsurf,
1173 struct weston_seat *ws, uint32_t edges)
1175 struct weston_resize_grab *resize;
1177 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1180 if (edges == 0 || edges > 15 ||
1181 (edges & 3) == 3 || (edges & 12) == 12)
1184 resize = malloc(sizeof *resize);
1188 resize->edges = edges;
1189 resize->width = shsurf->surface->geometry.width;
1190 resize->height = shsurf->surface->geometry.height;
1192 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1193 ws->seat.pointer, edges);
1199 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1200 struct wl_resource *seat_resource, uint32_t serial,
1203 struct weston_seat *ws = seat_resource->data;
1204 struct shell_surface *shsurf = resource->data;
1206 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1209 if (ws->seat.pointer->button_count == 0 ||
1210 ws->seat.pointer->grab_serial != serial ||
1211 ws->seat.pointer->focus != &shsurf->surface->surface)
1214 if (surface_resize(shsurf, ws, edges) < 0)
1215 wl_resource_post_no_memory(resource);
1219 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1220 struct wl_surface *surface, int32_t x, int32_t y)
1222 struct shell_grab *grab = (struct shell_grab *) base;
1224 if (grab->grab.focus != surface) {
1225 shell_grab_end(grab);
1231 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1232 uint32_t time, int32_t x, int32_t y)
1237 busy_cursor_grab_button(struct wl_pointer_grab *base,
1238 uint32_t time, uint32_t button, uint32_t state)
1240 struct shell_grab *grab = (struct shell_grab *) base;
1241 struct shell_surface *shsurf;
1242 struct weston_surface *surface =
1243 (struct weston_surface *) grab->grab.pointer->current;
1244 struct weston_seat *seat =
1245 (struct weston_seat *) grab->grab.pointer->seat;
1247 shsurf = get_shell_surface(surface);
1248 if (shsurf && button == BTN_LEFT && state) {
1249 activate(shsurf->shell, shsurf->surface, seat);
1250 surface_move(shsurf, seat);
1251 } else if (shsurf && button == BTN_RIGHT && state) {
1252 activate(shsurf->shell, shsurf->surface, seat);
1253 surface_rotate(shsurf, &seat->seat);
1257 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1258 busy_cursor_grab_focus,
1259 busy_cursor_grab_motion,
1260 busy_cursor_grab_button,
1264 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1266 struct shell_grab *grab;
1268 grab = malloc(sizeof *grab);
1272 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1273 DESKTOP_SHELL_CURSOR_BUSY);
1277 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1279 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1281 if (grab->grab.interface == &busy_cursor_grab_interface) {
1282 shell_grab_end(grab);
1288 ping_timer_destroy(struct shell_surface *shsurf)
1290 if (!shsurf || !shsurf->ping_timer)
1293 if (shsurf->ping_timer->source)
1294 wl_event_source_remove(shsurf->ping_timer->source);
1296 free(shsurf->ping_timer);
1297 shsurf->ping_timer = NULL;
1301 ping_timeout_handler(void *data)
1303 struct shell_surface *shsurf = data;
1304 struct weston_seat *seat;
1306 /* Client is not responding */
1307 shsurf->unresponsive = 1;
1309 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1310 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1311 set_busy_cursor(shsurf, seat->seat.pointer);
1317 ping_handler(struct weston_surface *surface, uint32_t serial)
1319 struct shell_surface *shsurf = get_shell_surface(surface);
1320 struct wl_event_loop *loop;
1321 int ping_timeout = 200;
1325 if (!shsurf->resource.client)
1328 if (shsurf->surface == shsurf->shell->grab_surface)
1331 if (!shsurf->ping_timer) {
1332 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1333 if (!shsurf->ping_timer)
1336 shsurf->ping_timer->serial = serial;
1337 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1338 shsurf->ping_timer->source =
1339 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1340 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1342 wl_shell_surface_send_ping(&shsurf->resource, serial);
1347 handle_pointer_focus(struct wl_listener *listener, void *data)
1349 struct wl_pointer *pointer = data;
1350 struct weston_surface *surface =
1351 (struct weston_surface *) pointer->focus;
1352 struct weston_compositor *compositor;
1353 struct shell_surface *shsurf;
1359 compositor = surface->compositor;
1360 shsurf = get_shell_surface(surface);
1362 if (shsurf && shsurf->unresponsive) {
1363 set_busy_cursor(shsurf, pointer);
1365 serial = wl_display_next_serial(compositor->wl_display);
1366 ping_handler(surface, serial);
1371 create_pointer_focus_listener(struct weston_seat *seat)
1373 struct wl_listener *listener;
1375 if (!seat->seat.pointer)
1378 listener = malloc(sizeof *listener);
1379 listener->notify = handle_pointer_focus;
1380 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1384 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1387 struct shell_surface *shsurf = resource->data;
1388 struct desktop_shell *shell = shsurf->shell;
1389 struct weston_seat *seat;
1390 struct weston_compositor *ec = shsurf->surface->compositor;
1391 struct wl_pointer *pointer;
1392 int was_unresponsive;
1394 if (shsurf->ping_timer == NULL)
1395 /* Just ignore unsolicited pong. */
1398 if (shsurf->ping_timer->serial == serial) {
1399 was_unresponsive = shsurf->unresponsive;
1400 shsurf->unresponsive = 0;
1401 if (was_unresponsive) {
1402 /* Received pong from previously unresponsive client */
1403 wl_list_for_each(seat, &ec->seat_list, link) {
1404 pointer = seat->seat.pointer;
1405 if (pointer->focus ==
1406 &shell->grab_surface->surface &&
1408 &shsurf->surface->surface)
1409 end_busy_cursor(shsurf, pointer);
1412 ping_timer_destroy(shsurf);
1417 shell_surface_set_title(struct wl_client *client,
1418 struct wl_resource *resource, const char *title)
1420 struct shell_surface *shsurf = resource->data;
1422 free(shsurf->title);
1423 shsurf->title = strdup(title);
1427 shell_surface_set_class(struct wl_client *client,
1428 struct wl_resource *resource, const char *class)
1430 struct shell_surface *shsurf = resource->data;
1432 free(shsurf->class);
1433 shsurf->class = strdup(class);
1436 static struct weston_output *
1437 get_default_output(struct weston_compositor *compositor)
1439 return container_of(compositor->output_list.next,
1440 struct weston_output, link);
1444 shell_unset_fullscreen(struct shell_surface *shsurf)
1446 struct workspace *ws;
1447 /* undo all fullscreen things here */
1448 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1449 shell_surface_is_top_fullscreen(shsurf)) {
1450 weston_output_switch_mode(shsurf->fullscreen_output,
1451 shsurf->fullscreen_output->origin);
1453 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1454 shsurf->fullscreen.framerate = 0;
1455 wl_list_remove(&shsurf->fullscreen.transform.link);
1456 wl_list_init(&shsurf->fullscreen.transform.link);
1457 if (shsurf->fullscreen.black_surface)
1458 weston_surface_destroy(shsurf->fullscreen.black_surface);
1459 shsurf->fullscreen.black_surface = NULL;
1460 shsurf->fullscreen_output = NULL;
1461 weston_surface_set_position(shsurf->surface,
1462 shsurf->saved_x, shsurf->saved_y);
1463 if (shsurf->saved_rotation_valid) {
1464 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1465 &shsurf->rotation.transform.link);
1466 shsurf->saved_rotation_valid = false;
1469 ws = get_current_workspace(shsurf->shell);
1470 wl_list_remove(&shsurf->surface->layer_link);
1471 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1475 reset_shell_surface_type(struct shell_surface *surface)
1477 switch (surface->type) {
1478 case SHELL_SURFACE_FULLSCREEN:
1479 shell_unset_fullscreen(surface);
1481 case SHELL_SURFACE_MAXIMIZED:
1482 surface->output = get_default_output(surface->surface->compositor);
1483 weston_surface_set_position(surface->surface,
1487 case SHELL_SURFACE_NONE:
1488 case SHELL_SURFACE_TOPLEVEL:
1489 case SHELL_SURFACE_TRANSIENT:
1490 case SHELL_SURFACE_POPUP:
1494 surface->type = SHELL_SURFACE_NONE;
1499 set_surface_type(struct shell_surface *shsurf)
1501 struct weston_surface *surface = shsurf->surface;
1502 struct weston_surface *pes = shsurf->parent;
1504 reset_shell_surface_type(shsurf);
1506 shsurf->type = shsurf->next_type;
1507 shsurf->next_type = SHELL_SURFACE_NONE;
1509 switch (shsurf->type) {
1510 case SHELL_SURFACE_TOPLEVEL:
1512 case SHELL_SURFACE_TRANSIENT:
1513 weston_surface_set_position(surface,
1514 pes->geometry.x + shsurf->transient.x,
1515 pes->geometry.y + shsurf->transient.y);
1518 case SHELL_SURFACE_MAXIMIZED:
1519 shsurf->saved_x = surface->geometry.x;
1520 shsurf->saved_y = surface->geometry.y;
1521 shsurf->saved_position_valid = true;
1524 case SHELL_SURFACE_FULLSCREEN:
1525 shsurf->saved_x = surface->geometry.x;
1526 shsurf->saved_y = surface->geometry.y;
1527 shsurf->saved_position_valid = true;
1529 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1530 wl_list_remove(&shsurf->rotation.transform.link);
1531 wl_list_init(&shsurf->rotation.transform.link);
1532 weston_surface_geometry_dirty(shsurf->surface);
1533 shsurf->saved_rotation_valid = true;
1543 set_toplevel(struct shell_surface *shsurf)
1545 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1549 shell_surface_set_toplevel(struct wl_client *client,
1550 struct wl_resource *resource)
1552 struct shell_surface *surface = resource->data;
1554 set_toplevel(surface);
1558 set_transient(struct shell_surface *shsurf,
1559 struct weston_surface *parent, int x, int y, uint32_t flags)
1561 /* assign to parents output */
1562 shsurf->parent = parent;
1563 shsurf->transient.x = x;
1564 shsurf->transient.y = y;
1565 shsurf->transient.flags = flags;
1566 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1570 shell_surface_set_transient(struct wl_client *client,
1571 struct wl_resource *resource,
1572 struct wl_resource *parent_resource,
1573 int x, int y, uint32_t flags)
1575 struct shell_surface *shsurf = resource->data;
1576 struct weston_surface *parent = parent_resource->data;
1578 set_transient(shsurf, parent, x, y, flags);
1581 static struct desktop_shell *
1582 shell_surface_get_shell(struct shell_surface *shsurf)
1584 return shsurf->shell;
1588 get_output_panel_height(struct desktop_shell *shell,
1589 struct weston_output *output)
1591 struct weston_surface *surface;
1592 int panel_height = 0;
1597 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1598 if (surface->output == output) {
1599 panel_height = surface->geometry.height;
1604 return panel_height;
1608 shell_surface_set_maximized(struct wl_client *client,
1609 struct wl_resource *resource,
1610 struct wl_resource *output_resource )
1612 struct shell_surface *shsurf = resource->data;
1613 struct weston_surface *es = shsurf->surface;
1614 struct desktop_shell *shell = NULL;
1615 uint32_t edges = 0, panel_height = 0;
1617 /* get the default output, if the client set it as NULL
1618 check whether the ouput is available */
1619 if (output_resource)
1620 shsurf->output = output_resource->data;
1621 else if (es->output)
1622 shsurf->output = es->output;
1624 shsurf->output = get_default_output(es->compositor);
1626 shell = shell_surface_get_shell(shsurf);
1627 panel_height = get_output_panel_height(shell, shsurf->output);
1628 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1630 shsurf->client->send_configure(shsurf->surface, edges,
1631 shsurf->output->width,
1632 shsurf->output->height - panel_height);
1634 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1638 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
1640 static struct weston_surface *
1641 create_black_surface(struct weston_compositor *ec,
1642 struct weston_surface *fs_surface,
1643 float x, float y, int w, int h)
1645 struct weston_surface *surface = NULL;
1647 surface = weston_surface_create(ec);
1648 if (surface == NULL) {
1649 weston_log("no memory\n");
1653 surface->configure = black_surface_configure;
1654 surface->private = fs_surface;
1655 weston_surface_configure(surface, x, y, w, h);
1656 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1657 pixman_region32_fini(&surface->opaque);
1658 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1659 pixman_region32_fini(&surface->input);
1660 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
1665 /* Create black surface and append it to the associated fullscreen surface.
1666 * Handle size dismatch and positioning according to the method. */
1668 shell_configure_fullscreen(struct shell_surface *shsurf)
1670 struct weston_output *output = shsurf->fullscreen_output;
1671 struct weston_surface *surface = shsurf->surface;
1672 struct weston_matrix *matrix;
1673 float scale, output_aspect, surface_aspect, x, y;
1675 if (!shsurf->fullscreen.black_surface)
1676 shsurf->fullscreen.black_surface =
1677 create_black_surface(surface->compositor,
1679 output->x, output->y,
1683 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1684 wl_list_insert(&surface->layer_link,
1685 &shsurf->fullscreen.black_surface->layer_link);
1686 shsurf->fullscreen.black_surface->output = output;
1688 switch (shsurf->fullscreen.type) {
1689 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1690 if (surface->buffer_ref.buffer)
1691 center_on_output(surface, shsurf->fullscreen_output);
1693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1694 /* 1:1 mapping between surface and output dimensions */
1695 if (output->width == surface->geometry.width &&
1696 output->height == surface->geometry.height) {
1697 weston_surface_set_position(surface, output->x, output->y);
1701 matrix = &shsurf->fullscreen.transform.matrix;
1702 weston_matrix_init(matrix);
1704 output_aspect = (float) output->width /
1705 (float) output->height;
1706 surface_aspect = (float) surface->geometry.width /
1707 (float) surface->geometry.height;
1708 if (output_aspect < surface_aspect)
1709 scale = (float) output->width /
1710 (float) surface->geometry.width;
1712 scale = (float) output->height /
1713 (float) surface->geometry.height;
1715 weston_matrix_scale(matrix, scale, scale, 1);
1716 wl_list_remove(&shsurf->fullscreen.transform.link);
1717 wl_list_insert(&surface->geometry.transformation_list,
1718 &shsurf->fullscreen.transform.link);
1719 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1720 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1721 weston_surface_set_position(surface, x, y);
1724 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1725 if (shell_surface_is_top_fullscreen(shsurf)) {
1726 struct weston_mode mode = {0,
1727 surface->geometry.width,
1728 surface->geometry.height,
1729 shsurf->fullscreen.framerate};
1731 if (weston_output_switch_mode(output, &mode) == 0) {
1732 weston_surface_configure(shsurf->fullscreen.black_surface,
1733 output->x, output->y,
1736 weston_surface_set_position(surface, output->x, output->y);
1741 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1748 /* make the fullscreen and black surface at the top */
1750 shell_stack_fullscreen(struct shell_surface *shsurf)
1752 struct weston_output *output = shsurf->fullscreen_output;
1753 struct weston_surface *surface = shsurf->surface;
1754 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1756 wl_list_remove(&surface->layer_link);
1757 wl_list_insert(&shell->fullscreen_layer.surface_list,
1758 &surface->layer_link);
1759 weston_surface_damage(surface);
1761 if (!shsurf->fullscreen.black_surface)
1762 shsurf->fullscreen.black_surface =
1763 create_black_surface(surface->compositor,
1765 output->x, output->y,
1769 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1770 wl_list_insert(&surface->layer_link,
1771 &shsurf->fullscreen.black_surface->layer_link);
1772 weston_surface_damage(shsurf->fullscreen.black_surface);
1776 shell_map_fullscreen(struct shell_surface *shsurf)
1778 shell_stack_fullscreen(shsurf);
1779 shell_configure_fullscreen(shsurf);
1783 set_fullscreen(struct shell_surface *shsurf,
1786 struct weston_output *output)
1788 struct weston_surface *es = shsurf->surface;
1791 shsurf->output = output;
1792 else if (es->output)
1793 shsurf->output = es->output;
1795 shsurf->output = get_default_output(es->compositor);
1797 shsurf->fullscreen_output = shsurf->output;
1798 shsurf->fullscreen.type = method;
1799 shsurf->fullscreen.framerate = framerate;
1800 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1802 shsurf->client->send_configure(shsurf->surface, 0,
1803 shsurf->output->width,
1804 shsurf->output->height);
1808 shell_surface_set_fullscreen(struct wl_client *client,
1809 struct wl_resource *resource,
1812 struct wl_resource *output_resource)
1814 struct shell_surface *shsurf = resource->data;
1815 struct weston_output *output;
1817 if (output_resource)
1818 output = output_resource->data;
1822 set_fullscreen(shsurf, method, framerate, output);
1826 popup_grab_focus(struct wl_pointer_grab *grab,
1827 struct wl_surface *surface,
1831 struct wl_pointer *pointer = grab->pointer;
1832 struct shell_surface *priv =
1833 container_of(grab, struct shell_surface, popup.grab);
1834 struct wl_client *client = priv->surface->surface.resource.client;
1836 if (surface && surface->resource.client == client) {
1837 wl_pointer_set_focus(pointer, surface, x, y);
1838 grab->focus = surface;
1840 wl_pointer_set_focus(pointer, NULL,
1841 wl_fixed_from_int(0),
1842 wl_fixed_from_int(0));
1848 popup_grab_motion(struct wl_pointer_grab *grab,
1849 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1851 struct wl_resource *resource;
1853 resource = grab->pointer->focus_resource;
1855 wl_pointer_send_motion(resource, time, sx, sy);
1859 popup_grab_button(struct wl_pointer_grab *grab,
1860 uint32_t time, uint32_t button, uint32_t state_w)
1862 struct wl_resource *resource;
1863 struct shell_surface *shsurf =
1864 container_of(grab, struct shell_surface, popup.grab);
1865 struct wl_display *display;
1866 enum wl_pointer_button_state state = state_w;
1869 resource = grab->pointer->focus_resource;
1871 display = wl_client_get_display(resource->client);
1872 serial = wl_display_get_serial(display);
1873 wl_pointer_send_button(resource, serial, time, button, state);
1874 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1875 (shsurf->popup.initial_up ||
1876 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1877 popup_grab_end(grab->pointer);
1880 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1881 shsurf->popup.initial_up = 1;
1884 static const struct wl_pointer_grab_interface popup_grab_interface = {
1891 popup_grab_end(struct wl_pointer *pointer)
1893 struct wl_pointer_grab *grab = pointer->grab;
1894 struct shell_surface *shsurf =
1895 container_of(grab, struct shell_surface, popup.grab);
1897 if (pointer->grab->interface == &popup_grab_interface) {
1898 wl_shell_surface_send_popup_done(&shsurf->resource);
1899 wl_pointer_end_grab(grab->pointer);
1900 shsurf->popup.grab.pointer = NULL;
1905 shell_map_popup(struct shell_surface *shsurf)
1907 struct wl_seat *seat = shsurf->popup.seat;
1908 struct weston_surface *es = shsurf->surface;
1909 struct weston_surface *parent = shsurf->parent;
1911 es->output = parent->output;
1912 shsurf->popup.grab.interface = &popup_grab_interface;
1914 shsurf->popup.initial_up = 0;
1915 weston_surface_set_transform_parent(es, parent);
1916 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1917 weston_surface_update_transform(es);
1919 /* We don't require the grab to still be active, but if another
1920 * grab has started in the meantime, we end the popup now. */
1921 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1922 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1924 wl_shell_surface_send_popup_done(&shsurf->resource);
1929 shell_surface_set_popup(struct wl_client *client,
1930 struct wl_resource *resource,
1931 struct wl_resource *seat_resource,
1933 struct wl_resource *parent_resource,
1934 int32_t x, int32_t y, uint32_t flags)
1936 struct shell_surface *shsurf = resource->data;
1938 shsurf->type = SHELL_SURFACE_POPUP;
1939 shsurf->parent = parent_resource->data;
1940 shsurf->popup.seat = seat_resource->data;
1941 shsurf->popup.serial = serial;
1942 shsurf->popup.x = x;
1943 shsurf->popup.y = y;
1946 static const struct wl_shell_surface_interface shell_surface_implementation = {
1949 shell_surface_resize,
1950 shell_surface_set_toplevel,
1951 shell_surface_set_transient,
1952 shell_surface_set_fullscreen,
1953 shell_surface_set_popup,
1954 shell_surface_set_maximized,
1955 shell_surface_set_title,
1956 shell_surface_set_class
1960 destroy_shell_surface(struct shell_surface *shsurf)
1962 if (shsurf->popup.grab.pointer)
1963 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1965 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1966 shell_surface_is_top_fullscreen(shsurf)) {
1967 weston_output_switch_mode(shsurf->fullscreen_output,
1968 shsurf->fullscreen_output->origin);
1971 if (shsurf->fullscreen.black_surface)
1972 weston_surface_destroy(shsurf->fullscreen.black_surface);
1974 /* As destroy_resource() use wl_list_for_each_safe(),
1975 * we can always remove the listener.
1977 wl_list_remove(&shsurf->surface_destroy_listener.link);
1978 shsurf->surface->configure = NULL;
1979 ping_timer_destroy(shsurf);
1980 free(shsurf->title);
1982 wl_list_remove(&shsurf->link);
1987 shell_destroy_shell_surface(struct wl_resource *resource)
1989 struct shell_surface *shsurf = resource->data;
1991 destroy_shell_surface(shsurf);
1995 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1997 struct shell_surface *shsurf = container_of(listener,
1998 struct shell_surface,
1999 surface_destroy_listener);
2001 if (shsurf->resource.client) {
2002 wl_resource_destroy(&shsurf->resource);
2004 wl_signal_emit(&shsurf->resource.destroy_signal,
2006 destroy_shell_surface(shsurf);
2011 shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
2013 static struct shell_surface *
2014 get_shell_surface(struct weston_surface *surface)
2016 if (surface->configure == shell_surface_configure)
2017 return surface->private;
2022 static struct shell_surface *
2023 create_shell_surface(void *shell, struct weston_surface *surface,
2024 const struct weston_shell_client *client)
2026 struct shell_surface *shsurf;
2028 if (surface->configure) {
2029 weston_log("surface->configure already set\n");
2033 shsurf = calloc(1, sizeof *shsurf);
2035 weston_log("no memory to allocate shell surface\n");
2039 surface->configure = shell_surface_configure;
2040 surface->private = shsurf;
2042 shsurf->shell = (struct desktop_shell *) shell;
2043 shsurf->unresponsive = 0;
2044 shsurf->saved_position_valid = false;
2045 shsurf->saved_rotation_valid = false;
2046 shsurf->surface = surface;
2047 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2048 shsurf->fullscreen.framerate = 0;
2049 shsurf->fullscreen.black_surface = NULL;
2050 shsurf->ping_timer = NULL;
2051 wl_list_init(&shsurf->fullscreen.transform.link);
2053 wl_signal_init(&shsurf->resource.destroy_signal);
2054 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2055 wl_signal_add(&surface->surface.resource.destroy_signal,
2056 &shsurf->surface_destroy_listener);
2058 /* init link so its safe to always remove it in destroy_shell_surface */
2059 wl_list_init(&shsurf->link);
2061 /* empty when not in use */
2062 wl_list_init(&shsurf->rotation.transform.link);
2063 weston_matrix_init(&shsurf->rotation.rotation);
2065 wl_list_init(&shsurf->workspace_transform.link);
2067 shsurf->type = SHELL_SURFACE_NONE;
2068 shsurf->next_type = SHELL_SURFACE_NONE;
2070 shsurf->client = client;
2076 shell_get_shell_surface(struct wl_client *client,
2077 struct wl_resource *resource,
2079 struct wl_resource *surface_resource)
2081 struct weston_surface *surface = surface_resource->data;
2082 struct desktop_shell *shell = resource->data;
2083 struct shell_surface *shsurf;
2085 if (get_shell_surface(surface)) {
2086 wl_resource_post_error(surface_resource,
2087 WL_DISPLAY_ERROR_INVALID_OBJECT,
2088 "desktop_shell::get_shell_surface already requested");
2092 shsurf = create_shell_surface(shell, surface, &shell_client);
2094 wl_resource_post_error(surface_resource,
2095 WL_DISPLAY_ERROR_INVALID_OBJECT,
2096 "surface->configure already set");
2100 shsurf->resource.destroy = shell_destroy_shell_surface;
2101 shsurf->resource.object.id = id;
2102 shsurf->resource.object.interface = &wl_shell_surface_interface;
2103 shsurf->resource.object.implementation =
2104 (void (**)(void)) &shell_surface_implementation;
2105 shsurf->resource.data = shsurf;
2107 wl_client_add_resource(client, &shsurf->resource);
2110 static const struct wl_shell_interface shell_implementation = {
2111 shell_get_shell_surface
2115 shell_fade(struct desktop_shell *shell, enum fade_type type);
2118 screensaver_timeout(void *data)
2120 struct desktop_shell *shell = data;
2122 shell_fade(shell, FADE_OUT);
2128 handle_screensaver_sigchld(struct weston_process *proc, int status)
2130 struct desktop_shell *shell =
2131 container_of(proc, struct desktop_shell, screensaver.process);
2136 weston_compositor_sleep(shell->compositor);
2140 launch_screensaver(struct desktop_shell *shell)
2142 if (shell->screensaver.binding)
2145 if (!shell->screensaver.path) {
2146 weston_compositor_sleep(shell->compositor);
2150 if (shell->screensaver.process.pid != 0) {
2151 weston_log("old screensaver still running\n");
2155 weston_client_launch(shell->compositor,
2156 &shell->screensaver.process,
2157 shell->screensaver.path,
2158 handle_screensaver_sigchld);
2162 terminate_screensaver(struct desktop_shell *shell)
2164 if (shell->screensaver.process.pid == 0)
2167 kill(shell->screensaver.process.pid, SIGTERM);
2171 configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
2173 struct weston_surface *s, *next;
2178 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2179 if (s->output == es->output && s != es) {
2180 weston_surface_unmap(s);
2181 s->configure = NULL;
2185 weston_surface_configure(es, es->output->x, es->output->y, width, height);
2187 if (wl_list_empty(&es->layer_link)) {
2188 wl_list_insert(&layer->surface_list, &es->layer_link);
2189 weston_compositor_schedule_repaint(es->compositor);
2194 background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2196 struct desktop_shell *shell = es->private;
2198 configure_static_surface(es, &shell->background_layer, width, height);
2202 desktop_shell_set_background(struct wl_client *client,
2203 struct wl_resource *resource,
2204 struct wl_resource *output_resource,
2205 struct wl_resource *surface_resource)
2207 struct desktop_shell *shell = resource->data;
2208 struct weston_surface *surface = surface_resource->data;
2210 if (surface->configure) {
2211 wl_resource_post_error(surface_resource,
2212 WL_DISPLAY_ERROR_INVALID_OBJECT,
2213 "surface role already assigned");
2217 surface->configure = background_configure;
2218 surface->private = shell;
2219 surface->output = output_resource->data;
2220 desktop_shell_send_configure(resource, 0,
2222 surface->output->width,
2223 surface->output->height);
2227 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2229 struct desktop_shell *shell = es->private;
2231 configure_static_surface(es, &shell->panel_layer, width, height);
2235 desktop_shell_set_panel(struct wl_client *client,
2236 struct wl_resource *resource,
2237 struct wl_resource *output_resource,
2238 struct wl_resource *surface_resource)
2240 struct desktop_shell *shell = resource->data;
2241 struct weston_surface *surface = surface_resource->data;
2243 if (surface->configure) {
2244 wl_resource_post_error(surface_resource,
2245 WL_DISPLAY_ERROR_INVALID_OBJECT,
2246 "surface role already assigned");
2250 surface->configure = panel_configure;
2251 surface->private = shell;
2252 surface->output = output_resource->data;
2253 desktop_shell_send_configure(resource, 0,
2255 surface->output->width,
2256 surface->output->height);
2260 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
2262 struct desktop_shell *shell = surface->private;
2267 center_on_output(surface, get_default_output(shell->compositor));
2269 if (!weston_surface_is_mapped(surface)) {
2270 wl_list_insert(&shell->lock_layer.surface_list,
2271 &surface->layer_link);
2272 weston_surface_update_transform(surface);
2273 shell_fade(shell, FADE_IN);
2278 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2280 struct desktop_shell *shell =
2281 container_of(listener, struct desktop_shell, lock_surface_listener);
2283 weston_log("lock surface gone\n");
2284 shell->lock_surface = NULL;
2288 desktop_shell_set_lock_surface(struct wl_client *client,
2289 struct wl_resource *resource,
2290 struct wl_resource *surface_resource)
2292 struct desktop_shell *shell = resource->data;
2293 struct weston_surface *surface = surface_resource->data;
2295 shell->prepare_event_sent = false;
2300 shell->lock_surface = surface;
2302 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2303 wl_signal_add(&surface_resource->destroy_signal,
2304 &shell->lock_surface_listener);
2306 surface->configure = lock_surface_configure;
2307 surface->private = shell;
2311 resume_desktop(struct desktop_shell *shell)
2313 struct workspace *ws = get_current_workspace(shell);
2315 terminate_screensaver(shell);
2317 wl_list_remove(&shell->lock_layer.link);
2318 wl_list_insert(&shell->compositor->cursor_layer.link,
2319 &shell->fullscreen_layer.link);
2320 wl_list_insert(&shell->fullscreen_layer.link,
2321 &shell->panel_layer.link);
2322 if (shell->showing_input_panels) {
2323 wl_list_insert(&shell->panel_layer.link,
2324 &shell->input_panel_layer.link);
2325 wl_list_insert(&shell->input_panel_layer.link,
2328 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2331 restore_focus_state(shell, get_current_workspace(shell));
2333 shell->locked = false;
2334 shell_fade(shell, FADE_IN);
2335 weston_compositor_damage_all(shell->compositor);
2339 desktop_shell_unlock(struct wl_client *client,
2340 struct wl_resource *resource)
2342 struct desktop_shell *shell = resource->data;
2344 shell->prepare_event_sent = false;
2347 resume_desktop(shell);
2351 desktop_shell_set_grab_surface(struct wl_client *client,
2352 struct wl_resource *resource,
2353 struct wl_resource *surface_resource)
2355 struct desktop_shell *shell = resource->data;
2357 shell->grab_surface = surface_resource->data;
2360 static const struct desktop_shell_interface desktop_shell_implementation = {
2361 desktop_shell_set_background,
2362 desktop_shell_set_panel,
2363 desktop_shell_set_lock_surface,
2364 desktop_shell_unlock,
2365 desktop_shell_set_grab_surface
2368 static enum shell_surface_type
2369 get_shell_surface_type(struct weston_surface *surface)
2371 struct shell_surface *shsurf;
2373 shsurf = get_shell_surface(surface);
2375 return SHELL_SURFACE_NONE;
2376 return shsurf->type;
2380 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2382 struct weston_surface *surface =
2383 (struct weston_surface *) seat->pointer->focus;
2384 struct shell_surface *shsurf;
2386 if (surface == NULL)
2389 shsurf = get_shell_surface(surface);
2390 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2393 surface_move(shsurf, (struct weston_seat *) seat);
2397 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2399 struct weston_surface *surface =
2400 (struct weston_surface *) seat->pointer->focus;
2403 struct shell_surface *shsurf;
2405 if (surface == NULL)
2408 shsurf = get_shell_surface(surface);
2409 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2412 weston_surface_from_global(surface,
2413 wl_fixed_to_int(seat->pointer->grab_x),
2414 wl_fixed_to_int(seat->pointer->grab_y),
2417 if (x < surface->geometry.width / 3)
2418 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2419 else if (x < 2 * surface->geometry.width / 3)
2422 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2424 if (y < surface->geometry.height / 3)
2425 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2426 else if (y < 2 * surface->geometry.height / 3)
2429 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2431 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2435 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2436 wl_fixed_t value, void *data)
2439 struct shell_surface *shsurf;
2440 struct weston_surface *surface =
2441 (struct weston_surface *) seat->pointer->focus;
2443 if (surface == NULL)
2446 shsurf = get_shell_surface(surface);
2450 surface->alpha -= wl_fixed_to_double(value) * step;
2452 if (surface->alpha > 1.0)
2453 surface->alpha = 1.0;
2454 if (surface->alpha < step)
2455 surface->alpha = step;
2457 weston_surface_geometry_dirty(surface);
2458 weston_surface_damage(surface);
2462 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2465 struct weston_seat *ws = (struct weston_seat *) seat;
2466 struct weston_compositor *compositor = ws->compositor;
2467 struct weston_output *output;
2470 wl_list_for_each(output, &compositor->output_list, link) {
2471 if (pixman_region32_contains_point(&output->region,
2472 wl_fixed_to_double(seat->pointer->x),
2473 wl_fixed_to_double(seat->pointer->y),
2475 if (key == KEY_PAGEUP)
2476 increment = output->zoom.increment;
2477 else if (key == KEY_PAGEDOWN)
2478 increment = -output->zoom.increment;
2479 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2480 /* For every pixel zoom 20th of a step */
2481 increment = output->zoom.increment *
2482 -wl_fixed_to_double(value) / 20.0;
2486 output->zoom.level += increment;
2488 if (output->zoom.level < 0.0)
2489 output->zoom.level = 0.0;
2490 else if (output->zoom.level > output->zoom.max_level)
2491 output->zoom.level = output->zoom.max_level;
2492 else if (!output->zoom.active) {
2493 output->zoom.active = 1;
2494 output->disable_planes++;
2497 output->zoom.spring_z.target = output->zoom.level;
2499 weston_output_update_zoom(output, output->zoom.type);
2505 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2506 wl_fixed_t value, void *data)
2508 do_zoom(seat, time, 0, axis, value);
2512 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2515 do_zoom(seat, time, key, 0, 0);
2519 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2522 struct weston_compositor *compositor = data;
2524 wl_display_terminate(compositor->wl_display);
2528 rotate_grab_motion(struct wl_pointer_grab *grab,
2529 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2531 struct rotate_grab *rotate =
2532 container_of(grab, struct rotate_grab, base.grab);
2533 struct wl_pointer *pointer = grab->pointer;
2534 struct shell_surface *shsurf = rotate->base.shsurf;
2535 struct weston_surface *surface;
2536 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2541 surface = shsurf->surface;
2543 cx = 0.5f * surface->geometry.width;
2544 cy = 0.5f * surface->geometry.height;
2546 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2547 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2548 r = sqrtf(dx * dx + dy * dy);
2550 wl_list_remove(&shsurf->rotation.transform.link);
2551 weston_surface_geometry_dirty(shsurf->surface);
2554 struct weston_matrix *matrix =
2555 &shsurf->rotation.transform.matrix;
2557 weston_matrix_init(&rotate->rotation);
2558 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
2560 weston_matrix_init(matrix);
2561 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2562 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2563 weston_matrix_multiply(matrix, &rotate->rotation);
2564 weston_matrix_translate(matrix, cx, cy, 0.0f);
2567 &shsurf->surface->geometry.transformation_list,
2568 &shsurf->rotation.transform.link);
2570 wl_list_init(&shsurf->rotation.transform.link);
2571 weston_matrix_init(&shsurf->rotation.rotation);
2572 weston_matrix_init(&rotate->rotation);
2575 /* We need to adjust the position of the surface
2576 * in case it was resized in a rotated state before */
2577 cposx = surface->geometry.x + cx;
2578 cposy = surface->geometry.y + cy;
2579 dposx = rotate->center.x - cposx;
2580 dposy = rotate->center.y - cposy;
2581 if (dposx != 0.0f || dposy != 0.0f) {
2582 weston_surface_set_position(surface,
2583 surface->geometry.x + dposx,
2584 surface->geometry.y + dposy);
2587 /* Repaint implies weston_surface_update_transform(), which
2588 * lazily applies the damage due to rotation update.
2590 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2594 rotate_grab_button(struct wl_pointer_grab *grab,
2595 uint32_t time, uint32_t button, uint32_t state_w)
2597 struct rotate_grab *rotate =
2598 container_of(grab, struct rotate_grab, base.grab);
2599 struct wl_pointer *pointer = grab->pointer;
2600 struct shell_surface *shsurf = rotate->base.shsurf;
2601 enum wl_pointer_button_state state = state_w;
2603 if (pointer->button_count == 0 &&
2604 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2606 weston_matrix_multiply(&shsurf->rotation.rotation,
2608 shell_grab_end(&rotate->base);
2613 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2620 surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
2622 struct rotate_grab *rotate;
2626 rotate = malloc(sizeof *rotate);
2630 weston_surface_to_global_float(surface->surface,
2631 surface->surface->geometry.width / 2,
2632 surface->surface->geometry.height / 2,
2633 &rotate->center.x, &rotate->center.y);
2635 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2636 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2637 r = sqrtf(dx * dx + dy * dy);
2639 struct weston_matrix inverse;
2641 weston_matrix_init(&inverse);
2642 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
2643 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2645 weston_matrix_init(&rotate->rotation);
2646 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
2648 weston_matrix_init(&surface->rotation.rotation);
2649 weston_matrix_init(&rotate->rotation);
2652 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2653 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2657 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2660 struct weston_surface *base_surface =
2661 (struct weston_surface *) seat->pointer->focus;
2662 struct shell_surface *surface;
2664 if (base_surface == NULL)
2667 surface = get_shell_surface(base_surface);
2668 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2671 surface_rotate(surface, seat);
2675 lower_fullscreen_layer(struct desktop_shell *shell)
2677 struct workspace *ws;
2678 struct weston_surface *surface, *prev;
2680 ws = get_current_workspace(shell);
2681 wl_list_for_each_reverse_safe(surface, prev,
2682 &shell->fullscreen_layer.surface_list,
2684 weston_surface_restack(surface, &ws->layer.surface_list);
2688 activate(struct desktop_shell *shell, struct weston_surface *es,
2689 struct weston_seat *seat)
2691 struct focus_state *state;
2692 struct workspace *ws;
2694 weston_surface_activate(es, seat);
2696 state = ensure_focus_state(shell, seat);
2700 state->keyboard_focus = es;
2701 wl_list_remove(&state->surface_destroy_listener.link);
2702 wl_signal_add(&es->surface.resource.destroy_signal,
2703 &state->surface_destroy_listener);
2705 switch (get_shell_surface_type(es)) {
2706 case SHELL_SURFACE_FULLSCREEN:
2707 /* should on top of panels */
2708 shell_stack_fullscreen(get_shell_surface(es));
2709 shell_configure_fullscreen(get_shell_surface(es));
2712 ws = get_current_workspace(shell);
2713 weston_surface_restack(es, &ws->layer.surface_list);
2718 /* no-op func for checking black surface */
2720 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2725 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2727 if (es->configure == black_surface_configure) {
2729 *fs_surface = (struct weston_surface *)es->private;
2736 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2739 struct weston_seat *ws = (struct weston_seat *) seat;
2740 struct desktop_shell *shell = data;
2741 struct weston_surface *focus;
2742 struct weston_surface *upper;
2744 focus = (struct weston_surface *) seat->pointer->focus;
2748 if (is_black_surface(focus, &upper))
2751 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2754 if (seat->pointer->grab == &seat->pointer->default_grab)
2755 activate(shell, focus, ws);
2759 lock(struct desktop_shell *shell)
2761 struct workspace *ws = get_current_workspace(shell);
2763 if (shell->locked) {
2764 weston_compositor_sleep(shell->compositor);
2768 shell->locked = true;
2770 /* Hide all surfaces by removing the fullscreen, panel and
2771 * toplevel layers. This way nothing else can show or receive
2772 * input events while we are locked. */
2774 wl_list_remove(&shell->panel_layer.link);
2775 wl_list_remove(&shell->fullscreen_layer.link);
2776 if (shell->showing_input_panels)
2777 wl_list_remove(&shell->input_panel_layer.link);
2778 wl_list_remove(&ws->layer.link);
2779 wl_list_insert(&shell->compositor->cursor_layer.link,
2780 &shell->lock_layer.link);
2782 launch_screensaver(shell);
2784 /* TODO: disable bindings that should not work while locked. */
2786 /* All this must be undone in resume_desktop(). */
2790 unlock(struct desktop_shell *shell)
2792 if (!shell->locked || shell->lock_surface) {
2793 shell_fade(shell, FADE_IN);
2797 /* If desktop-shell client has gone away, unlock immediately. */
2798 if (!shell->child.desktop_shell) {
2799 resume_desktop(shell);
2803 if (shell->prepare_event_sent)
2806 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2807 shell->prepare_event_sent = true;
2811 shell_fade_done(struct weston_surface_animation *animation, void *data)
2813 struct desktop_shell *shell = data;
2815 shell->fade.animation = NULL;
2817 switch (shell->fade.type) {
2819 weston_surface_destroy(shell->fade.surface);
2820 shell->fade.surface = NULL;
2829 shell_fade(struct desktop_shell *shell, enum fade_type type)
2831 struct weston_compositor *compositor = shell->compositor;
2832 struct weston_surface *surface;
2843 weston_log("shell: invalid fade type\n");
2847 shell->fade.type = type;
2849 if (shell->fade.surface == NULL) {
2850 surface = weston_surface_create(compositor);
2854 weston_surface_configure(surface, 0, 0, 8192, 8192);
2855 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2856 surface->alpha = 1.0 - tint;
2857 wl_list_insert(&compositor->fade_layer.surface_list,
2858 &surface->layer_link);
2859 weston_surface_update_transform(surface);
2860 shell->fade.surface = surface;
2861 pixman_region32_init(&surface->input);
2864 if (shell->fade.animation)
2865 weston_fade_update(shell->fade.animation,
2866 shell->fade.surface->alpha, tint, 30.0);
2868 shell->fade.animation =
2869 weston_fade_run(shell->fade.surface,
2870 1.0 - tint, tint, 30.0,
2871 shell_fade_done, shell);
2875 idle_handler(struct wl_listener *listener, void *data)
2877 struct desktop_shell *shell =
2878 container_of(listener, struct desktop_shell, idle_listener);
2880 shell_fade(shell, FADE_OUT);
2881 /* lock() is called from shell_fade_done() */
2885 wake_handler(struct wl_listener *listener, void *data)
2887 struct desktop_shell *shell =
2888 container_of(listener, struct desktop_shell, wake_listener);
2894 show_input_panels(struct wl_listener *listener, void *data)
2896 struct desktop_shell *shell =
2897 container_of(listener, struct desktop_shell,
2898 show_input_panel_listener);
2899 struct input_panel_surface *surface, *next;
2900 struct weston_surface *ws;
2902 if (shell->showing_input_panels)
2905 shell->showing_input_panels = true;
2908 wl_list_insert(&shell->panel_layer.link,
2909 &shell->input_panel_layer.link);
2911 wl_list_for_each_safe(surface, next,
2912 &shell->input_panel.surfaces, link) {
2913 ws = surface->surface;
2914 wl_list_insert(&shell->input_panel_layer.surface_list,
2916 weston_surface_geometry_dirty(ws);
2917 weston_surface_update_transform(ws);
2918 weston_surface_damage(ws);
2919 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2924 hide_input_panels(struct wl_listener *listener, void *data)
2926 struct desktop_shell *shell =
2927 container_of(listener, struct desktop_shell,
2928 hide_input_panel_listener);
2929 struct weston_surface *surface, *next;
2931 if (!shell->showing_input_panels)
2934 shell->showing_input_panels = false;
2937 wl_list_remove(&shell->input_panel_layer.link);
2939 wl_list_for_each_safe(surface, next,
2940 &shell->input_panel_layer.surface_list, layer_link)
2941 weston_surface_unmap(surface);
2945 center_on_output(struct weston_surface *surface, struct weston_output *output)
2947 int32_t width = weston_surface_buffer_width(surface);
2948 int32_t height = weston_surface_buffer_height(surface);
2951 x = output->x + (output->width - width) / 2;
2952 y = output->y + (output->height - height) / 2;
2954 weston_surface_configure(surface, x, y, width, height);
2958 weston_surface_set_initial_position (struct weston_surface *surface,
2959 struct desktop_shell *shell)
2961 struct weston_compositor *compositor = shell->compositor;
2963 int range_x, range_y;
2964 int dx, dy, x, y, panel_height;
2965 struct weston_output *output, *target_output = NULL;
2966 struct weston_seat *seat;
2968 /* As a heuristic place the new window on the same output as the
2969 * pointer. Falling back to the output containing 0, 0.
2971 * TODO: Do something clever for touch too?
2973 wl_list_for_each(seat, &compositor->seat_list, link) {
2974 if (seat->has_pointer) {
2975 ix = wl_fixed_to_int(seat->pointer.x);
2976 iy = wl_fixed_to_int(seat->pointer.y);
2981 wl_list_for_each(output, &compositor->output_list, link) {
2982 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2983 target_output = output;
2988 if (!target_output) {
2989 weston_surface_set_position(surface, 10 + random() % 400,
2990 10 + random() % 400);
2994 /* Valid range within output where the surface will still be onscreen.
2995 * If this is negative it means that the surface is bigger than
2998 panel_height = get_output_panel_height(shell, target_output);
2999 range_x = target_output->width - surface->geometry.width;
3000 range_y = (target_output->height - panel_height) -
3001 surface->geometry.height;
3004 dx = random() % range_x;
3009 dy = panel_height + random() % range_y;
3013 x = target_output->x + dx;
3014 y = target_output->y + dy;
3016 weston_surface_set_position (surface, x, y);
3020 map(struct desktop_shell *shell, struct weston_surface *surface,
3021 int32_t width, int32_t height, int32_t sx, int32_t sy)
3023 struct weston_compositor *compositor = shell->compositor;
3024 struct shell_surface *shsurf = get_shell_surface(surface);
3025 enum shell_surface_type surface_type = shsurf->type;
3026 struct weston_surface *parent;
3027 struct weston_seat *seat;
3028 struct workspace *ws;
3029 int panel_height = 0;
3031 surface->geometry.width = width;
3032 surface->geometry.height = height;
3033 weston_surface_geometry_dirty(surface);
3035 /* initial positioning, see also configure() */
3036 switch (surface_type) {
3037 case SHELL_SURFACE_TOPLEVEL:
3038 weston_surface_set_initial_position(surface, shell);
3040 case SHELL_SURFACE_FULLSCREEN:
3041 center_on_output(surface, shsurf->fullscreen_output);
3042 shell_map_fullscreen(shsurf);
3044 case SHELL_SURFACE_MAXIMIZED:
3045 /* use surface configure to set the geometry */
3046 panel_height = get_output_panel_height(shell,surface->output);
3047 weston_surface_set_position(surface, shsurf->output->x,
3048 shsurf->output->y + panel_height);
3050 case SHELL_SURFACE_POPUP:
3051 shell_map_popup(shsurf);
3053 case SHELL_SURFACE_NONE:
3054 weston_surface_set_position(surface,
3055 surface->geometry.x + sx,
3056 surface->geometry.y + sy);
3062 /* surface stacking order, see also activate() */
3063 switch (surface_type) {
3064 case SHELL_SURFACE_POPUP:
3065 case SHELL_SURFACE_TRANSIENT:
3066 parent = shsurf->parent;
3067 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3069 case SHELL_SURFACE_FULLSCREEN:
3070 case SHELL_SURFACE_NONE:
3073 ws = get_current_workspace(shell);
3074 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
3078 if (surface_type != SHELL_SURFACE_NONE) {
3079 weston_surface_update_transform(surface);
3080 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3081 surface->output = shsurf->output;
3084 switch (surface_type) {
3085 case SHELL_SURFACE_TRANSIENT:
3086 if (shsurf->transient.flags ==
3087 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3089 case SHELL_SURFACE_TOPLEVEL:
3090 case SHELL_SURFACE_FULLSCREEN:
3091 case SHELL_SURFACE_MAXIMIZED:
3092 if (!shell->locked) {
3093 wl_list_for_each(seat, &compositor->seat_list, link)
3094 activate(shell, surface, seat);
3101 if (surface_type == SHELL_SURFACE_TOPLEVEL)
3103 switch (shell->win_animation_type) {
3104 case ANIMATION_FADE:
3105 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
3107 case ANIMATION_ZOOM:
3108 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3117 configure(struct desktop_shell *shell, struct weston_surface *surface,
3118 float x, float y, int32_t width, int32_t height)
3120 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3121 struct shell_surface *shsurf;
3123 shsurf = get_shell_surface(surface);
3125 surface_type = shsurf->type;
3127 weston_surface_configure(surface, x, y, width, height);
3129 switch (surface_type) {
3130 case SHELL_SURFACE_FULLSCREEN:
3131 shell_stack_fullscreen(shsurf);
3132 shell_configure_fullscreen(shsurf);
3134 case SHELL_SURFACE_MAXIMIZED:
3135 /* setting x, y and using configure to change that geometry */
3136 surface->geometry.x = surface->output->x;
3137 surface->geometry.y = surface->output->y +
3138 get_output_panel_height(shell,surface->output);
3140 case SHELL_SURFACE_TOPLEVEL:
3146 /* XXX: would a fullscreen surface need the same handling? */
3147 if (surface->output) {
3148 weston_surface_update_transform(surface);
3150 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3151 surface->output = shsurf->output;
3156 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3158 struct shell_surface *shsurf = get_shell_surface(es);
3159 struct desktop_shell *shell = shsurf->shell;
3161 int type_changed = 0;
3166 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3167 shsurf->type != shsurf->next_type) {
3168 set_surface_type(shsurf);
3172 if (!weston_surface_is_mapped(es)) {
3173 map(shell, es, width, height, sx, sy);
3174 } else if (type_changed || sx != 0 || sy != 0 ||
3175 es->geometry.width != width ||
3176 es->geometry.height != height) {
3177 float from_x, from_y;
3180 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3181 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3182 configure(shell, es,
3183 es->geometry.x + to_x - from_x,
3184 es->geometry.y + to_y - from_y,
3189 static void launch_desktop_shell_process(void *data);
3192 desktop_shell_sigchld(struct weston_process *process, int status)
3195 struct desktop_shell *shell =
3196 container_of(process, struct desktop_shell, child.process);
3198 shell->child.process.pid = 0;
3199 shell->child.client = NULL; /* already destroyed by wayland */
3201 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3202 time = weston_compositor_get_time();
3203 if (time - shell->child.deathstamp > 30000) {
3204 shell->child.deathstamp = time;
3205 shell->child.deathcount = 0;
3208 shell->child.deathcount++;
3209 if (shell->child.deathcount > 5) {
3210 weston_log("weston-desktop-shell died, giving up.\n");
3214 weston_log("weston-desktop-shell died, respawning...\n");
3215 launch_desktop_shell_process(shell);
3219 launch_desktop_shell_process(void *data)
3221 struct desktop_shell *shell = data;
3222 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3224 shell->child.client = weston_client_launch(shell->compositor,
3225 &shell->child.process,
3227 desktop_shell_sigchld);
3229 if (!shell->child.client)
3230 weston_log("not able to start %s\n", shell_exe);
3234 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3236 struct desktop_shell *shell = data;
3238 wl_client_add_object(client, &wl_shell_interface,
3239 &shell_implementation, id, shell);
3243 unbind_desktop_shell(struct wl_resource *resource)
3245 struct desktop_shell *shell = resource->data;
3248 resume_desktop(shell);
3250 shell->child.desktop_shell = NULL;
3251 shell->prepare_event_sent = false;
3256 bind_desktop_shell(struct wl_client *client,
3257 void *data, uint32_t version, uint32_t id)
3259 struct desktop_shell *shell = data;
3260 struct wl_resource *resource;
3262 resource = wl_client_add_object(client, &desktop_shell_interface,
3263 &desktop_shell_implementation,
3266 if (client == shell->child.client) {
3267 resource->destroy = unbind_desktop_shell;
3268 shell->child.desktop_shell = resource;
3272 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3273 "permission to bind desktop_shell denied");
3274 wl_resource_destroy(resource);
3278 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3280 struct desktop_shell *shell = surface->private;
3285 /* XXX: starting weston-screensaver beforehand does not work */
3289 center_on_output(surface, surface->output);
3291 if (wl_list_empty(&surface->layer_link)) {
3292 wl_list_insert(shell->lock_layer.surface_list.prev,
3293 &surface->layer_link);
3294 weston_surface_update_transform(surface);
3295 wl_event_source_timer_update(shell->screensaver.timer,
3296 shell->screensaver.duration);
3297 shell_fade(shell, FADE_IN);
3302 screensaver_set_surface(struct wl_client *client,
3303 struct wl_resource *resource,
3304 struct wl_resource *surface_resource,
3305 struct wl_resource *output_resource)
3307 struct desktop_shell *shell = resource->data;
3308 struct weston_surface *surface = surface_resource->data;
3309 struct weston_output *output = output_resource->data;
3311 surface->configure = screensaver_configure;
3312 surface->private = shell;
3313 surface->output = output;
3316 static const struct screensaver_interface screensaver_implementation = {
3317 screensaver_set_surface
3321 unbind_screensaver(struct wl_resource *resource)
3323 struct desktop_shell *shell = resource->data;
3325 shell->screensaver.binding = NULL;
3330 bind_screensaver(struct wl_client *client,
3331 void *data, uint32_t version, uint32_t id)
3333 struct desktop_shell *shell = data;
3334 struct wl_resource *resource;
3336 resource = wl_client_add_object(client, &screensaver_interface,
3337 &screensaver_implementation,
3340 if (shell->screensaver.binding == NULL) {
3341 resource->destroy = unbind_screensaver;
3342 shell->screensaver.binding = resource;
3346 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3347 "interface object already bound");
3348 wl_resource_destroy(resource);
3352 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3354 struct weston_mode *mode;
3360 if (!weston_surface_is_mapped(surface))
3363 mode = surface->output->current;
3364 x = (mode->width - width) / 2;
3365 y = mode->height - height;
3367 /* Don't map the input panel here, wait for
3368 * show_input_panels signal. */
3370 weston_surface_configure(surface,
3371 surface->output->x + x,
3372 surface->output->y + y,
3377 destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
3379 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
3380 wl_list_remove(&input_panel_surface->link);
3382 input_panel_surface->surface->configure = NULL;
3384 free(input_panel_surface);
3387 static struct input_panel_surface *
3388 get_input_panel_surface(struct weston_surface *surface)
3390 if (surface->configure == input_panel_configure) {
3391 return surface->private;
3398 input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3400 struct input_panel_surface *ipsurface = container_of(listener,
3401 struct input_panel_surface,
3402 surface_destroy_listener);
3404 if (ipsurface->resource.client) {
3405 wl_resource_destroy(&ipsurface->resource);
3407 wl_signal_emit(&ipsurface->resource.destroy_signal,
3408 &ipsurface->resource);
3409 destroy_input_panel_surface(ipsurface);
3412 static struct input_panel_surface *
3413 create_input_panel_surface(struct desktop_shell *shell,
3414 struct weston_surface *surface)
3416 struct input_panel_surface *input_panel_surface;
3418 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3419 if (!input_panel_surface)
3422 surface->configure = input_panel_configure;
3423 surface->private = input_panel_surface;
3425 input_panel_surface->shell = shell;
3427 input_panel_surface->surface = surface;
3429 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3430 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3431 wl_signal_add(&surface->surface.resource.destroy_signal,
3432 &input_panel_surface->surface_destroy_listener);
3434 wl_list_init(&input_panel_surface->link);
3436 return input_panel_surface;
3440 input_panel_surface_set_toplevel(struct wl_client *client,
3441 struct wl_resource *resource,
3444 struct input_panel_surface *input_panel_surface = resource->data;
3445 struct desktop_shell *shell = input_panel_surface->shell;
3447 wl_list_insert(&shell->input_panel.surfaces,
3448 &input_panel_surface->link);
3451 static const struct input_panel_surface_interface input_panel_surface_implementation = {
3452 input_panel_surface_set_toplevel
3456 destroy_input_panel_surface_resource(struct wl_resource *resource)
3458 struct input_panel_surface *ipsurf = resource->data;
3460 destroy_input_panel_surface(ipsurf);
3464 input_panel_get_input_panel_surface(struct wl_client *client,
3465 struct wl_resource *resource,
3467 struct wl_resource *surface_resource)
3469 struct weston_surface *surface = surface_resource->data;
3470 struct desktop_shell *shell = resource->data;
3471 struct input_panel_surface *ipsurf;
3473 if (get_input_panel_surface(surface)) {
3474 wl_resource_post_error(surface_resource,
3475 WL_DISPLAY_ERROR_INVALID_OBJECT,
3476 "input_panel::get_input_panel_surface already requested");
3480 ipsurf = create_input_panel_surface(shell, surface);
3482 wl_resource_post_error(surface_resource,
3483 WL_DISPLAY_ERROR_INVALID_OBJECT,
3484 "surface->configure already set");
3488 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3489 ipsurf->resource.object.id = id;
3490 ipsurf->resource.object.interface = &input_panel_surface_interface;
3491 ipsurf->resource.object.implementation =
3492 (void (**)(void)) &input_panel_surface_implementation;
3493 ipsurf->resource.data = ipsurf;
3495 wl_client_add_resource(client, &ipsurf->resource);
3498 static const struct input_panel_interface input_panel_implementation = {
3499 input_panel_get_input_panel_surface
3503 unbind_input_panel(struct wl_resource *resource)
3505 struct desktop_shell *shell = resource->data;
3507 shell->input_panel.binding = NULL;
3512 bind_input_panel(struct wl_client *client,
3513 void *data, uint32_t version, uint32_t id)
3515 struct desktop_shell *shell = data;
3516 struct wl_resource *resource;
3518 resource = wl_client_add_object(client, &input_panel_interface,
3519 &input_panel_implementation,
3522 if (shell->input_panel.binding == NULL) {
3523 resource->destroy = unbind_input_panel;
3524 shell->input_panel.binding = resource;
3528 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3529 "interface object already bound");
3530 wl_resource_destroy(resource);
3534 struct desktop_shell *shell;
3535 struct weston_surface *current;
3536 struct wl_listener listener;
3537 struct wl_keyboard_grab grab;
3541 switcher_next(struct switcher *switcher)
3543 struct weston_surface *surface;
3544 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3545 struct shell_surface *shsurf;
3546 struct workspace *ws = get_current_workspace(switcher->shell);
3548 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3549 switch (get_shell_surface_type(surface)) {
3550 case SHELL_SURFACE_TOPLEVEL:
3551 case SHELL_SURFACE_FULLSCREEN:
3552 case SHELL_SURFACE_MAXIMIZED:
3555 if (prev == switcher->current)
3558 surface->alpha = 0.25;
3559 weston_surface_geometry_dirty(surface);
3560 weston_surface_damage(surface);
3566 if (is_black_surface(surface, NULL)) {
3567 surface->alpha = 0.25;
3568 weston_surface_geometry_dirty(surface);
3569 weston_surface_damage(surface);
3579 wl_list_remove(&switcher->listener.link);
3580 wl_signal_add(&next->surface.resource.destroy_signal,
3581 &switcher->listener);
3583 switcher->current = next;
3586 shsurf = get_shell_surface(switcher->current);
3587 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3588 shsurf->fullscreen.black_surface->alpha = 1.0;
3592 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3594 struct switcher *switcher =
3595 container_of(listener, struct switcher, listener);
3597 switcher_next(switcher);
3601 switcher_destroy(struct switcher *switcher)
3603 struct weston_surface *surface;
3604 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3605 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
3606 struct workspace *ws = get_current_workspace(switcher->shell);
3608 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3609 surface->alpha = 1.0;
3610 weston_surface_damage(surface);
3613 if (switcher->current)
3614 activate(switcher->shell, switcher->current,
3615 (struct weston_seat *) keyboard->seat);
3616 wl_list_remove(&switcher->listener.link);
3617 wl_keyboard_end_grab(keyboard);
3618 if (weston_keyboard->input_method_resource)
3619 keyboard->grab = &weston_keyboard->input_method_grab;
3624 switcher_key(struct wl_keyboard_grab *grab,
3625 uint32_t time, uint32_t key, uint32_t state_w)
3627 struct switcher *switcher = container_of(grab, struct switcher, grab);
3628 enum wl_keyboard_key_state state = state_w;
3630 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3631 switcher_next(switcher);
3635 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3636 uint32_t mods_depressed, uint32_t mods_latched,
3637 uint32_t mods_locked, uint32_t group)
3639 struct switcher *switcher = container_of(grab, struct switcher, grab);
3640 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3642 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3643 switcher_destroy(switcher);
3646 static const struct wl_keyboard_grab_interface switcher_grab = {
3652 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3655 struct desktop_shell *shell = data;
3656 struct switcher *switcher;
3658 switcher = malloc(sizeof *switcher);
3659 switcher->shell = shell;
3660 switcher->current = NULL;
3661 switcher->listener.notify = switcher_handle_surface_destroy;
3662 wl_list_init(&switcher->listener.link);
3664 lower_fullscreen_layer(switcher->shell);
3665 switcher->grab.interface = &switcher_grab;
3666 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3667 wl_keyboard_set_focus(seat->keyboard, NULL);
3668 switcher_next(switcher);
3672 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3675 struct weston_compositor *compositor = data;
3676 struct weston_output *output;
3677 long backlight_new = 0;
3679 /* TODO: we're limiting to simple use cases, where we assume just
3680 * control on the primary display. We'd have to extend later if we
3681 * ever get support for setting backlights on random desktop LCD
3683 output = get_default_output(compositor);
3687 if (!output->set_backlight)
3690 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3691 backlight_new = output->backlight_current - 25;
3692 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3693 backlight_new = output->backlight_current + 25;
3695 if (backlight_new < 5)
3697 if (backlight_new > 255)
3698 backlight_new = 255;
3700 output->backlight_current = backlight_new;
3701 output->set_backlight(output, output->backlight_current);
3705 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3708 struct desktop_shell *shell = data;
3709 struct weston_compositor *compositor = shell->compositor;
3710 compositor->fan_debug = !compositor->fan_debug;
3711 weston_compositor_damage_all(compositor);
3714 struct debug_binding_grab {
3715 struct wl_keyboard_grab grab;
3716 struct weston_seat *seat;
3718 int key_released[2];
3722 debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3723 uint32_t key, uint32_t state)
3725 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3726 struct wl_resource *resource;
3727 struct wl_display *display;
3729 int send = 0, terminate = 0;
3730 int check_binding = 1;
3733 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3734 /* Do not run bindings on key releases */
3737 for (i = 0; i < 2; i++)
3738 if (key == db->key[i])
3739 db->key_released[i] = 1;
3741 if (db->key_released[0] && db->key_released[1]) {
3742 /* All key releases been swalled so end the grab */
3744 } else if (key != db->key[0] && key != db->key[1]) {
3745 /* Should not swallow release of other keys */
3748 } else if (key == db->key[0] && !db->key_released[0]) {
3749 /* Do not check bindings for the first press of the binding
3750 * key. This allows it to be used as a debug shortcut.
3751 * We still need to swallow this event. */
3753 } else if (db->key[1]) {
3754 /* If we already ran a binding don't process another one since
3755 * we can't keep track of all the binding keys that were
3756 * pressed in order to swallow the release events. */
3761 if (check_binding) {
3762 struct weston_compositor *ec = db->seat->compositor;
3764 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3766 /* We ran a binding so swallow the press and keep the
3767 * grab to swallow the released too. */
3772 /* Terminate the grab since the key pressed is not a
3773 * debug binding key. */
3780 resource = grab->keyboard->focus_resource;
3783 display = wl_client_get_display(resource->client);
3784 serial = wl_display_next_serial(display);
3785 wl_keyboard_send_key(resource, serial, time, key, state);
3790 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
3791 wl_keyboard_end_grab(grab->keyboard);
3792 if (weston_keyboard->input_method_resource)
3793 grab->keyboard->grab = &weston_keyboard->input_method_grab;
3799 debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3800 uint32_t mods_depressed, uint32_t mods_latched,
3801 uint32_t mods_locked, uint32_t group)
3803 struct wl_resource *resource;
3805 resource = grab->keyboard->focus_resource;
3809 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3810 mods_latched, mods_locked, group);
3813 struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3815 debug_binding_modifiers
3819 debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3821 struct debug_binding_grab *grab;
3823 grab = calloc(1, sizeof *grab);
3827 grab->seat = (struct weston_seat *) seat;
3829 grab->grab.interface = &debug_binding_keyboard_grab;
3830 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3834 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3837 struct wl_surface *focus_surface;
3838 struct wl_client *client;
3839 struct desktop_shell *shell = data;
3840 struct weston_compositor *compositor = shell->compositor;
3843 focus_surface = seat->keyboard->focus;
3847 wl_signal_emit(&compositor->kill_signal, focus_surface);
3849 client = focus_surface->resource.client;
3850 wl_client_get_credentials(client, &pid, NULL, NULL);
3852 /* Skip clients that we launched ourselves (the credentials of
3853 * the socketpair is ours) */
3854 if (pid == getpid())
3861 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3862 uint32_t key, void *data)
3864 struct desktop_shell *shell = data;
3865 unsigned int new_index = shell->workspaces.current;
3872 change_workspace(shell, new_index);
3876 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3877 uint32_t key, void *data)
3879 struct desktop_shell *shell = data;
3880 unsigned int new_index = shell->workspaces.current;
3884 if (new_index < shell->workspaces.num - 1)
3887 change_workspace(shell, new_index);
3891 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3892 uint32_t key, void *data)
3894 struct desktop_shell *shell = data;
3895 unsigned int new_index;
3899 new_index = key - KEY_F1;
3900 if (new_index >= shell->workspaces.num)
3901 new_index = shell->workspaces.num - 1;
3903 change_workspace(shell, new_index);
3907 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3908 uint32_t key, void *data)
3910 struct desktop_shell *shell = data;
3911 unsigned int new_index = shell->workspaces.current;
3919 take_surface_to_workspace_by_seat(shell, seat, new_index);
3923 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3924 uint32_t key, void *data)
3926 struct desktop_shell *shell = data;
3927 unsigned int new_index = shell->workspaces.current;
3932 if (new_index < shell->workspaces.num - 1)
3935 take_surface_to_workspace_by_seat(shell, seat, new_index);
3939 shell_destroy(struct wl_listener *listener, void *data)
3941 struct desktop_shell *shell =
3942 container_of(listener, struct desktop_shell, destroy_listener);
3943 struct workspace **ws;
3945 if (shell->child.client)
3946 wl_client_destroy(shell->child.client);
3948 wl_list_remove(&shell->idle_listener.link);
3949 wl_list_remove(&shell->wake_listener.link);
3950 wl_list_remove(&shell->show_input_panel_listener.link);
3951 wl_list_remove(&shell->hide_input_panel_listener.link);
3953 wl_array_for_each(ws, &shell->workspaces.array)
3954 workspace_destroy(*ws);
3955 wl_array_release(&shell->workspaces.array);
3957 free(shell->screensaver.path);
3962 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3965 int i, num_workspace_bindings;
3967 /* fixed bindings */
3968 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3969 MODIFIER_CTRL | MODIFIER_ALT,
3970 terminate_binding, ec);
3971 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3972 click_to_activate_binding,
3974 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3975 MODIFIER_SUPER | MODIFIER_ALT,
3976 surface_opacity_binding, NULL);
3977 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3978 MODIFIER_SUPER, zoom_axis_binding,
3981 /* configurable bindings */
3982 mod = shell->binding_modifier;
3983 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3984 zoom_key_binding, NULL);
3985 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3986 zoom_key_binding, NULL);
3987 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3989 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3990 resize_binding, shell);
3991 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3992 rotate_binding, NULL);
3993 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3995 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3997 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3998 backlight_binding, ec);
3999 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4001 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4002 backlight_binding, ec);
4003 weston_compositor_add_key_binding(ec, KEY_K, mod,
4004 force_kill_binding, shell);
4005 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4006 workspace_up_binding, shell);
4007 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4008 workspace_down_binding, shell);
4009 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4010 workspace_move_surface_up_binding,
4012 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4013 workspace_move_surface_down_binding,
4016 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4017 if (shell->workspaces.num > 1) {
4018 num_workspace_bindings = shell->workspaces.num;
4019 if (num_workspace_bindings > 6)
4020 num_workspace_bindings = 6;
4021 for (i = 0; i < num_workspace_bindings; i++)
4022 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4023 workspace_f_binding,
4027 /* Debug bindings */
4028 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4029 debug_binding, shell);
4030 weston_compositor_add_debug_binding(ec, KEY_F,
4031 fan_debug_repaint_binding, shell);
4035 module_init(struct weston_compositor *ec,
4036 int *argc, char *argv[], const char *config_file)
4038 struct weston_seat *seat;
4039 struct desktop_shell *shell;
4040 struct workspace **pws;
4042 struct wl_event_loop *loop;
4044 shell = malloc(sizeof *shell);
4048 memset(shell, 0, sizeof *shell);
4049 shell->compositor = ec;
4051 shell->destroy_listener.notify = shell_destroy;
4052 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
4053 shell->idle_listener.notify = idle_handler;
4054 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4055 shell->wake_listener.notify = wake_handler;
4056 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
4057 shell->show_input_panel_listener.notify = show_input_panels;
4058 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4059 shell->hide_input_panel_listener.notify = hide_input_panels;
4060 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
4061 ec->ping_handler = ping_handler;
4062 ec->shell_interface.shell = shell;
4063 ec->shell_interface.create_shell_surface = create_shell_surface;
4064 ec->shell_interface.set_toplevel = set_toplevel;
4065 ec->shell_interface.set_transient = set_transient;
4066 ec->shell_interface.set_fullscreen = set_fullscreen;
4067 ec->shell_interface.move = surface_move;
4068 ec->shell_interface.resize = surface_resize;
4070 wl_list_init(&shell->input_panel.surfaces);
4072 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4073 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
4074 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4075 weston_layer_init(&shell->lock_layer, NULL);
4076 weston_layer_init(&shell->input_panel_layer, NULL);
4078 wl_array_init(&shell->workspaces.array);
4079 wl_list_init(&shell->workspaces.client_list);
4081 shell_configuration(shell, config_file);
4083 for (i = 0; i < shell->workspaces.num; i++) {
4084 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4088 *pws = workspace_create();
4092 activate_workspace(shell, 0);
4094 wl_list_init(&shell->workspaces.anim_sticky_list);
4095 wl_list_init(&shell->workspaces.animation.link);
4096 shell->workspaces.animation.frame = animate_workspace_change_frame;
4098 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4099 shell, bind_shell) == NULL)
4102 if (wl_display_add_global(ec->wl_display,
4103 &desktop_shell_interface,
4104 shell, bind_desktop_shell) == NULL)
4107 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4108 shell, bind_screensaver) == NULL)
4111 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4112 shell, bind_input_panel) == NULL)
4115 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4116 shell, bind_workspace_manager) == NULL)
4119 shell->child.deathstamp = weston_compositor_get_time();
4121 loop = wl_display_get_event_loop(ec->wl_display);
4122 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
4124 shell->screensaver.timer =
4125 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4127 wl_list_for_each(seat, &ec->seat_list, link)
4128 create_pointer_focus_listener(seat);
4130 shell_add_bindings(ec, shell);
4132 shell_fade(shell, FADE_IN);