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
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_subcompositor *subcompositor;
88 struct wl_shell *shell;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
94 EGLConfig argb_config;
96 cairo_device_t *argb_device;
100 uint32_t display_fd_events;
101 struct task display_task;
104 struct wl_list deferred_list;
108 struct wl_list global_list;
109 struct wl_list window_list;
110 struct wl_list input_list;
111 struct wl_list output_list;
115 struct wl_cursor_theme *cursor_theme;
116 struct wl_cursor **cursors;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
123 struct xkb_context *xkb_context;
126 uint32_t workspace_count;
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
146 struct window_output {
147 struct output *output;
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
156 * width,height are the new buffer size.
157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
159 * Returns the Cairo surface to draw to.
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
162 int32_t width, int32_t height, uint32_t flags,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
170 void (*swap)(struct toysurface *base,
171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
172 struct rectangle *server_allocation);
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
181 * Release the toysurface from the EGL context, returning control
184 void (*release)(struct toysurface *base);
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
190 void (*destroy)(struct toysurface *base);
194 struct window *window;
196 struct wl_surface *surface;
197 struct wl_subsurface *subsurface;
199 int synchronized_default;
200 struct toysurface *toysurface;
201 struct widget *widget;
203 struct wl_callback *frame_cb;
206 struct rectangle allocation;
207 struct rectangle server_allocation;
209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
214 int32_t buffer_scale;
216 cairo_surface_t *cairo_surface;
222 struct display *display;
223 struct window *parent;
224 struct wl_list window_output_list;
226 struct rectangle saved_allocation;
227 struct rectangle min_allocation;
228 struct rectangle pending_allocation;
232 int redraw_task_scheduled;
233 struct task redraw_task;
240 int fullscreen_method;
241 int configure_requests;
243 enum preferred_format preferred_format;
245 window_key_handler_t key_handler;
246 window_keyboard_focus_handler_t keyboard_focus_handler;
247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
249 window_close_handler_t close_handler;
250 window_fullscreen_handler_t fullscreen_handler;
251 window_output_handler_t output_handler;
253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
266 struct window *window;
267 struct surface *surface;
268 struct tooltip *tooltip;
269 struct wl_list child_list;
271 struct rectangle allocation;
272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
276 widget_motion_handler_t motion_handler;
277 widget_button_handler_t button_handler;
278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
283 widget_axis_handler_t axis_handler;
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
332 struct task repeat_task;
336 uint32_t repeat_time;
340 struct display *display;
341 struct wl_output *output;
342 struct rectangle allocation;
347 display_output_handler_t destroy_handler;
351 enum frame_button_action {
352 FRAME_BUTTON_NULL = 0,
353 FRAME_BUTTON_ICON = 1,
354 FRAME_BUTTON_CLOSE = 2,
355 FRAME_BUTTON_MINIMIZE = 3,
356 FRAME_BUTTON_MAXIMIZE = 4,
359 enum frame_button_pointer {
360 FRAME_BUTTON_DEFAULT = 0,
361 FRAME_BUTTON_OVER = 1,
362 FRAME_BUTTON_ACTIVE = 2,
365 enum frame_button_align {
366 FRAME_BUTTON_RIGHT = 0,
367 FRAME_BUTTON_LEFT = 1,
370 enum frame_button_decoration {
371 FRAME_BUTTON_NONE = 0,
372 FRAME_BUTTON_FANCY = 1,
375 struct frame_button {
376 struct widget *widget;
378 cairo_surface_t *icon;
379 enum frame_button_action type;
380 enum frame_button_pointer state;
381 struct wl_list link; /* buttons_list */
382 enum frame_button_align align;
383 enum frame_button_decoration decoration;
387 struct widget *widget;
388 struct widget *child;
389 struct wl_list buttons_list;
393 struct window *window;
394 struct widget *widget;
396 const char **entries;
405 struct widget *parent;
406 struct window *window;
407 struct widget *widget;
409 struct task tooltip_task;
415 struct wl_shm_pool *pool;
422 CURSOR_DEFAULT = 100,
426 enum window_location {
428 WINDOW_RESIZING_TOP = 1,
429 WINDOW_RESIZING_BOTTOM = 2,
430 WINDOW_RESIZING_LEFT = 4,
431 WINDOW_RESIZING_TOP_LEFT = 5,
432 WINDOW_RESIZING_BOTTOM_LEFT = 6,
433 WINDOW_RESIZING_RIGHT = 8,
434 WINDOW_RESIZING_TOP_RIGHT = 9,
435 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
436 WINDOW_RESIZING_MASK = 15,
437 WINDOW_EXTERIOR = 16,
438 WINDOW_TITLEBAR = 17,
439 WINDOW_CLIENT_AREA = 18,
442 static const cairo_user_data_key_t shm_surface_data_key;
449 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
450 __attribute__ ((format (printf, 4, 5)));
453 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
458 gettimeofday(&tv, NULL);
459 fprintf(stderr, "%8ld.%03ld ",
460 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
463 fprintf(stderr, "%s@%d ",
464 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
466 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
467 fprintf(stderr, "%s ", func);
470 vfprintf(stderr, fmt, ap);
474 #define DBG(fmt, ...) \
475 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
477 #define DBG_OBJ(obj, fmt, ...) \
478 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
482 #define DBG(...) do {} while (0)
483 #define DBG_OBJ(...) do {} while (0)
488 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
492 switch (buffer_transform) {
493 case WL_OUTPUT_TRANSFORM_90:
494 case WL_OUTPUT_TRANSFORM_270:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
505 *width *= buffer_scale;
506 *height *= buffer_scale;
510 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
514 switch (buffer_transform) {
515 case WL_OUTPUT_TRANSFORM_90:
516 case WL_OUTPUT_TRANSFORM_270:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
518 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
527 *width /= buffer_scale;
528 *height /= buffer_scale;
531 #ifdef HAVE_CAIRO_EGL
533 struct egl_window_surface {
534 struct toysurface base;
535 cairo_surface_t *cairo_surface;
536 struct display *display;
537 struct wl_surface *surface;
538 struct wl_egl_window *egl_window;
539 EGLSurface egl_surface;
542 static struct egl_window_surface *
543 to_egl_window_surface(struct toysurface *base)
545 return container_of(base, struct egl_window_surface, base);
548 static cairo_surface_t *
549 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
550 int32_t width, int32_t height, uint32_t flags,
551 enum wl_output_transform buffer_transform, int32_t buffer_scale)
553 struct egl_window_surface *surface = to_egl_window_surface(base);
555 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
557 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
558 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
560 return cairo_surface_reference(surface->cairo_surface);
564 egl_window_surface_swap(struct toysurface *base,
565 enum wl_output_transform buffer_transform, int32_t buffer_scale,
566 struct rectangle *server_allocation)
568 struct egl_window_surface *surface = to_egl_window_surface(base);
570 cairo_gl_surface_swapbuffers(surface->cairo_surface);
571 wl_egl_window_get_attached_size(surface->egl_window,
572 &server_allocation->width,
573 &server_allocation->height);
575 buffer_to_surface_size (buffer_transform, buffer_scale,
576 &server_allocation->width,
577 &server_allocation->height);
581 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 cairo_device_t *device;
586 device = cairo_surface_get_device(surface->cairo_surface);
591 if (device == surface->display->argb_device)
592 ctx = surface->display->argb_ctx;
597 cairo_device_flush(device);
598 cairo_device_acquire(device);
599 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
600 surface->egl_surface, ctx))
601 fprintf(stderr, "failed to make surface current\n");
607 egl_window_surface_release(struct toysurface *base)
609 struct egl_window_surface *surface = to_egl_window_surface(base);
610 cairo_device_t *device;
612 device = cairo_surface_get_device(surface->cairo_surface);
616 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
617 surface->display->argb_ctx))
618 fprintf(stderr, "failed to make context current\n");
620 cairo_device_release(device);
624 egl_window_surface_destroy(struct toysurface *base)
626 struct egl_window_surface *surface = to_egl_window_surface(base);
627 struct display *d = surface->display;
629 cairo_surface_destroy(surface->cairo_surface);
630 eglDestroySurface(d->dpy, surface->egl_surface);
631 wl_egl_window_destroy(surface->egl_window);
632 surface->surface = NULL;
637 static struct toysurface *
638 egl_window_surface_create(struct display *display,
639 struct wl_surface *wl_surface,
641 struct rectangle *rectangle)
643 struct egl_window_surface *surface;
645 if (display->dpy == EGL_NO_DISPLAY)
648 surface = calloc(1, sizeof *surface);
652 surface->base.prepare = egl_window_surface_prepare;
653 surface->base.swap = egl_window_surface_swap;
654 surface->base.acquire = egl_window_surface_acquire;
655 surface->base.release = egl_window_surface_release;
656 surface->base.destroy = egl_window_surface_destroy;
658 surface->display = display;
659 surface->surface = wl_surface;
661 surface->egl_window = wl_egl_window_create(surface->surface,
665 surface->egl_surface = eglCreateWindowSurface(display->dpy,
666 display->argb_config,
670 surface->cairo_surface =
671 cairo_gl_surface_create_for_egl(display->argb_device,
672 surface->egl_surface,
676 return &surface->base;
681 static struct toysurface *
682 egl_window_surface_create(struct display *display,
683 struct wl_surface *wl_surface,
685 struct rectangle *rectangle)
692 struct shm_surface_data {
693 struct wl_buffer *buffer;
694 struct shm_pool *pool;
698 display_get_buffer_for_surface(struct display *display,
699 cairo_surface_t *surface)
701 struct shm_surface_data *data;
703 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
709 shm_pool_destroy(struct shm_pool *pool);
712 shm_surface_data_destroy(void *p)
714 struct shm_surface_data *data = p;
716 wl_buffer_destroy(data->buffer);
718 shm_pool_destroy(data->pool);
723 static struct wl_shm_pool *
724 make_shm_pool(struct display *display, int size, void **data)
726 struct wl_shm_pool *pool;
729 fd = os_create_anonymous_file(size);
731 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
736 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
737 if (*data == MAP_FAILED) {
738 fprintf(stderr, "mmap failed: %m\n");
743 pool = wl_shm_create_pool(display->shm, fd, size);
750 static struct shm_pool *
751 shm_pool_create(struct display *display, size_t size)
753 struct shm_pool *pool = malloc(sizeof *pool);
758 pool->pool = make_shm_pool(display, size, &pool->data);
771 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
773 if (pool->used + size > pool->size)
776 *offset = pool->used;
779 return (char *) pool->data + *offset;
782 /* destroy the pool. this does not unmap the memory though */
784 shm_pool_destroy(struct shm_pool *pool)
786 munmap(pool->data, pool->size);
787 wl_shm_pool_destroy(pool->pool);
791 /* Start allocating from the beginning of the pool again */
793 shm_pool_reset(struct shm_pool *pool)
799 data_length_for_shm_surface(struct rectangle *rect)
803 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
805 return stride * rect->height;
808 static cairo_surface_t *
809 display_create_shm_surface_from_pool(struct display *display,
810 struct rectangle *rectangle,
811 uint32_t flags, struct shm_pool *pool)
813 struct shm_surface_data *data;
815 cairo_surface_t *surface;
816 cairo_format_t cairo_format;
817 int stride, length, offset;
820 data = malloc(sizeof *data);
824 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
825 cairo_format = CAIRO_FORMAT_RGB16_565;
827 cairo_format = CAIRO_FORMAT_ARGB32;
829 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
830 length = stride * rectangle->height;
832 map = shm_pool_allocate(pool, length, &offset);
839 surface = cairo_image_surface_create_for_data (map,
845 cairo_surface_set_user_data(surface, &shm_surface_data_key,
846 data, shm_surface_data_destroy);
848 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
849 format = WL_SHM_FORMAT_RGB565;
851 if (flags & SURFACE_OPAQUE)
852 format = WL_SHM_FORMAT_XRGB8888;
854 format = WL_SHM_FORMAT_ARGB8888;
857 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
865 static cairo_surface_t *
866 display_create_shm_surface(struct display *display,
867 struct rectangle *rectangle, uint32_t flags,
868 struct shm_pool *alternate_pool,
869 struct shm_surface_data **data_ret)
871 struct shm_surface_data *data;
872 struct shm_pool *pool;
873 cairo_surface_t *surface;
875 if (alternate_pool) {
876 shm_pool_reset(alternate_pool);
877 surface = display_create_shm_surface_from_pool(display,
882 data = cairo_surface_get_user_data(surface,
883 &shm_surface_data_key);
888 pool = shm_pool_create(display,
889 data_length_for_shm_surface(rectangle));
894 display_create_shm_surface_from_pool(display, rectangle,
898 shm_pool_destroy(pool);
902 /* make sure we destroy the pool when the surface is destroyed */
903 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
914 check_size(struct rectangle *rect)
916 if (rect->width && rect->height)
919 fprintf(stderr, "tried to create surface of "
920 "width: %d, height: %d\n", rect->width, rect->height);
925 display_create_surface(struct display *display,
926 struct wl_surface *surface,
927 struct rectangle *rectangle,
930 if (check_size(rectangle) < 0)
933 assert(flags & SURFACE_SHM);
934 return display_create_shm_surface(display, rectangle, flags,
938 struct shm_surface_leaf {
939 cairo_surface_t *cairo_surface;
940 /* 'data' is automatically destroyed, when 'cairo_surface' is */
941 struct shm_surface_data *data;
943 struct shm_pool *resize_pool;
948 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
950 if (leaf->cairo_surface)
951 cairo_surface_destroy(leaf->cairo_surface);
952 /* leaf->data already destroyed via cairo private */
954 if (leaf->resize_pool)
955 shm_pool_destroy(leaf->resize_pool);
957 memset(leaf, 0, sizeof *leaf);
963 struct toysurface base;
964 struct display *display;
965 struct wl_surface *surface;
969 struct shm_surface_leaf leaf[MAX_LEAVES];
970 struct shm_surface_leaf *current;
973 static struct shm_surface *
974 to_shm_surface(struct toysurface *base)
976 return container_of(base, struct shm_surface, base);
980 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
983 struct shm_surface_leaf *leaf;
984 char bufs[MAX_LEAVES + 1];
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
992 else if (leaf->cairo_surface)
998 bufs[MAX_LEAVES] = '\0';
999 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1004 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1006 struct shm_surface *surface = data;
1007 struct shm_surface_leaf *leaf;
1011 shm_surface_buffer_state_debug(surface, "buffer_release before");
1013 for (i = 0; i < MAX_LEAVES; i++) {
1014 leaf = &surface->leaf[i];
1015 if (leaf->data && leaf->data->buffer == buffer) {
1020 assert(i < MAX_LEAVES && "unknown buffer released");
1022 /* Leave one free leaf with storage, release others */
1024 for (i = 0; i < MAX_LEAVES; i++) {
1025 leaf = &surface->leaf[i];
1027 if (!leaf->cairo_surface || leaf->busy)
1033 shm_surface_leaf_release(leaf);
1036 shm_surface_buffer_state_debug(surface, "buffer_release after");
1039 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1040 shm_surface_buffer_release
1043 static cairo_surface_t *
1044 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1045 int32_t width, int32_t height, uint32_t flags,
1046 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1048 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1049 struct shm_surface *surface = to_shm_surface(base);
1050 struct rectangle rect = { 0};
1051 struct shm_surface_leaf *leaf = NULL;
1057 /* pick a free buffer, preferrably one that already has storage */
1058 for (i = 0; i < MAX_LEAVES; i++) {
1059 if (surface->leaf[i].busy)
1062 if (!leaf || surface->leaf[i].cairo_surface)
1063 leaf = &surface->leaf[i];
1065 DBG_OBJ(surface->surface, "pick leaf %d\n",
1066 (int)(leaf - &surface->leaf[0]));
1069 fprintf(stderr, "%s: all buffers are held by the server.\n",
1075 if (!resize_hint && leaf->resize_pool) {
1076 cairo_surface_destroy(leaf->cairo_surface);
1077 leaf->cairo_surface = NULL;
1078 shm_pool_destroy(leaf->resize_pool);
1079 leaf->resize_pool = NULL;
1082 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1084 if (leaf->cairo_surface &&
1085 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1086 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1089 if (leaf->cairo_surface)
1090 cairo_surface_destroy(leaf->cairo_surface);
1092 #ifdef USE_RESIZE_POOL
1093 if (resize_hint && !leaf->resize_pool) {
1094 /* Create a big pool to allocate from, while continuously
1095 * resizing. Mmapping a new pool in the server
1096 * is relatively expensive, so reusing a pool performs
1097 * better, but may temporarily reserve unneeded memory.
1099 /* We should probably base this number on the output size. */
1100 leaf->resize_pool = shm_pool_create(surface->display,
1106 rect.height = height;
1108 leaf->cairo_surface =
1109 display_create_shm_surface(surface->display, &rect,
1113 wl_buffer_add_listener(leaf->data->buffer,
1114 &shm_surface_buffer_listener, surface);
1117 surface->current = leaf;
1119 return cairo_surface_reference(leaf->cairo_surface);
1123 shm_surface_swap(struct toysurface *base,
1124 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1125 struct rectangle *server_allocation)
1127 struct shm_surface *surface = to_shm_surface(base);
1128 struct shm_surface_leaf *leaf = surface->current;
1130 server_allocation->width =
1131 cairo_image_surface_get_width(leaf->cairo_surface);
1132 server_allocation->height =
1133 cairo_image_surface_get_height(leaf->cairo_surface);
1135 buffer_to_surface_size (buffer_transform, buffer_scale,
1136 &server_allocation->width,
1137 &server_allocation->height);
1139 wl_surface_attach(surface->surface, leaf->data->buffer,
1140 surface->dx, surface->dy);
1141 wl_surface_damage(surface->surface, 0, 0,
1142 server_allocation->width, server_allocation->height);
1143 wl_surface_commit(surface->surface);
1145 DBG_OBJ(surface->surface, "leaf %d busy\n",
1146 (int)(leaf - &surface->leaf[0]));
1149 surface->current = NULL;
1153 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1159 shm_surface_release(struct toysurface *base)
1164 shm_surface_destroy(struct toysurface *base)
1166 struct shm_surface *surface = to_shm_surface(base);
1169 for (i = 0; i < MAX_LEAVES; i++)
1170 shm_surface_leaf_release(&surface->leaf[i]);
1175 static struct toysurface *
1176 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1177 uint32_t flags, struct rectangle *rectangle)
1179 struct shm_surface *surface;
1180 DBG_OBJ(wl_surface, "\n");
1182 surface = xmalloc(sizeof *surface);
1183 memset(surface, 0, sizeof *surface);
1188 surface->base.prepare = shm_surface_prepare;
1189 surface->base.swap = shm_surface_swap;
1190 surface->base.acquire = shm_surface_acquire;
1191 surface->base.release = shm_surface_release;
1192 surface->base.destroy = shm_surface_destroy;
1194 surface->display = display;
1195 surface->surface = wl_surface;
1196 surface->flags = flags;
1198 return &surface->base;
1202 * The following correspondences between file names and cursors was copied
1203 * from: https://bugs.kde.org/attachment.cgi?id=67313
1206 static const char *bottom_left_corners[] = {
1207 "bottom_left_corner",
1212 static const char *bottom_right_corners[] = {
1213 "bottom_right_corner",
1218 static const char *bottom_sides[] = {
1224 static const char *grabbings[] = {
1227 "208530c400c041818281048008011002"
1230 static const char *left_ptrs[] = {
1237 static const char *left_sides[] = {
1243 static const char *right_sides[] = {
1249 static const char *top_left_corners[] = {
1255 static const char *top_right_corners[] = {
1261 static const char *top_sides[] = {
1267 static const char *xterms[] = {
1273 static const char *hand1s[] = {
1277 "e29285e634086352946a0e7090d73106"
1280 static const char *watches[] = {
1283 "0426c94ea35c87780ff01dc239897213"
1286 struct cursor_alternatives {
1291 static const struct cursor_alternatives cursors[] = {
1292 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1293 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1294 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1295 {grabbings, ARRAY_LENGTH(grabbings)},
1296 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1297 {left_sides, ARRAY_LENGTH(left_sides)},
1298 {right_sides, ARRAY_LENGTH(right_sides)},
1299 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1300 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1301 {top_sides, ARRAY_LENGTH(top_sides)},
1302 {xterms, ARRAY_LENGTH(xterms)},
1303 {hand1s, ARRAY_LENGTH(hand1s)},
1304 {watches, ARRAY_LENGTH(watches)},
1308 create_cursors(struct display *display)
1312 unsigned int size = 32;
1314 struct wl_cursor *cursor;
1315 struct config_key shell_keys[] = {
1316 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1317 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
1319 struct config_section cs[] = {
1320 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1323 config_fd = open_config_file("weston.ini");
1324 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1327 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1329 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1331 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1333 for (j = 0; !cursor && j < cursors[i].count; ++j)
1334 cursor = wl_cursor_theme_get_cursor(
1335 display->cursor_theme, cursors[i].names[j]);
1338 fprintf(stderr, "could not load cursor '%s'\n",
1339 cursors[i].names[0]);
1341 display->cursors[i] = cursor;
1346 destroy_cursors(struct display *display)
1348 wl_cursor_theme_destroy(display->cursor_theme);
1349 free(display->cursors);
1352 struct wl_cursor_image *
1353 display_get_pointer_image(struct display *display, int pointer)
1355 struct wl_cursor *cursor = display->cursors[pointer];
1357 return cursor ? cursor->images[0] : NULL;
1361 surface_flush(struct surface *surface)
1363 if (!surface->cairo_surface)
1366 if (surface->opaque_region) {
1367 wl_surface_set_opaque_region(surface->surface,
1368 surface->opaque_region);
1369 wl_region_destroy(surface->opaque_region);
1370 surface->opaque_region = NULL;
1373 if (surface->input_region) {
1374 wl_surface_set_input_region(surface->surface,
1375 surface->input_region);
1376 wl_region_destroy(surface->input_region);
1377 surface->input_region = NULL;
1380 surface->toysurface->swap(surface->toysurface,
1381 surface->buffer_transform, surface->buffer_scale,
1382 &surface->server_allocation);
1384 cairo_surface_destroy(surface->cairo_surface);
1385 surface->cairo_surface = NULL;
1389 window_has_focus(struct window *window)
1391 return window->focus_count > 0;
1395 window_flush(struct window *window)
1397 struct surface *surface;
1399 if (window->type == TYPE_NONE) {
1400 window->type = TYPE_TOPLEVEL;
1401 if (window->shell_surface)
1402 wl_shell_surface_set_toplevel(window->shell_surface);
1405 wl_list_for_each(surface, &window->subsurface_list, link) {
1406 if (surface == window->main_surface)
1409 surface_flush(surface);
1412 surface_flush(window->main_surface);
1416 window_get_display(struct window *window)
1418 return window->display;
1422 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1424 struct display *display = surface->window->display;
1425 struct rectangle allocation = surface->allocation;
1427 if (!surface->toysurface && display->dpy &&
1428 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1429 surface->toysurface =
1430 egl_window_surface_create(display,
1436 if (!surface->toysurface)
1437 surface->toysurface = shm_surface_create(display,
1439 flags, &allocation);
1441 surface->cairo_surface = surface->toysurface->prepare(
1442 surface->toysurface, dx, dy,
1443 allocation.width, allocation.height, flags,
1444 surface->buffer_transform, surface->buffer_scale);
1448 window_create_main_surface(struct window *window)
1450 struct surface *surface = window->main_surface;
1455 if (window->resizing)
1456 flags |= SURFACE_HINT_RESIZE;
1458 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1459 flags |= SURFACE_HINT_RGB565;
1461 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1462 dx = surface->server_allocation.width -
1463 surface->allocation.width;
1465 if (window->resize_edges & WINDOW_RESIZING_TOP)
1466 dy = surface->server_allocation.height -
1467 surface->allocation.height;
1469 window->resize_edges = 0;
1471 surface_create_surface(surface, dx, dy, flags);
1475 window_get_buffer_transform(struct window *window)
1477 return window->main_surface->buffer_transform;
1481 window_set_buffer_transform(struct window *window,
1482 enum wl_output_transform transform)
1484 window->main_surface->buffer_transform = transform;
1485 wl_surface_set_buffer_transform(window->main_surface->surface,
1490 window_set_buffer_scale(struct window *window,
1493 window->main_surface->buffer_scale = scale;
1494 wl_surface_set_buffer_scale(window->main_surface->surface,
1499 window_get_buffer_scale(struct window *window)
1501 return window->main_surface->buffer_scale;
1505 window_get_output_scale(struct window *window)
1507 struct window_output *window_output;
1508 struct window_output *window_output_tmp;
1511 wl_list_for_each_safe(window_output, window_output_tmp,
1512 &window->window_output_list, link) {
1513 if (window_output->output->scale > scale)
1514 scale = window_output->output->scale;
1520 static void frame_destroy(struct frame *frame);
1523 surface_destroy(struct surface *surface)
1525 if (surface->frame_cb)
1526 wl_callback_destroy(surface->frame_cb);
1528 if (surface->input_region)
1529 wl_region_destroy(surface->input_region);
1531 if (surface->opaque_region)
1532 wl_region_destroy(surface->opaque_region);
1534 if (surface->subsurface)
1535 wl_subsurface_destroy(surface->subsurface);
1537 wl_surface_destroy(surface->surface);
1539 if (surface->toysurface)
1540 surface->toysurface->destroy(surface->toysurface);
1542 wl_list_remove(&surface->link);
1547 window_destroy(struct window *window)
1549 struct display *display = window->display;
1550 struct input *input;
1551 struct window_output *window_output;
1552 struct window_output *window_output_tmp;
1554 wl_list_remove(&window->redraw_task.link);
1556 wl_list_for_each(input, &display->input_list, link) {
1557 if (input->touch_focus == window)
1558 input->touch_focus = NULL;
1559 if (input->pointer_focus == window)
1560 input->pointer_focus = NULL;
1561 if (input->keyboard_focus == window)
1562 input->keyboard_focus = NULL;
1563 if (input->focus_widget &&
1564 input->focus_widget->window == window)
1565 input->focus_widget = NULL;
1568 wl_list_for_each_safe(window_output, window_output_tmp,
1569 &window->window_output_list, link) {
1570 free (window_output);
1574 frame_destroy(window->frame);
1576 if (window->shell_surface)
1577 wl_shell_surface_destroy(window->shell_surface);
1579 surface_destroy(window->main_surface);
1581 wl_list_remove(&window->link);
1583 free(window->title);
1587 static struct widget *
1588 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1590 struct widget *child, *target;
1592 wl_list_for_each(child, &widget->child_list, link) {
1593 target = widget_find_widget(child, x, y);
1598 if (widget->allocation.x <= x &&
1599 x < widget->allocation.x + widget->allocation.width &&
1600 widget->allocation.y <= y &&
1601 y < widget->allocation.y + widget->allocation.height) {
1608 static struct widget *
1609 window_find_widget(struct window *window, int32_t x, int32_t y)
1611 struct surface *surface;
1612 struct widget *widget;
1614 wl_list_for_each(surface, &window->subsurface_list, link) {
1615 widget = widget_find_widget(surface->widget, x, y);
1623 static struct widget *
1624 widget_create(struct window *window, struct surface *surface, void *data)
1626 struct widget *widget;
1628 widget = xzalloc(sizeof *widget);
1629 widget->window = window;
1630 widget->surface = surface;
1631 widget->user_data = data;
1632 widget->allocation = surface->allocation;
1633 wl_list_init(&widget->child_list);
1635 widget->tooltip = NULL;
1636 widget->tooltip_count = 0;
1637 widget->default_cursor = CURSOR_LEFT_PTR;
1643 window_add_widget(struct window *window, void *data)
1645 struct widget *widget;
1647 widget = widget_create(window, window->main_surface, data);
1648 wl_list_init(&widget->link);
1649 window->main_surface->widget = widget;
1655 widget_add_widget(struct widget *parent, void *data)
1657 struct widget *widget;
1659 widget = widget_create(parent->window, parent->surface, data);
1660 wl_list_insert(parent->child_list.prev, &widget->link);
1666 widget_destroy(struct widget *widget)
1668 struct display *display = widget->window->display;
1669 struct surface *surface = widget->surface;
1670 struct input *input;
1672 /* Destroy the sub-surface along with the root widget */
1673 if (surface->widget == widget && surface->subsurface)
1674 surface_destroy(widget->surface);
1676 if (widget->tooltip) {
1677 free(widget->tooltip);
1678 widget->tooltip = NULL;
1681 wl_list_for_each(input, &display->input_list, link) {
1682 if (input->focus_widget == widget)
1683 input->focus_widget = NULL;
1686 wl_list_remove(&widget->link);
1691 widget_set_default_cursor(struct widget *widget, int cursor)
1693 widget->default_cursor = cursor;
1697 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1699 *allocation = widget->allocation;
1703 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1705 widget->allocation.width = width;
1706 widget->allocation.height = height;
1710 widget_set_allocation(struct widget *widget,
1711 int32_t x, int32_t y, int32_t width, int32_t height)
1713 widget->allocation.x = x;
1714 widget->allocation.y = y;
1715 widget_set_size(widget, width, height);
1719 widget_set_transparent(struct widget *widget, int transparent)
1721 widget->opaque = !transparent;
1725 widget_get_user_data(struct widget *widget)
1727 return widget->user_data;
1730 static cairo_surface_t *
1731 widget_get_cairo_surface(struct widget *widget)
1733 struct surface *surface = widget->surface;
1734 struct window *window = widget->window;
1736 if (!surface->cairo_surface) {
1737 if (surface == window->main_surface)
1738 window_create_main_surface(window);
1740 surface_create_surface(surface, 0, 0, 0);
1743 return surface->cairo_surface;
1747 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1749 struct surface *surface = widget->surface;
1752 enum wl_output_transform transform;
1753 int surface_width, surface_height;
1754 int translate_x, translate_y;
1757 surface_width = surface->allocation.width;
1758 surface_height = surface->allocation.height;
1760 transform = surface->buffer_transform;
1761 scale = surface->buffer_scale;
1763 switch (transform) {
1764 case WL_OUTPUT_TRANSFORM_FLIPPED:
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1766 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1767 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1768 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1771 cairo_matrix_init_identity(&m);
1775 switch (transform) {
1776 case WL_OUTPUT_TRANSFORM_NORMAL:
1782 case WL_OUTPUT_TRANSFORM_FLIPPED:
1784 translate_x = surface_width;
1787 case WL_OUTPUT_TRANSFORM_90:
1789 translate_x = surface_height;
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1794 translate_x = surface_height;
1795 translate_y = surface_width;
1797 case WL_OUTPUT_TRANSFORM_180:
1799 translate_x = surface_width;
1800 translate_y = surface_height;
1802 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1805 translate_y = surface_height;
1807 case WL_OUTPUT_TRANSFORM_270:
1808 angle = M_PI + M_PI_2;
1810 translate_y = surface_width;
1812 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1813 angle = M_PI + M_PI_2;
1819 cairo_scale(cr, scale, scale);
1820 cairo_translate(cr, translate_x, translate_y);
1821 cairo_rotate(cr, angle);
1822 cairo_transform(cr, &m);
1826 widget_cairo_create(struct widget *widget)
1828 struct surface *surface = widget->surface;
1829 cairo_surface_t *cairo_surface;
1832 cairo_surface = widget_get_cairo_surface(widget);
1833 cr = cairo_create(cairo_surface);
1835 widget_cairo_update_transform(widget, cr);
1837 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1843 widget_get_wl_surface(struct widget *widget)
1845 return widget->surface->surface;
1849 widget_get_last_time(struct widget *widget)
1851 return widget->surface->last_time;
1855 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1857 struct wl_compositor *comp = widget->window->display->compositor;
1858 struct surface *surface = widget->surface;
1860 if (!surface->input_region)
1861 surface->input_region = wl_compositor_create_region(comp);
1864 wl_region_add(surface->input_region,
1865 rect->x, rect->y, rect->width, rect->height);
1870 widget_set_resize_handler(struct widget *widget,
1871 widget_resize_handler_t handler)
1873 widget->resize_handler = handler;
1877 widget_set_redraw_handler(struct widget *widget,
1878 widget_redraw_handler_t handler)
1880 widget->redraw_handler = handler;
1884 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1886 widget->enter_handler = handler;
1890 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1892 widget->leave_handler = handler;
1896 widget_set_motion_handler(struct widget *widget,
1897 widget_motion_handler_t handler)
1899 widget->motion_handler = handler;
1903 widget_set_button_handler(struct widget *widget,
1904 widget_button_handler_t handler)
1906 widget->button_handler = handler;
1910 widget_set_touch_up_handler(struct widget *widget,
1911 widget_touch_up_handler_t handler)
1913 widget->touch_up_handler = handler;
1917 widget_set_touch_down_handler(struct widget *widget,
1918 widget_touch_down_handler_t handler)
1920 widget->touch_down_handler = handler;
1924 widget_set_touch_motion_handler(struct widget *widget,
1925 widget_touch_motion_handler_t handler)
1927 widget->touch_motion_handler = handler;
1931 widget_set_touch_frame_handler(struct widget *widget,
1932 widget_touch_frame_handler_t handler)
1934 widget->touch_frame_handler = handler;
1938 widget_set_touch_cancel_handler(struct widget *widget,
1939 widget_touch_cancel_handler_t handler)
1941 widget->touch_cancel_handler = handler;
1945 widget_set_axis_handler(struct widget *widget,
1946 widget_axis_handler_t handler)
1948 widget->axis_handler = handler;
1952 window_schedule_redraw_task(struct window *window);
1955 widget_schedule_redraw(struct widget *widget)
1957 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1958 widget->surface->redraw_needed = 1;
1959 window_schedule_redraw_task(widget->window);
1963 window_get_surface(struct window *window)
1965 cairo_surface_t *cairo_surface;
1967 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1969 return cairo_surface_reference(cairo_surface);
1973 window_get_wl_surface(struct window *window)
1975 return window->main_surface->surface;
1978 struct wl_shell_surface *
1979 window_get_wl_shell_surface(struct window *window)
1981 return window->shell_surface;
1985 tooltip_redraw_handler(struct widget *widget, void *data)
1988 const int32_t r = 3;
1989 struct tooltip *tooltip = data;
1990 int32_t width, height;
1992 cr = widget_cairo_create(widget);
1993 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1994 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1997 width = widget->allocation.width;
1998 height = widget->allocation.height;
1999 rounded_rect(cr, 0, 0, width, height, r);
2001 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2002 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2005 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2006 cairo_move_to(cr, 10, 16);
2007 cairo_show_text(cr, tooltip->entry);
2011 static cairo_text_extents_t
2012 get_text_extents(struct tooltip *tooltip)
2015 cairo_text_extents_t extents;
2017 /* Use the dummy_surface because tooltip's surface was not
2018 * created yet, and parent does not have a valid surface
2019 * outside repaint, either.
2021 cr = cairo_create(tooltip->window->display->dummy_surface);
2022 cairo_text_extents(cr, tooltip->entry, &extents);
2029 window_create_tooltip(struct tooltip *tooltip)
2031 struct widget *parent = tooltip->parent;
2032 struct display *display = parent->window->display;
2033 struct window *window;
2034 const int offset_y = 27;
2035 const int margin = 3;
2036 cairo_text_extents_t extents;
2038 if (tooltip->widget)
2041 window = window_create_transient(display, parent->window, tooltip->x,
2042 tooltip->y + offset_y,
2043 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2047 tooltip->window = window;
2048 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2050 extents = get_text_extents(tooltip);
2051 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2052 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2058 widget_destroy_tooltip(struct widget *parent)
2060 struct tooltip *tooltip = parent->tooltip;
2062 parent->tooltip_count = 0;
2066 if (tooltip->widget) {
2067 widget_destroy(tooltip->widget);
2068 window_destroy(tooltip->window);
2069 tooltip->widget = NULL;
2070 tooltip->window = NULL;
2073 close(tooltip->tooltip_fd);
2074 free(tooltip->entry);
2076 parent->tooltip = NULL;
2080 tooltip_func(struct task *task, uint32_t events)
2082 struct tooltip *tooltip =
2083 container_of(task, struct tooltip, tooltip_task);
2086 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2088 window_create_tooltip(tooltip);
2091 #define TOOLTIP_TIMEOUT 500
2093 tooltip_timer_reset(struct tooltip *tooltip)
2095 struct itimerspec its;
2097 its.it_interval.tv_sec = 0;
2098 its.it_interval.tv_nsec = 0;
2099 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2100 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2101 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2102 fprintf(stderr, "could not set timerfd\n: %m");
2110 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2112 struct tooltip *tooltip = parent->tooltip;
2114 parent->tooltip_count++;
2118 tooltip_timer_reset(tooltip);
2122 /* the handler might be triggered too fast via input device motion, so
2123 * we need this check here to make sure tooltip is fully initialized */
2124 if (parent->tooltip_count > 1)
2127 tooltip = malloc(sizeof *tooltip);
2131 parent->tooltip = tooltip;
2132 tooltip->parent = parent;
2133 tooltip->widget = NULL;
2134 tooltip->window = NULL;
2137 tooltip->entry = strdup(entry);
2138 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2139 if (tooltip->tooltip_fd < 0) {
2140 fprintf(stderr, "could not create timerfd\n: %m");
2144 tooltip->tooltip_task.run = tooltip_func;
2145 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2146 EPOLLIN, &tooltip->tooltip_task);
2147 tooltip_timer_reset(tooltip);
2153 workspace_manager_state(void *data,
2154 struct workspace_manager *workspace_manager,
2158 struct display *display = data;
2160 display->workspace = current;
2161 display->workspace_count = count;
2164 static const struct workspace_manager_listener workspace_manager_listener = {
2165 workspace_manager_state
2169 frame_resize_handler(struct widget *widget,
2170 int32_t width, int32_t height, void *data)
2172 struct frame *frame = data;
2173 struct widget *child = frame->child;
2174 struct rectangle allocation;
2175 struct display *display = widget->window->display;
2176 struct surface *surface = widget->surface;
2177 struct frame_button * button;
2178 struct theme *t = display->theme;
2179 int x_l, x_r, y, w, h;
2180 int decoration_width, decoration_height;
2181 int opaque_margin, shadow_margin;
2183 switch (widget->window->type) {
2184 case TYPE_FULLSCREEN:
2185 decoration_width = 0;
2186 decoration_height = 0;
2190 allocation.width = width;
2191 allocation.height = height;
2194 wl_list_for_each(button, &frame->buttons_list, link)
2195 button->widget->opaque = 1;
2197 case TYPE_MAXIMIZED:
2198 decoration_width = t->width * 2;
2199 decoration_height = t->width + t->titlebar_height;
2201 allocation.x = t->width;
2202 allocation.y = t->titlebar_height;
2203 allocation.width = width - decoration_width;
2204 allocation.height = height - decoration_height;
2208 wl_list_for_each(button, &frame->buttons_list, link)
2209 button->widget->opaque = 0;
2212 decoration_width = (t->width + t->margin) * 2;
2213 decoration_height = t->width +
2214 t->titlebar_height + t->margin * 2;
2216 allocation.x = t->width + t->margin;
2217 allocation.y = t->titlebar_height + t->margin;
2218 allocation.width = width - decoration_width;
2219 allocation.height = height - decoration_height;
2221 opaque_margin = t->margin + t->frame_radius;
2223 wl_list_for_each(button, &frame->buttons_list, link)
2224 button->widget->opaque = 0;
2228 widget_set_allocation(child, allocation.x, allocation.y,
2229 allocation.width, allocation.height);
2231 if (child->resize_handler)
2232 child->resize_handler(child,
2237 width = child->allocation.width + decoration_width;
2238 height = child->allocation.height + decoration_height;
2240 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2242 surface->input_region =
2243 wl_compositor_create_region(display->compositor);
2244 if (widget->window->type != TYPE_FULLSCREEN) {
2245 wl_region_add(surface->input_region,
2246 shadow_margin, shadow_margin,
2247 width - 2 * shadow_margin,
2248 height - 2 * shadow_margin);
2250 wl_region_add(surface->input_region, 0, 0, width, height);
2253 widget_set_allocation(widget, 0, 0, width, height);
2256 wl_region_add(surface->opaque_region,
2257 opaque_margin, opaque_margin,
2258 widget->allocation.width - 2 * opaque_margin,
2259 widget->allocation.height - 2 * opaque_margin);
2261 /* frame internal buttons */
2262 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2263 x_l = t->width + shadow_margin;
2264 y = t->width + shadow_margin;
2265 wl_list_for_each(button, &frame->buttons_list, link) {
2266 const int button_padding = 4;
2267 w = cairo_image_surface_get_width(button->icon);
2268 h = cairo_image_surface_get_height(button->icon);
2270 if (button->decoration == FRAME_BUTTON_FANCY)
2273 if (button->align == FRAME_BUTTON_LEFT) {
2274 widget_set_allocation(button->widget,
2275 x_l, y , w + 1, h + 1);
2277 x_l += button_padding;
2280 widget_set_allocation(button->widget,
2281 x_r, y , w + 1, h + 1);
2282 x_r -= button_padding;
2288 frame_button_enter_handler(struct widget *widget,
2289 struct input *input, float x, float y, void *data)
2291 struct frame_button *frame_button = data;
2293 widget_schedule_redraw(frame_button->widget);
2294 frame_button->state = FRAME_BUTTON_OVER;
2296 return CURSOR_LEFT_PTR;
2300 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2302 struct frame_button *frame_button = data;
2304 widget_schedule_redraw(frame_button->widget);
2305 frame_button->state = FRAME_BUTTON_DEFAULT;
2309 frame_button_button_handler(struct widget *widget,
2310 struct input *input, uint32_t time,
2312 enum wl_pointer_button_state state, void *data)
2314 struct frame_button *frame_button = data;
2315 struct window *window = widget->window;
2316 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
2318 if (button != BTN_LEFT)
2322 case WL_POINTER_BUTTON_STATE_PRESSED:
2323 frame_button->state = FRAME_BUTTON_ACTIVE;
2324 widget_schedule_redraw(frame_button->widget);
2326 if (frame_button->type == FRAME_BUTTON_ICON)
2327 window_show_frame_menu(window, input, time);
2329 case WL_POINTER_BUTTON_STATE_RELEASED:
2330 frame_button->state = FRAME_BUTTON_DEFAULT;
2331 widget_schedule_redraw(frame_button->widget);
2338 switch (frame_button->type) {
2339 case FRAME_BUTTON_CLOSE:
2340 if (window->close_handler)
2341 window->close_handler(window->parent,
2344 display_exit(window->display);
2346 case FRAME_BUTTON_MINIMIZE:
2347 fprintf(stderr,"Minimize stub\n");
2349 case FRAME_BUTTON_MAXIMIZE:
2350 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2353 /* Unknown operation */
2359 frame_button_touch_down_handler(struct widget *widget, struct input *input,
2360 uint32_t serial, uint32_t time, int32_t id,
2361 float x, float y, void *data)
2363 struct frame_button *frame_button = data;
2364 struct window *window = widget->window;
2366 switch (frame_button->type) {
2367 case FRAME_BUTTON_CLOSE:
2368 if (window->close_handler)
2369 window->close_handler(window->parent,
2372 display_exit(window->display);
2374 case FRAME_BUTTON_MINIMIZE:
2375 fprintf(stderr,"Minimize stub\n");
2377 case FRAME_BUTTON_MAXIMIZE:
2378 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2381 /* Unknown operation */
2388 frame_button_motion_handler(struct widget *widget,
2389 struct input *input, uint32_t time,
2390 float x, float y, void *data)
2392 struct frame_button *frame_button = data;
2393 enum frame_button_pointer previous_button_state = frame_button->state;
2395 /* only track state for a pressed button */
2396 if (input->grab != widget)
2397 return CURSOR_LEFT_PTR;
2399 if (x > widget->allocation.x &&
2400 x < (widget->allocation.x + widget->allocation.width) &&
2401 y > widget->allocation.y &&
2402 y < (widget->allocation.y + widget->allocation.height)) {
2403 frame_button->state = FRAME_BUTTON_ACTIVE;
2405 frame_button->state = FRAME_BUTTON_DEFAULT;
2408 if (frame_button->state != previous_button_state)
2409 widget_schedule_redraw(frame_button->widget);
2411 return CURSOR_LEFT_PTR;
2415 frame_button_redraw_handler(struct widget *widget, void *data)
2417 struct frame_button *frame_button = data;
2419 int width, height, x, y;
2421 x = widget->allocation.x;
2422 y = widget->allocation.y;
2423 width = widget->allocation.width;
2424 height = widget->allocation.height;
2433 cr = widget_cairo_create(widget);
2435 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2436 cairo_set_line_width(cr, 1);
2438 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2439 cairo_rectangle (cr, x, y, 25, 16);
2441 cairo_stroke_preserve(cr);
2443 switch (frame_button->state) {
2444 case FRAME_BUTTON_DEFAULT:
2445 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2447 case FRAME_BUTTON_OVER:
2448 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2450 case FRAME_BUTTON_ACTIVE:
2451 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2460 cairo_set_source_surface(cr, frame_button->icon, x, y);
2466 static struct widget *
2467 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2468 enum frame_button_align align, enum frame_button_decoration style)
2470 struct frame_button *frame_button;
2471 const char *icon = data;
2473 frame_button = xzalloc (sizeof *frame_button);
2474 frame_button->icon = cairo_image_surface_create_from_png(icon);
2475 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2476 frame_button->frame = frame;
2477 frame_button->type = type;
2478 frame_button->align = align;
2479 frame_button->decoration = style;
2481 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2483 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2484 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2485 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2486 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
2487 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
2488 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
2489 return frame_button->widget;
2493 frame_button_destroy(struct frame_button *frame_button)
2495 widget_destroy(frame_button->widget);
2496 wl_list_remove(&frame_button->link);
2497 cairo_surface_destroy(frame_button->icon);
2504 frame_redraw_handler(struct widget *widget, void *data)
2507 struct window *window = widget->window;
2508 struct theme *t = window->display->theme;
2511 if (window->type == TYPE_FULLSCREEN)
2514 cr = widget_cairo_create(widget);
2516 if (window->focus_count)
2517 flags |= THEME_FRAME_ACTIVE;
2518 if (window->type == TYPE_MAXIMIZED)
2519 flags |= THEME_FRAME_MAXIMIZED;
2520 theme_render_frame(t, cr, widget->allocation.width,
2521 widget->allocation.height, window->title, flags);
2527 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
2529 struct theme *t = frame->widget->window->display->theme;
2530 struct window *window = frame->widget->window;
2533 if (window->type != TYPE_TOPLEVEL)
2534 return CURSOR_LEFT_PTR;
2536 location = theme_get_location(t, input->sx, input->sy,
2537 frame->widget->allocation.width,
2538 frame->widget->allocation.height,
2539 window->type == TYPE_MAXIMIZED ?
2540 THEME_FRAME_MAXIMIZED : 0);
2543 case THEME_LOCATION_RESIZING_TOP:
2545 case THEME_LOCATION_RESIZING_BOTTOM:
2546 return CURSOR_BOTTOM;
2547 case THEME_LOCATION_RESIZING_LEFT:
2549 case THEME_LOCATION_RESIZING_RIGHT:
2550 return CURSOR_RIGHT;
2551 case THEME_LOCATION_RESIZING_TOP_LEFT:
2552 return CURSOR_TOP_LEFT;
2553 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2554 return CURSOR_TOP_RIGHT;
2555 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2556 return CURSOR_BOTTOM_LEFT;
2557 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2558 return CURSOR_BOTTOM_RIGHT;
2559 case THEME_LOCATION_EXTERIOR:
2560 case THEME_LOCATION_TITLEBAR:
2562 return CURSOR_LEFT_PTR;
2567 frame_menu_func(struct window *window, int index, void *data)
2569 struct display *display;
2573 if (window->close_handler)
2574 window->close_handler(window->parent,
2577 display_exit(window->display);
2579 case 1: /* move to workspace above */
2580 display = window->display;
2581 if (display->workspace > 0)
2582 workspace_manager_move_surface(
2583 display->workspace_manager,
2584 window->main_surface->surface,
2585 display->workspace - 1);
2587 case 2: /* move to workspace below */
2588 display = window->display;
2589 if (display->workspace < display->workspace_count - 1)
2590 workspace_manager_move_surface(
2591 display->workspace_manager,
2592 window->main_surface->surface,
2593 display->workspace + 1);
2595 case 3: /* fullscreen */
2596 /* we don't have a way to get out of fullscreen for now */
2597 if (window->fullscreen_handler)
2598 window->fullscreen_handler(window, window->user_data);
2604 window_show_frame_menu(struct window *window,
2605 struct input *input, uint32_t time)
2610 static const char *entries[] = {
2612 "Move to workspace above", "Move to workspace below",
2616 if (window->fullscreen_handler)
2617 count = ARRAY_LENGTH(entries);
2619 count = ARRAY_LENGTH(entries) - 1;
2621 input_get_position(input, &x, &y);
2622 window_show_menu(window->display, input, time, window,
2623 x - 10, y - 10, frame_menu_func, entries, count);
2627 frame_enter_handler(struct widget *widget,
2628 struct input *input, float x, float y, void *data)
2630 return frame_get_pointer_image_for_location(data, input);
2634 frame_motion_handler(struct widget *widget,
2635 struct input *input, uint32_t time,
2636 float x, float y, void *data)
2638 return frame_get_pointer_image_for_location(data, input);
2642 frame_button_handler(struct widget *widget,
2643 struct input *input, uint32_t time,
2644 uint32_t button, enum wl_pointer_button_state state,
2648 struct frame *frame = data;
2649 struct window *window = widget->window;
2650 struct display *display = window->display;
2653 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
2656 location = theme_get_location(display->theme, input->sx, input->sy,
2657 frame->widget->allocation.width,
2658 frame->widget->allocation.height,
2659 window->type == TYPE_MAXIMIZED ?
2660 THEME_FRAME_MAXIMIZED : 0);
2662 if (window->display->shell && button == BTN_LEFT &&
2663 window->type == TYPE_TOPLEVEL) {
2665 case THEME_LOCATION_TITLEBAR:
2666 if (!window->shell_surface)
2668 input_ungrab(input);
2669 wl_shell_surface_move(window->shell_surface,
2670 input_get_seat(input),
2673 case THEME_LOCATION_RESIZING_TOP:
2674 case THEME_LOCATION_RESIZING_BOTTOM:
2675 case THEME_LOCATION_RESIZING_LEFT:
2676 case THEME_LOCATION_RESIZING_RIGHT:
2677 case THEME_LOCATION_RESIZING_TOP_LEFT:
2678 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2679 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2680 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2681 if (!window->shell_surface)
2683 input_ungrab(input);
2685 window->resizing = 1;
2686 wl_shell_surface_resize(window->shell_surface,
2687 input_get_seat(input),
2688 display->serial, location);
2691 } else if (button == BTN_RIGHT &&
2692 (window->type == TYPE_TOPLEVEL ||
2693 window->type == TYPE_MAXIMIZED)) {
2694 window_show_frame_menu(window, input, time);
2699 frame_touch_down_handler(struct widget *widget, struct input *input,
2700 uint32_t serial, uint32_t time, int32_t id,
2701 float x, float y, void *data)
2703 struct window *window = widget->window;
2704 struct display *display = window->display;
2706 wl_shell_surface_move(window->shell_surface,
2707 input_get_seat(input),
2712 frame_create(struct window *window, void *data)
2714 struct frame *frame;
2716 frame = xzalloc(sizeof *frame);
2717 frame->widget = window_add_widget(window, frame);
2718 frame->child = widget_add_widget(frame->widget, data);
2720 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2721 widget_set_resize_handler(frame->widget, frame_resize_handler);
2722 widget_set_enter_handler(frame->widget, frame_enter_handler);
2723 widget_set_motion_handler(frame->widget, frame_motion_handler);
2724 widget_set_button_handler(frame->widget, frame_button_handler);
2725 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2727 /* Create empty list for frame buttons */
2728 wl_list_init(&frame->buttons_list);
2730 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2731 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2733 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2734 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2736 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2737 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2739 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2740 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2742 window->frame = frame;
2744 return frame->child;
2748 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2750 struct display *display = widget->window->display;
2751 struct theme *t = display->theme;
2752 int decoration_width, decoration_height;
2754 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2756 if (widget->window->type != TYPE_FULLSCREEN) {
2757 decoration_width = (t->width + margin) * 2;
2758 decoration_height = t->width +
2759 t->titlebar_height + margin * 2;
2761 width = child_width + decoration_width;
2762 height = child_height + decoration_height;
2764 width = child_width;
2765 height = child_height;
2768 window_schedule_resize(widget->window, width, height);
2772 frame_destroy(struct frame *frame)
2774 struct frame_button *button, *tmp;
2776 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2777 frame_button_destroy(button);
2779 /* frame->child must be destroyed by the application */
2780 widget_destroy(frame->widget);
2785 input_set_focus_widget(struct input *input, struct widget *focus,
2788 struct widget *old, *widget;
2791 if (focus == input->focus_widget)
2794 old = input->focus_widget;
2798 widget = input->grab;
2799 if (widget->leave_handler)
2800 widget->leave_handler(old, input, widget->user_data);
2801 input->focus_widget = NULL;
2807 widget = input->grab;
2808 input->focus_widget = focus;
2809 if (widget->enter_handler)
2810 cursor = widget->enter_handler(focus, input, x, y,
2813 cursor = widget->default_cursor;
2815 input_set_pointer_image(input, cursor);
2820 input_grab(struct input *input, struct widget *widget, uint32_t button)
2822 input->grab = widget;
2823 input->grab_button = button;
2827 input_ungrab(struct input *input)
2829 struct widget *widget;
2832 if (input->pointer_focus) {
2833 widget = window_find_widget(input->pointer_focus,
2834 input->sx, input->sy);
2835 input_set_focus_widget(input, widget, input->sx, input->sy);
2840 input_remove_pointer_focus(struct input *input)
2842 struct window *window = input->pointer_focus;
2847 input_set_focus_widget(input, NULL, 0, 0);
2849 input->pointer_focus = NULL;
2850 input->current_cursor = CURSOR_UNSET;
2854 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2855 uint32_t serial, struct wl_surface *surface,
2856 wl_fixed_t sx_w, wl_fixed_t sy_w)
2858 struct input *input = data;
2859 struct window *window;
2860 struct widget *widget;
2861 float sx = wl_fixed_to_double(sx_w);
2862 float sy = wl_fixed_to_double(sy_w);
2865 /* enter event for a window we've just destroyed */
2869 input->display->serial = serial;
2870 input->pointer_enter_serial = serial;
2871 input->pointer_focus = wl_surface_get_user_data(surface);
2872 window = input->pointer_focus;
2874 if (window->resizing) {
2875 window->resizing = 0;
2876 /* Schedule a redraw to free the pool */
2877 window_schedule_redraw(window);
2883 widget = window_find_widget(window, sx, sy);
2884 input_set_focus_widget(input, widget, sx, sy);
2888 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2889 uint32_t serial, struct wl_surface *surface)
2891 struct input *input = data;
2893 input->display->serial = serial;
2894 input_remove_pointer_focus(input);
2898 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2899 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2901 struct input *input = data;
2902 struct window *window = input->pointer_focus;
2903 struct widget *widget;
2905 float sx = wl_fixed_to_double(sx_w);
2906 float sy = wl_fixed_to_double(sy_w);
2914 /* when making the window smaller - e.g. after a unmaximise we might
2915 * still have a pending motion event that the compositor has picked
2916 * based on the old surface dimensions
2918 if (sx > window->main_surface->allocation.width ||
2919 sy > window->main_surface->allocation.height)
2922 if (!(input->grab && input->grab_button)) {
2923 widget = window_find_widget(window, sx, sy);
2924 input_set_focus_widget(input, widget, sx, sy);
2928 widget = input->grab;
2930 widget = input->focus_widget;
2932 if (widget->motion_handler)
2933 cursor = widget->motion_handler(input->focus_widget,
2934 input, time, sx, sy,
2937 cursor = widget->default_cursor;
2939 cursor = CURSOR_LEFT_PTR;
2941 input_set_pointer_image(input, cursor);
2945 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2946 uint32_t time, uint32_t button, uint32_t state_w)
2948 struct input *input = data;
2949 struct widget *widget;
2950 enum wl_pointer_button_state state = state_w;
2952 input->display->serial = serial;
2953 if (input->focus_widget && input->grab == NULL &&
2954 state == WL_POINTER_BUTTON_STATE_PRESSED)
2955 input_grab(input, input->focus_widget, button);
2957 widget = input->grab;
2958 if (widget && widget->button_handler)
2959 (*widget->button_handler)(widget,
2962 input->grab->user_data);
2964 if (input->grab && input->grab_button == button &&
2965 state == WL_POINTER_BUTTON_STATE_RELEASED)
2966 input_ungrab(input);
2970 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2971 uint32_t time, uint32_t axis, wl_fixed_t value)
2973 struct input *input = data;
2974 struct widget *widget;
2976 widget = input->focus_widget;
2978 widget = input->grab;
2979 if (widget && widget->axis_handler)
2980 (*widget->axis_handler)(widget,
2986 static const struct wl_pointer_listener pointer_listener = {
2987 pointer_handle_enter,
2988 pointer_handle_leave,
2989 pointer_handle_motion,
2990 pointer_handle_button,
2991 pointer_handle_axis,
2995 input_remove_keyboard_focus(struct input *input)
2997 struct window *window = input->keyboard_focus;
2998 struct itimerspec its;
3000 its.it_interval.tv_sec = 0;
3001 its.it_interval.tv_nsec = 0;
3002 its.it_value.tv_sec = 0;
3003 its.it_value.tv_nsec = 0;
3004 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3009 window->focus_count--;
3010 if (window->keyboard_focus_handler)
3011 (*window->keyboard_focus_handler)(window, NULL,
3014 input->keyboard_focus = NULL;
3018 keyboard_repeat_func(struct task *task, uint32_t events)
3020 struct input *input =
3021 container_of(task, struct input, repeat_task);
3022 struct window *window = input->keyboard_focus;
3025 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3026 /* If we change the timer between the fd becoming
3027 * readable and getting here, there'll be nothing to
3028 * read and we get EAGAIN. */
3031 if (window && window->key_handler) {
3032 (*window->key_handler)(window, input, input->repeat_time,
3033 input->repeat_key, input->repeat_sym,
3034 WL_KEYBOARD_KEY_STATE_PRESSED,
3040 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3041 uint32_t format, int fd, uint32_t size)
3043 struct input *input = data;
3051 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3056 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3057 if (map_str == MAP_FAILED) {
3062 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3064 XKB_KEYMAP_FORMAT_TEXT_V1,
3066 munmap(map_str, size);
3069 if (!input->xkb.keymap) {
3070 fprintf(stderr, "failed to compile keymap\n");
3074 input->xkb.state = xkb_state_new(input->xkb.keymap);
3075 if (!input->xkb.state) {
3076 fprintf(stderr, "failed to create XKB state\n");
3077 xkb_map_unref(input->xkb.keymap);
3078 input->xkb.keymap = NULL;
3082 input->xkb.control_mask =
3083 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3084 input->xkb.alt_mask =
3085 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3086 input->xkb.shift_mask =
3087 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3091 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3092 uint32_t serial, struct wl_surface *surface,
3093 struct wl_array *keys)
3095 struct input *input = data;
3096 struct window *window;
3098 input->display->serial = serial;
3099 input->keyboard_focus = wl_surface_get_user_data(surface);
3101 window = input->keyboard_focus;
3102 window->focus_count++;
3103 if (window->keyboard_focus_handler)
3104 (*window->keyboard_focus_handler)(window,
3105 input, window->user_data);
3109 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3110 uint32_t serial, struct wl_surface *surface)
3112 struct input *input = data;
3114 input->display->serial = serial;
3115 input_remove_keyboard_focus(input);
3119 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
3120 uint32_t serial, uint32_t time, uint32_t key,
3123 struct input *input = data;
3124 struct window *window = input->keyboard_focus;
3125 uint32_t code, num_syms;
3126 enum wl_keyboard_key_state state = state_w;
3127 const xkb_keysym_t *syms;
3129 struct itimerspec its;
3131 input->display->serial = serial;
3133 if (!window || !input->xkb.state)
3136 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
3138 sym = XKB_KEY_NoSymbol;
3142 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
3143 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3144 window_set_maximized(window,
3145 window->type != TYPE_MAXIMIZED);
3146 } else if (sym == XKB_KEY_F11 &&
3147 window->fullscreen_handler &&
3148 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3149 window->fullscreen_handler(window, window->user_data);
3150 } else if (sym == XKB_KEY_F4 &&
3151 input->modifiers == MOD_ALT_MASK &&
3152 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3153 if (window->close_handler)
3154 window->close_handler(window->parent,
3157 display_exit(window->display);
3158 } else if (window->key_handler) {
3159 (*window->key_handler)(window, input, time, key,
3160 sym, state, window->user_data);
3163 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3164 key == input->repeat_key) {
3165 its.it_interval.tv_sec = 0;
3166 its.it_interval.tv_nsec = 0;
3167 its.it_value.tv_sec = 0;
3168 its.it_value.tv_nsec = 0;
3169 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3170 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3171 input->repeat_sym = sym;
3172 input->repeat_key = key;
3173 input->repeat_time = time;
3174 its.it_interval.tv_sec = 0;
3175 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3176 its.it_value.tv_sec = 0;
3177 its.it_value.tv_nsec = 400 * 1000 * 1000;
3178 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3183 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3184 uint32_t serial, uint32_t mods_depressed,
3185 uint32_t mods_latched, uint32_t mods_locked,
3188 struct input *input = data;
3189 xkb_mod_mask_t mask;
3191 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3192 if (!input->xkb.keymap)
3195 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3196 mods_locked, 0, 0, group);
3197 mask = xkb_state_serialize_mods(input->xkb.state,
3198 XKB_STATE_DEPRESSED |
3200 input->modifiers = 0;
3201 if (mask & input->xkb.control_mask)
3202 input->modifiers |= MOD_CONTROL_MASK;
3203 if (mask & input->xkb.alt_mask)
3204 input->modifiers |= MOD_ALT_MASK;
3205 if (mask & input->xkb.shift_mask)
3206 input->modifiers |= MOD_SHIFT_MASK;
3209 static const struct wl_keyboard_listener keyboard_listener = {
3210 keyboard_handle_keymap,
3211 keyboard_handle_enter,
3212 keyboard_handle_leave,
3213 keyboard_handle_key,
3214 keyboard_handle_modifiers,
3218 touch_handle_down(void *data, struct wl_touch *wl_touch,
3219 uint32_t serial, uint32_t time, struct wl_surface *surface,
3220 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3222 struct input *input = data;
3223 struct widget *widget;
3224 float sx = wl_fixed_to_double(x_w);
3225 float sy = wl_fixed_to_double(y_w);
3227 input->display->serial = serial;
3228 input->touch_focus = wl_surface_get_user_data(surface);
3229 if (!input->touch_focus) {
3230 DBG("Failed to find to touch focus for surface %p\n", surface);
3234 widget = window_find_widget(input->touch_focus,
3235 wl_fixed_to_double(x_w),
3236 wl_fixed_to_double(y_w));
3238 struct touch_point *tp = xmalloc(sizeof *tp);
3241 tp->widget = widget;
3242 wl_list_insert(&input->touch_point_list, &tp->link);
3244 if (widget->touch_down_handler)
3245 (*widget->touch_down_handler)(widget, input,
3254 touch_handle_up(void *data, struct wl_touch *wl_touch,
3255 uint32_t serial, uint32_t time, int32_t id)
3257 struct input *input = data;
3258 struct touch_point *tp, *tmp;
3260 if (!input->touch_focus) {
3261 DBG("No touch focus found for touch up event!\n");
3265 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3269 if (tp->widget->touch_up_handler)
3270 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3272 tp->widget->user_data);
3274 wl_list_remove(&tp->link);
3282 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3283 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3285 struct input *input = data;
3286 struct touch_point *tp;
3287 float sx = wl_fixed_to_double(x_w);
3288 float sy = wl_fixed_to_double(y_w);
3290 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3292 if (!input->touch_focus) {
3293 DBG("No touch focus found for touch motion event!\n");
3297 wl_list_for_each(tp, &input->touch_point_list, link) {
3301 if (tp->widget->touch_motion_handler)
3302 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3304 tp->widget->user_data);
3310 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3312 struct input *input = data;
3313 struct touch_point *tp, *tmp;
3315 DBG("touch_handle_frame\n");
3317 if (!input->touch_focus) {
3318 DBG("No touch focus found for touch frame event!\n");
3322 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3323 if (tp->widget->touch_frame_handler)
3324 (*tp->widget->touch_frame_handler)(tp->widget, input,
3325 tp->widget->user_data);
3327 wl_list_remove(&tp->link);
3333 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3335 struct input *input = data;
3336 struct touch_point *tp, *tmp;
3338 DBG("touch_handle_cancel\n");
3340 if (!input->touch_focus) {
3341 DBG("No touch focus found for touch cancel event!\n");
3345 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3346 if (tp->widget->touch_cancel_handler)
3347 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3348 tp->widget->user_data);
3350 wl_list_remove(&tp->link);
3355 static const struct wl_touch_listener touch_listener = {
3358 touch_handle_motion,
3360 touch_handle_cancel,
3364 seat_handle_capabilities(void *data, struct wl_seat *seat,
3365 enum wl_seat_capability caps)
3367 struct input *input = data;
3369 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3370 input->pointer = wl_seat_get_pointer(seat);
3371 wl_pointer_set_user_data(input->pointer, input);
3372 wl_pointer_add_listener(input->pointer, &pointer_listener,
3374 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3375 wl_pointer_destroy(input->pointer);
3376 input->pointer = NULL;
3379 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3380 input->keyboard = wl_seat_get_keyboard(seat);
3381 wl_keyboard_set_user_data(input->keyboard, input);
3382 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3384 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3385 wl_keyboard_destroy(input->keyboard);
3386 input->keyboard = NULL;
3389 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3390 input->touch = wl_seat_get_touch(seat);
3391 wl_touch_set_user_data(input->touch, input);
3392 wl_touch_add_listener(input->touch, &touch_listener, input);
3393 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3394 wl_touch_destroy(input->touch);
3395 input->touch = NULL;
3400 seat_handle_name(void *data, struct wl_seat *seat,
3406 static const struct wl_seat_listener seat_listener = {
3407 seat_handle_capabilities,
3412 input_get_position(struct input *input, int32_t *x, int32_t *y)
3419 input_get_display(struct input *input)
3421 return input->display;
3425 input_get_seat(struct input *input)
3431 input_get_modifiers(struct input *input)
3433 return input->modifiers;
3437 input_get_focus_widget(struct input *input)
3439 return input->focus_widget;
3443 struct wl_data_offer *offer;
3444 struct input *input;
3445 struct wl_array types;
3448 struct task io_task;
3456 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3458 struct data_offer *offer = data;
3461 p = wl_array_add(&offer->types, sizeof *p);
3465 static const struct wl_data_offer_listener data_offer_listener = {
3470 data_offer_destroy(struct data_offer *offer)
3475 if (offer->refcount == 0) {
3476 wl_data_offer_destroy(offer->offer);
3477 for (p = offer->types.data; *p; p++)
3479 wl_array_release(&offer->types);
3485 data_device_data_offer(void *data,
3486 struct wl_data_device *data_device,
3487 struct wl_data_offer *_offer)
3489 struct data_offer *offer;
3491 offer = xmalloc(sizeof *offer);
3493 wl_array_init(&offer->types);
3494 offer->refcount = 1;
3495 offer->input = data;
3496 offer->offer = _offer;
3497 wl_data_offer_add_listener(offer->offer,
3498 &data_offer_listener, offer);
3502 data_device_enter(void *data, struct wl_data_device *data_device,
3503 uint32_t serial, struct wl_surface *surface,
3504 wl_fixed_t x_w, wl_fixed_t y_w,
3505 struct wl_data_offer *offer)
3507 struct input *input = data;
3508 struct window *window;
3510 float x = wl_fixed_to_double(x_w);
3511 float y = wl_fixed_to_double(y_w);
3514 input->pointer_enter_serial = serial;
3515 window = wl_surface_get_user_data(surface);
3516 input->pointer_focus = window;
3519 input->drag_offer = wl_data_offer_get_user_data(offer);
3521 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3524 types_data = input->drag_offer->types.data;
3526 input->drag_offer = NULL;
3530 window = input->pointer_focus;
3531 if (window->data_handler)
3532 window->data_handler(window, input, x, y, types_data,
3537 data_device_leave(void *data, struct wl_data_device *data_device)
3539 struct input *input = data;
3541 if (input->drag_offer) {
3542 data_offer_destroy(input->drag_offer);
3543 input->drag_offer = NULL;
3548 data_device_motion(void *data, struct wl_data_device *data_device,
3549 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3551 struct input *input = data;
3552 struct window *window = input->pointer_focus;
3553 float x = wl_fixed_to_double(x_w);
3554 float y = wl_fixed_to_double(y_w);
3560 if (input->drag_offer)
3561 types_data = input->drag_offer->types.data;
3565 if (window->data_handler)
3566 window->data_handler(window, input, x, y, types_data,
3571 data_device_drop(void *data, struct wl_data_device *data_device)
3573 struct input *input = data;
3574 struct window *window = input->pointer_focus;
3576 if (window->drop_handler)
3577 window->drop_handler(window, input,
3578 input->sx, input->sy, window->user_data);
3582 data_device_selection(void *data,
3583 struct wl_data_device *wl_data_device,
3584 struct wl_data_offer *offer)
3586 struct input *input = data;
3589 if (input->selection_offer)
3590 data_offer_destroy(input->selection_offer);
3593 input->selection_offer = wl_data_offer_get_user_data(offer);
3594 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3597 input->selection_offer = NULL;
3601 static const struct wl_data_device_listener data_device_listener = {
3602 data_device_data_offer,
3607 data_device_selection
3611 input_set_pointer_image_index(struct input *input, int index)
3613 struct wl_buffer *buffer;
3614 struct wl_cursor *cursor;
3615 struct wl_cursor_image *image;
3617 if (!input->pointer)
3620 cursor = input->display->cursors[input->current_cursor];
3624 if (index >= (int) cursor->image_count) {
3625 fprintf(stderr, "cursor index out of range\n");
3629 image = cursor->images[index];
3630 buffer = wl_cursor_image_get_buffer(image);
3634 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3635 input->pointer_surface,
3636 image->hotspot_x, image->hotspot_y);
3637 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3638 wl_surface_damage(input->pointer_surface, 0, 0,
3639 image->width, image->height);
3640 wl_surface_commit(input->pointer_surface);
3643 static const struct wl_callback_listener pointer_surface_listener;
3646 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3649 struct input *input = data;
3650 struct wl_cursor *cursor;
3654 assert(callback == input->cursor_frame_cb);
3655 wl_callback_destroy(callback);
3656 input->cursor_frame_cb = NULL;
3659 if (!input->pointer)
3662 if (input->current_cursor == CURSOR_BLANK) {
3663 wl_pointer_set_cursor(input->pointer,
3664 input->pointer_enter_serial,
3669 if (input->current_cursor == CURSOR_UNSET)
3671 cursor = input->display->cursors[input->current_cursor];
3675 /* FIXME We don't have the current time on the first call so we set
3676 * the animation start to the time of the first frame callback. */
3678 input->cursor_anim_start = 0;
3679 else if (input->cursor_anim_start == 0)
3680 input->cursor_anim_start = time;
3682 if (time == 0 || input->cursor_anim_start == 0)
3685 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3687 if (cursor->image_count > 1) {
3688 input->cursor_frame_cb =
3689 wl_surface_frame(input->pointer_surface);
3690 wl_callback_add_listener(input->cursor_frame_cb,
3691 &pointer_surface_listener, input);
3694 input_set_pointer_image_index(input, i);
3697 static const struct wl_callback_listener pointer_surface_listener = {
3698 pointer_surface_frame_callback
3702 input_set_pointer_image(struct input *input, int pointer)
3706 if (!input->pointer)
3709 if (input->pointer_enter_serial > input->cursor_serial)
3712 if (!force && pointer == input->current_cursor)
3715 input->current_cursor = pointer;
3716 input->cursor_serial = input->pointer_enter_serial;
3717 if (!input->cursor_frame_cb)
3718 pointer_surface_frame_callback(input, NULL, 0);
3720 /* The current frame callback may be stuck if, for instance,
3721 * the set cursor request was processed by the server after
3722 * this client lost the focus. In this case the cursor surface
3723 * might not be mapped and the frame callback wouldn't ever
3724 * complete. Send a set_cursor and attach to try to map the
3725 * cursor surface again so that the callback will finish */
3726 input_set_pointer_image_index(input, 0);
3730 struct wl_data_device *
3731 input_get_data_device(struct input *input)
3733 return input->data_device;
3737 input_set_selection(struct input *input,
3738 struct wl_data_source *source, uint32_t time)
3740 wl_data_device_set_selection(input->data_device, source, time);
3744 input_accept(struct input *input, const char *type)
3746 wl_data_offer_accept(input->drag_offer->offer,
3747 input->pointer_enter_serial, type);
3751 offer_io_func(struct task *task, uint32_t events)
3753 struct data_offer *offer =
3754 container_of(task, struct data_offer, io_task);
3758 len = read(offer->fd, buffer, sizeof buffer);
3759 offer->func(buffer, len,
3760 offer->x, offer->y, offer->user_data);
3764 data_offer_destroy(offer);
3769 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3770 data_func_t func, void *user_data)
3774 if (pipe2(p, O_CLOEXEC) == -1)
3777 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3780 offer->io_task.run = offer_io_func;
3784 offer->user_data = user_data;
3786 display_watch_fd(offer->input->display,
3787 offer->fd, EPOLLIN, &offer->io_task);
3791 input_receive_drag_data(struct input *input, const char *mime_type,
3792 data_func_t func, void *data)
3794 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3795 input->drag_offer->x = input->sx;
3796 input->drag_offer->y = input->sy;
3800 input_receive_drag_data_to_fd(struct input *input,
3801 const char *mime_type, int fd)
3803 if (input->drag_offer)
3804 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3810 input_receive_selection_data(struct input *input, const char *mime_type,
3811 data_func_t func, void *data)
3815 if (input->selection_offer == NULL)
3818 for (p = input->selection_offer->types.data; *p; p++)
3819 if (strcmp(mime_type, *p) == 0)
3825 data_offer_receive_data(input->selection_offer,
3826 mime_type, func, data);
3831 input_receive_selection_data_to_fd(struct input *input,
3832 const char *mime_type, int fd)
3834 if (input->selection_offer)
3835 wl_data_offer_receive(input->selection_offer->offer,
3842 window_move(struct window *window, struct input *input, uint32_t serial)
3844 if (!window->shell_surface)
3847 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3851 window_touch_move(struct window *window, struct input *input, uint32_t serial)
3853 if (!window->shell_surface)
3856 wl_shell_surface_move(window->shell_surface, input->seat,
3857 window->display->serial);
3861 surface_set_synchronized(struct surface *surface)
3863 if (!surface->subsurface)
3866 if (surface->synchronized)
3869 wl_subsurface_set_sync(surface->subsurface);
3870 surface->synchronized = 1;
3874 surface_set_synchronized_default(struct surface *surface)
3876 if (!surface->subsurface)
3879 if (surface->synchronized == surface->synchronized_default)
3882 if (surface->synchronized_default)
3883 wl_subsurface_set_sync(surface->subsurface);
3885 wl_subsurface_set_desync(surface->subsurface);
3887 surface->synchronized = surface->synchronized_default;
3891 surface_resize(struct surface *surface)
3893 struct widget *widget = surface->widget;
3894 struct wl_compositor *compositor = widget->window->display->compositor;
3896 if (surface->input_region) {
3897 wl_region_destroy(surface->input_region);
3898 surface->input_region = NULL;
3901 if (surface->opaque_region)
3902 wl_region_destroy(surface->opaque_region);
3904 surface->opaque_region = wl_compositor_create_region(compositor);
3906 if (widget->resize_handler)
3907 widget->resize_handler(widget,
3908 widget->allocation.width,
3909 widget->allocation.height,
3912 if (surface->subsurface &&
3913 (surface->allocation.x != widget->allocation.x ||
3914 surface->allocation.y != widget->allocation.y)) {
3915 wl_subsurface_set_position(surface->subsurface,
3916 widget->allocation.x,
3917 widget->allocation.y);
3919 if (surface->allocation.width != widget->allocation.width ||
3920 surface->allocation.height != widget->allocation.height) {
3921 window_schedule_redraw(widget->window);
3923 surface->allocation = widget->allocation;
3926 wl_region_add(surface->opaque_region, 0, 0,
3927 widget->allocation.width,
3928 widget->allocation.height);
3932 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3935 * This hack should be removed, when EGL respects
3936 * eglSwapInterval(0).
3938 * If this window has sub-surfaces, especially a free-running
3939 * EGL-widget, we need to post the parent surface once with
3940 * all the old state to guarantee, that the EGL-widget will
3941 * receive its frame callback soon. Otherwise, a forced call
3942 * to eglSwapBuffers may end up blocking, waiting for a frame
3943 * event that will never come, because we will commit the parent
3944 * surface with all new state only after eglSwapBuffers returns.
3946 * This assumes, that:
3947 * 1. When the EGL widget's resize hook is called, it pauses.
3948 * 2. When the EGL widget's redraw hook is called, it forces a
3949 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3950 * In a single threaded application condition 1 is a no-op.
3952 * XXX: This should actually be after the surface_resize() calls,
3953 * but cannot, because then it would commit the incomplete state
3954 * accumulated from the widget resize hooks.
3956 if (window->subsurface_list.next != &window->main_surface->link ||
3957 window->subsurface_list.prev != &window->main_surface->link)
3958 wl_surface_commit(window->main_surface->surface);
3962 idle_resize(struct window *window)
3964 struct surface *surface;
3966 window->resize_needed = 0;
3967 window->redraw_needed = 1;
3969 DBG("from %dx%d to %dx%d\n",
3970 window->main_surface->server_allocation.width,
3971 window->main_surface->server_allocation.height,
3972 window->pending_allocation.width,
3973 window->pending_allocation.height);
3975 hack_prevent_EGL_sub_surface_deadlock(window);
3977 widget_set_allocation(window->main_surface->widget,
3978 window->pending_allocation.x,
3979 window->pending_allocation.y,
3980 window->pending_allocation.width,
3981 window->pending_allocation.height);
3983 surface_resize(window->main_surface);
3985 /* The main surface is in the list, too. Main surface's
3986 * resize_handler is responsible for calling widget_set_allocation()
3987 * on all sub-surface root widgets, so they will be resized
3990 wl_list_for_each(surface, &window->subsurface_list, link) {
3991 if (surface == window->main_surface)
3994 surface_set_synchronized(surface);
3995 surface_resize(surface);
4000 window_schedule_resize(struct window *window, int width, int height)
4002 window->pending_allocation.x = 0;
4003 window->pending_allocation.y = 0;
4004 window->pending_allocation.width = width;
4005 window->pending_allocation.height = height;
4007 if (window->min_allocation.width == 0)
4008 window->min_allocation = window->pending_allocation;
4009 if (window->pending_allocation.width < window->min_allocation.width)
4010 window->pending_allocation.width = window->min_allocation.width;
4011 if (window->pending_allocation.height < window->min_allocation.height)
4012 window->pending_allocation.height = window->min_allocation.height;
4014 window->resize_needed = 1;
4015 window_schedule_redraw(window);
4019 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4021 window_schedule_resize(widget->window, width, height);
4025 handle_ping(void *data, struct wl_shell_surface *shell_surface,
4028 wl_shell_surface_pong(shell_surface, serial);
4032 handle_configure(void *data, struct wl_shell_surface *shell_surface,
4033 uint32_t edges, int32_t width, int32_t height)
4035 struct window *window = data;
4037 window->resize_edges = edges;
4038 window_schedule_resize(window, width, height);
4042 menu_destroy(struct menu *menu)
4044 widget_destroy(menu->widget);
4045 window_destroy(menu->window);
4050 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4052 struct window *window = data;
4053 struct menu *menu = window->main_surface->widget->user_data;
4055 /* FIXME: Need more context in this event, at least the input
4056 * device. Or just use wl_callback. And this really needs to
4057 * be a window vfunc that the menu can set. And we need the
4060 input_ungrab(menu->input);
4064 static const struct wl_shell_surface_listener shell_surface_listener = {
4071 window_get_allocation(struct window *window,
4072 struct rectangle *allocation)
4074 *allocation = window->main_surface->allocation;
4078 widget_redraw(struct widget *widget)
4080 struct widget *child;
4082 if (widget->redraw_handler)
4083 widget->redraw_handler(widget, widget->user_data);
4084 wl_list_for_each(child, &widget->child_list, link)
4085 widget_redraw(child);
4089 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4091 struct surface *surface = data;
4093 assert(callback == surface->frame_cb);
4094 DBG_OBJ(callback, "done\n");
4095 wl_callback_destroy(callback);
4096 surface->frame_cb = NULL;
4098 surface->last_time = time;
4100 if (surface->redraw_needed || surface->window->redraw_needed) {
4101 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4102 window_schedule_redraw_task(surface->window);
4106 static const struct wl_callback_listener listener = {
4111 surface_redraw(struct surface *surface)
4113 DBG_OBJ(surface->surface, "begin\n");
4115 if (!surface->window->redraw_needed && !surface->redraw_needed)
4118 /* Whole-window redraw forces a redraw even if the previous has
4119 * not yet hit the screen.
4121 if (surface->frame_cb) {
4122 if (!surface->window->redraw_needed)
4125 DBG_OBJ(surface->frame_cb, "cancelled\n");
4126 wl_callback_destroy(surface->frame_cb);
4129 surface->frame_cb = wl_surface_frame(surface->surface);
4130 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4131 DBG_OBJ(surface->frame_cb, "new\n");
4133 surface->redraw_needed = 0;
4134 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4135 widget_redraw(surface->widget);
4136 DBG_OBJ(surface->surface, "done\n");
4140 idle_redraw(struct task *task, uint32_t events)
4142 struct window *window = container_of(task, struct window, redraw_task);
4143 struct surface *surface;
4145 DBG(" --------- \n");
4147 wl_list_init(&window->redraw_task.link);
4148 window->redraw_task_scheduled = 0;
4150 if (window->resize_needed) {
4151 /* throttle resizing to the main surface display */
4152 if (window->main_surface->frame_cb) {
4153 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4157 idle_resize(window);
4160 wl_list_for_each(surface, &window->subsurface_list, link)
4161 surface_redraw(surface);
4163 window->redraw_needed = 0;
4164 window_flush(window);
4166 wl_list_for_each(surface, &window->subsurface_list, link)
4167 surface_set_synchronized_default(surface);
4171 window_schedule_redraw_task(struct window *window)
4173 if (window->configure_requests)
4175 if (!window->redraw_task_scheduled) {
4176 window->redraw_task.run = idle_redraw;
4177 display_defer(window->display, &window->redraw_task);
4178 window->redraw_task_scheduled = 1;
4183 window_schedule_redraw(struct window *window)
4185 struct surface *surface;
4187 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4189 wl_list_for_each(surface, &window->subsurface_list, link)
4190 surface->redraw_needed = 1;
4192 window_schedule_redraw_task(window);
4196 window_is_fullscreen(struct window *window)
4198 return window->type == TYPE_FULLSCREEN;
4202 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4204 struct window *window = data;
4206 wl_callback_destroy(callback);
4207 window->configure_requests--;
4209 if (!window->configure_requests)
4210 window_schedule_redraw(window);
4213 static struct wl_callback_listener configure_request_listener = {
4214 configure_request_completed,
4218 window_defer_redraw_until_configure(struct window* window)
4220 struct wl_callback *callback;
4222 if (window->redraw_task_scheduled) {
4223 wl_list_remove(&window->redraw_task.link);
4224 window->redraw_task_scheduled = 0;
4227 callback = wl_display_sync(window->display->display);
4228 wl_callback_add_listener(callback, &configure_request_listener, window);
4229 window->configure_requests++;
4233 window_set_fullscreen(struct window *window, int fullscreen)
4235 if (!window->display->shell)
4238 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4242 window->saved_type = window->type;
4243 if (window->type == TYPE_TOPLEVEL) {
4244 window->saved_allocation = window->main_surface->allocation;
4246 window->type = TYPE_FULLSCREEN;
4247 wl_shell_surface_set_fullscreen(window->shell_surface,
4248 window->fullscreen_method,
4250 window_defer_redraw_until_configure (window);
4252 if (window->saved_type == TYPE_MAXIMIZED) {
4253 window_set_maximized(window, 1);
4255 window->type = TYPE_TOPLEVEL;
4256 wl_shell_surface_set_toplevel(window->shell_surface);
4257 window_schedule_resize(window,
4258 window->saved_allocation.width,
4259 window->saved_allocation.height);
4266 window_set_fullscreen_method(struct window *window,
4267 enum wl_shell_surface_fullscreen_method method)
4269 window->fullscreen_method = method;
4273 window_is_maximized(struct window *window)
4275 return window->type == TYPE_MAXIMIZED;
4279 window_set_maximized(struct window *window, int maximized)
4281 if (!window->display->shell)
4284 if ((window->type == TYPE_MAXIMIZED) == maximized)
4287 if (window->type == TYPE_TOPLEVEL) {
4288 window->saved_allocation = window->main_surface->allocation;
4289 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4290 window->type = TYPE_MAXIMIZED;
4291 window_defer_redraw_until_configure(window);
4292 } else if (window->type == TYPE_FULLSCREEN) {
4293 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4294 window->type = TYPE_MAXIMIZED;
4295 window_defer_redraw_until_configure(window);
4297 wl_shell_surface_set_toplevel(window->shell_surface);
4298 window->type = TYPE_TOPLEVEL;
4299 window_schedule_resize(window,
4300 window->saved_allocation.width,
4301 window->saved_allocation.height);
4306 window_set_user_data(struct window *window, void *data)
4308 window->user_data = data;
4312 window_get_user_data(struct window *window)
4314 return window->user_data;
4318 window_set_key_handler(struct window *window,
4319 window_key_handler_t handler)
4321 window->key_handler = handler;
4325 window_set_keyboard_focus_handler(struct window *window,
4326 window_keyboard_focus_handler_t handler)
4328 window->keyboard_focus_handler = handler;
4332 window_set_data_handler(struct window *window, window_data_handler_t handler)
4334 window->data_handler = handler;
4338 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4340 window->drop_handler = handler;
4344 window_set_close_handler(struct window *window,
4345 window_close_handler_t handler)
4347 window->close_handler = handler;
4351 window_set_fullscreen_handler(struct window *window,
4352 window_fullscreen_handler_t handler)
4354 window->fullscreen_handler = handler;
4358 window_set_output_handler(struct window *window,
4359 window_output_handler_t handler)
4361 window->output_handler = handler;
4365 window_set_title(struct window *window, const char *title)
4367 free(window->title);
4368 window->title = strdup(title);
4369 if (window->shell_surface)
4370 wl_shell_surface_set_title(window->shell_surface, title);
4374 window_get_title(struct window *window)
4376 return window->title;
4380 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4382 struct text_cursor_position *text_cursor_position =
4383 window->display->text_cursor_position;
4385 if (!text_cursor_position)
4388 text_cursor_position_notify(text_cursor_position,
4389 window->main_surface->surface,
4390 wl_fixed_from_int(x),
4391 wl_fixed_from_int(y));
4395 window_damage(struct window *window, int32_t x, int32_t y,
4396 int32_t width, int32_t height)
4398 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4402 surface_enter(void *data,
4403 struct wl_surface *wl_surface, struct wl_output *wl_output)
4405 struct window *window = data;
4406 struct output *output;
4407 struct output *output_found = NULL;
4408 struct window_output *window_output;
4410 wl_list_for_each(output, &window->display->output_list, link) {
4411 if (output->output == wl_output) {
4412 output_found = output;
4420 window_output = xmalloc(sizeof *window_output);
4421 window_output->output = output_found;
4423 wl_list_insert (&window->window_output_list, &window_output->link);
4425 if (window->output_handler)
4426 window->output_handler(window, output_found, 1,
4431 surface_leave(void *data,
4432 struct wl_surface *wl_surface, struct wl_output *output)
4434 struct window *window = data;
4435 struct window_output *window_output;
4436 struct window_output *window_output_found = NULL;
4438 wl_list_for_each(window_output, &window->window_output_list, link) {
4439 if (window_output->output->output == output) {
4440 window_output_found = window_output;
4445 if (window_output_found) {
4446 wl_list_remove(&window_output_found->link);
4448 if (window->output_handler)
4449 window->output_handler(window, window_output->output,
4450 0, window->user_data);
4452 free(window_output_found);
4456 static const struct wl_surface_listener surface_listener = {
4461 static struct surface *
4462 surface_create(struct window *window)
4464 struct display *display = window->display;
4465 struct surface *surface;
4467 surface = xmalloc(sizeof *surface);
4468 memset(surface, 0, sizeof *surface);
4472 surface->window = window;
4473 surface->surface = wl_compositor_create_surface(display->compositor);
4474 surface->buffer_scale = 1;
4475 wl_surface_add_listener(surface->surface, &surface_listener, window);
4477 wl_list_insert(&window->subsurface_list, &surface->link);
4482 static struct window *
4483 window_create_internal(struct display *display,
4484 struct window *parent, int type)
4486 struct window *window;
4487 struct surface *surface;
4489 window = xzalloc(sizeof *window);
4490 wl_list_init(&window->subsurface_list);
4491 window->display = display;
4492 window->parent = parent;
4494 surface = surface_create(window);
4495 window->main_surface = surface;
4497 if (type != TYPE_CUSTOM && display->shell) {
4498 window->shell_surface =
4499 wl_shell_get_shell_surface(display->shell,
4501 fail_on_null(window->shell_surface);
4504 window->type = type;
4505 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4506 window->configure_requests = 0;
4507 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4509 if (display->argb_device)
4510 #ifdef HAVE_CAIRO_EGL
4511 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4513 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4516 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4518 wl_surface_set_user_data(surface->surface, window);
4519 wl_list_insert(display->window_list.prev, &window->link);
4520 wl_list_init(&window->redraw_task.link);
4522 if (window->shell_surface) {
4523 wl_shell_surface_set_user_data(window->shell_surface, window);
4524 wl_shell_surface_add_listener(window->shell_surface,
4525 &shell_surface_listener, window);
4528 wl_list_init (&window->window_output_list);
4534 window_create(struct display *display)
4536 return window_create_internal(display, NULL, TYPE_NONE);
4540 window_create_custom(struct display *display)
4542 return window_create_internal(display, NULL, TYPE_CUSTOM);
4546 window_create_transient(struct display *display, struct window *parent,
4547 int32_t x, int32_t y, uint32_t flags)
4549 struct window *window;
4551 window = window_create_internal(parent->display,
4552 parent, TYPE_TRANSIENT);
4558 wl_shell_surface_set_transient(
4559 window->shell_surface,
4560 window->parent->main_surface->surface,
4561 window->x, window->y, flags);
4567 menu_set_item(struct menu *menu, int sy)
4571 next = (sy - 8) / 20;
4572 if (menu->current != next) {
4573 menu->current = next;
4574 widget_schedule_redraw(menu->widget);
4579 menu_motion_handler(struct widget *widget,
4580 struct input *input, uint32_t time,
4581 float x, float y, void *data)
4583 struct menu *menu = data;
4585 if (widget == menu->widget)
4586 menu_set_item(data, y);
4588 return CURSOR_LEFT_PTR;
4592 menu_enter_handler(struct widget *widget,
4593 struct input *input, float x, float y, void *data)
4595 struct menu *menu = data;
4597 if (widget == menu->widget)
4598 menu_set_item(data, y);
4600 return CURSOR_LEFT_PTR;
4604 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4606 struct menu *menu = data;
4608 if (widget == menu->widget)
4609 menu_set_item(data, -200);
4613 menu_button_handler(struct widget *widget,
4614 struct input *input, uint32_t time,
4615 uint32_t button, enum wl_pointer_button_state state,
4619 struct menu *menu = data;
4621 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4622 (menu->release_count > 0 || time - menu->time > 500)) {
4623 /* Either relase after press-drag-release or
4624 * click-motion-click. */
4625 menu->func(menu->window->parent,
4626 menu->current, menu->window->parent->user_data);
4627 input_ungrab(input);
4629 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4630 menu->release_count++;
4635 menu_redraw_handler(struct widget *widget, void *data)
4638 const int32_t r = 3, margin = 3;
4639 struct menu *menu = data;
4640 int32_t width, height, i;
4642 cr = widget_cairo_create(widget);
4643 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4644 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4647 width = widget->allocation.width;
4648 height = widget->allocation.height;
4649 rounded_rect(cr, 0, 0, width, height, r);
4651 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
4652 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4655 for (i = 0; i < menu->count; i++) {
4656 if (i == menu->current) {
4657 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4658 cairo_rectangle(cr, margin, i * 20 + margin,
4659 width - 2 * margin, 20);
4661 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4662 cairo_move_to(cr, 10, i * 20 + 16);
4663 cairo_show_text(cr, menu->entries[i]);
4665 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4666 cairo_move_to(cr, 10, i * 20 + 16);
4667 cairo_show_text(cr, menu->entries[i]);
4675 window_show_menu(struct display *display,
4676 struct input *input, uint32_t time, struct window *parent,
4677 int32_t x, int32_t y,
4678 menu_func_t func, const char **entries, int count)
4680 struct window *window;
4682 const int32_t margin = 3;
4684 menu = malloc(sizeof *menu);
4688 window = window_create_internal(parent->display, parent, TYPE_MENU);
4694 menu->window = window;
4695 menu->widget = window_add_widget(menu->window, menu);
4696 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4697 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4698 menu->entries = entries;
4699 menu->count = count;
4700 menu->release_count = 0;
4704 menu->input = input;
4705 window->type = TYPE_MENU;
4709 input_ungrab(input);
4710 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4711 display_get_serial(window->display),
4712 window->parent->main_surface->surface,
4713 window->x, window->y, 0);
4715 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4716 widget_set_enter_handler(menu->widget, menu_enter_handler);
4717 widget_set_leave_handler(menu->widget, menu_leave_handler);
4718 widget_set_motion_handler(menu->widget, menu_motion_handler);
4719 widget_set_button_handler(menu->widget, menu_button_handler);
4721 input_grab(input, menu->widget, 0);
4722 window_schedule_resize(window, 200, count * 20 + margin * 2);
4726 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4728 window->main_surface->buffer_type = type;
4732 window_set_preferred_format(struct window *window,
4733 enum preferred_format format)
4735 window->preferred_format = format;
4739 window_add_subsurface(struct window *window, void *data,
4740 enum subsurface_mode default_mode)
4742 struct widget *widget;
4743 struct surface *surface;
4744 struct wl_surface *parent;
4745 struct wl_subcompositor *subcompo = window->display->subcompositor;
4747 surface = surface_create(window);
4748 widget = widget_create(window, surface, data);
4749 wl_list_init(&widget->link);
4750 surface->widget = widget;
4752 parent = window->main_surface->surface;
4753 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4756 surface->synchronized = 1;
4758 switch (default_mode) {
4759 case SUBSURFACE_SYNCHRONIZED:
4760 surface->synchronized_default = 1;
4762 case SUBSURFACE_DESYNCHRONIZED:
4763 surface->synchronized_default = 0;
4766 assert(!"bad enum subsurface_mode");
4773 display_handle_geometry(void *data,
4774 struct wl_output *wl_output,
4777 int physical_height,
4783 struct output *output = data;
4785 output->allocation.x = x;
4786 output->allocation.y = y;
4787 output->transform = transform;
4791 display_handle_done(void *data,
4792 struct wl_output *wl_output)
4797 display_handle_scale(void *data,
4798 struct wl_output *wl_output,
4801 struct output *output = data;
4803 output->scale = scale;
4807 display_handle_mode(void *data,
4808 struct wl_output *wl_output,
4814 struct output *output = data;
4815 struct display *display = output->display;
4817 if (flags & WL_OUTPUT_MODE_CURRENT) {
4818 output->allocation.width = width;
4819 output->allocation.height = height;
4820 if (display->output_configure_handler)
4821 (*display->output_configure_handler)(
4822 output, display->user_data);
4826 static const struct wl_output_listener output_listener = {
4827 display_handle_geometry,
4828 display_handle_mode,
4829 display_handle_done,
4830 display_handle_scale
4834 display_add_output(struct display *d, uint32_t id)
4836 struct output *output;
4838 output = xzalloc(sizeof *output);
4839 output->display = d;
4842 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4843 wl_list_insert(d->output_list.prev, &output->link);
4845 wl_output_add_listener(output->output, &output_listener, output);
4849 output_destroy(struct output *output)
4851 if (output->destroy_handler)
4852 (*output->destroy_handler)(output, output->user_data);
4854 wl_output_destroy(output->output);
4855 wl_list_remove(&output->link);
4860 display_set_global_handler(struct display *display,
4861 display_global_handler_t handler)
4863 struct global *global;
4865 display->global_handler = handler;
4869 wl_list_for_each(global, &display->global_list, link)
4870 display->global_handler(display,
4871 global->name, global->interface,
4872 global->version, display->user_data);
4876 display_set_output_configure_handler(struct display *display,
4877 display_output_handler_t handler)
4879 struct output *output;
4881 display->output_configure_handler = handler;
4885 wl_list_for_each(output, &display->output_list, link) {
4886 if (output->allocation.width == 0 &&
4887 output->allocation.height == 0)
4890 (*display->output_configure_handler)(output,
4891 display->user_data);
4896 output_set_user_data(struct output *output, void *data)
4898 output->user_data = data;
4902 output_get_user_data(struct output *output)
4904 return output->user_data;
4908 output_set_destroy_handler(struct output *output,
4909 display_output_handler_t handler)
4911 output->destroy_handler = handler;
4912 /* FIXME: implement this, once we have way to remove outputs */
4916 output_get_allocation(struct output *output, struct rectangle *base)
4918 struct rectangle allocation = output->allocation;
4920 switch (output->transform) {
4921 case WL_OUTPUT_TRANSFORM_90:
4922 case WL_OUTPUT_TRANSFORM_270:
4923 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4924 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4925 /* Swap width and height */
4926 allocation.width = output->allocation.height;
4927 allocation.height = output->allocation.width;
4935 output_get_wl_output(struct output *output)
4937 return output->output;
4940 enum wl_output_transform
4941 output_get_transform(struct output *output)
4943 return output->transform;
4947 output_get_scale(struct output *output)
4949 return output->scale;
4953 fini_xkb(struct input *input)
4955 xkb_state_unref(input->xkb.state);
4956 xkb_map_unref(input->xkb.keymap);
4959 #define MAX(a,b) ((a) > (b) ? a : b)
4962 display_add_input(struct display *d, uint32_t id)
4964 struct input *input;
4966 input = xzalloc(sizeof *input);
4968 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4969 MAX(d->seat_version, 3));
4970 input->touch_focus = NULL;
4971 input->pointer_focus = NULL;
4972 input->keyboard_focus = NULL;
4973 wl_list_init(&input->touch_point_list);
4974 wl_list_insert(d->input_list.prev, &input->link);
4976 wl_seat_add_listener(input->seat, &seat_listener, input);
4977 wl_seat_set_user_data(input->seat, input);
4979 input->data_device =
4980 wl_data_device_manager_get_data_device(d->data_device_manager,
4982 wl_data_device_add_listener(input->data_device, &data_device_listener,
4985 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4987 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4988 TFD_CLOEXEC | TFD_NONBLOCK);
4989 input->repeat_task.run = keyboard_repeat_func;
4990 display_watch_fd(d, input->repeat_timer_fd,
4991 EPOLLIN, &input->repeat_task);
4995 input_destroy(struct input *input)
4997 input_remove_keyboard_focus(input);
4998 input_remove_pointer_focus(input);
5000 if (input->drag_offer)
5001 data_offer_destroy(input->drag_offer);
5003 if (input->selection_offer)
5004 data_offer_destroy(input->selection_offer);
5006 wl_data_device_destroy(input->data_device);
5008 if (input->display->seat_version >= 3) {
5010 wl_pointer_release(input->pointer);
5011 if (input->keyboard)
5012 wl_keyboard_release(input->keyboard);
5017 wl_surface_destroy(input->pointer_surface);
5019 wl_list_remove(&input->link);
5020 wl_seat_destroy(input->seat);
5021 close(input->repeat_timer_fd);
5026 init_workspace_manager(struct display *d, uint32_t id)
5028 d->workspace_manager =
5029 wl_registry_bind(d->registry, id,
5030 &workspace_manager_interface, 1);
5031 if (d->workspace_manager != NULL)
5032 workspace_manager_add_listener(d->workspace_manager,
5033 &workspace_manager_listener,
5038 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5040 struct display *d = data;
5042 if (format == WL_SHM_FORMAT_RGB565)
5046 struct wl_shm_listener shm_listener = {
5051 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5052 const char *interface, uint32_t version)
5054 struct display *d = data;
5055 struct global *global;
5057 global = xmalloc(sizeof *global);
5059 global->interface = strdup(interface);
5060 global->version = version;
5061 wl_list_insert(d->global_list.prev, &global->link);
5063 if (strcmp(interface, "wl_compositor") == 0) {
5064 d->compositor = wl_registry_bind(registry, id,
5065 &wl_compositor_interface, 3);
5066 } else if (strcmp(interface, "wl_output") == 0) {
5067 display_add_output(d, id);
5068 } else if (strcmp(interface, "wl_seat") == 0) {
5069 d->seat_version = version;
5070 display_add_input(d, id);
5071 } else if (strcmp(interface, "wl_shell") == 0) {
5072 d->shell = wl_registry_bind(registry,
5073 id, &wl_shell_interface, 1);
5074 } else if (strcmp(interface, "wl_shm") == 0) {
5075 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5076 wl_shm_add_listener(d->shm, &shm_listener, d);
5077 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5078 d->data_device_manager =
5079 wl_registry_bind(registry, id,
5080 &wl_data_device_manager_interface, 1);
5081 } else if (strcmp(interface, "text_cursor_position") == 0) {
5082 d->text_cursor_position =
5083 wl_registry_bind(registry, id,
5084 &text_cursor_position_interface, 1);
5085 } else if (strcmp(interface, "workspace_manager") == 0) {
5086 init_workspace_manager(d, id);
5087 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5089 wl_registry_bind(registry, id,
5090 &wl_subcompositor_interface, 1);
5093 if (d->global_handler)
5094 d->global_handler(d, id, interface, version, d->user_data);
5098 registry_handle_global_remove(void *data, struct wl_registry *registry,
5101 struct display *d = data;
5102 struct global *global;
5105 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5106 if (global->name != name)
5109 /* XXX: Should destroy bound globals, and call
5110 * the counterpart of display::global_handler
5112 wl_list_remove(&global->link);
5113 free(global->interface);
5119 display_bind(struct display *display, uint32_t name,
5120 const struct wl_interface *interface, uint32_t version)
5122 return wl_registry_bind(display->registry, name, interface, version);
5125 static const struct wl_registry_listener registry_listener = {
5126 registry_handle_global,
5127 registry_handle_global_remove
5130 #ifdef HAVE_CAIRO_EGL
5132 init_egl(struct display *d)
5134 EGLint major, minor;
5137 #ifdef USE_CAIRO_GLESV2
5138 # define GL_BIT EGL_OPENGL_ES2_BIT
5140 # define GL_BIT EGL_OPENGL_BIT
5143 static const EGLint argb_cfg_attribs[] = {
5144 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5150 EGL_RENDERABLE_TYPE, GL_BIT,
5154 #ifdef USE_CAIRO_GLESV2
5155 static const EGLint context_attribs[] = {
5156 EGL_CONTEXT_CLIENT_VERSION, 2,
5159 EGLint api = EGL_OPENGL_ES_API;
5161 EGLint *context_attribs = NULL;
5162 EGLint api = EGL_OPENGL_API;
5165 d->dpy = eglGetDisplay(d->display);
5166 if (!eglInitialize(d->dpy, &major, &minor)) {
5167 fprintf(stderr, "failed to initialize EGL\n");
5171 if (!eglBindAPI(api)) {
5172 fprintf(stderr, "failed to bind EGL client API\n");
5176 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5177 &d->argb_config, 1, &n) || n != 1) {
5178 fprintf(stderr, "failed to choose argb EGL config\n");
5182 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5183 EGL_NO_CONTEXT, context_attribs);
5184 if (d->argb_ctx == NULL) {
5185 fprintf(stderr, "failed to create EGL context\n");
5189 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5190 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5191 fprintf(stderr, "failed to get cairo EGL argb device\n");
5199 fini_egl(struct display *display)
5201 cairo_device_destroy(display->argb_device);
5203 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5206 eglTerminate(display->dpy);
5212 init_dummy_surface(struct display *display)
5217 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5219 display->dummy_surface =
5220 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5222 display->dummy_surface_data = data;
5226 handle_display_data(struct task *task, uint32_t events)
5228 struct display *display =
5229 container_of(task, struct display, display_task);
5230 struct epoll_event ep;
5233 display->display_fd_events = events;
5235 if (events & EPOLLERR || events & EPOLLHUP) {
5236 display_exit(display);
5240 if (events & EPOLLIN) {
5241 ret = wl_display_dispatch(display->display);
5243 display_exit(display);
5248 if (events & EPOLLOUT) {
5249 ret = wl_display_flush(display->display);
5251 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5252 ep.data.ptr = &display->display_task;
5253 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5254 display->display_fd, &ep);
5255 } else if (ret == -1 && errno != EAGAIN) {
5256 display_exit(display);
5263 log_handler(const char *format, va_list args)
5265 vfprintf(stderr, format, args);
5269 display_create(int *argc, char *argv[])
5273 wl_log_set_handler_client(log_handler);
5275 d = zalloc(sizeof *d);
5279 d->display = wl_display_connect(NULL);
5280 if (d->display == NULL) {
5281 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5286 d->xkb_context = xkb_context_new(0);
5287 if (d->xkb_context == NULL) {
5288 fprintf(stderr, "Failed to create XKB context\n");
5293 d->epoll_fd = os_epoll_create_cloexec();
5294 d->display_fd = wl_display_get_fd(d->display);
5295 d->display_task.run = handle_display_data;
5296 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5299 wl_list_init(&d->deferred_list);
5300 wl_list_init(&d->input_list);
5301 wl_list_init(&d->output_list);
5302 wl_list_init(&d->global_list);
5305 d->workspace_count = 1;
5307 d->registry = wl_display_get_registry(d->display);
5308 wl_registry_add_listener(d->registry, ®istry_listener, d);
5310 if (wl_display_dispatch(d->display) < 0) {
5311 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5315 #ifdef HAVE_CAIRO_EGL
5316 if (init_egl(d) < 0)
5317 fprintf(stderr, "EGL does not seem to work, "
5318 "falling back to software rendering and wl_shm.\n");
5323 d->theme = theme_create();
5325 wl_list_init(&d->window_list);
5327 init_dummy_surface(d);
5333 display_destroy_outputs(struct display *display)
5336 struct output *output;
5338 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5339 output_destroy(output);
5343 display_destroy_inputs(struct display *display)
5346 struct input *input;
5348 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5349 input_destroy(input);
5353 display_destroy(struct display *display)
5355 if (!wl_list_empty(&display->window_list))
5356 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5357 wl_list_length(&display->window_list));
5359 if (!wl_list_empty(&display->deferred_list))
5360 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5362 cairo_surface_destroy(display->dummy_surface);
5363 free(display->dummy_surface_data);
5365 display_destroy_outputs(display);
5366 display_destroy_inputs(display);
5368 xkb_context_unref(display->xkb_context);
5370 theme_destroy(display->theme);
5371 destroy_cursors(display);
5373 #ifdef HAVE_CAIRO_EGL
5374 if (display->argb_device)
5378 if (display->subcompositor)
5379 wl_subcompositor_destroy(display->subcompositor);
5382 wl_shell_destroy(display->shell);
5385 wl_shm_destroy(display->shm);
5387 if (display->data_device_manager)
5388 wl_data_device_manager_destroy(display->data_device_manager);
5390 wl_compositor_destroy(display->compositor);
5391 wl_registry_destroy(display->registry);
5393 close(display->epoll_fd);
5395 if (!(display->display_fd_events & EPOLLERR) &&
5396 !(display->display_fd_events & EPOLLHUP))
5397 wl_display_flush(display->display);
5399 wl_display_disconnect(display->display);
5404 display_set_user_data(struct display *display, void *data)
5406 display->user_data = data;
5410 display_get_user_data(struct display *display)
5412 return display->user_data;
5416 display_get_display(struct display *display)
5418 return display->display;
5422 display_has_subcompositor(struct display *display)
5424 if (display->subcompositor)
5427 wl_display_roundtrip(display->display);
5429 return display->subcompositor != NULL;
5433 display_get_cairo_device(struct display *display)
5435 return display->argb_device;
5439 display_get_output(struct display *display)
5441 return container_of(display->output_list.next, struct output, link);
5444 struct wl_compositor *
5445 display_get_compositor(struct display *display)
5447 return display->compositor;
5451 display_get_serial(struct display *display)
5453 return display->serial;
5457 display_get_egl_display(struct display *d)
5462 struct wl_data_source *
5463 display_create_data_source(struct display *display)
5465 return wl_data_device_manager_create_data_source(display->data_device_manager);
5469 display_get_argb_egl_config(struct display *d)
5471 return d->argb_config;
5475 display_get_shell(struct display *display)
5477 return display->shell;
5481 display_acquire_window_surface(struct display *display,
5482 struct window *window,
5485 struct surface *surface = window->main_surface;
5487 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5490 widget_get_cairo_surface(window->main_surface->widget);
5491 return surface->toysurface->acquire(surface->toysurface, ctx);
5495 display_release_window_surface(struct display *display,
5496 struct window *window)
5498 struct surface *surface = window->main_surface;
5500 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5503 surface->toysurface->release(surface->toysurface);
5507 display_defer(struct display *display, struct task *task)
5509 wl_list_insert(&display->deferred_list, &task->link);
5513 display_watch_fd(struct display *display,
5514 int fd, uint32_t events, struct task *task)
5516 struct epoll_event ep;
5520 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5524 display_unwatch_fd(struct display *display, int fd)
5526 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5530 display_run(struct display *display)
5533 struct epoll_event ep[16];
5536 display->running = 1;
5538 while (!wl_list_empty(&display->deferred_list)) {
5539 task = container_of(display->deferred_list.prev,
5541 wl_list_remove(&task->link);
5545 wl_display_dispatch_pending(display->display);
5547 if (!display->running)
5550 ret = wl_display_flush(display->display);
5551 if (ret < 0 && errno == EAGAIN) {
5553 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5554 ep[0].data.ptr = &display->display_task;
5556 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5557 display->display_fd, &ep[0]);
5558 } else if (ret < 0) {
5562 count = epoll_wait(display->epoll_fd,
5563 ep, ARRAY_LENGTH(ep), -1);
5564 for (i = 0; i < count; i++) {
5565 task = ep[i].data.ptr;
5566 task->run(task, ep[i].events);
5572 display_exit(struct display *display)
5574 display->running = 0;
5578 keysym_modifiers_add(struct wl_array *modifiers_map,
5581 size_t len = strlen(name) + 1;
5584 p = wl_array_add(modifiers_map, len);
5589 strncpy(p, name, len);
5592 static xkb_mod_index_t
5593 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5596 xkb_mod_index_t index = 0;
5597 char *p = modifiers_map->data;
5599 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5600 if (strcmp(p, name) == 0)
5607 return XKB_MOD_INVALID;
5611 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5614 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5616 if (index == XKB_MOD_INVALID)
5617 return XKB_MOD_INVALID;
5623 fail_on_null(void *p)
5626 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5636 return fail_on_null(malloc(s));
5642 return fail_on_null(zalloc(s));
5646 xstrdup(const char *s)
5648 return fail_on_null(strdup(s));