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 struct task repeat_task;
341 uint32_t repeat_time;
345 struct display *display;
346 struct wl_output *output;
347 uint32_t server_output_id;
348 struct rectangle allocation;
355 display_output_handler_t destroy_handler;
359 struct window_frame {
360 struct widget *widget;
361 struct widget *child;
366 struct window *window;
367 struct window *parent;
368 struct widget *widget;
371 const char **entries;
380 struct widget *parent;
381 struct widget *widget;
383 struct task tooltip_task;
389 struct wl_shm_pool *pool;
396 CURSOR_DEFAULT = 100,
400 static const cairo_user_data_key_t shm_surface_data_key;
407 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
408 __attribute__ ((format (printf, 4, 5)));
411 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
416 gettimeofday(&tv, NULL);
417 fprintf(stderr, "%8ld.%03ld ",
418 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
421 fprintf(stderr, "%s@%d ",
422 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
424 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
425 fprintf(stderr, "%s ", func);
428 vfprintf(stderr, fmt, ap);
432 #define DBG(fmt, ...) \
433 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
435 #define DBG_OBJ(obj, fmt, ...) \
436 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
440 #define DBG(...) do {} while (0)
441 #define DBG_OBJ(...) do {} while (0)
446 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
450 switch (buffer_transform) {
451 case WL_OUTPUT_TRANSFORM_90:
452 case WL_OUTPUT_TRANSFORM_270:
453 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
454 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
463 *width *= buffer_scale;
464 *height *= buffer_scale;
468 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
472 switch (buffer_transform) {
473 case WL_OUTPUT_TRANSFORM_90:
474 case WL_OUTPUT_TRANSFORM_270:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
485 *width /= buffer_scale;
486 *height /= buffer_scale;
489 #ifdef HAVE_CAIRO_EGL
491 struct egl_window_surface {
492 struct toysurface base;
493 cairo_surface_t *cairo_surface;
494 struct display *display;
495 struct wl_surface *surface;
496 struct wl_egl_window *egl_window;
497 EGLSurface egl_surface;
500 static struct egl_window_surface *
501 to_egl_window_surface(struct toysurface *base)
503 return container_of(base, struct egl_window_surface, base);
506 static cairo_surface_t *
507 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
508 int32_t width, int32_t height, uint32_t flags,
509 enum wl_output_transform buffer_transform, int32_t buffer_scale)
511 struct egl_window_surface *surface = to_egl_window_surface(base);
513 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
515 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
516 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
518 return cairo_surface_reference(surface->cairo_surface);
522 egl_window_surface_swap(struct toysurface *base,
523 enum wl_output_transform buffer_transform, int32_t buffer_scale,
524 struct rectangle *server_allocation)
526 struct egl_window_surface *surface = to_egl_window_surface(base);
528 cairo_gl_surface_swapbuffers(surface->cairo_surface);
529 wl_egl_window_get_attached_size(surface->egl_window,
530 &server_allocation->width,
531 &server_allocation->height);
533 buffer_to_surface_size (buffer_transform, buffer_scale,
534 &server_allocation->width,
535 &server_allocation->height);
539 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
541 struct egl_window_surface *surface = to_egl_window_surface(base);
542 cairo_device_t *device;
544 device = cairo_surface_get_device(surface->cairo_surface);
549 if (device == surface->display->argb_device)
550 ctx = surface->display->argb_ctx;
555 cairo_device_flush(device);
556 cairo_device_acquire(device);
557 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
558 surface->egl_surface, ctx))
559 fprintf(stderr, "failed to make surface current\n");
565 egl_window_surface_release(struct toysurface *base)
567 struct egl_window_surface *surface = to_egl_window_surface(base);
568 cairo_device_t *device;
570 device = cairo_surface_get_device(surface->cairo_surface);
574 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
575 surface->display->argb_ctx))
576 fprintf(stderr, "failed to make context current\n");
578 cairo_device_release(device);
582 egl_window_surface_destroy(struct toysurface *base)
584 struct egl_window_surface *surface = to_egl_window_surface(base);
585 struct display *d = surface->display;
587 cairo_surface_destroy(surface->cairo_surface);
588 eglDestroySurface(d->dpy, surface->egl_surface);
589 wl_egl_window_destroy(surface->egl_window);
590 surface->surface = NULL;
595 static struct toysurface *
596 egl_window_surface_create(struct display *display,
597 struct wl_surface *wl_surface,
599 struct rectangle *rectangle)
601 struct egl_window_surface *surface;
603 if (display->dpy == EGL_NO_DISPLAY)
606 surface = calloc(1, sizeof *surface);
610 surface->base.prepare = egl_window_surface_prepare;
611 surface->base.swap = egl_window_surface_swap;
612 surface->base.acquire = egl_window_surface_acquire;
613 surface->base.release = egl_window_surface_release;
614 surface->base.destroy = egl_window_surface_destroy;
616 surface->display = display;
617 surface->surface = wl_surface;
619 surface->egl_window = wl_egl_window_create(surface->surface,
623 surface->egl_surface = eglCreateWindowSurface(display->dpy,
624 display->argb_config,
628 surface->cairo_surface =
629 cairo_gl_surface_create_for_egl(display->argb_device,
630 surface->egl_surface,
634 return &surface->base;
639 static struct toysurface *
640 egl_window_surface_create(struct display *display,
641 struct wl_surface *wl_surface,
643 struct rectangle *rectangle)
650 struct shm_surface_data {
651 struct wl_buffer *buffer;
652 struct shm_pool *pool;
656 display_get_buffer_for_surface(struct display *display,
657 cairo_surface_t *surface)
659 struct shm_surface_data *data;
661 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
667 shm_pool_destroy(struct shm_pool *pool);
670 shm_surface_data_destroy(void *p)
672 struct shm_surface_data *data = p;
674 wl_buffer_destroy(data->buffer);
676 shm_pool_destroy(data->pool);
681 static struct wl_shm_pool *
682 make_shm_pool(struct display *display, int size, void **data)
684 struct wl_shm_pool *pool;
687 fd = os_create_anonymous_file(size);
689 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
694 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
695 if (*data == MAP_FAILED) {
696 fprintf(stderr, "mmap failed: %m\n");
701 pool = wl_shm_create_pool(display->shm, fd, size);
708 static struct shm_pool *
709 shm_pool_create(struct display *display, size_t size)
711 struct shm_pool *pool = malloc(sizeof *pool);
716 pool->pool = make_shm_pool(display, size, &pool->data);
729 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
731 if (pool->used + size > pool->size)
734 *offset = pool->used;
737 return (char *) pool->data + *offset;
740 /* destroy the pool. this does not unmap the memory though */
742 shm_pool_destroy(struct shm_pool *pool)
744 munmap(pool->data, pool->size);
745 wl_shm_pool_destroy(pool->pool);
749 /* Start allocating from the beginning of the pool again */
751 shm_pool_reset(struct shm_pool *pool)
757 data_length_for_shm_surface(struct rectangle *rect)
761 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
763 return stride * rect->height;
766 static cairo_surface_t *
767 display_create_shm_surface_from_pool(struct display *display,
768 struct rectangle *rectangle,
769 uint32_t flags, struct shm_pool *pool)
771 struct shm_surface_data *data;
773 cairo_surface_t *surface;
774 cairo_format_t cairo_format;
775 int stride, length, offset;
778 data = malloc(sizeof *data);
782 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
783 cairo_format = CAIRO_FORMAT_RGB16_565;
785 cairo_format = CAIRO_FORMAT_ARGB32;
787 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
788 length = stride * rectangle->height;
790 map = shm_pool_allocate(pool, length, &offset);
797 surface = cairo_image_surface_create_for_data (map,
803 cairo_surface_set_user_data(surface, &shm_surface_data_key,
804 data, shm_surface_data_destroy);
806 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
807 format = WL_SHM_FORMAT_RGB565;
809 if (flags & SURFACE_OPAQUE)
810 format = WL_SHM_FORMAT_XRGB8888;
812 format = WL_SHM_FORMAT_ARGB8888;
815 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
823 static cairo_surface_t *
824 display_create_shm_surface(struct display *display,
825 struct rectangle *rectangle, uint32_t flags,
826 struct shm_pool *alternate_pool,
827 struct shm_surface_data **data_ret)
829 struct shm_surface_data *data;
830 struct shm_pool *pool;
831 cairo_surface_t *surface;
833 if (alternate_pool) {
834 shm_pool_reset(alternate_pool);
835 surface = display_create_shm_surface_from_pool(display,
840 data = cairo_surface_get_user_data(surface,
841 &shm_surface_data_key);
846 pool = shm_pool_create(display,
847 data_length_for_shm_surface(rectangle));
852 display_create_shm_surface_from_pool(display, rectangle,
856 shm_pool_destroy(pool);
860 /* make sure we destroy the pool when the surface is destroyed */
861 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
872 check_size(struct rectangle *rect)
874 if (rect->width && rect->height)
877 fprintf(stderr, "tried to create surface of "
878 "width: %d, height: %d\n", rect->width, rect->height);
883 display_create_surface(struct display *display,
884 struct wl_surface *surface,
885 struct rectangle *rectangle,
888 if (check_size(rectangle) < 0)
891 assert(flags & SURFACE_SHM);
892 return display_create_shm_surface(display, rectangle, flags,
896 struct shm_surface_leaf {
897 cairo_surface_t *cairo_surface;
898 /* 'data' is automatically destroyed, when 'cairo_surface' is */
899 struct shm_surface_data *data;
901 struct shm_pool *resize_pool;
906 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
908 if (leaf->cairo_surface)
909 cairo_surface_destroy(leaf->cairo_surface);
910 /* leaf->data already destroyed via cairo private */
912 if (leaf->resize_pool)
913 shm_pool_destroy(leaf->resize_pool);
915 memset(leaf, 0, sizeof *leaf);
921 struct toysurface base;
922 struct display *display;
923 struct wl_surface *surface;
927 struct shm_surface_leaf leaf[MAX_LEAVES];
928 struct shm_surface_leaf *current;
931 static struct shm_surface *
932 to_shm_surface(struct toysurface *base)
934 return container_of(base, struct shm_surface, base);
938 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
941 struct shm_surface_leaf *leaf;
942 char bufs[MAX_LEAVES + 1];
945 for (i = 0; i < MAX_LEAVES; i++) {
946 leaf = &surface->leaf[i];
950 else if (leaf->cairo_surface)
956 bufs[MAX_LEAVES] = '\0';
957 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
962 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
964 struct shm_surface *surface = data;
965 struct shm_surface_leaf *leaf;
969 shm_surface_buffer_state_debug(surface, "buffer_release before");
971 for (i = 0; i < MAX_LEAVES; i++) {
972 leaf = &surface->leaf[i];
973 if (leaf->data && leaf->data->buffer == buffer) {
978 assert(i < MAX_LEAVES && "unknown buffer released");
980 /* Leave one free leaf with storage, release others */
982 for (i = 0; i < MAX_LEAVES; i++) {
983 leaf = &surface->leaf[i];
985 if (!leaf->cairo_surface || leaf->busy)
991 shm_surface_leaf_release(leaf);
994 shm_surface_buffer_state_debug(surface, "buffer_release after");
997 static const struct wl_buffer_listener shm_surface_buffer_listener = {
998 shm_surface_buffer_release
1001 static cairo_surface_t *
1002 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1003 int32_t width, int32_t height, uint32_t flags,
1004 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1006 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1007 struct shm_surface *surface = to_shm_surface(base);
1008 struct rectangle rect = { 0};
1009 struct shm_surface_leaf *leaf = NULL;
1015 /* pick a free buffer, preferrably one that already has storage */
1016 for (i = 0; i < MAX_LEAVES; i++) {
1017 if (surface->leaf[i].busy)
1020 if (!leaf || surface->leaf[i].cairo_surface)
1021 leaf = &surface->leaf[i];
1023 DBG_OBJ(surface->surface, "pick leaf %d\n",
1024 (int)(leaf - &surface->leaf[0]));
1027 fprintf(stderr, "%s: all buffers are held by the server.\n",
1033 if (!resize_hint && leaf->resize_pool) {
1034 cairo_surface_destroy(leaf->cairo_surface);
1035 leaf->cairo_surface = NULL;
1036 shm_pool_destroy(leaf->resize_pool);
1037 leaf->resize_pool = NULL;
1040 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1042 if (leaf->cairo_surface &&
1043 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1044 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1047 if (leaf->cairo_surface)
1048 cairo_surface_destroy(leaf->cairo_surface);
1050 #ifdef USE_RESIZE_POOL
1051 if (resize_hint && !leaf->resize_pool) {
1052 /* Create a big pool to allocate from, while continuously
1053 * resizing. Mmapping a new pool in the server
1054 * is relatively expensive, so reusing a pool performs
1055 * better, but may temporarily reserve unneeded memory.
1057 /* We should probably base this number on the output size. */
1058 leaf->resize_pool = shm_pool_create(surface->display,
1064 rect.height = height;
1066 leaf->cairo_surface =
1067 display_create_shm_surface(surface->display, &rect,
1071 if (!leaf->cairo_surface)
1074 wl_buffer_add_listener(leaf->data->buffer,
1075 &shm_surface_buffer_listener, surface);
1078 surface->current = leaf;
1080 return cairo_surface_reference(leaf->cairo_surface);
1084 shm_surface_swap(struct toysurface *base,
1085 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1086 struct rectangle *server_allocation)
1088 struct shm_surface *surface = to_shm_surface(base);
1089 struct shm_surface_leaf *leaf = surface->current;
1091 server_allocation->width =
1092 cairo_image_surface_get_width(leaf->cairo_surface);
1093 server_allocation->height =
1094 cairo_image_surface_get_height(leaf->cairo_surface);
1096 buffer_to_surface_size (buffer_transform, buffer_scale,
1097 &server_allocation->width,
1098 &server_allocation->height);
1100 wl_surface_attach(surface->surface, leaf->data->buffer,
1101 surface->dx, surface->dy);
1102 wl_surface_damage(surface->surface, 0, 0,
1103 server_allocation->width, server_allocation->height);
1104 wl_surface_commit(surface->surface);
1106 DBG_OBJ(surface->surface, "leaf %d busy\n",
1107 (int)(leaf - &surface->leaf[0]));
1110 surface->current = NULL;
1114 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1120 shm_surface_release(struct toysurface *base)
1125 shm_surface_destroy(struct toysurface *base)
1127 struct shm_surface *surface = to_shm_surface(base);
1130 for (i = 0; i < MAX_LEAVES; i++)
1131 shm_surface_leaf_release(&surface->leaf[i]);
1136 static struct toysurface *
1137 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1138 uint32_t flags, struct rectangle *rectangle)
1140 struct shm_surface *surface;
1141 DBG_OBJ(wl_surface, "\n");
1143 surface = xmalloc(sizeof *surface);
1144 memset(surface, 0, sizeof *surface);
1149 surface->base.prepare = shm_surface_prepare;
1150 surface->base.swap = shm_surface_swap;
1151 surface->base.acquire = shm_surface_acquire;
1152 surface->base.release = shm_surface_release;
1153 surface->base.destroy = shm_surface_destroy;
1155 surface->display = display;
1156 surface->surface = wl_surface;
1157 surface->flags = flags;
1159 return &surface->base;
1163 * The following correspondences between file names and cursors was copied
1164 * from: https://bugs.kde.org/attachment.cgi?id=67313
1167 static const char *bottom_left_corners[] = {
1168 "bottom_left_corner",
1173 static const char *bottom_right_corners[] = {
1174 "bottom_right_corner",
1179 static const char *bottom_sides[] = {
1185 static const char *grabbings[] = {
1188 "208530c400c041818281048008011002"
1191 static const char *left_ptrs[] = {
1198 static const char *left_sides[] = {
1204 static const char *right_sides[] = {
1210 static const char *top_left_corners[] = {
1216 static const char *top_right_corners[] = {
1222 static const char *top_sides[] = {
1228 static const char *xterms[] = {
1234 static const char *hand1s[] = {
1238 "e29285e634086352946a0e7090d73106"
1241 static const char *watches[] = {
1244 "0426c94ea35c87780ff01dc239897213"
1247 struct cursor_alternatives {
1252 static const struct cursor_alternatives cursors[] = {
1253 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1254 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1255 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1256 {grabbings, ARRAY_LENGTH(grabbings)},
1257 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1258 {left_sides, ARRAY_LENGTH(left_sides)},
1259 {right_sides, ARRAY_LENGTH(right_sides)},
1260 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1261 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1262 {top_sides, ARRAY_LENGTH(top_sides)},
1263 {xterms, ARRAY_LENGTH(xterms)},
1264 {hand1s, ARRAY_LENGTH(hand1s)},
1265 {watches, ARRAY_LENGTH(watches)},
1269 create_cursors(struct display *display)
1271 struct weston_config *config;
1272 struct weston_config_section *s;
1276 struct wl_cursor *cursor;
1278 config = weston_config_parse("weston.ini");
1279 s = weston_config_get_section(config, "shell", NULL, NULL);
1280 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1281 weston_config_section_get_int(s, "cursor-size", &size, 32);
1282 weston_config_destroy(config);
1284 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1285 if (!display->cursor_theme) {
1286 fprintf(stderr, "could not load theme '%s'\n", theme);
1291 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1293 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1295 for (j = 0; !cursor && j < cursors[i].count; ++j)
1296 cursor = wl_cursor_theme_get_cursor(
1297 display->cursor_theme, cursors[i].names[j]);
1300 fprintf(stderr, "could not load cursor '%s'\n",
1301 cursors[i].names[0]);
1303 display->cursors[i] = cursor;
1308 destroy_cursors(struct display *display)
1310 wl_cursor_theme_destroy(display->cursor_theme);
1311 free(display->cursors);
1314 struct wl_cursor_image *
1315 display_get_pointer_image(struct display *display, int pointer)
1317 struct wl_cursor *cursor = display->cursors[pointer];
1319 return cursor ? cursor->images[0] : NULL;
1323 surface_flush(struct surface *surface)
1325 if (!surface->cairo_surface)
1328 if (surface->opaque_region) {
1329 wl_surface_set_opaque_region(surface->surface,
1330 surface->opaque_region);
1331 wl_region_destroy(surface->opaque_region);
1332 surface->opaque_region = NULL;
1335 if (surface->input_region) {
1336 wl_surface_set_input_region(surface->surface,
1337 surface->input_region);
1338 wl_region_destroy(surface->input_region);
1339 surface->input_region = NULL;
1342 surface->toysurface->swap(surface->toysurface,
1343 surface->buffer_transform, surface->buffer_scale,
1344 &surface->server_allocation);
1346 cairo_surface_destroy(surface->cairo_surface);
1347 surface->cairo_surface = NULL;
1351 window_has_focus(struct window *window)
1353 return window->focused;
1357 window_close(struct window *window)
1359 if (window->close_handler)
1360 window->close_handler(window->user_data);
1362 display_exit(window->display);
1366 window_get_display(struct window *window)
1368 return window->display;
1372 surface_create_surface(struct surface *surface, uint32_t flags)
1374 struct display *display = surface->window->display;
1375 struct rectangle allocation = surface->allocation;
1377 if (!surface->toysurface && display->dpy &&
1378 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1379 surface->toysurface =
1380 egl_window_surface_create(display,
1386 if (!surface->toysurface)
1387 surface->toysurface = shm_surface_create(display,
1389 flags, &allocation);
1391 surface->cairo_surface = surface->toysurface->prepare(
1392 surface->toysurface, 0, 0,
1393 allocation.width, allocation.height, flags,
1394 surface->buffer_transform, surface->buffer_scale);
1398 window_create_main_surface(struct window *window)
1400 struct surface *surface = window->main_surface;
1403 if (window->resizing)
1404 flags |= SURFACE_HINT_RESIZE;
1406 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1407 flags |= SURFACE_HINT_RGB565;
1409 surface_create_surface(surface, flags);
1413 window_get_buffer_transform(struct window *window)
1415 return window->main_surface->buffer_transform;
1419 window_set_buffer_transform(struct window *window,
1420 enum wl_output_transform transform)
1422 window->main_surface->buffer_transform = transform;
1423 wl_surface_set_buffer_transform(window->main_surface->surface,
1428 window_set_buffer_scale(struct window *window,
1431 window->main_surface->buffer_scale = scale;
1432 wl_surface_set_buffer_scale(window->main_surface->surface,
1437 window_get_buffer_scale(struct window *window)
1439 return window->main_surface->buffer_scale;
1443 window_get_output_scale(struct window *window)
1445 struct window_output *window_output;
1446 struct window_output *window_output_tmp;
1449 wl_list_for_each_safe(window_output, window_output_tmp,
1450 &window->window_output_list, link) {
1451 if (window_output->output->scale > scale)
1452 scale = window_output->output->scale;
1458 static void window_frame_destroy(struct window_frame *frame);
1461 surface_destroy(struct surface *surface)
1463 if (surface->frame_cb)
1464 wl_callback_destroy(surface->frame_cb);
1466 if (surface->input_region)
1467 wl_region_destroy(surface->input_region);
1469 if (surface->opaque_region)
1470 wl_region_destroy(surface->opaque_region);
1472 if (surface->subsurface)
1473 wl_subsurface_destroy(surface->subsurface);
1475 wl_surface_destroy(surface->surface);
1477 if (surface->toysurface)
1478 surface->toysurface->destroy(surface->toysurface);
1480 wl_list_remove(&surface->link);
1485 window_destroy(struct window *window)
1487 struct display *display = window->display;
1488 struct input *input;
1489 struct window_output *window_output;
1490 struct window_output *window_output_tmp;
1492 wl_list_remove(&window->redraw_task.link);
1494 wl_list_for_each(input, &display->input_list, link) {
1495 if (input->touch_focus == window)
1496 input->touch_focus = NULL;
1497 if (input->pointer_focus == window)
1498 input->pointer_focus = NULL;
1499 if (input->keyboard_focus == window)
1500 input->keyboard_focus = NULL;
1501 if (input->focus_widget &&
1502 input->focus_widget->window == window)
1503 input->focus_widget = NULL;
1506 wl_list_for_each_safe(window_output, window_output_tmp,
1507 &window->window_output_list, link) {
1508 free (window_output);
1512 window_frame_destroy(window->frame);
1514 if (window->xdg_surface)
1515 xdg_surface_destroy(window->xdg_surface);
1516 if (window->xdg_popup)
1517 xdg_popup_destroy(window->xdg_popup);
1519 surface_destroy(window->main_surface);
1521 wl_list_remove(&window->link);
1523 free(window->title);
1527 static struct widget *
1528 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1530 struct widget *child, *target;
1532 wl_list_for_each(child, &widget->child_list, link) {
1533 target = widget_find_widget(child, x, y);
1538 if (widget->allocation.x <= x &&
1539 x < widget->allocation.x + widget->allocation.width &&
1540 widget->allocation.y <= y &&
1541 y < widget->allocation.y + widget->allocation.height) {
1548 static struct widget *
1549 window_find_widget(struct window *window, int32_t x, int32_t y)
1551 struct surface *surface;
1552 struct widget *widget;
1554 wl_list_for_each(surface, &window->subsurface_list, link) {
1555 widget = widget_find_widget(surface->widget, x, y);
1563 static struct widget *
1564 widget_create(struct window *window, struct surface *surface, void *data)
1566 struct widget *widget;
1568 widget = xzalloc(sizeof *widget);
1569 widget->window = window;
1570 widget->surface = surface;
1571 widget->user_data = data;
1572 widget->allocation = surface->allocation;
1573 wl_list_init(&widget->child_list);
1575 widget->tooltip = NULL;
1576 widget->tooltip_count = 0;
1577 widget->default_cursor = CURSOR_LEFT_PTR;
1578 widget->use_cairo = 1;
1584 window_add_widget(struct window *window, void *data)
1586 struct widget *widget;
1588 widget = widget_create(window, window->main_surface, data);
1589 wl_list_init(&widget->link);
1590 window->main_surface->widget = widget;
1596 widget_add_widget(struct widget *parent, void *data)
1598 struct widget *widget;
1600 widget = widget_create(parent->window, parent->surface, data);
1601 wl_list_insert(parent->child_list.prev, &widget->link);
1607 widget_destroy(struct widget *widget)
1609 struct display *display = widget->window->display;
1610 struct surface *surface = widget->surface;
1611 struct input *input;
1613 /* Destroy the sub-surface along with the root widget */
1614 if (surface->widget == widget && surface->subsurface)
1615 surface_destroy(widget->surface);
1617 if (widget->tooltip)
1618 widget_destroy_tooltip(widget);
1620 wl_list_for_each(input, &display->input_list, link) {
1621 if (input->focus_widget == widget)
1622 input->focus_widget = NULL;
1625 wl_list_remove(&widget->link);
1630 widget_set_default_cursor(struct widget *widget, int cursor)
1632 widget->default_cursor = cursor;
1636 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1638 *allocation = widget->allocation;
1642 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1644 widget->allocation.width = width;
1645 widget->allocation.height = height;
1649 widget_set_allocation(struct widget *widget,
1650 int32_t x, int32_t y, int32_t width, int32_t height)
1652 widget->allocation.x = x;
1653 widget->allocation.y = y;
1654 widget_set_size(widget, width, height);
1658 widget_set_transparent(struct widget *widget, int transparent)
1660 widget->opaque = !transparent;
1664 widget_get_user_data(struct widget *widget)
1666 return widget->user_data;
1669 static cairo_surface_t *
1670 widget_get_cairo_surface(struct widget *widget)
1672 struct surface *surface = widget->surface;
1673 struct window *window = widget->window;
1675 assert(widget->use_cairo);
1677 if (!surface->cairo_surface) {
1678 if (surface == window->main_surface)
1679 window_create_main_surface(window);
1681 surface_create_surface(surface, 0);
1684 return surface->cairo_surface;
1688 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1690 struct surface *surface = widget->surface;
1693 enum wl_output_transform transform;
1694 int surface_width, surface_height;
1695 int translate_x, translate_y;
1698 surface_width = surface->allocation.width;
1699 surface_height = surface->allocation.height;
1701 transform = surface->buffer_transform;
1702 scale = surface->buffer_scale;
1704 switch (transform) {
1705 case WL_OUTPUT_TRANSFORM_FLIPPED:
1706 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1707 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1708 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1709 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1712 cairo_matrix_init_identity(&m);
1716 switch (transform) {
1717 case WL_OUTPUT_TRANSFORM_NORMAL:
1723 case WL_OUTPUT_TRANSFORM_FLIPPED:
1725 translate_x = surface_width;
1728 case WL_OUTPUT_TRANSFORM_90:
1730 translate_x = surface_height;
1733 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1735 translate_x = surface_height;
1736 translate_y = surface_width;
1738 case WL_OUTPUT_TRANSFORM_180:
1740 translate_x = surface_width;
1741 translate_y = surface_height;
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1746 translate_y = surface_height;
1748 case WL_OUTPUT_TRANSFORM_270:
1749 angle = M_PI + M_PI_2;
1751 translate_y = surface_width;
1753 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1754 angle = M_PI + M_PI_2;
1760 cairo_scale(cr, scale, scale);
1761 cairo_translate(cr, translate_x, translate_y);
1762 cairo_rotate(cr, angle);
1763 cairo_transform(cr, &m);
1767 widget_cairo_create(struct widget *widget)
1769 struct surface *surface = widget->surface;
1770 cairo_surface_t *cairo_surface;
1773 cairo_surface = widget_get_cairo_surface(widget);
1774 cr = cairo_create(cairo_surface);
1776 widget_cairo_update_transform(widget, cr);
1778 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1784 widget_get_wl_surface(struct widget *widget)
1786 return widget->surface->surface;
1789 struct wl_subsurface *
1790 widget_get_wl_subsurface(struct widget *widget)
1792 return widget->surface->subsurface;
1796 widget_get_last_time(struct widget *widget)
1798 return widget->surface->last_time;
1802 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1804 struct wl_compositor *comp = widget->window->display->compositor;
1805 struct surface *surface = widget->surface;
1807 if (!surface->input_region)
1808 surface->input_region = wl_compositor_create_region(comp);
1811 wl_region_add(surface->input_region,
1812 rect->x, rect->y, rect->width, rect->height);
1817 widget_set_resize_handler(struct widget *widget,
1818 widget_resize_handler_t handler)
1820 widget->resize_handler = handler;
1824 widget_set_redraw_handler(struct widget *widget,
1825 widget_redraw_handler_t handler)
1827 widget->redraw_handler = handler;
1831 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1833 widget->enter_handler = handler;
1837 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1839 widget->leave_handler = handler;
1843 widget_set_motion_handler(struct widget *widget,
1844 widget_motion_handler_t handler)
1846 widget->motion_handler = handler;
1850 widget_set_button_handler(struct widget *widget,
1851 widget_button_handler_t handler)
1853 widget->button_handler = handler;
1857 widget_set_touch_up_handler(struct widget *widget,
1858 widget_touch_up_handler_t handler)
1860 widget->touch_up_handler = handler;
1864 widget_set_touch_down_handler(struct widget *widget,
1865 widget_touch_down_handler_t handler)
1867 widget->touch_down_handler = handler;
1871 widget_set_touch_motion_handler(struct widget *widget,
1872 widget_touch_motion_handler_t handler)
1874 widget->touch_motion_handler = handler;
1878 widget_set_touch_frame_handler(struct widget *widget,
1879 widget_touch_frame_handler_t handler)
1881 widget->touch_frame_handler = handler;
1885 widget_set_touch_cancel_handler(struct widget *widget,
1886 widget_touch_cancel_handler_t handler)
1888 widget->touch_cancel_handler = handler;
1892 widget_set_axis_handler(struct widget *widget,
1893 widget_axis_handler_t handler)
1895 widget->axis_handler = handler;
1899 window_schedule_redraw_task(struct window *window);
1902 widget_schedule_redraw(struct widget *widget)
1904 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1905 widget->surface->redraw_needed = 1;
1906 window_schedule_redraw_task(widget->window);
1910 widget_set_use_cairo(struct widget *widget,
1913 widget->use_cairo = use_cairo;
1917 window_get_surface(struct window *window)
1919 cairo_surface_t *cairo_surface;
1921 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1923 return cairo_surface_reference(cairo_surface);
1927 window_get_wl_surface(struct window *window)
1929 return window->main_surface->surface;
1933 tooltip_redraw_handler(struct widget *widget, void *data)
1936 const int32_t r = 3;
1937 struct tooltip *tooltip = data;
1938 int32_t width, height;
1940 cr = widget_cairo_create(widget);
1941 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1942 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1943 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1946 width = widget->allocation.width;
1947 height = widget->allocation.height;
1948 rounded_rect(cr, 0, 0, width, height, r);
1950 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1954 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1955 cairo_move_to(cr, 10, 16);
1956 cairo_show_text(cr, tooltip->entry);
1960 static cairo_text_extents_t
1961 get_text_extents(struct display *display, struct tooltip *tooltip)
1964 cairo_text_extents_t extents;
1966 /* Use the dummy_surface because tooltip's surface was not
1967 * created yet, and parent does not have a valid surface
1968 * outside repaint, either.
1970 cr = cairo_create(display->dummy_surface);
1971 cairo_text_extents(cr, tooltip->entry, &extents);
1978 window_create_tooltip(struct tooltip *tooltip)
1980 struct widget *parent = tooltip->parent;
1981 struct display *display = parent->window->display;
1982 const int offset_y = 27;
1983 const int margin = 3;
1984 cairo_text_extents_t extents;
1986 if (tooltip->widget)
1989 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1991 extents = get_text_extents(display, tooltip);
1992 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1993 widget_set_allocation(tooltip->widget,
1994 tooltip->x, tooltip->y + offset_y,
1995 extents.width + 20, 20 + margin * 2);
2001 widget_destroy_tooltip(struct widget *parent)
2003 struct tooltip *tooltip = parent->tooltip;
2005 parent->tooltip_count = 0;
2009 if (tooltip->widget) {
2010 widget_destroy(tooltip->widget);
2011 tooltip->widget = NULL;
2014 close(tooltip->tooltip_fd);
2015 free(tooltip->entry);
2017 parent->tooltip = NULL;
2021 tooltip_func(struct task *task, uint32_t events)
2023 struct tooltip *tooltip =
2024 container_of(task, struct tooltip, tooltip_task);
2027 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2029 window_create_tooltip(tooltip);
2032 #define TOOLTIP_TIMEOUT 500
2034 tooltip_timer_reset(struct tooltip *tooltip)
2036 struct itimerspec its;
2038 its.it_interval.tv_sec = 0;
2039 its.it_interval.tv_nsec = 0;
2040 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2041 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2042 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2043 fprintf(stderr, "could not set timerfd\n: %m");
2051 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2053 struct tooltip *tooltip = parent->tooltip;
2055 parent->tooltip_count++;
2059 tooltip_timer_reset(tooltip);
2063 /* the handler might be triggered too fast via input device motion, so
2064 * we need this check here to make sure tooltip is fully initialized */
2065 if (parent->tooltip_count > 1)
2068 tooltip = malloc(sizeof *tooltip);
2072 parent->tooltip = tooltip;
2073 tooltip->parent = parent;
2074 tooltip->widget = NULL;
2077 tooltip->entry = strdup(entry);
2078 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2079 if (tooltip->tooltip_fd < 0) {
2080 fprintf(stderr, "could not create timerfd\n: %m");
2084 tooltip->tooltip_task.run = tooltip_func;
2085 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2086 EPOLLIN, &tooltip->tooltip_task);
2087 tooltip_timer_reset(tooltip);
2093 workspace_manager_state(void *data,
2094 struct workspace_manager *workspace_manager,
2098 struct display *display = data;
2100 display->workspace = current;
2101 display->workspace_count = count;
2104 static const struct workspace_manager_listener workspace_manager_listener = {
2105 workspace_manager_state
2109 frame_resize_handler(struct widget *widget,
2110 int32_t width, int32_t height, void *data)
2112 struct window_frame *frame = data;
2113 struct widget *child = frame->child;
2114 struct rectangle interior;
2115 struct rectangle input;
2116 struct rectangle opaque;
2118 if (widget->window->fullscreen) {
2121 interior.width = width;
2122 interior.height = height;
2124 frame_resize(frame->frame, width, height);
2125 frame_interior(frame->frame, &interior.x, &interior.y,
2126 &interior.width, &interior.height);
2129 widget_set_allocation(child, interior.x, interior.y,
2130 interior.width, interior.height);
2132 if (child->resize_handler) {
2133 child->resize_handler(child, interior.width, interior.height,
2136 if (widget->window->fullscreen) {
2137 width = child->allocation.width;
2138 height = child->allocation.height;
2140 frame_resize_inside(frame->frame,
2141 child->allocation.width,
2142 child->allocation.height);
2143 width = frame_width(frame->frame);
2144 height = frame_height(frame->frame);
2148 widget_set_allocation(widget, 0, 0, width, height);
2150 widget->surface->input_region =
2151 wl_compositor_create_region(widget->window->display->compositor);
2152 if (!widget->window->fullscreen) {
2153 frame_input_rect(frame->frame, &input.x, &input.y,
2154 &input.width, &input.height);
2155 wl_region_add(widget->surface->input_region,
2156 input.x, input.y, input.width, input.height);
2158 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2161 widget_set_allocation(widget, 0, 0, width, height);
2163 if (child->opaque) {
2164 if (!widget->window->fullscreen) {
2165 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2166 &opaque.width, &opaque.height);
2168 wl_region_add(widget->surface->opaque_region,
2170 opaque.width, opaque.height);
2172 wl_region_add(widget->surface->opaque_region,
2173 0, 0, width, height);
2178 widget_schedule_redraw(widget);
2182 frame_redraw_handler(struct widget *widget, void *data)
2185 struct window_frame *frame = data;
2186 struct window *window = widget->window;
2188 if (window->fullscreen)
2191 cr = widget_cairo_create(widget);
2193 frame_repaint(frame->frame, cr);
2199 frame_get_pointer_image_for_location(struct window_frame *frame,
2200 enum theme_location location)
2202 struct window *window = frame->widget->window;
2205 return CURSOR_LEFT_PTR;
2208 case THEME_LOCATION_RESIZING_TOP:
2210 case THEME_LOCATION_RESIZING_BOTTOM:
2211 return CURSOR_BOTTOM;
2212 case THEME_LOCATION_RESIZING_LEFT:
2214 case THEME_LOCATION_RESIZING_RIGHT:
2215 return CURSOR_RIGHT;
2216 case THEME_LOCATION_RESIZING_TOP_LEFT:
2217 return CURSOR_TOP_LEFT;
2218 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2219 return CURSOR_TOP_RIGHT;
2220 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2221 return CURSOR_BOTTOM_LEFT;
2222 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2223 return CURSOR_BOTTOM_RIGHT;
2224 case THEME_LOCATION_EXTERIOR:
2225 case THEME_LOCATION_TITLEBAR:
2227 return CURSOR_LEFT_PTR;
2232 frame_menu_func(struct window *window,
2233 struct input *input, int index, void *data)
2235 struct display *display;
2239 window_close(window);
2241 case 1: /* move to workspace above */
2242 display = window->display;
2243 if (display->workspace > 0)
2244 workspace_manager_move_surface(
2245 display->workspace_manager,
2246 window->main_surface->surface,
2247 display->workspace - 1);
2249 case 2: /* move to workspace below */
2250 display = window->display;
2251 if (display->workspace < display->workspace_count - 1)
2252 workspace_manager_move_surface(
2253 display->workspace_manager,
2254 window->main_surface->surface,
2255 display->workspace + 1);
2257 case 3: /* fullscreen */
2258 /* we don't have a way to get out of fullscreen for now */
2259 if (window->fullscreen_handler)
2260 window->fullscreen_handler(window, window->user_data);
2266 window_show_frame_menu(struct window *window,
2267 struct input *input, uint32_t time)
2272 static const char *entries[] = {
2274 "Move to workspace above", "Move to workspace below",
2278 if (window->fullscreen_handler)
2279 count = ARRAY_LENGTH(entries);
2281 count = ARRAY_LENGTH(entries) - 1;
2283 input_get_position(input, &x, &y);
2284 window_show_menu(window->display, input, time, window,
2285 x - 10, y - 10, frame_menu_func, entries, count);
2289 frame_enter_handler(struct widget *widget,
2290 struct input *input, float x, float y, void *data)
2292 struct window_frame *frame = data;
2293 enum theme_location location;
2295 location = frame_pointer_enter(frame->frame, input, x, y);
2296 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2297 widget_schedule_redraw(frame->widget);
2299 return frame_get_pointer_image_for_location(data, location);
2303 frame_motion_handler(struct widget *widget,
2304 struct input *input, uint32_t time,
2305 float x, float y, void *data)
2307 struct window_frame *frame = data;
2308 enum theme_location location;
2310 location = frame_pointer_motion(frame->frame, input, x, y);
2311 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2312 widget_schedule_redraw(frame->widget);
2314 return frame_get_pointer_image_for_location(data, location);
2318 frame_leave_handler(struct widget *widget,
2319 struct input *input, void *data)
2321 struct window_frame *frame = data;
2323 frame_pointer_leave(frame->frame, input);
2324 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2325 widget_schedule_redraw(frame->widget);
2329 frame_handle_status(struct window_frame *frame, struct input *input,
2330 uint32_t time, enum theme_location location)
2332 struct window *window = frame->widget->window;
2335 status = frame_status(frame->frame);
2336 if (status & FRAME_STATUS_REPAINT)
2337 widget_schedule_redraw(frame->widget);
2339 if (status & FRAME_STATUS_MINIMIZE) {
2340 window_set_minimized(window);
2341 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2344 if (status & FRAME_STATUS_MENU) {
2345 window_show_frame_menu(window, input, time);
2346 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2349 if (status & FRAME_STATUS_MAXIMIZE) {
2350 window_set_maximized(window, !window->maximized);
2351 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2354 if (status & FRAME_STATUS_CLOSE) {
2355 window_close(window);
2359 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2360 input_ungrab(input);
2361 xdg_surface_move(window->xdg_surface,
2362 input_get_seat(input),
2363 window->display->serial);
2365 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2368 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2369 input_ungrab(input);
2371 xdg_surface_resize(window->xdg_surface,
2372 input_get_seat(input),
2373 window->display->serial,
2376 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2381 frame_button_handler(struct widget *widget,
2382 struct input *input, uint32_t time,
2383 uint32_t button, enum wl_pointer_button_state state,
2387 struct window_frame *frame = data;
2388 enum theme_location location;
2390 location = frame_pointer_button(frame->frame, input, button, state);
2391 frame_handle_status(frame, input, time, location);
2395 frame_touch_down_handler(struct widget *widget, struct input *input,
2396 uint32_t serial, uint32_t time, int32_t id,
2397 float x, float y, void *data)
2399 struct window_frame *frame = data;
2401 frame_touch_down(frame->frame, input, id, x, y);
2402 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2406 frame_touch_up_handler(struct widget *widget,
2407 struct input *input, uint32_t serial, uint32_t time,
2408 int32_t id, void *data)
2410 struct window_frame *frame = data;
2412 frame_touch_up(frame->frame, input, id);
2413 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2417 window_frame_create(struct window *window, void *data)
2419 struct window_frame *frame;
2422 if (window->custom) {
2423 buttons = FRAME_BUTTON_NONE;
2425 buttons = FRAME_BUTTON_ALL;
2428 frame = xzalloc(sizeof *frame);
2429 frame->frame = frame_create(window->display->theme, 0, 0,
2430 buttons, window->title);
2432 frame->widget = window_add_widget(window, frame);
2433 frame->child = widget_add_widget(frame->widget, data);
2435 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2436 widget_set_resize_handler(frame->widget, frame_resize_handler);
2437 widget_set_enter_handler(frame->widget, frame_enter_handler);
2438 widget_set_leave_handler(frame->widget, frame_leave_handler);
2439 widget_set_motion_handler(frame->widget, frame_motion_handler);
2440 widget_set_button_handler(frame->widget, frame_button_handler);
2441 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2442 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2444 window->frame = frame;
2446 return frame->child;
2450 window_frame_set_child_size(struct widget *widget, int child_width,
2453 struct display *display = widget->window->display;
2454 struct theme *t = display->theme;
2455 int decoration_width, decoration_height;
2457 int margin = widget->window->maximized ? 0 : t->margin;
2459 if (!widget->window->fullscreen) {
2460 decoration_width = (t->width + margin) * 2;
2461 decoration_height = t->width +
2462 t->titlebar_height + margin * 2;
2464 width = child_width + decoration_width;
2465 height = child_height + decoration_height;
2467 width = child_width;
2468 height = child_height;
2471 window_schedule_resize(widget->window, width, height);
2475 window_frame_destroy(struct window_frame *frame)
2477 frame_destroy(frame->frame);
2479 /* frame->child must be destroyed by the application */
2480 widget_destroy(frame->widget);
2485 input_set_focus_widget(struct input *input, struct widget *focus,
2488 struct widget *old, *widget;
2491 if (focus == input->focus_widget)
2494 old = input->focus_widget;
2498 widget = input->grab;
2499 if (widget->leave_handler)
2500 widget->leave_handler(old, input, widget->user_data);
2501 input->focus_widget = NULL;
2507 widget = input->grab;
2508 input->focus_widget = focus;
2509 if (widget->enter_handler)
2510 cursor = widget->enter_handler(focus, input, x, y,
2513 cursor = widget->default_cursor;
2515 input_set_pointer_image(input, cursor);
2520 touch_grab(struct input *input, int32_t touch_id)
2522 input->touch_grab = 1;
2523 input->touch_grab_id = touch_id;
2527 touch_ungrab(struct input *input)
2529 struct touch_point *tp, *tmp;
2531 input->touch_grab = 0;
2533 wl_list_for_each_safe(tp, tmp,
2534 &input->touch_point_list, link) {
2535 if (tp->id != input->touch_grab_id)
2537 wl_list_remove(&tp->link);
2545 input_grab(struct input *input, struct widget *widget, uint32_t button)
2547 input->grab = widget;
2548 input->grab_button = button;
2550 input_set_focus_widget(input, widget, input->sx, input->sy);
2554 input_ungrab(struct input *input)
2556 struct widget *widget;
2559 if (input->pointer_focus) {
2560 widget = window_find_widget(input->pointer_focus,
2561 input->sx, input->sy);
2562 input_set_focus_widget(input, widget, input->sx, input->sy);
2567 input_remove_pointer_focus(struct input *input)
2569 struct window *window = input->pointer_focus;
2574 input_set_focus_widget(input, NULL, 0, 0);
2576 input->pointer_focus = NULL;
2577 input->current_cursor = CURSOR_UNSET;
2581 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2582 uint32_t serial, struct wl_surface *surface,
2583 wl_fixed_t sx_w, wl_fixed_t sy_w)
2585 struct input *input = data;
2586 struct window *window;
2587 struct widget *widget;
2588 float sx = wl_fixed_to_double(sx_w);
2589 float sy = wl_fixed_to_double(sy_w);
2592 /* enter event for a window we've just destroyed */
2596 window = wl_surface_get_user_data(surface);
2597 if (surface != window->main_surface->surface) {
2598 DBG("Ignoring input event from subsurface %p\n", surface);
2602 input->display->serial = serial;
2603 input->pointer_enter_serial = serial;
2604 input->pointer_focus = window;
2609 widget = window_find_widget(window, sx, sy);
2610 input_set_focus_widget(input, widget, sx, sy);
2614 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2615 uint32_t serial, struct wl_surface *surface)
2617 struct input *input = data;
2619 input->display->serial = serial;
2620 input_remove_pointer_focus(input);
2624 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2625 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2627 struct input *input = data;
2628 struct window *window = input->pointer_focus;
2629 struct widget *widget;
2631 float sx = wl_fixed_to_double(sx_w);
2632 float sy = wl_fixed_to_double(sy_w);
2640 /* when making the window smaller - e.g. after a unmaximise we might
2641 * still have a pending motion event that the compositor has picked
2642 * based on the old surface dimensions
2644 if (sx > window->main_surface->allocation.width ||
2645 sy > window->main_surface->allocation.height)
2648 if (!(input->grab && input->grab_button)) {
2649 widget = window_find_widget(window, sx, sy);
2650 input_set_focus_widget(input, widget, sx, sy);
2654 widget = input->grab;
2656 widget = input->focus_widget;
2658 if (widget->motion_handler)
2659 cursor = widget->motion_handler(input->focus_widget,
2660 input, time, sx, sy,
2663 cursor = widget->default_cursor;
2665 cursor = CURSOR_LEFT_PTR;
2667 input_set_pointer_image(input, cursor);
2671 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2672 uint32_t time, uint32_t button, uint32_t state_w)
2674 struct input *input = data;
2675 struct widget *widget;
2676 enum wl_pointer_button_state state = state_w;
2678 input->display->serial = serial;
2679 if (input->focus_widget && input->grab == NULL &&
2680 state == WL_POINTER_BUTTON_STATE_PRESSED)
2681 input_grab(input, input->focus_widget, button);
2683 widget = input->grab;
2684 if (widget && widget->button_handler)
2685 (*widget->button_handler)(widget,
2688 input->grab->user_data);
2690 if (input->grab && input->grab_button == button &&
2691 state == WL_POINTER_BUTTON_STATE_RELEASED)
2692 input_ungrab(input);
2696 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2697 uint32_t time, uint32_t axis, wl_fixed_t value)
2699 struct input *input = data;
2700 struct widget *widget;
2702 widget = input->focus_widget;
2704 widget = input->grab;
2705 if (widget && widget->axis_handler)
2706 (*widget->axis_handler)(widget,
2712 static const struct wl_pointer_listener pointer_listener = {
2713 pointer_handle_enter,
2714 pointer_handle_leave,
2715 pointer_handle_motion,
2716 pointer_handle_button,
2717 pointer_handle_axis,
2721 input_remove_keyboard_focus(struct input *input)
2723 struct window *window = input->keyboard_focus;
2724 struct itimerspec its;
2726 its.it_interval.tv_sec = 0;
2727 its.it_interval.tv_nsec = 0;
2728 its.it_value.tv_sec = 0;
2729 its.it_value.tv_nsec = 0;
2730 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2735 if (window->keyboard_focus_handler)
2736 (*window->keyboard_focus_handler)(window, NULL,
2739 input->keyboard_focus = NULL;
2743 keyboard_repeat_func(struct task *task, uint32_t events)
2745 struct input *input =
2746 container_of(task, struct input, repeat_task);
2747 struct window *window = input->keyboard_focus;
2750 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2751 /* If we change the timer between the fd becoming
2752 * readable and getting here, there'll be nothing to
2753 * read and we get EAGAIN. */
2756 if (window && window->key_handler) {
2757 (*window->key_handler)(window, input, input->repeat_time,
2758 input->repeat_key, input->repeat_sym,
2759 WL_KEYBOARD_KEY_STATE_PRESSED,
2765 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2766 uint32_t format, int fd, uint32_t size)
2768 struct input *input = data;
2769 struct xkb_keymap *keymap;
2770 struct xkb_state *state;
2778 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2783 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2784 if (map_str == MAP_FAILED) {
2789 keymap = xkb_map_new_from_string(input->display->xkb_context,
2791 XKB_KEYMAP_FORMAT_TEXT_V1,
2793 munmap(map_str, size);
2797 fprintf(stderr, "failed to compile keymap\n");
2801 state = xkb_state_new(keymap);
2803 fprintf(stderr, "failed to create XKB state\n");
2804 xkb_map_unref(keymap);
2808 xkb_keymap_unref(input->xkb.keymap);
2809 xkb_state_unref(input->xkb.state);
2810 input->xkb.keymap = keymap;
2811 input->xkb.state = state;
2813 input->xkb.control_mask =
2814 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2815 input->xkb.alt_mask =
2816 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2817 input->xkb.shift_mask =
2818 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2822 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2823 uint32_t serial, struct wl_surface *surface,
2824 struct wl_array *keys)
2826 struct input *input = data;
2827 struct window *window;
2829 input->display->serial = serial;
2830 input->keyboard_focus = wl_surface_get_user_data(surface);
2832 window = input->keyboard_focus;
2833 if (window->keyboard_focus_handler)
2834 (*window->keyboard_focus_handler)(window,
2835 input, window->user_data);
2839 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2840 uint32_t serial, struct wl_surface *surface)
2842 struct input *input = data;
2844 input->display->serial = serial;
2845 input_remove_keyboard_focus(input);
2849 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2850 uint32_t serial, uint32_t time, uint32_t key,
2853 struct input *input = data;
2854 struct window *window = input->keyboard_focus;
2855 uint32_t code, num_syms;
2856 enum wl_keyboard_key_state state = state_w;
2857 const xkb_keysym_t *syms;
2859 struct itimerspec its;
2861 input->display->serial = serial;
2863 if (!window || !input->xkb.state)
2866 /* We only use input grabs for pointer events for now, so just
2867 * ignore key presses if a grab is active. We expand the key
2868 * event delivery mechanism to route events to widgets to
2869 * properly handle key grabs. In the meantime, this prevents
2870 * key event devlivery while a grab is active. */
2871 if (input->grab && input->grab_button == 0)
2874 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2876 sym = XKB_KEY_NoSymbol;
2881 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2882 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2883 window_set_maximized(window, !window->maximized);
2884 } else if (sym == XKB_KEY_F11 &&
2885 window->fullscreen_handler &&
2886 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2887 window->fullscreen_handler(window, window->user_data);
2888 } else if (sym == XKB_KEY_F4 &&
2889 input->modifiers == MOD_ALT_MASK &&
2890 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2891 window_close(window);
2892 } else if (window->key_handler) {
2893 (*window->key_handler)(window, input, time, key,
2894 sym, state, window->user_data);
2897 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2898 key == input->repeat_key) {
2899 its.it_interval.tv_sec = 0;
2900 its.it_interval.tv_nsec = 0;
2901 its.it_value.tv_sec = 0;
2902 its.it_value.tv_nsec = 0;
2903 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2904 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2905 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2906 input->repeat_sym = sym;
2907 input->repeat_key = key;
2908 input->repeat_time = time;
2909 its.it_interval.tv_sec = 0;
2910 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2911 its.it_value.tv_sec = 0;
2912 its.it_value.tv_nsec = 400 * 1000 * 1000;
2913 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2918 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2919 uint32_t serial, uint32_t mods_depressed,
2920 uint32_t mods_latched, uint32_t mods_locked,
2923 struct input *input = data;
2924 xkb_mod_mask_t mask;
2926 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2927 if (!input->xkb.keymap)
2930 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2931 mods_locked, 0, 0, group);
2932 mask = xkb_state_serialize_mods(input->xkb.state,
2933 XKB_STATE_DEPRESSED |
2935 input->modifiers = 0;
2936 if (mask & input->xkb.control_mask)
2937 input->modifiers |= MOD_CONTROL_MASK;
2938 if (mask & input->xkb.alt_mask)
2939 input->modifiers |= MOD_ALT_MASK;
2940 if (mask & input->xkb.shift_mask)
2941 input->modifiers |= MOD_SHIFT_MASK;
2944 static const struct wl_keyboard_listener keyboard_listener = {
2945 keyboard_handle_keymap,
2946 keyboard_handle_enter,
2947 keyboard_handle_leave,
2948 keyboard_handle_key,
2949 keyboard_handle_modifiers,
2953 touch_handle_down(void *data, struct wl_touch *wl_touch,
2954 uint32_t serial, uint32_t time, struct wl_surface *surface,
2955 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2957 struct input *input = data;
2958 struct widget *widget;
2959 float sx = wl_fixed_to_double(x_w);
2960 float sy = wl_fixed_to_double(y_w);
2962 input->display->serial = serial;
2963 input->touch_focus = wl_surface_get_user_data(surface);
2964 if (!input->touch_focus) {
2965 DBG("Failed to find to touch focus for surface %p\n", surface);
2969 if (surface != input->touch_focus->main_surface->surface) {
2970 DBG("Ignoring input event from subsurface %p\n", surface);
2971 input->touch_focus = NULL;
2976 widget = input->grab;
2978 widget = window_find_widget(input->touch_focus,
2979 wl_fixed_to_double(x_w),
2980 wl_fixed_to_double(y_w));
2982 struct touch_point *tp = xmalloc(sizeof *tp);
2985 tp->widget = widget;
2988 wl_list_insert(&input->touch_point_list, &tp->link);
2990 if (widget->touch_down_handler)
2991 (*widget->touch_down_handler)(widget, input,
3000 touch_handle_up(void *data, struct wl_touch *wl_touch,
3001 uint32_t serial, uint32_t time, int32_t id)
3003 struct input *input = data;
3004 struct touch_point *tp, *tmp;
3006 if (!input->touch_focus) {
3007 DBG("No touch focus found for touch up event!\n");
3011 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3015 if (tp->widget->touch_up_handler)
3016 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3018 tp->widget->user_data);
3020 wl_list_remove(&tp->link);
3028 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3029 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3031 struct input *input = data;
3032 struct touch_point *tp;
3033 float sx = wl_fixed_to_double(x_w);
3034 float sy = wl_fixed_to_double(y_w);
3036 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3038 if (!input->touch_focus) {
3039 DBG("No touch focus found for touch motion event!\n");
3043 wl_list_for_each(tp, &input->touch_point_list, link) {
3049 if (tp->widget->touch_motion_handler)
3050 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3052 tp->widget->user_data);
3058 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3060 struct input *input = data;
3061 struct touch_point *tp, *tmp;
3063 DBG("touch_handle_frame\n");
3065 if (!input->touch_focus) {
3066 DBG("No touch focus found for touch frame event!\n");
3070 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3071 if (tp->widget->touch_frame_handler)
3072 (*tp->widget->touch_frame_handler)(tp->widget, input,
3073 tp->widget->user_data);
3078 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3080 struct input *input = data;
3081 struct touch_point *tp, *tmp;
3083 DBG("touch_handle_cancel\n");
3085 if (!input->touch_focus) {
3086 DBG("No touch focus found for touch cancel event!\n");
3090 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3091 if (tp->widget->touch_cancel_handler)
3092 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3093 tp->widget->user_data);
3095 wl_list_remove(&tp->link);
3100 static const struct wl_touch_listener touch_listener = {
3103 touch_handle_motion,
3105 touch_handle_cancel,
3109 seat_handle_capabilities(void *data, struct wl_seat *seat,
3110 enum wl_seat_capability caps)
3112 struct input *input = data;
3114 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3115 input->pointer = wl_seat_get_pointer(seat);
3116 wl_pointer_set_user_data(input->pointer, input);
3117 wl_pointer_add_listener(input->pointer, &pointer_listener,
3119 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3120 wl_pointer_destroy(input->pointer);
3121 input->pointer = NULL;
3124 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3125 input->keyboard = wl_seat_get_keyboard(seat);
3126 wl_keyboard_set_user_data(input->keyboard, input);
3127 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3129 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3130 wl_keyboard_destroy(input->keyboard);
3131 input->keyboard = NULL;
3134 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3135 input->touch = wl_seat_get_touch(seat);
3136 wl_touch_set_user_data(input->touch, input);
3137 wl_touch_add_listener(input->touch, &touch_listener, input);
3138 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3139 wl_touch_destroy(input->touch);
3140 input->touch = NULL;
3145 seat_handle_name(void *data, struct wl_seat *seat,
3151 static const struct wl_seat_listener seat_listener = {
3152 seat_handle_capabilities,
3157 input_get_position(struct input *input, int32_t *x, int32_t *y)
3164 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3166 struct touch_point *tp;
3168 wl_list_for_each(tp, &input->touch_point_list, link) {
3181 input_get_display(struct input *input)
3183 return input->display;
3187 input_get_seat(struct input *input)
3193 input_get_modifiers(struct input *input)
3195 return input->modifiers;
3199 input_get_focus_widget(struct input *input)
3201 return input->focus_widget;
3205 struct wl_data_offer *offer;
3206 struct input *input;
3207 struct wl_array types;
3210 struct task io_task;
3218 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3220 struct data_offer *offer = data;
3223 p = wl_array_add(&offer->types, sizeof *p);
3227 static const struct wl_data_offer_listener data_offer_listener = {
3232 data_offer_destroy(struct data_offer *offer)
3237 if (offer->refcount == 0) {
3238 wl_data_offer_destroy(offer->offer);
3239 for (p = offer->types.data; *p; p++)
3241 wl_array_release(&offer->types);
3247 data_device_data_offer(void *data,
3248 struct wl_data_device *data_device,
3249 struct wl_data_offer *_offer)
3251 struct data_offer *offer;
3253 offer = xmalloc(sizeof *offer);
3255 wl_array_init(&offer->types);
3256 offer->refcount = 1;
3257 offer->input = data;
3258 offer->offer = _offer;
3259 wl_data_offer_add_listener(offer->offer,
3260 &data_offer_listener, offer);
3264 data_device_enter(void *data, struct wl_data_device *data_device,
3265 uint32_t serial, struct wl_surface *surface,
3266 wl_fixed_t x_w, wl_fixed_t y_w,
3267 struct wl_data_offer *offer)
3269 struct input *input = data;
3270 struct window *window;
3272 float x = wl_fixed_to_double(x_w);
3273 float y = wl_fixed_to_double(y_w);
3276 window = wl_surface_get_user_data(surface);
3277 input->drag_enter_serial = serial;
3278 input->drag_focus = window,
3282 if (!input->touch_grab)
3283 input->pointer_enter_serial = serial;
3286 input->drag_offer = wl_data_offer_get_user_data(offer);
3288 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3291 types_data = input->drag_offer->types.data;
3293 input->drag_offer = NULL;
3297 if (window->data_handler)
3298 window->data_handler(window, input, x, y, types_data,
3303 data_device_leave(void *data, struct wl_data_device *data_device)
3305 struct input *input = data;
3307 if (input->drag_offer) {
3308 data_offer_destroy(input->drag_offer);
3309 input->drag_offer = NULL;
3314 data_device_motion(void *data, struct wl_data_device *data_device,
3315 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3317 struct input *input = data;
3318 struct window *window = input->drag_focus;
3319 float x = wl_fixed_to_double(x_w);
3320 float y = wl_fixed_to_double(y_w);
3326 if (input->drag_offer)
3327 types_data = input->drag_offer->types.data;
3331 if (window->data_handler)
3332 window->data_handler(window, input, x, y, types_data,
3337 data_device_drop(void *data, struct wl_data_device *data_device)
3339 struct input *input = data;
3340 struct window *window = input->drag_focus;
3346 if (window->drop_handler)
3347 window->drop_handler(window, input,
3348 x, y, window->user_data);
3350 if (input->touch_grab)
3351 touch_ungrab(input);
3355 data_device_selection(void *data,
3356 struct wl_data_device *wl_data_device,
3357 struct wl_data_offer *offer)
3359 struct input *input = data;
3362 if (input->selection_offer)
3363 data_offer_destroy(input->selection_offer);
3366 input->selection_offer = wl_data_offer_get_user_data(offer);
3367 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3370 input->selection_offer = NULL;
3374 static const struct wl_data_device_listener data_device_listener = {
3375 data_device_data_offer,
3380 data_device_selection
3384 input_set_pointer_image_index(struct input *input, int index)
3386 struct wl_buffer *buffer;
3387 struct wl_cursor *cursor;
3388 struct wl_cursor_image *image;
3390 if (!input->pointer)
3393 cursor = input->display->cursors[input->current_cursor];
3397 if (index >= (int) cursor->image_count) {
3398 fprintf(stderr, "cursor index out of range\n");
3402 image = cursor->images[index];
3403 buffer = wl_cursor_image_get_buffer(image);
3407 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3408 wl_surface_damage(input->pointer_surface, 0, 0,
3409 image->width, image->height);
3410 wl_surface_commit(input->pointer_surface);
3411 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3412 input->pointer_surface,
3413 image->hotspot_x, image->hotspot_y);
3416 static const struct wl_callback_listener pointer_surface_listener;
3419 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3422 struct input *input = data;
3423 struct wl_cursor *cursor;
3427 assert(callback == input->cursor_frame_cb);
3428 wl_callback_destroy(callback);
3429 input->cursor_frame_cb = NULL;
3432 if (!input->pointer)
3435 if (input->current_cursor == CURSOR_BLANK) {
3436 wl_pointer_set_cursor(input->pointer,
3437 input->pointer_enter_serial,
3442 if (input->current_cursor == CURSOR_UNSET)
3444 cursor = input->display->cursors[input->current_cursor];
3448 /* FIXME We don't have the current time on the first call so we set
3449 * the animation start to the time of the first frame callback. */
3451 input->cursor_anim_start = 0;
3452 else if (input->cursor_anim_start == 0)
3453 input->cursor_anim_start = time;
3455 if (time == 0 || input->cursor_anim_start == 0)
3458 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3460 if (cursor->image_count > 1) {
3461 input->cursor_frame_cb =
3462 wl_surface_frame(input->pointer_surface);
3463 wl_callback_add_listener(input->cursor_frame_cb,
3464 &pointer_surface_listener, input);
3467 input_set_pointer_image_index(input, i);
3470 static const struct wl_callback_listener pointer_surface_listener = {
3471 pointer_surface_frame_callback
3475 input_set_pointer_image(struct input *input, int pointer)
3479 if (!input->pointer)
3482 if (input->pointer_enter_serial > input->cursor_serial)
3485 if (!force && pointer == input->current_cursor)
3488 input->current_cursor = pointer;
3489 input->cursor_serial = input->pointer_enter_serial;
3490 if (!input->cursor_frame_cb)
3491 pointer_surface_frame_callback(input, NULL, 0);
3493 /* The current frame callback may be stuck if, for instance,
3494 * the set cursor request was processed by the server after
3495 * this client lost the focus. In this case the cursor surface
3496 * might not be mapped and the frame callback wouldn't ever
3497 * complete. Send a set_cursor and attach to try to map the
3498 * cursor surface again so that the callback will finish */
3499 input_set_pointer_image_index(input, 0);
3503 struct wl_data_device *
3504 input_get_data_device(struct input *input)
3506 return input->data_device;
3510 input_set_selection(struct input *input,
3511 struct wl_data_source *source, uint32_t time)
3513 if (input->data_device)
3514 wl_data_device_set_selection(input->data_device, source, time);
3518 input_accept(struct input *input, const char *type)
3520 wl_data_offer_accept(input->drag_offer->offer,
3521 input->drag_enter_serial, type);
3525 offer_io_func(struct task *task, uint32_t events)
3527 struct data_offer *offer =
3528 container_of(task, struct data_offer, io_task);
3532 len = read(offer->fd, buffer, sizeof buffer);
3533 offer->func(buffer, len,
3534 offer->x, offer->y, offer->user_data);
3538 data_offer_destroy(offer);
3543 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3544 data_func_t func, void *user_data)
3548 if (pipe2(p, O_CLOEXEC) == -1)
3551 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3554 offer->io_task.run = offer_io_func;
3558 offer->user_data = user_data;
3560 display_watch_fd(offer->input->display,
3561 offer->fd, EPOLLIN, &offer->io_task);
3565 input_receive_drag_data(struct input *input, const char *mime_type,
3566 data_func_t func, void *data)
3568 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3569 input->drag_offer->x = input->drag_x;
3570 input->drag_offer->y = input->drag_y;
3574 input_receive_drag_data_to_fd(struct input *input,
3575 const char *mime_type, int fd)
3577 if (input->drag_offer)
3578 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3584 input_receive_selection_data(struct input *input, const char *mime_type,
3585 data_func_t func, void *data)
3589 if (input->selection_offer == NULL)
3592 for (p = input->selection_offer->types.data; *p; p++)
3593 if (strcmp(mime_type, *p) == 0)
3599 data_offer_receive_data(input->selection_offer,
3600 mime_type, func, data);
3605 input_receive_selection_data_to_fd(struct input *input,
3606 const char *mime_type, int fd)
3608 if (input->selection_offer)
3609 wl_data_offer_receive(input->selection_offer->offer,
3616 window_move(struct window *window, struct input *input, uint32_t serial)
3618 if (!window->xdg_surface)
3621 xdg_surface_move(window->xdg_surface, input->seat, serial);
3625 surface_set_synchronized(struct surface *surface)
3627 if (!surface->subsurface)
3630 if (surface->synchronized)
3633 wl_subsurface_set_sync(surface->subsurface);
3634 surface->synchronized = 1;
3638 surface_set_synchronized_default(struct surface *surface)
3640 if (!surface->subsurface)
3643 if (surface->synchronized == surface->synchronized_default)
3646 if (surface->synchronized_default)
3647 wl_subsurface_set_sync(surface->subsurface);
3649 wl_subsurface_set_desync(surface->subsurface);
3651 surface->synchronized = surface->synchronized_default;
3655 surface_resize(struct surface *surface)
3657 struct widget *widget = surface->widget;
3658 struct wl_compositor *compositor = widget->window->display->compositor;
3660 if (surface->input_region) {
3661 wl_region_destroy(surface->input_region);
3662 surface->input_region = NULL;
3665 if (surface->opaque_region)
3666 wl_region_destroy(surface->opaque_region);
3668 surface->opaque_region = wl_compositor_create_region(compositor);
3670 if (widget->resize_handler)
3671 widget->resize_handler(widget,
3672 widget->allocation.width,
3673 widget->allocation.height,
3676 if (surface->subsurface &&
3677 (surface->allocation.x != widget->allocation.x ||
3678 surface->allocation.y != widget->allocation.y)) {
3679 wl_subsurface_set_position(surface->subsurface,
3680 widget->allocation.x,
3681 widget->allocation.y);
3683 if (surface->allocation.width != widget->allocation.width ||
3684 surface->allocation.height != widget->allocation.height) {
3685 window_schedule_redraw(widget->window);
3687 surface->allocation = widget->allocation;
3690 wl_region_add(surface->opaque_region, 0, 0,
3691 widget->allocation.width,
3692 widget->allocation.height);
3696 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3699 * This hack should be removed, when EGL respects
3700 * eglSwapInterval(0).
3702 * If this window has sub-surfaces, especially a free-running
3703 * EGL-widget, we need to post the parent surface once with
3704 * all the old state to guarantee, that the EGL-widget will
3705 * receive its frame callback soon. Otherwise, a forced call
3706 * to eglSwapBuffers may end up blocking, waiting for a frame
3707 * event that will never come, because we will commit the parent
3708 * surface with all new state only after eglSwapBuffers returns.
3710 * This assumes, that:
3711 * 1. When the EGL widget's resize hook is called, it pauses.
3712 * 2. When the EGL widget's redraw hook is called, it forces a
3713 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3714 * In a single threaded application condition 1 is a no-op.
3716 * XXX: This should actually be after the surface_resize() calls,
3717 * but cannot, because then it would commit the incomplete state
3718 * accumulated from the widget resize hooks.
3720 if (window->subsurface_list.next != &window->main_surface->link ||
3721 window->subsurface_list.prev != &window->main_surface->link)
3722 wl_surface_commit(window->main_surface->surface);
3726 window_do_resize(struct window *window)
3728 struct surface *surface;
3730 widget_set_allocation(window->main_surface->widget,
3731 window->pending_allocation.x,
3732 window->pending_allocation.y,
3733 window->pending_allocation.width,
3734 window->pending_allocation.height);
3736 surface_resize(window->main_surface);
3738 /* The main surface is in the list, too. Main surface's
3739 * resize_handler is responsible for calling widget_set_allocation()
3740 * on all sub-surface root widgets, so they will be resized
3743 wl_list_for_each(surface, &window->subsurface_list, link) {
3744 if (surface == window->main_surface)
3747 surface_set_synchronized(surface);
3748 surface_resize(surface);
3751 if (!window->fullscreen && !window->maximized)
3752 window->saved_allocation = window->pending_allocation;
3756 idle_resize(struct window *window)
3758 window->resize_needed = 0;
3759 window->redraw_needed = 1;
3761 DBG("from %dx%d to %dx%d\n",
3762 window->main_surface->server_allocation.width,
3763 window->main_surface->server_allocation.height,
3764 window->pending_allocation.width,
3765 window->pending_allocation.height);
3767 hack_prevent_EGL_sub_surface_deadlock(window);
3769 window_do_resize(window);
3773 undo_resize(struct window *window)
3775 window->pending_allocation.width =
3776 window->main_surface->server_allocation.width;
3777 window->pending_allocation.height =
3778 window->main_surface->server_allocation.height;
3780 DBG("back to %dx%d\n",
3781 window->main_surface->server_allocation.width,
3782 window->main_surface->server_allocation.height);
3784 window_do_resize(window);
3786 if (window->pending_allocation.width == 0 &&
3787 window->pending_allocation.height == 0) {
3788 fprintf(stderr, "Error: Could not draw a surface, "
3789 "most likely due to insufficient disk space in "
3790 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3796 window_schedule_resize(struct window *window, int width, int height)
3798 /* We should probably get these numbers from the theme. */
3799 const int min_width = 200, min_height = 200;
3801 window->pending_allocation.x = 0;
3802 window->pending_allocation.y = 0;
3803 window->pending_allocation.width = width;
3804 window->pending_allocation.height = height;
3806 if (window->min_allocation.width == 0) {
3807 if (width < min_width && window->frame)
3808 window->min_allocation.width = min_width;
3810 window->min_allocation.width = width;
3811 if (height < min_height && window->frame)
3812 window->min_allocation.height = min_height;
3814 window->min_allocation.height = height;
3817 if (window->pending_allocation.width < window->min_allocation.width)
3818 window->pending_allocation.width = window->min_allocation.width;
3819 if (window->pending_allocation.height < window->min_allocation.height)
3820 window->pending_allocation.height = window->min_allocation.height;
3822 window->resize_needed = 1;
3823 window_schedule_redraw(window);
3827 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3829 window_schedule_resize(widget->window, width, height);
3833 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3834 int32_t width, int32_t height,
3835 struct wl_array *states, uint32_t serial)
3837 struct window *window = data;
3840 window->maximized = 0;
3841 window->fullscreen = 0;
3842 window->resizing = 0;
3843 window->focused = 0;
3845 wl_array_for_each(p, states) {
3846 uint32_t state = *p;
3848 case XDG_SURFACE_STATE_MAXIMIZED:
3849 window->maximized = 1;
3851 case XDG_SURFACE_STATE_FULLSCREEN:
3852 window->fullscreen = 1;
3854 case XDG_SURFACE_STATE_RESIZING:
3855 window->resizing = 1;
3857 case XDG_SURFACE_STATE_ACTIVATED:
3858 window->focused = 1;
3866 if (window->frame) {
3867 if (window->maximized) {
3868 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3870 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3873 if (window->focused) {
3874 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3876 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3880 if (width > 0 && height > 0) {
3881 window_schedule_resize(window, width, height);
3883 window_schedule_resize(window,
3884 window->saved_allocation.width,
3885 window->saved_allocation.height);
3888 xdg_surface_ack_configure(window->xdg_surface, serial);
3890 if (window->state_changed_handler)
3891 window->state_changed_handler(window, window->user_data);
3895 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3897 struct window *window = data;
3898 window_close(window);
3901 static const struct xdg_surface_listener xdg_surface_listener = {
3902 handle_surface_configure,
3903 handle_surface_delete,
3907 window_sync_parent(struct window *window)
3909 struct wl_surface *parent_surface;
3911 if (!window->xdg_surface)
3915 parent_surface = window->parent->main_surface->surface;
3917 parent_surface = NULL;
3919 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3923 window_sync_margin(struct window *window)
3927 if (!window->xdg_surface)
3933 margin = frame_get_shadow_margin(window->frame->frame);
3935 /* Shadow size is the same on every side. */
3936 xdg_surface_set_margin(window->xdg_surface,
3944 window_flush(struct window *window)
3946 struct surface *surface;
3948 if (!window->custom) {
3949 if (window->xdg_surface) {
3950 window_sync_parent(window);
3951 window_sync_margin(window);
3955 wl_list_for_each(surface, &window->subsurface_list, link) {
3956 if (surface == window->main_surface)
3959 surface_flush(surface);
3962 surface_flush(window->main_surface);
3966 menu_destroy(struct menu *menu)
3968 widget_destroy(menu->widget);
3969 window_destroy(menu->window);
3970 frame_destroy(menu->frame);
3975 window_get_allocation(struct window *window,
3976 struct rectangle *allocation)
3978 *allocation = window->main_surface->allocation;
3982 widget_redraw(struct widget *widget)
3984 struct widget *child;
3986 if (widget->redraw_handler)
3987 widget->redraw_handler(widget, widget->user_data);
3988 wl_list_for_each(child, &widget->child_list, link)
3989 widget_redraw(child);
3993 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3995 struct surface *surface = data;
3997 assert(callback == surface->frame_cb);
3998 DBG_OBJ(callback, "done\n");
3999 wl_callback_destroy(callback);
4000 surface->frame_cb = NULL;
4002 surface->last_time = time;
4004 if (surface->redraw_needed || surface->window->redraw_needed) {
4005 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4006 window_schedule_redraw_task(surface->window);
4010 static const struct wl_callback_listener listener = {
4015 surface_redraw(struct surface *surface)
4017 DBG_OBJ(surface->surface, "begin\n");
4019 if (!surface->window->redraw_needed && !surface->redraw_needed)
4022 /* Whole-window redraw forces a redraw even if the previous has
4023 * not yet hit the screen.
4025 if (surface->frame_cb) {
4026 if (!surface->window->redraw_needed)
4029 DBG_OBJ(surface->frame_cb, "cancelled\n");
4030 wl_callback_destroy(surface->frame_cb);
4033 if (surface->widget->use_cairo &&
4034 !widget_get_cairo_surface(surface->widget)) {
4035 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4039 surface->frame_cb = wl_surface_frame(surface->surface);
4040 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4041 DBG_OBJ(surface->frame_cb, "new\n");
4043 surface->redraw_needed = 0;
4044 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4045 widget_redraw(surface->widget);
4046 DBG_OBJ(surface->surface, "done\n");
4051 idle_redraw(struct task *task, uint32_t events)
4053 struct window *window = container_of(task, struct window, redraw_task);
4054 struct surface *surface;
4058 DBG(" --------- \n");
4060 wl_list_init(&window->redraw_task.link);
4061 window->redraw_task_scheduled = 0;
4063 if (window->resize_needed) {
4064 /* throttle resizing to the main surface display */
4065 if (window->main_surface->frame_cb) {
4066 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4070 idle_resize(window);
4074 if (surface_redraw(window->main_surface) < 0) {
4076 * Only main_surface failure will cause us to undo the resize.
4077 * If sub-surfaces fail, they will just be broken with old
4082 wl_list_for_each(surface, &window->subsurface_list, link) {
4083 if (surface == window->main_surface)
4086 surface_redraw(surface);
4090 window->redraw_needed = 0;
4091 window_flush(window);
4093 wl_list_for_each(surface, &window->subsurface_list, link)
4094 surface_set_synchronized_default(surface);
4096 if (resized && failed) {
4097 /* Restore widget tree to correspond to what is on screen. */
4098 undo_resize(window);
4103 window_schedule_redraw_task(struct window *window)
4105 if (!window->redraw_task_scheduled) {
4106 window->redraw_task.run = idle_redraw;
4107 display_defer(window->display, &window->redraw_task);
4108 window->redraw_task_scheduled = 1;
4113 window_schedule_redraw(struct window *window)
4115 struct surface *surface;
4117 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4119 wl_list_for_each(surface, &window->subsurface_list, link)
4120 surface->redraw_needed = 1;
4122 window_schedule_redraw_task(window);
4126 window_is_fullscreen(struct window *window)
4128 return window->fullscreen;
4132 window_set_fullscreen(struct window *window, int fullscreen)
4134 if (!window->xdg_surface)
4137 if (window->fullscreen == fullscreen)
4141 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4143 xdg_surface_unset_fullscreen(window->xdg_surface);
4147 window_is_maximized(struct window *window)
4149 return window->maximized;
4153 window_set_maximized(struct window *window, int maximized)
4155 if (!window->xdg_surface)
4158 if (window->maximized == maximized)
4162 xdg_surface_set_maximized(window->xdg_surface);
4164 xdg_surface_unset_maximized(window->xdg_surface);
4168 window_is_resizing(struct window *window)
4170 return window->resizing;
4174 window_set_minimized(struct window *window)
4176 if (!window->xdg_surface)
4179 xdg_surface_set_minimized(window->xdg_surface);
4183 window_set_user_data(struct window *window, void *data)
4185 window->user_data = data;
4189 window_get_user_data(struct window *window)
4191 return window->user_data;
4195 window_set_key_handler(struct window *window,
4196 window_key_handler_t handler)
4198 window->key_handler = handler;
4202 window_set_keyboard_focus_handler(struct window *window,
4203 window_keyboard_focus_handler_t handler)
4205 window->keyboard_focus_handler = handler;
4209 window_set_data_handler(struct window *window, window_data_handler_t handler)
4211 window->data_handler = handler;
4215 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4217 window->drop_handler = handler;
4221 window_set_close_handler(struct window *window,
4222 window_close_handler_t handler)
4224 window->close_handler = handler;
4228 window_set_fullscreen_handler(struct window *window,
4229 window_fullscreen_handler_t handler)
4231 window->fullscreen_handler = handler;
4235 window_set_output_handler(struct window *window,
4236 window_output_handler_t handler)
4238 window->output_handler = handler;
4242 window_set_state_changed_handler(struct window *window,
4243 window_state_changed_handler_t handler)
4245 window->state_changed_handler = handler;
4249 window_set_title(struct window *window, const char *title)
4251 free(window->title);
4252 window->title = strdup(title);
4253 if (window->frame) {
4254 frame_set_title(window->frame->frame, title);
4255 widget_schedule_redraw(window->frame->widget);
4257 if (window->xdg_surface)
4258 xdg_surface_set_title(window->xdg_surface, title);
4262 window_get_title(struct window *window)
4264 return window->title;
4268 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4270 struct text_cursor_position *text_cursor_position =
4271 window->display->text_cursor_position;
4273 if (!text_cursor_position)
4276 text_cursor_position_notify(text_cursor_position,
4277 window->main_surface->surface,
4278 wl_fixed_from_int(x),
4279 wl_fixed_from_int(y));
4283 window_damage(struct window *window, int32_t x, int32_t y,
4284 int32_t width, int32_t height)
4286 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4290 surface_enter(void *data,
4291 struct wl_surface *wl_surface, struct wl_output *wl_output)
4293 struct window *window = data;
4294 struct output *output;
4295 struct output *output_found = NULL;
4296 struct window_output *window_output;
4298 wl_list_for_each(output, &window->display->output_list, link) {
4299 if (output->output == wl_output) {
4300 output_found = output;
4308 window_output = xmalloc(sizeof *window_output);
4309 window_output->output = output_found;
4311 wl_list_insert (&window->window_output_list, &window_output->link);
4313 if (window->output_handler)
4314 window->output_handler(window, output_found, 1,
4319 surface_leave(void *data,
4320 struct wl_surface *wl_surface, struct wl_output *output)
4322 struct window *window = data;
4323 struct window_output *window_output;
4324 struct window_output *window_output_found = NULL;
4326 wl_list_for_each(window_output, &window->window_output_list, link) {
4327 if (window_output->output->output == output) {
4328 window_output_found = window_output;
4333 if (window_output_found) {
4334 wl_list_remove(&window_output_found->link);
4336 if (window->output_handler)
4337 window->output_handler(window, window_output->output,
4338 0, window->user_data);
4340 free(window_output_found);
4344 static const struct wl_surface_listener surface_listener = {
4349 static struct surface *
4350 surface_create(struct window *window)
4352 struct display *display = window->display;
4353 struct surface *surface;
4355 surface = xmalloc(sizeof *surface);
4356 memset(surface, 0, sizeof *surface);
4360 surface->window = window;
4361 surface->surface = wl_compositor_create_surface(display->compositor);
4362 surface->buffer_scale = 1;
4363 wl_surface_add_listener(surface->surface, &surface_listener, window);
4365 wl_list_insert(&window->subsurface_list, &surface->link);
4370 static enum window_buffer_type
4371 get_preferred_buffer_type(struct display *display)
4373 #ifdef HAVE_CAIRO_EGL
4374 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4375 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4378 return WINDOW_BUFFER_TYPE_SHM;
4381 static struct window *
4382 window_create_internal(struct display *display, int custom)
4384 struct window *window;
4385 struct surface *surface;
4387 window = xzalloc(sizeof *window);
4388 wl_list_init(&window->subsurface_list);
4389 window->display = display;
4391 surface = surface_create(window);
4392 window->main_surface = surface;
4394 assert(custom || display->xdg_shell);
4396 window->custom = custom;
4397 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4399 surface->buffer_type = get_preferred_buffer_type(display);
4401 wl_surface_set_user_data(surface->surface, window);
4402 wl_list_insert(display->window_list.prev, &window->link);
4403 wl_list_init(&window->redraw_task.link);
4405 wl_list_init (&window->window_output_list);
4411 window_create(struct display *display)
4413 struct window *window;
4415 window = window_create_internal(display, 0);
4417 window->xdg_surface =
4418 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4419 window->main_surface->surface);
4420 fail_on_null(window->xdg_surface);
4422 xdg_surface_set_user_data(window->xdg_surface, window);
4423 xdg_surface_add_listener(window->xdg_surface,
4424 &xdg_surface_listener, window);
4430 window_create_custom(struct display *display)
4432 return window_create_internal(display, 1);
4436 window_set_parent(struct window *window,
4437 struct window *parent_window)
4439 window->parent = parent_window;
4440 window_sync_parent(window);
4444 window_get_parent(struct window *window)
4446 return window->parent;
4450 menu_set_item(struct menu *menu, int sy)
4452 int32_t x, y, width, height;
4455 frame_interior(menu->frame, &x, &y, &width, &height);
4456 next = (sy - y) / 20;
4457 if (menu->current != next) {
4458 menu->current = next;
4459 widget_schedule_redraw(menu->widget);
4464 menu_motion_handler(struct widget *widget,
4465 struct input *input, uint32_t time,
4466 float x, float y, void *data)
4468 struct menu *menu = data;
4470 if (widget == menu->widget)
4471 menu_set_item(data, y);
4473 return CURSOR_LEFT_PTR;
4477 menu_enter_handler(struct widget *widget,
4478 struct input *input, float x, float y, void *data)
4480 struct menu *menu = data;
4482 if (widget == menu->widget)
4483 menu_set_item(data, y);
4485 return CURSOR_LEFT_PTR;
4489 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4491 struct menu *menu = data;
4493 if (widget == menu->widget)
4494 menu_set_item(data, -200);
4498 menu_button_handler(struct widget *widget,
4499 struct input *input, uint32_t time,
4500 uint32_t button, enum wl_pointer_button_state state,
4504 struct menu *menu = data;
4506 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4507 (menu->release_count > 0 || time - menu->time > 500)) {
4508 /* Either relase after press-drag-release or
4509 * click-motion-click. */
4510 menu->func(menu->parent, input,
4511 menu->current, menu->parent->user_data);
4512 input_ungrab(input);
4514 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4515 menu->release_count++;
4520 menu_touch_up_handler(struct widget *widget,
4521 struct input *input,
4527 struct menu *menu = data;
4529 input_ungrab(input);
4534 menu_redraw_handler(struct widget *widget, void *data)
4537 struct menu *menu = data;
4538 int32_t x, y, width, height, i;
4540 cr = widget_cairo_create(widget);
4542 frame_repaint(menu->frame, cr);
4543 frame_interior(menu->frame, &x, &y, &width, &height);
4545 theme_set_background_source(menu->window->display->theme,
4546 cr, THEME_FRAME_ACTIVE);
4547 cairo_rectangle(cr, x, y, width, height);
4550 cairo_select_font_face(cr, "sans",
4551 CAIRO_FONT_SLANT_NORMAL,
4552 CAIRO_FONT_WEIGHT_NORMAL);
4553 cairo_set_font_size(cr, 12);
4555 for (i = 0; i < menu->count; i++) {
4556 if (i == menu->current) {
4557 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4558 cairo_rectangle(cr, x, y + i * 20, width, 20);
4560 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4561 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4562 cairo_show_text(cr, menu->entries[i]);
4564 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4565 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4566 cairo_show_text(cr, menu->entries[i]);
4574 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4576 struct window *window = data;
4577 struct menu *menu = window->main_surface->widget->user_data;
4579 input_ungrab(menu->input);
4583 static const struct xdg_popup_listener xdg_popup_listener = {
4584 handle_popup_popup_done,
4588 window_show_menu(struct display *display,
4589 struct input *input, uint32_t time, struct window *parent,
4590 int32_t x, int32_t y,
4591 menu_func_t func, const char **entries, int count)
4593 struct window *window;
4597 menu = malloc(sizeof *menu);
4601 window = window_create_internal(parent->display, 0);
4607 menu->window = window;
4608 menu->parent = parent;
4609 menu->widget = window_add_widget(menu->window, menu);
4610 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4611 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4612 menu->frame = frame_create(window->display->theme, 0, 0,
4613 FRAME_BUTTON_NONE, NULL);
4614 fail_on_null(menu->frame);
4615 menu->entries = entries;
4616 menu->count = count;
4617 menu->release_count = 0;
4621 menu->input = input;
4625 input_ungrab(input);
4627 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4628 widget_set_enter_handler(menu->widget, menu_enter_handler);
4629 widget_set_leave_handler(menu->widget, menu_leave_handler);
4630 widget_set_motion_handler(menu->widget, menu_motion_handler);
4631 widget_set_button_handler(menu->widget, menu_button_handler);
4632 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4634 input_grab(input, menu->widget, 0);
4635 frame_resize_inside(menu->frame, 200, count * 20);
4636 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4637 window_schedule_resize(window, frame_width(menu->frame),
4638 frame_height(menu->frame));
4640 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4642 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4643 window->main_surface->surface,
4644 parent->main_surface->surface,
4646 display_get_serial(window->display),
4650 fail_on_null(window->xdg_popup);
4652 xdg_popup_set_user_data(window->xdg_popup, window);
4653 xdg_popup_add_listener(window->xdg_popup,
4654 &xdg_popup_listener, window);
4658 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4660 window->main_surface->buffer_type = type;
4663 enum window_buffer_type
4664 window_get_buffer_type(struct window *window)
4666 return window->main_surface->buffer_type;
4670 window_set_preferred_format(struct window *window,
4671 enum preferred_format format)
4673 window->preferred_format = format;
4677 window_add_subsurface(struct window *window, void *data,
4678 enum subsurface_mode default_mode)
4680 struct widget *widget;
4681 struct surface *surface;
4682 struct wl_surface *parent;
4683 struct wl_subcompositor *subcompo = window->display->subcompositor;
4685 surface = surface_create(window);
4686 surface->buffer_type = window_get_buffer_type(window);
4687 widget = widget_create(window, surface, data);
4688 wl_list_init(&widget->link);
4689 surface->widget = widget;
4691 parent = window->main_surface->surface;
4692 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4695 surface->synchronized = 1;
4697 switch (default_mode) {
4698 case SUBSURFACE_SYNCHRONIZED:
4699 surface->synchronized_default = 1;
4701 case SUBSURFACE_DESYNCHRONIZED:
4702 surface->synchronized_default = 0;
4705 assert(!"bad enum subsurface_mode");
4708 window->resize_needed = 1;
4709 window_schedule_redraw(window);
4715 display_handle_geometry(void *data,
4716 struct wl_output *wl_output,
4719 int physical_height,
4725 struct output *output = data;
4727 output->allocation.x = x;
4728 output->allocation.y = y;
4729 output->transform = transform;
4733 output->make = strdup(make);
4736 free(output->model);
4737 output->model = strdup(model);
4741 display_handle_done(void *data,
4742 struct wl_output *wl_output)
4747 display_handle_scale(void *data,
4748 struct wl_output *wl_output,
4751 struct output *output = data;
4753 output->scale = scale;
4757 display_handle_mode(void *data,
4758 struct wl_output *wl_output,
4764 struct output *output = data;
4765 struct display *display = output->display;
4767 if (flags & WL_OUTPUT_MODE_CURRENT) {
4768 output->allocation.width = width;
4769 output->allocation.height = height;
4770 if (display->output_configure_handler)
4771 (*display->output_configure_handler)(
4772 output, display->user_data);
4776 static const struct wl_output_listener output_listener = {
4777 display_handle_geometry,
4778 display_handle_mode,
4779 display_handle_done,
4780 display_handle_scale
4784 display_add_output(struct display *d, uint32_t id)
4786 struct output *output;
4788 output = xzalloc(sizeof *output);
4789 output->display = d;
4792 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4793 output->server_output_id = id;
4794 wl_list_insert(d->output_list.prev, &output->link);
4796 wl_output_add_listener(output->output, &output_listener, output);
4800 output_destroy(struct output *output)
4802 if (output->destroy_handler)
4803 (*output->destroy_handler)(output, output->user_data);
4805 wl_output_destroy(output->output);
4806 wl_list_remove(&output->link);
4811 display_destroy_output(struct display *d, uint32_t id)
4813 struct output *output;
4815 wl_list_for_each(output, &d->output_list, link) {
4816 if (output->server_output_id == id) {
4817 output_destroy(output);
4824 display_set_global_handler(struct display *display,
4825 display_global_handler_t handler)
4827 struct global *global;
4829 display->global_handler = handler;
4833 wl_list_for_each(global, &display->global_list, link)
4834 display->global_handler(display,
4835 global->name, global->interface,
4836 global->version, display->user_data);
4840 display_set_global_handler_remove(struct display *display,
4841 display_global_handler_t remove_handler)
4843 display->global_handler_remove = remove_handler;
4844 if (!remove_handler)
4849 display_set_output_configure_handler(struct display *display,
4850 display_output_handler_t handler)
4852 struct output *output;
4854 display->output_configure_handler = handler;
4858 wl_list_for_each(output, &display->output_list, link) {
4859 if (output->allocation.width == 0 &&
4860 output->allocation.height == 0)
4863 (*display->output_configure_handler)(output,
4864 display->user_data);
4869 output_set_user_data(struct output *output, void *data)
4871 output->user_data = data;
4875 output_get_user_data(struct output *output)
4877 return output->user_data;
4881 output_set_destroy_handler(struct output *output,
4882 display_output_handler_t handler)
4884 output->destroy_handler = handler;
4885 /* FIXME: implement this, once we have way to remove outputs */
4889 output_get_allocation(struct output *output, struct rectangle *base)
4891 struct rectangle allocation = output->allocation;
4893 switch (output->transform) {
4894 case WL_OUTPUT_TRANSFORM_90:
4895 case WL_OUTPUT_TRANSFORM_270:
4896 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4897 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4898 /* Swap width and height */
4899 allocation.width = output->allocation.height;
4900 allocation.height = output->allocation.width;
4908 output_get_wl_output(struct output *output)
4910 return output->output;
4913 enum wl_output_transform
4914 output_get_transform(struct output *output)
4916 return output->transform;
4920 output_get_scale(struct output *output)
4922 return output->scale;
4926 output_get_make(struct output *output)
4928 return output->make;
4932 output_get_model(struct output *output)
4934 return output->model;
4938 fini_xkb(struct input *input)
4940 xkb_state_unref(input->xkb.state);
4941 xkb_map_unref(input->xkb.keymap);
4944 #define MIN(a,b) ((a) < (b) ? a : b)
4947 display_add_input(struct display *d, uint32_t id)
4949 struct input *input;
4951 input = xzalloc(sizeof *input);
4953 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4954 MIN(d->seat_version, 3));
4955 input->touch_focus = NULL;
4956 input->pointer_focus = NULL;
4957 input->keyboard_focus = NULL;
4958 wl_list_init(&input->touch_point_list);
4959 wl_list_insert(d->input_list.prev, &input->link);
4961 wl_seat_add_listener(input->seat, &seat_listener, input);
4962 wl_seat_set_user_data(input->seat, input);
4964 if (d->data_device_manager) {
4965 input->data_device =
4966 wl_data_device_manager_get_data_device(d->data_device_manager,
4968 wl_data_device_add_listener(input->data_device,
4969 &data_device_listener,
4973 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4975 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4976 TFD_CLOEXEC | TFD_NONBLOCK);
4977 input->repeat_task.run = keyboard_repeat_func;
4978 display_watch_fd(d, input->repeat_timer_fd,
4979 EPOLLIN, &input->repeat_task);
4983 input_destroy(struct input *input)
4985 input_remove_keyboard_focus(input);
4986 input_remove_pointer_focus(input);
4988 if (input->drag_offer)
4989 data_offer_destroy(input->drag_offer);
4991 if (input->selection_offer)
4992 data_offer_destroy(input->selection_offer);
4994 if (input->data_device)
4995 wl_data_device_destroy(input->data_device);
4997 if (input->display->seat_version >= 3) {
4999 wl_pointer_release(input->pointer);
5000 if (input->keyboard)
5001 wl_keyboard_release(input->keyboard);
5006 wl_surface_destroy(input->pointer_surface);
5008 wl_list_remove(&input->link);
5009 wl_seat_destroy(input->seat);
5010 close(input->repeat_timer_fd);
5015 init_workspace_manager(struct display *d, uint32_t id)
5017 d->workspace_manager =
5018 wl_registry_bind(d->registry, id,
5019 &workspace_manager_interface, 1);
5020 if (d->workspace_manager != NULL)
5021 workspace_manager_add_listener(d->workspace_manager,
5022 &workspace_manager_listener,
5027 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5029 struct display *d = data;
5031 if (format == WL_SHM_FORMAT_RGB565)
5035 struct wl_shm_listener shm_listener = {
5040 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5042 xdg_shell_pong(shell, serial);
5045 static const struct xdg_shell_listener xdg_shell_listener = {
5049 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
5050 #ifdef static_assert
5051 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5052 "Interface version doesn't match implementation version");
5056 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5057 const char *interface, uint32_t version)
5059 struct display *d = data;
5060 struct global *global;
5062 global = xmalloc(sizeof *global);
5064 global->interface = strdup(interface);
5065 global->version = version;
5066 wl_list_insert(d->global_list.prev, &global->link);
5068 if (strcmp(interface, "wl_compositor") == 0) {
5069 d->compositor = wl_registry_bind(registry, id,
5070 &wl_compositor_interface, 3);
5071 } else if (strcmp(interface, "wl_output") == 0) {
5072 display_add_output(d, id);
5073 } else if (strcmp(interface, "wl_seat") == 0) {
5074 d->seat_version = version;
5075 display_add_input(d, id);
5076 } else if (strcmp(interface, "wl_shm") == 0) {
5077 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5078 wl_shm_add_listener(d->shm, &shm_listener, d);
5079 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5080 d->data_device_manager =
5081 wl_registry_bind(registry, id,
5082 &wl_data_device_manager_interface, 1);
5083 } else if (strcmp(interface, "xdg_shell") == 0) {
5084 d->xdg_shell = wl_registry_bind(registry, id,
5085 &xdg_shell_interface, 1);
5086 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5087 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5088 } else if (strcmp(interface, "text_cursor_position") == 0) {
5089 d->text_cursor_position =
5090 wl_registry_bind(registry, id,
5091 &text_cursor_position_interface, 1);
5092 } else if (strcmp(interface, "workspace_manager") == 0) {
5093 init_workspace_manager(d, id);
5094 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5096 wl_registry_bind(registry, id,
5097 &wl_subcompositor_interface, 1);
5100 if (d->global_handler)
5101 d->global_handler(d, id, interface, version, d->user_data);
5105 registry_handle_global_remove(void *data, struct wl_registry *registry,
5108 struct display *d = data;
5109 struct global *global;
5112 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5113 if (global->name != name)
5116 if (strcmp(global->interface, "wl_output") == 0)
5117 display_destroy_output(d, name);
5119 /* XXX: Should destroy remaining bound globals */
5121 if (d->global_handler_remove)
5122 d->global_handler_remove(d, name, global->interface,
5123 global->version, d->user_data);
5125 wl_list_remove(&global->link);
5126 free(global->interface);
5132 display_bind(struct display *display, uint32_t name,
5133 const struct wl_interface *interface, uint32_t version)
5135 return wl_registry_bind(display->registry, name, interface, version);
5138 static const struct wl_registry_listener registry_listener = {
5139 registry_handle_global,
5140 registry_handle_global_remove
5143 #ifdef HAVE_CAIRO_EGL
5145 init_egl(struct display *d)
5147 EGLint major, minor;
5150 #ifdef USE_CAIRO_GLESV2
5151 # define GL_BIT EGL_OPENGL_ES2_BIT
5153 # define GL_BIT EGL_OPENGL_BIT
5156 static const EGLint argb_cfg_attribs[] = {
5157 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5163 EGL_RENDERABLE_TYPE, GL_BIT,
5167 #ifdef USE_CAIRO_GLESV2
5168 static const EGLint context_attribs[] = {
5169 EGL_CONTEXT_CLIENT_VERSION, 2,
5172 EGLint api = EGL_OPENGL_ES_API;
5174 EGLint *context_attribs = NULL;
5175 EGLint api = EGL_OPENGL_API;
5178 d->dpy = eglGetDisplay(d->display);
5179 if (!eglInitialize(d->dpy, &major, &minor)) {
5180 fprintf(stderr, "failed to initialize EGL\n");
5184 if (!eglBindAPI(api)) {
5185 fprintf(stderr, "failed to bind EGL client API\n");
5189 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5190 &d->argb_config, 1, &n) || n != 1) {
5191 fprintf(stderr, "failed to choose argb EGL config\n");
5195 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5196 EGL_NO_CONTEXT, context_attribs);
5197 if (d->argb_ctx == NULL) {
5198 fprintf(stderr, "failed to create EGL context\n");
5202 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5203 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5204 fprintf(stderr, "failed to get cairo EGL argb device\n");
5212 fini_egl(struct display *display)
5214 cairo_device_destroy(display->argb_device);
5216 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5219 eglTerminate(display->dpy);
5225 init_dummy_surface(struct display *display)
5230 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5232 display->dummy_surface =
5233 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5235 display->dummy_surface_data = data;
5239 handle_display_data(struct task *task, uint32_t events)
5241 struct display *display =
5242 container_of(task, struct display, display_task);
5243 struct epoll_event ep;
5246 display->display_fd_events = events;
5248 if (events & EPOLLERR || events & EPOLLHUP) {
5249 display_exit(display);
5253 if (events & EPOLLIN) {
5254 ret = wl_display_dispatch(display->display);
5256 display_exit(display);
5261 if (events & EPOLLOUT) {
5262 ret = wl_display_flush(display->display);
5264 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5265 ep.data.ptr = &display->display_task;
5266 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5267 display->display_fd, &ep);
5268 } else if (ret == -1 && errno != EAGAIN) {
5269 display_exit(display);
5276 log_handler(const char *format, va_list args)
5278 vfprintf(stderr, format, args);
5282 display_create(int *argc, char *argv[])
5286 wl_log_set_handler_client(log_handler);
5288 d = zalloc(sizeof *d);
5292 d->display = wl_display_connect(NULL);
5293 if (d->display == NULL) {
5294 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5299 d->xkb_context = xkb_context_new(0);
5300 if (d->xkb_context == NULL) {
5301 fprintf(stderr, "Failed to create XKB context\n");
5306 d->epoll_fd = os_epoll_create_cloexec();
5307 d->display_fd = wl_display_get_fd(d->display);
5308 d->display_task.run = handle_display_data;
5309 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5312 wl_list_init(&d->deferred_list);
5313 wl_list_init(&d->input_list);
5314 wl_list_init(&d->output_list);
5315 wl_list_init(&d->global_list);
5318 d->workspace_count = 1;
5320 d->registry = wl_display_get_registry(d->display);
5321 wl_registry_add_listener(d->registry, ®istry_listener, d);
5323 if (wl_display_dispatch(d->display) < 0) {
5324 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5328 #ifdef HAVE_CAIRO_EGL
5329 if (init_egl(d) < 0)
5330 fprintf(stderr, "EGL does not seem to work, "
5331 "falling back to software rendering and wl_shm.\n");
5336 d->theme = theme_create();
5338 wl_list_init(&d->window_list);
5340 init_dummy_surface(d);
5346 display_destroy_outputs(struct display *display)
5349 struct output *output;
5351 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5352 output_destroy(output);
5356 display_destroy_inputs(struct display *display)
5359 struct input *input;
5361 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5362 input_destroy(input);
5366 display_destroy(struct display *display)
5368 if (!wl_list_empty(&display->window_list))
5369 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5370 wl_list_length(&display->window_list));
5372 if (!wl_list_empty(&display->deferred_list))
5373 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5375 cairo_surface_destroy(display->dummy_surface);
5376 free(display->dummy_surface_data);
5378 display_destroy_outputs(display);
5379 display_destroy_inputs(display);
5381 xkb_context_unref(display->xkb_context);
5383 theme_destroy(display->theme);
5384 destroy_cursors(display);
5386 #ifdef HAVE_CAIRO_EGL
5387 if (display->argb_device)
5391 if (display->subcompositor)
5392 wl_subcompositor_destroy(display->subcompositor);
5394 if (display->xdg_shell)
5395 xdg_shell_destroy(display->xdg_shell);
5398 wl_shm_destroy(display->shm);
5400 if (display->data_device_manager)
5401 wl_data_device_manager_destroy(display->data_device_manager);
5403 wl_compositor_destroy(display->compositor);
5404 wl_registry_destroy(display->registry);
5406 close(display->epoll_fd);
5408 if (!(display->display_fd_events & EPOLLERR) &&
5409 !(display->display_fd_events & EPOLLHUP))
5410 wl_display_flush(display->display);
5412 wl_display_disconnect(display->display);
5417 display_set_user_data(struct display *display, void *data)
5419 display->user_data = data;
5423 display_get_user_data(struct display *display)
5425 return display->user_data;
5429 display_get_display(struct display *display)
5431 return display->display;
5435 display_has_subcompositor(struct display *display)
5437 if (display->subcompositor)
5440 wl_display_roundtrip(display->display);
5442 return display->subcompositor != NULL;
5446 display_get_cairo_device(struct display *display)
5448 return display->argb_device;
5452 display_get_output(struct display *display)
5454 return container_of(display->output_list.next, struct output, link);
5457 struct wl_compositor *
5458 display_get_compositor(struct display *display)
5460 return display->compositor;
5464 display_get_serial(struct display *display)
5466 return display->serial;
5470 display_get_egl_display(struct display *d)
5475 struct wl_data_source *
5476 display_create_data_source(struct display *display)
5478 if (display->data_device_manager)
5479 return wl_data_device_manager_create_data_source(display->data_device_manager);
5485 display_get_argb_egl_config(struct display *d)
5487 return d->argb_config;
5491 display_acquire_window_surface(struct display *display,
5492 struct window *window,
5495 struct surface *surface = window->main_surface;
5497 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5500 widget_get_cairo_surface(window->main_surface->widget);
5501 return surface->toysurface->acquire(surface->toysurface, ctx);
5505 display_release_window_surface(struct display *display,
5506 struct window *window)
5508 struct surface *surface = window->main_surface;
5510 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5513 surface->toysurface->release(surface->toysurface);
5517 display_defer(struct display *display, struct task *task)
5519 wl_list_insert(&display->deferred_list, &task->link);
5523 display_watch_fd(struct display *display,
5524 int fd, uint32_t events, struct task *task)
5526 struct epoll_event ep;
5530 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5534 display_unwatch_fd(struct display *display, int fd)
5536 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5540 display_run(struct display *display)
5543 struct epoll_event ep[16];
5546 display->running = 1;
5548 while (!wl_list_empty(&display->deferred_list)) {
5549 task = container_of(display->deferred_list.prev,
5551 wl_list_remove(&task->link);
5555 wl_display_dispatch_pending(display->display);
5557 if (!display->running)
5560 ret = wl_display_flush(display->display);
5561 if (ret < 0 && errno == EAGAIN) {
5563 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5564 ep[0].data.ptr = &display->display_task;
5566 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5567 display->display_fd, &ep[0]);
5568 } else if (ret < 0) {
5572 count = epoll_wait(display->epoll_fd,
5573 ep, ARRAY_LENGTH(ep), -1);
5574 for (i = 0; i < count; i++) {
5575 task = ep[i].data.ptr;
5576 task->run(task, ep[i].events);
5582 display_exit(struct display *display)
5584 display->running = 0;
5588 keysym_modifiers_add(struct wl_array *modifiers_map,
5591 size_t len = strlen(name) + 1;
5594 p = wl_array_add(modifiers_map, len);
5599 strncpy(p, name, len);
5602 static xkb_mod_index_t
5603 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5606 xkb_mod_index_t index = 0;
5607 char *p = modifiers_map->data;
5609 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5610 if (strcmp(p, name) == 0)
5617 return XKB_MOD_INVALID;
5621 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5624 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5626 if (index == XKB_MOD_INVALID)
5627 return XKB_MOD_INVALID;
5633 fail_on_null(void *p)
5636 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5646 return fail_on_null(malloc(s));
5652 return fail_on_null(zalloc(s));
5656 xstrdup(const char *s)
5658 return fail_on_null(strdup(s));
5662 xrealloc(char *p, size_t s)
5664 return fail_on_null(realloc(p, s));