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 surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
245 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
247 struct desktop_shell *shell;
248 struct weston_surface *top_fs_es;
250 shell = shell_surface_get_shell(shsurf);
252 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
255 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
256 struct weston_surface,
258 return (shsurf == get_shell_surface(top_fs_es));
262 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
264 struct shell_grab *grab;
266 grab = container_of(listener, struct shell_grab,
267 shsurf_destroy_listener);
273 popup_grab_end(struct wl_pointer *pointer);
276 shell_grab_start(struct shell_grab *grab,
277 const struct wl_pointer_grab_interface *interface,
278 struct shell_surface *shsurf,
279 struct wl_pointer *pointer,
280 enum desktop_shell_cursor cursor)
282 struct desktop_shell *shell = shsurf->shell;
284 popup_grab_end(pointer);
286 grab->grab.interface = interface;
287 grab->shsurf = shsurf;
288 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
289 wl_signal_add(&shsurf->resource.destroy_signal,
290 &grab->shsurf_destroy_listener);
292 grab->pointer = pointer;
293 grab->grab.focus = &shsurf->surface->surface;
295 wl_pointer_start_grab(pointer, &grab->grab);
296 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
297 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
298 wl_fixed_from_int(0), wl_fixed_from_int(0));
302 shell_grab_end(struct shell_grab *grab)
305 wl_list_remove(&grab->shsurf_destroy_listener.link);
307 wl_pointer_end_grab(grab->pointer);
311 center_on_output(struct weston_surface *surface,
312 struct weston_output *output);
314 static enum weston_keyboard_modifier
315 get_modifier(char *modifier)
318 return MODIFIER_SUPER;
320 if (!strcmp("ctrl", modifier))
321 return MODIFIER_CTRL;
322 else if (!strcmp("alt", modifier))
324 else if (!strcmp("super", modifier))
325 return MODIFIER_SUPER;
327 return MODIFIER_SUPER;
330 static enum animation_type
331 get_animation_type(char *animation)
334 return ANIMATION_NONE;
336 if (!strcmp("zoom", animation))
337 return ANIMATION_ZOOM;
338 else if (!strcmp("fade", animation))
339 return ANIMATION_FADE;
341 return ANIMATION_NONE;
345 shell_configuration(struct desktop_shell *shell)
350 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
351 char *modifier = NULL;
352 char *win_animation = NULL;
354 struct config_key shell_keys[] = {
355 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
356 { "animation", CONFIG_KEY_STRING, &win_animation},
358 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
361 struct config_key saver_keys[] = {
362 { "path", CONFIG_KEY_STRING, &path },
363 { "duration", CONFIG_KEY_INTEGER, &duration },
366 struct config_section cs[] = {
367 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
368 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
371 config_file = config_file_path("weston.ini");
372 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
375 shell->screensaver.path = path;
376 shell->screensaver.duration = duration;
377 shell->binding_modifier = get_modifier(modifier);
378 shell->win_animation_type = get_animation_type(win_animation);
379 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
383 focus_state_destroy(struct focus_state *state)
385 wl_list_remove(&state->seat_destroy_listener.link);
386 wl_list_remove(&state->surface_destroy_listener.link);
391 focus_state_seat_destroy(struct wl_listener *listener, void *data)
393 struct focus_state *state = container_of(listener,
395 seat_destroy_listener);
397 wl_list_remove(&state->link);
398 focus_state_destroy(state);
402 focus_state_surface_destroy(struct wl_listener *listener, void *data)
404 struct focus_state *state = container_of(listener,
406 surface_destroy_listener);
407 struct desktop_shell *shell;
408 struct weston_surface *surface, *next;
411 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
412 if (surface == state->keyboard_focus)
420 shell = state->seat->compositor->shell_interface.shell;
421 activate(shell, next, state->seat);
423 wl_list_remove(&state->link);
424 focus_state_destroy(state);
428 static struct focus_state *
429 focus_state_create(struct weston_seat *seat, struct workspace *ws)
431 struct focus_state *state;
433 state = malloc(sizeof *state);
439 wl_list_insert(&ws->focus_list, &state->link);
441 state->seat_destroy_listener.notify = focus_state_seat_destroy;
442 state->surface_destroy_listener.notify = focus_state_surface_destroy;
443 wl_signal_add(&seat->seat.destroy_signal,
444 &state->seat_destroy_listener);
445 wl_list_init(&state->surface_destroy_listener.link);
450 static struct focus_state *
451 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
453 struct workspace *ws = get_current_workspace(shell);
454 struct focus_state *state;
456 wl_list_for_each(state, &ws->focus_list, link)
457 if (state->seat == seat)
460 if (&state->link == &ws->focus_list)
461 state = focus_state_create(seat, ws);
467 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
469 struct focus_state *state, *next;
470 struct wl_surface *surface;
472 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
473 surface = state->keyboard_focus ?
474 &state->keyboard_focus->surface : NULL;
476 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
481 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
482 struct weston_seat *seat)
484 struct focus_state *state;
485 struct wl_surface *surface;
487 wl_list_for_each(state, &ws->focus_list, link) {
488 if (state->seat == seat) {
489 surface = seat->seat.keyboard->focus;
490 state->keyboard_focus =
491 (struct weston_surface *) surface;
498 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
499 struct weston_surface *surface)
501 struct focus_state *state;
503 wl_list_for_each(state, &ws->focus_list, link)
504 if (state->keyboard_focus == surface)
505 state->keyboard_focus = NULL;
509 workspace_destroy(struct workspace *ws)
511 struct focus_state *state, *next;
513 wl_list_for_each_safe(state, next, &ws->focus_list, link)
514 focus_state_destroy(state);
520 seat_destroyed(struct wl_listener *listener, void *data)
522 struct weston_seat *seat = data;
523 struct focus_state *state, *next;
524 struct workspace *ws = container_of(listener,
526 seat_destroyed_listener);
528 wl_list_for_each_safe(state, next, &ws->focus_list, link)
529 if (state->seat == seat)
530 wl_list_remove(&state->link);
533 static struct workspace *
534 workspace_create(void)
536 struct workspace *ws = malloc(sizeof *ws);
540 weston_layer_init(&ws->layer, NULL);
542 wl_list_init(&ws->focus_list);
543 wl_list_init(&ws->seat_destroyed_listener.link);
544 ws->seat_destroyed_listener.notify = seat_destroyed;
550 workspace_is_empty(struct workspace *ws)
552 return wl_list_empty(&ws->layer.surface_list);
555 static struct workspace *
556 get_workspace(struct desktop_shell *shell, unsigned int index)
558 struct workspace **pws = shell->workspaces.array.data;
559 assert(index < shell->workspaces.num);
564 static struct workspace *
565 get_current_workspace(struct desktop_shell *shell)
567 return get_workspace(shell, shell->workspaces.current);
571 activate_workspace(struct desktop_shell *shell, unsigned int index)
573 struct workspace *ws;
575 ws = get_workspace(shell, index);
576 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
578 shell->workspaces.current = index;
582 get_output_height(struct weston_output *output)
584 return abs(output->region.extents.y1 - output->region.extents.y2);
588 surface_translate(struct weston_surface *surface, double d)
590 struct shell_surface *shsurf = get_shell_surface(surface);
591 struct weston_transform *transform;
593 transform = &shsurf->workspace_transform;
594 if (wl_list_empty(&transform->link))
595 wl_list_insert(surface->geometry.transformation_list.prev,
596 &shsurf->workspace_transform.link);
598 weston_matrix_init(&shsurf->workspace_transform.matrix);
599 weston_matrix_translate(&shsurf->workspace_transform.matrix,
601 surface->geometry.dirty = 1;
605 workspace_translate_out(struct workspace *ws, double fraction)
607 struct weston_surface *surface;
611 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612 height = get_output_height(surface->output);
613 d = height * fraction;
615 surface_translate(surface, d);
620 workspace_translate_in(struct workspace *ws, double fraction)
622 struct weston_surface *surface;
626 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
627 height = get_output_height(surface->output);
630 d = -(height - height * fraction);
632 d = height + height * fraction;
634 surface_translate(surface, d);
639 broadcast_current_workspace_state(struct desktop_shell *shell)
641 struct wl_resource *resource;
643 wl_list_for_each(resource, &shell->workspaces.client_list, link)
644 workspace_manager_send_state(resource,
645 shell->workspaces.current,
646 shell->workspaces.num);
650 reverse_workspace_change_animation(struct desktop_shell *shell,
652 struct workspace *from,
653 struct workspace *to)
655 shell->workspaces.current = index;
657 shell->workspaces.anim_to = to;
658 shell->workspaces.anim_from = from;
659 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
660 shell->workspaces.anim_timestamp = 0;
662 weston_compositor_schedule_repaint(shell->compositor);
666 workspace_deactivate_transforms(struct workspace *ws)
668 struct weston_surface *surface;
669 struct shell_surface *shsurf;
671 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
672 shsurf = get_shell_surface(surface);
673 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
674 wl_list_remove(&shsurf->workspace_transform.link);
675 wl_list_init(&shsurf->workspace_transform.link);
677 shsurf->surface->geometry.dirty = 1;
682 finish_workspace_change_animation(struct desktop_shell *shell,
683 struct workspace *from,
684 struct workspace *to)
686 weston_compositor_schedule_repaint(shell->compositor);
688 wl_list_remove(&shell->workspaces.animation.link);
689 workspace_deactivate_transforms(from);
690 workspace_deactivate_transforms(to);
691 shell->workspaces.anim_to = NULL;
693 wl_list_remove(&shell->workspaces.anim_from->layer.link);
697 animate_workspace_change_frame(struct weston_animation *animation,
698 struct weston_output *output, uint32_t msecs)
700 struct desktop_shell *shell =
701 container_of(animation, struct desktop_shell,
702 workspaces.animation);
703 struct workspace *from = shell->workspaces.anim_from;
704 struct workspace *to = shell->workspaces.anim_to;
708 if (workspace_is_empty(from) && workspace_is_empty(to)) {
709 finish_workspace_change_animation(shell, from, to);
713 if (shell->workspaces.anim_timestamp == 0) {
714 if (shell->workspaces.anim_current == 0.0)
715 shell->workspaces.anim_timestamp = msecs;
717 shell->workspaces.anim_timestamp =
719 /* Invers of movement function 'y' below. */
720 (asin(1.0 - shell->workspaces.anim_current) *
721 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
725 t = msecs - shell->workspaces.anim_timestamp;
731 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
734 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
735 weston_compositor_schedule_repaint(shell->compositor);
737 workspace_translate_out(from, shell->workspaces.anim_dir * y);
738 workspace_translate_in(to, shell->workspaces.anim_dir * y);
739 shell->workspaces.anim_current = y;
741 weston_compositor_schedule_repaint(shell->compositor);
744 finish_workspace_change_animation(shell, from, to);
748 animate_workspace_change(struct desktop_shell *shell,
750 struct workspace *from,
751 struct workspace *to)
753 struct weston_output *output;
757 if (index > shell->workspaces.current)
762 shell->workspaces.current = index;
764 shell->workspaces.anim_dir = dir;
765 shell->workspaces.anim_from = from;
766 shell->workspaces.anim_to = to;
767 shell->workspaces.anim_current = 0.0;
768 shell->workspaces.anim_timestamp = 0;
770 output = container_of(shell->compositor->output_list.next,
771 struct weston_output, link);
772 wl_list_insert(&output->animation_list,
773 &shell->workspaces.animation.link);
775 wl_list_insert(from->layer.link.prev, &to->layer.link);
777 workspace_translate_in(to, 0);
779 restore_focus_state(shell, to);
781 weston_compositor_schedule_repaint(shell->compositor);
785 update_workspace(struct desktop_shell *shell, unsigned int index,
786 struct workspace *from, struct workspace *to)
788 shell->workspaces.current = index;
789 wl_list_insert(&from->layer.link, &to->layer.link);
790 wl_list_remove(&from->layer.link);
794 change_workspace(struct desktop_shell *shell, unsigned int index)
796 struct workspace *from;
797 struct workspace *to;
799 if (index == shell->workspaces.current)
802 /* Don't change workspace when there is any fullscreen surfaces. */
803 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
806 from = get_current_workspace(shell);
807 to = get_workspace(shell, index);
809 if (shell->workspaces.anim_from == to &&
810 shell->workspaces.anim_to == from) {
811 restore_focus_state(shell, to);
812 reverse_workspace_change_animation(shell, index, from, to);
813 broadcast_current_workspace_state(shell);
817 if (shell->workspaces.anim_to != NULL)
818 finish_workspace_change_animation(shell,
819 shell->workspaces.anim_from,
820 shell->workspaces.anim_to);
822 restore_focus_state(shell, to);
824 if (workspace_is_empty(to) && workspace_is_empty(from))
825 update_workspace(shell, index, from, to);
827 animate_workspace_change(shell, index, from, to);
829 broadcast_current_workspace_state(shell);
833 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
835 struct wl_list *list = &ws->layer.surface_list;
838 if (wl_list_empty(list))
846 return container_of(e, struct weston_surface, layer_link) == surface;
850 move_surface_to_workspace(struct desktop_shell *shell,
851 struct weston_surface *surface,
854 struct workspace *from;
855 struct workspace *to;
856 struct weston_seat *seat;
858 if (workspace == shell->workspaces.current)
861 if (workspace >= shell->workspaces.num)
862 workspace = shell->workspaces.num - 1;
864 from = get_current_workspace(shell);
865 to = get_workspace(shell, workspace);
867 wl_list_remove(&surface->layer_link);
868 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
870 drop_focus_state(shell, from, surface);
871 wl_list_for_each(seat, &shell->compositor->seat_list, link)
872 if (seat->has_keyboard &&
873 seat->keyboard.focus == &surface->surface)
874 wl_keyboard_set_focus(&seat->keyboard, NULL);
876 weston_surface_damage_below(surface);
880 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
881 struct wl_seat *wl_seat,
884 struct weston_seat *seat = (struct weston_seat *) wl_seat;
885 struct weston_surface *surface =
886 (struct weston_surface *) wl_seat->keyboard->focus;
887 struct shell_surface *shsurf;
888 struct workspace *from;
889 struct workspace *to;
890 struct focus_state *state;
892 if (surface == NULL ||
893 index == shell->workspaces.current)
896 from = get_current_workspace(shell);
897 to = get_workspace(shell, index);
899 wl_list_remove(&surface->layer_link);
900 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
902 replace_focus_state(shell, to, seat);
903 drop_focus_state(shell, from, surface);
905 if (shell->workspaces.anim_from == to &&
906 shell->workspaces.anim_to == from) {
907 wl_list_remove(&to->layer.link);
908 wl_list_insert(from->layer.link.prev, &to->layer.link);
910 reverse_workspace_change_animation(shell, index, from, to);
911 broadcast_current_workspace_state(shell);
916 if (shell->workspaces.anim_to != NULL)
917 finish_workspace_change_animation(shell,
918 shell->workspaces.anim_from,
919 shell->workspaces.anim_to);
921 if (workspace_is_empty(from) &&
922 workspace_has_only(to, surface))
923 update_workspace(shell, index, from, to);
925 shsurf = get_shell_surface(surface);
926 if (wl_list_empty(&shsurf->workspace_transform.link))
927 wl_list_insert(&shell->workspaces.anim_sticky_list,
928 &shsurf->workspace_transform.link);
930 animate_workspace_change(shell, index, from, to);
933 broadcast_current_workspace_state(shell);
935 state = ensure_focus_state(shell, seat);
937 state->keyboard_focus = surface;
941 workspace_manager_move_surface(struct wl_client *client,
942 struct wl_resource *resource,
943 struct wl_resource *surface_resource,
946 struct desktop_shell *shell = resource->data;
947 struct weston_surface *surface =
948 (struct weston_surface *) surface_resource;
950 move_surface_to_workspace(shell, surface, workspace);
953 static const struct workspace_manager_interface workspace_manager_implementation = {
954 workspace_manager_move_surface,
958 unbind_resource(struct wl_resource *resource)
960 wl_list_remove(&resource->link);
965 bind_workspace_manager(struct wl_client *client,
966 void *data, uint32_t version, uint32_t id)
968 struct desktop_shell *shell = data;
969 struct wl_resource *resource;
971 resource = wl_client_add_object(client, &workspace_manager_interface,
972 &workspace_manager_implementation,
975 if (resource == NULL) {
976 weston_log("couldn't add workspace manager object");
980 resource->destroy = unbind_resource;
981 wl_list_insert(&shell->workspaces.client_list, &resource->link);
983 workspace_manager_send_state(resource,
984 shell->workspaces.current,
985 shell->workspaces.num);
989 noop_grab_focus(struct wl_pointer_grab *grab,
990 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
996 move_grab_motion(struct wl_pointer_grab *grab,
997 uint32_t time, wl_fixed_t x, wl_fixed_t y)
999 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1000 struct wl_pointer *pointer = grab->pointer;
1001 struct shell_surface *shsurf = move->base.shsurf;
1002 struct weston_surface *es;
1003 int dx = wl_fixed_to_int(pointer->x + move->dx);
1004 int dy = wl_fixed_to_int(pointer->y + move->dy);
1009 es = shsurf->surface;
1011 weston_surface_configure(es, dx, dy,
1012 es->geometry.width, es->geometry.height);
1014 weston_compositor_schedule_repaint(es->compositor);
1018 move_grab_button(struct wl_pointer_grab *grab,
1019 uint32_t time, uint32_t button, uint32_t state_w)
1021 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1023 struct wl_pointer *pointer = grab->pointer;
1024 enum wl_pointer_button_state state = state_w;
1026 if (pointer->button_count == 0 &&
1027 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1028 shell_grab_end(shell_grab);
1033 static const struct wl_pointer_grab_interface move_grab_interface = {
1040 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1042 struct weston_move_grab *move;
1047 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1050 move = malloc(sizeof *move);
1054 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1055 ws->seat.pointer->grab_x;
1056 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1057 ws->seat.pointer->grab_y;
1059 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1060 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1066 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1067 struct wl_resource *seat_resource, uint32_t serial)
1069 struct weston_seat *ws = seat_resource->data;
1070 struct shell_surface *shsurf = resource->data;
1072 if (ws->seat.pointer->button_count == 0 ||
1073 ws->seat.pointer->grab_serial != serial ||
1074 ws->seat.pointer->focus != &shsurf->surface->surface)
1077 if (surface_move(shsurf, ws) < 0)
1078 wl_resource_post_no_memory(resource);
1081 struct weston_resize_grab {
1082 struct shell_grab base;
1084 int32_t width, height;
1088 resize_grab_motion(struct wl_pointer_grab *grab,
1089 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1091 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1092 struct wl_pointer *pointer = grab->pointer;
1093 struct shell_surface *shsurf = resize->base.shsurf;
1094 int32_t width, height;
1095 wl_fixed_t from_x, from_y;
1096 wl_fixed_t to_x, to_y;
1101 weston_surface_from_global_fixed(shsurf->surface,
1102 pointer->grab_x, pointer->grab_y,
1104 weston_surface_from_global_fixed(shsurf->surface,
1105 pointer->x, pointer->y, &to_x, &to_y);
1107 width = resize->width;
1108 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1109 width += wl_fixed_to_int(from_x - to_x);
1110 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1111 width += wl_fixed_to_int(to_x - from_x);
1114 height = resize->height;
1115 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1116 height += wl_fixed_to_int(from_y - to_y);
1117 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1118 height += wl_fixed_to_int(to_y - from_y);
1121 shsurf->client->send_configure(shsurf->surface,
1122 resize->edges, width, height);
1126 send_configure(struct weston_surface *surface,
1127 uint32_t edges, int32_t width, int32_t height)
1129 struct shell_surface *shsurf = get_shell_surface(surface);
1131 wl_shell_surface_send_configure(&shsurf->resource,
1132 edges, width, height);
1135 static const struct weston_shell_client shell_client = {
1140 resize_grab_button(struct wl_pointer_grab *grab,
1141 uint32_t time, uint32_t button, uint32_t state_w)
1143 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1144 struct wl_pointer *pointer = grab->pointer;
1145 enum wl_pointer_button_state state = state_w;
1147 if (pointer->button_count == 0 &&
1148 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1149 shell_grab_end(&resize->base);
1154 static const struct wl_pointer_grab_interface resize_grab_interface = {
1161 surface_resize(struct shell_surface *shsurf,
1162 struct weston_seat *ws, uint32_t edges)
1164 struct weston_resize_grab *resize;
1166 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1169 if (edges == 0 || edges > 15 ||
1170 (edges & 3) == 3 || (edges & 12) == 12)
1173 resize = malloc(sizeof *resize);
1177 resize->edges = edges;
1178 resize->width = shsurf->surface->geometry.width;
1179 resize->height = shsurf->surface->geometry.height;
1181 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1182 ws->seat.pointer, edges);
1188 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1189 struct wl_resource *seat_resource, uint32_t serial,
1192 struct weston_seat *ws = seat_resource->data;
1193 struct shell_surface *shsurf = resource->data;
1195 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1198 if (ws->seat.pointer->button_count == 0 ||
1199 ws->seat.pointer->grab_serial != serial ||
1200 ws->seat.pointer->focus != &shsurf->surface->surface)
1203 if (surface_resize(shsurf, ws, edges) < 0)
1204 wl_resource_post_no_memory(resource);
1208 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1209 struct wl_surface *surface, int32_t x, int32_t y)
1211 struct shell_grab *grab = (struct shell_grab *) base;
1213 if (grab->grab.focus != surface) {
1214 shell_grab_end(grab);
1220 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1221 uint32_t time, int32_t x, int32_t y)
1226 busy_cursor_grab_button(struct wl_pointer_grab *base,
1227 uint32_t time, uint32_t button, uint32_t state)
1229 struct shell_grab *grab = (struct shell_grab *) base;
1230 struct shell_surface *shsurf;
1231 struct weston_surface *surface =
1232 (struct weston_surface *) grab->grab.pointer->current;
1233 struct weston_seat *seat =
1234 (struct weston_seat *) grab->grab.pointer->seat;
1236 shsurf = get_shell_surface(surface);
1237 if (shsurf && button == BTN_LEFT && state) {
1238 activate(shsurf->shell, shsurf->surface, seat);
1239 surface_move(shsurf, seat);
1240 } else if (shsurf && button == BTN_RIGHT && state) {
1241 activate(shsurf->shell, shsurf->surface, seat);
1242 surface_rotate(shsurf, &seat->seat);
1246 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1247 busy_cursor_grab_focus,
1248 busy_cursor_grab_motion,
1249 busy_cursor_grab_button,
1253 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1255 struct shell_grab *grab;
1257 grab = malloc(sizeof *grab);
1261 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1262 DESKTOP_SHELL_CURSOR_BUSY);
1266 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1268 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1270 if (grab->grab.interface == &busy_cursor_grab_interface) {
1271 shell_grab_end(grab);
1277 ping_timer_destroy(struct shell_surface *shsurf)
1279 if (!shsurf || !shsurf->ping_timer)
1282 if (shsurf->ping_timer->source)
1283 wl_event_source_remove(shsurf->ping_timer->source);
1285 free(shsurf->ping_timer);
1286 shsurf->ping_timer = NULL;
1290 ping_timeout_handler(void *data)
1292 struct shell_surface *shsurf = data;
1293 struct weston_seat *seat;
1295 /* Client is not responding */
1296 shsurf->unresponsive = 1;
1298 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1299 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1300 set_busy_cursor(shsurf, seat->seat.pointer);
1306 ping_handler(struct weston_surface *surface, uint32_t serial)
1308 struct shell_surface *shsurf = get_shell_surface(surface);
1309 struct wl_event_loop *loop;
1310 int ping_timeout = 200;
1314 if (!shsurf->resource.client)
1317 if (shsurf->surface == shsurf->shell->grab_surface)
1320 if (!shsurf->ping_timer) {
1321 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1322 if (!shsurf->ping_timer)
1325 shsurf->ping_timer->serial = serial;
1326 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1327 shsurf->ping_timer->source =
1328 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1329 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1331 wl_shell_surface_send_ping(&shsurf->resource, serial);
1336 handle_pointer_focus(struct wl_listener *listener, void *data)
1338 struct wl_pointer *pointer = data;
1339 struct weston_surface *surface =
1340 (struct weston_surface *) pointer->focus;
1341 struct weston_compositor *compositor;
1342 struct shell_surface *shsurf;
1348 compositor = surface->compositor;
1349 shsurf = get_shell_surface(surface);
1351 if (shsurf && shsurf->unresponsive) {
1352 set_busy_cursor(shsurf, pointer);
1354 serial = wl_display_next_serial(compositor->wl_display);
1355 ping_handler(surface, serial);
1360 create_pointer_focus_listener(struct weston_seat *seat)
1362 struct wl_listener *listener;
1364 if (!seat->seat.pointer)
1367 listener = malloc(sizeof *listener);
1368 listener->notify = handle_pointer_focus;
1369 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1373 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1376 struct shell_surface *shsurf = resource->data;
1377 struct desktop_shell *shell = shsurf->shell;
1378 struct weston_seat *seat;
1379 struct weston_compositor *ec = shsurf->surface->compositor;
1380 struct wl_pointer *pointer;
1381 int was_unresponsive;
1383 if (shsurf->ping_timer == NULL)
1384 /* Just ignore unsolicited pong. */
1387 if (shsurf->ping_timer->serial == serial) {
1388 was_unresponsive = shsurf->unresponsive;
1389 shsurf->unresponsive = 0;
1390 if (was_unresponsive) {
1391 /* Received pong from previously unresponsive client */
1392 wl_list_for_each(seat, &ec->seat_list, link) {
1393 pointer = seat->seat.pointer;
1394 if (pointer->focus ==
1395 &shell->grab_surface->surface &&
1397 &shsurf->surface->surface)
1398 end_busy_cursor(shsurf, pointer);
1401 ping_timer_destroy(shsurf);
1406 shell_surface_set_title(struct wl_client *client,
1407 struct wl_resource *resource, const char *title)
1409 struct shell_surface *shsurf = resource->data;
1411 free(shsurf->title);
1412 shsurf->title = strdup(title);
1416 shell_surface_set_class(struct wl_client *client,
1417 struct wl_resource *resource, const char *class)
1419 struct shell_surface *shsurf = resource->data;
1421 free(shsurf->class);
1422 shsurf->class = strdup(class);
1425 static struct weston_output *
1426 get_default_output(struct weston_compositor *compositor)
1428 return container_of(compositor->output_list.next,
1429 struct weston_output, link);
1433 shell_unset_fullscreen(struct shell_surface *shsurf)
1435 struct workspace *ws;
1436 /* undo all fullscreen things here */
1437 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1438 shell_surface_is_top_fullscreen(shsurf)) {
1439 weston_output_switch_mode(shsurf->fullscreen_output,
1440 shsurf->fullscreen_output->origin);
1442 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1443 shsurf->fullscreen.framerate = 0;
1444 wl_list_remove(&shsurf->fullscreen.transform.link);
1445 wl_list_init(&shsurf->fullscreen.transform.link);
1446 if (shsurf->fullscreen.black_surface)
1447 weston_surface_destroy(shsurf->fullscreen.black_surface);
1448 shsurf->fullscreen.black_surface = NULL;
1449 shsurf->fullscreen_output = NULL;
1450 weston_surface_set_position(shsurf->surface,
1451 shsurf->saved_x, shsurf->saved_y);
1452 if (shsurf->saved_rotation_valid) {
1453 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1454 &shsurf->rotation.transform.link);
1455 shsurf->saved_rotation_valid = false;
1458 ws = get_current_workspace(shsurf->shell);
1459 wl_list_remove(&shsurf->surface->layer_link);
1460 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1464 reset_shell_surface_type(struct shell_surface *surface)
1466 switch (surface->type) {
1467 case SHELL_SURFACE_FULLSCREEN:
1468 shell_unset_fullscreen(surface);
1470 case SHELL_SURFACE_MAXIMIZED:
1471 surface->output = get_default_output(surface->surface->compositor);
1472 weston_surface_set_position(surface->surface,
1476 case SHELL_SURFACE_NONE:
1477 case SHELL_SURFACE_TOPLEVEL:
1478 case SHELL_SURFACE_TRANSIENT:
1479 case SHELL_SURFACE_POPUP:
1483 surface->type = SHELL_SURFACE_NONE;
1488 set_surface_type(struct shell_surface *shsurf)
1490 struct weston_surface *surface = shsurf->surface;
1491 struct weston_surface *pes = shsurf->parent;
1493 reset_shell_surface_type(shsurf);
1495 shsurf->type = shsurf->next_type;
1496 shsurf->next_type = SHELL_SURFACE_NONE;
1498 switch (shsurf->type) {
1499 case SHELL_SURFACE_TOPLEVEL:
1501 case SHELL_SURFACE_TRANSIENT:
1502 weston_surface_set_position(surface,
1503 pes->geometry.x + shsurf->transient.x,
1504 pes->geometry.y + shsurf->transient.y);
1507 case SHELL_SURFACE_MAXIMIZED:
1508 shsurf->saved_x = surface->geometry.x;
1509 shsurf->saved_y = surface->geometry.y;
1510 shsurf->saved_position_valid = true;
1513 case SHELL_SURFACE_FULLSCREEN:
1514 shsurf->saved_x = surface->geometry.x;
1515 shsurf->saved_y = surface->geometry.y;
1516 shsurf->saved_position_valid = true;
1518 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1519 wl_list_remove(&shsurf->rotation.transform.link);
1520 wl_list_init(&shsurf->rotation.transform.link);
1521 shsurf->surface->geometry.dirty = 1;
1522 shsurf->saved_rotation_valid = true;
1532 set_toplevel(struct shell_surface *shsurf)
1534 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1538 shell_surface_set_toplevel(struct wl_client *client,
1539 struct wl_resource *resource)
1541 struct shell_surface *surface = resource->data;
1543 set_toplevel(surface);
1547 set_transient(struct shell_surface *shsurf,
1548 struct weston_surface *parent, int x, int y, uint32_t flags)
1550 /* assign to parents output */
1551 shsurf->parent = parent;
1552 shsurf->transient.x = x;
1553 shsurf->transient.y = y;
1554 shsurf->transient.flags = flags;
1555 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1559 shell_surface_set_transient(struct wl_client *client,
1560 struct wl_resource *resource,
1561 struct wl_resource *parent_resource,
1562 int x, int y, uint32_t flags)
1564 struct shell_surface *shsurf = resource->data;
1565 struct weston_surface *parent = parent_resource->data;
1567 set_transient(shsurf, parent, x, y, flags);
1570 static struct desktop_shell *
1571 shell_surface_get_shell(struct shell_surface *shsurf)
1573 return shsurf->shell;
1577 get_output_panel_height(struct desktop_shell *shell,
1578 struct weston_output *output)
1580 struct weston_surface *surface;
1581 int panel_height = 0;
1586 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1587 if (surface->output == output) {
1588 panel_height = surface->geometry.height;
1593 return panel_height;
1597 shell_surface_set_maximized(struct wl_client *client,
1598 struct wl_resource *resource,
1599 struct wl_resource *output_resource )
1601 struct shell_surface *shsurf = resource->data;
1602 struct weston_surface *es = shsurf->surface;
1603 struct desktop_shell *shell = NULL;
1604 uint32_t edges = 0, panel_height = 0;
1606 /* get the default output, if the client set it as NULL
1607 check whether the ouput is available */
1608 if (output_resource)
1609 shsurf->output = output_resource->data;
1610 else if (es->output)
1611 shsurf->output = es->output;
1613 shsurf->output = get_default_output(es->compositor);
1615 shell = shell_surface_get_shell(shsurf);
1616 panel_height = get_output_panel_height(shell, shsurf->output);
1617 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1619 shsurf->client->send_configure(shsurf->surface, edges,
1620 shsurf->output->width,
1621 shsurf->output->height - panel_height);
1623 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1627 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1629 static struct weston_surface *
1630 create_black_surface(struct weston_compositor *ec,
1631 struct weston_surface *fs_surface,
1632 float x, float y, int w, int h)
1634 struct weston_surface *surface = NULL;
1636 surface = weston_surface_create(ec);
1637 if (surface == NULL) {
1638 weston_log("no memory\n");
1642 surface->configure = black_surface_configure;
1643 surface->private = fs_surface;
1644 weston_surface_configure(surface, x, y, w, h);
1645 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1646 pixman_region32_fini(&surface->opaque);
1647 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1652 /* Create black surface and append it to the associated fullscreen surface.
1653 * Handle size dismatch and positioning according to the method. */
1655 shell_configure_fullscreen(struct shell_surface *shsurf)
1657 struct weston_output *output = shsurf->fullscreen_output;
1658 struct weston_surface *surface = shsurf->surface;
1659 struct weston_matrix *matrix;
1660 float scale, output_aspect, surface_aspect, x, y;
1662 if (!shsurf->fullscreen.black_surface)
1663 shsurf->fullscreen.black_surface =
1664 create_black_surface(surface->compositor,
1666 output->x, output->y,
1670 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1671 wl_list_insert(&surface->layer_link,
1672 &shsurf->fullscreen.black_surface->layer_link);
1673 shsurf->fullscreen.black_surface->output = output;
1675 switch (shsurf->fullscreen.type) {
1676 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1677 if (surface->buffer)
1678 center_on_output(surface, shsurf->fullscreen_output);
1680 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1681 /* 1:1 mapping between surface and output dimensions */
1682 if (output->width == surface->geometry.width &&
1683 output->height == surface->geometry.height) {
1684 weston_surface_set_position(surface, output->x, output->y);
1688 matrix = &shsurf->fullscreen.transform.matrix;
1689 weston_matrix_init(matrix);
1691 output_aspect = (float) output->width /
1692 (float) output->height;
1693 surface_aspect = (float) surface->geometry.width /
1694 (float) surface->geometry.height;
1695 if (output_aspect < surface_aspect)
1696 scale = (float) output->width /
1697 (float) surface->geometry.width;
1699 scale = (float) output->height /
1700 (float) surface->geometry.height;
1702 weston_matrix_scale(matrix, scale, scale, 1);
1703 wl_list_remove(&shsurf->fullscreen.transform.link);
1704 wl_list_insert(&surface->geometry.transformation_list,
1705 &shsurf->fullscreen.transform.link);
1706 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1707 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1708 weston_surface_set_position(surface, x, y);
1711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1712 if (shell_surface_is_top_fullscreen(shsurf)) {
1713 struct weston_mode mode = {0,
1714 surface->geometry.width,
1715 surface->geometry.height,
1716 shsurf->fullscreen.framerate};
1718 if (weston_output_switch_mode(output, &mode) == 0) {
1719 weston_surface_configure(shsurf->fullscreen.black_surface,
1720 output->x, output->y,
1723 weston_surface_set_position(surface, output->x, output->y);
1728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1735 /* make the fullscreen and black surface at the top */
1737 shell_stack_fullscreen(struct shell_surface *shsurf)
1739 struct weston_output *output = shsurf->fullscreen_output;
1740 struct weston_surface *surface = shsurf->surface;
1741 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1743 wl_list_remove(&surface->layer_link);
1744 wl_list_insert(&shell->fullscreen_layer.surface_list,
1745 &surface->layer_link);
1746 weston_surface_damage(surface);
1748 if (!shsurf->fullscreen.black_surface)
1749 shsurf->fullscreen.black_surface =
1750 create_black_surface(surface->compositor,
1752 output->x, output->y,
1756 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1757 wl_list_insert(&surface->layer_link,
1758 &shsurf->fullscreen.black_surface->layer_link);
1759 weston_surface_damage(shsurf->fullscreen.black_surface);
1763 shell_map_fullscreen(struct shell_surface *shsurf)
1765 shell_stack_fullscreen(shsurf);
1766 shell_configure_fullscreen(shsurf);
1770 shell_surface_set_fullscreen(struct wl_client *client,
1771 struct wl_resource *resource,
1774 struct wl_resource *output_resource)
1776 struct shell_surface *shsurf = resource->data;
1777 struct weston_surface *es = shsurf->surface;
1779 if (output_resource)
1780 shsurf->output = output_resource->data;
1781 else if (es->output)
1782 shsurf->output = es->output;
1784 shsurf->output = get_default_output(es->compositor);
1786 shsurf->fullscreen_output = shsurf->output;
1787 shsurf->fullscreen.type = method;
1788 shsurf->fullscreen.framerate = framerate;
1789 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1791 shsurf->client->send_configure(shsurf->surface, 0,
1792 shsurf->output->width,
1793 shsurf->output->height);
1797 popup_grab_focus(struct wl_pointer_grab *grab,
1798 struct wl_surface *surface,
1802 struct wl_pointer *pointer = grab->pointer;
1803 struct shell_surface *priv =
1804 container_of(grab, struct shell_surface, popup.grab);
1805 struct wl_client *client = priv->surface->surface.resource.client;
1807 if (surface && surface->resource.client == client) {
1808 wl_pointer_set_focus(pointer, surface, x, y);
1809 grab->focus = surface;
1811 wl_pointer_set_focus(pointer, NULL,
1812 wl_fixed_from_int(0),
1813 wl_fixed_from_int(0));
1819 popup_grab_motion(struct wl_pointer_grab *grab,
1820 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1822 struct wl_resource *resource;
1824 resource = grab->pointer->focus_resource;
1826 wl_pointer_send_motion(resource, time, sx, sy);
1830 popup_grab_button(struct wl_pointer_grab *grab,
1831 uint32_t time, uint32_t button, uint32_t state_w)
1833 struct wl_resource *resource;
1834 struct shell_surface *shsurf =
1835 container_of(grab, struct shell_surface, popup.grab);
1836 struct wl_display *display;
1837 enum wl_pointer_button_state state = state_w;
1840 resource = grab->pointer->focus_resource;
1842 display = wl_client_get_display(resource->client);
1843 serial = wl_display_get_serial(display);
1844 wl_pointer_send_button(resource, serial, time, button, state);
1845 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1846 (shsurf->popup.initial_up ||
1847 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1848 popup_grab_end(grab->pointer);
1851 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1852 shsurf->popup.initial_up = 1;
1855 static const struct wl_pointer_grab_interface popup_grab_interface = {
1862 popup_grab_end(struct wl_pointer *pointer)
1864 struct wl_pointer_grab *grab = pointer->grab;
1865 struct shell_surface *shsurf =
1866 container_of(grab, struct shell_surface, popup.grab);
1868 if (pointer->grab->interface == &popup_grab_interface) {
1869 wl_shell_surface_send_popup_done(&shsurf->resource);
1870 wl_pointer_end_grab(grab->pointer);
1871 shsurf->popup.grab.pointer = NULL;
1876 shell_map_popup(struct shell_surface *shsurf)
1878 struct wl_seat *seat = shsurf->popup.seat;
1879 struct weston_surface *es = shsurf->surface;
1880 struct weston_surface *parent = shsurf->parent;
1882 es->output = parent->output;
1883 shsurf->popup.grab.interface = &popup_grab_interface;
1885 weston_surface_update_transform(parent);
1886 if (parent->transform.enabled) {
1887 shsurf->popup.parent_transform.matrix =
1888 parent->transform.matrix;
1890 /* construct x, y translation matrix */
1891 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1892 shsurf->popup.parent_transform.matrix.d[12] =
1894 shsurf->popup.parent_transform.matrix.d[13] =
1897 wl_list_insert(es->geometry.transformation_list.prev,
1898 &shsurf->popup.parent_transform.link);
1900 shsurf->popup.initial_up = 0;
1901 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1902 weston_surface_update_transform(es);
1904 /* We don't require the grab to still be active, but if another
1905 * grab has started in the meantime, we end the popup now. */
1906 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1907 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1909 wl_shell_surface_send_popup_done(&shsurf->resource);
1914 shell_surface_set_popup(struct wl_client *client,
1915 struct wl_resource *resource,
1916 struct wl_resource *seat_resource,
1918 struct wl_resource *parent_resource,
1919 int32_t x, int32_t y, uint32_t flags)
1921 struct shell_surface *shsurf = resource->data;
1923 shsurf->type = SHELL_SURFACE_POPUP;
1924 shsurf->parent = parent_resource->data;
1925 shsurf->popup.seat = seat_resource->data;
1926 shsurf->popup.serial = serial;
1927 shsurf->popup.x = x;
1928 shsurf->popup.y = y;
1931 static const struct wl_shell_surface_interface shell_surface_implementation = {
1934 shell_surface_resize,
1935 shell_surface_set_toplevel,
1936 shell_surface_set_transient,
1937 shell_surface_set_fullscreen,
1938 shell_surface_set_popup,
1939 shell_surface_set_maximized,
1940 shell_surface_set_title,
1941 shell_surface_set_class
1945 destroy_shell_surface(struct shell_surface *shsurf)
1947 if (shsurf->popup.grab.pointer)
1948 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1950 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1951 shell_surface_is_top_fullscreen(shsurf)) {
1952 weston_output_switch_mode(shsurf->fullscreen_output,
1953 shsurf->fullscreen_output->origin);
1956 if (shsurf->fullscreen.black_surface)
1957 weston_surface_destroy(shsurf->fullscreen.black_surface);
1959 /* As destroy_resource() use wl_list_for_each_safe(),
1960 * we can always remove the listener.
1962 wl_list_remove(&shsurf->surface_destroy_listener.link);
1963 shsurf->surface->configure = NULL;
1964 ping_timer_destroy(shsurf);
1966 wl_list_remove(&shsurf->link);
1971 shell_destroy_shell_surface(struct wl_resource *resource)
1973 struct shell_surface *shsurf = resource->data;
1975 destroy_shell_surface(shsurf);
1979 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1981 struct shell_surface *shsurf = container_of(listener,
1982 struct shell_surface,
1983 surface_destroy_listener);
1985 if (shsurf->resource.client) {
1986 wl_resource_destroy(&shsurf->resource);
1988 wl_signal_emit(&shsurf->resource.destroy_signal,
1990 destroy_shell_surface(shsurf);
1995 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1997 static struct shell_surface *
1998 get_shell_surface(struct weston_surface *surface)
2000 if (surface->configure == shell_surface_configure)
2001 return surface->private;
2006 static struct shell_surface *
2007 create_shell_surface(void *shell, struct weston_surface *surface,
2008 const struct weston_shell_client *client)
2010 struct shell_surface *shsurf;
2012 if (surface->configure) {
2013 weston_log("surface->configure already set\n");
2017 shsurf = calloc(1, sizeof *shsurf);
2019 weston_log("no memory to allocate shell surface\n");
2023 surface->configure = shell_surface_configure;
2024 surface->private = shsurf;
2026 shsurf->shell = (struct desktop_shell *) shell;
2027 shsurf->unresponsive = 0;
2028 shsurf->saved_position_valid = false;
2029 shsurf->saved_rotation_valid = false;
2030 shsurf->surface = surface;
2031 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2032 shsurf->fullscreen.framerate = 0;
2033 shsurf->fullscreen.black_surface = NULL;
2034 shsurf->ping_timer = NULL;
2035 wl_list_init(&shsurf->fullscreen.transform.link);
2037 wl_signal_init(&shsurf->resource.destroy_signal);
2038 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2039 wl_signal_add(&surface->surface.resource.destroy_signal,
2040 &shsurf->surface_destroy_listener);
2042 /* init link so its safe to always remove it in destroy_shell_surface */
2043 wl_list_init(&shsurf->link);
2045 /* empty when not in use */
2046 wl_list_init(&shsurf->rotation.transform.link);
2047 weston_matrix_init(&shsurf->rotation.rotation);
2049 wl_list_init(&shsurf->workspace_transform.link);
2051 shsurf->type = SHELL_SURFACE_NONE;
2052 shsurf->next_type = SHELL_SURFACE_NONE;
2054 shsurf->client = client;
2060 shell_get_shell_surface(struct wl_client *client,
2061 struct wl_resource *resource,
2063 struct wl_resource *surface_resource)
2065 struct weston_surface *surface = surface_resource->data;
2066 struct desktop_shell *shell = resource->data;
2067 struct shell_surface *shsurf;
2069 if (get_shell_surface(surface)) {
2070 wl_resource_post_error(surface_resource,
2071 WL_DISPLAY_ERROR_INVALID_OBJECT,
2072 "desktop_shell::get_shell_surface already requested");
2076 shsurf = create_shell_surface(shell, surface, &shell_client);
2078 wl_resource_post_error(surface_resource,
2079 WL_DISPLAY_ERROR_INVALID_OBJECT,
2080 "surface->configure already set");
2084 shsurf->resource.destroy = shell_destroy_shell_surface;
2085 shsurf->resource.object.id = id;
2086 shsurf->resource.object.interface = &wl_shell_surface_interface;
2087 shsurf->resource.object.implementation =
2088 (void (**)(void)) &shell_surface_implementation;
2089 shsurf->resource.data = shsurf;
2091 wl_client_add_resource(client, &shsurf->resource);
2094 static const struct wl_shell_interface shell_implementation = {
2095 shell_get_shell_surface
2099 handle_screensaver_sigchld(struct weston_process *proc, int status)
2105 launch_screensaver(struct desktop_shell *shell)
2107 if (shell->screensaver.binding)
2110 if (!shell->screensaver.path)
2113 if (shell->screensaver.process.pid != 0) {
2114 weston_log("old screensaver still running\n");
2118 weston_client_launch(shell->compositor,
2119 &shell->screensaver.process,
2120 shell->screensaver.path,
2121 handle_screensaver_sigchld);
2125 terminate_screensaver(struct desktop_shell *shell)
2127 if (shell->screensaver.process.pid == 0)
2130 kill(shell->screensaver.process.pid, SIGTERM);
2134 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
2136 struct weston_surface *s, *next;
2138 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2139 if (s->output == es->output && s != es) {
2140 weston_surface_unmap(s);
2141 s->configure = NULL;
2145 weston_surface_configure(es, es->output->x, es->output->y,
2146 es->buffer->width, es->buffer->height);
2148 if (wl_list_empty(&es->layer_link)) {
2149 wl_list_insert(&layer->surface_list, &es->layer_link);
2150 weston_compositor_schedule_repaint(es->compositor);
2155 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2157 struct desktop_shell *shell = es->private;
2159 configure_static_surface(es, &shell->background_layer);
2163 desktop_shell_set_background(struct wl_client *client,
2164 struct wl_resource *resource,
2165 struct wl_resource *output_resource,
2166 struct wl_resource *surface_resource)
2168 struct desktop_shell *shell = resource->data;
2169 struct weston_surface *surface = surface_resource->data;
2171 if (surface->configure) {
2172 wl_resource_post_error(surface_resource,
2173 WL_DISPLAY_ERROR_INVALID_OBJECT,
2174 "surface role already assigned");
2178 surface->configure = background_configure;
2179 surface->private = shell;
2180 surface->output = output_resource->data;
2181 desktop_shell_send_configure(resource, 0,
2183 surface->output->width,
2184 surface->output->height);
2188 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2190 struct desktop_shell *shell = es->private;
2192 configure_static_surface(es, &shell->panel_layer);
2196 desktop_shell_set_panel(struct wl_client *client,
2197 struct wl_resource *resource,
2198 struct wl_resource *output_resource,
2199 struct wl_resource *surface_resource)
2201 struct desktop_shell *shell = resource->data;
2202 struct weston_surface *surface = surface_resource->data;
2204 if (surface->configure) {
2205 wl_resource_post_error(surface_resource,
2206 WL_DISPLAY_ERROR_INVALID_OBJECT,
2207 "surface role already assigned");
2211 surface->configure = panel_configure;
2212 surface->private = shell;
2213 surface->output = output_resource->data;
2214 desktop_shell_send_configure(resource, 0,
2216 surface->output->width,
2217 surface->output->height);
2221 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2223 struct desktop_shell *shell = surface->private;
2225 center_on_output(surface, get_default_output(shell->compositor));
2227 if (!weston_surface_is_mapped(surface)) {
2228 wl_list_insert(&shell->lock_layer.surface_list,
2229 &surface->layer_link);
2230 weston_surface_update_transform(surface);
2231 weston_compositor_wake(shell->compositor);
2236 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2238 struct desktop_shell *shell =
2239 container_of(listener, struct desktop_shell, lock_surface_listener);
2241 weston_log("lock surface gone\n");
2242 shell->lock_surface = NULL;
2246 desktop_shell_set_lock_surface(struct wl_client *client,
2247 struct wl_resource *resource,
2248 struct wl_resource *surface_resource)
2250 struct desktop_shell *shell = resource->data;
2251 struct weston_surface *surface = surface_resource->data;
2253 shell->prepare_event_sent = false;
2258 shell->lock_surface = surface;
2260 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2261 wl_signal_add(&surface_resource->destroy_signal,
2262 &shell->lock_surface_listener);
2264 surface->configure = lock_surface_configure;
2265 surface->private = shell;
2269 resume_desktop(struct desktop_shell *shell)
2271 struct weston_surface *surface;
2272 struct workspace *ws = get_current_workspace(shell);
2274 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2275 weston_surface_unmap(surface);
2277 terminate_screensaver(shell);
2279 wl_list_remove(&shell->lock_layer.link);
2280 wl_list_insert(&shell->compositor->cursor_layer.link,
2281 &shell->fullscreen_layer.link);
2282 wl_list_insert(&shell->fullscreen_layer.link,
2283 &shell->panel_layer.link);
2284 if (shell->showing_input_panels) {
2285 wl_list_insert(&shell->panel_layer.link,
2286 &shell->input_panel_layer.link);
2287 wl_list_insert(&shell->input_panel_layer.link,
2290 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2293 restore_focus_state(shell, get_current_workspace(shell));
2295 shell->locked = false;
2296 shell->compositor->idle_time = shell->compositor->option_idle_time;
2297 weston_compositor_wake(shell->compositor);
2298 weston_compositor_damage_all(shell->compositor);
2302 desktop_shell_unlock(struct wl_client *client,
2303 struct wl_resource *resource)
2305 struct desktop_shell *shell = resource->data;
2307 shell->prepare_event_sent = false;
2310 resume_desktop(shell);
2314 desktop_shell_set_grab_surface(struct wl_client *client,
2315 struct wl_resource *resource,
2316 struct wl_resource *surface_resource)
2318 struct desktop_shell *shell = resource->data;
2320 shell->grab_surface = surface_resource->data;
2323 static const struct desktop_shell_interface desktop_shell_implementation = {
2324 desktop_shell_set_background,
2325 desktop_shell_set_panel,
2326 desktop_shell_set_lock_surface,
2327 desktop_shell_unlock,
2328 desktop_shell_set_grab_surface
2331 static enum shell_surface_type
2332 get_shell_surface_type(struct weston_surface *surface)
2334 struct shell_surface *shsurf;
2336 shsurf = get_shell_surface(surface);
2338 return SHELL_SURFACE_NONE;
2339 return shsurf->type;
2343 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2345 struct weston_surface *surface =
2346 (struct weston_surface *) seat->pointer->focus;
2347 struct shell_surface *shsurf;
2349 if (surface == NULL)
2352 shsurf = get_shell_surface(surface);
2353 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2356 surface_move(shsurf, (struct weston_seat *) seat);
2360 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2362 struct weston_surface *surface =
2363 (struct weston_surface *) seat->pointer->focus;
2366 struct shell_surface *shsurf;
2368 if (surface == NULL)
2371 shsurf = get_shell_surface(surface);
2372 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2375 weston_surface_from_global(surface,
2376 wl_fixed_to_int(seat->pointer->grab_x),
2377 wl_fixed_to_int(seat->pointer->grab_y),
2380 if (x < surface->geometry.width / 3)
2381 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2382 else if (x < 2 * surface->geometry.width / 3)
2385 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2387 if (y < surface->geometry.height / 3)
2388 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2389 else if (y < 2 * surface->geometry.height / 3)
2392 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2394 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2398 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2399 wl_fixed_t value, void *data)
2402 struct shell_surface *shsurf;
2403 struct weston_surface *surface =
2404 (struct weston_surface *) seat->pointer->focus;
2406 if (surface == NULL)
2409 shsurf = get_shell_surface(surface);
2413 surface->alpha -= wl_fixed_to_double(value) * step;
2415 if (surface->alpha > 1.0)
2416 surface->alpha = 1.0;
2417 if (surface->alpha < step)
2418 surface->alpha = step;
2420 surface->geometry.dirty = 1;
2421 weston_surface_damage(surface);
2425 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2428 struct weston_seat *ws = (struct weston_seat *) seat;
2429 struct weston_compositor *compositor = ws->compositor;
2430 struct weston_output *output;
2433 wl_list_for_each(output, &compositor->output_list, link) {
2434 if (pixman_region32_contains_point(&output->region,
2435 wl_fixed_to_double(seat->pointer->x),
2436 wl_fixed_to_double(seat->pointer->y),
2438 if (key == KEY_PAGEUP)
2439 increment = output->zoom.increment;
2440 else if (key == KEY_PAGEDOWN)
2441 increment = -output->zoom.increment;
2442 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2443 /* For every pixel zoom 20th of a step */
2444 increment = output->zoom.increment *
2445 -wl_fixed_to_double(value) / 20.0;
2449 output->zoom.level += increment;
2451 if (output->zoom.level < 0.0)
2452 output->zoom.level = 0.0;
2453 else if (output->zoom.level > output->zoom.max_level)
2454 output->zoom.level = output->zoom.max_level;
2455 else if (!output->zoom.active) {
2456 output->zoom.active = 1;
2457 output->disable_planes++;
2460 output->zoom.spring_z.target = output->zoom.level;
2462 weston_output_update_zoom(output, output->zoom.type);
2468 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2469 wl_fixed_t value, void *data)
2471 do_zoom(seat, time, 0, axis, value);
2475 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2478 do_zoom(seat, time, key, 0, 0);
2482 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2485 struct weston_compositor *compositor = data;
2487 wl_display_terminate(compositor->wl_display);
2491 rotate_grab_motion(struct wl_pointer_grab *grab,
2492 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2494 struct rotate_grab *rotate =
2495 container_of(grab, struct rotate_grab, base.grab);
2496 struct wl_pointer *pointer = grab->pointer;
2497 struct shell_surface *shsurf = rotate->base.shsurf;
2498 struct weston_surface *surface;
2499 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2504 surface = shsurf->surface;
2506 cx = 0.5f * surface->geometry.width;
2507 cy = 0.5f * surface->geometry.height;
2509 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2510 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2511 r = sqrtf(dx * dx + dy * dy);
2513 wl_list_remove(&shsurf->rotation.transform.link);
2514 shsurf->surface->geometry.dirty = 1;
2517 struct weston_matrix *matrix =
2518 &shsurf->rotation.transform.matrix;
2520 weston_matrix_init(&rotate->rotation);
2521 rotate->rotation.d[0] = dx / r;
2522 rotate->rotation.d[4] = -dy / r;
2523 rotate->rotation.d[1] = -rotate->rotation.d[4];
2524 rotate->rotation.d[5] = rotate->rotation.d[0];
2526 weston_matrix_init(matrix);
2527 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2528 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2529 weston_matrix_multiply(matrix, &rotate->rotation);
2530 weston_matrix_translate(matrix, cx, cy, 0.0f);
2533 &shsurf->surface->geometry.transformation_list,
2534 &shsurf->rotation.transform.link);
2536 wl_list_init(&shsurf->rotation.transform.link);
2537 weston_matrix_init(&shsurf->rotation.rotation);
2538 weston_matrix_init(&rotate->rotation);
2541 /* We need to adjust the position of the surface
2542 * in case it was resized in a rotated state before */
2543 cposx = surface->geometry.x + cx;
2544 cposy = surface->geometry.y + cy;
2545 dposx = rotate->center.x - cposx;
2546 dposy = rotate->center.y - cposy;
2547 if (dposx != 0.0f || dposy != 0.0f) {
2548 weston_surface_set_position(surface,
2549 surface->geometry.x + dposx,
2550 surface->geometry.y + dposy);
2553 /* Repaint implies weston_surface_update_transform(), which
2554 * lazily applies the damage due to rotation update.
2556 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2560 rotate_grab_button(struct wl_pointer_grab *grab,
2561 uint32_t time, uint32_t button, uint32_t state_w)
2563 struct rotate_grab *rotate =
2564 container_of(grab, struct rotate_grab, base.grab);
2565 struct wl_pointer *pointer = grab->pointer;
2566 struct shell_surface *shsurf = rotate->base.shsurf;
2567 enum wl_pointer_button_state state = state_w;
2569 if (pointer->button_count == 0 &&
2570 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2572 weston_matrix_multiply(&shsurf->rotation.rotation,
2574 shell_grab_end(&rotate->base);
2579 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2586 surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
2588 struct rotate_grab *rotate;
2592 rotate = malloc(sizeof *rotate);
2596 weston_surface_to_global_float(surface->surface,
2597 surface->surface->geometry.width / 2,
2598 surface->surface->geometry.height / 2,
2599 &rotate->center.x, &rotate->center.y);
2601 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2602 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2603 r = sqrtf(dx * dx + dy * dy);
2605 struct weston_matrix inverse;
2607 weston_matrix_init(&inverse);
2608 inverse.d[0] = dx / r;
2609 inverse.d[4] = dy / r;
2610 inverse.d[1] = -inverse.d[4];
2611 inverse.d[5] = inverse.d[0];
2612 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2614 weston_matrix_init(&rotate->rotation);
2615 rotate->rotation.d[0] = dx / r;
2616 rotate->rotation.d[4] = -dy / r;
2617 rotate->rotation.d[1] = -rotate->rotation.d[4];
2618 rotate->rotation.d[5] = rotate->rotation.d[0];
2620 weston_matrix_init(&surface->rotation.rotation);
2621 weston_matrix_init(&rotate->rotation);
2624 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2625 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2629 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2632 struct weston_surface *base_surface =
2633 (struct weston_surface *) seat->pointer->focus;
2634 struct shell_surface *surface;
2636 if (base_surface == NULL)
2639 surface = get_shell_surface(base_surface);
2640 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2643 surface_rotate(surface, seat);
2647 lower_fullscreen_layer(struct desktop_shell *shell)
2649 struct workspace *ws;
2650 struct weston_surface *surface, *prev;
2652 ws = get_current_workspace(shell);
2653 wl_list_for_each_reverse_safe(surface, prev,
2654 &shell->fullscreen_layer.surface_list,
2656 weston_surface_restack(surface, &ws->layer.surface_list);
2660 activate(struct desktop_shell *shell, struct weston_surface *es,
2661 struct weston_seat *seat)
2663 struct focus_state *state;
2664 struct workspace *ws;
2666 weston_surface_activate(es, seat);
2668 state = ensure_focus_state(shell, seat);
2672 state->keyboard_focus = es;
2673 wl_list_remove(&state->surface_destroy_listener.link);
2674 wl_signal_add(&es->surface.resource.destroy_signal,
2675 &state->surface_destroy_listener);
2677 switch (get_shell_surface_type(es)) {
2678 case SHELL_SURFACE_FULLSCREEN:
2679 /* should on top of panels */
2680 shell_stack_fullscreen(get_shell_surface(es));
2681 shell_configure_fullscreen(get_shell_surface(es));
2684 ws = get_current_workspace(shell);
2685 lower_fullscreen_layer(shell);
2686 weston_surface_restack(es, &ws->layer.surface_list);
2691 /* no-op func for checking black surface */
2693 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2698 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2700 if (es->configure == black_surface_configure) {
2702 *fs_surface = (struct weston_surface *)es->private;
2709 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2712 struct weston_seat *ws = (struct weston_seat *) seat;
2713 struct desktop_shell *shell = data;
2714 struct weston_surface *focus;
2715 struct weston_surface *upper;
2717 focus = (struct weston_surface *) seat->pointer->focus;
2721 if (is_black_surface(focus, &upper))
2724 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2727 if (seat->pointer->grab == &seat->pointer->default_grab)
2728 activate(shell, focus, ws);
2732 lock(struct wl_listener *listener, void *data)
2734 struct desktop_shell *shell =
2735 container_of(listener, struct desktop_shell, lock_listener);
2736 struct weston_output *output;
2737 struct workspace *ws = get_current_workspace(shell);
2739 if (shell->locked) {
2740 wl_list_for_each(output, &shell->compositor->output_list, link)
2741 /* TODO: find a way to jump to other DPMS levels */
2742 if (output->set_dpms)
2743 output->set_dpms(output, WESTON_DPMS_STANDBY);
2747 shell->locked = true;
2749 /* Hide all surfaces by removing the fullscreen, panel and
2750 * toplevel layers. This way nothing else can show or receive
2751 * input events while we are locked. */
2753 wl_list_remove(&shell->panel_layer.link);
2754 wl_list_remove(&shell->fullscreen_layer.link);
2755 if (shell->showing_input_panels)
2756 wl_list_remove(&shell->input_panel_layer.link);
2757 wl_list_remove(&ws->layer.link);
2758 wl_list_insert(&shell->compositor->cursor_layer.link,
2759 &shell->lock_layer.link);
2761 launch_screensaver(shell);
2763 /* TODO: disable bindings that should not work while locked. */
2765 /* All this must be undone in resume_desktop(). */
2769 unlock(struct wl_listener *listener, void *data)
2771 struct desktop_shell *shell =
2772 container_of(listener, struct desktop_shell, unlock_listener);
2774 if (!shell->locked || shell->lock_surface) {
2775 weston_compositor_wake(shell->compositor);
2779 /* If desktop-shell client has gone away, unlock immediately. */
2780 if (!shell->child.desktop_shell) {
2781 resume_desktop(shell);
2785 if (shell->prepare_event_sent)
2788 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2789 shell->prepare_event_sent = true;
2793 show_input_panels(struct wl_listener *listener, void *data)
2795 struct desktop_shell *shell =
2796 container_of(listener, struct desktop_shell,
2797 show_input_panel_listener);
2798 struct input_panel_surface *surface, *next;
2799 struct weston_surface *ws;
2801 shell->showing_input_panels = true;
2804 wl_list_insert(&shell->panel_layer.link,
2805 &shell->input_panel_layer.link);
2807 wl_list_for_each_safe(surface, next,
2808 &shell->input_panel.surfaces, link) {
2809 ws = surface->surface;
2810 wl_list_insert(&shell->input_panel_layer.surface_list,
2812 ws->geometry.dirty = 1;
2813 weston_surface_update_transform(ws);
2814 weston_surface_damage(ws);
2815 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2820 hide_input_panels(struct wl_listener *listener, void *data)
2822 struct desktop_shell *shell =
2823 container_of(listener, struct desktop_shell,
2824 hide_input_panel_listener);
2825 struct weston_surface *surface, *next;
2827 shell->showing_input_panels = false;
2830 wl_list_remove(&shell->input_panel_layer.link);
2832 wl_list_for_each_safe(surface, next,
2833 &shell->input_panel_layer.surface_list, layer_link)
2834 weston_surface_unmap(surface);
2838 center_on_output(struct weston_surface *surface, struct weston_output *output)
2840 float x = (output->width - surface->buffer->width) / 2;
2841 float y = (output->height - surface->buffer->height) / 2;
2843 weston_surface_configure(surface, output->x + x, output->y + y,
2844 surface->buffer->width,
2845 surface->buffer->height);
2849 weston_surface_set_initial_position (struct weston_surface *surface,
2850 struct desktop_shell *shell)
2852 struct weston_compositor *compositor = shell->compositor;
2854 int range_x, range_y;
2855 int dx, dy, x, y, panel_height;
2856 struct weston_output *output, *target_output = NULL;
2857 struct weston_seat *seat;
2859 /* As a heuristic place the new window on the same output as the
2860 * pointer. Falling back to the output containing 0, 0.
2862 * TODO: Do something clever for touch too?
2864 wl_list_for_each(seat, &compositor->seat_list, link) {
2865 if (seat->has_pointer) {
2866 ix = wl_fixed_to_int(seat->pointer.x);
2867 iy = wl_fixed_to_int(seat->pointer.y);
2872 wl_list_for_each(output, &compositor->output_list, link) {
2873 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2874 target_output = output;
2879 if (!target_output) {
2880 weston_surface_set_position(surface, 10 + random() % 400,
2881 10 + random() % 400);
2885 /* Valid range within output where the surface will still be onscreen.
2886 * If this is negative it means that the surface is bigger than
2889 panel_height = get_output_panel_height(shell, target_output);
2890 range_x = target_output->width - surface->geometry.width;
2891 range_y = (target_output->height - panel_height) -
2892 surface->geometry.height;
2895 dx = random() % range_x;
2900 dy = panel_height + random() % range_y;
2904 x = target_output->x + dx;
2905 y = target_output->y + dy;
2907 weston_surface_set_position (surface, x, y);
2911 map(struct desktop_shell *shell, struct weston_surface *surface,
2912 int32_t width, int32_t height, int32_t sx, int32_t sy)
2914 struct weston_compositor *compositor = shell->compositor;
2915 struct shell_surface *shsurf = get_shell_surface(surface);
2916 enum shell_surface_type surface_type = shsurf->type;
2917 struct weston_surface *parent;
2918 struct weston_seat *seat;
2919 struct workspace *ws;
2920 int panel_height = 0;
2922 surface->geometry.width = width;
2923 surface->geometry.height = height;
2924 surface->geometry.dirty = 1;
2926 /* initial positioning, see also configure() */
2927 switch (surface_type) {
2928 case SHELL_SURFACE_TOPLEVEL:
2929 weston_surface_set_initial_position(surface, shell);
2931 case SHELL_SURFACE_FULLSCREEN:
2932 center_on_output(surface, shsurf->fullscreen_output);
2933 shell_map_fullscreen(shsurf);
2935 case SHELL_SURFACE_MAXIMIZED:
2936 /* use surface configure to set the geometry */
2937 panel_height = get_output_panel_height(shell,surface->output);
2938 weston_surface_set_position(surface, shsurf->output->x,
2939 shsurf->output->y + panel_height);
2941 case SHELL_SURFACE_POPUP:
2942 shell_map_popup(shsurf);
2944 case SHELL_SURFACE_NONE:
2945 weston_surface_set_position(surface,
2946 surface->geometry.x + sx,
2947 surface->geometry.y + sy);
2953 /* surface stacking order, see also activate() */
2954 switch (surface_type) {
2955 case SHELL_SURFACE_POPUP:
2956 case SHELL_SURFACE_TRANSIENT:
2957 parent = shsurf->parent;
2958 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2960 case SHELL_SURFACE_FULLSCREEN:
2961 case SHELL_SURFACE_NONE:
2964 ws = get_current_workspace(shell);
2965 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2969 if (surface_type != SHELL_SURFACE_NONE) {
2970 weston_surface_update_transform(surface);
2971 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2972 surface->output = shsurf->output;
2975 switch (surface_type) {
2976 case SHELL_SURFACE_TRANSIENT:
2977 if (shsurf->transient.flags ==
2978 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2980 case SHELL_SURFACE_TOPLEVEL:
2981 case SHELL_SURFACE_FULLSCREEN:
2982 case SHELL_SURFACE_MAXIMIZED:
2983 if (!shell->locked) {
2984 wl_list_for_each(seat, &compositor->seat_list, link)
2985 activate(shell, surface, seat);
2992 if (surface_type == SHELL_SURFACE_TOPLEVEL)
2994 switch (shell->win_animation_type) {
2995 case ANIMATION_FADE:
2996 weston_fade_run(surface, NULL, NULL);
2998 case ANIMATION_ZOOM:
2999 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3008 configure(struct desktop_shell *shell, struct weston_surface *surface,
3009 float x, float y, int32_t width, int32_t height)
3011 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3012 struct shell_surface *shsurf;
3014 shsurf = get_shell_surface(surface);
3016 surface_type = shsurf->type;
3018 surface->geometry.x = x;
3019 surface->geometry.y = y;
3020 surface->geometry.width = width;
3021 surface->geometry.height = height;
3022 surface->geometry.dirty = 1;
3024 switch (surface_type) {
3025 case SHELL_SURFACE_FULLSCREEN:
3026 shell_stack_fullscreen(shsurf);
3027 shell_configure_fullscreen(shsurf);
3029 case SHELL_SURFACE_MAXIMIZED:
3030 /* setting x, y and using configure to change that geometry */
3031 surface->geometry.x = surface->output->x;
3032 surface->geometry.y = surface->output->y +
3033 get_output_panel_height(shell,surface->output);
3035 case SHELL_SURFACE_TOPLEVEL:
3041 /* XXX: would a fullscreen surface need the same handling? */
3042 if (surface->output) {
3043 weston_surface_update_transform(surface);
3045 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3046 surface->output = shsurf->output;
3051 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3053 struct shell_surface *shsurf = get_shell_surface(es);
3054 struct desktop_shell *shell = shsurf->shell;
3055 int type_changed = 0;
3057 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3058 shsurf->type != shsurf->next_type) {
3059 set_surface_type(shsurf);
3063 if (!weston_surface_is_mapped(es)) {
3064 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
3065 } else if (type_changed || sx != 0 || sy != 0 ||
3066 es->geometry.width != es->buffer->width ||
3067 es->geometry.height != es->buffer->height) {
3068 float from_x, from_y;
3071 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3072 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3073 configure(shell, es,
3074 es->geometry.x + to_x - from_x,
3075 es->geometry.y + to_y - from_y,
3076 es->buffer->width, es->buffer->height);
3080 static void launch_desktop_shell_process(void *data);
3083 desktop_shell_sigchld(struct weston_process *process, int status)
3086 struct desktop_shell *shell =
3087 container_of(process, struct desktop_shell, child.process);
3089 shell->child.process.pid = 0;
3090 shell->child.client = NULL; /* already destroyed by wayland */
3092 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3093 time = weston_compositor_get_time();
3094 if (time - shell->child.deathstamp > 30000) {
3095 shell->child.deathstamp = time;
3096 shell->child.deathcount = 0;
3099 shell->child.deathcount++;
3100 if (shell->child.deathcount > 5) {
3101 weston_log("weston-desktop-shell died, giving up.\n");
3105 weston_log("weston-desktop-shell died, respawning...\n");
3106 launch_desktop_shell_process(shell);
3110 launch_desktop_shell_process(void *data)
3112 struct desktop_shell *shell = data;
3113 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3115 shell->child.client = weston_client_launch(shell->compositor,
3116 &shell->child.process,
3118 desktop_shell_sigchld);
3120 if (!shell->child.client)
3121 weston_log("not able to start %s\n", shell_exe);
3125 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3127 struct desktop_shell *shell = data;
3129 wl_client_add_object(client, &wl_shell_interface,
3130 &shell_implementation, id, shell);
3134 unbind_desktop_shell(struct wl_resource *resource)
3136 struct desktop_shell *shell = resource->data;
3139 resume_desktop(shell);
3141 shell->child.desktop_shell = NULL;
3142 shell->prepare_event_sent = false;
3147 bind_desktop_shell(struct wl_client *client,
3148 void *data, uint32_t version, uint32_t id)
3150 struct desktop_shell *shell = data;
3151 struct wl_resource *resource;
3153 resource = wl_client_add_object(client, &desktop_shell_interface,
3154 &desktop_shell_implementation,
3157 if (client == shell->child.client) {
3158 resource->destroy = unbind_desktop_shell;
3159 shell->child.desktop_shell = resource;
3163 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3164 "permission to bind desktop_shell denied");
3165 wl_resource_destroy(resource);
3169 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3171 struct desktop_shell *shell = surface->private;
3176 center_on_output(surface, surface->output);
3178 if (wl_list_empty(&surface->layer_link)) {
3179 wl_list_insert(shell->lock_layer.surface_list.prev,
3180 &surface->layer_link);
3181 weston_surface_update_transform(surface);
3182 shell->compositor->idle_time = shell->screensaver.duration;
3183 weston_compositor_wake(shell->compositor);
3184 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3189 screensaver_set_surface(struct wl_client *client,
3190 struct wl_resource *resource,
3191 struct wl_resource *surface_resource,
3192 struct wl_resource *output_resource)
3194 struct desktop_shell *shell = resource->data;
3195 struct weston_surface *surface = surface_resource->data;
3196 struct weston_output *output = output_resource->data;
3198 surface->configure = screensaver_configure;
3199 surface->private = shell;
3200 surface->output = output;
3203 static const struct screensaver_interface screensaver_implementation = {
3204 screensaver_set_surface
3208 unbind_screensaver(struct wl_resource *resource)
3210 struct desktop_shell *shell = resource->data;
3212 shell->screensaver.binding = NULL;
3217 bind_screensaver(struct wl_client *client,
3218 void *data, uint32_t version, uint32_t id)
3220 struct desktop_shell *shell = data;
3221 struct wl_resource *resource;
3223 resource = wl_client_add_object(client, &screensaver_interface,
3224 &screensaver_implementation,
3227 if (shell->screensaver.binding == NULL) {
3228 resource->destroy = unbind_screensaver;
3229 shell->screensaver.binding = resource;
3233 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3234 "interface object already bound");
3235 wl_resource_destroy(resource);
3239 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3241 struct weston_mode *mode = surface->output->current;
3242 float x = (mode->width - surface->buffer->width) / 2;
3243 float y = mode->height - surface->buffer->height;
3245 /* Don't map the input panel here, wait for
3246 * show_input_panels signal. */
3248 weston_surface_configure(surface,
3249 surface->output->x + x,
3250 surface->output->y + y,
3251 surface->buffer->width,
3252 surface->buffer->height);
3256 destroy_input_panel_surface(struct wl_listener *listener,
3259 struct input_panel_surface *input_panel_surface =
3260 container_of(listener, struct input_panel_surface, listener);
3262 wl_list_remove(&listener->link);
3263 wl_list_remove(&input_panel_surface->link);
3265 free(input_panel_surface);
3269 input_panel_set_surface(struct wl_client *client,
3270 struct wl_resource *resource,
3271 struct wl_resource *surface_resource,
3272 struct wl_resource *output_resource)
3274 struct desktop_shell *shell = resource->data;
3275 struct weston_surface *surface = surface_resource->data;
3276 struct weston_output *output = output_resource->data;
3277 struct input_panel_surface *input_panel_surface;
3279 surface->configure = input_panel_configure;
3280 surface->private = shell;
3281 surface->output = output;
3283 input_panel_surface = malloc(sizeof *input_panel_surface);
3284 if (!input_panel_surface) {
3285 wl_resource_post_no_memory(resource);
3289 input_panel_surface->surface = surface;
3290 input_panel_surface->listener.notify = destroy_input_panel_surface;
3292 wl_signal_add(&surface_resource->destroy_signal,
3293 &input_panel_surface->listener);
3295 wl_list_insert(&shell->input_panel.surfaces,
3296 &input_panel_surface->link);
3299 static const struct input_panel_interface input_panel_implementation = {
3300 input_panel_set_surface
3304 unbind_input_panel(struct wl_resource *resource)
3306 struct desktop_shell *shell = resource->data;
3308 shell->input_panel.binding = NULL;
3313 bind_input_panel(struct wl_client *client,
3314 void *data, uint32_t version, uint32_t id)
3316 struct desktop_shell *shell = data;
3317 struct wl_resource *resource;
3319 resource = wl_client_add_object(client, &input_panel_interface,
3320 &input_panel_implementation,
3323 if (shell->input_panel.binding == NULL) {
3324 resource->destroy = unbind_input_panel;
3325 shell->input_panel.binding = resource;
3329 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3330 "interface object already bound");
3331 wl_resource_destroy(resource);
3335 struct desktop_shell *shell;
3336 struct weston_surface *current;
3337 struct wl_listener listener;
3338 struct wl_keyboard_grab grab;
3342 switcher_next(struct switcher *switcher)
3344 struct weston_surface *surface;
3345 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3346 struct shell_surface *shsurf;
3347 struct workspace *ws = get_current_workspace(switcher->shell);
3349 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3350 switch (get_shell_surface_type(surface)) {
3351 case SHELL_SURFACE_TOPLEVEL:
3352 case SHELL_SURFACE_FULLSCREEN:
3353 case SHELL_SURFACE_MAXIMIZED:
3356 if (prev == switcher->current)
3359 surface->alpha = 0.25;
3360 surface->geometry.dirty = 1;
3361 weston_surface_damage(surface);
3367 if (is_black_surface(surface, NULL)) {
3368 surface->alpha = 0.25;
3369 surface->geometry.dirty = 1;
3370 weston_surface_damage(surface);
3380 wl_list_remove(&switcher->listener.link);
3381 wl_signal_add(&next->surface.resource.destroy_signal,
3382 &switcher->listener);
3384 switcher->current = next;
3387 shsurf = get_shell_surface(switcher->current);
3388 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3389 shsurf->fullscreen.black_surface->alpha = 1.0;
3393 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3395 struct switcher *switcher =
3396 container_of(listener, struct switcher, listener);
3398 switcher_next(switcher);
3402 switcher_destroy(struct switcher *switcher)
3404 struct weston_surface *surface;
3405 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3406 struct workspace *ws = get_current_workspace(switcher->shell);
3408 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3409 surface->alpha = 1.0;
3410 weston_surface_damage(surface);
3413 if (switcher->current)
3414 activate(switcher->shell, switcher->current,
3415 (struct weston_seat *) keyboard->seat);
3416 wl_list_remove(&switcher->listener.link);
3417 wl_keyboard_end_grab(keyboard);
3422 switcher_key(struct wl_keyboard_grab *grab,
3423 uint32_t time, uint32_t key, uint32_t state_w)
3425 struct switcher *switcher = container_of(grab, struct switcher, grab);
3426 enum wl_keyboard_key_state state = state_w;
3428 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3429 switcher_next(switcher);
3433 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3434 uint32_t mods_depressed, uint32_t mods_latched,
3435 uint32_t mods_locked, uint32_t group)
3437 struct switcher *switcher = container_of(grab, struct switcher, grab);
3438 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3440 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3441 switcher_destroy(switcher);
3444 static const struct wl_keyboard_grab_interface switcher_grab = {
3450 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3453 struct desktop_shell *shell = data;
3454 struct switcher *switcher;
3456 switcher = malloc(sizeof *switcher);
3457 switcher->shell = shell;
3458 switcher->current = NULL;
3459 switcher->listener.notify = switcher_handle_surface_destroy;
3460 wl_list_init(&switcher->listener.link);
3462 lower_fullscreen_layer(switcher->shell);
3463 switcher->grab.interface = &switcher_grab;
3464 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3465 wl_keyboard_set_focus(seat->keyboard, NULL);
3466 switcher_next(switcher);
3470 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3473 struct weston_compositor *compositor = data;
3474 struct weston_output *output;
3475 long backlight_new = 0;
3477 /* TODO: we're limiting to simple use cases, where we assume just
3478 * control on the primary display. We'd have to extend later if we
3479 * ever get support for setting backlights on random desktop LCD
3481 output = get_default_output(compositor);
3485 if (!output->set_backlight)
3488 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3489 backlight_new = output->backlight_current - 25;
3490 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3491 backlight_new = output->backlight_current + 25;
3493 if (backlight_new < 5)
3495 if (backlight_new > 255)
3496 backlight_new = 255;
3498 output->backlight_current = backlight_new;
3499 output->set_backlight(output, output->backlight_current);
3503 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3506 struct desktop_shell *shell = data;
3507 struct weston_compositor *compositor = shell->compositor;
3508 struct weston_surface *surface;
3509 struct weston_plane plane;
3511 if (shell->debug_repaint_surface) {
3512 weston_surface_destroy(shell->debug_repaint_surface);
3513 shell->debug_repaint_surface = NULL;
3515 surface = weston_surface_create(compositor);
3516 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3517 weston_surface_configure(surface, 0, 0, 8192, 8192);
3518 wl_list_insert(&compositor->fade_layer.surface_list,
3519 &surface->layer_link);
3520 pixman_region32_fini(&surface->input);
3521 pixman_region32_init(&surface->input);
3523 /* Here's the dirty little trick that makes the
3524 * repaint debugging work: we move the surface to a
3525 * different plane and force an update_transform to
3526 * update dependent state and clear the
3527 * geometry.dirty bit. This way the call to
3528 * damage_below() in update_transform() does not
3529 * add damage to the primary plane. */
3531 weston_plane_init(&plane, 0, 0);
3532 surface->plane = &plane;
3533 weston_surface_update_transform(surface);
3534 shell->debug_repaint_surface = surface;
3535 surface->plane = &compositor->primary_plane;
3536 weston_plane_release(&plane);
3542 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3545 struct desktop_shell *shell = data;
3546 struct weston_compositor *compositor = shell->compositor;
3547 compositor->fan_debug = !compositor->fan_debug;
3548 weston_compositor_damage_all(compositor);
3552 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3555 struct wl_surface *focus_surface;
3556 struct wl_client *client;
3557 struct desktop_shell *shell = data;
3558 struct weston_compositor *compositor = shell->compositor;
3561 focus_surface = seat->keyboard->focus;
3565 wl_signal_emit(&compositor->kill_signal, focus_surface);
3567 client = focus_surface->resource.client;
3568 wl_client_get_credentials(client, &pid, NULL, NULL);
3570 /* Skip clients that we launched ourselves (the credentials of
3571 * the socketpair is ours) */
3572 if (pid == getpid())
3579 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3580 uint32_t key, void *data)
3582 struct desktop_shell *shell = data;
3583 unsigned int new_index = shell->workspaces.current;
3590 change_workspace(shell, new_index);
3594 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3595 uint32_t key, void *data)
3597 struct desktop_shell *shell = data;
3598 unsigned int new_index = shell->workspaces.current;
3602 if (new_index < shell->workspaces.num - 1)
3605 change_workspace(shell, new_index);
3609 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3610 uint32_t key, void *data)
3612 struct desktop_shell *shell = data;
3613 unsigned int new_index;
3617 new_index = key - KEY_F1;
3618 if (new_index >= shell->workspaces.num)
3619 new_index = shell->workspaces.num - 1;
3621 change_workspace(shell, new_index);
3625 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3626 uint32_t key, void *data)
3628 struct desktop_shell *shell = data;
3629 unsigned int new_index = shell->workspaces.current;
3637 take_surface_to_workspace_by_seat(shell, seat, new_index);
3641 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3642 uint32_t key, void *data)
3644 struct desktop_shell *shell = data;
3645 unsigned int new_index = shell->workspaces.current;
3650 if (new_index < shell->workspaces.num - 1)
3653 take_surface_to_workspace_by_seat(shell, seat, new_index);
3657 shell_destroy(struct wl_listener *listener, void *data)
3659 struct desktop_shell *shell =
3660 container_of(listener, struct desktop_shell, destroy_listener);
3661 struct workspace **ws;
3663 if (shell->child.client)
3664 wl_client_destroy(shell->child.client);
3666 wl_list_remove(&shell->lock_listener.link);
3667 wl_list_remove(&shell->unlock_listener.link);
3668 wl_list_remove(&shell->show_input_panel_listener.link);
3669 wl_list_remove(&shell->hide_input_panel_listener.link);
3671 wl_array_for_each(ws, &shell->workspaces.array)
3672 workspace_destroy(*ws);
3673 wl_array_release(&shell->workspaces.array);
3675 free(shell->screensaver.path);
3680 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3683 int i, num_workspace_bindings;
3685 /* fixed bindings */
3686 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3687 MODIFIER_CTRL | MODIFIER_ALT,
3688 terminate_binding, ec);
3689 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3690 click_to_activate_binding,
3692 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3693 MODIFIER_SUPER | MODIFIER_ALT,
3694 surface_opacity_binding, NULL);
3695 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3696 MODIFIER_SUPER, zoom_axis_binding,
3699 /* configurable bindings */
3700 mod = shell->binding_modifier;
3701 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3702 zoom_key_binding, NULL);
3703 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3704 zoom_key_binding, NULL);
3705 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3707 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3708 resize_binding, shell);
3709 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3710 rotate_binding, NULL);
3711 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3713 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3715 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3716 backlight_binding, ec);
3717 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3719 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3720 backlight_binding, ec);
3721 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3722 debug_repaint_binding, shell);
3723 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3724 fan_debug_repaint_binding, shell);
3725 weston_compositor_add_key_binding(ec, KEY_K, mod,
3726 force_kill_binding, shell);
3727 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3728 workspace_up_binding, shell);
3729 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3730 workspace_down_binding, shell);
3731 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3732 workspace_move_surface_up_binding,
3734 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3735 workspace_move_surface_down_binding,
3738 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3739 if (shell->workspaces.num > 1) {
3740 num_workspace_bindings = shell->workspaces.num;
3741 if (num_workspace_bindings > 6)
3742 num_workspace_bindings = 6;
3743 for (i = 0; i < num_workspace_bindings; i++)
3744 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3745 workspace_f_binding,
3751 module_init(struct weston_compositor *ec)
3753 struct weston_seat *seat;
3754 struct desktop_shell *shell;
3755 struct workspace **pws;
3757 struct wl_event_loop *loop;
3759 shell = malloc(sizeof *shell);
3763 memset(shell, 0, sizeof *shell);
3764 shell->compositor = ec;
3766 shell->destroy_listener.notify = shell_destroy;
3767 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3768 shell->lock_listener.notify = lock;
3769 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3770 shell->unlock_listener.notify = unlock;
3771 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3772 shell->show_input_panel_listener.notify = show_input_panels;
3773 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3774 shell->hide_input_panel_listener.notify = hide_input_panels;
3775 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3776 ec->ping_handler = ping_handler;
3777 ec->shell_interface.shell = shell;
3778 ec->shell_interface.create_shell_surface = create_shell_surface;
3779 ec->shell_interface.set_toplevel = set_toplevel;
3780 ec->shell_interface.set_transient = set_transient;
3781 ec->shell_interface.move = surface_move;
3782 ec->shell_interface.resize = surface_resize;
3784 wl_list_init(&shell->screensaver.surfaces);
3785 wl_list_init(&shell->input_panel.surfaces);
3787 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3788 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3789 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3790 weston_layer_init(&shell->lock_layer, NULL);
3791 weston_layer_init(&shell->input_panel_layer, NULL);
3793 wl_array_init(&shell->workspaces.array);
3794 wl_list_init(&shell->workspaces.client_list);
3796 shell_configuration(shell);
3798 for (i = 0; i < shell->workspaces.num; i++) {
3799 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3803 *pws = workspace_create();
3807 activate_workspace(shell, 0);
3809 wl_list_init(&shell->workspaces.anim_sticky_list);
3810 wl_list_init(&shell->workspaces.animation.link);
3811 shell->workspaces.animation.frame = animate_workspace_change_frame;
3813 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3814 shell, bind_shell) == NULL)
3817 if (wl_display_add_global(ec->wl_display,
3818 &desktop_shell_interface,
3819 shell, bind_desktop_shell) == NULL)
3822 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3823 shell, bind_screensaver) == NULL)
3826 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3827 shell, bind_input_panel) == NULL)
3830 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3831 shell, bind_workspace_manager) == NULL)
3834 shell->child.deathstamp = weston_compositor_get_time();
3836 loop = wl_display_get_event_loop(ec->wl_display);
3837 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
3839 wl_list_for_each(seat, &ec->seat_list, link)
3840 create_pointer_focus_listener(seat);
3842 shell_add_bindings(ec, shell);