2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
33 #include <sys/types.h>
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "input-method-server-protocol.h"
39 #include "workspaces-server-protocol.h"
40 #include "../shared/config-parser.h"
42 #define DEFAULT_NUM_WORKSPACES 1
43 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
58 struct weston_seat *seat;
60 struct weston_surface *keyboard_focus;
62 struct wl_listener seat_destroy_listener;
63 struct wl_listener surface_destroy_listener;
67 struct weston_layer layer;
69 struct wl_list focus_list;
70 struct wl_listener seat_destroyed_listener;
73 struct input_panel_surface {
74 struct wl_resource resource;
76 struct desktop_shell *shell;
79 struct weston_surface *surface;
80 struct wl_listener surface_destroy_listener;
83 struct desktop_shell {
84 struct weston_compositor *compositor;
86 struct wl_listener idle_listener;
87 struct wl_listener wake_listener;
88 struct wl_listener destroy_listener;
89 struct wl_listener show_input_panel_listener;
90 struct wl_listener hide_input_panel_listener;
92 struct weston_layer fullscreen_layer;
93 struct weston_layer panel_layer;
94 struct weston_layer background_layer;
95 struct weston_layer lock_layer;
96 struct weston_layer input_panel_layer;
98 struct wl_listener pointer_focus_listener;
99 struct weston_surface *grab_surface;
102 struct weston_process process;
103 struct wl_client *client;
104 struct wl_resource *desktop_shell;
111 bool showing_input_panels;
112 bool prepare_event_sent;
114 struct weston_surface *lock_surface;
115 struct wl_listener lock_surface_listener;
118 struct wl_array array;
119 unsigned int current;
122 struct wl_list client_list;
124 struct weston_animation animation;
125 struct wl_list anim_sticky_list;
127 uint32_t anim_timestamp;
129 struct workspace *anim_from;
130 struct workspace *anim_to;
136 struct wl_resource *binding;
137 struct weston_process process;
138 struct wl_event_source *timer;
142 struct wl_resource *binding;
143 struct wl_list surfaces;
147 struct weston_surface *surface;
148 struct weston_surface_animation *animation;
152 uint32_t binding_modifier;
153 enum animation_type win_animation_type;
156 enum shell_surface_type {
158 SHELL_SURFACE_TOPLEVEL,
159 SHELL_SURFACE_TRANSIENT,
160 SHELL_SURFACE_FULLSCREEN,
161 SHELL_SURFACE_MAXIMIZED,
166 struct wl_event_source *source;
170 struct shell_surface {
171 struct wl_resource resource;
173 struct weston_surface *surface;
174 struct wl_listener surface_destroy_listener;
175 struct weston_surface *parent;
176 struct desktop_shell *shell;
178 enum shell_surface_type type, next_type;
180 int32_t saved_x, saved_y;
181 bool saved_position_valid;
182 bool saved_rotation_valid;
186 struct weston_transform transform;
187 struct weston_matrix rotation;
191 struct wl_list grab_link;
193 struct shell_seat *shseat;
203 enum wl_shell_surface_fullscreen_method type;
204 struct weston_transform transform; /* matrix from x, y */
206 struct weston_surface *black_surface;
209 struct ping_timer *ping_timer;
211 struct weston_transform workspace_transform;
213 struct weston_output *fullscreen_output;
214 struct weston_output *output;
217 const struct weston_shell_client *client;
221 struct wl_pointer_grab grab;
222 struct shell_surface *shsurf;
223 struct wl_listener shsurf_destroy_listener;
224 struct wl_pointer *pointer;
227 struct weston_move_grab {
228 struct shell_grab base;
233 struct shell_grab base;
234 struct weston_matrix rotation;
242 struct weston_seat *seat;
243 struct wl_listener seat_destroy_listener;
246 struct wl_pointer_grab grab;
247 struct wl_list surfaces_list;
248 struct wl_client *client;
254 activate(struct desktop_shell *shell, struct weston_surface *es,
255 struct weston_seat *seat);
257 static struct workspace *
258 get_current_workspace(struct desktop_shell *shell);
260 static struct shell_surface *
261 get_shell_surface(struct weston_surface *surface);
263 static struct desktop_shell *
264 shell_surface_get_shell(struct shell_surface *shsurf);
267 surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
270 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
272 struct desktop_shell *shell;
273 struct weston_surface *top_fs_es;
275 shell = shell_surface_get_shell(shsurf);
277 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
280 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
281 struct weston_surface,
283 return (shsurf == get_shell_surface(top_fs_es));
287 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
289 struct shell_grab *grab;
291 grab = container_of(listener, struct shell_grab,
292 shsurf_destroy_listener);
298 popup_grab_end(struct wl_pointer *pointer);
301 shell_grab_start(struct shell_grab *grab,
302 const struct wl_pointer_grab_interface *interface,
303 struct shell_surface *shsurf,
304 struct wl_pointer *pointer,
305 enum desktop_shell_cursor cursor)
307 struct desktop_shell *shell = shsurf->shell;
309 popup_grab_end(pointer);
311 grab->grab.interface = interface;
312 grab->shsurf = shsurf;
313 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
314 wl_signal_add(&shsurf->resource.destroy_signal,
315 &grab->shsurf_destroy_listener);
317 grab->pointer = pointer;
318 grab->grab.focus = &shsurf->surface->surface;
320 wl_pointer_start_grab(pointer, &grab->grab);
321 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
322 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
323 wl_fixed_from_int(0), wl_fixed_from_int(0));
327 shell_grab_end(struct shell_grab *grab)
330 wl_list_remove(&grab->shsurf_destroy_listener.link);
332 wl_pointer_end_grab(grab->pointer);
336 center_on_output(struct weston_surface *surface,
337 struct weston_output *output);
339 static enum weston_keyboard_modifier
340 get_modifier(char *modifier)
343 return MODIFIER_SUPER;
345 if (!strcmp("ctrl", modifier))
346 return MODIFIER_CTRL;
347 else if (!strcmp("alt", modifier))
349 else if (!strcmp("super", modifier))
350 return MODIFIER_SUPER;
352 return MODIFIER_SUPER;
355 static enum animation_type
356 get_animation_type(char *animation)
359 return ANIMATION_NONE;
361 if (!strcmp("zoom", animation))
362 return ANIMATION_ZOOM;
363 else if (!strcmp("fade", animation))
364 return ANIMATION_FADE;
366 return ANIMATION_NONE;
370 shell_configuration(struct desktop_shell *shell, const char *config_file)
374 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
375 char *modifier = NULL;
376 char *win_animation = NULL;
378 struct config_key shell_keys[] = {
379 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
380 { "animation", CONFIG_KEY_STRING, &win_animation},
382 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
385 struct config_key saver_keys[] = {
386 { "path", CONFIG_KEY_STRING, &path },
387 { "duration", CONFIG_KEY_INTEGER, &duration },
390 struct config_section cs[] = {
391 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
392 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
395 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
397 shell->screensaver.path = path;
398 shell->screensaver.duration = duration * 1000;
399 shell->binding_modifier = get_modifier(modifier);
400 shell->win_animation_type = get_animation_type(win_animation);
401 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
405 focus_state_destroy(struct focus_state *state)
407 wl_list_remove(&state->seat_destroy_listener.link);
408 wl_list_remove(&state->surface_destroy_listener.link);
413 focus_state_seat_destroy(struct wl_listener *listener, void *data)
415 struct focus_state *state = container_of(listener,
417 seat_destroy_listener);
419 wl_list_remove(&state->link);
420 focus_state_destroy(state);
424 focus_state_surface_destroy(struct wl_listener *listener, void *data)
426 struct focus_state *state = container_of(listener,
428 surface_destroy_listener);
429 struct desktop_shell *shell;
430 struct weston_surface *surface, *next;
433 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
434 if (surface == state->keyboard_focus)
442 shell = state->seat->compositor->shell_interface.shell;
443 activate(shell, next, state->seat);
445 wl_list_remove(&state->link);
446 focus_state_destroy(state);
450 static struct focus_state *
451 focus_state_create(struct weston_seat *seat, struct workspace *ws)
453 struct focus_state *state;
455 state = malloc(sizeof *state);
461 wl_list_insert(&ws->focus_list, &state->link);
463 state->seat_destroy_listener.notify = focus_state_seat_destroy;
464 state->surface_destroy_listener.notify = focus_state_surface_destroy;
465 wl_signal_add(&seat->seat.destroy_signal,
466 &state->seat_destroy_listener);
467 wl_list_init(&state->surface_destroy_listener.link);
472 static struct focus_state *
473 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
475 struct workspace *ws = get_current_workspace(shell);
476 struct focus_state *state;
478 wl_list_for_each(state, &ws->focus_list, link)
479 if (state->seat == seat)
482 if (&state->link == &ws->focus_list)
483 state = focus_state_create(seat, ws);
489 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
491 struct focus_state *state, *next;
492 struct wl_surface *surface;
494 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
495 surface = state->keyboard_focus ?
496 &state->keyboard_focus->surface : NULL;
498 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
503 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
504 struct weston_seat *seat)
506 struct focus_state *state;
507 struct wl_surface *surface;
509 wl_list_for_each(state, &ws->focus_list, link) {
510 if (state->seat == seat) {
511 surface = seat->seat.keyboard->focus;
512 state->keyboard_focus =
513 (struct weston_surface *) surface;
520 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
521 struct weston_surface *surface)
523 struct focus_state *state;
525 wl_list_for_each(state, &ws->focus_list, link)
526 if (state->keyboard_focus == surface)
527 state->keyboard_focus = NULL;
531 workspace_destroy(struct workspace *ws)
533 struct focus_state *state, *next;
535 wl_list_for_each_safe(state, next, &ws->focus_list, link)
536 focus_state_destroy(state);
542 seat_destroyed(struct wl_listener *listener, void *data)
544 struct weston_seat *seat = data;
545 struct focus_state *state, *next;
546 struct workspace *ws = container_of(listener,
548 seat_destroyed_listener);
550 wl_list_for_each_safe(state, next, &ws->focus_list, link)
551 if (state->seat == seat)
552 wl_list_remove(&state->link);
555 static struct workspace *
556 workspace_create(void)
558 struct workspace *ws = malloc(sizeof *ws);
562 weston_layer_init(&ws->layer, NULL);
564 wl_list_init(&ws->focus_list);
565 wl_list_init(&ws->seat_destroyed_listener.link);
566 ws->seat_destroyed_listener.notify = seat_destroyed;
572 workspace_is_empty(struct workspace *ws)
574 return wl_list_empty(&ws->layer.surface_list);
577 static struct workspace *
578 get_workspace(struct desktop_shell *shell, unsigned int index)
580 struct workspace **pws = shell->workspaces.array.data;
581 assert(index < shell->workspaces.num);
586 static struct workspace *
587 get_current_workspace(struct desktop_shell *shell)
589 return get_workspace(shell, shell->workspaces.current);
593 activate_workspace(struct desktop_shell *shell, unsigned int index)
595 struct workspace *ws;
597 ws = get_workspace(shell, index);
598 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
600 shell->workspaces.current = index;
604 get_output_height(struct weston_output *output)
606 return abs(output->region.extents.y1 - output->region.extents.y2);
610 surface_translate(struct weston_surface *surface, double d)
612 struct shell_surface *shsurf = get_shell_surface(surface);
613 struct weston_transform *transform;
615 transform = &shsurf->workspace_transform;
616 if (wl_list_empty(&transform->link))
617 wl_list_insert(surface->geometry.transformation_list.prev,
618 &shsurf->workspace_transform.link);
620 weston_matrix_init(&shsurf->workspace_transform.matrix);
621 weston_matrix_translate(&shsurf->workspace_transform.matrix,
623 weston_surface_geometry_dirty(surface);
627 workspace_translate_out(struct workspace *ws, double fraction)
629 struct weston_surface *surface;
633 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
634 height = get_output_height(surface->output);
635 d = height * fraction;
637 surface_translate(surface, d);
642 workspace_translate_in(struct workspace *ws, double fraction)
644 struct weston_surface *surface;
648 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
649 height = get_output_height(surface->output);
652 d = -(height - height * fraction);
654 d = height + height * fraction;
656 surface_translate(surface, d);
661 broadcast_current_workspace_state(struct desktop_shell *shell)
663 struct wl_resource *resource;
665 wl_list_for_each(resource, &shell->workspaces.client_list, link)
666 workspace_manager_send_state(resource,
667 shell->workspaces.current,
668 shell->workspaces.num);
672 reverse_workspace_change_animation(struct desktop_shell *shell,
674 struct workspace *from,
675 struct workspace *to)
677 shell->workspaces.current = index;
679 shell->workspaces.anim_to = to;
680 shell->workspaces.anim_from = from;
681 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
682 shell->workspaces.anim_timestamp = 0;
684 weston_compositor_schedule_repaint(shell->compositor);
688 workspace_deactivate_transforms(struct workspace *ws)
690 struct weston_surface *surface;
691 struct shell_surface *shsurf;
693 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
694 shsurf = get_shell_surface(surface);
695 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
696 wl_list_remove(&shsurf->workspace_transform.link);
697 wl_list_init(&shsurf->workspace_transform.link);
699 weston_surface_geometry_dirty(surface);
704 finish_workspace_change_animation(struct desktop_shell *shell,
705 struct workspace *from,
706 struct workspace *to)
708 weston_compositor_schedule_repaint(shell->compositor);
710 wl_list_remove(&shell->workspaces.animation.link);
711 workspace_deactivate_transforms(from);
712 workspace_deactivate_transforms(to);
713 shell->workspaces.anim_to = NULL;
715 wl_list_remove(&shell->workspaces.anim_from->layer.link);
719 animate_workspace_change_frame(struct weston_animation *animation,
720 struct weston_output *output, uint32_t msecs)
722 struct desktop_shell *shell =
723 container_of(animation, struct desktop_shell,
724 workspaces.animation);
725 struct workspace *from = shell->workspaces.anim_from;
726 struct workspace *to = shell->workspaces.anim_to;
730 if (workspace_is_empty(from) && workspace_is_empty(to)) {
731 finish_workspace_change_animation(shell, from, to);
735 if (shell->workspaces.anim_timestamp == 0) {
736 if (shell->workspaces.anim_current == 0.0)
737 shell->workspaces.anim_timestamp = msecs;
739 shell->workspaces.anim_timestamp =
741 /* Invers of movement function 'y' below. */
742 (asin(1.0 - shell->workspaces.anim_current) *
743 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
747 t = msecs - shell->workspaces.anim_timestamp;
753 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
756 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
757 weston_compositor_schedule_repaint(shell->compositor);
759 workspace_translate_out(from, shell->workspaces.anim_dir * y);
760 workspace_translate_in(to, shell->workspaces.anim_dir * y);
761 shell->workspaces.anim_current = y;
763 weston_compositor_schedule_repaint(shell->compositor);
766 finish_workspace_change_animation(shell, from, to);
770 animate_workspace_change(struct desktop_shell *shell,
772 struct workspace *from,
773 struct workspace *to)
775 struct weston_output *output;
779 if (index > shell->workspaces.current)
784 shell->workspaces.current = index;
786 shell->workspaces.anim_dir = dir;
787 shell->workspaces.anim_from = from;
788 shell->workspaces.anim_to = to;
789 shell->workspaces.anim_current = 0.0;
790 shell->workspaces.anim_timestamp = 0;
792 output = container_of(shell->compositor->output_list.next,
793 struct weston_output, link);
794 wl_list_insert(&output->animation_list,
795 &shell->workspaces.animation.link);
797 wl_list_insert(from->layer.link.prev, &to->layer.link);
799 workspace_translate_in(to, 0);
801 restore_focus_state(shell, to);
803 weston_compositor_schedule_repaint(shell->compositor);
807 update_workspace(struct desktop_shell *shell, unsigned int index,
808 struct workspace *from, struct workspace *to)
810 shell->workspaces.current = index;
811 wl_list_insert(&from->layer.link, &to->layer.link);
812 wl_list_remove(&from->layer.link);
816 change_workspace(struct desktop_shell *shell, unsigned int index)
818 struct workspace *from;
819 struct workspace *to;
821 if (index == shell->workspaces.current)
824 /* Don't change workspace when there is any fullscreen surfaces. */
825 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
828 from = get_current_workspace(shell);
829 to = get_workspace(shell, index);
831 if (shell->workspaces.anim_from == to &&
832 shell->workspaces.anim_to == from) {
833 restore_focus_state(shell, to);
834 reverse_workspace_change_animation(shell, index, from, to);
835 broadcast_current_workspace_state(shell);
839 if (shell->workspaces.anim_to != NULL)
840 finish_workspace_change_animation(shell,
841 shell->workspaces.anim_from,
842 shell->workspaces.anim_to);
844 restore_focus_state(shell, to);
846 if (workspace_is_empty(to) && workspace_is_empty(from))
847 update_workspace(shell, index, from, to);
849 animate_workspace_change(shell, index, from, to);
851 broadcast_current_workspace_state(shell);
855 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
857 struct wl_list *list = &ws->layer.surface_list;
860 if (wl_list_empty(list))
868 return container_of(e, struct weston_surface, layer_link) == surface;
872 move_surface_to_workspace(struct desktop_shell *shell,
873 struct weston_surface *surface,
876 struct workspace *from;
877 struct workspace *to;
878 struct weston_seat *seat;
880 if (workspace == shell->workspaces.current)
883 if (workspace >= shell->workspaces.num)
884 workspace = shell->workspaces.num - 1;
886 from = get_current_workspace(shell);
887 to = get_workspace(shell, workspace);
889 wl_list_remove(&surface->layer_link);
890 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
892 drop_focus_state(shell, from, surface);
893 wl_list_for_each(seat, &shell->compositor->seat_list, link)
894 if (seat->has_keyboard &&
895 seat->keyboard.keyboard.focus == &surface->surface)
896 wl_keyboard_set_focus(&seat->keyboard.keyboard, NULL);
898 weston_surface_damage_below(surface);
902 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
903 struct wl_seat *wl_seat,
906 struct weston_seat *seat = (struct weston_seat *) wl_seat;
907 struct weston_surface *surface =
908 (struct weston_surface *) wl_seat->keyboard->focus;
909 struct shell_surface *shsurf;
910 struct workspace *from;
911 struct workspace *to;
912 struct focus_state *state;
914 if (surface == NULL ||
915 index == shell->workspaces.current)
918 from = get_current_workspace(shell);
919 to = get_workspace(shell, index);
921 wl_list_remove(&surface->layer_link);
922 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
924 replace_focus_state(shell, to, seat);
925 drop_focus_state(shell, from, surface);
927 if (shell->workspaces.anim_from == to &&
928 shell->workspaces.anim_to == from) {
929 wl_list_remove(&to->layer.link);
930 wl_list_insert(from->layer.link.prev, &to->layer.link);
932 reverse_workspace_change_animation(shell, index, from, to);
933 broadcast_current_workspace_state(shell);
938 if (shell->workspaces.anim_to != NULL)
939 finish_workspace_change_animation(shell,
940 shell->workspaces.anim_from,
941 shell->workspaces.anim_to);
943 if (workspace_is_empty(from) &&
944 workspace_has_only(to, surface))
945 update_workspace(shell, index, from, to);
947 shsurf = get_shell_surface(surface);
948 if (wl_list_empty(&shsurf->workspace_transform.link))
949 wl_list_insert(&shell->workspaces.anim_sticky_list,
950 &shsurf->workspace_transform.link);
952 animate_workspace_change(shell, index, from, to);
955 broadcast_current_workspace_state(shell);
957 state = ensure_focus_state(shell, seat);
959 state->keyboard_focus = surface;
963 workspace_manager_move_surface(struct wl_client *client,
964 struct wl_resource *resource,
965 struct wl_resource *surface_resource,
968 struct desktop_shell *shell = resource->data;
969 struct weston_surface *surface =
970 (struct weston_surface *) surface_resource;
972 move_surface_to_workspace(shell, surface, workspace);
975 static const struct workspace_manager_interface workspace_manager_implementation = {
976 workspace_manager_move_surface,
980 unbind_resource(struct wl_resource *resource)
982 wl_list_remove(&resource->link);
987 bind_workspace_manager(struct wl_client *client,
988 void *data, uint32_t version, uint32_t id)
990 struct desktop_shell *shell = data;
991 struct wl_resource *resource;
993 resource = wl_client_add_object(client, &workspace_manager_interface,
994 &workspace_manager_implementation,
997 if (resource == NULL) {
998 weston_log("couldn't add workspace manager object");
1002 resource->destroy = unbind_resource;
1003 wl_list_insert(&shell->workspaces.client_list, &resource->link);
1005 workspace_manager_send_state(resource,
1006 shell->workspaces.current,
1007 shell->workspaces.num);
1011 noop_grab_focus(struct wl_pointer_grab *grab,
1012 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
1018 move_grab_motion(struct wl_pointer_grab *grab,
1019 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1021 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1022 struct wl_pointer *pointer = grab->pointer;
1023 struct shell_surface *shsurf = move->base.shsurf;
1024 struct weston_surface *es;
1025 int dx = wl_fixed_to_int(pointer->x + move->dx);
1026 int dy = wl_fixed_to_int(pointer->y + move->dy);
1031 es = shsurf->surface;
1033 weston_surface_configure(es, dx, dy,
1034 es->geometry.width, es->geometry.height);
1036 weston_compositor_schedule_repaint(es->compositor);
1040 move_grab_button(struct wl_pointer_grab *grab,
1041 uint32_t time, uint32_t button, uint32_t state_w)
1043 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1045 struct wl_pointer *pointer = grab->pointer;
1046 enum wl_pointer_button_state state = state_w;
1048 if (pointer->button_count == 0 &&
1049 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1050 shell_grab_end(shell_grab);
1055 static const struct wl_pointer_grab_interface move_grab_interface = {
1062 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1064 struct weston_move_grab *move;
1069 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1072 move = malloc(sizeof *move);
1076 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1077 ws->seat.pointer->grab_x;
1078 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1079 ws->seat.pointer->grab_y;
1081 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1082 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1088 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1089 struct wl_resource *seat_resource, uint32_t serial)
1091 struct weston_seat *ws = seat_resource->data;
1092 struct shell_surface *shsurf = resource->data;
1094 if (ws->seat.pointer->button_count == 0 ||
1095 ws->seat.pointer->grab_serial != serial ||
1096 ws->seat.pointer->focus != &shsurf->surface->surface)
1099 if (surface_move(shsurf, ws) < 0)
1100 wl_resource_post_no_memory(resource);
1103 struct weston_resize_grab {
1104 struct shell_grab base;
1106 int32_t width, height;
1110 resize_grab_motion(struct wl_pointer_grab *grab,
1111 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1113 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1114 struct wl_pointer *pointer = grab->pointer;
1115 struct shell_surface *shsurf = resize->base.shsurf;
1116 int32_t width, height;
1117 wl_fixed_t from_x, from_y;
1118 wl_fixed_t to_x, to_y;
1123 weston_surface_from_global_fixed(shsurf->surface,
1124 pointer->grab_x, pointer->grab_y,
1126 weston_surface_from_global_fixed(shsurf->surface,
1127 pointer->x, pointer->y, &to_x, &to_y);
1129 width = resize->width;
1130 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1131 width += wl_fixed_to_int(from_x - to_x);
1132 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1133 width += wl_fixed_to_int(to_x - from_x);
1136 height = resize->height;
1137 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1138 height += wl_fixed_to_int(from_y - to_y);
1139 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1140 height += wl_fixed_to_int(to_y - from_y);
1143 shsurf->client->send_configure(shsurf->surface,
1144 resize->edges, width, height);
1148 send_configure(struct weston_surface *surface,
1149 uint32_t edges, int32_t width, int32_t height)
1151 struct shell_surface *shsurf = get_shell_surface(surface);
1153 wl_shell_surface_send_configure(&shsurf->resource,
1154 edges, width, height);
1157 static const struct weston_shell_client shell_client = {
1162 resize_grab_button(struct wl_pointer_grab *grab,
1163 uint32_t time, uint32_t button, uint32_t state_w)
1165 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1166 struct wl_pointer *pointer = grab->pointer;
1167 enum wl_pointer_button_state state = state_w;
1169 if (pointer->button_count == 0 &&
1170 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1171 shell_grab_end(&resize->base);
1176 static const struct wl_pointer_grab_interface resize_grab_interface = {
1183 surface_resize(struct shell_surface *shsurf,
1184 struct weston_seat *ws, uint32_t edges)
1186 struct weston_resize_grab *resize;
1188 if (shsurf->type == SHELL_SURFACE_FULLSCREEN ||
1189 shsurf->type == SHELL_SURFACE_MAXIMIZED)
1192 if (edges == 0 || edges > 15 ||
1193 (edges & 3) == 3 || (edges & 12) == 12)
1196 resize = malloc(sizeof *resize);
1200 resize->edges = edges;
1201 resize->width = shsurf->surface->geometry.width;
1202 resize->height = shsurf->surface->geometry.height;
1204 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1205 ws->seat.pointer, edges);
1211 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1212 struct wl_resource *seat_resource, uint32_t serial,
1215 struct weston_seat *ws = seat_resource->data;
1216 struct shell_surface *shsurf = resource->data;
1218 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1221 if (ws->seat.pointer->button_count == 0 ||
1222 ws->seat.pointer->grab_serial != serial ||
1223 ws->seat.pointer->focus != &shsurf->surface->surface)
1226 if (surface_resize(shsurf, ws, edges) < 0)
1227 wl_resource_post_no_memory(resource);
1231 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1232 struct wl_surface *surface, int32_t x, int32_t y)
1234 struct shell_grab *grab = (struct shell_grab *) base;
1236 if (grab->grab.focus != surface) {
1237 shell_grab_end(grab);
1243 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1244 uint32_t time, int32_t x, int32_t y)
1249 busy_cursor_grab_button(struct wl_pointer_grab *base,
1250 uint32_t time, uint32_t button, uint32_t state)
1252 struct shell_grab *grab = (struct shell_grab *) base;
1253 struct shell_surface *shsurf;
1254 struct weston_surface *surface =
1255 (struct weston_surface *) grab->grab.pointer->current;
1256 struct weston_seat *seat =
1257 (struct weston_seat *) grab->grab.pointer->seat;
1259 shsurf = get_shell_surface(surface);
1260 if (shsurf && button == BTN_LEFT && state) {
1261 activate(shsurf->shell, shsurf->surface, seat);
1262 surface_move(shsurf, seat);
1263 } else if (shsurf && button == BTN_RIGHT && state) {
1264 activate(shsurf->shell, shsurf->surface, seat);
1265 surface_rotate(shsurf, &seat->seat);
1269 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1270 busy_cursor_grab_focus,
1271 busy_cursor_grab_motion,
1272 busy_cursor_grab_button,
1276 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1278 struct shell_grab *grab;
1280 grab = malloc(sizeof *grab);
1284 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1285 DESKTOP_SHELL_CURSOR_BUSY);
1289 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1291 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1293 if (grab->grab.interface == &busy_cursor_grab_interface) {
1294 shell_grab_end(grab);
1300 ping_timer_destroy(struct shell_surface *shsurf)
1302 if (!shsurf || !shsurf->ping_timer)
1305 if (shsurf->ping_timer->source)
1306 wl_event_source_remove(shsurf->ping_timer->source);
1308 free(shsurf->ping_timer);
1309 shsurf->ping_timer = NULL;
1313 ping_timeout_handler(void *data)
1315 struct shell_surface *shsurf = data;
1316 struct weston_seat *seat;
1318 /* Client is not responding */
1319 shsurf->unresponsive = 1;
1321 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1322 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1323 set_busy_cursor(shsurf, seat->seat.pointer);
1329 ping_handler(struct weston_surface *surface, uint32_t serial)
1331 struct shell_surface *shsurf = get_shell_surface(surface);
1332 struct wl_event_loop *loop;
1333 int ping_timeout = 200;
1337 if (!shsurf->resource.client)
1340 if (shsurf->surface == shsurf->shell->grab_surface)
1343 if (!shsurf->ping_timer) {
1344 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1345 if (!shsurf->ping_timer)
1348 shsurf->ping_timer->serial = serial;
1349 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1350 shsurf->ping_timer->source =
1351 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1352 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1354 wl_shell_surface_send_ping(&shsurf->resource, serial);
1359 handle_pointer_focus(struct wl_listener *listener, void *data)
1361 struct wl_pointer *pointer = data;
1362 struct weston_surface *surface =
1363 (struct weston_surface *) pointer->focus;
1364 struct weston_compositor *compositor;
1365 struct shell_surface *shsurf;
1371 compositor = surface->compositor;
1372 shsurf = get_shell_surface(surface);
1374 if (shsurf && shsurf->unresponsive) {
1375 set_busy_cursor(shsurf, pointer);
1377 serial = wl_display_next_serial(compositor->wl_display);
1378 ping_handler(surface, serial);
1383 create_pointer_focus_listener(struct weston_seat *seat)
1385 struct wl_listener *listener;
1387 if (!seat->seat.pointer)
1390 listener = malloc(sizeof *listener);
1391 listener->notify = handle_pointer_focus;
1392 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1396 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1399 struct shell_surface *shsurf = resource->data;
1400 struct desktop_shell *shell = shsurf->shell;
1401 struct weston_seat *seat;
1402 struct weston_compositor *ec = shsurf->surface->compositor;
1403 struct wl_pointer *pointer;
1404 int was_unresponsive;
1406 if (shsurf->ping_timer == NULL)
1407 /* Just ignore unsolicited pong. */
1410 if (shsurf->ping_timer->serial == serial) {
1411 was_unresponsive = shsurf->unresponsive;
1412 shsurf->unresponsive = 0;
1413 if (was_unresponsive) {
1414 /* Received pong from previously unresponsive client */
1415 wl_list_for_each(seat, &ec->seat_list, link) {
1416 pointer = seat->seat.pointer;
1417 if (pointer->focus ==
1418 &shell->grab_surface->surface &&
1420 &shsurf->surface->surface)
1421 end_busy_cursor(shsurf, pointer);
1424 ping_timer_destroy(shsurf);
1429 shell_surface_set_title(struct wl_client *client,
1430 struct wl_resource *resource, const char *title)
1432 struct shell_surface *shsurf = resource->data;
1434 free(shsurf->title);
1435 shsurf->title = strdup(title);
1439 shell_surface_set_class(struct wl_client *client,
1440 struct wl_resource *resource, const char *class)
1442 struct shell_surface *shsurf = resource->data;
1444 free(shsurf->class);
1445 shsurf->class = strdup(class);
1448 static struct weston_output *
1449 get_default_output(struct weston_compositor *compositor)
1451 return container_of(compositor->output_list.next,
1452 struct weston_output, link);
1456 shell_unset_fullscreen(struct shell_surface *shsurf)
1458 struct workspace *ws;
1459 /* undo all fullscreen things here */
1460 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1461 shell_surface_is_top_fullscreen(shsurf)) {
1462 weston_output_switch_mode(shsurf->fullscreen_output,
1463 shsurf->fullscreen_output->origin);
1465 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1466 shsurf->fullscreen.framerate = 0;
1467 wl_list_remove(&shsurf->fullscreen.transform.link);
1468 wl_list_init(&shsurf->fullscreen.transform.link);
1469 if (shsurf->fullscreen.black_surface)
1470 weston_surface_destroy(shsurf->fullscreen.black_surface);
1471 shsurf->fullscreen.black_surface = NULL;
1472 shsurf->fullscreen_output = NULL;
1473 weston_surface_set_position(shsurf->surface,
1474 shsurf->saved_x, shsurf->saved_y);
1475 if (shsurf->saved_rotation_valid) {
1476 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1477 &shsurf->rotation.transform.link);
1478 shsurf->saved_rotation_valid = false;
1481 ws = get_current_workspace(shsurf->shell);
1482 wl_list_remove(&shsurf->surface->layer_link);
1483 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1487 shell_unset_maximized(struct shell_surface *shsurf)
1489 struct workspace *ws;
1490 /* undo all maximized things here */
1491 shsurf->output = get_default_output(shsurf->surface->compositor);
1492 weston_surface_set_position(shsurf->surface,
1496 if (shsurf->saved_rotation_valid) {
1497 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1498 &shsurf->rotation.transform.link);
1499 shsurf->saved_rotation_valid = false;
1502 ws = get_current_workspace(shsurf->shell);
1503 wl_list_remove(&shsurf->surface->layer_link);
1504 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1508 reset_shell_surface_type(struct shell_surface *surface)
1510 switch (surface->type) {
1511 case SHELL_SURFACE_FULLSCREEN:
1512 shell_unset_fullscreen(surface);
1514 case SHELL_SURFACE_MAXIMIZED:
1515 shell_unset_maximized(surface);
1517 case SHELL_SURFACE_NONE:
1518 case SHELL_SURFACE_TOPLEVEL:
1519 case SHELL_SURFACE_TRANSIENT:
1520 case SHELL_SURFACE_POPUP:
1524 surface->type = SHELL_SURFACE_NONE;
1529 set_surface_type(struct shell_surface *shsurf)
1531 struct weston_surface *surface = shsurf->surface;
1532 struct weston_surface *pes = shsurf->parent;
1534 reset_shell_surface_type(shsurf);
1536 shsurf->type = shsurf->next_type;
1537 shsurf->next_type = SHELL_SURFACE_NONE;
1539 switch (shsurf->type) {
1540 case SHELL_SURFACE_TOPLEVEL:
1542 case SHELL_SURFACE_TRANSIENT:
1543 weston_surface_set_position(surface,
1544 pes->geometry.x + shsurf->transient.x,
1545 pes->geometry.y + shsurf->transient.y);
1548 case SHELL_SURFACE_MAXIMIZED:
1549 case SHELL_SURFACE_FULLSCREEN:
1550 shsurf->saved_x = surface->geometry.x;
1551 shsurf->saved_y = surface->geometry.y;
1552 shsurf->saved_position_valid = true;
1554 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1555 wl_list_remove(&shsurf->rotation.transform.link);
1556 wl_list_init(&shsurf->rotation.transform.link);
1557 weston_surface_geometry_dirty(shsurf->surface);
1558 shsurf->saved_rotation_valid = true;
1568 set_toplevel(struct shell_surface *shsurf)
1570 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1574 shell_surface_set_toplevel(struct wl_client *client,
1575 struct wl_resource *resource)
1577 struct shell_surface *surface = resource->data;
1579 set_toplevel(surface);
1583 set_transient(struct shell_surface *shsurf,
1584 struct weston_surface *parent, int x, int y, uint32_t flags)
1586 /* assign to parents output */
1587 shsurf->parent = parent;
1588 shsurf->transient.x = x;
1589 shsurf->transient.y = y;
1590 shsurf->transient.flags = flags;
1591 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1595 shell_surface_set_transient(struct wl_client *client,
1596 struct wl_resource *resource,
1597 struct wl_resource *parent_resource,
1598 int x, int y, uint32_t flags)
1600 struct shell_surface *shsurf = resource->data;
1601 struct weston_surface *parent = parent_resource->data;
1603 set_transient(shsurf, parent, x, y, flags);
1606 static struct desktop_shell *
1607 shell_surface_get_shell(struct shell_surface *shsurf)
1609 return shsurf->shell;
1613 get_output_panel_height(struct desktop_shell *shell,
1614 struct weston_output *output)
1616 struct weston_surface *surface;
1617 int panel_height = 0;
1622 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1623 if (surface->output == output) {
1624 panel_height = surface->geometry.height;
1629 return panel_height;
1633 shell_surface_set_maximized(struct wl_client *client,
1634 struct wl_resource *resource,
1635 struct wl_resource *output_resource )
1637 struct shell_surface *shsurf = resource->data;
1638 struct weston_surface *es = shsurf->surface;
1639 struct desktop_shell *shell = NULL;
1640 uint32_t edges = 0, panel_height = 0;
1642 /* get the default output, if the client set it as NULL
1643 check whether the ouput is available */
1644 if (output_resource)
1645 shsurf->output = output_resource->data;
1646 else if (es->output)
1647 shsurf->output = es->output;
1649 shsurf->output = get_default_output(es->compositor);
1651 shell = shell_surface_get_shell(shsurf);
1652 panel_height = get_output_panel_height(shell, shsurf->output);
1653 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1655 shsurf->client->send_configure(shsurf->surface, edges,
1656 shsurf->output->width,
1657 shsurf->output->height - panel_height);
1659 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1663 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height);
1665 static struct weston_surface *
1666 create_black_surface(struct weston_compositor *ec,
1667 struct weston_surface *fs_surface,
1668 float x, float y, int w, int h)
1670 struct weston_surface *surface = NULL;
1672 surface = weston_surface_create(ec);
1673 if (surface == NULL) {
1674 weston_log("no memory\n");
1678 surface->configure = black_surface_configure;
1679 surface->configure_private = fs_surface;
1680 weston_surface_configure(surface, x, y, w, h);
1681 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1682 pixman_region32_fini(&surface->opaque);
1683 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1684 pixman_region32_fini(&surface->input);
1685 pixman_region32_init_rect(&surface->input, 0, 0, w, h);
1690 /* Create black surface and append it to the associated fullscreen surface.
1691 * Handle size dismatch and positioning according to the method. */
1693 shell_configure_fullscreen(struct shell_surface *shsurf)
1695 struct weston_output *output = shsurf->fullscreen_output;
1696 struct weston_surface *surface = shsurf->surface;
1697 struct weston_matrix *matrix;
1698 float scale, output_aspect, surface_aspect, x, y;
1700 if (!shsurf->fullscreen.black_surface)
1701 shsurf->fullscreen.black_surface =
1702 create_black_surface(surface->compositor,
1704 output->x, output->y,
1708 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1709 wl_list_insert(&surface->layer_link,
1710 &shsurf->fullscreen.black_surface->layer_link);
1711 shsurf->fullscreen.black_surface->output = output;
1713 switch (shsurf->fullscreen.type) {
1714 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1715 if (surface->buffer_ref.buffer)
1716 center_on_output(surface, shsurf->fullscreen_output);
1718 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1719 /* 1:1 mapping between surface and output dimensions */
1720 if (output->width == surface->geometry.width &&
1721 output->height == surface->geometry.height) {
1722 weston_surface_set_position(surface, output->x, output->y);
1726 matrix = &shsurf->fullscreen.transform.matrix;
1727 weston_matrix_init(matrix);
1729 output_aspect = (float) output->width /
1730 (float) output->height;
1731 surface_aspect = (float) surface->geometry.width /
1732 (float) surface->geometry.height;
1733 if (output_aspect < surface_aspect)
1734 scale = (float) output->width /
1735 (float) surface->geometry.width;
1737 scale = (float) output->height /
1738 (float) surface->geometry.height;
1740 weston_matrix_scale(matrix, scale, scale, 1);
1741 wl_list_remove(&shsurf->fullscreen.transform.link);
1742 wl_list_insert(&surface->geometry.transformation_list,
1743 &shsurf->fullscreen.transform.link);
1744 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1745 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1746 weston_surface_set_position(surface, x, y);
1749 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1750 if (shell_surface_is_top_fullscreen(shsurf)) {
1751 struct weston_mode mode = {0,
1752 surface->geometry.width,
1753 surface->geometry.height,
1754 shsurf->fullscreen.framerate};
1756 if (weston_output_switch_mode(output, &mode) == 0) {
1757 weston_surface_configure(shsurf->fullscreen.black_surface,
1758 output->x, output->y,
1761 weston_surface_set_position(surface, output->x, output->y);
1766 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1773 /* make the fullscreen and black surface at the top */
1775 shell_stack_fullscreen(struct shell_surface *shsurf)
1777 struct weston_output *output = shsurf->fullscreen_output;
1778 struct weston_surface *surface = shsurf->surface;
1779 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1781 wl_list_remove(&surface->layer_link);
1782 wl_list_insert(&shell->fullscreen_layer.surface_list,
1783 &surface->layer_link);
1784 weston_surface_damage(surface);
1786 if (!shsurf->fullscreen.black_surface)
1787 shsurf->fullscreen.black_surface =
1788 create_black_surface(surface->compositor,
1790 output->x, output->y,
1794 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1795 wl_list_insert(&surface->layer_link,
1796 &shsurf->fullscreen.black_surface->layer_link);
1797 weston_surface_damage(shsurf->fullscreen.black_surface);
1801 shell_map_fullscreen(struct shell_surface *shsurf)
1803 shell_stack_fullscreen(shsurf);
1804 shell_configure_fullscreen(shsurf);
1808 set_fullscreen(struct shell_surface *shsurf,
1811 struct weston_output *output)
1813 struct weston_surface *es = shsurf->surface;
1816 shsurf->output = output;
1817 else if (es->output)
1818 shsurf->output = es->output;
1820 shsurf->output = get_default_output(es->compositor);
1822 shsurf->fullscreen_output = shsurf->output;
1823 shsurf->fullscreen.type = method;
1824 shsurf->fullscreen.framerate = framerate;
1825 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1827 shsurf->client->send_configure(shsurf->surface, 0,
1828 shsurf->output->width,
1829 shsurf->output->height);
1833 shell_surface_set_fullscreen(struct wl_client *client,
1834 struct wl_resource *resource,
1837 struct wl_resource *output_resource)
1839 struct shell_surface *shsurf = resource->data;
1840 struct weston_output *output;
1842 if (output_resource)
1843 output = output_resource->data;
1847 set_fullscreen(shsurf, method, framerate, output);
1850 static const struct wl_pointer_grab_interface popup_grab_interface;
1853 destroy_shell_seat(struct wl_listener *listener, void *data)
1855 struct shell_seat *shseat =
1856 container_of(listener,
1857 struct shell_seat, seat_destroy_listener);
1858 struct shell_surface *shsurf, *prev = NULL;
1860 if (shseat->popup_grab.grab.interface == &popup_grab_interface) {
1861 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
1862 shseat->popup_grab.client = NULL;
1864 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1865 shsurf->popup.shseat = NULL;
1867 wl_list_init(&prev->popup.grab_link);
1871 wl_list_init(&prev->popup.grab_link);
1874 wl_list_remove(&shseat->seat_destroy_listener.link);
1878 static struct shell_seat *
1879 create_shell_seat(struct weston_seat *seat)
1881 struct shell_seat *shseat;
1883 shseat = calloc(1, sizeof *shseat);
1885 weston_log("no memory to allocate shell seat\n");
1889 shseat->seat = seat;
1890 wl_list_init(&shseat->popup_grab.surfaces_list);
1892 shseat->seat_destroy_listener.notify = destroy_shell_seat;
1893 wl_signal_add(&seat->destroy_signal,
1894 &shseat->seat_destroy_listener);
1899 static struct shell_seat *
1900 get_shell_seat(struct weston_seat *seat)
1902 struct wl_listener *listener;
1904 listener = wl_signal_get(&seat->destroy_signal, destroy_shell_seat);
1905 if (listener == NULL)
1906 return create_shell_seat(seat);
1908 return container_of(listener,
1909 struct shell_seat, seat_destroy_listener);
1913 popup_grab_focus(struct wl_pointer_grab *grab,
1914 struct wl_surface *surface,
1918 struct wl_pointer *pointer = grab->pointer;
1919 struct shell_seat *shseat =
1920 container_of(grab, struct shell_seat, popup_grab.grab);
1921 struct wl_client *client = shseat->popup_grab.client;
1923 if (surface && surface->resource.client == client) {
1924 wl_pointer_set_focus(pointer, surface, x, y);
1925 grab->focus = surface;
1927 wl_pointer_set_focus(pointer, NULL,
1928 wl_fixed_from_int(0),
1929 wl_fixed_from_int(0));
1935 popup_grab_motion(struct wl_pointer_grab *grab,
1936 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1938 struct wl_resource *resource;
1940 resource = grab->pointer->focus_resource;
1942 wl_pointer_send_motion(resource, time, sx, sy);
1946 popup_grab_button(struct wl_pointer_grab *grab,
1947 uint32_t time, uint32_t button, uint32_t state_w)
1949 struct wl_resource *resource;
1950 struct shell_seat *shseat =
1951 container_of(grab, struct shell_seat, popup_grab.grab);
1952 struct wl_display *display;
1953 enum wl_pointer_button_state state = state_w;
1956 resource = grab->pointer->focus_resource;
1958 display = wl_client_get_display(resource->client);
1959 serial = wl_display_get_serial(display);
1960 wl_pointer_send_button(resource, serial, time, button, state);
1961 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1962 (shseat->popup_grab.initial_up ||
1963 time - shseat->seat->pointer.grab_time > 500)) {
1964 popup_grab_end(grab->pointer);
1967 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1968 shseat->popup_grab.initial_up = 1;
1971 static const struct wl_pointer_grab_interface popup_grab_interface = {
1978 popup_grab_end(struct wl_pointer *pointer)
1980 struct wl_pointer_grab *grab = pointer->grab;
1981 struct shell_seat *shseat =
1982 container_of(grab, struct shell_seat, popup_grab.grab);
1983 struct shell_surface *shsurf;
1984 struct shell_surface *prev = NULL;
1986 if (pointer->grab->interface == &popup_grab_interface) {
1987 wl_pointer_end_grab(grab->pointer);
1988 shseat->popup_grab.client = NULL;
1989 shseat->popup_grab.grab.interface = NULL;
1990 assert(!wl_list_empty(&shseat->popup_grab.surfaces_list));
1991 /* Send the popup_done event to all the popups open */
1992 wl_list_for_each(shsurf, &shseat->popup_grab.surfaces_list, popup.grab_link) {
1993 wl_shell_surface_send_popup_done(&shsurf->resource);
1994 shsurf->popup.shseat = NULL;
1996 wl_list_init(&prev->popup.grab_link);
2000 wl_list_init(&prev->popup.grab_link);
2001 wl_list_init(&shseat->popup_grab.surfaces_list);
2006 add_popup_grab(struct shell_surface *shsurf, struct shell_seat *shseat)
2008 struct wl_seat *seat = &shseat->seat->seat;
2010 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2011 shseat->popup_grab.client = shsurf->surface->surface.resource.client;
2012 shseat->popup_grab.grab.interface = &popup_grab_interface;
2013 /* We must make sure here that this popup was opened after
2014 * a mouse press, and not just by moving around with other
2015 * popups already open. */
2016 if (shseat->seat->pointer.button_count > 0)
2017 shseat->popup_grab.initial_up = 0;
2019 wl_pointer_start_grab(seat->pointer, &shseat->popup_grab.grab);
2021 wl_list_insert(&shseat->popup_grab.surfaces_list, &shsurf->popup.grab_link);
2025 remove_popup_grab(struct shell_surface *shsurf)
2027 struct shell_seat *shseat = shsurf->popup.shseat;
2029 wl_list_remove(&shsurf->popup.grab_link);
2030 wl_list_init(&shsurf->popup.grab_link);
2031 if (wl_list_empty(&shseat->popup_grab.surfaces_list)) {
2032 wl_pointer_end_grab(shseat->popup_grab.grab.pointer);
2033 shseat->popup_grab.grab.interface = NULL;
2038 shell_map_popup(struct shell_surface *shsurf)
2040 struct shell_seat *shseat = shsurf->popup.shseat;
2041 struct weston_surface *es = shsurf->surface;
2042 struct weston_surface *parent = shsurf->parent;
2044 es->output = parent->output;
2046 weston_surface_set_transform_parent(es, parent);
2047 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
2048 weston_surface_update_transform(es);
2050 if (shseat->seat->pointer.grab_serial == shsurf->popup.serial) {
2051 add_popup_grab(shsurf, shseat);
2053 wl_shell_surface_send_popup_done(&shsurf->resource);
2054 shseat->popup_grab.client = NULL;
2059 shell_surface_set_popup(struct wl_client *client,
2060 struct wl_resource *resource,
2061 struct wl_resource *seat_resource,
2063 struct wl_resource *parent_resource,
2064 int32_t x, int32_t y, uint32_t flags)
2066 struct shell_surface *shsurf = resource->data;
2068 shsurf->type = SHELL_SURFACE_POPUP;
2069 shsurf->parent = parent_resource->data;
2070 shsurf->popup.shseat = get_shell_seat(seat_resource->data);
2071 shsurf->popup.serial = serial;
2072 shsurf->popup.x = x;
2073 shsurf->popup.y = y;
2076 static const struct wl_shell_surface_interface shell_surface_implementation = {
2079 shell_surface_resize,
2080 shell_surface_set_toplevel,
2081 shell_surface_set_transient,
2082 shell_surface_set_fullscreen,
2083 shell_surface_set_popup,
2084 shell_surface_set_maximized,
2085 shell_surface_set_title,
2086 shell_surface_set_class
2090 destroy_shell_surface(struct shell_surface *shsurf)
2092 if (!wl_list_empty(&shsurf->popup.grab_link)) {
2093 remove_popup_grab(shsurf);
2096 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
2097 shell_surface_is_top_fullscreen(shsurf)) {
2098 weston_output_switch_mode(shsurf->fullscreen_output,
2099 shsurf->fullscreen_output->origin);
2102 if (shsurf->fullscreen.black_surface)
2103 weston_surface_destroy(shsurf->fullscreen.black_surface);
2105 /* As destroy_resource() use wl_list_for_each_safe(),
2106 * we can always remove the listener.
2108 wl_list_remove(&shsurf->surface_destroy_listener.link);
2109 shsurf->surface->configure = NULL;
2110 ping_timer_destroy(shsurf);
2111 free(shsurf->title);
2113 wl_list_remove(&shsurf->link);
2118 shell_destroy_shell_surface(struct wl_resource *resource)
2120 struct shell_surface *shsurf = resource->data;
2122 destroy_shell_surface(shsurf);
2126 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
2128 struct shell_surface *shsurf = container_of(listener,
2129 struct shell_surface,
2130 surface_destroy_listener);
2132 if (shsurf->resource.client) {
2133 wl_resource_destroy(&shsurf->resource);
2135 wl_signal_emit(&shsurf->resource.destroy_signal,
2137 destroy_shell_surface(shsurf);
2142 shell_surface_configure(struct weston_surface *, int32_t, int32_t, int32_t, int32_t);
2144 static struct shell_surface *
2145 get_shell_surface(struct weston_surface *surface)
2147 if (surface->configure == shell_surface_configure)
2148 return surface->configure_private;
2153 static struct shell_surface *
2154 create_shell_surface(void *shell, struct weston_surface *surface,
2155 const struct weston_shell_client *client)
2157 struct shell_surface *shsurf;
2159 if (surface->configure) {
2160 weston_log("surface->configure already set\n");
2164 shsurf = calloc(1, sizeof *shsurf);
2166 weston_log("no memory to allocate shell surface\n");
2170 surface->configure = shell_surface_configure;
2171 surface->configure_private = shsurf;
2173 shsurf->shell = (struct desktop_shell *) shell;
2174 shsurf->unresponsive = 0;
2175 shsurf->saved_position_valid = false;
2176 shsurf->saved_rotation_valid = false;
2177 shsurf->surface = surface;
2178 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2179 shsurf->fullscreen.framerate = 0;
2180 shsurf->fullscreen.black_surface = NULL;
2181 shsurf->ping_timer = NULL;
2182 wl_list_init(&shsurf->fullscreen.transform.link);
2184 wl_signal_init(&shsurf->resource.destroy_signal);
2185 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2186 wl_signal_add(&surface->surface.resource.destroy_signal,
2187 &shsurf->surface_destroy_listener);
2189 /* init link so its safe to always remove it in destroy_shell_surface */
2190 wl_list_init(&shsurf->link);
2191 wl_list_init(&shsurf->popup.grab_link);
2193 /* empty when not in use */
2194 wl_list_init(&shsurf->rotation.transform.link);
2195 weston_matrix_init(&shsurf->rotation.rotation);
2197 wl_list_init(&shsurf->workspace_transform.link);
2199 shsurf->type = SHELL_SURFACE_NONE;
2200 shsurf->next_type = SHELL_SURFACE_NONE;
2202 shsurf->client = client;
2208 shell_get_shell_surface(struct wl_client *client,
2209 struct wl_resource *resource,
2211 struct wl_resource *surface_resource)
2213 struct weston_surface *surface = surface_resource->data;
2214 struct desktop_shell *shell = resource->data;
2215 struct shell_surface *shsurf;
2217 if (get_shell_surface(surface)) {
2218 wl_resource_post_error(surface_resource,
2219 WL_DISPLAY_ERROR_INVALID_OBJECT,
2220 "desktop_shell::get_shell_surface already requested");
2224 shsurf = create_shell_surface(shell, surface, &shell_client);
2226 wl_resource_post_error(surface_resource,
2227 WL_DISPLAY_ERROR_INVALID_OBJECT,
2228 "surface->configure already set");
2232 shsurf->resource.destroy = shell_destroy_shell_surface;
2233 shsurf->resource.object.id = id;
2234 shsurf->resource.object.interface = &wl_shell_surface_interface;
2235 shsurf->resource.object.implementation =
2236 (void (**)(void)) &shell_surface_implementation;
2237 shsurf->resource.data = shsurf;
2239 wl_client_add_resource(client, &shsurf->resource);
2242 static const struct wl_shell_interface shell_implementation = {
2243 shell_get_shell_surface
2247 shell_fade(struct desktop_shell *shell, enum fade_type type);
2250 screensaver_timeout(void *data)
2252 struct desktop_shell *shell = data;
2254 shell_fade(shell, FADE_OUT);
2260 handle_screensaver_sigchld(struct weston_process *proc, int status)
2262 struct desktop_shell *shell =
2263 container_of(proc, struct desktop_shell, screensaver.process);
2268 weston_compositor_sleep(shell->compositor);
2272 launch_screensaver(struct desktop_shell *shell)
2274 if (shell->screensaver.binding)
2277 if (!shell->screensaver.path) {
2278 weston_compositor_sleep(shell->compositor);
2282 if (shell->screensaver.process.pid != 0) {
2283 weston_log("old screensaver still running\n");
2287 weston_client_launch(shell->compositor,
2288 &shell->screensaver.process,
2289 shell->screensaver.path,
2290 handle_screensaver_sigchld);
2294 terminate_screensaver(struct desktop_shell *shell)
2296 if (shell->screensaver.process.pid == 0)
2299 kill(shell->screensaver.process.pid, SIGTERM);
2303 configure_static_surface(struct weston_surface *es, struct weston_layer *layer, int32_t width, int32_t height)
2305 struct weston_surface *s, *next;
2310 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2311 if (s->output == es->output && s != es) {
2312 weston_surface_unmap(s);
2313 s->configure = NULL;
2317 weston_surface_configure(es, es->output->x, es->output->y, width, height);
2319 if (wl_list_empty(&es->layer_link)) {
2320 wl_list_insert(&layer->surface_list, &es->layer_link);
2321 weston_compositor_schedule_repaint(es->compositor);
2326 background_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2328 struct desktop_shell *shell = es->configure_private;
2330 configure_static_surface(es, &shell->background_layer, width, height);
2334 desktop_shell_set_background(struct wl_client *client,
2335 struct wl_resource *resource,
2336 struct wl_resource *output_resource,
2337 struct wl_resource *surface_resource)
2339 struct desktop_shell *shell = resource->data;
2340 struct weston_surface *surface = surface_resource->data;
2342 if (surface->configure) {
2343 wl_resource_post_error(surface_resource,
2344 WL_DISPLAY_ERROR_INVALID_OBJECT,
2345 "surface role already assigned");
2349 surface->configure = background_configure;
2350 surface->configure_private = shell;
2351 surface->output = output_resource->data;
2352 desktop_shell_send_configure(resource, 0,
2354 surface->output->width,
2355 surface->output->height);
2359 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2361 struct desktop_shell *shell = es->configure_private;
2363 configure_static_surface(es, &shell->panel_layer, width, height);
2367 desktop_shell_set_panel(struct wl_client *client,
2368 struct wl_resource *resource,
2369 struct wl_resource *output_resource,
2370 struct wl_resource *surface_resource)
2372 struct desktop_shell *shell = resource->data;
2373 struct weston_surface *surface = surface_resource->data;
2375 if (surface->configure) {
2376 wl_resource_post_error(surface_resource,
2377 WL_DISPLAY_ERROR_INVALID_OBJECT,
2378 "surface role already assigned");
2382 surface->configure = panel_configure;
2383 surface->configure_private = shell;
2384 surface->output = output_resource->data;
2385 desktop_shell_send_configure(resource, 0,
2387 surface->output->width,
2388 surface->output->height);
2392 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
2394 struct desktop_shell *shell = surface->configure_private;
2399 center_on_output(surface, get_default_output(shell->compositor));
2401 if (!weston_surface_is_mapped(surface)) {
2402 wl_list_insert(&shell->lock_layer.surface_list,
2403 &surface->layer_link);
2404 weston_surface_update_transform(surface);
2405 shell_fade(shell, FADE_IN);
2410 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2412 struct desktop_shell *shell =
2413 container_of(listener, struct desktop_shell, lock_surface_listener);
2415 weston_log("lock surface gone\n");
2416 shell->lock_surface = NULL;
2420 desktop_shell_set_lock_surface(struct wl_client *client,
2421 struct wl_resource *resource,
2422 struct wl_resource *surface_resource)
2424 struct desktop_shell *shell = resource->data;
2425 struct weston_surface *surface = surface_resource->data;
2427 shell->prepare_event_sent = false;
2432 shell->lock_surface = surface;
2434 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2435 wl_signal_add(&surface_resource->destroy_signal,
2436 &shell->lock_surface_listener);
2438 surface->configure = lock_surface_configure;
2439 surface->configure_private = shell;
2443 resume_desktop(struct desktop_shell *shell)
2445 struct workspace *ws = get_current_workspace(shell);
2447 terminate_screensaver(shell);
2449 wl_list_remove(&shell->lock_layer.link);
2450 wl_list_insert(&shell->compositor->cursor_layer.link,
2451 &shell->fullscreen_layer.link);
2452 wl_list_insert(&shell->fullscreen_layer.link,
2453 &shell->panel_layer.link);
2454 if (shell->showing_input_panels) {
2455 wl_list_insert(&shell->panel_layer.link,
2456 &shell->input_panel_layer.link);
2457 wl_list_insert(&shell->input_panel_layer.link,
2460 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2463 restore_focus_state(shell, get_current_workspace(shell));
2465 shell->locked = false;
2466 shell_fade(shell, FADE_IN);
2467 weston_compositor_damage_all(shell->compositor);
2471 desktop_shell_unlock(struct wl_client *client,
2472 struct wl_resource *resource)
2474 struct desktop_shell *shell = resource->data;
2476 shell->prepare_event_sent = false;
2479 resume_desktop(shell);
2483 desktop_shell_set_grab_surface(struct wl_client *client,
2484 struct wl_resource *resource,
2485 struct wl_resource *surface_resource)
2487 struct desktop_shell *shell = resource->data;
2489 shell->grab_surface = surface_resource->data;
2492 static const struct desktop_shell_interface desktop_shell_implementation = {
2493 desktop_shell_set_background,
2494 desktop_shell_set_panel,
2495 desktop_shell_set_lock_surface,
2496 desktop_shell_unlock,
2497 desktop_shell_set_grab_surface
2500 static enum shell_surface_type
2501 get_shell_surface_type(struct weston_surface *surface)
2503 struct shell_surface *shsurf;
2505 shsurf = get_shell_surface(surface);
2507 return SHELL_SURFACE_NONE;
2508 return shsurf->type;
2512 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2514 struct weston_surface *surface =
2515 (struct weston_surface *) seat->pointer->focus;
2516 struct shell_surface *shsurf;
2518 if (surface == NULL)
2521 shsurf = get_shell_surface(surface);
2522 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2523 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2526 surface_move(shsurf, (struct weston_seat *) seat);
2530 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2532 struct weston_surface *surface =
2533 (struct weston_surface *) seat->pointer->focus;
2536 struct shell_surface *shsurf;
2538 if (surface == NULL)
2541 shsurf = get_shell_surface(surface);
2542 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN ||
2543 shsurf->type == SHELL_SURFACE_MAXIMIZED)
2546 weston_surface_from_global(surface,
2547 wl_fixed_to_int(seat->pointer->grab_x),
2548 wl_fixed_to_int(seat->pointer->grab_y),
2551 if (x < surface->geometry.width / 3)
2552 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2553 else if (x < 2 * surface->geometry.width / 3)
2556 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2558 if (y < surface->geometry.height / 3)
2559 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2560 else if (y < 2 * surface->geometry.height / 3)
2563 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2565 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2569 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2570 wl_fixed_t value, void *data)
2573 struct shell_surface *shsurf;
2574 struct weston_surface *surface =
2575 (struct weston_surface *) seat->pointer->focus;
2577 if (surface == NULL)
2580 shsurf = get_shell_surface(surface);
2584 surface->alpha -= wl_fixed_to_double(value) * step;
2586 if (surface->alpha > 1.0)
2587 surface->alpha = 1.0;
2588 if (surface->alpha < step)
2589 surface->alpha = step;
2591 weston_surface_geometry_dirty(surface);
2592 weston_surface_damage(surface);
2596 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2599 struct weston_seat *ws = (struct weston_seat *) seat;
2600 struct weston_compositor *compositor = ws->compositor;
2601 struct weston_output *output;
2604 wl_list_for_each(output, &compositor->output_list, link) {
2605 if (pixman_region32_contains_point(&output->region,
2606 wl_fixed_to_double(seat->pointer->x),
2607 wl_fixed_to_double(seat->pointer->y),
2609 if (key == KEY_PAGEUP)
2610 increment = output->zoom.increment;
2611 else if (key == KEY_PAGEDOWN)
2612 increment = -output->zoom.increment;
2613 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2614 /* For every pixel zoom 20th of a step */
2615 increment = output->zoom.increment *
2616 -wl_fixed_to_double(value) / 20.0;
2620 output->zoom.level += increment;
2622 if (output->zoom.level < 0.0)
2623 output->zoom.level = 0.0;
2624 else if (output->zoom.level > output->zoom.max_level)
2625 output->zoom.level = output->zoom.max_level;
2626 else if (!output->zoom.active) {
2627 output->zoom.active = 1;
2628 output->disable_planes++;
2631 output->zoom.spring_z.target = output->zoom.level;
2633 weston_output_update_zoom(output, output->zoom.type);
2639 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2640 wl_fixed_t value, void *data)
2642 do_zoom(seat, time, 0, axis, value);
2646 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2649 do_zoom(seat, time, key, 0, 0);
2653 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2656 struct weston_compositor *compositor = data;
2658 wl_display_terminate(compositor->wl_display);
2662 rotate_grab_motion(struct wl_pointer_grab *grab,
2663 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2665 struct rotate_grab *rotate =
2666 container_of(grab, struct rotate_grab, base.grab);
2667 struct wl_pointer *pointer = grab->pointer;
2668 struct shell_surface *shsurf = rotate->base.shsurf;
2669 struct weston_surface *surface;
2670 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2675 surface = shsurf->surface;
2677 cx = 0.5f * surface->geometry.width;
2678 cy = 0.5f * surface->geometry.height;
2680 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2681 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2682 r = sqrtf(dx * dx + dy * dy);
2684 wl_list_remove(&shsurf->rotation.transform.link);
2685 weston_surface_geometry_dirty(shsurf->surface);
2688 struct weston_matrix *matrix =
2689 &shsurf->rotation.transform.matrix;
2691 weston_matrix_init(&rotate->rotation);
2692 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
2694 weston_matrix_init(matrix);
2695 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2696 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2697 weston_matrix_multiply(matrix, &rotate->rotation);
2698 weston_matrix_translate(matrix, cx, cy, 0.0f);
2701 &shsurf->surface->geometry.transformation_list,
2702 &shsurf->rotation.transform.link);
2704 wl_list_init(&shsurf->rotation.transform.link);
2705 weston_matrix_init(&shsurf->rotation.rotation);
2706 weston_matrix_init(&rotate->rotation);
2709 /* We need to adjust the position of the surface
2710 * in case it was resized in a rotated state before */
2711 cposx = surface->geometry.x + cx;
2712 cposy = surface->geometry.y + cy;
2713 dposx = rotate->center.x - cposx;
2714 dposy = rotate->center.y - cposy;
2715 if (dposx != 0.0f || dposy != 0.0f) {
2716 weston_surface_set_position(surface,
2717 surface->geometry.x + dposx,
2718 surface->geometry.y + dposy);
2721 /* Repaint implies weston_surface_update_transform(), which
2722 * lazily applies the damage due to rotation update.
2724 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2728 rotate_grab_button(struct wl_pointer_grab *grab,
2729 uint32_t time, uint32_t button, uint32_t state_w)
2731 struct rotate_grab *rotate =
2732 container_of(grab, struct rotate_grab, base.grab);
2733 struct wl_pointer *pointer = grab->pointer;
2734 struct shell_surface *shsurf = rotate->base.shsurf;
2735 enum wl_pointer_button_state state = state_w;
2737 if (pointer->button_count == 0 &&
2738 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2740 weston_matrix_multiply(&shsurf->rotation.rotation,
2742 shell_grab_end(&rotate->base);
2747 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2754 surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
2756 struct rotate_grab *rotate;
2760 rotate = malloc(sizeof *rotate);
2764 weston_surface_to_global_float(surface->surface,
2765 surface->surface->geometry.width / 2,
2766 surface->surface->geometry.height / 2,
2767 &rotate->center.x, &rotate->center.y);
2769 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2770 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2771 r = sqrtf(dx * dx + dy * dy);
2773 struct weston_matrix inverse;
2775 weston_matrix_init(&inverse);
2776 weston_matrix_rotate_xy(&inverse, dx / r, -dy / r);
2777 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2779 weston_matrix_init(&rotate->rotation);
2780 weston_matrix_rotate_xy(&rotate->rotation, dx / r, dy / r);
2782 weston_matrix_init(&surface->rotation.rotation);
2783 weston_matrix_init(&rotate->rotation);
2786 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2787 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2791 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2794 struct weston_surface *base_surface =
2795 (struct weston_surface *) seat->pointer->focus;
2796 struct shell_surface *surface;
2798 if (base_surface == NULL)
2801 surface = get_shell_surface(base_surface);
2802 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN ||
2803 surface->type == SHELL_SURFACE_MAXIMIZED)
2806 surface_rotate(surface, seat);
2810 lower_fullscreen_layer(struct desktop_shell *shell)
2812 struct workspace *ws;
2813 struct weston_surface *surface, *prev;
2815 ws = get_current_workspace(shell);
2816 wl_list_for_each_reverse_safe(surface, prev,
2817 &shell->fullscreen_layer.surface_list,
2819 weston_surface_restack(surface, &ws->layer.surface_list);
2823 activate(struct desktop_shell *shell, struct weston_surface *es,
2824 struct weston_seat *seat)
2826 struct focus_state *state;
2827 struct workspace *ws;
2829 weston_surface_activate(es, seat);
2831 state = ensure_focus_state(shell, seat);
2835 state->keyboard_focus = es;
2836 wl_list_remove(&state->surface_destroy_listener.link);
2837 wl_signal_add(&es->surface.resource.destroy_signal,
2838 &state->surface_destroy_listener);
2840 switch (get_shell_surface_type(es)) {
2841 case SHELL_SURFACE_FULLSCREEN:
2842 /* should on top of panels */
2843 shell_stack_fullscreen(get_shell_surface(es));
2844 shell_configure_fullscreen(get_shell_surface(es));
2847 ws = get_current_workspace(shell);
2848 weston_surface_restack(es, &ws->layer.surface_list);
2853 /* no-op func for checking black surface */
2855 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
2860 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2862 if (es->configure == black_surface_configure) {
2864 *fs_surface = (struct weston_surface *)es->configure_private;
2871 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2874 struct weston_seat *ws = (struct weston_seat *) seat;
2875 struct desktop_shell *shell = data;
2876 struct weston_surface *focus;
2877 struct weston_surface *upper;
2879 focus = (struct weston_surface *) seat->pointer->focus;
2883 if (is_black_surface(focus, &upper))
2886 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2889 if (seat->pointer->grab == &seat->pointer->default_grab)
2890 activate(shell, focus, ws);
2894 lock(struct desktop_shell *shell)
2896 struct workspace *ws = get_current_workspace(shell);
2898 if (shell->locked) {
2899 weston_compositor_sleep(shell->compositor);
2903 shell->locked = true;
2905 /* Hide all surfaces by removing the fullscreen, panel and
2906 * toplevel layers. This way nothing else can show or receive
2907 * input events while we are locked. */
2909 wl_list_remove(&shell->panel_layer.link);
2910 wl_list_remove(&shell->fullscreen_layer.link);
2911 if (shell->showing_input_panels)
2912 wl_list_remove(&shell->input_panel_layer.link);
2913 wl_list_remove(&ws->layer.link);
2914 wl_list_insert(&shell->compositor->cursor_layer.link,
2915 &shell->lock_layer.link);
2917 launch_screensaver(shell);
2919 /* TODO: disable bindings that should not work while locked. */
2921 /* All this must be undone in resume_desktop(). */
2925 unlock(struct desktop_shell *shell)
2927 if (!shell->locked || shell->lock_surface) {
2928 shell_fade(shell, FADE_IN);
2932 /* If desktop-shell client has gone away, unlock immediately. */
2933 if (!shell->child.desktop_shell) {
2934 resume_desktop(shell);
2938 if (shell->prepare_event_sent)
2941 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2942 shell->prepare_event_sent = true;
2946 shell_fade_done(struct weston_surface_animation *animation, void *data)
2948 struct desktop_shell *shell = data;
2950 shell->fade.animation = NULL;
2952 switch (shell->fade.type) {
2954 weston_surface_destroy(shell->fade.surface);
2955 shell->fade.surface = NULL;
2964 shell_fade(struct desktop_shell *shell, enum fade_type type)
2966 struct weston_compositor *compositor = shell->compositor;
2967 struct weston_surface *surface;
2978 weston_log("shell: invalid fade type\n");
2982 shell->fade.type = type;
2984 if (shell->fade.surface == NULL) {
2985 surface = weston_surface_create(compositor);
2989 weston_surface_configure(surface, 0, 0, 8192, 8192);
2990 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1.0);
2991 surface->alpha = 1.0 - tint;
2992 wl_list_insert(&compositor->fade_layer.surface_list,
2993 &surface->layer_link);
2994 weston_surface_update_transform(surface);
2995 shell->fade.surface = surface;
2996 pixman_region32_init(&surface->input);
2999 if (shell->fade.animation)
3000 weston_fade_update(shell->fade.animation,
3001 shell->fade.surface->alpha, tint, 30.0);
3003 shell->fade.animation =
3004 weston_fade_run(shell->fade.surface,
3005 1.0 - tint, tint, 30.0,
3006 shell_fade_done, shell);
3010 idle_handler(struct wl_listener *listener, void *data)
3012 struct desktop_shell *shell =
3013 container_of(listener, struct desktop_shell, idle_listener);
3015 shell_fade(shell, FADE_OUT);
3016 /* lock() is called from shell_fade_done() */
3020 wake_handler(struct wl_listener *listener, void *data)
3022 struct desktop_shell *shell =
3023 container_of(listener, struct desktop_shell, wake_listener);
3029 show_input_panels(struct wl_listener *listener, void *data)
3031 struct desktop_shell *shell =
3032 container_of(listener, struct desktop_shell,
3033 show_input_panel_listener);
3034 struct input_panel_surface *surface, *next;
3035 struct weston_surface *ws;
3037 if (shell->showing_input_panels)
3040 shell->showing_input_panels = true;
3043 wl_list_insert(&shell->panel_layer.link,
3044 &shell->input_panel_layer.link);
3046 wl_list_for_each_safe(surface, next,
3047 &shell->input_panel.surfaces, link) {
3048 ws = surface->surface;
3049 wl_list_insert(&shell->input_panel_layer.surface_list,
3051 weston_surface_geometry_dirty(ws);
3052 weston_surface_update_transform(ws);
3053 weston_surface_damage(ws);
3054 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
3059 hide_input_panels(struct wl_listener *listener, void *data)
3061 struct desktop_shell *shell =
3062 container_of(listener, struct desktop_shell,
3063 hide_input_panel_listener);
3064 struct weston_surface *surface, *next;
3066 if (!shell->showing_input_panels)
3069 shell->showing_input_panels = false;
3072 wl_list_remove(&shell->input_panel_layer.link);
3074 wl_list_for_each_safe(surface, next,
3075 &shell->input_panel_layer.surface_list, layer_link)
3076 weston_surface_unmap(surface);
3080 center_on_output(struct weston_surface *surface, struct weston_output *output)
3082 int32_t width = weston_surface_buffer_width(surface);
3083 int32_t height = weston_surface_buffer_height(surface);
3086 x = output->x + (output->width - width) / 2;
3087 y = output->y + (output->height - height) / 2;
3089 weston_surface_configure(surface, x, y, width, height);
3093 weston_surface_set_initial_position (struct weston_surface *surface,
3094 struct desktop_shell *shell)
3096 struct weston_compositor *compositor = shell->compositor;
3098 int range_x, range_y;
3099 int dx, dy, x, y, panel_height;
3100 struct weston_output *output, *target_output = NULL;
3101 struct weston_seat *seat;
3103 /* As a heuristic place the new window on the same output as the
3104 * pointer. Falling back to the output containing 0, 0.
3106 * TODO: Do something clever for touch too?
3108 wl_list_for_each(seat, &compositor->seat_list, link) {
3109 if (seat->has_pointer) {
3110 ix = wl_fixed_to_int(seat->pointer.x);
3111 iy = wl_fixed_to_int(seat->pointer.y);
3116 wl_list_for_each(output, &compositor->output_list, link) {
3117 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
3118 target_output = output;
3123 if (!target_output) {
3124 weston_surface_set_position(surface, 10 + random() % 400,
3125 10 + random() % 400);
3129 /* Valid range within output where the surface will still be onscreen.
3130 * If this is negative it means that the surface is bigger than
3133 panel_height = get_output_panel_height(shell, target_output);
3134 range_x = target_output->width - surface->geometry.width;
3135 range_y = (target_output->height - panel_height) -
3136 surface->geometry.height;
3139 dx = random() % range_x;
3144 dy = panel_height + random() % range_y;
3148 x = target_output->x + dx;
3149 y = target_output->y + dy;
3151 weston_surface_set_position (surface, x, y);
3155 map(struct desktop_shell *shell, struct weston_surface *surface,
3156 int32_t width, int32_t height, int32_t sx, int32_t sy)
3158 struct weston_compositor *compositor = shell->compositor;
3159 struct shell_surface *shsurf = get_shell_surface(surface);
3160 enum shell_surface_type surface_type = shsurf->type;
3161 struct weston_surface *parent;
3162 struct weston_seat *seat;
3163 struct workspace *ws;
3164 int panel_height = 0;
3166 surface->geometry.width = width;
3167 surface->geometry.height = height;
3168 weston_surface_geometry_dirty(surface);
3170 /* initial positioning, see also configure() */
3171 switch (surface_type) {
3172 case SHELL_SURFACE_TOPLEVEL:
3173 weston_surface_set_initial_position(surface, shell);
3175 case SHELL_SURFACE_FULLSCREEN:
3176 center_on_output(surface, shsurf->fullscreen_output);
3177 shell_map_fullscreen(shsurf);
3179 case SHELL_SURFACE_MAXIMIZED:
3180 /* use surface configure to set the geometry */
3181 panel_height = get_output_panel_height(shell,surface->output);
3182 weston_surface_set_position(surface, shsurf->output->x,
3183 shsurf->output->y + panel_height);
3185 case SHELL_SURFACE_POPUP:
3186 shell_map_popup(shsurf);
3188 case SHELL_SURFACE_NONE:
3189 weston_surface_set_position(surface,
3190 surface->geometry.x + sx,
3191 surface->geometry.y + sy);
3197 /* surface stacking order, see also activate() */
3198 switch (surface_type) {
3199 case SHELL_SURFACE_POPUP:
3200 case SHELL_SURFACE_TRANSIENT:
3201 parent = shsurf->parent;
3202 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
3204 case SHELL_SURFACE_FULLSCREEN:
3205 case SHELL_SURFACE_NONE:
3208 ws = get_current_workspace(shell);
3209 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
3213 if (surface_type != SHELL_SURFACE_NONE) {
3214 weston_surface_update_transform(surface);
3215 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3216 surface->output = shsurf->output;
3219 switch (surface_type) {
3220 case SHELL_SURFACE_TRANSIENT:
3221 if (shsurf->transient.flags ==
3222 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3224 case SHELL_SURFACE_TOPLEVEL:
3225 case SHELL_SURFACE_FULLSCREEN:
3226 case SHELL_SURFACE_MAXIMIZED:
3227 if (!shell->locked) {
3228 wl_list_for_each(seat, &compositor->seat_list, link)
3229 activate(shell, surface, seat);
3236 if (surface_type == SHELL_SURFACE_TOPLEVEL)
3238 switch (shell->win_animation_type) {
3239 case ANIMATION_FADE:
3240 weston_fade_run(surface, 0.0, 1.0, 200.0, NULL, NULL);
3242 case ANIMATION_ZOOM:
3243 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3252 configure(struct desktop_shell *shell, struct weston_surface *surface,
3253 float x, float y, int32_t width, int32_t height)
3255 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3256 struct shell_surface *shsurf;
3258 shsurf = get_shell_surface(surface);
3260 surface_type = shsurf->type;
3262 weston_surface_configure(surface, x, y, width, height);
3264 switch (surface_type) {
3265 case SHELL_SURFACE_FULLSCREEN:
3266 shell_stack_fullscreen(shsurf);
3267 shell_configure_fullscreen(shsurf);
3269 case SHELL_SURFACE_MAXIMIZED:
3270 /* setting x, y and using configure to change that geometry */
3271 surface->geometry.x = surface->output->x;
3272 surface->geometry.y = surface->output->y +
3273 get_output_panel_height(shell,surface->output);
3275 case SHELL_SURFACE_TOPLEVEL:
3281 /* XXX: would a fullscreen surface need the same handling? */
3282 if (surface->output) {
3283 weston_surface_update_transform(surface);
3285 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3286 surface->output = shsurf->output;
3291 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy, int32_t width, int32_t height)
3293 struct shell_surface *shsurf = get_shell_surface(es);
3294 struct desktop_shell *shell = shsurf->shell;
3296 int type_changed = 0;
3298 if (!weston_surface_is_mapped(es) && !wl_list_empty(&shsurf->popup.grab_link)) {
3299 remove_popup_grab(shsurf);
3305 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3306 shsurf->type != shsurf->next_type) {
3307 set_surface_type(shsurf);
3311 if (!weston_surface_is_mapped(es)) {
3312 map(shell, es, width, height, sx, sy);
3313 } else if (type_changed || sx != 0 || sy != 0 ||
3314 es->geometry.width != width ||
3315 es->geometry.height != height) {
3316 float from_x, from_y;
3319 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3320 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3321 configure(shell, es,
3322 es->geometry.x + to_x - from_x,
3323 es->geometry.y + to_y - from_y,
3328 static void launch_desktop_shell_process(void *data);
3331 desktop_shell_sigchld(struct weston_process *process, int status)
3334 struct desktop_shell *shell =
3335 container_of(process, struct desktop_shell, child.process);
3337 shell->child.process.pid = 0;
3338 shell->child.client = NULL; /* already destroyed by wayland */
3340 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3341 time = weston_compositor_get_time();
3342 if (time - shell->child.deathstamp > 30000) {
3343 shell->child.deathstamp = time;
3344 shell->child.deathcount = 0;
3347 shell->child.deathcount++;
3348 if (shell->child.deathcount > 5) {
3349 weston_log("weston-desktop-shell died, giving up.\n");
3353 weston_log("weston-desktop-shell died, respawning...\n");
3354 launch_desktop_shell_process(shell);
3358 launch_desktop_shell_process(void *data)
3360 struct desktop_shell *shell = data;
3361 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3363 shell->child.client = weston_client_launch(shell->compositor,
3364 &shell->child.process,
3366 desktop_shell_sigchld);
3368 if (!shell->child.client)
3369 weston_log("not able to start %s\n", shell_exe);
3373 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3375 struct desktop_shell *shell = data;
3377 wl_client_add_object(client, &wl_shell_interface,
3378 &shell_implementation, id, shell);
3382 unbind_desktop_shell(struct wl_resource *resource)
3384 struct desktop_shell *shell = resource->data;
3387 resume_desktop(shell);
3389 shell->child.desktop_shell = NULL;
3390 shell->prepare_event_sent = false;
3395 bind_desktop_shell(struct wl_client *client,
3396 void *data, uint32_t version, uint32_t id)
3398 struct desktop_shell *shell = data;
3399 struct wl_resource *resource;
3401 resource = wl_client_add_object(client, &desktop_shell_interface,
3402 &desktop_shell_implementation,
3405 if (client == shell->child.client) {
3406 resource->destroy = unbind_desktop_shell;
3407 shell->child.desktop_shell = resource;
3411 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3412 "permission to bind desktop_shell denied");
3413 wl_resource_destroy(resource);
3417 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3419 struct desktop_shell *shell = surface->configure_private;
3424 /* XXX: starting weston-screensaver beforehand does not work */
3428 center_on_output(surface, surface->output);
3430 if (wl_list_empty(&surface->layer_link)) {
3431 wl_list_insert(shell->lock_layer.surface_list.prev,
3432 &surface->layer_link);
3433 weston_surface_update_transform(surface);
3434 wl_event_source_timer_update(shell->screensaver.timer,
3435 shell->screensaver.duration);
3436 shell_fade(shell, FADE_IN);
3441 screensaver_set_surface(struct wl_client *client,
3442 struct wl_resource *resource,
3443 struct wl_resource *surface_resource,
3444 struct wl_resource *output_resource)
3446 struct desktop_shell *shell = resource->data;
3447 struct weston_surface *surface = surface_resource->data;
3448 struct weston_output *output = output_resource->data;
3450 surface->configure = screensaver_configure;
3451 surface->configure_private = shell;
3452 surface->output = output;
3455 static const struct screensaver_interface screensaver_implementation = {
3456 screensaver_set_surface
3460 unbind_screensaver(struct wl_resource *resource)
3462 struct desktop_shell *shell = resource->data;
3464 shell->screensaver.binding = NULL;
3469 bind_screensaver(struct wl_client *client,
3470 void *data, uint32_t version, uint32_t id)
3472 struct desktop_shell *shell = data;
3473 struct wl_resource *resource;
3475 resource = wl_client_add_object(client, &screensaver_interface,
3476 &screensaver_implementation,
3479 if (shell->screensaver.binding == NULL) {
3480 resource->destroy = unbind_screensaver;
3481 shell->screensaver.binding = resource;
3485 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3486 "interface object already bound");
3487 wl_resource_destroy(resource);
3491 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy, int32_t width, int32_t height)
3493 struct weston_mode *mode;
3499 if (!weston_surface_is_mapped(surface))
3502 mode = surface->output->current;
3503 x = (mode->width - width) / 2;
3504 y = mode->height - height;
3506 /* Don't map the input panel here, wait for
3507 * show_input_panels signal. */
3509 weston_surface_configure(surface,
3510 surface->output->x + x,
3511 surface->output->y + y,
3516 destroy_input_panel_surface(struct input_panel_surface *input_panel_surface)
3518 wl_list_remove(&input_panel_surface->surface_destroy_listener.link);
3519 wl_list_remove(&input_panel_surface->link);
3521 input_panel_surface->surface->configure = NULL;
3523 free(input_panel_surface);
3526 static struct input_panel_surface *
3527 get_input_panel_surface(struct weston_surface *surface)
3529 if (surface->configure == input_panel_configure) {
3530 return surface->configure_private;
3537 input_panel_handle_surface_destroy(struct wl_listener *listener, void *data)
3539 struct input_panel_surface *ipsurface = container_of(listener,
3540 struct input_panel_surface,
3541 surface_destroy_listener);
3543 if (ipsurface->resource.client) {
3544 wl_resource_destroy(&ipsurface->resource);
3546 wl_signal_emit(&ipsurface->resource.destroy_signal,
3547 &ipsurface->resource);
3548 destroy_input_panel_surface(ipsurface);
3551 static struct input_panel_surface *
3552 create_input_panel_surface(struct desktop_shell *shell,
3553 struct weston_surface *surface)
3555 struct input_panel_surface *input_panel_surface;
3557 input_panel_surface = calloc(1, sizeof *input_panel_surface);
3558 if (!input_panel_surface)
3561 surface->configure = input_panel_configure;
3562 surface->configure_private = input_panel_surface;
3564 input_panel_surface->shell = shell;
3566 input_panel_surface->surface = surface;
3568 wl_signal_init(&input_panel_surface->resource.destroy_signal);
3569 input_panel_surface->surface_destroy_listener.notify = input_panel_handle_surface_destroy;
3570 wl_signal_add(&surface->surface.resource.destroy_signal,
3571 &input_panel_surface->surface_destroy_listener);
3573 wl_list_init(&input_panel_surface->link);
3575 return input_panel_surface;
3579 input_panel_surface_set_toplevel(struct wl_client *client,
3580 struct wl_resource *resource,
3583 struct input_panel_surface *input_panel_surface = resource->data;
3584 struct desktop_shell *shell = input_panel_surface->shell;
3586 wl_list_insert(&shell->input_panel.surfaces,
3587 &input_panel_surface->link);
3590 static const struct input_panel_surface_interface input_panel_surface_implementation = {
3591 input_panel_surface_set_toplevel
3595 destroy_input_panel_surface_resource(struct wl_resource *resource)
3597 struct input_panel_surface *ipsurf = resource->data;
3599 destroy_input_panel_surface(ipsurf);
3603 input_panel_get_input_panel_surface(struct wl_client *client,
3604 struct wl_resource *resource,
3606 struct wl_resource *surface_resource)
3608 struct weston_surface *surface = surface_resource->data;
3609 struct desktop_shell *shell = resource->data;
3610 struct input_panel_surface *ipsurf;
3612 if (get_input_panel_surface(surface)) {
3613 wl_resource_post_error(surface_resource,
3614 WL_DISPLAY_ERROR_INVALID_OBJECT,
3615 "input_panel::get_input_panel_surface already requested");
3619 ipsurf = create_input_panel_surface(shell, surface);
3621 wl_resource_post_error(surface_resource,
3622 WL_DISPLAY_ERROR_INVALID_OBJECT,
3623 "surface->configure already set");
3627 ipsurf->resource.destroy = destroy_input_panel_surface_resource;
3628 ipsurf->resource.object.id = id;
3629 ipsurf->resource.object.interface = &input_panel_surface_interface;
3630 ipsurf->resource.object.implementation =
3631 (void (**)(void)) &input_panel_surface_implementation;
3632 ipsurf->resource.data = ipsurf;
3634 wl_client_add_resource(client, &ipsurf->resource);
3637 static const struct input_panel_interface input_panel_implementation = {
3638 input_panel_get_input_panel_surface
3642 unbind_input_panel(struct wl_resource *resource)
3644 struct desktop_shell *shell = resource->data;
3646 shell->input_panel.binding = NULL;
3651 bind_input_panel(struct wl_client *client,
3652 void *data, uint32_t version, uint32_t id)
3654 struct desktop_shell *shell = data;
3655 struct wl_resource *resource;
3657 resource = wl_client_add_object(client, &input_panel_interface,
3658 &input_panel_implementation,
3661 if (shell->input_panel.binding == NULL) {
3662 resource->destroy = unbind_input_panel;
3663 shell->input_panel.binding = resource;
3667 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3668 "interface object already bound");
3669 wl_resource_destroy(resource);
3673 struct desktop_shell *shell;
3674 struct weston_surface *current;
3675 struct wl_listener listener;
3676 struct wl_keyboard_grab grab;
3680 switcher_next(struct switcher *switcher)
3682 struct weston_surface *surface;
3683 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3684 struct shell_surface *shsurf;
3685 struct workspace *ws = get_current_workspace(switcher->shell);
3687 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3688 switch (get_shell_surface_type(surface)) {
3689 case SHELL_SURFACE_TOPLEVEL:
3690 case SHELL_SURFACE_FULLSCREEN:
3691 case SHELL_SURFACE_MAXIMIZED:
3694 if (prev == switcher->current)
3697 surface->alpha = 0.25;
3698 weston_surface_geometry_dirty(surface);
3699 weston_surface_damage(surface);
3705 if (is_black_surface(surface, NULL)) {
3706 surface->alpha = 0.25;
3707 weston_surface_geometry_dirty(surface);
3708 weston_surface_damage(surface);
3718 wl_list_remove(&switcher->listener.link);
3719 wl_signal_add(&next->surface.resource.destroy_signal,
3720 &switcher->listener);
3722 switcher->current = next;
3725 shsurf = get_shell_surface(switcher->current);
3726 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3727 shsurf->fullscreen.black_surface->alpha = 1.0;
3731 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3733 struct switcher *switcher =
3734 container_of(listener, struct switcher, listener);
3736 switcher_next(switcher);
3740 switcher_destroy(struct switcher *switcher)
3742 struct weston_surface *surface;
3743 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3744 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *)keyboard;
3745 struct workspace *ws = get_current_workspace(switcher->shell);
3747 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3748 surface->alpha = 1.0;
3749 weston_surface_damage(surface);
3752 if (switcher->current)
3753 activate(switcher->shell, switcher->current,
3754 (struct weston_seat *) keyboard->seat);
3755 wl_list_remove(&switcher->listener.link);
3756 wl_keyboard_end_grab(keyboard);
3757 if (weston_keyboard->input_method_resource)
3758 keyboard->grab = &weston_keyboard->input_method_grab;
3763 switcher_key(struct wl_keyboard_grab *grab,
3764 uint32_t time, uint32_t key, uint32_t state_w)
3766 struct switcher *switcher = container_of(grab, struct switcher, grab);
3767 enum wl_keyboard_key_state state = state_w;
3769 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3770 switcher_next(switcher);
3774 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3775 uint32_t mods_depressed, uint32_t mods_latched,
3776 uint32_t mods_locked, uint32_t group)
3778 struct switcher *switcher = container_of(grab, struct switcher, grab);
3779 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3781 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3782 switcher_destroy(switcher);
3785 static const struct wl_keyboard_grab_interface switcher_grab = {
3791 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3794 struct desktop_shell *shell = data;
3795 struct switcher *switcher;
3797 switcher = malloc(sizeof *switcher);
3798 switcher->shell = shell;
3799 switcher->current = NULL;
3800 switcher->listener.notify = switcher_handle_surface_destroy;
3801 wl_list_init(&switcher->listener.link);
3803 lower_fullscreen_layer(switcher->shell);
3804 switcher->grab.interface = &switcher_grab;
3805 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3806 wl_keyboard_set_focus(seat->keyboard, NULL);
3807 switcher_next(switcher);
3811 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3814 struct weston_compositor *compositor = data;
3815 struct weston_output *output;
3816 long backlight_new = 0;
3818 /* TODO: we're limiting to simple use cases, where we assume just
3819 * control on the primary display. We'd have to extend later if we
3820 * ever get support for setting backlights on random desktop LCD
3822 output = get_default_output(compositor);
3826 if (!output->set_backlight)
3829 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3830 backlight_new = output->backlight_current - 25;
3831 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3832 backlight_new = output->backlight_current + 25;
3834 if (backlight_new < 5)
3836 if (backlight_new > 255)
3837 backlight_new = 255;
3839 output->backlight_current = backlight_new;
3840 output->set_backlight(output, output->backlight_current);
3844 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3847 struct desktop_shell *shell = data;
3848 struct weston_compositor *compositor = shell->compositor;
3849 compositor->fan_debug = !compositor->fan_debug;
3850 weston_compositor_damage_all(compositor);
3853 struct debug_binding_grab {
3854 struct wl_keyboard_grab grab;
3855 struct weston_seat *seat;
3857 int key_released[2];
3861 debug_binding_key(struct wl_keyboard_grab *grab, uint32_t time,
3862 uint32_t key, uint32_t state)
3864 struct debug_binding_grab *db = (struct debug_binding_grab *) grab;
3865 struct wl_resource *resource;
3866 struct wl_display *display;
3868 int send = 0, terminate = 0;
3869 int check_binding = 1;
3872 if (state == WL_KEYBOARD_KEY_STATE_RELEASED) {
3873 /* Do not run bindings on key releases */
3876 for (i = 0; i < 2; i++)
3877 if (key == db->key[i])
3878 db->key_released[i] = 1;
3880 if (db->key_released[0] && db->key_released[1]) {
3881 /* All key releases been swalled so end the grab */
3883 } else if (key != db->key[0] && key != db->key[1]) {
3884 /* Should not swallow release of other keys */
3887 } else if (key == db->key[0] && !db->key_released[0]) {
3888 /* Do not check bindings for the first press of the binding
3889 * key. This allows it to be used as a debug shortcut.
3890 * We still need to swallow this event. */
3892 } else if (db->key[1]) {
3893 /* If we already ran a binding don't process another one since
3894 * we can't keep track of all the binding keys that were
3895 * pressed in order to swallow the release events. */
3900 if (check_binding) {
3901 struct weston_compositor *ec = db->seat->compositor;
3903 if (weston_compositor_run_debug_binding(ec, db->seat, time,
3905 /* We ran a binding so swallow the press and keep the
3906 * grab to swallow the released too. */
3911 /* Terminate the grab since the key pressed is not a
3912 * debug binding key. */
3919 resource = grab->keyboard->focus_resource;
3922 display = wl_client_get_display(resource->client);
3923 serial = wl_display_next_serial(display);
3924 wl_keyboard_send_key(resource, serial, time, key, state);
3929 struct weston_keyboard *weston_keyboard = (struct weston_keyboard *) grab->keyboard;
3930 wl_keyboard_end_grab(grab->keyboard);
3931 if (weston_keyboard->input_method_resource)
3932 grab->keyboard->grab = &weston_keyboard->input_method_grab;
3938 debug_binding_modifiers(struct wl_keyboard_grab *grab, uint32_t serial,
3939 uint32_t mods_depressed, uint32_t mods_latched,
3940 uint32_t mods_locked, uint32_t group)
3942 struct wl_resource *resource;
3944 resource = grab->keyboard->focus_resource;
3948 wl_keyboard_send_modifiers(resource, serial, mods_depressed,
3949 mods_latched, mods_locked, group);
3952 struct wl_keyboard_grab_interface debug_binding_keyboard_grab = {
3954 debug_binding_modifiers
3958 debug_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
3960 struct debug_binding_grab *grab;
3962 grab = calloc(1, sizeof *grab);
3966 grab->seat = (struct weston_seat *) seat;
3968 grab->grab.interface = &debug_binding_keyboard_grab;
3969 wl_keyboard_start_grab(seat->keyboard, &grab->grab);
3973 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3976 struct wl_surface *focus_surface;
3977 struct wl_client *client;
3978 struct desktop_shell *shell = data;
3979 struct weston_compositor *compositor = shell->compositor;
3982 focus_surface = seat->keyboard->focus;
3986 wl_signal_emit(&compositor->kill_signal, focus_surface);
3988 client = focus_surface->resource.client;
3989 wl_client_get_credentials(client, &pid, NULL, NULL);
3991 /* Skip clients that we launched ourselves (the credentials of
3992 * the socketpair is ours) */
3993 if (pid == getpid())
4000 workspace_up_binding(struct wl_seat *seat, uint32_t time,
4001 uint32_t key, void *data)
4003 struct desktop_shell *shell = data;
4004 unsigned int new_index = shell->workspaces.current;
4011 change_workspace(shell, new_index);
4015 workspace_down_binding(struct wl_seat *seat, uint32_t time,
4016 uint32_t key, void *data)
4018 struct desktop_shell *shell = data;
4019 unsigned int new_index = shell->workspaces.current;
4023 if (new_index < shell->workspaces.num - 1)
4026 change_workspace(shell, new_index);
4030 workspace_f_binding(struct wl_seat *seat, uint32_t time,
4031 uint32_t key, void *data)
4033 struct desktop_shell *shell = data;
4034 unsigned int new_index;
4038 new_index = key - KEY_F1;
4039 if (new_index >= shell->workspaces.num)
4040 new_index = shell->workspaces.num - 1;
4042 change_workspace(shell, new_index);
4046 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
4047 uint32_t key, void *data)
4049 struct desktop_shell *shell = data;
4050 unsigned int new_index = shell->workspaces.current;
4058 take_surface_to_workspace_by_seat(shell, seat, new_index);
4062 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
4063 uint32_t key, void *data)
4065 struct desktop_shell *shell = data;
4066 unsigned int new_index = shell->workspaces.current;
4071 if (new_index < shell->workspaces.num - 1)
4074 take_surface_to_workspace_by_seat(shell, seat, new_index);
4078 shell_destroy(struct wl_listener *listener, void *data)
4080 struct desktop_shell *shell =
4081 container_of(listener, struct desktop_shell, destroy_listener);
4082 struct workspace **ws;
4084 if (shell->child.client)
4085 wl_client_destroy(shell->child.client);
4087 wl_list_remove(&shell->idle_listener.link);
4088 wl_list_remove(&shell->wake_listener.link);
4089 wl_list_remove(&shell->show_input_panel_listener.link);
4090 wl_list_remove(&shell->hide_input_panel_listener.link);
4092 wl_array_for_each(ws, &shell->workspaces.array)
4093 workspace_destroy(*ws);
4094 wl_array_release(&shell->workspaces.array);
4096 free(shell->screensaver.path);
4101 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
4104 int i, num_workspace_bindings;
4106 /* fixed bindings */
4107 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
4108 MODIFIER_CTRL | MODIFIER_ALT,
4109 terminate_binding, ec);
4110 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
4111 click_to_activate_binding,
4113 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4114 MODIFIER_SUPER | MODIFIER_ALT,
4115 surface_opacity_binding, NULL);
4116 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
4117 MODIFIER_SUPER, zoom_axis_binding,
4120 /* configurable bindings */
4121 mod = shell->binding_modifier;
4122 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
4123 zoom_key_binding, NULL);
4124 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
4125 zoom_key_binding, NULL);
4126 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
4128 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
4129 resize_binding, shell);
4130 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
4131 rotate_binding, NULL);
4132 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
4134 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
4136 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
4137 backlight_binding, ec);
4138 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
4140 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
4141 backlight_binding, ec);
4142 weston_compositor_add_key_binding(ec, KEY_K, mod,
4143 force_kill_binding, shell);
4144 weston_compositor_add_key_binding(ec, KEY_UP, mod,
4145 workspace_up_binding, shell);
4146 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
4147 workspace_down_binding, shell);
4148 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
4149 workspace_move_surface_up_binding,
4151 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
4152 workspace_move_surface_down_binding,
4155 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
4156 if (shell->workspaces.num > 1) {
4157 num_workspace_bindings = shell->workspaces.num;
4158 if (num_workspace_bindings > 6)
4159 num_workspace_bindings = 6;
4160 for (i = 0; i < num_workspace_bindings; i++)
4161 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
4162 workspace_f_binding,
4166 /* Debug bindings */
4167 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
4168 debug_binding, shell);
4169 weston_compositor_add_debug_binding(ec, KEY_F,
4170 fan_debug_repaint_binding, shell);
4174 module_init(struct weston_compositor *ec,
4175 int *argc, char *argv[], const char *config_file)
4177 struct weston_seat *seat;
4178 struct desktop_shell *shell;
4179 struct workspace **pws;
4181 struct wl_event_loop *loop;
4183 shell = malloc(sizeof *shell);
4187 memset(shell, 0, sizeof *shell);
4188 shell->compositor = ec;
4190 shell->destroy_listener.notify = shell_destroy;
4191 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
4192 shell->idle_listener.notify = idle_handler;
4193 wl_signal_add(&ec->idle_signal, &shell->idle_listener);
4194 shell->wake_listener.notify = wake_handler;
4195 wl_signal_add(&ec->wake_signal, &shell->wake_listener);
4196 shell->show_input_panel_listener.notify = show_input_panels;
4197 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
4198 shell->hide_input_panel_listener.notify = hide_input_panels;
4199 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
4200 ec->ping_handler = ping_handler;
4201 ec->shell_interface.shell = shell;
4202 ec->shell_interface.create_shell_surface = create_shell_surface;
4203 ec->shell_interface.set_toplevel = set_toplevel;
4204 ec->shell_interface.set_transient = set_transient;
4205 ec->shell_interface.set_fullscreen = set_fullscreen;
4206 ec->shell_interface.move = surface_move;
4207 ec->shell_interface.resize = surface_resize;
4209 wl_list_init(&shell->input_panel.surfaces);
4211 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
4212 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
4213 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
4214 weston_layer_init(&shell->lock_layer, NULL);
4215 weston_layer_init(&shell->input_panel_layer, NULL);
4217 wl_array_init(&shell->workspaces.array);
4218 wl_list_init(&shell->workspaces.client_list);
4220 shell_configuration(shell, config_file);
4222 for (i = 0; i < shell->workspaces.num; i++) {
4223 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
4227 *pws = workspace_create();
4231 activate_workspace(shell, 0);
4233 wl_list_init(&shell->workspaces.anim_sticky_list);
4234 wl_list_init(&shell->workspaces.animation.link);
4235 shell->workspaces.animation.frame = animate_workspace_change_frame;
4237 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
4238 shell, bind_shell) == NULL)
4241 if (wl_display_add_global(ec->wl_display,
4242 &desktop_shell_interface,
4243 shell, bind_desktop_shell) == NULL)
4246 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
4247 shell, bind_screensaver) == NULL)
4250 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
4251 shell, bind_input_panel) == NULL)
4254 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
4255 shell, bind_workspace_manager) == NULL)
4258 shell->child.deathstamp = weston_compositor_get_time();
4260 loop = wl_display_get_event_loop(ec->wl_display);
4261 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
4263 shell->screensaver.timer =
4264 wl_event_loop_add_timer(loop, screensaver_timeout, shell);
4266 wl_list_for_each(seat, &ec->seat_list, link)
4267 create_pointer_focus_listener(seat);
4269 shell_add_bindings(ec, shell);
4271 shell_fade(shell, FADE_IN);