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>
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;
45 struct weston_layer fullscreen_layer;
46 struct weston_layer panel_layer;
47 struct weston_layer toplevel_layer;
48 struct weston_layer background_layer;
49 struct weston_layer lock_layer;
52 struct weston_process process;
53 struct wl_client *client;
54 struct wl_resource *desktop_shell;
61 bool prepare_event_sent;
63 struct shell_surface *lock_surface;
64 struct wl_listener lock_surface_listener;
66 struct wl_list backgrounds;
67 struct wl_list panels;
72 struct wl_resource *binding;
73 struct wl_list surfaces;
74 struct weston_process process;
77 struct weston_surface *debug_repaint_surface;
80 enum shell_surface_type {
84 SHELL_SURFACE_BACKGROUND,
86 SHELL_SURFACE_SCREENSAVER,
88 SHELL_SURFACE_TOPLEVEL,
89 SHELL_SURFACE_TRANSIENT,
90 SHELL_SURFACE_FULLSCREEN,
91 SHELL_SURFACE_MAXIMIZED,
95 struct shell_surface {
96 struct wl_resource resource;
98 struct weston_surface *surface;
99 struct wl_listener surface_destroy_listener;
100 struct shell_surface *parent;
102 enum shell_surface_type type;
103 int32_t saved_x, saved_y;
104 bool saved_position_valid;
107 struct weston_transform transform;
108 struct weston_matrix rotation;
112 struct wl_pointer_grab grab;
115 struct weston_transform parent_transform;
120 enum wl_shell_surface_fullscreen_method type;
121 struct weston_transform transform; /* matrix from x, y */
123 struct weston_surface *black_surface;
126 struct weston_output *fullscreen_output;
127 struct weston_output *output;
133 struct weston_move_grab {
134 struct wl_pointer_grab grab;
135 struct weston_surface *surface;
140 struct wl_pointer_grab grab;
141 struct shell_surface *surface;
142 struct weston_matrix rotation;
150 center_on_output(struct weston_surface *surface,
151 struct weston_output *output);
154 shell_configuration(struct wl_shell *shell)
160 struct config_key saver_keys[] = {
161 { "path", CONFIG_KEY_STRING, &path },
162 { "duration", CONFIG_KEY_INTEGER, &duration },
165 struct config_section cs[] = {
166 { "screensaver", saver_keys, ARRAY_LENGTH(saver_keys), NULL },
169 config_file = config_file_path("weston.ini");
170 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
173 shell->screensaver.path = path;
174 shell->screensaver.duration = duration;
178 noop_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
179 struct wl_surface *surface, int32_t x, int32_t y)
185 move_grab_motion(struct wl_pointer_grab *grab,
186 uint32_t time, int32_t x, int32_t y)
188 struct weston_move_grab *move = (struct weston_move_grab *) grab;
189 struct wl_input_device *device = grab->input_device;
190 struct weston_surface *es = move->surface;
192 weston_surface_configure(es,
193 device->x + move->dx,
194 device->y + move->dy,
195 es->geometry.width, es->geometry.height);
199 move_grab_button(struct wl_pointer_grab *grab,
200 uint32_t time, int32_t button, int32_t state)
202 struct wl_input_device *device = grab->input_device;
204 if (device->button_count == 0 && state == 0) {
205 wl_input_device_end_pointer_grab(device, time);
210 static const struct wl_pointer_grab_interface move_grab_interface = {
217 weston_surface_move(struct weston_surface *es,
218 struct weston_input_device *wd, uint32_t time)
220 struct weston_move_grab *move;
222 move = malloc(sizeof *move);
226 move->grab.interface = &move_grab_interface;
227 move->dx = es->geometry.x - wd->input_device.grab_x;
228 move->dy = es->geometry.y - wd->input_device.grab_y;
231 wl_input_device_start_pointer_grab(&wd->input_device, &move->grab, time);
233 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
239 shell_surface_move(struct wl_client *client, struct wl_resource *resource,
240 struct wl_resource *input_resource, uint32_t time)
242 struct weston_input_device *wd = input_resource->data;
243 struct shell_surface *shsurf = resource->data;
245 if (wd->input_device.button_count == 0 ||
246 wd->input_device.grab_time != time ||
247 wd->input_device.pointer_focus != &shsurf->surface->surface)
250 if (weston_surface_move(shsurf->surface, wd, time) < 0)
251 wl_resource_post_no_memory(resource);
254 struct weston_resize_grab {
255 struct wl_pointer_grab grab;
257 int32_t width, height;
258 struct shell_surface *shsurf;
262 resize_grab_motion(struct wl_pointer_grab *grab,
263 uint32_t time, int32_t x, int32_t y)
265 struct weston_resize_grab *resize = (struct weston_resize_grab *) grab;
266 struct wl_input_device *device = grab->input_device;
267 int32_t width, height;
268 int32_t from_x, from_y;
271 weston_surface_from_global(resize->shsurf->surface,
272 device->grab_x, device->grab_y,
274 weston_surface_from_global(resize->shsurf->surface,
275 device->x, device->y, &to_x, &to_y);
277 if (resize->edges & WL_SHELL_SURFACE_RESIZE_LEFT) {
278 width = resize->width + from_x - to_x;
279 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_RIGHT) {
280 width = resize->width + to_x - from_x;
282 width = resize->width;
285 if (resize->edges & WL_SHELL_SURFACE_RESIZE_TOP) {
286 height = resize->height + from_y - to_y;
287 } else if (resize->edges & WL_SHELL_SURFACE_RESIZE_BOTTOM) {
288 height = resize->height + to_y - from_y;
290 height = resize->height;
293 wl_shell_surface_send_configure(&resize->shsurf->resource,
294 time, resize->edges, width, height);
298 resize_grab_button(struct wl_pointer_grab *grab,
299 uint32_t time, int32_t button, int32_t state)
301 struct wl_input_device *device = grab->input_device;
303 if (device->button_count == 0 && state == 0) {
304 wl_input_device_end_pointer_grab(device, time);
309 static const struct wl_pointer_grab_interface resize_grab_interface = {
316 weston_surface_resize(struct shell_surface *shsurf,
317 struct weston_input_device *wd,
318 uint32_t time, uint32_t edges)
320 struct weston_resize_grab *resize;
322 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
325 if (edges == 0 || edges > 15 ||
326 (edges & 3) == 3 || (edges & 12) == 12)
329 resize = malloc(sizeof *resize);
333 resize->grab.interface = &resize_grab_interface;
334 resize->edges = edges;
335 resize->width = shsurf->surface->geometry.width;
336 resize->height = shsurf->surface->geometry.height;
337 resize->shsurf = shsurf;
339 wl_input_device_start_pointer_grab(&wd->input_device, &resize->grab, time);
341 wl_input_device_set_pointer_focus(&wd->input_device, NULL, time, 0, 0);
347 shell_surface_resize(struct wl_client *client, struct wl_resource *resource,
348 struct wl_resource *input_resource, uint32_t time,
351 struct weston_input_device *wd = input_resource->data;
352 struct shell_surface *shsurf = resource->data;
354 if (shsurf->type == SHELL_SURFACE_FULLSCREEN)
357 if (wd->input_device.button_count == 0 ||
358 wd->input_device.grab_time != time ||
359 wd->input_device.pointer_focus != &shsurf->surface->surface)
362 if (weston_surface_resize(shsurf, wd, time, edges) < 0)
363 wl_resource_post_no_memory(resource);
366 static struct weston_output *
367 get_default_output(struct weston_compositor *compositor)
369 return container_of(compositor->output_list.next,
370 struct weston_output, link);
374 shell_unset_fullscreen(struct shell_surface *shsurf)
376 /* undo all fullscreen things here */
377 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
378 shsurf->fullscreen.framerate = 0;
379 wl_list_remove(&shsurf->fullscreen.transform.link);
380 wl_list_init(&shsurf->fullscreen.transform.link);
381 weston_surface_destroy(shsurf->fullscreen.black_surface);
382 shsurf->fullscreen.black_surface = NULL;
383 shsurf->fullscreen_output = NULL;
384 shsurf->force_configure = 1;
385 weston_surface_set_position(shsurf->surface,
386 shsurf->saved_x, shsurf->saved_y);
390 reset_shell_surface_type(struct shell_surface *surface)
392 switch (surface->type) {
393 case SHELL_SURFACE_FULLSCREEN:
394 shell_unset_fullscreen(surface);
396 case SHELL_SURFACE_MAXIMIZED:
397 surface->output = get_default_output(surface->surface->compositor);
398 weston_surface_set_position(surface->surface,
402 case SHELL_SURFACE_PANEL:
403 case SHELL_SURFACE_BACKGROUND:
404 wl_list_remove(&surface->link);
405 wl_list_init(&surface->link);
407 case SHELL_SURFACE_SCREENSAVER:
408 case SHELL_SURFACE_LOCK:
409 wl_resource_post_error(&surface->resource,
410 WL_DISPLAY_ERROR_INVALID_METHOD,
411 "cannot reassign surface type");
413 case SHELL_SURFACE_NONE:
414 case SHELL_SURFACE_TOPLEVEL:
415 case SHELL_SURFACE_TRANSIENT:
416 case SHELL_SURFACE_POPUP:
420 surface->type = SHELL_SURFACE_NONE;
425 shell_surface_set_toplevel(struct wl_client *client,
426 struct wl_resource *resource)
429 struct shell_surface *surface = resource->data;
431 if (reset_shell_surface_type(surface))
434 surface->type = SHELL_SURFACE_TOPLEVEL;
438 shell_surface_set_transient(struct wl_client *client,
439 struct wl_resource *resource,
440 struct wl_resource *parent_resource,
441 int x, int y, uint32_t flags)
443 struct shell_surface *shsurf = resource->data;
444 struct weston_surface *es = shsurf->surface;
445 struct shell_surface *pshsurf = parent_resource->data;
446 struct weston_surface *pes = pshsurf->surface;
448 if (reset_shell_surface_type(shsurf))
451 /* assign to parents output */
452 shsurf->output = pes->output;
453 weston_surface_set_position(es, pes->geometry.x + x,
454 pes->geometry.y + y);
456 shsurf->type = SHELL_SURFACE_TRANSIENT;
459 static struct wl_shell *
460 shell_surface_get_shell(struct shell_surface *shsurf)
462 struct weston_surface *es = shsurf->surface;
463 struct weston_shell *shell = es->compositor->shell;
465 return (struct wl_shell *)container_of(shell, struct wl_shell, shell);
469 get_output_panel_height(struct wl_shell *wlshell,struct weston_output *output)
471 struct shell_surface *priv;
472 int panel_height = 0;
477 wl_list_for_each(priv, &wlshell->panels, link) {
478 if (priv->output == output) {
479 panel_height = priv->surface->geometry.height;
487 shell_surface_set_maximized(struct wl_client *client,
488 struct wl_resource *resource,
489 struct wl_resource *output_resource )
491 struct shell_surface *shsurf = resource->data;
492 struct weston_surface *es = shsurf->surface;
493 struct wl_shell *wlshell = NULL;
494 uint32_t edges = 0, panel_height = 0;
496 /* get the default output, if the client set it as NULL
497 check whether the ouput is available */
499 shsurf->output = output_resource->data;
501 shsurf->output = get_default_output(es->compositor);
503 if (reset_shell_surface_type(shsurf))
506 shsurf->saved_x = es->geometry.x;
507 shsurf->saved_y = es->geometry.y;
508 shsurf->saved_position_valid = true;
510 wlshell = shell_surface_get_shell(shsurf);
511 panel_height = get_output_panel_height(wlshell, es->output);
512 edges = WL_SHELL_SURFACE_RESIZE_TOP|WL_SHELL_SURFACE_RESIZE_LEFT;
514 wl_shell_surface_send_configure(&shsurf->resource,
515 weston_compositor_get_time(), edges,
516 es->output->current->width,
517 es->output->current->height - panel_height);
519 shsurf->type = SHELL_SURFACE_MAXIMIZED;
522 static struct weston_surface *
523 create_black_surface(struct weston_compositor *ec,
524 GLfloat x, GLfloat y, int w, int h)
526 struct weston_surface *surface = NULL;
528 surface = weston_surface_create(ec);
529 if (surface == NULL) {
530 fprintf(stderr, "no memory\n");
534 weston_surface_configure(surface, x, y, w, h);
535 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
539 /* Create black surface and append it to the associated fullscreen surface.
540 * Handle size dismatch and positioning according to the method. */
542 shell_configure_fullscreen(struct shell_surface *shsurf)
544 struct weston_output *output = shsurf->fullscreen_output;
545 struct weston_surface *surface = shsurf->surface;
546 struct weston_matrix *matrix;
549 center_on_output(surface, output);
551 if (!shsurf->fullscreen.black_surface)
552 shsurf->fullscreen.black_surface =
553 create_black_surface(surface->compositor,
554 output->x, output->y,
555 output->current->width,
556 output->current->height);
558 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
559 wl_list_insert(&surface->layer_link,
560 &shsurf->fullscreen.black_surface->layer_link);
561 shsurf->fullscreen.black_surface->output = output;
563 switch (shsurf->fullscreen.type) {
564 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
566 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
567 matrix = &shsurf->fullscreen.transform.matrix;
568 weston_matrix_init(matrix);
569 scale = (float)output->current->width/(float)surface->geometry.width;
570 weston_matrix_scale(matrix, scale, scale, 1);
571 wl_list_remove(&shsurf->fullscreen.transform.link);
572 wl_list_insert(surface->geometry.transformation_list.prev,
573 &shsurf->fullscreen.transform.link);
574 weston_surface_set_position(surface, output->x, output->y);
576 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
578 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
585 /* make the fullscreen and black surface at the top */
587 shell_stack_fullscreen(struct shell_surface *shsurf)
589 struct weston_surface *surface = shsurf->surface;
590 struct wl_shell *shell = shell_surface_get_shell(shsurf);
592 wl_list_remove(&surface->layer_link);
593 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
595 wl_list_insert(&shell->fullscreen_layer.surface_list,
596 &surface->layer_link);
597 wl_list_insert(&surface->layer_link,
598 &shsurf->fullscreen.black_surface->layer_link);
600 weston_surface_damage(surface);
601 weston_surface_damage(shsurf->fullscreen.black_surface);
605 shell_map_fullscreen(struct shell_surface *shsurf)
607 shell_configure_fullscreen(shsurf);
608 shell_stack_fullscreen(shsurf);
612 shell_surface_set_fullscreen(struct wl_client *client,
613 struct wl_resource *resource,
616 struct wl_resource *output_resource)
618 struct shell_surface *shsurf = resource->data;
619 struct weston_surface *es = shsurf->surface;
622 shsurf->output = output_resource->data;
624 shsurf->output = get_default_output(es->compositor);
626 if (reset_shell_surface_type(shsurf))
629 shsurf->fullscreen_output = shsurf->output;
630 shsurf->fullscreen.type = method;
631 shsurf->fullscreen.framerate = framerate;
632 shsurf->type = SHELL_SURFACE_FULLSCREEN;
634 shsurf->saved_x = es->geometry.x;
635 shsurf->saved_y = es->geometry.y;
636 shsurf->saved_position_valid = true;
638 if (weston_surface_is_mapped(es))
639 shsurf->force_configure = 1;
641 wl_shell_surface_send_configure(&shsurf->resource,
642 weston_compositor_get_time(), 0,
643 shsurf->output->current->width,
644 shsurf->output->current->height);
648 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
649 struct wl_surface *surface, int32_t x, int32_t y)
651 struct wl_input_device *device = grab->input_device;
652 struct shell_surface *priv =
653 container_of(grab, struct shell_surface, popup.grab);
654 struct wl_client *client = priv->surface->surface.resource.client;
656 if (surface && surface->resource.client == client) {
657 wl_input_device_set_pointer_focus(device, surface, time, x, y);
658 grab->focus = surface;
660 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
666 popup_grab_motion(struct wl_pointer_grab *grab,
667 uint32_t time, int32_t sx, int32_t sy)
669 struct wl_resource *resource;
671 resource = grab->input_device->pointer_focus_resource;
673 wl_input_device_send_motion(resource, time, sx, sy);
677 popup_grab_button(struct wl_pointer_grab *grab,
678 uint32_t time, int32_t button, int32_t state)
680 struct wl_resource *resource;
681 struct shell_surface *shsurf =
682 container_of(grab, struct shell_surface, popup.grab);
684 resource = grab->input_device->pointer_focus_resource;
686 wl_input_device_send_button(resource, time, button, state);
687 } else if (state == 0 &&
688 (shsurf->popup.initial_up ||
689 time - shsurf->popup.time > 500)) {
690 wl_shell_surface_send_popup_done(&shsurf->resource);
691 wl_input_device_end_pointer_grab(grab->input_device, time);
692 shsurf->popup.grab.input_device = NULL;
696 shsurf->popup.initial_up = 1;
699 static const struct wl_pointer_grab_interface popup_grab_interface = {
706 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
708 struct wl_input_device *device;
709 struct weston_surface *es = shsurf->surface;
710 struct weston_surface *parent = shsurf->parent->surface;
712 es->output = parent->output;
714 shsurf->popup.grab.interface = &popup_grab_interface;
715 device = es->compositor->input_device;
717 weston_surface_update_transform(parent);
718 if (parent->transform.enabled) {
719 shsurf->popup.parent_transform.matrix =
720 parent->transform.matrix;
722 /* construct x, y translation matrix */
723 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
724 shsurf->popup.parent_transform.matrix.d[12] =
726 shsurf->popup.parent_transform.matrix.d[13] =
729 wl_list_insert(es->geometry.transformation_list.prev,
730 &shsurf->popup.parent_transform.link);
731 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
733 shsurf->popup.grab.input_device = device;
734 shsurf->popup.time = device->grab_time;
735 shsurf->popup.initial_up = 0;
737 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
738 &shsurf->popup.grab, shsurf->popup.time);
742 shell_surface_set_popup(struct wl_client *client,
743 struct wl_resource *resource,
744 struct wl_resource *input_device_resource,
746 struct wl_resource *parent_resource,
747 int32_t x, int32_t y, uint32_t flags)
749 struct shell_surface *shsurf = resource->data;
751 shsurf->type = SHELL_SURFACE_POPUP;
752 shsurf->parent = parent_resource->data;
757 static const struct wl_shell_surface_interface shell_surface_implementation = {
759 shell_surface_resize,
760 shell_surface_set_toplevel,
761 shell_surface_set_transient,
762 shell_surface_set_fullscreen,
763 shell_surface_set_popup,
764 shell_surface_set_maximized
768 destroy_shell_surface(struct wl_resource *resource)
770 struct shell_surface *shsurf = resource->data;
772 if (shsurf->popup.grab.input_device)
773 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
775 /* in case cleaning up a dead client destroys shell_surface first */
776 if (shsurf->surface) {
777 wl_list_remove(&shsurf->surface_destroy_listener.link);
778 shsurf->surface->configure = NULL;
781 if (shsurf->fullscreen.black_surface)
782 weston_surface_destroy(shsurf->fullscreen.black_surface);
784 wl_list_remove(&shsurf->link);
789 shell_handle_surface_destroy(struct wl_listener *listener,
790 struct wl_resource *resource, uint32_t time)
792 struct shell_surface *shsurf = container_of(listener,
793 struct shell_surface,
794 surface_destroy_listener);
796 shsurf->surface = NULL;
797 wl_resource_destroy(&shsurf->resource, time);
800 static struct shell_surface *
801 get_shell_surface(struct weston_surface *surface)
803 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
804 struct wl_listener *listener;
806 /* search the destroy listener list for our callback */
807 wl_list_for_each(listener, lst, link) {
808 if (listener->func == shell_handle_surface_destroy) {
809 return container_of(listener, struct shell_surface,
810 surface_destroy_listener);
818 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
821 shell_get_shell_surface(struct wl_client *client,
822 struct wl_resource *resource,
824 struct wl_resource *surface_resource)
826 struct weston_surface *surface = surface_resource->data;
827 struct shell_surface *shsurf;
829 if (get_shell_surface(surface)) {
830 wl_resource_post_error(surface_resource,
831 WL_DISPLAY_ERROR_INVALID_OBJECT,
832 "wl_shell::get_shell_surface already requested");
836 if (surface->configure) {
837 wl_resource_post_error(surface_resource,
838 WL_DISPLAY_ERROR_INVALID_OBJECT,
839 "surface->configure already set");
843 shsurf = calloc(1, sizeof *shsurf);
845 wl_resource_post_no_memory(resource);
849 surface->configure = shell_surface_configure;
851 shsurf->resource.destroy = destroy_shell_surface;
852 shsurf->resource.object.id = id;
853 shsurf->resource.object.interface = &wl_shell_surface_interface;
854 shsurf->resource.object.implementation =
855 (void (**)(void)) &shell_surface_implementation;
856 shsurf->resource.data = shsurf;
858 shsurf->saved_position_valid = false;
859 shsurf->surface = surface;
860 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
861 shsurf->fullscreen.framerate = 0;
862 shsurf->fullscreen.black_surface = NULL;
863 wl_list_init(&shsurf->fullscreen.transform.link);
865 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
866 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
867 &shsurf->surface_destroy_listener.link);
869 /* init link so its safe to always remove it in destroy_shell_surface */
870 wl_list_init(&shsurf->link);
872 /* empty when not in use */
873 wl_list_init(&shsurf->rotation.transform.link);
874 weston_matrix_init(&shsurf->rotation.rotation);
876 shsurf->type = SHELL_SURFACE_NONE;
878 wl_client_add_resource(client, &shsurf->resource);
881 static const struct wl_shell_interface shell_implementation = {
882 shell_get_shell_surface
886 handle_screensaver_sigchld(struct weston_process *proc, int status)
892 launch_screensaver(struct wl_shell *shell)
894 if (shell->screensaver.binding)
897 if (!shell->screensaver.path)
900 if (shell->screensaver.process.pid != 0) {
901 fprintf(stderr, "old screensaver still running\n");
905 weston_client_launch(shell->compositor,
906 &shell->screensaver.process,
907 shell->screensaver.path,
908 handle_screensaver_sigchld);
912 terminate_screensaver(struct wl_shell *shell)
914 if (shell->screensaver.process.pid == 0)
917 kill(shell->screensaver.process.pid, SIGTERM);
921 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
923 struct wl_list *list;
925 if (shell->lock_surface)
926 list = &shell->lock_surface->surface->layer_link;
928 list = &shell->lock_layer.surface_list;
930 wl_list_remove(&surface->surface->layer_link);
931 wl_list_insert(list, &surface->surface->layer_link);
932 surface->surface->output = surface->output;
933 weston_surface_damage(surface->surface);
937 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
939 wl_list_remove(&surface->surface->layer_link);
940 wl_list_init(&surface->surface->layer_link);
941 surface->surface->output = NULL;
945 desktop_shell_set_background(struct wl_client *client,
946 struct wl_resource *resource,
947 struct wl_resource *output_resource,
948 struct wl_resource *surface_resource)
950 struct wl_shell *shell = resource->data;
951 struct shell_surface *shsurf = surface_resource->data;
952 struct weston_surface *surface = shsurf->surface;
953 struct shell_surface *priv;
955 if (reset_shell_surface_type(shsurf))
958 wl_list_for_each(priv, &shell->backgrounds, link) {
959 if (priv->output == output_resource->data) {
960 priv->surface->output = NULL;
961 wl_list_remove(&priv->surface->layer_link);
962 wl_list_remove(&priv->link);
967 shsurf->type = SHELL_SURFACE_BACKGROUND;
968 shsurf->output = output_resource->data;
970 wl_list_insert(&shell->backgrounds, &shsurf->link);
972 weston_surface_set_position(surface, shsurf->output->x,
975 desktop_shell_send_configure(resource,
976 weston_compositor_get_time(), 0,
978 shsurf->output->current->width,
979 shsurf->output->current->height);
983 desktop_shell_set_panel(struct wl_client *client,
984 struct wl_resource *resource,
985 struct wl_resource *output_resource,
986 struct wl_resource *surface_resource)
988 struct wl_shell *shell = resource->data;
989 struct shell_surface *shsurf = surface_resource->data;
990 struct weston_surface *surface = shsurf->surface;
991 struct shell_surface *priv;
993 if (reset_shell_surface_type(shsurf))
996 wl_list_for_each(priv, &shell->panels, link) {
997 if (priv->output == output_resource->data) {
998 priv->surface->output = NULL;
999 wl_list_remove(&priv->surface->layer_link);
1000 wl_list_remove(&priv->link);
1005 shsurf->type = SHELL_SURFACE_PANEL;
1006 shsurf->output = output_resource->data;
1008 wl_list_insert(&shell->panels, &shsurf->link);
1010 weston_surface_set_position(surface, shsurf->output->x,
1013 desktop_shell_send_configure(resource,
1014 weston_compositor_get_time(), 0,
1016 shsurf->output->current->width,
1017 shsurf->output->current->height);
1021 handle_lock_surface_destroy(struct wl_listener *listener,
1022 struct wl_resource *resource, uint32_t time)
1024 struct wl_shell *shell =
1025 container_of(listener, struct wl_shell, lock_surface_listener);
1027 fprintf(stderr, "lock surface gone\n");
1028 shell->lock_surface = NULL;
1032 desktop_shell_set_lock_surface(struct wl_client *client,
1033 struct wl_resource *resource,
1034 struct wl_resource *surface_resource)
1036 struct wl_shell *shell = resource->data;
1037 struct shell_surface *surface = surface_resource->data;
1039 if (reset_shell_surface_type(surface))
1042 shell->prepare_event_sent = false;
1047 shell->lock_surface = surface;
1049 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1050 wl_list_insert(&surface_resource->destroy_listener_list,
1051 &shell->lock_surface_listener.link);
1053 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1057 resume_desktop(struct wl_shell *shell)
1059 struct shell_surface *tmp;
1061 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1062 hide_screensaver(shell, tmp);
1064 terminate_screensaver(shell);
1066 wl_list_remove(&shell->lock_layer.link);
1067 wl_list_insert(&shell->compositor->cursor_layer.link,
1068 &shell->fullscreen_layer.link);
1069 wl_list_insert(&shell->fullscreen_layer.link,
1070 &shell->panel_layer.link);
1071 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1073 shell->locked = false;
1074 shell->compositor->idle_time = shell->compositor->option_idle_time;
1075 weston_compositor_wake(shell->compositor);
1076 weston_compositor_damage_all(shell->compositor);
1080 desktop_shell_unlock(struct wl_client *client,
1081 struct wl_resource *resource)
1083 struct wl_shell *shell = resource->data;
1085 shell->prepare_event_sent = false;
1088 resume_desktop(shell);
1091 static const struct desktop_shell_interface desktop_shell_implementation = {
1092 desktop_shell_set_background,
1093 desktop_shell_set_panel,
1094 desktop_shell_set_lock_surface,
1095 desktop_shell_unlock
1098 static enum shell_surface_type
1099 get_shell_surface_type(struct weston_surface *surface)
1101 struct shell_surface *shsurf;
1103 shsurf = get_shell_surface(surface);
1105 return SHELL_SURFACE_NONE;
1106 return shsurf->type;
1110 move_binding(struct wl_input_device *device, uint32_t time,
1111 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1113 struct weston_surface *surface =
1114 (struct weston_surface *) device->pointer_focus;
1116 if (surface == NULL)
1119 switch (get_shell_surface_type(surface)) {
1120 case SHELL_SURFACE_PANEL:
1121 case SHELL_SURFACE_BACKGROUND:
1122 case SHELL_SURFACE_FULLSCREEN:
1123 case SHELL_SURFACE_SCREENSAVER:
1129 weston_surface_move(surface, (struct weston_input_device *) device, time);
1133 resize_binding(struct wl_input_device *device, uint32_t time,
1134 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1136 struct weston_surface *surface =
1137 (struct weston_surface *) device->pointer_focus;
1140 struct shell_surface *shsurf;
1142 if (surface == NULL)
1145 shsurf = get_shell_surface(surface);
1149 switch (shsurf->type) {
1150 case SHELL_SURFACE_PANEL:
1151 case SHELL_SURFACE_BACKGROUND:
1152 case SHELL_SURFACE_FULLSCREEN:
1153 case SHELL_SURFACE_SCREENSAVER:
1159 weston_surface_from_global(surface,
1160 device->grab_x, device->grab_y, &x, &y);
1162 if (x < surface->geometry.width / 3)
1163 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1164 else if (x < 2 * surface->geometry.width / 3)
1167 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1169 if (y < surface->geometry.height / 3)
1170 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1171 else if (y < 2 * surface->geometry.height / 3)
1174 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1176 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1181 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1182 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1185 struct shell_surface *shsurf;
1186 struct weston_surface *surface =
1187 (struct weston_surface *) device->pointer_focus;
1189 if (surface == NULL)
1192 shsurf = get_shell_surface(surface);
1196 switch (shsurf->type) {
1197 case SHELL_SURFACE_BACKGROUND:
1198 case SHELL_SURFACE_SCREENSAVER:
1204 surface->alpha += value * step;
1206 if (surface->alpha > 255)
1207 surface->alpha = 255;
1208 if (surface->alpha < step)
1209 surface->alpha = step;
1211 surface->geometry.dirty = 1;
1212 weston_surface_damage(surface);
1216 zoom_binding(struct wl_input_device *device, uint32_t time,
1217 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1219 struct weston_input_device *wd = (struct weston_input_device *) device;
1220 struct weston_compositor *compositor = wd->compositor;
1221 struct weston_output *output;
1223 wl_list_for_each(output, &compositor->output_list, link) {
1224 if (pixman_region32_contains_point(&output->region,
1225 device->x, device->y, NULL)) {
1226 output->zoom.active = 1;
1227 output->zoom.level += output->zoom.increment * -value;
1229 if (output->zoom.level >= 1.0) {
1230 output->zoom.active = 0;
1231 output->zoom.level = 1.0;
1234 if (output->zoom.level < output->zoom.increment)
1235 output->zoom.level = output->zoom.increment;
1237 weston_output_update_zoom(output, device->x, device->y);
1243 terminate_binding(struct wl_input_device *device, uint32_t time,
1244 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1246 struct weston_compositor *compositor = data;
1249 wl_display_terminate(compositor->wl_display);
1253 rotate_grab_motion(struct wl_pointer_grab *grab,
1254 uint32_t time, int32_t x, int32_t y)
1256 struct rotate_grab *rotate =
1257 container_of(grab, struct rotate_grab, grab);
1258 struct wl_input_device *device = grab->input_device;
1259 struct shell_surface *surface = rotate->surface;
1260 struct weston_surface *base_surface = surface->surface;
1261 GLfloat cx = 0.5f * base_surface->geometry.width;
1262 GLfloat cy = 0.5f * base_surface->geometry.height;
1263 GLfloat dx, dy, cposx, cposy, dposx, dposy;
1266 dx = device->x - rotate->center.x;
1267 dy = device->y - rotate->center.y;
1268 r = sqrtf(dx * dx + dy * dy);
1270 wl_list_remove(&surface->rotation.transform.link);
1271 surface->surface->geometry.dirty = 1;
1274 struct weston_matrix *matrix =
1275 &surface->rotation.transform.matrix;
1277 weston_matrix_init(&rotate->rotation);
1278 rotate->rotation.d[0] = dx / r;
1279 rotate->rotation.d[4] = -dy / r;
1280 rotate->rotation.d[1] = -rotate->rotation.d[4];
1281 rotate->rotation.d[5] = rotate->rotation.d[0];
1283 weston_matrix_init(matrix);
1284 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1285 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1286 weston_matrix_multiply(matrix, &rotate->rotation);
1287 weston_matrix_translate(matrix, cx, cy, 0.0f);
1290 &surface->surface->geometry.transformation_list,
1291 &surface->rotation.transform.link);
1293 wl_list_init(&surface->rotation.transform.link);
1294 weston_matrix_init(&surface->rotation.rotation);
1295 weston_matrix_init(&rotate->rotation);
1298 /* We need to adjust the position of the surface
1299 * in case it was resized in a rotated state before */
1300 cposx = base_surface->geometry.x + cx;
1301 cposy = base_surface->geometry.y + cy;
1302 dposx = rotate->center.x - cposx;
1303 dposy = rotate->center.y - cposy;
1304 if (dposx != 0.0f || dposy != 0.0f) {
1305 weston_surface_set_position(base_surface,
1306 base_surface->geometry.x + dposx,
1307 base_surface->geometry.y + dposy);
1310 /* Repaint implies weston_surface_update_transform(), which
1311 * lazily applies the damage due to rotation update.
1313 weston_compositor_schedule_repaint(surface->surface->compositor);
1317 rotate_grab_button(struct wl_pointer_grab *grab,
1318 uint32_t time, int32_t button, int32_t state)
1320 struct rotate_grab *rotate =
1321 container_of(grab, struct rotate_grab, grab);
1322 struct wl_input_device *device = grab->input_device;
1323 struct shell_surface *surface = rotate->surface;
1325 if (device->button_count == 0 && state == 0) {
1326 weston_matrix_multiply(&surface->rotation.rotation,
1328 wl_input_device_end_pointer_grab(device, time);
1333 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1340 rotate_binding(struct wl_input_device *device, uint32_t time,
1341 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1343 struct weston_surface *base_surface =
1344 (struct weston_surface *) device->pointer_focus;
1345 struct shell_surface *surface;
1346 struct rotate_grab *rotate;
1350 if (base_surface == NULL)
1353 surface = get_shell_surface(base_surface);
1357 switch (surface->type) {
1358 case SHELL_SURFACE_PANEL:
1359 case SHELL_SURFACE_BACKGROUND:
1360 case SHELL_SURFACE_FULLSCREEN:
1361 case SHELL_SURFACE_SCREENSAVER:
1367 rotate = malloc(sizeof *rotate);
1371 rotate->grab.interface = &rotate_grab_interface;
1372 rotate->surface = surface;
1374 weston_surface_to_global(surface->surface,
1375 surface->surface->geometry.width / 2,
1376 surface->surface->geometry.height / 2,
1377 &rotate->center.x, &rotate->center.y);
1379 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1381 dx = device->x - rotate->center.x;
1382 dy = device->y - rotate->center.y;
1383 r = sqrtf(dx * dx + dy * dy);
1385 struct weston_matrix inverse;
1387 weston_matrix_init(&inverse);
1388 inverse.d[0] = dx / r;
1389 inverse.d[4] = dy / r;
1390 inverse.d[1] = -inverse.d[4];
1391 inverse.d[5] = inverse.d[0];
1392 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1394 weston_matrix_init(&rotate->rotation);
1395 rotate->rotation.d[0] = dx / r;
1396 rotate->rotation.d[4] = -dy / r;
1397 rotate->rotation.d[1] = -rotate->rotation.d[4];
1398 rotate->rotation.d[5] = rotate->rotation.d[0];
1400 weston_matrix_init(&surface->rotation.rotation);
1401 weston_matrix_init(&rotate->rotation);
1404 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1408 activate(struct weston_shell *base, struct weston_surface *es,
1409 struct weston_input_device *device, uint32_t time)
1411 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1412 struct weston_compositor *compositor = shell->compositor;
1414 weston_surface_activate(es, device, time);
1416 if (compositor->wxs)
1417 weston_xserver_surface_activate(es);
1419 switch (get_shell_surface_type(es)) {
1420 case SHELL_SURFACE_BACKGROUND:
1421 case SHELL_SURFACE_PANEL:
1422 case SHELL_SURFACE_LOCK:
1425 case SHELL_SURFACE_SCREENSAVER:
1426 /* always below lock surface */
1427 if (shell->lock_surface)
1428 weston_surface_restack(es,
1429 &shell->lock_surface->surface->layer_link);
1431 case SHELL_SURFACE_FULLSCREEN:
1432 /* should on top of panels */
1435 weston_surface_restack(es,
1436 &shell->toplevel_layer.surface_list);
1442 click_to_activate_binding(struct wl_input_device *device,
1443 uint32_t time, uint32_t key,
1444 uint32_t button, uint32_t axis, int32_t state, void *data)
1446 struct weston_input_device *wd = (struct weston_input_device *) device;
1447 struct weston_compositor *compositor = data;
1448 struct weston_surface *focus;
1449 struct weston_surface *upper;
1451 focus = (struct weston_surface *) device->pointer_focus;
1455 upper = container_of(focus->link.prev, struct weston_surface, link);
1456 if (focus->link.prev != &compositor->surface_list &&
1457 get_shell_surface_type(upper) == SHELL_SURFACE_FULLSCREEN) {
1458 printf("%s: focus is black surface, raise its fullscreen surface\n", __func__);
1459 shell_stack_fullscreen(get_shell_surface(upper));
1463 if (state && device->pointer_grab == &device->default_pointer_grab)
1464 activate(compositor->shell, focus, wd, time);
1468 lock(struct weston_shell *base)
1470 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1471 struct weston_input_device *device;
1472 struct shell_surface *shsurf;
1473 struct weston_output *output;
1476 if (shell->locked) {
1477 wl_list_for_each(output, &shell->compositor->output_list, link)
1478 /* TODO: find a way to jump to other DPMS levels */
1479 if (output->set_dpms)
1480 output->set_dpms(output, WESTON_DPMS_STANDBY);
1484 shell->locked = true;
1486 /* Hide all surfaces by removing the fullscreen, panel and
1487 * toplevel layers. This way nothing else can show or receive
1488 * input events while we are locked. */
1490 wl_list_remove(&shell->panel_layer.link);
1491 wl_list_remove(&shell->toplevel_layer.link);
1492 wl_list_remove(&shell->fullscreen_layer.link);
1493 wl_list_insert(&shell->compositor->cursor_layer.link,
1494 &shell->lock_layer.link);
1496 launch_screensaver(shell);
1498 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1499 show_screensaver(shell, shsurf);
1501 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1502 shell->compositor->idle_time = shell->screensaver.duration;
1503 weston_compositor_wake(shell->compositor);
1504 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1507 /* reset pointer foci */
1508 weston_compositor_schedule_repaint(shell->compositor);
1510 /* reset keyboard foci */
1511 time = weston_compositor_get_time();
1512 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1513 wl_input_device_set_keyboard_focus(&device->input_device,
1517 /* TODO: disable bindings that should not work while locked. */
1519 /* All this must be undone in resume_desktop(). */
1523 unlock(struct weston_shell *base)
1525 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1527 if (!shell->locked || shell->lock_surface) {
1528 weston_compositor_wake(shell->compositor);
1532 /* If desktop-shell client has gone away, unlock immediately. */
1533 if (!shell->child.desktop_shell) {
1534 resume_desktop(shell);
1538 if (shell->prepare_event_sent)
1541 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1542 shell->prepare_event_sent = true;
1546 center_on_output(struct weston_surface *surface, struct weston_output *output)
1548 struct weston_mode *mode = output->current;
1549 GLfloat x = (mode->width - surface->geometry.width) / 2;
1550 GLfloat y = (mode->height - surface->geometry.height) / 2;
1552 weston_surface_set_position(surface, output->x + x, output->y + y);
1556 map(struct weston_shell *base, struct weston_surface *surface,
1557 int32_t width, int32_t height, int32_t sx, int32_t sy)
1559 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1560 struct weston_compositor *compositor = shell->compositor;
1561 struct shell_surface *shsurf;
1562 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1563 struct weston_surface *parent;
1564 int panel_height = 0;
1566 shsurf = get_shell_surface(surface);
1568 surface_type = shsurf->type;
1570 surface->geometry.width = width;
1571 surface->geometry.height = height;
1572 surface->geometry.dirty = 1;
1574 /* initial positioning, see also configure() */
1575 switch (surface_type) {
1576 case SHELL_SURFACE_TOPLEVEL:
1577 weston_surface_set_position(surface, 10 + random() % 400,
1578 10 + random() % 400);
1580 case SHELL_SURFACE_SCREENSAVER:
1581 center_on_output(surface, shsurf->fullscreen_output);
1583 case SHELL_SURFACE_FULLSCREEN:
1584 shell_map_fullscreen(shsurf);
1586 case SHELL_SURFACE_MAXIMIZED:
1587 /* use surface configure to set the geometry */
1588 panel_height = get_output_panel_height(shell,surface->output);
1589 weston_surface_set_position(surface, surface->output->x,
1590 surface->output->y + panel_height);
1592 case SHELL_SURFACE_LOCK:
1593 center_on_output(surface, get_default_output(compositor));
1595 case SHELL_SURFACE_POPUP:
1596 shell_map_popup(shsurf, shsurf->popup.time);
1597 case SHELL_SURFACE_NONE:
1598 weston_surface_set_position(surface,
1599 surface->geometry.x + sx,
1600 surface->geometry.y + sy);
1606 /* surface stacking order, see also activate() */
1607 switch (surface_type) {
1608 case SHELL_SURFACE_BACKGROUND:
1609 /* background always visible, at the bottom */
1610 wl_list_insert(&shell->background_layer.surface_list,
1611 &surface->layer_link);
1613 case SHELL_SURFACE_PANEL:
1614 /* panel always on top, hidden while locked */
1615 wl_list_insert(&shell->panel_layer.surface_list,
1616 &surface->layer_link);
1618 case SHELL_SURFACE_LOCK:
1619 /* lock surface always visible, on top */
1620 wl_list_insert(&shell->lock_layer.surface_list,
1621 &surface->layer_link);
1622 weston_compositor_wake(compositor);
1624 case SHELL_SURFACE_SCREENSAVER:
1625 /* If locked, show it. */
1626 if (shell->locked) {
1627 show_screensaver(shell, shsurf);
1628 compositor->idle_time = shell->screensaver.duration;
1629 weston_compositor_wake(compositor);
1630 if (!shell->lock_surface)
1631 compositor->state = WESTON_COMPOSITOR_IDLE;
1634 case SHELL_SURFACE_POPUP:
1635 case SHELL_SURFACE_TRANSIENT:
1636 parent = shsurf->parent->surface;
1637 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1639 case SHELL_SURFACE_FULLSCREEN:
1640 case SHELL_SURFACE_NONE:
1643 wl_list_insert(&shell->toplevel_layer.surface_list,
1644 &surface->layer_link);
1648 if (surface_type != SHELL_SURFACE_NONE) {
1649 weston_surface_assign_output(surface);
1650 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1651 surface->output = shsurf->output;
1654 switch (surface_type) {
1655 case SHELL_SURFACE_TOPLEVEL:
1656 case SHELL_SURFACE_TRANSIENT:
1657 case SHELL_SURFACE_FULLSCREEN:
1658 case SHELL_SURFACE_MAXIMIZED:
1660 activate(base, surface,
1661 (struct weston_input_device *)
1662 compositor->input_device,
1663 weston_compositor_get_time());
1669 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1670 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1674 configure(struct weston_shell *base, struct weston_surface *surface,
1675 GLfloat x, GLfloat y, int32_t width, int32_t height)
1677 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1678 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1679 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1680 struct shell_surface *shsurf;
1682 shsurf = get_shell_surface(surface);
1684 surface_type = shsurf->type;
1686 surface->geometry.x = x;
1687 surface->geometry.y = y;
1688 surface->geometry.width = width;
1689 surface->geometry.height = height;
1690 surface->geometry.dirty = 1;
1692 switch (surface_type) {
1693 case SHELL_SURFACE_SCREENSAVER:
1694 center_on_output(surface, shsurf->fullscreen_output);
1696 case SHELL_SURFACE_FULLSCREEN:
1697 shell_configure_fullscreen(shsurf);
1698 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1699 shell_stack_fullscreen(shsurf);
1701 case SHELL_SURFACE_MAXIMIZED:
1702 /* setting x, y and using configure to change that geometry */
1703 surface->geometry.x = surface->output->x;
1704 surface->geometry.y = surface->output->y +
1705 get_output_panel_height(shell,surface->output);
1707 case SHELL_SURFACE_TOPLEVEL:
1713 /* XXX: would a fullscreen surface need the same handling? */
1714 if (surface->output) {
1715 weston_surface_assign_output(surface);
1717 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1718 surface->output = shsurf->output;
1719 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1720 surface->output = shsurf->output;
1725 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1727 struct weston_shell *shell = es->compositor->shell;
1728 struct shell_surface *shsurf = get_shell_surface(es);
1730 if (!weston_surface_is_mapped(es)) {
1731 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
1732 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
1733 es->geometry.width != es->buffer->width ||
1734 es->geometry.height != es->buffer->height) {
1735 GLfloat from_x, from_y;
1738 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1739 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1740 configure(shell, es,
1741 es->geometry.x + to_x - from_x,
1742 es->geometry.y + to_y - from_y,
1743 es->buffer->width, es->buffer->height);
1744 shsurf->force_configure = 0;
1748 static int launch_desktop_shell_process(struct wl_shell *shell);
1751 desktop_shell_sigchld(struct weston_process *process, int status)
1754 struct wl_shell *shell =
1755 container_of(process, struct wl_shell, child.process);
1757 shell->child.process.pid = 0;
1758 shell->child.client = NULL; /* already destroyed by wayland */
1760 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1761 time = weston_compositor_get_time();
1762 if (time - shell->child.deathstamp > 30000) {
1763 shell->child.deathstamp = time;
1764 shell->child.deathcount = 0;
1767 shell->child.deathcount++;
1768 if (shell->child.deathcount > 5) {
1769 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1773 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1774 launch_desktop_shell_process(shell);
1778 launch_desktop_shell_process(struct wl_shell *shell)
1780 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1782 shell->child.client = weston_client_launch(shell->compositor,
1783 &shell->child.process,
1785 desktop_shell_sigchld);
1787 if (!shell->child.client)
1793 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1795 struct wl_shell *shell = data;
1797 wl_client_add_object(client, &wl_shell_interface,
1798 &shell_implementation, id, shell);
1802 unbind_desktop_shell(struct wl_resource *resource)
1804 struct wl_shell *shell = resource->data;
1807 resume_desktop(shell);
1809 shell->child.desktop_shell = NULL;
1810 shell->prepare_event_sent = false;
1815 bind_desktop_shell(struct wl_client *client,
1816 void *data, uint32_t version, uint32_t id)
1818 struct wl_shell *shell = data;
1819 struct wl_resource *resource;
1821 resource = wl_client_add_object(client, &desktop_shell_interface,
1822 &desktop_shell_implementation,
1825 if (client == shell->child.client) {
1826 resource->destroy = unbind_desktop_shell;
1827 shell->child.desktop_shell = resource;
1831 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1832 "permission to bind desktop_shell denied");
1833 wl_resource_destroy(resource, 0);
1837 screensaver_set_surface(struct wl_client *client,
1838 struct wl_resource *resource,
1839 struct wl_resource *shell_surface_resource,
1840 struct wl_resource *output_resource)
1842 struct wl_shell *shell = resource->data;
1843 struct shell_surface *surface = shell_surface_resource->data;
1844 struct weston_output *output = output_resource->data;
1846 if (reset_shell_surface_type(surface))
1849 surface->type = SHELL_SURFACE_SCREENSAVER;
1851 surface->fullscreen_output = output;
1852 surface->output = output;
1853 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1856 static const struct screensaver_interface screensaver_implementation = {
1857 screensaver_set_surface
1861 unbind_screensaver(struct wl_resource *resource)
1863 struct wl_shell *shell = resource->data;
1865 shell->screensaver.binding = NULL;
1870 bind_screensaver(struct wl_client *client,
1871 void *data, uint32_t version, uint32_t id)
1873 struct wl_shell *shell = data;
1874 struct wl_resource *resource;
1876 resource = wl_client_add_object(client, &screensaver_interface,
1877 &screensaver_implementation,
1880 if (shell->screensaver.binding == NULL) {
1881 resource->destroy = unbind_screensaver;
1882 shell->screensaver.binding = resource;
1886 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1887 "interface object already bound");
1888 wl_resource_destroy(resource, 0);
1892 struct weston_compositor *compositor;
1893 struct weston_surface *current;
1894 struct wl_listener listener;
1895 struct wl_keyboard_grab grab;
1899 switcher_next(struct switcher *switcher)
1901 struct weston_compositor *compositor = switcher->compositor;
1902 struct weston_surface *surface;
1903 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1905 wl_list_for_each(surface, &compositor->surface_list, link) {
1906 switch (get_shell_surface_type(surface)) {
1907 case SHELL_SURFACE_TOPLEVEL:
1908 case SHELL_SURFACE_FULLSCREEN:
1909 case SHELL_SURFACE_MAXIMIZED:
1912 if (prev == switcher->current)
1915 surface->alpha = 64;
1916 surface->geometry.dirty = 1;
1917 weston_surface_damage(surface);
1930 wl_list_remove(&switcher->listener.link);
1931 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1932 &switcher->listener.link);
1934 switcher->current = next;
1939 switcher_handle_surface_destroy(struct wl_listener *listener,
1940 struct wl_resource *resource, uint32_t time)
1942 struct switcher *switcher =
1943 container_of(listener, struct switcher, listener);
1945 switcher_next(switcher);
1949 switcher_destroy(struct switcher *switcher, uint32_t time)
1951 struct weston_compositor *compositor = switcher->compositor;
1952 struct weston_surface *surface;
1953 struct weston_input_device *device =
1954 (struct weston_input_device *) switcher->grab.input_device;
1956 wl_list_for_each(surface, &compositor->surface_list, link) {
1957 surface->alpha = 255;
1958 weston_surface_damage(surface);
1961 if (switcher->current)
1962 activate(compositor->shell, switcher->current, device, time);
1963 wl_list_remove(&switcher->listener.link);
1964 wl_input_device_end_keyboard_grab(&device->input_device, time);
1969 switcher_key(struct wl_keyboard_grab *grab,
1970 uint32_t time, uint32_t key, int32_t state)
1972 struct switcher *switcher = container_of(grab, struct switcher, grab);
1973 struct weston_input_device *device =
1974 (struct weston_input_device *) grab->input_device;
1976 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
1977 switcher_destroy(switcher, time);
1978 } else if (key == KEY_TAB && state) {
1979 switcher_next(switcher);
1983 static const struct wl_keyboard_grab_interface switcher_grab = {
1988 switcher_binding(struct wl_input_device *device, uint32_t time,
1989 uint32_t key, uint32_t button, uint32_t axis,
1990 int32_t state, void *data)
1992 struct weston_compositor *compositor = data;
1993 struct switcher *switcher;
1995 switcher = malloc(sizeof *switcher);
1996 switcher->compositor = compositor;
1997 switcher->current = NULL;
1998 switcher->listener.func = switcher_handle_surface_destroy;
1999 wl_list_init(&switcher->listener.link);
2001 switcher->grab.interface = &switcher_grab;
2002 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
2003 wl_input_device_set_keyboard_focus(device, NULL,
2004 weston_compositor_get_time());
2005 switcher_next(switcher);
2009 backlight_binding(struct wl_input_device *device, uint32_t time,
2010 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2012 struct weston_compositor *compositor = data;
2013 struct weston_output *output;
2014 long backlight_new = 0;
2016 /* TODO: we're limiting to simple use cases, where we assume just
2017 * control on the primary display. We'd have to extend later if we
2018 * ever get support for setting backlights on random desktop LCD
2020 output = get_default_output(compositor);
2024 if (!output->set_backlight)
2027 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2028 backlight_new = output->backlight_current - 25;
2029 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2030 backlight_new = output->backlight_current + 25;
2032 if (backlight_new < 5)
2034 if (backlight_new > 255)
2035 backlight_new = 255;
2037 output->backlight_current = backlight_new;
2038 output->set_backlight(output, output->backlight_current);
2042 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2043 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2045 struct weston_compositor *compositor = data;
2046 struct wl_shell *shell =
2047 container_of(compositor->shell, struct wl_shell, shell);
2048 struct weston_surface *surface;
2050 if (shell->debug_repaint_surface) {
2051 weston_surface_destroy(shell->debug_repaint_surface);
2052 shell->debug_repaint_surface = NULL;
2054 surface = weston_surface_create(compositor);
2055 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2056 weston_surface_configure(surface, 0, 0, 8192, 8192);
2057 wl_list_insert(&compositor->fade_layer.surface_list,
2058 &surface->layer_link);
2059 weston_surface_assign_output(surface);
2060 pixman_region32_init(&surface->input);
2062 /* Here's the dirty little trick that makes the
2063 * repaint debugging work: we force an
2064 * update_transform first to update dependent state
2065 * and clear the geometry.dirty bit. Then we clear
2066 * the surface damage so it only gets repainted
2067 * piecewise as we repaint other things. */
2069 weston_surface_update_transform(surface);
2070 pixman_region32_fini(&surface->damage);
2071 pixman_region32_init(&surface->damage);
2072 shell->debug_repaint_surface = surface;
2077 shell_destroy(struct weston_shell *base)
2079 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2081 if (shell->child.client)
2082 wl_client_destroy(shell->child.client);
2084 free(shell->screensaver.path);
2089 shell_init(struct weston_compositor *ec);
2092 shell_init(struct weston_compositor *ec)
2094 struct wl_shell *shell;
2096 shell = malloc(sizeof *shell);
2100 memset(shell, 0, sizeof *shell);
2101 shell->compositor = ec;
2102 shell->shell.lock = lock;
2103 shell->shell.unlock = unlock;
2104 shell->shell.destroy = shell_destroy;
2106 wl_list_init(&shell->backgrounds);
2107 wl_list_init(&shell->panels);
2108 wl_list_init(&shell->screensaver.surfaces);
2110 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2111 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2112 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2113 weston_layer_init(&shell->background_layer,
2114 &shell->toplevel_layer.link);
2115 wl_list_init(&shell->lock_layer.surface_list);
2117 shell_configuration(shell);
2119 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2120 shell, bind_shell) == NULL)
2123 if (wl_display_add_global(ec->wl_display,
2124 &desktop_shell_interface,
2125 shell, bind_desktop_shell) == NULL)
2128 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2129 shell, bind_screensaver) == NULL)
2132 shell->child.deathstamp = weston_compositor_get_time();
2133 if (launch_desktop_shell_process(shell) != 0)
2136 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2137 move_binding, shell);
2138 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2139 resize_binding, shell);
2140 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2141 MODIFIER_CTRL | MODIFIER_ALT,
2142 terminate_binding, ec);
2143 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2144 click_to_activate_binding, ec);
2145 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2146 MODIFIER_SUPER | MODIFIER_ALT,
2147 surface_opacity_binding, NULL);
2148 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2149 MODIFIER_SUPER, zoom_binding, NULL);
2150 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2151 MODIFIER_SUPER | MODIFIER_ALT,
2152 rotate_binding, NULL);
2153 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2154 switcher_binding, ec);
2157 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2158 backlight_binding, ec);
2159 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2160 backlight_binding, ec);
2161 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2162 backlight_binding, ec);
2163 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2164 backlight_binding, ec);
2166 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2167 debug_repaint_binding, ec);
2169 ec->shell = &shell->shell;