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 "xdg-shell-client-protocol.h"
69 #include "text-cursor-position-client-protocol.h"
70 #include "workspaces-client-protocol.h"
71 #include "../shared/os-compatibility.h"
85 struct wl_display *display;
86 struct wl_registry *registry;
87 struct wl_compositor *compositor;
88 struct wl_subcompositor *subcompositor;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
93 struct xdg_shell *xdg_shell;
95 EGLConfig argb_config;
97 cairo_device_t *argb_device;
101 uint32_t display_fd_events;
102 struct task display_task;
105 struct wl_list deferred_list;
109 struct wl_list global_list;
110 struct wl_list window_list;
111 struct wl_list input_list;
112 struct wl_list output_list;
116 struct wl_cursor_theme *cursor_theme;
117 struct wl_cursor **cursors;
119 display_output_handler_t output_configure_handler;
120 display_global_handler_t global_handler;
121 display_global_handler_t global_handler_remove;
125 struct xkb_context *xkb_context;
128 uint32_t workspace_count;
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
138 struct window_output {
139 struct output *output;
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
148 * width,height are the new buffer size.
149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
151 * Returns the Cairo surface to draw to.
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
154 int32_t width, int32_t height, uint32_t flags,
155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
162 void (*swap)(struct toysurface *base,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
164 struct rectangle *server_allocation);
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
173 * Release the toysurface from the EGL context, returning control
176 void (*release)(struct toysurface *base);
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
182 void (*destroy)(struct toysurface *base);
186 struct window *window;
188 struct wl_surface *surface;
189 struct wl_subsurface *subsurface;
191 int synchronized_default;
192 struct toysurface *toysurface;
193 struct widget *widget;
195 struct wl_callback *frame_cb;
198 struct rectangle allocation;
199 struct rectangle server_allocation;
201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
206 int32_t buffer_scale;
208 cairo_surface_t *cairo_surface;
214 struct display *display;
215 struct wl_list window_output_list;
217 struct rectangle saved_allocation;
218 struct rectangle min_allocation;
219 struct rectangle pending_allocation;
222 int redraw_task_scheduled;
223 struct task redraw_task;
233 enum preferred_format preferred_format;
235 window_key_handler_t key_handler;
236 window_keyboard_focus_handler_t keyboard_focus_handler;
237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
239 window_close_handler_t close_handler;
240 window_fullscreen_handler_t fullscreen_handler;
241 window_output_handler_t output_handler;
242 window_state_changed_handler_t state_changed_handler;
244 struct surface *main_surface;
245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
248 struct window *parent;
250 struct window_frame *frame;
252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
260 struct window *window;
261 struct surface *surface;
262 struct tooltip *tooltip;
263 struct wl_list child_list;
265 struct rectangle allocation;
266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
270 widget_motion_handler_t motion_handler;
271 widget_button_handler_t button_handler;
272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
277 widget_axis_handler_t axis_handler;
282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
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;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
337 int32_t repeat_rate_sec;
338 int32_t repeat_rate_nsec;
339 int32_t repeat_delay_sec;
340 int32_t repeat_delay_nsec;
342 struct task repeat_task;
346 uint32_t repeat_time;
350 struct display *display;
351 struct wl_output *output;
352 uint32_t server_output_id;
353 struct rectangle allocation;
360 display_output_handler_t destroy_handler;
364 struct window_frame {
365 struct widget *widget;
366 struct widget *child;
370 uint32_t did_double, double_click;
375 struct window *window;
376 struct widget *widget;
379 const char **entries;
388 struct widget *parent;
389 struct widget *widget;
391 struct task tooltip_task;
397 struct wl_shm_pool *pool;
404 CURSOR_DEFAULT = 100,
408 static const cairo_user_data_key_t shm_surface_data_key;
415 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
416 __attribute__ ((format (printf, 4, 5)));
419 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
424 gettimeofday(&tv, NULL);
425 fprintf(stderr, "%8ld.%03ld ",
426 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
429 fprintf(stderr, "%s@%d ",
430 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
432 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
433 fprintf(stderr, "%s ", func);
436 vfprintf(stderr, fmt, ap);
440 #define DBG(fmt, ...) \
441 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
443 #define DBG_OBJ(obj, fmt, ...) \
444 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
448 #define DBG(...) do {} while (0)
449 #define DBG_OBJ(...) do {} while (0)
454 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
458 switch (buffer_transform) {
459 case WL_OUTPUT_TRANSFORM_90:
460 case WL_OUTPUT_TRANSFORM_270:
461 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
462 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
471 *width *= buffer_scale;
472 *height *= buffer_scale;
476 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
480 switch (buffer_transform) {
481 case WL_OUTPUT_TRANSFORM_90:
482 case WL_OUTPUT_TRANSFORM_270:
483 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
484 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
493 *width /= buffer_scale;
494 *height /= buffer_scale;
497 #ifdef HAVE_CAIRO_EGL
499 struct egl_window_surface {
500 struct toysurface base;
501 cairo_surface_t *cairo_surface;
502 struct display *display;
503 struct wl_surface *surface;
504 struct wl_egl_window *egl_window;
505 EGLSurface egl_surface;
508 static struct egl_window_surface *
509 to_egl_window_surface(struct toysurface *base)
511 return container_of(base, struct egl_window_surface, base);
514 static cairo_surface_t *
515 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
516 int32_t width, int32_t height, uint32_t flags,
517 enum wl_output_transform buffer_transform, int32_t buffer_scale)
519 struct egl_window_surface *surface = to_egl_window_surface(base);
521 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
523 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
524 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
526 return cairo_surface_reference(surface->cairo_surface);
530 egl_window_surface_swap(struct toysurface *base,
531 enum wl_output_transform buffer_transform, int32_t buffer_scale,
532 struct rectangle *server_allocation)
534 struct egl_window_surface *surface = to_egl_window_surface(base);
536 cairo_gl_surface_swapbuffers(surface->cairo_surface);
537 wl_egl_window_get_attached_size(surface->egl_window,
538 &server_allocation->width,
539 &server_allocation->height);
541 buffer_to_surface_size (buffer_transform, buffer_scale,
542 &server_allocation->width,
543 &server_allocation->height);
547 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
549 struct egl_window_surface *surface = to_egl_window_surface(base);
550 cairo_device_t *device;
552 device = cairo_surface_get_device(surface->cairo_surface);
557 if (device == surface->display->argb_device)
558 ctx = surface->display->argb_ctx;
563 cairo_device_flush(device);
564 cairo_device_acquire(device);
565 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
566 surface->egl_surface, ctx))
567 fprintf(stderr, "failed to make surface current\n");
573 egl_window_surface_release(struct toysurface *base)
575 struct egl_window_surface *surface = to_egl_window_surface(base);
576 cairo_device_t *device;
578 device = cairo_surface_get_device(surface->cairo_surface);
582 if (!eglMakeCurrent(surface->display->dpy,
583 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
584 fprintf(stderr, "failed to make context current\n");
586 cairo_device_release(device);
590 egl_window_surface_destroy(struct toysurface *base)
592 struct egl_window_surface *surface = to_egl_window_surface(base);
593 struct display *d = surface->display;
595 cairo_surface_destroy(surface->cairo_surface);
596 eglDestroySurface(d->dpy, surface->egl_surface);
597 wl_egl_window_destroy(surface->egl_window);
598 surface->surface = NULL;
603 static struct toysurface *
604 egl_window_surface_create(struct display *display,
605 struct wl_surface *wl_surface,
607 struct rectangle *rectangle)
609 struct egl_window_surface *surface;
611 if (display->dpy == EGL_NO_DISPLAY)
614 surface = calloc(1, sizeof *surface);
618 surface->base.prepare = egl_window_surface_prepare;
619 surface->base.swap = egl_window_surface_swap;
620 surface->base.acquire = egl_window_surface_acquire;
621 surface->base.release = egl_window_surface_release;
622 surface->base.destroy = egl_window_surface_destroy;
624 surface->display = display;
625 surface->surface = wl_surface;
627 surface->egl_window = wl_egl_window_create(surface->surface,
631 surface->egl_surface = eglCreateWindowSurface(display->dpy,
632 display->argb_config,
636 surface->cairo_surface =
637 cairo_gl_surface_create_for_egl(display->argb_device,
638 surface->egl_surface,
642 return &surface->base;
647 static struct toysurface *
648 egl_window_surface_create(struct display *display,
649 struct wl_surface *wl_surface,
651 struct rectangle *rectangle)
658 struct shm_surface_data {
659 struct wl_buffer *buffer;
660 struct shm_pool *pool;
664 display_get_buffer_for_surface(struct display *display,
665 cairo_surface_t *surface)
667 struct shm_surface_data *data;
669 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
675 shm_pool_destroy(struct shm_pool *pool);
678 shm_surface_data_destroy(void *p)
680 struct shm_surface_data *data = p;
682 wl_buffer_destroy(data->buffer);
684 shm_pool_destroy(data->pool);
689 static struct wl_shm_pool *
690 make_shm_pool(struct display *display, int size, void **data)
692 struct wl_shm_pool *pool;
695 fd = os_create_anonymous_file(size);
697 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
702 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
703 if (*data == MAP_FAILED) {
704 fprintf(stderr, "mmap failed: %m\n");
709 pool = wl_shm_create_pool(display->shm, fd, size);
716 static struct shm_pool *
717 shm_pool_create(struct display *display, size_t size)
719 struct shm_pool *pool = malloc(sizeof *pool);
724 pool->pool = make_shm_pool(display, size, &pool->data);
737 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
739 if (pool->used + size > pool->size)
742 *offset = pool->used;
745 return (char *) pool->data + *offset;
748 /* destroy the pool. this does not unmap the memory though */
750 shm_pool_destroy(struct shm_pool *pool)
752 munmap(pool->data, pool->size);
753 wl_shm_pool_destroy(pool->pool);
757 /* Start allocating from the beginning of the pool again */
759 shm_pool_reset(struct shm_pool *pool)
765 data_length_for_shm_surface(struct rectangle *rect)
769 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
771 return stride * rect->height;
774 static cairo_surface_t *
775 display_create_shm_surface_from_pool(struct display *display,
776 struct rectangle *rectangle,
777 uint32_t flags, struct shm_pool *pool)
779 struct shm_surface_data *data;
781 cairo_surface_t *surface;
782 cairo_format_t cairo_format;
783 int stride, length, offset;
786 data = malloc(sizeof *data);
790 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
791 cairo_format = CAIRO_FORMAT_RGB16_565;
793 cairo_format = CAIRO_FORMAT_ARGB32;
795 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
796 length = stride * rectangle->height;
798 map = shm_pool_allocate(pool, length, &offset);
805 surface = cairo_image_surface_create_for_data (map,
811 cairo_surface_set_user_data(surface, &shm_surface_data_key,
812 data, shm_surface_data_destroy);
814 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
815 format = WL_SHM_FORMAT_RGB565;
817 if (flags & SURFACE_OPAQUE)
818 format = WL_SHM_FORMAT_XRGB8888;
820 format = WL_SHM_FORMAT_ARGB8888;
823 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
831 static cairo_surface_t *
832 display_create_shm_surface(struct display *display,
833 struct rectangle *rectangle, uint32_t flags,
834 struct shm_pool *alternate_pool,
835 struct shm_surface_data **data_ret)
837 struct shm_surface_data *data;
838 struct shm_pool *pool;
839 cairo_surface_t *surface;
841 if (alternate_pool) {
842 shm_pool_reset(alternate_pool);
843 surface = display_create_shm_surface_from_pool(display,
848 data = cairo_surface_get_user_data(surface,
849 &shm_surface_data_key);
854 pool = shm_pool_create(display,
855 data_length_for_shm_surface(rectangle));
860 display_create_shm_surface_from_pool(display, rectangle,
864 shm_pool_destroy(pool);
868 /* make sure we destroy the pool when the surface is destroyed */
869 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
880 check_size(struct rectangle *rect)
882 if (rect->width && rect->height)
885 fprintf(stderr, "tried to create surface of "
886 "width: %d, height: %d\n", rect->width, rect->height);
891 display_create_surface(struct display *display,
892 struct wl_surface *surface,
893 struct rectangle *rectangle,
896 if (check_size(rectangle) < 0)
899 assert(flags & SURFACE_SHM);
900 return display_create_shm_surface(display, rectangle, flags,
904 struct shm_surface_leaf {
905 cairo_surface_t *cairo_surface;
906 /* 'data' is automatically destroyed, when 'cairo_surface' is */
907 struct shm_surface_data *data;
909 struct shm_pool *resize_pool;
914 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
916 if (leaf->cairo_surface)
917 cairo_surface_destroy(leaf->cairo_surface);
918 /* leaf->data already destroyed via cairo private */
920 if (leaf->resize_pool)
921 shm_pool_destroy(leaf->resize_pool);
923 memset(leaf, 0, sizeof *leaf);
929 struct toysurface base;
930 struct display *display;
931 struct wl_surface *surface;
935 struct shm_surface_leaf leaf[MAX_LEAVES];
936 struct shm_surface_leaf *current;
939 static struct shm_surface *
940 to_shm_surface(struct toysurface *base)
942 return container_of(base, struct shm_surface, base);
946 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
949 struct shm_surface_leaf *leaf;
950 char bufs[MAX_LEAVES + 1];
953 for (i = 0; i < MAX_LEAVES; i++) {
954 leaf = &surface->leaf[i];
958 else if (leaf->cairo_surface)
964 bufs[MAX_LEAVES] = '\0';
965 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
970 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
972 struct shm_surface *surface = data;
973 struct shm_surface_leaf *leaf;
977 shm_surface_buffer_state_debug(surface, "buffer_release before");
979 for (i = 0; i < MAX_LEAVES; i++) {
980 leaf = &surface->leaf[i];
981 if (leaf->data && leaf->data->buffer == buffer) {
986 assert(i < MAX_LEAVES && "unknown buffer released");
988 /* Leave one free leaf with storage, release others */
990 for (i = 0; i < MAX_LEAVES; i++) {
991 leaf = &surface->leaf[i];
993 if (!leaf->cairo_surface || leaf->busy)
999 shm_surface_leaf_release(leaf);
1002 shm_surface_buffer_state_debug(surface, "buffer_release after");
1005 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1006 shm_surface_buffer_release
1009 static cairo_surface_t *
1010 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1011 int32_t width, int32_t height, uint32_t flags,
1012 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1014 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1015 struct shm_surface *surface = to_shm_surface(base);
1016 struct rectangle rect = { 0};
1017 struct shm_surface_leaf *leaf = NULL;
1023 /* pick a free buffer, preferrably one that already has storage */
1024 for (i = 0; i < MAX_LEAVES; i++) {
1025 if (surface->leaf[i].busy)
1028 if (!leaf || surface->leaf[i].cairo_surface)
1029 leaf = &surface->leaf[i];
1031 DBG_OBJ(surface->surface, "pick leaf %d\n",
1032 (int)(leaf - &surface->leaf[0]));
1035 fprintf(stderr, "%s: all buffers are held by the server.\n",
1041 if (!resize_hint && leaf->resize_pool) {
1042 cairo_surface_destroy(leaf->cairo_surface);
1043 leaf->cairo_surface = NULL;
1044 shm_pool_destroy(leaf->resize_pool);
1045 leaf->resize_pool = NULL;
1048 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1050 if (leaf->cairo_surface &&
1051 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1052 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1055 if (leaf->cairo_surface)
1056 cairo_surface_destroy(leaf->cairo_surface);
1058 #ifdef USE_RESIZE_POOL
1059 if (resize_hint && !leaf->resize_pool) {
1060 /* Create a big pool to allocate from, while continuously
1061 * resizing. Mmapping a new pool in the server
1062 * is relatively expensive, so reusing a pool performs
1063 * better, but may temporarily reserve unneeded memory.
1065 /* We should probably base this number on the output size. */
1066 leaf->resize_pool = shm_pool_create(surface->display,
1072 rect.height = height;
1074 leaf->cairo_surface =
1075 display_create_shm_surface(surface->display, &rect,
1079 if (!leaf->cairo_surface)
1082 wl_buffer_add_listener(leaf->data->buffer,
1083 &shm_surface_buffer_listener, surface);
1086 surface->current = leaf;
1088 return cairo_surface_reference(leaf->cairo_surface);
1092 shm_surface_swap(struct toysurface *base,
1093 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1094 struct rectangle *server_allocation)
1096 struct shm_surface *surface = to_shm_surface(base);
1097 struct shm_surface_leaf *leaf = surface->current;
1099 server_allocation->width =
1100 cairo_image_surface_get_width(leaf->cairo_surface);
1101 server_allocation->height =
1102 cairo_image_surface_get_height(leaf->cairo_surface);
1104 buffer_to_surface_size (buffer_transform, buffer_scale,
1105 &server_allocation->width,
1106 &server_allocation->height);
1108 wl_surface_attach(surface->surface, leaf->data->buffer,
1109 surface->dx, surface->dy);
1110 wl_surface_damage(surface->surface, 0, 0,
1111 server_allocation->width, server_allocation->height);
1112 wl_surface_commit(surface->surface);
1114 DBG_OBJ(surface->surface, "leaf %d busy\n",
1115 (int)(leaf - &surface->leaf[0]));
1118 surface->current = NULL;
1122 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1128 shm_surface_release(struct toysurface *base)
1133 shm_surface_destroy(struct toysurface *base)
1135 struct shm_surface *surface = to_shm_surface(base);
1138 for (i = 0; i < MAX_LEAVES; i++)
1139 shm_surface_leaf_release(&surface->leaf[i]);
1144 static struct toysurface *
1145 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1146 uint32_t flags, struct rectangle *rectangle)
1148 struct shm_surface *surface;
1149 DBG_OBJ(wl_surface, "\n");
1151 surface = xmalloc(sizeof *surface);
1152 memset(surface, 0, sizeof *surface);
1157 surface->base.prepare = shm_surface_prepare;
1158 surface->base.swap = shm_surface_swap;
1159 surface->base.acquire = shm_surface_acquire;
1160 surface->base.release = shm_surface_release;
1161 surface->base.destroy = shm_surface_destroy;
1163 surface->display = display;
1164 surface->surface = wl_surface;
1165 surface->flags = flags;
1167 return &surface->base;
1171 * The following correspondences between file names and cursors was copied
1172 * from: https://bugs.kde.org/attachment.cgi?id=67313
1175 static const char *bottom_left_corners[] = {
1176 "bottom_left_corner",
1181 static const char *bottom_right_corners[] = {
1182 "bottom_right_corner",
1187 static const char *bottom_sides[] = {
1193 static const char *grabbings[] = {
1196 "208530c400c041818281048008011002"
1199 static const char *left_ptrs[] = {
1206 static const char *left_sides[] = {
1212 static const char *right_sides[] = {
1218 static const char *top_left_corners[] = {
1224 static const char *top_right_corners[] = {
1230 static const char *top_sides[] = {
1236 static const char *xterms[] = {
1242 static const char *hand1s[] = {
1246 "e29285e634086352946a0e7090d73106"
1249 static const char *watches[] = {
1252 "0426c94ea35c87780ff01dc239897213"
1255 struct cursor_alternatives {
1260 static const struct cursor_alternatives cursors[] = {
1261 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1262 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1263 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1264 {grabbings, ARRAY_LENGTH(grabbings)},
1265 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1266 {left_sides, ARRAY_LENGTH(left_sides)},
1267 {right_sides, ARRAY_LENGTH(right_sides)},
1268 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1269 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1270 {top_sides, ARRAY_LENGTH(top_sides)},
1271 {xterms, ARRAY_LENGTH(xterms)},
1272 {hand1s, ARRAY_LENGTH(hand1s)},
1273 {watches, ARRAY_LENGTH(watches)},
1277 create_cursors(struct display *display)
1279 struct weston_config *config;
1280 struct weston_config_section *s;
1284 struct wl_cursor *cursor;
1286 config = weston_config_parse("weston.ini");
1287 s = weston_config_get_section(config, "shell", NULL, NULL);
1288 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1289 weston_config_section_get_int(s, "cursor-size", &size, 32);
1290 weston_config_destroy(config);
1292 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1293 if (!display->cursor_theme) {
1294 fprintf(stderr, "could not load theme '%s'\n", theme);
1299 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1308 fprintf(stderr, "could not load cursor '%s'\n",
1309 cursors[i].names[0]);
1311 display->cursors[i] = cursor;
1316 destroy_cursors(struct display *display)
1318 wl_cursor_theme_destroy(display->cursor_theme);
1319 free(display->cursors);
1322 struct wl_cursor_image *
1323 display_get_pointer_image(struct display *display, int pointer)
1325 struct wl_cursor *cursor = display->cursors[pointer];
1327 return cursor ? cursor->images[0] : NULL;
1331 surface_flush(struct surface *surface)
1333 if (!surface->cairo_surface)
1336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1350 surface->toysurface->swap(surface->toysurface,
1351 surface->buffer_transform, surface->buffer_scale,
1352 &surface->server_allocation);
1354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
1359 window_has_focus(struct window *window)
1361 return window->focused;
1365 window_close(struct window *window)
1367 if (window->close_handler)
1368 window->close_handler(window->user_data);
1370 display_exit(window->display);
1374 window_get_display(struct window *window)
1376 return window->display;
1380 surface_create_surface(struct surface *surface, uint32_t flags)
1382 struct display *display = surface->window->display;
1383 struct rectangle allocation = surface->allocation;
1385 if (!surface->toysurface && display->dpy &&
1386 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1387 surface->toysurface =
1388 egl_window_surface_create(display,
1394 if (!surface->toysurface)
1395 surface->toysurface = shm_surface_create(display,
1397 flags, &allocation);
1399 surface->cairo_surface = surface->toysurface->prepare(
1400 surface->toysurface, 0, 0,
1401 allocation.width, allocation.height, flags,
1402 surface->buffer_transform, surface->buffer_scale);
1406 window_create_main_surface(struct window *window)
1408 struct surface *surface = window->main_surface;
1411 if (window->resizing)
1412 flags |= SURFACE_HINT_RESIZE;
1414 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1415 flags |= SURFACE_HINT_RGB565;
1417 surface_create_surface(surface, flags);
1421 window_get_buffer_transform(struct window *window)
1423 return window->main_surface->buffer_transform;
1427 window_set_buffer_transform(struct window *window,
1428 enum wl_output_transform transform)
1430 window->main_surface->buffer_transform = transform;
1431 wl_surface_set_buffer_transform(window->main_surface->surface,
1436 window_set_buffer_scale(struct window *window,
1439 window->main_surface->buffer_scale = scale;
1440 wl_surface_set_buffer_scale(window->main_surface->surface,
1445 window_get_buffer_scale(struct window *window)
1447 return window->main_surface->buffer_scale;
1451 window_get_output_scale(struct window *window)
1453 struct window_output *window_output;
1454 struct window_output *window_output_tmp;
1457 wl_list_for_each_safe(window_output, window_output_tmp,
1458 &window->window_output_list, link) {
1459 if (window_output->output->scale > scale)
1460 scale = window_output->output->scale;
1466 static void window_frame_destroy(struct window_frame *frame);
1469 surface_destroy(struct surface *surface)
1471 if (surface->frame_cb)
1472 wl_callback_destroy(surface->frame_cb);
1474 if (surface->input_region)
1475 wl_region_destroy(surface->input_region);
1477 if (surface->opaque_region)
1478 wl_region_destroy(surface->opaque_region);
1480 if (surface->subsurface)
1481 wl_subsurface_destroy(surface->subsurface);
1483 wl_surface_destroy(surface->surface);
1485 if (surface->toysurface)
1486 surface->toysurface->destroy(surface->toysurface);
1488 wl_list_remove(&surface->link);
1493 window_destroy(struct window *window)
1495 struct display *display = window->display;
1496 struct input *input;
1497 struct window_output *window_output;
1498 struct window_output *window_output_tmp;
1500 wl_list_remove(&window->redraw_task.link);
1502 wl_list_for_each(input, &display->input_list, link) {
1503 if (input->touch_focus == window)
1504 input->touch_focus = NULL;
1505 if (input->pointer_focus == window)
1506 input->pointer_focus = NULL;
1507 if (input->keyboard_focus == window)
1508 input->keyboard_focus = NULL;
1509 if (input->focus_widget &&
1510 input->focus_widget->window == window)
1511 input->focus_widget = NULL;
1514 wl_list_for_each_safe(window_output, window_output_tmp,
1515 &window->window_output_list, link) {
1516 free (window_output);
1520 window_frame_destroy(window->frame);
1522 if (window->xdg_surface)
1523 xdg_surface_destroy(window->xdg_surface);
1524 if (window->xdg_popup)
1525 xdg_popup_destroy(window->xdg_popup);
1527 surface_destroy(window->main_surface);
1529 wl_list_remove(&window->link);
1531 free(window->title);
1535 static struct widget *
1536 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1538 struct widget *child, *target;
1540 wl_list_for_each(child, &widget->child_list, link) {
1541 target = widget_find_widget(child, x, y);
1546 if (widget->allocation.x <= x &&
1547 x < widget->allocation.x + widget->allocation.width &&
1548 widget->allocation.y <= y &&
1549 y < widget->allocation.y + widget->allocation.height) {
1556 static struct widget *
1557 window_find_widget(struct window *window, int32_t x, int32_t y)
1559 struct surface *surface;
1560 struct widget *widget;
1562 wl_list_for_each(surface, &window->subsurface_list, link) {
1563 widget = widget_find_widget(surface->widget, x, y);
1571 static struct widget *
1572 widget_create(struct window *window, struct surface *surface, void *data)
1574 struct widget *widget;
1576 widget = xzalloc(sizeof *widget);
1577 widget->window = window;
1578 widget->surface = surface;
1579 widget->user_data = data;
1580 widget->allocation = surface->allocation;
1581 wl_list_init(&widget->child_list);
1583 widget->tooltip = NULL;
1584 widget->tooltip_count = 0;
1585 widget->default_cursor = CURSOR_LEFT_PTR;
1586 widget->use_cairo = 1;
1592 window_add_widget(struct window *window, void *data)
1594 struct widget *widget;
1596 widget = widget_create(window, window->main_surface, data);
1597 wl_list_init(&widget->link);
1598 window->main_surface->widget = widget;
1604 widget_add_widget(struct widget *parent, void *data)
1606 struct widget *widget;
1608 widget = widget_create(parent->window, parent->surface, data);
1609 wl_list_insert(parent->child_list.prev, &widget->link);
1615 widget_destroy(struct widget *widget)
1617 struct display *display = widget->window->display;
1618 struct surface *surface = widget->surface;
1619 struct input *input;
1621 /* Destroy the sub-surface along with the root widget */
1622 if (surface->widget == widget && surface->subsurface)
1623 surface_destroy(widget->surface);
1625 if (widget->tooltip)
1626 widget_destroy_tooltip(widget);
1628 wl_list_for_each(input, &display->input_list, link) {
1629 if (input->focus_widget == widget)
1630 input->focus_widget = NULL;
1633 wl_list_remove(&widget->link);
1638 widget_set_default_cursor(struct widget *widget, int cursor)
1640 widget->default_cursor = cursor;
1644 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1646 *allocation = widget->allocation;
1650 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1652 widget->allocation.width = width;
1653 widget->allocation.height = height;
1657 widget_set_allocation(struct widget *widget,
1658 int32_t x, int32_t y, int32_t width, int32_t height)
1660 widget->allocation.x = x;
1661 widget->allocation.y = y;
1662 widget_set_size(widget, width, height);
1666 widget_set_transparent(struct widget *widget, int transparent)
1668 widget->opaque = !transparent;
1672 widget_get_user_data(struct widget *widget)
1674 return widget->user_data;
1677 static cairo_surface_t *
1678 widget_get_cairo_surface(struct widget *widget)
1680 struct surface *surface = widget->surface;
1681 struct window *window = widget->window;
1683 assert(widget->use_cairo);
1685 if (!surface->cairo_surface) {
1686 if (surface == window->main_surface)
1687 window_create_main_surface(window);
1689 surface_create_surface(surface, 0);
1692 return surface->cairo_surface;
1696 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1698 struct surface *surface = widget->surface;
1701 enum wl_output_transform transform;
1702 int surface_width, surface_height;
1703 int translate_x, translate_y;
1706 surface_width = surface->allocation.width;
1707 surface_height = surface->allocation.height;
1709 transform = surface->buffer_transform;
1710 scale = surface->buffer_scale;
1712 switch (transform) {
1713 case WL_OUTPUT_TRANSFORM_FLIPPED:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1717 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1720 cairo_matrix_init_identity(&m);
1724 switch (transform) {
1725 case WL_OUTPUT_TRANSFORM_NORMAL:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED:
1733 translate_x = surface_width;
1736 case WL_OUTPUT_TRANSFORM_90:
1738 translate_x = surface_height;
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1743 translate_x = surface_height;
1744 translate_y = surface_width;
1746 case WL_OUTPUT_TRANSFORM_180:
1748 translate_x = surface_width;
1749 translate_y = surface_height;
1751 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1754 translate_y = surface_height;
1756 case WL_OUTPUT_TRANSFORM_270:
1757 angle = M_PI + M_PI_2;
1759 translate_y = surface_width;
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1762 angle = M_PI + M_PI_2;
1768 cairo_scale(cr, scale, scale);
1769 cairo_translate(cr, translate_x, translate_y);
1770 cairo_rotate(cr, angle);
1771 cairo_transform(cr, &m);
1775 widget_cairo_create(struct widget *widget)
1777 struct surface *surface = widget->surface;
1778 cairo_surface_t *cairo_surface;
1781 cairo_surface = widget_get_cairo_surface(widget);
1782 cr = cairo_create(cairo_surface);
1784 widget_cairo_update_transform(widget, cr);
1786 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1792 widget_get_wl_surface(struct widget *widget)
1794 return widget->surface->surface;
1797 struct wl_subsurface *
1798 widget_get_wl_subsurface(struct widget *widget)
1800 return widget->surface->subsurface;
1804 widget_get_last_time(struct widget *widget)
1806 return widget->surface->last_time;
1810 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1812 struct wl_compositor *comp = widget->window->display->compositor;
1813 struct surface *surface = widget->surface;
1815 if (!surface->input_region)
1816 surface->input_region = wl_compositor_create_region(comp);
1819 wl_region_add(surface->input_region,
1820 rect->x, rect->y, rect->width, rect->height);
1825 widget_set_resize_handler(struct widget *widget,
1826 widget_resize_handler_t handler)
1828 widget->resize_handler = handler;
1832 widget_set_redraw_handler(struct widget *widget,
1833 widget_redraw_handler_t handler)
1835 widget->redraw_handler = handler;
1839 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1841 widget->enter_handler = handler;
1845 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1847 widget->leave_handler = handler;
1851 widget_set_motion_handler(struct widget *widget,
1852 widget_motion_handler_t handler)
1854 widget->motion_handler = handler;
1858 widget_set_button_handler(struct widget *widget,
1859 widget_button_handler_t handler)
1861 widget->button_handler = handler;
1865 widget_set_touch_up_handler(struct widget *widget,
1866 widget_touch_up_handler_t handler)
1868 widget->touch_up_handler = handler;
1872 widget_set_touch_down_handler(struct widget *widget,
1873 widget_touch_down_handler_t handler)
1875 widget->touch_down_handler = handler;
1879 widget_set_touch_motion_handler(struct widget *widget,
1880 widget_touch_motion_handler_t handler)
1882 widget->touch_motion_handler = handler;
1886 widget_set_touch_frame_handler(struct widget *widget,
1887 widget_touch_frame_handler_t handler)
1889 widget->touch_frame_handler = handler;
1893 widget_set_touch_cancel_handler(struct widget *widget,
1894 widget_touch_cancel_handler_t handler)
1896 widget->touch_cancel_handler = handler;
1900 widget_set_axis_handler(struct widget *widget,
1901 widget_axis_handler_t handler)
1903 widget->axis_handler = handler;
1907 window_schedule_redraw_task(struct window *window);
1910 widget_schedule_redraw(struct widget *widget)
1912 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1913 widget->surface->redraw_needed = 1;
1914 window_schedule_redraw_task(widget->window);
1918 widget_set_use_cairo(struct widget *widget,
1921 widget->use_cairo = use_cairo;
1925 window_get_surface(struct window *window)
1927 cairo_surface_t *cairo_surface;
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1931 return cairo_surface_reference(cairo_surface);
1935 window_get_wl_surface(struct window *window)
1937 return window->main_surface->surface;
1941 tooltip_redraw_handler(struct widget *widget, void *data)
1944 const int32_t r = 3;
1945 struct tooltip *tooltip = data;
1946 int32_t width, height;
1948 cr = widget_cairo_create(widget);
1949 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1954 width = widget->allocation.width;
1955 height = widget->allocation.height;
1956 rounded_rect(cr, 0, 0, width, height, r);
1958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1959 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1962 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1963 cairo_move_to(cr, 10, 16);
1964 cairo_show_text(cr, tooltip->entry);
1968 static cairo_text_extents_t
1969 get_text_extents(struct display *display, struct tooltip *tooltip)
1972 cairo_text_extents_t extents;
1974 /* Use the dummy_surface because tooltip's surface was not
1975 * created yet, and parent does not have a valid surface
1976 * outside repaint, either.
1978 cr = cairo_create(display->dummy_surface);
1979 cairo_text_extents(cr, tooltip->entry, &extents);
1986 window_create_tooltip(struct tooltip *tooltip)
1988 struct widget *parent = tooltip->parent;
1989 struct display *display = parent->window->display;
1990 const int offset_y = 27;
1991 const int margin = 3;
1992 cairo_text_extents_t extents;
1994 if (tooltip->widget)
1997 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1999 extents = get_text_extents(display, tooltip);
2000 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2001 widget_set_allocation(tooltip->widget,
2002 tooltip->x, tooltip->y + offset_y,
2003 extents.width + 20, 20 + margin * 2);
2009 widget_destroy_tooltip(struct widget *parent)
2011 struct tooltip *tooltip = parent->tooltip;
2013 parent->tooltip_count = 0;
2017 if (tooltip->widget) {
2018 widget_destroy(tooltip->widget);
2019 tooltip->widget = NULL;
2022 close(tooltip->tooltip_fd);
2023 free(tooltip->entry);
2025 parent->tooltip = NULL;
2029 tooltip_func(struct task *task, uint32_t events)
2031 struct tooltip *tooltip =
2032 container_of(task, struct tooltip, tooltip_task);
2035 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2037 window_create_tooltip(tooltip);
2040 #define TOOLTIP_TIMEOUT 500
2042 tooltip_timer_reset(struct tooltip *tooltip)
2044 struct itimerspec its;
2046 its.it_interval.tv_sec = 0;
2047 its.it_interval.tv_nsec = 0;
2048 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2049 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2050 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2051 fprintf(stderr, "could not set timerfd\n: %m");
2059 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2061 struct tooltip *tooltip = parent->tooltip;
2063 parent->tooltip_count++;
2067 tooltip_timer_reset(tooltip);
2071 /* the handler might be triggered too fast via input device motion, so
2072 * we need this check here to make sure tooltip is fully initialized */
2073 if (parent->tooltip_count > 1)
2076 tooltip = malloc(sizeof *tooltip);
2080 parent->tooltip = tooltip;
2081 tooltip->parent = parent;
2082 tooltip->widget = NULL;
2085 tooltip->entry = strdup(entry);
2086 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2087 if (tooltip->tooltip_fd < 0) {
2088 fprintf(stderr, "could not create timerfd\n: %m");
2092 tooltip->tooltip_task.run = tooltip_func;
2093 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2094 EPOLLIN, &tooltip->tooltip_task);
2095 tooltip_timer_reset(tooltip);
2101 workspace_manager_state(void *data,
2102 struct workspace_manager *workspace_manager,
2106 struct display *display = data;
2108 display->workspace = current;
2109 display->workspace_count = count;
2112 static const struct workspace_manager_listener workspace_manager_listener = {
2113 workspace_manager_state
2117 frame_resize_handler(struct widget *widget,
2118 int32_t width, int32_t height, void *data)
2120 struct window_frame *frame = data;
2121 struct widget *child = frame->child;
2122 struct rectangle interior;
2123 struct rectangle input;
2124 struct rectangle opaque;
2126 if (widget->window->fullscreen) {
2129 interior.width = width;
2130 interior.height = height;
2132 frame_resize(frame->frame, width, height);
2133 frame_interior(frame->frame, &interior.x, &interior.y,
2134 &interior.width, &interior.height);
2137 widget_set_allocation(child, interior.x, interior.y,
2138 interior.width, interior.height);
2140 if (child->resize_handler) {
2141 child->resize_handler(child, interior.width, interior.height,
2144 if (widget->window->fullscreen) {
2145 width = child->allocation.width;
2146 height = child->allocation.height;
2148 frame_resize_inside(frame->frame,
2149 child->allocation.width,
2150 child->allocation.height);
2151 width = frame_width(frame->frame);
2152 height = frame_height(frame->frame);
2156 widget_set_allocation(widget, 0, 0, width, height);
2158 widget->surface->input_region =
2159 wl_compositor_create_region(widget->window->display->compositor);
2160 if (!widget->window->fullscreen) {
2161 frame_input_rect(frame->frame, &input.x, &input.y,
2162 &input.width, &input.height);
2163 wl_region_add(widget->surface->input_region,
2164 input.x, input.y, input.width, input.height);
2166 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2169 widget_set_allocation(widget, 0, 0, width, height);
2171 if (child->opaque) {
2172 if (!widget->window->fullscreen) {
2173 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2174 &opaque.width, &opaque.height);
2176 wl_region_add(widget->surface->opaque_region,
2178 opaque.width, opaque.height);
2180 wl_region_add(widget->surface->opaque_region,
2181 0, 0, width, height);
2186 widget_schedule_redraw(widget);
2190 frame_redraw_handler(struct widget *widget, void *data)
2193 struct window_frame *frame = data;
2194 struct window *window = widget->window;
2196 if (window->fullscreen)
2199 cr = widget_cairo_create(widget);
2201 frame_repaint(frame->frame, cr);
2207 frame_get_pointer_image_for_location(struct window_frame *frame,
2208 enum theme_location location)
2210 struct window *window = frame->widget->window;
2213 return CURSOR_LEFT_PTR;
2216 case THEME_LOCATION_RESIZING_TOP:
2218 case THEME_LOCATION_RESIZING_BOTTOM:
2219 return CURSOR_BOTTOM;
2220 case THEME_LOCATION_RESIZING_LEFT:
2222 case THEME_LOCATION_RESIZING_RIGHT:
2223 return CURSOR_RIGHT;
2224 case THEME_LOCATION_RESIZING_TOP_LEFT:
2225 return CURSOR_TOP_LEFT;
2226 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2227 return CURSOR_TOP_RIGHT;
2228 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2229 return CURSOR_BOTTOM_LEFT;
2230 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2231 return CURSOR_BOTTOM_RIGHT;
2232 case THEME_LOCATION_EXTERIOR:
2233 case THEME_LOCATION_TITLEBAR:
2235 return CURSOR_LEFT_PTR;
2240 window_show_frame_menu(struct window *window,
2241 struct input *input, uint32_t time)
2245 if (window->xdg_surface) {
2246 input_get_position(input, &x, &y);
2247 xdg_surface_show_window_menu(window->xdg_surface,
2248 input_get_seat(input),
2249 window->display->serial,
2255 frame_enter_handler(struct widget *widget,
2256 struct input *input, float x, float y, void *data)
2258 struct window_frame *frame = data;
2259 enum theme_location location;
2261 location = frame_pointer_enter(frame->frame, input, x, y);
2262 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2263 widget_schedule_redraw(frame->widget);
2265 return frame_get_pointer_image_for_location(data, location);
2269 frame_motion_handler(struct widget *widget,
2270 struct input *input, uint32_t time,
2271 float x, float y, void *data)
2273 struct window_frame *frame = data;
2274 enum theme_location location;
2276 location = frame_pointer_motion(frame->frame, input, x, y);
2277 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2278 widget_schedule_redraw(frame->widget);
2280 return frame_get_pointer_image_for_location(data, location);
2284 frame_leave_handler(struct widget *widget,
2285 struct input *input, void *data)
2287 struct window_frame *frame = data;
2289 frame_pointer_leave(frame->frame, input);
2290 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2291 widget_schedule_redraw(frame->widget);
2295 frame_handle_status(struct window_frame *frame, struct input *input,
2296 uint32_t time, enum theme_location location)
2298 struct window *window = frame->widget->window;
2301 status = frame_status(frame->frame);
2302 if (status & FRAME_STATUS_REPAINT)
2303 widget_schedule_redraw(frame->widget);
2305 if (status & FRAME_STATUS_MINIMIZE) {
2306 window_set_minimized(window);
2307 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2310 if (status & FRAME_STATUS_MENU) {
2311 window_show_frame_menu(window, input, time);
2312 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2315 if (status & FRAME_STATUS_MAXIMIZE) {
2316 window_set_maximized(window, !window->maximized);
2317 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2320 if (status & FRAME_STATUS_CLOSE) {
2321 window_close(window);
2325 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2326 input_ungrab(input);
2327 xdg_surface_move(window->xdg_surface,
2328 input_get_seat(input),
2329 window->display->serial);
2331 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2334 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2335 input_ungrab(input);
2337 xdg_surface_resize(window->xdg_surface,
2338 input_get_seat(input),
2339 window->display->serial,
2342 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2346 #define DOUBLE_CLICK_PERIOD 250
2348 frame_button_handler(struct widget *widget,
2349 struct input *input, uint32_t time,
2350 uint32_t button, enum wl_pointer_button_state state,
2354 struct window_frame *frame = data;
2355 enum theme_location location;
2357 frame->double_click = 0;
2358 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2359 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2360 frame->double_click = 1;
2361 frame->did_double = 1;
2363 frame->did_double = 0;
2365 frame->last_time = time;
2366 } else if (frame->did_double == 1) {
2367 frame->double_click = 1;
2368 frame->did_double = 0;
2371 if (frame->double_click)
2372 location = frame_double_click(frame->frame, input,
2375 location = frame_pointer_button(frame->frame, input,
2378 frame_handle_status(frame, input, time, location);
2382 frame_touch_down_handler(struct widget *widget, struct input *input,
2383 uint32_t serial, uint32_t time, int32_t id,
2384 float x, float y, void *data)
2386 struct window_frame *frame = data;
2388 frame_touch_down(frame->frame, input, id, x, y);
2389 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2393 frame_touch_up_handler(struct widget *widget,
2394 struct input *input, uint32_t serial, uint32_t time,
2395 int32_t id, void *data)
2397 struct window_frame *frame = data;
2399 frame_touch_up(frame->frame, input, id);
2400 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2404 window_frame_create(struct window *window, void *data)
2406 struct window_frame *frame;
2409 if (window->custom) {
2410 buttons = FRAME_BUTTON_NONE;
2412 buttons = FRAME_BUTTON_ALL;
2415 frame = xzalloc(sizeof *frame);
2416 frame->frame = frame_create(window->display->theme, 0, 0,
2417 buttons, window->title);
2419 frame->widget = window_add_widget(window, frame);
2420 frame->child = widget_add_widget(frame->widget, data);
2422 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2423 widget_set_resize_handler(frame->widget, frame_resize_handler);
2424 widget_set_enter_handler(frame->widget, frame_enter_handler);
2425 widget_set_leave_handler(frame->widget, frame_leave_handler);
2426 widget_set_motion_handler(frame->widget, frame_motion_handler);
2427 widget_set_button_handler(frame->widget, frame_button_handler);
2428 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2429 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2431 window->frame = frame;
2433 return frame->child;
2437 window_frame_set_child_size(struct widget *widget, int child_width,
2440 struct display *display = widget->window->display;
2441 struct theme *t = display->theme;
2442 int decoration_width, decoration_height;
2444 int margin = widget->window->maximized ? 0 : t->margin;
2446 if (!widget->window->fullscreen) {
2447 decoration_width = (t->width + margin) * 2;
2448 decoration_height = t->width +
2449 t->titlebar_height + margin * 2;
2451 width = child_width + decoration_width;
2452 height = child_height + decoration_height;
2454 width = child_width;
2455 height = child_height;
2458 window_schedule_resize(widget->window, width, height);
2462 window_frame_destroy(struct window_frame *frame)
2464 frame_destroy(frame->frame);
2466 /* frame->child must be destroyed by the application */
2467 widget_destroy(frame->widget);
2472 input_set_focus_widget(struct input *input, struct widget *focus,
2475 struct widget *old, *widget;
2478 if (focus == input->focus_widget)
2481 old = input->focus_widget;
2485 widget = input->grab;
2486 if (widget->leave_handler)
2487 widget->leave_handler(old, input, widget->user_data);
2488 input->focus_widget = NULL;
2494 widget = input->grab;
2495 input->focus_widget = focus;
2496 if (widget->enter_handler)
2497 cursor = widget->enter_handler(focus, input, x, y,
2500 cursor = widget->default_cursor;
2502 input_set_pointer_image(input, cursor);
2507 touch_grab(struct input *input, int32_t touch_id)
2509 input->touch_grab = 1;
2510 input->touch_grab_id = touch_id;
2514 touch_ungrab(struct input *input)
2516 struct touch_point *tp, *tmp;
2518 input->touch_grab = 0;
2520 wl_list_for_each_safe(tp, tmp,
2521 &input->touch_point_list, link) {
2522 if (tp->id != input->touch_grab_id)
2524 wl_list_remove(&tp->link);
2532 input_grab(struct input *input, struct widget *widget, uint32_t button)
2534 input->grab = widget;
2535 input->grab_button = button;
2537 input_set_focus_widget(input, widget, input->sx, input->sy);
2541 input_ungrab(struct input *input)
2543 struct widget *widget;
2546 if (input->pointer_focus) {
2547 widget = window_find_widget(input->pointer_focus,
2548 input->sx, input->sy);
2549 input_set_focus_widget(input, widget, input->sx, input->sy);
2554 input_remove_pointer_focus(struct input *input)
2556 struct window *window = input->pointer_focus;
2561 input_set_focus_widget(input, NULL, 0, 0);
2563 input->pointer_focus = NULL;
2564 input->current_cursor = CURSOR_UNSET;
2568 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2569 uint32_t serial, struct wl_surface *surface,
2570 wl_fixed_t sx_w, wl_fixed_t sy_w)
2572 struct input *input = data;
2573 struct window *window;
2574 struct widget *widget;
2575 float sx = wl_fixed_to_double(sx_w);
2576 float sy = wl_fixed_to_double(sy_w);
2579 /* enter event for a window we've just destroyed */
2583 window = wl_surface_get_user_data(surface);
2584 if (surface != window->main_surface->surface) {
2585 DBG("Ignoring input event from subsurface %p\n", surface);
2589 input->display->serial = serial;
2590 input->pointer_enter_serial = serial;
2591 input->pointer_focus = window;
2596 widget = window_find_widget(window, sx, sy);
2597 input_set_focus_widget(input, widget, sx, sy);
2601 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2602 uint32_t serial, struct wl_surface *surface)
2604 struct input *input = data;
2606 input->display->serial = serial;
2607 input_remove_pointer_focus(input);
2611 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2612 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2614 struct input *input = data;
2615 struct window *window = input->pointer_focus;
2616 struct widget *widget;
2618 float sx = wl_fixed_to_double(sx_w);
2619 float sy = wl_fixed_to_double(sy_w);
2627 /* when making the window smaller - e.g. after a unmaximise we might
2628 * still have a pending motion event that the compositor has picked
2629 * based on the old surface dimensions
2631 if (sx > window->main_surface->allocation.width ||
2632 sy > window->main_surface->allocation.height)
2635 if (!(input->grab && input->grab_button)) {
2636 widget = window_find_widget(window, sx, sy);
2637 input_set_focus_widget(input, widget, sx, sy);
2641 widget = input->grab;
2643 widget = input->focus_widget;
2645 if (widget->motion_handler)
2646 cursor = widget->motion_handler(input->focus_widget,
2647 input, time, sx, sy,
2650 cursor = widget->default_cursor;
2652 cursor = CURSOR_LEFT_PTR;
2654 input_set_pointer_image(input, cursor);
2658 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2659 uint32_t time, uint32_t button, uint32_t state_w)
2661 struct input *input = data;
2662 struct widget *widget;
2663 enum wl_pointer_button_state state = state_w;
2665 input->display->serial = serial;
2666 if (input->focus_widget && input->grab == NULL &&
2667 state == WL_POINTER_BUTTON_STATE_PRESSED)
2668 input_grab(input, input->focus_widget, button);
2670 widget = input->grab;
2671 if (widget && widget->button_handler)
2672 (*widget->button_handler)(widget,
2675 input->grab->user_data);
2677 if (input->grab && input->grab_button == button &&
2678 state == WL_POINTER_BUTTON_STATE_RELEASED)
2679 input_ungrab(input);
2683 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2684 uint32_t time, uint32_t axis, wl_fixed_t value)
2686 struct input *input = data;
2687 struct widget *widget;
2689 widget = input->focus_widget;
2691 widget = input->grab;
2692 if (widget && widget->axis_handler)
2693 (*widget->axis_handler)(widget,
2699 static const struct wl_pointer_listener pointer_listener = {
2700 pointer_handle_enter,
2701 pointer_handle_leave,
2702 pointer_handle_motion,
2703 pointer_handle_button,
2704 pointer_handle_axis,
2708 input_remove_keyboard_focus(struct input *input)
2710 struct window *window = input->keyboard_focus;
2711 struct itimerspec its;
2713 its.it_interval.tv_sec = 0;
2714 its.it_interval.tv_nsec = 0;
2715 its.it_value.tv_sec = 0;
2716 its.it_value.tv_nsec = 0;
2717 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2722 if (window->keyboard_focus_handler)
2723 (*window->keyboard_focus_handler)(window, NULL,
2726 input->keyboard_focus = NULL;
2730 keyboard_repeat_func(struct task *task, uint32_t events)
2732 struct input *input =
2733 container_of(task, struct input, repeat_task);
2734 struct window *window = input->keyboard_focus;
2737 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2738 /* If we change the timer between the fd becoming
2739 * readable and getting here, there'll be nothing to
2740 * read and we get EAGAIN. */
2743 if (window && window->key_handler) {
2744 (*window->key_handler)(window, input, input->repeat_time,
2745 input->repeat_key, input->repeat_sym,
2746 WL_KEYBOARD_KEY_STATE_PRESSED,
2752 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2753 uint32_t format, int fd, uint32_t size)
2755 struct input *input = data;
2756 struct xkb_keymap *keymap;
2757 struct xkb_state *state;
2765 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2770 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2771 if (map_str == MAP_FAILED) {
2776 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2778 XKB_KEYMAP_FORMAT_TEXT_V1,
2780 munmap(map_str, size);
2784 fprintf(stderr, "failed to compile keymap\n");
2788 state = xkb_state_new(keymap);
2790 fprintf(stderr, "failed to create XKB state\n");
2791 xkb_keymap_unref(keymap);
2795 xkb_keymap_unref(input->xkb.keymap);
2796 xkb_state_unref(input->xkb.state);
2797 input->xkb.keymap = keymap;
2798 input->xkb.state = state;
2800 input->xkb.control_mask =
2801 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
2802 input->xkb.alt_mask =
2803 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
2804 input->xkb.shift_mask =
2805 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
2809 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2810 uint32_t serial, struct wl_surface *surface,
2811 struct wl_array *keys)
2813 struct input *input = data;
2814 struct window *window;
2816 input->display->serial = serial;
2817 input->keyboard_focus = wl_surface_get_user_data(surface);
2819 window = input->keyboard_focus;
2820 if (window->keyboard_focus_handler)
2821 (*window->keyboard_focus_handler)(window,
2822 input, window->user_data);
2826 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2827 uint32_t serial, struct wl_surface *surface)
2829 struct input *input = data;
2831 input->display->serial = serial;
2832 input_remove_keyboard_focus(input);
2836 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2837 uint32_t serial, uint32_t time, uint32_t key,
2840 struct input *input = data;
2841 struct window *window = input->keyboard_focus;
2842 uint32_t code, num_syms;
2843 enum wl_keyboard_key_state state = state_w;
2844 const xkb_keysym_t *syms;
2846 struct itimerspec its;
2848 input->display->serial = serial;
2850 if (!window || !input->xkb.state)
2853 /* We only use input grabs for pointer events for now, so just
2854 * ignore key presses if a grab is active. We expand the key
2855 * event delivery mechanism to route events to widgets to
2856 * properly handle key grabs. In the meantime, this prevents
2857 * key event devlivery while a grab is active. */
2858 if (input->grab && input->grab_button == 0)
2861 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
2863 sym = XKB_KEY_NoSymbol;
2868 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2869 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2870 window_set_maximized(window, !window->maximized);
2871 } else if (sym == XKB_KEY_F11 &&
2872 window->fullscreen_handler &&
2873 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2874 window->fullscreen_handler(window, window->user_data);
2875 } else if (sym == XKB_KEY_F4 &&
2876 input->modifiers == MOD_ALT_MASK &&
2877 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2878 window_close(window);
2879 } else if (window->key_handler) {
2880 (*window->key_handler)(window, input, time, key,
2881 sym, state, window->user_data);
2884 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2885 key == input->repeat_key) {
2886 its.it_interval.tv_sec = 0;
2887 its.it_interval.tv_nsec = 0;
2888 its.it_value.tv_sec = 0;
2889 its.it_value.tv_nsec = 0;
2890 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2891 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2892 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2893 input->repeat_sym = sym;
2894 input->repeat_key = key;
2895 input->repeat_time = time;
2896 its.it_interval.tv_sec = input->repeat_rate_sec;
2897 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2898 its.it_value.tv_sec = input->repeat_delay_sec;
2899 its.it_value.tv_nsec = input->repeat_delay_nsec;
2900 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2905 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2906 uint32_t serial, uint32_t mods_depressed,
2907 uint32_t mods_latched, uint32_t mods_locked,
2910 struct input *input = data;
2911 xkb_mod_mask_t mask;
2913 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2914 if (!input->xkb.keymap)
2917 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2918 mods_locked, 0, 0, group);
2919 mask = xkb_state_serialize_mods(input->xkb.state,
2920 XKB_STATE_MODS_DEPRESSED |
2921 XKB_STATE_MODS_LATCHED);
2922 input->modifiers = 0;
2923 if (mask & input->xkb.control_mask)
2924 input->modifiers |= MOD_CONTROL_MASK;
2925 if (mask & input->xkb.alt_mask)
2926 input->modifiers |= MOD_ALT_MASK;
2927 if (mask & input->xkb.shift_mask)
2928 input->modifiers |= MOD_SHIFT_MASK;
2932 set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2934 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
2935 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
2937 /* a rate of zero disables any repeating, regardless of the delay's
2943 input->repeat_rate_sec = 1;
2945 input->repeat_rate_nsec = 1000000000 / rate;
2947 input->repeat_delay_sec = delay / 1000;
2948 delay -= (input->repeat_delay_sec * 1000);
2949 input->repeat_delay_nsec = delay * 1000 * 1000;
2953 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2954 int32_t rate, int32_t delay)
2956 struct input *input = data;
2958 set_repeat_info(input, rate, delay);
2961 static const struct wl_keyboard_listener keyboard_listener = {
2962 keyboard_handle_keymap,
2963 keyboard_handle_enter,
2964 keyboard_handle_leave,
2965 keyboard_handle_key,
2966 keyboard_handle_modifiers,
2967 keyboard_handle_repeat_info
2972 touch_handle_down(void *data, struct wl_touch *wl_touch,
2973 uint32_t serial, uint32_t time, struct wl_surface *surface,
2974 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2976 struct input *input = data;
2977 struct widget *widget;
2978 float sx = wl_fixed_to_double(x_w);
2979 float sy = wl_fixed_to_double(y_w);
2981 input->display->serial = serial;
2982 input->touch_focus = wl_surface_get_user_data(surface);
2983 if (!input->touch_focus) {
2984 DBG("Failed to find to touch focus for surface %p\n", surface);
2988 if (surface != input->touch_focus->main_surface->surface) {
2989 DBG("Ignoring input event from subsurface %p\n", surface);
2990 input->touch_focus = NULL;
2995 widget = input->grab;
2997 widget = window_find_widget(input->touch_focus,
2998 wl_fixed_to_double(x_w),
2999 wl_fixed_to_double(y_w));
3001 struct touch_point *tp = xmalloc(sizeof *tp);
3004 tp->widget = widget;
3007 wl_list_insert(&input->touch_point_list, &tp->link);
3009 if (widget->touch_down_handler)
3010 (*widget->touch_down_handler)(widget, input,
3019 touch_handle_up(void *data, struct wl_touch *wl_touch,
3020 uint32_t serial, uint32_t time, int32_t id)
3022 struct input *input = data;
3023 struct touch_point *tp, *tmp;
3025 if (!input->touch_focus) {
3026 DBG("No touch focus found for touch up event!\n");
3030 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3034 if (tp->widget->touch_up_handler)
3035 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3037 tp->widget->user_data);
3039 wl_list_remove(&tp->link);
3047 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3048 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3050 struct input *input = data;
3051 struct touch_point *tp;
3052 float sx = wl_fixed_to_double(x_w);
3053 float sy = wl_fixed_to_double(y_w);
3055 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3057 if (!input->touch_focus) {
3058 DBG("No touch focus found for touch motion event!\n");
3062 wl_list_for_each(tp, &input->touch_point_list, link) {
3068 if (tp->widget->touch_motion_handler)
3069 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3071 tp->widget->user_data);
3077 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3079 struct input *input = data;
3080 struct touch_point *tp, *tmp;
3082 DBG("touch_handle_frame\n");
3084 if (!input->touch_focus) {
3085 DBG("No touch focus found for touch frame event!\n");
3089 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3090 if (tp->widget->touch_frame_handler)
3091 (*tp->widget->touch_frame_handler)(tp->widget, input,
3092 tp->widget->user_data);
3097 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3099 struct input *input = data;
3100 struct touch_point *tp, *tmp;
3102 DBG("touch_handle_cancel\n");
3104 if (!input->touch_focus) {
3105 DBG("No touch focus found for touch cancel event!\n");
3109 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3110 if (tp->widget->touch_cancel_handler)
3111 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3112 tp->widget->user_data);
3114 wl_list_remove(&tp->link);
3119 static const struct wl_touch_listener touch_listener = {
3122 touch_handle_motion,
3124 touch_handle_cancel,
3128 seat_handle_capabilities(void *data, struct wl_seat *seat,
3129 enum wl_seat_capability caps)
3131 struct input *input = data;
3133 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3134 input->pointer = wl_seat_get_pointer(seat);
3135 wl_pointer_set_user_data(input->pointer, input);
3136 wl_pointer_add_listener(input->pointer, &pointer_listener,
3138 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3139 wl_pointer_destroy(input->pointer);
3140 input->pointer = NULL;
3143 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3144 input->keyboard = wl_seat_get_keyboard(seat);
3145 wl_keyboard_set_user_data(input->keyboard, input);
3146 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3148 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3149 wl_keyboard_destroy(input->keyboard);
3150 input->keyboard = NULL;
3153 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3154 input->touch = wl_seat_get_touch(seat);
3155 wl_touch_set_user_data(input->touch, input);
3156 wl_touch_add_listener(input->touch, &touch_listener, input);
3157 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3158 wl_touch_destroy(input->touch);
3159 input->touch = NULL;
3164 seat_handle_name(void *data, struct wl_seat *seat,
3170 static const struct wl_seat_listener seat_listener = {
3171 seat_handle_capabilities,
3176 input_get_position(struct input *input, int32_t *x, int32_t *y)
3183 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3185 struct touch_point *tp;
3187 wl_list_for_each(tp, &input->touch_point_list, link) {
3200 input_get_display(struct input *input)
3202 return input->display;
3206 input_get_seat(struct input *input)
3212 input_get_modifiers(struct input *input)
3214 return input->modifiers;
3218 input_get_focus_widget(struct input *input)
3220 return input->focus_widget;
3224 struct wl_data_offer *offer;
3225 struct input *input;
3226 struct wl_array types;
3229 struct task io_task;
3237 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3239 struct data_offer *offer = data;
3242 p = wl_array_add(&offer->types, sizeof *p);
3246 static const struct wl_data_offer_listener data_offer_listener = {
3251 data_offer_destroy(struct data_offer *offer)
3256 if (offer->refcount == 0) {
3257 wl_data_offer_destroy(offer->offer);
3258 for (p = offer->types.data; *p; p++)
3260 wl_array_release(&offer->types);
3266 data_device_data_offer(void *data,
3267 struct wl_data_device *data_device,
3268 struct wl_data_offer *_offer)
3270 struct data_offer *offer;
3272 offer = xmalloc(sizeof *offer);
3274 wl_array_init(&offer->types);
3275 offer->refcount = 1;
3276 offer->input = data;
3277 offer->offer = _offer;
3278 wl_data_offer_add_listener(offer->offer,
3279 &data_offer_listener, offer);
3283 data_device_enter(void *data, struct wl_data_device *data_device,
3284 uint32_t serial, struct wl_surface *surface,
3285 wl_fixed_t x_w, wl_fixed_t y_w,
3286 struct wl_data_offer *offer)
3288 struct input *input = data;
3289 struct window *window;
3291 float x = wl_fixed_to_double(x_w);
3292 float y = wl_fixed_to_double(y_w);
3295 window = wl_surface_get_user_data(surface);
3296 input->drag_enter_serial = serial;
3297 input->drag_focus = window,
3301 if (!input->touch_grab)
3302 input->pointer_enter_serial = serial;
3305 input->drag_offer = wl_data_offer_get_user_data(offer);
3307 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3310 types_data = input->drag_offer->types.data;
3312 input->drag_offer = NULL;
3316 if (window->data_handler)
3317 window->data_handler(window, input, x, y, types_data,
3322 data_device_leave(void *data, struct wl_data_device *data_device)
3324 struct input *input = data;
3326 if (input->drag_offer) {
3327 data_offer_destroy(input->drag_offer);
3328 input->drag_offer = NULL;
3333 data_device_motion(void *data, struct wl_data_device *data_device,
3334 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3336 struct input *input = data;
3337 struct window *window = input->drag_focus;
3338 float x = wl_fixed_to_double(x_w);
3339 float y = wl_fixed_to_double(y_w);
3345 if (input->drag_offer)
3346 types_data = input->drag_offer->types.data;
3350 if (window->data_handler)
3351 window->data_handler(window, input, x, y, types_data,
3356 data_device_drop(void *data, struct wl_data_device *data_device)
3358 struct input *input = data;
3359 struct window *window = input->drag_focus;
3365 if (window->drop_handler)
3366 window->drop_handler(window, input,
3367 x, y, window->user_data);
3369 if (input->touch_grab)
3370 touch_ungrab(input);
3374 data_device_selection(void *data,
3375 struct wl_data_device *wl_data_device,
3376 struct wl_data_offer *offer)
3378 struct input *input = data;
3381 if (input->selection_offer)
3382 data_offer_destroy(input->selection_offer);
3385 input->selection_offer = wl_data_offer_get_user_data(offer);
3386 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3389 input->selection_offer = NULL;
3393 static const struct wl_data_device_listener data_device_listener = {
3394 data_device_data_offer,
3399 data_device_selection
3403 input_set_pointer_image_index(struct input *input, int index)
3405 struct wl_buffer *buffer;
3406 struct wl_cursor *cursor;
3407 struct wl_cursor_image *image;
3409 if (!input->pointer)
3412 cursor = input->display->cursors[input->current_cursor];
3416 if (index >= (int) cursor->image_count) {
3417 fprintf(stderr, "cursor index out of range\n");
3421 image = cursor->images[index];
3422 buffer = wl_cursor_image_get_buffer(image);
3426 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3427 wl_surface_damage(input->pointer_surface, 0, 0,
3428 image->width, image->height);
3429 wl_surface_commit(input->pointer_surface);
3430 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3431 input->pointer_surface,
3432 image->hotspot_x, image->hotspot_y);
3435 static const struct wl_callback_listener pointer_surface_listener;
3438 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3441 struct input *input = data;
3442 struct wl_cursor *cursor;
3446 assert(callback == input->cursor_frame_cb);
3447 wl_callback_destroy(callback);
3448 input->cursor_frame_cb = NULL;
3451 if (!input->pointer)
3454 if (input->current_cursor == CURSOR_BLANK) {
3455 wl_pointer_set_cursor(input->pointer,
3456 input->pointer_enter_serial,
3461 if (input->current_cursor == CURSOR_UNSET)
3463 cursor = input->display->cursors[input->current_cursor];
3467 /* FIXME We don't have the current time on the first call so we set
3468 * the animation start to the time of the first frame callback. */
3470 input->cursor_anim_start = 0;
3471 else if (input->cursor_anim_start == 0)
3472 input->cursor_anim_start = time;
3474 if (time == 0 || input->cursor_anim_start == 0)
3477 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3479 if (cursor->image_count > 1) {
3480 input->cursor_frame_cb =
3481 wl_surface_frame(input->pointer_surface);
3482 wl_callback_add_listener(input->cursor_frame_cb,
3483 &pointer_surface_listener, input);
3486 input_set_pointer_image_index(input, i);
3489 static const struct wl_callback_listener pointer_surface_listener = {
3490 pointer_surface_frame_callback
3494 input_set_pointer_image(struct input *input, int pointer)
3498 if (!input->pointer)
3501 if (input->pointer_enter_serial > input->cursor_serial)
3504 if (!force && pointer == input->current_cursor)
3507 input->current_cursor = pointer;
3508 input->cursor_serial = input->pointer_enter_serial;
3509 if (!input->cursor_frame_cb)
3510 pointer_surface_frame_callback(input, NULL, 0);
3512 /* The current frame callback may be stuck if, for instance,
3513 * the set cursor request was processed by the server after
3514 * this client lost the focus. In this case the cursor surface
3515 * might not be mapped and the frame callback wouldn't ever
3516 * complete. Send a set_cursor and attach to try to map the
3517 * cursor surface again so that the callback will finish */
3518 input_set_pointer_image_index(input, 0);
3522 struct wl_data_device *
3523 input_get_data_device(struct input *input)
3525 return input->data_device;
3529 input_set_selection(struct input *input,
3530 struct wl_data_source *source, uint32_t time)
3532 if (input->data_device)
3533 wl_data_device_set_selection(input->data_device, source, time);
3537 input_accept(struct input *input, const char *type)
3539 wl_data_offer_accept(input->drag_offer->offer,
3540 input->drag_enter_serial, type);
3544 offer_io_func(struct task *task, uint32_t events)
3546 struct data_offer *offer =
3547 container_of(task, struct data_offer, io_task);
3551 len = read(offer->fd, buffer, sizeof buffer);
3552 offer->func(buffer, len,
3553 offer->x, offer->y, offer->user_data);
3557 data_offer_destroy(offer);
3562 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3563 data_func_t func, void *user_data)
3567 if (pipe2(p, O_CLOEXEC) == -1)
3570 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3573 offer->io_task.run = offer_io_func;
3577 offer->user_data = user_data;
3579 display_watch_fd(offer->input->display,
3580 offer->fd, EPOLLIN, &offer->io_task);
3584 input_receive_drag_data(struct input *input, const char *mime_type,
3585 data_func_t func, void *data)
3587 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3588 input->drag_offer->x = input->drag_x;
3589 input->drag_offer->y = input->drag_y;
3593 input_receive_drag_data_to_fd(struct input *input,
3594 const char *mime_type, int fd)
3596 if (input->drag_offer)
3597 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3603 input_receive_selection_data(struct input *input, const char *mime_type,
3604 data_func_t func, void *data)
3608 if (input->selection_offer == NULL)
3611 for (p = input->selection_offer->types.data; *p; p++)
3612 if (strcmp(mime_type, *p) == 0)
3618 data_offer_receive_data(input->selection_offer,
3619 mime_type, func, data);
3624 input_receive_selection_data_to_fd(struct input *input,
3625 const char *mime_type, int fd)
3627 if (input->selection_offer)
3628 wl_data_offer_receive(input->selection_offer->offer,
3635 window_move(struct window *window, struct input *input, uint32_t serial)
3637 if (!window->xdg_surface)
3640 xdg_surface_move(window->xdg_surface, input->seat, serial);
3644 surface_set_synchronized(struct surface *surface)
3646 if (!surface->subsurface)
3649 if (surface->synchronized)
3652 wl_subsurface_set_sync(surface->subsurface);
3653 surface->synchronized = 1;
3657 surface_set_synchronized_default(struct surface *surface)
3659 if (!surface->subsurface)
3662 if (surface->synchronized == surface->synchronized_default)
3665 if (surface->synchronized_default)
3666 wl_subsurface_set_sync(surface->subsurface);
3668 wl_subsurface_set_desync(surface->subsurface);
3670 surface->synchronized = surface->synchronized_default;
3674 surface_resize(struct surface *surface)
3676 struct widget *widget = surface->widget;
3677 struct wl_compositor *compositor = widget->window->display->compositor;
3679 if (surface->input_region) {
3680 wl_region_destroy(surface->input_region);
3681 surface->input_region = NULL;
3684 if (surface->opaque_region)
3685 wl_region_destroy(surface->opaque_region);
3687 surface->opaque_region = wl_compositor_create_region(compositor);
3689 if (widget->resize_handler)
3690 widget->resize_handler(widget,
3691 widget->allocation.width,
3692 widget->allocation.height,
3695 if (surface->subsurface &&
3696 (surface->allocation.x != widget->allocation.x ||
3697 surface->allocation.y != widget->allocation.y)) {
3698 wl_subsurface_set_position(surface->subsurface,
3699 widget->allocation.x,
3700 widget->allocation.y);
3702 if (surface->allocation.width != widget->allocation.width ||
3703 surface->allocation.height != widget->allocation.height) {
3704 window_schedule_redraw(widget->window);
3706 surface->allocation = widget->allocation;
3709 wl_region_add(surface->opaque_region, 0, 0,
3710 widget->allocation.width,
3711 widget->allocation.height);
3715 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3718 * This hack should be removed, when EGL respects
3719 * eglSwapInterval(0).
3721 * If this window has sub-surfaces, especially a free-running
3722 * EGL-widget, we need to post the parent surface once with
3723 * all the old state to guarantee, that the EGL-widget will
3724 * receive its frame callback soon. Otherwise, a forced call
3725 * to eglSwapBuffers may end up blocking, waiting for a frame
3726 * event that will never come, because we will commit the parent
3727 * surface with all new state only after eglSwapBuffers returns.
3729 * This assumes, that:
3730 * 1. When the EGL widget's resize hook is called, it pauses.
3731 * 2. When the EGL widget's redraw hook is called, it forces a
3732 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3733 * In a single threaded application condition 1 is a no-op.
3735 * XXX: This should actually be after the surface_resize() calls,
3736 * but cannot, because then it would commit the incomplete state
3737 * accumulated from the widget resize hooks.
3739 if (window->subsurface_list.next != &window->main_surface->link ||
3740 window->subsurface_list.prev != &window->main_surface->link)
3741 wl_surface_commit(window->main_surface->surface);
3745 window_do_resize(struct window *window)
3747 struct surface *surface;
3749 widget_set_allocation(window->main_surface->widget,
3750 window->pending_allocation.x,
3751 window->pending_allocation.y,
3752 window->pending_allocation.width,
3753 window->pending_allocation.height);
3755 surface_resize(window->main_surface);
3757 /* The main surface is in the list, too. Main surface's
3758 * resize_handler is responsible for calling widget_set_allocation()
3759 * on all sub-surface root widgets, so they will be resized
3762 wl_list_for_each(surface, &window->subsurface_list, link) {
3763 if (surface == window->main_surface)
3766 surface_set_synchronized(surface);
3767 surface_resize(surface);
3770 if (!window->fullscreen && !window->maximized)
3771 window->saved_allocation = window->pending_allocation;
3775 idle_resize(struct window *window)
3777 window->resize_needed = 0;
3778 window->redraw_needed = 1;
3780 DBG("from %dx%d to %dx%d\n",
3781 window->main_surface->server_allocation.width,
3782 window->main_surface->server_allocation.height,
3783 window->pending_allocation.width,
3784 window->pending_allocation.height);
3786 hack_prevent_EGL_sub_surface_deadlock(window);
3788 window_do_resize(window);
3792 undo_resize(struct window *window)
3794 window->pending_allocation.width =
3795 window->main_surface->server_allocation.width;
3796 window->pending_allocation.height =
3797 window->main_surface->server_allocation.height;
3799 DBG("back to %dx%d\n",
3800 window->main_surface->server_allocation.width,
3801 window->main_surface->server_allocation.height);
3803 window_do_resize(window);
3805 if (window->pending_allocation.width == 0 &&
3806 window->pending_allocation.height == 0) {
3807 fprintf(stderr, "Error: Could not draw a surface, "
3808 "most likely due to insufficient disk space in "
3809 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3815 window_schedule_resize(struct window *window, int width, int height)
3817 /* We should probably get these numbers from the theme. */
3818 const int min_width = 200, min_height = 200;
3820 window->pending_allocation.x = 0;
3821 window->pending_allocation.y = 0;
3822 window->pending_allocation.width = width;
3823 window->pending_allocation.height = height;
3825 if (window->min_allocation.width == 0) {
3826 if (width < min_width && window->frame)
3827 window->min_allocation.width = min_width;
3829 window->min_allocation.width = width;
3830 if (height < min_height && window->frame)
3831 window->min_allocation.height = min_height;
3833 window->min_allocation.height = height;
3836 if (window->pending_allocation.width < window->min_allocation.width)
3837 window->pending_allocation.width = window->min_allocation.width;
3838 if (window->pending_allocation.height < window->min_allocation.height)
3839 window->pending_allocation.height = window->min_allocation.height;
3841 window->resize_needed = 1;
3842 window_schedule_redraw(window);
3846 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3848 window_schedule_resize(widget->window, width, height);
3852 window_get_shadow_margin(struct window *window)
3854 if (window->frame && !window->fullscreen)
3855 return frame_get_shadow_margin(window->frame->frame);
3861 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3862 int32_t width, int32_t height,
3863 struct wl_array *states, uint32_t serial)
3865 struct window *window = data;
3868 window->maximized = 0;
3869 window->fullscreen = 0;
3870 window->resizing = 0;
3871 window->focused = 0;
3873 wl_array_for_each(p, states) {
3874 uint32_t state = *p;
3876 case XDG_SURFACE_STATE_MAXIMIZED:
3877 window->maximized = 1;
3879 case XDG_SURFACE_STATE_FULLSCREEN:
3880 window->fullscreen = 1;
3882 case XDG_SURFACE_STATE_RESIZING:
3883 window->resizing = 1;
3885 case XDG_SURFACE_STATE_ACTIVATED:
3886 window->focused = 1;
3894 if (window->frame) {
3895 if (window->maximized) {
3896 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3898 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3901 if (window->focused) {
3902 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3904 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3908 if (width > 0 && height > 0) {
3909 /* The width / height params are for window geometry,
3910 * but window_schedule_resize takes allocation. Add
3911 * on the shadow margin to get the difference. */
3912 int margin = window_get_shadow_margin(window);
3914 window_schedule_resize(window,
3916 height + margin * 2);
3918 window_schedule_resize(window,
3919 window->saved_allocation.width,
3920 window->saved_allocation.height);
3923 xdg_surface_ack_configure(window->xdg_surface, serial);
3925 if (window->state_changed_handler)
3926 window->state_changed_handler(window, window->user_data);
3930 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3932 struct window *window = data;
3933 window_close(window);
3936 static const struct xdg_surface_listener xdg_surface_listener = {
3937 handle_surface_configure,
3938 handle_surface_delete,
3942 window_sync_parent(struct window *window)
3944 struct wl_surface *parent_surface;
3946 if (!window->xdg_surface)
3950 parent_surface = window->parent->main_surface->surface;
3952 parent_surface = NULL;
3954 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3958 window_get_geometry(struct window *window, struct rectangle *geometry)
3960 if (window->frame && !window->fullscreen)
3961 frame_input_rect(window->frame->frame,
3967 window_get_allocation(window, geometry);
3971 window_sync_geometry(struct window *window)
3973 struct rectangle geometry;
3975 if (!window->xdg_surface)
3978 window_get_geometry(window, &geometry);
3980 xdg_surface_set_window_geometry(window->xdg_surface,
3988 window_flush(struct window *window)
3990 struct surface *surface;
3992 if (!window->custom) {
3993 if (window->xdg_surface) {
3994 window_sync_parent(window);
3995 window_sync_geometry(window);
3999 wl_list_for_each(surface, &window->subsurface_list, link) {
4000 if (surface == window->main_surface)
4003 surface_flush(surface);
4006 surface_flush(window->main_surface);
4010 menu_destroy(struct menu *menu)
4012 widget_destroy(menu->widget);
4013 window_destroy(menu->window);
4014 frame_destroy(menu->frame);
4019 window_get_allocation(struct window *window,
4020 struct rectangle *allocation)
4022 *allocation = window->main_surface->allocation;
4026 widget_redraw(struct widget *widget)
4028 struct widget *child;
4030 if (widget->redraw_handler)
4031 widget->redraw_handler(widget, widget->user_data);
4032 wl_list_for_each(child, &widget->child_list, link)
4033 widget_redraw(child);
4037 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4039 struct surface *surface = data;
4041 assert(callback == surface->frame_cb);
4042 DBG_OBJ(callback, "done\n");
4043 wl_callback_destroy(callback);
4044 surface->frame_cb = NULL;
4046 surface->last_time = time;
4048 if (surface->redraw_needed || surface->window->redraw_needed) {
4049 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4050 window_schedule_redraw_task(surface->window);
4054 static const struct wl_callback_listener listener = {
4059 surface_redraw(struct surface *surface)
4061 DBG_OBJ(surface->surface, "begin\n");
4063 if (!surface->window->redraw_needed && !surface->redraw_needed)
4066 /* Whole-window redraw forces a redraw even if the previous has
4067 * not yet hit the screen.
4069 if (surface->frame_cb) {
4070 if (!surface->window->redraw_needed)
4073 DBG_OBJ(surface->frame_cb, "cancelled\n");
4074 wl_callback_destroy(surface->frame_cb);
4077 if (surface->widget->use_cairo &&
4078 !widget_get_cairo_surface(surface->widget)) {
4079 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4083 surface->frame_cb = wl_surface_frame(surface->surface);
4084 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4085 DBG_OBJ(surface->frame_cb, "new\n");
4087 surface->redraw_needed = 0;
4088 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4089 widget_redraw(surface->widget);
4090 DBG_OBJ(surface->surface, "done\n");
4095 idle_redraw(struct task *task, uint32_t events)
4097 struct window *window = container_of(task, struct window, redraw_task);
4098 struct surface *surface;
4102 DBG(" --------- \n");
4104 wl_list_init(&window->redraw_task.link);
4105 window->redraw_task_scheduled = 0;
4107 if (window->resize_needed) {
4108 /* throttle resizing to the main surface display */
4109 if (window->main_surface->frame_cb) {
4110 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4114 idle_resize(window);
4118 if (surface_redraw(window->main_surface) < 0) {
4120 * Only main_surface failure will cause us to undo the resize.
4121 * If sub-surfaces fail, they will just be broken with old
4126 wl_list_for_each(surface, &window->subsurface_list, link) {
4127 if (surface == window->main_surface)
4130 surface_redraw(surface);
4134 window->redraw_needed = 0;
4135 window_flush(window);
4137 wl_list_for_each(surface, &window->subsurface_list, link)
4138 surface_set_synchronized_default(surface);
4140 if (resized && failed) {
4141 /* Restore widget tree to correspond to what is on screen. */
4142 undo_resize(window);
4147 window_schedule_redraw_task(struct window *window)
4149 if (!window->redraw_task_scheduled) {
4150 window->redraw_task.run = idle_redraw;
4151 display_defer(window->display, &window->redraw_task);
4152 window->redraw_task_scheduled = 1;
4157 window_schedule_redraw(struct window *window)
4159 struct surface *surface;
4161 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4163 wl_list_for_each(surface, &window->subsurface_list, link)
4164 surface->redraw_needed = 1;
4166 window_schedule_redraw_task(window);
4170 window_is_fullscreen(struct window *window)
4172 return window->fullscreen;
4176 window_set_fullscreen(struct window *window, int fullscreen)
4178 if (!window->xdg_surface)
4181 if (window->fullscreen == fullscreen)
4185 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4187 xdg_surface_unset_fullscreen(window->xdg_surface);
4191 window_is_maximized(struct window *window)
4193 return window->maximized;
4197 window_set_maximized(struct window *window, int maximized)
4199 if (!window->xdg_surface)
4202 if (window->maximized == maximized)
4206 xdg_surface_set_maximized(window->xdg_surface);
4208 xdg_surface_unset_maximized(window->xdg_surface);
4212 window_is_resizing(struct window *window)
4214 return window->resizing;
4218 window_set_minimized(struct window *window)
4220 if (!window->xdg_surface)
4223 xdg_surface_set_minimized(window->xdg_surface);
4227 window_set_user_data(struct window *window, void *data)
4229 window->user_data = data;
4233 window_get_user_data(struct window *window)
4235 return window->user_data;
4239 window_set_key_handler(struct window *window,
4240 window_key_handler_t handler)
4242 window->key_handler = handler;
4246 window_set_keyboard_focus_handler(struct window *window,
4247 window_keyboard_focus_handler_t handler)
4249 window->keyboard_focus_handler = handler;
4253 window_set_data_handler(struct window *window, window_data_handler_t handler)
4255 window->data_handler = handler;
4259 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4261 window->drop_handler = handler;
4265 window_set_close_handler(struct window *window,
4266 window_close_handler_t handler)
4268 window->close_handler = handler;
4272 window_set_fullscreen_handler(struct window *window,
4273 window_fullscreen_handler_t handler)
4275 window->fullscreen_handler = handler;
4279 window_set_output_handler(struct window *window,
4280 window_output_handler_t handler)
4282 window->output_handler = handler;
4286 window_set_state_changed_handler(struct window *window,
4287 window_state_changed_handler_t handler)
4289 window->state_changed_handler = handler;
4293 window_set_title(struct window *window, const char *title)
4295 free(window->title);
4296 window->title = strdup(title);
4297 if (window->frame) {
4298 frame_set_title(window->frame->frame, title);
4299 widget_schedule_redraw(window->frame->widget);
4301 if (window->xdg_surface)
4302 xdg_surface_set_title(window->xdg_surface, title);
4306 window_get_title(struct window *window)
4308 return window->title;
4312 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4314 struct text_cursor_position *text_cursor_position =
4315 window->display->text_cursor_position;
4317 if (!text_cursor_position)
4320 text_cursor_position_notify(text_cursor_position,
4321 window->main_surface->surface,
4322 wl_fixed_from_int(x),
4323 wl_fixed_from_int(y));
4327 window_damage(struct window *window, int32_t x, int32_t y,
4328 int32_t width, int32_t height)
4330 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4334 surface_enter(void *data,
4335 struct wl_surface *wl_surface, struct wl_output *wl_output)
4337 struct window *window = data;
4338 struct output *output;
4339 struct output *output_found = NULL;
4340 struct window_output *window_output;
4342 wl_list_for_each(output, &window->display->output_list, link) {
4343 if (output->output == wl_output) {
4344 output_found = output;
4352 window_output = xmalloc(sizeof *window_output);
4353 window_output->output = output_found;
4355 wl_list_insert (&window->window_output_list, &window_output->link);
4357 if (window->output_handler)
4358 window->output_handler(window, output_found, 1,
4363 surface_leave(void *data,
4364 struct wl_surface *wl_surface, struct wl_output *output)
4366 struct window *window = data;
4367 struct window_output *window_output;
4368 struct window_output *window_output_found = NULL;
4370 wl_list_for_each(window_output, &window->window_output_list, link) {
4371 if (window_output->output->output == output) {
4372 window_output_found = window_output;
4377 if (window_output_found) {
4378 wl_list_remove(&window_output_found->link);
4380 if (window->output_handler)
4381 window->output_handler(window, window_output->output,
4382 0, window->user_data);
4384 free(window_output_found);
4388 static const struct wl_surface_listener surface_listener = {
4393 static struct surface *
4394 surface_create(struct window *window)
4396 struct display *display = window->display;
4397 struct surface *surface;
4399 surface = xmalloc(sizeof *surface);
4400 memset(surface, 0, sizeof *surface);
4404 surface->window = window;
4405 surface->surface = wl_compositor_create_surface(display->compositor);
4406 surface->buffer_scale = 1;
4407 wl_surface_add_listener(surface->surface, &surface_listener, window);
4409 wl_list_insert(&window->subsurface_list, &surface->link);
4414 static enum window_buffer_type
4415 get_preferred_buffer_type(struct display *display)
4417 #ifdef HAVE_CAIRO_EGL
4418 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4419 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4422 return WINDOW_BUFFER_TYPE_SHM;
4425 static struct window *
4426 window_create_internal(struct display *display, int custom)
4428 struct window *window;
4429 struct surface *surface;
4431 window = xzalloc(sizeof *window);
4432 wl_list_init(&window->subsurface_list);
4433 window->display = display;
4435 surface = surface_create(window);
4436 window->main_surface = surface;
4438 assert(custom || display->xdg_shell);
4440 window->custom = custom;
4441 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4443 surface->buffer_type = get_preferred_buffer_type(display);
4445 wl_surface_set_user_data(surface->surface, window);
4446 wl_list_insert(display->window_list.prev, &window->link);
4447 wl_list_init(&window->redraw_task.link);
4449 wl_list_init (&window->window_output_list);
4455 window_create(struct display *display)
4457 struct window *window;
4459 window = window_create_internal(display, 0);
4461 window->xdg_surface =
4462 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4463 window->main_surface->surface);
4464 fail_on_null(window->xdg_surface);
4466 xdg_surface_set_user_data(window->xdg_surface, window);
4467 xdg_surface_add_listener(window->xdg_surface,
4468 &xdg_surface_listener, window);
4474 window_create_custom(struct display *display)
4476 return window_create_internal(display, 1);
4480 window_set_parent(struct window *window,
4481 struct window *parent_window)
4483 window->parent = parent_window;
4484 window_sync_parent(window);
4488 window_get_parent(struct window *window)
4490 return window->parent;
4494 menu_set_item(struct menu *menu, int sy)
4496 int32_t x, y, width, height;
4499 frame_interior(menu->frame, &x, &y, &width, &height);
4500 next = (sy - y) / 20;
4501 if (menu->current != next) {
4502 menu->current = next;
4503 widget_schedule_redraw(menu->widget);
4508 menu_motion_handler(struct widget *widget,
4509 struct input *input, uint32_t time,
4510 float x, float y, void *data)
4512 struct menu *menu = data;
4514 if (widget == menu->widget)
4515 menu_set_item(data, y);
4517 return CURSOR_LEFT_PTR;
4521 menu_enter_handler(struct widget *widget,
4522 struct input *input, float x, float y, void *data)
4524 struct menu *menu = data;
4526 if (widget == menu->widget)
4527 menu_set_item(data, y);
4529 return CURSOR_LEFT_PTR;
4533 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4535 struct menu *menu = data;
4537 if (widget == menu->widget)
4538 menu_set_item(data, -200);
4542 menu_button_handler(struct widget *widget,
4543 struct input *input, uint32_t time,
4544 uint32_t button, enum wl_pointer_button_state state,
4548 struct menu *menu = data;
4550 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4551 (menu->release_count > 0 || time - menu->time > 500)) {
4552 /* Either relase after press-drag-release or
4553 * click-motion-click. */
4554 menu->func(menu->user_data, input, menu->current);
4555 input_ungrab(input);
4557 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4558 menu->release_count++;
4563 menu_touch_up_handler(struct widget *widget,
4564 struct input *input,
4570 struct menu *menu = data;
4572 input_ungrab(input);
4577 menu_redraw_handler(struct widget *widget, void *data)
4580 struct menu *menu = data;
4581 int32_t x, y, width, height, i;
4583 cr = widget_cairo_create(widget);
4585 frame_repaint(menu->frame, cr);
4586 frame_interior(menu->frame, &x, &y, &width, &height);
4588 theme_set_background_source(menu->window->display->theme,
4589 cr, THEME_FRAME_ACTIVE);
4590 cairo_rectangle(cr, x, y, width, height);
4593 cairo_select_font_face(cr, "sans",
4594 CAIRO_FONT_SLANT_NORMAL,
4595 CAIRO_FONT_WEIGHT_NORMAL);
4596 cairo_set_font_size(cr, 12);
4598 for (i = 0; i < menu->count; i++) {
4599 if (i == menu->current) {
4600 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4601 cairo_rectangle(cr, x, y + i * 20, width, 20);
4603 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4604 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4605 cairo_show_text(cr, menu->entries[i]);
4607 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4608 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4609 cairo_show_text(cr, menu->entries[i]);
4617 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4619 struct window *window = data;
4620 struct menu *menu = window->main_surface->widget->user_data;
4622 input_ungrab(menu->input);
4626 static const struct xdg_popup_listener xdg_popup_listener = {
4627 handle_popup_popup_done,
4630 static struct menu *
4631 create_menu(struct display *display,
4632 struct input *input, uint32_t time,
4633 menu_func_t func, const char **entries, int count,
4636 struct window *window;
4639 menu = malloc(sizeof *menu);
4643 window = window_create_internal(display, 0);
4649 menu->window = window;
4650 menu->user_data = user_data;
4651 menu->widget = window_add_widget(menu->window, menu);
4652 menu->frame = frame_create(window->display->theme, 0, 0,
4653 FRAME_BUTTON_NONE, NULL);
4654 fail_on_null(menu->frame);
4655 menu->entries = entries;
4656 menu->count = count;
4657 menu->release_count = 0;
4661 menu->input = input;
4663 input_ungrab(input);
4665 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4666 widget_set_enter_handler(menu->widget, menu_enter_handler);
4667 widget_set_leave_handler(menu->widget, menu_leave_handler);
4668 widget_set_motion_handler(menu->widget, menu_motion_handler);
4669 widget_set_button_handler(menu->widget, menu_button_handler);
4670 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4672 input_grab(input, menu->widget, 0);
4673 frame_resize_inside(menu->frame, 200, count * 20);
4674 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4675 window_schedule_resize(window, frame_width(menu->frame),
4676 frame_height(menu->frame));
4682 window_create_menu(struct display *display,
4683 struct input *input, uint32_t time,
4684 menu_func_t func, const char **entries, int count,
4688 menu = create_menu(display, input, time, func, entries, count, user_data);
4693 return menu->window;
4697 window_show_menu(struct display *display,
4698 struct input *input, uint32_t time, struct window *parent,
4699 int32_t x, int32_t y,
4700 menu_func_t func, const char **entries, int count)
4703 struct window *window;
4706 menu = create_menu(display, input, time, func, entries, count, parent);
4711 window = menu->window;
4713 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4714 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4719 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4721 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4722 window->main_surface->surface,
4723 parent->main_surface->surface,
4725 display_get_serial(window->display),
4729 fail_on_null(window->xdg_popup);
4731 xdg_popup_set_user_data(window->xdg_popup, window);
4732 xdg_popup_add_listener(window->xdg_popup,
4733 &xdg_popup_listener, window);
4737 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4739 window->main_surface->buffer_type = type;
4742 enum window_buffer_type
4743 window_get_buffer_type(struct window *window)
4745 return window->main_surface->buffer_type;
4749 window_set_preferred_format(struct window *window,
4750 enum preferred_format format)
4752 window->preferred_format = format;
4756 window_add_subsurface(struct window *window, void *data,
4757 enum subsurface_mode default_mode)
4759 struct widget *widget;
4760 struct surface *surface;
4761 struct wl_surface *parent;
4762 struct wl_subcompositor *subcompo = window->display->subcompositor;
4764 surface = surface_create(window);
4765 surface->buffer_type = window_get_buffer_type(window);
4766 widget = widget_create(window, surface, data);
4767 wl_list_init(&widget->link);
4768 surface->widget = widget;
4770 parent = window->main_surface->surface;
4771 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4774 surface->synchronized = 1;
4776 switch (default_mode) {
4777 case SUBSURFACE_SYNCHRONIZED:
4778 surface->synchronized_default = 1;
4780 case SUBSURFACE_DESYNCHRONIZED:
4781 surface->synchronized_default = 0;
4784 assert(!"bad enum subsurface_mode");
4787 window->resize_needed = 1;
4788 window_schedule_redraw(window);
4794 display_handle_geometry(void *data,
4795 struct wl_output *wl_output,
4798 int physical_height,
4804 struct output *output = data;
4806 output->allocation.x = x;
4807 output->allocation.y = y;
4808 output->transform = transform;
4812 output->make = strdup(make);
4815 free(output->model);
4816 output->model = strdup(model);
4820 display_handle_done(void *data,
4821 struct wl_output *wl_output)
4826 display_handle_scale(void *data,
4827 struct wl_output *wl_output,
4830 struct output *output = data;
4832 output->scale = scale;
4836 display_handle_mode(void *data,
4837 struct wl_output *wl_output,
4843 struct output *output = data;
4844 struct display *display = output->display;
4846 if (flags & WL_OUTPUT_MODE_CURRENT) {
4847 output->allocation.width = width;
4848 output->allocation.height = height;
4849 if (display->output_configure_handler)
4850 (*display->output_configure_handler)(
4851 output, display->user_data);
4855 static const struct wl_output_listener output_listener = {
4856 display_handle_geometry,
4857 display_handle_mode,
4858 display_handle_done,
4859 display_handle_scale
4863 display_add_output(struct display *d, uint32_t id)
4865 struct output *output;
4867 output = xzalloc(sizeof *output);
4868 output->display = d;
4871 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4872 output->server_output_id = id;
4873 wl_list_insert(d->output_list.prev, &output->link);
4875 wl_output_add_listener(output->output, &output_listener, output);
4879 output_destroy(struct output *output)
4881 if (output->destroy_handler)
4882 (*output->destroy_handler)(output, output->user_data);
4884 wl_output_destroy(output->output);
4885 wl_list_remove(&output->link);
4890 display_destroy_output(struct display *d, uint32_t id)
4892 struct output *output;
4894 wl_list_for_each(output, &d->output_list, link) {
4895 if (output->server_output_id == id) {
4896 output_destroy(output);
4903 display_set_global_handler(struct display *display,
4904 display_global_handler_t handler)
4906 struct global *global;
4908 display->global_handler = handler;
4912 wl_list_for_each(global, &display->global_list, link)
4913 display->global_handler(display,
4914 global->name, global->interface,
4915 global->version, display->user_data);
4919 display_set_global_handler_remove(struct display *display,
4920 display_global_handler_t remove_handler)
4922 display->global_handler_remove = remove_handler;
4923 if (!remove_handler)
4928 display_set_output_configure_handler(struct display *display,
4929 display_output_handler_t handler)
4931 struct output *output;
4933 display->output_configure_handler = handler;
4937 wl_list_for_each(output, &display->output_list, link) {
4938 if (output->allocation.width == 0 &&
4939 output->allocation.height == 0)
4942 (*display->output_configure_handler)(output,
4943 display->user_data);
4948 output_set_user_data(struct output *output, void *data)
4950 output->user_data = data;
4954 output_get_user_data(struct output *output)
4956 return output->user_data;
4960 output_set_destroy_handler(struct output *output,
4961 display_output_handler_t handler)
4963 output->destroy_handler = handler;
4964 /* FIXME: implement this, once we have way to remove outputs */
4968 output_get_allocation(struct output *output, struct rectangle *base)
4970 struct rectangle allocation = output->allocation;
4972 switch (output->transform) {
4973 case WL_OUTPUT_TRANSFORM_90:
4974 case WL_OUTPUT_TRANSFORM_270:
4975 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4976 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4977 /* Swap width and height */
4978 allocation.width = output->allocation.height;
4979 allocation.height = output->allocation.width;
4987 output_get_wl_output(struct output *output)
4989 return output->output;
4992 enum wl_output_transform
4993 output_get_transform(struct output *output)
4995 return output->transform;
4999 output_get_scale(struct output *output)
5001 return output->scale;
5005 output_get_make(struct output *output)
5007 return output->make;
5011 output_get_model(struct output *output)
5013 return output->model;
5017 fini_xkb(struct input *input)
5019 xkb_state_unref(input->xkb.state);
5020 xkb_keymap_unref(input->xkb.keymap);
5023 #define MIN(a,b) ((a) < (b) ? a : b)
5026 display_add_input(struct display *d, uint32_t id)
5028 struct input *input;
5030 input = xzalloc(sizeof *input);
5032 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
5033 MIN(d->seat_version, 4));
5034 input->touch_focus = NULL;
5035 input->pointer_focus = NULL;
5036 input->keyboard_focus = NULL;
5037 wl_list_init(&input->touch_point_list);
5038 wl_list_insert(d->input_list.prev, &input->link);
5040 wl_seat_add_listener(input->seat, &seat_listener, input);
5041 wl_seat_set_user_data(input->seat, input);
5043 if (d->data_device_manager) {
5044 input->data_device =
5045 wl_data_device_manager_get_data_device(d->data_device_manager,
5047 wl_data_device_add_listener(input->data_device,
5048 &data_device_listener,
5052 input->pointer_surface = wl_compositor_create_surface(d->compositor);
5054 set_repeat_info(input, 40, 400);
5056 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5057 TFD_CLOEXEC | TFD_NONBLOCK);
5058 input->repeat_task.run = keyboard_repeat_func;
5059 display_watch_fd(d, input->repeat_timer_fd,
5060 EPOLLIN, &input->repeat_task);
5064 input_destroy(struct input *input)
5066 input_remove_keyboard_focus(input);
5067 input_remove_pointer_focus(input);
5069 if (input->drag_offer)
5070 data_offer_destroy(input->drag_offer);
5072 if (input->selection_offer)
5073 data_offer_destroy(input->selection_offer);
5075 if (input->data_device)
5076 wl_data_device_destroy(input->data_device);
5078 if (input->display->seat_version >= 3) {
5080 wl_pointer_release(input->pointer);
5081 if (input->keyboard)
5082 wl_keyboard_release(input->keyboard);
5087 wl_surface_destroy(input->pointer_surface);
5089 wl_list_remove(&input->link);
5090 wl_seat_destroy(input->seat);
5091 close(input->repeat_timer_fd);
5096 init_workspace_manager(struct display *d, uint32_t id)
5098 d->workspace_manager =
5099 wl_registry_bind(d->registry, id,
5100 &workspace_manager_interface, 1);
5101 if (d->workspace_manager != NULL)
5102 workspace_manager_add_listener(d->workspace_manager,
5103 &workspace_manager_listener,
5108 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5110 struct display *d = data;
5112 if (format == WL_SHM_FORMAT_RGB565)
5116 struct wl_shm_listener shm_listener = {
5121 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5123 xdg_shell_pong(shell, serial);
5126 static const struct xdg_shell_listener xdg_shell_listener = {
5130 #define XDG_VERSION 4 /* The version of xdg-shell that we implement */
5131 #ifdef static_assert
5132 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5133 "Interface version doesn't match implementation version");
5137 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5138 const char *interface, uint32_t version)
5140 struct display *d = data;
5141 struct global *global;
5143 global = xmalloc(sizeof *global);
5145 global->interface = strdup(interface);
5146 global->version = version;
5147 wl_list_insert(d->global_list.prev, &global->link);
5149 if (strcmp(interface, "wl_compositor") == 0) {
5150 d->compositor = wl_registry_bind(registry, id,
5151 &wl_compositor_interface, 3);
5152 } else if (strcmp(interface, "wl_output") == 0) {
5153 display_add_output(d, id);
5154 } else if (strcmp(interface, "wl_seat") == 0) {
5155 d->seat_version = version;
5156 display_add_input(d, id);
5157 } else if (strcmp(interface, "wl_shm") == 0) {
5158 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5159 wl_shm_add_listener(d->shm, &shm_listener, d);
5160 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5161 d->data_device_manager =
5162 wl_registry_bind(registry, id,
5163 &wl_data_device_manager_interface, 1);
5164 } else if (strcmp(interface, "xdg_shell") == 0) {
5165 d->xdg_shell = wl_registry_bind(registry, id,
5166 &xdg_shell_interface, 1);
5167 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5168 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5169 } else if (strcmp(interface, "text_cursor_position") == 0) {
5170 d->text_cursor_position =
5171 wl_registry_bind(registry, id,
5172 &text_cursor_position_interface, 1);
5173 } else if (strcmp(interface, "workspace_manager") == 0) {
5174 init_workspace_manager(d, id);
5175 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5177 wl_registry_bind(registry, id,
5178 &wl_subcompositor_interface, 1);
5181 if (d->global_handler)
5182 d->global_handler(d, id, interface, version, d->user_data);
5186 registry_handle_global_remove(void *data, struct wl_registry *registry,
5189 struct display *d = data;
5190 struct global *global;
5193 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5194 if (global->name != name)
5197 if (strcmp(global->interface, "wl_output") == 0)
5198 display_destroy_output(d, name);
5200 /* XXX: Should destroy remaining bound globals */
5202 if (d->global_handler_remove)
5203 d->global_handler_remove(d, name, global->interface,
5204 global->version, d->user_data);
5206 wl_list_remove(&global->link);
5207 free(global->interface);
5213 display_bind(struct display *display, uint32_t name,
5214 const struct wl_interface *interface, uint32_t version)
5216 return wl_registry_bind(display->registry, name, interface, version);
5219 static const struct wl_registry_listener registry_listener = {
5220 registry_handle_global,
5221 registry_handle_global_remove
5224 #ifdef HAVE_CAIRO_EGL
5226 init_egl(struct display *d)
5228 EGLint major, minor;
5231 #ifdef USE_CAIRO_GLESV2
5232 # define GL_BIT EGL_OPENGL_ES2_BIT
5234 # define GL_BIT EGL_OPENGL_BIT
5237 static const EGLint argb_cfg_attribs[] = {
5238 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5244 EGL_RENDERABLE_TYPE, GL_BIT,
5248 #ifdef USE_CAIRO_GLESV2
5249 static const EGLint context_attribs[] = {
5250 EGL_CONTEXT_CLIENT_VERSION, 2,
5253 EGLint api = EGL_OPENGL_ES_API;
5255 EGLint *context_attribs = NULL;
5256 EGLint api = EGL_OPENGL_API;
5259 d->dpy = eglGetDisplay(d->display);
5260 if (!eglInitialize(d->dpy, &major, &minor)) {
5261 fprintf(stderr, "failed to initialize EGL\n");
5265 if (!eglBindAPI(api)) {
5266 fprintf(stderr, "failed to bind EGL client API\n");
5270 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5271 &d->argb_config, 1, &n) || n != 1) {
5272 fprintf(stderr, "failed to choose argb EGL config\n");
5276 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5277 EGL_NO_CONTEXT, context_attribs);
5278 if (d->argb_ctx == NULL) {
5279 fprintf(stderr, "failed to create EGL context\n");
5283 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5284 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5285 fprintf(stderr, "failed to get cairo EGL argb device\n");
5293 fini_egl(struct display *display)
5295 cairo_device_destroy(display->argb_device);
5297 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5300 eglTerminate(display->dpy);
5306 init_dummy_surface(struct display *display)
5311 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5313 display->dummy_surface =
5314 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5316 display->dummy_surface_data = data;
5320 handle_display_data(struct task *task, uint32_t events)
5322 struct display *display =
5323 container_of(task, struct display, display_task);
5324 struct epoll_event ep;
5327 display->display_fd_events = events;
5329 if (events & EPOLLERR || events & EPOLLHUP) {
5330 display_exit(display);
5334 if (events & EPOLLIN) {
5335 ret = wl_display_dispatch(display->display);
5337 display_exit(display);
5342 if (events & EPOLLOUT) {
5343 ret = wl_display_flush(display->display);
5345 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5346 ep.data.ptr = &display->display_task;
5347 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5348 display->display_fd, &ep);
5349 } else if (ret == -1 && errno != EAGAIN) {
5350 display_exit(display);
5357 log_handler(const char *format, va_list args)
5359 vfprintf(stderr, format, args);
5363 display_create(int *argc, char *argv[])
5367 wl_log_set_handler_client(log_handler);
5369 d = zalloc(sizeof *d);
5373 d->display = wl_display_connect(NULL);
5374 if (d->display == NULL) {
5375 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5380 d->xkb_context = xkb_context_new(0);
5381 if (d->xkb_context == NULL) {
5382 fprintf(stderr, "Failed to create XKB context\n");
5387 d->epoll_fd = os_epoll_create_cloexec();
5388 d->display_fd = wl_display_get_fd(d->display);
5389 d->display_task.run = handle_display_data;
5390 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5393 wl_list_init(&d->deferred_list);
5394 wl_list_init(&d->input_list);
5395 wl_list_init(&d->output_list);
5396 wl_list_init(&d->global_list);
5399 d->workspace_count = 1;
5401 d->registry = wl_display_get_registry(d->display);
5402 wl_registry_add_listener(d->registry, ®istry_listener, d);
5404 if (wl_display_dispatch(d->display) < 0) {
5405 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5409 #ifdef HAVE_CAIRO_EGL
5410 if (init_egl(d) < 0)
5411 fprintf(stderr, "EGL does not seem to work, "
5412 "falling back to software rendering and wl_shm.\n");
5417 d->theme = theme_create();
5419 wl_list_init(&d->window_list);
5421 init_dummy_surface(d);
5427 display_destroy_outputs(struct display *display)
5430 struct output *output;
5432 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5433 output_destroy(output);
5437 display_destroy_inputs(struct display *display)
5440 struct input *input;
5442 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5443 input_destroy(input);
5447 display_destroy(struct display *display)
5449 if (!wl_list_empty(&display->window_list))
5450 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5451 wl_list_length(&display->window_list));
5453 if (!wl_list_empty(&display->deferred_list))
5454 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5456 cairo_surface_destroy(display->dummy_surface);
5457 free(display->dummy_surface_data);
5459 display_destroy_outputs(display);
5460 display_destroy_inputs(display);
5462 xkb_context_unref(display->xkb_context);
5464 theme_destroy(display->theme);
5465 destroy_cursors(display);
5467 #ifdef HAVE_CAIRO_EGL
5468 if (display->argb_device)
5472 if (display->subcompositor)
5473 wl_subcompositor_destroy(display->subcompositor);
5475 if (display->xdg_shell)
5476 xdg_shell_destroy(display->xdg_shell);
5479 wl_shm_destroy(display->shm);
5481 if (display->data_device_manager)
5482 wl_data_device_manager_destroy(display->data_device_manager);
5484 wl_compositor_destroy(display->compositor);
5485 wl_registry_destroy(display->registry);
5487 close(display->epoll_fd);
5489 if (!(display->display_fd_events & EPOLLERR) &&
5490 !(display->display_fd_events & EPOLLHUP))
5491 wl_display_flush(display->display);
5493 wl_display_disconnect(display->display);
5498 display_set_user_data(struct display *display, void *data)
5500 display->user_data = data;
5504 display_get_user_data(struct display *display)
5506 return display->user_data;
5510 display_get_display(struct display *display)
5512 return display->display;
5516 display_has_subcompositor(struct display *display)
5518 if (display->subcompositor)
5521 wl_display_roundtrip(display->display);
5523 return display->subcompositor != NULL;
5527 display_get_cairo_device(struct display *display)
5529 return display->argb_device;
5533 display_get_output(struct display *display)
5535 return container_of(display->output_list.next, struct output, link);
5538 struct wl_compositor *
5539 display_get_compositor(struct display *display)
5541 return display->compositor;
5545 display_get_serial(struct display *display)
5547 return display->serial;
5551 display_get_egl_display(struct display *d)
5556 struct wl_data_source *
5557 display_create_data_source(struct display *display)
5559 if (display->data_device_manager)
5560 return wl_data_device_manager_create_data_source(display->data_device_manager);
5566 display_get_argb_egl_config(struct display *d)
5568 return d->argb_config;
5572 display_acquire_window_surface(struct display *display,
5573 struct window *window,
5576 struct surface *surface = window->main_surface;
5578 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5581 widget_get_cairo_surface(window->main_surface->widget);
5582 return surface->toysurface->acquire(surface->toysurface, ctx);
5586 display_release_window_surface(struct display *display,
5587 struct window *window)
5589 struct surface *surface = window->main_surface;
5591 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5594 surface->toysurface->release(surface->toysurface);
5598 display_defer(struct display *display, struct task *task)
5600 wl_list_insert(&display->deferred_list, &task->link);
5604 display_watch_fd(struct display *display,
5605 int fd, uint32_t events, struct task *task)
5607 struct epoll_event ep;
5611 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5615 display_unwatch_fd(struct display *display, int fd)
5617 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5621 display_run(struct display *display)
5624 struct epoll_event ep[16];
5627 display->running = 1;
5629 while (!wl_list_empty(&display->deferred_list)) {
5630 task = container_of(display->deferred_list.prev,
5632 wl_list_remove(&task->link);
5636 wl_display_dispatch_pending(display->display);
5638 if (!display->running)
5641 ret = wl_display_flush(display->display);
5642 if (ret < 0 && errno == EAGAIN) {
5644 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5645 ep[0].data.ptr = &display->display_task;
5647 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5648 display->display_fd, &ep[0]);
5649 } else if (ret < 0) {
5653 count = epoll_wait(display->epoll_fd,
5654 ep, ARRAY_LENGTH(ep), -1);
5655 for (i = 0; i < count; i++) {
5656 task = ep[i].data.ptr;
5657 task->run(task, ep[i].events);
5663 display_exit(struct display *display)
5665 display->running = 0;
5669 keysym_modifiers_add(struct wl_array *modifiers_map,
5672 size_t len = strlen(name) + 1;
5675 p = wl_array_add(modifiers_map, len);
5680 strncpy(p, name, len);
5683 static xkb_mod_index_t
5684 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5687 xkb_mod_index_t index = 0;
5688 char *p = modifiers_map->data;
5690 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5691 if (strcmp(p, name) == 0)
5698 return XKB_MOD_INVALID;
5702 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5705 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5707 if (index == XKB_MOD_INVALID)
5708 return XKB_MOD_INVALID;
5714 fail_on_null(void *p)
5717 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5727 return fail_on_null(malloc(s));
5733 return fail_on_null(zalloc(s));
5737 xstrdup(const char *s)
5739 return fail_on_null(strdup(s));
5743 xrealloc(char *p, size_t s)
5745 return fail_on_null(realloc(p, s));