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.
31 #include <linux/input.h>
35 #include <sys/types.h>
37 #include "compositor.h"
38 #include "desktop-shell-server-protocol.h"
39 #include "input-method-server-protocol.h"
40 #include "workspaces-server-protocol.h"
41 #include "../shared/config-parser.h"
43 #define DEFAULT_NUM_WORKSPACES 1
44 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
59 struct weston_seat *seat;
61 struct weston_surface *keyboard_focus;
63 struct wl_listener seat_destroy_listener;
64 struct wl_listener surface_destroy_listener;
68 struct weston_layer layer;
70 struct wl_list focus_list;
71 struct wl_listener seat_destroyed_listener;
74 struct input_panel_surface {
75 struct wl_resource *resource;
76 struct wl_signal destroy_signal;
78 struct desktop_shell *shell;
81 struct weston_surface *surface;
82 struct wl_listener surface_destroy_listener;
84 struct weston_output *output;
88 struct desktop_shell {
89 struct weston_compositor *compositor;
91 struct wl_listener idle_listener;
92 struct wl_listener wake_listener;
93 struct wl_listener destroy_listener;
94 struct wl_listener show_input_panel_listener;
95 struct wl_listener hide_input_panel_listener;
96 struct wl_listener update_input_panel_listener;
98 struct weston_layer fullscreen_layer;
99 struct weston_layer panel_layer;
100 struct weston_layer background_layer;
101 struct weston_layer lock_layer;
102 struct weston_layer input_panel_layer;
104 struct wl_listener pointer_focus_listener;
105 struct weston_surface *grab_surface;
108 struct weston_process process;
109 struct wl_client *client;
110 struct wl_resource *desktop_shell;
117 bool showing_input_panels;
118 bool prepare_event_sent;
121 struct weston_surface *surface;
122 pixman_box32_t cursor_rectangle;
125 struct weston_surface *lock_surface;
126 struct wl_listener lock_surface_listener;
129 struct wl_array array;
130 unsigned int current;
133 struct wl_list client_list;
135 struct weston_animation animation;
136 struct wl_list anim_sticky_list;
138 uint32_t anim_timestamp;
140 struct workspace *anim_from;
141 struct workspace *anim_to;
147 struct wl_resource *binding;
148 struct weston_process process;
149 struct wl_event_source *timer;
153 struct wl_resource *binding;
154 struct wl_list surfaces;
158 struct weston_surface *surface;
159 struct weston_surface_animation *animation;
161 struct wl_event_source *startup_timer;
164 uint32_t binding_modifier;
165 enum animation_type win_animation_type;
168 enum shell_surface_type {
170 SHELL_SURFACE_TOPLEVEL,
171 SHELL_SURFACE_TRANSIENT,
172 SHELL_SURFACE_FULLSCREEN,
173 SHELL_SURFACE_MAXIMIZED,
175 SHELL_SURFACE_XWAYLAND
179 struct wl_event_source *source;
183 struct shell_surface {
184 struct wl_resource *resource;
185 struct wl_signal destroy_signal;
187 struct weston_surface *surface;
188 struct wl_listener surface_destroy_listener;
189 struct weston_surface *parent;
190 struct desktop_shell *shell;
192 enum shell_surface_type type, next_type;
194 int32_t saved_x, saved_y;
195 bool saved_position_valid;
196 bool saved_rotation_valid;
200 struct weston_transform transform;
201 struct weston_matrix rotation;
205 struct wl_list grab_link;
207 struct shell_seat *shseat;
217 enum wl_shell_surface_fullscreen_method type;
218 struct weston_transform transform; /* matrix from x, y */
220 struct weston_surface *black_surface;
223 struct ping_timer *ping_timer;
225 struct weston_transform workspace_transform;
227 struct weston_output *fullscreen_output;
228 struct weston_output *output;
231 const struct weston_shell_client *client;
235 struct weston_pointer_grab grab;
236 struct shell_surface *shsurf;
237 struct wl_listener shsurf_destroy_listener;
240 struct shell_touch_grab {
241 struct weston_touch_grab grab;
242 struct shell_surface *shsurf;
243 struct wl_listener shsurf_destroy_listener;
244 struct weston_touch *touch;
247 struct weston_move_grab {
248 struct shell_grab base;
252 struct weston_touch_move_grab {
253 struct shell_touch_grab base;
258 struct shell_grab base;
259 struct weston_matrix rotation;
267 struct weston_seat *seat;
268 struct wl_listener seat_destroy_listener;
271 struct weston_pointer_grab grab;
272 struct wl_list surfaces_list;
273 struct wl_client *client;
279 activate(struct desktop_shell *shell, struct weston_surface *es,
280 struct weston_seat *seat);
282 static struct workspace *
283 get_current_workspace(struct desktop_shell *shell);
285 static struct shell_surface *
286 get_shell_surface(struct weston_surface *surface);
288 static struct desktop_shell *
289 shell_surface_get_shell(struct shell_surface *shsurf);
292 surface_rotate(struct shell_surface *surface, struct weston_seat *seat);
295 shell_fade_startup(struct desktop_shell *shell);
298 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
300 struct desktop_shell *shell;
301 struct weston_surface *top_fs_es;
303 shell = shell_surface_get_shell(shsurf);
305 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
308 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
309 struct weston_surface,
311 return (shsurf == get_shell_surface(top_fs_es));
315 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
317 struct shell_grab *grab;
319 grab = container_of(listener, struct shell_grab,
320 shsurf_destroy_listener);
326 popup_grab_end(struct weston_pointer *pointer);
329 shell_grab_start(struct shell_grab *grab,
330 const struct weston_pointer_grab_interface *interface,
331 struct shell_surface *shsurf,
332 struct weston_pointer *pointer,
333 enum desktop_shell_cursor cursor)
335 struct desktop_shell *shell = shsurf->shell;
337 popup_grab_end(pointer);
339 grab->grab.interface = interface;
340 grab->shsurf = shsurf;
341 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
342 wl_signal_add(&shsurf->destroy_signal,
343 &grab->shsurf_destroy_listener);
345 weston_pointer_start_grab(pointer, &grab->grab);
346 if (shell->child.desktop_shell) {
347 desktop_shell_send_grab_cursor(shell->child.desktop_shell,
349 weston_pointer_set_focus(pointer, shell->grab_surface,
350 wl_fixed_from_int(0),
351 wl_fixed_from_int(0));
356 shell_grab_end(struct shell_grab *grab)
359 wl_list_remove(&grab->shsurf_destroy_listener.link);
361 weston_pointer_end_grab(grab->grab.pointer);
365 shell_touch_grab_start(struct shell_touch_grab *grab,
366 const struct weston_touch_grab_interface *interface,
367 struct shell_surface *shsurf,
368 struct weston_touch *touch)
370 struct desktop_shell *shell = shsurf->shell;
372 grab->grab.interface = interface;
373 grab->shsurf = shsurf;
374 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
375 wl_signal_add(&shsurf->destroy_signal,
376 &grab->shsurf_destroy_listener);
380 weston_touch_start_grab(touch, &grab->grab);
381 if (shell->child.desktop_shell)
382 weston_touch_set_focus(touch->seat, shell->grab_surface);
386 shell_touch_grab_end(struct shell_touch_grab *grab)
389 wl_list_remove(&grab->shsurf_destroy_listener.link);
391 weston_touch_end_grab(grab->touch);
395 center_on_output(struct weston_surface *surface,
396 struct weston_output *output);
398 static enum weston_keyboard_modifier
399 get_modifier(char *modifier)
402 return MODIFIER_SUPER;
404 if (!strcmp("ctrl", modifier))
405 return MODIFIER_CTRL;
406 else if (!strcmp("alt", modifier))
408 else if (!strcmp("super", modifier))
409 return MODIFIER_SUPER;
411 return MODIFIER_SUPER;
414 static enum animation_type
415 get_animation_type(char *animation)
418 return ANIMATION_NONE;
420 if (!strcmp("zoom", animation))
421 return ANIMATION_ZOOM;
422 else if (!strcmp("fade", animation))
423 return ANIMATION_FADE;
425 return ANIMATION_NONE;
429 shell_configuration(struct desktop_shell *shell)
431 struct weston_config_section *section;
435 section = weston_config_get_section(shell->compositor->config,
436 "screensaver", NULL, NULL);
437 weston_config_section_get_string(section,
438 "path", &shell->screensaver.path, NULL);
439 weston_config_section_get_int(section, "duration", &duration, 60);
440 shell->screensaver.duration = duration * 1000;
442 section = weston_config_get_section(shell->compositor->config,
443 "shell", NULL, NULL);
444 weston_config_section_get_string(section,
445 "binding-modifier", &s, "super");
446 shell->binding_modifier = get_modifier(s);
448 weston_config_section_get_string(section, "animation", &s, "none");
449 shell->win_animation_type = get_animation_type(s);
451 weston_config_section_get_uint(section, "num-workspaces",
452 &shell->workspaces.num,
453 DEFAULT_NUM_WORKSPACES);
457 focus_state_destroy(struct focus_state *state)
459 wl_list_remove(&state->seat_destroy_listener.link);
460 wl_list_remove(&state->surface_destroy_listener.link);
465 focus_state_seat_destroy(struct wl_listener *listener, void *data)
467 struct focus_state *state = container_of(listener,
469 seat_destroy_listener);
471 wl_list_remove(&state->link);
472 focus_state_destroy(state);
476 focus_state_surface_destroy(struct wl_listener *listener, void *data)
478 struct focus_state *state = container_of(listener,
480 surface_destroy_listener);
481 struct desktop_shell *shell;
482 struct weston_surface *main_surface;
483 struct weston_surface *surface, *next;
485 main_surface = weston_surface_get_main_surface(state->keyboard_focus);
488 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
489 if (surface == main_surface)
496 /* if the focus was a sub-surface, activate its main surface */
497 if (main_surface != state->keyboard_focus)
501 shell = state->seat->compositor->shell_interface.shell;
502 activate(shell, next, state->seat);
504 wl_list_remove(&state->link);
505 focus_state_destroy(state);
509 static struct focus_state *
510 focus_state_create(struct weston_seat *seat, struct workspace *ws)
512 struct focus_state *state;
514 state = malloc(sizeof *state);
520 wl_list_insert(&ws->focus_list, &state->link);
522 state->seat_destroy_listener.notify = focus_state_seat_destroy;
523 state->surface_destroy_listener.notify = focus_state_surface_destroy;
524 wl_signal_add(&seat->destroy_signal,
525 &state->seat_destroy_listener);
526 wl_list_init(&state->surface_destroy_listener.link);
531 static struct focus_state *
532 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
534 struct workspace *ws = get_current_workspace(shell);
535 struct focus_state *state;
537 wl_list_for_each(state, &ws->focus_list, link)
538 if (state->seat == seat)
541 if (&state->link == &ws->focus_list)
542 state = focus_state_create(seat, ws);
548 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
550 struct focus_state *state, *next;
551 struct weston_surface *surface;
553 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
554 surface = state->keyboard_focus;
556 weston_keyboard_set_focus(state->seat->keyboard, surface);
561 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
562 struct weston_seat *seat)
564 struct focus_state *state;
565 struct weston_surface *surface;
567 wl_list_for_each(state, &ws->focus_list, link) {
568 if (state->seat == seat) {
569 surface = seat->keyboard->focus;
570 state->keyboard_focus = surface;
577 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
578 struct weston_surface *surface)
580 struct focus_state *state;
582 wl_list_for_each(state, &ws->focus_list, link)
583 if (state->keyboard_focus == surface)
584 state->keyboard_focus = NULL;
588 workspace_destroy(struct workspace *ws)
590 struct focus_state *state, *next;
592 wl_list_for_each_safe(state, next, &ws->focus_list, link)
593 focus_state_destroy(state);
599 seat_destroyed(struct wl_listener *listener, void *data)
601 struct weston_seat *seat = data;
602 struct focus_state *state, *next;
603 struct workspace *ws = container_of(listener,
605 seat_destroyed_listener);
607 wl_list_for_each_safe(state, next, &ws->focus_list, link)
608 if (state->seat == seat)
609 wl_list_remove(&state->link);
612 static struct workspace *
613 workspace_create(void)
615 struct workspace *ws = malloc(sizeof *ws);
619 weston_layer_init(&ws->layer, NULL);
621 wl_list_init(&ws->focus_list);
622 wl_list_init(&ws->seat_destroyed_listener.link);
623 ws->seat_destroyed_listener.notify = seat_destroyed;
629 workspace_is_empty(struct workspace *ws)
631 return wl_list_empty(&ws->layer.surface_list);
634 static struct workspace *
635 get_workspace(struct desktop_shell *shell, unsigned int index)
637 struct workspace **pws = shell->workspaces.array.data;
638 assert(index < shell->workspaces.num);
643 static struct workspace *
644 get_current_workspace(struct desktop_shell *shell)
646 return get_workspace(shell, shell->workspaces.current);
650 activate_workspace(struct desktop_shell *shell, unsigned int index)
652 struct workspace *ws;
654 ws = get_workspace(shell, index);
655 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
657 shell->workspaces.current = index;
661 get_output_height(struct weston_output *output)
663 return abs(output->region.extents.y1 - output->region.extents.y2);
667 surface_translate(struct weston_surface *surface, double d)
669 struct shell_surface *shsurf = get_shell_surface(surface);
670 struct weston_transform *transform;
672 transform = &shsurf->workspace_transform;
673 if (wl_list_empty(&transform->link))
674 wl_list_insert(surface->geometry.transformation_list.prev,
675 &shsurf->workspace_transform.link);
677 weston_matrix_init(&shsurf->workspace_transform.matrix);
678 weston_matrix_translate(&shsurf->workspace_transform.matrix,
680 weston_surface_geometry_dirty(surface);
684 workspace_translate_out(struct workspace *ws, double fraction)
686 struct weston_surface *surface;
690 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
691 height = get_output_height(surface->output);
692 d = height * fraction;
694 surface_translate(surface, d);
699 workspace_translate_in(struct workspace *ws, double fraction)
701 struct weston_surface *surface;
705 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
706 height = get_output_height(surface->output);
709 d = -(height - height * fraction);
711 d = height + height * fraction;
713 surface_translate(surface, d);
718 broadcast_current_workspace_state(struct desktop_shell *shell)
720 struct wl_resource *resource;
722 wl_resource_for_each(resource, &shell->workspaces.client_list)
723 workspace_manager_send_state(resource,
724 shell->workspaces.current,
725 shell->workspaces.num);
729 reverse_workspace_change_animation(struct desktop_shell *shell,
731 struct workspace *from,
732 struct workspace *to)
734 shell->workspaces.current = index;
736 shell->workspaces.anim_to = to;
737 shell->workspaces.anim_from = from;
738 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
739 shell->workspaces.anim_timestamp = 0;
741 weston_compositor_schedule_repaint(shell->compositor);
745 workspace_deactivate_transforms(struct workspace *ws)
747 struct weston_surface *surface;
748 struct shell_surface *shsurf;
750 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
751 shsurf = get_shell_surface(surface);
752 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
753 wl_list_remove(&shsurf->workspace_transform.link);
754 wl_list_init(&shsurf->workspace_transform.link);
756 weston_surface_geometry_dirty(surface);
761 finish_workspace_change_animation(struct desktop_shell *shell,
762 struct workspace *from,
763 struct workspace *to)
765 weston_compositor_schedule_repaint(shell->compositor);
767 wl_list_remove(&shell->workspaces.animation.link);
768 workspace_deactivate_transforms(from);
769 workspace_deactivate_transforms(to);
770 shell->workspaces.anim_to = NULL;
772 wl_list_remove(&shell->workspaces.anim_from->layer.link);
776 animate_workspace_change_frame(struct weston_animation *animation,
777 struct weston_output *output, uint32_t msecs)
779 struct desktop_shell *shell =
780 container_of(animation, struct desktop_shell,
781 workspaces.animation);
782 struct workspace *from = shell->workspaces.anim_from;
783 struct workspace *to = shell->workspaces.anim_to;
787 if (workspace_is_empty(from) && workspace_is_empty(to)) {
788 finish_workspace_change_animation(shell, from, to);
792 if (shell->workspaces.anim_timestamp == 0) {
793 if (shell->workspaces.anim_current == 0.0)
794 shell->workspaces.anim_timestamp = msecs;
796 shell->workspaces.anim_timestamp =
798 /* Invers of movement function 'y' below. */
799 (asin(1.0 - shell->workspaces.anim_current) *
800 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
804 t = msecs - shell->workspaces.anim_timestamp;
810 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
813 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
814 weston_compositor_schedule_repaint(shell->compositor);
816 workspace_translate_out(from, shell->workspaces.anim_dir * y);
817 workspace_translate_in(to, shell->workspaces.anim_dir * y);
818 shell->workspaces.anim_current = y;
820 weston_compositor_schedule_repaint(shell->compositor);
823 finish_workspace_change_animation(shell, from, to);
827 animate_workspace_change(struct desktop_shell *shell,
829 struct workspace *from,
830 struct workspace *to)
832 struct weston_output *output;
836 if (index > shell->workspaces.current)
841 shell->workspaces.current = index;
843 shell->workspaces.anim_dir = dir;
844 shell->workspaces.anim_from = from;
845 shell->workspaces.anim_to = to;
846 shell->workspaces.anim_current = 0.0;
847 shell->workspaces.anim_timestamp = 0;
849 output = container_of(shell->compositor->output_list.next,
850 struct weston_output, link);
851 wl_list_insert(&output->animation_list,
852 &shell->workspaces.animation.link);
854 wl_list_insert(from->layer.link.prev, &to->layer.link);
856 workspace_translate_in(to, 0);
858 restore_focus_state(shell, to);
860 weston_compositor_schedule_repaint(shell->compositor);
864 update_workspace(struct desktop_shell *shell, unsigned int index,
865 struct workspace *from, struct workspace *to)
867 shell->workspaces.current = index;
868 wl_list_insert(&from->layer.link, &to->layer.link);
869 wl_list_remove(&from->layer.link);
873 change_workspace(struct desktop_shell *shell, unsigned int index)
875 struct workspace *from;
876 struct workspace *to;
878 if (index == shell->workspaces.current)
881 /* Don't change workspace when there is any fullscreen surfaces. */
882 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
885 from = get_current_workspace(shell);
886 to = get_workspace(shell, index);
888 if (shell->workspaces.anim_from == to &&
889 shell->workspaces.anim_to == from) {
890 restore_focus_state(shell, to);
891 reverse_workspace_change_animation(shell, index, from, to);
892 broadcast_current_workspace_state(shell);
896 if (shell->workspaces.anim_to != NULL)
897 finish_workspace_change_animation(shell,
898 shell->workspaces.anim_from,
899 shell->workspaces.anim_to);
901 restore_focus_state(shell, to);
903 if (workspace_is_empty(to) && workspace_is_empty(from))
904 update_workspace(shell, index, from, to);
906 animate_workspace_change(shell, index, from, to);
908 broadcast_current_workspace_state(shell);
912 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
914 struct wl_list *list = &ws->layer.surface_list;
917 if (wl_list_empty(list))
925 return container_of(e, struct weston_surface, layer_link) == surface;
929 move_surface_to_workspace(struct desktop_shell *shell,
930 struct weston_surface *surface,
933 struct workspace *from;
934 struct workspace *to;
935 struct weston_seat *seat;
936 struct weston_surface *focus;
938 assert(weston_surface_get_main_surface(surface) == surface);
940 if (workspace == shell->workspaces.current)
943 if (workspace >= shell->workspaces.num)
944 workspace = shell->workspaces.num - 1;
946 from = get_current_workspace(shell);
947 to = get_workspace(shell, workspace);
949 wl_list_remove(&surface->layer_link);
950 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
952 drop_focus_state(shell, from, surface);
953 wl_list_for_each(seat, &shell->compositor->seat_list, link) {
957 focus = weston_surface_get_main_surface(seat->keyboard->focus);
958 if (focus == surface)
959 weston_keyboard_set_focus(seat->keyboard, NULL);
962 weston_surface_damage_below(surface);
966 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
967 struct weston_seat *seat,
970 struct weston_surface *surface;
971 struct shell_surface *shsurf;
972 struct workspace *from;
973 struct workspace *to;
974 struct focus_state *state;
976 surface = weston_surface_get_main_surface(seat->keyboard->focus);
977 if (surface == NULL ||
978 index == shell->workspaces.current)
981 from = get_current_workspace(shell);
982 to = get_workspace(shell, index);
984 wl_list_remove(&surface->layer_link);
985 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
987 replace_focus_state(shell, to, seat);
988 drop_focus_state(shell, from, surface);
990 if (shell->workspaces.anim_from == to &&
991 shell->workspaces.anim_to == from) {
992 wl_list_remove(&to->layer.link);
993 wl_list_insert(from->layer.link.prev, &to->layer.link);
995 reverse_workspace_change_animation(shell, index, from, to);
996 broadcast_current_workspace_state(shell);
1001 if (shell->workspaces.anim_to != NULL)
1002 finish_workspace_change_animation(shell,
1003 shell->workspaces.anim_from,
1004 shell->workspaces.anim_to);
1006 if (workspace_is_empty(from) &&
1007 workspace_has_only(to, surface))
1008 update_workspace(shell, index, from, to);
1010 shsurf = get_shell_surface(surface);
1011 if (wl_list_empty(&shsurf->workspace_transform.link))
1012 wl_list_insert(&shell->workspaces.anim_sticky_list,
1013 &shsurf->workspace_transform.link);
1015 animate_workspace_change(shell, index, from, to);
1018 broadcast_current_workspace_state(shell);
1020 state = ensure_focus_state(shell, seat);
1022 state->keyboard_focus = surface;
1026 workspace_manager_move_surface(struct wl_client *client,
1027 struct wl_resource *resource,
1028 struct wl_resource *surface_resource,
1031 struct desktop_shell *shell = wl_resource_get_user_data(resource);
1032 struct weston_surface *surface =
1033 wl_resource_get_user_data(surface_resource);
1034 struct weston_surface *main_surface;
1036 main_surface = weston_surface_get_main_surface(surface);
1037 move_surface_to_workspace(shell, main_surface, workspace);
1040 static const struct workspace_manager_interface workspace_manager_implementation = {
1041 workspace_manager_move_surface,
1045 unbind_resource(struct wl_resource *resource)
1047 wl_list_remove(wl_resource_get_link(resource));
1051 bind_workspace_manager(struct wl_client *client,
1052 void *data, uint32_t version, uint32_t id)
1054 struct desktop_shell *shell = data;
1055 struct wl_resource *resource;
1057 resource = wl_resource_create(client,
1058 &workspace_manager_interface, 1, id);
1060 if (resource == NULL) {
1061 weston_log("couldn't add workspace manager object");
1065 wl_resource_set_implementation(resource,
1066 &workspace_manager_implementation,
1067 shell, unbind_resource);
1068 wl_list_insert(&shell->workspaces.client_list,
1069 wl_resource_get_link(resource));
1071 workspace_manager_send_state(resource,
1072 shell->workspaces.current,
1073 shell->workspaces.num);
1077 touch_move_grab_down(struct weston_touch_grab *grab, uint32_t time,
1078 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1083 touch_move_grab_up(struct weston_touch_grab *grab, uint32_t time, int touch_id)
1085 struct shell_touch_grab *shell_grab = container_of(grab,
1086 struct shell_touch_grab,
1089 if (grab->touch->seat->num_tp == 0)
1090 shell_touch_grab_end(shell_grab);
1094 touch_move_grab_motion(struct weston_touch_grab *grab, uint32_t time,
1095 int touch_id, wl_fixed_t sx, wl_fixed_t sy)
1097 struct weston_touch_move_grab *move = (struct weston_touch_move_grab *) grab;
1098 struct shell_surface *shsurf = move->base.shsurf;
1099 struct weston_surface *es;
1100 int dx = wl_fixed_to_int(grab->touch->grab_x + move->dx);
1101 int dy = wl_fixed_to_int(grab->touch->grab_y + move->dy);
1106 es = shsurf->surface;
1108 weston_surface_configure(es, dx, dy,
1109 es->geometry.width, es->geometry.height);
1111 weston_compositor_schedule_repaint(es->compositor);
1114 static const struct weston_touch_grab_interface touch_move_grab_interface = {
1115 touch_move_grab_down,
1117 touch_move_grab_motion,
1121 surface_touch_move(struct shell_surface *shsurf, struct weston_seat *seat)
1123 struct weston_touch_move_grab *move;
1128 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1131 move = malloc(sizeof *move);
1135 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1136 seat->touch->grab_x;
1137 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1138 seat->touch->grab_y;
1140 shell_touch_grab_start(&move->base, &touch_move_grab_interface, shsurf,
1147 noop_grab_focus(struct weston_pointer_grab *grab)
1152 move_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1154 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1155 struct weston_pointer *pointer = grab->pointer;
1156 struct shell_surface *shsurf = move->base.shsurf;
1157 struct weston_surface *es;
1158 int dx = wl_fixed_to_int(pointer->x + move->dx);
1159 int dy = wl_fixed_to_int(pointer->y + move->dy);
1164 es = shsurf->surface;
1166 weston_surface_configure(es, dx, dy,
1167 es->geometry.width, es->geometry.height);
1169 weston_compositor_schedule_repaint(es->compositor);
1173 move_grab_button(struct weston_pointer_grab *grab,
1174 uint32_t time, uint32_t button, uint32_t state_w)
1176 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1178 struct weston_pointer *pointer = grab->pointer;
1179 enum wl_pointer_button_state state = state_w;
1181 if (pointer->button_count == 0 &&
1182 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1183 shell_grab_end(shell_grab);
1188 static const struct weston_pointer_grab_interface move_grab_interface = {
1195 surface_move(struct shell_surface *shsurf, struct weston_seat *seat)
1197 struct weston_move_grab *move;
1202 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1205 move = malloc(sizeof *move);
1209 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1210 seat->pointer->grab_x;
1211 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1212 seat->pointer->grab_y;
1214 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1215 seat->pointer, DESKTOP_SHELL_CURSOR_MOVE);
1221 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1222 struct wl_resource *seat_resource, uint32_t serial)
1224 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1225 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1226 struct weston_surface *surface;
1228 if (seat->pointer &&
1229 seat->pointer->button_count > 0 &&
1230 seat->pointer->grab_serial == serial) {
1231 surface = weston_surface_get_main_surface(seat->pointer->focus);
1232 if ((surface == shsurf->surface) &&
1233 (surface_move(shsurf, seat) < 0))
1234 wl_resource_post_no_memory(resource);
1235 } else if (seat->touch &&
1236 seat->touch->grab_serial == serial) {
1237 surface = weston_surface_get_main_surface(seat->touch->focus);
1238 if ((surface == shsurf->surface) &&
1239 (surface_touch_move(shsurf, seat) < 0))
1240 wl_resource_post_no_memory(resource);
1244 struct weston_resize_grab {
1245 struct shell_grab base;
1247 int32_t width, height;
1251 resize_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1253 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1254 struct weston_pointer *pointer = grab->pointer;
1255 struct shell_surface *shsurf = resize->base.shsurf;
1256 int32_t width, height;
1257 wl_fixed_t from_x, from_y;
1258 wl_fixed_t to_x, to_y;
1263 weston_surface_from_global_fixed(shsurf->surface,
1264 pointer->grab_x, pointer->grab_y,
1266 weston_surface_from_global_fixed(shsurf->surface,
1267 pointer->x, pointer->y, &to_x, &to_y);
1269 width = resize->width;
1270 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1271 width += wl_fixed_to_int(from_x - to_x);
1272 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1273 width += wl_fixed_to_int(to_x - from_x);
1276 height = resize->height;
1277 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1278 height += wl_fixed_to_int(from_y - to_y);
1279 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1280 height += wl_fixed_to_int(to_y - from_y);
1283 shsurf->client->send_configure(shsurf->surface,
1284 resize->edges, width, height);
1288 send_configure(struct weston_surface *surface,
1289 uint32_t edges, int32_t width, int32_t height)
1291 struct shell_surface *shsurf = get_shell_surface(surface);
1293 wl_shell_surface_send_configure(shsurf->resource,
1294 edges, width, height);
1297 static const struct weston_shell_client shell_client = {
1302 resize_grab_button(struct weston_pointer_grab *grab,
1303 uint32_t time, uint32_t button, uint32_t state_w)
1305 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1306 struct weston_pointer *pointer = grab->pointer;
1307 enum wl_pointer_button_state state = state_w;
1309 if (pointer->button_count == 0 &&
1310 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1311 shell_grab_end(&resize->base);
1316 static const struct weston_pointer_grab_interface resize_grab_interface = {
1323 * Returns the bounding box of a surface and all its sub-surfaces,
1324 * in the surface coordinates system. */
1326 surface_subsurfaces_boundingbox(struct weston_surface *surface, int32_t *x,
1327 int32_t *y, int32_t *w, int32_t *h) {
1328 pixman_region32_t region;
1329 pixman_box32_t *box;
1330 struct weston_subsurface *subsurface;
1332 pixman_region32_init_rect(®ion, 0, 0,
1333 surface->geometry.width,
1334 surface->geometry.height);
1336 wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
1337 pixman_region32_union_rect(®ion, ®ion,
1338 subsurface->position.x,
1339 subsurface->position.y,
1340 subsurface->surface->geometry.width,
1341 subsurface->surface->geometry.height);
1344 box = pixman_region32_extents(®ion);
1350 *w = box->x2 - box->x1;
1352 *h = box->y2 - box->y1;
1354 pixman_region32_fini(®ion);
1358 surface_resize(struct shell_surface *shsurf,
1359 struct weston_seat *seat, uint32_t edges)
1361 struct weston_resize_grab *resize;
1363 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1364 shsurf->type == SHELL_SURFACE_MAXIMIZED)
1367 if (edges == 0 || edges > 15 ||
1368 (edges & 3) == 3 || (edges & 12) == 12)
1371 resize = malloc(sizeof *resize);
1375 resize->edges = edges;
1376 surface_subsurfaces_boundingbox(shsurf->surface, NULL, NULL,
1377 &resize->width, &resize->height);
1379 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1380 seat->pointer, edges);
1386 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1387 struct wl_resource *seat_resource, uint32_t serial,
1390 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
1391 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1392 struct weston_surface *surface;
1394 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1397 surface = weston_surface_get_main_surface(seat->pointer->focus);
1398 if (seat->pointer->button_count == 0 ||
1399 seat->pointer->grab_serial != serial ||
1400 surface != shsurf->surface)
1403 if (surface_resize(shsurf, seat, edges) < 0)
1404 wl_resource_post_no_memory(resource);
1408 end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer);
1411 busy_cursor_grab_focus(struct weston_pointer_grab *base)
1413 struct shell_grab *grab = (struct shell_grab *) base;
1414 struct weston_pointer *pointer = base->pointer;
1415 struct weston_surface *surface;
1418 surface = weston_compositor_pick_surface(pointer->seat->compositor,
1419 pointer->x, pointer->y,
1422 if (!grab->shsurf || grab->shsurf->surface != surface)
1423 end_busy_cursor(grab->shsurf, pointer);
1427 busy_cursor_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
1432 busy_cursor_grab_button(struct weston_pointer_grab *base,
1433 uint32_t time, uint32_t button, uint32_t state)
1435 struct shell_grab *grab = (struct shell_grab *) base;
1436 struct shell_surface *shsurf = grab->shsurf;
1437 struct weston_seat *seat = grab->grab.pointer->seat;
1439 if (shsurf && button == BTN_LEFT && state) {
1440 activate(shsurf->shell, shsurf->surface, seat);
1441 surface_move(shsurf, seat);
1442 } else if (shsurf && button == BTN_RIGHT && state) {
1443 activate(shsurf->shell, shsurf->surface, seat);
1444 surface_rotate(shsurf, seat);
1448 static const struct weston_pointer_grab_interface busy_cursor_grab_interface = {
1449 busy_cursor_grab_focus,
1450 busy_cursor_grab_motion,
1451 busy_cursor_grab_button,
1455 set_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
1457 struct shell_grab *grab;
1459 grab = malloc(sizeof *grab);
1463 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1464 DESKTOP_SHELL_CURSOR_BUSY);
1468 end_busy_cursor(struct shell_surface *shsurf, struct weston_pointer *pointer)
1470 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1472 if (grab->grab.interface == &busy_cursor_grab_interface &&
1473 grab->shsurf == shsurf) {
1474 shell_grab_end(grab);
1480 ping_timer_destroy(struct shell_surface *shsurf)
1482 if (!shsurf || !shsurf->ping_timer)
1485 if (shsurf->ping_timer->source)
1486 wl_event_source_remove(shsurf->ping_timer->source);
1488 free(shsurf->ping_timer);
1489 shsurf->ping_timer = NULL;
1493 ping_timeout_handler(void *data)
1495 struct shell_surface *shsurf = data;
1496 struct weston_seat *seat;
1498 /* Client is not responding */
1499 shsurf->unresponsive = 1;
1501 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1502 if (seat->pointer->focus == shsurf->surface)
1503 set_busy_cursor(shsurf, seat->pointer);
1509 ping_handler(struct weston_surface *surface, uint32_t serial)
1511 struct shell_surface *shsurf = get_shell_surface(surface);
1512 struct wl_event_loop *loop;
1513 int ping_timeout = 200;
1517 if (!shsurf->resource)
1520 if (shsurf->surface == shsurf->shell->grab_surface)
1523 if (!shsurf->ping_timer) {
1524 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1525 if (!shsurf->ping_timer)
1528 shsurf->ping_timer->serial = serial;
1529 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1530 shsurf->ping_timer->source =
1531 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1532 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1534 wl_shell_surface_send_ping(shsurf->resource, serial);
1539 handle_pointer_focus(struct wl_listener *listener, void *data)
1541 struct weston_pointer *pointer = data;
1542 struct weston_surface *surface = pointer->focus;
1543 struct weston_compositor *compositor;
1544 struct shell_surface *shsurf;
1550 compositor = surface->compositor;
1551 shsurf = get_shell_surface(surface);
1553 if (shsurf && shsurf->unresponsive) {
1554 set_busy_cursor(shsurf, pointer);
1556 serial = wl_display_next_serial(compositor->wl_display);
1557 ping_handler(surface, serial);
1562 create_pointer_focus_listener(struct weston_seat *seat)
1564 struct wl_listener *listener;
1569 listener = malloc(sizeof *listener);
1570 listener->notify = handle_pointer_focus;
1571 wl_signal_add(&seat->pointer->focus_signal, listener);
1575 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1578 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1579 struct weston_seat *seat;
1580 struct weston_compositor *ec = shsurf->surface->compositor;
1582 if (shsurf->ping_timer == NULL)
1583 /* Just ignore unsolicited pong. */
1586 if (shsurf->ping_timer->serial == serial) {
1587 shsurf->unresponsive = 0;
1588 wl_list_for_each(seat, &ec->seat_list, link) {
1590 end_busy_cursor(shsurf, seat->pointer);
1592 ping_timer_destroy(shsurf);
1597 set_title(struct shell_surface *shsurf, const char *title)
1599 free(shsurf->title);
1600 shsurf->title = strdup(title);
1604 shell_surface_set_title(struct wl_client *client,
1605 struct wl_resource *resource, const char *title)
1607 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1609 set_title(shsurf, title);
1613 shell_surface_set_class(struct wl_client *client,
1614 struct wl_resource *resource, const char *class)
1616 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1618 free(shsurf->class);
1619 shsurf->class = strdup(class);
1622 static struct weston_output *
1623 get_default_output(struct weston_compositor *compositor)
1625 return container_of(compositor->output_list.next,
1626 struct weston_output, link);
1630 restore_output_mode(struct weston_output *output)
1632 if (output->current_mode != output->original_mode ||
1633 (int32_t)output->current_scale != output->original_scale)
1634 weston_output_switch_mode(output,
1635 output->original_mode,
1636 output->original_scale,
1637 WESTON_MODE_SWITCH_RESTORE_NATIVE);
1641 restore_all_output_modes(struct weston_compositor *compositor)
1643 struct weston_output *output;
1645 wl_list_for_each(output, &compositor->output_list, link)
1646 restore_output_mode(output);
1650 shell_unset_fullscreen(struct shell_surface *shsurf)
1652 struct workspace *ws;
1653 /* undo all fullscreen things here */
1654 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1655 shell_surface_is_top_fullscreen(shsurf)) {
1656 restore_output_mode(shsurf->fullscreen_output);
1658 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1659 shsurf->fullscreen.framerate = 0;
1660 wl_list_remove(&shsurf->fullscreen.transform.link);
1661 wl_list_init(&shsurf->fullscreen.transform.link);
1662 if (shsurf->fullscreen.black_surface)
1663 weston_surface_destroy(shsurf->fullscreen.black_surface);
1664 shsurf->fullscreen.black_surface = NULL;
1665 shsurf->fullscreen_output = NULL;
1666 weston_surface_set_position(shsurf->surface,
1667 shsurf->saved_x, shsurf->saved_y);
1668 if (shsurf->saved_rotation_valid) {
1669 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1670 &shsurf->rotation.transform.link);
1671 shsurf->saved_rotation_valid = false;
1674 ws = get_current_workspace(shsurf->shell);
1675 wl_list_remove(&shsurf->surface->layer_link);
1676 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1680 shell_unset_maximized(struct shell_surface *shsurf)
1682 struct workspace *ws;
1683 /* undo all maximized things here */
1684 shsurf->output = get_default_output(shsurf->surface->compositor);
1685 weston_surface_set_position(shsurf->surface,
1689 if (shsurf->saved_rotation_valid) {
1690 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1691 &shsurf->rotation.transform.link);
1692 shsurf->saved_rotation_valid = false;
1695 ws = get_current_workspace(shsurf->shell);
1696 wl_list_remove(&shsurf->surface->layer_link);
1697 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1701 reset_shell_surface_type(struct shell_surface *surface)
1703 switch (surface->type) {
1704 case SHELL_SURFACE_FULLSCREEN:
1705 shell_unset_fullscreen(surface);
1707 case SHELL_SURFACE_MAXIMIZED:
1708 shell_unset_maximized(surface);
1710 case SHELL_SURFACE_NONE:
1711 case SHELL_SURFACE_TOPLEVEL:
1712 case SHELL_SURFACE_TRANSIENT:
1713 case SHELL_SURFACE_POPUP:
1714 case SHELL_SURFACE_XWAYLAND:
1718 surface->type = SHELL_SURFACE_NONE;
1723 set_surface_type(struct shell_surface *shsurf)
1725 struct weston_surface *surface = shsurf->surface;
1726 struct weston_surface *pes = shsurf->parent;
1728 reset_shell_surface_type(shsurf);
1730 shsurf->type = shsurf->next_type;
1731 shsurf->next_type = SHELL_SURFACE_NONE;
1733 switch (shsurf->type) {
1734 case SHELL_SURFACE_TOPLEVEL:
1736 case SHELL_SURFACE_TRANSIENT:
1737 weston_surface_set_position(surface,
1738 pes->geometry.x + shsurf->transient.x,
1739 pes->geometry.y + shsurf->transient.y);
1742 case SHELL_SURFACE_MAXIMIZED:
1743 case SHELL_SURFACE_FULLSCREEN:
1744 shsurf->saved_x = surface->geometry.x;
1745 shsurf->saved_y = surface->geometry.y;
1746 shsurf->saved_position_valid = true;
1748 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1749 wl_list_remove(&shsurf->rotation.transform.link);
1750 wl_list_init(&shsurf->rotation.transform.link);
1751 weston_surface_geometry_dirty(shsurf->surface);
1752 shsurf->saved_rotation_valid = true;
1756 case SHELL_SURFACE_XWAYLAND:
1757 weston_surface_set_position(surface, shsurf->transient.x,
1758 shsurf->transient.y);
1767 set_toplevel(struct shell_surface *shsurf)
1769 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1773 shell_surface_set_toplevel(struct wl_client *client,
1774 struct wl_resource *resource)
1776 struct shell_surface *surface = wl_resource_get_user_data(resource);
1778 set_toplevel(surface);
1782 set_transient(struct shell_surface *shsurf,
1783 struct weston_surface *parent, int x, int y, uint32_t flags)
1785 /* assign to parents output */
1786 shsurf->parent = parent;
1787 shsurf->transient.x = x;
1788 shsurf->transient.y = y;
1789 shsurf->transient.flags = flags;
1790 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1794 shell_surface_set_transient(struct wl_client *client,
1795 struct wl_resource *resource,
1796 struct wl_resource *parent_resource,
1797 int x, int y, uint32_t flags)
1799 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1800 struct weston_surface *parent =
1801 wl_resource_get_user_data(parent_resource);
1803 set_transient(shsurf, parent, x, y, flags);
1806 static struct desktop_shell *
1807 shell_surface_get_shell(struct shell_surface *shsurf)
1809 return shsurf->shell;
1813 get_output_panel_height(struct desktop_shell *shell,
1814 struct weston_output *output)
1816 struct weston_surface *surface;
1817 int panel_height = 0;
1822 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1823 if (surface->output == output) {
1824 panel_height = surface->geometry.height;
1829 return panel_height;
1833 shell_surface_set_maximized(struct wl_client *client,
1834 struct wl_resource *resource,
1835 struct wl_resource *output_resource )
1837 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
1838 struct weston_surface *es = shsurf->surface;
1839 struct desktop_shell *shell = NULL;
1840 uint32_t edges = 0, panel_height = 0;
1842 /* get the default output, if the client set it as NULL
1843 check whether the ouput is available */
1844 if (output_resource)
1845 shsurf->output = wl_resource_get_user_data(output_resource);
1846 else if (es->output)
1847 shsurf->output = es->output;
1849 shsurf->output = get_default_output(es->compositor);
1851 shell = shell_surface_get_shell(shsurf);
1852 panel_height = get_output_panel_height(shell, shsurf->output);
1853 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1855 shsurf->client->send_configure(shsurf->surface, edges,
1856 shsurf->output->width,
1857 shsurf->output->height - panel_height);
1859 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1863 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
1865 static struct weston_surface *
1866 create_black_surface(struct weston_compositor *ec,
1867 struct weston_surface *fs_surface,
1868 float x, float y, int w, int h)
1870 struct weston_surface *surface = NULL;
1872 surface = weston_surface_create(ec);
1873 if (surface == NULL) {
1874 weston_log("no memory\n");
1878 surface->configure = black_surface_configure;
1879 surface->configure_private = fs_surface;
1880 weston_surface_configure(surface, x, y, w, h);
1881 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1882 pixman_region32_fini(&surface->opaque);
1883 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1884 pixman_region32_fini(&surface->input);
1885 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
1890 /* Create black surface and append it to the associated fullscreen surface.
1891 * Handle size dismatch and positioning according to the method. */
1893 shell_configure_fullscreen(struct shell_surface *shsurf)
1895 struct weston_output *output = shsurf->fullscreen_output;
1896 struct weston_surface *surface = shsurf->surface;
1897 struct weston_matrix *matrix;
1898 float scale, output_aspect, surface_aspect, x, y;
1899 int32_t surf_x, surf_y, surf_width, surf_height;
1901 if (shsurf->fullscreen.type != WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER)
1902 restore_output_mode(output);
1904 if (!shsurf->fullscreen.black_surface)
1905 shsurf->fullscreen.black_surface =
1906 create_black_surface(surface->compositor,
1908 output->x, output->y,
1912 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1913 wl_list_insert(&surface->layer_link,
1914 &shsurf->fullscreen.black_surface->layer_link);
1915 shsurf->fullscreen.black_surface->output = output;
1917 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y,
1918 &surf_width, &surf_height);
1920 switch (shsurf->fullscreen.type) {
1921 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1922 if (surface->buffer_ref.buffer)
1923 center_on_output(surface, shsurf->fullscreen_output);
1925 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1926 /* 1:1 mapping between surface and output dimensions */
1927 if (output->width == surf_width &&
1928 output->height == surf_height) {
1929 weston_surface_set_position(surface, output->x - surf_x,
1930 output->y - surf_y);
1934 matrix = &shsurf->fullscreen.transform.matrix;
1935 weston_matrix_init(matrix);
1937 output_aspect = (float) output->width /
1938 (float) output->height;
1939 surface_aspect = (float) surface->geometry.width /
1940 (float) surface->geometry.height;
1941 if (output_aspect < surface_aspect)
1942 scale = (float) output->width /
1945 scale = (float) output->height /
1946 (float) surf_height;
1948 weston_matrix_scale(matrix, scale, scale, 1);
1949 wl_list_remove(&shsurf->fullscreen.transform.link);
1950 wl_list_insert(&surface->geometry.transformation_list,
1951 &shsurf->fullscreen.transform.link);
1952 x = output->x + (output->width - surf_width * scale) / 2 - surf_x;
1953 y = output->y + (output->height - surf_height * scale) / 2 - surf_y;
1954 weston_surface_set_position(surface, x, y);
1957 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1958 if (shell_surface_is_top_fullscreen(shsurf)) {
1959 struct weston_mode mode = {0,
1960 surf_width * surface->buffer_scale,
1961 surf_height * surface->buffer_scale,
1962 shsurf->fullscreen.framerate};
1964 if (weston_output_switch_mode(output, &mode, surface->buffer_scale,
1965 WESTON_MODE_SWITCH_SET_TEMPORARY) == 0) {
1966 weston_surface_set_position(surface,
1968 output->y - surf_y);
1969 weston_surface_configure(shsurf->fullscreen.black_surface,
1976 restore_output_mode(output);
1977 center_on_output(surface, output);
1981 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1982 center_on_output(surface, output);
1989 /* make the fullscreen and black surface at the top */
1991 shell_stack_fullscreen(struct shell_surface *shsurf)
1993 struct weston_output *output = shsurf->fullscreen_output;
1994 struct weston_surface *surface = shsurf->surface;
1995 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1997 wl_list_remove(&surface->layer_link);
1998 wl_list_insert(&shell->fullscreen_layer.surface_list,
1999 &surface->layer_link);
2000 weston_surface_damage(surface);
2002 if (!shsurf->fullscreen.black_surface)
2003 shsurf->fullscreen.black_surface =
2004 create_black_surface(surface->compositor,
2006 output->x, output->y,
2010 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
2011 wl_list_insert(&surface->layer_link,
2012 &shsurf->fullscreen.black_surface->layer_link);
2013 weston_surface_damage(shsurf->fullscreen.black_surface);
2017 shell_map_fullscreen(struct shell_surface *shsurf)
2019 shell_stack_fullscreen(shsurf);
2020 shell_configure_fullscreen(shsurf);
2024 set_fullscreen(struct shell_surface *shsurf,
2027 struct weston_output *output)
2029 struct weston_surface *es = shsurf->surface;
2032 shsurf->output = output;
2033 else if (es->output)
2034 shsurf->output = es->output;
2036 shsurf->output = get_default_output(es->compositor);
2038 shsurf->fullscreen_output = shsurf->output;
2039 shsurf->fullscreen.type = method;
2040 shsurf->fullscreen.framerate = framerate;
2041 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
2043 shsurf->client->send_configure(shsurf->surface, 0,
2044 shsurf->output->width,
2045 shsurf->output->height);
2049 shell_surface_set_fullscreen(struct wl_client *client,
2050 struct wl_resource *resource,
2053 struct wl_resource *output_resource)
2055 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2056 struct weston_output *output;
2058 if (output_resource)
2059 output = wl_resource_get_user_data(output_resource);
2063 set_fullscreen(shsurf, method, framerate, output);
2067 set_xwayland(struct shell_surface *shsurf, int x, int y, uint32_t flags)
2069 /* XXX: using the same fields for transient type */
2070 shsurf->transient.x = x;
2071 shsurf->transient.y = y;
2072 shsurf->transient.flags = flags;
2073 shsurf->next_type = SHELL_SURFACE_XWAYLAND;
2076 static const struct weston_pointer_grab_interface popup_grab_interface;
2079 destroy_shell_seat(struct wl_listener *listener, void *data)
2081 struct shell_seat *shseat =
2082 container_of(listener,
2083 struct shell_seat, seat_destroy_listener);
2084 struct shell_surface *shsurf, *prev = NULL;
2086 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
2087 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2088 shseat->popup_grab.client = NULL;
2090 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2091 shsurf->popup.shseat = NULL;
2093 wl_list_init(&prev->popup.grab_link);
2097 wl_list_init(&prev->popup.grab_link);
2100 wl_list_remove(&shseat->seat_destroy_listener.link);
2104 static struct shell_seat *
2105 create_shell_seat(struct weston_seat *seat)
2107 struct shell_seat *shseat;
2109 shseat = calloc(1, sizeof *shseat);
2111 weston_log("no memory to allocate shell seat\n");
2115 shseat->seat = seat;
2116 wl_list_init(&shseat->popup_grab.surfaces_list);
2118 shseat->seat_destroy_listener.notify = destroy_shell_seat;
2119 wl_signal_add(&seat->destroy_signal,
2120 &shseat->seat_destroy_listener);
2125 static struct shell_seat *
2126 get_shell_seat(struct weston_seat *seat)
2128 struct wl_listener *listener;
2130 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
2131 if (listener == NULL)
2132 return create_shell_seat(seat);
2134 return container_of(listener,
2135 struct shell_seat, seat_destroy_listener);
2139 popup_grab_focus(struct weston_pointer_grab *grab)
2141 struct weston_pointer *pointer = grab->pointer;
2142 struct weston_surface *surface;
2143 struct shell_seat *shseat =
2144 container_of(grab, struct shell_seat, popup_grab.grab);
2145 struct wl_client *client = shseat->popup_grab.client;
2148 surface = weston_compositor_pick_surface(pointer->seat->compositor,
2149 pointer->x, pointer->y,
2152 if (surface && wl_resource_get_client(surface->resource) == client) {
2153 weston_pointer_set_focus(pointer, surface, sx, sy);
2155 weston_pointer_set_focus(pointer, NULL,
2156 wl_fixed_from_int(0),
2157 wl_fixed_from_int(0));
2162 popup_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
2164 struct weston_pointer *pointer = grab->pointer;
2165 struct wl_resource *resource;
2168 wl_resource_for_each(resource, &pointer->focus_resource_list) {
2169 weston_surface_from_global_fixed(pointer->focus,
2170 pointer->x, pointer->y,
2172 wl_pointer_send_motion(resource, time, sx, sy);
2177 popup_grab_button(struct weston_pointer_grab *grab,
2178 uint32_t time, uint32_t button, uint32_t state_w)
2180 struct wl_resource *resource;
2181 struct shell_seat *shseat =
2182 container_of(grab, struct shell_seat, popup_grab.grab);
2183 struct wl_display *display = shseat->seat->compositor->wl_display;
2184 enum wl_pointer_button_state state = state_w;
2186 struct wl_list *resource_list;
2188 resource_list = &grab->pointer->focus_resource_list;
2189 if (!wl_list_empty(resource_list)) {
2190 serial = wl_display_get_serial(display);
2191 wl_resource_for_each(resource, resource_list) {
2192 wl_pointer_send_button(resource, serial,
2193 time, button, state);
2195 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
2196 (shseat->popup_grab.initial_up ||
2197 time - shseat->seat->pointer->grab_time > 500)) {
2198 popup_grab_end(grab->pointer);
2201 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
2202 shseat->popup_grab.initial_up = 1;
2205 static const struct weston_pointer_grab_interface popup_grab_interface = {
2212 popup_grab_end(struct weston_pointer *pointer)
2214 struct weston_pointer_grab *grab = pointer->grab;
2215 struct shell_seat *shseat =
2216 container_of(grab, struct shell_seat, popup_grab.grab);
2217 struct shell_surface *shsurf;
2218 struct shell_surface *prev = NULL;
2220 if (pointer->grab->interface == &popup_grab_interface) {
2221 weston_pointer_end_grab(grab->pointer);
2222 shseat->popup_grab.client = NULL;
2223 shseat->popup_grab.grab.interface = NULL;
2224 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
2225 /* Send the popup_done event to all the popups open */
2226 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
2227 wl_shell_surface_send_popup_done(shsurf->resource);
2228 shsurf->popup.shseat = NULL;
2230 wl_list_init(&prev->popup.grab_link);
2234 wl_list_init(&prev->popup.grab_link);
2235 wl_list_init(&shseat->popup_grab.surfaces_list);
2240 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2242 struct weston_seat *seat = shseat->seat;
2244 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2245 shseat->popup_grab.client = wl_resource_get_client(shsurf->resource);
2246 shseat->popup_grab.grab.interface = &popup_grab_interface;
2247 /* We must make sure here that this popup was opened after
2248 * a mouse press, and not just by moving around with other
2249 * popups already open. */
2250 if (shseat->seat->pointer->button_count > 0)
2251 shseat->popup_grab.initial_up = 0;
2253 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2254 weston_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2256 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2261 remove_popup_grab(struct shell_surface *shsurf)
2263 struct shell_seat *shseat = shsurf->popup.shseat;
2265 wl_list_remove(&shsurf->popup.grab_link);
2266 wl_list_init(&shsurf->popup.grab_link);
2267 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2268 weston_pointer_end_grab(shseat->popup_grab.grab.pointer);
2269 shseat->popup_grab.grab.interface = NULL;
2274 shell_map_popup(struct shell_surface *shsurf)
2276 struct shell_seat *shseat = shsurf->popup.shseat;
2277 struct weston_surface *es = shsurf->surface;
2278 struct weston_surface *parent = shsurf->parent;
2280 es->output = parent->output;
2282 weston_surface_set_transform_parent(es, parent);
2283 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2284 weston_surface_update_transform(es);
2286 if (shseat->seat->pointer->grab_serial == shsurf->popup.serial) {
2287 add_popup_grab(shsurf, shseat);
2289 wl_shell_surface_send_popup_done(shsurf->resource);
2290 shseat->popup_grab.client = NULL;
2295 shell_surface_set_popup(struct wl_client *client,
2296 struct wl_resource *resource,
2297 struct wl_resource *seat_resource,
2299 struct wl_resource *parent_resource,
2300 int32_t x, int32_t y, uint32_t flags)
2302 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2304 shsurf->type = SHELL_SURFACE_POPUP;
2305 shsurf->parent = wl_resource_get_user_data(parent_resource);
2306 shsurf->popup.shseat = get_shell_seat(wl_resource_get_user_data(seat_resource));
2307 shsurf->popup.serial = serial;
2308 shsurf->popup.x = x;
2309 shsurf->popup.y = y;
2312 static const struct wl_shell_surface_interface shell_surface_implementation = {
2315 shell_surface_resize,
2316 shell_surface_set_toplevel,
2317 shell_surface_set_transient,
2318 shell_surface_set_fullscreen,
2319 shell_surface_set_popup,
2320 shell_surface_set_maximized,
2321 shell_surface_set_title,
2322 shell_surface_set_class
2326 destroy_shell_surface(struct shell_surface *shsurf)
2328 wl_signal_emit(&shsurf->destroy_signal, shsurf);
2330 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2331 remove_popup_grab(shsurf);
2334 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2335 shell_surface_is_top_fullscreen(shsurf))
2336 restore_output_mode (shsurf->fullscreen_output);
2338 if (shsurf->fullscreen.black_surface)
2339 weston_surface_destroy(shsurf->fullscreen.black_surface);
2341 /* As destroy_resource() use wl_list_for_each_safe(),
2342 * we can always remove the listener.
2344 wl_list_remove(&shsurf->surface_destroy_listener.link);
2345 shsurf->surface->configure = NULL;
2346 ping_timer_destroy(shsurf);
2347 free(shsurf->title);
2349 wl_list_remove(&shsurf->link);
2354 shell_destroy_shell_surface(struct wl_resource *resource)
2356 struct shell_surface *shsurf = wl_resource_get_user_data(resource);
2358 destroy_shell_surface(shsurf);
2362 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
2364 struct shell_surface *shsurf = container_of(listener,
2365 struct shell_surface,
2366 surface_destroy_listener);
2368 if (shsurf->resource)
2369 wl_resource_destroy(shsurf->resource);
2371 destroy_shell_surface(shsurf);
2375 shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
2377 static struct shell_surface *
2378 get_shell_surface(struct weston_surface *surface)
2380 if (surface->configure == shell_surface_configure)
2381 return surface->configure_private;
2386 static struct shell_surface *
2387 create_shell_surface(void *shell, struct weston_surface *surface,
2388 const struct weston_shell_client *client)
2390 struct shell_surface *shsurf;
2392 if (surface->configure) {
2393 weston_log("surface->configure already set\n");
2397 shsurf = calloc(1, sizeof *shsurf);
2399 weston_log("no memory to allocate shell surface\n");
2403 surface->configure = shell_surface_configure;
2404 surface->configure_private = shsurf;
2406 shsurf->shell = (struct desktop_shell *) shell;
2407 shsurf->unresponsive = 0;
2408 shsurf->saved_position_valid = false;
2409 shsurf->saved_rotation_valid = false;
2410 shsurf->surface = surface;
2411 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2412 shsurf->fullscreen.framerate = 0;
2413 shsurf->fullscreen.black_surface = NULL;
2414 shsurf->ping_timer = NULL;
2415 wl_list_init(&shsurf->fullscreen.transform.link);
2417 wl_signal_init(&shsurf->destroy_signal);
2418 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2419 wl_signal_add(&surface->destroy_signal,
2420 &shsurf->surface_destroy_listener);
2422 /* init link so its safe to always remove it in destroy_shell_surface */
2423 wl_list_init(&shsurf->link);
2424 wl_list_init(&shsurf->popup.grab_link);
2426 /* empty when not in use */
2427 wl_list_init(&shsurf->rotation.transform.link);
2428 weston_matrix_init(&shsurf->rotation.rotation);
2430 wl_list_init(&shsurf->workspace_transform.link);
2432 shsurf->type = SHELL_SURFACE_NONE;
2433 shsurf->next_type = SHELL_SURFACE_NONE;
2435 shsurf->client = client;
2441 shell_get_shell_surface(struct wl_client *client,
2442 struct wl_resource *resource,
2444 struct wl_resource *surface_resource)
2446 struct weston_surface *surface =
2447 wl_resource_get_user_data(surface_resource);
2448 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2449 struct shell_surface *shsurf;
2451 if (get_shell_surface(surface)) {
2452 wl_resource_post_error(surface_resource,
2453 WL_DISPLAY_ERROR_INVALID_OBJECT,
2454 "desktop_shell::get_shell_surface already requested");
2458 shsurf = create_shell_surface(shell, surface, &shell_client);
2460 wl_resource_post_error(surface_resource,
2461 WL_DISPLAY_ERROR_INVALID_OBJECT,
2462 "surface->configure already set");
2467 wl_resource_create(client,
2468 &wl_shell_surface_interface, 1, id);
2469 wl_resource_set_implementation(shsurf->resource,
2470 &shell_surface_implementation,
2471 shsurf, shell_destroy_shell_surface);
2474 static const struct wl_shell_interface shell_implementation = {
2475 shell_get_shell_surface
2479 shell_fade(struct desktop_shell *shell, enum fade_type type);
2482 screensaver_timeout(void *data)
2484 struct desktop_shell *shell = data;
2486 shell_fade(shell, FADE_OUT);
2492 handle_screensaver_sigchld(struct weston_process *proc, int status)
2494 struct desktop_shell *shell =
2495 container_of(proc, struct desktop_shell, screensaver.process);
2500 weston_compositor_sleep(shell->compositor);
2504 launch_screensaver(struct desktop_shell *shell)
2506 if (shell->screensaver.binding)
2509 if (!shell->screensaver.path) {
2510 weston_compositor_sleep(shell->compositor);
2514 if (shell->screensaver.process.pid != 0) {
2515 weston_log("old screensaver still running\n");
2519 weston_client_launch(shell->compositor,
2520 &shell->screensaver.process,
2521 shell->screensaver.path,
2522 handle_screensaver_sigchld);
2526 terminate_screensaver(struct desktop_shell *shell)
2528 if (shell->screensaver.process.pid == 0)
2531 kill(shell->screensaver.process.pid, SIGTERM);
2535 configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
2537 struct weston_surface *s, *next;
2542 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2543 if (s->output == es->output && s != es) {
2544 weston_surface_unmap(s);
2545 s->configure = NULL;
2549 weston_surface_configure(es, es->output->x, es->output->y, width, height);
2551 if (wl_list_empty(&es->layer_link)) {
2552 wl_list_insert(&layer->surface_list, &es->layer_link);
2553 weston_compositor_schedule_repaint(es->compositor);
2558 background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2560 struct desktop_shell *shell = es->configure_private;
2562 configure_static_surface(es, &shell->background_layer, width, height);
2566 desktop_shell_set_background(struct wl_client *client,
2567 struct wl_resource *resource,
2568 struct wl_resource *output_resource,
2569 struct wl_resource *surface_resource)
2571 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2572 struct weston_surface *surface =
2573 wl_resource_get_user_data(surface_resource);
2575 if (surface->configure) {
2576 wl_resource_post_error(surface_resource,
2577 WL_DISPLAY_ERROR_INVALID_OBJECT,
2578 "surface role already assigned");
2582 surface->configure = background_configure;
2583 surface->configure_private = shell;
2584 surface->output = wl_resource_get_user_data(output_resource);
2585 desktop_shell_send_configure(resource, 0,
2587 surface->output->width,
2588 surface->output->height);
2592 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2594 struct desktop_shell *shell = es->configure_private;
2596 configure_static_surface(es, &shell->panel_layer, width, height);
2600 desktop_shell_set_panel(struct wl_client *client,
2601 struct wl_resource *resource,
2602 struct wl_resource *output_resource,
2603 struct wl_resource *surface_resource)
2605 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2606 struct weston_surface *surface =
2607 wl_resource_get_user_data(surface_resource);
2609 if (surface->configure) {
2610 wl_resource_post_error(surface_resource,
2611 WL_DISPLAY_ERROR_INVALID_OBJECT,
2612 "surface role already assigned");
2616 surface->configure = panel_configure;
2617 surface->configure_private = shell;
2618 surface->output = wl_resource_get_user_data(output_resource);
2619 desktop_shell_send_configure(resource, 0,
2621 surface->output->width,
2622 surface->output->height);
2626 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
2628 struct desktop_shell *shell = surface->configure_private;
2633 surface->geometry.width = width;
2634 surface->geometry.height = height;
2635 center_on_output(surface, get_default_output(shell->compositor));
2637 if (!weston_surface_is_mapped(surface)) {
2638 wl_list_insert(&shell->lock_layer.surface_list,
2639 &surface->layer_link);
2640 weston_surface_update_transform(surface);
2641 shell_fade(shell, FADE_IN);
2646 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2648 struct desktop_shell *shell =
2649 container_of(listener, struct desktop_shell, lock_surface_listener);
2651 weston_log("lock surface gone\n");
2652 shell->lock_surface = NULL;
2656 desktop_shell_set_lock_surface(struct wl_client *client,
2657 struct wl_resource *resource,
2658 struct wl_resource *surface_resource)
2660 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2661 struct weston_surface *surface =
2662 wl_resource_get_user_data(surface_resource);
2664 shell->prepare_event_sent = false;
2669 shell->lock_surface = surface;
2671 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2672 wl_signal_add(&surface->destroy_signal,
2673 &shell->lock_surface_listener);
2675 surface->configure = lock_surface_configure;
2676 surface->configure_private = shell;
2680 resume_desktop(struct desktop_shell *shell)
2682 struct workspace *ws = get_current_workspace(shell);
2684 terminate_screensaver(shell);
2686 wl_list_remove(&shell->lock_layer.link);
2687 wl_list_insert(&shell->compositor->cursor_layer.link,
2688 &shell->fullscreen_layer.link);
2689 wl_list_insert(&shell->fullscreen_layer.link,
2690 &shell->panel_layer.link);
2691 if (shell->showing_input_panels) {
2692 wl_list_insert(&shell->panel_layer.link,
2693 &shell->input_panel_layer.link);
2694 wl_list_insert(&shell->input_panel_layer.link,
2697 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2700 restore_focus_state(shell, get_current_workspace(shell));
2702 shell->locked = false;
2703 shell_fade(shell, FADE_IN);
2704 weston_compositor_damage_all(shell->compositor);
2708 desktop_shell_unlock(struct wl_client *client,
2709 struct wl_resource *resource)
2711 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2713 shell->prepare_event_sent = false;
2716 resume_desktop(shell);
2720 desktop_shell_set_grab_surface(struct wl_client *client,
2721 struct wl_resource *resource,
2722 struct wl_resource *surface_resource)
2724 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2726 shell->grab_surface = wl_resource_get_user_data(surface_resource);
2730 desktop_shell_desktop_ready(struct wl_client *client,
2731 struct wl_resource *resource)
2733 struct desktop_shell *shell = wl_resource_get_user_data(resource);
2735 shell_fade_startup(shell);
2738 static const struct desktop_shell_interface desktop_shell_implementation = {
2739 desktop_shell_set_background,
2740 desktop_shell_set_panel,
2741 desktop_shell_set_lock_surface,
2742 desktop_shell_unlock,
2743 desktop_shell_set_grab_surface,
2744 desktop_shell_desktop_ready
2747 static enum shell_surface_type
2748 get_shell_surface_type(struct weston_surface *surface)
2750 struct shell_surface *shsurf;
2752 shsurf = get_shell_surface(surface);
2754 return SHELL_SURFACE_NONE;
2755 return shsurf->type;
2759 move_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
2761 struct weston_surface *focus =
2762 (struct weston_surface *) seat->pointer->focus;
2763 struct weston_surface *surface;
2764 struct shell_surface *shsurf;
2766 surface = weston_surface_get_main_surface(focus);
2767 if (surface == NULL)
2770 shsurf = get_shell_surface(surface);
2771 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2772 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2775 surface_move(shsurf, (struct weston_seat *) seat);
2779 touch_move_binding(struct weston_seat *seat, uint32_t time, void *data)
2781 struct weston_surface *focus =
2782 (struct weston_surface *) seat->touch->focus;
2783 struct weston_surface *surface;
2784 struct shell_surface *shsurf;
2786 surface = weston_surface_get_main_surface(focus);
2787 if (surface == NULL)
2790 shsurf = get_shell_surface(surface);
2791 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2792 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2795 surface_touch_move(shsurf, (struct weston_seat *) seat);
2799 resize_binding(struct weston_seat *seat, uint32_t time, uint32_t button, void *data)
2801 struct weston_surface *focus =
2802 (struct weston_surface *) seat->pointer->focus;
2803 struct weston_surface *surface;
2806 struct shell_surface *shsurf;
2808 surface = weston_surface_get_main_surface(focus);
2809 if (surface == NULL)
2812 shsurf = get_shell_surface(surface);
2813 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2814 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2817 weston_surface_from_global(surface,
2818 wl_fixed_to_int(seat->pointer->grab_x),
2819 wl_fixed_to_int(seat->pointer->grab_y),
2822 if (x < surface->geometry.width / 3)
2823 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2824 else if (x < 2 * surface->geometry.width / 3)
2827 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2829 if (y < surface->geometry.height / 3)
2830 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2831 else if (y < 2 * surface->geometry.height / 3)
2834 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2836 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2840 surface_opacity_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
2841 wl_fixed_t value, void *data)
2844 struct shell_surface *shsurf;
2845 struct weston_surface *focus =
2846 (struct weston_surface *) seat->pointer->focus;
2847 struct weston_surface *surface;
2849 /* XXX: broken for windows containing sub-surfaces */
2850 surface = weston_surface_get_main_surface(focus);
2851 if (surface == NULL)
2854 shsurf = get_shell_surface(surface);
2858 surface->alpha -= wl_fixed_to_double(value) * step;
2860 if (surface->alpha > 1.0)
2861 surface->alpha = 1.0;
2862 if (surface->alpha < step)
2863 surface->alpha = step;
2865 weston_surface_geometry_dirty(surface);
2866 weston_surface_damage(surface);
2870 do_zoom(struct weston_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2873 struct weston_seat *ws = (struct weston_seat *) seat;
2874 struct weston_compositor *compositor = ws->compositor;
2875 struct weston_output *output;
2878 wl_list_for_each(output, &compositor->output_list, link) {
2879 if (pixman_region32_contains_point(&output->region,
2880 wl_fixed_to_double(seat->pointer->x),
2881 wl_fixed_to_double(seat->pointer->y),
2883 if (key == KEY_PAGEUP)
2884 increment = output->zoom.increment;
2885 else if (key == KEY_PAGEDOWN)
2886 increment = -output->zoom.increment;
2887 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2888 /* For every pixel zoom 20th of a step */
2889 increment = output->zoom.increment *
2890 -wl_fixed_to_double(value) / 20.0;
2894 output->zoom.level += increment;
2896 if (output->zoom.level < 0.0)
2897 output->zoom.level = 0.0;
2898 else if (output->zoom.level > output->zoom.max_level)
2899 output->zoom.level = output->zoom.max_level;
2900 else if (!output->zoom.active) {
2901 output->zoom.active = 1;
2902 output->disable_planes++;
2905 output->zoom.spring_z.target = output->zoom.level;
2907 weston_output_update_zoom(output, output->zoom.type);
2913 zoom_axis_binding(struct weston_seat *seat, uint32_t time, uint32_t axis,
2914 wl_fixed_t value, void *data)
2916 do_zoom(seat, time, 0, axis, value);
2920 zoom_key_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2923 do_zoom(seat, time, key, 0, 0);
2927 terminate_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2930 struct weston_compositor *compositor = data;
2932 wl_display_terminate(compositor->wl_display);
2936 rotate_grab_motion(struct weston_pointer_grab *grab, uint32_t time)
2938 struct rotate_grab *rotate =
2939 container_of(grab, struct rotate_grab, base.grab);
2940 struct weston_pointer *pointer = grab->pointer;
2941 struct shell_surface *shsurf = rotate->base.shsurf;
2942 struct weston_surface *surface;
2943 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2948 surface = shsurf->surface;
2950 cx = 0.5f * surface->geometry.width;
2951 cy = 0.5f * surface->geometry.height;
2953 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2954 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2955 r = sqrtf(dx * dx + dy * dy);
2957 wl_list_remove(&shsurf->rotation.transform.link);
2958 weston_surface_geometry_dirty(shsurf->surface);
2961 struct weston_matrix *matrix =
2962 &shsurf->rotation.transform.matrix;
2964 weston_matrix_init(&rotate->rotation);
2965 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
2967 weston_matrix_init(matrix);
2968 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2969 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2970 weston_matrix_multiply(matrix, &rotate->rotation);
2971 weston_matrix_translate(matrix, cx, cy, 0.0f);
2974 &shsurf->surface->geometry.transformation_list,
2975 &shsurf->rotation.transform.link);
2977 wl_list_init(&shsurf->rotation.transform.link);
2978 weston_matrix_init(&shsurf->rotation.rotation);
2979 weston_matrix_init(&rotate->rotation);
2982 /* We need to adjust the position of the surface
2983 * in case it was resized in a rotated state before */
2984 cposx = surface->geometry.x + cx;
2985 cposy = surface->geometry.y + cy;
2986 dposx = rotate->center.x - cposx;
2987 dposy = rotate->center.y - cposy;
2988 if (dposx != 0.0f || dposy != 0.0f) {
2989 weston_surface_set_position(surface,
2990 surface->geometry.x + dposx,
2991 surface->geometry.y + dposy);
2994 /* Repaint implies weston_surface_update_transform(), which
2995 * lazily applies the damage due to rotation update.
2997 weston_compositor_schedule_repaint(shsurf->surface->compositor);
3001 rotate_grab_button(struct weston_pointer_grab *grab,
3002 uint32_t time, uint32_t button, uint32_t state_w)
3004 struct rotate_grab *rotate =
3005 container_of(grab, struct rotate_grab, base.grab);
3006 struct weston_pointer *pointer = grab->pointer;
3007 struct shell_surface *shsurf = rotate->base.shsurf;
3008 enum wl_pointer_button_state state = state_w;
3010 if (pointer->button_count == 0 &&
3011 state == WL_POINTER_BUTTON_STATE_RELEASED) {
3013 weston_matrix_multiply(&shsurf->rotation.rotation,
3015 shell_grab_end(&rotate->base);
3020 static const struct weston_pointer_grab_interface rotate_grab_interface = {
3027 surface_rotate(struct shell_surface *surface, struct weston_seat *seat)
3029 struct rotate_grab *rotate;
3033 rotate = malloc(sizeof *rotate);
3037 weston_surface_to_global_float(surface->surface,
3038 surface->surface->geometry.width * 0.5f,
3039 surface->surface->geometry.height * 0.5f,
3040 &rotate->center.x, &rotate->center.y);
3042 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
3043 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
3044 r = sqrtf(dx * dx + dy * dy);
3046 struct weston_matrix inverse;
3048 weston_matrix_init(&inverse);
3049 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
3050 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
3052 weston_matrix_init(&rotate->rotation);
3053 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
3055 weston_matrix_init(&surface->rotation.rotation);
3056 weston_matrix_init(&rotate->rotation);
3059 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
3060 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
3064 rotate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3067 struct weston_surface *focus =
3068 (struct weston_surface *) seat->pointer->focus;
3069 struct weston_surface *base_surface;
3070 struct shell_surface *surface;
3072 base_surface = weston_surface_get_main_surface(focus);
3073 if (base_surface == NULL)
3076 surface = get_shell_surface(base_surface);
3077 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
3078 surface->type == SHELL_SURFACE_MAXIMIZED)
3081 surface_rotate(surface, seat);
3085 lower_fullscreen_layer(struct desktop_shell *shell)
3087 struct workspace *ws;
3088 struct weston_surface *surface, *prev;
3090 ws = get_current_workspace(shell);
3091 wl_list_for_each_reverse_safe(surface, prev,
3092 &shell->fullscreen_layer.surface_list,
3094 weston_surface_restack(surface, &ws->layer.surface_list);
3098 activate(struct desktop_shell *shell, struct weston_surface *es,
3099 struct weston_seat *seat)
3101 struct weston_surface *main_surface;
3102 struct focus_state *state;
3103 struct workspace *ws;
3105 main_surface = weston_surface_get_main_surface(es);
3107 weston_surface_activate(es, seat);
3109 state = ensure_focus_state(shell, seat);
3113 state->keyboard_focus = es;
3114 wl_list_remove(&state->surface_destroy_listener.link);
3115 wl_signal_add(&es->destroy_signal, &state->surface_destroy_listener);
3117 switch (get_shell_surface_type(main_surface)) {
3118 case SHELL_SURFACE_FULLSCREEN:
3119 /* should on top of panels */
3120 shell_stack_fullscreen(get_shell_surface(main_surface));
3121 shell_configure_fullscreen(get_shell_surface(main_surface));
3124 restore_all_output_modes(shell->compositor);
3125 ws = get_current_workspace(shell);
3126 weston_surface_restack(main_surface, &ws->layer.surface_list);
3131 /* no-op func for checking black surface */
3133 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3138 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
3140 if (es->configure == black_surface_configure) {
3142 *fs_surface = (struct weston_surface *)es->configure_private;
3149 activate_binding(struct weston_seat *seat,
3150 struct desktop_shell *shell,
3151 struct weston_surface *focus)
3153 struct weston_surface *main_surface;
3158 if (is_black_surface(focus, &main_surface))
3159 focus = main_surface;
3161 main_surface = weston_surface_get_main_surface(focus);
3162 if (get_shell_surface_type(main_surface) == SHELL_SURFACE_NONE)
3165 activate(shell, focus, seat);
3169 click_to_activate_binding(struct weston_seat *seat, uint32_t time, uint32_t button,
3172 if (seat->pointer->grab != &seat->pointer->default_grab)
3175 activate_binding(seat, data,
3176 (struct weston_surface *) seat->pointer->focus);
3180 touch_to_activate_binding(struct weston_seat *seat, uint32_t time, void *data)
3182 if (seat->touch->grab != &seat->touch->default_grab)
3185 activate_binding(seat, data,
3186 (struct weston_surface *) seat->touch->focus);
3190 lock(struct desktop_shell *shell)
3192 struct workspace *ws = get_current_workspace(shell);
3194 if (shell->locked) {
3195 weston_compositor_sleep(shell->compositor);
3199 shell->locked = true;
3201 /* Hide all surfaces by removing the fullscreen, panel and
3202 * toplevel layers. This way nothing else can show or receive
3203 * input events while we are locked. */
3205 wl_list_remove(&shell->panel_layer.link);
3206 wl_list_remove(&shell->fullscreen_layer.link);
3207 if (shell->showing_input_panels)
3208 wl_list_remove(&shell->input_panel_layer.link);
3209 wl_list_remove(&ws->layer.link);
3210 wl_list_insert(&shell->compositor->cursor_layer.link,
3211 &shell->lock_layer.link);
3213 launch_screensaver(shell);
3215 /* TODO: disable bindings that should not work while locked. */
3217 /* All this must be undone in resume_desktop(). */
3221 unlock(struct desktop_shell *shell)
3223 if (!shell->locked || shell->lock_surface) {
3224 shell_fade(shell, FADE_IN);
3228 /* If desktop-shell client has gone away, unlock immediately. */
3229 if (!shell->child.desktop_shell) {
3230 resume_desktop(shell);
3234 if (shell->prepare_event_sent)
3237 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
3238 shell->prepare_event_sent = true;
3242 shell_fade_done(struct weston_surface_animation *animation, void *data)
3244 struct desktop_shell *shell = data;
3246 shell->fade.animation = NULL;
3248 switch (shell->fade.type) {
3250 weston_surface_destroy(shell->fade.surface);
3251 shell->fade.surface = NULL;
3259 static struct weston_surface *
3260 shell_fade_create_surface(struct desktop_shell *shell)
3262 struct weston_compositor *compositor = shell->compositor;
3263 struct weston_surface *surface;
3265 surface = weston_surface_create(compositor);
3269 weston_surface_configure(surface, 0, 0, 8192, 8192);
3270 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
3271 wl_list_insert(&compositor->fade_layer.surface_list,
3272 &surface->layer_link);
3273 pixman_region32_init(&surface->input);
3279 shell_fade(struct desktop_shell *shell, enum fade_type type)
3291 weston_log("shell: invalid fade type\n");
3295 shell->fade.type = type;
3297 if (shell->fade.surface == NULL) {
3298 shell->fade.surface = shell_fade_create_surface(shell);
3299 if (!shell->fade.surface)
3302 shell->fade.surface->alpha = 1.0 - tint;
3303 weston_surface_update_transform(shell->fade.surface);
3306 if (shell->fade.animation)
3307 weston_fade_update(shell->fade.animation, tint);
3309 shell->fade.animation =
3310 weston_fade_run(shell->fade.surface,
3311 1.0 - tint, tint, 300.0,
3312 shell_fade_done, shell);
3316 do_shell_fade_startup(void *data)
3318 struct desktop_shell *shell = data;
3320 shell_fade(shell, FADE_IN);
3324 shell_fade_startup(struct desktop_shell *shell)
3326 struct wl_event_loop *loop;
3328 if (!shell->fade.startup_timer)
3331 wl_event_source_remove(shell->fade.startup_timer);
3332 shell->fade.startup_timer = NULL;
3334 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3335 wl_event_loop_add_idle(loop, do_shell_fade_startup, shell);
3339 fade_startup_timeout(void *data)
3341 struct desktop_shell *shell = data;
3343 shell_fade_startup(shell);
3348 shell_fade_init(struct desktop_shell *shell)
3350 /* Make compositor output all black, and wait for the desktop-shell
3351 * client to signal it is ready, then fade in. The timer triggers a
3352 * fade-in, in case the desktop-shell client takes too long.
3355 struct wl_event_loop *loop;
3357 if (shell->fade.surface != NULL) {
3358 weston_log("%s: warning: fade surface already exists\n",
3363 shell->fade.surface = shell_fade_create_surface(shell);
3364 if (!shell->fade.surface)
3367 weston_surface_update_transform(shell->fade.surface);
3368 weston_surface_damage(shell->fade.surface);
3370 loop = wl_display_get_event_loop(shell->compositor->wl_display);
3371 shell->fade.startup_timer =
3372 wl_event_loop_add_timer(loop, fade_startup_timeout, shell);
3373 wl_event_source_timer_update(shell->fade.startup_timer, 15000);
3377 idle_handler(struct wl_listener *listener, void *data)
3379 struct desktop_shell *shell =
3380 container_of(listener, struct desktop_shell, idle_listener);
3382 shell_fade(shell, FADE_OUT);
3383 /* lock() is called from shell_fade_done() */
3387 wake_handler(struct wl_listener *listener, void *data)
3389 struct desktop_shell *shell =
3390 container_of(listener, struct desktop_shell, wake_listener);
3396 show_input_panels(struct wl_listener *listener, void *data)
3398 struct desktop_shell *shell =
3399 container_of(listener, struct desktop_shell,
3400 show_input_panel_listener);
3401 struct input_panel_surface *surface, *next;
3402 struct weston_surface *ws;
3404 shell->text_input.surface = (struct weston_surface*)data;
3406 if (shell->showing_input_panels)
3409 shell->showing_input_panels = true;
3412 wl_list_insert(&shell->panel_layer.link,
3413 &shell->input_panel_layer.link);
3415 wl_list_for_each_safe(surface, next,
3416 &shell->input_panel.surfaces, link) {
3417 ws = surface->surface;
3418 if (!ws->buffer_ref.buffer)
3420 wl_list_insert(&shell->input_panel_layer.surface_list,
3422 weston_surface_geometry_dirty(ws);
3423 weston_surface_update_transform(ws);
3424 weston_surface_damage(ws);
3425 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
3430 hide_input_panels(struct wl_listener *listener, void *data)
3432 struct desktop_shell *shell =
3433 container_of(listener, struct desktop_shell,
3434 hide_input_panel_listener);
3435 struct weston_surface *surface, *next;
3437 if (!shell->showing_input_panels)
3440 shell->showing_input_panels = false;
3443 wl_list_remove(&shell->input_panel_layer.link);
3445 wl_list_for_each_safe(surface, next,
3446 &shell->input_panel_layer.surface_list, layer_link)
3447 weston_surface_unmap(surface);
3451 update_input_panels(struct wl_listener *listener, void *data)
3453 struct desktop_shell *shell =
3454 container_of(listener, struct desktop_shell,
3455 update_input_panel_listener);
3457 memcpy(&shell->text_input.cursor_rectangle, data, sizeof(pixman_box32_t));
3461 center_on_output(struct weston_surface *surface, struct weston_output *output)
3463 int32_t surf_x, surf_y, width, height;
3466 surface_subsurfaces_boundingbox(surface, &surf_x, &surf_y, &width, &height);
3468 x = output->x + (output->width - width) / 2 - surf_x / 2;
3469 y = output->y + (output->height - height) / 2 - surf_y / 2;
3471 weston_surface_configure(surface, x, y, width, height);
3475 weston_surface_set_initial_position (struct weston_surface *surface,
3476 struct desktop_shell *shell)
3478 struct weston_compositor *compositor = shell->compositor;
3480 int range_x, range_y;
3481 int dx, dy, x, y, panel_height;
3482 struct weston_output *output, *target_output = NULL;
3483 struct weston_seat *seat;
3485 /* As a heuristic place the new window on the same output as the
3486 * pointer. Falling back to the output containing 0, 0.
3488 * TODO: Do something clever for touch too?
3490 wl_list_for_each(seat, &compositor->seat_list, link) {
3491 if (seat->pointer) {
3492 ix = wl_fixed_to_int(seat->pointer->x);
3493 iy = wl_fixed_to_int(seat->pointer->y);
3498 wl_list_for_each(output, &compositor->output_list, link) {
3499 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3500 target_output = output;
3505 if (!target_output) {
3506 weston_surface_set_position(surface, 10 + random() % 400,
3507 10 + random() % 400);
3511 /* Valid range within output where the surface will still be onscreen.
3512 * If this is negative it means that the surface is bigger than
3515 panel_height = get_output_panel_height(shell, target_output);
3516 range_x = target_output->width - surface->geometry.width;
3517 range_y = (target_output->height - panel_height) -
3518 surface->geometry.height;
3521 dx = random() % range_x;
3526 dy = panel_height + random() % range_y;
3530 x = target_output->x + dx;
3531 y = target_output->y + dy;
3533 weston_surface_set_position (surface, x, y);
3537 map(struct desktop_shell *shell, struct weston_surface *surface,
3538 int32_t width, int32_t height, int32_t sx, int32_t sy)
3540 struct weston_compositor *compositor = shell->compositor;
3541 struct shell_surface *shsurf = get_shell_surface(surface);
3542 enum shell_surface_type surface_type = shsurf->type;
3543 struct weston_surface *parent;
3544 struct weston_seat *seat;
3545 struct workspace *ws;
3546 int panel_height = 0;
3547 int32_t surf_x, surf_y;
3549 surface->geometry.width = width;
3550 surface->geometry.height = height;
3551 weston_surface_geometry_dirty(surface);
3553 /* initial positioning, see also configure() */
3554 switch (surface_type) {
3555 case SHELL_SURFACE_TOPLEVEL:
3556 weston_surface_set_initial_position(surface, shell);
3558 case SHELL_SURFACE_FULLSCREEN:
3559 center_on_output(surface, shsurf->fullscreen_output);
3560 shell_map_fullscreen(shsurf);
3562 case SHELL_SURFACE_MAXIMIZED:
3563 /* use surface configure to set the geometry */
3564 panel_height = get_output_panel_height(shell,surface->output);
3565 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3567 weston_surface_set_position(surface, shsurf->output->x - surf_x,
3568 shsurf->output->y + panel_height - surf_y);
3570 case SHELL_SURFACE_POPUP:
3571 shell_map_popup(shsurf);
3573 case SHELL_SURFACE_NONE:
3574 weston_surface_set_position(surface,
3575 surface->geometry.x + sx,
3576 surface->geometry.y + sy);
3582 /* surface stacking order, see also activate() */
3583 switch (surface_type) {
3584 case SHELL_SURFACE_POPUP:
3585 case SHELL_SURFACE_TRANSIENT:
3586 parent = shsurf->parent;
3587 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3589 case SHELL_SURFACE_FULLSCREEN:
3590 case SHELL_SURFACE_NONE:
3592 case SHELL_SURFACE_XWAYLAND:
3594 ws = get_current_workspace(shell);
3595 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
3599 if (surface_type != SHELL_SURFACE_NONE) {
3600 weston_surface_update_transform(surface);
3601 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3602 surface->output = shsurf->output;
3605 switch (surface_type) {
3606 /* XXX: xwayland's using the same fields for transient type */
3607 case SHELL_SURFACE_XWAYLAND:
3608 case SHELL_SURFACE_TRANSIENT:
3609 if (shsurf->transient.flags ==
3610 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3612 case SHELL_SURFACE_TOPLEVEL:
3613 case SHELL_SURFACE_FULLSCREEN:
3614 case SHELL_SURFACE_MAXIMIZED:
3615 if (!shell->locked) {
3616 wl_list_for_each(seat, &compositor->seat_list, link)
3617 activate(shell, surface, seat);
3624 if (surface_type == SHELL_SURFACE_TOPLEVEL)
3626 switch (shell->win_animation_type) {
3627 case ANIMATION_FADE:
3628 weston_fade_run(surface, 0.0, 1.0, 300.0, NULL, NULL);
3630 case ANIMATION_ZOOM:
3631 weston_zoom_run(surface, 0.5, 1.0, NULL, NULL);
3640 configure(struct desktop_shell *shell, struct weston_surface *surface,
3641 float x, float y, int32_t width, int32_t height)
3643 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3644 struct shell_surface *shsurf;
3645 int32_t surf_x, surf_y;
3647 shsurf = get_shell_surface(surface);
3649 surface_type = shsurf->type;
3651 weston_surface_configure(surface, x, y, width, height);
3653 switch (surface_type) {
3654 case SHELL_SURFACE_FULLSCREEN:
3655 shell_stack_fullscreen(shsurf);
3656 shell_configure_fullscreen(shsurf);
3658 case SHELL_SURFACE_MAXIMIZED:
3659 /* setting x, y and using configure to change that geometry */
3660 surface_subsurfaces_boundingbox(shsurf->surface, &surf_x, &surf_y,
3662 surface->geometry.x = surface->output->x - surf_x;
3663 surface->geometry.y = surface->output->y +
3664 get_output_panel_height(shell,surface->output) - surf_y;
3666 case SHELL_SURFACE_TOPLEVEL:
3672 /* XXX: would a fullscreen surface need the same handling? */
3673 if (surface->output) {
3674 weston_surface_update_transform(surface);
3676 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3677 surface->output = shsurf->output;
3682 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3684 struct shell_surface *shsurf = get_shell_surface(es);
3685 struct desktop_shell *shell = shsurf->shell;
3687 int type_changed = 0;
3689 if (!weston_surface_is_mapped(es) &&
3690 !wl_list_empty(&shsurf->popup.grab_link)) {
3691 remove_popup_grab(shsurf);
3697 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3698 shsurf->type != shsurf->next_type) {
3699 set_surface_type(shsurf);
3703 if (!weston_surface_is_mapped(es)) {
3704 map(shell, es, width, height, sx, sy);
3705 } else if (type_changed || sx != 0 || sy != 0 ||
3706 es->geometry.width != width ||
3707 es->geometry.height != height) {
3708 float from_x, from_y;
3711 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3712 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3713 configure(shell, es,
3714 es->geometry.x + to_x - from_x,
3715 es->geometry.y + to_y - from_y,
3720 static void launch_desktop_shell_process(void *data);
3723 desktop_shell_sigchld(struct weston_process *process, int status)
3726 struct desktop_shell *shell =
3727 container_of(process, struct desktop_shell, child.process);
3729 shell->child.process.pid = 0;
3730 shell->child.client = NULL; /* already destroyed by wayland */
3732 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3733 time = weston_compositor_get_time();
3734 if (time - shell->child.deathstamp > 30000) {
3735 shell->child.deathstamp = time;
3736 shell->child.deathcount = 0;
3739 shell->child.deathcount++;
3740 if (shell->child.deathcount > 5) {
3741 weston_log("weston-desktop-shell died, giving up.\n");
3745 weston_log("weston-desktop-shell died, respawning...\n");
3746 launch_desktop_shell_process(shell);
3747 shell_fade_startup(shell);
3751 launch_desktop_shell_process(void *data)
3753 struct desktop_shell *shell = data;
3754 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3756 shell->child.client = weston_client_launch(shell->compositor,
3757 &shell->child.process,
3759 desktop_shell_sigchld);
3761 if (!shell->child.client)
3762 weston_log("not able to start %s\n", shell_exe);
3766 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3768 struct desktop_shell *shell = data;
3769 struct wl_resource *resource;
3771 resource = wl_resource_create(client, &wl_shell_interface, 1, id);
3773 wl_resource_set_implementation(resource, &shell_implementation,
3778 unbind_desktop_shell(struct wl_resource *resource)
3780 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3783 resume_desktop(shell);
3785 shell->child.desktop_shell = NULL;
3786 shell->prepare_event_sent = false;
3790 bind_desktop_shell(struct wl_client *client,
3791 void *data, uint32_t version, uint32_t id)
3793 struct desktop_shell *shell = data;
3794 struct wl_resource *resource;
3796 resource = wl_resource_create(client, &desktop_shell_interface,
3797 MIN(version, 2), id);
3799 if (client == shell->child.client) {
3800 wl_resource_set_implementation(resource,
3801 &desktop_shell_implementation,
3802 shell, unbind_desktop_shell);
3803 shell->child.desktop_shell = resource;
3806 shell_fade_startup(shell);
3811 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3812 "permission to bind desktop_shell denied");
3813 wl_resource_destroy(resource);
3817 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3819 struct desktop_shell *shell = surface->configure_private;
3824 /* XXX: starting weston-screensaver beforehand does not work */
3828 center_on_output(surface, surface->output);
3830 if (wl_list_empty(&surface->layer_link)) {
3831 wl_list_insert(shell->lock_layer.surface_list.prev,
3832 &surface->layer_link);
3833 weston_surface_update_transform(surface);
3834 wl_event_source_timer_update(shell->screensaver.timer,
3835 shell->screensaver.duration);
3836 shell_fade(shell, FADE_IN);
3841 screensaver_set_surface(struct wl_client *client,
3842 struct wl_resource *resource,
3843 struct wl_resource *surface_resource,
3844 struct wl_resource *output_resource)
3846 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3847 struct weston_surface *surface =
3848 wl_resource_get_user_data(surface_resource);
3849 struct weston_output *output = wl_resource_get_user_data(output_resource);
3851 surface->configure = screensaver_configure;
3852 surface->configure_private = shell;
3853 surface->output = output;
3856 static const struct screensaver_interface screensaver_implementation = {
3857 screensaver_set_surface
3861 unbind_screensaver(struct wl_resource *resource)
3863 struct desktop_shell *shell = wl_resource_get_user_data(resource);
3865 shell->screensaver.binding = NULL;
3869 bind_screensaver(struct wl_client *client,
3870 void *data, uint32_t version, uint32_t id)
3872 struct desktop_shell *shell = data;
3873 struct wl_resource *resource;
3875 resource = wl_resource_create(client, &screensaver_interface, 1, id);
3877 if (shell->screensaver.binding == NULL) {
3878 wl_resource_set_implementation(resource,
3879 &screensaver_implementation,
3880 shell, unbind_screensaver);
3881 shell->screensaver.binding = resource;
3885 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3886 "interface object already bound");
3887 wl_resource_destroy(resource);
3891 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3893 struct input_panel_surface *ip_surface = surface->configure_private;
3894 struct desktop_shell *shell = ip_surface->shell;
3896 uint32_t show_surface = 0;
3901 if (!weston_surface_is_mapped(surface)) {
3902 if (!shell->showing_input_panels)
3908 fprintf(stderr, "%s panel: %d, output: %p\n", __FUNCTION__, ip_surface->panel, ip_surface->output);
3910 if (ip_surface->panel) {
3911 x = shell->text_input.surface->geometry.x + shell->text_input.cursor_rectangle.x2;
3912 y = shell->text_input.surface->geometry.y + shell->text_input.cursor_rectangle.y2;
3914 x = ip_surface->output->x + (ip_surface->output->width - width) / 2;
3915 y = ip_surface->output->y + ip_surface->output->height - height;
3918 weston_surface_configure(surface,
3923 wl_list_insert(&shell->input_panel_layer.surface_list,
3924 &surface->layer_link);
3925 weston_surface_update_transform(surface);
3926 weston_surface_damage(surface);
3927 weston_slide_run(surface, surface->geometry.height, 0, NULL, NULL);
3932 destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
3934 wl_signal_emit(&input_panel_surface->destroy_signal, input_panel_surface);
3936 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
3937 wl_list_remove(&input_panel_surface->link);
3939 input_panel_surface->surface->configure = NULL;
3941 free(input_panel_surface);
3944 static struct input_panel_surface *
3945 get_input_panel_surface(struct weston_surface *surface)
3947 if (surface->configure == input_panel_configure) {
3948 return surface->configure_private;
3955 input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3957 struct input_panel_surface *ipsurface = container_of(listener,
3958 struct input_panel_surface,
3959 surface_destroy_listener);
3961 if (ipsurface->resource) {
3962 wl_resource_destroy(ipsurface->resource);
3964 destroy_input_panel_surface(ipsurface);
3968 static struct input_panel_surface *
3969 create_input_panel_surface(struct desktop_shell *shell,
3970 struct weston_surface *surface)
3972 struct input_panel_surface *input_panel_surface;
3974 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3975 if (!input_panel_surface)
3978 surface->configure = input_panel_configure;
3979 surface->configure_private = input_panel_surface;
3981 input_panel_surface->shell = shell;
3983 input_panel_surface->surface = surface;
3985 wl_signal_init(&input_panel_surface->destroy_signal);
3986 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3987 wl_signal_add(&surface->destroy_signal,
3988 &input_panel_surface->surface_destroy_listener);
3990 wl_list_init(&input_panel_surface->link);
3992 return input_panel_surface;
3996 input_panel_surface_set_toplevel(struct wl_client *client,
3997 struct wl_resource *resource,
3998 struct wl_resource *output_resource,
4001 struct input_panel_surface *input_panel_surface =
4002 wl_resource_get_user_data(resource);
4003 struct desktop_shell *shell = input_panel_surface->shell;
4005 wl_list_insert(&shell->input_panel.surfaces,
4006 &input_panel_surface->link);
4008 input_panel_surface->output = wl_resource_get_user_data(output_resource);
4009 input_panel_surface->panel = 0;
4013 input_panel_surface_set_overlay_panel(struct wl_client *client,
4014 struct wl_resource *resource)
4016 struct input_panel_surface *input_panel_surface =
4017 wl_resource_get_user_data(resource);
4018 struct desktop_shell *shell = input_panel_surface->shell;
4020 wl_list_insert(&shell->input_panel.surfaces,
4021 &input_panel_surface->link);
4023 input_panel_surface->panel = 1;
4026 static const struct wl_input_panel_surface_interface input_panel_surface_implementation = {
4027 input_panel_surface_set_toplevel,
4028 input_panel_surface_set_overlay_panel
4032 destroy_input_panel_surface_resource(struct wl_resource *resource)
4034 struct input_panel_surface *ipsurf =
4035 wl_resource_get_user_data(resource);
4037 destroy_input_panel_surface(ipsurf);
4041 input_panel_get_input_panel_surface(struct wl_client *client,
4042 struct wl_resource *resource,
4044 struct wl_resource *surface_resource)
4046 struct weston_surface *surface =
4047 wl_resource_get_user_data(surface_resource);
4048 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4049 struct input_panel_surface *ipsurf;
4051 if (get_input_panel_surface(surface)) {
4052 wl_resource_post_error(surface_resource,
4053 WL_DISPLAY_ERROR_INVALID_OBJECT,
4054 "wl_input_panel::get_input_panel_surface already requested");
4058 ipsurf = create_input_panel_surface(shell, surface);
4060 wl_resource_post_error(surface_resource,
4061 WL_DISPLAY_ERROR_INVALID_OBJECT,
4062 "surface->configure already set");
4067 wl_resource_create(client,
4068 &wl_input_panel_surface_interface, 1, id);
4069 wl_resource_set_implementation(ipsurf->resource,
4070 &input_panel_surface_implementation,
4072 destroy_input_panel_surface_resource);
4075 static const struct wl_input_panel_interface input_panel_implementation = {
4076 input_panel_get_input_panel_surface
4080 unbind_input_panel(struct wl_resource *resource)
4082 struct desktop_shell *shell = wl_resource_get_user_data(resource);
4084 shell->input_panel.binding = NULL;
4088 bind_input_panel(struct wl_client *client,
4089 void *data, uint32_t version, uint32_t id)
4091 struct desktop_shell *shell = data;
4092 struct wl_resource *resource;
4094 resource = wl_resource_create(client,
4095 &wl_input_panel_interface, 1, id);
4097 if (shell->input_panel.binding == NULL) {
4098 wl_resource_set_implementation(resource,
4099 &input_panel_implementation,
4100 shell, unbind_input_panel);
4101 shell->input_panel.binding = resource;
4105 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
4106 "interface object already bound");
4107 wl_resource_destroy(resource);
4111 struct desktop_shell *shell;
4112 struct weston_surface *current;
4113 struct wl_listener listener;
4114 struct weston_keyboard_grab grab;
4118 switcher_next(struct switcher *switcher)
4120 struct weston_surface *surface;
4121 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
4122 struct shell_surface *shsurf;
4123 struct workspace *ws = get_current_workspace(switcher->shell);
4125 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
4126 switch (get_shell_surface_type(surface)) {
4127 case SHELL_SURFACE_TOPLEVEL:
4128 case SHELL_SURFACE_FULLSCREEN:
4129 case SHELL_SURFACE_MAXIMIZED:
4132 if (prev == switcher->current)
4135 surface->alpha = 0.25;
4136 weston_surface_geometry_dirty(surface);
4137 weston_surface_damage(surface);
4143 if (is_black_surface(surface, NULL)) {
4144 surface->alpha = 0.25;
4145 weston_surface_geometry_dirty(surface);
4146 weston_surface_damage(surface);
4156 wl_list_remove(&switcher->listener.link);
4157 wl_signal_add(&next->destroy_signal, &switcher->listener);
4159 switcher->current = next;
4162 shsurf = get_shell_surface(switcher->current);
4163 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
4164 shsurf->fullscreen.black_surface->alpha = 1.0;
4168 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
4170 struct switcher *switcher =
4171 container_of(listener, struct switcher, listener);
4173 switcher_next(switcher);
4177 switcher_destroy(struct switcher *switcher)
4179 struct weston_surface *surface;
4180 struct weston_keyboard *keyboard = switcher->grab.keyboard;
4181 struct workspace *ws = get_current_workspace(switcher->shell);
4183 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
4184 surface->alpha = 1.0;
4185 weston_surface_damage(surface);
4188 if (switcher->current)
4189 activate(switcher->shell, switcher->current,
4190 (struct weston_seat *) keyboard->seat);
4191 wl_list_remove(&switcher->listener.link);
4192 weston_keyboard_end_grab(keyboard);
4193 if (keyboard->input_method_resource)
4194 keyboard->grab = &keyboard->input_method_grab;
4199 switcher_key(struct weston_keyboard_grab *grab,
4200 uint32_t time, uint32_t key, uint32_t state_w)
4202 struct switcher *switcher = container_of(grab, struct switcher, grab);
4203 enum wl_keyboard_key_state state = state_w;
4205 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
4206 switcher_next(switcher);
4210 switcher_modifier(struct weston_keyboard_grab *grab, uint32_t serial,
4211 uint32_t mods_depressed, uint32_t mods_latched,
4212 uint32_t mods_locked, uint32_t group)
4214 struct switcher *switcher = container_of(grab, struct switcher, grab);
4215 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
4217 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
4218 switcher_destroy(switcher);
4221 static const struct weston_keyboard_grab_interface switcher_grab = {
4227 switcher_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4230 struct desktop_shell *shell = data;
4231 struct switcher *switcher;
4233 switcher = malloc(sizeof *switcher);
4234 switcher->shell = shell;
4235 switcher->current = NULL;
4236 switcher->listener.notify = switcher_handle_surface_destroy;
4237 wl_list_init(&switcher->listener.link);
4239 restore_all_output_modes(shell->compositor);
4240 lower_fullscreen_layer(switcher->shell);
4241 switcher->grab.interface = &switcher_grab;
4242 weston_keyboard_start_grab(seat->keyboard, &switcher->grab);
4243 weston_keyboard_set_focus(seat->keyboard, NULL);
4244 switcher_next(switcher);
4248 backlight_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4251 struct weston_compositor *compositor = data;
4252 struct weston_output *output;
4253 long backlight_new = 0;
4255 /* TODO: we're limiting to simple use cases, where we assume just
4256 * control on the primary display. We'd have to extend later if we
4257 * ever get support for setting backlights on random desktop LCD
4259 output = get_default_output(compositor);
4263 if (!output->set_backlight)
4266 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
4267 backlight_new = output->backlight_current - 25;
4268 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
4269 backlight_new = output->backlight_current + 25;
4271 if (backlight_new < 5)
4273 if (backlight_new > 255)
4274 backlight_new = 255;
4276 output->backlight_current = backlight_new;
4277 output->set_backlight(output, output->backlight_current);
4280 struct debug_binding_grab {
4281 struct weston_keyboard_grab grab;
4282 struct weston_seat *seat;
4284 int key_released[2];
4288 debug_binding_key(struct weston_keyboard_grab *grab, uint32_t time,
4289 uint32_t key, uint32_t state)
4291 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
4292 struct weston_compositor *ec = db->seat->compositor;
4293 struct wl_display *display = ec->wl_display;
4294 struct wl_resource *resource;
4296 int send = 0, terminate = 0;
4297 int check_binding = 1;
4299 struct wl_list *resource_list;
4301 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
4302 /* Do not run bindings on key releases */
4305 for (i = 0; i < 2; i++)
4306 if (key == db->key[i])
4307 db->key_released[i] = 1;
4309 if (db->key_released[0] && db->key_released[1]) {
4310 /* All key releases been swalled so end the grab */
4312 } else if (key != db->key[0] && key != db->key[1]) {
4313 /* Should not swallow release of other keys */
4316 } else if (key == db->key[0] && !db->key_released[0]) {
4317 /* Do not check bindings for the first press of the binding
4318 * key. This allows it to be used as a debug shortcut.
4319 * We still need to swallow this event. */
4321 } else if (db->key[1]) {
4322 /* If we already ran a binding don't process another one since
4323 * we can't keep track of all the binding keys that were
4324 * pressed in order to swallow the release events. */
4329 if (check_binding) {
4330 if (weston_compositor_run_debug_binding(ec, db->seat, time,
4332 /* We ran a binding so swallow the press and keep the
4333 * grab to swallow the released too. */
4338 /* Terminate the grab since the key pressed is not a
4339 * debug binding key. */
4346 serial = wl_display_next_serial(display);
4347 resource_list = &grab->keyboard->focus_resource_list;
4348 wl_resource_for_each(resource, resource_list) {
4349 wl_keyboard_send_key(resource, serial, time, key, state);
4354 weston_keyboard_end_grab(grab->keyboard);
4355 if (grab->keyboard->input_method_resource)
4356 grab->keyboard->grab = &grab->keyboard->input_method_grab;
4362 debug_binding_modifiers(struct weston_keyboard_grab *grab, uint32_t serial,
4363 uint32_t mods_depressed, uint32_t mods_latched,
4364 uint32_t mods_locked, uint32_t group)
4366 struct wl_resource *resource;
4367 struct wl_list *resource_list;
4369 resource_list = &grab->keyboard->focus_resource_list;
4371 wl_resource_for_each(resource, resource_list) {
4372 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
4373 mods_latched, mods_locked, group);
4377 struct weston_keyboard_grab_interface debug_binding_keyboard_grab = {
4379 debug_binding_modifiers
4383 debug_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
4385 struct debug_binding_grab *grab;
4387 grab = calloc(1, sizeof *grab);
4391 grab->seat = (struct weston_seat *) seat;
4393 grab->grab.interface = &debug_binding_keyboard_grab;
4394 weston_keyboard_start_grab(seat->keyboard, &grab->grab);
4398 force_kill_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
4401 struct weston_surface *focus_surface;
4402 struct wl_client *client;
4403 struct desktop_shell *shell = data;
4404 struct weston_compositor *compositor = shell->compositor;
4407 focus_surface = seat->keyboard->focus;
4411 wl_signal_emit(&compositor->kill_signal, focus_surface);
4413 client = wl_resource_get_client(focus_surface->resource);
4414 wl_client_get_credentials(client, &pid, NULL, NULL);
4416 /* Skip clients that we launched ourselves (the credentials of
4417 * the socketpair is ours) */
4418 if (pid == getpid())
4425 workspace_up_binding(struct weston_seat *seat, uint32_t time,
4426 uint32_t key, void *data)
4428 struct desktop_shell *shell = data;
4429 unsigned int new_index = shell->workspaces.current;
4436 change_workspace(shell, new_index);
4440 workspace_down_binding(struct weston_seat *seat, uint32_t time,
4441 uint32_t key, void *data)
4443 struct desktop_shell *shell = data;
4444 unsigned int new_index = shell->workspaces.current;
4448 if (new_index < shell->workspaces.num - 1)
4451 change_workspace(shell, new_index);
4455 workspace_f_binding(struct weston_seat *seat, uint32_t time,
4456 uint32_t key, void *data)
4458 struct desktop_shell *shell = data;
4459 unsigned int new_index;
4463 new_index = key - KEY_F1;
4464 if (new_index >= shell->workspaces.num)
4465 new_index = shell->workspaces.num - 1;
4467 change_workspace(shell, new_index);
4471 workspace_move_surface_up_binding(struct weston_seat *seat, uint32_t time,
4472 uint32_t key, void *data)
4474 struct desktop_shell *shell = data;
4475 unsigned int new_index = shell->workspaces.current;
4483 take_surface_to_workspace_by_seat(shell, seat, new_index);
4487 workspace_move_surface_down_binding(struct weston_seat *seat, uint32_t time,
4488 uint32_t key, void *data)
4490 struct desktop_shell *shell = data;
4491 unsigned int new_index = shell->workspaces.current;
4496 if (new_index < shell->workspaces.num - 1)
4499 take_surface_to_workspace_by_seat(shell, seat, new_index);
4503 shell_destroy(struct wl_listener *listener, void *data)
4505 struct desktop_shell *shell =
4506 container_of(listener, struct desktop_shell, destroy_listener);
4507 struct workspace **ws;
4509 if (shell->child.client)
4510 wl_client_destroy(shell->child.client);
4512 wl_list_remove(&shell->idle_listener.link);
4513 wl_list_remove(&shell->wake_listener.link);
4514 wl_list_remove(&shell->show_input_panel_listener.link);
4515 wl_list_remove(&shell->hide_input_panel_listener.link);
4517 wl_array_for_each(ws, &shell->workspaces.array)
4518 workspace_destroy(*ws);
4519 wl_array_release(&shell->workspaces.array);
4521 free(shell->screensaver.path);
4526 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4529 int i, num_workspace_bindings;
4531 /* fixed bindings */
4532 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4533 MODIFIER_CTRL | MODIFIER_ALT,
4534 terminate_binding, ec);
4535 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4536 click_to_activate_binding,
4538 weston_compositor_add_touch_binding(ec, 0,
4539 touch_to_activate_binding,
4541 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4542 MODIFIER_SUPER | MODIFIER_ALT,
4543 surface_opacity_binding, NULL);
4544 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4545 MODIFIER_SUPER, zoom_axis_binding,
4548 /* configurable bindings */
4549 mod = shell->binding_modifier;
4550 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4551 zoom_key_binding, NULL);
4552 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4553 zoom_key_binding, NULL);
4554 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4556 weston_compositor_add_touch_binding(ec, mod, touch_move_binding, shell);
4557 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4558 resize_binding, shell);
4560 if (ec->capabilities & WESTON_CAP_ROTATION_ANY)
4561 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4562 rotate_binding, NULL);
4564 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4566 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4568 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4569 backlight_binding, ec);
4570 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4572 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4573 backlight_binding, ec);
4574 weston_compositor_add_key_binding(ec, KEY_K, mod,
4575 force_kill_binding, shell);
4576 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4577 workspace_up_binding, shell);
4578 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4579 workspace_down_binding, shell);
4580 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4581 workspace_move_surface_up_binding,
4583 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4584 workspace_move_surface_down_binding,
4587 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4588 if (shell->workspaces.num > 1) {
4589 num_workspace_bindings = shell->workspaces.num;
4590 if (num_workspace_bindings > 6)
4591 num_workspace_bindings = 6;
4592 for (i = 0; i < num_workspace_bindings; i++)
4593 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4594 workspace_f_binding,
4598 /* Debug bindings */
4599 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4600 debug_binding, shell);
4604 module_init(struct weston_compositor *ec,
4605 int *argc, char *argv[])
4607 struct weston_seat *seat;
4608 struct desktop_shell *shell;
4609 struct workspace **pws;
4611 struct wl_event_loop *loop;
4613 shell = zalloc(sizeof *shell);
4617 shell->compositor = ec;
4619 shell->destroy_listener.notify = shell_destroy;
4620 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
4621 shell->idle_listener.notify = idle_handler;
4622 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4623 shell->wake_listener.notify = wake_handler;
4624 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
4625 shell->show_input_panel_listener.notify = show_input_panels;
4626 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4627 shell->hide_input_panel_listener.notify = hide_input_panels;
4628 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
4629 shell->update_input_panel_listener.notify = update_input_panels;
4630 wl_signal_add(&ec->update_input_panel_signal, &shell->update_input_panel_listener);
4631 ec->ping_handler = ping_handler;
4632 ec->shell_interface.shell = shell;
4633 ec->shell_interface.create_shell_surface = create_shell_surface;
4634 ec->shell_interface.set_toplevel = set_toplevel;
4635 ec->shell_interface.set_transient = set_transient;
4636 ec->shell_interface.set_fullscreen = set_fullscreen;
4637 ec->shell_interface.set_xwayland = set_xwayland;
4638 ec->shell_interface.move = surface_move;
4639 ec->shell_interface.resize = surface_resize;
4640 ec->shell_interface.set_title = set_title;
4642 wl_list_init(&shell->input_panel.surfaces);
4644 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4645 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
4646 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4647 weston_layer_init(&shell->lock_layer, NULL);
4648 weston_layer_init(&shell->input_panel_layer, NULL);
4650 wl_array_init(&shell->workspaces.array);
4651 wl_list_init(&shell->workspaces.client_list);
4653 shell_configuration(shell);
4655 for (i = 0; i < shell->workspaces.num; i++) {
4656 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4660 *pws = workspace_create();
4664 activate_workspace(shell, 0);
4666 wl_list_init(&shell->workspaces.anim_sticky_list);
4667 wl_list_init(&shell->workspaces.animation.link);
4668 shell->workspaces.animation.frame = animate_workspace_change_frame;
4670 if (wl_global_create(ec->wl_display, &wl_shell_interface, 1,
4671 shell, bind_shell) == NULL)
4674 if (wl_global_create(ec->wl_display,
4675 &desktop_shell_interface, 2,
4676 shell, bind_desktop_shell) == NULL)
4679 if (wl_global_create(ec->wl_display, &screensaver_interface, 1,
4680 shell, bind_screensaver) == NULL)
4683 if (wl_global_create(ec->wl_display, &wl_input_panel_interface, 1,
4684 shell, bind_input_panel) == NULL)
4687 if (wl_global_create(ec->wl_display, &workspace_manager_interface, 1,
4688 shell, bind_workspace_manager) == NULL)
4691 shell->child.deathstamp = weston_compositor_get_time();
4693 loop = wl_display_get_event_loop(ec->wl_display);
4694 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
4696 shell->screensaver.timer =
4697 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4699 wl_list_for_each(seat, &ec->seat_list, link)
4700 create_pointer_focus_listener(seat);
4702 shell_add_bindings(ec, shell);
4704 shell_fade_init(shell);