2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
33 #include <sys/types.h>
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "workspaces-server-protocol.h"
39 #include "../shared/config-parser.h"
41 #define DEFAULT_NUM_WORKSPACES 1
42 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
52 struct weston_seat *seat;
54 struct weston_surface *keyboard_focus;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
61 struct weston_layer layer;
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
67 struct input_panel_surface {
69 struct weston_surface *surface;
70 struct wl_listener listener;
73 struct desktop_shell {
74 struct weston_compositor *compositor;
76 struct wl_listener lock_listener;
77 struct wl_listener unlock_listener;
78 struct wl_listener destroy_listener;
79 struct wl_listener show_input_panel_listener;
80 struct wl_listener hide_input_panel_listener;
82 struct weston_layer fullscreen_layer;
83 struct weston_layer panel_layer;
84 struct weston_layer background_layer;
85 struct weston_layer lock_layer;
86 struct weston_layer input_panel_layer;
88 struct wl_listener pointer_focus_listener;
89 struct weston_surface *grab_surface;
92 struct weston_process process;
93 struct wl_client *client;
94 struct wl_resource *desktop_shell;
101 bool showing_input_panels;
102 bool prepare_event_sent;
104 struct weston_surface *lock_surface;
105 struct wl_listener lock_surface_listener;
108 struct wl_array array;
109 unsigned int current;
112 struct wl_list client_list;
114 struct weston_animation animation;
115 struct wl_list anim_sticky_list;
117 uint32_t anim_timestamp;
119 struct workspace *anim_from;
120 struct workspace *anim_to;
126 struct wl_resource *binding;
127 struct wl_list surfaces;
128 struct weston_process process;
132 struct wl_resource *binding;
133 struct wl_list surfaces;
136 uint32_t binding_modifier;
137 enum animation_type win_animation_type;
138 struct weston_surface *debug_repaint_surface;
141 enum shell_surface_type {
143 SHELL_SURFACE_TOPLEVEL,
144 SHELL_SURFACE_TRANSIENT,
145 SHELL_SURFACE_FULLSCREEN,
146 SHELL_SURFACE_MAXIMIZED,
151 struct wl_event_source *source;
155 struct shell_surface {
156 struct wl_resource resource;
158 struct weston_surface *surface;
159 struct wl_listener surface_destroy_listener;
160 struct weston_surface *parent;
161 struct desktop_shell *shell;
163 enum shell_surface_type type, next_type;
165 int32_t saved_x, saved_y;
166 bool saved_position_valid;
167 bool saved_rotation_valid;
171 struct weston_transform transform;
172 struct weston_matrix rotation;
176 struct wl_pointer_grab grab;
178 struct weston_transform parent_transform;
180 struct wl_seat *seat;
190 enum wl_shell_surface_fullscreen_method type;
191 struct weston_transform transform; /* matrix from x, y */
193 struct weston_surface *black_surface;
196 struct ping_timer *ping_timer;
198 struct weston_transform workspace_transform;
200 struct weston_output *fullscreen_output;
201 struct weston_output *output;
204 const struct weston_shell_client *client;
208 struct wl_pointer_grab grab;
209 struct shell_surface *shsurf;
210 struct wl_listener shsurf_destroy_listener;
211 struct wl_pointer *pointer;
214 struct weston_move_grab {
215 struct shell_grab base;
220 struct shell_grab base;
221 struct weston_matrix rotation;
229 activate(struct desktop_shell *shell, struct weston_surface *es,
230 struct weston_seat *seat);
232 static struct workspace *
233 get_current_workspace(struct desktop_shell *shell);
235 static struct shell_surface *
236 get_shell_surface(struct weston_surface *surface);
238 static struct desktop_shell *
239 shell_surface_get_shell(struct shell_surface *shsurf);
242 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
244 struct desktop_shell *shell;
245 struct weston_surface *top_fs_es;
247 shell = shell_surface_get_shell(shsurf);
249 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
252 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
253 struct weston_surface,
255 return (shsurf == get_shell_surface(top_fs_es));
259 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
261 struct shell_grab *grab;
263 grab = container_of(listener, struct shell_grab,
264 shsurf_destroy_listener);
270 shell_grab_start(struct shell_grab *grab,
271 const struct wl_pointer_grab_interface *interface,
272 struct shell_surface *shsurf,
273 struct wl_pointer *pointer,
274 enum desktop_shell_cursor cursor)
276 struct desktop_shell *shell = shsurf->shell;
278 grab->grab.interface = interface;
279 grab->shsurf = shsurf;
280 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
281 wl_signal_add(&shsurf->resource.destroy_signal,
282 &grab->shsurf_destroy_listener);
284 grab->pointer = pointer;
285 grab->grab.focus = &shsurf->surface->surface;
287 wl_pointer_start_grab(pointer, &grab->grab);
288 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
289 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
290 wl_fixed_from_int(0), wl_fixed_from_int(0));
294 shell_grab_end(struct shell_grab *grab)
297 wl_list_remove(&grab->shsurf_destroy_listener.link);
299 wl_pointer_end_grab(grab->pointer);
303 center_on_output(struct weston_surface *surface,
304 struct weston_output *output);
306 static enum weston_keyboard_modifier
307 get_modifier(char *modifier)
310 return MODIFIER_SUPER;
312 if (!strcmp("ctrl", modifier))
313 return MODIFIER_CTRL;
314 else if (!strcmp("alt", modifier))
316 else if (!strcmp("super", modifier))
317 return MODIFIER_SUPER;
319 return MODIFIER_SUPER;
322 static enum animation_type
323 get_animation_type(char *animation)
326 return ANIMATION_NONE;
328 if (!strcmp("zoom", animation))
329 return ANIMATION_ZOOM;
330 else if (!strcmp("fade", animation))
331 return ANIMATION_FADE;
333 return ANIMATION_NONE;
337 shell_configuration(struct desktop_shell *shell)
342 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
343 char *modifier = NULL;
344 char *win_animation = NULL;
346 struct config_key shell_keys[] = {
347 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
348 { "animation", CONFIG_KEY_STRING, &win_animation},
350 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
353 struct config_key saver_keys[] = {
354 { "path", CONFIG_KEY_STRING, &path },
355 { "duration", CONFIG_KEY_INTEGER, &duration },
358 struct config_section cs[] = {
359 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
360 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
363 config_file = config_file_path("weston.ini");
364 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
367 shell->screensaver.path = path;
368 shell->screensaver.duration = duration;
369 shell->binding_modifier = get_modifier(modifier);
370 shell->win_animation_type = get_animation_type(win_animation);
371 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
375 focus_state_destroy(struct focus_state *state)
377 wl_list_remove(&state->seat_destroy_listener.link);
378 wl_list_remove(&state->surface_destroy_listener.link);
383 focus_state_seat_destroy(struct wl_listener *listener, void *data)
385 struct focus_state *state = container_of(listener,
387 seat_destroy_listener);
389 wl_list_remove(&state->link);
390 focus_state_destroy(state);
394 focus_state_surface_destroy(struct wl_listener *listener, void *data)
396 struct focus_state *state = container_of(listener,
398 surface_destroy_listener);
399 struct desktop_shell *shell;
400 struct weston_surface *surface, *next;
403 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
404 if (surface == state->keyboard_focus)
412 shell = state->seat->compositor->shell_interface.shell;
413 activate(shell, next, state->seat);
415 wl_list_remove(&state->link);
416 focus_state_destroy(state);
420 static struct focus_state *
421 focus_state_create(struct weston_seat *seat, struct workspace *ws)
423 struct focus_state *state;
425 state = malloc(sizeof *state);
431 wl_list_insert(&ws->focus_list, &state->link);
433 state->seat_destroy_listener.notify = focus_state_seat_destroy;
434 state->surface_destroy_listener.notify = focus_state_surface_destroy;
435 wl_signal_add(&seat->seat.destroy_signal,
436 &state->seat_destroy_listener);
437 wl_list_init(&state->surface_destroy_listener.link);
442 static struct focus_state *
443 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
445 struct workspace *ws = get_current_workspace(shell);
446 struct focus_state *state;
448 wl_list_for_each(state, &ws->focus_list, link)
449 if (state->seat == seat)
452 if (&state->link == &ws->focus_list)
453 state = focus_state_create(seat, ws);
459 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
461 struct focus_state *state, *next;
462 struct wl_surface *surface;
464 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
465 surface = state->keyboard_focus ?
466 &state->keyboard_focus->surface : NULL;
468 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
473 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
474 struct weston_seat *seat)
476 struct focus_state *state;
477 struct wl_surface *surface;
479 wl_list_for_each(state, &ws->focus_list, link) {
480 if (state->seat == seat) {
481 surface = seat->seat.keyboard->focus;
482 state->keyboard_focus =
483 (struct weston_surface *) surface;
490 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
491 struct weston_surface *surface)
493 struct focus_state *state;
495 wl_list_for_each(state, &ws->focus_list, link)
496 if (state->keyboard_focus == surface)
497 state->keyboard_focus = NULL;
501 workspace_destroy(struct workspace *ws)
503 struct focus_state *state, *next;
505 wl_list_for_each_safe(state, next, &ws->focus_list, link)
506 focus_state_destroy(state);
512 seat_destroyed(struct wl_listener *listener, void *data)
514 struct weston_seat *seat = data;
515 struct focus_state *state, *next;
516 struct workspace *ws = container_of(listener,
518 seat_destroyed_listener);
520 wl_list_for_each_safe(state, next, &ws->focus_list, link)
521 if (state->seat == seat)
522 wl_list_remove(&state->link);
525 static struct workspace *
526 workspace_create(void)
528 struct workspace *ws = malloc(sizeof *ws);
532 weston_layer_init(&ws->layer, NULL);
534 wl_list_init(&ws->focus_list);
535 wl_list_init(&ws->seat_destroyed_listener.link);
536 ws->seat_destroyed_listener.notify = seat_destroyed;
542 workspace_is_empty(struct workspace *ws)
544 return wl_list_empty(&ws->layer.surface_list);
547 static struct workspace *
548 get_workspace(struct desktop_shell *shell, unsigned int index)
550 struct workspace **pws = shell->workspaces.array.data;
551 assert(index < shell->workspaces.num);
556 static struct workspace *
557 get_current_workspace(struct desktop_shell *shell)
559 return get_workspace(shell, shell->workspaces.current);
563 activate_workspace(struct desktop_shell *shell, unsigned int index)
565 struct workspace *ws;
567 ws = get_workspace(shell, index);
568 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
570 shell->workspaces.current = index;
574 get_output_height(struct weston_output *output)
576 return abs(output->region.extents.y1 - output->region.extents.y2);
580 surface_translate(struct weston_surface *surface, double d)
582 struct shell_surface *shsurf = get_shell_surface(surface);
583 struct weston_transform *transform;
585 transform = &shsurf->workspace_transform;
586 if (wl_list_empty(&transform->link))
587 wl_list_insert(surface->geometry.transformation_list.prev,
588 &shsurf->workspace_transform.link);
590 weston_matrix_init(&shsurf->workspace_transform.matrix);
591 weston_matrix_translate(&shsurf->workspace_transform.matrix,
593 surface->geometry.dirty = 1;
597 workspace_translate_out(struct workspace *ws, double fraction)
599 struct weston_surface *surface;
603 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
604 height = get_output_height(surface->output);
605 d = height * fraction;
607 surface_translate(surface, d);
612 workspace_translate_in(struct workspace *ws, double fraction)
614 struct weston_surface *surface;
618 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
619 height = get_output_height(surface->output);
622 d = -(height - height * fraction);
624 d = height + height * fraction;
626 surface_translate(surface, d);
631 broadcast_current_workspace_state(struct desktop_shell *shell)
633 struct wl_resource *resource;
635 wl_list_for_each(resource, &shell->workspaces.client_list, link)
636 workspace_manager_send_state(resource,
637 shell->workspaces.current,
638 shell->workspaces.num);
642 reverse_workspace_change_animation(struct desktop_shell *shell,
644 struct workspace *from,
645 struct workspace *to)
647 shell->workspaces.current = index;
649 shell->workspaces.anim_to = to;
650 shell->workspaces.anim_from = from;
651 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
652 shell->workspaces.anim_timestamp = 0;
654 weston_compositor_schedule_repaint(shell->compositor);
658 workspace_deactivate_transforms(struct workspace *ws)
660 struct weston_surface *surface;
661 struct shell_surface *shsurf;
663 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
664 shsurf = get_shell_surface(surface);
665 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
666 wl_list_remove(&shsurf->workspace_transform.link);
667 wl_list_init(&shsurf->workspace_transform.link);
669 shsurf->surface->geometry.dirty = 1;
674 finish_workspace_change_animation(struct desktop_shell *shell,
675 struct workspace *from,
676 struct workspace *to)
678 weston_compositor_schedule_repaint(shell->compositor);
680 wl_list_remove(&shell->workspaces.animation.link);
681 workspace_deactivate_transforms(from);
682 workspace_deactivate_transforms(to);
683 shell->workspaces.anim_to = NULL;
685 wl_list_remove(&shell->workspaces.anim_from->layer.link);
689 animate_workspace_change_frame(struct weston_animation *animation,
690 struct weston_output *output, uint32_t msecs)
692 struct desktop_shell *shell =
693 container_of(animation, struct desktop_shell,
694 workspaces.animation);
695 struct workspace *from = shell->workspaces.anim_from;
696 struct workspace *to = shell->workspaces.anim_to;
700 if (workspace_is_empty(from) && workspace_is_empty(to)) {
701 finish_workspace_change_animation(shell, from, to);
705 if (shell->workspaces.anim_timestamp == 0) {
706 if (shell->workspaces.anim_current == 0.0)
707 shell->workspaces.anim_timestamp = msecs;
709 shell->workspaces.anim_timestamp =
711 /* Invers of movement function 'y' below. */
712 (asin(1.0 - shell->workspaces.anim_current) *
713 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
717 t = msecs - shell->workspaces.anim_timestamp;
723 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
726 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
727 weston_compositor_schedule_repaint(shell->compositor);
729 workspace_translate_out(from, shell->workspaces.anim_dir * y);
730 workspace_translate_in(to, shell->workspaces.anim_dir * y);
731 shell->workspaces.anim_current = y;
733 weston_compositor_schedule_repaint(shell->compositor);
736 finish_workspace_change_animation(shell, from, to);
740 animate_workspace_change(struct desktop_shell *shell,
742 struct workspace *from,
743 struct workspace *to)
745 struct weston_output *output;
749 if (index > shell->workspaces.current)
754 shell->workspaces.current = index;
756 shell->workspaces.anim_dir = dir;
757 shell->workspaces.anim_from = from;
758 shell->workspaces.anim_to = to;
759 shell->workspaces.anim_current = 0.0;
760 shell->workspaces.anim_timestamp = 0;
762 output = container_of(shell->compositor->output_list.next,
763 struct weston_output, link);
764 wl_list_insert(&output->animation_list,
765 &shell->workspaces.animation.link);
767 wl_list_insert(from->layer.link.prev, &to->layer.link);
769 workspace_translate_in(to, 0);
771 restore_focus_state(shell, to);
773 weston_compositor_schedule_repaint(shell->compositor);
777 update_workspace(struct desktop_shell *shell, unsigned int index,
778 struct workspace *from, struct workspace *to)
780 shell->workspaces.current = index;
781 wl_list_insert(&from->layer.link, &to->layer.link);
782 wl_list_remove(&from->layer.link);
786 change_workspace(struct desktop_shell *shell, unsigned int index)
788 struct workspace *from;
789 struct workspace *to;
791 if (index == shell->workspaces.current)
794 /* Don't change workspace when there is any fullscreen surfaces. */
795 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
798 from = get_current_workspace(shell);
799 to = get_workspace(shell, index);
801 if (shell->workspaces.anim_from == to &&
802 shell->workspaces.anim_to == from) {
803 restore_focus_state(shell, to);
804 reverse_workspace_change_animation(shell, index, from, to);
805 broadcast_current_workspace_state(shell);
809 if (shell->workspaces.anim_to != NULL)
810 finish_workspace_change_animation(shell,
811 shell->workspaces.anim_from,
812 shell->workspaces.anim_to);
814 restore_focus_state(shell, to);
816 if (workspace_is_empty(to) && workspace_is_empty(from))
817 update_workspace(shell, index, from, to);
819 animate_workspace_change(shell, index, from, to);
821 broadcast_current_workspace_state(shell);
825 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
827 struct wl_list *list = &ws->layer.surface_list;
830 if (wl_list_empty(list))
838 return container_of(e, struct weston_surface, layer_link) == surface;
842 move_surface_to_workspace(struct desktop_shell *shell,
843 struct weston_surface *surface,
846 struct workspace *from;
847 struct workspace *to;
848 struct weston_seat *seat;
850 if (workspace == shell->workspaces.current)
853 if (workspace >= shell->workspaces.num)
854 workspace = shell->workspaces.num - 1;
856 from = get_current_workspace(shell);
857 to = get_workspace(shell, workspace);
859 wl_list_remove(&surface->layer_link);
860 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
862 drop_focus_state(shell, from, surface);
863 wl_list_for_each(seat, &shell->compositor->seat_list, link)
864 if (seat->has_keyboard &&
865 seat->keyboard.focus == &surface->surface)
866 wl_keyboard_set_focus(&seat->keyboard, NULL);
868 weston_surface_damage_below(surface);
872 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
873 struct wl_seat *wl_seat,
876 struct weston_seat *seat = (struct weston_seat *) wl_seat;
877 struct weston_surface *surface =
878 (struct weston_surface *) wl_seat->keyboard->focus;
879 struct shell_surface *shsurf;
880 struct workspace *from;
881 struct workspace *to;
882 struct focus_state *state;
884 if (surface == NULL ||
885 index == shell->workspaces.current)
888 from = get_current_workspace(shell);
889 to = get_workspace(shell, index);
891 wl_list_remove(&surface->layer_link);
892 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
894 replace_focus_state(shell, to, seat);
895 drop_focus_state(shell, from, surface);
897 if (shell->workspaces.anim_from == to &&
898 shell->workspaces.anim_to == from) {
899 wl_list_remove(&to->layer.link);
900 wl_list_insert(from->layer.link.prev, &to->layer.link);
902 reverse_workspace_change_animation(shell, index, from, to);
903 broadcast_current_workspace_state(shell);
908 if (shell->workspaces.anim_to != NULL)
909 finish_workspace_change_animation(shell,
910 shell->workspaces.anim_from,
911 shell->workspaces.anim_to);
913 if (workspace_is_empty(from) &&
914 workspace_has_only(to, surface))
915 update_workspace(shell, index, from, to);
917 shsurf = get_shell_surface(surface);
918 if (wl_list_empty(&shsurf->workspace_transform.link))
919 wl_list_insert(&shell->workspaces.anim_sticky_list,
920 &shsurf->workspace_transform.link);
922 animate_workspace_change(shell, index, from, to);
925 broadcast_current_workspace_state(shell);
927 state = ensure_focus_state(shell, seat);
929 state->keyboard_focus = surface;
933 workspace_manager_move_surface(struct wl_client *client,
934 struct wl_resource *resource,
935 struct wl_resource *surface_resource,
938 struct desktop_shell *shell = resource->data;
939 struct weston_surface *surface =
940 (struct weston_surface *) surface_resource;
942 move_surface_to_workspace(shell, surface, workspace);
945 static const struct workspace_manager_interface workspace_manager_implementation = {
946 workspace_manager_move_surface,
950 unbind_resource(struct wl_resource *resource)
952 wl_list_remove(&resource->link);
957 bind_workspace_manager(struct wl_client *client,
958 void *data, uint32_t version, uint32_t id)
960 struct desktop_shell *shell = data;
961 struct wl_resource *resource;
963 resource = wl_client_add_object(client, &workspace_manager_interface,
964 &workspace_manager_implementation,
967 if (resource == NULL) {
968 weston_log("couldn't add workspace manager object");
972 resource->destroy = unbind_resource;
973 wl_list_insert(&shell->workspaces.client_list, &resource->link);
975 workspace_manager_send_state(resource,
976 shell->workspaces.current,
977 shell->workspaces.num);
981 noop_grab_focus(struct wl_pointer_grab *grab,
982 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
988 move_grab_motion(struct wl_pointer_grab *grab,
989 uint32_t time, wl_fixed_t x, wl_fixed_t y)
991 struct weston_move_grab *move = (struct weston_move_grab *) grab;
992 struct wl_pointer *pointer = grab->pointer;
993 struct shell_surface *shsurf = move->base.shsurf;
994 struct weston_surface *es;
995 int dx = wl_fixed_to_int(pointer->x + move->dx);
996 int dy = wl_fixed_to_int(pointer->y + move->dy);
1001 es = shsurf->surface;
1003 weston_surface_configure(es, dx, dy,
1004 es->geometry.width, es->geometry.height);
1006 weston_compositor_schedule_repaint(es->compositor);
1010 move_grab_button(struct wl_pointer_grab *grab,
1011 uint32_t time, uint32_t button, uint32_t state_w)
1013 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1015 struct wl_pointer *pointer = grab->pointer;
1016 enum wl_pointer_button_state state = state_w;
1018 if (pointer->button_count == 0 &&
1019 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1020 shell_grab_end(shell_grab);
1025 static const struct wl_pointer_grab_interface move_grab_interface = {
1032 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1034 struct weston_move_grab *move;
1039 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1042 move = malloc(sizeof *move);
1046 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1047 ws->seat.pointer->grab_x;
1048 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1049 ws->seat.pointer->grab_y;
1051 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1052 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1058 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1059 struct wl_resource *seat_resource, uint32_t serial)
1061 struct weston_seat *ws = seat_resource->data;
1062 struct shell_surface *shsurf = resource->data;
1064 if (ws->seat.pointer->button_count == 0 ||
1065 ws->seat.pointer->grab_serial != serial ||
1066 ws->seat.pointer->focus != &shsurf->surface->surface)
1069 if (surface_move(shsurf, ws) < 0)
1070 wl_resource_post_no_memory(resource);
1073 struct weston_resize_grab {
1074 struct shell_grab base;
1076 int32_t width, height;
1080 resize_grab_motion(struct wl_pointer_grab *grab,
1081 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1083 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1084 struct wl_pointer *pointer = grab->pointer;
1085 struct shell_surface *shsurf = resize->base.shsurf;
1086 int32_t width, height;
1087 wl_fixed_t from_x, from_y;
1088 wl_fixed_t to_x, to_y;
1093 weston_surface_from_global_fixed(shsurf->surface,
1094 pointer->grab_x, pointer->grab_y,
1096 weston_surface_from_global_fixed(shsurf->surface,
1097 pointer->x, pointer->y, &to_x, &to_y);
1099 width = resize->width;
1100 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1101 width += wl_fixed_to_int(from_x - to_x);
1102 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1103 width += wl_fixed_to_int(to_x - from_x);
1106 height = resize->height;
1107 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1108 height += wl_fixed_to_int(from_y - to_y);
1109 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1110 height += wl_fixed_to_int(to_y - from_y);
1113 shsurf->client->send_configure(shsurf->surface,
1114 resize->edges, width, height);
1118 send_configure(struct weston_surface *surface,
1119 uint32_t edges, int32_t width, int32_t height)
1121 struct shell_surface *shsurf = get_shell_surface(surface);
1123 wl_shell_surface_send_configure(&shsurf->resource,
1124 edges, width, height);
1127 static const struct weston_shell_client shell_client = {
1132 resize_grab_button(struct wl_pointer_grab *grab,
1133 uint32_t time, uint32_t button, uint32_t state_w)
1135 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1136 struct wl_pointer *pointer = grab->pointer;
1137 enum wl_pointer_button_state state = state_w;
1139 if (pointer->button_count == 0 &&
1140 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1141 shell_grab_end(&resize->base);
1146 static const struct wl_pointer_grab_interface resize_grab_interface = {
1153 surface_resize(struct shell_surface *shsurf,
1154 struct weston_seat *ws, uint32_t edges)
1156 struct weston_resize_grab *resize;
1158 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1161 if (edges == 0 || edges > 15 ||
1162 (edges & 3) == 3 || (edges & 12) == 12)
1165 resize = malloc(sizeof *resize);
1169 resize->edges = edges;
1170 resize->width = shsurf->surface->geometry.width;
1171 resize->height = shsurf->surface->geometry.height;
1173 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1174 ws->seat.pointer, edges);
1180 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1181 struct wl_resource *seat_resource, uint32_t serial,
1184 struct weston_seat *ws = seat_resource->data;
1185 struct shell_surface *shsurf = resource->data;
1187 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1190 if (ws->seat.pointer->button_count == 0 ||
1191 ws->seat.pointer->grab_serial != serial ||
1192 ws->seat.pointer->focus != &shsurf->surface->surface)
1195 if (surface_resize(shsurf, ws, edges) < 0)
1196 wl_resource_post_no_memory(resource);
1200 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1201 struct wl_surface *surface, int32_t x, int32_t y)
1203 struct shell_grab *grab = (struct shell_grab *) base;
1205 if (grab->grab.focus != surface) {
1206 shell_grab_end(grab);
1212 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1213 uint32_t time, int32_t x, int32_t y)
1218 busy_cursor_grab_button(struct wl_pointer_grab *base,
1219 uint32_t time, uint32_t button, uint32_t state)
1221 struct shell_grab *grab = (struct shell_grab *) base;
1222 struct shell_surface *shsurf;
1223 struct weston_surface *surface =
1224 (struct weston_surface *) grab->grab.pointer->current;
1225 struct weston_seat *seat =
1226 (struct weston_seat *) grab->grab.pointer->seat;
1228 shsurf = get_shell_surface(surface);
1229 if (shsurf && button == BTN_LEFT && state) {
1230 activate(shsurf->shell, shsurf->surface, seat);
1231 surface_move(shsurf, seat);
1235 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1236 busy_cursor_grab_focus,
1237 busy_cursor_grab_motion,
1238 busy_cursor_grab_button,
1242 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1244 struct shell_grab *grab;
1246 grab = malloc(sizeof *grab);
1250 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1251 DESKTOP_SHELL_CURSOR_BUSY);
1255 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1257 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1259 if (grab->grab.interface == &busy_cursor_grab_interface) {
1260 shell_grab_end(grab);
1266 ping_timer_destroy(struct shell_surface *shsurf)
1268 if (!shsurf || !shsurf->ping_timer)
1271 if (shsurf->ping_timer->source)
1272 wl_event_source_remove(shsurf->ping_timer->source);
1274 free(shsurf->ping_timer);
1275 shsurf->ping_timer = NULL;
1279 ping_timeout_handler(void *data)
1281 struct shell_surface *shsurf = data;
1282 struct weston_seat *seat;
1284 /* Client is not responding */
1285 shsurf->unresponsive = 1;
1287 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1288 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1289 set_busy_cursor(shsurf, seat->seat.pointer);
1295 ping_handler(struct weston_surface *surface, uint32_t serial)
1297 struct shell_surface *shsurf = get_shell_surface(surface);
1298 struct wl_event_loop *loop;
1299 int ping_timeout = 200;
1303 if (!shsurf->resource.client)
1306 if (shsurf->surface == shsurf->shell->grab_surface)
1309 if (!shsurf->ping_timer) {
1310 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1311 if (!shsurf->ping_timer)
1314 shsurf->ping_timer->serial = serial;
1315 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1316 shsurf->ping_timer->source =
1317 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1318 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1320 wl_shell_surface_send_ping(&shsurf->resource, serial);
1325 handle_pointer_focus(struct wl_listener *listener, void *data)
1327 struct wl_pointer *pointer = data;
1328 struct weston_surface *surface =
1329 (struct weston_surface *) pointer->focus;
1330 struct weston_compositor *compositor;
1331 struct shell_surface *shsurf;
1337 compositor = surface->compositor;
1338 shsurf = get_shell_surface(surface);
1340 if (shsurf && shsurf->unresponsive) {
1341 set_busy_cursor(shsurf, pointer);
1343 serial = wl_display_next_serial(compositor->wl_display);
1344 ping_handler(surface, serial);
1349 create_pointer_focus_listener(struct weston_seat *seat)
1351 struct wl_listener *listener;
1353 if (!seat->seat.pointer)
1356 listener = malloc(sizeof *listener);
1357 listener->notify = handle_pointer_focus;
1358 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1362 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1365 struct shell_surface *shsurf = resource->data;
1366 struct desktop_shell *shell = shsurf->shell;
1367 struct weston_seat *seat;
1368 struct weston_compositor *ec = shsurf->surface->compositor;
1369 struct wl_pointer *pointer;
1370 int was_unresponsive;
1372 if (shsurf->ping_timer == NULL)
1373 /* Just ignore unsolicited pong. */
1376 if (shsurf->ping_timer->serial == serial) {
1377 was_unresponsive = shsurf->unresponsive;
1378 shsurf->unresponsive = 0;
1379 if (was_unresponsive) {
1380 /* Received pong from previously unresponsive client */
1381 wl_list_for_each(seat, &ec->seat_list, link) {
1382 pointer = seat->seat.pointer;
1383 if (pointer->focus ==
1384 &shell->grab_surface->surface &&
1386 &shsurf->surface->surface)
1387 end_busy_cursor(shsurf, pointer);
1390 ping_timer_destroy(shsurf);
1395 shell_surface_set_title(struct wl_client *client,
1396 struct wl_resource *resource, const char *title)
1398 struct shell_surface *shsurf = resource->data;
1400 free(shsurf->title);
1401 shsurf->title = strdup(title);
1405 shell_surface_set_class(struct wl_client *client,
1406 struct wl_resource *resource, const char *class)
1408 struct shell_surface *shsurf = resource->data;
1410 free(shsurf->class);
1411 shsurf->class = strdup(class);
1414 static struct weston_output *
1415 get_default_output(struct weston_compositor *compositor)
1417 return container_of(compositor->output_list.next,
1418 struct weston_output, link);
1422 shell_unset_fullscreen(struct shell_surface *shsurf)
1424 struct workspace *ws;
1425 /* undo all fullscreen things here */
1426 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1427 shell_surface_is_top_fullscreen(shsurf)) {
1428 weston_output_switch_mode(shsurf->fullscreen_output,
1429 shsurf->fullscreen_output->origin);
1431 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1432 shsurf->fullscreen.framerate = 0;
1433 wl_list_remove(&shsurf->fullscreen.transform.link);
1434 wl_list_init(&shsurf->fullscreen.transform.link);
1435 if (shsurf->fullscreen.black_surface)
1436 weston_surface_destroy(shsurf->fullscreen.black_surface);
1437 shsurf->fullscreen.black_surface = NULL;
1438 shsurf->fullscreen_output = NULL;
1439 weston_surface_set_position(shsurf->surface,
1440 shsurf->saved_x, shsurf->saved_y);
1441 if (shsurf->saved_rotation_valid) {
1442 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1443 &shsurf->rotation.transform.link);
1444 shsurf->saved_rotation_valid = false;
1447 ws = get_current_workspace(shsurf->shell);
1448 wl_list_remove(&shsurf->surface->layer_link);
1449 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1453 reset_shell_surface_type(struct shell_surface *surface)
1455 switch (surface->type) {
1456 case SHELL_SURFACE_FULLSCREEN:
1457 shell_unset_fullscreen(surface);
1459 case SHELL_SURFACE_MAXIMIZED:
1460 surface->output = get_default_output(surface->surface->compositor);
1461 weston_surface_set_position(surface->surface,
1465 case SHELL_SURFACE_NONE:
1466 case SHELL_SURFACE_TOPLEVEL:
1467 case SHELL_SURFACE_TRANSIENT:
1468 case SHELL_SURFACE_POPUP:
1472 surface->type = SHELL_SURFACE_NONE;
1477 set_surface_type(struct shell_surface *shsurf)
1479 struct weston_surface *surface = shsurf->surface;
1480 struct weston_surface *pes = shsurf->parent;
1482 reset_shell_surface_type(shsurf);
1484 shsurf->type = shsurf->next_type;
1485 shsurf->next_type = SHELL_SURFACE_NONE;
1487 switch (shsurf->type) {
1488 case SHELL_SURFACE_TOPLEVEL:
1490 case SHELL_SURFACE_TRANSIENT:
1491 weston_surface_set_position(surface,
1492 pes->geometry.x + shsurf->transient.x,
1493 pes->geometry.y + shsurf->transient.y);
1496 case SHELL_SURFACE_MAXIMIZED:
1497 shsurf->saved_x = surface->geometry.x;
1498 shsurf->saved_y = surface->geometry.y;
1499 shsurf->saved_position_valid = true;
1502 case SHELL_SURFACE_FULLSCREEN:
1503 shsurf->saved_x = surface->geometry.x;
1504 shsurf->saved_y = surface->geometry.y;
1505 shsurf->saved_position_valid = true;
1507 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1508 wl_list_remove(&shsurf->rotation.transform.link);
1509 wl_list_init(&shsurf->rotation.transform.link);
1510 shsurf->surface->geometry.dirty = 1;
1511 shsurf->saved_rotation_valid = true;
1521 set_toplevel(struct shell_surface *shsurf)
1523 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1527 shell_surface_set_toplevel(struct wl_client *client,
1528 struct wl_resource *resource)
1530 struct shell_surface *surface = resource->data;
1532 set_toplevel(surface);
1536 set_transient(struct shell_surface *shsurf,
1537 struct weston_surface *parent, int x, int y, uint32_t flags)
1539 /* assign to parents output */
1540 shsurf->parent = parent;
1541 shsurf->transient.x = x;
1542 shsurf->transient.y = y;
1543 shsurf->transient.flags = flags;
1544 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1548 shell_surface_set_transient(struct wl_client *client,
1549 struct wl_resource *resource,
1550 struct wl_resource *parent_resource,
1551 int x, int y, uint32_t flags)
1553 struct shell_surface *shsurf = resource->data;
1554 struct weston_surface *parent = parent_resource->data;
1556 set_transient(shsurf, parent, x, y, flags);
1559 static struct desktop_shell *
1560 shell_surface_get_shell(struct shell_surface *shsurf)
1562 return shsurf->shell;
1566 get_output_panel_height(struct desktop_shell *shell,
1567 struct weston_output *output)
1569 struct weston_surface *surface;
1570 int panel_height = 0;
1575 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1576 if (surface->output == output) {
1577 panel_height = surface->geometry.height;
1582 return panel_height;
1586 shell_surface_set_maximized(struct wl_client *client,
1587 struct wl_resource *resource,
1588 struct wl_resource *output_resource )
1590 struct shell_surface *shsurf = resource->data;
1591 struct weston_surface *es = shsurf->surface;
1592 struct desktop_shell *shell = NULL;
1593 uint32_t edges = 0, panel_height = 0;
1595 /* get the default output, if the client set it as NULL
1596 check whether the ouput is available */
1597 if (output_resource)
1598 shsurf->output = output_resource->data;
1599 else if (es->output)
1600 shsurf->output = es->output;
1602 shsurf->output = get_default_output(es->compositor);
1604 shell = shell_surface_get_shell(shsurf);
1605 panel_height = get_output_panel_height(shell, shsurf->output);
1606 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1608 shsurf->client->send_configure(shsurf->surface, edges,
1609 shsurf->output->width,
1610 shsurf->output->height - panel_height);
1612 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1616 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1618 static struct weston_surface *
1619 create_black_surface(struct weston_compositor *ec,
1620 struct weston_surface *fs_surface,
1621 float x, float y, int w, int h)
1623 struct weston_surface *surface = NULL;
1625 surface = weston_surface_create(ec);
1626 if (surface == NULL) {
1627 weston_log("no memory\n");
1631 surface->configure = black_surface_configure;
1632 surface->private = fs_surface;
1633 weston_surface_configure(surface, x, y, w, h);
1634 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1635 pixman_region32_fini(&surface->opaque);
1636 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1641 /* Create black surface and append it to the associated fullscreen surface.
1642 * Handle size dismatch and positioning according to the method. */
1644 shell_configure_fullscreen(struct shell_surface *shsurf)
1646 struct weston_output *output = shsurf->fullscreen_output;
1647 struct weston_surface *surface = shsurf->surface;
1648 struct weston_matrix *matrix;
1649 float scale, output_aspect, surface_aspect, x, y;
1651 if (!shsurf->fullscreen.black_surface)
1652 shsurf->fullscreen.black_surface =
1653 create_black_surface(surface->compositor,
1655 output->x, output->y,
1659 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1660 wl_list_insert(&surface->layer_link,
1661 &shsurf->fullscreen.black_surface->layer_link);
1662 shsurf->fullscreen.black_surface->output = output;
1664 switch (shsurf->fullscreen.type) {
1665 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1666 if (surface->buffer)
1667 center_on_output(surface, shsurf->fullscreen_output);
1669 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1670 matrix = &shsurf->fullscreen.transform.matrix;
1671 weston_matrix_init(matrix);
1673 output_aspect = (float) output->width /
1674 (float) output->height;
1675 surface_aspect = (float) surface->geometry.width /
1676 (float) surface->geometry.height;
1677 if (output_aspect < surface_aspect)
1678 scale = (float) output->width /
1679 (float) surface->geometry.width;
1681 scale = (float) output->height /
1682 (float) surface->geometry.height;
1684 weston_matrix_scale(matrix, scale, scale, 1);
1685 wl_list_remove(&shsurf->fullscreen.transform.link);
1686 wl_list_insert(&surface->geometry.transformation_list,
1687 &shsurf->fullscreen.transform.link);
1688 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1689 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1690 weston_surface_set_position(surface, x, y);
1693 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1694 if (shell_surface_is_top_fullscreen(shsurf)) {
1695 struct weston_mode mode = {0,
1696 surface->geometry.width,
1697 surface->geometry.height,
1698 shsurf->fullscreen.framerate};
1700 if (weston_output_switch_mode(output, &mode) == 0) {
1701 weston_surface_configure(shsurf->fullscreen.black_surface,
1702 output->x, output->y,
1705 weston_surface_set_position(surface, output->x, output->y);
1710 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1717 /* make the fullscreen and black surface at the top */
1719 shell_stack_fullscreen(struct shell_surface *shsurf)
1721 struct weston_output *output = shsurf->fullscreen_output;
1722 struct weston_surface *surface = shsurf->surface;
1723 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1725 wl_list_remove(&surface->layer_link);
1726 wl_list_insert(&shell->fullscreen_layer.surface_list,
1727 &surface->layer_link);
1728 weston_surface_damage(surface);
1730 if (!shsurf->fullscreen.black_surface)
1731 shsurf->fullscreen.black_surface =
1732 create_black_surface(surface->compositor,
1734 output->x, output->y,
1738 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1739 wl_list_insert(&surface->layer_link,
1740 &shsurf->fullscreen.black_surface->layer_link);
1741 weston_surface_damage(shsurf->fullscreen.black_surface);
1745 shell_map_fullscreen(struct shell_surface *shsurf)
1747 shell_stack_fullscreen(shsurf);
1748 shell_configure_fullscreen(shsurf);
1752 shell_surface_set_fullscreen(struct wl_client *client,
1753 struct wl_resource *resource,
1756 struct wl_resource *output_resource)
1758 struct shell_surface *shsurf = resource->data;
1759 struct weston_surface *es = shsurf->surface;
1761 if (output_resource)
1762 shsurf->output = output_resource->data;
1763 else if (es->output)
1764 shsurf->output = es->output;
1766 shsurf->output = get_default_output(es->compositor);
1768 shsurf->fullscreen_output = shsurf->output;
1769 shsurf->fullscreen.type = method;
1770 shsurf->fullscreen.framerate = framerate;
1771 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1773 shsurf->client->send_configure(shsurf->surface, 0,
1774 shsurf->output->width,
1775 shsurf->output->height);
1779 popup_grab_focus(struct wl_pointer_grab *grab,
1780 struct wl_surface *surface,
1784 struct wl_pointer *pointer = grab->pointer;
1785 struct shell_surface *priv =
1786 container_of(grab, struct shell_surface, popup.grab);
1787 struct wl_client *client = priv->surface->surface.resource.client;
1789 if (surface && surface->resource.client == client) {
1790 wl_pointer_set_focus(pointer, surface, x, y);
1791 grab->focus = surface;
1793 wl_pointer_set_focus(pointer, NULL,
1794 wl_fixed_from_int(0),
1795 wl_fixed_from_int(0));
1801 popup_grab_motion(struct wl_pointer_grab *grab,
1802 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1804 struct wl_resource *resource;
1806 resource = grab->pointer->focus_resource;
1808 wl_pointer_send_motion(resource, time, sx, sy);
1812 popup_grab_button(struct wl_pointer_grab *grab,
1813 uint32_t time, uint32_t button, uint32_t state_w)
1815 struct wl_resource *resource;
1816 struct shell_surface *shsurf =
1817 container_of(grab, struct shell_surface, popup.grab);
1818 struct wl_display *display;
1819 enum wl_pointer_button_state state = state_w;
1822 resource = grab->pointer->focus_resource;
1824 display = wl_client_get_display(resource->client);
1825 serial = wl_display_get_serial(display);
1826 wl_pointer_send_button(resource, serial, time, button, state);
1827 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1828 (shsurf->popup.initial_up ||
1829 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1830 wl_shell_surface_send_popup_done(&shsurf->resource);
1831 wl_pointer_end_grab(grab->pointer);
1832 shsurf->popup.grab.pointer = NULL;
1835 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1836 shsurf->popup.initial_up = 1;
1839 static const struct wl_pointer_grab_interface popup_grab_interface = {
1846 shell_map_popup(struct shell_surface *shsurf)
1848 struct wl_seat *seat = shsurf->popup.seat;
1849 struct weston_surface *es = shsurf->surface;
1850 struct weston_surface *parent = shsurf->parent;
1852 es->output = parent->output;
1853 shsurf->popup.grab.interface = &popup_grab_interface;
1855 weston_surface_update_transform(parent);
1856 if (parent->transform.enabled) {
1857 shsurf->popup.parent_transform.matrix =
1858 parent->transform.matrix;
1860 /* construct x, y translation matrix */
1861 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1862 shsurf->popup.parent_transform.matrix.d[12] =
1864 shsurf->popup.parent_transform.matrix.d[13] =
1867 wl_list_insert(es->geometry.transformation_list.prev,
1868 &shsurf->popup.parent_transform.link);
1870 shsurf->popup.initial_up = 0;
1871 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1872 weston_surface_update_transform(es);
1874 /* We don't require the grab to still be active, but if another
1875 * grab has started in the meantime, we end the popup now. */
1876 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1877 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1879 wl_shell_surface_send_popup_done(&shsurf->resource);
1884 shell_surface_set_popup(struct wl_client *client,
1885 struct wl_resource *resource,
1886 struct wl_resource *seat_resource,
1888 struct wl_resource *parent_resource,
1889 int32_t x, int32_t y, uint32_t flags)
1891 struct shell_surface *shsurf = resource->data;
1893 shsurf->type = SHELL_SURFACE_POPUP;
1894 shsurf->parent = parent_resource->data;
1895 shsurf->popup.seat = seat_resource->data;
1896 shsurf->popup.serial = serial;
1897 shsurf->popup.x = x;
1898 shsurf->popup.y = y;
1901 static const struct wl_shell_surface_interface shell_surface_implementation = {
1904 shell_surface_resize,
1905 shell_surface_set_toplevel,
1906 shell_surface_set_transient,
1907 shell_surface_set_fullscreen,
1908 shell_surface_set_popup,
1909 shell_surface_set_maximized,
1910 shell_surface_set_title,
1911 shell_surface_set_class
1915 destroy_shell_surface(struct shell_surface *shsurf)
1917 if (shsurf->popup.grab.pointer)
1918 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1920 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1921 shell_surface_is_top_fullscreen(shsurf)) {
1922 weston_output_switch_mode(shsurf->fullscreen_output,
1923 shsurf->fullscreen_output->origin);
1926 if (shsurf->fullscreen.black_surface)
1927 weston_surface_destroy(shsurf->fullscreen.black_surface);
1929 /* As destroy_resource() use wl_list_for_each_safe(),
1930 * we can always remove the listener.
1932 wl_list_remove(&shsurf->surface_destroy_listener.link);
1933 shsurf->surface->configure = NULL;
1934 ping_timer_destroy(shsurf);
1936 wl_list_remove(&shsurf->link);
1941 shell_destroy_shell_surface(struct wl_resource *resource)
1943 struct shell_surface *shsurf = resource->data;
1945 destroy_shell_surface(shsurf);
1949 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1951 struct shell_surface *shsurf = container_of(listener,
1952 struct shell_surface,
1953 surface_destroy_listener);
1955 if (shsurf->resource.client) {
1956 wl_resource_destroy(&shsurf->resource);
1958 wl_signal_emit(&shsurf->resource.destroy_signal,
1960 destroy_shell_surface(shsurf);
1965 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1967 static struct shell_surface *
1968 get_shell_surface(struct weston_surface *surface)
1970 if (surface->configure == shell_surface_configure)
1971 return surface->private;
1976 static struct shell_surface *
1977 create_shell_surface(void *shell, struct weston_surface *surface,
1978 const struct weston_shell_client *client)
1980 struct shell_surface *shsurf;
1982 if (surface->configure) {
1983 weston_log("surface->configure already set\n");
1987 shsurf = calloc(1, sizeof *shsurf);
1989 weston_log("no memory to allocate shell surface\n");
1993 surface->configure = shell_surface_configure;
1994 surface->private = shsurf;
1996 shsurf->shell = (struct desktop_shell *) shell;
1997 shsurf->unresponsive = 0;
1998 shsurf->saved_position_valid = false;
1999 shsurf->saved_rotation_valid = false;
2000 shsurf->surface = surface;
2001 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2002 shsurf->fullscreen.framerate = 0;
2003 shsurf->fullscreen.black_surface = NULL;
2004 shsurf->ping_timer = NULL;
2005 wl_list_init(&shsurf->fullscreen.transform.link);
2007 wl_signal_init(&shsurf->resource.destroy_signal);
2008 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2009 wl_signal_add(&surface->surface.resource.destroy_signal,
2010 &shsurf->surface_destroy_listener);
2012 /* init link so its safe to always remove it in destroy_shell_surface */
2013 wl_list_init(&shsurf->link);
2015 /* empty when not in use */
2016 wl_list_init(&shsurf->rotation.transform.link);
2017 weston_matrix_init(&shsurf->rotation.rotation);
2019 wl_list_init(&shsurf->workspace_transform.link);
2021 shsurf->type = SHELL_SURFACE_NONE;
2022 shsurf->next_type = SHELL_SURFACE_NONE;
2024 shsurf->client = client;
2030 shell_get_shell_surface(struct wl_client *client,
2031 struct wl_resource *resource,
2033 struct wl_resource *surface_resource)
2035 struct weston_surface *surface = surface_resource->data;
2036 struct desktop_shell *shell = resource->data;
2037 struct shell_surface *shsurf;
2039 if (get_shell_surface(surface)) {
2040 wl_resource_post_error(surface_resource,
2041 WL_DISPLAY_ERROR_INVALID_OBJECT,
2042 "desktop_shell::get_shell_surface already requested");
2046 shsurf = create_shell_surface(shell, surface, &shell_client);
2048 wl_resource_post_error(surface_resource,
2049 WL_DISPLAY_ERROR_INVALID_OBJECT,
2050 "surface->configure already set");
2054 shsurf->resource.destroy = shell_destroy_shell_surface;
2055 shsurf->resource.object.id = id;
2056 shsurf->resource.object.interface = &wl_shell_surface_interface;
2057 shsurf->resource.object.implementation =
2058 (void (**)(void)) &shell_surface_implementation;
2059 shsurf->resource.data = shsurf;
2061 wl_client_add_resource(client, &shsurf->resource);
2064 static const struct wl_shell_interface shell_implementation = {
2065 shell_get_shell_surface
2069 handle_screensaver_sigchld(struct weston_process *proc, int status)
2075 launch_screensaver(struct desktop_shell *shell)
2077 if (shell->screensaver.binding)
2080 if (!shell->screensaver.path)
2083 if (shell->screensaver.process.pid != 0) {
2084 weston_log("old screensaver still running\n");
2088 weston_client_launch(shell->compositor,
2089 &shell->screensaver.process,
2090 shell->screensaver.path,
2091 handle_screensaver_sigchld);
2095 terminate_screensaver(struct desktop_shell *shell)
2097 if (shell->screensaver.process.pid == 0)
2100 kill(shell->screensaver.process.pid, SIGTERM);
2104 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
2106 struct weston_surface *s, *next;
2108 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2109 if (s->output == es->output && s != es) {
2110 weston_surface_unmap(s);
2111 s->configure = NULL;
2115 weston_surface_configure(es, es->output->x, es->output->y,
2116 es->buffer->width, es->buffer->height);
2118 if (wl_list_empty(&es->layer_link)) {
2119 wl_list_insert(&layer->surface_list, &es->layer_link);
2120 weston_compositor_schedule_repaint(es->compositor);
2125 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2127 struct desktop_shell *shell = es->private;
2129 configure_static_surface(es, &shell->background_layer);
2133 desktop_shell_set_background(struct wl_client *client,
2134 struct wl_resource *resource,
2135 struct wl_resource *output_resource,
2136 struct wl_resource *surface_resource)
2138 struct desktop_shell *shell = resource->data;
2139 struct weston_surface *surface = surface_resource->data;
2141 if (surface->configure) {
2142 wl_resource_post_error(surface_resource,
2143 WL_DISPLAY_ERROR_INVALID_OBJECT,
2144 "surface role already assigned");
2148 surface->configure = background_configure;
2149 surface->private = shell;
2150 surface->output = output_resource->data;
2151 desktop_shell_send_configure(resource, 0,
2153 surface->output->width,
2154 surface->output->height);
2158 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2160 struct desktop_shell *shell = es->private;
2162 configure_static_surface(es, &shell->panel_layer);
2166 desktop_shell_set_panel(struct wl_client *client,
2167 struct wl_resource *resource,
2168 struct wl_resource *output_resource,
2169 struct wl_resource *surface_resource)
2171 struct desktop_shell *shell = resource->data;
2172 struct weston_surface *surface = surface_resource->data;
2174 if (surface->configure) {
2175 wl_resource_post_error(surface_resource,
2176 WL_DISPLAY_ERROR_INVALID_OBJECT,
2177 "surface role already assigned");
2181 surface->configure = panel_configure;
2182 surface->private = shell;
2183 surface->output = output_resource->data;
2184 desktop_shell_send_configure(resource, 0,
2186 surface->output->width,
2187 surface->output->height);
2191 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2193 struct desktop_shell *shell = surface->private;
2195 center_on_output(surface, get_default_output(shell->compositor));
2197 if (!weston_surface_is_mapped(surface)) {
2198 wl_list_insert(&shell->lock_layer.surface_list,
2199 &surface->layer_link);
2200 weston_surface_update_transform(surface);
2201 weston_compositor_wake(shell->compositor);
2206 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2208 struct desktop_shell *shell =
2209 container_of(listener, struct desktop_shell, lock_surface_listener);
2211 weston_log("lock surface gone\n");
2212 shell->lock_surface = NULL;
2216 desktop_shell_set_lock_surface(struct wl_client *client,
2217 struct wl_resource *resource,
2218 struct wl_resource *surface_resource)
2220 struct desktop_shell *shell = resource->data;
2221 struct weston_surface *surface = surface_resource->data;
2223 shell->prepare_event_sent = false;
2228 shell->lock_surface = surface;
2230 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2231 wl_signal_add(&surface_resource->destroy_signal,
2232 &shell->lock_surface_listener);
2234 surface->configure = lock_surface_configure;
2235 surface->private = shell;
2239 resume_desktop(struct desktop_shell *shell)
2241 struct weston_surface *surface;
2242 struct workspace *ws = get_current_workspace(shell);
2244 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2245 weston_surface_unmap(surface);
2247 terminate_screensaver(shell);
2249 wl_list_remove(&shell->lock_layer.link);
2250 wl_list_insert(&shell->compositor->cursor_layer.link,
2251 &shell->fullscreen_layer.link);
2252 wl_list_insert(&shell->fullscreen_layer.link,
2253 &shell->panel_layer.link);
2254 if (shell->showing_input_panels) {
2255 wl_list_insert(&shell->panel_layer.link,
2256 &shell->input_panel_layer.link);
2257 wl_list_insert(&shell->input_panel_layer.link,
2260 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2263 restore_focus_state(shell, get_current_workspace(shell));
2265 shell->locked = false;
2266 shell->compositor->idle_time = shell->compositor->option_idle_time;
2267 weston_compositor_wake(shell->compositor);
2268 weston_compositor_damage_all(shell->compositor);
2272 desktop_shell_unlock(struct wl_client *client,
2273 struct wl_resource *resource)
2275 struct desktop_shell *shell = resource->data;
2277 shell->prepare_event_sent = false;
2280 resume_desktop(shell);
2284 desktop_shell_set_grab_surface(struct wl_client *client,
2285 struct wl_resource *resource,
2286 struct wl_resource *surface_resource)
2288 struct desktop_shell *shell = resource->data;
2290 shell->grab_surface = surface_resource->data;
2293 static const struct desktop_shell_interface desktop_shell_implementation = {
2294 desktop_shell_set_background,
2295 desktop_shell_set_panel,
2296 desktop_shell_set_lock_surface,
2297 desktop_shell_unlock,
2298 desktop_shell_set_grab_surface
2301 static enum shell_surface_type
2302 get_shell_surface_type(struct weston_surface *surface)
2304 struct shell_surface *shsurf;
2306 shsurf = get_shell_surface(surface);
2308 return SHELL_SURFACE_NONE;
2309 return shsurf->type;
2313 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2315 struct weston_surface *surface =
2316 (struct weston_surface *) seat->pointer->focus;
2317 struct shell_surface *shsurf;
2319 if (surface == NULL)
2322 shsurf = get_shell_surface(surface);
2323 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2326 surface_move(shsurf, (struct weston_seat *) seat);
2330 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2332 struct weston_surface *surface =
2333 (struct weston_surface *) seat->pointer->focus;
2336 struct shell_surface *shsurf;
2338 if (surface == NULL)
2341 shsurf = get_shell_surface(surface);
2342 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2345 weston_surface_from_global(surface,
2346 wl_fixed_to_int(seat->pointer->grab_x),
2347 wl_fixed_to_int(seat->pointer->grab_y),
2350 if (x < surface->geometry.width / 3)
2351 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2352 else if (x < 2 * surface->geometry.width / 3)
2355 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2357 if (y < surface->geometry.height / 3)
2358 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2359 else if (y < 2 * surface->geometry.height / 3)
2362 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2364 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2368 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2369 wl_fixed_t value, void *data)
2372 struct shell_surface *shsurf;
2373 struct weston_surface *surface =
2374 (struct weston_surface *) seat->pointer->focus;
2376 if (surface == NULL)
2379 shsurf = get_shell_surface(surface);
2383 surface->alpha -= wl_fixed_to_double(value) * step;
2385 if (surface->alpha > 1.0)
2386 surface->alpha = 1.0;
2387 if (surface->alpha < step)
2388 surface->alpha = step;
2390 surface->geometry.dirty = 1;
2391 weston_surface_damage(surface);
2395 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2398 struct weston_seat *ws = (struct weston_seat *) seat;
2399 struct weston_compositor *compositor = ws->compositor;
2400 struct weston_output *output;
2403 wl_list_for_each(output, &compositor->output_list, link) {
2404 if (pixman_region32_contains_point(&output->region,
2405 wl_fixed_to_double(seat->pointer->x),
2406 wl_fixed_to_double(seat->pointer->y),
2408 if (key == KEY_PAGEUP)
2409 increment = output->zoom.increment;
2410 else if (key == KEY_PAGEDOWN)
2411 increment = -output->zoom.increment;
2412 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2413 /* For every pixel zoom 20th of a step */
2414 increment = output->zoom.increment *
2415 -wl_fixed_to_double(value) / 20.0;
2419 output->zoom.level += increment;
2421 if (output->zoom.level < 0.0)
2422 output->zoom.level = 0.0;
2423 else if (output->zoom.level > output->zoom.max_level)
2424 output->zoom.level = output->zoom.max_level;
2426 output->zoom.active = 1;
2427 output->disable_planes++;
2430 output->zoom.spring_z.target = output->zoom.level;
2432 weston_output_update_zoom(output, output->zoom.type);
2438 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2439 wl_fixed_t value, void *data)
2441 do_zoom(seat, time, 0, axis, value);
2445 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2448 do_zoom(seat, time, key, 0, 0);
2452 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2455 struct weston_compositor *compositor = data;
2457 wl_display_terminate(compositor->wl_display);
2461 rotate_grab_motion(struct wl_pointer_grab *grab,
2462 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2464 struct rotate_grab *rotate =
2465 container_of(grab, struct rotate_grab, base.grab);
2466 struct wl_pointer *pointer = grab->pointer;
2467 struct shell_surface *shsurf = rotate->base.shsurf;
2468 struct weston_surface *surface;
2469 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2474 surface = shsurf->surface;
2476 cx = 0.5f * surface->geometry.width;
2477 cy = 0.5f * surface->geometry.height;
2479 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2480 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2481 r = sqrtf(dx * dx + dy * dy);
2483 wl_list_remove(&shsurf->rotation.transform.link);
2484 shsurf->surface->geometry.dirty = 1;
2487 struct weston_matrix *matrix =
2488 &shsurf->rotation.transform.matrix;
2490 weston_matrix_init(&rotate->rotation);
2491 rotate->rotation.d[0] = dx / r;
2492 rotate->rotation.d[4] = -dy / r;
2493 rotate->rotation.d[1] = -rotate->rotation.d[4];
2494 rotate->rotation.d[5] = rotate->rotation.d[0];
2496 weston_matrix_init(matrix);
2497 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2498 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2499 weston_matrix_multiply(matrix, &rotate->rotation);
2500 weston_matrix_translate(matrix, cx, cy, 0.0f);
2503 &shsurf->surface->geometry.transformation_list,
2504 &shsurf->rotation.transform.link);
2506 wl_list_init(&shsurf->rotation.transform.link);
2507 weston_matrix_init(&shsurf->rotation.rotation);
2508 weston_matrix_init(&rotate->rotation);
2511 /* We need to adjust the position of the surface
2512 * in case it was resized in a rotated state before */
2513 cposx = surface->geometry.x + cx;
2514 cposy = surface->geometry.y + cy;
2515 dposx = rotate->center.x - cposx;
2516 dposy = rotate->center.y - cposy;
2517 if (dposx != 0.0f || dposy != 0.0f) {
2518 weston_surface_set_position(surface,
2519 surface->geometry.x + dposx,
2520 surface->geometry.y + dposy);
2523 /* Repaint implies weston_surface_update_transform(), which
2524 * lazily applies the damage due to rotation update.
2526 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2530 rotate_grab_button(struct wl_pointer_grab *grab,
2531 uint32_t time, uint32_t button, uint32_t state_w)
2533 struct rotate_grab *rotate =
2534 container_of(grab, struct rotate_grab, base.grab);
2535 struct wl_pointer *pointer = grab->pointer;
2536 struct shell_surface *shsurf = rotate->base.shsurf;
2537 enum wl_pointer_button_state state = state_w;
2539 if (pointer->button_count == 0 &&
2540 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2542 weston_matrix_multiply(&shsurf->rotation.rotation,
2544 shell_grab_end(&rotate->base);
2549 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2556 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2559 struct weston_surface *base_surface =
2560 (struct weston_surface *) seat->pointer->focus;
2561 struct shell_surface *surface;
2562 struct rotate_grab *rotate;
2566 if (base_surface == NULL)
2569 surface = get_shell_surface(base_surface);
2570 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2573 rotate = malloc(sizeof *rotate);
2577 weston_surface_to_global_float(surface->surface,
2578 surface->surface->geometry.width / 2,
2579 surface->surface->geometry.height / 2,
2580 &rotate->center.x, &rotate->center.y);
2582 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2583 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2584 r = sqrtf(dx * dx + dy * dy);
2586 struct weston_matrix inverse;
2588 weston_matrix_init(&inverse);
2589 inverse.d[0] = dx / r;
2590 inverse.d[4] = dy / r;
2591 inverse.d[1] = -inverse.d[4];
2592 inverse.d[5] = inverse.d[0];
2593 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2595 weston_matrix_init(&rotate->rotation);
2596 rotate->rotation.d[0] = dx / r;
2597 rotate->rotation.d[4] = -dy / r;
2598 rotate->rotation.d[1] = -rotate->rotation.d[4];
2599 rotate->rotation.d[5] = rotate->rotation.d[0];
2601 weston_matrix_init(&surface->rotation.rotation);
2602 weston_matrix_init(&rotate->rotation);
2605 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2606 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2610 lower_fullscreen_layer(struct desktop_shell *shell)
2612 struct workspace *ws;
2613 struct weston_surface *surface, *prev;
2615 ws = get_current_workspace(shell);
2616 wl_list_for_each_reverse_safe(surface, prev,
2617 &shell->fullscreen_layer.surface_list,
2619 weston_surface_restack(surface, &ws->layer.surface_list);
2623 activate(struct desktop_shell *shell, struct weston_surface *es,
2624 struct weston_seat *seat)
2626 struct focus_state *state;
2627 struct workspace *ws;
2629 weston_surface_activate(es, seat);
2631 state = ensure_focus_state(shell, seat);
2635 state->keyboard_focus = es;
2636 wl_list_remove(&state->surface_destroy_listener.link);
2637 wl_signal_add(&es->surface.resource.destroy_signal,
2638 &state->surface_destroy_listener);
2640 switch (get_shell_surface_type(es)) {
2641 case SHELL_SURFACE_FULLSCREEN:
2642 /* should on top of panels */
2643 shell_stack_fullscreen(get_shell_surface(es));
2644 shell_configure_fullscreen(get_shell_surface(es));
2647 ws = get_current_workspace(shell);
2648 lower_fullscreen_layer(shell);
2649 weston_surface_restack(es, &ws->layer.surface_list);
2654 /* no-op func for checking black surface */
2656 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2661 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2663 if (es->configure == black_surface_configure) {
2665 *fs_surface = (struct weston_surface *)es->private;
2672 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2675 struct weston_seat *ws = (struct weston_seat *) seat;
2676 struct desktop_shell *shell = data;
2677 struct weston_surface *focus;
2678 struct weston_surface *upper;
2680 focus = (struct weston_surface *) seat->pointer->focus;
2684 if (is_black_surface(focus, &upper))
2687 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2690 if (seat->pointer->grab == &seat->pointer->default_grab)
2691 activate(shell, focus, ws);
2695 lock(struct wl_listener *listener, void *data)
2697 struct desktop_shell *shell =
2698 container_of(listener, struct desktop_shell, lock_listener);
2699 struct weston_output *output;
2700 struct workspace *ws = get_current_workspace(shell);
2702 if (shell->locked) {
2703 wl_list_for_each(output, &shell->compositor->output_list, link)
2704 /* TODO: find a way to jump to other DPMS levels */
2705 if (output->set_dpms)
2706 output->set_dpms(output, WESTON_DPMS_STANDBY);
2710 shell->locked = true;
2712 /* Hide all surfaces by removing the fullscreen, panel and
2713 * toplevel layers. This way nothing else can show or receive
2714 * input events while we are locked. */
2716 wl_list_remove(&shell->panel_layer.link);
2717 wl_list_remove(&shell->fullscreen_layer.link);
2718 if (shell->showing_input_panels)
2719 wl_list_remove(&shell->input_panel_layer.link);
2720 wl_list_remove(&ws->layer.link);
2721 wl_list_insert(&shell->compositor->cursor_layer.link,
2722 &shell->lock_layer.link);
2724 launch_screensaver(shell);
2726 /* TODO: disable bindings that should not work while locked. */
2728 /* All this must be undone in resume_desktop(). */
2732 unlock(struct wl_listener *listener, void *data)
2734 struct desktop_shell *shell =
2735 container_of(listener, struct desktop_shell, unlock_listener);
2737 if (!shell->locked || shell->lock_surface) {
2738 weston_compositor_wake(shell->compositor);
2742 /* If desktop-shell client has gone away, unlock immediately. */
2743 if (!shell->child.desktop_shell) {
2744 resume_desktop(shell);
2748 if (shell->prepare_event_sent)
2751 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2752 shell->prepare_event_sent = true;
2756 show_input_panels(struct wl_listener *listener, void *data)
2758 struct desktop_shell *shell =
2759 container_of(listener, struct desktop_shell,
2760 show_input_panel_listener);
2761 struct input_panel_surface *surface, *next;
2762 struct weston_surface *ws;
2764 shell->showing_input_panels = true;
2766 wl_list_insert(&shell->panel_layer.link,
2767 &shell->input_panel_layer.link);
2769 wl_list_for_each_safe(surface, next,
2770 &shell->input_panel.surfaces, link) {
2771 ws = surface->surface;
2772 wl_list_insert(&shell->input_panel_layer.surface_list,
2774 ws->geometry.dirty = 1;
2775 weston_surface_update_transform(ws);
2776 weston_surface_damage(ws);
2777 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2782 hide_input_panels(struct wl_listener *listener, void *data)
2784 struct desktop_shell *shell =
2785 container_of(listener, struct desktop_shell,
2786 hide_input_panel_listener);
2787 struct weston_surface *surface, *next;
2789 shell->showing_input_panels = false;
2791 wl_list_remove(&shell->input_panel_layer.link);
2793 wl_list_for_each_safe(surface, next,
2794 &shell->input_panel_layer.surface_list, layer_link)
2795 weston_surface_unmap(surface);
2799 center_on_output(struct weston_surface *surface, struct weston_output *output)
2801 float x = (output->width - surface->buffer->width) / 2;
2802 float y = (output->height - surface->buffer->height) / 2;
2804 weston_surface_configure(surface, output->x + x, output->y + y,
2805 surface->buffer->width,
2806 surface->buffer->height);
2810 weston_surface_set_initial_position (struct weston_surface *surface,
2811 struct desktop_shell *shell)
2813 struct weston_compositor *compositor = shell->compositor;
2815 int range_x, range_y;
2816 int dx, dy, x, y, panel_height;
2817 struct weston_output *output, *target_output = NULL;
2818 struct weston_seat *seat;
2820 /* As a heuristic place the new window on the same output as the
2821 * pointer. Falling back to the output containing 0, 0.
2823 * TODO: Do something clever for touch too?
2825 wl_list_for_each(seat, &compositor->seat_list, link) {
2826 if (seat->has_pointer) {
2827 ix = wl_fixed_to_int(seat->pointer.x);
2828 iy = wl_fixed_to_int(seat->pointer.y);
2833 wl_list_for_each(output, &compositor->output_list, link) {
2834 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2835 target_output = output;
2840 if (!target_output) {
2841 weston_surface_set_position(surface, 10 + random() % 400,
2842 10 + random() % 400);
2846 /* Valid range within output where the surface will still be onscreen.
2847 * If this is negative it means that the surface is bigger than
2850 panel_height = get_output_panel_height(shell, target_output);
2851 range_x = target_output->width - surface->geometry.width;
2852 range_y = (target_output->height - panel_height) -
2853 surface->geometry.height;
2856 dx = random() % range_x;
2861 dy = panel_height + random() % range_y;
2865 x = target_output->x + dx;
2866 y = target_output->y + dy;
2868 weston_surface_set_position (surface, x, y);
2872 map(struct desktop_shell *shell, struct weston_surface *surface,
2873 int32_t width, int32_t height, int32_t sx, int32_t sy)
2875 struct weston_compositor *compositor = shell->compositor;
2876 struct shell_surface *shsurf = get_shell_surface(surface);
2877 enum shell_surface_type surface_type = shsurf->type;
2878 struct weston_surface *parent;
2879 struct weston_seat *seat;
2880 struct workspace *ws;
2881 int panel_height = 0;
2883 surface->geometry.width = width;
2884 surface->geometry.height = height;
2885 surface->geometry.dirty = 1;
2887 /* initial positioning, see also configure() */
2888 switch (surface_type) {
2889 case SHELL_SURFACE_TOPLEVEL:
2890 weston_surface_set_initial_position(surface, shell);
2892 case SHELL_SURFACE_FULLSCREEN:
2893 center_on_output(surface, shsurf->fullscreen_output);
2894 shell_map_fullscreen(shsurf);
2896 case SHELL_SURFACE_MAXIMIZED:
2897 /* use surface configure to set the geometry */
2898 panel_height = get_output_panel_height(shell,surface->output);
2899 weston_surface_set_position(surface, shsurf->output->x,
2900 shsurf->output->y + panel_height);
2902 case SHELL_SURFACE_POPUP:
2903 shell_map_popup(shsurf);
2904 case SHELL_SURFACE_NONE:
2905 weston_surface_set_position(surface,
2906 surface->geometry.x + sx,
2907 surface->geometry.y + sy);
2913 /* surface stacking order, see also activate() */
2914 switch (surface_type) {
2915 case SHELL_SURFACE_POPUP:
2916 case SHELL_SURFACE_TRANSIENT:
2917 parent = shsurf->parent;
2918 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2920 case SHELL_SURFACE_FULLSCREEN:
2921 case SHELL_SURFACE_NONE:
2924 ws = get_current_workspace(shell);
2925 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2929 if (surface_type != SHELL_SURFACE_NONE) {
2930 weston_surface_update_transform(surface);
2931 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2932 surface->output = shsurf->output;
2935 switch (surface_type) {
2936 case SHELL_SURFACE_TRANSIENT:
2937 if (shsurf->transient.flags ==
2938 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2940 case SHELL_SURFACE_TOPLEVEL:
2941 case SHELL_SURFACE_FULLSCREEN:
2942 case SHELL_SURFACE_MAXIMIZED:
2943 if (!shell->locked) {
2944 wl_list_for_each(seat, &compositor->seat_list, link)
2945 activate(shell, surface, seat);
2952 if (surface_type == SHELL_SURFACE_TOPLEVEL)
2954 switch (shell->win_animation_type) {
2955 case ANIMATION_FADE:
2956 weston_fade_run(surface, NULL, NULL);
2958 case ANIMATION_ZOOM:
2959 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2968 configure(struct desktop_shell *shell, struct weston_surface *surface,
2969 float x, float y, int32_t width, int32_t height)
2971 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2972 struct shell_surface *shsurf;
2974 shsurf = get_shell_surface(surface);
2976 surface_type = shsurf->type;
2978 surface->geometry.x = x;
2979 surface->geometry.y = y;
2980 surface->geometry.width = width;
2981 surface->geometry.height = height;
2982 surface->geometry.dirty = 1;
2984 switch (surface_type) {
2985 case SHELL_SURFACE_FULLSCREEN:
2986 shell_stack_fullscreen(shsurf);
2987 shell_configure_fullscreen(shsurf);
2989 case SHELL_SURFACE_MAXIMIZED:
2990 /* setting x, y and using configure to change that geometry */
2991 surface->geometry.x = surface->output->x;
2992 surface->geometry.y = surface->output->y +
2993 get_output_panel_height(shell,surface->output);
2995 case SHELL_SURFACE_TOPLEVEL:
3001 /* XXX: would a fullscreen surface need the same handling? */
3002 if (surface->output) {
3003 weston_surface_update_transform(surface);
3005 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3006 surface->output = shsurf->output;
3011 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3013 struct shell_surface *shsurf = get_shell_surface(es);
3014 struct desktop_shell *shell = shsurf->shell;
3015 int type_changed = 0;
3017 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3018 shsurf->type != shsurf->next_type) {
3019 set_surface_type(shsurf);
3023 if (!weston_surface_is_mapped(es)) {
3024 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
3025 } else if (type_changed || sx != 0 || sy != 0 ||
3026 es->geometry.width != es->buffer->width ||
3027 es->geometry.height != es->buffer->height) {
3028 float from_x, from_y;
3031 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3032 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3033 configure(shell, es,
3034 es->geometry.x + to_x - from_x,
3035 es->geometry.y + to_y - from_y,
3036 es->buffer->width, es->buffer->height);
3040 static void launch_desktop_shell_process(void *data);
3043 desktop_shell_sigchld(struct weston_process *process, int status)
3046 struct desktop_shell *shell =
3047 container_of(process, struct desktop_shell, child.process);
3049 shell->child.process.pid = 0;
3050 shell->child.client = NULL; /* already destroyed by wayland */
3052 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3053 time = weston_compositor_get_time();
3054 if (time - shell->child.deathstamp > 30000) {
3055 shell->child.deathstamp = time;
3056 shell->child.deathcount = 0;
3059 shell->child.deathcount++;
3060 if (shell->child.deathcount > 5) {
3061 weston_log("weston-desktop-shell died, giving up.\n");
3065 weston_log("weston-desktop-shell died, respawning...\n");
3066 launch_desktop_shell_process(shell);
3070 launch_desktop_shell_process(void *data)
3072 struct desktop_shell *shell = data;
3073 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3075 shell->child.client = weston_client_launch(shell->compositor,
3076 &shell->child.process,
3078 desktop_shell_sigchld);
3080 if (!shell->child.client)
3081 weston_log("not able to start %s\n", shell_exe);
3085 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3087 struct desktop_shell *shell = data;
3089 wl_client_add_object(client, &wl_shell_interface,
3090 &shell_implementation, id, shell);
3094 unbind_desktop_shell(struct wl_resource *resource)
3096 struct desktop_shell *shell = resource->data;
3099 resume_desktop(shell);
3101 shell->child.desktop_shell = NULL;
3102 shell->prepare_event_sent = false;
3107 bind_desktop_shell(struct wl_client *client,
3108 void *data, uint32_t version, uint32_t id)
3110 struct desktop_shell *shell = data;
3111 struct wl_resource *resource;
3113 resource = wl_client_add_object(client, &desktop_shell_interface,
3114 &desktop_shell_implementation,
3117 if (client == shell->child.client) {
3118 resource->destroy = unbind_desktop_shell;
3119 shell->child.desktop_shell = resource;
3123 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3124 "permission to bind desktop_shell denied");
3125 wl_resource_destroy(resource);
3129 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3131 struct desktop_shell *shell = surface->private;
3136 center_on_output(surface, surface->output);
3138 if (wl_list_empty(&surface->layer_link)) {
3139 wl_list_insert(shell->lock_layer.surface_list.prev,
3140 &surface->layer_link);
3141 weston_surface_update_transform(surface);
3142 shell->compositor->idle_time = shell->screensaver.duration;
3143 weston_compositor_wake(shell->compositor);
3144 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3149 screensaver_set_surface(struct wl_client *client,
3150 struct wl_resource *resource,
3151 struct wl_resource *surface_resource,
3152 struct wl_resource *output_resource)
3154 struct desktop_shell *shell = resource->data;
3155 struct weston_surface *surface = surface_resource->data;
3156 struct weston_output *output = output_resource->data;
3158 surface->configure = screensaver_configure;
3159 surface->private = shell;
3160 surface->output = output;
3163 static const struct screensaver_interface screensaver_implementation = {
3164 screensaver_set_surface
3168 unbind_screensaver(struct wl_resource *resource)
3170 struct desktop_shell *shell = resource->data;
3172 shell->screensaver.binding = NULL;
3177 bind_screensaver(struct wl_client *client,
3178 void *data, uint32_t version, uint32_t id)
3180 struct desktop_shell *shell = data;
3181 struct wl_resource *resource;
3183 resource = wl_client_add_object(client, &screensaver_interface,
3184 &screensaver_implementation,
3187 if (shell->screensaver.binding == NULL) {
3188 resource->destroy = unbind_screensaver;
3189 shell->screensaver.binding = resource;
3193 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3194 "interface object already bound");
3195 wl_resource_destroy(resource);
3199 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3201 struct weston_mode *mode = surface->output->current;
3202 float x = (mode->width - surface->buffer->width) / 2;
3203 float y = mode->height - surface->buffer->height;
3205 /* Don't map the input panel here, wait for
3206 * show_input_panels signal. */
3208 weston_surface_configure(surface,
3209 surface->output->x + x,
3210 surface->output->y + y,
3211 surface->buffer->width,
3212 surface->buffer->height);
3216 destroy_input_panel_surface(struct wl_listener *listener,
3219 struct input_panel_surface *input_panel_surface =
3220 container_of(listener, struct input_panel_surface, listener);
3222 wl_list_remove(&listener->link);
3223 wl_list_remove(&input_panel_surface->link);
3225 free(input_panel_surface);
3229 input_panel_set_surface(struct wl_client *client,
3230 struct wl_resource *resource,
3231 struct wl_resource *surface_resource,
3232 struct wl_resource *output_resource)
3234 struct desktop_shell *shell = resource->data;
3235 struct weston_surface *surface = surface_resource->data;
3236 struct weston_output *output = output_resource->data;
3237 struct input_panel_surface *input_panel_surface;
3239 surface->configure = input_panel_configure;
3240 surface->private = shell;
3241 surface->output = output;
3243 input_panel_surface = malloc(sizeof *input_panel_surface);
3244 if (!input_panel_surface) {
3245 wl_resource_post_no_memory(resource);
3249 input_panel_surface->surface = surface;
3250 input_panel_surface->listener.notify = destroy_input_panel_surface;
3252 wl_signal_add(&surface_resource->destroy_signal,
3253 &input_panel_surface->listener);
3255 wl_list_insert(&shell->input_panel.surfaces,
3256 &input_panel_surface->link);
3259 static const struct input_panel_interface input_panel_implementation = {
3260 input_panel_set_surface
3264 unbind_input_panel(struct wl_resource *resource)
3266 struct desktop_shell *shell = resource->data;
3268 shell->input_panel.binding = NULL;
3273 bind_input_panel(struct wl_client *client,
3274 void *data, uint32_t version, uint32_t id)
3276 struct desktop_shell *shell = data;
3277 struct wl_resource *resource;
3279 resource = wl_client_add_object(client, &input_panel_interface,
3280 &input_panel_implementation,
3283 if (shell->input_panel.binding == NULL) {
3284 resource->destroy = unbind_input_panel;
3285 shell->input_panel.binding = resource;
3289 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3290 "interface object already bound");
3291 wl_resource_destroy(resource);
3295 struct desktop_shell *shell;
3296 struct weston_surface *current;
3297 struct wl_listener listener;
3298 struct wl_keyboard_grab grab;
3302 switcher_next(struct switcher *switcher)
3304 struct weston_surface *surface;
3305 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3306 struct shell_surface *shsurf;
3307 struct workspace *ws = get_current_workspace(switcher->shell);
3309 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3310 switch (get_shell_surface_type(surface)) {
3311 case SHELL_SURFACE_TOPLEVEL:
3312 case SHELL_SURFACE_FULLSCREEN:
3313 case SHELL_SURFACE_MAXIMIZED:
3316 if (prev == switcher->current)
3319 surface->alpha = 0.25;
3320 surface->geometry.dirty = 1;
3321 weston_surface_damage(surface);
3327 if (is_black_surface(surface, NULL)) {
3328 surface->alpha = 0.25;
3329 surface->geometry.dirty = 1;
3330 weston_surface_damage(surface);
3340 wl_list_remove(&switcher->listener.link);
3341 wl_signal_add(&next->surface.resource.destroy_signal,
3342 &switcher->listener);
3344 switcher->current = next;
3347 shsurf = get_shell_surface(switcher->current);
3348 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3349 shsurf->fullscreen.black_surface->alpha = 1.0;
3353 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3355 struct switcher *switcher =
3356 container_of(listener, struct switcher, listener);
3358 switcher_next(switcher);
3362 switcher_destroy(struct switcher *switcher)
3364 struct weston_surface *surface;
3365 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3366 struct workspace *ws = get_current_workspace(switcher->shell);
3368 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3369 surface->alpha = 1.0;
3370 weston_surface_damage(surface);
3373 if (switcher->current)
3374 activate(switcher->shell, switcher->current,
3375 (struct weston_seat *) keyboard->seat);
3376 wl_list_remove(&switcher->listener.link);
3377 wl_keyboard_end_grab(keyboard);
3382 switcher_key(struct wl_keyboard_grab *grab,
3383 uint32_t time, uint32_t key, uint32_t state_w)
3385 struct switcher *switcher = container_of(grab, struct switcher, grab);
3386 enum wl_keyboard_key_state state = state_w;
3388 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3389 switcher_next(switcher);
3393 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3394 uint32_t mods_depressed, uint32_t mods_latched,
3395 uint32_t mods_locked, uint32_t group)
3397 struct switcher *switcher = container_of(grab, struct switcher, grab);
3398 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3400 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3401 switcher_destroy(switcher);
3404 static const struct wl_keyboard_grab_interface switcher_grab = {
3410 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3413 struct desktop_shell *shell = data;
3414 struct switcher *switcher;
3416 switcher = malloc(sizeof *switcher);
3417 switcher->shell = shell;
3418 switcher->current = NULL;
3419 switcher->listener.notify = switcher_handle_surface_destroy;
3420 wl_list_init(&switcher->listener.link);
3422 lower_fullscreen_layer(switcher->shell);
3423 switcher->grab.interface = &switcher_grab;
3424 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3425 wl_keyboard_set_focus(seat->keyboard, NULL);
3426 switcher_next(switcher);
3430 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3433 struct weston_compositor *compositor = data;
3434 struct weston_output *output;
3435 long backlight_new = 0;
3437 /* TODO: we're limiting to simple use cases, where we assume just
3438 * control on the primary display. We'd have to extend later if we
3439 * ever get support for setting backlights on random desktop LCD
3441 output = get_default_output(compositor);
3445 if (!output->set_backlight)
3448 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3449 backlight_new = output->backlight_current - 25;
3450 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3451 backlight_new = output->backlight_current + 25;
3453 if (backlight_new < 5)
3455 if (backlight_new > 255)
3456 backlight_new = 255;
3458 output->backlight_current = backlight_new;
3459 output->set_backlight(output, output->backlight_current);
3463 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3466 struct desktop_shell *shell = data;
3467 struct weston_compositor *compositor = shell->compositor;
3468 struct weston_surface *surface;
3469 struct weston_plane plane;
3471 if (shell->debug_repaint_surface) {
3472 weston_surface_destroy(shell->debug_repaint_surface);
3473 shell->debug_repaint_surface = NULL;
3475 surface = weston_surface_create(compositor);
3476 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3477 weston_surface_configure(surface, 0, 0, 8192, 8192);
3478 wl_list_insert(&compositor->fade_layer.surface_list,
3479 &surface->layer_link);
3480 pixman_region32_fini(&surface->input);
3481 pixman_region32_init(&surface->input);
3483 /* Here's the dirty little trick that makes the
3484 * repaint debugging work: we move the surface to a
3485 * different plane and force an update_transform to
3486 * update dependent state and clear the
3487 * geometry.dirty bit. This way the call to
3488 * damage_below() in update_transform() does not
3489 * add damage to the primary plane. */
3491 weston_plane_init(&plane, 0, 0);
3492 surface->plane = &plane;
3493 weston_surface_update_transform(surface);
3494 shell->debug_repaint_surface = surface;
3495 surface->plane = &compositor->primary_plane;
3496 weston_plane_release(&plane);
3502 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3505 struct desktop_shell *shell = data;
3506 struct weston_compositor *compositor = shell->compositor;
3507 compositor->fan_debug = !compositor->fan_debug;
3508 weston_compositor_damage_all(compositor);
3512 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3515 struct wl_surface *focus_surface;
3516 struct wl_client *client;
3517 struct desktop_shell *shell = data;
3518 struct weston_compositor *compositor = shell->compositor;
3521 focus_surface = seat->keyboard->focus;
3525 wl_signal_emit(&compositor->kill_signal, focus_surface);
3527 client = focus_surface->resource.client;
3528 wl_client_get_credentials(client, &pid, NULL, NULL);
3530 /* Skip clients that we launched ourselves (the credentials of
3531 * the socketpair is ours) */
3532 if (pid == getpid())
3539 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3540 uint32_t key, void *data)
3542 struct desktop_shell *shell = data;
3543 unsigned int new_index = shell->workspaces.current;
3550 change_workspace(shell, new_index);
3554 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3555 uint32_t key, void *data)
3557 struct desktop_shell *shell = data;
3558 unsigned int new_index = shell->workspaces.current;
3562 if (new_index < shell->workspaces.num - 1)
3565 change_workspace(shell, new_index);
3569 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3570 uint32_t key, void *data)
3572 struct desktop_shell *shell = data;
3573 unsigned int new_index;
3577 new_index = key - KEY_F1;
3578 if (new_index >= shell->workspaces.num)
3579 new_index = shell->workspaces.num - 1;
3581 change_workspace(shell, new_index);
3585 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3586 uint32_t key, void *data)
3588 struct desktop_shell *shell = data;
3589 unsigned int new_index = shell->workspaces.current;
3597 take_surface_to_workspace_by_seat(shell, seat, new_index);
3601 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3602 uint32_t key, void *data)
3604 struct desktop_shell *shell = data;
3605 unsigned int new_index = shell->workspaces.current;
3610 if (new_index < shell->workspaces.num - 1)
3613 take_surface_to_workspace_by_seat(shell, seat, new_index);
3617 shell_destroy(struct wl_listener *listener, void *data)
3619 struct desktop_shell *shell =
3620 container_of(listener, struct desktop_shell, destroy_listener);
3621 struct workspace **ws;
3623 if (shell->child.client)
3624 wl_client_destroy(shell->child.client);
3626 wl_list_remove(&shell->lock_listener.link);
3627 wl_list_remove(&shell->unlock_listener.link);
3628 wl_list_remove(&shell->show_input_panel_listener.link);
3629 wl_list_remove(&shell->hide_input_panel_listener.link);
3631 wl_array_for_each(ws, &shell->workspaces.array)
3632 workspace_destroy(*ws);
3633 wl_array_release(&shell->workspaces.array);
3635 free(shell->screensaver.path);
3640 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3643 int i, num_workspace_bindings;
3645 /* fixed bindings */
3646 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3647 MODIFIER_CTRL | MODIFIER_ALT,
3648 terminate_binding, ec);
3649 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3650 click_to_activate_binding,
3652 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3653 MODIFIER_SUPER | MODIFIER_ALT,
3654 surface_opacity_binding, NULL);
3655 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3656 MODIFIER_SUPER, zoom_axis_binding,
3659 /* configurable bindings */
3660 mod = shell->binding_modifier;
3661 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3662 zoom_key_binding, NULL);
3663 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3664 zoom_key_binding, NULL);
3665 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3667 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3668 resize_binding, shell);
3669 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3670 rotate_binding, NULL);
3671 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3673 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3675 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3676 backlight_binding, ec);
3677 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3679 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3680 backlight_binding, ec);
3681 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3682 debug_repaint_binding, shell);
3683 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3684 fan_debug_repaint_binding, shell);
3685 weston_compositor_add_key_binding(ec, KEY_K, mod,
3686 force_kill_binding, shell);
3687 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3688 workspace_up_binding, shell);
3689 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3690 workspace_down_binding, shell);
3691 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3692 workspace_move_surface_up_binding,
3694 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3695 workspace_move_surface_down_binding,
3698 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3699 if (shell->workspaces.num > 1) {
3700 num_workspace_bindings = shell->workspaces.num;
3701 if (num_workspace_bindings > 6)
3702 num_workspace_bindings = 6;
3703 for (i = 0; i < num_workspace_bindings; i++)
3704 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3705 workspace_f_binding,
3711 module_init(struct weston_compositor *ec)
3713 struct weston_seat *seat;
3714 struct desktop_shell *shell;
3715 struct workspace **pws;
3717 struct wl_event_loop *loop;
3719 shell = malloc(sizeof *shell);
3723 memset(shell, 0, sizeof *shell);
3724 shell->compositor = ec;
3726 shell->destroy_listener.notify = shell_destroy;
3727 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3728 shell->lock_listener.notify = lock;
3729 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3730 shell->unlock_listener.notify = unlock;
3731 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3732 shell->show_input_panel_listener.notify = show_input_panels;
3733 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3734 shell->hide_input_panel_listener.notify = hide_input_panels;
3735 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3736 ec->ping_handler = ping_handler;
3737 ec->shell_interface.shell = shell;
3738 ec->shell_interface.create_shell_surface = create_shell_surface;
3739 ec->shell_interface.set_toplevel = set_toplevel;
3740 ec->shell_interface.set_transient = set_transient;
3741 ec->shell_interface.move = surface_move;
3742 ec->shell_interface.resize = surface_resize;
3744 wl_list_init(&shell->screensaver.surfaces);
3745 wl_list_init(&shell->input_panel.surfaces);
3747 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3748 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3749 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3750 weston_layer_init(&shell->lock_layer, NULL);
3751 weston_layer_init(&shell->input_panel_layer, NULL);
3753 wl_array_init(&shell->workspaces.array);
3754 wl_list_init(&shell->workspaces.client_list);
3756 shell_configuration(shell);
3758 for (i = 0; i < shell->workspaces.num; i++) {
3759 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3763 *pws = workspace_create();
3767 activate_workspace(shell, 0);
3769 wl_list_init(&shell->workspaces.anim_sticky_list);
3770 wl_list_init(&shell->workspaces.animation.link);
3771 shell->workspaces.animation.frame = animate_workspace_change_frame;
3773 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3774 shell, bind_shell) == NULL)
3777 if (wl_display_add_global(ec->wl_display,
3778 &desktop_shell_interface,
3779 shell, bind_desktop_shell) == NULL)
3782 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3783 shell, bind_screensaver) == NULL)
3786 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3787 shell, bind_input_panel) == NULL)
3790 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3791 shell, bind_workspace_manager) == NULL)
3794 shell->child.deathstamp = weston_compositor_get_time();
3796 loop = wl_display_get_event_loop(ec->wl_display);
3797 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
3799 wl_list_for_each(seat, &ec->seat_list, link)
3800 create_pointer_focus_listener(seat);
3802 shell_add_bindings(ec, shell);