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 int next_attach_serial;
235 enum preferred_format preferred_format;
237 window_key_handler_t key_handler;
238 window_keyboard_focus_handler_t keyboard_focus_handler;
239 window_data_handler_t data_handler;
240 window_drop_handler_t drop_handler;
241 window_close_handler_t close_handler;
242 window_fullscreen_handler_t fullscreen_handler;
243 window_output_handler_t output_handler;
245 struct surface *main_surface;
246 struct xdg_surface *xdg_surface;
247 struct xdg_popup *xdg_popup;
249 struct window *parent;
251 struct window_frame *frame;
253 /* struct surface::link, contains also main_surface */
254 struct wl_list subsurface_list;
261 struct window *window;
262 struct surface *surface;
263 struct tooltip *tooltip;
264 struct wl_list child_list;
266 struct rectangle allocation;
267 widget_resize_handler_t resize_handler;
268 widget_redraw_handler_t redraw_handler;
269 widget_enter_handler_t enter_handler;
270 widget_leave_handler_t leave_handler;
271 widget_motion_handler_t motion_handler;
272 widget_button_handler_t button_handler;
273 widget_touch_down_handler_t touch_down_handler;
274 widget_touch_up_handler_t touch_up_handler;
275 widget_touch_motion_handler_t touch_motion_handler;
276 widget_touch_frame_handler_t touch_frame_handler;
277 widget_touch_cancel_handler_t touch_cancel_handler;
278 widget_axis_handler_t axis_handler;
283 /* If this is set to false then no cairo surface will be
284 * created before redrawing the surface. This is useful if the
285 * redraw handler is going to do completely custom rendering
286 * such as using EGL directly */
293 struct widget *widget;
298 struct display *display;
299 struct wl_seat *seat;
300 struct wl_pointer *pointer;
301 struct wl_keyboard *keyboard;
302 struct wl_touch *touch;
303 struct wl_list touch_point_list;
304 struct window *pointer_focus;
305 struct window *keyboard_focus;
306 struct window *touch_focus;
308 uint32_t cursor_anim_start;
309 struct wl_callback *cursor_frame_cb;
310 struct wl_surface *pointer_surface;
312 uint32_t pointer_enter_serial;
313 uint32_t cursor_serial;
317 struct widget *focus_widget;
319 uint32_t grab_button;
321 struct wl_data_device *data_device;
322 struct data_offer *drag_offer;
323 struct data_offer *selection_offer;
325 int32_t touch_grab_id;
326 float drag_x, drag_y;
327 struct window *drag_focus;
328 uint32_t drag_enter_serial;
331 struct xkb_keymap *keymap;
332 struct xkb_state *state;
333 xkb_mod_mask_t control_mask;
334 xkb_mod_mask_t alt_mask;
335 xkb_mod_mask_t shift_mask;
338 struct task repeat_task;
342 uint32_t repeat_time;
346 struct display *display;
347 struct wl_output *output;
348 uint32_t server_output_id;
349 struct rectangle allocation;
356 display_output_handler_t destroy_handler;
360 struct window_frame {
361 struct widget *widget;
362 struct widget *child;
367 struct window *window;
368 struct window *parent;
369 struct widget *widget;
372 const char **entries;
381 struct widget *parent;
382 struct widget *widget;
384 struct task tooltip_task;
390 struct wl_shm_pool *pool;
397 CURSOR_DEFAULT = 100,
401 static const cairo_user_data_key_t shm_surface_data_key;
408 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
409 __attribute__ ((format (printf, 4, 5)));
412 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
417 gettimeofday(&tv, NULL);
418 fprintf(stderr, "%8ld.%03ld ",
419 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
422 fprintf(stderr, "%s@%d ",
423 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
425 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
426 fprintf(stderr, "%s ", func);
429 vfprintf(stderr, fmt, ap);
433 #define DBG(fmt, ...) \
434 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
436 #define DBG_OBJ(obj, fmt, ...) \
437 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
441 #define DBG(...) do {} while (0)
442 #define DBG_OBJ(...) do {} while (0)
447 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
451 switch (buffer_transform) {
452 case WL_OUTPUT_TRANSFORM_90:
453 case WL_OUTPUT_TRANSFORM_270:
454 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
455 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
464 *width *= buffer_scale;
465 *height *= buffer_scale;
469 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
473 switch (buffer_transform) {
474 case WL_OUTPUT_TRANSFORM_90:
475 case WL_OUTPUT_TRANSFORM_270:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
486 *width /= buffer_scale;
487 *height /= buffer_scale;
490 #ifdef HAVE_CAIRO_EGL
492 struct egl_window_surface {
493 struct toysurface base;
494 cairo_surface_t *cairo_surface;
495 struct display *display;
496 struct wl_surface *surface;
497 struct wl_egl_window *egl_window;
498 EGLSurface egl_surface;
501 static struct egl_window_surface *
502 to_egl_window_surface(struct toysurface *base)
504 return container_of(base, struct egl_window_surface, base);
507 static cairo_surface_t *
508 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
509 int32_t width, int32_t height, uint32_t flags,
510 enum wl_output_transform buffer_transform, int32_t buffer_scale)
512 struct egl_window_surface *surface = to_egl_window_surface(base);
514 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
516 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
517 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
519 return cairo_surface_reference(surface->cairo_surface);
523 egl_window_surface_swap(struct toysurface *base,
524 enum wl_output_transform buffer_transform, int32_t buffer_scale,
525 struct rectangle *server_allocation)
527 struct egl_window_surface *surface = to_egl_window_surface(base);
529 cairo_gl_surface_swapbuffers(surface->cairo_surface);
530 wl_egl_window_get_attached_size(surface->egl_window,
531 &server_allocation->width,
532 &server_allocation->height);
534 buffer_to_surface_size (buffer_transform, buffer_scale,
535 &server_allocation->width,
536 &server_allocation->height);
540 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
542 struct egl_window_surface *surface = to_egl_window_surface(base);
543 cairo_device_t *device;
545 device = cairo_surface_get_device(surface->cairo_surface);
550 if (device == surface->display->argb_device)
551 ctx = surface->display->argb_ctx;
556 cairo_device_flush(device);
557 cairo_device_acquire(device);
558 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
559 surface->egl_surface, ctx))
560 fprintf(stderr, "failed to make surface current\n");
566 egl_window_surface_release(struct toysurface *base)
568 struct egl_window_surface *surface = to_egl_window_surface(base);
569 cairo_device_t *device;
571 device = cairo_surface_get_device(surface->cairo_surface);
575 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
576 surface->display->argb_ctx))
577 fprintf(stderr, "failed to make context current\n");
579 cairo_device_release(device);
583 egl_window_surface_destroy(struct toysurface *base)
585 struct egl_window_surface *surface = to_egl_window_surface(base);
586 struct display *d = surface->display;
588 cairo_surface_destroy(surface->cairo_surface);
589 eglDestroySurface(d->dpy, surface->egl_surface);
590 wl_egl_window_destroy(surface->egl_window);
591 surface->surface = NULL;
596 static struct toysurface *
597 egl_window_surface_create(struct display *display,
598 struct wl_surface *wl_surface,
600 struct rectangle *rectangle)
602 struct egl_window_surface *surface;
604 if (display->dpy == EGL_NO_DISPLAY)
607 surface = calloc(1, sizeof *surface);
611 surface->base.prepare = egl_window_surface_prepare;
612 surface->base.swap = egl_window_surface_swap;
613 surface->base.acquire = egl_window_surface_acquire;
614 surface->base.release = egl_window_surface_release;
615 surface->base.destroy = egl_window_surface_destroy;
617 surface->display = display;
618 surface->surface = wl_surface;
620 surface->egl_window = wl_egl_window_create(surface->surface,
624 surface->egl_surface = eglCreateWindowSurface(display->dpy,
625 display->argb_config,
629 surface->cairo_surface =
630 cairo_gl_surface_create_for_egl(display->argb_device,
631 surface->egl_surface,
635 return &surface->base;
640 static struct toysurface *
641 egl_window_surface_create(struct display *display,
642 struct wl_surface *wl_surface,
644 struct rectangle *rectangle)
651 struct shm_surface_data {
652 struct wl_buffer *buffer;
653 struct shm_pool *pool;
657 display_get_buffer_for_surface(struct display *display,
658 cairo_surface_t *surface)
660 struct shm_surface_data *data;
662 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
668 shm_pool_destroy(struct shm_pool *pool);
671 shm_surface_data_destroy(void *p)
673 struct shm_surface_data *data = p;
675 wl_buffer_destroy(data->buffer);
677 shm_pool_destroy(data->pool);
682 static struct wl_shm_pool *
683 make_shm_pool(struct display *display, int size, void **data)
685 struct wl_shm_pool *pool;
688 fd = os_create_anonymous_file(size);
690 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
695 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
696 if (*data == MAP_FAILED) {
697 fprintf(stderr, "mmap failed: %m\n");
702 pool = wl_shm_create_pool(display->shm, fd, size);
709 static struct shm_pool *
710 shm_pool_create(struct display *display, size_t size)
712 struct shm_pool *pool = malloc(sizeof *pool);
717 pool->pool = make_shm_pool(display, size, &pool->data);
730 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
732 if (pool->used + size > pool->size)
735 *offset = pool->used;
738 return (char *) pool->data + *offset;
741 /* destroy the pool. this does not unmap the memory though */
743 shm_pool_destroy(struct shm_pool *pool)
745 munmap(pool->data, pool->size);
746 wl_shm_pool_destroy(pool->pool);
750 /* Start allocating from the beginning of the pool again */
752 shm_pool_reset(struct shm_pool *pool)
758 data_length_for_shm_surface(struct rectangle *rect)
762 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
764 return stride * rect->height;
767 static cairo_surface_t *
768 display_create_shm_surface_from_pool(struct display *display,
769 struct rectangle *rectangle,
770 uint32_t flags, struct shm_pool *pool)
772 struct shm_surface_data *data;
774 cairo_surface_t *surface;
775 cairo_format_t cairo_format;
776 int stride, length, offset;
779 data = malloc(sizeof *data);
783 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
784 cairo_format = CAIRO_FORMAT_RGB16_565;
786 cairo_format = CAIRO_FORMAT_ARGB32;
788 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
789 length = stride * rectangle->height;
791 map = shm_pool_allocate(pool, length, &offset);
798 surface = cairo_image_surface_create_for_data (map,
804 cairo_surface_set_user_data(surface, &shm_surface_data_key,
805 data, shm_surface_data_destroy);
807 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
808 format = WL_SHM_FORMAT_RGB565;
810 if (flags & SURFACE_OPAQUE)
811 format = WL_SHM_FORMAT_XRGB8888;
813 format = WL_SHM_FORMAT_ARGB8888;
816 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
824 static cairo_surface_t *
825 display_create_shm_surface(struct display *display,
826 struct rectangle *rectangle, uint32_t flags,
827 struct shm_pool *alternate_pool,
828 struct shm_surface_data **data_ret)
830 struct shm_surface_data *data;
831 struct shm_pool *pool;
832 cairo_surface_t *surface;
834 if (alternate_pool) {
835 shm_pool_reset(alternate_pool);
836 surface = display_create_shm_surface_from_pool(display,
841 data = cairo_surface_get_user_data(surface,
842 &shm_surface_data_key);
847 pool = shm_pool_create(display,
848 data_length_for_shm_surface(rectangle));
853 display_create_shm_surface_from_pool(display, rectangle,
857 shm_pool_destroy(pool);
861 /* make sure we destroy the pool when the surface is destroyed */
862 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
873 check_size(struct rectangle *rect)
875 if (rect->width && rect->height)
878 fprintf(stderr, "tried to create surface of "
879 "width: %d, height: %d\n", rect->width, rect->height);
884 display_create_surface(struct display *display,
885 struct wl_surface *surface,
886 struct rectangle *rectangle,
889 if (check_size(rectangle) < 0)
892 assert(flags & SURFACE_SHM);
893 return display_create_shm_surface(display, rectangle, flags,
897 struct shm_surface_leaf {
898 cairo_surface_t *cairo_surface;
899 /* 'data' is automatically destroyed, when 'cairo_surface' is */
900 struct shm_surface_data *data;
902 struct shm_pool *resize_pool;
907 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
909 if (leaf->cairo_surface)
910 cairo_surface_destroy(leaf->cairo_surface);
911 /* leaf->data already destroyed via cairo private */
913 if (leaf->resize_pool)
914 shm_pool_destroy(leaf->resize_pool);
916 memset(leaf, 0, sizeof *leaf);
922 struct toysurface base;
923 struct display *display;
924 struct wl_surface *surface;
928 struct shm_surface_leaf leaf[MAX_LEAVES];
929 struct shm_surface_leaf *current;
932 static struct shm_surface *
933 to_shm_surface(struct toysurface *base)
935 return container_of(base, struct shm_surface, base);
939 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
942 struct shm_surface_leaf *leaf;
943 char bufs[MAX_LEAVES + 1];
946 for (i = 0; i < MAX_LEAVES; i++) {
947 leaf = &surface->leaf[i];
951 else if (leaf->cairo_surface)
957 bufs[MAX_LEAVES] = '\0';
958 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
963 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
965 struct shm_surface *surface = data;
966 struct shm_surface_leaf *leaf;
970 shm_surface_buffer_state_debug(surface, "buffer_release before");
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
974 if (leaf->data && leaf->data->buffer == buffer) {
979 assert(i < MAX_LEAVES && "unknown buffer released");
981 /* Leave one free leaf with storage, release others */
983 for (i = 0; i < MAX_LEAVES; i++) {
984 leaf = &surface->leaf[i];
986 if (!leaf->cairo_surface || leaf->busy)
992 shm_surface_leaf_release(leaf);
995 shm_surface_buffer_state_debug(surface, "buffer_release after");
998 static const struct wl_buffer_listener shm_surface_buffer_listener = {
999 shm_surface_buffer_release
1002 static cairo_surface_t *
1003 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1004 int32_t width, int32_t height, uint32_t flags,
1005 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1007 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1008 struct shm_surface *surface = to_shm_surface(base);
1009 struct rectangle rect = { 0};
1010 struct shm_surface_leaf *leaf = NULL;
1016 /* pick a free buffer, preferrably one that already has storage */
1017 for (i = 0; i < MAX_LEAVES; i++) {
1018 if (surface->leaf[i].busy)
1021 if (!leaf || surface->leaf[i].cairo_surface)
1022 leaf = &surface->leaf[i];
1024 DBG_OBJ(surface->surface, "pick leaf %d\n",
1025 (int)(leaf - &surface->leaf[0]));
1028 fprintf(stderr, "%s: all buffers are held by the server.\n",
1034 if (!resize_hint && leaf->resize_pool) {
1035 cairo_surface_destroy(leaf->cairo_surface);
1036 leaf->cairo_surface = NULL;
1037 shm_pool_destroy(leaf->resize_pool);
1038 leaf->resize_pool = NULL;
1041 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1043 if (leaf->cairo_surface &&
1044 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1045 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1048 if (leaf->cairo_surface)
1049 cairo_surface_destroy(leaf->cairo_surface);
1051 #ifdef USE_RESIZE_POOL
1052 if (resize_hint && !leaf->resize_pool) {
1053 /* Create a big pool to allocate from, while continuously
1054 * resizing. Mmapping a new pool in the server
1055 * is relatively expensive, so reusing a pool performs
1056 * better, but may temporarily reserve unneeded memory.
1058 /* We should probably base this number on the output size. */
1059 leaf->resize_pool = shm_pool_create(surface->display,
1065 rect.height = height;
1067 leaf->cairo_surface =
1068 display_create_shm_surface(surface->display, &rect,
1072 if (!leaf->cairo_surface)
1075 wl_buffer_add_listener(leaf->data->buffer,
1076 &shm_surface_buffer_listener, surface);
1079 surface->current = leaf;
1081 return cairo_surface_reference(leaf->cairo_surface);
1085 shm_surface_swap(struct toysurface *base,
1086 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1087 struct rectangle *server_allocation)
1089 struct shm_surface *surface = to_shm_surface(base);
1090 struct shm_surface_leaf *leaf = surface->current;
1092 server_allocation->width =
1093 cairo_image_surface_get_width(leaf->cairo_surface);
1094 server_allocation->height =
1095 cairo_image_surface_get_height(leaf->cairo_surface);
1097 buffer_to_surface_size (buffer_transform, buffer_scale,
1098 &server_allocation->width,
1099 &server_allocation->height);
1101 wl_surface_attach(surface->surface, leaf->data->buffer,
1102 surface->dx, surface->dy);
1103 wl_surface_damage(surface->surface, 0, 0,
1104 server_allocation->width, server_allocation->height);
1105 wl_surface_commit(surface->surface);
1107 DBG_OBJ(surface->surface, "leaf %d busy\n",
1108 (int)(leaf - &surface->leaf[0]));
1111 surface->current = NULL;
1115 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1121 shm_surface_release(struct toysurface *base)
1126 shm_surface_destroy(struct toysurface *base)
1128 struct shm_surface *surface = to_shm_surface(base);
1131 for (i = 0; i < MAX_LEAVES; i++)
1132 shm_surface_leaf_release(&surface->leaf[i]);
1137 static struct toysurface *
1138 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1139 uint32_t flags, struct rectangle *rectangle)
1141 struct shm_surface *surface;
1142 DBG_OBJ(wl_surface, "\n");
1144 surface = xmalloc(sizeof *surface);
1145 memset(surface, 0, sizeof *surface);
1150 surface->base.prepare = shm_surface_prepare;
1151 surface->base.swap = shm_surface_swap;
1152 surface->base.acquire = shm_surface_acquire;
1153 surface->base.release = shm_surface_release;
1154 surface->base.destroy = shm_surface_destroy;
1156 surface->display = display;
1157 surface->surface = wl_surface;
1158 surface->flags = flags;
1160 return &surface->base;
1164 * The following correspondences between file names and cursors was copied
1165 * from: https://bugs.kde.org/attachment.cgi?id=67313
1168 static const char *bottom_left_corners[] = {
1169 "bottom_left_corner",
1174 static const char *bottom_right_corners[] = {
1175 "bottom_right_corner",
1180 static const char *bottom_sides[] = {
1186 static const char *grabbings[] = {
1189 "208530c400c041818281048008011002"
1192 static const char *left_ptrs[] = {
1199 static const char *left_sides[] = {
1205 static const char *right_sides[] = {
1211 static const char *top_left_corners[] = {
1217 static const char *top_right_corners[] = {
1223 static const char *top_sides[] = {
1229 static const char *xterms[] = {
1235 static const char *hand1s[] = {
1239 "e29285e634086352946a0e7090d73106"
1242 static const char *watches[] = {
1245 "0426c94ea35c87780ff01dc239897213"
1248 struct cursor_alternatives {
1253 static const struct cursor_alternatives cursors[] = {
1254 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1255 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1256 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1257 {grabbings, ARRAY_LENGTH(grabbings)},
1258 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1259 {left_sides, ARRAY_LENGTH(left_sides)},
1260 {right_sides, ARRAY_LENGTH(right_sides)},
1261 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1262 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1263 {top_sides, ARRAY_LENGTH(top_sides)},
1264 {xterms, ARRAY_LENGTH(xterms)},
1265 {hand1s, ARRAY_LENGTH(hand1s)},
1266 {watches, ARRAY_LENGTH(watches)},
1270 create_cursors(struct display *display)
1272 struct weston_config *config;
1273 struct weston_config_section *s;
1277 struct wl_cursor *cursor;
1279 config = weston_config_parse("weston.ini");
1280 s = weston_config_get_section(config, "shell", NULL, NULL);
1281 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1282 weston_config_section_get_int(s, "cursor-size", &size, 32);
1283 weston_config_destroy(config);
1285 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1286 if (!display->cursor_theme) {
1287 fprintf(stderr, "could not load theme '%s'\n", theme);
1292 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1294 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1296 for (j = 0; !cursor && j < cursors[i].count; ++j)
1297 cursor = wl_cursor_theme_get_cursor(
1298 display->cursor_theme, cursors[i].names[j]);
1301 fprintf(stderr, "could not load cursor '%s'\n",
1302 cursors[i].names[0]);
1304 display->cursors[i] = cursor;
1309 destroy_cursors(struct display *display)
1311 wl_cursor_theme_destroy(display->cursor_theme);
1312 free(display->cursors);
1315 struct wl_cursor_image *
1316 display_get_pointer_image(struct display *display, int pointer)
1318 struct wl_cursor *cursor = display->cursors[pointer];
1320 return cursor ? cursor->images[0] : NULL;
1324 surface_flush(struct surface *surface)
1326 if (!surface->cairo_surface)
1329 if (surface->opaque_region) {
1330 wl_surface_set_opaque_region(surface->surface,
1331 surface->opaque_region);
1332 wl_region_destroy(surface->opaque_region);
1333 surface->opaque_region = NULL;
1336 if (surface->input_region) {
1337 wl_surface_set_input_region(surface->surface,
1338 surface->input_region);
1339 wl_region_destroy(surface->input_region);
1340 surface->input_region = NULL;
1343 if (surface->window->next_attach_serial > 0) {
1344 xdg_surface_ack_configure(surface->window->xdg_surface,
1345 surface->window->next_attach_serial);
1346 surface->window->next_attach_serial = 0;
1349 surface->toysurface->swap(surface->toysurface,
1350 surface->buffer_transform, surface->buffer_scale,
1351 &surface->server_allocation);
1353 cairo_surface_destroy(surface->cairo_surface);
1354 surface->cairo_surface = NULL;
1358 window_has_focus(struct window *window)
1360 return window->focused;
1364 window_close(struct window *window)
1366 if (window->close_handler)
1367 window->close_handler(window->user_data);
1369 display_exit(window->display);
1373 window_get_display(struct window *window)
1375 return window->display;
1379 surface_create_surface(struct surface *surface, uint32_t flags)
1381 struct display *display = surface->window->display;
1382 struct rectangle allocation = surface->allocation;
1384 if (!surface->toysurface && display->dpy &&
1385 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1386 surface->toysurface =
1387 egl_window_surface_create(display,
1393 if (!surface->toysurface)
1394 surface->toysurface = shm_surface_create(display,
1396 flags, &allocation);
1398 surface->cairo_surface = surface->toysurface->prepare(
1399 surface->toysurface, 0, 0,
1400 allocation.width, allocation.height, flags,
1401 surface->buffer_transform, surface->buffer_scale);
1405 window_create_main_surface(struct window *window)
1407 struct surface *surface = window->main_surface;
1410 if (window->resizing)
1411 flags |= SURFACE_HINT_RESIZE;
1413 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1414 flags |= SURFACE_HINT_RGB565;
1416 surface_create_surface(surface, flags);
1420 window_get_buffer_transform(struct window *window)
1422 return window->main_surface->buffer_transform;
1426 window_set_buffer_transform(struct window *window,
1427 enum wl_output_transform transform)
1429 window->main_surface->buffer_transform = transform;
1430 wl_surface_set_buffer_transform(window->main_surface->surface,
1435 window_set_buffer_scale(struct window *window,
1438 window->main_surface->buffer_scale = scale;
1439 wl_surface_set_buffer_scale(window->main_surface->surface,
1444 window_get_buffer_scale(struct window *window)
1446 return window->main_surface->buffer_scale;
1450 window_get_output_scale(struct window *window)
1452 struct window_output *window_output;
1453 struct window_output *window_output_tmp;
1456 wl_list_for_each_safe(window_output, window_output_tmp,
1457 &window->window_output_list, link) {
1458 if (window_output->output->scale > scale)
1459 scale = window_output->output->scale;
1465 static void window_frame_destroy(struct window_frame *frame);
1468 surface_destroy(struct surface *surface)
1470 if (surface->frame_cb)
1471 wl_callback_destroy(surface->frame_cb);
1473 if (surface->input_region)
1474 wl_region_destroy(surface->input_region);
1476 if (surface->opaque_region)
1477 wl_region_destroy(surface->opaque_region);
1479 if (surface->subsurface)
1480 wl_subsurface_destroy(surface->subsurface);
1482 wl_surface_destroy(surface->surface);
1484 if (surface->toysurface)
1485 surface->toysurface->destroy(surface->toysurface);
1487 wl_list_remove(&surface->link);
1492 window_destroy(struct window *window)
1494 struct display *display = window->display;
1495 struct input *input;
1496 struct window_output *window_output;
1497 struct window_output *window_output_tmp;
1499 wl_list_remove(&window->redraw_task.link);
1501 wl_list_for_each(input, &display->input_list, link) {
1502 if (input->touch_focus == window)
1503 input->touch_focus = NULL;
1504 if (input->pointer_focus == window)
1505 input->pointer_focus = NULL;
1506 if (input->keyboard_focus == window)
1507 input->keyboard_focus = NULL;
1508 if (input->focus_widget &&
1509 input->focus_widget->window == window)
1510 input->focus_widget = NULL;
1513 wl_list_for_each_safe(window_output, window_output_tmp,
1514 &window->window_output_list, link) {
1515 free (window_output);
1519 window_frame_destroy(window->frame);
1521 if (window->xdg_surface)
1522 xdg_surface_destroy(window->xdg_surface);
1523 if (window->xdg_popup)
1524 xdg_popup_destroy(window->xdg_popup);
1526 surface_destroy(window->main_surface);
1528 wl_list_remove(&window->link);
1530 free(window->title);
1534 static struct widget *
1535 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1537 struct widget *child, *target;
1539 wl_list_for_each(child, &widget->child_list, link) {
1540 target = widget_find_widget(child, x, y);
1545 if (widget->allocation.x <= x &&
1546 x < widget->allocation.x + widget->allocation.width &&
1547 widget->allocation.y <= y &&
1548 y < widget->allocation.y + widget->allocation.height) {
1555 static struct widget *
1556 window_find_widget(struct window *window, int32_t x, int32_t y)
1558 struct surface *surface;
1559 struct widget *widget;
1561 wl_list_for_each(surface, &window->subsurface_list, link) {
1562 widget = widget_find_widget(surface->widget, x, y);
1570 static struct widget *
1571 widget_create(struct window *window, struct surface *surface, void *data)
1573 struct widget *widget;
1575 widget = xzalloc(sizeof *widget);
1576 widget->window = window;
1577 widget->surface = surface;
1578 widget->user_data = data;
1579 widget->allocation = surface->allocation;
1580 wl_list_init(&widget->child_list);
1582 widget->tooltip = NULL;
1583 widget->tooltip_count = 0;
1584 widget->default_cursor = CURSOR_LEFT_PTR;
1585 widget->use_cairo = 1;
1591 window_add_widget(struct window *window, void *data)
1593 struct widget *widget;
1595 widget = widget_create(window, window->main_surface, data);
1596 wl_list_init(&widget->link);
1597 window->main_surface->widget = widget;
1603 widget_add_widget(struct widget *parent, void *data)
1605 struct widget *widget;
1607 widget = widget_create(parent->window, parent->surface, data);
1608 wl_list_insert(parent->child_list.prev, &widget->link);
1614 widget_destroy(struct widget *widget)
1616 struct display *display = widget->window->display;
1617 struct surface *surface = widget->surface;
1618 struct input *input;
1620 /* Destroy the sub-surface along with the root widget */
1621 if (surface->widget == widget && surface->subsurface)
1622 surface_destroy(widget->surface);
1624 if (widget->tooltip)
1625 widget_destroy_tooltip(widget);
1627 wl_list_for_each(input, &display->input_list, link) {
1628 if (input->focus_widget == widget)
1629 input->focus_widget = NULL;
1632 wl_list_remove(&widget->link);
1637 widget_set_default_cursor(struct widget *widget, int cursor)
1639 widget->default_cursor = cursor;
1643 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1645 *allocation = widget->allocation;
1649 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1651 widget->allocation.width = width;
1652 widget->allocation.height = height;
1656 widget_set_allocation(struct widget *widget,
1657 int32_t x, int32_t y, int32_t width, int32_t height)
1659 widget->allocation.x = x;
1660 widget->allocation.y = y;
1661 widget_set_size(widget, width, height);
1665 widget_set_transparent(struct widget *widget, int transparent)
1667 widget->opaque = !transparent;
1671 widget_get_user_data(struct widget *widget)
1673 return widget->user_data;
1676 static cairo_surface_t *
1677 widget_get_cairo_surface(struct widget *widget)
1679 struct surface *surface = widget->surface;
1680 struct window *window = widget->window;
1682 assert(widget->use_cairo);
1684 if (!surface->cairo_surface) {
1685 if (surface == window->main_surface)
1686 window_create_main_surface(window);
1688 surface_create_surface(surface, 0);
1691 return surface->cairo_surface;
1695 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1697 struct surface *surface = widget->surface;
1700 enum wl_output_transform transform;
1701 int surface_width, surface_height;
1702 int translate_x, translate_y;
1705 surface_width = surface->allocation.width;
1706 surface_height = surface->allocation.height;
1708 transform = surface->buffer_transform;
1709 scale = surface->buffer_scale;
1711 switch (transform) {
1712 case WL_OUTPUT_TRANSFORM_FLIPPED:
1713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1716 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1719 cairo_matrix_init_identity(&m);
1723 switch (transform) {
1724 case WL_OUTPUT_TRANSFORM_NORMAL:
1730 case WL_OUTPUT_TRANSFORM_FLIPPED:
1732 translate_x = surface_width;
1735 case WL_OUTPUT_TRANSFORM_90:
1737 translate_x = surface_height;
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1742 translate_x = surface_height;
1743 translate_y = surface_width;
1745 case WL_OUTPUT_TRANSFORM_180:
1747 translate_x = surface_width;
1748 translate_y = surface_height;
1750 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1753 translate_y = surface_height;
1755 case WL_OUTPUT_TRANSFORM_270:
1756 angle = M_PI + M_PI_2;
1758 translate_y = surface_width;
1760 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1761 angle = M_PI + M_PI_2;
1767 cairo_scale(cr, scale, scale);
1768 cairo_translate(cr, translate_x, translate_y);
1769 cairo_rotate(cr, angle);
1770 cairo_transform(cr, &m);
1774 widget_cairo_create(struct widget *widget)
1776 struct surface *surface = widget->surface;
1777 cairo_surface_t *cairo_surface;
1780 cairo_surface = widget_get_cairo_surface(widget);
1781 cr = cairo_create(cairo_surface);
1783 widget_cairo_update_transform(widget, cr);
1785 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1791 widget_get_wl_surface(struct widget *widget)
1793 return widget->surface->surface;
1796 struct wl_subsurface *
1797 widget_get_wl_subsurface(struct widget *widget)
1799 return widget->surface->subsurface;
1803 widget_get_last_time(struct widget *widget)
1805 return widget->surface->last_time;
1809 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1811 struct wl_compositor *comp = widget->window->display->compositor;
1812 struct surface *surface = widget->surface;
1814 if (!surface->input_region)
1815 surface->input_region = wl_compositor_create_region(comp);
1818 wl_region_add(surface->input_region,
1819 rect->x, rect->y, rect->width, rect->height);
1824 widget_set_resize_handler(struct widget *widget,
1825 widget_resize_handler_t handler)
1827 widget->resize_handler = handler;
1831 widget_set_redraw_handler(struct widget *widget,
1832 widget_redraw_handler_t handler)
1834 widget->redraw_handler = handler;
1838 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1840 widget->enter_handler = handler;
1844 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1846 widget->leave_handler = handler;
1850 widget_set_motion_handler(struct widget *widget,
1851 widget_motion_handler_t handler)
1853 widget->motion_handler = handler;
1857 widget_set_button_handler(struct widget *widget,
1858 widget_button_handler_t handler)
1860 widget->button_handler = handler;
1864 widget_set_touch_up_handler(struct widget *widget,
1865 widget_touch_up_handler_t handler)
1867 widget->touch_up_handler = handler;
1871 widget_set_touch_down_handler(struct widget *widget,
1872 widget_touch_down_handler_t handler)
1874 widget->touch_down_handler = handler;
1878 widget_set_touch_motion_handler(struct widget *widget,
1879 widget_touch_motion_handler_t handler)
1881 widget->touch_motion_handler = handler;
1885 widget_set_touch_frame_handler(struct widget *widget,
1886 widget_touch_frame_handler_t handler)
1888 widget->touch_frame_handler = handler;
1892 widget_set_touch_cancel_handler(struct widget *widget,
1893 widget_touch_cancel_handler_t handler)
1895 widget->touch_cancel_handler = handler;
1899 widget_set_axis_handler(struct widget *widget,
1900 widget_axis_handler_t handler)
1902 widget->axis_handler = handler;
1906 window_schedule_redraw_task(struct window *window);
1909 widget_schedule_redraw(struct widget *widget)
1911 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1912 widget->surface->redraw_needed = 1;
1913 window_schedule_redraw_task(widget->window);
1917 widget_set_use_cairo(struct widget *widget,
1920 widget->use_cairo = use_cairo;
1924 window_get_surface(struct window *window)
1926 cairo_surface_t *cairo_surface;
1928 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1930 return cairo_surface_reference(cairo_surface);
1934 window_get_wl_surface(struct window *window)
1936 return window->main_surface->surface;
1940 tooltip_redraw_handler(struct widget *widget, void *data)
1943 const int32_t r = 3;
1944 struct tooltip *tooltip = data;
1945 int32_t width, height;
1947 cr = widget_cairo_create(widget);
1948 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1949 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1950 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1953 width = widget->allocation.width;
1954 height = widget->allocation.height;
1955 rounded_rect(cr, 0, 0, width, height, r);
1957 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1958 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1961 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1962 cairo_move_to(cr, 10, 16);
1963 cairo_show_text(cr, tooltip->entry);
1967 static cairo_text_extents_t
1968 get_text_extents(struct display *display, struct tooltip *tooltip)
1971 cairo_text_extents_t extents;
1973 /* Use the dummy_surface because tooltip's surface was not
1974 * created yet, and parent does not have a valid surface
1975 * outside repaint, either.
1977 cr = cairo_create(display->dummy_surface);
1978 cairo_text_extents(cr, tooltip->entry, &extents);
1985 window_create_tooltip(struct tooltip *tooltip)
1987 struct widget *parent = tooltip->parent;
1988 struct display *display = parent->window->display;
1989 const int offset_y = 27;
1990 const int margin = 3;
1991 cairo_text_extents_t extents;
1993 if (tooltip->widget)
1996 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1998 extents = get_text_extents(display, tooltip);
1999 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2000 widget_set_allocation(tooltip->widget,
2001 tooltip->x, tooltip->y + offset_y,
2002 extents.width + 20, 20 + margin * 2);
2008 widget_destroy_tooltip(struct widget *parent)
2010 struct tooltip *tooltip = parent->tooltip;
2012 parent->tooltip_count = 0;
2016 if (tooltip->widget) {
2017 widget_destroy(tooltip->widget);
2018 tooltip->widget = NULL;
2021 close(tooltip->tooltip_fd);
2022 free(tooltip->entry);
2024 parent->tooltip = NULL;
2028 tooltip_func(struct task *task, uint32_t events)
2030 struct tooltip *tooltip =
2031 container_of(task, struct tooltip, tooltip_task);
2034 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2036 window_create_tooltip(tooltip);
2039 #define TOOLTIP_TIMEOUT 500
2041 tooltip_timer_reset(struct tooltip *tooltip)
2043 struct itimerspec its;
2045 its.it_interval.tv_sec = 0;
2046 its.it_interval.tv_nsec = 0;
2047 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2048 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2049 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2050 fprintf(stderr, "could not set timerfd\n: %m");
2058 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2060 struct tooltip *tooltip = parent->tooltip;
2062 parent->tooltip_count++;
2066 tooltip_timer_reset(tooltip);
2070 /* the handler might be triggered too fast via input device motion, so
2071 * we need this check here to make sure tooltip is fully initialized */
2072 if (parent->tooltip_count > 1)
2075 tooltip = malloc(sizeof *tooltip);
2079 parent->tooltip = tooltip;
2080 tooltip->parent = parent;
2081 tooltip->widget = NULL;
2084 tooltip->entry = strdup(entry);
2085 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2086 if (tooltip->tooltip_fd < 0) {
2087 fprintf(stderr, "could not create timerfd\n: %m");
2091 tooltip->tooltip_task.run = tooltip_func;
2092 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2093 EPOLLIN, &tooltip->tooltip_task);
2094 tooltip_timer_reset(tooltip);
2100 workspace_manager_state(void *data,
2101 struct workspace_manager *workspace_manager,
2105 struct display *display = data;
2107 display->workspace = current;
2108 display->workspace_count = count;
2111 static const struct workspace_manager_listener workspace_manager_listener = {
2112 workspace_manager_state
2116 frame_resize_handler(struct widget *widget,
2117 int32_t width, int32_t height, void *data)
2119 struct window_frame *frame = data;
2120 struct widget *child = frame->child;
2121 struct rectangle interior;
2122 struct rectangle input;
2123 struct rectangle opaque;
2125 if (widget->window->fullscreen) {
2128 interior.width = width;
2129 interior.height = height;
2131 if (widget->window->maximized) {
2132 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2134 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2137 frame_resize(frame->frame, width, height);
2138 frame_interior(frame->frame, &interior.x, &interior.y,
2139 &interior.width, &interior.height);
2142 widget_set_allocation(child, interior.x, interior.y,
2143 interior.width, interior.height);
2145 if (child->resize_handler) {
2146 child->resize_handler(child, interior.width, interior.height,
2149 if (widget->window->fullscreen) {
2150 width = child->allocation.width;
2151 height = child->allocation.height;
2153 frame_resize_inside(frame->frame,
2154 child->allocation.width,
2155 child->allocation.height);
2156 width = frame_width(frame->frame);
2157 height = frame_height(frame->frame);
2161 widget_set_allocation(widget, 0, 0, width, height);
2163 widget->surface->input_region =
2164 wl_compositor_create_region(widget->window->display->compositor);
2165 if (!widget->window->fullscreen) {
2166 frame_input_rect(frame->frame, &input.x, &input.y,
2167 &input.width, &input.height);
2168 wl_region_add(widget->surface->input_region,
2169 input.x, input.y, input.width, input.height);
2171 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2174 widget_set_allocation(widget, 0, 0, width, height);
2176 if (child->opaque) {
2177 if (!widget->window->fullscreen) {
2178 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2179 &opaque.width, &opaque.height);
2181 wl_region_add(widget->surface->opaque_region,
2183 opaque.width, opaque.height);
2185 wl_region_add(widget->surface->opaque_region,
2186 0, 0, width, height);
2191 widget_schedule_redraw(widget);
2195 frame_redraw_handler(struct widget *widget, void *data)
2198 struct window_frame *frame = data;
2199 struct window *window = widget->window;
2201 if (window->fullscreen)
2204 if (window->focused) {
2205 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2207 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2210 cr = widget_cairo_create(widget);
2212 frame_repaint(frame->frame, cr);
2218 frame_get_pointer_image_for_location(struct window_frame *frame,
2219 enum theme_location location)
2221 struct window *window = frame->widget->window;
2224 return CURSOR_LEFT_PTR;
2227 case THEME_LOCATION_RESIZING_TOP:
2229 case THEME_LOCATION_RESIZING_BOTTOM:
2230 return CURSOR_BOTTOM;
2231 case THEME_LOCATION_RESIZING_LEFT:
2233 case THEME_LOCATION_RESIZING_RIGHT:
2234 return CURSOR_RIGHT;
2235 case THEME_LOCATION_RESIZING_TOP_LEFT:
2236 return CURSOR_TOP_LEFT;
2237 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2238 return CURSOR_TOP_RIGHT;
2239 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2240 return CURSOR_BOTTOM_LEFT;
2241 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2242 return CURSOR_BOTTOM_RIGHT;
2243 case THEME_LOCATION_EXTERIOR:
2244 case THEME_LOCATION_TITLEBAR:
2246 return CURSOR_LEFT_PTR;
2251 frame_menu_func(struct window *window,
2252 struct input *input, int index, void *data)
2254 struct display *display;
2258 window_close(window);
2260 case 1: /* move to workspace above */
2261 display = window->display;
2262 if (display->workspace > 0)
2263 workspace_manager_move_surface(
2264 display->workspace_manager,
2265 window->main_surface->surface,
2266 display->workspace - 1);
2268 case 2: /* move to workspace below */
2269 display = window->display;
2270 if (display->workspace < display->workspace_count - 1)
2271 workspace_manager_move_surface(
2272 display->workspace_manager,
2273 window->main_surface->surface,
2274 display->workspace + 1);
2276 case 3: /* fullscreen */
2277 /* we don't have a way to get out of fullscreen for now */
2278 if (window->fullscreen_handler)
2279 window->fullscreen_handler(window, window->user_data);
2285 window_show_frame_menu(struct window *window,
2286 struct input *input, uint32_t time)
2291 static const char *entries[] = {
2293 "Move to workspace above", "Move to workspace below",
2297 if (window->fullscreen_handler)
2298 count = ARRAY_LENGTH(entries);
2300 count = ARRAY_LENGTH(entries) - 1;
2302 input_get_position(input, &x, &y);
2303 window_show_menu(window->display, input, time, window,
2304 x - 10, y - 10, frame_menu_func, entries, count);
2308 frame_enter_handler(struct widget *widget,
2309 struct input *input, float x, float y, void *data)
2311 struct window_frame *frame = data;
2312 enum theme_location location;
2314 location = frame_pointer_enter(frame->frame, input, x, y);
2315 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2316 widget_schedule_redraw(frame->widget);
2318 return frame_get_pointer_image_for_location(data, location);
2322 frame_motion_handler(struct widget *widget,
2323 struct input *input, uint32_t time,
2324 float x, float y, void *data)
2326 struct window_frame *frame = data;
2327 enum theme_location location;
2329 location = frame_pointer_motion(frame->frame, input, x, y);
2330 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2331 widget_schedule_redraw(frame->widget);
2333 return frame_get_pointer_image_for_location(data, location);
2337 frame_leave_handler(struct widget *widget,
2338 struct input *input, void *data)
2340 struct window_frame *frame = data;
2342 frame_pointer_leave(frame->frame, input);
2343 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2344 widget_schedule_redraw(frame->widget);
2348 frame_handle_status(struct window_frame *frame, struct input *input,
2349 uint32_t time, enum theme_location location)
2351 struct window *window = frame->widget->window;
2354 status = frame_status(frame->frame);
2355 if (status & FRAME_STATUS_REPAINT)
2356 widget_schedule_redraw(frame->widget);
2358 if (status & FRAME_STATUS_MINIMIZE) {
2359 window_set_minimized(window);
2360 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2363 if (status & FRAME_STATUS_MENU) {
2364 window_show_frame_menu(window, input, time);
2365 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2368 if (status & FRAME_STATUS_MAXIMIZE) {
2369 window_set_maximized(window, !window->maximized);
2370 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2373 if (status & FRAME_STATUS_CLOSE) {
2374 window_close(window);
2378 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2379 input_ungrab(input);
2380 xdg_surface_move(window->xdg_surface,
2381 input_get_seat(input),
2382 window->display->serial);
2384 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2387 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2388 input_ungrab(input);
2390 xdg_surface_resize(window->xdg_surface,
2391 input_get_seat(input),
2392 window->display->serial,
2395 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2400 frame_button_handler(struct widget *widget,
2401 struct input *input, uint32_t time,
2402 uint32_t button, enum wl_pointer_button_state state,
2406 struct window_frame *frame = data;
2407 enum theme_location location;
2409 location = frame_pointer_button(frame->frame, input, button, state);
2410 frame_handle_status(frame, input, time, location);
2414 frame_touch_down_handler(struct widget *widget, struct input *input,
2415 uint32_t serial, uint32_t time, int32_t id,
2416 float x, float y, void *data)
2418 struct window_frame *frame = data;
2420 frame_touch_down(frame->frame, input, id, x, y);
2421 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2425 frame_touch_up_handler(struct widget *widget,
2426 struct input *input, uint32_t serial, uint32_t time,
2427 int32_t id, void *data)
2429 struct window_frame *frame = data;
2431 frame_touch_up(frame->frame, input, id);
2432 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2436 window_frame_create(struct window *window, void *data)
2438 struct window_frame *frame;
2441 if (window->custom) {
2442 buttons = FRAME_BUTTON_NONE;
2444 buttons = FRAME_BUTTON_ALL;
2447 frame = xzalloc(sizeof *frame);
2448 frame->frame = frame_create(window->display->theme, 0, 0,
2449 buttons, window->title);
2451 frame->widget = window_add_widget(window, frame);
2452 frame->child = widget_add_widget(frame->widget, data);
2454 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2455 widget_set_resize_handler(frame->widget, frame_resize_handler);
2456 widget_set_enter_handler(frame->widget, frame_enter_handler);
2457 widget_set_leave_handler(frame->widget, frame_leave_handler);
2458 widget_set_motion_handler(frame->widget, frame_motion_handler);
2459 widget_set_button_handler(frame->widget, frame_button_handler);
2460 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2461 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2463 window->frame = frame;
2465 return frame->child;
2469 window_frame_set_child_size(struct widget *widget, int child_width,
2472 struct display *display = widget->window->display;
2473 struct theme *t = display->theme;
2474 int decoration_width, decoration_height;
2476 int margin = widget->window->maximized ? 0 : t->margin;
2478 if (!widget->window->fullscreen) {
2479 decoration_width = (t->width + margin) * 2;
2480 decoration_height = t->width +
2481 t->titlebar_height + margin * 2;
2483 width = child_width + decoration_width;
2484 height = child_height + decoration_height;
2486 width = child_width;
2487 height = child_height;
2490 window_schedule_resize(widget->window, width, height);
2494 window_frame_destroy(struct window_frame *frame)
2496 frame_destroy(frame->frame);
2498 /* frame->child must be destroyed by the application */
2499 widget_destroy(frame->widget);
2504 input_set_focus_widget(struct input *input, struct widget *focus,
2507 struct widget *old, *widget;
2510 if (focus == input->focus_widget)
2513 old = input->focus_widget;
2517 widget = input->grab;
2518 if (widget->leave_handler)
2519 widget->leave_handler(old, input, widget->user_data);
2520 input->focus_widget = NULL;
2526 widget = input->grab;
2527 input->focus_widget = focus;
2528 if (widget->enter_handler)
2529 cursor = widget->enter_handler(focus, input, x, y,
2532 cursor = widget->default_cursor;
2534 input_set_pointer_image(input, cursor);
2539 touch_grab(struct input *input, int32_t touch_id)
2541 input->touch_grab = 1;
2542 input->touch_grab_id = touch_id;
2546 touch_ungrab(struct input *input)
2548 struct touch_point *tp, *tmp;
2550 input->touch_grab = 0;
2552 wl_list_for_each_safe(tp, tmp,
2553 &input->touch_point_list, link) {
2554 if (tp->id != input->touch_grab_id)
2556 wl_list_remove(&tp->link);
2564 input_grab(struct input *input, struct widget *widget, uint32_t button)
2566 input->grab = widget;
2567 input->grab_button = button;
2569 input_set_focus_widget(input, widget, input->sx, input->sy);
2573 input_ungrab(struct input *input)
2575 struct widget *widget;
2578 if (input->pointer_focus) {
2579 widget = window_find_widget(input->pointer_focus,
2580 input->sx, input->sy);
2581 input_set_focus_widget(input, widget, input->sx, input->sy);
2586 input_remove_pointer_focus(struct input *input)
2588 struct window *window = input->pointer_focus;
2593 input_set_focus_widget(input, NULL, 0, 0);
2595 input->pointer_focus = NULL;
2596 input->current_cursor = CURSOR_UNSET;
2600 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2601 uint32_t serial, struct wl_surface *surface,
2602 wl_fixed_t sx_w, wl_fixed_t sy_w)
2604 struct input *input = data;
2605 struct window *window;
2606 struct widget *widget;
2607 float sx = wl_fixed_to_double(sx_w);
2608 float sy = wl_fixed_to_double(sy_w);
2611 /* enter event for a window we've just destroyed */
2615 window = wl_surface_get_user_data(surface);
2616 if (surface != window->main_surface->surface) {
2617 DBG("Ignoring input event from subsurface %p\n", surface);
2621 input->display->serial = serial;
2622 input->pointer_enter_serial = serial;
2623 input->pointer_focus = window;
2628 widget = window_find_widget(window, sx, sy);
2629 input_set_focus_widget(input, widget, sx, sy);
2633 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2634 uint32_t serial, struct wl_surface *surface)
2636 struct input *input = data;
2638 input->display->serial = serial;
2639 input_remove_pointer_focus(input);
2643 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2644 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2646 struct input *input = data;
2647 struct window *window = input->pointer_focus;
2648 struct widget *widget;
2650 float sx = wl_fixed_to_double(sx_w);
2651 float sy = wl_fixed_to_double(sy_w);
2659 /* when making the window smaller - e.g. after a unmaximise we might
2660 * still have a pending motion event that the compositor has picked
2661 * based on the old surface dimensions
2663 if (sx > window->main_surface->allocation.width ||
2664 sy > window->main_surface->allocation.height)
2667 if (!(input->grab && input->grab_button)) {
2668 widget = window_find_widget(window, sx, sy);
2669 input_set_focus_widget(input, widget, sx, sy);
2673 widget = input->grab;
2675 widget = input->focus_widget;
2677 if (widget->motion_handler)
2678 cursor = widget->motion_handler(input->focus_widget,
2679 input, time, sx, sy,
2682 cursor = widget->default_cursor;
2684 cursor = CURSOR_LEFT_PTR;
2686 input_set_pointer_image(input, cursor);
2690 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2691 uint32_t time, uint32_t button, uint32_t state_w)
2693 struct input *input = data;
2694 struct widget *widget;
2695 enum wl_pointer_button_state state = state_w;
2697 input->display->serial = serial;
2698 if (input->focus_widget && input->grab == NULL &&
2699 state == WL_POINTER_BUTTON_STATE_PRESSED)
2700 input_grab(input, input->focus_widget, button);
2702 widget = input->grab;
2703 if (widget && widget->button_handler)
2704 (*widget->button_handler)(widget,
2707 input->grab->user_data);
2709 if (input->grab && input->grab_button == button &&
2710 state == WL_POINTER_BUTTON_STATE_RELEASED)
2711 input_ungrab(input);
2715 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2716 uint32_t time, uint32_t axis, wl_fixed_t value)
2718 struct input *input = data;
2719 struct widget *widget;
2721 widget = input->focus_widget;
2723 widget = input->grab;
2724 if (widget && widget->axis_handler)
2725 (*widget->axis_handler)(widget,
2731 static const struct wl_pointer_listener pointer_listener = {
2732 pointer_handle_enter,
2733 pointer_handle_leave,
2734 pointer_handle_motion,
2735 pointer_handle_button,
2736 pointer_handle_axis,
2740 input_remove_keyboard_focus(struct input *input)
2742 struct window *window = input->keyboard_focus;
2743 struct itimerspec its;
2745 its.it_interval.tv_sec = 0;
2746 its.it_interval.tv_nsec = 0;
2747 its.it_value.tv_sec = 0;
2748 its.it_value.tv_nsec = 0;
2749 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2754 if (window->keyboard_focus_handler)
2755 (*window->keyboard_focus_handler)(window, NULL,
2758 input->keyboard_focus = NULL;
2762 keyboard_repeat_func(struct task *task, uint32_t events)
2764 struct input *input =
2765 container_of(task, struct input, repeat_task);
2766 struct window *window = input->keyboard_focus;
2769 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2770 /* If we change the timer between the fd becoming
2771 * readable and getting here, there'll be nothing to
2772 * read and we get EAGAIN. */
2775 if (window && window->key_handler) {
2776 (*window->key_handler)(window, input, input->repeat_time,
2777 input->repeat_key, input->repeat_sym,
2778 WL_KEYBOARD_KEY_STATE_PRESSED,
2784 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2785 uint32_t format, int fd, uint32_t size)
2787 struct input *input = data;
2788 struct xkb_keymap *keymap;
2789 struct xkb_state *state;
2797 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2802 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2803 if (map_str == MAP_FAILED) {
2808 keymap = xkb_map_new_from_string(input->display->xkb_context,
2810 XKB_KEYMAP_FORMAT_TEXT_V1,
2812 munmap(map_str, size);
2816 fprintf(stderr, "failed to compile keymap\n");
2820 state = xkb_state_new(keymap);
2822 fprintf(stderr, "failed to create XKB state\n");
2823 xkb_map_unref(keymap);
2827 xkb_keymap_unref(input->xkb.keymap);
2828 xkb_state_unref(input->xkb.state);
2829 input->xkb.keymap = keymap;
2830 input->xkb.state = state;
2832 input->xkb.control_mask =
2833 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2834 input->xkb.alt_mask =
2835 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2836 input->xkb.shift_mask =
2837 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2841 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2842 uint32_t serial, struct wl_surface *surface,
2843 struct wl_array *keys)
2845 struct input *input = data;
2846 struct window *window;
2848 input->display->serial = serial;
2849 input->keyboard_focus = wl_surface_get_user_data(surface);
2851 window = input->keyboard_focus;
2852 if (window->keyboard_focus_handler)
2853 (*window->keyboard_focus_handler)(window,
2854 input, window->user_data);
2858 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2859 uint32_t serial, struct wl_surface *surface)
2861 struct input *input = data;
2863 input->display->serial = serial;
2864 input_remove_keyboard_focus(input);
2868 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2869 uint32_t serial, uint32_t time, uint32_t key,
2872 struct input *input = data;
2873 struct window *window = input->keyboard_focus;
2874 uint32_t code, num_syms;
2875 enum wl_keyboard_key_state state = state_w;
2876 const xkb_keysym_t *syms;
2878 struct itimerspec its;
2880 input->display->serial = serial;
2882 if (!window || !input->xkb.state)
2885 /* We only use input grabs for pointer events for now, so just
2886 * ignore key presses if a grab is active. We expand the key
2887 * event delivery mechanism to route events to widgets to
2888 * properly handle key grabs. In the meantime, this prevents
2889 * key event devlivery while a grab is active. */
2890 if (input->grab && input->grab_button == 0)
2893 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2895 sym = XKB_KEY_NoSymbol;
2900 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2901 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2902 window_set_maximized(window, !window->maximized);
2903 } else if (sym == XKB_KEY_F11 &&
2904 window->fullscreen_handler &&
2905 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2906 window->fullscreen_handler(window, window->user_data);
2907 } else if (sym == XKB_KEY_F4 &&
2908 input->modifiers == MOD_ALT_MASK &&
2909 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2910 window_close(window);
2911 } else if (window->key_handler) {
2912 (*window->key_handler)(window, input, time, key,
2913 sym, state, window->user_data);
2916 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2917 key == input->repeat_key) {
2918 its.it_interval.tv_sec = 0;
2919 its.it_interval.tv_nsec = 0;
2920 its.it_value.tv_sec = 0;
2921 its.it_value.tv_nsec = 0;
2922 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2923 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2924 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2925 input->repeat_sym = sym;
2926 input->repeat_key = key;
2927 input->repeat_time = time;
2928 its.it_interval.tv_sec = 0;
2929 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2930 its.it_value.tv_sec = 0;
2931 its.it_value.tv_nsec = 400 * 1000 * 1000;
2932 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2937 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2938 uint32_t serial, uint32_t mods_depressed,
2939 uint32_t mods_latched, uint32_t mods_locked,
2942 struct input *input = data;
2943 xkb_mod_mask_t mask;
2945 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2946 if (!input->xkb.keymap)
2949 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2950 mods_locked, 0, 0, group);
2951 mask = xkb_state_serialize_mods(input->xkb.state,
2952 XKB_STATE_DEPRESSED |
2954 input->modifiers = 0;
2955 if (mask & input->xkb.control_mask)
2956 input->modifiers |= MOD_CONTROL_MASK;
2957 if (mask & input->xkb.alt_mask)
2958 input->modifiers |= MOD_ALT_MASK;
2959 if (mask & input->xkb.shift_mask)
2960 input->modifiers |= MOD_SHIFT_MASK;
2963 static const struct wl_keyboard_listener keyboard_listener = {
2964 keyboard_handle_keymap,
2965 keyboard_handle_enter,
2966 keyboard_handle_leave,
2967 keyboard_handle_key,
2968 keyboard_handle_modifiers,
2972 touch_handle_down(void *data, struct wl_touch *wl_touch,
2973 uint32_t serial, uint32_t time, struct wl_surface *surface,
2974 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2976 struct input *input = data;
2977 struct widget *widget;
2978 float sx = wl_fixed_to_double(x_w);
2979 float sy = wl_fixed_to_double(y_w);
2981 input->display->serial = serial;
2982 input->touch_focus = wl_surface_get_user_data(surface);
2983 if (!input->touch_focus) {
2984 DBG("Failed to find to touch focus for surface %p\n", surface);
2988 if (surface != input->touch_focus->main_surface->surface) {
2989 DBG("Ignoring input event from subsurface %p\n", surface);
2990 input->touch_focus = NULL;
2995 widget = input->grab;
2997 widget = window_find_widget(input->touch_focus,
2998 wl_fixed_to_double(x_w),
2999 wl_fixed_to_double(y_w));
3001 struct touch_point *tp = xmalloc(sizeof *tp);
3004 tp->widget = widget;
3007 wl_list_insert(&input->touch_point_list, &tp->link);
3009 if (widget->touch_down_handler)
3010 (*widget->touch_down_handler)(widget, input,
3019 touch_handle_up(void *data, struct wl_touch *wl_touch,
3020 uint32_t serial, uint32_t time, int32_t id)
3022 struct input *input = data;
3023 struct touch_point *tp, *tmp;
3025 if (!input->touch_focus) {
3026 DBG("No touch focus found for touch up event!\n");
3030 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3034 if (tp->widget->touch_up_handler)
3035 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3037 tp->widget->user_data);
3039 wl_list_remove(&tp->link);
3047 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3048 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3050 struct input *input = data;
3051 struct touch_point *tp;
3052 float sx = wl_fixed_to_double(x_w);
3053 float sy = wl_fixed_to_double(y_w);
3055 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3057 if (!input->touch_focus) {
3058 DBG("No touch focus found for touch motion event!\n");
3062 wl_list_for_each(tp, &input->touch_point_list, link) {
3068 if (tp->widget->touch_motion_handler)
3069 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3071 tp->widget->user_data);
3077 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3079 struct input *input = data;
3080 struct touch_point *tp, *tmp;
3082 DBG("touch_handle_frame\n");
3084 if (!input->touch_focus) {
3085 DBG("No touch focus found for touch frame event!\n");
3089 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3090 if (tp->widget->touch_frame_handler)
3091 (*tp->widget->touch_frame_handler)(tp->widget, input,
3092 tp->widget->user_data);
3097 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3099 struct input *input = data;
3100 struct touch_point *tp, *tmp;
3102 DBG("touch_handle_cancel\n");
3104 if (!input->touch_focus) {
3105 DBG("No touch focus found for touch cancel event!\n");
3109 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3110 if (tp->widget->touch_cancel_handler)
3111 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3112 tp->widget->user_data);
3114 wl_list_remove(&tp->link);
3119 static const struct wl_touch_listener touch_listener = {
3122 touch_handle_motion,
3124 touch_handle_cancel,
3128 seat_handle_capabilities(void *data, struct wl_seat *seat,
3129 enum wl_seat_capability caps)
3131 struct input *input = data;
3133 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3134 input->pointer = wl_seat_get_pointer(seat);
3135 wl_pointer_set_user_data(input->pointer, input);
3136 wl_pointer_add_listener(input->pointer, &pointer_listener,
3138 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3139 wl_pointer_destroy(input->pointer);
3140 input->pointer = NULL;
3143 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3144 input->keyboard = wl_seat_get_keyboard(seat);
3145 wl_keyboard_set_user_data(input->keyboard, input);
3146 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3148 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3149 wl_keyboard_destroy(input->keyboard);
3150 input->keyboard = NULL;
3153 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3154 input->touch = wl_seat_get_touch(seat);
3155 wl_touch_set_user_data(input->touch, input);
3156 wl_touch_add_listener(input->touch, &touch_listener, input);
3157 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3158 wl_touch_destroy(input->touch);
3159 input->touch = NULL;
3164 seat_handle_name(void *data, struct wl_seat *seat,
3170 static const struct wl_seat_listener seat_listener = {
3171 seat_handle_capabilities,
3176 input_get_position(struct input *input, int32_t *x, int32_t *y)
3183 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3185 struct touch_point *tp;
3187 wl_list_for_each(tp, &input->touch_point_list, link) {
3200 input_get_display(struct input *input)
3202 return input->display;
3206 input_get_seat(struct input *input)
3212 input_get_modifiers(struct input *input)
3214 return input->modifiers;
3218 input_get_focus_widget(struct input *input)
3220 return input->focus_widget;
3224 struct wl_data_offer *offer;
3225 struct input *input;
3226 struct wl_array types;
3229 struct task io_task;
3237 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3239 struct data_offer *offer = data;
3242 p = wl_array_add(&offer->types, sizeof *p);
3246 static const struct wl_data_offer_listener data_offer_listener = {
3251 data_offer_destroy(struct data_offer *offer)
3256 if (offer->refcount == 0) {
3257 wl_data_offer_destroy(offer->offer);
3258 for (p = offer->types.data; *p; p++)
3260 wl_array_release(&offer->types);
3266 data_device_data_offer(void *data,
3267 struct wl_data_device *data_device,
3268 struct wl_data_offer *_offer)
3270 struct data_offer *offer;
3272 offer = xmalloc(sizeof *offer);
3274 wl_array_init(&offer->types);
3275 offer->refcount = 1;
3276 offer->input = data;
3277 offer->offer = _offer;
3278 wl_data_offer_add_listener(offer->offer,
3279 &data_offer_listener, offer);
3283 data_device_enter(void *data, struct wl_data_device *data_device,
3284 uint32_t serial, struct wl_surface *surface,
3285 wl_fixed_t x_w, wl_fixed_t y_w,
3286 struct wl_data_offer *offer)
3288 struct input *input = data;
3289 struct window *window;
3291 float x = wl_fixed_to_double(x_w);
3292 float y = wl_fixed_to_double(y_w);
3295 window = wl_surface_get_user_data(surface);
3296 input->drag_enter_serial = serial;
3297 input->drag_focus = window,
3301 if (!input->touch_grab)
3302 input->pointer_enter_serial = serial;
3305 input->drag_offer = wl_data_offer_get_user_data(offer);
3307 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3310 types_data = input->drag_offer->types.data;
3312 input->drag_offer = NULL;
3316 if (window->data_handler)
3317 window->data_handler(window, input, x, y, types_data,
3322 data_device_leave(void *data, struct wl_data_device *data_device)
3324 struct input *input = data;
3326 if (input->drag_offer) {
3327 data_offer_destroy(input->drag_offer);
3328 input->drag_offer = NULL;
3333 data_device_motion(void *data, struct wl_data_device *data_device,
3334 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3336 struct input *input = data;
3337 struct window *window = input->drag_focus;
3338 float x = wl_fixed_to_double(x_w);
3339 float y = wl_fixed_to_double(y_w);
3345 if (input->drag_offer)
3346 types_data = input->drag_offer->types.data;
3350 if (window->data_handler)
3351 window->data_handler(window, input, x, y, types_data,
3356 data_device_drop(void *data, struct wl_data_device *data_device)
3358 struct input *input = data;
3359 struct window *window = input->drag_focus;
3365 if (window->drop_handler)
3366 window->drop_handler(window, input,
3367 x, y, window->user_data);
3369 if (input->touch_grab)
3370 touch_ungrab(input);
3374 data_device_selection(void *data,
3375 struct wl_data_device *wl_data_device,
3376 struct wl_data_offer *offer)
3378 struct input *input = data;
3381 if (input->selection_offer)
3382 data_offer_destroy(input->selection_offer);
3385 input->selection_offer = wl_data_offer_get_user_data(offer);
3386 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3389 input->selection_offer = NULL;
3393 static const struct wl_data_device_listener data_device_listener = {
3394 data_device_data_offer,
3399 data_device_selection
3403 input_set_pointer_image_index(struct input *input, int index)
3405 struct wl_buffer *buffer;
3406 struct wl_cursor *cursor;
3407 struct wl_cursor_image *image;
3409 if (!input->pointer)
3412 cursor = input->display->cursors[input->current_cursor];
3416 if (index >= (int) cursor->image_count) {
3417 fprintf(stderr, "cursor index out of range\n");
3421 image = cursor->images[index];
3422 buffer = wl_cursor_image_get_buffer(image);
3426 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3427 wl_surface_damage(input->pointer_surface, 0, 0,
3428 image->width, image->height);
3429 wl_surface_commit(input->pointer_surface);
3430 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3431 input->pointer_surface,
3432 image->hotspot_x, image->hotspot_y);
3435 static const struct wl_callback_listener pointer_surface_listener;
3438 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3441 struct input *input = data;
3442 struct wl_cursor *cursor;
3446 assert(callback == input->cursor_frame_cb);
3447 wl_callback_destroy(callback);
3448 input->cursor_frame_cb = NULL;
3451 if (!input->pointer)
3454 if (input->current_cursor == CURSOR_BLANK) {
3455 wl_pointer_set_cursor(input->pointer,
3456 input->pointer_enter_serial,
3461 if (input->current_cursor == CURSOR_UNSET)
3463 cursor = input->display->cursors[input->current_cursor];
3467 /* FIXME We don't have the current time on the first call so we set
3468 * the animation start to the time of the first frame callback. */
3470 input->cursor_anim_start = 0;
3471 else if (input->cursor_anim_start == 0)
3472 input->cursor_anim_start = time;
3474 if (time == 0 || input->cursor_anim_start == 0)
3477 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3479 if (cursor->image_count > 1) {
3480 input->cursor_frame_cb =
3481 wl_surface_frame(input->pointer_surface);
3482 wl_callback_add_listener(input->cursor_frame_cb,
3483 &pointer_surface_listener, input);
3486 input_set_pointer_image_index(input, i);
3489 static const struct wl_callback_listener pointer_surface_listener = {
3490 pointer_surface_frame_callback
3494 input_set_pointer_image(struct input *input, int pointer)
3498 if (!input->pointer)
3501 if (input->pointer_enter_serial > input->cursor_serial)
3504 if (!force && pointer == input->current_cursor)
3507 input->current_cursor = pointer;
3508 input->cursor_serial = input->pointer_enter_serial;
3509 if (!input->cursor_frame_cb)
3510 pointer_surface_frame_callback(input, NULL, 0);
3512 /* The current frame callback may be stuck if, for instance,
3513 * the set cursor request was processed by the server after
3514 * this client lost the focus. In this case the cursor surface
3515 * might not be mapped and the frame callback wouldn't ever
3516 * complete. Send a set_cursor and attach to try to map the
3517 * cursor surface again so that the callback will finish */
3518 input_set_pointer_image_index(input, 0);
3522 struct wl_data_device *
3523 input_get_data_device(struct input *input)
3525 return input->data_device;
3529 input_set_selection(struct input *input,
3530 struct wl_data_source *source, uint32_t time)
3532 if (input->data_device)
3533 wl_data_device_set_selection(input->data_device, source, time);
3537 input_accept(struct input *input, const char *type)
3539 wl_data_offer_accept(input->drag_offer->offer,
3540 input->drag_enter_serial, type);
3544 offer_io_func(struct task *task, uint32_t events)
3546 struct data_offer *offer =
3547 container_of(task, struct data_offer, io_task);
3551 len = read(offer->fd, buffer, sizeof buffer);
3552 offer->func(buffer, len,
3553 offer->x, offer->y, offer->user_data);
3557 data_offer_destroy(offer);
3562 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3563 data_func_t func, void *user_data)
3567 if (pipe2(p, O_CLOEXEC) == -1)
3570 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3573 offer->io_task.run = offer_io_func;
3577 offer->user_data = user_data;
3579 display_watch_fd(offer->input->display,
3580 offer->fd, EPOLLIN, &offer->io_task);
3584 input_receive_drag_data(struct input *input, const char *mime_type,
3585 data_func_t func, void *data)
3587 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3588 input->drag_offer->x = input->drag_x;
3589 input->drag_offer->y = input->drag_y;
3593 input_receive_drag_data_to_fd(struct input *input,
3594 const char *mime_type, int fd)
3596 if (input->drag_offer)
3597 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3603 input_receive_selection_data(struct input *input, const char *mime_type,
3604 data_func_t func, void *data)
3608 if (input->selection_offer == NULL)
3611 for (p = input->selection_offer->types.data; *p; p++)
3612 if (strcmp(mime_type, *p) == 0)
3618 data_offer_receive_data(input->selection_offer,
3619 mime_type, func, data);
3624 input_receive_selection_data_to_fd(struct input *input,
3625 const char *mime_type, int fd)
3627 if (input->selection_offer)
3628 wl_data_offer_receive(input->selection_offer->offer,
3635 window_move(struct window *window, struct input *input, uint32_t serial)
3637 if (!window->xdg_surface)
3640 xdg_surface_move(window->xdg_surface, input->seat, serial);
3644 surface_set_synchronized(struct surface *surface)
3646 if (!surface->subsurface)
3649 if (surface->synchronized)
3652 wl_subsurface_set_sync(surface->subsurface);
3653 surface->synchronized = 1;
3657 surface_set_synchronized_default(struct surface *surface)
3659 if (!surface->subsurface)
3662 if (surface->synchronized == surface->synchronized_default)
3665 if (surface->synchronized_default)
3666 wl_subsurface_set_sync(surface->subsurface);
3668 wl_subsurface_set_desync(surface->subsurface);
3670 surface->synchronized = surface->synchronized_default;
3674 surface_resize(struct surface *surface)
3676 struct widget *widget = surface->widget;
3677 struct wl_compositor *compositor = widget->window->display->compositor;
3679 if (surface->input_region) {
3680 wl_region_destroy(surface->input_region);
3681 surface->input_region = NULL;
3684 if (surface->opaque_region)
3685 wl_region_destroy(surface->opaque_region);
3687 surface->opaque_region = wl_compositor_create_region(compositor);
3689 if (widget->resize_handler)
3690 widget->resize_handler(widget,
3691 widget->allocation.width,
3692 widget->allocation.height,
3695 if (surface->subsurface &&
3696 (surface->allocation.x != widget->allocation.x ||
3697 surface->allocation.y != widget->allocation.y)) {
3698 wl_subsurface_set_position(surface->subsurface,
3699 widget->allocation.x,
3700 widget->allocation.y);
3702 if (surface->allocation.width != widget->allocation.width ||
3703 surface->allocation.height != widget->allocation.height) {
3704 window_schedule_redraw(widget->window);
3706 surface->allocation = widget->allocation;
3709 wl_region_add(surface->opaque_region, 0, 0,
3710 widget->allocation.width,
3711 widget->allocation.height);
3715 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3718 * This hack should be removed, when EGL respects
3719 * eglSwapInterval(0).
3721 * If this window has sub-surfaces, especially a free-running
3722 * EGL-widget, we need to post the parent surface once with
3723 * all the old state to guarantee, that the EGL-widget will
3724 * receive its frame callback soon. Otherwise, a forced call
3725 * to eglSwapBuffers may end up blocking, waiting for a frame
3726 * event that will never come, because we will commit the parent
3727 * surface with all new state only after eglSwapBuffers returns.
3729 * This assumes, that:
3730 * 1. When the EGL widget's resize hook is called, it pauses.
3731 * 2. When the EGL widget's redraw hook is called, it forces a
3732 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3733 * In a single threaded application condition 1 is a no-op.
3735 * XXX: This should actually be after the surface_resize() calls,
3736 * but cannot, because then it would commit the incomplete state
3737 * accumulated from the widget resize hooks.
3739 if (window->subsurface_list.next != &window->main_surface->link ||
3740 window->subsurface_list.prev != &window->main_surface->link)
3741 wl_surface_commit(window->main_surface->surface);
3745 window_do_resize(struct window *window)
3747 struct surface *surface;
3749 widget_set_allocation(window->main_surface->widget,
3750 window->pending_allocation.x,
3751 window->pending_allocation.y,
3752 window->pending_allocation.width,
3753 window->pending_allocation.height);
3755 surface_resize(window->main_surface);
3757 /* The main surface is in the list, too. Main surface's
3758 * resize_handler is responsible for calling widget_set_allocation()
3759 * on all sub-surface root widgets, so they will be resized
3762 wl_list_for_each(surface, &window->subsurface_list, link) {
3763 if (surface == window->main_surface)
3766 surface_set_synchronized(surface);
3767 surface_resize(surface);
3770 if (!window->fullscreen && !window->maximized)
3771 window->saved_allocation = window->pending_allocation;
3775 idle_resize(struct window *window)
3777 window->resize_needed = 0;
3778 window->redraw_needed = 1;
3780 DBG("from %dx%d to %dx%d\n",
3781 window->main_surface->server_allocation.width,
3782 window->main_surface->server_allocation.height,
3783 window->pending_allocation.width,
3784 window->pending_allocation.height);
3786 hack_prevent_EGL_sub_surface_deadlock(window);
3788 window_do_resize(window);
3792 undo_resize(struct window *window)
3794 window->pending_allocation.width =
3795 window->main_surface->server_allocation.width;
3796 window->pending_allocation.height =
3797 window->main_surface->server_allocation.height;
3799 DBG("back to %dx%d\n",
3800 window->main_surface->server_allocation.width,
3801 window->main_surface->server_allocation.height);
3803 window_do_resize(window);
3805 if (window->pending_allocation.width == 0 &&
3806 window->pending_allocation.height == 0) {
3807 fprintf(stderr, "Error: Could not draw a surface, "
3808 "most likely due to insufficient disk space in "
3809 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3815 window_schedule_resize(struct window *window, int width, int height)
3817 /* We should probably get these numbers from the theme. */
3818 const int min_width = 200, min_height = 200;
3820 window->pending_allocation.x = 0;
3821 window->pending_allocation.y = 0;
3822 window->pending_allocation.width = width;
3823 window->pending_allocation.height = height;
3825 if (window->min_allocation.width == 0) {
3826 if (width < min_width && window->frame)
3827 window->min_allocation.width = min_width;
3829 window->min_allocation.width = width;
3830 if (height < min_height && window->frame)
3831 window->min_allocation.height = min_height;
3833 window->min_allocation.height = height;
3836 if (window->pending_allocation.width < window->min_allocation.width)
3837 window->pending_allocation.width = window->min_allocation.width;
3838 if (window->pending_allocation.height < window->min_allocation.height)
3839 window->pending_allocation.height = window->min_allocation.height;
3841 window->resize_needed = 1;
3842 window_schedule_redraw(window);
3846 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3848 window_schedule_resize(widget->window, width, height);
3852 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3853 int32_t width, int32_t height,
3854 struct wl_array *states, uint32_t serial)
3856 struct window *window = data;
3859 if (width > 0 && height > 0) {
3860 window_schedule_resize(window, width, height);
3862 window_schedule_resize(window,
3863 window->saved_allocation.width,
3864 window->saved_allocation.height);
3867 window->maximized = 0;
3868 window->fullscreen = 0;
3869 window->resizing = 0;
3871 wl_array_for_each(p, states) {
3872 uint32_t state = *p;
3874 case XDG_SURFACE_STATE_MAXIMIZED:
3875 window->maximized = 1;
3877 case XDG_SURFACE_STATE_FULLSCREEN:
3878 window->fullscreen = 1;
3880 case XDG_SURFACE_STATE_RESIZING:
3881 window->resizing = 1;
3889 window->next_attach_serial = serial;
3893 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
3895 struct window *window = data;
3896 window->focused = 1;
3900 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
3902 struct window *window = data;
3903 window->focused = 0;
3907 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3909 struct window *window = data;
3910 window_close(window);
3913 static const struct xdg_surface_listener xdg_surface_listener = {
3914 handle_surface_configure,
3915 handle_surface_activated,
3916 handle_surface_deactivated,
3917 handle_surface_delete,
3921 window_sync_parent(struct window *window)
3923 struct wl_surface *parent_surface;
3925 if (!window->xdg_surface)
3929 parent_surface = window->parent->main_surface->surface;
3931 parent_surface = NULL;
3933 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3937 window_sync_margin(struct window *window)
3941 if (!window->xdg_surface)
3947 margin = frame_get_shadow_margin(window->frame->frame);
3949 /* Shadow size is the same on every side. */
3950 xdg_surface_set_margin(window->xdg_surface,
3958 window_flush(struct window *window)
3960 struct surface *surface;
3962 if (!window->custom) {
3963 if (window->xdg_surface) {
3964 window_sync_parent(window);
3965 window_sync_margin(window);
3969 wl_list_for_each(surface, &window->subsurface_list, link) {
3970 if (surface == window->main_surface)
3973 surface_flush(surface);
3976 surface_flush(window->main_surface);
3980 menu_destroy(struct menu *menu)
3982 widget_destroy(menu->widget);
3983 window_destroy(menu->window);
3984 frame_destroy(menu->frame);
3989 window_get_allocation(struct window *window,
3990 struct rectangle *allocation)
3992 *allocation = window->main_surface->allocation;
3996 widget_redraw(struct widget *widget)
3998 struct widget *child;
4000 if (widget->redraw_handler)
4001 widget->redraw_handler(widget, widget->user_data);
4002 wl_list_for_each(child, &widget->child_list, link)
4003 widget_redraw(child);
4007 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4009 struct surface *surface = data;
4011 assert(callback == surface->frame_cb);
4012 DBG_OBJ(callback, "done\n");
4013 wl_callback_destroy(callback);
4014 surface->frame_cb = NULL;
4016 surface->last_time = time;
4018 if (surface->redraw_needed || surface->window->redraw_needed) {
4019 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4020 window_schedule_redraw_task(surface->window);
4024 static const struct wl_callback_listener listener = {
4029 surface_redraw(struct surface *surface)
4031 DBG_OBJ(surface->surface, "begin\n");
4033 if (!surface->window->redraw_needed && !surface->redraw_needed)
4036 /* Whole-window redraw forces a redraw even if the previous has
4037 * not yet hit the screen.
4039 if (surface->frame_cb) {
4040 if (!surface->window->redraw_needed)
4043 DBG_OBJ(surface->frame_cb, "cancelled\n");
4044 wl_callback_destroy(surface->frame_cb);
4047 if (surface->widget->use_cairo &&
4048 !widget_get_cairo_surface(surface->widget)) {
4049 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4053 surface->frame_cb = wl_surface_frame(surface->surface);
4054 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4055 DBG_OBJ(surface->frame_cb, "new\n");
4057 surface->redraw_needed = 0;
4058 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4059 widget_redraw(surface->widget);
4060 DBG_OBJ(surface->surface, "done\n");
4065 idle_redraw(struct task *task, uint32_t events)
4067 struct window *window = container_of(task, struct window, redraw_task);
4068 struct surface *surface;
4072 DBG(" --------- \n");
4074 wl_list_init(&window->redraw_task.link);
4075 window->redraw_task_scheduled = 0;
4077 if (window->resize_needed) {
4078 /* throttle resizing to the main surface display */
4079 if (window->main_surface->frame_cb) {
4080 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4084 idle_resize(window);
4088 if (surface_redraw(window->main_surface) < 0) {
4090 * Only main_surface failure will cause us to undo the resize.
4091 * If sub-surfaces fail, they will just be broken with old
4096 wl_list_for_each(surface, &window->subsurface_list, link) {
4097 if (surface == window->main_surface)
4100 surface_redraw(surface);
4104 window->redraw_needed = 0;
4105 window_flush(window);
4107 wl_list_for_each(surface, &window->subsurface_list, link)
4108 surface_set_synchronized_default(surface);
4110 if (resized && failed) {
4111 /* Restore widget tree to correspond to what is on screen. */
4112 undo_resize(window);
4117 window_schedule_redraw_task(struct window *window)
4119 if (!window->redraw_task_scheduled) {
4120 window->redraw_task.run = idle_redraw;
4121 display_defer(window->display, &window->redraw_task);
4122 window->redraw_task_scheduled = 1;
4127 window_schedule_redraw(struct window *window)
4129 struct surface *surface;
4131 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4133 wl_list_for_each(surface, &window->subsurface_list, link)
4134 surface->redraw_needed = 1;
4136 window_schedule_redraw_task(window);
4140 window_is_fullscreen(struct window *window)
4142 return window->fullscreen;
4146 window_set_fullscreen(struct window *window, int fullscreen)
4148 if (!window->xdg_surface)
4151 if (window->fullscreen == fullscreen)
4155 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4157 xdg_surface_unset_fullscreen(window->xdg_surface);
4161 window_is_maximized(struct window *window)
4163 return window->maximized;
4167 window_set_maximized(struct window *window, int maximized)
4169 if (!window->xdg_surface)
4172 if (window->maximized == maximized)
4176 xdg_surface_set_maximized(window->xdg_surface);
4178 xdg_surface_unset_maximized(window->xdg_surface);
4182 window_set_minimized(struct window *window)
4184 if (!window->xdg_surface)
4187 xdg_surface_set_minimized(window->xdg_surface);
4191 window_set_user_data(struct window *window, void *data)
4193 window->user_data = data;
4197 window_get_user_data(struct window *window)
4199 return window->user_data;
4203 window_set_key_handler(struct window *window,
4204 window_key_handler_t handler)
4206 window->key_handler = handler;
4210 window_set_keyboard_focus_handler(struct window *window,
4211 window_keyboard_focus_handler_t handler)
4213 window->keyboard_focus_handler = handler;
4217 window_set_data_handler(struct window *window, window_data_handler_t handler)
4219 window->data_handler = handler;
4223 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4225 window->drop_handler = handler;
4229 window_set_close_handler(struct window *window,
4230 window_close_handler_t handler)
4232 window->close_handler = handler;
4236 window_set_fullscreen_handler(struct window *window,
4237 window_fullscreen_handler_t handler)
4239 window->fullscreen_handler = handler;
4243 window_set_output_handler(struct window *window,
4244 window_output_handler_t handler)
4246 window->output_handler = handler;
4250 window_set_title(struct window *window, const char *title)
4252 free(window->title);
4253 window->title = strdup(title);
4254 if (window->frame) {
4255 frame_set_title(window->frame->frame, title);
4256 widget_schedule_redraw(window->frame->widget);
4258 if (window->xdg_surface)
4259 xdg_surface_set_title(window->xdg_surface, title);
4263 window_get_title(struct window *window)
4265 return window->title;
4269 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4271 struct text_cursor_position *text_cursor_position =
4272 window->display->text_cursor_position;
4274 if (!text_cursor_position)
4277 text_cursor_position_notify(text_cursor_position,
4278 window->main_surface->surface,
4279 wl_fixed_from_int(x),
4280 wl_fixed_from_int(y));
4284 window_damage(struct window *window, int32_t x, int32_t y,
4285 int32_t width, int32_t height)
4287 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4291 surface_enter(void *data,
4292 struct wl_surface *wl_surface, struct wl_output *wl_output)
4294 struct window *window = data;
4295 struct output *output;
4296 struct output *output_found = NULL;
4297 struct window_output *window_output;
4299 wl_list_for_each(output, &window->display->output_list, link) {
4300 if (output->output == wl_output) {
4301 output_found = output;
4309 window_output = xmalloc(sizeof *window_output);
4310 window_output->output = output_found;
4312 wl_list_insert (&window->window_output_list, &window_output->link);
4314 if (window->output_handler)
4315 window->output_handler(window, output_found, 1,
4320 surface_leave(void *data,
4321 struct wl_surface *wl_surface, struct wl_output *output)
4323 struct window *window = data;
4324 struct window_output *window_output;
4325 struct window_output *window_output_found = NULL;
4327 wl_list_for_each(window_output, &window->window_output_list, link) {
4328 if (window_output->output->output == output) {
4329 window_output_found = window_output;
4334 if (window_output_found) {
4335 wl_list_remove(&window_output_found->link);
4337 if (window->output_handler)
4338 window->output_handler(window, window_output->output,
4339 0, window->user_data);
4341 free(window_output_found);
4345 static const struct wl_surface_listener surface_listener = {
4350 static struct surface *
4351 surface_create(struct window *window)
4353 struct display *display = window->display;
4354 struct surface *surface;
4356 surface = xmalloc(sizeof *surface);
4357 memset(surface, 0, sizeof *surface);
4361 surface->window = window;
4362 surface->surface = wl_compositor_create_surface(display->compositor);
4363 surface->buffer_scale = 1;
4364 wl_surface_add_listener(surface->surface, &surface_listener, window);
4366 wl_list_insert(&window->subsurface_list, &surface->link);
4371 static enum window_buffer_type
4372 get_preferred_buffer_type(struct display *display)
4374 #ifdef HAVE_CAIRO_EGL
4375 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4376 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4379 return WINDOW_BUFFER_TYPE_SHM;
4382 static struct window *
4383 window_create_internal(struct display *display, int custom)
4385 struct window *window;
4386 struct surface *surface;
4388 window = xzalloc(sizeof *window);
4389 wl_list_init(&window->subsurface_list);
4390 window->display = display;
4392 surface = surface_create(window);
4393 window->main_surface = surface;
4395 assert(custom || display->xdg_shell);
4397 window->custom = custom;
4398 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4400 surface->buffer_type = get_preferred_buffer_type(display);
4402 wl_surface_set_user_data(surface->surface, window);
4403 wl_list_insert(display->window_list.prev, &window->link);
4404 wl_list_init(&window->redraw_task.link);
4406 wl_list_init (&window->window_output_list);
4412 window_create(struct display *display)
4414 struct window *window;
4416 window = window_create_internal(display, 0);
4418 window->xdg_surface =
4419 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4420 window->main_surface->surface);
4421 fail_on_null(window->xdg_surface);
4423 xdg_surface_set_user_data(window->xdg_surface, window);
4424 xdg_surface_add_listener(window->xdg_surface,
4425 &xdg_surface_listener, window);
4431 window_create_custom(struct display *display)
4433 return window_create_internal(display, 1);
4437 window_set_parent(struct window *window,
4438 struct window *parent_window)
4440 window->parent = parent_window;
4441 window_sync_parent(window);
4445 window_get_parent(struct window *window)
4447 return window->parent;
4451 menu_set_item(struct menu *menu, int sy)
4453 int32_t x, y, width, height;
4456 frame_interior(menu->frame, &x, &y, &width, &height);
4457 next = (sy - y) / 20;
4458 if (menu->current != next) {
4459 menu->current = next;
4460 widget_schedule_redraw(menu->widget);
4465 menu_motion_handler(struct widget *widget,
4466 struct input *input, uint32_t time,
4467 float x, float y, void *data)
4469 struct menu *menu = data;
4471 if (widget == menu->widget)
4472 menu_set_item(data, y);
4474 return CURSOR_LEFT_PTR;
4478 menu_enter_handler(struct widget *widget,
4479 struct input *input, float x, float y, void *data)
4481 struct menu *menu = data;
4483 if (widget == menu->widget)
4484 menu_set_item(data, y);
4486 return CURSOR_LEFT_PTR;
4490 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4492 struct menu *menu = data;
4494 if (widget == menu->widget)
4495 menu_set_item(data, -200);
4499 menu_button_handler(struct widget *widget,
4500 struct input *input, uint32_t time,
4501 uint32_t button, enum wl_pointer_button_state state,
4505 struct menu *menu = data;
4507 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4508 (menu->release_count > 0 || time - menu->time > 500)) {
4509 /* Either relase after press-drag-release or
4510 * click-motion-click. */
4511 menu->func(menu->parent, input,
4512 menu->current, menu->parent->user_data);
4513 input_ungrab(input);
4515 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4516 menu->release_count++;
4521 menu_touch_up_handler(struct widget *widget,
4522 struct input *input,
4528 struct menu *menu = data;
4530 input_ungrab(input);
4535 menu_redraw_handler(struct widget *widget, void *data)
4538 struct menu *menu = data;
4539 int32_t x, y, width, height, i;
4541 cr = widget_cairo_create(widget);
4543 frame_repaint(menu->frame, cr);
4544 frame_interior(menu->frame, &x, &y, &width, &height);
4546 theme_set_background_source(menu->window->display->theme,
4547 cr, THEME_FRAME_ACTIVE);
4548 cairo_rectangle(cr, x, y, width, height);
4551 cairo_select_font_face(cr, "sans",
4552 CAIRO_FONT_SLANT_NORMAL,
4553 CAIRO_FONT_WEIGHT_NORMAL);
4554 cairo_set_font_size(cr, 12);
4556 for (i = 0; i < menu->count; i++) {
4557 if (i == menu->current) {
4558 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4559 cairo_rectangle(cr, x, y + i * 20, width, 20);
4561 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4562 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4563 cairo_show_text(cr, menu->entries[i]);
4565 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4566 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4567 cairo_show_text(cr, menu->entries[i]);
4575 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4577 struct window *window = data;
4578 struct menu *menu = window->main_surface->widget->user_data;
4580 input_ungrab(menu->input);
4584 static const struct xdg_popup_listener xdg_popup_listener = {
4585 handle_popup_popup_done,
4589 window_show_menu(struct display *display,
4590 struct input *input, uint32_t time, struct window *parent,
4591 int32_t x, int32_t y,
4592 menu_func_t func, const char **entries, int count)
4594 struct window *window;
4598 menu = malloc(sizeof *menu);
4602 window = window_create_internal(parent->display, 0);
4608 menu->window = window;
4609 menu->parent = parent;
4610 menu->widget = window_add_widget(menu->window, menu);
4611 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4612 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4613 menu->frame = frame_create(window->display->theme, 0, 0,
4614 FRAME_BUTTON_NONE, NULL);
4615 fail_on_null(menu->frame);
4616 menu->entries = entries;
4617 menu->count = count;
4618 menu->release_count = 0;
4622 menu->input = input;
4626 input_ungrab(input);
4628 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4629 widget_set_enter_handler(menu->widget, menu_enter_handler);
4630 widget_set_leave_handler(menu->widget, menu_leave_handler);
4631 widget_set_motion_handler(menu->widget, menu_motion_handler);
4632 widget_set_button_handler(menu->widget, menu_button_handler);
4633 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4635 input_grab(input, menu->widget, 0);
4636 frame_resize_inside(menu->frame, 200, count * 20);
4637 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4638 window_schedule_resize(window, frame_width(menu->frame),
4639 frame_height(menu->frame));
4641 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4643 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4644 window->main_surface->surface,
4645 parent->main_surface->surface,
4647 display_get_serial(window->display),
4651 fail_on_null(window->xdg_popup);
4653 xdg_popup_set_user_data(window->xdg_popup, window);
4654 xdg_popup_add_listener(window->xdg_popup,
4655 &xdg_popup_listener, window);
4659 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4661 window->main_surface->buffer_type = type;
4664 enum window_buffer_type
4665 window_get_buffer_type(struct window *window)
4667 return window->main_surface->buffer_type;
4671 window_set_preferred_format(struct window *window,
4672 enum preferred_format format)
4674 window->preferred_format = format;
4678 window_add_subsurface(struct window *window, void *data,
4679 enum subsurface_mode default_mode)
4681 struct widget *widget;
4682 struct surface *surface;
4683 struct wl_surface *parent;
4684 struct wl_subcompositor *subcompo = window->display->subcompositor;
4686 surface = surface_create(window);
4687 surface->buffer_type = window_get_buffer_type(window);
4688 widget = widget_create(window, surface, data);
4689 wl_list_init(&widget->link);
4690 surface->widget = widget;
4692 parent = window->main_surface->surface;
4693 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4696 surface->synchronized = 1;
4698 switch (default_mode) {
4699 case SUBSURFACE_SYNCHRONIZED:
4700 surface->synchronized_default = 1;
4702 case SUBSURFACE_DESYNCHRONIZED:
4703 surface->synchronized_default = 0;
4706 assert(!"bad enum subsurface_mode");
4709 window->resize_needed = 1;
4710 window_schedule_redraw(window);
4716 display_handle_geometry(void *data,
4717 struct wl_output *wl_output,
4720 int physical_height,
4726 struct output *output = data;
4728 output->allocation.x = x;
4729 output->allocation.y = y;
4730 output->transform = transform;
4734 output->make = strdup(make);
4737 free(output->model);
4738 output->model = strdup(model);
4742 display_handle_done(void *data,
4743 struct wl_output *wl_output)
4748 display_handle_scale(void *data,
4749 struct wl_output *wl_output,
4752 struct output *output = data;
4754 output->scale = scale;
4758 display_handle_mode(void *data,
4759 struct wl_output *wl_output,
4765 struct output *output = data;
4766 struct display *display = output->display;
4768 if (flags & WL_OUTPUT_MODE_CURRENT) {
4769 output->allocation.width = width;
4770 output->allocation.height = height;
4771 if (display->output_configure_handler)
4772 (*display->output_configure_handler)(
4773 output, display->user_data);
4777 static const struct wl_output_listener output_listener = {
4778 display_handle_geometry,
4779 display_handle_mode,
4780 display_handle_done,
4781 display_handle_scale
4785 display_add_output(struct display *d, uint32_t id)
4787 struct output *output;
4789 output = xzalloc(sizeof *output);
4790 output->display = d;
4793 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4794 output->server_output_id = id;
4795 wl_list_insert(d->output_list.prev, &output->link);
4797 wl_output_add_listener(output->output, &output_listener, output);
4801 output_destroy(struct output *output)
4803 if (output->destroy_handler)
4804 (*output->destroy_handler)(output, output->user_data);
4806 wl_output_destroy(output->output);
4807 wl_list_remove(&output->link);
4812 display_destroy_output(struct display *d, uint32_t id)
4814 struct output *output;
4816 wl_list_for_each(output, &d->output_list, link) {
4817 if (output->server_output_id == id) {
4818 output_destroy(output);
4825 display_set_global_handler(struct display *display,
4826 display_global_handler_t handler)
4828 struct global *global;
4830 display->global_handler = handler;
4834 wl_list_for_each(global, &display->global_list, link)
4835 display->global_handler(display,
4836 global->name, global->interface,
4837 global->version, display->user_data);
4841 display_set_global_handler_remove(struct display *display,
4842 display_global_handler_t remove_handler)
4844 display->global_handler_remove = remove_handler;
4845 if (!remove_handler)
4850 display_set_output_configure_handler(struct display *display,
4851 display_output_handler_t handler)
4853 struct output *output;
4855 display->output_configure_handler = handler;
4859 wl_list_for_each(output, &display->output_list, link) {
4860 if (output->allocation.width == 0 &&
4861 output->allocation.height == 0)
4864 (*display->output_configure_handler)(output,
4865 display->user_data);
4870 output_set_user_data(struct output *output, void *data)
4872 output->user_data = data;
4876 output_get_user_data(struct output *output)
4878 return output->user_data;
4882 output_set_destroy_handler(struct output *output,
4883 display_output_handler_t handler)
4885 output->destroy_handler = handler;
4886 /* FIXME: implement this, once we have way to remove outputs */
4890 output_get_allocation(struct output *output, struct rectangle *base)
4892 struct rectangle allocation = output->allocation;
4894 switch (output->transform) {
4895 case WL_OUTPUT_TRANSFORM_90:
4896 case WL_OUTPUT_TRANSFORM_270:
4897 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4898 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4899 /* Swap width and height */
4900 allocation.width = output->allocation.height;
4901 allocation.height = output->allocation.width;
4909 output_get_wl_output(struct output *output)
4911 return output->output;
4914 enum wl_output_transform
4915 output_get_transform(struct output *output)
4917 return output->transform;
4921 output_get_scale(struct output *output)
4923 return output->scale;
4927 output_get_make(struct output *output)
4929 return output->make;
4933 output_get_model(struct output *output)
4935 return output->model;
4939 fini_xkb(struct input *input)
4941 xkb_state_unref(input->xkb.state);
4942 xkb_map_unref(input->xkb.keymap);
4945 #define MIN(a,b) ((a) < (b) ? a : b)
4948 display_add_input(struct display *d, uint32_t id)
4950 struct input *input;
4952 input = xzalloc(sizeof *input);
4954 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4955 MIN(d->seat_version, 3));
4956 input->touch_focus = NULL;
4957 input->pointer_focus = NULL;
4958 input->keyboard_focus = NULL;
4959 wl_list_init(&input->touch_point_list);
4960 wl_list_insert(d->input_list.prev, &input->link);
4962 wl_seat_add_listener(input->seat, &seat_listener, input);
4963 wl_seat_set_user_data(input->seat, input);
4965 if (d->data_device_manager) {
4966 input->data_device =
4967 wl_data_device_manager_get_data_device(d->data_device_manager,
4969 wl_data_device_add_listener(input->data_device,
4970 &data_device_listener,
4974 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4976 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4977 TFD_CLOEXEC | TFD_NONBLOCK);
4978 input->repeat_task.run = keyboard_repeat_func;
4979 display_watch_fd(d, input->repeat_timer_fd,
4980 EPOLLIN, &input->repeat_task);
4984 input_destroy(struct input *input)
4986 input_remove_keyboard_focus(input);
4987 input_remove_pointer_focus(input);
4989 if (input->drag_offer)
4990 data_offer_destroy(input->drag_offer);
4992 if (input->selection_offer)
4993 data_offer_destroy(input->selection_offer);
4995 if (input->data_device)
4996 wl_data_device_destroy(input->data_device);
4998 if (input->display->seat_version >= 3) {
5000 wl_pointer_release(input->pointer);
5001 if (input->keyboard)
5002 wl_keyboard_release(input->keyboard);
5007 wl_surface_destroy(input->pointer_surface);
5009 wl_list_remove(&input->link);
5010 wl_seat_destroy(input->seat);
5011 close(input->repeat_timer_fd);
5016 init_workspace_manager(struct display *d, uint32_t id)
5018 d->workspace_manager =
5019 wl_registry_bind(d->registry, id,
5020 &workspace_manager_interface, 1);
5021 if (d->workspace_manager != NULL)
5022 workspace_manager_add_listener(d->workspace_manager,
5023 &workspace_manager_listener,
5028 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5030 struct display *d = data;
5032 if (format == WL_SHM_FORMAT_RGB565)
5036 struct wl_shm_listener shm_listener = {
5041 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5043 xdg_shell_pong(shell, serial);
5046 static const struct xdg_shell_listener xdg_shell_listener = {
5050 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
5051 #ifdef static_assert
5052 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5053 "Interface version doesn't match implementation version");
5057 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5058 const char *interface, uint32_t version)
5060 struct display *d = data;
5061 struct global *global;
5063 global = xmalloc(sizeof *global);
5065 global->interface = strdup(interface);
5066 global->version = version;
5067 wl_list_insert(d->global_list.prev, &global->link);
5069 if (strcmp(interface, "wl_compositor") == 0) {
5070 d->compositor = wl_registry_bind(registry, id,
5071 &wl_compositor_interface, 3);
5072 } else if (strcmp(interface, "wl_output") == 0) {
5073 display_add_output(d, id);
5074 } else if (strcmp(interface, "wl_seat") == 0) {
5075 d->seat_version = version;
5076 display_add_input(d, id);
5077 } else if (strcmp(interface, "wl_shm") == 0) {
5078 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5079 wl_shm_add_listener(d->shm, &shm_listener, d);
5080 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5081 d->data_device_manager =
5082 wl_registry_bind(registry, id,
5083 &wl_data_device_manager_interface, 1);
5084 } else if (strcmp(interface, "xdg_shell") == 0) {
5085 d->xdg_shell = wl_registry_bind(registry, id,
5086 &xdg_shell_interface, 1);
5087 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5088 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5089 } else if (strcmp(interface, "text_cursor_position") == 0) {
5090 d->text_cursor_position =
5091 wl_registry_bind(registry, id,
5092 &text_cursor_position_interface, 1);
5093 } else if (strcmp(interface, "workspace_manager") == 0) {
5094 init_workspace_manager(d, id);
5095 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5097 wl_registry_bind(registry, id,
5098 &wl_subcompositor_interface, 1);
5101 if (d->global_handler)
5102 d->global_handler(d, id, interface, version, d->user_data);
5106 registry_handle_global_remove(void *data, struct wl_registry *registry,
5109 struct display *d = data;
5110 struct global *global;
5113 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5114 if (global->name != name)
5117 if (strcmp(global->interface, "wl_output") == 0)
5118 display_destroy_output(d, name);
5120 /* XXX: Should destroy remaining bound globals */
5122 if (d->global_handler_remove)
5123 d->global_handler_remove(d, name, global->interface,
5124 global->version, d->user_data);
5126 wl_list_remove(&global->link);
5127 free(global->interface);
5133 display_bind(struct display *display, uint32_t name,
5134 const struct wl_interface *interface, uint32_t version)
5136 return wl_registry_bind(display->registry, name, interface, version);
5139 static const struct wl_registry_listener registry_listener = {
5140 registry_handle_global,
5141 registry_handle_global_remove
5144 #ifdef HAVE_CAIRO_EGL
5146 init_egl(struct display *d)
5148 EGLint major, minor;
5151 #ifdef USE_CAIRO_GLESV2
5152 # define GL_BIT EGL_OPENGL_ES2_BIT
5154 # define GL_BIT EGL_OPENGL_BIT
5157 static const EGLint argb_cfg_attribs[] = {
5158 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5164 EGL_RENDERABLE_TYPE, GL_BIT,
5168 #ifdef USE_CAIRO_GLESV2
5169 static const EGLint context_attribs[] = {
5170 EGL_CONTEXT_CLIENT_VERSION, 2,
5173 EGLint api = EGL_OPENGL_ES_API;
5175 EGLint *context_attribs = NULL;
5176 EGLint api = EGL_OPENGL_API;
5179 d->dpy = eglGetDisplay(d->display);
5180 if (!eglInitialize(d->dpy, &major, &minor)) {
5181 fprintf(stderr, "failed to initialize EGL\n");
5185 if (!eglBindAPI(api)) {
5186 fprintf(stderr, "failed to bind EGL client API\n");
5190 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5191 &d->argb_config, 1, &n) || n != 1) {
5192 fprintf(stderr, "failed to choose argb EGL config\n");
5196 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5197 EGL_NO_CONTEXT, context_attribs);
5198 if (d->argb_ctx == NULL) {
5199 fprintf(stderr, "failed to create EGL context\n");
5203 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5204 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5205 fprintf(stderr, "failed to get cairo EGL argb device\n");
5213 fini_egl(struct display *display)
5215 cairo_device_destroy(display->argb_device);
5217 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5220 eglTerminate(display->dpy);
5226 init_dummy_surface(struct display *display)
5231 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5233 display->dummy_surface =
5234 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5236 display->dummy_surface_data = data;
5240 handle_display_data(struct task *task, uint32_t events)
5242 struct display *display =
5243 container_of(task, struct display, display_task);
5244 struct epoll_event ep;
5247 display->display_fd_events = events;
5249 if (events & EPOLLERR || events & EPOLLHUP) {
5250 display_exit(display);
5254 if (events & EPOLLIN) {
5255 ret = wl_display_dispatch(display->display);
5257 display_exit(display);
5262 if (events & EPOLLOUT) {
5263 ret = wl_display_flush(display->display);
5265 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5266 ep.data.ptr = &display->display_task;
5267 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5268 display->display_fd, &ep);
5269 } else if (ret == -1 && errno != EAGAIN) {
5270 display_exit(display);
5277 log_handler(const char *format, va_list args)
5279 vfprintf(stderr, format, args);
5283 display_create(int *argc, char *argv[])
5287 wl_log_set_handler_client(log_handler);
5289 d = zalloc(sizeof *d);
5293 d->display = wl_display_connect(NULL);
5294 if (d->display == NULL) {
5295 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5300 d->xkb_context = xkb_context_new(0);
5301 if (d->xkb_context == NULL) {
5302 fprintf(stderr, "Failed to create XKB context\n");
5307 d->epoll_fd = os_epoll_create_cloexec();
5308 d->display_fd = wl_display_get_fd(d->display);
5309 d->display_task.run = handle_display_data;
5310 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5313 wl_list_init(&d->deferred_list);
5314 wl_list_init(&d->input_list);
5315 wl_list_init(&d->output_list);
5316 wl_list_init(&d->global_list);
5319 d->workspace_count = 1;
5321 d->registry = wl_display_get_registry(d->display);
5322 wl_registry_add_listener(d->registry, ®istry_listener, d);
5324 if (wl_display_dispatch(d->display) < 0) {
5325 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5329 #ifdef HAVE_CAIRO_EGL
5330 if (init_egl(d) < 0)
5331 fprintf(stderr, "EGL does not seem to work, "
5332 "falling back to software rendering and wl_shm.\n");
5337 d->theme = theme_create();
5339 wl_list_init(&d->window_list);
5341 init_dummy_surface(d);
5347 display_destroy_outputs(struct display *display)
5350 struct output *output;
5352 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5353 output_destroy(output);
5357 display_destroy_inputs(struct display *display)
5360 struct input *input;
5362 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5363 input_destroy(input);
5367 display_destroy(struct display *display)
5369 if (!wl_list_empty(&display->window_list))
5370 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5371 wl_list_length(&display->window_list));
5373 if (!wl_list_empty(&display->deferred_list))
5374 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5376 cairo_surface_destroy(display->dummy_surface);
5377 free(display->dummy_surface_data);
5379 display_destroy_outputs(display);
5380 display_destroy_inputs(display);
5382 xkb_context_unref(display->xkb_context);
5384 theme_destroy(display->theme);
5385 destroy_cursors(display);
5387 #ifdef HAVE_CAIRO_EGL
5388 if (display->argb_device)
5392 if (display->subcompositor)
5393 wl_subcompositor_destroy(display->subcompositor);
5395 if (display->xdg_shell)
5396 xdg_shell_destroy(display->xdg_shell);
5399 wl_shm_destroy(display->shm);
5401 if (display->data_device_manager)
5402 wl_data_device_manager_destroy(display->data_device_manager);
5404 wl_compositor_destroy(display->compositor);
5405 wl_registry_destroy(display->registry);
5407 close(display->epoll_fd);
5409 if (!(display->display_fd_events & EPOLLERR) &&
5410 !(display->display_fd_events & EPOLLHUP))
5411 wl_display_flush(display->display);
5413 wl_display_disconnect(display->display);
5418 display_set_user_data(struct display *display, void *data)
5420 display->user_data = data;
5424 display_get_user_data(struct display *display)
5426 return display->user_data;
5430 display_get_display(struct display *display)
5432 return display->display;
5436 display_has_subcompositor(struct display *display)
5438 if (display->subcompositor)
5441 wl_display_roundtrip(display->display);
5443 return display->subcompositor != NULL;
5447 display_get_cairo_device(struct display *display)
5449 return display->argb_device;
5453 display_get_output(struct display *display)
5455 return container_of(display->output_list.next, struct output, link);
5458 struct wl_compositor *
5459 display_get_compositor(struct display *display)
5461 return display->compositor;
5465 display_get_serial(struct display *display)
5467 return display->serial;
5471 display_get_egl_display(struct display *d)
5476 struct wl_data_source *
5477 display_create_data_source(struct display *display)
5479 if (display->data_device_manager)
5480 return wl_data_device_manager_create_data_source(display->data_device_manager);
5486 display_get_argb_egl_config(struct display *d)
5488 return d->argb_config;
5492 display_acquire_window_surface(struct display *display,
5493 struct window *window,
5496 struct surface *surface = window->main_surface;
5498 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5501 widget_get_cairo_surface(window->main_surface->widget);
5502 return surface->toysurface->acquire(surface->toysurface, ctx);
5506 display_release_window_surface(struct display *display,
5507 struct window *window)
5509 struct surface *surface = window->main_surface;
5511 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5514 surface->toysurface->release(surface->toysurface);
5518 display_defer(struct display *display, struct task *task)
5520 wl_list_insert(&display->deferred_list, &task->link);
5524 display_watch_fd(struct display *display,
5525 int fd, uint32_t events, struct task *task)
5527 struct epoll_event ep;
5531 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5535 display_unwatch_fd(struct display *display, int fd)
5537 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5541 display_run(struct display *display)
5544 struct epoll_event ep[16];
5547 display->running = 1;
5549 while (!wl_list_empty(&display->deferred_list)) {
5550 task = container_of(display->deferred_list.prev,
5552 wl_list_remove(&task->link);
5556 wl_display_dispatch_pending(display->display);
5558 if (!display->running)
5561 ret = wl_display_flush(display->display);
5562 if (ret < 0 && errno == EAGAIN) {
5564 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5565 ep[0].data.ptr = &display->display_task;
5567 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5568 display->display_fd, &ep[0]);
5569 } else if (ret < 0) {
5573 count = epoll_wait(display->epoll_fd,
5574 ep, ARRAY_LENGTH(ep), -1);
5575 for (i = 0; i < count; i++) {
5576 task = ep[i].data.ptr;
5577 task->run(task, ep[i].events);
5583 display_exit(struct display *display)
5585 display->running = 0;
5589 keysym_modifiers_add(struct wl_array *modifiers_map,
5592 size_t len = strlen(name) + 1;
5595 p = wl_array_add(modifiers_map, len);
5600 strncpy(p, name, len);
5603 static xkb_mod_index_t
5604 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5607 xkb_mod_index_t index = 0;
5608 char *p = modifiers_map->data;
5610 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5611 if (strcmp(p, name) == 0)
5618 return XKB_MOD_INVALID;
5622 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5625 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5627 if (index == XKB_MOD_INVALID)
5628 return XKB_MOD_INVALID;
5634 fail_on_null(void *p)
5637 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5647 return fail_on_null(malloc(s));
5653 return fail_on_null(zalloc(s));
5657 xstrdup(const char *s)
5659 return fail_on_null(strdup(s));
5663 xrealloc(char *p, size_t s)
5665 return fail_on_null(realloc(p, s));