2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <linux/input.h>
33 #include <sys/types.h>
35 #include <wayland-server.h>
36 #include "compositor.h"
37 #include "desktop-shell-server-protocol.h"
38 #include "workspaces-server-protocol.h"
39 #include "../shared/config-parser.h"
41 #define DEFAULT_NUM_WORKSPACES 1
42 #define DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH 200
52 struct weston_seat *seat;
54 struct weston_surface *keyboard_focus;
56 struct wl_listener seat_destroy_listener;
57 struct wl_listener surface_destroy_listener;
61 struct weston_layer layer;
63 struct wl_list focus_list;
64 struct wl_listener seat_destroyed_listener;
67 struct input_panel_surface {
69 struct weston_surface *surface;
70 struct wl_listener listener;
73 struct desktop_shell {
74 struct weston_compositor *compositor;
76 struct wl_listener lock_listener;
77 struct wl_listener unlock_listener;
78 struct wl_listener destroy_listener;
79 struct wl_listener show_input_panel_listener;
80 struct wl_listener hide_input_panel_listener;
82 struct weston_layer fullscreen_layer;
83 struct weston_layer panel_layer;
84 struct weston_layer background_layer;
85 struct weston_layer lock_layer;
86 struct weston_layer input_panel_layer;
88 struct wl_listener pointer_focus_listener;
89 struct weston_surface *grab_surface;
92 struct weston_process process;
93 struct wl_client *client;
94 struct wl_resource *desktop_shell;
101 bool showing_input_panels;
102 bool prepare_event_sent;
104 struct weston_surface *lock_surface;
105 struct wl_listener lock_surface_listener;
108 struct wl_array array;
109 unsigned int current;
112 struct wl_list client_list;
114 struct weston_animation animation;
115 struct wl_list anim_sticky_list;
117 uint32_t anim_timestamp;
119 struct workspace *anim_from;
120 struct workspace *anim_to;
126 struct wl_resource *binding;
127 struct wl_list surfaces;
128 struct weston_process process;
132 struct wl_resource *binding;
133 struct wl_list surfaces;
136 uint32_t binding_modifier;
137 enum animation_type win_animation_type;
138 struct weston_surface *debug_repaint_surface;
141 enum shell_surface_type {
143 SHELL_SURFACE_TOPLEVEL,
144 SHELL_SURFACE_TRANSIENT,
145 SHELL_SURFACE_FULLSCREEN,
146 SHELL_SURFACE_MAXIMIZED,
151 struct wl_event_source *source;
155 struct shell_surface {
156 struct wl_resource resource;
158 struct weston_surface *surface;
159 struct wl_listener surface_destroy_listener;
160 struct weston_surface *parent;
161 struct desktop_shell *shell;
163 enum shell_surface_type type, next_type;
165 int32_t saved_x, saved_y;
166 bool saved_position_valid;
167 bool saved_rotation_valid;
171 struct weston_transform transform;
172 struct weston_matrix rotation;
176 struct wl_pointer_grab grab;
178 struct weston_transform parent_transform;
180 struct wl_seat *seat;
190 enum wl_shell_surface_fullscreen_method type;
191 struct weston_transform transform; /* matrix from x, y */
193 struct weston_surface *black_surface;
196 struct ping_timer *ping_timer;
198 struct weston_transform workspace_transform;
200 struct weston_output *fullscreen_output;
201 struct weston_output *output;
204 const struct weston_shell_client *client;
208 struct wl_pointer_grab grab;
209 struct shell_surface *shsurf;
210 struct wl_listener shsurf_destroy_listener;
211 struct wl_pointer *pointer;
214 struct weston_move_grab {
215 struct shell_grab base;
220 struct shell_grab base;
221 struct weston_matrix rotation;
229 activate(struct desktop_shell *shell, struct weston_surface *es,
230 struct weston_seat *seat);
232 static struct workspace *
233 get_current_workspace(struct desktop_shell *shell);
235 static struct shell_surface *
236 get_shell_surface(struct weston_surface *surface);
238 static struct desktop_shell *
239 shell_surface_get_shell(struct shell_surface *shsurf);
242 surface_rotate(struct shell_surface *surface, struct wl_seat *seat);
245 shell_surface_is_top_fullscreen(struct shell_surface *shsurf)
247 struct desktop_shell *shell;
248 struct weston_surface *top_fs_es;
250 shell = shell_surface_get_shell(shsurf);
252 if (wl_list_empty(&shell->fullscreen_layer.surface_list))
255 top_fs_es = container_of(shell->fullscreen_layer.surface_list.next,
256 struct weston_surface,
258 return (shsurf == get_shell_surface(top_fs_es));
262 destroy_shell_grab_shsurf(struct wl_listener *listener, void *data)
264 struct shell_grab *grab;
266 grab = container_of(listener, struct shell_grab,
267 shsurf_destroy_listener);
273 popup_grab_end(struct wl_pointer *pointer);
276 shell_grab_start(struct shell_grab *grab,
277 const struct wl_pointer_grab_interface *interface,
278 struct shell_surface *shsurf,
279 struct wl_pointer *pointer,
280 enum desktop_shell_cursor cursor)
282 struct desktop_shell *shell = shsurf->shell;
284 popup_grab_end(pointer);
286 grab->grab.interface = interface;
287 grab->shsurf = shsurf;
288 grab->shsurf_destroy_listener.notify = destroy_shell_grab_shsurf;
289 wl_signal_add(&shsurf->resource.destroy_signal,
290 &grab->shsurf_destroy_listener);
292 grab->pointer = pointer;
293 grab->grab.focus = &shsurf->surface->surface;
295 wl_pointer_start_grab(pointer, &grab->grab);
296 desktop_shell_send_grab_cursor(shell->child.desktop_shell, cursor);
297 wl_pointer_set_focus(pointer, &shell->grab_surface->surface,
298 wl_fixed_from_int(0), wl_fixed_from_int(0));
302 shell_grab_end(struct shell_grab *grab)
305 wl_list_remove(&grab->shsurf_destroy_listener.link);
307 wl_pointer_end_grab(grab->pointer);
311 center_on_output(struct weston_surface *surface,
312 struct weston_output *output);
314 static enum weston_keyboard_modifier
315 get_modifier(char *modifier)
318 return MODIFIER_SUPER;
320 if (!strcmp("ctrl", modifier))
321 return MODIFIER_CTRL;
322 else if (!strcmp("alt", modifier))
324 else if (!strcmp("super", modifier))
325 return MODIFIER_SUPER;
327 return MODIFIER_SUPER;
330 static enum animation_type
331 get_animation_type(char *animation)
334 return ANIMATION_NONE;
336 if (!strcmp("zoom", animation))
337 return ANIMATION_ZOOM;
338 else if (!strcmp("fade", animation))
339 return ANIMATION_FADE;
341 return ANIMATION_NONE;
345 shell_configuration(struct desktop_shell *shell)
350 unsigned int num_workspaces = DEFAULT_NUM_WORKSPACES;
351 char *modifier = NULL;
352 char *win_animation = NULL;
354 struct config_key shell_keys[] = {
355 { "binding-modifier", CONFIG_KEY_STRING, &modifier },
356 { "animation", CONFIG_KEY_STRING, &win_animation},
358 CONFIG_KEY_UNSIGNED_INTEGER, &num_workspaces },
361 struct config_key saver_keys[] = {
362 { "path", CONFIG_KEY_STRING, &path },
363 { "duration", CONFIG_KEY_INTEGER, &duration },
366 struct config_section cs[] = {
367 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
368 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
371 config_file = config_file_path("weston.ini");
372 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
375 shell->screensaver.path = path;
376 shell->screensaver.duration = duration;
377 shell->binding_modifier = get_modifier(modifier);
378 shell->win_animation_type = get_animation_type(win_animation);
379 shell->workspaces.num = num_workspaces > 0 ? num_workspaces : 1;
383 focus_state_destroy(struct focus_state *state)
385 wl_list_remove(&state->seat_destroy_listener.link);
386 wl_list_remove(&state->surface_destroy_listener.link);
391 focus_state_seat_destroy(struct wl_listener *listener, void *data)
393 struct focus_state *state = container_of(listener,
395 seat_destroy_listener);
397 wl_list_remove(&state->link);
398 focus_state_destroy(state);
402 focus_state_surface_destroy(struct wl_listener *listener, void *data)
404 struct focus_state *state = container_of(listener,
406 surface_destroy_listener);
407 struct desktop_shell *shell;
408 struct weston_surface *surface, *next;
411 wl_list_for_each(surface, &state->ws->layer.surface_list, layer_link) {
412 if (surface == state->keyboard_focus)
420 shell = state->seat->compositor->shell_interface.shell;
421 activate(shell, next, state->seat);
423 wl_list_remove(&state->link);
424 focus_state_destroy(state);
428 static struct focus_state *
429 focus_state_create(struct weston_seat *seat, struct workspace *ws)
431 struct focus_state *state;
433 state = malloc(sizeof *state);
439 wl_list_insert(&ws->focus_list, &state->link);
441 state->seat_destroy_listener.notify = focus_state_seat_destroy;
442 state->surface_destroy_listener.notify = focus_state_surface_destroy;
443 wl_signal_add(&seat->seat.destroy_signal,
444 &state->seat_destroy_listener);
445 wl_list_init(&state->surface_destroy_listener.link);
450 static struct focus_state *
451 ensure_focus_state(struct desktop_shell *shell, struct weston_seat *seat)
453 struct workspace *ws = get_current_workspace(shell);
454 struct focus_state *state;
456 wl_list_for_each(state, &ws->focus_list, link)
457 if (state->seat == seat)
460 if (&state->link == &ws->focus_list)
461 state = focus_state_create(seat, ws);
467 restore_focus_state(struct desktop_shell *shell, struct workspace *ws)
469 struct focus_state *state, *next;
470 struct wl_surface *surface;
472 wl_list_for_each_safe(state, next, &ws->focus_list, link) {
473 surface = state->keyboard_focus ?
474 &state->keyboard_focus->surface : NULL;
476 wl_keyboard_set_focus(state->seat->seat.keyboard, surface);
481 replace_focus_state(struct desktop_shell *shell, struct workspace *ws,
482 struct weston_seat *seat)
484 struct focus_state *state;
485 struct wl_surface *surface;
487 wl_list_for_each(state, &ws->focus_list, link) {
488 if (state->seat == seat) {
489 surface = seat->seat.keyboard->focus;
490 state->keyboard_focus =
491 (struct weston_surface *) surface;
498 drop_focus_state(struct desktop_shell *shell, struct workspace *ws,
499 struct weston_surface *surface)
501 struct focus_state *state;
503 wl_list_for_each(state, &ws->focus_list, link)
504 if (state->keyboard_focus == surface)
505 state->keyboard_focus = NULL;
509 workspace_destroy(struct workspace *ws)
511 struct focus_state *state, *next;
513 wl_list_for_each_safe(state, next, &ws->focus_list, link)
514 focus_state_destroy(state);
520 seat_destroyed(struct wl_listener *listener, void *data)
522 struct weston_seat *seat = data;
523 struct focus_state *state, *next;
524 struct workspace *ws = container_of(listener,
526 seat_destroyed_listener);
528 wl_list_for_each_safe(state, next, &ws->focus_list, link)
529 if (state->seat == seat)
530 wl_list_remove(&state->link);
533 static struct workspace *
534 workspace_create(void)
536 struct workspace *ws = malloc(sizeof *ws);
540 weston_layer_init(&ws->layer, NULL);
542 wl_list_init(&ws->focus_list);
543 wl_list_init(&ws->seat_destroyed_listener.link);
544 ws->seat_destroyed_listener.notify = seat_destroyed;
550 workspace_is_empty(struct workspace *ws)
552 return wl_list_empty(&ws->layer.surface_list);
555 static struct workspace *
556 get_workspace(struct desktop_shell *shell, unsigned int index)
558 struct workspace **pws = shell->workspaces.array.data;
559 assert(index < shell->workspaces.num);
564 static struct workspace *
565 get_current_workspace(struct desktop_shell *shell)
567 return get_workspace(shell, shell->workspaces.current);
571 activate_workspace(struct desktop_shell *shell, unsigned int index)
573 struct workspace *ws;
575 ws = get_workspace(shell, index);
576 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
578 shell->workspaces.current = index;
582 get_output_height(struct weston_output *output)
584 return abs(output->region.extents.y1 - output->region.extents.y2);
588 surface_translate(struct weston_surface *surface, double d)
590 struct shell_surface *shsurf = get_shell_surface(surface);
591 struct weston_transform *transform;
593 transform = &shsurf->workspace_transform;
594 if (wl_list_empty(&transform->link))
595 wl_list_insert(surface->geometry.transformation_list.prev,
596 &shsurf->workspace_transform.link);
598 weston_matrix_init(&shsurf->workspace_transform.matrix);
599 weston_matrix_translate(&shsurf->workspace_transform.matrix,
601 surface->geometry.dirty = 1;
605 workspace_translate_out(struct workspace *ws, double fraction)
607 struct weston_surface *surface;
611 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
612 height = get_output_height(surface->output);
613 d = height * fraction;
615 surface_translate(surface, d);
620 workspace_translate_in(struct workspace *ws, double fraction)
622 struct weston_surface *surface;
626 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
627 height = get_output_height(surface->output);
630 d = -(height - height * fraction);
632 d = height + height * fraction;
634 surface_translate(surface, d);
639 broadcast_current_workspace_state(struct desktop_shell *shell)
641 struct wl_resource *resource;
643 wl_list_for_each(resource, &shell->workspaces.client_list, link)
644 workspace_manager_send_state(resource,
645 shell->workspaces.current,
646 shell->workspaces.num);
650 reverse_workspace_change_animation(struct desktop_shell *shell,
652 struct workspace *from,
653 struct workspace *to)
655 shell->workspaces.current = index;
657 shell->workspaces.anim_to = to;
658 shell->workspaces.anim_from = from;
659 shell->workspaces.anim_dir = -1 * shell->workspaces.anim_dir;
660 shell->workspaces.anim_timestamp = 0;
662 weston_compositor_schedule_repaint(shell->compositor);
666 workspace_deactivate_transforms(struct workspace *ws)
668 struct weston_surface *surface;
669 struct shell_surface *shsurf;
671 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
672 shsurf = get_shell_surface(surface);
673 if (!wl_list_empty(&shsurf->workspace_transform.link)) {
674 wl_list_remove(&shsurf->workspace_transform.link);
675 wl_list_init(&shsurf->workspace_transform.link);
677 shsurf->surface->geometry.dirty = 1;
682 finish_workspace_change_animation(struct desktop_shell *shell,
683 struct workspace *from,
684 struct workspace *to)
686 weston_compositor_schedule_repaint(shell->compositor);
688 wl_list_remove(&shell->workspaces.animation.link);
689 workspace_deactivate_transforms(from);
690 workspace_deactivate_transforms(to);
691 shell->workspaces.anim_to = NULL;
693 wl_list_remove(&shell->workspaces.anim_from->layer.link);
697 animate_workspace_change_frame(struct weston_animation *animation,
698 struct weston_output *output, uint32_t msecs)
700 struct desktop_shell *shell =
701 container_of(animation, struct desktop_shell,
702 workspaces.animation);
703 struct workspace *from = shell->workspaces.anim_from;
704 struct workspace *to = shell->workspaces.anim_to;
708 if (workspace_is_empty(from) && workspace_is_empty(to)) {
709 finish_workspace_change_animation(shell, from, to);
713 if (shell->workspaces.anim_timestamp == 0) {
714 if (shell->workspaces.anim_current == 0.0)
715 shell->workspaces.anim_timestamp = msecs;
717 shell->workspaces.anim_timestamp =
719 /* Invers of movement function 'y' below. */
720 (asin(1.0 - shell->workspaces.anim_current) *
721 DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH *
725 t = msecs - shell->workspaces.anim_timestamp;
731 x = t * (1.0/DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) * M_PI_2;
734 if (t < DEFAULT_WORKSPACE_CHANGE_ANIMATION_LENGTH) {
735 weston_compositor_schedule_repaint(shell->compositor);
737 workspace_translate_out(from, shell->workspaces.anim_dir * y);
738 workspace_translate_in(to, shell->workspaces.anim_dir * y);
739 shell->workspaces.anim_current = y;
741 weston_compositor_schedule_repaint(shell->compositor);
744 finish_workspace_change_animation(shell, from, to);
748 animate_workspace_change(struct desktop_shell *shell,
750 struct workspace *from,
751 struct workspace *to)
753 struct weston_output *output;
757 if (index > shell->workspaces.current)
762 shell->workspaces.current = index;
764 shell->workspaces.anim_dir = dir;
765 shell->workspaces.anim_from = from;
766 shell->workspaces.anim_to = to;
767 shell->workspaces.anim_current = 0.0;
768 shell->workspaces.anim_timestamp = 0;
770 output = container_of(shell->compositor->output_list.next,
771 struct weston_output, link);
772 wl_list_insert(&output->animation_list,
773 &shell->workspaces.animation.link);
775 wl_list_insert(from->layer.link.prev, &to->layer.link);
777 workspace_translate_in(to, 0);
779 restore_focus_state(shell, to);
781 weston_compositor_schedule_repaint(shell->compositor);
785 update_workspace(struct desktop_shell *shell, unsigned int index,
786 struct workspace *from, struct workspace *to)
788 shell->workspaces.current = index;
789 wl_list_insert(&from->layer.link, &to->layer.link);
790 wl_list_remove(&from->layer.link);
794 change_workspace(struct desktop_shell *shell, unsigned int index)
796 struct workspace *from;
797 struct workspace *to;
799 if (index == shell->workspaces.current)
802 /* Don't change workspace when there is any fullscreen surfaces. */
803 if (!wl_list_empty(&shell->fullscreen_layer.surface_list))
806 from = get_current_workspace(shell);
807 to = get_workspace(shell, index);
809 if (shell->workspaces.anim_from == to &&
810 shell->workspaces.anim_to == from) {
811 restore_focus_state(shell, to);
812 reverse_workspace_change_animation(shell, index, from, to);
813 broadcast_current_workspace_state(shell);
817 if (shell->workspaces.anim_to != NULL)
818 finish_workspace_change_animation(shell,
819 shell->workspaces.anim_from,
820 shell->workspaces.anim_to);
822 restore_focus_state(shell, to);
824 if (workspace_is_empty(to) && workspace_is_empty(from))
825 update_workspace(shell, index, from, to);
827 animate_workspace_change(shell, index, from, to);
829 broadcast_current_workspace_state(shell);
833 workspace_has_only(struct workspace *ws, struct weston_surface *surface)
835 struct wl_list *list = &ws->layer.surface_list;
838 if (wl_list_empty(list))
846 return container_of(e, struct weston_surface, layer_link) == surface;
850 move_surface_to_workspace(struct desktop_shell *shell,
851 struct weston_surface *surface,
854 struct workspace *from;
855 struct workspace *to;
856 struct weston_seat *seat;
858 if (workspace == shell->workspaces.current)
861 if (workspace >= shell->workspaces.num)
862 workspace = shell->workspaces.num - 1;
864 from = get_current_workspace(shell);
865 to = get_workspace(shell, workspace);
867 wl_list_remove(&surface->layer_link);
868 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
870 drop_focus_state(shell, from, surface);
871 wl_list_for_each(seat, &shell->compositor->seat_list, link)
872 if (seat->has_keyboard &&
873 seat->keyboard.focus == &surface->surface)
874 wl_keyboard_set_focus(&seat->keyboard, NULL);
876 weston_surface_damage_below(surface);
880 take_surface_to_workspace_by_seat(struct desktop_shell *shell,
881 struct wl_seat *wl_seat,
884 struct weston_seat *seat = (struct weston_seat *) wl_seat;
885 struct weston_surface *surface =
886 (struct weston_surface *) wl_seat->keyboard->focus;
887 struct shell_surface *shsurf;
888 struct workspace *from;
889 struct workspace *to;
890 struct focus_state *state;
892 if (surface == NULL ||
893 index == shell->workspaces.current)
896 from = get_current_workspace(shell);
897 to = get_workspace(shell, index);
899 wl_list_remove(&surface->layer_link);
900 wl_list_insert(&to->layer.surface_list, &surface->layer_link);
902 replace_focus_state(shell, to, seat);
903 drop_focus_state(shell, from, surface);
905 if (shell->workspaces.anim_from == to &&
906 shell->workspaces.anim_to == from) {
907 wl_list_remove(&to->layer.link);
908 wl_list_insert(from->layer.link.prev, &to->layer.link);
910 reverse_workspace_change_animation(shell, index, from, to);
911 broadcast_current_workspace_state(shell);
916 if (shell->workspaces.anim_to != NULL)
917 finish_workspace_change_animation(shell,
918 shell->workspaces.anim_from,
919 shell->workspaces.anim_to);
921 if (workspace_is_empty(from) &&
922 workspace_has_only(to, surface))
923 update_workspace(shell, index, from, to);
925 shsurf = get_shell_surface(surface);
926 if (wl_list_empty(&shsurf->workspace_transform.link))
927 wl_list_insert(&shell->workspaces.anim_sticky_list,
928 &shsurf->workspace_transform.link);
930 animate_workspace_change(shell, index, from, to);
933 broadcast_current_workspace_state(shell);
935 state = ensure_focus_state(shell, seat);
937 state->keyboard_focus = surface;
941 workspace_manager_move_surface(struct wl_client *client,
942 struct wl_resource *resource,
943 struct wl_resource *surface_resource,
946 struct desktop_shell *shell = resource->data;
947 struct weston_surface *surface =
948 (struct weston_surface *) surface_resource;
950 move_surface_to_workspace(shell, surface, workspace);
953 static const struct workspace_manager_interface workspace_manager_implementation = {
954 workspace_manager_move_surface,
958 unbind_resource(struct wl_resource *resource)
960 wl_list_remove(&resource->link);
965 bind_workspace_manager(struct wl_client *client,
966 void *data, uint32_t version, uint32_t id)
968 struct desktop_shell *shell = data;
969 struct wl_resource *resource;
971 resource = wl_client_add_object(client, &workspace_manager_interface,
972 &workspace_manager_implementation,
975 if (resource == NULL) {
976 weston_log("couldn't add workspace manager object");
980 resource->destroy = unbind_resource;
981 wl_list_insert(&shell->workspaces.client_list, &resource->link);
983 workspace_manager_send_state(resource,
984 shell->workspaces.current,
985 shell->workspaces.num);
989 noop_grab_focus(struct wl_pointer_grab *grab,
990 struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
996 move_grab_motion(struct wl_pointer_grab *grab,
997 uint32_t time, wl_fixed_t x, wl_fixed_t y)
999 struct weston_move_grab *move = (struct weston_move_grab *) grab;
1000 struct wl_pointer *pointer = grab->pointer;
1001 struct shell_surface *shsurf = move->base.shsurf;
1002 struct weston_surface *es;
1003 int dx = wl_fixed_to_int(pointer->x + move->dx);
1004 int dy = wl_fixed_to_int(pointer->y + move->dy);
1009 es = shsurf->surface;
1011 weston_surface_configure(es, dx, dy,
1012 es->geometry.width, es->geometry.height);
1014 weston_compositor_schedule_repaint(es->compositor);
1018 move_grab_button(struct wl_pointer_grab *grab,
1019 uint32_t time, uint32_t button, uint32_t state_w)
1021 struct shell_grab *shell_grab = container_of(grab, struct shell_grab,
1023 struct wl_pointer *pointer = grab->pointer;
1024 enum wl_pointer_button_state state = state_w;
1026 if (pointer->button_count == 0 &&
1027 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1028 shell_grab_end(shell_grab);
1033 static const struct wl_pointer_grab_interface move_grab_interface = {
1040 surface_move(struct shell_surface *shsurf, struct weston_seat *ws)
1042 struct weston_move_grab *move;
1047 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1050 move = malloc(sizeof *move);
1054 move->dx = wl_fixed_from_double(shsurf->surface->geometry.x) -
1055 ws->seat.pointer->grab_x;
1056 move->dy = wl_fixed_from_double(shsurf->surface->geometry.y) -
1057 ws->seat.pointer->grab_y;
1059 shell_grab_start(&move->base, &move_grab_interface, shsurf,
1060 ws->seat.pointer, DESKTOP_SHELL_CURSOR_MOVE);
1066 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
1067 struct wl_resource *seat_resource, uint32_t serial)
1069 struct weston_seat *ws = seat_resource->data;
1070 struct shell_surface *shsurf = resource->data;
1072 if (ws->seat.pointer->button_count == 0 ||
1073 ws->seat.pointer->grab_serial != serial ||
1074 ws->seat.pointer->focus != &shsurf->surface->surface)
1077 if (surface_move(shsurf, ws) < 0)
1078 wl_resource_post_no_memory(resource);
1081 struct weston_resize_grab {
1082 struct shell_grab base;
1084 int32_t width, height;
1088 resize_grab_motion(struct wl_pointer_grab *grab,
1089 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1091 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1092 struct wl_pointer *pointer = grab->pointer;
1093 struct shell_surface *shsurf = resize->base.shsurf;
1094 int32_t width, height;
1095 wl_fixed_t from_x, from_y;
1096 wl_fixed_t to_x, to_y;
1101 weston_surface_from_global_fixed(shsurf->surface,
1102 pointer->grab_x, pointer->grab_y,
1104 weston_surface_from_global_fixed(shsurf->surface,
1105 pointer->x, pointer->y, &to_x, &to_y);
1107 width = resize->width;
1108 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
1109 width += wl_fixed_to_int(from_x - to_x);
1110 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
1111 width += wl_fixed_to_int(to_x - from_x);
1114 height = resize->height;
1115 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
1116 height += wl_fixed_to_int(from_y - to_y);
1117 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
1118 height += wl_fixed_to_int(to_y - from_y);
1121 shsurf->client->send_configure(shsurf->surface,
1122 resize->edges, width, height);
1126 send_configure(struct weston_surface *surface,
1127 uint32_t edges, int32_t width, int32_t height)
1129 struct shell_surface *shsurf = get_shell_surface(surface);
1131 wl_shell_surface_send_configure(&shsurf->resource,
1132 edges, width, height);
1135 static const struct weston_shell_client shell_client = {
1140 resize_grab_button(struct wl_pointer_grab *grab,
1141 uint32_t time, uint32_t button, uint32_t state_w)
1143 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
1144 struct wl_pointer *pointer = grab->pointer;
1145 enum wl_pointer_button_state state = state_w;
1147 if (pointer->button_count == 0 &&
1148 state == WL_POINTER_BUTTON_STATE_RELEASED) {
1149 shell_grab_end(&resize->base);
1154 static const struct wl_pointer_grab_interface resize_grab_interface = {
1161 surface_resize(struct shell_surface *shsurf,
1162 struct weston_seat *ws, uint32_t edges)
1164 struct weston_resize_grab *resize;
1166 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1169 if (edges == 0 || edges > 15 ||
1170 (edges & 3) == 3 || (edges & 12) == 12)
1173 resize = malloc(sizeof *resize);
1177 resize->edges = edges;
1178 resize->width = shsurf->surface->geometry.width;
1179 resize->height = shsurf->surface->geometry.height;
1181 shell_grab_start(&resize->base, &resize_grab_interface, shsurf,
1182 ws->seat.pointer, edges);
1188 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
1189 struct wl_resource *seat_resource, uint32_t serial,
1192 struct weston_seat *ws = seat_resource->data;
1193 struct shell_surface *shsurf = resource->data;
1195 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
1198 if (ws->seat.pointer->button_count == 0 ||
1199 ws->seat.pointer->grab_serial != serial ||
1200 ws->seat.pointer->focus != &shsurf->surface->surface)
1203 if (surface_resize(shsurf, ws, edges) < 0)
1204 wl_resource_post_no_memory(resource);
1208 busy_cursor_grab_focus(struct wl_pointer_grab *base,
1209 struct wl_surface *surface, int32_t x, int32_t y)
1211 struct shell_grab *grab = (struct shell_grab *) base;
1213 if (grab->grab.focus != surface) {
1214 shell_grab_end(grab);
1220 busy_cursor_grab_motion(struct wl_pointer_grab *grab,
1221 uint32_t time, int32_t x, int32_t y)
1226 busy_cursor_grab_button(struct wl_pointer_grab *base,
1227 uint32_t time, uint32_t button, uint32_t state)
1229 struct shell_grab *grab = (struct shell_grab *) base;
1230 struct shell_surface *shsurf;
1231 struct weston_surface *surface =
1232 (struct weston_surface *) grab->grab.pointer->current;
1233 struct weston_seat *seat =
1234 (struct weston_seat *) grab->grab.pointer->seat;
1236 shsurf = get_shell_surface(surface);
1237 if (shsurf && button == BTN_LEFT && state) {
1238 activate(shsurf->shell, shsurf->surface, seat);
1239 surface_move(shsurf, seat);
1240 } else if (shsurf && button == BTN_RIGHT && state) {
1241 activate(shsurf->shell, shsurf->surface, seat);
1242 surface_rotate(shsurf, &seat->seat);
1246 static const struct wl_pointer_grab_interface busy_cursor_grab_interface = {
1247 busy_cursor_grab_focus,
1248 busy_cursor_grab_motion,
1249 busy_cursor_grab_button,
1253 set_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1255 struct shell_grab *grab;
1257 grab = malloc(sizeof *grab);
1261 shell_grab_start(grab, &busy_cursor_grab_interface, shsurf, pointer,
1262 DESKTOP_SHELL_CURSOR_BUSY);
1266 end_busy_cursor(struct shell_surface *shsurf, struct wl_pointer *pointer)
1268 struct shell_grab *grab = (struct shell_grab *) pointer->grab;
1270 if (grab->grab.interface == &busy_cursor_grab_interface) {
1271 shell_grab_end(grab);
1277 ping_timer_destroy(struct shell_surface *shsurf)
1279 if (!shsurf || !shsurf->ping_timer)
1282 if (shsurf->ping_timer->source)
1283 wl_event_source_remove(shsurf->ping_timer->source);
1285 free(shsurf->ping_timer);
1286 shsurf->ping_timer = NULL;
1290 ping_timeout_handler(void *data)
1292 struct shell_surface *shsurf = data;
1293 struct weston_seat *seat;
1295 /* Client is not responding */
1296 shsurf->unresponsive = 1;
1298 wl_list_for_each(seat, &shsurf->surface->compositor->seat_list, link)
1299 if (seat->seat.pointer->focus == &shsurf->surface->surface)
1300 set_busy_cursor(shsurf, seat->seat.pointer);
1306 ping_handler(struct weston_surface *surface, uint32_t serial)
1308 struct shell_surface *shsurf = get_shell_surface(surface);
1309 struct wl_event_loop *loop;
1310 int ping_timeout = 200;
1314 if (!shsurf->resource.client)
1317 if (shsurf->surface == shsurf->shell->grab_surface)
1320 if (!shsurf->ping_timer) {
1321 shsurf->ping_timer = malloc(sizeof *shsurf->ping_timer);
1322 if (!shsurf->ping_timer)
1325 shsurf->ping_timer->serial = serial;
1326 loop = wl_display_get_event_loop(surface->compositor->wl_display);
1327 shsurf->ping_timer->source =
1328 wl_event_loop_add_timer(loop, ping_timeout_handler, shsurf);
1329 wl_event_source_timer_update(shsurf->ping_timer->source, ping_timeout);
1331 wl_shell_surface_send_ping(&shsurf->resource, serial);
1336 handle_pointer_focus(struct wl_listener *listener, void *data)
1338 struct wl_pointer *pointer = data;
1339 struct weston_surface *surface =
1340 (struct weston_surface *) pointer->focus;
1341 struct weston_compositor *compositor;
1342 struct shell_surface *shsurf;
1348 compositor = surface->compositor;
1349 shsurf = get_shell_surface(surface);
1351 if (shsurf && shsurf->unresponsive) {
1352 set_busy_cursor(shsurf, pointer);
1354 serial = wl_display_next_serial(compositor->wl_display);
1355 ping_handler(surface, serial);
1360 create_pointer_focus_listener(struct weston_seat *seat)
1362 struct wl_listener *listener;
1364 if (!seat->seat.pointer)
1367 listener = malloc(sizeof *listener);
1368 listener->notify = handle_pointer_focus;
1369 wl_signal_add(&seat->seat.pointer->focus_signal, listener);
1373 shell_surface_pong(struct wl_client *client, struct wl_resource *resource,
1376 struct shell_surface *shsurf = resource->data;
1377 struct desktop_shell *shell = shsurf->shell;
1378 struct weston_seat *seat;
1379 struct weston_compositor *ec = shsurf->surface->compositor;
1380 struct wl_pointer *pointer;
1381 int was_unresponsive;
1383 if (shsurf->ping_timer == NULL)
1384 /* Just ignore unsolicited pong. */
1387 if (shsurf->ping_timer->serial == serial) {
1388 was_unresponsive = shsurf->unresponsive;
1389 shsurf->unresponsive = 0;
1390 if (was_unresponsive) {
1391 /* Received pong from previously unresponsive client */
1392 wl_list_for_each(seat, &ec->seat_list, link) {
1393 pointer = seat->seat.pointer;
1394 if (pointer->focus ==
1395 &shell->grab_surface->surface &&
1397 &shsurf->surface->surface)
1398 end_busy_cursor(shsurf, pointer);
1401 ping_timer_destroy(shsurf);
1406 shell_surface_set_title(struct wl_client *client,
1407 struct wl_resource *resource, const char *title)
1409 struct shell_surface *shsurf = resource->data;
1411 free(shsurf->title);
1412 shsurf->title = strdup(title);
1416 shell_surface_set_class(struct wl_client *client,
1417 struct wl_resource *resource, const char *class)
1419 struct shell_surface *shsurf = resource->data;
1421 free(shsurf->class);
1422 shsurf->class = strdup(class);
1425 static struct weston_output *
1426 get_default_output(struct weston_compositor *compositor)
1428 return container_of(compositor->output_list.next,
1429 struct weston_output, link);
1433 shell_unset_fullscreen(struct shell_surface *shsurf)
1435 struct workspace *ws;
1436 /* undo all fullscreen things here */
1437 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1438 shell_surface_is_top_fullscreen(shsurf)) {
1439 weston_output_switch_mode(shsurf->fullscreen_output,
1440 shsurf->fullscreen_output->origin);
1442 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
1443 shsurf->fullscreen.framerate = 0;
1444 wl_list_remove(&shsurf->fullscreen.transform.link);
1445 wl_list_init(&shsurf->fullscreen.transform.link);
1446 if (shsurf->fullscreen.black_surface)
1447 weston_surface_destroy(shsurf->fullscreen.black_surface);
1448 shsurf->fullscreen.black_surface = NULL;
1449 shsurf->fullscreen_output = NULL;
1450 weston_surface_set_position(shsurf->surface,
1451 shsurf->saved_x, shsurf->saved_y);
1452 if (shsurf->saved_rotation_valid) {
1453 wl_list_insert(&shsurf->surface->geometry.transformation_list,
1454 &shsurf->rotation.transform.link);
1455 shsurf->saved_rotation_valid = false;
1458 ws = get_current_workspace(shsurf->shell);
1459 wl_list_remove(&shsurf->surface->layer_link);
1460 wl_list_insert(&ws->layer.surface_list, &shsurf->surface->layer_link);
1464 reset_shell_surface_type(struct shell_surface *surface)
1466 switch (surface->type) {
1467 case SHELL_SURFACE_FULLSCREEN:
1468 shell_unset_fullscreen(surface);
1470 case SHELL_SURFACE_MAXIMIZED:
1471 surface->output = get_default_output(surface->surface->compositor);
1472 weston_surface_set_position(surface->surface,
1476 case SHELL_SURFACE_NONE:
1477 case SHELL_SURFACE_TOPLEVEL:
1478 case SHELL_SURFACE_TRANSIENT:
1479 case SHELL_SURFACE_POPUP:
1483 surface->type = SHELL_SURFACE_NONE;
1488 set_surface_type(struct shell_surface *shsurf)
1490 struct weston_surface *surface = shsurf->surface;
1491 struct weston_surface *pes = shsurf->parent;
1493 reset_shell_surface_type(shsurf);
1495 shsurf->type = shsurf->next_type;
1496 shsurf->next_type = SHELL_SURFACE_NONE;
1498 switch (shsurf->type) {
1499 case SHELL_SURFACE_TOPLEVEL:
1501 case SHELL_SURFACE_TRANSIENT:
1502 weston_surface_set_position(surface,
1503 pes->geometry.x + shsurf->transient.x,
1504 pes->geometry.y + shsurf->transient.y);
1507 case SHELL_SURFACE_MAXIMIZED:
1508 shsurf->saved_x = surface->geometry.x;
1509 shsurf->saved_y = surface->geometry.y;
1510 shsurf->saved_position_valid = true;
1513 case SHELL_SURFACE_FULLSCREEN:
1514 shsurf->saved_x = surface->geometry.x;
1515 shsurf->saved_y = surface->geometry.y;
1516 shsurf->saved_position_valid = true;
1518 if (!wl_list_empty(&shsurf->rotation.transform.link)) {
1519 wl_list_remove(&shsurf->rotation.transform.link);
1520 wl_list_init(&shsurf->rotation.transform.link);
1521 shsurf->surface->geometry.dirty = 1;
1522 shsurf->saved_rotation_valid = true;
1532 set_toplevel(struct shell_surface *shsurf)
1534 shsurf->next_type = SHELL_SURFACE_TOPLEVEL;
1538 shell_surface_set_toplevel(struct wl_client *client,
1539 struct wl_resource *resource)
1541 struct shell_surface *surface = resource->data;
1543 set_toplevel(surface);
1547 set_transient(struct shell_surface *shsurf,
1548 struct weston_surface *parent, int x, int y, uint32_t flags)
1550 /* assign to parents output */
1551 shsurf->parent = parent;
1552 shsurf->transient.x = x;
1553 shsurf->transient.y = y;
1554 shsurf->transient.flags = flags;
1555 shsurf->next_type = SHELL_SURFACE_TRANSIENT;
1559 shell_surface_set_transient(struct wl_client *client,
1560 struct wl_resource *resource,
1561 struct wl_resource *parent_resource,
1562 int x, int y, uint32_t flags)
1564 struct shell_surface *shsurf = resource->data;
1565 struct weston_surface *parent = parent_resource->data;
1567 set_transient(shsurf, parent, x, y, flags);
1570 static struct desktop_shell *
1571 shell_surface_get_shell(struct shell_surface *shsurf)
1573 return shsurf->shell;
1577 get_output_panel_height(struct desktop_shell *shell,
1578 struct weston_output *output)
1580 struct weston_surface *surface;
1581 int panel_height = 0;
1586 wl_list_for_each(surface, &shell->panel_layer.surface_list, layer_link) {
1587 if (surface->output == output) {
1588 panel_height = surface->geometry.height;
1593 return panel_height;
1597 shell_surface_set_maximized(struct wl_client *client,
1598 struct wl_resource *resource,
1599 struct wl_resource *output_resource )
1601 struct shell_surface *shsurf = resource->data;
1602 struct weston_surface *es = shsurf->surface;
1603 struct desktop_shell *shell = NULL;
1604 uint32_t edges = 0, panel_height = 0;
1606 /* get the default output, if the client set it as NULL
1607 check whether the ouput is available */
1608 if (output_resource)
1609 shsurf->output = output_resource->data;
1610 else if (es->output)
1611 shsurf->output = es->output;
1613 shsurf->output = get_default_output(es->compositor);
1615 shell = shell_surface_get_shell(shsurf);
1616 panel_height = get_output_panel_height(shell, shsurf->output);
1617 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
1619 shsurf->client->send_configure(shsurf->surface, edges,
1620 shsurf->output->width,
1621 shsurf->output->height - panel_height);
1623 shsurf->next_type = SHELL_SURFACE_MAXIMIZED;
1627 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
1629 static struct weston_surface *
1630 create_black_surface(struct weston_compositor *ec,
1631 struct weston_surface *fs_surface,
1632 float x, float y, int w, int h)
1634 struct weston_surface *surface = NULL;
1636 surface = weston_surface_create(ec);
1637 if (surface == NULL) {
1638 weston_log("no memory\n");
1642 surface->configure = black_surface_configure;
1643 surface->private = fs_surface;
1644 weston_surface_configure(surface, x, y, w, h);
1645 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
1646 pixman_region32_fini(&surface->opaque);
1647 pixman_region32_init_rect(&surface->opaque, 0, 0, w, h);
1652 /* Create black surface and append it to the associated fullscreen surface.
1653 * Handle size dismatch and positioning according to the method. */
1655 shell_configure_fullscreen(struct shell_surface *shsurf)
1657 struct weston_output *output = shsurf->fullscreen_output;
1658 struct weston_surface *surface = shsurf->surface;
1659 struct weston_matrix *matrix;
1660 float scale, output_aspect, surface_aspect, x, y;
1662 if (!shsurf->fullscreen.black_surface)
1663 shsurf->fullscreen.black_surface =
1664 create_black_surface(surface->compositor,
1666 output->x, output->y,
1670 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1671 wl_list_insert(&surface->layer_link,
1672 &shsurf->fullscreen.black_surface->layer_link);
1673 shsurf->fullscreen.black_surface->output = output;
1675 switch (shsurf->fullscreen.type) {
1676 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
1677 if (surface->buffer)
1678 center_on_output(surface, shsurf->fullscreen_output);
1680 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
1681 /* 1:1 mapping between surface and output dimensions */
1682 if (output->width == surface->geometry.width &&
1683 output->height == surface->geometry.height) {
1684 weston_surface_set_position(surface, output->x, output->y);
1688 matrix = &shsurf->fullscreen.transform.matrix;
1689 weston_matrix_init(matrix);
1691 output_aspect = (float) output->width /
1692 (float) output->height;
1693 surface_aspect = (float) surface->geometry.width /
1694 (float) surface->geometry.height;
1695 if (output_aspect < surface_aspect)
1696 scale = (float) output->width /
1697 (float) surface->geometry.width;
1699 scale = (float) output->height /
1700 (float) surface->geometry.height;
1702 weston_matrix_scale(matrix, scale, scale, 1);
1703 wl_list_remove(&shsurf->fullscreen.transform.link);
1704 wl_list_insert(&surface->geometry.transformation_list,
1705 &shsurf->fullscreen.transform.link);
1706 x = output->x + (output->width - surface->geometry.width * scale) / 2;
1707 y = output->y + (output->height - surface->geometry.height * scale) / 2;
1708 weston_surface_set_position(surface, x, y);
1711 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
1712 if (shell_surface_is_top_fullscreen(shsurf)) {
1713 struct weston_mode mode = {0,
1714 surface->geometry.width,
1715 surface->geometry.height,
1716 shsurf->fullscreen.framerate};
1718 if (weston_output_switch_mode(output, &mode) == 0) {
1719 weston_surface_configure(shsurf->fullscreen.black_surface,
1720 output->x, output->y,
1723 weston_surface_set_position(surface, output->x, output->y);
1728 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
1735 /* make the fullscreen and black surface at the top */
1737 shell_stack_fullscreen(struct shell_surface *shsurf)
1739 struct weston_output *output = shsurf->fullscreen_output;
1740 struct weston_surface *surface = shsurf->surface;
1741 struct desktop_shell *shell = shell_surface_get_shell(shsurf);
1743 wl_list_remove(&surface->layer_link);
1744 wl_list_insert(&shell->fullscreen_layer.surface_list,
1745 &surface->layer_link);
1746 weston_surface_damage(surface);
1748 if (!shsurf->fullscreen.black_surface)
1749 shsurf->fullscreen.black_surface =
1750 create_black_surface(surface->compositor,
1752 output->x, output->y,
1756 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
1757 wl_list_insert(&surface->layer_link,
1758 &shsurf->fullscreen.black_surface->layer_link);
1759 weston_surface_damage(shsurf->fullscreen.black_surface);
1763 shell_map_fullscreen(struct shell_surface *shsurf)
1765 shell_stack_fullscreen(shsurf);
1766 shell_configure_fullscreen(shsurf);
1770 shell_surface_set_fullscreen(struct wl_client *client,
1771 struct wl_resource *resource,
1774 struct wl_resource *output_resource)
1776 struct shell_surface *shsurf = resource->data;
1777 struct weston_surface *es = shsurf->surface;
1779 if (output_resource)
1780 shsurf->output = output_resource->data;
1781 else if (es->output)
1782 shsurf->output = es->output;
1784 shsurf->output = get_default_output(es->compositor);
1786 shsurf->fullscreen_output = shsurf->output;
1787 shsurf->fullscreen.type = method;
1788 shsurf->fullscreen.framerate = framerate;
1789 shsurf->next_type = SHELL_SURFACE_FULLSCREEN;
1791 shsurf->client->send_configure(shsurf->surface, 0,
1792 shsurf->output->width,
1793 shsurf->output->height);
1797 popup_grab_focus(struct wl_pointer_grab *grab,
1798 struct wl_surface *surface,
1802 struct wl_pointer *pointer = grab->pointer;
1803 struct shell_surface *priv =
1804 container_of(grab, struct shell_surface, popup.grab);
1805 struct wl_client *client = priv->surface->surface.resource.client;
1807 if (surface && surface->resource.client == client) {
1808 wl_pointer_set_focus(pointer, surface, x, y);
1809 grab->focus = surface;
1811 wl_pointer_set_focus(pointer, NULL,
1812 wl_fixed_from_int(0),
1813 wl_fixed_from_int(0));
1819 popup_grab_motion(struct wl_pointer_grab *grab,
1820 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
1822 struct wl_resource *resource;
1824 resource = grab->pointer->focus_resource;
1826 wl_pointer_send_motion(resource, time, sx, sy);
1830 popup_grab_button(struct wl_pointer_grab *grab,
1831 uint32_t time, uint32_t button, uint32_t state_w)
1833 struct wl_resource *resource;
1834 struct shell_surface *shsurf =
1835 container_of(grab, struct shell_surface, popup.grab);
1836 struct wl_display *display;
1837 enum wl_pointer_button_state state = state_w;
1840 resource = grab->pointer->focus_resource;
1842 display = wl_client_get_display(resource->client);
1843 serial = wl_display_get_serial(display);
1844 wl_pointer_send_button(resource, serial, time, button, state);
1845 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
1846 (shsurf->popup.initial_up ||
1847 time - shsurf->popup.seat->pointer->grab_time > 500)) {
1848 popup_grab_end(grab->pointer);
1851 if (state == WL_POINTER_BUTTON_STATE_RELEASED)
1852 shsurf->popup.initial_up = 1;
1855 static const struct wl_pointer_grab_interface popup_grab_interface = {
1862 popup_grab_end(struct wl_pointer *pointer)
1864 struct wl_pointer_grab *grab = pointer->grab;
1865 struct shell_surface *shsurf =
1866 container_of(grab, struct shell_surface, popup.grab);
1868 if (pointer->grab->interface == &popup_grab_interface) {
1869 wl_shell_surface_send_popup_done(&shsurf->resource);
1870 wl_pointer_end_grab(grab->pointer);
1871 shsurf->popup.grab.pointer = NULL;
1876 shell_map_popup(struct shell_surface *shsurf)
1878 struct wl_seat *seat = shsurf->popup.seat;
1879 struct weston_surface *es = shsurf->surface;
1880 struct weston_surface *parent = shsurf->parent;
1882 /* Remove the old transform. We don't want to add it twice
1883 * otherwise Weston will go into an infinite loop when going
1884 * through the transforms. */
1885 if (!wl_list_empty(&shsurf->popup.parent_transform.link)) {
1886 wl_list_remove(&shsurf->popup.parent_transform.link);
1887 wl_list_init(&shsurf->popup.parent_transform.link);
1890 es->output = parent->output;
1891 shsurf->popup.grab.interface = &popup_grab_interface;
1893 weston_surface_update_transform(parent);
1894 if (parent->transform.enabled) {
1895 shsurf->popup.parent_transform.matrix =
1896 parent->transform.matrix;
1898 /* construct x, y translation matrix */
1899 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
1900 shsurf->popup.parent_transform.matrix.d[12] =
1902 shsurf->popup.parent_transform.matrix.d[13] =
1905 wl_list_insert(es->geometry.transformation_list.prev,
1906 &shsurf->popup.parent_transform.link);
1908 shsurf->popup.initial_up = 0;
1909 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
1910 weston_surface_update_transform(es);
1912 /* We don't require the grab to still be active, but if another
1913 * grab has started in the meantime, we end the popup now. */
1914 if (seat->pointer->grab_serial == shsurf->popup.serial) {
1915 wl_pointer_start_grab(seat->pointer, &shsurf->popup.grab);
1917 wl_shell_surface_send_popup_done(&shsurf->resource);
1922 shell_surface_set_popup(struct wl_client *client,
1923 struct wl_resource *resource,
1924 struct wl_resource *seat_resource,
1926 struct wl_resource *parent_resource,
1927 int32_t x, int32_t y, uint32_t flags)
1929 struct shell_surface *shsurf = resource->data;
1931 shsurf->type = SHELL_SURFACE_POPUP;
1932 shsurf->parent = parent_resource->data;
1933 shsurf->popup.seat = seat_resource->data;
1934 shsurf->popup.serial = serial;
1935 shsurf->popup.x = x;
1936 shsurf->popup.y = y;
1939 static const struct wl_shell_surface_interface shell_surface_implementation = {
1942 shell_surface_resize,
1943 shell_surface_set_toplevel,
1944 shell_surface_set_transient,
1945 shell_surface_set_fullscreen,
1946 shell_surface_set_popup,
1947 shell_surface_set_maximized,
1948 shell_surface_set_title,
1949 shell_surface_set_class
1953 destroy_shell_surface(struct shell_surface *shsurf)
1955 if (shsurf->popup.grab.pointer)
1956 wl_pointer_end_grab(shsurf->popup.grab.pointer);
1958 if (shsurf->fullscreen.type == WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER &&
1959 shell_surface_is_top_fullscreen(shsurf)) {
1960 weston_output_switch_mode(shsurf->fullscreen_output,
1961 shsurf->fullscreen_output->origin);
1964 if (shsurf->fullscreen.black_surface)
1965 weston_surface_destroy(shsurf->fullscreen.black_surface);
1967 /* As destroy_resource() use wl_list_for_each_safe(),
1968 * we can always remove the listener.
1970 wl_list_remove(&shsurf->surface_destroy_listener.link);
1971 shsurf->surface->configure = NULL;
1972 ping_timer_destroy(shsurf);
1974 wl_list_remove(&shsurf->link);
1979 shell_destroy_shell_surface(struct wl_resource *resource)
1981 struct shell_surface *shsurf = resource->data;
1983 destroy_shell_surface(shsurf);
1987 shell_handle_surface_destroy(struct wl_listener *listener, void *data)
1989 struct shell_surface *shsurf = container_of(listener,
1990 struct shell_surface,
1991 surface_destroy_listener);
1993 if (shsurf->resource.client) {
1994 wl_resource_destroy(&shsurf->resource);
1996 wl_signal_emit(&shsurf->resource.destroy_signal,
1998 destroy_shell_surface(shsurf);
2003 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
2005 static struct shell_surface *
2006 get_shell_surface(struct weston_surface *surface)
2008 if (surface->configure == shell_surface_configure)
2009 return surface->private;
2014 static struct shell_surface *
2015 create_shell_surface(void *shell, struct weston_surface *surface,
2016 const struct weston_shell_client *client)
2018 struct shell_surface *shsurf;
2020 if (surface->configure) {
2021 weston_log("surface->configure already set\n");
2025 shsurf = calloc(1, sizeof *shsurf);
2027 weston_log("no memory to allocate shell surface\n");
2031 surface->configure = shell_surface_configure;
2032 surface->private = shsurf;
2034 shsurf->shell = (struct desktop_shell *) shell;
2035 shsurf->unresponsive = 0;
2036 shsurf->saved_position_valid = false;
2037 shsurf->saved_rotation_valid = false;
2038 shsurf->surface = surface;
2039 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
2040 shsurf->fullscreen.framerate = 0;
2041 shsurf->fullscreen.black_surface = NULL;
2042 shsurf->ping_timer = NULL;
2043 wl_list_init(&shsurf->fullscreen.transform.link);
2045 wl_signal_init(&shsurf->resource.destroy_signal);
2046 shsurf->surface_destroy_listener.notify = shell_handle_surface_destroy;
2047 wl_signal_add(&surface->surface.resource.destroy_signal,
2048 &shsurf->surface_destroy_listener);
2050 /* init link so its safe to always remove it in destroy_shell_surface */
2051 wl_list_init(&shsurf->link);
2053 /* empty when not in use */
2054 wl_list_init(&shsurf->rotation.transform.link);
2055 weston_matrix_init(&shsurf->rotation.rotation);
2057 wl_list_init(&shsurf->workspace_transform.link);
2058 wl_list_init(&shsurf->popup.parent_transform.link);
2060 shsurf->type = SHELL_SURFACE_NONE;
2061 shsurf->next_type = SHELL_SURFACE_NONE;
2063 shsurf->client = client;
2069 shell_get_shell_surface(struct wl_client *client,
2070 struct wl_resource *resource,
2072 struct wl_resource *surface_resource)
2074 struct weston_surface *surface = surface_resource->data;
2075 struct desktop_shell *shell = resource->data;
2076 struct shell_surface *shsurf;
2078 if (get_shell_surface(surface)) {
2079 wl_resource_post_error(surface_resource,
2080 WL_DISPLAY_ERROR_INVALID_OBJECT,
2081 "desktop_shell::get_shell_surface already requested");
2085 shsurf = create_shell_surface(shell, surface, &shell_client);
2087 wl_resource_post_error(surface_resource,
2088 WL_DISPLAY_ERROR_INVALID_OBJECT,
2089 "surface->configure already set");
2093 shsurf->resource.destroy = shell_destroy_shell_surface;
2094 shsurf->resource.object.id = id;
2095 shsurf->resource.object.interface = &wl_shell_surface_interface;
2096 shsurf->resource.object.implementation =
2097 (void (**)(void)) &shell_surface_implementation;
2098 shsurf->resource.data = shsurf;
2100 wl_client_add_resource(client, &shsurf->resource);
2103 static const struct wl_shell_interface shell_implementation = {
2104 shell_get_shell_surface
2108 handle_screensaver_sigchld(struct weston_process *proc, int status)
2110 struct desktop_shell *shell =
2111 container_of(proc, struct desktop_shell, screensaver.process);
2112 struct weston_output *output;
2117 wl_list_for_each(output, &shell->compositor->output_list, link)
2118 if (output->set_dpms)
2119 output->set_dpms(output, WESTON_DPMS_STANDBY);
2123 launch_screensaver(struct desktop_shell *shell)
2125 if (shell->screensaver.binding)
2128 if (!shell->screensaver.path)
2131 if (shell->screensaver.process.pid != 0) {
2132 weston_log("old screensaver still running\n");
2136 weston_client_launch(shell->compositor,
2137 &shell->screensaver.process,
2138 shell->screensaver.path,
2139 handle_screensaver_sigchld);
2143 terminate_screensaver(struct desktop_shell *shell)
2145 if (shell->screensaver.process.pid == 0)
2148 kill(shell->screensaver.process.pid, SIGTERM);
2152 configure_static_surface(struct weston_surface *es, struct weston_layer *layer)
2154 struct weston_surface *s, *next;
2156 wl_list_for_each_safe(s, next, &layer->surface_list, layer_link) {
2157 if (s->output == es->output && s != es) {
2158 weston_surface_unmap(s);
2159 s->configure = NULL;
2163 weston_surface_configure(es, es->output->x, es->output->y,
2164 es->buffer->width, es->buffer->height);
2166 if (wl_list_empty(&es->layer_link)) {
2167 wl_list_insert(&layer->surface_list, &es->layer_link);
2168 weston_compositor_schedule_repaint(es->compositor);
2173 background_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2175 struct desktop_shell *shell = es->private;
2177 configure_static_surface(es, &shell->background_layer);
2181 desktop_shell_set_background(struct wl_client *client,
2182 struct wl_resource *resource,
2183 struct wl_resource *output_resource,
2184 struct wl_resource *surface_resource)
2186 struct desktop_shell *shell = resource->data;
2187 struct weston_surface *surface = surface_resource->data;
2189 if (surface->configure) {
2190 wl_resource_post_error(surface_resource,
2191 WL_DISPLAY_ERROR_INVALID_OBJECT,
2192 "surface role already assigned");
2196 surface->configure = background_configure;
2197 surface->private = shell;
2198 surface->output = output_resource->data;
2199 desktop_shell_send_configure(resource, 0,
2201 surface->output->width,
2202 surface->output->height);
2206 panel_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2208 struct desktop_shell *shell = es->private;
2210 configure_static_surface(es, &shell->panel_layer);
2214 desktop_shell_set_panel(struct wl_client *client,
2215 struct wl_resource *resource,
2216 struct wl_resource *output_resource,
2217 struct wl_resource *surface_resource)
2219 struct desktop_shell *shell = resource->data;
2220 struct weston_surface *surface = surface_resource->data;
2222 if (surface->configure) {
2223 wl_resource_post_error(surface_resource,
2224 WL_DISPLAY_ERROR_INVALID_OBJECT,
2225 "surface role already assigned");
2229 surface->configure = panel_configure;
2230 surface->private = shell;
2231 surface->output = output_resource->data;
2232 desktop_shell_send_configure(resource, 0,
2234 surface->output->width,
2235 surface->output->height);
2239 lock_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
2241 struct desktop_shell *shell = surface->private;
2243 center_on_output(surface, get_default_output(shell->compositor));
2245 if (!weston_surface_is_mapped(surface)) {
2246 wl_list_insert(&shell->lock_layer.surface_list,
2247 &surface->layer_link);
2248 weston_surface_update_transform(surface);
2249 weston_compositor_wake(shell->compositor);
2254 handle_lock_surface_destroy(struct wl_listener *listener, void *data)
2256 struct desktop_shell *shell =
2257 container_of(listener, struct desktop_shell, lock_surface_listener);
2259 weston_log("lock surface gone\n");
2260 shell->lock_surface = NULL;
2264 desktop_shell_set_lock_surface(struct wl_client *client,
2265 struct wl_resource *resource,
2266 struct wl_resource *surface_resource)
2268 struct desktop_shell *shell = resource->data;
2269 struct weston_surface *surface = surface_resource->data;
2271 shell->prepare_event_sent = false;
2276 shell->lock_surface = surface;
2278 shell->lock_surface_listener.notify = handle_lock_surface_destroy;
2279 wl_signal_add(&surface_resource->destroy_signal,
2280 &shell->lock_surface_listener);
2282 surface->configure = lock_surface_configure;
2283 surface->private = shell;
2287 resume_desktop(struct desktop_shell *shell)
2289 struct weston_surface *surface;
2290 struct workspace *ws = get_current_workspace(shell);
2292 wl_list_for_each(surface, &shell->screensaver.surfaces, link)
2293 weston_surface_unmap(surface);
2295 terminate_screensaver(shell);
2297 wl_list_remove(&shell->lock_layer.link);
2298 wl_list_insert(&shell->compositor->cursor_layer.link,
2299 &shell->fullscreen_layer.link);
2300 wl_list_insert(&shell->fullscreen_layer.link,
2301 &shell->panel_layer.link);
2302 if (shell->showing_input_panels) {
2303 wl_list_insert(&shell->panel_layer.link,
2304 &shell->input_panel_layer.link);
2305 wl_list_insert(&shell->input_panel_layer.link,
2308 wl_list_insert(&shell->panel_layer.link, &ws->layer.link);
2311 restore_focus_state(shell, get_current_workspace(shell));
2313 shell->locked = false;
2314 shell->compositor->idle_time = shell->compositor->option_idle_time;
2315 weston_compositor_wake(shell->compositor);
2316 weston_compositor_damage_all(shell->compositor);
2320 desktop_shell_unlock(struct wl_client *client,
2321 struct wl_resource *resource)
2323 struct desktop_shell *shell = resource->data;
2325 shell->prepare_event_sent = false;
2328 resume_desktop(shell);
2332 desktop_shell_set_grab_surface(struct wl_client *client,
2333 struct wl_resource *resource,
2334 struct wl_resource *surface_resource)
2336 struct desktop_shell *shell = resource->data;
2338 shell->grab_surface = surface_resource->data;
2341 static const struct desktop_shell_interface desktop_shell_implementation = {
2342 desktop_shell_set_background,
2343 desktop_shell_set_panel,
2344 desktop_shell_set_lock_surface,
2345 desktop_shell_unlock,
2346 desktop_shell_set_grab_surface
2349 static enum shell_surface_type
2350 get_shell_surface_type(struct weston_surface *surface)
2352 struct shell_surface *shsurf;
2354 shsurf = get_shell_surface(surface);
2356 return SHELL_SURFACE_NONE;
2357 return shsurf->type;
2361 move_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2363 struct weston_surface *surface =
2364 (struct weston_surface *) seat->pointer->focus;
2365 struct shell_surface *shsurf;
2367 if (surface == NULL)
2370 shsurf = get_shell_surface(surface);
2371 if (shsurf == NULL || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2374 surface_move(shsurf, (struct weston_seat *) seat);
2378 resize_binding(struct wl_seat *seat, uint32_t time, uint32_t button, void *data)
2380 struct weston_surface *surface =
2381 (struct weston_surface *) seat->pointer->focus;
2384 struct shell_surface *shsurf;
2386 if (surface == NULL)
2389 shsurf = get_shell_surface(surface);
2390 if (!shsurf || shsurf->type == SHELL_SURFACE_FULLSCREEN)
2393 weston_surface_from_global(surface,
2394 wl_fixed_to_int(seat->pointer->grab_x),
2395 wl_fixed_to_int(seat->pointer->grab_y),
2398 if (x < surface->geometry.width / 3)
2399 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
2400 else if (x < 2 * surface->geometry.width / 3)
2403 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
2405 if (y < surface->geometry.height / 3)
2406 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
2407 else if (y < 2 * surface->geometry.height / 3)
2410 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
2412 surface_resize(shsurf, (struct weston_seat *) seat, edges);
2416 surface_opacity_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2417 wl_fixed_t value, void *data)
2420 struct shell_surface *shsurf;
2421 struct weston_surface *surface =
2422 (struct weston_surface *) seat->pointer->focus;
2424 if (surface == NULL)
2427 shsurf = get_shell_surface(surface);
2431 surface->alpha -= wl_fixed_to_double(value) * step;
2433 if (surface->alpha > 1.0)
2434 surface->alpha = 1.0;
2435 if (surface->alpha < step)
2436 surface->alpha = step;
2438 surface->geometry.dirty = 1;
2439 weston_surface_damage(surface);
2443 do_zoom(struct wl_seat *seat, uint32_t time, uint32_t key, uint32_t axis,
2446 struct weston_seat *ws = (struct weston_seat *) seat;
2447 struct weston_compositor *compositor = ws->compositor;
2448 struct weston_output *output;
2451 wl_list_for_each(output, &compositor->output_list, link) {
2452 if (pixman_region32_contains_point(&output->region,
2453 wl_fixed_to_double(seat->pointer->x),
2454 wl_fixed_to_double(seat->pointer->y),
2456 if (key == KEY_PAGEUP)
2457 increment = output->zoom.increment;
2458 else if (key == KEY_PAGEDOWN)
2459 increment = -output->zoom.increment;
2460 else if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL)
2461 /* For every pixel zoom 20th of a step */
2462 increment = output->zoom.increment *
2463 -wl_fixed_to_double(value) / 20.0;
2467 output->zoom.level += increment;
2469 if (output->zoom.level < 0.0)
2470 output->zoom.level = 0.0;
2471 else if (output->zoom.level > output->zoom.max_level)
2472 output->zoom.level = output->zoom.max_level;
2473 else if (!output->zoom.active) {
2474 output->zoom.active = 1;
2475 output->disable_planes++;
2478 output->zoom.spring_z.target = output->zoom.level;
2480 weston_output_update_zoom(output, output->zoom.type);
2486 zoom_axis_binding(struct wl_seat *seat, uint32_t time, uint32_t axis,
2487 wl_fixed_t value, void *data)
2489 do_zoom(seat, time, 0, axis, value);
2493 zoom_key_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2496 do_zoom(seat, time, key, 0, 0);
2500 terminate_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
2503 struct weston_compositor *compositor = data;
2505 wl_display_terminate(compositor->wl_display);
2509 rotate_grab_motion(struct wl_pointer_grab *grab,
2510 uint32_t time, wl_fixed_t x, wl_fixed_t y)
2512 struct rotate_grab *rotate =
2513 container_of(grab, struct rotate_grab, base.grab);
2514 struct wl_pointer *pointer = grab->pointer;
2515 struct shell_surface *shsurf = rotate->base.shsurf;
2516 struct weston_surface *surface;
2517 float cx, cy, dx, dy, cposx, cposy, dposx, dposy, r;
2522 surface = shsurf->surface;
2524 cx = 0.5f * surface->geometry.width;
2525 cy = 0.5f * surface->geometry.height;
2527 dx = wl_fixed_to_double(pointer->x) - rotate->center.x;
2528 dy = wl_fixed_to_double(pointer->y) - rotate->center.y;
2529 r = sqrtf(dx * dx + dy * dy);
2531 wl_list_remove(&shsurf->rotation.transform.link);
2532 shsurf->surface->geometry.dirty = 1;
2535 struct weston_matrix *matrix =
2536 &shsurf->rotation.transform.matrix;
2538 weston_matrix_init(&rotate->rotation);
2539 rotate->rotation.d[0] = dx / r;
2540 rotate->rotation.d[4] = -dy / r;
2541 rotate->rotation.d[1] = -rotate->rotation.d[4];
2542 rotate->rotation.d[5] = rotate->rotation.d[0];
2544 weston_matrix_init(matrix);
2545 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
2546 weston_matrix_multiply(matrix, &shsurf->rotation.rotation);
2547 weston_matrix_multiply(matrix, &rotate->rotation);
2548 weston_matrix_translate(matrix, cx, cy, 0.0f);
2551 &shsurf->surface->geometry.transformation_list,
2552 &shsurf->rotation.transform.link);
2554 wl_list_init(&shsurf->rotation.transform.link);
2555 weston_matrix_init(&shsurf->rotation.rotation);
2556 weston_matrix_init(&rotate->rotation);
2559 /* We need to adjust the position of the surface
2560 * in case it was resized in a rotated state before */
2561 cposx = surface->geometry.x + cx;
2562 cposy = surface->geometry.y + cy;
2563 dposx = rotate->center.x - cposx;
2564 dposy = rotate->center.y - cposy;
2565 if (dposx != 0.0f || dposy != 0.0f) {
2566 weston_surface_set_position(surface,
2567 surface->geometry.x + dposx,
2568 surface->geometry.y + dposy);
2571 /* Repaint implies weston_surface_update_transform(), which
2572 * lazily applies the damage due to rotation update.
2574 weston_compositor_schedule_repaint(shsurf->surface->compositor);
2578 rotate_grab_button(struct wl_pointer_grab *grab,
2579 uint32_t time, uint32_t button, uint32_t state_w)
2581 struct rotate_grab *rotate =
2582 container_of(grab, struct rotate_grab, base.grab);
2583 struct wl_pointer *pointer = grab->pointer;
2584 struct shell_surface *shsurf = rotate->base.shsurf;
2585 enum wl_pointer_button_state state = state_w;
2587 if (pointer->button_count == 0 &&
2588 state == WL_POINTER_BUTTON_STATE_RELEASED) {
2590 weston_matrix_multiply(&shsurf->rotation.rotation,
2592 shell_grab_end(&rotate->base);
2597 static const struct wl_pointer_grab_interface rotate_grab_interface = {
2604 surface_rotate(struct shell_surface *surface, struct wl_seat *seat)
2606 struct rotate_grab *rotate;
2610 rotate = malloc(sizeof *rotate);
2614 weston_surface_to_global_float(surface->surface,
2615 surface->surface->geometry.width / 2,
2616 surface->surface->geometry.height / 2,
2617 &rotate->center.x, &rotate->center.y);
2619 dx = wl_fixed_to_double(seat->pointer->x) - rotate->center.x;
2620 dy = wl_fixed_to_double(seat->pointer->y) - rotate->center.y;
2621 r = sqrtf(dx * dx + dy * dy);
2623 struct weston_matrix inverse;
2625 weston_matrix_init(&inverse);
2626 inverse.d[0] = dx / r;
2627 inverse.d[4] = dy / r;
2628 inverse.d[1] = -inverse.d[4];
2629 inverse.d[5] = inverse.d[0];
2630 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
2632 weston_matrix_init(&rotate->rotation);
2633 rotate->rotation.d[0] = dx / r;
2634 rotate->rotation.d[4] = -dy / r;
2635 rotate->rotation.d[1] = -rotate->rotation.d[4];
2636 rotate->rotation.d[5] = rotate->rotation.d[0];
2638 weston_matrix_init(&surface->rotation.rotation);
2639 weston_matrix_init(&rotate->rotation);
2642 shell_grab_start(&rotate->base, &rotate_grab_interface, surface,
2643 seat->pointer, DESKTOP_SHELL_CURSOR_ARROW);
2647 rotate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2650 struct weston_surface *base_surface =
2651 (struct weston_surface *) seat->pointer->focus;
2652 struct shell_surface *surface;
2654 if (base_surface == NULL)
2657 surface = get_shell_surface(base_surface);
2658 if (!surface || surface->type == SHELL_SURFACE_FULLSCREEN)
2661 surface_rotate(surface, seat);
2665 lower_fullscreen_layer(struct desktop_shell *shell)
2667 struct workspace *ws;
2668 struct weston_surface *surface, *prev;
2670 ws = get_current_workspace(shell);
2671 wl_list_for_each_reverse_safe(surface, prev,
2672 &shell->fullscreen_layer.surface_list,
2674 weston_surface_restack(surface, &ws->layer.surface_list);
2678 activate(struct desktop_shell *shell, struct weston_surface *es,
2679 struct weston_seat *seat)
2681 struct focus_state *state;
2682 struct workspace *ws;
2684 weston_surface_activate(es, seat);
2686 state = ensure_focus_state(shell, seat);
2690 state->keyboard_focus = es;
2691 wl_list_remove(&state->surface_destroy_listener.link);
2692 wl_signal_add(&es->surface.resource.destroy_signal,
2693 &state->surface_destroy_listener);
2695 switch (get_shell_surface_type(es)) {
2696 case SHELL_SURFACE_FULLSCREEN:
2697 /* should on top of panels */
2698 shell_stack_fullscreen(get_shell_surface(es));
2699 shell_configure_fullscreen(get_shell_surface(es));
2702 ws = get_current_workspace(shell);
2703 lower_fullscreen_layer(shell);
2704 weston_surface_restack(es, &ws->layer.surface_list);
2709 /* no-op func for checking black surface */
2711 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2716 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
2718 if (es->configure == black_surface_configure) {
2720 *fs_surface = (struct weston_surface *)es->private;
2727 click_to_activate_binding(struct wl_seat *seat, uint32_t time, uint32_t button,
2730 struct weston_seat *ws = (struct weston_seat *) seat;
2731 struct desktop_shell *shell = data;
2732 struct weston_surface *focus;
2733 struct weston_surface *upper;
2735 focus = (struct weston_surface *) seat->pointer->focus;
2739 if (is_black_surface(focus, &upper))
2742 if (get_shell_surface_type(focus) == SHELL_SURFACE_NONE)
2745 if (seat->pointer->grab == &seat->pointer->default_grab)
2746 activate(shell, focus, ws);
2750 lock(struct wl_listener *listener, void *data)
2752 struct desktop_shell *shell =
2753 container_of(listener, struct desktop_shell, lock_listener);
2754 struct weston_output *output;
2755 struct workspace *ws = get_current_workspace(shell);
2757 if (shell->locked) {
2758 wl_list_for_each(output, &shell->compositor->output_list, link)
2759 /* TODO: find a way to jump to other DPMS levels */
2760 if (output->set_dpms)
2761 output->set_dpms(output, WESTON_DPMS_STANDBY);
2765 shell->locked = true;
2767 /* Hide all surfaces by removing the fullscreen, panel and
2768 * toplevel layers. This way nothing else can show or receive
2769 * input events while we are locked. */
2771 wl_list_remove(&shell->panel_layer.link);
2772 wl_list_remove(&shell->fullscreen_layer.link);
2773 if (shell->showing_input_panels)
2774 wl_list_remove(&shell->input_panel_layer.link);
2775 wl_list_remove(&ws->layer.link);
2776 wl_list_insert(&shell->compositor->cursor_layer.link,
2777 &shell->lock_layer.link);
2779 launch_screensaver(shell);
2781 /* TODO: disable bindings that should not work while locked. */
2783 /* All this must be undone in resume_desktop(). */
2787 unlock(struct wl_listener *listener, void *data)
2789 struct desktop_shell *shell =
2790 container_of(listener, struct desktop_shell, unlock_listener);
2792 if (!shell->locked || shell->lock_surface) {
2793 weston_compositor_wake(shell->compositor);
2797 /* If desktop-shell client has gone away, unlock immediately. */
2798 if (!shell->child.desktop_shell) {
2799 resume_desktop(shell);
2803 if (shell->prepare_event_sent)
2806 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
2807 shell->prepare_event_sent = true;
2811 show_input_panels(struct wl_listener *listener, void *data)
2813 struct desktop_shell *shell =
2814 container_of(listener, struct desktop_shell,
2815 show_input_panel_listener);
2816 struct input_panel_surface *surface, *next;
2817 struct weston_surface *ws;
2819 if (shell->showing_input_panels)
2822 shell->showing_input_panels = true;
2825 wl_list_insert(&shell->panel_layer.link,
2826 &shell->input_panel_layer.link);
2828 wl_list_for_each_safe(surface, next,
2829 &shell->input_panel.surfaces, link) {
2830 ws = surface->surface;
2831 wl_list_insert(&shell->input_panel_layer.surface_list,
2833 ws->geometry.dirty = 1;
2834 weston_surface_update_transform(ws);
2835 weston_surface_damage(ws);
2836 weston_slide_run(ws, ws->geometry.height, 0, NULL, NULL);
2841 hide_input_panels(struct wl_listener *listener, void *data)
2843 struct desktop_shell *shell =
2844 container_of(listener, struct desktop_shell,
2845 hide_input_panel_listener);
2846 struct weston_surface *surface, *next;
2848 shell->showing_input_panels = false;
2851 wl_list_remove(&shell->input_panel_layer.link);
2853 wl_list_for_each_safe(surface, next,
2854 &shell->input_panel_layer.surface_list, layer_link)
2855 weston_surface_unmap(surface);
2859 center_on_output(struct weston_surface *surface, struct weston_output *output)
2861 float x = (output->width - surface->buffer->width) / 2;
2862 float y = (output->height - surface->buffer->height) / 2;
2864 weston_surface_configure(surface, output->x + x, output->y + y,
2865 surface->buffer->width,
2866 surface->buffer->height);
2870 weston_surface_set_initial_position (struct weston_surface *surface,
2871 struct desktop_shell *shell)
2873 struct weston_compositor *compositor = shell->compositor;
2875 int range_x, range_y;
2876 int dx, dy, x, y, panel_height;
2877 struct weston_output *output, *target_output = NULL;
2878 struct weston_seat *seat;
2880 /* As a heuristic place the new window on the same output as the
2881 * pointer. Falling back to the output containing 0, 0.
2883 * TODO: Do something clever for touch too?
2885 wl_list_for_each(seat, &compositor->seat_list, link) {
2886 if (seat->has_pointer) {
2887 ix = wl_fixed_to_int(seat->pointer.x);
2888 iy = wl_fixed_to_int(seat->pointer.y);
2893 wl_list_for_each(output, &compositor->output_list, link) {
2894 if (pixman_region32_contains_point(&output->region, ix, iy, NULL)) {
2895 target_output = output;
2900 if (!target_output) {
2901 weston_surface_set_position(surface, 10 + random() % 400,
2902 10 + random() % 400);
2906 /* Valid range within output where the surface will still be onscreen.
2907 * If this is negative it means that the surface is bigger than
2910 panel_height = get_output_panel_height(shell, target_output);
2911 range_x = target_output->width - surface->geometry.width;
2912 range_y = (target_output->height - panel_height) -
2913 surface->geometry.height;
2916 dx = random() % range_x;
2921 dy = panel_height + random() % range_y;
2925 x = target_output->x + dx;
2926 y = target_output->y + dy;
2928 weston_surface_set_position (surface, x, y);
2932 map(struct desktop_shell *shell, struct weston_surface *surface,
2933 int32_t width, int32_t height, int32_t sx, int32_t sy)
2935 struct weston_compositor *compositor = shell->compositor;
2936 struct shell_surface *shsurf = get_shell_surface(surface);
2937 enum shell_surface_type surface_type = shsurf->type;
2938 struct weston_surface *parent;
2939 struct weston_seat *seat;
2940 struct workspace *ws;
2941 int panel_height = 0;
2943 surface->geometry.width = width;
2944 surface->geometry.height = height;
2945 surface->geometry.dirty = 1;
2947 /* initial positioning, see also configure() */
2948 switch (surface_type) {
2949 case SHELL_SURFACE_TOPLEVEL:
2950 weston_surface_set_initial_position(surface, shell);
2952 case SHELL_SURFACE_FULLSCREEN:
2953 center_on_output(surface, shsurf->fullscreen_output);
2954 shell_map_fullscreen(shsurf);
2956 case SHELL_SURFACE_MAXIMIZED:
2957 /* use surface configure to set the geometry */
2958 panel_height = get_output_panel_height(shell,surface->output);
2959 weston_surface_set_position(surface, shsurf->output->x,
2960 shsurf->output->y + panel_height);
2962 case SHELL_SURFACE_POPUP:
2963 shell_map_popup(shsurf);
2965 case SHELL_SURFACE_NONE:
2966 weston_surface_set_position(surface,
2967 surface->geometry.x + sx,
2968 surface->geometry.y + sy);
2974 /* surface stacking order, see also activate() */
2975 switch (surface_type) {
2976 case SHELL_SURFACE_POPUP:
2977 case SHELL_SURFACE_TRANSIENT:
2978 parent = shsurf->parent;
2979 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
2981 case SHELL_SURFACE_FULLSCREEN:
2982 case SHELL_SURFACE_NONE:
2985 ws = get_current_workspace(shell);
2986 wl_list_insert(&ws->layer.surface_list, &surface->layer_link);
2990 if (surface_type != SHELL_SURFACE_NONE) {
2991 weston_surface_update_transform(surface);
2992 if (surface_type == SHELL_SURFACE_MAXIMIZED)
2993 surface->output = shsurf->output;
2996 switch (surface_type) {
2997 case SHELL_SURFACE_TRANSIENT:
2998 if (shsurf->transient.flags ==
2999 WL_SHELL_SURFACE_TRANSIENT_INACTIVE)
3001 case SHELL_SURFACE_TOPLEVEL:
3002 case SHELL_SURFACE_FULLSCREEN:
3003 case SHELL_SURFACE_MAXIMIZED:
3004 if (!shell->locked) {
3005 wl_list_for_each(seat, &compositor->seat_list, link)
3006 activate(shell, surface, seat);
3013 if (surface_type == SHELL_SURFACE_TOPLEVEL)
3015 switch (shell->win_animation_type) {
3016 case ANIMATION_FADE:
3017 weston_fade_run(surface, NULL, NULL);
3019 case ANIMATION_ZOOM:
3020 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
3029 configure(struct desktop_shell *shell, struct weston_surface *surface,
3030 float x, float y, int32_t width, int32_t height)
3032 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
3033 struct shell_surface *shsurf;
3035 shsurf = get_shell_surface(surface);
3037 surface_type = shsurf->type;
3039 surface->geometry.x = x;
3040 surface->geometry.y = y;
3041 surface->geometry.width = width;
3042 surface->geometry.height = height;
3043 surface->geometry.dirty = 1;
3045 switch (surface_type) {
3046 case SHELL_SURFACE_FULLSCREEN:
3047 shell_stack_fullscreen(shsurf);
3048 shell_configure_fullscreen(shsurf);
3050 case SHELL_SURFACE_MAXIMIZED:
3051 /* setting x, y and using configure to change that geometry */
3052 surface->geometry.x = surface->output->x;
3053 surface->geometry.y = surface->output->y +
3054 get_output_panel_height(shell,surface->output);
3056 case SHELL_SURFACE_TOPLEVEL:
3062 /* XXX: would a fullscreen surface need the same handling? */
3063 if (surface->output) {
3064 weston_surface_update_transform(surface);
3066 if (surface_type == SHELL_SURFACE_MAXIMIZED)
3067 surface->output = shsurf->output;
3072 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3074 struct shell_surface *shsurf = get_shell_surface(es);
3075 struct desktop_shell *shell = shsurf->shell;
3076 int type_changed = 0;
3078 if (shsurf->next_type != SHELL_SURFACE_NONE &&
3079 shsurf->type != shsurf->next_type) {
3080 set_surface_type(shsurf);
3084 if (!weston_surface_is_mapped(es)) {
3085 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
3086 } else if (type_changed || sx != 0 || sy != 0 ||
3087 es->geometry.width != es->buffer->width ||
3088 es->geometry.height != es->buffer->height) {
3089 float from_x, from_y;
3092 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
3093 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
3094 configure(shell, es,
3095 es->geometry.x + to_x - from_x,
3096 es->geometry.y + to_y - from_y,
3097 es->buffer->width, es->buffer->height);
3101 static void launch_desktop_shell_process(void *data);
3104 desktop_shell_sigchld(struct weston_process *process, int status)
3107 struct desktop_shell *shell =
3108 container_of(process, struct desktop_shell, child.process);
3110 shell->child.process.pid = 0;
3111 shell->child.client = NULL; /* already destroyed by wayland */
3113 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
3114 time = weston_compositor_get_time();
3115 if (time - shell->child.deathstamp > 30000) {
3116 shell->child.deathstamp = time;
3117 shell->child.deathcount = 0;
3120 shell->child.deathcount++;
3121 if (shell->child.deathcount > 5) {
3122 weston_log("weston-desktop-shell died, giving up.\n");
3126 weston_log("weston-desktop-shell died, respawning...\n");
3127 launch_desktop_shell_process(shell);
3131 launch_desktop_shell_process(void *data)
3133 struct desktop_shell *shell = data;
3134 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
3136 shell->child.client = weston_client_launch(shell->compositor,
3137 &shell->child.process,
3139 desktop_shell_sigchld);
3141 if (!shell->child.client)
3142 weston_log("not able to start %s\n", shell_exe);
3146 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
3148 struct desktop_shell *shell = data;
3150 wl_client_add_object(client, &wl_shell_interface,
3151 &shell_implementation, id, shell);
3155 unbind_desktop_shell(struct wl_resource *resource)
3157 struct desktop_shell *shell = resource->data;
3160 resume_desktop(shell);
3162 shell->child.desktop_shell = NULL;
3163 shell->prepare_event_sent = false;
3168 bind_desktop_shell(struct wl_client *client,
3169 void *data, uint32_t version, uint32_t id)
3171 struct desktop_shell *shell = data;
3172 struct wl_resource *resource;
3174 resource = wl_client_add_object(client, &desktop_shell_interface,
3175 &desktop_shell_implementation,
3178 if (client == shell->child.client) {
3179 resource->destroy = unbind_desktop_shell;
3180 shell->child.desktop_shell = resource;
3184 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3185 "permission to bind desktop_shell denied");
3186 wl_resource_destroy(resource);
3190 screensaver_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3192 struct desktop_shell *shell = surface->private;
3197 center_on_output(surface, surface->output);
3199 if (wl_list_empty(&surface->layer_link)) {
3200 wl_list_insert(shell->lock_layer.surface_list.prev,
3201 &surface->layer_link);
3202 weston_surface_update_transform(surface);
3203 shell->compositor->idle_time = shell->screensaver.duration;
3204 weston_compositor_wake(shell->compositor);
3205 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
3210 screensaver_set_surface(struct wl_client *client,
3211 struct wl_resource *resource,
3212 struct wl_resource *surface_resource,
3213 struct wl_resource *output_resource)
3215 struct desktop_shell *shell = resource->data;
3216 struct weston_surface *surface = surface_resource->data;
3217 struct weston_output *output = output_resource->data;
3219 surface->configure = screensaver_configure;
3220 surface->private = shell;
3221 surface->output = output;
3224 static const struct screensaver_interface screensaver_implementation = {
3225 screensaver_set_surface
3229 unbind_screensaver(struct wl_resource *resource)
3231 struct desktop_shell *shell = resource->data;
3233 shell->screensaver.binding = NULL;
3238 bind_screensaver(struct wl_client *client,
3239 void *data, uint32_t version, uint32_t id)
3241 struct desktop_shell *shell = data;
3242 struct wl_resource *resource;
3244 resource = wl_client_add_object(client, &screensaver_interface,
3245 &screensaver_implementation,
3248 if (shell->screensaver.binding == NULL) {
3249 resource->destroy = unbind_screensaver;
3250 shell->screensaver.binding = resource;
3254 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3255 "interface object already bound");
3256 wl_resource_destroy(resource);
3260 input_panel_configure(struct weston_surface *surface, int32_t sx, int32_t sy)
3262 struct weston_mode *mode = surface->output->current;
3263 float x = (mode->width - surface->buffer->width) / 2;
3264 float y = mode->height - surface->buffer->height;
3266 /* Don't map the input panel here, wait for
3267 * show_input_panels signal. */
3269 weston_surface_configure(surface,
3270 surface->output->x + x,
3271 surface->output->y + y,
3272 surface->buffer->width,
3273 surface->buffer->height);
3277 destroy_input_panel_surface(struct wl_listener *listener,
3280 struct input_panel_surface *input_panel_surface =
3281 container_of(listener, struct input_panel_surface, listener);
3283 wl_list_remove(&listener->link);
3284 wl_list_remove(&input_panel_surface->link);
3286 free(input_panel_surface);
3290 input_panel_set_surface(struct wl_client *client,
3291 struct wl_resource *resource,
3292 struct wl_resource *surface_resource,
3293 struct wl_resource *output_resource)
3295 struct desktop_shell *shell = resource->data;
3296 struct weston_surface *surface = surface_resource->data;
3297 struct weston_output *output = output_resource->data;
3298 struct input_panel_surface *input_panel_surface;
3300 surface->configure = input_panel_configure;
3301 surface->private = shell;
3302 surface->output = output;
3304 input_panel_surface = malloc(sizeof *input_panel_surface);
3305 if (!input_panel_surface) {
3306 wl_resource_post_no_memory(resource);
3310 input_panel_surface->surface = surface;
3311 input_panel_surface->listener.notify = destroy_input_panel_surface;
3313 wl_signal_add(&surface_resource->destroy_signal,
3314 &input_panel_surface->listener);
3316 wl_list_insert(&shell->input_panel.surfaces,
3317 &input_panel_surface->link);
3320 static const struct input_panel_interface input_panel_implementation = {
3321 input_panel_set_surface
3325 unbind_input_panel(struct wl_resource *resource)
3327 struct desktop_shell *shell = resource->data;
3329 shell->input_panel.binding = NULL;
3334 bind_input_panel(struct wl_client *client,
3335 void *data, uint32_t version, uint32_t id)
3337 struct desktop_shell *shell = data;
3338 struct wl_resource *resource;
3340 resource = wl_client_add_object(client, &input_panel_interface,
3341 &input_panel_implementation,
3344 if (shell->input_panel.binding == NULL) {
3345 resource->destroy = unbind_input_panel;
3346 shell->input_panel.binding = resource;
3350 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
3351 "interface object already bound");
3352 wl_resource_destroy(resource);
3356 struct desktop_shell *shell;
3357 struct weston_surface *current;
3358 struct wl_listener listener;
3359 struct wl_keyboard_grab grab;
3363 switcher_next(struct switcher *switcher)
3365 struct weston_surface *surface;
3366 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
3367 struct shell_surface *shsurf;
3368 struct workspace *ws = get_current_workspace(switcher->shell);
3370 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3371 switch (get_shell_surface_type(surface)) {
3372 case SHELL_SURFACE_TOPLEVEL:
3373 case SHELL_SURFACE_FULLSCREEN:
3374 case SHELL_SURFACE_MAXIMIZED:
3377 if (prev == switcher->current)
3380 surface->alpha = 0.25;
3381 surface->geometry.dirty = 1;
3382 weston_surface_damage(surface);
3388 if (is_black_surface(surface, NULL)) {
3389 surface->alpha = 0.25;
3390 surface->geometry.dirty = 1;
3391 weston_surface_damage(surface);
3401 wl_list_remove(&switcher->listener.link);
3402 wl_signal_add(&next->surface.resource.destroy_signal,
3403 &switcher->listener);
3405 switcher->current = next;
3408 shsurf = get_shell_surface(switcher->current);
3409 if (shsurf && shsurf->type ==SHELL_SURFACE_FULLSCREEN)
3410 shsurf->fullscreen.black_surface->alpha = 1.0;
3414 switcher_handle_surface_destroy(struct wl_listener *listener, void *data)
3416 struct switcher *switcher =
3417 container_of(listener, struct switcher, listener);
3419 switcher_next(switcher);
3423 switcher_destroy(struct switcher *switcher)
3425 struct weston_surface *surface;
3426 struct wl_keyboard *keyboard = switcher->grab.keyboard;
3427 struct workspace *ws = get_current_workspace(switcher->shell);
3429 wl_list_for_each(surface, &ws->layer.surface_list, layer_link) {
3430 surface->alpha = 1.0;
3431 weston_surface_damage(surface);
3434 if (switcher->current)
3435 activate(switcher->shell, switcher->current,
3436 (struct weston_seat *) keyboard->seat);
3437 wl_list_remove(&switcher->listener.link);
3438 wl_keyboard_end_grab(keyboard);
3443 switcher_key(struct wl_keyboard_grab *grab,
3444 uint32_t time, uint32_t key, uint32_t state_w)
3446 struct switcher *switcher = container_of(grab, struct switcher, grab);
3447 enum wl_keyboard_key_state state = state_w;
3449 if (key == KEY_TAB && state == WL_KEYBOARD_KEY_STATE_PRESSED)
3450 switcher_next(switcher);
3454 switcher_modifier(struct wl_keyboard_grab *grab, uint32_t serial,
3455 uint32_t mods_depressed, uint32_t mods_latched,
3456 uint32_t mods_locked, uint32_t group)
3458 struct switcher *switcher = container_of(grab, struct switcher, grab);
3459 struct weston_seat *seat = (struct weston_seat *) grab->keyboard->seat;
3461 if ((seat->modifier_state & switcher->shell->binding_modifier) == 0)
3462 switcher_destroy(switcher);
3465 static const struct wl_keyboard_grab_interface switcher_grab = {
3471 switcher_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3474 struct desktop_shell *shell = data;
3475 struct switcher *switcher;
3477 switcher = malloc(sizeof *switcher);
3478 switcher->shell = shell;
3479 switcher->current = NULL;
3480 switcher->listener.notify = switcher_handle_surface_destroy;
3481 wl_list_init(&switcher->listener.link);
3483 lower_fullscreen_layer(switcher->shell);
3484 switcher->grab.interface = &switcher_grab;
3485 wl_keyboard_start_grab(seat->keyboard, &switcher->grab);
3486 wl_keyboard_set_focus(seat->keyboard, NULL);
3487 switcher_next(switcher);
3491 backlight_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3494 struct weston_compositor *compositor = data;
3495 struct weston_output *output;
3496 long backlight_new = 0;
3498 /* TODO: we're limiting to simple use cases, where we assume just
3499 * control on the primary display. We'd have to extend later if we
3500 * ever get support for setting backlights on random desktop LCD
3502 output = get_default_output(compositor);
3506 if (!output->set_backlight)
3509 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
3510 backlight_new = output->backlight_current - 25;
3511 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
3512 backlight_new = output->backlight_current + 25;
3514 if (backlight_new < 5)
3516 if (backlight_new > 255)
3517 backlight_new = 255;
3519 output->backlight_current = backlight_new;
3520 output->set_backlight(output, output->backlight_current);
3524 debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3527 struct desktop_shell *shell = data;
3528 struct weston_compositor *compositor = shell->compositor;
3529 struct weston_surface *surface;
3530 struct weston_plane plane;
3532 if (shell->debug_repaint_surface) {
3533 weston_surface_destroy(shell->debug_repaint_surface);
3534 shell->debug_repaint_surface = NULL;
3536 surface = weston_surface_create(compositor);
3537 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
3538 weston_surface_configure(surface, 0, 0, 8192, 8192);
3539 wl_list_insert(&compositor->fade_layer.surface_list,
3540 &surface->layer_link);
3541 pixman_region32_fini(&surface->input);
3542 pixman_region32_init(&surface->input);
3544 /* Here's the dirty little trick that makes the
3545 * repaint debugging work: we move the surface to a
3546 * different plane and force an update_transform to
3547 * update dependent state and clear the
3548 * geometry.dirty bit. This way the call to
3549 * damage_below() in update_transform() does not
3550 * add damage to the primary plane. */
3552 weston_plane_init(&plane, 0, 0);
3553 surface->plane = &plane;
3554 weston_surface_update_transform(surface);
3555 shell->debug_repaint_surface = surface;
3556 surface->plane = &compositor->primary_plane;
3557 weston_plane_release(&plane);
3563 fan_debug_repaint_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3566 struct desktop_shell *shell = data;
3567 struct weston_compositor *compositor = shell->compositor;
3568 compositor->fan_debug = !compositor->fan_debug;
3569 weston_compositor_damage_all(compositor);
3573 force_kill_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
3576 struct wl_surface *focus_surface;
3577 struct wl_client *client;
3578 struct desktop_shell *shell = data;
3579 struct weston_compositor *compositor = shell->compositor;
3582 focus_surface = seat->keyboard->focus;
3586 wl_signal_emit(&compositor->kill_signal, focus_surface);
3588 client = focus_surface->resource.client;
3589 wl_client_get_credentials(client, &pid, NULL, NULL);
3591 /* Skip clients that we launched ourselves (the credentials of
3592 * the socketpair is ours) */
3593 if (pid == getpid())
3600 workspace_up_binding(struct wl_seat *seat, uint32_t time,
3601 uint32_t key, void *data)
3603 struct desktop_shell *shell = data;
3604 unsigned int new_index = shell->workspaces.current;
3611 change_workspace(shell, new_index);
3615 workspace_down_binding(struct wl_seat *seat, uint32_t time,
3616 uint32_t key, void *data)
3618 struct desktop_shell *shell = data;
3619 unsigned int new_index = shell->workspaces.current;
3623 if (new_index < shell->workspaces.num - 1)
3626 change_workspace(shell, new_index);
3630 workspace_f_binding(struct wl_seat *seat, uint32_t time,
3631 uint32_t key, void *data)
3633 struct desktop_shell *shell = data;
3634 unsigned int new_index;
3638 new_index = key - KEY_F1;
3639 if (new_index >= shell->workspaces.num)
3640 new_index = shell->workspaces.num - 1;
3642 change_workspace(shell, new_index);
3646 workspace_move_surface_up_binding(struct wl_seat *seat, uint32_t time,
3647 uint32_t key, void *data)
3649 struct desktop_shell *shell = data;
3650 unsigned int new_index = shell->workspaces.current;
3658 take_surface_to_workspace_by_seat(shell, seat, new_index);
3662 workspace_move_surface_down_binding(struct wl_seat *seat, uint32_t time,
3663 uint32_t key, void *data)
3665 struct desktop_shell *shell = data;
3666 unsigned int new_index = shell->workspaces.current;
3671 if (new_index < shell->workspaces.num - 1)
3674 take_surface_to_workspace_by_seat(shell, seat, new_index);
3678 shell_destroy(struct wl_listener *listener, void *data)
3680 struct desktop_shell *shell =
3681 container_of(listener, struct desktop_shell, destroy_listener);
3682 struct workspace **ws;
3684 if (shell->child.client)
3685 wl_client_destroy(shell->child.client);
3687 wl_list_remove(&shell->lock_listener.link);
3688 wl_list_remove(&shell->unlock_listener.link);
3689 wl_list_remove(&shell->show_input_panel_listener.link);
3690 wl_list_remove(&shell->hide_input_panel_listener.link);
3692 wl_array_for_each(ws, &shell->workspaces.array)
3693 workspace_destroy(*ws);
3694 wl_array_release(&shell->workspaces.array);
3696 free(shell->screensaver.path);
3701 shell_add_bindings(struct weston_compositor *ec, struct desktop_shell *shell)
3704 int i, num_workspace_bindings;
3706 /* fixed bindings */
3707 weston_compositor_add_key_binding(ec, KEY_BACKSPACE,
3708 MODIFIER_CTRL | MODIFIER_ALT,
3709 terminate_binding, ec);
3710 weston_compositor_add_button_binding(ec, BTN_LEFT, 0,
3711 click_to_activate_binding,
3713 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3714 MODIFIER_SUPER | MODIFIER_ALT,
3715 surface_opacity_binding, NULL);
3716 weston_compositor_add_axis_binding(ec, WL_POINTER_AXIS_VERTICAL_SCROLL,
3717 MODIFIER_SUPER, zoom_axis_binding,
3720 /* configurable bindings */
3721 mod = shell->binding_modifier;
3722 weston_compositor_add_key_binding(ec, KEY_PAGEUP, mod,
3723 zoom_key_binding, NULL);
3724 weston_compositor_add_key_binding(ec, KEY_PAGEDOWN, mod,
3725 zoom_key_binding, NULL);
3726 weston_compositor_add_button_binding(ec, BTN_LEFT, mod, move_binding,
3728 weston_compositor_add_button_binding(ec, BTN_MIDDLE, mod,
3729 resize_binding, shell);
3730 weston_compositor_add_button_binding(ec, BTN_RIGHT, mod,
3731 rotate_binding, NULL);
3732 weston_compositor_add_key_binding(ec, KEY_TAB, mod, switcher_binding,
3734 weston_compositor_add_key_binding(ec, KEY_F9, mod, backlight_binding,
3736 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSDOWN, 0,
3737 backlight_binding, ec);
3738 weston_compositor_add_key_binding(ec, KEY_F10, mod, backlight_binding,
3740 weston_compositor_add_key_binding(ec, KEY_BRIGHTNESSUP, 0,
3741 backlight_binding, ec);
3742 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_SHIFT,
3743 debug_repaint_binding, shell);
3744 weston_compositor_add_key_binding(ec, KEY_SPACE, mod | MODIFIER_ALT,
3745 fan_debug_repaint_binding, shell);
3746 weston_compositor_add_key_binding(ec, KEY_K, mod,
3747 force_kill_binding, shell);
3748 weston_compositor_add_key_binding(ec, KEY_UP, mod,
3749 workspace_up_binding, shell);
3750 weston_compositor_add_key_binding(ec, KEY_DOWN, mod,
3751 workspace_down_binding, shell);
3752 weston_compositor_add_key_binding(ec, KEY_UP, mod | MODIFIER_SHIFT,
3753 workspace_move_surface_up_binding,
3755 weston_compositor_add_key_binding(ec, KEY_DOWN, mod | MODIFIER_SHIFT,
3756 workspace_move_surface_down_binding,
3759 /* Add bindings for mod+F[1-6] for workspace 1 to 6. */
3760 if (shell->workspaces.num > 1) {
3761 num_workspace_bindings = shell->workspaces.num;
3762 if (num_workspace_bindings > 6)
3763 num_workspace_bindings = 6;
3764 for (i = 0; i < num_workspace_bindings; i++)
3765 weston_compositor_add_key_binding(ec, KEY_F1 + i, mod,
3766 workspace_f_binding,
3772 module_init(struct weston_compositor *ec)
3774 struct weston_seat *seat;
3775 struct desktop_shell *shell;
3776 struct workspace **pws;
3778 struct wl_event_loop *loop;
3780 shell = malloc(sizeof *shell);
3784 memset(shell, 0, sizeof *shell);
3785 shell->compositor = ec;
3787 shell->destroy_listener.notify = shell_destroy;
3788 wl_signal_add(&ec->destroy_signal, &shell->destroy_listener);
3789 shell->lock_listener.notify = lock;
3790 wl_signal_add(&ec->lock_signal, &shell->lock_listener);
3791 shell->unlock_listener.notify = unlock;
3792 wl_signal_add(&ec->unlock_signal, &shell->unlock_listener);
3793 shell->show_input_panel_listener.notify = show_input_panels;
3794 wl_signal_add(&ec->show_input_panel_signal, &shell->show_input_panel_listener);
3795 shell->hide_input_panel_listener.notify = hide_input_panels;
3796 wl_signal_add(&ec->hide_input_panel_signal, &shell->hide_input_panel_listener);
3797 ec->ping_handler = ping_handler;
3798 ec->shell_interface.shell = shell;
3799 ec->shell_interface.create_shell_surface = create_shell_surface;
3800 ec->shell_interface.set_toplevel = set_toplevel;
3801 ec->shell_interface.set_transient = set_transient;
3802 ec->shell_interface.move = surface_move;
3803 ec->shell_interface.resize = surface_resize;
3805 wl_list_init(&shell->screensaver.surfaces);
3806 wl_list_init(&shell->input_panel.surfaces);
3808 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
3809 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
3810 weston_layer_init(&shell->background_layer, &shell->panel_layer.link);
3811 weston_layer_init(&shell->lock_layer, NULL);
3812 weston_layer_init(&shell->input_panel_layer, NULL);
3814 wl_array_init(&shell->workspaces.array);
3815 wl_list_init(&shell->workspaces.client_list);
3817 shell_configuration(shell);
3819 for (i = 0; i < shell->workspaces.num; i++) {
3820 pws = wl_array_add(&shell->workspaces.array, sizeof *pws);
3824 *pws = workspace_create();
3828 activate_workspace(shell, 0);
3830 wl_list_init(&shell->workspaces.anim_sticky_list);
3831 wl_list_init(&shell->workspaces.animation.link);
3832 shell->workspaces.animation.frame = animate_workspace_change_frame;
3834 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
3835 shell, bind_shell) == NULL)
3838 if (wl_display_add_global(ec->wl_display,
3839 &desktop_shell_interface,
3840 shell, bind_desktop_shell) == NULL)
3843 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
3844 shell, bind_screensaver) == NULL)
3847 if (wl_display_add_global(ec->wl_display, &input_panel_interface,
3848 shell, bind_input_panel) == NULL)
3851 if (wl_display_add_global(ec->wl_display, &workspace_manager_interface,
3852 shell, bind_workspace_manager) == NULL)
3855 shell->child.deathstamp = weston_compositor_get_time();
3857 loop = wl_display_get_event_loop(ec->wl_display);
3858 wl_event_loop_add_idle(loop, launch_desktop_shell_process, shell);
3860 wl_list_for_each(seat, &ec->seat_list, link)
3861 create_pointer_focus_listener(seat);
3863 shell_add_bindings(ec, shell);