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 "../shared/config-parser.h"
40 #define DEFAULT_NUM_WORKSPACES 1
41 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
51 struct weston_seat *seat;
53 struct weston_surface *keyboard_focus;
55 struct wl_listener seat_destroy_listener;
56 struct wl_listener surface_destroy_listener;
60 struct weston_layer layer;
62 struct wl_list focus_list;
63 struct wl_listener seat_destroyed_listener;
66 struct desktop_shell {
67 struct weston_compositor *compositor;
69 struct wl_listener lock_listener;
70 struct wl_listener unlock_listener;
71 struct wl_listener destroy_listener;
72 struct wl_listener show_input_panel_listener;
73 struct wl_listener hide_input_panel_listener;
75 struct weston_layer fullscreen_layer;
76 struct weston_layer panel_layer;
77 struct weston_layer background_layer;
78 struct weston_layer lock_layer;
80 struct wl_listener pointer_focus_listener;
81 struct weston_surface *grab_surface;
84 struct weston_process process;
85 struct wl_client *client;
86 struct wl_resource *desktop_shell;
93 bool prepare_event_sent;
95 struct weston_surface *lock_surface;
96 struct wl_listener lock_surface_listener;
99 struct wl_array array;
100 unsigned int current;
103 struct weston_animation animation;
105 uint32_t anim_timestamp;
107 struct workspace *anim_from;
108 struct workspace *anim_to;
114 struct wl_resource *binding;
115 struct wl_list surfaces;
116 struct weston_process process;
120 struct wl_resource *binding;
121 struct wl_list surfaces;
124 uint32_t binding_modifier;
125 enum animation_type win_animation_type;
126 struct weston_surface *debug_repaint_surface;
129 enum shell_surface_type {
131 SHELL_SURFACE_TOPLEVEL,
132 SHELL_SURFACE_TRANSIENT,
133 SHELL_SURFACE_FULLSCREEN,
134 SHELL_SURFACE_MAXIMIZED,
139 struct wl_event_source *source;
143 struct shell_surface {
144 struct wl_resource resource;
146 struct weston_surface *surface;
147 struct wl_listener surface_destroy_listener;
148 struct weston_surface *parent;
149 struct desktop_shell *shell;
151 enum shell_surface_type type, next_type;
153 int32_t saved_x, saved_y;
154 bool saved_position_valid;
155 bool saved_rotation_valid;
159 struct weston_transform transform;
160 struct weston_matrix rotation;
164 struct wl_pointer_grab grab;
166 struct weston_transform parent_transform;
168 struct wl_seat *seat;
178 enum wl_shell_surface_fullscreen_method type;
179 struct weston_transform transform; /* matrix from x, y */
181 struct weston_surface *black_surface;
184 struct ping_timer *ping_timer;
186 struct weston_transform workspace_transform;
188 struct weston_output *fullscreen_output;
189 struct weston_output *output;
192 const struct weston_shell_client *client;
196 struct wl_pointer_grab grab;
197 struct shell_surface *shsurf;
198 struct wl_listener shsurf_destroy_listener;
199 struct wl_pointer *pointer;
202 struct weston_move_grab {
203 struct shell_grab base;
208 struct shell_grab base;
209 struct weston_matrix rotation;
217 activate(struct desktop_shell *shell, struct weston_surface *es,
218 struct weston_seat *seat);
220 static struct workspace *
221 get_current_workspace(struct desktop_shell *shell);
223 static struct shell_surface *
224 get_shell_surface(struct weston_surface *surface);
226 static struct desktop_shell *
227 shell_surface_get_shell(struct shell_surface *shsurf);
230 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
232 struct desktop_shell *shell;
233 struct weston_surface *top_fs_es;
235 shell = shell_surface_get_shell(shsurf);
237 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
240 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
241 struct weston_surface,
243 return (shsurf == get_shell_surface(top_fs_es));
247 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
249 struct shell_grab *grab;
251 grab = container_of(listener, struct shell_grab,
252 shsurf_destroy_listener);
258 shell_grab_start(struct shell_grab *grab,
259 const struct wl_pointer_grab_interface *interface,
260 struct shell_surface *shsurf,
261 struct wl_pointer *pointer,
262 enum desktop_shell_cursor cursor)
264 struct desktop_shell *shell = shsurf->shell;
266 grab->grab.interface = interface;
267 grab->shsurf = shsurf;
268 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
269 wl_signal_add(&shsurf->resource.destroy_signal,
270 &grab->shsurf_destroy_listener);
272 grab->pointer = pointer;
273 grab->grab.focus = &shsurf->surface->surface;
275 wl_pointer_start_grab(pointer, &grab->grab);
276 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
277 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
278 wl_fixed_from_int(0), wl_fixed_from_int(0));
282 shell_grab_end(struct shell_grab *grab)
285 wl_list_remove(&grab->shsurf_destroy_listener.link);
287 wl_pointer_end_grab(grab->pointer);
291 center_on_output(struct weston_surface *surface,
292 struct weston_output *output);
294 static enum weston_keyboard_modifier
295 get_modifier(char *modifier)
298 return MODIFIER_SUPER;
300 if (!strcmp("ctrl", modifier))
301 return MODIFIER_CTRL;
302 else if (!strcmp("alt", modifier))
304 else if (!strcmp("super", modifier))
305 return MODIFIER_SUPER;
307 return MODIFIER_SUPER;
310 static enum animation_type
311 get_animation_type(char *animation)
314 return ANIMATION_NONE;
316 if (!strcmp("zoom", animation))
317 return ANIMATION_ZOOM;
318 else if (!strcmp("fade", animation))
319 return ANIMATION_FADE;
321 return ANIMATION_NONE;
325 shell_configuration(struct desktop_shell *shell)
330 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
331 char *modifier = NULL;
332 char *win_animation = NULL;
334 struct config_key shell_keys[] = {
335 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
336 { "animation", CONFIG_KEY_STRING, &win_animation},
338 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
341 struct config_key saver_keys[] = {
342 { "path", CONFIG_KEY_STRING, &path },
343 { "duration", CONFIG_KEY_INTEGER, &duration },
346 struct config_section cs[] = {
347 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
348 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
351 config_file = config_file_path("weston.ini");
352 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
355 shell->screensaver.path = path;
356 shell->screensaver.duration = duration;
357 shell->binding_modifier = get_modifier(modifier);
358 shell->win_animation_type = get_animation_type(win_animation);
359 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
363 focus_state_destroy(struct focus_state *state)
365 wl_list_remove(&state->seat_destroy_listener.link);
366 wl_list_remove(&state->surface_destroy_listener.link);
371 focus_state_seat_destroy(struct wl_listener *listener, void *data)
373 struct focus_state *state = container_of(listener,
375 seat_destroy_listener);
377 wl_list_remove(&state->link);
378 focus_state_destroy(state);
382 focus_state_surface_destroy(struct wl_listener *listener, void *data)
384 struct focus_state *state = container_of(listener,
386 surface_destroy_listener);
387 struct desktop_shell *shell;
388 struct weston_surface *surface, *next;
391 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
392 if (surface == state->keyboard_focus)
400 shell = state->seat->compositor->shell_interface.shell;
401 activate(shell, next, state->seat);
403 wl_list_remove(&state->link);
404 focus_state_destroy(state);
408 static struct focus_state *
409 focus_state_create(struct weston_seat *seat, struct workspace *ws)
411 struct focus_state *state;
413 state = malloc(sizeof *state);
419 wl_list_insert(&ws->focus_list, &state->link);
421 state->seat_destroy_listener.notify = focus_state_seat_destroy;
422 state->surface_destroy_listener.notify = focus_state_surface_destroy;
423 wl_signal_add(&seat->seat.destroy_signal,
424 &state->seat_destroy_listener);
425 wl_list_init(&state->surface_destroy_listener.link);
431 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
433 struct focus_state *state, *next;
435 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
436 if (state->keyboard_focus)
437 wl_keyboard_set_focus(state->seat->seat.keyboard,
438 &state->keyboard_focus->surface);
443 workspace_destroy(struct workspace *ws)
445 struct focus_state *state, *next;
447 wl_list_for_each_safe(state, next, &ws->focus_list, link)
448 focus_state_destroy(state);
454 seat_destroyed(struct wl_listener *listener, void *data)
456 struct weston_seat *seat = data;
457 struct focus_state *state, *next;
458 struct workspace *ws = container_of(listener,
460 seat_destroyed_listener);
462 wl_list_for_each_safe(state, next, &ws->focus_list, link)
463 if (state->seat == seat)
464 wl_list_remove(&state->link);
467 static struct workspace *
468 workspace_create(void)
470 struct workspace *ws = malloc(sizeof *ws);
474 weston_layer_init(&ws->layer, NULL);
476 wl_list_init(&ws->focus_list);
477 wl_list_init(&ws->seat_destroyed_listener.link);
478 ws->seat_destroyed_listener.notify = seat_destroyed;
484 workspace_is_empty(struct workspace *ws)
486 return wl_list_empty(&ws->layer.surface_list);
489 static struct workspace *
490 get_workspace(struct desktop_shell *shell, unsigned int index)
492 struct workspace **pws = shell->workspaces.array.data;
497 static struct workspace *
498 get_current_workspace(struct desktop_shell *shell)
500 return get_workspace(shell, shell->workspaces.current);
504 activate_workspace(struct desktop_shell *shell, unsigned int index)
506 struct workspace *ws;
508 ws = get_workspace(shell, index);
509 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
511 shell->workspaces.current = index;
515 get_output_height(struct weston_output *output)
517 return abs(output->region.extents.y1 - output->region.extents.y2);
521 surface_translate(struct weston_surface *surface, double d)
523 struct shell_surface *shsurf = get_shell_surface(surface);
524 struct weston_transform *transform;
526 transform = &shsurf->workspace_transform;
527 if (wl_list_empty(&transform->link))
528 wl_list_insert(surface->geometry.transformation_list.prev,
529 &shsurf->workspace_transform.link);
531 weston_matrix_init(&shsurf->workspace_transform.matrix);
532 weston_matrix_translate(&shsurf->workspace_transform.matrix,
534 surface->geometry.dirty = 1;
538 workspace_translate_out(struct workspace *ws, double fraction)
540 struct weston_surface *surface;
544 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
545 height = get_output_height(surface->output);
546 d = height * fraction;
548 surface_translate(surface, d);
553 workspace_translate_in(struct workspace *ws, double fraction)
555 struct weston_surface *surface;
559 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
560 height = get_output_height(surface->output);
563 d = -(height - height * fraction);
565 d = height + height * fraction;
567 surface_translate(surface, d);
572 workspace_damage_all_surfaces(struct workspace *ws)
574 struct weston_surface *surface;
576 wl_list_for_each(surface, &ws->layer.surface_list, layer_link)
577 weston_surface_damage(surface);
581 reverse_workspace_change_animation(struct desktop_shell *shell,
583 struct workspace *from,
584 struct workspace *to)
586 shell->workspaces.current = index;
588 shell->workspaces.anim_to = to;
589 shell->workspaces.anim_from = from;
590 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
591 shell->workspaces.anim_timestamp = 0;
593 restore_focus_state(shell, to);
595 workspace_damage_all_surfaces(from);
596 workspace_damage_all_surfaces(to);
600 workspace_deactivate_transforms(struct workspace *ws)
602 struct weston_surface *surface;
603 struct shell_surface *shsurf;
605 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
606 shsurf = get_shell_surface(surface);
607 wl_list_remove(&shsurf->workspace_transform.link);
608 wl_list_init(&shsurf->workspace_transform.link);
609 shsurf->surface->geometry.dirty = 1;
614 finish_workspace_change_animation(struct desktop_shell *shell,
615 struct workspace *from,
616 struct workspace *to)
618 workspace_damage_all_surfaces(from);
619 workspace_damage_all_surfaces(to);
621 wl_list_remove(&shell->workspaces.animation.link);
622 workspace_deactivate_transforms(from);
623 workspace_deactivate_transforms(to);
624 shell->workspaces.anim_to = NULL;
626 wl_list_remove(&shell->workspaces.anim_from->layer.link);
630 animate_workspace_change_frame(struct weston_animation *animation,
631 struct weston_output *output, uint32_t msecs)
633 struct desktop_shell *shell =
634 container_of(animation, struct desktop_shell,
635 workspaces.animation);
636 struct workspace *from = shell->workspaces.anim_from;
637 struct workspace *to = shell->workspaces.anim_to;
641 if (workspace_is_empty(from) && workspace_is_empty(to)) {
642 finish_workspace_change_animation(shell, from, to);
646 if (shell->workspaces.anim_timestamp == 0) {
647 if (shell->workspaces.anim_current == 0.0)
648 shell->workspaces.anim_timestamp = msecs;
650 shell->workspaces.anim_timestamp =
652 /* Invers of movement function 'y' below. */
653 (asin(1.0 - shell->workspaces.anim_current) *
654 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
658 t = msecs - shell->workspaces.anim_timestamp;
664 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
667 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
668 workspace_damage_all_surfaces(from);
669 workspace_damage_all_surfaces(to);
671 workspace_translate_out(from, shell->workspaces.anim_dir * y);
672 workspace_translate_in(to, shell->workspaces.anim_dir * y);
673 shell->workspaces.anim_current = y;
675 workspace_damage_all_surfaces(from);
676 workspace_damage_all_surfaces(to);
679 finish_workspace_change_animation(shell, from, to);
683 animate_workspace_change(struct desktop_shell *shell,
685 struct workspace *from,
686 struct workspace *to)
688 struct weston_output *output;
692 if (index > shell->workspaces.current)
697 shell->workspaces.current = index;
699 shell->workspaces.anim_dir = dir;
700 shell->workspaces.anim_from = from;
701 shell->workspaces.anim_to = to;
702 shell->workspaces.anim_current = 0.0;
703 shell->workspaces.anim_timestamp = 0;
705 output = container_of(shell->compositor->output_list.next,
706 struct weston_output, link);
707 wl_list_insert(&output->animation_list,
708 &shell->workspaces.animation.link);
710 wl_list_insert(&from->layer.link, &to->layer.link);
712 workspace_translate_in(to, 0);
714 restore_focus_state(shell, to);
716 workspace_damage_all_surfaces(from);
717 workspace_damage_all_surfaces(to);
721 change_workspace(struct desktop_shell *shell, unsigned int index)
723 struct workspace *from;
724 struct workspace *to;
726 if (index == shell->workspaces.current)
729 /* Don't change workspace when there is any fullscreen surfaces. */
730 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
733 from = get_current_workspace(shell);
734 to = get_workspace(shell, index);
736 if (shell->workspaces.anim_from == to &&
737 shell->workspaces.anim_to == from) {
738 reverse_workspace_change_animation(shell, index, from, to);
742 if (shell->workspaces.anim_to != NULL)
743 finish_workspace_change_animation(shell,
744 shell->workspaces.anim_from,
745 shell->workspaces.anim_to);
747 if (workspace_is_empty(to) && workspace_is_empty(from)) {
748 shell->workspaces.current = index;
749 wl_list_insert(&from->layer.link, &to->layer.link);
750 wl_list_remove(&from->layer.link);
752 restore_focus_state(shell, to);
755 animate_workspace_change(shell, index, from, to);
759 noop_grab_focus(struct wl_pointer_grab *grab,
760 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
766 move_grab_motion(struct wl_pointer_grab *grab,
767 uint32_t time, wl_fixed_t x, wl_fixed_t y)
769 struct weston_move_grab *move = (struct weston_move_grab *) grab;
770 struct wl_pointer *pointer = grab->pointer;
771 struct shell_surface *shsurf = move->base.shsurf;
772 struct weston_surface *es;
773 int dx = wl_fixed_to_int(pointer->x + move->dx);
774 int dy = wl_fixed_to_int(pointer->y + move->dy);
779 es = shsurf->surface;
781 weston_surface_configure(es, dx, dy,
782 es->geometry.width, es->geometry.height);
784 weston_compositor_schedule_repaint(es->compositor);
788 move_grab_button(struct wl_pointer_grab *grab,
789 uint32_t time, uint32_t button, uint32_t state_w)
791 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
793 struct wl_pointer *pointer = grab->pointer;
794 enum wl_pointer_button_state state = state_w;
796 if (pointer->button_count == 0 &&
797 state == WL_POINTER_BUTTON_STATE_RELEASED) {
798 shell_grab_end(shell_grab);
803 static const struct wl_pointer_grab_interface move_grab_interface = {
810 busy_cursor_grab_focus(struct wl_pointer_grab *base,
811 struct wl_surface *surface, int32_t x, int32_t y)
813 struct shell_grab *grab = (struct shell_grab *) base;
815 if (grab->grab.focus != surface) {
816 shell_grab_end(grab);
822 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
823 uint32_t time, int32_t x, int32_t y)
828 busy_cursor_grab_button(struct wl_pointer_grab *grab,
829 uint32_t time, uint32_t button, uint32_t state)
833 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
834 busy_cursor_grab_focus,
835 busy_cursor_grab_motion,
836 busy_cursor_grab_button,
840 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
842 struct shell_grab *grab;
844 grab = malloc(sizeof *grab);
848 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
849 DESKTOP_SHELL_CURSOR_BUSY);
853 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
855 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
857 if (grab->grab.interface == &busy_cursor_grab_interface) {
858 shell_grab_end(grab);
864 ping_timer_destroy(struct shell_surface *shsurf)
866 if (!shsurf || !shsurf->ping_timer)
869 if (shsurf->ping_timer->source)
870 wl_event_source_remove(shsurf->ping_timer->source);
872 free(shsurf->ping_timer);
873 shsurf->ping_timer = NULL;
877 ping_timeout_handler(void *data)
879 struct shell_surface *shsurf = data;
880 struct weston_seat *seat;
882 /* Client is not responding */
883 shsurf->unresponsive = 1;
885 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
886 if (seat->seat.pointer->focus == &shsurf->surface->surface)
887 set_busy_cursor(shsurf, seat->seat.pointer);
893 ping_handler(struct weston_surface *surface, uint32_t serial)
895 struct shell_surface *shsurf = get_shell_surface(surface);
896 struct wl_event_loop *loop;
897 int ping_timeout = 200;
901 if (!shsurf->resource.client)
904 if (shsurf->surface == shsurf->shell->grab_surface)
907 if (!shsurf->ping_timer) {
908 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
909 if (!shsurf->ping_timer)
912 shsurf->ping_timer->serial = serial;
913 loop = wl_display_get_event_loop(surface->compositor->wl_display);
914 shsurf->ping_timer->source =
915 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
916 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
918 wl_shell_surface_send_ping(&shsurf->resource, serial);
923 handle_pointer_focus(struct wl_listener *listener, void *data)
925 struct wl_pointer *pointer = data;
926 struct weston_surface *surface =
927 (struct weston_surface *) pointer->focus;
928 struct weston_compositor *compositor;
929 struct shell_surface *shsurf;
935 compositor = surface->compositor;
936 shsurf = get_shell_surface(surface);
938 if (shsurf && shsurf->unresponsive) {
939 set_busy_cursor(shsurf, pointer);
941 serial = wl_display_next_serial(compositor->wl_display);
942 ping_handler(surface, serial);
947 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
950 struct shell_surface *shsurf = resource->data;
951 struct desktop_shell *shell = shsurf->shell;
952 struct weston_seat *seat;
953 struct weston_compositor *ec = shsurf->surface->compositor;
954 struct wl_pointer *pointer;
955 int was_unresponsive;
957 if (shsurf->ping_timer->serial == serial) {
958 was_unresponsive = shsurf->unresponsive;
959 shsurf->unresponsive = 0;
960 if (was_unresponsive) {
961 /* Received pong from previously unresponsive client */
962 wl_list_for_each(seat, &ec->seat_list, link) {
963 pointer = seat->seat.pointer;
964 if (pointer->focus ==
965 &shell->grab_surface->surface &&
967 &shsurf->surface->surface)
968 end_busy_cursor(shsurf, pointer);
971 ping_timer_destroy(shsurf);
976 shell_surface_set_title(struct wl_client *client,
977 struct wl_resource *resource, const char *title)
979 struct shell_surface *shsurf = resource->data;
982 shsurf->title = strdup(title);
986 shell_surface_set_class(struct wl_client *client,
987 struct wl_resource *resource, const char *class)
989 struct shell_surface *shsurf = resource->data;
992 shsurf->class = strdup(class);
996 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
998 struct weston_move_grab *move;
1003 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1006 move = malloc(sizeof *move);
1010 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1011 ws->seat.pointer->grab_x;
1012 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1013 ws->seat.pointer->grab_y;
1015 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1016 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1022 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1023 struct wl_resource *seat_resource, uint32_t serial)
1025 struct weston_seat *ws = seat_resource->data;
1026 struct shell_surface *shsurf = resource->data;
1028 if (ws->seat.pointer->button_count == 0 ||
1029 ws->seat.pointer->grab_serial != serial ||
1030 ws->seat.pointer->focus != &shsurf->surface->surface)
1033 if (surface_move(shsurf, ws) < 0)
1034 wl_resource_post_no_memory(resource);
1037 struct weston_resize_grab {
1038 struct shell_grab base;
1040 int32_t width, height;
1044 resize_grab_motion(struct wl_pointer_grab *grab,
1045 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1047 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1048 struct wl_pointer *pointer = grab->pointer;
1049 struct shell_surface *shsurf = resize->base.shsurf;
1050 int32_t width, height;
1051 wl_fixed_t from_x, from_y;
1052 wl_fixed_t to_x, to_y;
1057 weston_surface_from_global_fixed(shsurf->surface,
1058 pointer->grab_x, pointer->grab_y,
1060 weston_surface_from_global_fixed(shsurf->surface,
1061 pointer->x, pointer->y, &to_x, &to_y);
1063 width = resize->width;
1064 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1065 width += wl_fixed_to_int(from_x - to_x);
1066 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1067 width += wl_fixed_to_int(to_x - from_x);
1070 height = resize->height;
1071 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1072 height += wl_fixed_to_int(from_y - to_y);
1073 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1074 height += wl_fixed_to_int(to_y - from_y);
1077 shsurf->client->send_configure(shsurf->surface,
1078 resize->edges, width, height);
1082 send_configure(struct weston_surface *surface,
1083 uint32_t edges, int32_t width, int32_t height)
1085 struct shell_surface *shsurf = get_shell_surface(surface);
1087 wl_shell_surface_send_configure(&shsurf->resource,
1088 edges, width, height);
1091 static const struct weston_shell_client shell_client = {
1096 resize_grab_button(struct wl_pointer_grab *grab,
1097 uint32_t time, uint32_t button, uint32_t state_w)
1099 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1100 struct wl_pointer *pointer = grab->pointer;
1101 enum wl_pointer_button_state state = state_w;
1103 if (pointer->button_count == 0 &&
1104 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1105 shell_grab_end(&resize->base);
1110 static const struct wl_pointer_grab_interface resize_grab_interface = {
1117 surface_resize(struct shell_surface *shsurf,
1118 struct weston_seat *ws, uint32_t edges)
1120 struct weston_resize_grab *resize;
1122 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1125 if (edges == 0 || edges > 15 ||
1126 (edges & 3) == 3 || (edges & 12) == 12)
1129 resize = malloc(sizeof *resize);
1133 resize->edges = edges;
1134 resize->width = shsurf->surface->geometry.width;
1135 resize->height = shsurf->surface->geometry.height;
1137 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1138 ws->seat.pointer, edges);
1144 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1145 struct wl_resource *seat_resource, uint32_t serial,
1148 struct weston_seat *ws = seat_resource->data;
1149 struct shell_surface *shsurf = resource->data;
1151 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1154 if (ws->seat.pointer->button_count == 0 ||
1155 ws->seat.pointer->grab_serial != serial ||
1156 ws->seat.pointer->focus != &shsurf->surface->surface)
1159 if (surface_resize(shsurf, ws, edges) < 0)
1160 wl_resource_post_no_memory(resource);
1163 static struct weston_output *
1164 get_default_output(struct weston_compositor *compositor)
1166 return container_of(compositor->output_list.next,
1167 struct weston_output, link);
1171 shell_unset_fullscreen(struct shell_surface *shsurf)
1173 /* undo all fullscreen things here */
1174 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1175 shell_surface_is_top_fullscreen(shsurf)) {
1176 weston_output_switch_mode(shsurf->fullscreen_output,
1177 shsurf->fullscreen_output->origin);
1179 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1180 shsurf->fullscreen.framerate = 0;
1181 wl_list_remove(&shsurf->fullscreen.transform.link);
1182 wl_list_init(&shsurf->fullscreen.transform.link);
1183 if (shsurf->fullscreen.black_surface)
1184 weston_surface_destroy(shsurf->fullscreen.black_surface);
1185 shsurf->fullscreen.black_surface = NULL;
1186 shsurf->fullscreen_output = NULL;
1187 weston_surface_set_position(shsurf->surface,
1188 shsurf->saved_x, shsurf->saved_y);
1189 if (shsurf->saved_rotation_valid) {
1190 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1191 &shsurf->rotation.transform.link);
1192 shsurf->saved_rotation_valid = false;
1197 reset_shell_surface_type(struct shell_surface *surface)
1199 switch (surface->type) {
1200 case SHELL_SURFACE_FULLSCREEN:
1201 shell_unset_fullscreen(surface);
1203 case SHELL_SURFACE_MAXIMIZED:
1204 surface->output = get_default_output(surface->surface->compositor);
1205 weston_surface_set_position(surface->surface,
1209 case SHELL_SURFACE_NONE:
1210 case SHELL_SURFACE_TOPLEVEL:
1211 case SHELL_SURFACE_TRANSIENT:
1212 case SHELL_SURFACE_POPUP:
1216 surface->type = SHELL_SURFACE_NONE;
1221 set_surface_type(struct shell_surface *shsurf)
1223 struct weston_surface *surface = shsurf->surface;
1224 struct weston_surface *pes = shsurf->parent;
1226 reset_shell_surface_type(shsurf);
1228 shsurf->type = shsurf->next_type;
1229 shsurf->next_type = SHELL_SURFACE_NONE;
1231 switch (shsurf->type) {
1232 case SHELL_SURFACE_TOPLEVEL:
1234 case SHELL_SURFACE_TRANSIENT:
1235 weston_surface_set_position(surface,
1236 pes->geometry.x + shsurf->transient.x,
1237 pes->geometry.y + shsurf->transient.y);
1240 case SHELL_SURFACE_MAXIMIZED:
1241 shsurf->saved_x = surface->geometry.x;
1242 shsurf->saved_y = surface->geometry.y;
1243 shsurf->saved_position_valid = true;
1246 case SHELL_SURFACE_FULLSCREEN:
1247 shsurf->saved_x = surface->geometry.x;
1248 shsurf->saved_y = surface->geometry.y;
1249 shsurf->saved_position_valid = true;
1251 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1252 wl_list_remove(&shsurf->rotation.transform.link);
1253 wl_list_init(&shsurf->rotation.transform.link);
1254 shsurf->surface->geometry.dirty = 1;
1255 shsurf->saved_rotation_valid = true;
1265 set_toplevel(struct shell_surface *shsurf)
1267 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1271 shell_surface_set_toplevel(struct wl_client *client,
1272 struct wl_resource *resource)
1274 struct shell_surface *surface = resource->data;
1276 set_toplevel(surface);
1280 set_transient(struct shell_surface *shsurf,
1281 struct weston_surface *parent, int x, int y, uint32_t flags)
1283 /* assign to parents output */
1284 shsurf->parent = parent;
1285 shsurf->transient.x = x;
1286 shsurf->transient.y = y;
1287 shsurf->transient.flags = flags;
1288 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1292 shell_surface_set_transient(struct wl_client *client,
1293 struct wl_resource *resource,
1294 struct wl_resource *parent_resource,
1295 int x, int y, uint32_t flags)
1297 struct shell_surface *shsurf = resource->data;
1298 struct weston_surface *parent = parent_resource->data;
1300 set_transient(shsurf, parent, x, y, flags);
1303 static struct desktop_shell *
1304 shell_surface_get_shell(struct shell_surface *shsurf)
1306 return shsurf->shell;
1310 get_output_panel_height(struct desktop_shell *shell,
1311 struct weston_output *output)
1313 struct weston_surface *surface;
1314 int panel_height = 0;
1319 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1320 if (surface->output == output) {
1321 panel_height = surface->geometry.height;
1326 return panel_height;
1330 shell_surface_set_maximized(struct wl_client *client,
1331 struct wl_resource *resource,
1332 struct wl_resource *output_resource )
1334 struct shell_surface *shsurf = resource->data;
1335 struct weston_surface *es = shsurf->surface;
1336 struct desktop_shell *shell = NULL;
1337 uint32_t edges = 0, panel_height = 0;
1339 /* get the default output, if the client set it as NULL
1340 check whether the ouput is available */
1341 if (output_resource)
1342 shsurf->output = output_resource->data;
1343 else if (es->output)
1344 shsurf->output = es->output;
1346 shsurf->output = get_default_output(es->compositor);
1348 shell = shell_surface_get_shell(shsurf);
1349 panel_height = get_output_panel_height(shell, shsurf->output);
1350 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1352 shsurf->client->send_configure(shsurf->surface, edges,
1353 shsurf->output->current->width,
1354 shsurf->output->current->height - panel_height);
1356 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1360 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1362 static struct weston_surface *
1363 create_black_surface(struct weston_compositor *ec,
1364 struct weston_surface *fs_surface,
1365 GLfloat x, GLfloat y, int w, int h)
1367 struct weston_surface *surface = NULL;
1369 surface = weston_surface_create(ec);
1370 if (surface == NULL) {
1371 weston_log("no memory\n");
1375 surface->configure = black_surface_configure;
1376 surface->private = fs_surface;
1377 weston_surface_configure(surface, x, y, w, h);
1378 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1382 /* Create black surface and append it to the associated fullscreen surface.
1383 * Handle size dismatch and positioning according to the method. */
1385 shell_configure_fullscreen(struct shell_surface *shsurf)
1387 struct weston_output *output = shsurf->fullscreen_output;
1388 struct weston_surface *surface = shsurf->surface;
1389 struct weston_matrix *matrix;
1390 float scale, output_aspect, surface_aspect, x, y;
1392 if (!shsurf->fullscreen.black_surface)
1393 shsurf->fullscreen.black_surface =
1394 create_black_surface(surface->compositor,
1396 output->x, output->y,
1397 output->current->width,
1398 output->current->height);
1400 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1401 wl_list_insert(&surface->layer_link,
1402 &shsurf->fullscreen.black_surface->layer_link);
1403 shsurf->fullscreen.black_surface->output = output;
1405 switch (shsurf->fullscreen.type) {
1406 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1407 if (surface->buffer)
1408 center_on_output(surface, shsurf->fullscreen_output);
1410 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1411 matrix = &shsurf->fullscreen.transform.matrix;
1412 weston_matrix_init(matrix);
1414 output_aspect = (float) output->current->width /
1415 (float) output->current->height;
1416 surface_aspect = (float) surface->geometry.width /
1417 (float) surface->geometry.height;
1418 if (output_aspect < surface_aspect)
1419 scale = (float) output->current->width /
1420 (float) surface->geometry.width;
1422 scale = (float) output->current->height /
1423 (float) surface->geometry.height;
1425 weston_matrix_scale(matrix, scale, scale, 1);
1426 wl_list_remove(&shsurf->fullscreen.transform.link);
1427 wl_list_insert(&surface->geometry.transformation_list,
1428 &shsurf->fullscreen.transform.link);
1429 x = output->x + (output->current->width - surface->geometry.width * scale) / 2;
1430 y = output->y + (output->current->height - surface->geometry.height * scale) / 2;
1431 weston_surface_set_position(surface, x, y);
1434 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1435 if (shell_surface_is_top_fullscreen(shsurf)) {
1436 struct weston_mode mode = {0,
1437 surface->geometry.width,
1438 surface->geometry.height,
1439 shsurf->fullscreen.framerate};
1441 if (weston_output_switch_mode(output, &mode) == 0) {
1442 weston_surface_configure(shsurf->fullscreen.black_surface,
1443 output->x, output->y,
1444 output->current->width,
1445 output->current->height);
1446 weston_surface_set_position(surface, output->x, output->y);
1451 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1458 /* make the fullscreen and black surface at the top */
1460 shell_stack_fullscreen(struct shell_surface *shsurf)
1462 struct weston_output *output = shsurf->fullscreen_output;
1463 struct weston_surface *surface = shsurf->surface;
1464 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1466 wl_list_remove(&surface->layer_link);
1467 wl_list_insert(&shell->fullscreen_layer.surface_list,
1468 &surface->layer_link);
1469 weston_surface_damage(surface);
1471 if (!shsurf->fullscreen.black_surface)
1472 shsurf->fullscreen.black_surface =
1473 create_black_surface(surface->compositor,
1475 output->x, output->y,
1476 output->current->width,
1477 output->current->height);
1479 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1480 wl_list_insert(&surface->layer_link,
1481 &shsurf->fullscreen.black_surface->layer_link);
1482 weston_surface_damage(shsurf->fullscreen.black_surface);
1486 shell_map_fullscreen(struct shell_surface *shsurf)
1488 shell_stack_fullscreen(shsurf);
1489 shell_configure_fullscreen(shsurf);
1493 shell_surface_set_fullscreen(struct wl_client *client,
1494 struct wl_resource *resource,
1497 struct wl_resource *output_resource)
1499 struct shell_surface *shsurf = resource->data;
1500 struct weston_surface *es = shsurf->surface;
1502 if (output_resource)
1503 shsurf->output = output_resource->data;
1504 else if (es->output)
1505 shsurf->output = es->output;
1507 shsurf->output = get_default_output(es->compositor);
1509 shsurf->fullscreen_output = shsurf->output;
1510 shsurf->fullscreen.type = method;
1511 shsurf->fullscreen.framerate = framerate;
1512 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1514 shsurf->client->send_configure(shsurf->surface, 0,
1515 shsurf->output->current->width,
1516 shsurf->output->current->height);
1520 popup_grab_focus(struct wl_pointer_grab *grab,
1521 struct wl_surface *surface,
1525 struct wl_pointer *pointer = grab->pointer;
1526 struct shell_surface *priv =
1527 container_of(grab, struct shell_surface, popup.grab);
1528 struct wl_client *client = priv->surface->surface.resource.client;
1530 if (surface && surface->resource.client == client) {
1531 wl_pointer_set_focus(pointer, surface, x, y);
1532 grab->focus = surface;
1534 wl_pointer_set_focus(pointer, NULL,
1535 wl_fixed_from_int(0),
1536 wl_fixed_from_int(0));
1542 popup_grab_motion(struct wl_pointer_grab *grab,
1543 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1545 struct wl_resource *resource;
1547 resource = grab->pointer->focus_resource;
1549 wl_pointer_send_motion(resource, time, sx, sy);
1553 popup_grab_button(struct wl_pointer_grab *grab,
1554 uint32_t time, uint32_t button, uint32_t state_w)
1556 struct wl_resource *resource;
1557 struct shell_surface *shsurf =
1558 container_of(grab, struct shell_surface, popup.grab);
1559 struct wl_display *display;
1560 enum wl_pointer_button_state state = state_w;
1563 resource = grab->pointer->focus_resource;
1565 display = wl_client_get_display(resource->client);
1566 serial = wl_display_get_serial(display);
1567 wl_pointer_send_button(resource, serial, time, button, state);
1568 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1569 (shsurf->popup.initial_up ||
1570 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1571 wl_shell_surface_send_popup_done(&shsurf->resource);
1572 wl_pointer_end_grab(grab->pointer);
1573 shsurf->popup.grab.pointer = NULL;
1576 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1577 shsurf->popup.initial_up = 1;
1580 static const struct wl_pointer_grab_interface popup_grab_interface = {
1587 shell_map_popup(struct shell_surface *shsurf)
1589 struct wl_seat *seat = shsurf->popup.seat;
1590 struct weston_surface *es = shsurf->surface;
1591 struct weston_surface *parent = shsurf->parent;
1593 es->output = parent->output;
1594 shsurf->popup.grab.interface = &popup_grab_interface;
1596 weston_surface_update_transform(parent);
1597 if (parent->transform.enabled) {
1598 shsurf->popup.parent_transform.matrix =
1599 parent->transform.matrix;
1601 /* construct x, y translation matrix */
1602 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1603 shsurf->popup.parent_transform.matrix.d[12] =
1605 shsurf->popup.parent_transform.matrix.d[13] =
1608 wl_list_insert(es->geometry.transformation_list.prev,
1609 &shsurf->popup.parent_transform.link);
1610 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1612 shsurf->popup.initial_up = 0;
1614 /* We don't require the grab to still be active, but if another
1615 * grab has started in the meantime, we end the popup now. */
1616 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1617 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1619 wl_shell_surface_send_popup_done(&shsurf->resource);
1624 shell_surface_set_popup(struct wl_client *client,
1625 struct wl_resource *resource,
1626 struct wl_resource *seat_resource,
1628 struct wl_resource *parent_resource,
1629 int32_t x, int32_t y, uint32_t flags)
1631 struct shell_surface *shsurf = resource->data;
1633 shsurf->type = SHELL_SURFACE_POPUP;
1634 shsurf->parent = parent_resource->data;
1635 shsurf->popup.seat = seat_resource->data;
1636 shsurf->popup.serial = serial;
1637 shsurf->popup.x = x;
1638 shsurf->popup.y = y;
1641 static const struct wl_shell_surface_interface shell_surface_implementation = {
1644 shell_surface_resize,
1645 shell_surface_set_toplevel,
1646 shell_surface_set_transient,
1647 shell_surface_set_fullscreen,
1648 shell_surface_set_popup,
1649 shell_surface_set_maximized,
1650 shell_surface_set_title,
1651 shell_surface_set_class
1655 destroy_shell_surface(struct shell_surface *shsurf)
1657 if (shsurf->popup.grab.pointer)
1658 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1660 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1661 shell_surface_is_top_fullscreen(shsurf)) {
1662 weston_output_switch_mode(shsurf->fullscreen_output,
1663 shsurf->fullscreen_output->origin);
1666 if (shsurf->fullscreen.black_surface)
1667 weston_surface_destroy(shsurf->fullscreen.black_surface);
1669 /* As destroy_resource() use wl_list_for_each_safe(),
1670 * we can always remove the listener.
1672 wl_list_remove(&shsurf->surface_destroy_listener.link);
1673 shsurf->surface->configure = NULL;
1674 ping_timer_destroy(shsurf);
1676 wl_list_remove(&shsurf->link);
1681 shell_destroy_shell_surface(struct wl_resource *resource)
1683 struct shell_surface *shsurf = resource->data;
1685 destroy_shell_surface(shsurf);
1689 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1691 struct shell_surface *shsurf = container_of(listener,
1692 struct shell_surface,
1693 surface_destroy_listener);
1695 if (shsurf->resource.client) {
1696 wl_resource_destroy(&shsurf->resource);
1698 wl_signal_emit(&shsurf->resource.destroy_signal,
1700 destroy_shell_surface(shsurf);
1705 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
1707 static struct shell_surface *
1708 get_shell_surface(struct weston_surface *surface)
1710 if (surface->configure == shell_surface_configure)
1711 return surface->private;
1716 static struct shell_surface *
1717 create_shell_surface(void *shell, struct weston_surface *surface,
1718 const struct weston_shell_client *client)
1720 struct shell_surface *shsurf;
1722 if (surface->configure) {
1723 weston_log("surface->configure already set\n");
1727 shsurf = calloc(1, sizeof *shsurf);
1729 weston_log("no memory to allocate shell surface\n");
1733 surface->configure = shell_surface_configure;
1734 surface->private = shsurf;
1736 shsurf->shell = (struct desktop_shell *) shell;
1737 shsurf->unresponsive = 0;
1738 shsurf->saved_position_valid = false;
1739 shsurf->saved_rotation_valid = false;
1740 shsurf->surface = surface;
1741 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1742 shsurf->fullscreen.framerate = 0;
1743 shsurf->fullscreen.black_surface = NULL;
1744 shsurf->ping_timer = NULL;
1745 wl_list_init(&shsurf->fullscreen.transform.link);
1747 wl_signal_init(&shsurf->resource.destroy_signal);
1748 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
1749 wl_signal_add(&surface->surface.resource.destroy_signal,
1750 &shsurf->surface_destroy_listener);
1752 /* init link so its safe to always remove it in destroy_shell_surface */
1753 wl_list_init(&shsurf->link);
1755 /* empty when not in use */
1756 wl_list_init(&shsurf->rotation.transform.link);
1757 weston_matrix_init(&shsurf->rotation.rotation);
1759 wl_list_init(&shsurf->workspace_transform.link);
1761 shsurf->type = SHELL_SURFACE_NONE;
1762 shsurf->next_type = SHELL_SURFACE_NONE;
1764 shsurf->client = client;
1770 shell_get_shell_surface(struct wl_client *client,
1771 struct wl_resource *resource,
1773 struct wl_resource *surface_resource)
1775 struct weston_surface *surface = surface_resource->data;
1776 struct desktop_shell *shell = resource->data;
1777 struct shell_surface *shsurf;
1779 if (get_shell_surface(surface)) {
1780 wl_resource_post_error(surface_resource,
1781 WL_DISPLAY_ERROR_INVALID_OBJECT,
1782 "desktop_shell::get_shell_surface already requested");
1786 shsurf = create_shell_surface(shell, surface, &shell_client);
1788 wl_resource_post_error(surface_resource,
1789 WL_DISPLAY_ERROR_INVALID_OBJECT,
1790 "surface->configure already set");
1794 shsurf->resource.destroy = shell_destroy_shell_surface;
1795 shsurf->resource.object.id = id;
1796 shsurf->resource.object.interface = &wl_shell_surface_interface;
1797 shsurf->resource.object.implementation =
1798 (void (**)(void)) &shell_surface_implementation;
1799 shsurf->resource.data = shsurf;
1801 wl_client_add_resource(client, &shsurf->resource);
1804 static const struct wl_shell_interface shell_implementation = {
1805 shell_get_shell_surface
1809 handle_screensaver_sigchld(struct weston_process *proc, int status)
1815 launch_screensaver(struct desktop_shell *shell)
1817 if (shell->screensaver.binding)
1820 if (!shell->screensaver.path)
1823 if (shell->screensaver.process.pid != 0) {
1824 weston_log("old screensaver still running\n");
1828 weston_client_launch(shell->compositor,
1829 &shell->screensaver.process,
1830 shell->screensaver.path,
1831 handle_screensaver_sigchld);
1835 terminate_screensaver(struct desktop_shell *shell)
1837 if (shell->screensaver.process.pid == 0)
1840 kill(shell->screensaver.process.pid, SIGTERM);
1844 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
1846 struct weston_surface *s, *next;
1848 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
1849 if (s->output == es->output && s != es) {
1850 weston_surface_unmap(s);
1851 s->configure = NULL;
1855 weston_surface_configure(es, es->output->x, es->output->y,
1856 es->buffer->width, es->buffer->height);
1858 if (wl_list_empty(&es->layer_link)) {
1859 wl_list_insert(&layer->surface_list, &es->layer_link);
1860 weston_compositor_schedule_repaint(es->compositor);
1865 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1867 struct desktop_shell *shell = es->private;
1869 configure_static_surface(es, &shell->background_layer);
1873 desktop_shell_set_background(struct wl_client *client,
1874 struct wl_resource *resource,
1875 struct wl_resource *output_resource,
1876 struct wl_resource *surface_resource)
1878 struct desktop_shell *shell = resource->data;
1879 struct weston_surface *surface = surface_resource->data;
1881 if (surface->configure) {
1882 wl_resource_post_error(surface_resource,
1883 WL_DISPLAY_ERROR_INVALID_OBJECT,
1884 "surface role already assigned");
1888 surface->configure = background_configure;
1889 surface->private = shell;
1890 surface->output = output_resource->data;
1891 desktop_shell_send_configure(resource, 0,
1893 surface->output->current->width,
1894 surface->output->current->height);
1898 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1900 struct desktop_shell *shell = es->private;
1902 configure_static_surface(es, &shell->panel_layer);
1906 desktop_shell_set_panel(struct wl_client *client,
1907 struct wl_resource *resource,
1908 struct wl_resource *output_resource,
1909 struct wl_resource *surface_resource)
1911 struct desktop_shell *shell = resource->data;
1912 struct weston_surface *surface = surface_resource->data;
1914 if (surface->configure) {
1915 wl_resource_post_error(surface_resource,
1916 WL_DISPLAY_ERROR_INVALID_OBJECT,
1917 "surface role already assigned");
1921 surface->configure = panel_configure;
1922 surface->private = shell;
1923 surface->output = output_resource->data;
1924 desktop_shell_send_configure(resource, 0,
1926 surface->output->current->width,
1927 surface->output->current->height);
1931 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
1933 struct desktop_shell *shell = surface->private;
1935 center_on_output(surface, get_default_output(shell->compositor));
1937 if (!weston_surface_is_mapped(surface)) {
1938 wl_list_insert(&shell->lock_layer.surface_list,
1939 &surface->layer_link);
1940 weston_surface_assign_output(surface);
1941 weston_compositor_wake(shell->compositor);
1946 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
1948 struct desktop_shell *shell =
1949 container_of(listener, struct desktop_shell, lock_surface_listener);
1951 weston_log("lock surface gone\n");
1952 shell->lock_surface = NULL;
1956 desktop_shell_set_lock_surface(struct wl_client *client,
1957 struct wl_resource *resource,
1958 struct wl_resource *surface_resource)
1960 struct desktop_shell *shell = resource->data;
1961 struct weston_surface *surface = surface_resource->data;
1963 shell->prepare_event_sent = false;
1968 shell->lock_surface = surface;
1970 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
1971 wl_signal_add(&surface_resource->destroy_signal,
1972 &shell->lock_surface_listener);
1974 surface->configure = lock_surface_configure;
1975 surface->private = shell;
1979 resume_desktop(struct desktop_shell *shell)
1981 struct weston_surface *surface;
1982 struct workspace *ws = get_current_workspace(shell);
1984 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
1985 weston_surface_unmap(surface);
1987 terminate_screensaver(shell);
1989 wl_list_remove(&shell->lock_layer.link);
1990 wl_list_insert(&shell->compositor->cursor_layer.link,
1991 &shell->fullscreen_layer.link);
1992 wl_list_insert(&shell->fullscreen_layer.link,
1993 &shell->panel_layer.link);
1994 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
1996 restore_focus_state(shell, get_current_workspace(shell));
1998 shell->locked = false;
1999 shell->compositor->idle_time = shell->compositor->option_idle_time;
2000 weston_compositor_wake(shell->compositor);
2001 weston_compositor_damage_all(shell->compositor);
2005 desktop_shell_unlock(struct wl_client *client,
2006 struct wl_resource *resource)
2008 struct desktop_shell *shell = resource->data;
2010 shell->prepare_event_sent = false;
2013 resume_desktop(shell);
2017 desktop_shell_set_grab_surface(struct wl_client *client,
2018 struct wl_resource *resource,
2019 struct wl_resource *surface_resource)
2021 struct desktop_shell *shell = resource->data;
2023 shell->grab_surface = surface_resource->data;
2026 static const struct desktop_shell_interface desktop_shell_implementation = {
2027 desktop_shell_set_background,
2028 desktop_shell_set_panel,
2029 desktop_shell_set_lock_surface,
2030 desktop_shell_unlock,
2031 desktop_shell_set_grab_surface
2034 static enum shell_surface_type
2035 get_shell_surface_type(struct weston_surface *surface)
2037 struct shell_surface *shsurf;
2039 shsurf = get_shell_surface(surface);
2041 return SHELL_SURFACE_NONE;
2042 return shsurf->type;
2046 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2048 struct weston_surface *surface =
2049 (struct weston_surface *) seat->pointer->focus;
2050 struct shell_surface *shsurf;
2052 if (surface == NULL)
2055 shsurf = get_shell_surface(surface);
2056 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2059 surface_move(shsurf, (struct weston_seat *) seat);
2063 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2065 struct weston_surface *surface =
2066 (struct weston_surface *) seat->pointer->focus;
2069 struct shell_surface *shsurf;
2071 if (surface == NULL)
2074 shsurf = get_shell_surface(surface);
2075 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2078 weston_surface_from_global(surface,
2079 wl_fixed_to_int(seat->pointer->grab_x),
2080 wl_fixed_to_int(seat->pointer->grab_y),
2083 if (x < surface->geometry.width / 3)
2084 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2085 else if (x < 2 * surface->geometry.width / 3)
2088 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2090 if (y < surface->geometry.height / 3)
2091 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2092 else if (y < 2 * surface->geometry.height / 3)
2095 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2097 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2101 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2102 wl_fixed_t value, void *data)
2105 struct shell_surface *shsurf;
2106 struct weston_surface *surface =
2107 (struct weston_surface *) seat->pointer->focus;
2109 if (surface == NULL)
2112 shsurf = get_shell_surface(surface);
2116 surface->alpha += wl_fixed_to_double(value) * step;
2118 if (surface->alpha > 1.0)
2119 surface->alpha = 1.0;
2120 if (surface->alpha < step)
2121 surface->alpha = step;
2123 surface->geometry.dirty = 1;
2124 weston_surface_damage(surface);
2128 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2131 struct weston_seat *ws = (struct weston_seat *) seat;
2132 struct weston_compositor *compositor = ws->compositor;
2133 struct weston_output *output;
2136 wl_list_for_each(output, &compositor->output_list, link) {
2137 if (pixman_region32_contains_point(&output->region,
2138 wl_fixed_to_double(seat->pointer->x),
2139 wl_fixed_to_double(seat->pointer->y),
2141 if (key == KEY_PAGEUP)
2142 increment = output->zoom.increment;
2143 else if (key == KEY_PAGEDOWN)
2144 increment = -output->zoom.increment;
2145 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2146 increment = output->zoom.increment *
2147 wl_fixed_to_double(value);
2151 output->zoom.level += increment;
2153 if (output->zoom.level < 0.0)
2154 output->zoom.level = 0.0;
2155 else if (output->zoom.level > output->zoom.max_level)
2156 output->zoom.level = output->zoom.max_level;
2158 output->zoom.active = 1;
2160 output->zoom.spring_z.target = output->zoom.level;
2162 weston_output_update_zoom(output, output->zoom.type);
2168 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2169 wl_fixed_t value, void *data)
2171 do_zoom(seat, time, 0, axis, value);
2175 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2178 do_zoom(seat, time, key, 0, 0);
2182 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2185 struct weston_compositor *compositor = data;
2187 wl_display_terminate(compositor->wl_display);
2191 rotate_grab_motion(struct wl_pointer_grab *grab,
2192 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2194 struct rotate_grab *rotate =
2195 container_of(grab, struct rotate_grab, base.grab);
2196 struct wl_pointer *pointer = grab->pointer;
2197 struct shell_surface *shsurf = rotate->base.shsurf;
2198 struct weston_surface *surface;
2199 GLfloat cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2204 surface = shsurf->surface;
2206 cx = 0.5f * surface->geometry.width;
2207 cy = 0.5f * surface->geometry.height;
2209 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2210 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2211 r = sqrtf(dx * dx + dy * dy);
2213 wl_list_remove(&shsurf->rotation.transform.link);
2214 shsurf->surface->geometry.dirty = 1;
2217 struct weston_matrix *matrix =
2218 &shsurf->rotation.transform.matrix;
2220 weston_matrix_init(&rotate->rotation);
2221 rotate->rotation.d[0] = dx / r;
2222 rotate->rotation.d[4] = -dy / r;
2223 rotate->rotation.d[1] = -rotate->rotation.d[4];
2224 rotate->rotation.d[5] = rotate->rotation.d[0];
2226 weston_matrix_init(matrix);
2227 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2228 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2229 weston_matrix_multiply(matrix, &rotate->rotation);
2230 weston_matrix_translate(matrix, cx, cy, 0.0f);
2233 &shsurf->surface->geometry.transformation_list,
2234 &shsurf->rotation.transform.link);
2236 wl_list_init(&shsurf->rotation.transform.link);
2237 weston_matrix_init(&shsurf->rotation.rotation);
2238 weston_matrix_init(&rotate->rotation);
2241 /* We need to adjust the position of the surface
2242 * in case it was resized in a rotated state before */
2243 cposx = surface->geometry.x + cx;
2244 cposy = surface->geometry.y + cy;
2245 dposx = rotate->center.x - cposx;
2246 dposy = rotate->center.y - cposy;
2247 if (dposx != 0.0f || dposy != 0.0f) {
2248 weston_surface_set_position(surface,
2249 surface->geometry.x + dposx,
2250 surface->geometry.y + dposy);
2253 /* Repaint implies weston_surface_update_transform(), which
2254 * lazily applies the damage due to rotation update.
2256 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2260 rotate_grab_button(struct wl_pointer_grab *grab,
2261 uint32_t time, uint32_t button, uint32_t state_w)
2263 struct rotate_grab *rotate =
2264 container_of(grab, struct rotate_grab, base.grab);
2265 struct wl_pointer *pointer = grab->pointer;
2266 struct shell_surface *shsurf = rotate->base.shsurf;
2267 enum wl_pointer_button_state state = state_w;
2269 if (pointer->button_count == 0 &&
2270 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2272 weston_matrix_multiply(&shsurf->rotation.rotation,
2274 shell_grab_end(&rotate->base);
2279 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2286 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2289 struct weston_surface *base_surface =
2290 (struct weston_surface *) seat->pointer->focus;
2291 struct shell_surface *surface;
2292 struct rotate_grab *rotate;
2296 if (base_surface == NULL)
2299 surface = get_shell_surface(base_surface);
2300 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2303 rotate = malloc(sizeof *rotate);
2307 weston_surface_to_global_float(surface->surface,
2308 surface->surface->geometry.width / 2,
2309 surface->surface->geometry.height / 2,
2310 &rotate->center.x, &rotate->center.y);
2312 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2313 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2314 r = sqrtf(dx * dx + dy * dy);
2316 struct weston_matrix inverse;
2318 weston_matrix_init(&inverse);
2319 inverse.d[0] = dx / r;
2320 inverse.d[4] = dy / r;
2321 inverse.d[1] = -inverse.d[4];
2322 inverse.d[5] = inverse.d[0];
2323 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2325 weston_matrix_init(&rotate->rotation);
2326 rotate->rotation.d[0] = dx / r;
2327 rotate->rotation.d[4] = -dy / r;
2328 rotate->rotation.d[1] = -rotate->rotation.d[4];
2329 rotate->rotation.d[5] = rotate->rotation.d[0];
2331 weston_matrix_init(&surface->rotation.rotation);
2332 weston_matrix_init(&rotate->rotation);
2335 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2336 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2340 lower_fullscreen_layer(struct desktop_shell *shell)
2342 struct workspace *ws;
2343 struct weston_surface *surface, *prev;
2345 ws = get_current_workspace(shell);
2346 wl_list_for_each_reverse_safe(surface, prev,
2347 &shell->fullscreen_layer.surface_list,
2349 weston_surface_restack(surface, &ws->layer.surface_list);
2353 activate(struct desktop_shell *shell, struct weston_surface *es,
2354 struct weston_seat *seat)
2356 struct workspace *ws = get_current_workspace(shell);
2357 struct focus_state *state;
2359 weston_surface_activate(es, seat);
2361 wl_list_for_each(state, &ws->focus_list, link)
2362 if (state->seat == seat)
2365 if (&state->link == &ws->focus_list) {
2366 state = focus_state_create(seat, ws);
2371 state->keyboard_focus = es;
2372 wl_list_remove(&state->surface_destroy_listener.link);
2373 wl_signal_add(&es->surface.resource.destroy_signal,
2374 &state->surface_destroy_listener);
2376 switch (get_shell_surface_type(es)) {
2377 case SHELL_SURFACE_FULLSCREEN:
2378 /* should on top of panels */
2379 shell_stack_fullscreen(get_shell_surface(es));
2380 shell_configure_fullscreen(get_shell_surface(es));
2383 ws = get_current_workspace(shell);
2384 lower_fullscreen_layer(shell);
2385 weston_surface_restack(es, &ws->layer.surface_list);
2390 /* no-op func for checking black surface */
2392 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2397 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2399 if (es->configure == black_surface_configure) {
2401 *fs_surface = (struct weston_surface *)es->private;
2408 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2411 struct weston_seat *ws = (struct weston_seat *) seat;
2412 struct desktop_shell *shell = data;
2413 struct weston_surface *focus;
2414 struct weston_surface *upper;
2416 focus = (struct weston_surface *) seat->pointer->focus;
2420 if (is_black_surface(focus, &upper))
2423 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2426 if (seat->pointer->grab == &seat->pointer->default_grab)
2427 activate(shell, focus, ws);
2431 lock(struct wl_listener *listener, void *data)
2433 struct desktop_shell *shell =
2434 container_of(listener, struct desktop_shell, lock_listener);
2435 struct weston_output *output;
2436 struct workspace *ws = get_current_workspace(shell);
2438 if (shell->locked) {
2439 wl_list_for_each(output, &shell->compositor->output_list, link)
2440 /* TODO: find a way to jump to other DPMS levels */
2441 if (output->set_dpms)
2442 output->set_dpms(output, WESTON_DPMS_STANDBY);
2446 shell->locked = true;
2448 /* Hide all surfaces by removing the fullscreen, panel and
2449 * toplevel layers. This way nothing else can show or receive
2450 * input events while we are locked. */
2452 wl_list_remove(&shell->panel_layer.link);
2453 wl_list_remove(&shell->fullscreen_layer.link);
2454 wl_list_remove(&ws->layer.link);
2455 wl_list_insert(&shell->compositor->cursor_layer.link,
2456 &shell->lock_layer.link);
2458 launch_screensaver(shell);
2460 /* TODO: disable bindings that should not work while locked. */
2462 /* All this must be undone in resume_desktop(). */
2466 unlock(struct wl_listener *listener, void *data)
2468 struct desktop_shell *shell =
2469 container_of(listener, struct desktop_shell, unlock_listener);
2471 if (!shell->locked || shell->lock_surface) {
2472 weston_compositor_wake(shell->compositor);
2476 /* If desktop-shell client has gone away, unlock immediately. */
2477 if (!shell->child.desktop_shell) {
2478 resume_desktop(shell);
2482 if (shell->prepare_event_sent)
2485 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2486 shell->prepare_event_sent = true;
2490 show_input_panels(struct wl_listener *listener, void *data)
2492 struct desktop_shell *shell =
2493 container_of(listener, struct desktop_shell, show_input_panel_listener);
2494 struct weston_surface *surface, *next;
2496 wl_list_for_each_safe(surface, next,
2497 &shell->input_panel.surfaces, layer_link) {
2498 wl_list_remove(&surface->layer_link);
2499 wl_list_insert(&shell->panel_layer.surface_list,
2500 &surface->layer_link);
2501 weston_surface_assign_output(surface);
2502 weston_surface_damage(surface);
2503 weston_slide_run(surface,
2504 surface->geometry.height, 0, NULL, NULL);
2509 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy);
2512 hide_input_panels(struct wl_listener *listener, void *data)
2514 struct desktop_shell *shell =
2515 container_of(listener, struct desktop_shell, hide_input_panel_listener);
2516 struct weston_surface *surface, *next;
2518 wl_list_for_each_safe(surface, next,
2519 &shell->panel_layer.surface_list, layer_link)
2520 if (surface->configure == input_panel_configure) {
2521 weston_surface_unmap(surface);
2522 wl_list_insert(&shell->input_panel.surfaces,
2523 &surface->layer_link);
2528 center_on_output(struct weston_surface *surface, struct weston_output *output)
2530 struct weston_mode *mode = output->current;
2531 GLfloat x = (mode->width - surface->buffer->width) / 2;
2532 GLfloat y = (mode->height - surface->buffer->height) / 2;
2534 weston_surface_configure(surface, output->x + x, output->y + y,
2535 surface->buffer->width,
2536 surface->buffer->height);
2540 map(struct desktop_shell *shell, struct weston_surface *surface,
2541 int32_t width, int32_t height, int32_t sx, int32_t sy)
2543 struct weston_compositor *compositor = shell->compositor;
2544 struct shell_surface *shsurf = get_shell_surface(surface);
2545 enum shell_surface_type surface_type = shsurf->type;
2546 struct weston_surface *parent;
2547 struct weston_seat *seat;
2548 struct workspace *ws;
2549 int panel_height = 0;
2551 surface->geometry.width = width;
2552 surface->geometry.height = height;
2553 surface->geometry.dirty = 1;
2555 /* initial positioning, see also configure() */
2556 switch (surface_type) {
2557 case SHELL_SURFACE_TOPLEVEL:
2558 weston_surface_set_position(surface, 10 + random() % 400,
2559 10 + random() % 400);
2561 case SHELL_SURFACE_FULLSCREEN:
2562 center_on_output(surface, shsurf->fullscreen_output);
2563 shell_map_fullscreen(shsurf);
2565 case SHELL_SURFACE_MAXIMIZED:
2566 /* use surface configure to set the geometry */
2567 panel_height = get_output_panel_height(shell,surface->output);
2568 weston_surface_set_position(surface, shsurf->output->x,
2569 shsurf->output->y + panel_height);
2571 case SHELL_SURFACE_POPUP:
2572 shell_map_popup(shsurf);
2573 case SHELL_SURFACE_NONE:
2574 weston_surface_set_position(surface,
2575 surface->geometry.x + sx,
2576 surface->geometry.y + sy);
2582 /* surface stacking order, see also activate() */
2583 switch (surface_type) {
2584 case SHELL_SURFACE_POPUP:
2585 case SHELL_SURFACE_TRANSIENT:
2586 parent = shsurf->parent;
2587 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2589 case SHELL_SURFACE_FULLSCREEN:
2590 case SHELL_SURFACE_NONE:
2593 ws = get_current_workspace(shell);
2594 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2598 if (surface_type != SHELL_SURFACE_NONE) {
2599 weston_surface_assign_output(surface);
2600 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2601 surface->output = shsurf->output;
2604 switch (surface_type) {
2605 case SHELL_SURFACE_TRANSIENT:
2606 if (shsurf->transient.flags ==
2607 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
2609 case SHELL_SURFACE_TOPLEVEL:
2610 case SHELL_SURFACE_FULLSCREEN:
2611 case SHELL_SURFACE_MAXIMIZED:
2612 if (!shell->locked) {
2613 wl_list_for_each(seat, &compositor->seat_list, link)
2614 activate(shell, surface, seat);
2621 if (surface_type == SHELL_SURFACE_TOPLEVEL)
2623 switch (shell->win_animation_type) {
2624 case ANIMATION_FADE:
2625 weston_fade_run(surface, NULL, NULL);
2627 case ANIMATION_ZOOM:
2628 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
2637 configure(struct desktop_shell *shell, struct weston_surface *surface,
2638 GLfloat x, GLfloat y, int32_t width, int32_t height)
2640 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
2641 struct shell_surface *shsurf;
2643 shsurf = get_shell_surface(surface);
2645 surface_type = shsurf->type;
2647 surface->geometry.x = x;
2648 surface->geometry.y = y;
2649 surface->geometry.width = width;
2650 surface->geometry.height = height;
2651 surface->geometry.dirty = 1;
2653 switch (surface_type) {
2654 case SHELL_SURFACE_FULLSCREEN:
2655 shell_stack_fullscreen(shsurf);
2656 shell_configure_fullscreen(shsurf);
2658 case SHELL_SURFACE_MAXIMIZED:
2659 /* setting x, y and using configure to change that geometry */
2660 surface->geometry.x = surface->output->x;
2661 surface->geometry.y = surface->output->y +
2662 get_output_panel_height(shell,surface->output);
2664 case SHELL_SURFACE_TOPLEVEL:
2670 /* XXX: would a fullscreen surface need the same handling? */
2671 if (surface->output) {
2672 weston_surface_assign_output(surface);
2674 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2675 surface->output = shsurf->output;
2680 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2682 struct shell_surface *shsurf = get_shell_surface(es);
2683 struct desktop_shell *shell = shsurf->shell;
2684 int type_changed = 0;
2686 if (shsurf->next_type != SHELL_SURFACE_NONE &&
2687 shsurf->type != shsurf->next_type) {
2688 set_surface_type(shsurf);
2692 if (!weston_surface_is_mapped(es)) {
2693 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
2694 } else if (type_changed || sx != 0 || sy != 0 ||
2695 es->geometry.width != es->buffer->width ||
2696 es->geometry.height != es->buffer->height) {
2697 GLfloat from_x, from_y;
2700 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
2701 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
2702 configure(shell, es,
2703 es->geometry.x + to_x - from_x,
2704 es->geometry.y + to_y - from_y,
2705 es->buffer->width, es->buffer->height);
2709 static int launch_desktop_shell_process(struct desktop_shell *shell);
2712 desktop_shell_sigchld(struct weston_process *process, int status)
2715 struct desktop_shell *shell =
2716 container_of(process, struct desktop_shell, child.process);
2718 shell->child.process.pid = 0;
2719 shell->child.client = NULL; /* already destroyed by wayland */
2721 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
2722 time = weston_compositor_get_time();
2723 if (time - shell->child.deathstamp > 30000) {
2724 shell->child.deathstamp = time;
2725 shell->child.deathcount = 0;
2728 shell->child.deathcount++;
2729 if (shell->child.deathcount > 5) {
2730 weston_log("weston-desktop-shell died, giving up.\n");
2734 weston_log("weston-desktop-shell died, respawning...\n");
2735 launch_desktop_shell_process(shell);
2739 launch_desktop_shell_process(struct desktop_shell *shell)
2741 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
2743 shell->child.client = weston_client_launch(shell->compositor,
2744 &shell->child.process,
2746 desktop_shell_sigchld);
2748 if (!shell->child.client)
2754 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2756 struct desktop_shell *shell = data;
2758 wl_client_add_object(client, &wl_shell_interface,
2759 &shell_implementation, id, shell);
2763 unbind_desktop_shell(struct wl_resource *resource)
2765 struct desktop_shell *shell = resource->data;
2768 resume_desktop(shell);
2770 shell->child.desktop_shell = NULL;
2771 shell->prepare_event_sent = false;
2776 bind_desktop_shell(struct wl_client *client,
2777 void *data, uint32_t version, uint32_t id)
2779 struct desktop_shell *shell = data;
2780 struct wl_resource *resource;
2782 resource = wl_client_add_object(client, &desktop_shell_interface,
2783 &desktop_shell_implementation,
2786 if (client == shell->child.client) {
2787 resource->destroy = unbind_desktop_shell;
2788 shell->child.desktop_shell = resource;
2792 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2793 "permission to bind desktop_shell denied");
2794 wl_resource_destroy(resource);
2798 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2800 struct desktop_shell *shell = surface->private;
2805 center_on_output(surface, surface->output);
2807 if (wl_list_empty(&surface->layer_link)) {
2808 wl_list_insert(shell->lock_layer.surface_list.prev,
2809 &surface->layer_link);
2810 weston_surface_assign_output(surface);
2811 shell->compositor->idle_time = shell->screensaver.duration;
2812 weston_compositor_wake(shell->compositor);
2813 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
2818 screensaver_set_surface(struct wl_client *client,
2819 struct wl_resource *resource,
2820 struct wl_resource *surface_resource,
2821 struct wl_resource *output_resource)
2823 struct desktop_shell *shell = resource->data;
2824 struct weston_surface *surface = surface_resource->data;
2825 struct weston_output *output = output_resource->data;
2827 surface->configure = screensaver_configure;
2828 surface->private = shell;
2829 surface->output = output;
2832 static const struct screensaver_interface screensaver_implementation = {
2833 screensaver_set_surface
2837 unbind_screensaver(struct wl_resource *resource)
2839 struct desktop_shell *shell = resource->data;
2841 shell->screensaver.binding = NULL;
2846 bind_screensaver(struct wl_client *client,
2847 void *data, uint32_t version, uint32_t id)
2849 struct desktop_shell *shell = data;
2850 struct wl_resource *resource;
2852 resource = wl_client_add_object(client, &screensaver_interface,
2853 &screensaver_implementation,
2856 if (shell->screensaver.binding == NULL) {
2857 resource->destroy = unbind_screensaver;
2858 shell->screensaver.binding = resource;
2862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2863 "interface object already bound");
2864 wl_resource_destroy(resource);
2868 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2870 struct weston_mode *mode = surface->output->current;
2871 GLfloat x = (mode->width - surface->buffer->width) / 2;
2872 GLfloat y = mode->height - surface->buffer->height;
2874 /* Don't map the input panel here, wait for
2875 * show_input_panels signal. */
2877 weston_surface_configure(surface,
2878 surface->output->x + x,
2879 surface->output->y + y,
2880 surface->buffer->width,
2881 surface->buffer->height);
2885 input_panel_set_surface(struct wl_client *client,
2886 struct wl_resource *resource,
2887 struct wl_resource *surface_resource,
2888 struct wl_resource *output_resource)
2890 struct desktop_shell *shell = resource->data;
2891 struct weston_surface *surface = surface_resource->data;
2892 struct weston_output *output = output_resource->data;
2894 surface->configure = input_panel_configure;
2895 surface->private = shell;
2896 surface->output = output;
2897 wl_list_insert(shell->input_panel.surfaces.prev, &surface->layer_link);
2900 static const struct input_panel_interface input_panel_implementation = {
2901 input_panel_set_surface
2905 unbind_input_panel(struct wl_resource *resource)
2907 struct desktop_shell *shell = resource->data;
2909 shell->input_panel.binding = NULL;
2914 bind_input_panel(struct wl_client *client,
2915 void *data, uint32_t version, uint32_t id)
2917 struct desktop_shell *shell = data;
2918 struct wl_resource *resource;
2920 resource = wl_client_add_object(client, &input_panel_interface,
2921 &input_panel_implementation,
2924 if (shell->input_panel.binding == NULL) {
2925 resource->destroy = unbind_input_panel;
2926 shell->input_panel.binding = resource;
2930 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
2931 "interface object already bound");
2932 wl_resource_destroy(resource);
2936 struct desktop_shell *shell;
2937 struct weston_surface *current;
2938 struct wl_listener listener;
2939 struct wl_keyboard_grab grab;
2943 switcher_next(struct switcher *switcher)
2945 struct weston_surface *surface;
2946 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
2947 struct shell_surface *shsurf;
2948 struct workspace *ws = get_current_workspace(switcher->shell);
2950 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
2951 switch (get_shell_surface_type(surface)) {
2952 case SHELL_SURFACE_TOPLEVEL:
2953 case SHELL_SURFACE_FULLSCREEN:
2954 case SHELL_SURFACE_MAXIMIZED:
2957 if (prev == switcher->current)
2960 surface->alpha = 0.25;
2961 surface->geometry.dirty = 1;
2962 weston_surface_damage(surface);
2968 if (is_black_surface(surface, NULL)) {
2969 surface->alpha = 0.25;
2970 surface->geometry.dirty = 1;
2971 weston_surface_damage(surface);
2981 wl_list_remove(&switcher->listener.link);
2982 wl_signal_add(&next->surface.resource.destroy_signal,
2983 &switcher->listener);
2985 switcher->current = next;
2988 shsurf = get_shell_surface(switcher->current);
2989 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
2990 shsurf->fullscreen.black_surface->alpha = 1.0;
2994 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
2996 struct switcher *switcher =
2997 container_of(listener, struct switcher, listener);
2999 switcher_next(switcher);
3003 switcher_destroy(struct switcher *switcher)
3005 struct weston_surface *surface;
3006 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3007 struct workspace *ws = get_current_workspace(switcher->shell);
3009 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3010 surface->alpha = 1.0;
3011 weston_surface_damage(surface);
3014 if (switcher->current)
3015 activate(switcher->shell, switcher->current,
3016 (struct weston_seat *) keyboard->seat);
3017 wl_list_remove(&switcher->listener.link);
3018 wl_keyboard_end_grab(keyboard);
3023 switcher_key(struct wl_keyboard_grab *grab,
3024 uint32_t time, uint32_t key, uint32_t state_w)
3026 struct switcher *switcher = container_of(grab, struct switcher, grab);
3027 enum wl_keyboard_key_state state = state_w;
3029 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3030 switcher_next(switcher);
3034 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3035 uint32_t mods_depressed, uint32_t mods_latched,
3036 uint32_t mods_locked, uint32_t group)
3038 struct switcher *switcher = container_of(grab, struct switcher, grab);
3039 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3041 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3042 switcher_destroy(switcher);
3045 static const struct wl_keyboard_grab_interface switcher_grab = {
3051 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3054 struct desktop_shell *shell = data;
3055 struct switcher *switcher;
3057 switcher = malloc(sizeof *switcher);
3058 switcher->shell = shell;
3059 switcher->current = NULL;
3060 switcher->listener.notify = switcher_handle_surface_destroy;
3061 wl_list_init(&switcher->listener.link);
3063 lower_fullscreen_layer(switcher->shell);
3064 switcher->grab.interface = &switcher_grab;
3065 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3066 wl_keyboard_set_focus(seat->keyboard, NULL);
3067 switcher_next(switcher);
3071 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3074 struct weston_compositor *compositor = data;
3075 struct weston_output *output;
3076 long backlight_new = 0;
3078 /* TODO: we're limiting to simple use cases, where we assume just
3079 * control on the primary display. We'd have to extend later if we
3080 * ever get support for setting backlights on random desktop LCD
3082 output = get_default_output(compositor);
3086 if (!output->set_backlight)
3089 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3090 backlight_new = output->backlight_current - 25;
3091 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3092 backlight_new = output->backlight_current + 25;
3094 if (backlight_new < 5)
3096 if (backlight_new > 255)
3097 backlight_new = 255;
3099 output->backlight_current = backlight_new;
3100 output->set_backlight(output, output->backlight_current);
3104 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3107 struct desktop_shell *shell = data;
3108 struct weston_compositor *compositor = shell->compositor;
3109 struct weston_surface *surface;
3111 if (shell->debug_repaint_surface) {
3112 weston_surface_destroy(shell->debug_repaint_surface);
3113 shell->debug_repaint_surface = NULL;
3115 surface = weston_surface_create(compositor);
3116 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3117 weston_surface_configure(surface, 0, 0, 8192, 8192);
3118 wl_list_insert(&compositor->fade_layer.surface_list,
3119 &surface->layer_link);
3120 weston_surface_assign_output(surface);
3121 pixman_region32_init(&surface->input);
3123 /* Here's the dirty little trick that makes the
3124 * repaint debugging work: we force an
3125 * update_transform first to update dependent state
3126 * and clear the geometry.dirty bit. Then we clear
3127 * the surface damage so it only gets repainted
3128 * piecewise as we repaint other things. */
3130 weston_surface_update_transform(surface);
3131 pixman_region32_fini(&surface->damage);
3132 pixman_region32_init(&surface->damage);
3133 shell->debug_repaint_surface = surface;
3138 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3141 struct wl_client *client;
3146 client = seat->keyboard->focus->resource.client;
3147 wl_client_get_credentials(client, &pid, &uid, &gid);
3153 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3154 uint32_t key, void *data)
3156 struct desktop_shell *shell = data;
3157 unsigned int new_index = shell->workspaces.current;
3164 change_workspace(shell, new_index);
3168 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3169 uint32_t key, void *data)
3171 struct desktop_shell *shell = data;
3172 unsigned int new_index = shell->workspaces.current;
3176 if (new_index < shell->workspaces.num - 1)
3179 change_workspace(shell, new_index);
3183 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3184 uint32_t key, void *data)
3186 struct desktop_shell *shell = data;
3187 unsigned int new_index;
3191 new_index = key - KEY_F1;
3192 if (new_index >= shell->workspaces.num)
3193 new_index = shell->workspaces.num - 1;
3195 change_workspace(shell, new_index);
3200 shell_destroy(struct wl_listener *listener, void *data)
3202 struct desktop_shell *shell =
3203 container_of(listener, struct desktop_shell, destroy_listener);
3204 struct workspace **ws;
3206 if (shell->child.client)
3207 wl_client_destroy(shell->child.client);
3209 wl_list_remove(&shell->lock_listener.link);
3210 wl_list_remove(&shell->unlock_listener.link);
3211 wl_list_remove(&shell->show_input_panel_listener.link);
3212 wl_list_remove(&shell->hide_input_panel_listener.link);
3214 wl_array_for_each(ws, &shell->workspaces.array)
3215 workspace_destroy(*ws);
3216 wl_array_release(&shell->workspaces.array);
3218 free(shell->screensaver.path);
3223 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3226 int i, num_workspace_bindings;
3228 /* fixed bindings */
3229 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3230 MODIFIER_CTRL | MODIFIER_ALT,
3231 terminate_binding, ec);
3232 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3233 click_to_activate_binding,
3235 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3236 MODIFIER_SUPER | MODIFIER_ALT,
3237 surface_opacity_binding, NULL);
3238 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3239 MODIFIER_SUPER, zoom_axis_binding,
3242 /* configurable bindings */
3243 mod = shell->binding_modifier;
3244 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3245 zoom_key_binding, NULL);
3246 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3247 zoom_key_binding, NULL);
3248 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3250 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3251 resize_binding, shell);
3252 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3253 rotate_binding, NULL);
3254 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3256 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3258 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3259 backlight_binding, ec);
3260 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3262 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3263 backlight_binding, ec);
3264 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3265 debug_repaint_binding, shell);
3266 weston_compositor_add_key_binding(ec, KEY_K, mod,
3267 force_kill_binding, shell);
3268 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3269 workspace_up_binding, shell);
3270 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3271 workspace_down_binding, shell);
3273 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3274 if (shell->workspaces.num > 1) {
3275 num_workspace_bindings = shell->workspaces.num;
3276 if (num_workspace_bindings > 6)
3277 num_workspace_bindings = 6;
3278 for (i = 0; i < num_workspace_bindings; i++)
3279 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3280 workspace_f_binding,
3286 shell_init(struct weston_compositor *ec);
3289 shell_init(struct weston_compositor *ec)
3291 struct desktop_shell *shell;
3292 struct workspace **pws;
3295 shell = malloc(sizeof *shell);
3299 memset(shell, 0, sizeof *shell);
3300 shell->compositor = ec;
3302 shell->destroy_listener.notify = shell_destroy;
3303 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3304 shell->lock_listener.notify = lock;
3305 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3306 shell->unlock_listener.notify = unlock;
3307 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3308 shell->show_input_panel_listener.notify = show_input_panels;
3309 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3310 shell->hide_input_panel_listener.notify = hide_input_panels;
3311 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3312 ec->ping_handler = ping_handler;
3313 ec->shell_interface.shell = shell;
3314 ec->shell_interface.create_shell_surface = create_shell_surface;
3315 ec->shell_interface.set_toplevel = set_toplevel;
3316 ec->shell_interface.set_transient = set_transient;
3317 ec->shell_interface.move = surface_move;
3318 ec->shell_interface.resize = surface_resize;
3320 wl_list_init(&shell->screensaver.surfaces);
3321 wl_list_init(&shell->input_panel.surfaces);
3323 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3324 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3325 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3326 weston_layer_init(&shell->lock_layer, NULL);
3328 wl_array_init(&shell->workspaces.array);
3330 shell_configuration(shell);
3332 for (i = 0; i < shell->workspaces.num; i++) {
3333 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3337 *pws = workspace_create();
3341 activate_workspace(shell, 0);
3343 wl_list_init(&shell->workspaces.animation.link);
3344 shell->workspaces.animation.frame = animate_workspace_change_frame;
3346 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3347 shell, bind_shell) == NULL)
3350 if (wl_display_add_global(ec->wl_display,
3351 &desktop_shell_interface,
3352 shell, bind_desktop_shell) == NULL)
3355 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3356 shell, bind_screensaver) == NULL)
3359 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3360 shell, bind_input_panel) == NULL)
3363 shell->child.deathstamp = weston_compositor_get_time();
3364 if (launch_desktop_shell_process(shell) != 0)
3367 shell->pointer_focus_listener.notify = handle_pointer_focus;
3368 if (ec->seat->seat.pointer)
3369 wl_signal_add(&ec->seat->seat.pointer->focus_signal,
3370 &shell->pointer_focus_listener);
3372 shell_add_bindings(ec, shell);