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 window->resizing = 1;
2391 xdg_surface_resize(window->xdg_surface,
2392 input_get_seat(input),
2393 window->display->serial,
2396 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2401 frame_button_handler(struct widget *widget,
2402 struct input *input, uint32_t time,
2403 uint32_t button, enum wl_pointer_button_state state,
2407 struct window_frame *frame = data;
2408 enum theme_location location;
2410 location = frame_pointer_button(frame->frame, input, button, state);
2411 frame_handle_status(frame, input, time, location);
2415 frame_touch_down_handler(struct widget *widget, struct input *input,
2416 uint32_t serial, uint32_t time, int32_t id,
2417 float x, float y, void *data)
2419 struct window_frame *frame = data;
2421 frame_touch_down(frame->frame, input, id, x, y);
2422 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2426 frame_touch_up_handler(struct widget *widget,
2427 struct input *input, uint32_t serial, uint32_t time,
2428 int32_t id, void *data)
2430 struct window_frame *frame = data;
2432 frame_touch_up(frame->frame, input, id);
2433 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2437 window_frame_create(struct window *window, void *data)
2439 struct window_frame *frame;
2442 if (window->custom) {
2443 buttons = FRAME_BUTTON_NONE;
2445 buttons = FRAME_BUTTON_ALL;
2448 frame = xzalloc(sizeof *frame);
2449 frame->frame = frame_create(window->display->theme, 0, 0,
2450 buttons, window->title);
2452 frame->widget = window_add_widget(window, frame);
2453 frame->child = widget_add_widget(frame->widget, data);
2455 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2456 widget_set_resize_handler(frame->widget, frame_resize_handler);
2457 widget_set_enter_handler(frame->widget, frame_enter_handler);
2458 widget_set_leave_handler(frame->widget, frame_leave_handler);
2459 widget_set_motion_handler(frame->widget, frame_motion_handler);
2460 widget_set_button_handler(frame->widget, frame_button_handler);
2461 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2462 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2464 window->frame = frame;
2466 return frame->child;
2470 window_frame_set_child_size(struct widget *widget, int child_width,
2473 struct display *display = widget->window->display;
2474 struct theme *t = display->theme;
2475 int decoration_width, decoration_height;
2477 int margin = widget->window->maximized ? 0 : t->margin;
2479 if (!widget->window->fullscreen) {
2480 decoration_width = (t->width + margin) * 2;
2481 decoration_height = t->width +
2482 t->titlebar_height + margin * 2;
2484 width = child_width + decoration_width;
2485 height = child_height + decoration_height;
2487 width = child_width;
2488 height = child_height;
2491 window_schedule_resize(widget->window, width, height);
2495 window_frame_destroy(struct window_frame *frame)
2497 frame_destroy(frame->frame);
2499 /* frame->child must be destroyed by the application */
2500 widget_destroy(frame->widget);
2505 input_set_focus_widget(struct input *input, struct widget *focus,
2508 struct widget *old, *widget;
2511 if (focus == input->focus_widget)
2514 old = input->focus_widget;
2518 widget = input->grab;
2519 if (widget->leave_handler)
2520 widget->leave_handler(old, input, widget->user_data);
2521 input->focus_widget = NULL;
2527 widget = input->grab;
2528 input->focus_widget = focus;
2529 if (widget->enter_handler)
2530 cursor = widget->enter_handler(focus, input, x, y,
2533 cursor = widget->default_cursor;
2535 input_set_pointer_image(input, cursor);
2540 touch_grab(struct input *input, int32_t touch_id)
2542 input->touch_grab = 1;
2543 input->touch_grab_id = touch_id;
2547 touch_ungrab(struct input *input)
2549 struct touch_point *tp, *tmp;
2551 input->touch_grab = 0;
2553 wl_list_for_each_safe(tp, tmp,
2554 &input->touch_point_list, link) {
2555 if (tp->id != input->touch_grab_id)
2557 wl_list_remove(&tp->link);
2565 input_grab(struct input *input, struct widget *widget, uint32_t button)
2567 input->grab = widget;
2568 input->grab_button = button;
2570 input_set_focus_widget(input, widget, input->sx, input->sy);
2574 input_ungrab(struct input *input)
2576 struct widget *widget;
2579 if (input->pointer_focus) {
2580 widget = window_find_widget(input->pointer_focus,
2581 input->sx, input->sy);
2582 input_set_focus_widget(input, widget, input->sx, input->sy);
2587 input_remove_pointer_focus(struct input *input)
2589 struct window *window = input->pointer_focus;
2594 input_set_focus_widget(input, NULL, 0, 0);
2596 input->pointer_focus = NULL;
2597 input->current_cursor = CURSOR_UNSET;
2601 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2602 uint32_t serial, struct wl_surface *surface,
2603 wl_fixed_t sx_w, wl_fixed_t sy_w)
2605 struct input *input = data;
2606 struct window *window;
2607 struct widget *widget;
2608 float sx = wl_fixed_to_double(sx_w);
2609 float sy = wl_fixed_to_double(sy_w);
2612 /* enter event for a window we've just destroyed */
2616 window = wl_surface_get_user_data(surface);
2617 if (surface != window->main_surface->surface) {
2618 DBG("Ignoring input event from subsurface %p\n", surface);
2622 input->display->serial = serial;
2623 input->pointer_enter_serial = serial;
2624 input->pointer_focus = window;
2626 if (window->resizing) {
2627 window->resizing = 0;
2628 /* Schedule a redraw to free the pool */
2629 window_schedule_redraw(window);
2635 widget = window_find_widget(window, sx, sy);
2636 input_set_focus_widget(input, widget, sx, sy);
2640 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2641 uint32_t serial, struct wl_surface *surface)
2643 struct input *input = data;
2645 input->display->serial = serial;
2646 input_remove_pointer_focus(input);
2650 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2651 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2653 struct input *input = data;
2654 struct window *window = input->pointer_focus;
2655 struct widget *widget;
2657 float sx = wl_fixed_to_double(sx_w);
2658 float sy = wl_fixed_to_double(sy_w);
2666 /* when making the window smaller - e.g. after a unmaximise we might
2667 * still have a pending motion event that the compositor has picked
2668 * based on the old surface dimensions
2670 if (sx > window->main_surface->allocation.width ||
2671 sy > window->main_surface->allocation.height)
2674 if (!(input->grab && input->grab_button)) {
2675 widget = window_find_widget(window, sx, sy);
2676 input_set_focus_widget(input, widget, sx, sy);
2680 widget = input->grab;
2682 widget = input->focus_widget;
2684 if (widget->motion_handler)
2685 cursor = widget->motion_handler(input->focus_widget,
2686 input, time, sx, sy,
2689 cursor = widget->default_cursor;
2691 cursor = CURSOR_LEFT_PTR;
2693 input_set_pointer_image(input, cursor);
2697 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2698 uint32_t time, uint32_t button, uint32_t state_w)
2700 struct input *input = data;
2701 struct widget *widget;
2702 enum wl_pointer_button_state state = state_w;
2704 input->display->serial = serial;
2705 if (input->focus_widget && input->grab == NULL &&
2706 state == WL_POINTER_BUTTON_STATE_PRESSED)
2707 input_grab(input, input->focus_widget, button);
2709 widget = input->grab;
2710 if (widget && widget->button_handler)
2711 (*widget->button_handler)(widget,
2714 input->grab->user_data);
2716 if (input->grab && input->grab_button == button &&
2717 state == WL_POINTER_BUTTON_STATE_RELEASED)
2718 input_ungrab(input);
2722 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2723 uint32_t time, uint32_t axis, wl_fixed_t value)
2725 struct input *input = data;
2726 struct widget *widget;
2728 widget = input->focus_widget;
2730 widget = input->grab;
2731 if (widget && widget->axis_handler)
2732 (*widget->axis_handler)(widget,
2738 static const struct wl_pointer_listener pointer_listener = {
2739 pointer_handle_enter,
2740 pointer_handle_leave,
2741 pointer_handle_motion,
2742 pointer_handle_button,
2743 pointer_handle_axis,
2747 input_remove_keyboard_focus(struct input *input)
2749 struct window *window = input->keyboard_focus;
2750 struct itimerspec its;
2752 its.it_interval.tv_sec = 0;
2753 its.it_interval.tv_nsec = 0;
2754 its.it_value.tv_sec = 0;
2755 its.it_value.tv_nsec = 0;
2756 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2761 if (window->keyboard_focus_handler)
2762 (*window->keyboard_focus_handler)(window, NULL,
2765 input->keyboard_focus = NULL;
2769 keyboard_repeat_func(struct task *task, uint32_t events)
2771 struct input *input =
2772 container_of(task, struct input, repeat_task);
2773 struct window *window = input->keyboard_focus;
2776 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2777 /* If we change the timer between the fd becoming
2778 * readable and getting here, there'll be nothing to
2779 * read and we get EAGAIN. */
2782 if (window && window->key_handler) {
2783 (*window->key_handler)(window, input, input->repeat_time,
2784 input->repeat_key, input->repeat_sym,
2785 WL_KEYBOARD_KEY_STATE_PRESSED,
2791 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2792 uint32_t format, int fd, uint32_t size)
2794 struct input *input = data;
2795 struct xkb_keymap *keymap;
2796 struct xkb_state *state;
2804 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2809 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2810 if (map_str == MAP_FAILED) {
2815 keymap = xkb_map_new_from_string(input->display->xkb_context,
2817 XKB_KEYMAP_FORMAT_TEXT_V1,
2819 munmap(map_str, size);
2823 fprintf(stderr, "failed to compile keymap\n");
2827 state = xkb_state_new(keymap);
2829 fprintf(stderr, "failed to create XKB state\n");
2830 xkb_map_unref(keymap);
2834 xkb_keymap_unref(input->xkb.keymap);
2835 xkb_state_unref(input->xkb.state);
2836 input->xkb.keymap = keymap;
2837 input->xkb.state = state;
2839 input->xkb.control_mask =
2840 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2841 input->xkb.alt_mask =
2842 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2843 input->xkb.shift_mask =
2844 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2848 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2849 uint32_t serial, struct wl_surface *surface,
2850 struct wl_array *keys)
2852 struct input *input = data;
2853 struct window *window;
2855 input->display->serial = serial;
2856 input->keyboard_focus = wl_surface_get_user_data(surface);
2858 window = input->keyboard_focus;
2859 if (window->keyboard_focus_handler)
2860 (*window->keyboard_focus_handler)(window,
2861 input, window->user_data);
2865 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2866 uint32_t serial, struct wl_surface *surface)
2868 struct input *input = data;
2870 input->display->serial = serial;
2871 input_remove_keyboard_focus(input);
2875 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2876 uint32_t serial, uint32_t time, uint32_t key,
2879 struct input *input = data;
2880 struct window *window = input->keyboard_focus;
2881 uint32_t code, num_syms;
2882 enum wl_keyboard_key_state state = state_w;
2883 const xkb_keysym_t *syms;
2885 struct itimerspec its;
2887 input->display->serial = serial;
2889 if (!window || !input->xkb.state)
2892 /* We only use input grabs for pointer events for now, so just
2893 * ignore key presses if a grab is active. We expand the key
2894 * event delivery mechanism to route events to widgets to
2895 * properly handle key grabs. In the meantime, this prevents
2896 * key event devlivery while a grab is active. */
2897 if (input->grab && input->grab_button == 0)
2900 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2902 sym = XKB_KEY_NoSymbol;
2907 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2908 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2909 window_set_maximized(window, !window->maximized);
2910 } else if (sym == XKB_KEY_F11 &&
2911 window->fullscreen_handler &&
2912 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2913 window->fullscreen_handler(window, window->user_data);
2914 } else if (sym == XKB_KEY_F4 &&
2915 input->modifiers == MOD_ALT_MASK &&
2916 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2917 window_close(window);
2918 } else if (window->key_handler) {
2919 (*window->key_handler)(window, input, time, key,
2920 sym, state, window->user_data);
2923 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2924 key == input->repeat_key) {
2925 its.it_interval.tv_sec = 0;
2926 its.it_interval.tv_nsec = 0;
2927 its.it_value.tv_sec = 0;
2928 its.it_value.tv_nsec = 0;
2929 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2930 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2931 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2932 input->repeat_sym = sym;
2933 input->repeat_key = key;
2934 input->repeat_time = time;
2935 its.it_interval.tv_sec = 0;
2936 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2937 its.it_value.tv_sec = 0;
2938 its.it_value.tv_nsec = 400 * 1000 * 1000;
2939 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2944 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2945 uint32_t serial, uint32_t mods_depressed,
2946 uint32_t mods_latched, uint32_t mods_locked,
2949 struct input *input = data;
2950 xkb_mod_mask_t mask;
2952 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2953 if (!input->xkb.keymap)
2956 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2957 mods_locked, 0, 0, group);
2958 mask = xkb_state_serialize_mods(input->xkb.state,
2959 XKB_STATE_DEPRESSED |
2961 input->modifiers = 0;
2962 if (mask & input->xkb.control_mask)
2963 input->modifiers |= MOD_CONTROL_MASK;
2964 if (mask & input->xkb.alt_mask)
2965 input->modifiers |= MOD_ALT_MASK;
2966 if (mask & input->xkb.shift_mask)
2967 input->modifiers |= MOD_SHIFT_MASK;
2970 static const struct wl_keyboard_listener keyboard_listener = {
2971 keyboard_handle_keymap,
2972 keyboard_handle_enter,
2973 keyboard_handle_leave,
2974 keyboard_handle_key,
2975 keyboard_handle_modifiers,
2979 touch_handle_down(void *data, struct wl_touch *wl_touch,
2980 uint32_t serial, uint32_t time, struct wl_surface *surface,
2981 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2983 struct input *input = data;
2984 struct widget *widget;
2985 float sx = wl_fixed_to_double(x_w);
2986 float sy = wl_fixed_to_double(y_w);
2988 input->display->serial = serial;
2989 input->touch_focus = wl_surface_get_user_data(surface);
2990 if (!input->touch_focus) {
2991 DBG("Failed to find to touch focus for surface %p\n", surface);
2995 if (surface != input->touch_focus->main_surface->surface) {
2996 DBG("Ignoring input event from subsurface %p\n", surface);
2997 input->touch_focus = NULL;
3002 widget = input->grab;
3004 widget = window_find_widget(input->touch_focus,
3005 wl_fixed_to_double(x_w),
3006 wl_fixed_to_double(y_w));
3008 struct touch_point *tp = xmalloc(sizeof *tp);
3011 tp->widget = widget;
3014 wl_list_insert(&input->touch_point_list, &tp->link);
3016 if (widget->touch_down_handler)
3017 (*widget->touch_down_handler)(widget, input,
3026 touch_handle_up(void *data, struct wl_touch *wl_touch,
3027 uint32_t serial, uint32_t time, int32_t id)
3029 struct input *input = data;
3030 struct touch_point *tp, *tmp;
3032 if (!input->touch_focus) {
3033 DBG("No touch focus found for touch up event!\n");
3037 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3041 if (tp->widget->touch_up_handler)
3042 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3044 tp->widget->user_data);
3046 wl_list_remove(&tp->link);
3054 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3055 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3057 struct input *input = data;
3058 struct touch_point *tp;
3059 float sx = wl_fixed_to_double(x_w);
3060 float sy = wl_fixed_to_double(y_w);
3062 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3064 if (!input->touch_focus) {
3065 DBG("No touch focus found for touch motion event!\n");
3069 wl_list_for_each(tp, &input->touch_point_list, link) {
3075 if (tp->widget->touch_motion_handler)
3076 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3078 tp->widget->user_data);
3084 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3086 struct input *input = data;
3087 struct touch_point *tp, *tmp;
3089 DBG("touch_handle_frame\n");
3091 if (!input->touch_focus) {
3092 DBG("No touch focus found for touch frame event!\n");
3096 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3097 if (tp->widget->touch_frame_handler)
3098 (*tp->widget->touch_frame_handler)(tp->widget, input,
3099 tp->widget->user_data);
3104 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3106 struct input *input = data;
3107 struct touch_point *tp, *tmp;
3109 DBG("touch_handle_cancel\n");
3111 if (!input->touch_focus) {
3112 DBG("No touch focus found for touch cancel event!\n");
3116 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3117 if (tp->widget->touch_cancel_handler)
3118 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3119 tp->widget->user_data);
3121 wl_list_remove(&tp->link);
3126 static const struct wl_touch_listener touch_listener = {
3129 touch_handle_motion,
3131 touch_handle_cancel,
3135 seat_handle_capabilities(void *data, struct wl_seat *seat,
3136 enum wl_seat_capability caps)
3138 struct input *input = data;
3140 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3141 input->pointer = wl_seat_get_pointer(seat);
3142 wl_pointer_set_user_data(input->pointer, input);
3143 wl_pointer_add_listener(input->pointer, &pointer_listener,
3145 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3146 wl_pointer_destroy(input->pointer);
3147 input->pointer = NULL;
3150 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3151 input->keyboard = wl_seat_get_keyboard(seat);
3152 wl_keyboard_set_user_data(input->keyboard, input);
3153 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3155 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3156 wl_keyboard_destroy(input->keyboard);
3157 input->keyboard = NULL;
3160 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3161 input->touch = wl_seat_get_touch(seat);
3162 wl_touch_set_user_data(input->touch, input);
3163 wl_touch_add_listener(input->touch, &touch_listener, input);
3164 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3165 wl_touch_destroy(input->touch);
3166 input->touch = NULL;
3171 seat_handle_name(void *data, struct wl_seat *seat,
3177 static const struct wl_seat_listener seat_listener = {
3178 seat_handle_capabilities,
3183 input_get_position(struct input *input, int32_t *x, int32_t *y)
3190 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3192 struct touch_point *tp;
3194 wl_list_for_each(tp, &input->touch_point_list, link) {
3207 input_get_display(struct input *input)
3209 return input->display;
3213 input_get_seat(struct input *input)
3219 input_get_modifiers(struct input *input)
3221 return input->modifiers;
3225 input_get_focus_widget(struct input *input)
3227 return input->focus_widget;
3231 struct wl_data_offer *offer;
3232 struct input *input;
3233 struct wl_array types;
3236 struct task io_task;
3244 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3246 struct data_offer *offer = data;
3249 p = wl_array_add(&offer->types, sizeof *p);
3253 static const struct wl_data_offer_listener data_offer_listener = {
3258 data_offer_destroy(struct data_offer *offer)
3263 if (offer->refcount == 0) {
3264 wl_data_offer_destroy(offer->offer);
3265 for (p = offer->types.data; *p; p++)
3267 wl_array_release(&offer->types);
3273 data_device_data_offer(void *data,
3274 struct wl_data_device *data_device,
3275 struct wl_data_offer *_offer)
3277 struct data_offer *offer;
3279 offer = xmalloc(sizeof *offer);
3281 wl_array_init(&offer->types);
3282 offer->refcount = 1;
3283 offer->input = data;
3284 offer->offer = _offer;
3285 wl_data_offer_add_listener(offer->offer,
3286 &data_offer_listener, offer);
3290 data_device_enter(void *data, struct wl_data_device *data_device,
3291 uint32_t serial, struct wl_surface *surface,
3292 wl_fixed_t x_w, wl_fixed_t y_w,
3293 struct wl_data_offer *offer)
3295 struct input *input = data;
3296 struct window *window;
3298 float x = wl_fixed_to_double(x_w);
3299 float y = wl_fixed_to_double(y_w);
3302 window = wl_surface_get_user_data(surface);
3303 input->drag_enter_serial = serial;
3304 input->drag_focus = window,
3308 if (!input->touch_grab)
3309 input->pointer_enter_serial = serial;
3312 input->drag_offer = wl_data_offer_get_user_data(offer);
3314 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3317 types_data = input->drag_offer->types.data;
3319 input->drag_offer = NULL;
3323 if (window->data_handler)
3324 window->data_handler(window, input, x, y, types_data,
3329 data_device_leave(void *data, struct wl_data_device *data_device)
3331 struct input *input = data;
3333 if (input->drag_offer) {
3334 data_offer_destroy(input->drag_offer);
3335 input->drag_offer = NULL;
3340 data_device_motion(void *data, struct wl_data_device *data_device,
3341 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3343 struct input *input = data;
3344 struct window *window = input->drag_focus;
3345 float x = wl_fixed_to_double(x_w);
3346 float y = wl_fixed_to_double(y_w);
3352 if (input->drag_offer)
3353 types_data = input->drag_offer->types.data;
3357 if (window->data_handler)
3358 window->data_handler(window, input, x, y, types_data,
3363 data_device_drop(void *data, struct wl_data_device *data_device)
3365 struct input *input = data;
3366 struct window *window = input->drag_focus;
3372 if (window->drop_handler)
3373 window->drop_handler(window, input,
3374 x, y, window->user_data);
3376 if (input->touch_grab)
3377 touch_ungrab(input);
3381 data_device_selection(void *data,
3382 struct wl_data_device *wl_data_device,
3383 struct wl_data_offer *offer)
3385 struct input *input = data;
3388 if (input->selection_offer)
3389 data_offer_destroy(input->selection_offer);
3392 input->selection_offer = wl_data_offer_get_user_data(offer);
3393 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3396 input->selection_offer = NULL;
3400 static const struct wl_data_device_listener data_device_listener = {
3401 data_device_data_offer,
3406 data_device_selection
3410 input_set_pointer_image_index(struct input *input, int index)
3412 struct wl_buffer *buffer;
3413 struct wl_cursor *cursor;
3414 struct wl_cursor_image *image;
3416 if (!input->pointer)
3419 cursor = input->display->cursors[input->current_cursor];
3423 if (index >= (int) cursor->image_count) {
3424 fprintf(stderr, "cursor index out of range\n");
3428 image = cursor->images[index];
3429 buffer = wl_cursor_image_get_buffer(image);
3433 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3434 wl_surface_damage(input->pointer_surface, 0, 0,
3435 image->width, image->height);
3436 wl_surface_commit(input->pointer_surface);
3437 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3438 input->pointer_surface,
3439 image->hotspot_x, image->hotspot_y);
3442 static const struct wl_callback_listener pointer_surface_listener;
3445 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3448 struct input *input = data;
3449 struct wl_cursor *cursor;
3453 assert(callback == input->cursor_frame_cb);
3454 wl_callback_destroy(callback);
3455 input->cursor_frame_cb = NULL;
3458 if (!input->pointer)
3461 if (input->current_cursor == CURSOR_BLANK) {
3462 wl_pointer_set_cursor(input->pointer,
3463 input->pointer_enter_serial,
3468 if (input->current_cursor == CURSOR_UNSET)
3470 cursor = input->display->cursors[input->current_cursor];
3474 /* FIXME We don't have the current time on the first call so we set
3475 * the animation start to the time of the first frame callback. */
3477 input->cursor_anim_start = 0;
3478 else if (input->cursor_anim_start == 0)
3479 input->cursor_anim_start = time;
3481 if (time == 0 || input->cursor_anim_start == 0)
3484 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3486 if (cursor->image_count > 1) {
3487 input->cursor_frame_cb =
3488 wl_surface_frame(input->pointer_surface);
3489 wl_callback_add_listener(input->cursor_frame_cb,
3490 &pointer_surface_listener, input);
3493 input_set_pointer_image_index(input, i);
3496 static const struct wl_callback_listener pointer_surface_listener = {
3497 pointer_surface_frame_callback
3501 input_set_pointer_image(struct input *input, int pointer)
3505 if (!input->pointer)
3508 if (input->pointer_enter_serial > input->cursor_serial)
3511 if (!force && pointer == input->current_cursor)
3514 input->current_cursor = pointer;
3515 input->cursor_serial = input->pointer_enter_serial;
3516 if (!input->cursor_frame_cb)
3517 pointer_surface_frame_callback(input, NULL, 0);
3519 /* The current frame callback may be stuck if, for instance,
3520 * the set cursor request was processed by the server after
3521 * this client lost the focus. In this case the cursor surface
3522 * might not be mapped and the frame callback wouldn't ever
3523 * complete. Send a set_cursor and attach to try to map the
3524 * cursor surface again so that the callback will finish */
3525 input_set_pointer_image_index(input, 0);
3529 struct wl_data_device *
3530 input_get_data_device(struct input *input)
3532 return input->data_device;
3536 input_set_selection(struct input *input,
3537 struct wl_data_source *source, uint32_t time)
3539 if (input->data_device)
3540 wl_data_device_set_selection(input->data_device, source, time);
3544 input_accept(struct input *input, const char *type)
3546 wl_data_offer_accept(input->drag_offer->offer,
3547 input->drag_enter_serial, type);
3551 offer_io_func(struct task *task, uint32_t events)
3553 struct data_offer *offer =
3554 container_of(task, struct data_offer, io_task);
3558 len = read(offer->fd, buffer, sizeof buffer);
3559 offer->func(buffer, len,
3560 offer->x, offer->y, offer->user_data);
3564 data_offer_destroy(offer);
3569 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3570 data_func_t func, void *user_data)
3574 if (pipe2(p, O_CLOEXEC) == -1)
3577 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3580 offer->io_task.run = offer_io_func;
3584 offer->user_data = user_data;
3586 display_watch_fd(offer->input->display,
3587 offer->fd, EPOLLIN, &offer->io_task);
3591 input_receive_drag_data(struct input *input, const char *mime_type,
3592 data_func_t func, void *data)
3594 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3595 input->drag_offer->x = input->drag_x;
3596 input->drag_offer->y = input->drag_y;
3600 input_receive_drag_data_to_fd(struct input *input,
3601 const char *mime_type, int fd)
3603 if (input->drag_offer)
3604 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3610 input_receive_selection_data(struct input *input, const char *mime_type,
3611 data_func_t func, void *data)
3615 if (input->selection_offer == NULL)
3618 for (p = input->selection_offer->types.data; *p; p++)
3619 if (strcmp(mime_type, *p) == 0)
3625 data_offer_receive_data(input->selection_offer,
3626 mime_type, func, data);
3631 input_receive_selection_data_to_fd(struct input *input,
3632 const char *mime_type, int fd)
3634 if (input->selection_offer)
3635 wl_data_offer_receive(input->selection_offer->offer,
3642 window_move(struct window *window, struct input *input, uint32_t serial)
3644 if (!window->xdg_surface)
3647 xdg_surface_move(window->xdg_surface, input->seat, serial);
3651 surface_set_synchronized(struct surface *surface)
3653 if (!surface->subsurface)
3656 if (surface->synchronized)
3659 wl_subsurface_set_sync(surface->subsurface);
3660 surface->synchronized = 1;
3664 surface_set_synchronized_default(struct surface *surface)
3666 if (!surface->subsurface)
3669 if (surface->synchronized == surface->synchronized_default)
3672 if (surface->synchronized_default)
3673 wl_subsurface_set_sync(surface->subsurface);
3675 wl_subsurface_set_desync(surface->subsurface);
3677 surface->synchronized = surface->synchronized_default;
3681 surface_resize(struct surface *surface)
3683 struct widget *widget = surface->widget;
3684 struct wl_compositor *compositor = widget->window->display->compositor;
3686 if (surface->input_region) {
3687 wl_region_destroy(surface->input_region);
3688 surface->input_region = NULL;
3691 if (surface->opaque_region)
3692 wl_region_destroy(surface->opaque_region);
3694 surface->opaque_region = wl_compositor_create_region(compositor);
3696 if (widget->resize_handler)
3697 widget->resize_handler(widget,
3698 widget->allocation.width,
3699 widget->allocation.height,
3702 if (surface->subsurface &&
3703 (surface->allocation.x != widget->allocation.x ||
3704 surface->allocation.y != widget->allocation.y)) {
3705 wl_subsurface_set_position(surface->subsurface,
3706 widget->allocation.x,
3707 widget->allocation.y);
3709 if (surface->allocation.width != widget->allocation.width ||
3710 surface->allocation.height != widget->allocation.height) {
3711 window_schedule_redraw(widget->window);
3713 surface->allocation = widget->allocation;
3716 wl_region_add(surface->opaque_region, 0, 0,
3717 widget->allocation.width,
3718 widget->allocation.height);
3722 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3725 * This hack should be removed, when EGL respects
3726 * eglSwapInterval(0).
3728 * If this window has sub-surfaces, especially a free-running
3729 * EGL-widget, we need to post the parent surface once with
3730 * all the old state to guarantee, that the EGL-widget will
3731 * receive its frame callback soon. Otherwise, a forced call
3732 * to eglSwapBuffers may end up blocking, waiting for a frame
3733 * event that will never come, because we will commit the parent
3734 * surface with all new state only after eglSwapBuffers returns.
3736 * This assumes, that:
3737 * 1. When the EGL widget's resize hook is called, it pauses.
3738 * 2. When the EGL widget's redraw hook is called, it forces a
3739 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3740 * In a single threaded application condition 1 is a no-op.
3742 * XXX: This should actually be after the surface_resize() calls,
3743 * but cannot, because then it would commit the incomplete state
3744 * accumulated from the widget resize hooks.
3746 if (window->subsurface_list.next != &window->main_surface->link ||
3747 window->subsurface_list.prev != &window->main_surface->link)
3748 wl_surface_commit(window->main_surface->surface);
3752 window_do_resize(struct window *window)
3754 struct surface *surface;
3756 widget_set_allocation(window->main_surface->widget,
3757 window->pending_allocation.x,
3758 window->pending_allocation.y,
3759 window->pending_allocation.width,
3760 window->pending_allocation.height);
3762 surface_resize(window->main_surface);
3764 /* The main surface is in the list, too. Main surface's
3765 * resize_handler is responsible for calling widget_set_allocation()
3766 * on all sub-surface root widgets, so they will be resized
3769 wl_list_for_each(surface, &window->subsurface_list, link) {
3770 if (surface == window->main_surface)
3773 surface_set_synchronized(surface);
3774 surface_resize(surface);
3777 if (!window->fullscreen && !window->maximized)
3778 window->saved_allocation = window->pending_allocation;
3782 idle_resize(struct window *window)
3784 window->resize_needed = 0;
3785 window->redraw_needed = 1;
3787 DBG("from %dx%d to %dx%d\n",
3788 window->main_surface->server_allocation.width,
3789 window->main_surface->server_allocation.height,
3790 window->pending_allocation.width,
3791 window->pending_allocation.height);
3793 hack_prevent_EGL_sub_surface_deadlock(window);
3795 window_do_resize(window);
3799 undo_resize(struct window *window)
3801 window->pending_allocation.width =
3802 window->main_surface->server_allocation.width;
3803 window->pending_allocation.height =
3804 window->main_surface->server_allocation.height;
3806 DBG("back to %dx%d\n",
3807 window->main_surface->server_allocation.width,
3808 window->main_surface->server_allocation.height);
3810 window_do_resize(window);
3812 if (window->pending_allocation.width == 0 &&
3813 window->pending_allocation.height == 0) {
3814 fprintf(stderr, "Error: Could not draw a surface, "
3815 "most likely due to insufficient disk space in "
3816 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3822 window_schedule_resize(struct window *window, int width, int height)
3824 /* We should probably get these numbers from the theme. */
3825 const int min_width = 200, min_height = 200;
3827 window->pending_allocation.x = 0;
3828 window->pending_allocation.y = 0;
3829 window->pending_allocation.width = width;
3830 window->pending_allocation.height = height;
3832 if (window->min_allocation.width == 0) {
3833 if (width < min_width && window->frame)
3834 window->min_allocation.width = min_width;
3836 window->min_allocation.width = width;
3837 if (height < min_height && window->frame)
3838 window->min_allocation.height = min_height;
3840 window->min_allocation.height = height;
3843 if (window->pending_allocation.width < window->min_allocation.width)
3844 window->pending_allocation.width = window->min_allocation.width;
3845 if (window->pending_allocation.height < window->min_allocation.height)
3846 window->pending_allocation.height = window->min_allocation.height;
3848 window->resize_needed = 1;
3849 window_schedule_redraw(window);
3853 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3855 window_schedule_resize(widget->window, width, height);
3859 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3860 int32_t width, int32_t height,
3861 struct wl_array *states, uint32_t serial)
3863 struct window *window = data;
3866 if (width > 0 && height > 0) {
3867 window_schedule_resize(window, width, height);
3869 window_schedule_resize(window,
3870 window->saved_allocation.width,
3871 window->saved_allocation.height);
3874 window->maximized = 0;
3875 window->fullscreen = 0;
3877 wl_array_for_each(p, states) {
3878 uint32_t state = *p;
3880 case XDG_SURFACE_STATE_MAXIMIZED:
3881 window->maximized = 1;
3883 case XDG_SURFACE_STATE_FULLSCREEN:
3884 window->fullscreen = 1;
3892 window->next_attach_serial = serial;
3896 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
3898 struct window *window = data;
3899 window->focused = 1;
3903 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
3905 struct window *window = data;
3906 window->focused = 0;
3910 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3912 struct window *window = data;
3913 window_close(window);
3916 static const struct xdg_surface_listener xdg_surface_listener = {
3917 handle_surface_configure,
3918 handle_surface_activated,
3919 handle_surface_deactivated,
3920 handle_surface_delete,
3924 window_sync_parent(struct window *window)
3926 struct wl_surface *parent_surface;
3928 if (!window->xdg_surface)
3932 parent_surface = window->parent->main_surface->surface;
3934 parent_surface = NULL;
3936 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3940 window_sync_margin(struct window *window)
3944 if (!window->xdg_surface)
3950 margin = frame_get_shadow_margin(window->frame->frame);
3952 /* Shadow size is the same on every side. */
3953 xdg_surface_set_margin(window->xdg_surface,
3961 window_flush(struct window *window)
3963 struct surface *surface;
3965 if (!window->custom) {
3966 if (window->xdg_surface) {
3967 window_sync_parent(window);
3968 window_sync_margin(window);
3972 wl_list_for_each(surface, &window->subsurface_list, link) {
3973 if (surface == window->main_surface)
3976 surface_flush(surface);
3979 surface_flush(window->main_surface);
3983 menu_destroy(struct menu *menu)
3985 widget_destroy(menu->widget);
3986 window_destroy(menu->window);
3987 frame_destroy(menu->frame);
3992 window_get_allocation(struct window *window,
3993 struct rectangle *allocation)
3995 *allocation = window->main_surface->allocation;
3999 widget_redraw(struct widget *widget)
4001 struct widget *child;
4003 if (widget->redraw_handler)
4004 widget->redraw_handler(widget, widget->user_data);
4005 wl_list_for_each(child, &widget->child_list, link)
4006 widget_redraw(child);
4010 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4012 struct surface *surface = data;
4014 assert(callback == surface->frame_cb);
4015 DBG_OBJ(callback, "done\n");
4016 wl_callback_destroy(callback);
4017 surface->frame_cb = NULL;
4019 surface->last_time = time;
4021 if (surface->redraw_needed || surface->window->redraw_needed) {
4022 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4023 window_schedule_redraw_task(surface->window);
4027 static const struct wl_callback_listener listener = {
4032 surface_redraw(struct surface *surface)
4034 DBG_OBJ(surface->surface, "begin\n");
4036 if (!surface->window->redraw_needed && !surface->redraw_needed)
4039 /* Whole-window redraw forces a redraw even if the previous has
4040 * not yet hit the screen.
4042 if (surface->frame_cb) {
4043 if (!surface->window->redraw_needed)
4046 DBG_OBJ(surface->frame_cb, "cancelled\n");
4047 wl_callback_destroy(surface->frame_cb);
4050 if (surface->widget->use_cairo &&
4051 !widget_get_cairo_surface(surface->widget)) {
4052 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4056 surface->frame_cb = wl_surface_frame(surface->surface);
4057 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4058 DBG_OBJ(surface->frame_cb, "new\n");
4060 surface->redraw_needed = 0;
4061 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4062 widget_redraw(surface->widget);
4063 DBG_OBJ(surface->surface, "done\n");
4068 idle_redraw(struct task *task, uint32_t events)
4070 struct window *window = container_of(task, struct window, redraw_task);
4071 struct surface *surface;
4075 DBG(" --------- \n");
4077 wl_list_init(&window->redraw_task.link);
4078 window->redraw_task_scheduled = 0;
4080 if (window->resize_needed) {
4081 /* throttle resizing to the main surface display */
4082 if (window->main_surface->frame_cb) {
4083 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4087 idle_resize(window);
4091 if (surface_redraw(window->main_surface) < 0) {
4093 * Only main_surface failure will cause us to undo the resize.
4094 * If sub-surfaces fail, they will just be broken with old
4099 wl_list_for_each(surface, &window->subsurface_list, link) {
4100 if (surface == window->main_surface)
4103 surface_redraw(surface);
4107 window->redraw_needed = 0;
4108 window_flush(window);
4110 wl_list_for_each(surface, &window->subsurface_list, link)
4111 surface_set_synchronized_default(surface);
4113 if (resized && failed) {
4114 /* Restore widget tree to correspond to what is on screen. */
4115 undo_resize(window);
4120 window_schedule_redraw_task(struct window *window)
4122 if (!window->redraw_task_scheduled) {
4123 window->redraw_task.run = idle_redraw;
4124 display_defer(window->display, &window->redraw_task);
4125 window->redraw_task_scheduled = 1;
4130 window_schedule_redraw(struct window *window)
4132 struct surface *surface;
4134 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4136 wl_list_for_each(surface, &window->subsurface_list, link)
4137 surface->redraw_needed = 1;
4139 window_schedule_redraw_task(window);
4143 window_is_fullscreen(struct window *window)
4145 return window->fullscreen;
4149 window_set_fullscreen(struct window *window, int fullscreen)
4151 if (!window->xdg_surface)
4154 if (window->fullscreen == fullscreen)
4158 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4160 xdg_surface_unset_fullscreen(window->xdg_surface);
4164 window_is_maximized(struct window *window)
4166 return window->maximized;
4170 window_set_maximized(struct window *window, int maximized)
4172 if (!window->xdg_surface)
4175 if (window->maximized == maximized)
4179 xdg_surface_set_maximized(window->xdg_surface);
4181 xdg_surface_unset_maximized(window->xdg_surface);
4185 window_set_minimized(struct window *window)
4187 if (!window->xdg_surface)
4190 xdg_surface_set_minimized(window->xdg_surface);
4194 window_set_user_data(struct window *window, void *data)
4196 window->user_data = data;
4200 window_get_user_data(struct window *window)
4202 return window->user_data;
4206 window_set_key_handler(struct window *window,
4207 window_key_handler_t handler)
4209 window->key_handler = handler;
4213 window_set_keyboard_focus_handler(struct window *window,
4214 window_keyboard_focus_handler_t handler)
4216 window->keyboard_focus_handler = handler;
4220 window_set_data_handler(struct window *window, window_data_handler_t handler)
4222 window->data_handler = handler;
4226 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4228 window->drop_handler = handler;
4232 window_set_close_handler(struct window *window,
4233 window_close_handler_t handler)
4235 window->close_handler = handler;
4239 window_set_fullscreen_handler(struct window *window,
4240 window_fullscreen_handler_t handler)
4242 window->fullscreen_handler = handler;
4246 window_set_output_handler(struct window *window,
4247 window_output_handler_t handler)
4249 window->output_handler = handler;
4253 window_set_title(struct window *window, const char *title)
4255 free(window->title);
4256 window->title = strdup(title);
4257 if (window->frame) {
4258 frame_set_title(window->frame->frame, title);
4259 widget_schedule_redraw(window->frame->widget);
4261 if (window->xdg_surface)
4262 xdg_surface_set_title(window->xdg_surface, title);
4266 window_get_title(struct window *window)
4268 return window->title;
4272 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4274 struct text_cursor_position *text_cursor_position =
4275 window->display->text_cursor_position;
4277 if (!text_cursor_position)
4280 text_cursor_position_notify(text_cursor_position,
4281 window->main_surface->surface,
4282 wl_fixed_from_int(x),
4283 wl_fixed_from_int(y));
4287 window_damage(struct window *window, int32_t x, int32_t y,
4288 int32_t width, int32_t height)
4290 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4294 surface_enter(void *data,
4295 struct wl_surface *wl_surface, struct wl_output *wl_output)
4297 struct window *window = data;
4298 struct output *output;
4299 struct output *output_found = NULL;
4300 struct window_output *window_output;
4302 wl_list_for_each(output, &window->display->output_list, link) {
4303 if (output->output == wl_output) {
4304 output_found = output;
4312 window_output = xmalloc(sizeof *window_output);
4313 window_output->output = output_found;
4315 wl_list_insert (&window->window_output_list, &window_output->link);
4317 if (window->output_handler)
4318 window->output_handler(window, output_found, 1,
4323 surface_leave(void *data,
4324 struct wl_surface *wl_surface, struct wl_output *output)
4326 struct window *window = data;
4327 struct window_output *window_output;
4328 struct window_output *window_output_found = NULL;
4330 wl_list_for_each(window_output, &window->window_output_list, link) {
4331 if (window_output->output->output == output) {
4332 window_output_found = window_output;
4337 if (window_output_found) {
4338 wl_list_remove(&window_output_found->link);
4340 if (window->output_handler)
4341 window->output_handler(window, window_output->output,
4342 0, window->user_data);
4344 free(window_output_found);
4348 static const struct wl_surface_listener surface_listener = {
4353 static struct surface *
4354 surface_create(struct window *window)
4356 struct display *display = window->display;
4357 struct surface *surface;
4359 surface = xmalloc(sizeof *surface);
4360 memset(surface, 0, sizeof *surface);
4364 surface->window = window;
4365 surface->surface = wl_compositor_create_surface(display->compositor);
4366 surface->buffer_scale = 1;
4367 wl_surface_add_listener(surface->surface, &surface_listener, window);
4369 wl_list_insert(&window->subsurface_list, &surface->link);
4374 static enum window_buffer_type
4375 get_preferred_buffer_type(struct display *display)
4377 #ifdef HAVE_CAIRO_EGL
4378 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4379 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4382 return WINDOW_BUFFER_TYPE_SHM;
4385 static struct window *
4386 window_create_internal(struct display *display, int custom)
4388 struct window *window;
4389 struct surface *surface;
4391 window = xzalloc(sizeof *window);
4392 wl_list_init(&window->subsurface_list);
4393 window->display = display;
4395 surface = surface_create(window);
4396 window->main_surface = surface;
4398 assert(custom || display->xdg_shell);
4400 window->custom = custom;
4401 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4403 surface->buffer_type = get_preferred_buffer_type(display);
4405 wl_surface_set_user_data(surface->surface, window);
4406 wl_list_insert(display->window_list.prev, &window->link);
4407 wl_list_init(&window->redraw_task.link);
4409 wl_list_init (&window->window_output_list);
4415 window_create(struct display *display)
4417 struct window *window;
4419 window = window_create_internal(display, 0);
4421 window->xdg_surface =
4422 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4423 window->main_surface->surface);
4424 fail_on_null(window->xdg_surface);
4426 xdg_surface_set_user_data(window->xdg_surface, window);
4427 xdg_surface_add_listener(window->xdg_surface,
4428 &xdg_surface_listener, window);
4434 window_create_custom(struct display *display)
4436 return window_create_internal(display, 1);
4440 window_set_parent(struct window *window,
4441 struct window *parent_window)
4443 window->parent = parent_window;
4444 window_sync_parent(window);
4448 window_get_parent(struct window *window)
4450 return window->parent;
4454 menu_set_item(struct menu *menu, int sy)
4456 int32_t x, y, width, height;
4459 frame_interior(menu->frame, &x, &y, &width, &height);
4460 next = (sy - y) / 20;
4461 if (menu->current != next) {
4462 menu->current = next;
4463 widget_schedule_redraw(menu->widget);
4468 menu_motion_handler(struct widget *widget,
4469 struct input *input, uint32_t time,
4470 float x, float y, void *data)
4472 struct menu *menu = data;
4474 if (widget == menu->widget)
4475 menu_set_item(data, y);
4477 return CURSOR_LEFT_PTR;
4481 menu_enter_handler(struct widget *widget,
4482 struct input *input, float x, float y, void *data)
4484 struct menu *menu = data;
4486 if (widget == menu->widget)
4487 menu_set_item(data, y);
4489 return CURSOR_LEFT_PTR;
4493 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4495 struct menu *menu = data;
4497 if (widget == menu->widget)
4498 menu_set_item(data, -200);
4502 menu_button_handler(struct widget *widget,
4503 struct input *input, uint32_t time,
4504 uint32_t button, enum wl_pointer_button_state state,
4508 struct menu *menu = data;
4510 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4511 (menu->release_count > 0 || time - menu->time > 500)) {
4512 /* Either relase after press-drag-release or
4513 * click-motion-click. */
4514 menu->func(menu->parent, input,
4515 menu->current, menu->parent->user_data);
4516 input_ungrab(input);
4518 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4519 menu->release_count++;
4524 menu_touch_up_handler(struct widget *widget,
4525 struct input *input,
4531 struct menu *menu = data;
4533 input_ungrab(input);
4538 menu_redraw_handler(struct widget *widget, void *data)
4541 struct menu *menu = data;
4542 int32_t x, y, width, height, i;
4544 cr = widget_cairo_create(widget);
4546 frame_repaint(menu->frame, cr);
4547 frame_interior(menu->frame, &x, &y, &width, &height);
4549 theme_set_background_source(menu->window->display->theme,
4550 cr, THEME_FRAME_ACTIVE);
4551 cairo_rectangle(cr, x, y, width, height);
4554 cairo_select_font_face(cr, "sans",
4555 CAIRO_FONT_SLANT_NORMAL,
4556 CAIRO_FONT_WEIGHT_NORMAL);
4557 cairo_set_font_size(cr, 12);
4559 for (i = 0; i < menu->count; i++) {
4560 if (i == menu->current) {
4561 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4562 cairo_rectangle(cr, x, y + i * 20, width, 20);
4564 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4565 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4566 cairo_show_text(cr, menu->entries[i]);
4568 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4569 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4570 cairo_show_text(cr, menu->entries[i]);
4578 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4580 struct window *window = data;
4581 struct menu *menu = window->main_surface->widget->user_data;
4583 input_ungrab(menu->input);
4587 static const struct xdg_popup_listener xdg_popup_listener = {
4588 handle_popup_popup_done,
4592 window_show_menu(struct display *display,
4593 struct input *input, uint32_t time, struct window *parent,
4594 int32_t x, int32_t y,
4595 menu_func_t func, const char **entries, int count)
4597 struct window *window;
4601 menu = malloc(sizeof *menu);
4605 window = window_create_internal(parent->display, 0);
4611 menu->window = window;
4612 menu->parent = parent;
4613 menu->widget = window_add_widget(menu->window, menu);
4614 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4615 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4616 menu->frame = frame_create(window->display->theme, 0, 0,
4617 FRAME_BUTTON_NONE, NULL);
4618 fail_on_null(menu->frame);
4619 menu->entries = entries;
4620 menu->count = count;
4621 menu->release_count = 0;
4625 menu->input = input;
4629 input_ungrab(input);
4631 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4632 widget_set_enter_handler(menu->widget, menu_enter_handler);
4633 widget_set_leave_handler(menu->widget, menu_leave_handler);
4634 widget_set_motion_handler(menu->widget, menu_motion_handler);
4635 widget_set_button_handler(menu->widget, menu_button_handler);
4636 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4638 input_grab(input, menu->widget, 0);
4639 frame_resize_inside(menu->frame, 200, count * 20);
4640 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4641 window_schedule_resize(window, frame_width(menu->frame),
4642 frame_height(menu->frame));
4644 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4646 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4647 window->main_surface->surface,
4648 parent->main_surface->surface,
4650 display_get_serial(window->display),
4654 fail_on_null(window->xdg_popup);
4656 xdg_popup_set_user_data(window->xdg_popup, window);
4657 xdg_popup_add_listener(window->xdg_popup,
4658 &xdg_popup_listener, window);
4662 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4664 window->main_surface->buffer_type = type;
4667 enum window_buffer_type
4668 window_get_buffer_type(struct window *window)
4670 return window->main_surface->buffer_type;
4674 window_set_preferred_format(struct window *window,
4675 enum preferred_format format)
4677 window->preferred_format = format;
4681 window_add_subsurface(struct window *window, void *data,
4682 enum subsurface_mode default_mode)
4684 struct widget *widget;
4685 struct surface *surface;
4686 struct wl_surface *parent;
4687 struct wl_subcompositor *subcompo = window->display->subcompositor;
4689 surface = surface_create(window);
4690 surface->buffer_type = window_get_buffer_type(window);
4691 widget = widget_create(window, surface, data);
4692 wl_list_init(&widget->link);
4693 surface->widget = widget;
4695 parent = window->main_surface->surface;
4696 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4699 surface->synchronized = 1;
4701 switch (default_mode) {
4702 case SUBSURFACE_SYNCHRONIZED:
4703 surface->synchronized_default = 1;
4705 case SUBSURFACE_DESYNCHRONIZED:
4706 surface->synchronized_default = 0;
4709 assert(!"bad enum subsurface_mode");
4712 window->resize_needed = 1;
4713 window_schedule_redraw(window);
4719 display_handle_geometry(void *data,
4720 struct wl_output *wl_output,
4723 int physical_height,
4729 struct output *output = data;
4731 output->allocation.x = x;
4732 output->allocation.y = y;
4733 output->transform = transform;
4737 output->make = strdup(make);
4740 free(output->model);
4741 output->model = strdup(model);
4745 display_handle_done(void *data,
4746 struct wl_output *wl_output)
4751 display_handle_scale(void *data,
4752 struct wl_output *wl_output,
4755 struct output *output = data;
4757 output->scale = scale;
4761 display_handle_mode(void *data,
4762 struct wl_output *wl_output,
4768 struct output *output = data;
4769 struct display *display = output->display;
4771 if (flags & WL_OUTPUT_MODE_CURRENT) {
4772 output->allocation.width = width;
4773 output->allocation.height = height;
4774 if (display->output_configure_handler)
4775 (*display->output_configure_handler)(
4776 output, display->user_data);
4780 static const struct wl_output_listener output_listener = {
4781 display_handle_geometry,
4782 display_handle_mode,
4783 display_handle_done,
4784 display_handle_scale
4788 display_add_output(struct display *d, uint32_t id)
4790 struct output *output;
4792 output = xzalloc(sizeof *output);
4793 output->display = d;
4796 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4797 output->server_output_id = id;
4798 wl_list_insert(d->output_list.prev, &output->link);
4800 wl_output_add_listener(output->output, &output_listener, output);
4804 output_destroy(struct output *output)
4806 if (output->destroy_handler)
4807 (*output->destroy_handler)(output, output->user_data);
4809 wl_output_destroy(output->output);
4810 wl_list_remove(&output->link);
4815 display_destroy_output(struct display *d, uint32_t id)
4817 struct output *output;
4819 wl_list_for_each(output, &d->output_list, link) {
4820 if (output->server_output_id == id) {
4821 output_destroy(output);
4828 display_set_global_handler(struct display *display,
4829 display_global_handler_t handler)
4831 struct global *global;
4833 display->global_handler = handler;
4837 wl_list_for_each(global, &display->global_list, link)
4838 display->global_handler(display,
4839 global->name, global->interface,
4840 global->version, display->user_data);
4844 display_set_global_handler_remove(struct display *display,
4845 display_global_handler_t remove_handler)
4847 display->global_handler_remove = remove_handler;
4848 if (!remove_handler)
4853 display_set_output_configure_handler(struct display *display,
4854 display_output_handler_t handler)
4856 struct output *output;
4858 display->output_configure_handler = handler;
4862 wl_list_for_each(output, &display->output_list, link) {
4863 if (output->allocation.width == 0 &&
4864 output->allocation.height == 0)
4867 (*display->output_configure_handler)(output,
4868 display->user_data);
4873 output_set_user_data(struct output *output, void *data)
4875 output->user_data = data;
4879 output_get_user_data(struct output *output)
4881 return output->user_data;
4885 output_set_destroy_handler(struct output *output,
4886 display_output_handler_t handler)
4888 output->destroy_handler = handler;
4889 /* FIXME: implement this, once we have way to remove outputs */
4893 output_get_allocation(struct output *output, struct rectangle *base)
4895 struct rectangle allocation = output->allocation;
4897 switch (output->transform) {
4898 case WL_OUTPUT_TRANSFORM_90:
4899 case WL_OUTPUT_TRANSFORM_270:
4900 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4901 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4902 /* Swap width and height */
4903 allocation.width = output->allocation.height;
4904 allocation.height = output->allocation.width;
4912 output_get_wl_output(struct output *output)
4914 return output->output;
4917 enum wl_output_transform
4918 output_get_transform(struct output *output)
4920 return output->transform;
4924 output_get_scale(struct output *output)
4926 return output->scale;
4930 output_get_make(struct output *output)
4932 return output->make;
4936 output_get_model(struct output *output)
4938 return output->model;
4942 fini_xkb(struct input *input)
4944 xkb_state_unref(input->xkb.state);
4945 xkb_map_unref(input->xkb.keymap);
4948 #define MIN(a,b) ((a) < (b) ? a : b)
4951 display_add_input(struct display *d, uint32_t id)
4953 struct input *input;
4955 input = xzalloc(sizeof *input);
4957 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4958 MIN(d->seat_version, 3));
4959 input->touch_focus = NULL;
4960 input->pointer_focus = NULL;
4961 input->keyboard_focus = NULL;
4962 wl_list_init(&input->touch_point_list);
4963 wl_list_insert(d->input_list.prev, &input->link);
4965 wl_seat_add_listener(input->seat, &seat_listener, input);
4966 wl_seat_set_user_data(input->seat, input);
4968 if (d->data_device_manager) {
4969 input->data_device =
4970 wl_data_device_manager_get_data_device(d->data_device_manager,
4972 wl_data_device_add_listener(input->data_device,
4973 &data_device_listener,
4977 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4979 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4980 TFD_CLOEXEC | TFD_NONBLOCK);
4981 input->repeat_task.run = keyboard_repeat_func;
4982 display_watch_fd(d, input->repeat_timer_fd,
4983 EPOLLIN, &input->repeat_task);
4987 input_destroy(struct input *input)
4989 input_remove_keyboard_focus(input);
4990 input_remove_pointer_focus(input);
4992 if (input->drag_offer)
4993 data_offer_destroy(input->drag_offer);
4995 if (input->selection_offer)
4996 data_offer_destroy(input->selection_offer);
4998 if (input->data_device)
4999 wl_data_device_destroy(input->data_device);
5001 if (input->display->seat_version >= 3) {
5003 wl_pointer_release(input->pointer);
5004 if (input->keyboard)
5005 wl_keyboard_release(input->keyboard);
5010 wl_surface_destroy(input->pointer_surface);
5012 wl_list_remove(&input->link);
5013 wl_seat_destroy(input->seat);
5014 close(input->repeat_timer_fd);
5019 init_workspace_manager(struct display *d, uint32_t id)
5021 d->workspace_manager =
5022 wl_registry_bind(d->registry, id,
5023 &workspace_manager_interface, 1);
5024 if (d->workspace_manager != NULL)
5025 workspace_manager_add_listener(d->workspace_manager,
5026 &workspace_manager_listener,
5031 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5033 struct display *d = data;
5035 if (format == WL_SHM_FORMAT_RGB565)
5039 struct wl_shm_listener shm_listener = {
5044 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5046 xdg_shell_pong(shell, serial);
5049 static const struct xdg_shell_listener xdg_shell_listener = {
5053 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
5054 #ifdef static_assert
5055 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5056 "Interface version doesn't match implementation version");
5060 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5061 const char *interface, uint32_t version)
5063 struct display *d = data;
5064 struct global *global;
5066 global = xmalloc(sizeof *global);
5068 global->interface = strdup(interface);
5069 global->version = version;
5070 wl_list_insert(d->global_list.prev, &global->link);
5072 if (strcmp(interface, "wl_compositor") == 0) {
5073 d->compositor = wl_registry_bind(registry, id,
5074 &wl_compositor_interface, 3);
5075 } else if (strcmp(interface, "wl_output") == 0) {
5076 display_add_output(d, id);
5077 } else if (strcmp(interface, "wl_seat") == 0) {
5078 d->seat_version = version;
5079 display_add_input(d, id);
5080 } else if (strcmp(interface, "wl_shm") == 0) {
5081 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5082 wl_shm_add_listener(d->shm, &shm_listener, d);
5083 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5084 d->data_device_manager =
5085 wl_registry_bind(registry, id,
5086 &wl_data_device_manager_interface, 1);
5087 } else if (strcmp(interface, "xdg_shell") == 0) {
5088 d->xdg_shell = wl_registry_bind(registry, id,
5089 &xdg_shell_interface, 1);
5090 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5091 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5092 } else if (strcmp(interface, "text_cursor_position") == 0) {
5093 d->text_cursor_position =
5094 wl_registry_bind(registry, id,
5095 &text_cursor_position_interface, 1);
5096 } else if (strcmp(interface, "workspace_manager") == 0) {
5097 init_workspace_manager(d, id);
5098 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5100 wl_registry_bind(registry, id,
5101 &wl_subcompositor_interface, 1);
5104 if (d->global_handler)
5105 d->global_handler(d, id, interface, version, d->user_data);
5109 registry_handle_global_remove(void *data, struct wl_registry *registry,
5112 struct display *d = data;
5113 struct global *global;
5116 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5117 if (global->name != name)
5120 if (strcmp(global->interface, "wl_output") == 0)
5121 display_destroy_output(d, name);
5123 /* XXX: Should destroy remaining bound globals */
5125 if (d->global_handler_remove)
5126 d->global_handler_remove(d, name, global->interface,
5127 global->version, d->user_data);
5129 wl_list_remove(&global->link);
5130 free(global->interface);
5136 display_bind(struct display *display, uint32_t name,
5137 const struct wl_interface *interface, uint32_t version)
5139 return wl_registry_bind(display->registry, name, interface, version);
5142 static const struct wl_registry_listener registry_listener = {
5143 registry_handle_global,
5144 registry_handle_global_remove
5147 #ifdef HAVE_CAIRO_EGL
5149 init_egl(struct display *d)
5151 EGLint major, minor;
5154 #ifdef USE_CAIRO_GLESV2
5155 # define GL_BIT EGL_OPENGL_ES2_BIT
5157 # define GL_BIT EGL_OPENGL_BIT
5160 static const EGLint argb_cfg_attribs[] = {
5161 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5167 EGL_RENDERABLE_TYPE, GL_BIT,
5171 #ifdef USE_CAIRO_GLESV2
5172 static const EGLint context_attribs[] = {
5173 EGL_CONTEXT_CLIENT_VERSION, 2,
5176 EGLint api = EGL_OPENGL_ES_API;
5178 EGLint *context_attribs = NULL;
5179 EGLint api = EGL_OPENGL_API;
5182 d->dpy = eglGetDisplay(d->display);
5183 if (!eglInitialize(d->dpy, &major, &minor)) {
5184 fprintf(stderr, "failed to initialize EGL\n");
5188 if (!eglBindAPI(api)) {
5189 fprintf(stderr, "failed to bind EGL client API\n");
5193 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5194 &d->argb_config, 1, &n) || n != 1) {
5195 fprintf(stderr, "failed to choose argb EGL config\n");
5199 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5200 EGL_NO_CONTEXT, context_attribs);
5201 if (d->argb_ctx == NULL) {
5202 fprintf(stderr, "failed to create EGL context\n");
5206 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5207 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5208 fprintf(stderr, "failed to get cairo EGL argb device\n");
5216 fini_egl(struct display *display)
5218 cairo_device_destroy(display->argb_device);
5220 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5223 eglTerminate(display->dpy);
5229 init_dummy_surface(struct display *display)
5234 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5236 display->dummy_surface =
5237 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5239 display->dummy_surface_data = data;
5243 handle_display_data(struct task *task, uint32_t events)
5245 struct display *display =
5246 container_of(task, struct display, display_task);
5247 struct epoll_event ep;
5250 display->display_fd_events = events;
5252 if (events & EPOLLERR || events & EPOLLHUP) {
5253 display_exit(display);
5257 if (events & EPOLLIN) {
5258 ret = wl_display_dispatch(display->display);
5260 display_exit(display);
5265 if (events & EPOLLOUT) {
5266 ret = wl_display_flush(display->display);
5268 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5269 ep.data.ptr = &display->display_task;
5270 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5271 display->display_fd, &ep);
5272 } else if (ret == -1 && errno != EAGAIN) {
5273 display_exit(display);
5280 log_handler(const char *format, va_list args)
5282 vfprintf(stderr, format, args);
5286 display_create(int *argc, char *argv[])
5290 wl_log_set_handler_client(log_handler);
5292 d = zalloc(sizeof *d);
5296 d->display = wl_display_connect(NULL);
5297 if (d->display == NULL) {
5298 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5303 d->xkb_context = xkb_context_new(0);
5304 if (d->xkb_context == NULL) {
5305 fprintf(stderr, "Failed to create XKB context\n");
5310 d->epoll_fd = os_epoll_create_cloexec();
5311 d->display_fd = wl_display_get_fd(d->display);
5312 d->display_task.run = handle_display_data;
5313 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5316 wl_list_init(&d->deferred_list);
5317 wl_list_init(&d->input_list);
5318 wl_list_init(&d->output_list);
5319 wl_list_init(&d->global_list);
5322 d->workspace_count = 1;
5324 d->registry = wl_display_get_registry(d->display);
5325 wl_registry_add_listener(d->registry, ®istry_listener, d);
5327 if (wl_display_dispatch(d->display) < 0) {
5328 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5332 #ifdef HAVE_CAIRO_EGL
5333 if (init_egl(d) < 0)
5334 fprintf(stderr, "EGL does not seem to work, "
5335 "falling back to software rendering and wl_shm.\n");
5340 d->theme = theme_create();
5342 wl_list_init(&d->window_list);
5344 init_dummy_surface(d);
5350 display_destroy_outputs(struct display *display)
5353 struct output *output;
5355 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5356 output_destroy(output);
5360 display_destroy_inputs(struct display *display)
5363 struct input *input;
5365 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5366 input_destroy(input);
5370 display_destroy(struct display *display)
5372 if (!wl_list_empty(&display->window_list))
5373 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5374 wl_list_length(&display->window_list));
5376 if (!wl_list_empty(&display->deferred_list))
5377 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5379 cairo_surface_destroy(display->dummy_surface);
5380 free(display->dummy_surface_data);
5382 display_destroy_outputs(display);
5383 display_destroy_inputs(display);
5385 xkb_context_unref(display->xkb_context);
5387 theme_destroy(display->theme);
5388 destroy_cursors(display);
5390 #ifdef HAVE_CAIRO_EGL
5391 if (display->argb_device)
5395 if (display->subcompositor)
5396 wl_subcompositor_destroy(display->subcompositor);
5398 if (display->xdg_shell)
5399 xdg_shell_destroy(display->xdg_shell);
5402 wl_shm_destroy(display->shm);
5404 if (display->data_device_manager)
5405 wl_data_device_manager_destroy(display->data_device_manager);
5407 wl_compositor_destroy(display->compositor);
5408 wl_registry_destroy(display->registry);
5410 close(display->epoll_fd);
5412 if (!(display->display_fd_events & EPOLLERR) &&
5413 !(display->display_fd_events & EPOLLHUP))
5414 wl_display_flush(display->display);
5416 wl_display_disconnect(display->display);
5421 display_set_user_data(struct display *display, void *data)
5423 display->user_data = data;
5427 display_get_user_data(struct display *display)
5429 return display->user_data;
5433 display_get_display(struct display *display)
5435 return display->display;
5439 display_has_subcompositor(struct display *display)
5441 if (display->subcompositor)
5444 wl_display_roundtrip(display->display);
5446 return display->subcompositor != NULL;
5450 display_get_cairo_device(struct display *display)
5452 return display->argb_device;
5456 display_get_output(struct display *display)
5458 return container_of(display->output_list.next, struct output, link);
5461 struct wl_compositor *
5462 display_get_compositor(struct display *display)
5464 return display->compositor;
5468 display_get_serial(struct display *display)
5470 return display->serial;
5474 display_get_egl_display(struct display *d)
5479 struct wl_data_source *
5480 display_create_data_source(struct display *display)
5482 if (display->data_device_manager)
5483 return wl_data_device_manager_create_data_source(display->data_device_manager);
5489 display_get_argb_egl_config(struct display *d)
5491 return d->argb_config;
5495 display_acquire_window_surface(struct display *display,
5496 struct window *window,
5499 struct surface *surface = window->main_surface;
5501 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5504 widget_get_cairo_surface(window->main_surface->widget);
5505 return surface->toysurface->acquire(surface->toysurface, ctx);
5509 display_release_window_surface(struct display *display,
5510 struct window *window)
5512 struct surface *surface = window->main_surface;
5514 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5517 surface->toysurface->release(surface->toysurface);
5521 display_defer(struct display *display, struct task *task)
5523 wl_list_insert(&display->deferred_list, &task->link);
5527 display_watch_fd(struct display *display,
5528 int fd, uint32_t events, struct task *task)
5530 struct epoll_event ep;
5534 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5538 display_unwatch_fd(struct display *display, int fd)
5540 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5544 display_run(struct display *display)
5547 struct epoll_event ep[16];
5550 display->running = 1;
5552 while (!wl_list_empty(&display->deferred_list)) {
5553 task = container_of(display->deferred_list.prev,
5555 wl_list_remove(&task->link);
5559 wl_display_dispatch_pending(display->display);
5561 if (!display->running)
5564 ret = wl_display_flush(display->display);
5565 if (ret < 0 && errno == EAGAIN) {
5567 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5568 ep[0].data.ptr = &display->display_task;
5570 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5571 display->display_fd, &ep[0]);
5572 } else if (ret < 0) {
5576 count = epoll_wait(display->epoll_fd,
5577 ep, ARRAY_LENGTH(ep), -1);
5578 for (i = 0; i < count; i++) {
5579 task = ep[i].data.ptr;
5580 task->run(task, ep[i].events);
5586 display_exit(struct display *display)
5588 display->running = 0;
5592 keysym_modifiers_add(struct wl_array *modifiers_map,
5595 size_t len = strlen(name) + 1;
5598 p = wl_array_add(modifiers_map, len);
5603 strncpy(p, name, len);
5606 static xkb_mod_index_t
5607 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5610 xkb_mod_index_t index = 0;
5611 char *p = modifiers_map->data;
5613 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5614 if (strcmp(p, name) == 0)
5621 return XKB_MOD_INVALID;
5625 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5628 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5630 if (index == XKB_MOD_INVALID)
5631 return XKB_MOD_INVALID;
5637 fail_on_null(void *p)
5640 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5650 return fail_on_null(malloc(s));
5656 return fail_on_null(zalloc(s));
5660 xstrdup(const char *s)
5662 return fail_on_null(strdup(s));
5666 xrealloc(char *p, size_t s)
5668 return fail_on_null(realloc(p, s));