2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 #include "../config.h"
40 #include <sys/epoll.h>
41 #include <sys/timerfd.h>
44 #include <wayland-egl.h>
46 #ifdef USE_CAIRO_GLESV2
47 #include <GLES2/gl2.h>
48 #include <GLES2/gl2ext.h>
53 #include <EGL/eglext.h>
56 #else /* HAVE_CAIRO_EGL */
57 typedef void *EGLDisplay;
58 typedef void *EGLConfig;
59 typedef void *EGLContext;
60 #define EGL_NO_DISPLAY ((EGLDisplay)0)
61 #endif /* no HAVE_CAIRO_EGL */
63 #include <xkbcommon/xkbcommon.h>
64 #include <wayland-cursor.h>
66 #include <linux/input.h>
67 #include <wayland-client.h>
68 #include "../shared/cairo-util.h"
69 #include "text-cursor-position-client-protocol.h"
70 #include "workspaces-client-protocol.h"
71 #include "../shared/os-compatibility.h"
85 struct wl_display *display;
86 struct wl_registry *registry;
87 struct wl_compositor *compositor;
88 struct wl_subcompositor *subcompositor;
89 struct wl_shell *shell;
91 struct wl_data_device_manager *data_device_manager;
92 struct text_cursor_position *text_cursor_position;
93 struct workspace_manager *workspace_manager;
95 EGLConfig argb_config;
97 cairo_device_t *argb_device;
101 uint32_t display_fd_events;
102 struct task display_task;
105 struct wl_list deferred_list;
109 struct wl_list global_list;
110 struct wl_list window_list;
111 struct wl_list input_list;
112 struct wl_list output_list;
116 struct wl_cursor_theme *cursor_theme;
117 struct wl_cursor **cursors;
119 display_output_handler_t output_configure_handler;
120 display_global_handler_t global_handler;
124 struct xkb_context *xkb_context;
127 uint32_t workspace_count;
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
144 struct window_output {
145 struct output *output;
151 * Prepare the surface for drawing. Makes sure there is a surface
152 * of the right size available for rendering, and returns it.
153 * dx,dy are the x,y of wl_surface.attach.
154 * width,height are the new surface size.
155 * If flags has SURFACE_HINT_RESIZE set, the user is
156 * doing continuous resizing.
157 * Returns the Cairo surface to draw to.
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
160 int width, int height, uint32_t flags);
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
177 * Release the toysurface from the EGL context, returning control
180 void (*release)(struct toysurface *base);
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
186 void (*destroy)(struct toysurface *base);
190 struct window *window;
192 struct wl_surface *surface;
193 struct wl_subsurface *subsurface;
195 int synchronized_default;
196 struct toysurface *toysurface;
197 struct widget *widget;
199 struct rectangle allocation;
200 struct rectangle server_allocation;
202 struct wl_region *input_region;
203 struct wl_region *opaque_region;
205 enum window_buffer_type buffer_type;
206 enum wl_output_transform buffer_transform;
208 cairo_surface_t *cairo_surface;
214 struct display *display;
215 struct window *parent;
216 struct wl_list window_output_list;
218 struct rectangle saved_allocation;
219 struct rectangle min_allocation;
220 struct rectangle pending_allocation;
223 int redraw_scheduled;
225 struct task redraw_task;
232 int fullscreen_method;
233 int configure_requests;
235 window_key_handler_t key_handler;
236 window_keyboard_focus_handler_t keyboard_focus_handler;
237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
239 window_close_handler_t close_handler;
240 window_fullscreen_handler_t fullscreen_handler;
241 window_output_handler_t output_handler;
243 struct surface *main_surface;
244 struct wl_shell_surface *shell_surface;
245 struct wl_callback *frame_cb;
249 /* struct surface::link, contains also main_surface */
250 struct wl_list subsurface_list;
257 struct window *window;
258 struct surface *surface;
259 struct tooltip *tooltip;
260 struct wl_list child_list;
262 struct rectangle allocation;
263 widget_resize_handler_t resize_handler;
264 widget_redraw_handler_t redraw_handler;
265 widget_enter_handler_t enter_handler;
266 widget_leave_handler_t leave_handler;
267 widget_motion_handler_t motion_handler;
268 widget_button_handler_t button_handler;
269 widget_axis_handler_t axis_handler;
277 struct display *display;
278 struct wl_seat *seat;
279 struct wl_pointer *pointer;
280 struct wl_keyboard *keyboard;
281 struct window *pointer_focus;
282 struct window *keyboard_focus;
284 uint32_t cursor_anim_start;
285 struct wl_callback *cursor_frame_cb;
286 struct wl_surface *pointer_surface;
288 uint32_t pointer_enter_serial;
289 uint32_t cursor_serial;
293 struct widget *focus_widget;
295 uint32_t grab_button;
297 struct wl_data_device *data_device;
298 struct data_offer *drag_offer;
299 struct data_offer *selection_offer;
302 struct xkb_keymap *keymap;
303 struct xkb_state *state;
304 xkb_mod_mask_t control_mask;
305 xkb_mod_mask_t alt_mask;
306 xkb_mod_mask_t shift_mask;
309 struct task repeat_task;
313 uint32_t repeat_time;
317 struct display *display;
318 struct wl_output *output;
319 struct rectangle allocation;
323 display_output_handler_t destroy_handler;
327 enum frame_button_action {
328 FRAME_BUTTON_NULL = 0,
329 FRAME_BUTTON_ICON = 1,
330 FRAME_BUTTON_CLOSE = 2,
331 FRAME_BUTTON_MINIMIZE = 3,
332 FRAME_BUTTON_MAXIMIZE = 4,
335 enum frame_button_pointer {
336 FRAME_BUTTON_DEFAULT = 0,
337 FRAME_BUTTON_OVER = 1,
338 FRAME_BUTTON_ACTIVE = 2,
341 enum frame_button_align {
342 FRAME_BUTTON_RIGHT = 0,
343 FRAME_BUTTON_LEFT = 1,
346 enum frame_button_decoration {
347 FRAME_BUTTON_NONE = 0,
348 FRAME_BUTTON_FANCY = 1,
351 struct frame_button {
352 struct widget *widget;
354 cairo_surface_t *icon;
355 enum frame_button_action type;
356 enum frame_button_pointer state;
357 struct wl_list link; /* buttons_list */
358 enum frame_button_align align;
359 enum frame_button_decoration decoration;
363 struct widget *widget;
364 struct widget *child;
365 struct wl_list buttons_list;
369 struct window *window;
370 struct widget *widget;
372 const char **entries;
381 struct widget *parent;
382 struct window *window;
383 struct widget *widget;
385 struct task tooltip_task;
391 struct wl_shm_pool *pool;
398 CURSOR_DEFAULT = 100,
402 enum window_location {
404 WINDOW_RESIZING_TOP = 1,
405 WINDOW_RESIZING_BOTTOM = 2,
406 WINDOW_RESIZING_LEFT = 4,
407 WINDOW_RESIZING_TOP_LEFT = 5,
408 WINDOW_RESIZING_BOTTOM_LEFT = 6,
409 WINDOW_RESIZING_RIGHT = 8,
410 WINDOW_RESIZING_TOP_RIGHT = 9,
411 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
412 WINDOW_RESIZING_MASK = 15,
413 WINDOW_EXTERIOR = 16,
414 WINDOW_TITLEBAR = 17,
415 WINDOW_CLIENT_AREA = 18,
418 static const cairo_user_data_key_t shm_surface_data_key;
420 #ifdef HAVE_CAIRO_EGL
422 struct egl_window_surface {
423 struct toysurface base;
424 cairo_surface_t *cairo_surface;
425 struct display *display;
426 struct wl_surface *surface;
427 struct wl_egl_window *egl_window;
428 EGLSurface egl_surface;
431 static struct egl_window_surface *
432 to_egl_window_surface(struct toysurface *base)
434 return container_of(base, struct egl_window_surface, base);
437 static cairo_surface_t *
438 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
439 int width, int height, uint32_t flags)
441 struct egl_window_surface *surface = to_egl_window_surface(base);
443 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
444 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
446 return cairo_surface_reference(surface->cairo_surface);
450 egl_window_surface_swap(struct toysurface *base,
451 struct rectangle *server_allocation)
453 struct egl_window_surface *surface = to_egl_window_surface(base);
455 cairo_gl_surface_swapbuffers(surface->cairo_surface);
456 wl_egl_window_get_attached_size(surface->egl_window,
457 &server_allocation->width,
458 &server_allocation->height);
462 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
464 struct egl_window_surface *surface = to_egl_window_surface(base);
465 cairo_device_t *device;
467 device = cairo_surface_get_device(surface->cairo_surface);
472 if (device == surface->display->argb_device)
473 ctx = surface->display->argb_ctx;
478 cairo_device_flush(device);
479 cairo_device_acquire(device);
480 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
481 surface->egl_surface, ctx))
482 fprintf(stderr, "failed to make surface current\n");
488 egl_window_surface_release(struct toysurface *base)
490 struct egl_window_surface *surface = to_egl_window_surface(base);
491 cairo_device_t *device;
493 device = cairo_surface_get_device(surface->cairo_surface);
497 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
498 surface->display->argb_ctx))
499 fprintf(stderr, "failed to make context current\n");
501 cairo_device_release(device);
505 egl_window_surface_destroy(struct toysurface *base)
507 struct egl_window_surface *surface = to_egl_window_surface(base);
508 struct display *d = surface->display;
510 cairo_surface_destroy(surface->cairo_surface);
511 eglDestroySurface(d->dpy, surface->egl_surface);
512 wl_egl_window_destroy(surface->egl_window);
513 surface->surface = NULL;
518 static struct toysurface *
519 egl_window_surface_create(struct display *display,
520 struct wl_surface *wl_surface,
522 struct rectangle *rectangle)
524 struct egl_window_surface *surface;
526 if (display->dpy == EGL_NO_DISPLAY)
529 surface = calloc(1, sizeof *surface);
533 surface->base.prepare = egl_window_surface_prepare;
534 surface->base.swap = egl_window_surface_swap;
535 surface->base.acquire = egl_window_surface_acquire;
536 surface->base.release = egl_window_surface_release;
537 surface->base.destroy = egl_window_surface_destroy;
539 surface->display = display;
540 surface->surface = wl_surface;
542 surface->egl_window = wl_egl_window_create(surface->surface,
546 surface->egl_surface = eglCreateWindowSurface(display->dpy,
547 display->argb_config,
551 surface->cairo_surface =
552 cairo_gl_surface_create_for_egl(display->argb_device,
553 surface->egl_surface,
557 return &surface->base;
562 static struct toysurface *
563 egl_window_surface_create(struct display *display,
564 struct wl_surface *wl_surface,
566 struct rectangle *rectangle)
573 struct shm_surface_data {
574 struct wl_buffer *buffer;
575 struct shm_pool *pool;
579 display_get_buffer_for_surface(struct display *display,
580 cairo_surface_t *surface)
582 struct shm_surface_data *data;
584 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
590 shm_pool_destroy(struct shm_pool *pool);
593 shm_surface_data_destroy(void *p)
595 struct shm_surface_data *data = p;
597 wl_buffer_destroy(data->buffer);
599 shm_pool_destroy(data->pool);
604 static struct wl_shm_pool *
605 make_shm_pool(struct display *display, int size, void **data)
607 struct wl_shm_pool *pool;
610 fd = os_create_anonymous_file(size);
612 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
617 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
618 if (*data == MAP_FAILED) {
619 fprintf(stderr, "mmap failed: %m\n");
624 pool = wl_shm_create_pool(display->shm, fd, size);
631 static struct shm_pool *
632 shm_pool_create(struct display *display, size_t size)
634 struct shm_pool *pool = malloc(sizeof *pool);
639 pool->pool = make_shm_pool(display, size, &pool->data);
652 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
654 if (pool->used + size > pool->size)
657 *offset = pool->used;
660 return (char *) pool->data + *offset;
663 /* destroy the pool. this does not unmap the memory though */
665 shm_pool_destroy(struct shm_pool *pool)
667 munmap(pool->data, pool->size);
668 wl_shm_pool_destroy(pool->pool);
672 /* Start allocating from the beginning of the pool again */
674 shm_pool_reset(struct shm_pool *pool)
680 data_length_for_shm_surface(struct rectangle *rect)
684 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
686 return stride * rect->height;
689 static cairo_surface_t *
690 display_create_shm_surface_from_pool(struct display *display,
691 struct rectangle *rectangle,
692 uint32_t flags, struct shm_pool *pool)
694 struct shm_surface_data *data;
696 cairo_surface_t *surface;
697 int stride, length, offset;
700 data = malloc(sizeof *data);
704 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
706 length = stride * rectangle->height;
708 map = shm_pool_allocate(pool, length, &offset);
715 surface = cairo_image_surface_create_for_data (map,
721 cairo_surface_set_user_data(surface, &shm_surface_data_key,
722 data, shm_surface_data_destroy);
724 if (flags & SURFACE_OPAQUE)
725 format = WL_SHM_FORMAT_XRGB8888;
727 format = WL_SHM_FORMAT_ARGB8888;
729 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
737 static cairo_surface_t *
738 display_create_shm_surface(struct display *display,
739 struct rectangle *rectangle, uint32_t flags,
740 struct shm_pool *alternate_pool,
741 struct shm_surface_data **data_ret)
743 struct shm_surface_data *data;
744 struct shm_pool *pool;
745 cairo_surface_t *surface;
747 if (alternate_pool) {
748 shm_pool_reset(alternate_pool);
749 surface = display_create_shm_surface_from_pool(display,
754 data = cairo_surface_get_user_data(surface,
755 &shm_surface_data_key);
760 pool = shm_pool_create(display,
761 data_length_for_shm_surface(rectangle));
766 display_create_shm_surface_from_pool(display, rectangle,
770 shm_pool_destroy(pool);
774 /* make sure we destroy the pool when the surface is destroyed */
775 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
786 check_size(struct rectangle *rect)
788 if (rect->width && rect->height)
791 fprintf(stderr, "tried to create surface of "
792 "width: %d, height: %d\n", rect->width, rect->height);
797 display_create_surface(struct display *display,
798 struct wl_surface *surface,
799 struct rectangle *rectangle,
802 if (check_size(rectangle) < 0)
805 assert(flags & SURFACE_SHM);
806 return display_create_shm_surface(display, rectangle, flags,
810 struct shm_surface_leaf {
811 cairo_surface_t *cairo_surface;
812 /* 'data' is automatically destroyed, when 'cairo_surface' is */
813 struct shm_surface_data *data;
815 struct shm_pool *resize_pool;
820 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
822 if (leaf->cairo_surface)
823 cairo_surface_destroy(leaf->cairo_surface);
824 /* leaf->data already destroyed via cairo private */
826 if (leaf->resize_pool)
827 shm_pool_destroy(leaf->resize_pool);
829 memset(leaf, 0, sizeof *leaf);
835 struct toysurface base;
836 struct display *display;
837 struct wl_surface *surface;
841 struct shm_surface_leaf leaf[MAX_LEAVES];
842 struct shm_surface_leaf *current;
845 static struct shm_surface *
846 to_shm_surface(struct toysurface *base)
848 return container_of(base, struct shm_surface, base);
852 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
854 struct shm_surface *surface = data;
855 struct shm_surface_leaf *leaf;
859 for (i = 0; i < MAX_LEAVES; i++) {
860 leaf = &surface->leaf[i];
861 if (leaf->data && leaf->data->buffer == buffer) {
866 assert(i < MAX_LEAVES && "unknown buffer released");
868 /* Leave one free leaf with storage, release others */
870 for (i = 0; i < MAX_LEAVES; i++) {
871 leaf = &surface->leaf[i];
873 if (!leaf->cairo_surface || leaf->busy)
879 shm_surface_leaf_release(leaf);
883 static const struct wl_buffer_listener shm_surface_buffer_listener = {
884 shm_surface_buffer_release
887 static cairo_surface_t *
888 shm_surface_prepare(struct toysurface *base, int dx, int dy,
889 int width, int height, uint32_t flags)
891 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
892 struct shm_surface *surface = to_shm_surface(base);
893 struct rectangle rect = { 0, 0, width, height };
894 struct shm_surface_leaf *leaf = NULL;
900 /* pick a free buffer, preferrably one that already has storage */
901 for (i = 0; i < MAX_LEAVES; i++) {
902 if (surface->leaf[i].busy)
905 if (!leaf || surface->leaf[i].cairo_surface)
906 leaf = &surface->leaf[i];
909 fprintf(stderr, "%s: all buffers are held by the server.\n",
914 if (!resize_hint && leaf->resize_pool) {
915 cairo_surface_destroy(leaf->cairo_surface);
916 leaf->cairo_surface = NULL;
917 shm_pool_destroy(leaf->resize_pool);
918 leaf->resize_pool = NULL;
921 if (leaf->cairo_surface &&
922 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
923 cairo_image_surface_get_height(leaf->cairo_surface) == height)
926 if (leaf->cairo_surface)
927 cairo_surface_destroy(leaf->cairo_surface);
929 if (resize_hint && !leaf->resize_pool) {
930 /* Create a big pool to allocate from, while continuously
931 * resizing. Mmapping a new pool in the server
932 * is relatively expensive, so reusing a pool performs
933 * better, but may temporarily reserve unneeded memory.
935 /* We should probably base this number on the output size. */
936 leaf->resize_pool = shm_pool_create(surface->display,
940 leaf->cairo_surface =
941 display_create_shm_surface(surface->display, &rect,
945 wl_buffer_add_listener(leaf->data->buffer,
946 &shm_surface_buffer_listener, surface);
949 surface->current = leaf;
951 return cairo_surface_reference(leaf->cairo_surface);
955 shm_surface_swap(struct toysurface *base,
956 struct rectangle *server_allocation)
958 struct shm_surface *surface = to_shm_surface(base);
959 struct shm_surface_leaf *leaf = surface->current;
961 server_allocation->width =
962 cairo_image_surface_get_width(leaf->cairo_surface);
963 server_allocation->height =
964 cairo_image_surface_get_height(leaf->cairo_surface);
966 wl_surface_attach(surface->surface, leaf->data->buffer,
967 surface->dx, surface->dy);
968 wl_surface_damage(surface->surface, 0, 0,
969 server_allocation->width, server_allocation->height);
970 wl_surface_commit(surface->surface);
973 surface->current = NULL;
977 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
983 shm_surface_release(struct toysurface *base)
988 shm_surface_destroy(struct toysurface *base)
990 struct shm_surface *surface = to_shm_surface(base);
993 for (i = 0; i < MAX_LEAVES; i++)
994 shm_surface_leaf_release(&surface->leaf[i]);
999 static struct toysurface *
1000 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1001 uint32_t flags, struct rectangle *rectangle)
1003 struct shm_surface *surface;
1005 surface = calloc(1, sizeof *surface);
1009 surface->base.prepare = shm_surface_prepare;
1010 surface->base.swap = shm_surface_swap;
1011 surface->base.acquire = shm_surface_acquire;
1012 surface->base.release = shm_surface_release;
1013 surface->base.destroy = shm_surface_destroy;
1015 surface->display = display;
1016 surface->surface = wl_surface;
1017 surface->flags = flags;
1019 return &surface->base;
1023 * The following correspondences between file names and cursors was copied
1024 * from: https://bugs.kde.org/attachment.cgi?id=67313
1027 static const char *bottom_left_corners[] = {
1028 "bottom_left_corner",
1032 static const char *bottom_right_corners[] = {
1033 "bottom_right_corner",
1037 static const char *bottom_sides[] = {
1042 static const char *grabbings[] = {
1045 "208530c400c041818281048008011002"
1048 static const char *left_ptrs[] = {
1055 static const char *left_sides[] = {
1060 static const char *right_sides[] = {
1065 static const char *top_left_corners[] = {
1070 static const char *top_right_corners[] = {
1075 static const char *top_sides[] = {
1080 static const char *xterms[] = {
1086 static const char *hand1s[] = {
1090 "e29285e634086352946a0e7090d73106"
1093 static const char *watches[] = {
1096 "0426c94ea35c87780ff01dc239897213"
1099 struct cursor_alternatives {
1104 static const struct cursor_alternatives cursors[] = {
1105 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1106 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1107 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1108 {grabbings, ARRAY_LENGTH(grabbings)},
1109 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1110 {left_sides, ARRAY_LENGTH(left_sides)},
1111 {right_sides, ARRAY_LENGTH(right_sides)},
1112 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1113 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1114 {top_sides, ARRAY_LENGTH(top_sides)},
1115 {xterms, ARRAY_LENGTH(xterms)},
1116 {hand1s, ARRAY_LENGTH(hand1s)},
1117 {watches, ARRAY_LENGTH(watches)},
1121 create_cursors(struct display *display)
1125 unsigned int size = 32;
1127 struct wl_cursor *cursor;
1128 struct config_key shell_keys[] = {
1129 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1130 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
1132 struct config_section cs[] = {
1133 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1136 config_file = config_file_path("weston.ini");
1137 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1140 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1142 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1144 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1146 for (j = 0; !cursor && j < cursors[i].count; ++j)
1147 cursor = wl_cursor_theme_get_cursor(
1148 display->cursor_theme, cursors[i].names[j]);
1151 fprintf(stderr, "could not load cursor '%s'\n",
1152 cursors[i].names[0]);
1154 display->cursors[i] = cursor;
1159 destroy_cursors(struct display *display)
1161 wl_cursor_theme_destroy(display->cursor_theme);
1162 free(display->cursors);
1165 struct wl_cursor_image *
1166 display_get_pointer_image(struct display *display, int pointer)
1168 struct wl_cursor *cursor = display->cursors[pointer];
1170 return cursor ? cursor->images[0] : NULL;
1174 surface_flush(struct surface *surface)
1176 if (!surface->cairo_surface)
1179 if (surface->opaque_region) {
1180 wl_surface_set_opaque_region(surface->surface,
1181 surface->opaque_region);
1182 wl_region_destroy(surface->opaque_region);
1183 surface->opaque_region = NULL;
1186 if (surface->input_region) {
1187 wl_surface_set_input_region(surface->surface,
1188 surface->input_region);
1189 wl_region_destroy(surface->input_region);
1190 surface->input_region = NULL;
1193 surface->toysurface->swap(surface->toysurface,
1194 &surface->server_allocation);
1196 cairo_surface_destroy(surface->cairo_surface);
1197 surface->cairo_surface = NULL;
1201 window_has_focus(struct window *window)
1203 return window->focus_count > 0;
1207 window_flush(struct window *window)
1209 struct surface *surface;
1211 if (window->type == TYPE_NONE) {
1212 window->type = TYPE_TOPLEVEL;
1213 if (window->shell_surface)
1214 wl_shell_surface_set_toplevel(window->shell_surface);
1217 wl_list_for_each(surface, &window->subsurface_list, link) {
1218 if (surface == window->main_surface)
1221 surface_flush(surface);
1224 surface_flush(window->main_surface);
1228 window_get_display(struct window *window)
1230 return window->display;
1234 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1236 struct display *display = surface->window->display;
1237 struct rectangle allocation = surface->allocation;
1239 switch (surface->buffer_transform) {
1240 case WL_OUTPUT_TRANSFORM_90:
1241 case WL_OUTPUT_TRANSFORM_270:
1242 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1243 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1244 allocation.width = surface->allocation.height;
1245 allocation.height = surface->allocation.width;
1251 if (!surface->toysurface && display->dpy &&
1252 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1253 surface->toysurface =
1254 egl_window_surface_create(display,
1260 if (!surface->toysurface)
1261 surface->toysurface = shm_surface_create(display,
1263 flags, &allocation);
1265 surface->cairo_surface = surface->toysurface->prepare(
1266 surface->toysurface, dx, dy,
1267 allocation.width, allocation.height, flags);
1271 window_create_main_surface(struct window *window)
1273 struct surface *surface = window->main_surface;
1278 if (window->resizing)
1279 flags |= SURFACE_HINT_RESIZE;
1281 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1282 dx = surface->server_allocation.width -
1283 surface->allocation.width;
1285 if (window->resize_edges & WINDOW_RESIZING_TOP)
1286 dy = surface->server_allocation.height -
1287 surface->allocation.height;
1289 window->resize_edges = 0;
1291 surface_create_surface(surface, dx, dy, flags);
1295 window_get_buffer_transform(struct window *window)
1297 return window->main_surface->buffer_transform;
1301 window_set_buffer_transform(struct window *window,
1302 enum wl_output_transform transform)
1304 window->main_surface->buffer_transform = transform;
1305 wl_surface_set_buffer_transform(window->main_surface->surface,
1309 static void frame_destroy(struct frame *frame);
1312 surface_destroy(struct surface *surface)
1314 if (surface->input_region)
1315 wl_region_destroy(surface->input_region);
1317 if (surface->opaque_region)
1318 wl_region_destroy(surface->opaque_region);
1320 if (surface->subsurface)
1321 wl_subsurface_destroy(surface->subsurface);
1323 wl_surface_destroy(surface->surface);
1325 if (surface->toysurface)
1326 surface->toysurface->destroy(surface->toysurface);
1328 wl_list_remove(&surface->link);
1333 window_destroy(struct window *window)
1335 struct display *display = window->display;
1336 struct input *input;
1337 struct window_output *window_output;
1338 struct window_output *window_output_tmp;
1340 if (window->redraw_scheduled)
1341 wl_list_remove(&window->redraw_task.link);
1343 wl_list_for_each(input, &display->input_list, link) {
1344 if (input->pointer_focus == window)
1345 input->pointer_focus = NULL;
1346 if (input->keyboard_focus == window)
1347 input->keyboard_focus = NULL;
1348 if (input->focus_widget &&
1349 input->focus_widget->window == window)
1350 input->focus_widget = NULL;
1353 wl_list_for_each_safe(window_output, window_output_tmp,
1354 &window->window_output_list, link) {
1355 free (window_output);
1359 frame_destroy(window->frame);
1361 if (window->shell_surface)
1362 wl_shell_surface_destroy(window->shell_surface);
1364 surface_destroy(window->main_surface);
1366 wl_list_remove(&window->link);
1368 if (window->frame_cb)
1369 wl_callback_destroy(window->frame_cb);
1370 free(window->title);
1374 static struct widget *
1375 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1377 struct widget *child, *target;
1379 wl_list_for_each(child, &widget->child_list, link) {
1380 target = widget_find_widget(child, x, y);
1385 if (widget->allocation.x <= x &&
1386 x < widget->allocation.x + widget->allocation.width &&
1387 widget->allocation.y <= y &&
1388 y < widget->allocation.y + widget->allocation.height) {
1395 static struct widget *
1396 window_find_widget(struct window *window, int32_t x, int32_t y)
1398 struct surface *surface;
1399 struct widget *widget;
1401 wl_list_for_each(surface, &window->subsurface_list, link) {
1402 widget = widget_find_widget(surface->widget, x, y);
1410 static struct widget *
1411 widget_create(struct window *window, struct surface *surface, void *data)
1413 struct widget *widget;
1415 widget = malloc(sizeof *widget);
1416 memset(widget, 0, sizeof *widget);
1417 widget->window = window;
1418 widget->surface = surface;
1419 widget->user_data = data;
1420 widget->allocation = surface->allocation;
1421 wl_list_init(&widget->child_list);
1423 widget->tooltip = NULL;
1424 widget->tooltip_count = 0;
1425 widget->default_cursor = CURSOR_LEFT_PTR;
1431 window_add_widget(struct window *window, void *data)
1433 struct widget *widget;
1435 widget = widget_create(window, window->main_surface, data);
1436 wl_list_init(&widget->link);
1437 window->main_surface->widget = widget;
1443 widget_add_widget(struct widget *parent, void *data)
1445 struct widget *widget;
1447 widget = widget_create(parent->window, parent->surface, data);
1448 wl_list_insert(parent->child_list.prev, &widget->link);
1454 widget_destroy(struct widget *widget)
1456 struct display *display = widget->window->display;
1457 struct surface *surface = widget->surface;
1458 struct input *input;
1460 /* Destroy the sub-surface along with the root widget */
1461 if (surface->widget == widget && surface->subsurface)
1462 surface_destroy(widget->surface);
1464 if (widget->tooltip) {
1465 free(widget->tooltip);
1466 widget->tooltip = NULL;
1469 wl_list_for_each(input, &display->input_list, link) {
1470 if (input->focus_widget == widget)
1471 input->focus_widget = NULL;
1474 wl_list_remove(&widget->link);
1479 widget_set_default_cursor(struct widget *widget, int cursor)
1481 widget->default_cursor = cursor;
1485 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1487 *allocation = widget->allocation;
1491 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1493 widget->allocation.width = width;
1494 widget->allocation.height = height;
1498 widget_set_allocation(struct widget *widget,
1499 int32_t x, int32_t y, int32_t width, int32_t height)
1501 widget->allocation.x = x;
1502 widget->allocation.y = y;
1503 widget_set_size(widget, width, height);
1507 widget_set_transparent(struct widget *widget, int transparent)
1509 widget->opaque = !transparent;
1513 widget_get_user_data(struct widget *widget)
1515 return widget->user_data;
1518 static cairo_surface_t *
1519 widget_get_cairo_surface(struct widget *widget)
1521 struct surface *surface = widget->surface;
1522 struct window *window = widget->window;
1524 if (!surface->cairo_surface) {
1525 if (surface == window->main_surface)
1526 window_create_main_surface(window);
1528 surface_create_surface(surface, 0, 0, 0);
1531 return surface->cairo_surface;
1535 widget_cairo_create(struct widget *widget)
1537 struct surface *surface = widget->surface;
1538 cairo_surface_t *cairo_surface;
1541 cairo_surface = widget_get_cairo_surface(widget);
1542 cr = cairo_create(cairo_surface);
1544 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1550 widget_set_resize_handler(struct widget *widget,
1551 widget_resize_handler_t handler)
1553 widget->resize_handler = handler;
1557 widget_set_redraw_handler(struct widget *widget,
1558 widget_redraw_handler_t handler)
1560 widget->redraw_handler = handler;
1564 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1566 widget->enter_handler = handler;
1570 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1572 widget->leave_handler = handler;
1576 widget_set_motion_handler(struct widget *widget,
1577 widget_motion_handler_t handler)
1579 widget->motion_handler = handler;
1583 widget_set_button_handler(struct widget *widget,
1584 widget_button_handler_t handler)
1586 widget->button_handler = handler;
1590 widget_set_axis_handler(struct widget *widget,
1591 widget_axis_handler_t handler)
1593 widget->axis_handler = handler;
1597 widget_schedule_redraw(struct widget *widget)
1599 window_schedule_redraw(widget->window);
1603 window_get_surface(struct window *window)
1605 cairo_surface_t *cairo_surface;
1607 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1609 return cairo_surface_reference(cairo_surface);
1613 window_get_wl_surface(struct window *window)
1615 return window->main_surface->surface;
1618 struct wl_shell_surface *
1619 window_get_wl_shell_surface(struct window *window)
1621 return window->shell_surface;
1625 tooltip_redraw_handler(struct widget *widget, void *data)
1628 const int32_t r = 3;
1629 struct tooltip *tooltip = data;
1630 int32_t width, height;
1632 cr = widget_cairo_create(widget);
1633 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1634 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1637 width = widget->allocation.width;
1638 height = widget->allocation.height;
1639 rounded_rect(cr, 0, 0, width, height, r);
1641 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1642 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1645 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1646 cairo_move_to(cr, 10, 16);
1647 cairo_show_text(cr, tooltip->entry);
1651 static cairo_text_extents_t
1652 get_text_extents(struct tooltip *tooltip)
1655 cairo_text_extents_t extents;
1657 /* Use the dummy_surface because tooltip's surface was not
1658 * created yet, and parent does not have a valid surface
1659 * outside repaint, either.
1661 cr = cairo_create(tooltip->window->display->dummy_surface);
1662 cairo_text_extents(cr, tooltip->entry, &extents);
1669 window_create_tooltip(struct tooltip *tooltip)
1671 struct widget *parent = tooltip->parent;
1672 struct display *display = parent->window->display;
1673 struct window *window;
1674 const int offset_y = 27;
1675 const int margin = 3;
1676 cairo_text_extents_t extents;
1678 if (tooltip->widget)
1681 window = window_create_transient(display, parent->window, tooltip->x,
1682 tooltip->y + offset_y,
1683 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1687 tooltip->window = window;
1688 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1690 extents = get_text_extents(tooltip);
1691 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1692 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1698 widget_destroy_tooltip(struct widget *parent)
1700 struct tooltip *tooltip = parent->tooltip;
1702 parent->tooltip_count = 0;
1706 if (tooltip->widget) {
1707 widget_destroy(tooltip->widget);
1708 window_destroy(tooltip->window);
1709 tooltip->widget = NULL;
1710 tooltip->window = NULL;
1713 close(tooltip->tooltip_fd);
1714 free(tooltip->entry);
1716 parent->tooltip = NULL;
1720 tooltip_func(struct task *task, uint32_t events)
1722 struct tooltip *tooltip =
1723 container_of(task, struct tooltip, tooltip_task);
1726 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1728 window_create_tooltip(tooltip);
1731 #define TOOLTIP_TIMEOUT 500
1733 tooltip_timer_reset(struct tooltip *tooltip)
1735 struct itimerspec its;
1737 its.it_interval.tv_sec = 0;
1738 its.it_interval.tv_nsec = 0;
1739 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1740 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1741 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1742 fprintf(stderr, "could not set timerfd\n: %m");
1750 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1752 struct tooltip *tooltip = parent->tooltip;
1754 parent->tooltip_count++;
1758 tooltip_timer_reset(tooltip);
1762 /* the handler might be triggered too fast via input device motion, so
1763 * we need this check here to make sure tooltip is fully initialized */
1764 if (parent->tooltip_count > 1)
1767 tooltip = malloc(sizeof *tooltip);
1771 parent->tooltip = tooltip;
1772 tooltip->parent = parent;
1773 tooltip->widget = NULL;
1774 tooltip->window = NULL;
1777 tooltip->entry = strdup(entry);
1778 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1779 if (tooltip->tooltip_fd < 0) {
1780 fprintf(stderr, "could not create timerfd\n: %m");
1784 tooltip->tooltip_task.run = tooltip_func;
1785 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1786 EPOLLIN, &tooltip->tooltip_task);
1787 tooltip_timer_reset(tooltip);
1793 workspace_manager_state(void *data,
1794 struct workspace_manager *workspace_manager,
1798 struct display *display = data;
1800 display->workspace = current;
1801 display->workspace_count = count;
1804 static const struct workspace_manager_listener workspace_manager_listener = {
1805 workspace_manager_state
1809 frame_resize_handler(struct widget *widget,
1810 int32_t width, int32_t height, void *data)
1812 struct frame *frame = data;
1813 struct widget *child = frame->child;
1814 struct rectangle allocation;
1815 struct display *display = widget->window->display;
1816 struct surface *surface = widget->surface;
1817 struct frame_button * button;
1818 struct theme *t = display->theme;
1819 int x_l, x_r, y, w, h;
1820 int decoration_width, decoration_height;
1821 int opaque_margin, shadow_margin;
1823 switch (widget->window->type) {
1824 case TYPE_FULLSCREEN:
1825 decoration_width = 0;
1826 decoration_height = 0;
1830 allocation.width = width;
1831 allocation.height = height;
1834 wl_list_for_each(button, &frame->buttons_list, link)
1835 button->widget->opaque = 1;
1837 case TYPE_MAXIMIZED:
1838 decoration_width = t->width * 2;
1839 decoration_height = t->width + t->titlebar_height;
1841 allocation.x = t->width;
1842 allocation.y = t->titlebar_height;
1843 allocation.width = width - decoration_width;
1844 allocation.height = height - decoration_height;
1848 wl_list_for_each(button, &frame->buttons_list, link)
1849 button->widget->opaque = 0;
1852 decoration_width = (t->width + t->margin) * 2;
1853 decoration_height = t->width +
1854 t->titlebar_height + t->margin * 2;
1856 allocation.x = t->width + t->margin;
1857 allocation.y = t->titlebar_height + t->margin;
1858 allocation.width = width - decoration_width;
1859 allocation.height = height - decoration_height;
1861 opaque_margin = t->margin + t->frame_radius;
1863 wl_list_for_each(button, &frame->buttons_list, link)
1864 button->widget->opaque = 0;
1868 widget_set_allocation(child, allocation.x, allocation.y,
1869 allocation.width, allocation.height);
1871 if (child->resize_handler)
1872 child->resize_handler(child,
1877 width = child->allocation.width + decoration_width;
1878 height = child->allocation.height + decoration_height;
1880 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1882 surface->input_region =
1883 wl_compositor_create_region(display->compositor);
1884 if (widget->window->type != TYPE_FULLSCREEN) {
1885 wl_region_add(surface->input_region,
1886 shadow_margin, shadow_margin,
1887 width - 2 * shadow_margin,
1888 height - 2 * shadow_margin);
1890 wl_region_add(surface->input_region, 0, 0, width, height);
1893 widget_set_allocation(widget, 0, 0, width, height);
1896 wl_region_add(surface->opaque_region,
1897 opaque_margin, opaque_margin,
1898 widget->allocation.width - 2 * opaque_margin,
1899 widget->allocation.height - 2 * opaque_margin);
1901 /* frame internal buttons */
1902 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1903 x_l = t->width + shadow_margin;
1904 y = t->width + shadow_margin;
1905 wl_list_for_each(button, &frame->buttons_list, link) {
1906 const int button_padding = 4;
1907 w = cairo_image_surface_get_width(button->icon);
1908 h = cairo_image_surface_get_height(button->icon);
1910 if (button->decoration == FRAME_BUTTON_FANCY)
1913 if (button->align == FRAME_BUTTON_LEFT) {
1914 widget_set_allocation(button->widget,
1915 x_l, y , w + 1, h + 1);
1917 x_l += button_padding;
1920 widget_set_allocation(button->widget,
1921 x_r, y , w + 1, h + 1);
1922 x_r -= button_padding;
1928 frame_button_enter_handler(struct widget *widget,
1929 struct input *input, float x, float y, void *data)
1931 struct frame_button *frame_button = data;
1933 widget_schedule_redraw(frame_button->widget);
1934 frame_button->state = FRAME_BUTTON_OVER;
1936 return CURSOR_LEFT_PTR;
1940 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1942 struct frame_button *frame_button = data;
1944 widget_schedule_redraw(frame_button->widget);
1945 frame_button->state = FRAME_BUTTON_DEFAULT;
1949 frame_button_button_handler(struct widget *widget,
1950 struct input *input, uint32_t time,
1952 enum wl_pointer_button_state state, void *data)
1954 struct frame_button *frame_button = data;
1955 struct window *window = widget->window;
1956 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1958 if (button != BTN_LEFT)
1962 case WL_POINTER_BUTTON_STATE_PRESSED:
1963 frame_button->state = FRAME_BUTTON_ACTIVE;
1964 widget_schedule_redraw(frame_button->widget);
1966 if (frame_button->type == FRAME_BUTTON_ICON)
1967 window_show_frame_menu(window, input, time);
1969 case WL_POINTER_BUTTON_STATE_RELEASED:
1970 frame_button->state = FRAME_BUTTON_DEFAULT;
1971 widget_schedule_redraw(frame_button->widget);
1978 switch (frame_button->type) {
1979 case FRAME_BUTTON_CLOSE:
1980 if (window->close_handler)
1981 window->close_handler(window->parent,
1984 display_exit(window->display);
1986 case FRAME_BUTTON_MINIMIZE:
1987 fprintf(stderr,"Minimize stub\n");
1989 case FRAME_BUTTON_MAXIMIZE:
1990 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1993 /* Unknown operation */
1999 frame_button_motion_handler(struct widget *widget,
2000 struct input *input, uint32_t time,
2001 float x, float y, void *data)
2003 struct frame_button *frame_button = data;
2004 enum frame_button_pointer previous_button_state = frame_button->state;
2006 /* only track state for a pressed button */
2007 if (input->grab != widget)
2008 return CURSOR_LEFT_PTR;
2010 if (x > widget->allocation.x &&
2011 x < (widget->allocation.x + widget->allocation.width) &&
2012 y > widget->allocation.y &&
2013 y < (widget->allocation.y + widget->allocation.height)) {
2014 frame_button->state = FRAME_BUTTON_ACTIVE;
2016 frame_button->state = FRAME_BUTTON_DEFAULT;
2019 if (frame_button->state != previous_button_state)
2020 widget_schedule_redraw(frame_button->widget);
2022 return CURSOR_LEFT_PTR;
2026 frame_button_redraw_handler(struct widget *widget, void *data)
2028 struct frame_button *frame_button = data;
2030 int width, height, x, y;
2032 x = widget->allocation.x;
2033 y = widget->allocation.y;
2034 width = widget->allocation.width;
2035 height = widget->allocation.height;
2044 cr = widget_cairo_create(widget);
2046 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2047 cairo_set_line_width(cr, 1);
2049 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2050 cairo_rectangle (cr, x, y, 25, 16);
2052 cairo_stroke_preserve(cr);
2054 switch (frame_button->state) {
2055 case FRAME_BUTTON_DEFAULT:
2056 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2058 case FRAME_BUTTON_OVER:
2059 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2061 case FRAME_BUTTON_ACTIVE:
2062 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2071 cairo_set_source_surface(cr, frame_button->icon, x, y);
2077 static struct widget *
2078 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2079 enum frame_button_align align, enum frame_button_decoration style)
2081 struct frame_button *frame_button;
2082 const char *icon = data;
2084 frame_button = malloc (sizeof *frame_button);
2085 memset(frame_button, 0, sizeof *frame_button);
2087 frame_button->icon = cairo_image_surface_create_from_png(icon);
2088 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2089 frame_button->frame = frame;
2090 frame_button->type = type;
2091 frame_button->align = align;
2092 frame_button->decoration = style;
2094 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2096 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2097 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2098 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2099 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
2100 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
2101 return frame_button->widget;
2105 frame_button_destroy(struct frame_button *frame_button)
2107 widget_destroy(frame_button->widget);
2108 wl_list_remove(&frame_button->link);
2109 cairo_surface_destroy(frame_button->icon);
2116 frame_redraw_handler(struct widget *widget, void *data)
2119 struct window *window = widget->window;
2120 struct theme *t = window->display->theme;
2123 if (window->type == TYPE_FULLSCREEN)
2126 cr = widget_cairo_create(widget);
2128 if (window->focus_count)
2129 flags |= THEME_FRAME_ACTIVE;
2130 if (window->type == TYPE_MAXIMIZED)
2131 flags |= THEME_FRAME_MAXIMIZED;
2132 theme_render_frame(t, cr, widget->allocation.width,
2133 widget->allocation.height, window->title, flags);
2139 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
2141 struct theme *t = frame->widget->window->display->theme;
2142 struct window *window = frame->widget->window;
2145 if (window->type != TYPE_TOPLEVEL)
2146 return CURSOR_LEFT_PTR;
2148 location = theme_get_location(t, input->sx, input->sy,
2149 frame->widget->allocation.width,
2150 frame->widget->allocation.height,
2151 window->type == TYPE_MAXIMIZED ?
2152 THEME_FRAME_MAXIMIZED : 0);
2155 case THEME_LOCATION_RESIZING_TOP:
2157 case THEME_LOCATION_RESIZING_BOTTOM:
2158 return CURSOR_BOTTOM;
2159 case THEME_LOCATION_RESIZING_LEFT:
2161 case THEME_LOCATION_RESIZING_RIGHT:
2162 return CURSOR_RIGHT;
2163 case THEME_LOCATION_RESIZING_TOP_LEFT:
2164 return CURSOR_TOP_LEFT;
2165 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2166 return CURSOR_TOP_RIGHT;
2167 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2168 return CURSOR_BOTTOM_LEFT;
2169 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2170 return CURSOR_BOTTOM_RIGHT;
2171 case THEME_LOCATION_EXTERIOR:
2172 case THEME_LOCATION_TITLEBAR:
2174 return CURSOR_LEFT_PTR;
2179 frame_menu_func(struct window *window, int index, void *data)
2181 struct display *display;
2185 if (window->close_handler)
2186 window->close_handler(window->parent,
2189 display_exit(window->display);
2191 case 1: /* move to workspace above */
2192 display = window->display;
2193 if (display->workspace > 0)
2194 workspace_manager_move_surface(
2195 display->workspace_manager,
2196 window->main_surface->surface,
2197 display->workspace - 1);
2199 case 2: /* move to workspace below */
2200 display = window->display;
2201 if (display->workspace < display->workspace_count - 1)
2202 workspace_manager_move_surface(
2203 display->workspace_manager,
2204 window->main_surface->surface,
2205 display->workspace + 1);
2207 case 3: /* fullscreen */
2208 /* we don't have a way to get out of fullscreen for now */
2209 if (window->fullscreen_handler)
2210 window->fullscreen_handler(window, window->user_data);
2216 window_show_frame_menu(struct window *window,
2217 struct input *input, uint32_t time)
2222 static const char *entries[] = {
2224 "Move to workspace above", "Move to workspace below",
2228 if (window->fullscreen_handler)
2229 count = ARRAY_LENGTH(entries);
2231 count = ARRAY_LENGTH(entries) - 1;
2233 input_get_position(input, &x, &y);
2234 window_show_menu(window->display, input, time, window,
2235 x - 10, y - 10, frame_menu_func, entries, count);
2239 frame_enter_handler(struct widget *widget,
2240 struct input *input, float x, float y, void *data)
2242 return frame_get_pointer_image_for_location(data, input);
2246 frame_motion_handler(struct widget *widget,
2247 struct input *input, uint32_t time,
2248 float x, float y, void *data)
2250 return frame_get_pointer_image_for_location(data, input);
2254 frame_button_handler(struct widget *widget,
2255 struct input *input, uint32_t time,
2256 uint32_t button, enum wl_pointer_button_state state,
2260 struct frame *frame = data;
2261 struct window *window = widget->window;
2262 struct display *display = window->display;
2265 if (window->type != TYPE_TOPLEVEL)
2268 location = theme_get_location(display->theme, input->sx, input->sy,
2269 frame->widget->allocation.width,
2270 frame->widget->allocation.height,
2271 window->type == TYPE_MAXIMIZED ?
2272 THEME_FRAME_MAXIMIZED : 0);
2274 if (window->display->shell && button == BTN_LEFT &&
2275 state == WL_POINTER_BUTTON_STATE_PRESSED) {
2277 case THEME_LOCATION_TITLEBAR:
2278 if (!window->shell_surface)
2280 input_ungrab(input);
2281 wl_shell_surface_move(window->shell_surface,
2282 input_get_seat(input),
2285 case THEME_LOCATION_RESIZING_TOP:
2286 case THEME_LOCATION_RESIZING_BOTTOM:
2287 case THEME_LOCATION_RESIZING_LEFT:
2288 case THEME_LOCATION_RESIZING_RIGHT:
2289 case THEME_LOCATION_RESIZING_TOP_LEFT:
2290 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2291 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2292 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2293 if (!window->shell_surface)
2295 input_ungrab(input);
2297 window->resizing = 1;
2298 wl_shell_surface_resize(window->shell_surface,
2299 input_get_seat(input),
2300 display->serial, location);
2303 } else if (button == BTN_RIGHT &&
2304 state == WL_POINTER_BUTTON_STATE_PRESSED) {
2305 window_show_frame_menu(window, input, time);
2310 frame_create(struct window *window, void *data)
2312 struct frame *frame;
2314 frame = malloc(sizeof *frame);
2315 memset(frame, 0, sizeof *frame);
2317 frame->widget = window_add_widget(window, frame);
2318 frame->child = widget_add_widget(frame->widget, data);
2320 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2321 widget_set_resize_handler(frame->widget, frame_resize_handler);
2322 widget_set_enter_handler(frame->widget, frame_enter_handler);
2323 widget_set_motion_handler(frame->widget, frame_motion_handler);
2324 widget_set_button_handler(frame->widget, frame_button_handler);
2326 /* Create empty list for frame buttons */
2327 wl_list_init(&frame->buttons_list);
2329 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2330 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2332 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2333 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2335 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2336 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2338 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2339 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2341 window->frame = frame;
2343 return frame->child;
2347 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2349 struct display *display = widget->window->display;
2350 struct theme *t = display->theme;
2351 int decoration_width, decoration_height;
2353 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2355 if (widget->window->type != TYPE_FULLSCREEN) {
2356 decoration_width = (t->width + margin) * 2;
2357 decoration_height = t->width +
2358 t->titlebar_height + margin * 2;
2360 width = child_width + decoration_width;
2361 height = child_height + decoration_height;
2363 width = child_width;
2364 height = child_height;
2367 window_schedule_resize(widget->window, width, height);
2371 frame_destroy(struct frame *frame)
2373 struct frame_button *button, *tmp;
2375 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2376 frame_button_destroy(button);
2378 /* frame->child must be destroyed by the application */
2379 widget_destroy(frame->widget);
2384 input_set_focus_widget(struct input *input, struct widget *focus,
2387 struct widget *old, *widget;
2390 if (focus == input->focus_widget)
2393 old = input->focus_widget;
2397 widget = input->grab;
2398 if (widget->leave_handler)
2399 widget->leave_handler(old, input, widget->user_data);
2400 input->focus_widget = NULL;
2406 widget = input->grab;
2407 input->focus_widget = focus;
2408 if (widget->enter_handler)
2409 cursor = widget->enter_handler(focus, input, x, y,
2412 cursor = widget->default_cursor;
2414 input_set_pointer_image(input, cursor);
2419 input_grab(struct input *input, struct widget *widget, uint32_t button)
2421 input->grab = widget;
2422 input->grab_button = button;
2426 input_ungrab(struct input *input)
2428 struct widget *widget;
2431 if (input->pointer_focus) {
2432 widget = window_find_widget(input->pointer_focus,
2433 input->sx, input->sy);
2434 input_set_focus_widget(input, widget, input->sx, input->sy);
2439 input_remove_pointer_focus(struct input *input)
2441 struct window *window = input->pointer_focus;
2446 input_set_focus_widget(input, NULL, 0, 0);
2448 input->pointer_focus = NULL;
2449 input->current_cursor = CURSOR_UNSET;
2453 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2454 uint32_t serial, struct wl_surface *surface,
2455 wl_fixed_t sx_w, wl_fixed_t sy_w)
2457 struct input *input = data;
2458 struct window *window;
2459 struct widget *widget;
2460 float sx = wl_fixed_to_double(sx_w);
2461 float sy = wl_fixed_to_double(sy_w);
2464 /* enter event for a window we've just destroyed */
2468 input->display->serial = serial;
2469 input->pointer_enter_serial = serial;
2470 input->pointer_focus = wl_surface_get_user_data(surface);
2471 window = input->pointer_focus;
2473 if (window->resizing) {
2474 window->resizing = 0;
2475 /* Schedule a redraw to free the pool */
2476 window_schedule_redraw(window);
2482 widget = window_find_widget(window, sx, sy);
2483 input_set_focus_widget(input, widget, sx, sy);
2487 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2488 uint32_t serial, struct wl_surface *surface)
2490 struct input *input = data;
2492 input->display->serial = serial;
2493 input_remove_pointer_focus(input);
2497 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2498 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2500 struct input *input = data;
2501 struct window *window = input->pointer_focus;
2502 struct widget *widget;
2504 float sx = wl_fixed_to_double(sx_w);
2505 float sy = wl_fixed_to_double(sy_w);
2513 if (!(input->grab && input->grab_button)) {
2514 widget = window_find_widget(window, sx, sy);
2515 input_set_focus_widget(input, widget, sx, sy);
2519 widget = input->grab;
2521 widget = input->focus_widget;
2522 if (widget && widget->motion_handler)
2523 cursor = widget->motion_handler(input->focus_widget,
2524 input, time, sx, sy,
2527 cursor = input->focus_widget->default_cursor;
2529 input_set_pointer_image(input, cursor);
2533 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2534 uint32_t time, uint32_t button, uint32_t state_w)
2536 struct input *input = data;
2537 struct widget *widget;
2538 enum wl_pointer_button_state state = state_w;
2540 input->display->serial = serial;
2541 if (input->focus_widget && input->grab == NULL &&
2542 state == WL_POINTER_BUTTON_STATE_PRESSED)
2543 input_grab(input, input->focus_widget, button);
2545 widget = input->grab;
2546 if (widget && widget->button_handler)
2547 (*widget->button_handler)(widget,
2550 input->grab->user_data);
2552 if (input->grab && input->grab_button == button &&
2553 state == WL_POINTER_BUTTON_STATE_RELEASED)
2554 input_ungrab(input);
2558 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2559 uint32_t time, uint32_t axis, wl_fixed_t value)
2561 struct input *input = data;
2562 struct widget *widget;
2564 widget = input->focus_widget;
2566 widget = input->grab;
2567 if (widget && widget->axis_handler)
2568 (*widget->axis_handler)(widget,
2574 static const struct wl_pointer_listener pointer_listener = {
2575 pointer_handle_enter,
2576 pointer_handle_leave,
2577 pointer_handle_motion,
2578 pointer_handle_button,
2579 pointer_handle_axis,
2583 input_remove_keyboard_focus(struct input *input)
2585 struct window *window = input->keyboard_focus;
2586 struct itimerspec its;
2588 its.it_interval.tv_sec = 0;
2589 its.it_interval.tv_nsec = 0;
2590 its.it_value.tv_sec = 0;
2591 its.it_value.tv_nsec = 0;
2592 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2597 window->focus_count--;
2598 if (window->keyboard_focus_handler)
2599 (*window->keyboard_focus_handler)(window, NULL,
2602 input->keyboard_focus = NULL;
2606 keyboard_repeat_func(struct task *task, uint32_t events)
2608 struct input *input =
2609 container_of(task, struct input, repeat_task);
2610 struct window *window = input->keyboard_focus;
2613 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2614 /* If we change the timer between the fd becoming
2615 * readable and getting here, there'll be nothing to
2616 * read and we get EAGAIN. */
2619 if (window && window->key_handler) {
2620 (*window->key_handler)(window, input, input->repeat_time,
2621 input->repeat_key, input->repeat_sym,
2622 WL_KEYBOARD_KEY_STATE_PRESSED,
2628 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2629 uint32_t format, int fd, uint32_t size)
2631 struct input *input = data;
2639 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2644 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2645 if (map_str == MAP_FAILED) {
2650 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2652 XKB_KEYMAP_FORMAT_TEXT_V1,
2654 munmap(map_str, size);
2657 if (!input->xkb.keymap) {
2658 fprintf(stderr, "failed to compile keymap\n");
2662 input->xkb.state = xkb_state_new(input->xkb.keymap);
2663 if (!input->xkb.state) {
2664 fprintf(stderr, "failed to create XKB state\n");
2665 xkb_map_unref(input->xkb.keymap);
2666 input->xkb.keymap = NULL;
2670 input->xkb.control_mask =
2671 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2672 input->xkb.alt_mask =
2673 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2674 input->xkb.shift_mask =
2675 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2679 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2680 uint32_t serial, struct wl_surface *surface,
2681 struct wl_array *keys)
2683 struct input *input = data;
2684 struct window *window;
2686 input->display->serial = serial;
2687 input->keyboard_focus = wl_surface_get_user_data(surface);
2689 window = input->keyboard_focus;
2690 window->focus_count++;
2691 if (window->keyboard_focus_handler)
2692 (*window->keyboard_focus_handler)(window,
2693 input, window->user_data);
2697 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2698 uint32_t serial, struct wl_surface *surface)
2700 struct input *input = data;
2702 input->display->serial = serial;
2703 input_remove_keyboard_focus(input);
2707 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2708 uint32_t serial, uint32_t time, uint32_t key,
2711 struct input *input = data;
2712 struct window *window = input->keyboard_focus;
2713 uint32_t code, num_syms;
2714 enum wl_keyboard_key_state state = state_w;
2715 const xkb_keysym_t *syms;
2717 struct itimerspec its;
2719 input->display->serial = serial;
2721 if (!window || !input->xkb.state)
2724 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2726 sym = XKB_KEY_NoSymbol;
2730 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2731 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2732 window_set_maximized(window,
2733 window->type != TYPE_MAXIMIZED);
2734 } else if (sym == XKB_KEY_F11 &&
2735 window->fullscreen_handler &&
2736 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2737 window->fullscreen_handler(window, window->user_data);
2738 } else if (sym == XKB_KEY_F4 &&
2739 input->modifiers == MOD_ALT_MASK &&
2740 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2741 if (window->close_handler)
2742 window->close_handler(window->parent,
2745 display_exit(window->display);
2746 } else if (window->key_handler) {
2747 (*window->key_handler)(window, input, time, key,
2748 sym, state, window->user_data);
2751 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2752 key == input->repeat_key) {
2753 its.it_interval.tv_sec = 0;
2754 its.it_interval.tv_nsec = 0;
2755 its.it_value.tv_sec = 0;
2756 its.it_value.tv_nsec = 0;
2757 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2758 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2759 input->repeat_sym = sym;
2760 input->repeat_key = key;
2761 input->repeat_time = time;
2762 its.it_interval.tv_sec = 0;
2763 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2764 its.it_value.tv_sec = 0;
2765 its.it_value.tv_nsec = 400 * 1000 * 1000;
2766 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2771 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2772 uint32_t serial, uint32_t mods_depressed,
2773 uint32_t mods_latched, uint32_t mods_locked,
2776 struct input *input = data;
2777 xkb_mod_mask_t mask;
2779 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2780 mods_locked, 0, 0, group);
2781 mask = xkb_state_serialize_mods(input->xkb.state,
2782 XKB_STATE_DEPRESSED |
2784 input->modifiers = 0;
2785 if (mask & input->xkb.control_mask)
2786 input->modifiers |= MOD_CONTROL_MASK;
2787 if (mask & input->xkb.alt_mask)
2788 input->modifiers |= MOD_ALT_MASK;
2789 if (mask & input->xkb.shift_mask)
2790 input->modifiers |= MOD_SHIFT_MASK;
2793 static const struct wl_keyboard_listener keyboard_listener = {
2794 keyboard_handle_keymap,
2795 keyboard_handle_enter,
2796 keyboard_handle_leave,
2797 keyboard_handle_key,
2798 keyboard_handle_modifiers,
2802 seat_handle_capabilities(void *data, struct wl_seat *seat,
2803 enum wl_seat_capability caps)
2805 struct input *input = data;
2807 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2808 input->pointer = wl_seat_get_pointer(seat);
2809 wl_pointer_set_user_data(input->pointer, input);
2810 wl_pointer_add_listener(input->pointer, &pointer_listener,
2812 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2813 wl_pointer_destroy(input->pointer);
2814 input->pointer = NULL;
2817 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2818 input->keyboard = wl_seat_get_keyboard(seat);
2819 wl_keyboard_set_user_data(input->keyboard, input);
2820 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2822 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2823 wl_keyboard_destroy(input->keyboard);
2824 input->keyboard = NULL;
2828 static const struct wl_seat_listener seat_listener = {
2829 seat_handle_capabilities,
2833 input_get_position(struct input *input, int32_t *x, int32_t *y)
2840 input_get_display(struct input *input)
2842 return input->display;
2846 input_get_seat(struct input *input)
2852 input_get_modifiers(struct input *input)
2854 return input->modifiers;
2858 input_get_focus_widget(struct input *input)
2860 return input->focus_widget;
2864 struct wl_data_offer *offer;
2865 struct input *input;
2866 struct wl_array types;
2869 struct task io_task;
2877 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2879 struct data_offer *offer = data;
2882 p = wl_array_add(&offer->types, sizeof *p);
2886 static const struct wl_data_offer_listener data_offer_listener = {
2891 data_offer_destroy(struct data_offer *offer)
2896 if (offer->refcount == 0) {
2897 wl_data_offer_destroy(offer->offer);
2898 for (p = offer->types.data; *p; p++)
2900 wl_array_release(&offer->types);
2906 data_device_data_offer(void *data,
2907 struct wl_data_device *data_device,
2908 struct wl_data_offer *_offer)
2910 struct data_offer *offer;
2912 offer = malloc(sizeof *offer);
2914 wl_array_init(&offer->types);
2915 offer->refcount = 1;
2916 offer->input = data;
2917 offer->offer = _offer;
2918 wl_data_offer_add_listener(offer->offer,
2919 &data_offer_listener, offer);
2923 data_device_enter(void *data, struct wl_data_device *data_device,
2924 uint32_t serial, struct wl_surface *surface,
2925 wl_fixed_t x_w, wl_fixed_t y_w,
2926 struct wl_data_offer *offer)
2928 struct input *input = data;
2929 struct window *window;
2931 float x = wl_fixed_to_double(x_w);
2932 float y = wl_fixed_to_double(y_w);
2935 input->pointer_enter_serial = serial;
2936 window = wl_surface_get_user_data(surface);
2937 input->pointer_focus = window;
2940 input->drag_offer = wl_data_offer_get_user_data(offer);
2942 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2945 types_data = input->drag_offer->types.data;
2947 input->drag_offer = NULL;
2951 window = input->pointer_focus;
2952 if (window->data_handler)
2953 window->data_handler(window, input, x, y, types_data,
2958 data_device_leave(void *data, struct wl_data_device *data_device)
2960 struct input *input = data;
2962 if (input->drag_offer) {
2963 data_offer_destroy(input->drag_offer);
2964 input->drag_offer = NULL;
2969 data_device_motion(void *data, struct wl_data_device *data_device,
2970 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2972 struct input *input = data;
2973 struct window *window = input->pointer_focus;
2974 float x = wl_fixed_to_double(x_w);
2975 float y = wl_fixed_to_double(y_w);
2981 if (input->drag_offer)
2982 types_data = input->drag_offer->types.data;
2986 if (window->data_handler)
2987 window->data_handler(window, input, x, y, types_data,
2992 data_device_drop(void *data, struct wl_data_device *data_device)
2994 struct input *input = data;
2995 struct window *window = input->pointer_focus;
2997 if (window->drop_handler)
2998 window->drop_handler(window, input,
2999 input->sx, input->sy, window->user_data);
3003 data_device_selection(void *data,
3004 struct wl_data_device *wl_data_device,
3005 struct wl_data_offer *offer)
3007 struct input *input = data;
3010 if (input->selection_offer)
3011 data_offer_destroy(input->selection_offer);
3014 input->selection_offer = wl_data_offer_get_user_data(offer);
3015 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3018 input->selection_offer = NULL;
3022 static const struct wl_data_device_listener data_device_listener = {
3023 data_device_data_offer,
3028 data_device_selection
3032 input_set_pointer_image_index(struct input *input, int index)
3034 struct wl_buffer *buffer;
3035 struct wl_cursor *cursor;
3036 struct wl_cursor_image *image;
3038 if (!input->pointer)
3041 cursor = input->display->cursors[input->current_cursor];
3045 if (index >= (int) cursor->image_count) {
3046 fprintf(stderr, "cursor index out of range\n");
3050 image = cursor->images[index];
3051 buffer = wl_cursor_image_get_buffer(image);
3055 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3056 input->pointer_surface,
3057 image->hotspot_x, image->hotspot_y);
3058 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3059 wl_surface_damage(input->pointer_surface, 0, 0,
3060 image->width, image->height);
3061 wl_surface_commit(input->pointer_surface);
3064 static const struct wl_callback_listener pointer_surface_listener;
3067 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3070 struct input *input = data;
3071 struct wl_cursor *cursor;
3075 assert(callback == input->cursor_frame_cb);
3076 wl_callback_destroy(callback);
3077 input->cursor_frame_cb = NULL;
3080 if (!input->pointer)
3083 if (input->current_cursor == CURSOR_BLANK) {
3084 wl_pointer_set_cursor(input->pointer,
3085 input->pointer_enter_serial,
3090 if (input->current_cursor == CURSOR_UNSET)
3092 cursor = input->display->cursors[input->current_cursor];
3096 /* FIXME We don't have the current time on the first call so we set
3097 * the animation start to the time of the first frame callback. */
3099 input->cursor_anim_start = 0;
3100 else if (input->cursor_anim_start == 0)
3101 input->cursor_anim_start = time;
3103 if (time == 0 || input->cursor_anim_start == 0)
3106 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3108 if (cursor->image_count > 1) {
3109 input->cursor_frame_cb =
3110 wl_surface_frame(input->pointer_surface);
3111 wl_callback_add_listener(input->cursor_frame_cb,
3112 &pointer_surface_listener, input);
3115 input_set_pointer_image_index(input, i);
3118 static const struct wl_callback_listener pointer_surface_listener = {
3119 pointer_surface_frame_callback
3123 input_set_pointer_image(struct input *input, int pointer)
3127 if (!input->pointer)
3130 if (input->pointer_enter_serial > input->cursor_serial)
3133 if (!force && pointer == input->current_cursor)
3136 input->current_cursor = pointer;
3137 input->cursor_serial = input->pointer_enter_serial;
3138 if (!input->cursor_frame_cb)
3139 pointer_surface_frame_callback(input, NULL, 0);
3141 /* The current frame callback may be stuck if, for instance,
3142 * the set cursor request was processed by the server after
3143 * this client lost the focus. In this case the cursor surface
3144 * might not be mapped and the frame callback wouldn't ever
3145 * complete. Send a set_cursor and attach to try to map the
3146 * cursor surface again so that the callback will finish */
3147 input_set_pointer_image_index(input, 0);
3151 struct wl_data_device *
3152 input_get_data_device(struct input *input)
3154 return input->data_device;
3158 input_set_selection(struct input *input,
3159 struct wl_data_source *source, uint32_t time)
3161 wl_data_device_set_selection(input->data_device, source, time);
3165 input_accept(struct input *input, const char *type)
3167 wl_data_offer_accept(input->drag_offer->offer,
3168 input->pointer_enter_serial, type);
3172 offer_io_func(struct task *task, uint32_t events)
3174 struct data_offer *offer =
3175 container_of(task, struct data_offer, io_task);
3179 len = read(offer->fd, buffer, sizeof buffer);
3180 offer->func(buffer, len,
3181 offer->x, offer->y, offer->user_data);
3185 data_offer_destroy(offer);
3190 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3191 data_func_t func, void *user_data)
3195 if (pipe2(p, O_CLOEXEC) == -1)
3198 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3201 offer->io_task.run = offer_io_func;
3205 offer->user_data = user_data;
3207 display_watch_fd(offer->input->display,
3208 offer->fd, EPOLLIN, &offer->io_task);
3212 input_receive_drag_data(struct input *input, const char *mime_type,
3213 data_func_t func, void *data)
3215 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3216 input->drag_offer->x = input->sx;
3217 input->drag_offer->y = input->sy;
3221 input_receive_selection_data(struct input *input, const char *mime_type,
3222 data_func_t func, void *data)
3226 if (input->selection_offer == NULL)
3229 for (p = input->selection_offer->types.data; *p; p++)
3230 if (strcmp(mime_type, *p) == 0)
3236 data_offer_receive_data(input->selection_offer,
3237 mime_type, func, data);
3242 input_receive_selection_data_to_fd(struct input *input,
3243 const char *mime_type, int fd)
3245 if (input->selection_offer)
3246 wl_data_offer_receive(input->selection_offer->offer,
3253 window_move(struct window *window, struct input *input, uint32_t serial)
3255 if (!window->shell_surface)
3258 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3262 surface_set_synchronized(struct surface *surface)
3264 if (!surface->subsurface)
3267 if (surface->synchronized)
3270 wl_subsurface_set_sync(surface->subsurface);
3271 surface->synchronized = 1;
3275 surface_set_synchronized_default(struct surface *surface)
3277 if (!surface->subsurface)
3280 if (surface->synchronized == surface->synchronized_default)
3283 if (surface->synchronized_default)
3284 wl_subsurface_set_sync(surface->subsurface);
3286 wl_subsurface_set_desync(surface->subsurface);
3288 surface->synchronized = surface->synchronized_default;
3292 surface_resize(struct surface *surface)
3294 struct widget *widget = surface->widget;
3295 struct wl_compositor *compositor = widget->window->display->compositor;
3297 if (surface->input_region) {
3298 wl_region_destroy(surface->input_region);
3299 surface->input_region = NULL;
3302 if (surface->opaque_region)
3303 wl_region_destroy(surface->opaque_region);
3305 surface->opaque_region = wl_compositor_create_region(compositor);
3307 if (widget->resize_handler)
3308 widget->resize_handler(widget,
3309 widget->allocation.width,
3310 widget->allocation.height,
3313 if (surface->subsurface &&
3314 (surface->allocation.x != widget->allocation.x ||
3315 surface->allocation.y != widget->allocation.y)) {
3316 wl_subsurface_set_position(surface->subsurface,
3317 widget->allocation.x,
3318 widget->allocation.y);
3320 if (surface->allocation.width != widget->allocation.width ||
3321 surface->allocation.height != widget->allocation.height) {
3322 window_schedule_redraw(widget->window);
3324 surface->allocation = widget->allocation;
3327 wl_region_add(surface->opaque_region, 0, 0,
3328 widget->allocation.width,
3329 widget->allocation.height);
3333 idle_resize(struct window *window)
3335 struct surface *surface;
3337 window->resize_needed = 0;
3339 widget_set_allocation(window->main_surface->widget,
3340 window->pending_allocation.x,
3341 window->pending_allocation.y,
3342 window->pending_allocation.width,
3343 window->pending_allocation.height);
3345 surface_resize(window->main_surface);
3347 /* The main surface is in the list, too. Main surface's
3348 * resize_handler is responsible for calling widget_set_allocation()
3349 * on all sub-surface root widgets, so they will be resized
3352 wl_list_for_each(surface, &window->subsurface_list, link) {
3353 if (surface == window->main_surface)
3356 surface_set_synchronized(surface);
3357 surface_resize(surface);
3362 window_schedule_resize(struct window *window, int width, int height)
3364 window->pending_allocation.x = 0;
3365 window->pending_allocation.y = 0;
3366 window->pending_allocation.width = width;
3367 window->pending_allocation.height = height;
3369 if (window->min_allocation.width == 0)
3370 window->min_allocation = window->pending_allocation;
3371 if (window->pending_allocation.width < window->min_allocation.width)
3372 window->pending_allocation.width = window->min_allocation.width;
3373 if (window->pending_allocation.height < window->min_allocation.height)
3374 window->pending_allocation.height = window->min_allocation.height;
3376 window->resize_needed = 1;
3377 window_schedule_redraw(window);
3381 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3383 window_schedule_resize(widget->window, width, height);
3387 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3390 wl_shell_surface_pong(shell_surface, serial);
3394 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3395 uint32_t edges, int32_t width, int32_t height)
3397 struct window *window = data;
3399 window->resize_edges = edges;
3400 window_schedule_resize(window, width, height);
3404 menu_destroy(struct menu *menu)
3406 widget_destroy(menu->widget);
3407 window_destroy(menu->window);
3412 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3414 struct window *window = data;
3415 struct menu *menu = window->main_surface->widget->user_data;
3417 /* FIXME: Need more context in this event, at least the input
3418 * device. Or just use wl_callback. And this really needs to
3419 * be a window vfunc that the menu can set. And we need the
3422 input_ungrab(menu->input);
3426 static const struct wl_shell_surface_listener shell_surface_listener = {
3433 window_get_allocation(struct window *window,
3434 struct rectangle *allocation)
3436 *allocation = window->main_surface->allocation;
3440 widget_redraw(struct widget *widget)
3442 struct widget *child;
3444 if (widget->redraw_handler)
3445 widget->redraw_handler(widget, widget->user_data);
3446 wl_list_for_each(child, &widget->child_list, link)
3447 widget_redraw(child);
3451 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3453 struct window *window = data;
3455 assert(callback == window->frame_cb);
3456 wl_callback_destroy(callback);
3457 window->frame_cb = 0;
3458 window->redraw_scheduled = 0;
3459 if (window->redraw_needed)
3460 window_schedule_redraw(window);
3463 static const struct wl_callback_listener listener = {
3468 idle_redraw(struct task *task, uint32_t events)
3470 struct window *window = container_of(task, struct window, redraw_task);
3471 struct surface *surface;
3473 if (window->resize_needed)
3474 idle_resize(window);
3476 wl_list_for_each(surface, &window->subsurface_list, link)
3477 widget_redraw(surface->widget);
3479 window->redraw_needed = 0;
3480 wl_list_init(&window->redraw_task.link);
3482 window->frame_cb = wl_surface_frame(window->main_surface->surface);
3483 wl_callback_add_listener(window->frame_cb, &listener, window);
3484 window_flush(window);
3486 wl_list_for_each(surface, &window->subsurface_list, link)
3487 surface_set_synchronized_default(surface);
3491 window_schedule_redraw(struct window *window)
3493 window->redraw_needed = 1;
3494 if (window->configure_requests)
3496 if (!window->redraw_scheduled) {
3497 window->redraw_task.run = idle_redraw;
3498 display_defer(window->display, &window->redraw_task);
3499 window->redraw_scheduled = 1;
3504 window_is_fullscreen(struct window *window)
3506 return window->type == TYPE_FULLSCREEN;
3510 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3512 struct window *window = data;
3514 wl_callback_destroy(callback);
3515 window->configure_requests--;
3517 if (!window->configure_requests)
3518 window_schedule_redraw(window);
3521 static struct wl_callback_listener configure_request_listener = {
3522 configure_request_completed,
3526 window_defer_redraw_until_configure(struct window* window)
3528 struct wl_callback *callback;
3530 if (window->redraw_scheduled) {
3531 wl_list_remove(&window->redraw_task.link);
3532 window->redraw_scheduled = 0;
3534 if (window->frame_cb) {
3535 wl_callback_destroy(window->frame_cb);
3536 window->frame_cb = 0;
3539 callback = wl_display_sync(window->display->display);
3540 wl_callback_add_listener(callback, &configure_request_listener, window);
3541 window->configure_requests++;
3545 window_set_fullscreen(struct window *window, int fullscreen)
3547 if (!window->display->shell)
3550 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3554 window->saved_type = window->type;
3555 if (window->type == TYPE_TOPLEVEL) {
3556 window->saved_allocation = window->main_surface->allocation;
3558 window->type = TYPE_FULLSCREEN;
3559 wl_shell_surface_set_fullscreen(window->shell_surface,
3560 window->fullscreen_method,
3562 window_defer_redraw_until_configure (window);
3564 if (window->saved_type == TYPE_MAXIMIZED) {
3565 window_set_maximized(window, 1);
3567 window->type = TYPE_TOPLEVEL;
3568 wl_shell_surface_set_toplevel(window->shell_surface);
3569 window_schedule_resize(window,
3570 window->saved_allocation.width,
3571 window->saved_allocation.height);
3578 window_set_fullscreen_method(struct window *window,
3579 enum wl_shell_surface_fullscreen_method method)
3581 window->fullscreen_method = method;
3585 window_is_maximized(struct window *window)
3587 return window->type == TYPE_MAXIMIZED;
3591 window_set_maximized(struct window *window, int maximized)
3593 if (!window->display->shell)
3596 if ((window->type == TYPE_MAXIMIZED) == maximized)
3599 if (window->type == TYPE_TOPLEVEL) {
3600 window->saved_allocation = window->main_surface->allocation;
3601 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3602 window->type = TYPE_MAXIMIZED;
3603 window_defer_redraw_until_configure(window);
3604 } else if (window->type == TYPE_FULLSCREEN) {
3605 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3606 window->type = TYPE_MAXIMIZED;
3607 window_defer_redraw_until_configure(window);
3609 wl_shell_surface_set_toplevel(window->shell_surface);
3610 window->type = TYPE_TOPLEVEL;
3611 window_schedule_resize(window,
3612 window->saved_allocation.width,
3613 window->saved_allocation.height);
3618 window_set_user_data(struct window *window, void *data)
3620 window->user_data = data;
3624 window_get_user_data(struct window *window)
3626 return window->user_data;
3630 window_set_key_handler(struct window *window,
3631 window_key_handler_t handler)
3633 window->key_handler = handler;
3637 window_set_keyboard_focus_handler(struct window *window,
3638 window_keyboard_focus_handler_t handler)
3640 window->keyboard_focus_handler = handler;
3644 window_set_data_handler(struct window *window, window_data_handler_t handler)
3646 window->data_handler = handler;
3650 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3652 window->drop_handler = handler;
3656 window_set_close_handler(struct window *window,
3657 window_close_handler_t handler)
3659 window->close_handler = handler;
3663 window_set_fullscreen_handler(struct window *window,
3664 window_fullscreen_handler_t handler)
3666 window->fullscreen_handler = handler;
3670 window_set_output_handler(struct window *window,
3671 window_output_handler_t handler)
3673 window->output_handler = handler;
3677 window_set_title(struct window *window, const char *title)
3679 free(window->title);
3680 window->title = strdup(title);
3681 if (window->shell_surface)
3682 wl_shell_surface_set_title(window->shell_surface, title);
3686 window_get_title(struct window *window)
3688 return window->title;
3692 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3694 struct text_cursor_position *text_cursor_position =
3695 window->display->text_cursor_position;
3697 if (!text_cursor_position)
3700 text_cursor_position_notify(text_cursor_position,
3701 window->main_surface->surface,
3702 wl_fixed_from_int(x),
3703 wl_fixed_from_int(y));
3707 window_damage(struct window *window, int32_t x, int32_t y,
3708 int32_t width, int32_t height)
3710 wl_surface_damage(window->main_surface->surface, x, y, width, height);
3714 surface_enter(void *data,
3715 struct wl_surface *wl_surface, struct wl_output *wl_output)
3717 struct window *window = data;
3718 struct output *output;
3719 struct output *output_found = NULL;
3720 struct window_output *window_output;
3722 wl_list_for_each(output, &window->display->output_list, link) {
3723 if (output->output == wl_output) {
3724 output_found = output;
3732 window_output = malloc (sizeof *window_output);
3733 window_output->output = output_found;
3735 wl_list_insert (&window->window_output_list, &window_output->link);
3737 if (window->output_handler)
3738 window->output_handler(window, output_found, 1,
3743 surface_leave(void *data,
3744 struct wl_surface *wl_surface, struct wl_output *output)
3746 struct window *window = data;
3747 struct window_output *window_output;
3748 struct window_output *window_output_found = NULL;
3750 wl_list_for_each(window_output, &window->window_output_list, link) {
3751 if (window_output->output->output == output) {
3752 window_output_found = window_output;
3757 if (window_output_found) {
3758 wl_list_remove(&window_output_found->link);
3760 if (window->output_handler)
3761 window->output_handler(window, window_output->output,
3762 0, window->user_data);
3764 free(window_output_found);
3768 static const struct wl_surface_listener surface_listener = {
3773 static struct surface *
3774 surface_create(struct window *window)
3776 struct display *display = window->display;
3777 struct surface *surface;
3779 surface = calloc(1, sizeof *surface);
3783 surface->window = window;
3784 surface->surface = wl_compositor_create_surface(display->compositor);
3785 wl_surface_add_listener(surface->surface, &surface_listener, window);
3787 wl_list_insert(&window->subsurface_list, &surface->link);
3792 static struct window *
3793 window_create_internal(struct display *display,
3794 struct window *parent, int type)
3796 struct window *window;
3797 struct surface *surface;
3799 window = malloc(sizeof *window);
3803 memset(window, 0, sizeof *window);
3804 wl_list_init(&window->subsurface_list);
3805 window->display = display;
3806 window->parent = parent;
3808 surface = surface_create(window);
3809 window->main_surface = surface;
3811 if (type != TYPE_CUSTOM && display->shell) {
3812 window->shell_surface =
3813 wl_shell_get_shell_surface(display->shell,
3817 window->type = type;
3818 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
3819 window->configure_requests = 0;
3821 if (display->argb_device)
3822 #ifdef HAVE_CAIRO_EGL
3823 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3825 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3828 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3830 wl_surface_set_user_data(surface->surface, window);
3831 wl_list_insert(display->window_list.prev, &window->link);
3832 wl_list_init(&window->redraw_task.link);
3834 if (window->shell_surface) {
3835 wl_shell_surface_set_user_data(window->shell_surface, window);
3836 wl_shell_surface_add_listener(window->shell_surface,
3837 &shell_surface_listener, window);
3840 wl_list_init (&window->window_output_list);
3846 window_create(struct display *display)
3848 struct window *window;
3850 window = window_create_internal(display, NULL, TYPE_NONE);
3858 window_create_custom(struct display *display)
3860 struct window *window;
3862 window = window_create_internal(display, NULL, TYPE_CUSTOM);
3870 window_create_transient(struct display *display, struct window *parent,
3871 int32_t x, int32_t y, uint32_t flags)
3873 struct window *window;
3875 window = window_create_internal(parent->display,
3876 parent, TYPE_TRANSIENT);
3884 wl_shell_surface_set_transient(
3885 window->shell_surface,
3886 window->parent->main_surface->surface,
3887 window->x, window->y, flags);
3893 menu_set_item(struct menu *menu, int sy)
3897 next = (sy - 8) / 20;
3898 if (menu->current != next) {
3899 menu->current = next;
3900 widget_schedule_redraw(menu->widget);
3905 menu_motion_handler(struct widget *widget,
3906 struct input *input, uint32_t time,
3907 float x, float y, void *data)
3909 struct menu *menu = data;
3911 if (widget == menu->widget)
3912 menu_set_item(data, y);
3914 return CURSOR_LEFT_PTR;
3918 menu_enter_handler(struct widget *widget,
3919 struct input *input, float x, float y, void *data)
3921 struct menu *menu = data;
3923 if (widget == menu->widget)
3924 menu_set_item(data, y);
3926 return CURSOR_LEFT_PTR;
3930 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3932 struct menu *menu = data;
3934 if (widget == menu->widget)
3935 menu_set_item(data, -200);
3939 menu_button_handler(struct widget *widget,
3940 struct input *input, uint32_t time,
3941 uint32_t button, enum wl_pointer_button_state state,
3945 struct menu *menu = data;
3947 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3948 (menu->release_count > 0 || time - menu->time > 500)) {
3949 /* Either relase after press-drag-release or
3950 * click-motion-click. */
3951 menu->func(menu->window->parent,
3952 menu->current, menu->window->parent->user_data);
3953 input_ungrab(input);
3955 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3956 menu->release_count++;
3961 menu_redraw_handler(struct widget *widget, void *data)
3964 const int32_t r = 3, margin = 3;
3965 struct menu *menu = data;
3966 int32_t width, height, i;
3968 cr = widget_cairo_create(widget);
3969 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3970 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3973 width = widget->allocation.width;
3974 height = widget->allocation.height;
3975 rounded_rect(cr, 0, 0, width, height, r);
3977 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3978 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3981 for (i = 0; i < menu->count; i++) {
3982 if (i == menu->current) {
3983 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3984 cairo_rectangle(cr, margin, i * 20 + margin,
3985 width - 2 * margin, 20);
3987 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3988 cairo_move_to(cr, 10, i * 20 + 16);
3989 cairo_show_text(cr, menu->entries[i]);
3991 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3992 cairo_move_to(cr, 10, i * 20 + 16);
3993 cairo_show_text(cr, menu->entries[i]);
4001 window_show_menu(struct display *display,
4002 struct input *input, uint32_t time, struct window *parent,
4003 int32_t x, int32_t y,
4004 menu_func_t func, const char **entries, int count)
4006 struct window *window;
4008 const int32_t margin = 3;
4010 menu = malloc(sizeof *menu);
4014 window = window_create_internal(parent->display, parent, TYPE_MENU);
4020 menu->window = window;
4021 menu->widget = window_add_widget(menu->window, menu);
4022 menu->entries = entries;
4023 menu->count = count;
4024 menu->release_count = 0;
4028 menu->input = input;
4029 window->type = TYPE_MENU;
4033 input_ungrab(input);
4034 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4035 display_get_serial(window->display),
4036 window->parent->main_surface->surface,
4037 window->x, window->y, 0);
4039 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4040 widget_set_enter_handler(menu->widget, menu_enter_handler);
4041 widget_set_leave_handler(menu->widget, menu_leave_handler);
4042 widget_set_motion_handler(menu->widget, menu_motion_handler);
4043 widget_set_button_handler(menu->widget, menu_button_handler);
4045 input_grab(input, menu->widget, 0);
4046 window_schedule_resize(window, 200, count * 20 + margin * 2);
4050 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4052 window->main_surface->buffer_type = type;
4056 window_add_subsurface(struct window *window, void *data,
4057 enum subsurface_mode default_mode)
4059 struct widget *widget;
4060 struct surface *surface;
4061 struct wl_surface *parent;
4062 struct wl_subcompositor *subcompo = window->display->subcompositor;
4067 surface = surface_create(window);
4068 widget = widget_create(window, surface, data);
4069 wl_list_init(&widget->link);
4070 surface->widget = widget;
4072 parent = window->main_surface->surface;
4073 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4076 surface->synchronized = 1;
4078 switch (default_mode) {
4079 case SUBSURFACE_SYNCHRONIZED:
4080 surface->synchronized_default = 1;
4082 case SUBSURFACE_DESYNCHRONIZED:
4083 surface->synchronized_default = 0;
4086 assert(!"bad enum subsurface_mode");
4093 display_handle_geometry(void *data,
4094 struct wl_output *wl_output,
4097 int physical_height,
4103 struct output *output = data;
4105 output->allocation.x = x;
4106 output->allocation.y = y;
4107 output->transform = transform;
4111 display_handle_mode(void *data,
4112 struct wl_output *wl_output,
4118 struct output *output = data;
4119 struct display *display = output->display;
4121 if (flags & WL_OUTPUT_MODE_CURRENT) {
4122 output->allocation.width = width;
4123 output->allocation.height = height;
4124 if (display->output_configure_handler)
4125 (*display->output_configure_handler)(
4126 output, display->user_data);
4130 static const struct wl_output_listener output_listener = {
4131 display_handle_geometry,
4136 display_add_output(struct display *d, uint32_t id)
4138 struct output *output;
4140 output = malloc(sizeof *output);
4144 memset(output, 0, sizeof *output);
4145 output->display = d;
4147 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
4148 wl_list_insert(d->output_list.prev, &output->link);
4150 wl_output_add_listener(output->output, &output_listener, output);
4154 output_destroy(struct output *output)
4156 if (output->destroy_handler)
4157 (*output->destroy_handler)(output, output->user_data);
4159 wl_output_destroy(output->output);
4160 wl_list_remove(&output->link);
4165 display_set_global_handler(struct display *display,
4166 display_global_handler_t handler)
4168 struct global *global;
4170 display->global_handler = handler;
4174 wl_list_for_each(global, &display->global_list, link)
4175 display->global_handler(display,
4176 global->name, global->interface,
4177 global->version, display->user_data);
4181 display_set_output_configure_handler(struct display *display,
4182 display_output_handler_t handler)
4184 struct output *output;
4186 display->output_configure_handler = handler;
4190 wl_list_for_each(output, &display->output_list, link) {
4191 if (output->allocation.width == 0 &&
4192 output->allocation.height == 0)
4195 (*display->output_configure_handler)(output,
4196 display->user_data);
4201 output_set_user_data(struct output *output, void *data)
4203 output->user_data = data;
4207 output_get_user_data(struct output *output)
4209 return output->user_data;
4213 output_set_destroy_handler(struct output *output,
4214 display_output_handler_t handler)
4216 output->destroy_handler = handler;
4217 /* FIXME: implement this, once we have way to remove outputs */
4221 output_get_allocation(struct output *output, struct rectangle *base)
4223 struct rectangle allocation = output->allocation;
4225 switch (output->transform) {
4226 case WL_OUTPUT_TRANSFORM_90:
4227 case WL_OUTPUT_TRANSFORM_270:
4228 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4229 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4230 /* Swap width and height */
4231 allocation.width = output->allocation.height;
4232 allocation.height = output->allocation.width;
4240 output_get_wl_output(struct output *output)
4242 return output->output;
4245 enum wl_output_transform
4246 output_get_transform(struct output *output)
4248 return output->transform;
4252 fini_xkb(struct input *input)
4254 xkb_state_unref(input->xkb.state);
4255 xkb_map_unref(input->xkb.keymap);
4259 display_add_input(struct display *d, uint32_t id)
4261 struct input *input;
4263 input = malloc(sizeof *input);
4267 memset(input, 0, sizeof *input);
4269 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
4270 input->pointer_focus = NULL;
4271 input->keyboard_focus = NULL;
4272 wl_list_insert(d->input_list.prev, &input->link);
4274 wl_seat_add_listener(input->seat, &seat_listener, input);
4275 wl_seat_set_user_data(input->seat, input);
4277 input->data_device =
4278 wl_data_device_manager_get_data_device(d->data_device_manager,
4280 wl_data_device_add_listener(input->data_device, &data_device_listener,
4283 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4285 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4286 TFD_CLOEXEC | TFD_NONBLOCK);
4287 input->repeat_task.run = keyboard_repeat_func;
4288 display_watch_fd(d, input->repeat_timer_fd,
4289 EPOLLIN, &input->repeat_task);
4293 input_destroy(struct input *input)
4295 input_remove_keyboard_focus(input);
4296 input_remove_pointer_focus(input);
4298 if (input->drag_offer)
4299 data_offer_destroy(input->drag_offer);
4301 if (input->selection_offer)
4302 data_offer_destroy(input->selection_offer);
4304 wl_data_device_destroy(input->data_device);
4307 wl_surface_destroy(input->pointer_surface);
4309 wl_list_remove(&input->link);
4310 wl_seat_destroy(input->seat);
4311 close(input->repeat_timer_fd);
4316 init_workspace_manager(struct display *d, uint32_t id)
4318 d->workspace_manager =
4319 wl_registry_bind(d->registry, id,
4320 &workspace_manager_interface, 1);
4321 if (d->workspace_manager != NULL)
4322 workspace_manager_add_listener(d->workspace_manager,
4323 &workspace_manager_listener,
4328 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4329 const char *interface, uint32_t version)
4331 struct display *d = data;
4332 struct global *global;
4334 global = malloc(sizeof *global);
4336 global->interface = strdup(interface);
4337 global->version = version;
4338 wl_list_insert(d->global_list.prev, &global->link);
4340 if (strcmp(interface, "wl_compositor") == 0) {
4341 d->compositor = wl_registry_bind(registry, id,
4342 &wl_compositor_interface, 1);
4343 } else if (strcmp(interface, "wl_output") == 0) {
4344 display_add_output(d, id);
4345 } else if (strcmp(interface, "wl_seat") == 0) {
4346 display_add_input(d, id);
4347 } else if (strcmp(interface, "wl_shell") == 0) {
4348 d->shell = wl_registry_bind(registry,
4349 id, &wl_shell_interface, 1);
4350 } else if (strcmp(interface, "wl_shm") == 0) {
4351 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4352 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4353 d->data_device_manager =
4354 wl_registry_bind(registry, id,
4355 &wl_data_device_manager_interface, 1);
4356 } else if (strcmp(interface, "text_cursor_position") == 0) {
4357 d->text_cursor_position =
4358 wl_registry_bind(registry, id,
4359 &text_cursor_position_interface, 1);
4360 } else if (strcmp(interface, "workspace_manager") == 0) {
4361 init_workspace_manager(d, id);
4362 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4364 wl_registry_bind(registry, id,
4365 &wl_subcompositor_interface, 1);
4368 if (d->global_handler)
4369 d->global_handler(d, id, interface, version, d->user_data);
4373 registry_handle_global_remove(void *data, struct wl_registry *registry,
4376 struct display *d = data;
4377 struct global *global;
4380 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4381 if (global->name != name)
4384 /* XXX: Should destroy bound globals, and call
4385 * the counterpart of display::global_handler
4387 wl_list_remove(&global->link);
4388 free(global->interface);
4394 display_bind(struct display *display, uint32_t name,
4395 const struct wl_interface *interface, uint32_t version)
4397 return wl_registry_bind(display->registry, name, interface, version);
4400 static const struct wl_registry_listener registry_listener = {
4401 registry_handle_global,
4402 registry_handle_global_remove
4405 #ifdef HAVE_CAIRO_EGL
4407 init_egl(struct display *d)
4409 EGLint major, minor;
4412 #ifdef USE_CAIRO_GLESV2
4413 # define GL_BIT EGL_OPENGL_ES2_BIT
4415 # define GL_BIT EGL_OPENGL_BIT
4418 static const EGLint argb_cfg_attribs[] = {
4419 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4425 EGL_RENDERABLE_TYPE, GL_BIT,
4429 #ifdef USE_CAIRO_GLESV2
4430 static const EGLint context_attribs[] = {
4431 EGL_CONTEXT_CLIENT_VERSION, 2,
4434 EGLint api = EGL_OPENGL_ES_API;
4436 EGLint *context_attribs = NULL;
4437 EGLint api = EGL_OPENGL_API;
4440 d->dpy = eglGetDisplay(d->display);
4441 if (!eglInitialize(d->dpy, &major, &minor)) {
4442 fprintf(stderr, "failed to initialize EGL\n");
4446 if (!eglBindAPI(api)) {
4447 fprintf(stderr, "failed to bind EGL client API\n");
4451 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4452 &d->argb_config, 1, &n) || n != 1) {
4453 fprintf(stderr, "failed to choose argb EGL config\n");
4457 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
4458 EGL_NO_CONTEXT, context_attribs);
4459 if (d->argb_ctx == NULL) {
4460 fprintf(stderr, "failed to create EGL context\n");
4464 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
4465 fprintf(stderr, "failed to make EGL context current\n");
4469 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4470 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4471 fprintf(stderr, "failed to get cairo EGL argb device\n");
4479 fini_egl(struct display *display)
4481 cairo_device_destroy(display->argb_device);
4483 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4486 eglTerminate(display->dpy);
4492 init_dummy_surface(struct display *display)
4497 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4499 display->dummy_surface =
4500 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4502 display->dummy_surface_data = data;
4506 handle_display_data(struct task *task, uint32_t events)
4508 struct display *display =
4509 container_of(task, struct display, display_task);
4510 struct epoll_event ep;
4513 display->display_fd_events = events;
4515 if (events & EPOLLERR || events & EPOLLHUP) {
4516 display_exit(display);
4520 if (events & EPOLLIN) {
4521 ret = wl_display_dispatch(display->display);
4523 display_exit(display);
4528 if (events & EPOLLOUT) {
4529 ret = wl_display_flush(display->display);
4531 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4532 ep.data.ptr = &display->display_task;
4533 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4534 display->display_fd, &ep);
4535 } else if (ret == -1 && errno != EAGAIN) {
4536 display_exit(display);
4543 log_handler(const char *format, va_list args)
4545 vfprintf(stderr, format, args);
4549 display_create(int *argc, char *argv[])
4553 wl_log_set_handler_client(log_handler);
4555 d = malloc(sizeof *d);
4559 memset(d, 0, sizeof *d);
4561 d->display = wl_display_connect(NULL);
4562 if (d->display == NULL) {
4563 fprintf(stderr, "failed to connect to Wayland display: %m\n");
4568 d->epoll_fd = os_epoll_create_cloexec();
4569 d->display_fd = wl_display_get_fd(d->display);
4570 d->display_task.run = handle_display_data;
4571 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4574 wl_list_init(&d->deferred_list);
4575 wl_list_init(&d->input_list);
4576 wl_list_init(&d->output_list);
4577 wl_list_init(&d->global_list);
4579 d->xkb_context = xkb_context_new(0);
4580 if (d->xkb_context == NULL) {
4581 fprintf(stderr, "Failed to create XKB context\n");
4586 d->workspace_count = 1;
4588 d->registry = wl_display_get_registry(d->display);
4589 wl_registry_add_listener(d->registry, ®istry_listener, d);
4591 if (wl_display_dispatch(d->display) < 0) {
4592 fprintf(stderr, "Failed to process Wayland connection: %m\n");
4596 #ifdef HAVE_CAIRO_EGL
4597 if (init_egl(d) < 0)
4598 fprintf(stderr, "EGL does not seem to work, "
4599 "falling back to software rendering and wl_shm.\n");
4604 d->theme = theme_create();
4606 wl_list_init(&d->window_list);
4608 init_dummy_surface(d);
4614 display_destroy_outputs(struct display *display)
4617 struct output *output;
4619 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4620 output_destroy(output);
4624 display_destroy_inputs(struct display *display)
4627 struct input *input;
4629 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4630 input_destroy(input);
4634 display_destroy(struct display *display)
4636 if (!wl_list_empty(&display->window_list))
4637 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4638 wl_list_length(&display->window_list));
4640 if (!wl_list_empty(&display->deferred_list))
4641 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4643 cairo_surface_destroy(display->dummy_surface);
4644 free(display->dummy_surface_data);
4646 display_destroy_outputs(display);
4647 display_destroy_inputs(display);
4649 xkb_context_unref(display->xkb_context);
4651 theme_destroy(display->theme);
4652 destroy_cursors(display);
4654 #ifdef HAVE_CAIRO_EGL
4655 if (display->argb_device)
4659 if (display->subcompositor)
4660 wl_subcompositor_destroy(display->subcompositor);
4663 wl_shell_destroy(display->shell);
4666 wl_shm_destroy(display->shm);
4668 if (display->data_device_manager)
4669 wl_data_device_manager_destroy(display->data_device_manager);
4671 wl_compositor_destroy(display->compositor);
4672 wl_registry_destroy(display->registry);
4674 close(display->epoll_fd);
4676 if (!(display->display_fd_events & EPOLLERR) &&
4677 !(display->display_fd_events & EPOLLHUP))
4678 wl_display_flush(display->display);
4680 wl_display_disconnect(display->display);
4685 display_set_user_data(struct display *display, void *data)
4687 display->user_data = data;
4691 display_get_user_data(struct display *display)
4693 return display->user_data;
4697 display_get_display(struct display *display)
4699 return display->display;
4703 display_get_output(struct display *display)
4705 return container_of(display->output_list.next, struct output, link);
4708 struct wl_compositor *
4709 display_get_compositor(struct display *display)
4711 return display->compositor;
4715 display_get_serial(struct display *display)
4717 return display->serial;
4721 display_get_egl_display(struct display *d)
4726 struct wl_data_source *
4727 display_create_data_source(struct display *display)
4729 return wl_data_device_manager_create_data_source(display->data_device_manager);
4733 display_get_argb_egl_config(struct display *d)
4735 return d->argb_config;
4739 display_get_shell(struct display *display)
4741 return display->shell;
4745 display_acquire_window_surface(struct display *display,
4746 struct window *window,
4749 struct surface *surface = window->main_surface;
4751 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
4754 widget_get_cairo_surface(window->main_surface->widget);
4755 return surface->toysurface->acquire(surface->toysurface, ctx);
4759 display_release_window_surface(struct display *display,
4760 struct window *window)
4762 struct surface *surface = window->main_surface;
4764 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
4767 surface->toysurface->release(surface->toysurface);
4771 display_defer(struct display *display, struct task *task)
4773 wl_list_insert(&display->deferred_list, &task->link);
4777 display_watch_fd(struct display *display,
4778 int fd, uint32_t events, struct task *task)
4780 struct epoll_event ep;
4784 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4788 display_unwatch_fd(struct display *display, int fd)
4790 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4794 display_run(struct display *display)
4797 struct epoll_event ep[16];
4800 display->running = 1;
4802 while (!wl_list_empty(&display->deferred_list)) {
4803 task = container_of(display->deferred_list.prev,
4805 wl_list_remove(&task->link);
4809 wl_display_dispatch_pending(display->display);
4811 if (!display->running)
4814 ret = wl_display_flush(display->display);
4815 if (ret < 0 && errno == EAGAIN) {
4817 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4818 ep[0].data.ptr = &display->display_task;
4820 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4821 display->display_fd, &ep[0]);
4822 } else if (ret < 0) {
4826 count = epoll_wait(display->epoll_fd,
4827 ep, ARRAY_LENGTH(ep), -1);
4828 for (i = 0; i < count; i++) {
4829 task = ep[i].data.ptr;
4830 task->run(task, ep[i].events);
4836 display_exit(struct display *display)
4838 display->running = 0;
4842 keysym_modifiers_add(struct wl_array *modifiers_map,
4845 size_t len = strlen(name) + 1;
4848 p = wl_array_add(modifiers_map, len);
4853 strncpy(p, name, len);
4856 static xkb_mod_index_t
4857 keysym_modifiers_get_index(struct wl_array *modifiers_map,
4860 xkb_mod_index_t index = 0;
4861 char *p = modifiers_map->data;
4863 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4864 if (strcmp(p, name) == 0)
4871 return XKB_MOD_INVALID;
4875 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4878 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4880 if (index == XKB_MOD_INVALID)
4881 return XKB_MOD_INVALID;