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);
443 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
445 struct focus_state *state, *next;
446 struct wl_surface *surface;
448 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
449 surface = state->keyboard_focus ?
450 &state->keyboard_focus->surface : NULL;
452 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
457 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
458 struct weston_seat *seat)
460 struct focus_state *state;
461 struct wl_surface *surface;
463 wl_list_for_each(state, &ws->focus_list, link) {
464 if (state->seat == seat) {
465 surface = seat->seat.keyboard->focus;
466 state->keyboard_focus =
467 (struct weston_surface *) surface;
474 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
475 struct weston_surface *surface)
477 struct focus_state *state;
479 wl_list_for_each(state, &ws->focus_list, link)
480 if (state->keyboard_focus == surface)
481 state->keyboard_focus = NULL;
485 workspace_destroy(struct workspace *ws)
487 struct focus_state *state, *next;
489 wl_list_for_each_safe(state, next, &ws->focus_list, link)
490 focus_state_destroy(state);
496 seat_destroyed(struct wl_listener *listener, void *data)
498 struct weston_seat *seat = data;
499 struct focus_state *state, *next;
500 struct workspace *ws = container_of(listener,
502 seat_destroyed_listener);
504 wl_list_for_each_safe(state, next, &ws->focus_list, link)
505 if (state->seat == seat)
506 wl_list_remove(&state->link);
509 static struct workspace *
510 workspace_create(void)
512 struct workspace *ws = malloc(sizeof *ws);
516 weston_layer_init(&ws->layer, NULL);
518 wl_list_init(&ws->focus_list);
519 wl_list_init(&ws->seat_destroyed_listener.link);
520 ws->seat_destroyed_listener.notify = seat_destroyed;
526 workspace_is_empty(struct workspace *ws)
528 return wl_list_empty(&ws->layer.surface_list);
531 static struct workspace *
532 get_workspace(struct desktop_shell *shell, unsigned int index)
534 struct workspace **pws = shell->workspaces.array.data;
539 static struct workspace *
540 get_current_workspace(struct desktop_shell *shell)
542 return get_workspace(shell, shell->workspaces.current);
546 activate_workspace(struct desktop_shell *shell, unsigned int index)
548 struct workspace *ws;
550 ws = get_workspace(shell, index);
551 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
553 shell->workspaces.current = index;
557 get_output_height(struct weston_output *output)
559 return abs(output->region.extents.y1 - output->region.extents.y2);
563 surface_translate(struct weston_surface *surface, double d)
565 struct shell_surface *shsurf = get_shell_surface(surface);
566 struct weston_transform *transform;
568 transform = &shsurf->workspace_transform;
569 if (wl_list_empty(&transform->link))
570 wl_list_insert(surface->geometry.transformation_list.prev,
571 &shsurf->workspace_transform.link);
573 weston_matrix_init(&shsurf->workspace_transform.matrix);
574 weston_matrix_translate(&shsurf->workspace_transform.matrix,
576 surface->geometry.dirty = 1;
580 workspace_translate_out(struct workspace *ws, double fraction)
582 struct weston_surface *surface;
586 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
587 height = get_output_height(surface->output);
588 d = height * fraction;
590 surface_translate(surface, d);
595 workspace_translate_in(struct workspace *ws, double fraction)
597 struct weston_surface *surface;
601 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
602 height = get_output_height(surface->output);
605 d = -(height - height * fraction);
607 d = height + height * fraction;
609 surface_translate(surface, d);
614 broadcast_current_workspace_state(struct desktop_shell *shell)
616 struct wl_resource *resource;
618 wl_list_for_each(resource, &shell->workspaces.client_list, link)
619 workspace_manager_send_state(resource,
620 shell->workspaces.current,
621 shell->workspaces.num);
625 reverse_workspace_change_animation(struct desktop_shell *shell,
627 struct workspace *from,
628 struct workspace *to)
630 shell->workspaces.current = index;
632 shell->workspaces.anim_to = to;
633 shell->workspaces.anim_from = from;
634 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
635 shell->workspaces.anim_timestamp = 0;
637 weston_compositor_schedule_repaint(shell->compositor);
641 workspace_deactivate_transforms(struct workspace *ws)
643 struct weston_surface *surface;
644 struct shell_surface *shsurf;
646 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
647 shsurf = get_shell_surface(surface);
648 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
649 wl_list_remove(&shsurf->workspace_transform.link);
650 wl_list_init(&shsurf->workspace_transform.link);
652 shsurf->surface->geometry.dirty = 1;
657 finish_workspace_change_animation(struct desktop_shell *shell,
658 struct workspace *from,
659 struct workspace *to)
661 weston_compositor_schedule_repaint(shell->compositor);
663 wl_list_remove(&shell->workspaces.animation.link);
664 workspace_deactivate_transforms(from);
665 workspace_deactivate_transforms(to);
666 shell->workspaces.anim_to = NULL;
668 wl_list_remove(&shell->workspaces.anim_from->layer.link);
672 animate_workspace_change_frame(struct weston_animation *animation,
673 struct weston_output *output, uint32_t msecs)
675 struct desktop_shell *shell =
676 container_of(animation, struct desktop_shell,
677 workspaces.animation);
678 struct workspace *from = shell->workspaces.anim_from;
679 struct workspace *to = shell->workspaces.anim_to;
683 if (workspace_is_empty(from) && workspace_is_empty(to)) {
684 finish_workspace_change_animation(shell, from, to);
688 if (shell->workspaces.anim_timestamp == 0) {
689 if (shell->workspaces.anim_current == 0.0)
690 shell->workspaces.anim_timestamp = msecs;
692 shell->workspaces.anim_timestamp =
694 /* Invers of movement function 'y' below. */
695 (asin(1.0 - shell->workspaces.anim_current) *
696 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
700 t = msecs - shell->workspaces.anim_timestamp;
706 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
709 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
710 weston_compositor_schedule_repaint(shell->compositor);
712 workspace_translate_out(from, shell->workspaces.anim_dir * y);
713 workspace_translate_in(to, shell->workspaces.anim_dir * y);
714 shell->workspaces.anim_current = y;
716 weston_compositor_schedule_repaint(shell->compositor);
719 finish_workspace_change_animation(shell, from, to);
723 animate_workspace_change(struct desktop_shell *shell,
725 struct workspace *from,
726 struct workspace *to)
728 struct weston_output *output;
732 if (index > shell->workspaces.current)
737 shell->workspaces.current = index;
739 shell->workspaces.anim_dir = dir;
740 shell->workspaces.anim_from = from;
741 shell->workspaces.anim_to = to;
742 shell->workspaces.anim_current = 0.0;
743 shell->workspaces.anim_timestamp = 0;
745 output = container_of(shell->compositor->output_list.next,
746 struct weston_output, link);
747 wl_list_insert(&output->animation_list,
748 &shell->workspaces.animation.link);
750 wl_list_insert(from->layer.link.prev, &to->layer.link);
752 workspace_translate_in(to, 0);
754 restore_focus_state(shell, to);
756 weston_compositor_schedule_repaint(shell->compositor);
760 update_workspace(struct desktop_shell *shell, unsigned int index,
761 struct workspace *from, struct workspace *to)
763 shell->workspaces.current = index;
764 wl_list_insert(&from->layer.link, &to->layer.link);
765 wl_list_remove(&from->layer.link);
769 change_workspace(struct desktop_shell *shell, unsigned int index)
771 struct workspace *from;
772 struct workspace *to;
774 if (index == shell->workspaces.current)
777 /* Don't change workspace when there is any fullscreen surfaces. */
778 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
781 from = get_current_workspace(shell);
782 to = get_workspace(shell, index);
784 if (shell->workspaces.anim_from == to &&
785 shell->workspaces.anim_to == from) {
786 restore_focus_state(shell, to);
787 reverse_workspace_change_animation(shell, index, from, to);
788 broadcast_current_workspace_state(shell);
792 if (shell->workspaces.anim_to != NULL)
793 finish_workspace_change_animation(shell,
794 shell->workspaces.anim_from,
795 shell->workspaces.anim_to);
797 restore_focus_state(shell, to);
799 if (workspace_is_empty(to) && workspace_is_empty(from))
800 update_workspace(shell, index, from, to);
802 animate_workspace_change(shell, index, from, to);
804 broadcast_current_workspace_state(shell);
808 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
810 struct wl_list *list = &ws->layer.surface_list;
813 if (wl_list_empty(list))
821 return container_of(e, struct weston_surface, layer_link) == surface;
825 move_surface_to_workspace(struct desktop_shell *shell,
826 struct weston_surface *surface,
829 struct workspace *from;
830 struct workspace *to;
831 struct weston_seat *seat;
833 if (workspace == shell->workspaces.current)
836 from = get_current_workspace(shell);
837 to = get_workspace(shell, workspace);
839 wl_list_remove(&surface->layer_link);
840 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
842 drop_focus_state(shell, from, surface);
843 wl_list_for_each(seat, &shell->compositor->seat_list, link)
844 if (seat->has_keyboard &&
845 seat->keyboard.focus == &surface->surface)
846 wl_keyboard_set_focus(&seat->keyboard, NULL);
848 weston_surface_damage_below(surface);
852 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
853 struct wl_seat *wl_seat,
856 struct weston_seat *seat = (struct weston_seat *) wl_seat;
857 struct weston_surface *surface =
858 (struct weston_surface *) wl_seat->keyboard->focus;
859 struct shell_surface *shsurf;
860 struct workspace *from;
861 struct workspace *to;
863 if (surface == NULL ||
864 index == shell->workspaces.current)
867 from = get_current_workspace(shell);
868 to = get_workspace(shell, index);
870 wl_list_remove(&surface->layer_link);
871 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
873 replace_focus_state(shell, to, seat);
874 drop_focus_state(shell, from, surface);
876 if (shell->workspaces.anim_from == to &&
877 shell->workspaces.anim_to == from) {
878 wl_list_remove(&to->layer.link);
879 wl_list_insert(from->layer.link.prev, &to->layer.link);
881 reverse_workspace_change_animation(shell, index, from, to);
882 broadcast_current_workspace_state(shell);
887 if (shell->workspaces.anim_to != NULL)
888 finish_workspace_change_animation(shell,
889 shell->workspaces.anim_from,
890 shell->workspaces.anim_to);
892 if (workspace_is_empty(from) &&
893 workspace_has_only(to, surface))
894 update_workspace(shell, index, from, to);
896 shsurf = get_shell_surface(surface);
897 if (wl_list_empty(&shsurf->workspace_transform.link))
898 wl_list_insert(&shell->workspaces.anim_sticky_list,
899 &shsurf->workspace_transform.link);
901 animate_workspace_change(shell, index, from, to);
904 broadcast_current_workspace_state(shell);
908 workspace_manager_move_surface(struct wl_client *client,
909 struct wl_resource *resource,
910 struct wl_resource *surface_resource,
913 struct desktop_shell *shell = resource->data;
914 struct weston_surface *surface =
915 (struct weston_surface *) surface_resource;
917 move_surface_to_workspace(shell, surface, workspace);
920 static const struct workspace_manager_interface workspace_manager_implementation = {
921 workspace_manager_move_surface,
925 unbind_resource(struct wl_resource *resource)
927 wl_list_remove(&resource->link);
932 bind_workspace_manager(struct wl_client *client,
933 void *data, uint32_t version, uint32_t id)
935 struct desktop_shell *shell = data;
936 struct wl_resource *resource;
938 resource = wl_client_add_object(client, &workspace_manager_interface,
939 &workspace_manager_implementation,
942 if (resource == NULL) {
943 weston_log("couldn't add workspace manager object");
947 resource->destroy = unbind_resource;
948 wl_list_insert(&shell->workspaces.client_list, &resource->link);
950 workspace_manager_send_state(resource,
951 shell->workspaces.current,
952 shell->workspaces.num);
956 noop_grab_focus(struct wl_pointer_grab *grab,
957 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
963 move_grab_motion(struct wl_pointer_grab *grab,
964 uint32_t time, wl_fixed_t x, wl_fixed_t y)
966 struct weston_move_grab *move = (struct weston_move_grab *) grab;
967 struct wl_pointer *pointer = grab->pointer;
968 struct shell_surface *shsurf = move->base.shsurf;
969 struct weston_surface *es;
970 int dx = wl_fixed_to_int(pointer->x + move->dx);
971 int dy = wl_fixed_to_int(pointer->y + move->dy);
976 es = shsurf->surface;
978 weston_surface_configure(es, dx, dy,
979 es->geometry.width, es->geometry.height);
981 weston_compositor_schedule_repaint(es->compositor);
985 move_grab_button(struct wl_pointer_grab *grab,
986 uint32_t time, uint32_t button, uint32_t state_w)
988 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
990 struct wl_pointer *pointer = grab->pointer;
991 enum wl_pointer_button_state state = state_w;
993 if (pointer->button_count == 0 &&
994 state == WL_POINTER_BUTTON_STATE_RELEASED) {
995 shell_grab_end(shell_grab);
1000 static const struct wl_pointer_grab_interface move_grab_interface = {
1007 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1009 struct weston_move_grab *move;
1014 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1017 move = malloc(sizeof *move);
1021 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1022 ws->seat.pointer->grab_x;
1023 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1024 ws->seat.pointer->grab_y;
1026 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1027 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1033 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1034 struct wl_resource *seat_resource, uint32_t serial)
1036 struct weston_seat *ws = seat_resource->data;
1037 struct shell_surface *shsurf = resource->data;
1039 if (ws->seat.pointer->button_count == 0 ||
1040 ws->seat.pointer->grab_serial != serial ||
1041 ws->seat.pointer->focus != &shsurf->surface->surface)
1044 if (surface_move(shsurf, ws) < 0)
1045 wl_resource_post_no_memory(resource);
1048 struct weston_resize_grab {
1049 struct shell_grab base;
1051 int32_t width, height;
1055 resize_grab_motion(struct wl_pointer_grab *grab,
1056 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1058 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1059 struct wl_pointer *pointer = grab->pointer;
1060 struct shell_surface *shsurf = resize->base.shsurf;
1061 int32_t width, height;
1062 wl_fixed_t from_x, from_y;
1063 wl_fixed_t to_x, to_y;
1068 weston_surface_from_global_fixed(shsurf->surface,
1069 pointer->grab_x, pointer->grab_y,
1071 weston_surface_from_global_fixed(shsurf->surface,
1072 pointer->x, pointer->y, &to_x, &to_y);
1074 width = resize->width;
1075 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1076 width += wl_fixed_to_int(from_x - to_x);
1077 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1078 width += wl_fixed_to_int(to_x - from_x);
1081 height = resize->height;
1082 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1083 height += wl_fixed_to_int(from_y - to_y);
1084 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1085 height += wl_fixed_to_int(to_y - from_y);
1088 shsurf->client->send_configure(shsurf->surface,
1089 resize->edges, width, height);
1093 send_configure(struct weston_surface *surface,
1094 uint32_t edges, int32_t width, int32_t height)
1096 struct shell_surface *shsurf = get_shell_surface(surface);
1098 wl_shell_surface_send_configure(&shsurf->resource,
1099 edges, width, height);
1102 static const struct weston_shell_client shell_client = {
1107 resize_grab_button(struct wl_pointer_grab *grab,
1108 uint32_t time, uint32_t button, uint32_t state_w)
1110 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1111 struct wl_pointer *pointer = grab->pointer;
1112 enum wl_pointer_button_state state = state_w;
1114 if (pointer->button_count == 0 &&
1115 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1116 shell_grab_end(&resize->base);
1121 static const struct wl_pointer_grab_interface resize_grab_interface = {
1128 surface_resize(struct shell_surface *shsurf,
1129 struct weston_seat *ws, uint32_t edges)
1131 struct weston_resize_grab *resize;
1133 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1136 if (edges == 0 || edges > 15 ||
1137 (edges & 3) == 3 || (edges & 12) == 12)
1140 resize = malloc(sizeof *resize);
1144 resize->edges = edges;
1145 resize->width = shsurf->surface->geometry.width;
1146 resize->height = shsurf->surface->geometry.height;
1148 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1149 ws->seat.pointer, edges);
1155 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1156 struct wl_resource *seat_resource, uint32_t serial,
1159 struct weston_seat *ws = seat_resource->data;
1160 struct shell_surface *shsurf = resource->data;
1162 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1165 if (ws->seat.pointer->button_count == 0 ||
1166 ws->seat.pointer->grab_serial != serial ||
1167 ws->seat.pointer->focus != &shsurf->surface->surface)
1170 if (surface_resize(shsurf, ws, edges) < 0)
1171 wl_resource_post_no_memory(resource);
1175 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1176 struct wl_surface *surface, int32_t x, int32_t y)
1178 struct shell_grab *grab = (struct shell_grab *) base;
1180 if (grab->grab.focus != surface) {
1181 shell_grab_end(grab);
1187 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1188 uint32_t time, int32_t x, int32_t y)
1193 busy_cursor_grab_button(struct wl_pointer_grab *base,
1194 uint32_t time, uint32_t button, uint32_t state)
1196 struct shell_grab *grab = (struct shell_grab *) base;
1197 struct shell_surface *shsurf;
1198 struct weston_surface *surface =
1199 (struct weston_surface *) grab->grab.pointer->current;
1200 struct weston_seat *seat =
1201 (struct weston_seat *) grab->grab.pointer->seat;
1203 shsurf = get_shell_surface(surface);
1204 if (shsurf && button == BTN_LEFT && state) {
1205 activate(shsurf->shell, shsurf->surface, seat);
1206 surface_move(shsurf, seat);
1210 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1211 busy_cursor_grab_focus,
1212 busy_cursor_grab_motion,
1213 busy_cursor_grab_button,
1217 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1219 struct shell_grab *grab;
1221 grab = malloc(sizeof *grab);
1225 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1226 DESKTOP_SHELL_CURSOR_BUSY);
1230 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1232 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1234 if (grab->grab.interface == &busy_cursor_grab_interface) {
1235 shell_grab_end(grab);
1241 ping_timer_destroy(struct shell_surface *shsurf)
1243 if (!shsurf || !shsurf->ping_timer)
1246 if (shsurf->ping_timer->source)
1247 wl_event_source_remove(shsurf->ping_timer->source);
1249 free(shsurf->ping_timer);
1250 shsurf->ping_timer = NULL;
1254 ping_timeout_handler(void *data)
1256 struct shell_surface *shsurf = data;
1257 struct weston_seat *seat;
1259 /* Client is not responding */
1260 shsurf->unresponsive = 1;
1262 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1263 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1264 set_busy_cursor(shsurf, seat->seat.pointer);
1270 ping_handler(struct weston_surface *surface, uint32_t serial)
1272 struct shell_surface *shsurf = get_shell_surface(surface);
1273 struct wl_event_loop *loop;
1274 int ping_timeout = 200;
1278 if (!shsurf->resource.client)
1281 if (shsurf->surface == shsurf->shell->grab_surface)
1284 if (!shsurf->ping_timer) {
1285 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1286 if (!shsurf->ping_timer)
1289 shsurf->ping_timer->serial = serial;
1290 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1291 shsurf->ping_timer->source =
1292 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1293 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1295 wl_shell_surface_send_ping(&shsurf->resource, serial);
1300 handle_pointer_focus(struct wl_listener *listener, void *data)
1302 struct wl_pointer *pointer = data;
1303 struct weston_surface *surface =
1304 (struct weston_surface *) pointer->focus;
1305 struct weston_compositor *compositor;
1306 struct shell_surface *shsurf;
1312 compositor = surface->compositor;
1313 shsurf = get_shell_surface(surface);
1315 if (shsurf && shsurf->unresponsive) {
1316 set_busy_cursor(shsurf, pointer);
1318 serial = wl_display_next_serial(compositor->wl_display);
1319 ping_handler(surface, serial);
1324 create_pointer_focus_listener(struct weston_seat *seat)
1326 struct wl_listener *listener;
1328 if (!seat->seat.pointer)
1331 listener = malloc(sizeof *listener);
1332 listener->notify = handle_pointer_focus;
1333 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1337 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1340 struct shell_surface *shsurf = resource->data;
1341 struct desktop_shell *shell = shsurf->shell;
1342 struct weston_seat *seat;
1343 struct weston_compositor *ec = shsurf->surface->compositor;
1344 struct wl_pointer *pointer;
1345 int was_unresponsive;
1347 if (shsurf->ping_timer == NULL)
1348 /* Just ignore unsolicited pong. */
1351 if (shsurf->ping_timer->serial == serial) {
1352 was_unresponsive = shsurf->unresponsive;
1353 shsurf->unresponsive = 0;
1354 if (was_unresponsive) {
1355 /* Received pong from previously unresponsive client */
1356 wl_list_for_each(seat, &ec->seat_list, link) {
1357 pointer = seat->seat.pointer;
1358 if (pointer->focus ==
1359 &shell->grab_surface->surface &&
1361 &shsurf->surface->surface)
1362 end_busy_cursor(shsurf, pointer);
1365 ping_timer_destroy(shsurf);
1370 shell_surface_set_title(struct wl_client *client,
1371 struct wl_resource *resource, const char *title)
1373 struct shell_surface *shsurf = resource->data;
1375 free(shsurf->title);
1376 shsurf->title = strdup(title);
1380 shell_surface_set_class(struct wl_client *client,
1381 struct wl_resource *resource, const char *class)
1383 struct shell_surface *shsurf = resource->data;
1385 free(shsurf->class);
1386 shsurf->class = strdup(class);
1389 static struct weston_output *
1390 get_default_output(struct weston_compositor *compositor)
1392 return container_of(compositor->output_list.next,
1393 struct weston_output, link);
1397 shell_unset_fullscreen(struct shell_surface *shsurf)
1399 /* undo all fullscreen things here */
1400 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1401 shell_surface_is_top_fullscreen(shsurf)) {
1402 weston_output_switch_mode(shsurf->fullscreen_output,
1403 shsurf->fullscreen_output->origin);
1405 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1406 shsurf->fullscreen.framerate = 0;
1407 wl_list_remove(&shsurf->fullscreen.transform.link);
1408 wl_list_init(&shsurf->fullscreen.transform.link);
1409 if (shsurf->fullscreen.black_surface)
1410 weston_surface_destroy(shsurf->fullscreen.black_surface);
1411 shsurf->fullscreen.black_surface = NULL;
1412 shsurf->fullscreen_output = NULL;
1413 weston_surface_set_position(shsurf->surface,
1414 shsurf->saved_x, shsurf->saved_y);
1415 if (shsurf->saved_rotation_valid) {
1416 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1417 &shsurf->rotation.transform.link);
1418 shsurf->saved_rotation_valid = false;
1423 reset_shell_surface_type(struct shell_surface *surface)
1425 switch (surface->type) {
1426 case SHELL_SURFACE_FULLSCREEN:
1427 shell_unset_fullscreen(surface);
1429 case SHELL_SURFACE_MAXIMIZED:
1430 surface->output = get_default_output(surface->surface->compositor);
1431 weston_surface_set_position(surface->surface,
1435 case SHELL_SURFACE_NONE:
1436 case SHELL_SURFACE_TOPLEVEL:
1437 case SHELL_SURFACE_TRANSIENT:
1438 case SHELL_SURFACE_POPUP:
1442 surface->type = SHELL_SURFACE_NONE;
1447 set_surface_type(struct shell_surface *shsurf)
1449 struct weston_surface *surface = shsurf->surface;
1450 struct weston_surface *pes = shsurf->parent;
1452 reset_shell_surface_type(shsurf);
1454 shsurf->type = shsurf->next_type;
1455 shsurf->next_type = SHELL_SURFACE_NONE;
1457 switch (shsurf->type) {
1458 case SHELL_SURFACE_TOPLEVEL:
1460 case SHELL_SURFACE_TRANSIENT:
1461 weston_surface_set_position(surface,
1462 pes->geometry.x + shsurf->transient.x,
1463 pes->geometry.y + shsurf->transient.y);
1466 case SHELL_SURFACE_MAXIMIZED:
1467 shsurf->saved_x = surface->geometry.x;
1468 shsurf->saved_y = surface->geometry.y;
1469 shsurf->saved_position_valid = true;
1472 case SHELL_SURFACE_FULLSCREEN:
1473 shsurf->saved_x = surface->geometry.x;
1474 shsurf->saved_y = surface->geometry.y;
1475 shsurf->saved_position_valid = true;
1477 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1478 wl_list_remove(&shsurf->rotation.transform.link);
1479 wl_list_init(&shsurf->rotation.transform.link);
1480 shsurf->surface->geometry.dirty = 1;
1481 shsurf->saved_rotation_valid = true;
1491 set_toplevel(struct shell_surface *shsurf)
1493 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1497 shell_surface_set_toplevel(struct wl_client *client,
1498 struct wl_resource *resource)
1500 struct shell_surface *surface = resource->data;
1502 set_toplevel(surface);
1506 set_transient(struct shell_surface *shsurf,
1507 struct weston_surface *parent, int x, int y, uint32_t flags)
1509 /* assign to parents output */
1510 shsurf->parent = parent;
1511 shsurf->transient.x = x;
1512 shsurf->transient.y = y;
1513 shsurf->transient.flags = flags;
1514 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1518 shell_surface_set_transient(struct wl_client *client,
1519 struct wl_resource *resource,
1520 struct wl_resource *parent_resource,
1521 int x, int y, uint32_t flags)
1523 struct shell_surface *shsurf = resource->data;
1524 struct weston_surface *parent = parent_resource->data;
1526 set_transient(shsurf, parent, x, y, flags);
1529 static struct desktop_shell *
1530 shell_surface_get_shell(struct shell_surface *shsurf)
1532 return shsurf->shell;
1536 get_output_panel_height(struct desktop_shell *shell,
1537 struct weston_output *output)
1539 struct weston_surface *surface;
1540 int panel_height = 0;
1545 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1546 if (surface->output == output) {
1547 panel_height = surface->geometry.height;
1552 return panel_height;
1556 shell_surface_set_maximized(struct wl_client *client,
1557 struct wl_resource *resource,
1558 struct wl_resource *output_resource )
1560 struct shell_surface *shsurf = resource->data;
1561 struct weston_surface *es = shsurf->surface;
1562 struct desktop_shell *shell = NULL;
1563 uint32_t edges = 0, panel_height = 0;
1565 /* get the default output, if the client set it as NULL
1566 check whether the ouput is available */
1567 if (output_resource)
1568 shsurf->output = output_resource->data;
1569 else if (es->output)
1570 shsurf->output = es->output;
1572 shsurf->output = get_default_output(es->compositor);
1574 shell = shell_surface_get_shell(shsurf);
1575 panel_height = get_output_panel_height(shell, shsurf->output);
1576 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1578 shsurf->client->send_configure(shsurf->surface, edges,
1579 shsurf->output->width,
1580 shsurf->output->height - panel_height);
1582 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1586 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1588 static struct weston_surface *
1589 create_black_surface(struct weston_compositor *ec,
1590 struct weston_surface *fs_surface,
1591 GLfloat x, GLfloat y, int w, int h)
1593 struct weston_surface *surface = NULL;
1595 surface = weston_surface_create(ec);
1596 if (surface == NULL) {
1597 weston_log("no memory\n");
1601 surface->configure = black_surface_configure;
1602 surface->private = fs_surface;
1603 weston_surface_configure(surface, x, y, w, h);
1604 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1605 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1610 /* Create black surface and append it to the associated fullscreen surface.
1611 * Handle size dismatch and positioning according to the method. */
1613 shell_configure_fullscreen(struct shell_surface *shsurf)
1615 struct weston_output *output = shsurf->fullscreen_output;
1616 struct weston_surface *surface = shsurf->surface;
1617 struct weston_matrix *matrix;
1618 float scale, output_aspect, surface_aspect, x, y;
1620 if (!shsurf->fullscreen.black_surface)
1621 shsurf->fullscreen.black_surface =
1622 create_black_surface(surface->compositor,
1624 output->x, output->y,
1628 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1629 wl_list_insert(&surface->layer_link,
1630 &shsurf->fullscreen.black_surface->layer_link);
1631 shsurf->fullscreen.black_surface->output = output;
1633 switch (shsurf->fullscreen.type) {
1634 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1635 if (surface->buffer)
1636 center_on_output(surface, shsurf->fullscreen_output);
1638 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1639 matrix = &shsurf->fullscreen.transform.matrix;
1640 weston_matrix_init(matrix);
1642 output_aspect = (float) output->width /
1643 (float) output->height;
1644 surface_aspect = (float) surface->geometry.width /
1645 (float) surface->geometry.height;
1646 if (output_aspect < surface_aspect)
1647 scale = (float) output->width /
1648 (float) surface->geometry.width;
1650 scale = (float) output->height /
1651 (float) surface->geometry.height;
1653 weston_matrix_scale(matrix, scale, scale, 1);
1654 wl_list_remove(&shsurf->fullscreen.transform.link);
1655 wl_list_insert(&surface->geometry.transformation_list,
1656 &shsurf->fullscreen.transform.link);
1657 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1658 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1659 weston_surface_set_position(surface, x, y);
1662 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1663 if (shell_surface_is_top_fullscreen(shsurf)) {
1664 struct weston_mode mode = {0,
1665 surface->geometry.width,
1666 surface->geometry.height,
1667 shsurf->fullscreen.framerate};
1669 if (weston_output_switch_mode(output, &mode) == 0) {
1670 weston_surface_configure(shsurf->fullscreen.black_surface,
1671 output->x, output->y,
1674 weston_surface_set_position(surface, output->x, output->y);
1679 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1686 /* make the fullscreen and black surface at the top */
1688 shell_stack_fullscreen(struct shell_surface *shsurf)
1690 struct weston_output *output = shsurf->fullscreen_output;
1691 struct weston_surface *surface = shsurf->surface;
1692 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1694 wl_list_remove(&surface->layer_link);
1695 wl_list_insert(&shell->fullscreen_layer.surface_list,
1696 &surface->layer_link);
1697 weston_surface_damage(surface);
1699 if (!shsurf->fullscreen.black_surface)
1700 shsurf->fullscreen.black_surface =
1701 create_black_surface(surface->compositor,
1703 output->x, output->y,
1707 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1708 wl_list_insert(&surface->layer_link,
1709 &shsurf->fullscreen.black_surface->layer_link);
1710 weston_surface_damage(shsurf->fullscreen.black_surface);
1714 shell_map_fullscreen(struct shell_surface *shsurf)
1716 shell_stack_fullscreen(shsurf);
1717 shell_configure_fullscreen(shsurf);
1721 shell_surface_set_fullscreen(struct wl_client *client,
1722 struct wl_resource *resource,
1725 struct wl_resource *output_resource)
1727 struct shell_surface *shsurf = resource->data;
1728 struct weston_surface *es = shsurf->surface;
1730 if (output_resource)
1731 shsurf->output = output_resource->data;
1732 else if (es->output)
1733 shsurf->output = es->output;
1735 shsurf->output = get_default_output(es->compositor);
1737 shsurf->fullscreen_output = shsurf->output;
1738 shsurf->fullscreen.type = method;
1739 shsurf->fullscreen.framerate = framerate;
1740 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1742 shsurf->client->send_configure(shsurf->surface, 0,
1743 shsurf->output->width,
1744 shsurf->output->height);
1748 popup_grab_focus(struct wl_pointer_grab *grab,
1749 struct wl_surface *surface,
1753 struct wl_pointer *pointer = grab->pointer;
1754 struct shell_surface *priv =
1755 container_of(grab, struct shell_surface, popup.grab);
1756 struct wl_client *client = priv->surface->surface.resource.client;
1758 if (surface && surface->resource.client == client) {
1759 wl_pointer_set_focus(pointer, surface, x, y);
1760 grab->focus = surface;
1762 wl_pointer_set_focus(pointer, NULL,
1763 wl_fixed_from_int(0),
1764 wl_fixed_from_int(0));
1770 popup_grab_motion(struct wl_pointer_grab *grab,
1771 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1773 struct wl_resource *resource;
1775 resource = grab->pointer->focus_resource;
1777 wl_pointer_send_motion(resource, time, sx, sy);
1781 popup_grab_button(struct wl_pointer_grab *grab,
1782 uint32_t time, uint32_t button, uint32_t state_w)
1784 struct wl_resource *resource;
1785 struct shell_surface *shsurf =
1786 container_of(grab, struct shell_surface, popup.grab);
1787 struct wl_display *display;
1788 enum wl_pointer_button_state state = state_w;
1791 resource = grab->pointer->focus_resource;
1793 display = wl_client_get_display(resource->client);
1794 serial = wl_display_get_serial(display);
1795 wl_pointer_send_button(resource, serial, time, button, state);
1796 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1797 (shsurf->popup.initial_up ||
1798 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1799 wl_shell_surface_send_popup_done(&shsurf->resource);
1800 wl_pointer_end_grab(grab->pointer);
1801 shsurf->popup.grab.pointer = NULL;
1804 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1805 shsurf->popup.initial_up = 1;
1808 static const struct wl_pointer_grab_interface popup_grab_interface = {
1815 shell_map_popup(struct shell_surface *shsurf)
1817 struct wl_seat *seat = shsurf->popup.seat;
1818 struct weston_surface *es = shsurf->surface;
1819 struct weston_surface *parent = shsurf->parent;
1821 es->output = parent->output;
1822 shsurf->popup.grab.interface = &popup_grab_interface;
1824 weston_surface_update_transform(parent);
1825 if (parent->transform.enabled) {
1826 shsurf->popup.parent_transform.matrix =
1827 parent->transform.matrix;
1829 /* construct x, y translation matrix */
1830 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1831 shsurf->popup.parent_transform.matrix.d[12] =
1833 shsurf->popup.parent_transform.matrix.d[13] =
1836 wl_list_insert(es->geometry.transformation_list.prev,
1837 &shsurf->popup.parent_transform.link);
1838 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1840 shsurf->popup.initial_up = 0;
1842 /* We don't require the grab to still be active, but if another
1843 * grab has started in the meantime, we end the popup now. */
1844 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1845 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1847 wl_shell_surface_send_popup_done(&shsurf->resource);
1852 shell_surface_set_popup(struct wl_client *client,
1853 struct wl_resource *resource,
1854 struct wl_resource *seat_resource,
1856 struct wl_resource *parent_resource,
1857 int32_t x, int32_t y, uint32_t flags)
1859 struct shell_surface *shsurf = resource->data;
1861 shsurf->type = SHELL_SURFACE_POPUP;
1862 shsurf->parent = parent_resource->data;
1863 shsurf->popup.seat = seat_resource->data;
1864 shsurf->popup.serial = serial;
1865 shsurf->popup.x = x;
1866 shsurf->popup.y = y;
1869 static const struct wl_shell_surface_interface shell_surface_implementation = {
1872 shell_surface_resize,
1873 shell_surface_set_toplevel,
1874 shell_surface_set_transient,
1875 shell_surface_set_fullscreen,
1876 shell_surface_set_popup,
1877 shell_surface_set_maximized,
1878 shell_surface_set_title,
1879 shell_surface_set_class
1883 destroy_shell_surface(struct shell_surface *shsurf)
1885 if (shsurf->popup.grab.pointer)
1886 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1888 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1889 shell_surface_is_top_fullscreen(shsurf)) {
1890 weston_output_switch_mode(shsurf->fullscreen_output,
1891 shsurf->fullscreen_output->origin);
1894 if (shsurf->fullscreen.black_surface)
1895 weston_surface_destroy(shsurf->fullscreen.black_surface);
1897 /* As destroy_resource() use wl_list_for_each_safe(),
1898 * we can always remove the listener.
1900 wl_list_remove(&shsurf->surface_destroy_listener.link);
1901 shsurf->surface->configure = NULL;
1902 ping_timer_destroy(shsurf);
1904 wl_list_remove(&shsurf->link);
1909 shell_destroy_shell_surface(struct wl_resource *resource)
1911 struct shell_surface *shsurf = resource->data;
1913 destroy_shell_surface(shsurf);
1917 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1919 struct shell_surface *shsurf = container_of(listener,
1920 struct shell_surface,
1921 surface_destroy_listener);
1923 if (shsurf->resource.client) {
1924 wl_resource_destroy(&shsurf->resource);
1926 wl_signal_emit(&shsurf->resource.destroy_signal,
1928 destroy_shell_surface(shsurf);
1933 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1935 static struct shell_surface *
1936 get_shell_surface(struct weston_surface *surface)
1938 if (surface->configure == shell_surface_configure)
1939 return surface->private;
1944 static struct shell_surface *
1945 create_shell_surface(void *shell, struct weston_surface *surface,
1946 const struct weston_shell_client *client)
1948 struct shell_surface *shsurf;
1950 if (surface->configure) {
1951 weston_log("surface->configure already set\n");
1955 shsurf = calloc(1, sizeof *shsurf);
1957 weston_log("no memory to allocate shell surface\n");
1961 surface->configure = shell_surface_configure;
1962 surface->private = shsurf;
1964 shsurf->shell = (struct desktop_shell *) shell;
1965 shsurf->unresponsive = 0;
1966 shsurf->saved_position_valid = false;
1967 shsurf->saved_rotation_valid = false;
1968 shsurf->surface = surface;
1969 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1970 shsurf->fullscreen.framerate = 0;
1971 shsurf->fullscreen.black_surface = NULL;
1972 shsurf->ping_timer = NULL;
1973 wl_list_init(&shsurf->fullscreen.transform.link);
1975 wl_signal_init(&shsurf->resource.destroy_signal);
1976 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1977 wl_signal_add(&surface->surface.resource.destroy_signal,
1978 &shsurf->surface_destroy_listener);
1980 /* init link so its safe to always remove it in destroy_shell_surface */
1981 wl_list_init(&shsurf->link);
1983 /* empty when not in use */
1984 wl_list_init(&shsurf->rotation.transform.link);
1985 weston_matrix_init(&shsurf->rotation.rotation);
1987 wl_list_init(&shsurf->workspace_transform.link);
1989 shsurf->type = SHELL_SURFACE_NONE;
1990 shsurf->next_type = SHELL_SURFACE_NONE;
1992 shsurf->client = client;
1998 shell_get_shell_surface(struct wl_client *client,
1999 struct wl_resource *resource,
2001 struct wl_resource *surface_resource)
2003 struct weston_surface *surface = surface_resource->data;
2004 struct desktop_shell *shell = resource->data;
2005 struct shell_surface *shsurf;
2007 if (get_shell_surface(surface)) {
2008 wl_resource_post_error(surface_resource,
2009 WL_DISPLAY_ERROR_INVALID_OBJECT,
2010 "desktop_shell::get_shell_surface already requested");
2014 shsurf = create_shell_surface(shell, surface, &shell_client);
2016 wl_resource_post_error(surface_resource,
2017 WL_DISPLAY_ERROR_INVALID_OBJECT,
2018 "surface->configure already set");
2022 shsurf->resource.destroy = shell_destroy_shell_surface;
2023 shsurf->resource.object.id = id;
2024 shsurf->resource.object.interface = &wl_shell_surface_interface;
2025 shsurf->resource.object.implementation =
2026 (void (**)(void)) &shell_surface_implementation;
2027 shsurf->resource.data = shsurf;
2029 wl_client_add_resource(client, &shsurf->resource);
2032 static const struct wl_shell_interface shell_implementation = {
2033 shell_get_shell_surface
2037 handle_screensaver_sigchld(struct weston_process *proc, int status)
2043 launch_screensaver(struct desktop_shell *shell)
2045 if (shell->screensaver.binding)
2048 if (!shell->screensaver.path)
2051 if (shell->screensaver.process.pid != 0) {
2052 weston_log("old screensaver still running\n");
2056 weston_client_launch(shell->compositor,
2057 &shell->screensaver.process,
2058 shell->screensaver.path,
2059 handle_screensaver_sigchld);
2063 terminate_screensaver(struct desktop_shell *shell)
2065 if (shell->screensaver.process.pid == 0)
2068 kill(shell->screensaver.process.pid, SIGTERM);
2072 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
2074 struct weston_surface *s, *next;
2076 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2077 if (s->output == es->output && s != es) {
2078 weston_surface_unmap(s);
2079 s->configure = NULL;
2083 weston_surface_configure(es, es->output->x, es->output->y,
2084 es->buffer->width, es->buffer->height);
2086 if (wl_list_empty(&es->layer_link)) {
2087 wl_list_insert(&layer->surface_list, &es->layer_link);
2088 weston_compositor_schedule_repaint(es->compositor);
2093 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2095 struct desktop_shell *shell = es->private;
2097 configure_static_surface(es, &shell->background_layer);
2101 desktop_shell_set_background(struct wl_client *client,
2102 struct wl_resource *resource,
2103 struct wl_resource *output_resource,
2104 struct wl_resource *surface_resource)
2106 struct desktop_shell *shell = resource->data;
2107 struct weston_surface *surface = surface_resource->data;
2109 if (surface->configure) {
2110 wl_resource_post_error(surface_resource,
2111 WL_DISPLAY_ERROR_INVALID_OBJECT,
2112 "surface role already assigned");
2116 surface->configure = background_configure;
2117 surface->private = shell;
2118 surface->output = output_resource->data;
2119 desktop_shell_send_configure(resource, 0,
2121 surface->output->width,
2122 surface->output->height);
2126 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2128 struct desktop_shell *shell = es->private;
2130 configure_static_surface(es, &shell->panel_layer);
2134 desktop_shell_set_panel(struct wl_client *client,
2135 struct wl_resource *resource,
2136 struct wl_resource *output_resource,
2137 struct wl_resource *surface_resource)
2139 struct desktop_shell *shell = resource->data;
2140 struct weston_surface *surface = surface_resource->data;
2142 if (surface->configure) {
2143 wl_resource_post_error(surface_resource,
2144 WL_DISPLAY_ERROR_INVALID_OBJECT,
2145 "surface role already assigned");
2149 surface->configure = panel_configure;
2150 surface->private = shell;
2151 surface->output = output_resource->data;
2152 desktop_shell_send_configure(resource, 0,
2154 surface->output->width,
2155 surface->output->height);
2159 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2161 struct desktop_shell *shell = surface->private;
2163 center_on_output(surface, get_default_output(shell->compositor));
2165 if (!weston_surface_is_mapped(surface)) {
2166 wl_list_insert(&shell->lock_layer.surface_list,
2167 &surface->layer_link);
2168 weston_surface_assign_output(surface);
2169 weston_compositor_wake(shell->compositor);
2174 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2176 struct desktop_shell *shell =
2177 container_of(listener, struct desktop_shell, lock_surface_listener);
2179 weston_log("lock surface gone\n");
2180 shell->lock_surface = NULL;
2184 desktop_shell_set_lock_surface(struct wl_client *client,
2185 struct wl_resource *resource,
2186 struct wl_resource *surface_resource)
2188 struct desktop_shell *shell = resource->data;
2189 struct weston_surface *surface = surface_resource->data;
2191 shell->prepare_event_sent = false;
2196 shell->lock_surface = surface;
2198 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2199 wl_signal_add(&surface_resource->destroy_signal,
2200 &shell->lock_surface_listener);
2202 surface->configure = lock_surface_configure;
2203 surface->private = shell;
2207 resume_desktop(struct desktop_shell *shell)
2209 struct weston_surface *surface;
2210 struct workspace *ws = get_current_workspace(shell);
2212 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2213 weston_surface_unmap(surface);
2215 terminate_screensaver(shell);
2217 wl_list_remove(&shell->lock_layer.link);
2218 wl_list_insert(&shell->compositor->cursor_layer.link,
2219 &shell->fullscreen_layer.link);
2220 wl_list_insert(&shell->fullscreen_layer.link,
2221 &shell->panel_layer.link);
2222 if (shell->showing_input_panels) {
2223 wl_list_insert(&shell->panel_layer.link,
2224 &shell->input_panel_layer.link);
2225 wl_list_insert(&shell->input_panel_layer.link,
2228 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2231 restore_focus_state(shell, get_current_workspace(shell));
2233 shell->locked = false;
2234 shell->compositor->idle_time = shell->compositor->option_idle_time;
2235 weston_compositor_wake(shell->compositor);
2236 weston_compositor_damage_all(shell->compositor);
2240 desktop_shell_unlock(struct wl_client *client,
2241 struct wl_resource *resource)
2243 struct desktop_shell *shell = resource->data;
2245 shell->prepare_event_sent = false;
2248 resume_desktop(shell);
2252 desktop_shell_set_grab_surface(struct wl_client *client,
2253 struct wl_resource *resource,
2254 struct wl_resource *surface_resource)
2256 struct desktop_shell *shell = resource->data;
2258 shell->grab_surface = surface_resource->data;
2261 static const struct desktop_shell_interface desktop_shell_implementation = {
2262 desktop_shell_set_background,
2263 desktop_shell_set_panel,
2264 desktop_shell_set_lock_surface,
2265 desktop_shell_unlock,
2266 desktop_shell_set_grab_surface
2269 static enum shell_surface_type
2270 get_shell_surface_type(struct weston_surface *surface)
2272 struct shell_surface *shsurf;
2274 shsurf = get_shell_surface(surface);
2276 return SHELL_SURFACE_NONE;
2277 return shsurf->type;
2281 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2283 struct weston_surface *surface =
2284 (struct weston_surface *) seat->pointer->focus;
2285 struct shell_surface *shsurf;
2287 if (surface == NULL)
2290 shsurf = get_shell_surface(surface);
2291 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2294 surface_move(shsurf, (struct weston_seat *) seat);
2298 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2300 struct weston_surface *surface =
2301 (struct weston_surface *) seat->pointer->focus;
2304 struct shell_surface *shsurf;
2306 if (surface == NULL)
2309 shsurf = get_shell_surface(surface);
2310 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2313 weston_surface_from_global(surface,
2314 wl_fixed_to_int(seat->pointer->grab_x),
2315 wl_fixed_to_int(seat->pointer->grab_y),
2318 if (x < surface->geometry.width / 3)
2319 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2320 else if (x < 2 * surface->geometry.width / 3)
2323 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2325 if (y < surface->geometry.height / 3)
2326 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2327 else if (y < 2 * surface->geometry.height / 3)
2330 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2332 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2336 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2337 wl_fixed_t value, void *data)
2340 struct shell_surface *shsurf;
2341 struct weston_surface *surface =
2342 (struct weston_surface *) seat->pointer->focus;
2344 if (surface == NULL)
2347 shsurf = get_shell_surface(surface);
2351 surface->alpha += wl_fixed_to_double(value) * step;
2353 if (surface->alpha > 1.0)
2354 surface->alpha = 1.0;
2355 if (surface->alpha < step)
2356 surface->alpha = step;
2358 surface->geometry.dirty = 1;
2359 weston_surface_damage(surface);
2363 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2366 struct weston_seat *ws = (struct weston_seat *) seat;
2367 struct weston_compositor *compositor = ws->compositor;
2368 struct weston_output *output;
2371 wl_list_for_each(output, &compositor->output_list, link) {
2372 if (pixman_region32_contains_point(&output->region,
2373 wl_fixed_to_double(seat->pointer->x),
2374 wl_fixed_to_double(seat->pointer->y),
2376 if (key == KEY_PAGEUP)
2377 increment = output->zoom.increment;
2378 else if (key == KEY_PAGEDOWN)
2379 increment = -output->zoom.increment;
2380 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2381 increment = output->zoom.increment *
2382 wl_fixed_to_double(value);
2386 output->zoom.level += increment;
2388 if (output->zoom.level < 0.0)
2389 output->zoom.level = 0.0;
2390 else if (output->zoom.level > output->zoom.max_level)
2391 output->zoom.level = output->zoom.max_level;
2393 output->zoom.active = 1;
2394 output->disable_planes++;
2397 output->zoom.spring_z.target = output->zoom.level;
2399 weston_output_update_zoom(output, output->zoom.type);
2405 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2406 wl_fixed_t value, void *data)
2408 do_zoom(seat, time, 0, axis, value);
2412 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2415 do_zoom(seat, time, key, 0, 0);
2419 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2422 struct weston_compositor *compositor = data;
2424 wl_display_terminate(compositor->wl_display);
2428 rotate_grab_motion(struct wl_pointer_grab *grab,
2429 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2431 struct rotate_grab *rotate =
2432 container_of(grab, struct rotate_grab, base.grab);
2433 struct wl_pointer *pointer = grab->pointer;
2434 struct shell_surface *shsurf = rotate->base.shsurf;
2435 struct weston_surface *surface;
2436 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2441 surface = shsurf->surface;
2443 cx = 0.5f * surface->geometry.width;
2444 cy = 0.5f * surface->geometry.height;
2446 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2447 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2448 r = sqrtf(dx * dx + dy * dy);
2450 wl_list_remove(&shsurf->rotation.transform.link);
2451 shsurf->surface->geometry.dirty = 1;
2454 struct weston_matrix *matrix =
2455 &shsurf->rotation.transform.matrix;
2457 weston_matrix_init(&rotate->rotation);
2458 rotate->rotation.d[0] = dx / r;
2459 rotate->rotation.d[4] = -dy / r;
2460 rotate->rotation.d[1] = -rotate->rotation.d[4];
2461 rotate->rotation.d[5] = rotate->rotation.d[0];
2463 weston_matrix_init(matrix);
2464 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2465 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2466 weston_matrix_multiply(matrix, &rotate->rotation);
2467 weston_matrix_translate(matrix, cx, cy, 0.0f);
2470 &shsurf->surface->geometry.transformation_list,
2471 &shsurf->rotation.transform.link);
2473 wl_list_init(&shsurf->rotation.transform.link);
2474 weston_matrix_init(&shsurf->rotation.rotation);
2475 weston_matrix_init(&rotate->rotation);
2478 /* We need to adjust the position of the surface
2479 * in case it was resized in a rotated state before */
2480 cposx = surface->geometry.x + cx;
2481 cposy = surface->geometry.y + cy;
2482 dposx = rotate->center.x - cposx;
2483 dposy = rotate->center.y - cposy;
2484 if (dposx != 0.0f || dposy != 0.0f) {
2485 weston_surface_set_position(surface,
2486 surface->geometry.x + dposx,
2487 surface->geometry.y + dposy);
2490 /* Repaint implies weston_surface_update_transform(), which
2491 * lazily applies the damage due to rotation update.
2493 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2497 rotate_grab_button(struct wl_pointer_grab *grab,
2498 uint32_t time, uint32_t button, uint32_t state_w)
2500 struct rotate_grab *rotate =
2501 container_of(grab, struct rotate_grab, base.grab);
2502 struct wl_pointer *pointer = grab->pointer;
2503 struct shell_surface *shsurf = rotate->base.shsurf;
2504 enum wl_pointer_button_state state = state_w;
2506 if (pointer->button_count == 0 &&
2507 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2509 weston_matrix_multiply(&shsurf->rotation.rotation,
2511 shell_grab_end(&rotate->base);
2516 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2523 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2526 struct weston_surface *base_surface =
2527 (struct weston_surface *) seat->pointer->focus;
2528 struct shell_surface *surface;
2529 struct rotate_grab *rotate;
2533 if (base_surface == NULL)
2536 surface = get_shell_surface(base_surface);
2537 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2540 rotate = malloc(sizeof *rotate);
2544 weston_surface_to_global_float(surface->surface,
2545 surface->surface->geometry.width / 2,
2546 surface->surface->geometry.height / 2,
2547 &rotate->center.x, &rotate->center.y);
2549 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2550 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2551 r = sqrtf(dx * dx + dy * dy);
2553 struct weston_matrix inverse;
2555 weston_matrix_init(&inverse);
2556 inverse.d[0] = dx / r;
2557 inverse.d[4] = dy / r;
2558 inverse.d[1] = -inverse.d[4];
2559 inverse.d[5] = inverse.d[0];
2560 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2562 weston_matrix_init(&rotate->rotation);
2563 rotate->rotation.d[0] = dx / r;
2564 rotate->rotation.d[4] = -dy / r;
2565 rotate->rotation.d[1] = -rotate->rotation.d[4];
2566 rotate->rotation.d[5] = rotate->rotation.d[0];
2568 weston_matrix_init(&surface->rotation.rotation);
2569 weston_matrix_init(&rotate->rotation);
2572 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2573 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2577 lower_fullscreen_layer(struct desktop_shell *shell)
2579 struct workspace *ws;
2580 struct weston_surface *surface, *prev;
2582 ws = get_current_workspace(shell);
2583 wl_list_for_each_reverse_safe(surface, prev,
2584 &shell->fullscreen_layer.surface_list,
2586 weston_surface_restack(surface, &ws->layer.surface_list);
2590 activate(struct desktop_shell *shell, struct weston_surface *es,
2591 struct weston_seat *seat)
2593 struct workspace *ws = get_current_workspace(shell);
2594 struct focus_state *state;
2596 weston_surface_activate(es, seat);
2598 wl_list_for_each(state, &ws->focus_list, link)
2599 if (state->seat == seat)
2602 if (&state->link == &ws->focus_list) {
2603 state = focus_state_create(seat, ws);
2608 state->keyboard_focus = es;
2609 wl_list_remove(&state->surface_destroy_listener.link);
2610 wl_signal_add(&es->surface.resource.destroy_signal,
2611 &state->surface_destroy_listener);
2613 switch (get_shell_surface_type(es)) {
2614 case SHELL_SURFACE_FULLSCREEN:
2615 /* should on top of panels */
2616 shell_stack_fullscreen(get_shell_surface(es));
2617 shell_configure_fullscreen(get_shell_surface(es));
2620 ws = get_current_workspace(shell);
2621 lower_fullscreen_layer(shell);
2622 weston_surface_restack(es, &ws->layer.surface_list);
2627 /* no-op func for checking black surface */
2629 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2634 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2636 if (es->configure == black_surface_configure) {
2638 *fs_surface = (struct weston_surface *)es->private;
2645 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2648 struct weston_seat *ws = (struct weston_seat *) seat;
2649 struct desktop_shell *shell = data;
2650 struct weston_surface *focus;
2651 struct weston_surface *upper;
2653 focus = (struct weston_surface *) seat->pointer->focus;
2657 if (is_black_surface(focus, &upper))
2660 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2663 if (seat->pointer->grab == &seat->pointer->default_grab)
2664 activate(shell, focus, ws);
2668 lock(struct wl_listener *listener, void *data)
2670 struct desktop_shell *shell =
2671 container_of(listener, struct desktop_shell, lock_listener);
2672 struct weston_output *output;
2673 struct workspace *ws = get_current_workspace(shell);
2675 if (shell->locked) {
2676 wl_list_for_each(output, &shell->compositor->output_list, link)
2677 /* TODO: find a way to jump to other DPMS levels */
2678 if (output->set_dpms)
2679 output->set_dpms(output, WESTON_DPMS_STANDBY);
2683 shell->locked = true;
2685 /* Hide all surfaces by removing the fullscreen, panel and
2686 * toplevel layers. This way nothing else can show or receive
2687 * input events while we are locked. */
2689 wl_list_remove(&shell->panel_layer.link);
2690 wl_list_remove(&shell->fullscreen_layer.link);
2691 if (shell->showing_input_panels)
2692 wl_list_remove(&shell->input_panel_layer.link);
2693 wl_list_remove(&ws->layer.link);
2694 wl_list_insert(&shell->compositor->cursor_layer.link,
2695 &shell->lock_layer.link);
2697 launch_screensaver(shell);
2699 /* TODO: disable bindings that should not work while locked. */
2701 /* All this must be undone in resume_desktop(). */
2705 unlock(struct wl_listener *listener, void *data)
2707 struct desktop_shell *shell =
2708 container_of(listener, struct desktop_shell, unlock_listener);
2710 if (!shell->locked || shell->lock_surface) {
2711 weston_compositor_wake(shell->compositor);
2715 /* If desktop-shell client has gone away, unlock immediately. */
2716 if (!shell->child.desktop_shell) {
2717 resume_desktop(shell);
2721 if (shell->prepare_event_sent)
2724 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2725 shell->prepare_event_sent = true;
2729 show_input_panels(struct wl_listener *listener, void *data)
2731 struct desktop_shell *shell =
2732 container_of(listener, struct desktop_shell,
2733 show_input_panel_listener);
2734 struct input_panel_surface *surface, *next;
2735 struct weston_surface *ws;
2737 shell->showing_input_panels = true;
2739 wl_list_insert(&shell->panel_layer.link,
2740 &shell->input_panel_layer.link);
2742 wl_list_for_each_safe(surface, next,
2743 &shell->input_panel.surfaces, link) {
2744 ws = surface->surface;
2745 wl_list_insert(&shell->input_panel_layer.surface_list,
2747 weston_surface_assign_output(ws);
2748 weston_surface_damage(ws);
2749 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2754 hide_input_panels(struct wl_listener *listener, void *data)
2756 struct desktop_shell *shell =
2757 container_of(listener, struct desktop_shell,
2758 hide_input_panel_listener);
2759 struct weston_surface *surface, *next;
2761 shell->showing_input_panels = false;
2763 wl_list_remove(&shell->input_panel_layer.link);
2765 wl_list_for_each_safe(surface, next,
2766 &shell->input_panel_layer.surface_list, layer_link)
2767 weston_surface_unmap(surface);
2771 center_on_output(struct weston_surface *surface, struct weston_output *output)
2773 GLfloat x = (output->width - surface->buffer->width) / 2;
2774 GLfloat y = (output->height - surface->buffer->height) / 2;
2776 weston_surface_configure(surface, output->x + x, output->y + y,
2777 surface->buffer->width,
2778 surface->buffer->height);
2782 weston_surface_set_initial_position (struct weston_surface *surface,
2783 struct desktop_shell *shell)
2785 struct weston_compositor *compositor = shell->compositor;
2787 int range_x, range_y;
2788 int dx, dy, x, y, panel_height;
2789 struct weston_output *output, *target_output = NULL;
2790 struct weston_seat *seat;
2792 /* As a heuristic place the new window on the same output as the
2793 * pointer. Falling back to the output containing 0, 0.
2795 * TODO: Do something clever for touch too?
2797 wl_list_for_each(seat, &compositor->seat_list, link) {
2798 if (seat->has_pointer) {
2799 ix = wl_fixed_to_int(seat->pointer.x);
2800 iy = wl_fixed_to_int(seat->pointer.y);
2805 wl_list_for_each(output, &compositor->output_list, link) {
2806 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2807 target_output = output;
2812 if (!target_output) {
2813 weston_surface_set_position(surface, 10 + random() % 400,
2814 10 + random() % 400);
2818 /* Valid range within output where the surface will still be onscreen.
2819 * If this is negative it means that the surface is bigger than
2822 panel_height = get_output_panel_height(shell, target_output);
2823 range_x = target_output->width - surface->geometry.width;
2824 range_y = (target_output->height - panel_height) -
2825 surface->geometry.height;
2828 dx = random() % range_x;
2833 dy = panel_height + random() % range_y;
2837 x = target_output->x + dx;
2838 y = target_output->y + dy;
2840 weston_surface_set_position (surface, x, y);
2844 map(struct desktop_shell *shell, struct weston_surface *surface,
2845 int32_t width, int32_t height, int32_t sx, int32_t sy)
2847 struct weston_compositor *compositor = shell->compositor;
2848 struct shell_surface *shsurf = get_shell_surface(surface);
2849 enum shell_surface_type surface_type = shsurf->type;
2850 struct weston_surface *parent;
2851 struct weston_seat *seat;
2852 struct workspace *ws;
2853 int panel_height = 0;
2855 surface->geometry.width = width;
2856 surface->geometry.height = height;
2857 surface->geometry.dirty = 1;
2859 /* initial positioning, see also configure() */
2860 switch (surface_type) {
2861 case SHELL_SURFACE_TOPLEVEL:
2862 weston_surface_set_initial_position(surface, shell);
2864 case SHELL_SURFACE_FULLSCREEN:
2865 center_on_output(surface, shsurf->fullscreen_output);
2866 shell_map_fullscreen(shsurf);
2868 case SHELL_SURFACE_MAXIMIZED:
2869 /* use surface configure to set the geometry */
2870 panel_height = get_output_panel_height(shell,surface->output);
2871 weston_surface_set_position(surface, shsurf->output->x,
2872 shsurf->output->y + panel_height);
2874 case SHELL_SURFACE_POPUP:
2875 shell_map_popup(shsurf);
2876 case SHELL_SURFACE_NONE:
2877 weston_surface_set_position(surface,
2878 surface->geometry.x + sx,
2879 surface->geometry.y + sy);
2885 /* surface stacking order, see also activate() */
2886 switch (surface_type) {
2887 case SHELL_SURFACE_POPUP:
2888 case SHELL_SURFACE_TRANSIENT:
2889 parent = shsurf->parent;
2890 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2892 case SHELL_SURFACE_FULLSCREEN:
2893 case SHELL_SURFACE_NONE:
2896 ws = get_current_workspace(shell);
2897 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2901 if (surface_type != SHELL_SURFACE_NONE) {
2902 weston_surface_assign_output(surface);
2903 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2904 surface->output = shsurf->output;
2907 switch (surface_type) {
2908 case SHELL_SURFACE_TRANSIENT:
2909 if (shsurf->transient.flags ==
2910 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2912 case SHELL_SURFACE_TOPLEVEL:
2913 case SHELL_SURFACE_FULLSCREEN:
2914 case SHELL_SURFACE_MAXIMIZED:
2915 if (!shell->locked) {
2916 wl_list_for_each(seat, &compositor->seat_list, link)
2917 activate(shell, surface, seat);
2924 if (surface_type == SHELL_SURFACE_TOPLEVEL)
2926 switch (shell->win_animation_type) {
2927 case ANIMATION_FADE:
2928 weston_fade_run(surface, NULL, NULL);
2930 case ANIMATION_ZOOM:
2931 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2940 configure(struct desktop_shell *shell, struct weston_surface *surface,
2941 GLfloat x, GLfloat y, int32_t width, int32_t height)
2943 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2944 struct shell_surface *shsurf;
2946 shsurf = get_shell_surface(surface);
2948 surface_type = shsurf->type;
2950 surface->geometry.x = x;
2951 surface->geometry.y = y;
2952 surface->geometry.width = width;
2953 surface->geometry.height = height;
2954 surface->geometry.dirty = 1;
2956 switch (surface_type) {
2957 case SHELL_SURFACE_FULLSCREEN:
2958 shell_stack_fullscreen(shsurf);
2959 shell_configure_fullscreen(shsurf);
2961 case SHELL_SURFACE_MAXIMIZED:
2962 /* setting x, y and using configure to change that geometry */
2963 surface->geometry.x = surface->output->x;
2964 surface->geometry.y = surface->output->y +
2965 get_output_panel_height(shell,surface->output);
2967 case SHELL_SURFACE_TOPLEVEL:
2973 /* XXX: would a fullscreen surface need the same handling? */
2974 if (surface->output) {
2975 weston_surface_assign_output(surface);
2977 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2978 surface->output = shsurf->output;
2983 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2985 struct shell_surface *shsurf = get_shell_surface(es);
2986 struct desktop_shell *shell = shsurf->shell;
2987 int type_changed = 0;
2989 if (shsurf->next_type != SHELL_SURFACE_NONE &&
2990 shsurf->type != shsurf->next_type) {
2991 set_surface_type(shsurf);
2995 if (!weston_surface_is_mapped(es)) {
2996 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
2997 } else if (type_changed || sx != 0 || sy != 0 ||
2998 es->geometry.width != es->buffer->width ||
2999 es->geometry.height != es->buffer->height) {
3000 GLfloat from_x, from_y;
3003 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3004 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3005 configure(shell, es,
3006 es->geometry.x + to_x - from_x,
3007 es->geometry.y + to_y - from_y,
3008 es->buffer->width, es->buffer->height);
3012 static int launch_desktop_shell_process(struct desktop_shell *shell);
3015 desktop_shell_sigchld(struct weston_process *process, int status)
3018 struct desktop_shell *shell =
3019 container_of(process, struct desktop_shell, child.process);
3021 shell->child.process.pid = 0;
3022 shell->child.client = NULL; /* already destroyed by wayland */
3024 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3025 time = weston_compositor_get_time();
3026 if (time - shell->child.deathstamp > 30000) {
3027 shell->child.deathstamp = time;
3028 shell->child.deathcount = 0;
3031 shell->child.deathcount++;
3032 if (shell->child.deathcount > 5) {
3033 weston_log("weston-desktop-shell died, giving up.\n");
3037 weston_log("weston-desktop-shell died, respawning...\n");
3038 launch_desktop_shell_process(shell);
3042 launch_desktop_shell_process(struct desktop_shell *shell)
3044 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3046 shell->child.client = weston_client_launch(shell->compositor,
3047 &shell->child.process,
3049 desktop_shell_sigchld);
3051 if (!shell->child.client)
3057 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3059 struct desktop_shell *shell = data;
3061 wl_client_add_object(client, &wl_shell_interface,
3062 &shell_implementation, id, shell);
3066 unbind_desktop_shell(struct wl_resource *resource)
3068 struct desktop_shell *shell = resource->data;
3071 resume_desktop(shell);
3073 shell->child.desktop_shell = NULL;
3074 shell->prepare_event_sent = false;
3079 bind_desktop_shell(struct wl_client *client,
3080 void *data, uint32_t version, uint32_t id)
3082 struct desktop_shell *shell = data;
3083 struct wl_resource *resource;
3085 resource = wl_client_add_object(client, &desktop_shell_interface,
3086 &desktop_shell_implementation,
3089 if (client == shell->child.client) {
3090 resource->destroy = unbind_desktop_shell;
3091 shell->child.desktop_shell = resource;
3095 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3096 "permission to bind desktop_shell denied");
3097 wl_resource_destroy(resource);
3101 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3103 struct desktop_shell *shell = surface->private;
3108 center_on_output(surface, surface->output);
3110 if (wl_list_empty(&surface->layer_link)) {
3111 wl_list_insert(shell->lock_layer.surface_list.prev,
3112 &surface->layer_link);
3113 weston_surface_assign_output(surface);
3114 shell->compositor->idle_time = shell->screensaver.duration;
3115 weston_compositor_wake(shell->compositor);
3116 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3121 screensaver_set_surface(struct wl_client *client,
3122 struct wl_resource *resource,
3123 struct wl_resource *surface_resource,
3124 struct wl_resource *output_resource)
3126 struct desktop_shell *shell = resource->data;
3127 struct weston_surface *surface = surface_resource->data;
3128 struct weston_output *output = output_resource->data;
3130 surface->configure = screensaver_configure;
3131 surface->private = shell;
3132 surface->output = output;
3135 static const struct screensaver_interface screensaver_implementation = {
3136 screensaver_set_surface
3140 unbind_screensaver(struct wl_resource *resource)
3142 struct desktop_shell *shell = resource->data;
3144 shell->screensaver.binding = NULL;
3149 bind_screensaver(struct wl_client *client,
3150 void *data, uint32_t version, uint32_t id)
3152 struct desktop_shell *shell = data;
3153 struct wl_resource *resource;
3155 resource = wl_client_add_object(client, &screensaver_interface,
3156 &screensaver_implementation,
3159 if (shell->screensaver.binding == NULL) {
3160 resource->destroy = unbind_screensaver;
3161 shell->screensaver.binding = resource;
3165 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3166 "interface object already bound");
3167 wl_resource_destroy(resource);
3171 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3173 struct weston_mode *mode = surface->output->current;
3174 GLfloat x = (mode->width - surface->buffer->width) / 2;
3175 GLfloat y = mode->height - surface->buffer->height;
3177 /* Don't map the input panel here, wait for
3178 * show_input_panels signal. */
3180 weston_surface_configure(surface,
3181 surface->output->x + x,
3182 surface->output->y + y,
3183 surface->buffer->width,
3184 surface->buffer->height);
3188 destroy_input_panel_surface(struct wl_listener *listener,
3191 struct input_panel_surface *input_panel_surface =
3192 container_of(listener, struct input_panel_surface, listener);
3194 wl_list_remove(&listener->link);
3195 wl_list_remove(&input_panel_surface->link);
3197 free(input_panel_surface);
3201 input_panel_set_surface(struct wl_client *client,
3202 struct wl_resource *resource,
3203 struct wl_resource *surface_resource,
3204 struct wl_resource *output_resource)
3206 struct desktop_shell *shell = resource->data;
3207 struct weston_surface *surface = surface_resource->data;
3208 struct weston_output *output = output_resource->data;
3209 struct input_panel_surface *input_panel_surface;
3211 surface->configure = input_panel_configure;
3212 surface->private = shell;
3213 surface->output = output;
3215 input_panel_surface = malloc(sizeof *input_panel_surface);
3216 if (!input_panel_surface) {
3217 wl_resource_post_no_memory(resource);
3221 input_panel_surface->surface = surface;
3222 input_panel_surface->listener.notify = destroy_input_panel_surface;
3224 wl_signal_add(&surface_resource->destroy_signal,
3225 &input_panel_surface->listener);
3227 wl_list_insert(&shell->input_panel.surfaces,
3228 &input_panel_surface->link);
3231 static const struct input_panel_interface input_panel_implementation = {
3232 input_panel_set_surface
3236 unbind_input_panel(struct wl_resource *resource)
3238 struct desktop_shell *shell = resource->data;
3240 shell->input_panel.binding = NULL;
3245 bind_input_panel(struct wl_client *client,
3246 void *data, uint32_t version, uint32_t id)
3248 struct desktop_shell *shell = data;
3249 struct wl_resource *resource;
3251 resource = wl_client_add_object(client, &input_panel_interface,
3252 &input_panel_implementation,
3255 if (shell->input_panel.binding == NULL) {
3256 resource->destroy = unbind_input_panel;
3257 shell->input_panel.binding = resource;
3261 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3262 "interface object already bound");
3263 wl_resource_destroy(resource);
3267 struct desktop_shell *shell;
3268 struct weston_surface *current;
3269 struct wl_listener listener;
3270 struct wl_keyboard_grab grab;
3274 switcher_next(struct switcher *switcher)
3276 struct weston_surface *surface;
3277 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3278 struct shell_surface *shsurf;
3279 struct workspace *ws = get_current_workspace(switcher->shell);
3281 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3282 switch (get_shell_surface_type(surface)) {
3283 case SHELL_SURFACE_TOPLEVEL:
3284 case SHELL_SURFACE_FULLSCREEN:
3285 case SHELL_SURFACE_MAXIMIZED:
3288 if (prev == switcher->current)
3291 surface->alpha = 0.25;
3292 surface->geometry.dirty = 1;
3293 weston_surface_damage(surface);
3299 if (is_black_surface(surface, NULL)) {
3300 surface->alpha = 0.25;
3301 surface->geometry.dirty = 1;
3302 weston_surface_damage(surface);
3312 wl_list_remove(&switcher->listener.link);
3313 wl_signal_add(&next->surface.resource.destroy_signal,
3314 &switcher->listener);
3316 switcher->current = next;
3319 shsurf = get_shell_surface(switcher->current);
3320 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3321 shsurf->fullscreen.black_surface->alpha = 1.0;
3325 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3327 struct switcher *switcher =
3328 container_of(listener, struct switcher, listener);
3330 switcher_next(switcher);
3334 switcher_destroy(struct switcher *switcher)
3336 struct weston_surface *surface;
3337 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3338 struct workspace *ws = get_current_workspace(switcher->shell);
3340 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3341 surface->alpha = 1.0;
3342 weston_surface_damage(surface);
3345 if (switcher->current)
3346 activate(switcher->shell, switcher->current,
3347 (struct weston_seat *) keyboard->seat);
3348 wl_list_remove(&switcher->listener.link);
3349 wl_keyboard_end_grab(keyboard);
3354 switcher_key(struct wl_keyboard_grab *grab,
3355 uint32_t time, uint32_t key, uint32_t state_w)
3357 struct switcher *switcher = container_of(grab, struct switcher, grab);
3358 enum wl_keyboard_key_state state = state_w;
3360 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3361 switcher_next(switcher);
3365 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3366 uint32_t mods_depressed, uint32_t mods_latched,
3367 uint32_t mods_locked, uint32_t group)
3369 struct switcher *switcher = container_of(grab, struct switcher, grab);
3370 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3372 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3373 switcher_destroy(switcher);
3376 static const struct wl_keyboard_grab_interface switcher_grab = {
3382 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3385 struct desktop_shell *shell = data;
3386 struct switcher *switcher;
3388 switcher = malloc(sizeof *switcher);
3389 switcher->shell = shell;
3390 switcher->current = NULL;
3391 switcher->listener.notify = switcher_handle_surface_destroy;
3392 wl_list_init(&switcher->listener.link);
3394 lower_fullscreen_layer(switcher->shell);
3395 switcher->grab.interface = &switcher_grab;
3396 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3397 wl_keyboard_set_focus(seat->keyboard, NULL);
3398 switcher_next(switcher);
3402 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3405 struct weston_compositor *compositor = data;
3406 struct weston_output *output;
3407 long backlight_new = 0;
3409 /* TODO: we're limiting to simple use cases, where we assume just
3410 * control on the primary display. We'd have to extend later if we
3411 * ever get support for setting backlights on random desktop LCD
3413 output = get_default_output(compositor);
3417 if (!output->set_backlight)
3420 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3421 backlight_new = output->backlight_current - 25;
3422 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3423 backlight_new = output->backlight_current + 25;
3425 if (backlight_new < 5)
3427 if (backlight_new > 255)
3428 backlight_new = 255;
3430 output->backlight_current = backlight_new;
3431 output->set_backlight(output, output->backlight_current);
3435 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3438 struct desktop_shell *shell = data;
3439 struct weston_compositor *compositor = shell->compositor;
3440 struct weston_surface *surface;
3441 struct weston_plane plane;
3443 if (shell->debug_repaint_surface) {
3444 weston_surface_destroy(shell->debug_repaint_surface);
3445 shell->debug_repaint_surface = NULL;
3447 surface = weston_surface_create(compositor);
3448 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3449 weston_surface_configure(surface, 0, 0, 8192, 8192);
3450 wl_list_insert(&compositor->fade_layer.surface_list,
3451 &surface->layer_link);
3452 weston_surface_assign_output(surface);
3453 pixman_region32_init(&surface->input);
3455 /* Here's the dirty little trick that makes the
3456 * repaint debugging work: we move the surface to a
3457 * different plane and force an update_transform to
3458 * update dependent state and clear the
3459 * geometry.dirty bit. This way the call to
3460 * damage_below() in update_transform() does not
3461 * add damage to the primary plane. */
3463 weston_plane_init(&plane, 0, 0);
3464 surface->plane = &plane;
3465 weston_surface_update_transform(surface);
3466 shell->debug_repaint_surface = surface;
3467 surface->plane = &compositor->primary_plane;
3468 weston_plane_release(&plane);
3474 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3477 struct desktop_shell *shell = data;
3478 struct weston_compositor *compositor = shell->compositor;
3479 compositor->fan_debug = !compositor->fan_debug;
3480 weston_compositor_damage_all(compositor);
3484 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3487 struct wl_surface *focus_surface;
3488 struct wl_client *client;
3493 focus_surface = seat->keyboard->focus;
3497 client = focus_surface->resource.client;
3498 wl_client_get_credentials(client, &pid, &uid, &gid);
3504 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3505 uint32_t key, void *data)
3507 struct desktop_shell *shell = data;
3508 unsigned int new_index = shell->workspaces.current;
3515 change_workspace(shell, new_index);
3519 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3520 uint32_t key, void *data)
3522 struct desktop_shell *shell = data;
3523 unsigned int new_index = shell->workspaces.current;
3527 if (new_index < shell->workspaces.num - 1)
3530 change_workspace(shell, new_index);
3534 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3535 uint32_t key, void *data)
3537 struct desktop_shell *shell = data;
3538 unsigned int new_index;
3542 new_index = key - KEY_F1;
3543 if (new_index >= shell->workspaces.num)
3544 new_index = shell->workspaces.num - 1;
3546 change_workspace(shell, new_index);
3550 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3551 uint32_t key, void *data)
3553 struct desktop_shell *shell = data;
3554 unsigned int new_index = shell->workspaces.current;
3562 take_surface_to_workspace_by_seat(shell, seat, new_index);
3566 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3567 uint32_t key, void *data)
3569 struct desktop_shell *shell = data;
3570 unsigned int new_index = shell->workspaces.current;
3575 if (new_index < shell->workspaces.num - 1)
3578 take_surface_to_workspace_by_seat(shell, seat, new_index);
3582 shell_destroy(struct wl_listener *listener, void *data)
3584 struct desktop_shell *shell =
3585 container_of(listener, struct desktop_shell, destroy_listener);
3586 struct workspace **ws;
3588 if (shell->child.client)
3589 wl_client_destroy(shell->child.client);
3591 wl_list_remove(&shell->lock_listener.link);
3592 wl_list_remove(&shell->unlock_listener.link);
3593 wl_list_remove(&shell->show_input_panel_listener.link);
3594 wl_list_remove(&shell->hide_input_panel_listener.link);
3596 wl_array_for_each(ws, &shell->workspaces.array)
3597 workspace_destroy(*ws);
3598 wl_array_release(&shell->workspaces.array);
3600 free(shell->screensaver.path);
3605 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3608 int i, num_workspace_bindings;
3610 /* fixed bindings */
3611 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3612 MODIFIER_CTRL | MODIFIER_ALT,
3613 terminate_binding, ec);
3614 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3615 click_to_activate_binding,
3617 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3618 MODIFIER_SUPER | MODIFIER_ALT,
3619 surface_opacity_binding, NULL);
3620 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3621 MODIFIER_SUPER, zoom_axis_binding,
3624 /* configurable bindings */
3625 mod = shell->binding_modifier;
3626 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3627 zoom_key_binding, NULL);
3628 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3629 zoom_key_binding, NULL);
3630 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3632 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3633 resize_binding, shell);
3634 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3635 rotate_binding, NULL);
3636 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3638 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3640 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3641 backlight_binding, ec);
3642 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3644 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3645 backlight_binding, ec);
3646 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3647 debug_repaint_binding, shell);
3648 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3649 fan_debug_repaint_binding, shell);
3650 weston_compositor_add_key_binding(ec, KEY_K, mod,
3651 force_kill_binding, shell);
3652 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3653 workspace_up_binding, shell);
3654 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3655 workspace_down_binding, shell);
3656 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3657 workspace_move_surface_up_binding,
3659 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3660 workspace_move_surface_down_binding,
3663 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3664 if (shell->workspaces.num > 1) {
3665 num_workspace_bindings = shell->workspaces.num;
3666 if (num_workspace_bindings > 6)
3667 num_workspace_bindings = 6;
3668 for (i = 0; i < num_workspace_bindings; i++)
3669 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3670 workspace_f_binding,
3676 shell_init(struct weston_compositor *ec);
3679 shell_init(struct weston_compositor *ec)
3681 struct weston_seat *seat;
3682 struct desktop_shell *shell;
3683 struct workspace **pws;
3686 shell = malloc(sizeof *shell);
3690 memset(shell, 0, sizeof *shell);
3691 shell->compositor = ec;
3693 shell->destroy_listener.notify = shell_destroy;
3694 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3695 shell->lock_listener.notify = lock;
3696 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3697 shell->unlock_listener.notify = unlock;
3698 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3699 shell->show_input_panel_listener.notify = show_input_panels;
3700 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3701 shell->hide_input_panel_listener.notify = hide_input_panels;
3702 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3703 ec->ping_handler = ping_handler;
3704 ec->shell_interface.shell = shell;
3705 ec->shell_interface.create_shell_surface = create_shell_surface;
3706 ec->shell_interface.set_toplevel = set_toplevel;
3707 ec->shell_interface.set_transient = set_transient;
3708 ec->shell_interface.move = surface_move;
3709 ec->shell_interface.resize = surface_resize;
3711 wl_list_init(&shell->screensaver.surfaces);
3712 wl_list_init(&shell->input_panel.surfaces);
3714 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3715 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3716 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3717 weston_layer_init(&shell->lock_layer, NULL);
3718 weston_layer_init(&shell->input_panel_layer, NULL);
3720 wl_array_init(&shell->workspaces.array);
3721 wl_list_init(&shell->workspaces.client_list);
3723 shell_configuration(shell);
3725 for (i = 0; i < shell->workspaces.num; i++) {
3726 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3730 *pws = workspace_create();
3734 activate_workspace(shell, 0);
3736 wl_list_init(&shell->workspaces.anim_sticky_list);
3737 wl_list_init(&shell->workspaces.animation.link);
3738 shell->workspaces.animation.frame = animate_workspace_change_frame;
3740 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3741 shell, bind_shell) == NULL)
3744 if (wl_display_add_global(ec->wl_display,
3745 &desktop_shell_interface,
3746 shell, bind_desktop_shell) == NULL)
3749 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3750 shell, bind_screensaver) == NULL)
3753 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3754 shell, bind_input_panel) == NULL)
3757 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3758 shell, bind_workspace_manager) == NULL)
3761 shell->child.deathstamp = weston_compositor_get_time();
3762 if (launch_desktop_shell_process(shell) != 0)
3765 wl_list_for_each(seat, &ec->seat_list, link)
3766 create_pointer_focus_listener(seat);
3768 shell_add_bindings(ec, shell);