2 * Copyright © 2010 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>
34 #include <wayland-server.h>
35 #include "compositor.h"
36 #include "desktop-shell-server-protocol.h"
37 #include "../shared/config-parser.h"
42 struct weston_compositor *compositor;
43 struct weston_shell shell;
46 struct weston_process process;
47 struct wl_client *client;
48 struct wl_resource *desktop_shell;
55 bool prepare_event_sent;
57 struct shell_surface *lock_surface;
58 struct wl_listener lock_surface_listener;
59 struct wl_list hidden_surface_list;
61 struct wl_list backgrounds;
62 struct wl_list panels;
67 struct wl_resource *binding;
68 struct wl_list surfaces;
69 struct weston_process process;
73 enum shell_surface_type {
77 SHELL_SURFACE_BACKGROUND,
79 SHELL_SURFACE_SCREENSAVER,
81 SHELL_SURFACE_TOPLEVEL,
82 SHELL_SURFACE_TRANSIENT,
83 SHELL_SURFACE_FULLSCREEN,
87 struct shell_surface {
88 struct wl_resource resource;
90 struct weston_surface *surface;
91 struct wl_listener surface_destroy_listener;
92 struct shell_surface *parent;
94 enum shell_surface_type type;
95 int32_t saved_x, saved_y;
98 struct weston_transform transform;
99 struct weston_matrix rotation;
109 struct weston_output *output;
113 struct weston_move_grab {
115 struct weston_surface *surface;
121 struct shell_surface *surface;
122 struct weston_matrix rotation;
130 shell_configuration(struct wl_shell *shell)
136 struct config_key saver_keys[] = {
137 { "path", CONFIG_KEY_STRING, &path },
138 { "duration", CONFIG_KEY_INTEGER, &duration },
141 struct config_section cs[] = {
142 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
145 config_file = config_file_path("weston-desktop-shell.ini");
146 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
149 shell->screensaver.path = path;
150 shell->screensaver.duration = duration;
154 noop_grab_focus(struct wl_grab *grab, uint32_t time,
155 struct wl_surface *surface, int32_t x, int32_t y)
161 move_grab_motion(struct wl_grab *grab,
162 uint32_t time, int32_t x, int32_t y)
164 struct weston_move_grab *move = (struct weston_move_grab *) grab;
165 struct wl_input_device *device = grab->input_device;
166 struct weston_surface *es = move->surface;
168 weston_surface_configure(es,
169 device->x + move->dx,
170 device->y + move->dy,
171 es->geometry.width, es->geometry.height);
175 move_grab_button(struct wl_grab *grab,
176 uint32_t time, int32_t button, int32_t state)
178 struct wl_input_device *device = grab->input_device;
180 if (device->button_count == 0 && state == 0) {
181 wl_input_device_end_grab(device, time);
186 static const struct wl_grab_interface move_grab_interface = {
193 weston_surface_move(struct weston_surface *es,
194 struct weston_input_device *wd, uint32_t time)
196 struct weston_move_grab *move;
198 move = malloc(sizeof *move);
202 move->grab.interface = &move_grab_interface;
203 move->dx = es->geometry.x - wd->input_device.grab_x;
204 move->dy = es->geometry.y - wd->input_device.grab_y;
207 wl_input_device_start_grab(&wd->input_device, &move->grab, time);
209 wl_input_device_set_pointer_focus(&wd->input_device,
210 NULL, time, 0, 0, 0, 0);
216 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
217 struct wl_resource *input_resource, uint32_t time)
219 struct weston_input_device *wd = input_resource->data;
220 struct shell_surface *shsurf = resource->data;
222 if (wd->input_device.button_count == 0 ||
223 wd->input_device.grab_time != time ||
224 wd->input_device.pointer_focus != &shsurf->surface->surface)
227 if (weston_surface_move(shsurf->surface, wd, time) < 0)
228 wl_resource_post_no_memory(resource);
231 struct weston_resize_grab {
234 int32_t dx, dy, width, height;
235 struct shell_surface *shsurf;
239 resize_grab_motion(struct wl_grab *grab,
240 uint32_t time, int32_t x, int32_t y)
242 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
243 struct wl_input_device *device = grab->input_device;
244 int32_t width, height;
246 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
247 width = device->grab_x - device->x + resize->width;
248 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
249 width = device->x - device->grab_x + resize->width;
251 width = resize->width;
254 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
255 height = device->grab_y - device->y + resize->height;
256 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
257 height = device->y - device->grab_y + resize->height;
259 height = resize->height;
262 wl_resource_post_event(&resize->shsurf->resource,
263 WL_SHELL_SURFACE_CONFIGURE, time, resize->edges,
268 resize_grab_button(struct wl_grab *grab,
269 uint32_t time, int32_t button, int32_t state)
271 struct wl_input_device *device = grab->input_device;
273 if (device->button_count == 0 && state == 0) {
274 wl_input_device_end_grab(device, time);
279 static const struct wl_grab_interface resize_grab_interface = {
286 weston_surface_resize(struct shell_surface *shsurf,
287 struct weston_input_device *wd,
288 uint32_t time, uint32_t edges)
290 struct weston_resize_grab *resize;
291 struct weston_surface *es = shsurf->surface;
293 /* FIXME: Reject if fullscreen */
295 resize = malloc(sizeof *resize);
299 resize->grab.interface = &resize_grab_interface;
300 resize->edges = edges;
301 resize->dx = es->geometry.x - wd->input_device.grab_x;
302 resize->dy = es->geometry.y - wd->input_device.grab_y;
303 resize->width = es->geometry.width;
304 resize->height = es->geometry.height;
305 resize->shsurf = shsurf;
307 if (edges == 0 || edges > 15 ||
308 (edges & 3) == 3 || (edges & 12) == 12)
311 wl_input_device_start_grab(&wd->input_device, &resize->grab, time);
313 wl_input_device_set_pointer_focus(&wd->input_device,
314 NULL, time, 0, 0, 0, 0);
324 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
325 struct wl_resource *input_resource, uint32_t time,
328 struct weston_input_device *wd = input_resource->data;
329 struct shell_surface *shsurf = resource->data;
331 /* FIXME: Reject if fullscreen */
333 if (wd->input_device.button_count == 0 ||
334 wd->input_device.grab_time != time ||
335 wd->input_device.pointer_focus != &shsurf->surface->surface)
338 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
339 wl_resource_post_no_memory(resource);
343 reset_shell_surface_type(struct shell_surface *surface)
345 switch (surface->type) {
346 case SHELL_SURFACE_FULLSCREEN:
347 surface->surface->geometry.x = surface->saved_x;
348 surface->surface->geometry.y = surface->saved_y;
349 surface->surface->geometry.dirty = 1;
350 surface->surface->fullscreen_output = NULL;
352 case SHELL_SURFACE_PANEL:
353 case SHELL_SURFACE_BACKGROUND:
354 wl_list_remove(&surface->link);
355 wl_list_init(&surface->link);
357 case SHELL_SURFACE_SCREENSAVER:
358 case SHELL_SURFACE_LOCK:
359 wl_resource_post_error(&surface->resource,
360 WL_DISPLAY_ERROR_INVALID_METHOD,
361 "cannot reassign surface type");
363 case SHELL_SURFACE_NONE:
364 case SHELL_SURFACE_TOPLEVEL:
365 case SHELL_SURFACE_TRANSIENT:
366 case SHELL_SURFACE_POPUP:
370 surface->type = SHELL_SURFACE_NONE;
375 shell_surface_set_toplevel(struct wl_client *client,
376 struct wl_resource *resource)
379 struct shell_surface *surface = resource->data;
381 if (reset_shell_surface_type(surface))
384 weston_surface_damage(surface->surface);
385 surface->type = SHELL_SURFACE_TOPLEVEL;
389 shell_surface_set_transient(struct wl_client *client,
390 struct wl_resource *resource,
391 struct wl_resource *parent_resource,
392 int x, int y, uint32_t flags)
394 struct shell_surface *shsurf = resource->data;
395 struct weston_surface *es = shsurf->surface;
396 struct shell_surface *pshsurf = parent_resource->data;
397 struct weston_surface *pes = pshsurf->surface;
399 if (reset_shell_surface_type(shsurf))
402 /* assign to parents output */
403 es->output = pes->output;
405 es->geometry.x = pes->geometry.x + x;
406 es->geometry.y = pes->geometry.y + y;
407 es->geometry.dirty = 1;
409 weston_surface_damage(es);
410 shsurf->type = SHELL_SURFACE_TRANSIENT;
413 static struct weston_output *
414 get_default_output(struct weston_compositor *compositor)
416 return container_of(compositor->output_list.next,
417 struct weston_output, link);
421 shell_surface_set_fullscreen(struct wl_client *client,
422 struct wl_resource *resource)
425 struct shell_surface *shsurf = resource->data;
426 struct weston_surface *es = shsurf->surface;
427 struct weston_output *output;
429 if (reset_shell_surface_type(shsurf))
432 /* FIXME: Fullscreen on first output */
433 /* FIXME: Handle output going away */
434 output = get_default_output(es->compositor);
437 shsurf->saved_x = es->geometry.x;
438 shsurf->saved_y = es->geometry.y;
439 es->geometry.x = (output->current->width - es->geometry.width) / 2;
440 es->geometry.y = (output->current->height - es->geometry.height) / 2;
441 es->geometry.dirty = 1;
442 es->fullscreen_output = output;
443 weston_surface_damage(es);
444 shsurf->type = SHELL_SURFACE_FULLSCREEN;
448 popup_grab_focus(struct wl_grab *grab, uint32_t time,
449 struct wl_surface *surface, int32_t x, int32_t y)
451 struct wl_input_device *device = grab->input_device;
452 struct shell_surface *priv =
453 container_of(grab, struct shell_surface, popup.grab);
454 struct wl_client *client = priv->surface->surface.resource.client;
456 if (surface && surface->resource.client == client) {
457 wl_input_device_set_pointer_focus(device, surface, time,
458 device->x, device->y, x, y);
459 grab->focus = surface;
461 wl_input_device_set_pointer_focus(device, NULL,
468 popup_grab_motion(struct wl_grab *grab,
469 uint32_t time, int32_t x, int32_t y)
471 struct wl_input_device *device = grab->input_device;
472 struct wl_resource *resource;
474 resource = grab->input_device->pointer_focus_resource;
476 wl_resource_post_event(resource, WL_INPUT_DEVICE_MOTION,
477 time, device->x, device->y, x, y);
481 popup_grab_button(struct wl_grab *grab,
482 uint32_t time, int32_t button, int32_t state)
484 struct wl_resource *resource;
485 struct shell_surface *shsurf =
486 container_of(grab, struct shell_surface, popup.grab);
488 resource = grab->input_device->pointer_focus_resource;
490 wl_resource_post_event(resource, WL_INPUT_DEVICE_BUTTON,
491 time, button, state);
492 } else if (state == 0 &&
493 (shsurf->popup.initial_up ||
494 time - shsurf->popup.time > 500)) {
495 wl_resource_post_event(&shsurf->resource,
496 WL_SHELL_SURFACE_POPUP_DONE);
497 wl_input_device_end_grab(grab->input_device, time);
498 shsurf->popup.grab.input_device = NULL;
502 shsurf->popup.initial_up = 1;
505 static const struct wl_grab_interface popup_grab_interface = {
512 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
514 struct wl_input_device *device;
515 struct weston_surface *es = shsurf->surface;
516 struct weston_surface *parent = shsurf->parent->surface;
518 es->output = parent->output;
520 shsurf->popup.grab.interface = &popup_grab_interface;
521 device = es->compositor->input_device;
523 es->geometry.x = shsurf->parent->surface->geometry.x + shsurf->popup.x;
524 es->geometry.y = shsurf->parent->surface->geometry.y + shsurf->popup.y;
525 es->geometry.dirty = 1;
527 shsurf->popup.grab.input_device = device;
528 shsurf->popup.time = device->grab_time;
529 shsurf->popup.initial_up = 0;
531 wl_input_device_start_grab(shsurf->popup.grab.input_device,
532 &shsurf->popup.grab, shsurf->popup.time);
536 shell_surface_set_popup(struct wl_client *client,
537 struct wl_resource *resource,
538 struct wl_resource *input_device_resource,
540 struct wl_resource *parent_resource,
541 int32_t x, int32_t y, uint32_t flags)
543 struct shell_surface *shsurf = resource->data;
544 struct weston_surface *es = shsurf->surface;
546 weston_surface_damage(es);
547 shsurf->type = SHELL_SURFACE_POPUP;
548 shsurf->parent = parent_resource->data;
553 static const struct wl_shell_surface_interface shell_surface_implementation = {
555 shell_surface_resize,
556 shell_surface_set_toplevel,
557 shell_surface_set_transient,
558 shell_surface_set_fullscreen,
559 shell_surface_set_popup
563 destroy_shell_surface(struct wl_resource *resource)
565 struct shell_surface *shsurf = resource->data;
567 if (shsurf->popup.grab.input_device)
568 wl_input_device_end_grab(shsurf->popup.grab.input_device, 0);
570 /* in case cleaning up a dead client destroys shell_surface first */
572 wl_list_remove(&shsurf->surface_destroy_listener.link);
574 wl_list_remove(&shsurf->link);
579 shell_handle_surface_destroy(struct wl_listener *listener,
580 struct wl_resource *resource, uint32_t time)
582 struct shell_surface *shsurf = container_of(listener,
583 struct shell_surface,
584 surface_destroy_listener);
586 shsurf->surface = NULL;
587 wl_resource_destroy(&shsurf->resource, time);
590 static struct shell_surface *
591 get_shell_surface(struct weston_surface *surface)
593 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
594 struct wl_listener *listener;
596 /* search the destroy listener list for our callback */
597 wl_list_for_each(listener, lst, link) {
598 if (listener->func == shell_handle_surface_destroy) {
599 return container_of(listener, struct shell_surface,
600 surface_destroy_listener);
608 shell_get_shell_surface(struct wl_client *client,
609 struct wl_resource *resource,
611 struct wl_resource *surface_resource)
613 struct weston_surface *surface = surface_resource->data;
614 struct shell_surface *shsurf;
616 if (get_shell_surface(surface)) {
617 wl_resource_post_error(surface_resource,
618 WL_DISPLAY_ERROR_INVALID_OBJECT,
619 "wl_shell::get_shell_surface already requested");
623 shsurf = calloc(1, sizeof *shsurf);
625 wl_resource_post_no_memory(resource);
629 shsurf->resource.destroy = destroy_shell_surface;
630 shsurf->resource.object.id = id;
631 shsurf->resource.object.interface = &wl_shell_surface_interface;
632 shsurf->resource.object.implementation =
633 (void (**)(void)) &shell_surface_implementation;
634 shsurf->resource.data = shsurf;
636 shsurf->surface = surface;
637 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
638 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
639 &shsurf->surface_destroy_listener.link);
641 /* init link so its safe to always remove it in destroy_shell_surface */
642 wl_list_init(&shsurf->link);
644 /* empty when not in use */
645 wl_list_init(&shsurf->rotation.transform.link);
646 weston_matrix_init(&shsurf->rotation.rotation);
648 shsurf->type = SHELL_SURFACE_NONE;
650 wl_client_add_resource(client, &shsurf->resource);
653 static const struct wl_shell_interface shell_implementation = {
654 shell_get_shell_surface
658 handle_screensaver_sigchld(struct weston_process *proc, int status)
664 launch_screensaver(struct wl_shell *shell)
666 if (shell->screensaver.binding)
669 if (!shell->screensaver.path)
672 weston_client_launch(shell->compositor,
673 &shell->screensaver.process,
674 shell->screensaver.path,
675 handle_screensaver_sigchld);
679 terminate_screensaver(struct wl_shell *shell)
681 if (shell->screensaver.process.pid == 0)
684 kill(shell->screensaver.process.pid, SIGTERM);
688 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
690 struct wl_list *list;
692 if (shell->lock_surface)
693 list = &shell->lock_surface->surface->link;
695 list = &shell->compositor->surface_list;
697 wl_list_remove(&surface->surface->link);
698 wl_list_insert(list, &surface->surface->link);
699 weston_surface_configure(surface->surface,
700 surface->surface->geometry.x,
701 surface->surface->geometry.y,
702 surface->surface->geometry.width,
703 surface->surface->geometry.height);
704 surface->surface->output = surface->output;
708 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
710 wl_list_remove(&surface->surface->link);
711 wl_list_init(&surface->surface->link);
712 surface->surface->output = NULL;
716 desktop_shell_set_background(struct wl_client *client,
717 struct wl_resource *resource,
718 struct wl_resource *output_resource,
719 struct wl_resource *surface_resource)
721 struct wl_shell *shell = resource->data;
722 struct shell_surface *shsurf = surface_resource->data;
723 struct weston_surface *surface = shsurf->surface;
724 struct shell_surface *priv;
726 if (reset_shell_surface_type(shsurf))
729 wl_list_for_each(priv, &shell->backgrounds, link) {
730 if (priv->output == output_resource->data) {
731 priv->surface->output = NULL;
732 wl_list_remove(&priv->surface->link);
733 wl_list_remove(&priv->link);
738 shsurf->type = SHELL_SURFACE_BACKGROUND;
739 shsurf->output = output_resource->data;
741 wl_list_insert(&shell->backgrounds, &shsurf->link);
743 surface->geometry.x = shsurf->output->x;
744 surface->geometry.y = shsurf->output->y;
745 surface->geometry.dirty = 1;
747 wl_resource_post_event(resource,
748 DESKTOP_SHELL_CONFIGURE,
749 weston_compositor_get_time(), 0, surface_resource,
750 shsurf->output->current->width,
751 shsurf->output->current->height);
755 desktop_shell_set_panel(struct wl_client *client,
756 struct wl_resource *resource,
757 struct wl_resource *output_resource,
758 struct wl_resource *surface_resource)
760 struct wl_shell *shell = resource->data;
761 struct shell_surface *shsurf = surface_resource->data;
762 struct weston_surface *surface = shsurf->surface;
763 struct shell_surface *priv;
765 if (reset_shell_surface_type(shsurf))
768 wl_list_for_each(priv, &shell->panels, link) {
769 if (priv->output == output_resource->data) {
770 priv->surface->output = NULL;
771 wl_list_remove(&priv->surface->link);
772 wl_list_remove(&priv->link);
777 shsurf->type = SHELL_SURFACE_PANEL;
778 shsurf->output = output_resource->data;
780 wl_list_insert(&shell->panels, &shsurf->link);
782 surface->geometry.x = shsurf->output->x;
783 surface->geometry.y = shsurf->output->y;
784 surface->geometry.dirty = 1;
786 wl_resource_post_event(resource,
787 DESKTOP_SHELL_CONFIGURE,
788 weston_compositor_get_time(), 0, surface_resource,
789 shsurf->output->current->width,
790 shsurf->output->current->height);
794 handle_lock_surface_destroy(struct wl_listener *listener,
795 struct wl_resource *resource, uint32_t time)
797 struct wl_shell *shell =
798 container_of(listener, struct wl_shell, lock_surface_listener);
800 fprintf(stderr, "lock surface gone\n");
801 shell->lock_surface = NULL;
805 desktop_shell_set_lock_surface(struct wl_client *client,
806 struct wl_resource *resource,
807 struct wl_resource *surface_resource)
809 struct wl_shell *shell = resource->data;
810 struct shell_surface *surface = surface_resource->data;
812 if (reset_shell_surface_type(surface))
815 shell->prepare_event_sent = false;
820 shell->lock_surface = surface;
822 shell->lock_surface_listener.func = handle_lock_surface_destroy;
823 wl_list_insert(&surface_resource->destroy_listener_list,
824 &shell->lock_surface_listener.link);
826 shell->lock_surface->type = SHELL_SURFACE_LOCK;
830 resume_desktop(struct wl_shell *shell)
832 struct weston_surface *surface;
833 struct wl_list *list;
834 struct shell_surface *tmp;
836 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
837 hide_screensaver(shell, tmp);
839 terminate_screensaver(shell);
841 wl_list_for_each(surface, &shell->hidden_surface_list, link)
842 weston_surface_configure(surface, surface->geometry.x,
844 surface->geometry.width,
845 surface->geometry.height);
847 if (wl_list_empty(&shell->backgrounds)) {
848 list = &shell->compositor->surface_list;
850 struct shell_surface *background;
851 background = container_of(shell->backgrounds.prev,
852 struct shell_surface, link);
853 list = background->surface->link.prev;
856 if (!wl_list_empty(&shell->hidden_surface_list))
857 wl_list_insert_list(list, &shell->hidden_surface_list);
858 wl_list_init(&shell->hidden_surface_list);
860 shell->locked = false;
861 weston_compositor_repick(shell->compositor);
862 shell->compositor->idle_time = shell->compositor->option_idle_time;
863 weston_compositor_wake(shell->compositor);
867 desktop_shell_unlock(struct wl_client *client,
868 struct wl_resource *resource)
870 struct wl_shell *shell = resource->data;
872 shell->prepare_event_sent = false;
875 resume_desktop(shell);
878 static const struct desktop_shell_interface desktop_shell_implementation = {
879 desktop_shell_set_background,
880 desktop_shell_set_panel,
881 desktop_shell_set_lock_surface,
885 static enum shell_surface_type
886 get_shell_surface_type(struct weston_surface *surface)
888 struct shell_surface *shsurf;
890 shsurf = get_shell_surface(surface);
892 return SHELL_SURFACE_NONE;
897 move_binding(struct wl_input_device *device, uint32_t time,
898 uint32_t key, uint32_t button, uint32_t state, void *data)
900 struct weston_surface *surface =
901 (struct weston_surface *) device->pointer_focus;
906 switch (get_shell_surface_type(surface)) {
907 case SHELL_SURFACE_PANEL:
908 case SHELL_SURFACE_BACKGROUND:
909 case SHELL_SURFACE_FULLSCREEN:
910 case SHELL_SURFACE_SCREENSAVER:
916 weston_surface_move(surface, (struct weston_input_device *) device, time);
920 resize_binding(struct wl_input_device *device, uint32_t time,
921 uint32_t key, uint32_t button, uint32_t state, void *data)
923 struct weston_surface *surface =
924 (struct weston_surface *) device->pointer_focus;
927 struct shell_surface *shsurf;
932 shsurf = get_shell_surface(surface);
936 switch (shsurf->type) {
937 case SHELL_SURFACE_PANEL:
938 case SHELL_SURFACE_BACKGROUND:
939 case SHELL_SURFACE_FULLSCREEN:
940 case SHELL_SURFACE_SCREENSAVER:
946 /* FIXME: convert properly to surface coordinates */
947 x = device->grab_x - surface->geometry.x;
948 y = device->grab_y - surface->geometry.y;
950 if (x < surface->geometry.width / 3)
951 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
952 else if (x < 2 * surface->geometry.width / 3)
955 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
957 if (y < surface->geometry.height / 3)
958 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
959 else if (y < 2 * surface->geometry.height / 3)
962 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
964 weston_surface_resize(shsurf, (struct weston_input_device *) device,
969 terminate_binding(struct wl_input_device *device, uint32_t time,
970 uint32_t key, uint32_t button, uint32_t state, void *data)
972 struct weston_compositor *compositor = data;
975 wl_display_terminate(compositor->wl_display);
979 rotate_grab_motion(struct wl_grab *grab,
980 uint32_t time, int32_t x, int32_t y)
982 struct rotate_grab *rotate =
983 container_of(grab, struct rotate_grab, grab);
984 struct wl_input_device *device = grab->input_device;
985 struct shell_surface *surface = rotate->surface;
986 GLfloat cx = 0.5f * surface->surface->geometry.width;
987 GLfloat cy = 0.5f * surface->surface->geometry.height;
991 dx = device->x - rotate->center.x;
992 dy = device->y - rotate->center.y;
993 r = sqrtf(dx * dx + dy * dy);
995 wl_list_remove(&surface->rotation.transform.link);
996 surface->surface->geometry.dirty = 1;
999 struct weston_matrix *matrix =
1000 &surface->rotation.transform.matrix;
1002 weston_matrix_init(&rotate->rotation);
1003 rotate->rotation.d[0] = dx / r;
1004 rotate->rotation.d[4] = -dy / r;
1005 rotate->rotation.d[1] = -rotate->rotation.d[4];
1006 rotate->rotation.d[5] = rotate->rotation.d[0];
1008 weston_matrix_init(matrix);
1009 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1010 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1011 weston_matrix_multiply(matrix, &rotate->rotation);
1012 weston_matrix_translate(matrix, cx, cy, 0.0f);
1015 &surface->surface->geometry.transformation_list,
1016 &surface->rotation.transform.link);
1018 wl_list_init(&surface->rotation.transform.link);
1019 weston_matrix_init(&surface->rotation.rotation);
1020 weston_matrix_init(&rotate->rotation);
1023 weston_compositor_damage_all(surface->surface->compositor);
1027 rotate_grab_button(struct wl_grab *grab,
1028 uint32_t time, int32_t button, int32_t state)
1030 struct rotate_grab *rotate =
1031 container_of(grab, struct rotate_grab, grab);
1032 struct wl_input_device *device = grab->input_device;
1033 struct shell_surface *surface = rotate->surface;
1035 if (device->button_count == 0 && state == 0) {
1036 weston_matrix_multiply(&surface->rotation.rotation,
1038 wl_input_device_end_grab(device, time);
1043 static const struct wl_grab_interface rotate_grab_interface = {
1050 rotate_binding(struct wl_input_device *device, uint32_t time,
1051 uint32_t key, uint32_t button, uint32_t state, void *data)
1053 struct weston_surface *base_surface =
1054 (struct weston_surface *) device->pointer_focus;
1055 struct shell_surface *surface;
1056 struct rotate_grab *rotate;
1060 if (base_surface == NULL)
1063 surface = get_shell_surface(base_surface);
1067 switch (surface->type) {
1068 case SHELL_SURFACE_PANEL:
1069 case SHELL_SURFACE_BACKGROUND:
1070 case SHELL_SURFACE_FULLSCREEN:
1071 case SHELL_SURFACE_SCREENSAVER:
1077 rotate = malloc(sizeof *rotate);
1081 rotate->grab.interface = &rotate_grab_interface;
1082 rotate->surface = surface;
1084 weston_surface_to_global(surface->surface,
1085 surface->surface->geometry.width / 2,
1086 surface->surface->geometry.height / 2,
1087 &rotate->center.x, &rotate->center.y);
1089 wl_input_device_start_grab(device, &rotate->grab, time);
1091 dx = device->x - rotate->center.x;
1092 dy = device->y - rotate->center.y;
1093 r = sqrtf(dx * dx + dy * dy);
1095 struct weston_matrix inverse;
1097 weston_matrix_init(&inverse);
1098 inverse.d[0] = dx / r;
1099 inverse.d[4] = dy / r;
1100 inverse.d[1] = -inverse.d[4];
1101 inverse.d[5] = inverse.d[0];
1102 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1104 weston_matrix_init(&surface->rotation.rotation);
1105 weston_matrix_init(&rotate->rotation);
1108 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
1112 activate(struct weston_shell *base, struct weston_surface *es,
1113 struct weston_input_device *device, uint32_t time)
1115 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1116 struct weston_compositor *compositor = shell->compositor;
1117 struct wl_list *list;
1119 weston_surface_activate(es, device, time);
1121 if (compositor->wxs)
1122 weston_xserver_surface_activate(es);
1124 switch (get_shell_surface_type(es)) {
1125 case SHELL_SURFACE_BACKGROUND:
1126 /* put background back to bottom */
1127 wl_list_remove(&es->link);
1128 wl_list_insert(compositor->surface_list.prev, &es->link);
1130 case SHELL_SURFACE_PANEL:
1131 /* already put on top */
1133 case SHELL_SURFACE_SCREENSAVER:
1134 /* always below lock surface */
1135 if (shell->lock_surface) {
1136 wl_list_remove(&es->link);
1137 wl_list_insert(&shell->lock_surface->surface->link,
1142 if (!shell->locked) {
1143 list = weston_compositor_top(compositor);
1145 /* bring panel back to top */
1146 struct shell_surface *panel;
1147 wl_list_for_each(panel, &shell->panels, link) {
1148 wl_list_remove(&panel->surface->link);
1149 wl_list_insert(list, &panel->surface->link);
1156 click_to_activate_binding(struct wl_input_device *device,
1157 uint32_t time, uint32_t key,
1158 uint32_t button, uint32_t state, void *data)
1160 struct weston_input_device *wd = (struct weston_input_device *) device;
1161 struct weston_compositor *compositor = data;
1162 struct weston_surface *focus;
1164 focus = (struct weston_surface *) device->pointer_focus;
1165 if (state && focus && device->grab == &device->default_grab)
1166 activate(compositor->shell, focus, wd, time);
1170 lock(struct weston_shell *base)
1172 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1173 struct wl_list *surface_list = &shell->compositor->surface_list;
1174 struct weston_surface *cur;
1175 struct weston_surface *tmp;
1176 struct weston_input_device *device;
1177 struct shell_surface *shsurf;
1183 shell->locked = true;
1185 /* Move all surfaces from compositor's list to our hidden list,
1186 * except the background. This way nothing else can show or
1187 * receive input events while we are locked. */
1189 if (!wl_list_empty(&shell->hidden_surface_list)) {
1191 "%s: Assertion failed: hidden_surface_list is not empty.\n",
1195 wl_list_for_each_safe(cur, tmp, surface_list, link) {
1196 /* skip input device sprites, cur->surface is uninitialised */
1197 if (cur->surface.resource.client == NULL)
1200 if (get_shell_surface_type(cur) == SHELL_SURFACE_BACKGROUND)
1204 wl_list_remove(&cur->link);
1205 wl_list_insert(shell->hidden_surface_list.prev, &cur->link);
1208 launch_screensaver(shell);
1210 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1211 show_screensaver(shell, shsurf);
1213 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1214 shell->compositor->idle_time = shell->screensaver.duration;
1215 weston_compositor_wake(shell->compositor);
1216 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1219 /* reset pointer foci */
1220 weston_compositor_repick(shell->compositor);
1222 /* reset keyboard foci */
1223 time = weston_compositor_get_time();
1224 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1225 wl_input_device_set_keyboard_focus(&device->input_device,
1229 /* TODO: disable bindings that should not work while locked. */
1231 /* All this must be undone in resume_desktop(). */
1235 unlock(struct weston_shell *base)
1237 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1239 if (!shell->locked || shell->lock_surface) {
1240 weston_compositor_wake(shell->compositor);
1244 /* If desktop-shell client has gone away, unlock immediately. */
1245 if (!shell->child.desktop_shell) {
1246 resume_desktop(shell);
1250 if (shell->prepare_event_sent)
1253 wl_resource_post_event(shell->child.desktop_shell,
1254 DESKTOP_SHELL_PREPARE_LOCK_SURFACE);
1255 shell->prepare_event_sent = true;
1259 center_on_output(struct weston_surface *surface, struct weston_output *output)
1261 struct weston_mode *mode = output->current;
1263 surface->geometry.x =
1264 output->x + (mode->width - surface->geometry.width) / 2;
1265 surface->geometry.y =
1266 output->y + (mode->height - surface->geometry.height) / 2;
1267 surface->geometry.dirty = 1;
1271 map(struct weston_shell *base,
1272 struct weston_surface *surface, int32_t width, int32_t height)
1274 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1275 struct weston_compositor *compositor = shell->compositor;
1276 struct wl_list *list;
1277 struct shell_surface *shsurf;
1278 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1281 shsurf = get_shell_surface(surface);
1283 surface_type = shsurf->type;
1285 if (shell->locked) {
1286 list = &shell->hidden_surface_list;
1289 list = weston_compositor_top(compositor);
1293 surface->geometry.width = width;
1294 surface->geometry.height = height;
1295 surface->geometry.dirty = 1;
1297 /* initial positioning, see also configure() */
1298 switch (surface_type) {
1299 case SHELL_SURFACE_TOPLEVEL:
1300 surface->geometry.x = 10 + random() % 400;
1301 surface->geometry.y = 10 + random() % 400;
1302 surface->geometry.dirty = 1;
1304 case SHELL_SURFACE_SCREENSAVER:
1305 case SHELL_SURFACE_FULLSCREEN:
1306 center_on_output(surface, surface->fullscreen_output);
1308 case SHELL_SURFACE_LOCK:
1309 center_on_output(surface, get_default_output(compositor));
1315 /* surface stacking order, see also activate() */
1316 switch (surface_type) {
1317 case SHELL_SURFACE_BACKGROUND:
1318 /* background always visible, at the bottom */
1319 wl_list_insert(compositor->surface_list.prev, &surface->link);
1322 case SHELL_SURFACE_PANEL:
1323 /* panel always on top, hidden while locked */
1324 wl_list_insert(list, &surface->link);
1326 case SHELL_SURFACE_LOCK:
1327 /* lock surface always visible, on top */
1328 wl_list_insert(&compositor->surface_list, &surface->link);
1330 weston_compositor_repick(compositor);
1331 weston_compositor_wake(compositor);
1334 case SHELL_SURFACE_SCREENSAVER:
1335 /* If locked, show it. */
1336 if (shell->locked) {
1337 show_screensaver(shell, shsurf);
1338 compositor->idle_time = shell->screensaver.duration;
1339 weston_compositor_wake(compositor);
1340 if (!shell->lock_surface)
1341 compositor->state = WESTON_COMPOSITOR_IDLE;
1346 /* everything else just below the panel */
1347 if (!wl_list_empty(&shell->panels)) {
1348 struct shell_surface *panel =
1349 container_of(shell->panels.prev,
1350 struct shell_surface, link);
1351 wl_list_insert(&panel->surface->link, &surface->link);
1353 wl_list_insert(list, &surface->link);
1357 switch (surface_type) {
1358 case SHELL_SURFACE_TOPLEVEL:
1359 surface->geometry.x = 10 + random() % 400;
1360 surface->geometry.y = 10 + random() % 400;
1361 surface->geometry.dirty = 1;
1363 case SHELL_SURFACE_POPUP:
1364 shell_map_popup(shsurf, shsurf->popup.time);
1370 surface->geometry.width = width;
1371 surface->geometry.height = height;
1372 surface->geometry.dirty = 1;
1374 weston_surface_configure(surface, surface->geometry.x,
1375 surface->geometry.y,
1377 weston_compositor_repick(compositor);
1380 switch (surface_type) {
1381 case SHELL_SURFACE_TOPLEVEL:
1382 case SHELL_SURFACE_TRANSIENT:
1383 case SHELL_SURFACE_FULLSCREEN:
1385 activate(base, surface,
1386 (struct weston_input_device *)
1387 compositor->input_device,
1388 weston_compositor_get_time());
1394 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1395 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1399 configure(struct weston_shell *base, struct weston_surface *surface,
1400 int32_t x, int32_t y, int32_t width, int32_t height)
1402 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1403 int do_configure = !shell->locked;
1404 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1405 struct shell_surface *shsurf;
1407 shsurf = get_shell_surface(surface);
1409 surface_type = shsurf->type;
1411 surface->geometry.width = width;
1412 surface->geometry.height = height;
1413 surface->geometry.dirty = 1;
1415 switch (surface_type) {
1416 case SHELL_SURFACE_SCREENSAVER:
1417 do_configure = !do_configure;
1419 case SHELL_SURFACE_FULLSCREEN:
1420 center_on_output(surface, surface->fullscreen_output);
1427 * weston_surface_configure() will assign an output, which means
1428 * the surface is supposed to be in compositor->surface_list.
1429 * Be careful with that, and make sure we stay on the right output.
1430 * XXX: would a fullscreen surface need the same handling?
1433 weston_surface_configure(surface, x, y, width, height);
1435 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1436 surface->output = shsurf->output;
1440 static int launch_desktop_shell_process(struct wl_shell *shell);
1443 desktop_shell_sigchld(struct weston_process *process, int status)
1446 struct wl_shell *shell =
1447 container_of(process, struct wl_shell, child.process);
1449 shell->child.process.pid = 0;
1450 shell->child.client = NULL; /* already destroyed by wayland */
1452 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1453 time = weston_compositor_get_time();
1454 if (time - shell->child.deathstamp > 30000) {
1455 shell->child.deathstamp = time;
1456 shell->child.deathcount = 0;
1459 shell->child.deathcount++;
1460 if (shell->child.deathcount > 5) {
1461 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1465 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1466 launch_desktop_shell_process(shell);
1470 launch_desktop_shell_process(struct wl_shell *shell)
1472 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1474 shell->child.client = weston_client_launch(shell->compositor,
1475 &shell->child.process,
1477 desktop_shell_sigchld);
1479 if (!shell->child.client)
1485 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1487 struct wl_shell *shell = data;
1489 wl_client_add_object(client, &wl_shell_interface,
1490 &shell_implementation, id, shell);
1494 unbind_desktop_shell(struct wl_resource *resource)
1496 struct wl_shell *shell = resource->data;
1499 resume_desktop(shell);
1501 shell->child.desktop_shell = NULL;
1502 shell->prepare_event_sent = false;
1507 bind_desktop_shell(struct wl_client *client,
1508 void *data, uint32_t version, uint32_t id)
1510 struct wl_shell *shell = data;
1511 struct wl_resource *resource;
1513 resource = wl_client_add_object(client, &desktop_shell_interface,
1514 &desktop_shell_implementation,
1517 if (client == shell->child.client) {
1518 resource->destroy = unbind_desktop_shell;
1519 shell->child.desktop_shell = resource;
1523 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1524 "permission to bind desktop_shell denied");
1525 wl_resource_destroy(resource, 0);
1529 screensaver_set_surface(struct wl_client *client,
1530 struct wl_resource *resource,
1531 struct wl_resource *shell_surface_resource,
1532 struct wl_resource *output_resource)
1534 struct wl_shell *shell = resource->data;
1535 struct shell_surface *surface = shell_surface_resource->data;
1536 struct weston_output *output = output_resource->data;
1538 if (reset_shell_surface_type(surface))
1541 surface->type = SHELL_SURFACE_SCREENSAVER;
1543 surface->surface->fullscreen_output = output;
1544 surface->output = output;
1545 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1548 static const struct screensaver_interface screensaver_implementation = {
1549 screensaver_set_surface
1553 unbind_screensaver(struct wl_resource *resource)
1555 struct wl_shell *shell = resource->data;
1557 shell->screensaver.binding = NULL;
1562 bind_screensaver(struct wl_client *client,
1563 void *data, uint32_t version, uint32_t id)
1565 struct wl_shell *shell = data;
1566 struct wl_resource *resource;
1568 resource = wl_client_add_object(client, &screensaver_interface,
1569 &screensaver_implementation,
1572 if (shell->screensaver.binding == NULL) {
1573 resource->destroy = unbind_screensaver;
1574 shell->screensaver.binding = resource;
1578 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1579 "interface object already bound");
1580 wl_resource_destroy(resource, 0);
1584 shell_destroy(struct weston_shell *base)
1586 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1588 if (shell->child.client)
1589 wl_client_destroy(shell->child.client);
1591 free(shell->screensaver.path);
1596 shell_init(struct weston_compositor *ec);
1599 shell_init(struct weston_compositor *ec)
1601 struct wl_shell *shell;
1603 shell = malloc(sizeof *shell);
1607 memset(shell, 0, sizeof *shell);
1608 shell->compositor = ec;
1609 shell->shell.lock = lock;
1610 shell->shell.unlock = unlock;
1611 shell->shell.map = map;
1612 shell->shell.configure = configure;
1613 shell->shell.destroy = shell_destroy;
1615 wl_list_init(&shell->hidden_surface_list);
1616 wl_list_init(&shell->backgrounds);
1617 wl_list_init(&shell->panels);
1618 wl_list_init(&shell->screensaver.surfaces);
1620 shell_configuration(shell);
1622 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
1623 shell, bind_shell) == NULL)
1626 if (wl_display_add_global(ec->wl_display,
1627 &desktop_shell_interface,
1628 shell, bind_desktop_shell) == NULL)
1631 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
1632 shell, bind_screensaver) == NULL)
1635 shell->child.deathstamp = weston_compositor_get_time();
1636 if (launch_desktop_shell_process(shell) != 0)
1639 weston_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
1640 move_binding, shell);
1641 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
1642 resize_binding, shell);
1643 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0,
1644 MODIFIER_CTRL | MODIFIER_ALT,
1645 terminate_binding, ec);
1646 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
1647 click_to_activate_binding, ec);
1648 weston_compositor_add_binding(ec, 0, BTN_LEFT,
1649 MODIFIER_SUPER | MODIFIER_ALT,
1650 rotate_binding, NULL);
1652 ec->shell = &shell->shell;