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, uint32_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, uint32_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;
523 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy);
525 static struct weston_surface *
526 create_black_surface(struct weston_compositor *ec,
527 struct weston_surface *fs_surface,
528 GLfloat x, GLfloat y, int w, int h)
530 struct weston_surface *surface = NULL;
532 surface = weston_surface_create(ec);
533 if (surface == NULL) {
534 fprintf(stderr, "no memory\n");
538 surface->configure = black_surface_configure;
539 surface->private = fs_surface;
540 weston_surface_configure(surface, x, y, w, h);
541 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 1);
545 /* Create black surface and append it to the associated fullscreen surface.
546 * Handle size dismatch and positioning according to the method. */
548 shell_configure_fullscreen(struct shell_surface *shsurf)
550 struct weston_output *output = shsurf->fullscreen_output;
551 struct weston_surface *surface = shsurf->surface;
552 struct weston_matrix *matrix;
555 center_on_output(surface, output);
557 if (!shsurf->fullscreen.black_surface)
558 shsurf->fullscreen.black_surface =
559 create_black_surface(surface->compositor,
561 output->x, output->y,
562 output->current->width,
563 output->current->height);
565 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
566 wl_list_insert(&surface->layer_link,
567 &shsurf->fullscreen.black_surface->layer_link);
568 shsurf->fullscreen.black_surface->output = output;
570 switch (shsurf->fullscreen.type) {
571 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT:
573 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE:
574 matrix = &shsurf->fullscreen.transform.matrix;
575 weston_matrix_init(matrix);
576 scale = (float)output->current->width/(float)surface->geometry.width;
577 weston_matrix_scale(matrix, scale, scale, 1);
578 wl_list_remove(&shsurf->fullscreen.transform.link);
579 wl_list_insert(surface->geometry.transformation_list.prev,
580 &shsurf->fullscreen.transform.link);
581 weston_surface_set_position(surface, output->x, output->y);
583 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER:
585 case WL_SHELL_SURFACE_FULLSCREEN_METHOD_FILL:
592 /* make the fullscreen and black surface at the top */
594 shell_stack_fullscreen(struct shell_surface *shsurf)
596 struct weston_surface *surface = shsurf->surface;
597 struct wl_shell *shell = shell_surface_get_shell(shsurf);
599 wl_list_remove(&surface->layer_link);
600 wl_list_remove(&shsurf->fullscreen.black_surface->layer_link);
602 wl_list_insert(&shell->fullscreen_layer.surface_list,
603 &surface->layer_link);
604 wl_list_insert(&surface->layer_link,
605 &shsurf->fullscreen.black_surface->layer_link);
607 weston_surface_damage(surface);
608 weston_surface_damage(shsurf->fullscreen.black_surface);
612 shell_map_fullscreen(struct shell_surface *shsurf)
614 shell_configure_fullscreen(shsurf);
615 shell_stack_fullscreen(shsurf);
619 shell_surface_set_fullscreen(struct wl_client *client,
620 struct wl_resource *resource,
623 struct wl_resource *output_resource)
625 struct shell_surface *shsurf = resource->data;
626 struct weston_surface *es = shsurf->surface;
629 shsurf->output = output_resource->data;
631 shsurf->output = get_default_output(es->compositor);
633 if (reset_shell_surface_type(shsurf))
636 shsurf->fullscreen_output = shsurf->output;
637 shsurf->fullscreen.type = method;
638 shsurf->fullscreen.framerate = framerate;
639 shsurf->type = SHELL_SURFACE_FULLSCREEN;
641 shsurf->saved_x = es->geometry.x;
642 shsurf->saved_y = es->geometry.y;
643 shsurf->saved_position_valid = true;
645 if (weston_surface_is_mapped(es))
646 shsurf->force_configure = 1;
648 wl_shell_surface_send_configure(&shsurf->resource,
649 weston_compositor_get_time(), 0,
650 shsurf->output->current->width,
651 shsurf->output->current->height);
655 popup_grab_focus(struct wl_pointer_grab *grab, uint32_t time,
656 struct wl_surface *surface, int32_t x, int32_t y)
658 struct wl_input_device *device = grab->input_device;
659 struct shell_surface *priv =
660 container_of(grab, struct shell_surface, popup.grab);
661 struct wl_client *client = priv->surface->surface.resource.client;
663 if (surface && surface->resource.client == client) {
664 wl_input_device_set_pointer_focus(device, surface, time, x, y);
665 grab->focus = surface;
667 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
673 popup_grab_motion(struct wl_pointer_grab *grab,
674 uint32_t time, int32_t sx, int32_t sy)
676 struct wl_resource *resource;
678 resource = grab->input_device->pointer_focus_resource;
680 wl_input_device_send_motion(resource, time, sx, sy);
684 popup_grab_button(struct wl_pointer_grab *grab,
685 uint32_t time, uint32_t button, int32_t state)
687 struct wl_resource *resource;
688 struct shell_surface *shsurf =
689 container_of(grab, struct shell_surface, popup.grab);
691 resource = grab->input_device->pointer_focus_resource;
693 wl_input_device_send_button(resource, time, button, state);
694 } else if (state == 0 &&
695 (shsurf->popup.initial_up ||
696 time - shsurf->popup.time > 500)) {
697 wl_shell_surface_send_popup_done(&shsurf->resource);
698 wl_input_device_end_pointer_grab(grab->input_device, time);
699 shsurf->popup.grab.input_device = NULL;
703 shsurf->popup.initial_up = 1;
706 static const struct wl_pointer_grab_interface popup_grab_interface = {
713 shell_map_popup(struct shell_surface *shsurf, uint32_t time)
715 struct wl_input_device *device;
716 struct weston_surface *es = shsurf->surface;
717 struct weston_surface *parent = shsurf->parent->surface;
719 es->output = parent->output;
721 shsurf->popup.grab.interface = &popup_grab_interface;
722 device = es->compositor->input_device;
724 weston_surface_update_transform(parent);
725 if (parent->transform.enabled) {
726 shsurf->popup.parent_transform.matrix =
727 parent->transform.matrix;
729 /* construct x, y translation matrix */
730 weston_matrix_init(&shsurf->popup.parent_transform.matrix);
731 shsurf->popup.parent_transform.matrix.d[12] =
733 shsurf->popup.parent_transform.matrix.d[13] =
736 wl_list_insert(es->geometry.transformation_list.prev,
737 &shsurf->popup.parent_transform.link);
738 weston_surface_set_position(es, shsurf->popup.x, shsurf->popup.y);
740 shsurf->popup.grab.input_device = device;
741 shsurf->popup.time = device->grab_time;
742 shsurf->popup.initial_up = 0;
744 wl_input_device_start_pointer_grab(shsurf->popup.grab.input_device,
745 &shsurf->popup.grab, shsurf->popup.time);
749 shell_surface_set_popup(struct wl_client *client,
750 struct wl_resource *resource,
751 struct wl_resource *input_device_resource,
753 struct wl_resource *parent_resource,
754 int32_t x, int32_t y, uint32_t flags)
756 struct shell_surface *shsurf = resource->data;
758 shsurf->type = SHELL_SURFACE_POPUP;
759 shsurf->parent = parent_resource->data;
764 static const struct wl_shell_surface_interface shell_surface_implementation = {
766 shell_surface_resize,
767 shell_surface_set_toplevel,
768 shell_surface_set_transient,
769 shell_surface_set_fullscreen,
770 shell_surface_set_popup,
771 shell_surface_set_maximized
775 destroy_shell_surface(struct wl_resource *resource)
777 struct shell_surface *shsurf = resource->data;
779 if (shsurf->popup.grab.input_device)
780 wl_input_device_end_pointer_grab(shsurf->popup.grab.input_device, 0);
782 /* in case cleaning up a dead client destroys shell_surface first */
783 if (shsurf->surface) {
784 wl_list_remove(&shsurf->surface_destroy_listener.link);
785 shsurf->surface->configure = NULL;
788 if (shsurf->fullscreen.black_surface)
789 weston_surface_destroy(shsurf->fullscreen.black_surface);
791 wl_list_remove(&shsurf->link);
796 shell_handle_surface_destroy(struct wl_listener *listener,
797 struct wl_resource *resource, uint32_t time)
799 struct shell_surface *shsurf = container_of(listener,
800 struct shell_surface,
801 surface_destroy_listener);
803 shsurf->surface = NULL;
804 wl_resource_destroy(&shsurf->resource, time);
807 static struct shell_surface *
808 get_shell_surface(struct weston_surface *surface)
810 struct wl_list *lst = &surface->surface.resource.destroy_listener_list;
811 struct wl_listener *listener;
813 /* search the destroy listener list for our callback */
814 wl_list_for_each(listener, lst, link) {
815 if (listener->func == shell_handle_surface_destroy) {
816 return container_of(listener, struct shell_surface,
817 surface_destroy_listener);
825 shell_surface_configure(struct weston_surface *, int32_t, int32_t);
828 shell_get_shell_surface(struct wl_client *client,
829 struct wl_resource *resource,
831 struct wl_resource *surface_resource)
833 struct weston_surface *surface = surface_resource->data;
834 struct shell_surface *shsurf;
836 if (get_shell_surface(surface)) {
837 wl_resource_post_error(surface_resource,
838 WL_DISPLAY_ERROR_INVALID_OBJECT,
839 "wl_shell::get_shell_surface already requested");
843 if (surface->configure) {
844 wl_resource_post_error(surface_resource,
845 WL_DISPLAY_ERROR_INVALID_OBJECT,
846 "surface->configure already set");
850 shsurf = calloc(1, sizeof *shsurf);
852 wl_resource_post_no_memory(resource);
856 surface->configure = shell_surface_configure;
858 shsurf->resource.destroy = destroy_shell_surface;
859 shsurf->resource.object.id = id;
860 shsurf->resource.object.interface = &wl_shell_surface_interface;
861 shsurf->resource.object.implementation =
862 (void (**)(void)) &shell_surface_implementation;
863 shsurf->resource.data = shsurf;
865 shsurf->saved_position_valid = false;
866 shsurf->surface = surface;
867 shsurf->fullscreen.type = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
868 shsurf->fullscreen.framerate = 0;
869 shsurf->fullscreen.black_surface = NULL;
870 wl_list_init(&shsurf->fullscreen.transform.link);
872 shsurf->surface_destroy_listener.func = shell_handle_surface_destroy;
873 wl_list_insert(surface->surface.resource.destroy_listener_list.prev,
874 &shsurf->surface_destroy_listener.link);
876 /* init link so its safe to always remove it in destroy_shell_surface */
877 wl_list_init(&shsurf->link);
879 /* empty when not in use */
880 wl_list_init(&shsurf->rotation.transform.link);
881 weston_matrix_init(&shsurf->rotation.rotation);
883 shsurf->type = SHELL_SURFACE_NONE;
885 wl_client_add_resource(client, &shsurf->resource);
888 static const struct wl_shell_interface shell_implementation = {
889 shell_get_shell_surface
893 handle_screensaver_sigchld(struct weston_process *proc, int status)
899 launch_screensaver(struct wl_shell *shell)
901 if (shell->screensaver.binding)
904 if (!shell->screensaver.path)
907 if (shell->screensaver.process.pid != 0) {
908 fprintf(stderr, "old screensaver still running\n");
912 weston_client_launch(shell->compositor,
913 &shell->screensaver.process,
914 shell->screensaver.path,
915 handle_screensaver_sigchld);
919 terminate_screensaver(struct wl_shell *shell)
921 if (shell->screensaver.process.pid == 0)
924 kill(shell->screensaver.process.pid, SIGTERM);
928 show_screensaver(struct wl_shell *shell, struct shell_surface *surface)
930 struct wl_list *list;
932 if (shell->lock_surface)
933 list = &shell->lock_surface->surface->layer_link;
935 list = &shell->lock_layer.surface_list;
937 wl_list_remove(&surface->surface->layer_link);
938 wl_list_insert(list, &surface->surface->layer_link);
939 surface->surface->output = surface->output;
940 weston_surface_damage(surface->surface);
944 hide_screensaver(struct wl_shell *shell, struct shell_surface *surface)
946 wl_list_remove(&surface->surface->layer_link);
947 wl_list_init(&surface->surface->layer_link);
948 surface->surface->output = NULL;
952 desktop_shell_set_background(struct wl_client *client,
953 struct wl_resource *resource,
954 struct wl_resource *output_resource,
955 struct wl_resource *surface_resource)
957 struct wl_shell *shell = resource->data;
958 struct shell_surface *shsurf = surface_resource->data;
959 struct weston_surface *surface = shsurf->surface;
960 struct shell_surface *priv;
962 if (reset_shell_surface_type(shsurf))
965 wl_list_for_each(priv, &shell->backgrounds, link) {
966 if (priv->output == output_resource->data) {
967 priv->surface->output = NULL;
968 wl_list_remove(&priv->surface->layer_link);
969 wl_list_remove(&priv->link);
974 shsurf->type = SHELL_SURFACE_BACKGROUND;
975 shsurf->output = output_resource->data;
977 wl_list_insert(&shell->backgrounds, &shsurf->link);
979 weston_surface_set_position(surface, shsurf->output->x,
982 desktop_shell_send_configure(resource,
983 weston_compositor_get_time(), 0,
985 shsurf->output->current->width,
986 shsurf->output->current->height);
990 desktop_shell_set_panel(struct wl_client *client,
991 struct wl_resource *resource,
992 struct wl_resource *output_resource,
993 struct wl_resource *surface_resource)
995 struct wl_shell *shell = resource->data;
996 struct shell_surface *shsurf = surface_resource->data;
997 struct weston_surface *surface = shsurf->surface;
998 struct shell_surface *priv;
1000 if (reset_shell_surface_type(shsurf))
1003 wl_list_for_each(priv, &shell->panels, link) {
1004 if (priv->output == output_resource->data) {
1005 priv->surface->output = NULL;
1006 wl_list_remove(&priv->surface->layer_link);
1007 wl_list_remove(&priv->link);
1012 shsurf->type = SHELL_SURFACE_PANEL;
1013 shsurf->output = output_resource->data;
1015 wl_list_insert(&shell->panels, &shsurf->link);
1017 weston_surface_set_position(surface, shsurf->output->x,
1020 desktop_shell_send_configure(resource,
1021 weston_compositor_get_time(), 0,
1023 shsurf->output->current->width,
1024 shsurf->output->current->height);
1028 handle_lock_surface_destroy(struct wl_listener *listener,
1029 struct wl_resource *resource, uint32_t time)
1031 struct wl_shell *shell =
1032 container_of(listener, struct wl_shell, lock_surface_listener);
1034 fprintf(stderr, "lock surface gone\n");
1035 shell->lock_surface = NULL;
1039 desktop_shell_set_lock_surface(struct wl_client *client,
1040 struct wl_resource *resource,
1041 struct wl_resource *surface_resource)
1043 struct wl_shell *shell = resource->data;
1044 struct shell_surface *surface = surface_resource->data;
1046 if (reset_shell_surface_type(surface))
1049 shell->prepare_event_sent = false;
1054 shell->lock_surface = surface;
1056 shell->lock_surface_listener.func = handle_lock_surface_destroy;
1057 wl_list_insert(&surface_resource->destroy_listener_list,
1058 &shell->lock_surface_listener.link);
1060 shell->lock_surface->type = SHELL_SURFACE_LOCK;
1064 resume_desktop(struct wl_shell *shell)
1066 struct shell_surface *tmp;
1068 wl_list_for_each(tmp, &shell->screensaver.surfaces, link)
1069 hide_screensaver(shell, tmp);
1071 terminate_screensaver(shell);
1073 wl_list_remove(&shell->lock_layer.link);
1074 wl_list_insert(&shell->compositor->cursor_layer.link,
1075 &shell->fullscreen_layer.link);
1076 wl_list_insert(&shell->fullscreen_layer.link,
1077 &shell->panel_layer.link);
1078 wl_list_insert(&shell->panel_layer.link, &shell->toplevel_layer.link);
1080 shell->locked = false;
1081 shell->compositor->idle_time = shell->compositor->option_idle_time;
1082 weston_compositor_wake(shell->compositor);
1083 weston_compositor_damage_all(shell->compositor);
1087 desktop_shell_unlock(struct wl_client *client,
1088 struct wl_resource *resource)
1090 struct wl_shell *shell = resource->data;
1092 shell->prepare_event_sent = false;
1095 resume_desktop(shell);
1098 static const struct desktop_shell_interface desktop_shell_implementation = {
1099 desktop_shell_set_background,
1100 desktop_shell_set_panel,
1101 desktop_shell_set_lock_surface,
1102 desktop_shell_unlock
1105 static enum shell_surface_type
1106 get_shell_surface_type(struct weston_surface *surface)
1108 struct shell_surface *shsurf;
1110 shsurf = get_shell_surface(surface);
1112 return SHELL_SURFACE_NONE;
1113 return shsurf->type;
1117 move_binding(struct wl_input_device *device, uint32_t time,
1118 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1120 struct weston_surface *surface =
1121 (struct weston_surface *) device->pointer_focus;
1123 if (surface == NULL)
1126 switch (get_shell_surface_type(surface)) {
1127 case SHELL_SURFACE_PANEL:
1128 case SHELL_SURFACE_BACKGROUND:
1129 case SHELL_SURFACE_FULLSCREEN:
1130 case SHELL_SURFACE_SCREENSAVER:
1136 weston_surface_move(surface, (struct weston_input_device *) device, time);
1140 resize_binding(struct wl_input_device *device, uint32_t time,
1141 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1143 struct weston_surface *surface =
1144 (struct weston_surface *) device->pointer_focus;
1147 struct shell_surface *shsurf;
1149 if (surface == NULL)
1152 shsurf = get_shell_surface(surface);
1156 switch (shsurf->type) {
1157 case SHELL_SURFACE_PANEL:
1158 case SHELL_SURFACE_BACKGROUND:
1159 case SHELL_SURFACE_FULLSCREEN:
1160 case SHELL_SURFACE_SCREENSAVER:
1166 weston_surface_from_global(surface,
1167 device->grab_x, device->grab_y, &x, &y);
1169 if (x < surface->geometry.width / 3)
1170 edges |= WL_SHELL_SURFACE_RESIZE_LEFT;
1171 else if (x < 2 * surface->geometry.width / 3)
1174 edges |= WL_SHELL_SURFACE_RESIZE_RIGHT;
1176 if (y < surface->geometry.height / 3)
1177 edges |= WL_SHELL_SURFACE_RESIZE_TOP;
1178 else if (y < 2 * surface->geometry.height / 3)
1181 edges |= WL_SHELL_SURFACE_RESIZE_BOTTOM;
1183 weston_surface_resize(shsurf, (struct weston_input_device *) device,
1188 surface_opacity_binding(struct wl_input_device *device, uint32_t time,
1189 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1192 struct shell_surface *shsurf;
1193 struct weston_surface *surface =
1194 (struct weston_surface *) device->pointer_focus;
1196 if (surface == NULL)
1199 shsurf = get_shell_surface(surface);
1203 switch (shsurf->type) {
1204 case SHELL_SURFACE_BACKGROUND:
1205 case SHELL_SURFACE_SCREENSAVER:
1211 surface->alpha += value * step;
1213 if (surface->alpha > 255)
1214 surface->alpha = 255;
1215 if (surface->alpha < step)
1216 surface->alpha = step;
1218 surface->geometry.dirty = 1;
1219 weston_surface_damage(surface);
1223 zoom_binding(struct wl_input_device *device, uint32_t time,
1224 uint32_t key, uint32_t button, uint32_t axis, int32_t value, void *data)
1226 struct weston_input_device *wd = (struct weston_input_device *) device;
1227 struct weston_compositor *compositor = wd->compositor;
1228 struct weston_output *output;
1230 wl_list_for_each(output, &compositor->output_list, link) {
1231 if (pixman_region32_contains_point(&output->region,
1232 device->x, device->y, NULL)) {
1233 output->zoom.active = 1;
1234 output->zoom.level += output->zoom.increment * -value;
1236 if (output->zoom.level >= 1.0) {
1237 output->zoom.active = 0;
1238 output->zoom.level = 1.0;
1241 if (output->zoom.level < output->zoom.increment)
1242 output->zoom.level = output->zoom.increment;
1244 weston_output_update_zoom(output, device->x, device->y);
1250 terminate_binding(struct wl_input_device *device, uint32_t time,
1251 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1253 struct weston_compositor *compositor = data;
1256 wl_display_terminate(compositor->wl_display);
1260 rotate_grab_motion(struct wl_pointer_grab *grab,
1261 uint32_t time, int32_t x, int32_t y)
1263 struct rotate_grab *rotate =
1264 container_of(grab, struct rotate_grab, grab);
1265 struct wl_input_device *device = grab->input_device;
1266 struct shell_surface *surface = rotate->surface;
1267 struct weston_surface *base_surface = surface->surface;
1268 GLfloat cx = 0.5f * base_surface->geometry.width;
1269 GLfloat cy = 0.5f * base_surface->geometry.height;
1270 GLfloat dx, dy, cposx, cposy, dposx, dposy;
1273 dx = device->x - rotate->center.x;
1274 dy = device->y - rotate->center.y;
1275 r = sqrtf(dx * dx + dy * dy);
1277 wl_list_remove(&surface->rotation.transform.link);
1278 surface->surface->geometry.dirty = 1;
1281 struct weston_matrix *matrix =
1282 &surface->rotation.transform.matrix;
1284 weston_matrix_init(&rotate->rotation);
1285 rotate->rotation.d[0] = dx / r;
1286 rotate->rotation.d[4] = -dy / r;
1287 rotate->rotation.d[1] = -rotate->rotation.d[4];
1288 rotate->rotation.d[5] = rotate->rotation.d[0];
1290 weston_matrix_init(matrix);
1291 weston_matrix_translate(matrix, -cx, -cy, 0.0f);
1292 weston_matrix_multiply(matrix, &surface->rotation.rotation);
1293 weston_matrix_multiply(matrix, &rotate->rotation);
1294 weston_matrix_translate(matrix, cx, cy, 0.0f);
1297 &surface->surface->geometry.transformation_list,
1298 &surface->rotation.transform.link);
1300 wl_list_init(&surface->rotation.transform.link);
1301 weston_matrix_init(&surface->rotation.rotation);
1302 weston_matrix_init(&rotate->rotation);
1305 /* We need to adjust the position of the surface
1306 * in case it was resized in a rotated state before */
1307 cposx = base_surface->geometry.x + cx;
1308 cposy = base_surface->geometry.y + cy;
1309 dposx = rotate->center.x - cposx;
1310 dposy = rotate->center.y - cposy;
1311 if (dposx != 0.0f || dposy != 0.0f) {
1312 weston_surface_set_position(base_surface,
1313 base_surface->geometry.x + dposx,
1314 base_surface->geometry.y + dposy);
1317 /* Repaint implies weston_surface_update_transform(), which
1318 * lazily applies the damage due to rotation update.
1320 weston_compositor_schedule_repaint(surface->surface->compositor);
1324 rotate_grab_button(struct wl_pointer_grab *grab,
1325 uint32_t time, uint32_t button, int32_t state)
1327 struct rotate_grab *rotate =
1328 container_of(grab, struct rotate_grab, grab);
1329 struct wl_input_device *device = grab->input_device;
1330 struct shell_surface *surface = rotate->surface;
1332 if (device->button_count == 0 && state == 0) {
1333 weston_matrix_multiply(&surface->rotation.rotation,
1335 wl_input_device_end_pointer_grab(device, time);
1340 static const struct wl_pointer_grab_interface rotate_grab_interface = {
1347 rotate_binding(struct wl_input_device *device, uint32_t time,
1348 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
1350 struct weston_surface *base_surface =
1351 (struct weston_surface *) device->pointer_focus;
1352 struct shell_surface *surface;
1353 struct rotate_grab *rotate;
1357 if (base_surface == NULL)
1360 surface = get_shell_surface(base_surface);
1364 switch (surface->type) {
1365 case SHELL_SURFACE_PANEL:
1366 case SHELL_SURFACE_BACKGROUND:
1367 case SHELL_SURFACE_FULLSCREEN:
1368 case SHELL_SURFACE_SCREENSAVER:
1374 rotate = malloc(sizeof *rotate);
1378 rotate->grab.interface = &rotate_grab_interface;
1379 rotate->surface = surface;
1381 weston_surface_to_global(surface->surface,
1382 surface->surface->geometry.width / 2,
1383 surface->surface->geometry.height / 2,
1384 &rotate->center.x, &rotate->center.y);
1386 wl_input_device_start_pointer_grab(device, &rotate->grab, time);
1388 dx = device->x - rotate->center.x;
1389 dy = device->y - rotate->center.y;
1390 r = sqrtf(dx * dx + dy * dy);
1392 struct weston_matrix inverse;
1394 weston_matrix_init(&inverse);
1395 inverse.d[0] = dx / r;
1396 inverse.d[4] = dy / r;
1397 inverse.d[1] = -inverse.d[4];
1398 inverse.d[5] = inverse.d[0];
1399 weston_matrix_multiply(&surface->rotation.rotation, &inverse);
1401 weston_matrix_init(&rotate->rotation);
1402 rotate->rotation.d[0] = dx / r;
1403 rotate->rotation.d[4] = -dy / r;
1404 rotate->rotation.d[1] = -rotate->rotation.d[4];
1405 rotate->rotation.d[5] = rotate->rotation.d[0];
1407 weston_matrix_init(&surface->rotation.rotation);
1408 weston_matrix_init(&rotate->rotation);
1411 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0);
1415 activate(struct weston_shell *base, struct weston_surface *es,
1416 struct weston_input_device *device, uint32_t time)
1418 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1419 struct weston_compositor *compositor = shell->compositor;
1420 struct weston_surface *surf, *prev;
1422 weston_surface_activate(es, device, time);
1424 if (compositor->wxs)
1425 weston_xserver_surface_activate(es);
1427 switch (get_shell_surface_type(es)) {
1428 case SHELL_SURFACE_BACKGROUND:
1429 case SHELL_SURFACE_PANEL:
1430 case SHELL_SURFACE_LOCK:
1433 case SHELL_SURFACE_SCREENSAVER:
1434 /* always below lock surface */
1435 if (shell->lock_surface)
1436 weston_surface_restack(es,
1437 &shell->lock_surface->surface->layer_link);
1439 case SHELL_SURFACE_FULLSCREEN:
1440 /* should on top of panels */
1441 shell_stack_fullscreen(get_shell_surface(es));
1444 /* move the fullscreen surfaces down into the toplevel layer */
1445 if (!wl_list_empty(&shell->fullscreen_layer.surface_list)) {
1446 wl_list_for_each_reverse_safe(surf,
1448 &shell->fullscreen_layer.surface_list,
1450 weston_surface_restack(surf,
1451 &shell->toplevel_layer.surface_list);
1454 weston_surface_restack(es,
1455 &shell->toplevel_layer.surface_list);
1460 /* no-op func for checking black surface */
1462 black_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1467 is_black_surface (struct weston_surface *es, struct weston_surface **fs_surface)
1469 if (es->configure == black_surface_configure) {
1471 *fs_surface = (struct weston_surface *)es->private;
1478 click_to_activate_binding(struct wl_input_device *device,
1479 uint32_t time, uint32_t key,
1480 uint32_t button, uint32_t axis, int32_t state, void *data)
1482 struct weston_input_device *wd = (struct weston_input_device *) device;
1483 struct weston_compositor *compositor = data;
1484 struct weston_surface *focus;
1485 struct weston_surface *upper;
1487 focus = (struct weston_surface *) device->pointer_focus;
1491 if (is_black_surface(focus, &upper))
1494 if (state && device->pointer_grab == &device->default_pointer_grab)
1495 activate(compositor->shell, focus, wd, time);
1499 lock(struct weston_shell *base)
1501 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1502 struct weston_input_device *device;
1503 struct shell_surface *shsurf;
1504 struct weston_output *output;
1507 if (shell->locked) {
1508 wl_list_for_each(output, &shell->compositor->output_list, link)
1509 /* TODO: find a way to jump to other DPMS levels */
1510 if (output->set_dpms)
1511 output->set_dpms(output, WESTON_DPMS_STANDBY);
1515 shell->locked = true;
1517 /* Hide all surfaces by removing the fullscreen, panel and
1518 * toplevel layers. This way nothing else can show or receive
1519 * input events while we are locked. */
1521 wl_list_remove(&shell->panel_layer.link);
1522 wl_list_remove(&shell->toplevel_layer.link);
1523 wl_list_remove(&shell->fullscreen_layer.link);
1524 wl_list_insert(&shell->compositor->cursor_layer.link,
1525 &shell->lock_layer.link);
1527 launch_screensaver(shell);
1529 wl_list_for_each(shsurf, &shell->screensaver.surfaces, link)
1530 show_screensaver(shell, shsurf);
1532 if (!wl_list_empty(&shell->screensaver.surfaces)) {
1533 shell->compositor->idle_time = shell->screensaver.duration;
1534 weston_compositor_wake(shell->compositor);
1535 shell->compositor->state = WESTON_COMPOSITOR_IDLE;
1538 /* reset pointer foci */
1539 weston_compositor_schedule_repaint(shell->compositor);
1541 /* reset keyboard foci */
1542 time = weston_compositor_get_time();
1543 wl_list_for_each(device, &shell->compositor->input_device_list, link) {
1544 wl_input_device_set_keyboard_focus(&device->input_device,
1548 /* TODO: disable bindings that should not work while locked. */
1550 /* All this must be undone in resume_desktop(). */
1554 unlock(struct weston_shell *base)
1556 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1558 if (!shell->locked || shell->lock_surface) {
1559 weston_compositor_wake(shell->compositor);
1563 /* If desktop-shell client has gone away, unlock immediately. */
1564 if (!shell->child.desktop_shell) {
1565 resume_desktop(shell);
1569 if (shell->prepare_event_sent)
1572 desktop_shell_send_prepare_lock_surface(shell->child.desktop_shell);
1573 shell->prepare_event_sent = true;
1577 center_on_output(struct weston_surface *surface, struct weston_output *output)
1579 struct weston_mode *mode = output->current;
1580 GLfloat x = (mode->width - surface->geometry.width) / 2;
1581 GLfloat y = (mode->height - surface->geometry.height) / 2;
1583 weston_surface_set_position(surface, output->x + x, output->y + y);
1587 map(struct weston_shell *base, struct weston_surface *surface,
1588 int32_t width, int32_t height, int32_t sx, int32_t sy)
1590 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1591 struct weston_compositor *compositor = shell->compositor;
1592 struct shell_surface *shsurf;
1593 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1594 struct weston_surface *parent;
1595 int panel_height = 0;
1597 shsurf = get_shell_surface(surface);
1599 surface_type = shsurf->type;
1601 surface->geometry.width = width;
1602 surface->geometry.height = height;
1603 surface->geometry.dirty = 1;
1605 /* initial positioning, see also configure() */
1606 switch (surface_type) {
1607 case SHELL_SURFACE_TOPLEVEL:
1608 weston_surface_set_position(surface, 10 + random() % 400,
1609 10 + random() % 400);
1611 case SHELL_SURFACE_SCREENSAVER:
1612 center_on_output(surface, shsurf->fullscreen_output);
1614 case SHELL_SURFACE_FULLSCREEN:
1615 shell_map_fullscreen(shsurf);
1617 case SHELL_SURFACE_MAXIMIZED:
1618 /* use surface configure to set the geometry */
1619 panel_height = get_output_panel_height(shell,surface->output);
1620 weston_surface_set_position(surface, surface->output->x,
1621 surface->output->y + panel_height);
1623 case SHELL_SURFACE_LOCK:
1624 center_on_output(surface, get_default_output(compositor));
1626 case SHELL_SURFACE_POPUP:
1627 shell_map_popup(shsurf, shsurf->popup.time);
1628 case SHELL_SURFACE_NONE:
1629 weston_surface_set_position(surface,
1630 surface->geometry.x + sx,
1631 surface->geometry.y + sy);
1637 /* surface stacking order, see also activate() */
1638 switch (surface_type) {
1639 case SHELL_SURFACE_BACKGROUND:
1640 /* background always visible, at the bottom */
1641 wl_list_insert(&shell->background_layer.surface_list,
1642 &surface->layer_link);
1644 case SHELL_SURFACE_PANEL:
1645 /* panel always on top, hidden while locked */
1646 wl_list_insert(&shell->panel_layer.surface_list,
1647 &surface->layer_link);
1649 case SHELL_SURFACE_LOCK:
1650 /* lock surface always visible, on top */
1651 wl_list_insert(&shell->lock_layer.surface_list,
1652 &surface->layer_link);
1653 weston_compositor_wake(compositor);
1655 case SHELL_SURFACE_SCREENSAVER:
1656 /* If locked, show it. */
1657 if (shell->locked) {
1658 show_screensaver(shell, shsurf);
1659 compositor->idle_time = shell->screensaver.duration;
1660 weston_compositor_wake(compositor);
1661 if (!shell->lock_surface)
1662 compositor->state = WESTON_COMPOSITOR_IDLE;
1665 case SHELL_SURFACE_POPUP:
1666 case SHELL_SURFACE_TRANSIENT:
1667 parent = shsurf->parent->surface;
1668 wl_list_insert(parent->layer_link.prev, &surface->layer_link);
1670 case SHELL_SURFACE_FULLSCREEN:
1671 case SHELL_SURFACE_NONE:
1674 wl_list_insert(&shell->toplevel_layer.surface_list,
1675 &surface->layer_link);
1679 if (surface_type != SHELL_SURFACE_NONE) {
1680 weston_surface_assign_output(surface);
1681 if (surface_type == SHELL_SURFACE_MAXIMIZED)
1682 surface->output = shsurf->output;
1685 switch (surface_type) {
1686 case SHELL_SURFACE_TOPLEVEL:
1687 case SHELL_SURFACE_TRANSIENT:
1688 case SHELL_SURFACE_FULLSCREEN:
1689 case SHELL_SURFACE_MAXIMIZED:
1691 activate(base, surface,
1692 (struct weston_input_device *)
1693 compositor->input_device,
1694 weston_compositor_get_time());
1700 if (surface_type == SHELL_SURFACE_TOPLEVEL)
1701 weston_zoom_run(surface, 0.8, 1.0, NULL, NULL);
1705 configure(struct weston_shell *base, struct weston_surface *surface,
1706 GLfloat x, GLfloat y, int32_t width, int32_t height)
1708 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
1709 enum shell_surface_type surface_type = SHELL_SURFACE_NONE;
1710 enum shell_surface_type prev_surface_type = SHELL_SURFACE_NONE;
1711 struct shell_surface *shsurf;
1713 shsurf = get_shell_surface(surface);
1715 surface_type = shsurf->type;
1717 surface->geometry.x = x;
1718 surface->geometry.y = y;
1719 surface->geometry.width = width;
1720 surface->geometry.height = height;
1721 surface->geometry.dirty = 1;
1723 switch (surface_type) {
1724 case SHELL_SURFACE_SCREENSAVER:
1725 center_on_output(surface, shsurf->fullscreen_output);
1727 case SHELL_SURFACE_FULLSCREEN:
1728 shell_configure_fullscreen(shsurf);
1729 if (prev_surface_type != SHELL_SURFACE_FULLSCREEN)
1730 shell_stack_fullscreen(shsurf);
1732 case SHELL_SURFACE_MAXIMIZED:
1733 /* setting x, y and using configure to change that geometry */
1734 surface->geometry.x = surface->output->x;
1735 surface->geometry.y = surface->output->y +
1736 get_output_panel_height(shell,surface->output);
1738 case SHELL_SURFACE_TOPLEVEL:
1744 /* XXX: would a fullscreen surface need the same handling? */
1745 if (surface->output) {
1746 weston_surface_assign_output(surface);
1748 if (surface_type == SHELL_SURFACE_SCREENSAVER)
1749 surface->output = shsurf->output;
1750 else if (surface_type == SHELL_SURFACE_MAXIMIZED)
1751 surface->output = shsurf->output;
1756 shell_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
1758 struct weston_shell *shell = es->compositor->shell;
1759 struct shell_surface *shsurf = get_shell_surface(es);
1761 if (!weston_surface_is_mapped(es)) {
1762 map(shell, es, es->buffer->width, es->buffer->height, sx, sy);
1763 } else if (shsurf->force_configure || sx != 0 || sy != 0 ||
1764 es->geometry.width != es->buffer->width ||
1765 es->geometry.height != es->buffer->height) {
1766 GLfloat from_x, from_y;
1769 weston_surface_to_global_float(es, 0, 0, &from_x, &from_y);
1770 weston_surface_to_global_float(es, sx, sy, &to_x, &to_y);
1771 configure(shell, es,
1772 es->geometry.x + to_x - from_x,
1773 es->geometry.y + to_y - from_y,
1774 es->buffer->width, es->buffer->height);
1775 shsurf->force_configure = 0;
1779 static int launch_desktop_shell_process(struct wl_shell *shell);
1782 desktop_shell_sigchld(struct weston_process *process, int status)
1785 struct wl_shell *shell =
1786 container_of(process, struct wl_shell, child.process);
1788 shell->child.process.pid = 0;
1789 shell->child.client = NULL; /* already destroyed by wayland */
1791 /* if desktop-shell dies more than 5 times in 30 seconds, give up */
1792 time = weston_compositor_get_time();
1793 if (time - shell->child.deathstamp > 30000) {
1794 shell->child.deathstamp = time;
1795 shell->child.deathcount = 0;
1798 shell->child.deathcount++;
1799 if (shell->child.deathcount > 5) {
1800 fprintf(stderr, "weston-desktop-shell died, giving up.\n");
1804 fprintf(stderr, "weston-desktop-shell died, respawning...\n");
1805 launch_desktop_shell_process(shell);
1809 launch_desktop_shell_process(struct wl_shell *shell)
1811 const char *shell_exe = LIBEXECDIR "/weston-desktop-shell";
1813 shell->child.client = weston_client_launch(shell->compositor,
1814 &shell->child.process,
1816 desktop_shell_sigchld);
1818 if (!shell->child.client)
1824 bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
1826 struct wl_shell *shell = data;
1828 wl_client_add_object(client, &wl_shell_interface,
1829 &shell_implementation, id, shell);
1833 unbind_desktop_shell(struct wl_resource *resource)
1835 struct wl_shell *shell = resource->data;
1838 resume_desktop(shell);
1840 shell->child.desktop_shell = NULL;
1841 shell->prepare_event_sent = false;
1846 bind_desktop_shell(struct wl_client *client,
1847 void *data, uint32_t version, uint32_t id)
1849 struct wl_shell *shell = data;
1850 struct wl_resource *resource;
1852 resource = wl_client_add_object(client, &desktop_shell_interface,
1853 &desktop_shell_implementation,
1856 if (client == shell->child.client) {
1857 resource->destroy = unbind_desktop_shell;
1858 shell->child.desktop_shell = resource;
1862 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1863 "permission to bind desktop_shell denied");
1864 wl_resource_destroy(resource, 0);
1868 screensaver_set_surface(struct wl_client *client,
1869 struct wl_resource *resource,
1870 struct wl_resource *shell_surface_resource,
1871 struct wl_resource *output_resource)
1873 struct wl_shell *shell = resource->data;
1874 struct shell_surface *surface = shell_surface_resource->data;
1875 struct weston_output *output = output_resource->data;
1877 if (reset_shell_surface_type(surface))
1880 surface->type = SHELL_SURFACE_SCREENSAVER;
1882 surface->fullscreen_output = output;
1883 surface->output = output;
1884 wl_list_insert(shell->screensaver.surfaces.prev, &surface->link);
1887 static const struct screensaver_interface screensaver_implementation = {
1888 screensaver_set_surface
1892 unbind_screensaver(struct wl_resource *resource)
1894 struct wl_shell *shell = resource->data;
1896 shell->screensaver.binding = NULL;
1901 bind_screensaver(struct wl_client *client,
1902 void *data, uint32_t version, uint32_t id)
1904 struct wl_shell *shell = data;
1905 struct wl_resource *resource;
1907 resource = wl_client_add_object(client, &screensaver_interface,
1908 &screensaver_implementation,
1911 if (shell->screensaver.binding == NULL) {
1912 resource->destroy = unbind_screensaver;
1913 shell->screensaver.binding = resource;
1917 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
1918 "interface object already bound");
1919 wl_resource_destroy(resource, 0);
1923 struct weston_compositor *compositor;
1924 struct weston_surface *current;
1925 struct wl_listener listener;
1926 struct wl_keyboard_grab grab;
1930 switcher_next(struct switcher *switcher)
1932 struct weston_compositor *compositor = switcher->compositor;
1933 struct weston_surface *surface;
1934 struct weston_surface *first = NULL, *prev = NULL, *next = NULL;
1936 wl_list_for_each(surface, &compositor->surface_list, link) {
1937 switch (get_shell_surface_type(surface)) {
1938 case SHELL_SURFACE_TOPLEVEL:
1939 case SHELL_SURFACE_FULLSCREEN:
1940 case SHELL_SURFACE_MAXIMIZED:
1943 if (prev == switcher->current)
1946 surface->alpha = 64;
1947 surface->geometry.dirty = 1;
1948 weston_surface_damage(surface);
1961 wl_list_remove(&switcher->listener.link);
1962 wl_list_insert(next->surface.resource.destroy_listener_list.prev,
1963 &switcher->listener.link);
1965 switcher->current = next;
1970 switcher_handle_surface_destroy(struct wl_listener *listener,
1971 struct wl_resource *resource, uint32_t time)
1973 struct switcher *switcher =
1974 container_of(listener, struct switcher, listener);
1976 switcher_next(switcher);
1980 switcher_destroy(struct switcher *switcher, uint32_t time)
1982 struct weston_compositor *compositor = switcher->compositor;
1983 struct weston_surface *surface;
1984 struct weston_input_device *device =
1985 (struct weston_input_device *) switcher->grab.input_device;
1987 wl_list_for_each(surface, &compositor->surface_list, link) {
1988 surface->alpha = 255;
1989 weston_surface_damage(surface);
1992 if (switcher->current)
1993 activate(compositor->shell, switcher->current, device, time);
1994 wl_list_remove(&switcher->listener.link);
1995 wl_input_device_end_keyboard_grab(&device->input_device, time);
2000 switcher_key(struct wl_keyboard_grab *grab,
2001 uint32_t time, uint32_t key, int32_t state)
2003 struct switcher *switcher = container_of(grab, struct switcher, grab);
2004 struct weston_input_device *device =
2005 (struct weston_input_device *) grab->input_device;
2007 if ((device->modifier_state & MODIFIER_SUPER) == 0) {
2008 switcher_destroy(switcher, time);
2009 } else if (key == KEY_TAB && state) {
2010 switcher_next(switcher);
2014 static const struct wl_keyboard_grab_interface switcher_grab = {
2019 switcher_binding(struct wl_input_device *device, uint32_t time,
2020 uint32_t key, uint32_t button, uint32_t axis,
2021 int32_t state, void *data)
2023 struct weston_compositor *compositor = data;
2024 struct switcher *switcher;
2026 switcher = malloc(sizeof *switcher);
2027 switcher->compositor = compositor;
2028 switcher->current = NULL;
2029 switcher->listener.func = switcher_handle_surface_destroy;
2030 wl_list_init(&switcher->listener.link);
2032 switcher->grab.interface = &switcher_grab;
2033 wl_input_device_start_keyboard_grab(device, &switcher->grab, time);
2034 wl_input_device_set_keyboard_focus(device, NULL,
2035 weston_compositor_get_time());
2036 switcher_next(switcher);
2040 backlight_binding(struct wl_input_device *device, uint32_t time,
2041 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2043 struct weston_compositor *compositor = data;
2044 struct weston_output *output;
2045 long backlight_new = 0;
2047 /* TODO: we're limiting to simple use cases, where we assume just
2048 * control on the primary display. We'd have to extend later if we
2049 * ever get support for setting backlights on random desktop LCD
2051 output = get_default_output(compositor);
2055 if (!output->set_backlight)
2058 if (key == KEY_F9 || key == KEY_BRIGHTNESSDOWN)
2059 backlight_new = output->backlight_current - 25;
2060 else if (key == KEY_F10 || key == KEY_BRIGHTNESSUP)
2061 backlight_new = output->backlight_current + 25;
2063 if (backlight_new < 5)
2065 if (backlight_new > 255)
2066 backlight_new = 255;
2068 output->backlight_current = backlight_new;
2069 output->set_backlight(output, output->backlight_current);
2073 debug_repaint_binding(struct wl_input_device *device, uint32_t time,
2074 uint32_t key, uint32_t button, uint32_t axis, int32_t state, void *data)
2076 struct weston_compositor *compositor = data;
2077 struct wl_shell *shell =
2078 container_of(compositor->shell, struct wl_shell, shell);
2079 struct weston_surface *surface;
2081 if (shell->debug_repaint_surface) {
2082 weston_surface_destroy(shell->debug_repaint_surface);
2083 shell->debug_repaint_surface = NULL;
2085 surface = weston_surface_create(compositor);
2086 weston_surface_set_color(surface, 1.0, 0.0, 0.0, 0.2);
2087 weston_surface_configure(surface, 0, 0, 8192, 8192);
2088 wl_list_insert(&compositor->fade_layer.surface_list,
2089 &surface->layer_link);
2090 weston_surface_assign_output(surface);
2091 pixman_region32_init(&surface->input);
2093 /* Here's the dirty little trick that makes the
2094 * repaint debugging work: we force an
2095 * update_transform first to update dependent state
2096 * and clear the geometry.dirty bit. Then we clear
2097 * the surface damage so it only gets repainted
2098 * piecewise as we repaint other things. */
2100 weston_surface_update_transform(surface);
2101 pixman_region32_fini(&surface->damage);
2102 pixman_region32_init(&surface->damage);
2103 shell->debug_repaint_surface = surface;
2108 shell_destroy(struct weston_shell *base)
2110 struct wl_shell *shell = container_of(base, struct wl_shell, shell);
2112 if (shell->child.client)
2113 wl_client_destroy(shell->child.client);
2115 free(shell->screensaver.path);
2120 shell_init(struct weston_compositor *ec);
2123 shell_init(struct weston_compositor *ec)
2125 struct wl_shell *shell;
2127 shell = malloc(sizeof *shell);
2131 memset(shell, 0, sizeof *shell);
2132 shell->compositor = ec;
2133 shell->shell.lock = lock;
2134 shell->shell.unlock = unlock;
2135 shell->shell.destroy = shell_destroy;
2137 wl_list_init(&shell->backgrounds);
2138 wl_list_init(&shell->panels);
2139 wl_list_init(&shell->screensaver.surfaces);
2141 weston_layer_init(&shell->fullscreen_layer, &ec->cursor_layer.link);
2142 weston_layer_init(&shell->panel_layer, &shell->fullscreen_layer.link);
2143 weston_layer_init(&shell->toplevel_layer, &shell->panel_layer.link);
2144 weston_layer_init(&shell->background_layer,
2145 &shell->toplevel_layer.link);
2146 wl_list_init(&shell->lock_layer.surface_list);
2148 shell_configuration(shell);
2150 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
2151 shell, bind_shell) == NULL)
2154 if (wl_display_add_global(ec->wl_display,
2155 &desktop_shell_interface,
2156 shell, bind_desktop_shell) == NULL)
2159 if (wl_display_add_global(ec->wl_display, &screensaver_interface,
2160 shell, bind_screensaver) == NULL)
2163 shell->child.deathstamp = weston_compositor_get_time();
2164 if (launch_desktop_shell_process(shell) != 0)
2167 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, MODIFIER_SUPER,
2168 move_binding, shell);
2169 weston_compositor_add_binding(ec, 0, BTN_MIDDLE, 0, MODIFIER_SUPER,
2170 resize_binding, shell);
2171 weston_compositor_add_binding(ec, KEY_BACKSPACE, 0, 0,
2172 MODIFIER_CTRL | MODIFIER_ALT,
2173 terminate_binding, ec);
2174 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0, 0,
2175 click_to_activate_binding, ec);
2176 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2177 MODIFIER_SUPER | MODIFIER_ALT,
2178 surface_opacity_binding, NULL);
2179 weston_compositor_add_binding(ec, 0, 0, WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL,
2180 MODIFIER_SUPER, zoom_binding, NULL);
2181 weston_compositor_add_binding(ec, 0, BTN_LEFT, 0,
2182 MODIFIER_SUPER | MODIFIER_ALT,
2183 rotate_binding, NULL);
2184 weston_compositor_add_binding(ec, KEY_TAB, 0, 0, MODIFIER_SUPER,
2185 switcher_binding, ec);
2188 weston_compositor_add_binding(ec, KEY_F9, 0, 0, MODIFIER_CTRL,
2189 backlight_binding, ec);
2190 weston_compositor_add_binding(ec, KEY_BRIGHTNESSDOWN, 0, 0, 0,
2191 backlight_binding, ec);
2192 weston_compositor_add_binding(ec, KEY_F10, 0, 0, MODIFIER_CTRL,
2193 backlight_binding, ec);
2194 weston_compositor_add_binding(ec, KEY_BRIGHTNESSUP, 0, 0, 0,
2195 backlight_binding, ec);
2197 weston_compositor_add_binding(ec, KEY_SPACE, 0, 0, MODIFIER_SUPER,
2198 debug_repaint_binding, ec);
2200 ec->shell = &shell->shell;