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;
244 window_state_changed_handler_t state_changed_handler;
246 struct surface *main_surface;
247 struct xdg_surface *xdg_surface;
248 struct xdg_popup *xdg_popup;
250 struct window *parent;
252 struct window_frame *frame;
254 /* struct surface::link, contains also main_surface */
255 struct wl_list subsurface_list;
262 struct window *window;
263 struct surface *surface;
264 struct tooltip *tooltip;
265 struct wl_list child_list;
267 struct rectangle allocation;
268 widget_resize_handler_t resize_handler;
269 widget_redraw_handler_t redraw_handler;
270 widget_enter_handler_t enter_handler;
271 widget_leave_handler_t leave_handler;
272 widget_motion_handler_t motion_handler;
273 widget_button_handler_t button_handler;
274 widget_touch_down_handler_t touch_down_handler;
275 widget_touch_up_handler_t touch_up_handler;
276 widget_touch_motion_handler_t touch_motion_handler;
277 widget_touch_frame_handler_t touch_frame_handler;
278 widget_touch_cancel_handler_t touch_cancel_handler;
279 widget_axis_handler_t axis_handler;
284 /* If this is set to false then no cairo surface will be
285 * created before redrawing the surface. This is useful if the
286 * redraw handler is going to do completely custom rendering
287 * such as using EGL directly */
294 struct widget *widget;
299 struct display *display;
300 struct wl_seat *seat;
301 struct wl_pointer *pointer;
302 struct wl_keyboard *keyboard;
303 struct wl_touch *touch;
304 struct wl_list touch_point_list;
305 struct window *pointer_focus;
306 struct window *keyboard_focus;
307 struct window *touch_focus;
309 uint32_t cursor_anim_start;
310 struct wl_callback *cursor_frame_cb;
311 struct wl_surface *pointer_surface;
313 uint32_t pointer_enter_serial;
314 uint32_t cursor_serial;
318 struct widget *focus_widget;
320 uint32_t grab_button;
322 struct wl_data_device *data_device;
323 struct data_offer *drag_offer;
324 struct data_offer *selection_offer;
326 int32_t touch_grab_id;
327 float drag_x, drag_y;
328 struct window *drag_focus;
329 uint32_t drag_enter_serial;
332 struct xkb_keymap *keymap;
333 struct xkb_state *state;
334 xkb_mod_mask_t control_mask;
335 xkb_mod_mask_t alt_mask;
336 xkb_mod_mask_t shift_mask;
339 struct task repeat_task;
343 uint32_t repeat_time;
347 struct display *display;
348 struct wl_output *output;
349 uint32_t server_output_id;
350 struct rectangle allocation;
357 display_output_handler_t destroy_handler;
361 struct window_frame {
362 struct widget *widget;
363 struct widget *child;
368 struct window *window;
369 struct window *parent;
370 struct widget *widget;
373 const char **entries;
382 struct widget *parent;
383 struct widget *widget;
385 struct task tooltip_task;
391 struct wl_shm_pool *pool;
398 CURSOR_DEFAULT = 100,
402 static const cairo_user_data_key_t shm_surface_data_key;
409 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
410 __attribute__ ((format (printf, 4, 5)));
413 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
418 gettimeofday(&tv, NULL);
419 fprintf(stderr, "%8ld.%03ld ",
420 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
423 fprintf(stderr, "%s@%d ",
424 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
426 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
427 fprintf(stderr, "%s ", func);
430 vfprintf(stderr, fmt, ap);
434 #define DBG(fmt, ...) \
435 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
437 #define DBG_OBJ(obj, fmt, ...) \
438 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
442 #define DBG(...) do {} while (0)
443 #define DBG_OBJ(...) do {} while (0)
448 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
452 switch (buffer_transform) {
453 case WL_OUTPUT_TRANSFORM_90:
454 case WL_OUTPUT_TRANSFORM_270:
455 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
456 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
465 *width *= buffer_scale;
466 *height *= buffer_scale;
470 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
474 switch (buffer_transform) {
475 case WL_OUTPUT_TRANSFORM_90:
476 case WL_OUTPUT_TRANSFORM_270:
477 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
487 *width /= buffer_scale;
488 *height /= buffer_scale;
491 #ifdef HAVE_CAIRO_EGL
493 struct egl_window_surface {
494 struct toysurface base;
495 cairo_surface_t *cairo_surface;
496 struct display *display;
497 struct wl_surface *surface;
498 struct wl_egl_window *egl_window;
499 EGLSurface egl_surface;
502 static struct egl_window_surface *
503 to_egl_window_surface(struct toysurface *base)
505 return container_of(base, struct egl_window_surface, base);
508 static cairo_surface_t *
509 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
510 int32_t width, int32_t height, uint32_t flags,
511 enum wl_output_transform buffer_transform, int32_t buffer_scale)
513 struct egl_window_surface *surface = to_egl_window_surface(base);
515 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
517 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
518 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
520 return cairo_surface_reference(surface->cairo_surface);
524 egl_window_surface_swap(struct toysurface *base,
525 enum wl_output_transform buffer_transform, int32_t buffer_scale,
526 struct rectangle *server_allocation)
528 struct egl_window_surface *surface = to_egl_window_surface(base);
530 cairo_gl_surface_swapbuffers(surface->cairo_surface);
531 wl_egl_window_get_attached_size(surface->egl_window,
532 &server_allocation->width,
533 &server_allocation->height);
535 buffer_to_surface_size (buffer_transform, buffer_scale,
536 &server_allocation->width,
537 &server_allocation->height);
541 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
543 struct egl_window_surface *surface = to_egl_window_surface(base);
544 cairo_device_t *device;
546 device = cairo_surface_get_device(surface->cairo_surface);
551 if (device == surface->display->argb_device)
552 ctx = surface->display->argb_ctx;
557 cairo_device_flush(device);
558 cairo_device_acquire(device);
559 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
560 surface->egl_surface, ctx))
561 fprintf(stderr, "failed to make surface current\n");
567 egl_window_surface_release(struct toysurface *base)
569 struct egl_window_surface *surface = to_egl_window_surface(base);
570 cairo_device_t *device;
572 device = cairo_surface_get_device(surface->cairo_surface);
576 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
577 surface->display->argb_ctx))
578 fprintf(stderr, "failed to make context current\n");
580 cairo_device_release(device);
584 egl_window_surface_destroy(struct toysurface *base)
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587 struct display *d = surface->display;
589 cairo_surface_destroy(surface->cairo_surface);
590 eglDestroySurface(d->dpy, surface->egl_surface);
591 wl_egl_window_destroy(surface->egl_window);
592 surface->surface = NULL;
597 static struct toysurface *
598 egl_window_surface_create(struct display *display,
599 struct wl_surface *wl_surface,
601 struct rectangle *rectangle)
603 struct egl_window_surface *surface;
605 if (display->dpy == EGL_NO_DISPLAY)
608 surface = calloc(1, sizeof *surface);
612 surface->base.prepare = egl_window_surface_prepare;
613 surface->base.swap = egl_window_surface_swap;
614 surface->base.acquire = egl_window_surface_acquire;
615 surface->base.release = egl_window_surface_release;
616 surface->base.destroy = egl_window_surface_destroy;
618 surface->display = display;
619 surface->surface = wl_surface;
621 surface->egl_window = wl_egl_window_create(surface->surface,
625 surface->egl_surface = eglCreateWindowSurface(display->dpy,
626 display->argb_config,
630 surface->cairo_surface =
631 cairo_gl_surface_create_for_egl(display->argb_device,
632 surface->egl_surface,
636 return &surface->base;
641 static struct toysurface *
642 egl_window_surface_create(struct display *display,
643 struct wl_surface *wl_surface,
645 struct rectangle *rectangle)
652 struct shm_surface_data {
653 struct wl_buffer *buffer;
654 struct shm_pool *pool;
658 display_get_buffer_for_surface(struct display *display,
659 cairo_surface_t *surface)
661 struct shm_surface_data *data;
663 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
669 shm_pool_destroy(struct shm_pool *pool);
672 shm_surface_data_destroy(void *p)
674 struct shm_surface_data *data = p;
676 wl_buffer_destroy(data->buffer);
678 shm_pool_destroy(data->pool);
683 static struct wl_shm_pool *
684 make_shm_pool(struct display *display, int size, void **data)
686 struct wl_shm_pool *pool;
689 fd = os_create_anonymous_file(size);
691 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
696 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
697 if (*data == MAP_FAILED) {
698 fprintf(stderr, "mmap failed: %m\n");
703 pool = wl_shm_create_pool(display->shm, fd, size);
710 static struct shm_pool *
711 shm_pool_create(struct display *display, size_t size)
713 struct shm_pool *pool = malloc(sizeof *pool);
718 pool->pool = make_shm_pool(display, size, &pool->data);
731 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
733 if (pool->used + size > pool->size)
736 *offset = pool->used;
739 return (char *) pool->data + *offset;
742 /* destroy the pool. this does not unmap the memory though */
744 shm_pool_destroy(struct shm_pool *pool)
746 munmap(pool->data, pool->size);
747 wl_shm_pool_destroy(pool->pool);
751 /* Start allocating from the beginning of the pool again */
753 shm_pool_reset(struct shm_pool *pool)
759 data_length_for_shm_surface(struct rectangle *rect)
763 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
765 return stride * rect->height;
768 static cairo_surface_t *
769 display_create_shm_surface_from_pool(struct display *display,
770 struct rectangle *rectangle,
771 uint32_t flags, struct shm_pool *pool)
773 struct shm_surface_data *data;
775 cairo_surface_t *surface;
776 cairo_format_t cairo_format;
777 int stride, length, offset;
780 data = malloc(sizeof *data);
784 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
785 cairo_format = CAIRO_FORMAT_RGB16_565;
787 cairo_format = CAIRO_FORMAT_ARGB32;
789 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
790 length = stride * rectangle->height;
792 map = shm_pool_allocate(pool, length, &offset);
799 surface = cairo_image_surface_create_for_data (map,
805 cairo_surface_set_user_data(surface, &shm_surface_data_key,
806 data, shm_surface_data_destroy);
808 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
809 format = WL_SHM_FORMAT_RGB565;
811 if (flags & SURFACE_OPAQUE)
812 format = WL_SHM_FORMAT_XRGB8888;
814 format = WL_SHM_FORMAT_ARGB8888;
817 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
825 static cairo_surface_t *
826 display_create_shm_surface(struct display *display,
827 struct rectangle *rectangle, uint32_t flags,
828 struct shm_pool *alternate_pool,
829 struct shm_surface_data **data_ret)
831 struct shm_surface_data *data;
832 struct shm_pool *pool;
833 cairo_surface_t *surface;
835 if (alternate_pool) {
836 shm_pool_reset(alternate_pool);
837 surface = display_create_shm_surface_from_pool(display,
842 data = cairo_surface_get_user_data(surface,
843 &shm_surface_data_key);
848 pool = shm_pool_create(display,
849 data_length_for_shm_surface(rectangle));
854 display_create_shm_surface_from_pool(display, rectangle,
858 shm_pool_destroy(pool);
862 /* make sure we destroy the pool when the surface is destroyed */
863 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
874 check_size(struct rectangle *rect)
876 if (rect->width && rect->height)
879 fprintf(stderr, "tried to create surface of "
880 "width: %d, height: %d\n", rect->width, rect->height);
885 display_create_surface(struct display *display,
886 struct wl_surface *surface,
887 struct rectangle *rectangle,
890 if (check_size(rectangle) < 0)
893 assert(flags & SURFACE_SHM);
894 return display_create_shm_surface(display, rectangle, flags,
898 struct shm_surface_leaf {
899 cairo_surface_t *cairo_surface;
900 /* 'data' is automatically destroyed, when 'cairo_surface' is */
901 struct shm_surface_data *data;
903 struct shm_pool *resize_pool;
908 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
910 if (leaf->cairo_surface)
911 cairo_surface_destroy(leaf->cairo_surface);
912 /* leaf->data already destroyed via cairo private */
914 if (leaf->resize_pool)
915 shm_pool_destroy(leaf->resize_pool);
917 memset(leaf, 0, sizeof *leaf);
923 struct toysurface base;
924 struct display *display;
925 struct wl_surface *surface;
929 struct shm_surface_leaf leaf[MAX_LEAVES];
930 struct shm_surface_leaf *current;
933 static struct shm_surface *
934 to_shm_surface(struct toysurface *base)
936 return container_of(base, struct shm_surface, base);
940 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
943 struct shm_surface_leaf *leaf;
944 char bufs[MAX_LEAVES + 1];
947 for (i = 0; i < MAX_LEAVES; i++) {
948 leaf = &surface->leaf[i];
952 else if (leaf->cairo_surface)
958 bufs[MAX_LEAVES] = '\0';
959 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
964 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
966 struct shm_surface *surface = data;
967 struct shm_surface_leaf *leaf;
971 shm_surface_buffer_state_debug(surface, "buffer_release before");
973 for (i = 0; i < MAX_LEAVES; i++) {
974 leaf = &surface->leaf[i];
975 if (leaf->data && leaf->data->buffer == buffer) {
980 assert(i < MAX_LEAVES && "unknown buffer released");
982 /* Leave one free leaf with storage, release others */
984 for (i = 0; i < MAX_LEAVES; i++) {
985 leaf = &surface->leaf[i];
987 if (!leaf->cairo_surface || leaf->busy)
993 shm_surface_leaf_release(leaf);
996 shm_surface_buffer_state_debug(surface, "buffer_release after");
999 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1000 shm_surface_buffer_release
1003 static cairo_surface_t *
1004 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1005 int32_t width, int32_t height, uint32_t flags,
1006 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1008 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1009 struct shm_surface *surface = to_shm_surface(base);
1010 struct rectangle rect = { 0};
1011 struct shm_surface_leaf *leaf = NULL;
1017 /* pick a free buffer, preferrably one that already has storage */
1018 for (i = 0; i < MAX_LEAVES; i++) {
1019 if (surface->leaf[i].busy)
1022 if (!leaf || surface->leaf[i].cairo_surface)
1023 leaf = &surface->leaf[i];
1025 DBG_OBJ(surface->surface, "pick leaf %d\n",
1026 (int)(leaf - &surface->leaf[0]));
1029 fprintf(stderr, "%s: all buffers are held by the server.\n",
1035 if (!resize_hint && leaf->resize_pool) {
1036 cairo_surface_destroy(leaf->cairo_surface);
1037 leaf->cairo_surface = NULL;
1038 shm_pool_destroy(leaf->resize_pool);
1039 leaf->resize_pool = NULL;
1042 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1044 if (leaf->cairo_surface &&
1045 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1046 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1049 if (leaf->cairo_surface)
1050 cairo_surface_destroy(leaf->cairo_surface);
1052 #ifdef USE_RESIZE_POOL
1053 if (resize_hint && !leaf->resize_pool) {
1054 /* Create a big pool to allocate from, while continuously
1055 * resizing. Mmapping a new pool in the server
1056 * is relatively expensive, so reusing a pool performs
1057 * better, but may temporarily reserve unneeded memory.
1059 /* We should probably base this number on the output size. */
1060 leaf->resize_pool = shm_pool_create(surface->display,
1066 rect.height = height;
1068 leaf->cairo_surface =
1069 display_create_shm_surface(surface->display, &rect,
1073 if (!leaf->cairo_surface)
1076 wl_buffer_add_listener(leaf->data->buffer,
1077 &shm_surface_buffer_listener, surface);
1080 surface->current = leaf;
1082 return cairo_surface_reference(leaf->cairo_surface);
1086 shm_surface_swap(struct toysurface *base,
1087 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1088 struct rectangle *server_allocation)
1090 struct shm_surface *surface = to_shm_surface(base);
1091 struct shm_surface_leaf *leaf = surface->current;
1093 server_allocation->width =
1094 cairo_image_surface_get_width(leaf->cairo_surface);
1095 server_allocation->height =
1096 cairo_image_surface_get_height(leaf->cairo_surface);
1098 buffer_to_surface_size (buffer_transform, buffer_scale,
1099 &server_allocation->width,
1100 &server_allocation->height);
1102 wl_surface_attach(surface->surface, leaf->data->buffer,
1103 surface->dx, surface->dy);
1104 wl_surface_damage(surface->surface, 0, 0,
1105 server_allocation->width, server_allocation->height);
1106 wl_surface_commit(surface->surface);
1108 DBG_OBJ(surface->surface, "leaf %d busy\n",
1109 (int)(leaf - &surface->leaf[0]));
1112 surface->current = NULL;
1116 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1122 shm_surface_release(struct toysurface *base)
1127 shm_surface_destroy(struct toysurface *base)
1129 struct shm_surface *surface = to_shm_surface(base);
1132 for (i = 0; i < MAX_LEAVES; i++)
1133 shm_surface_leaf_release(&surface->leaf[i]);
1138 static struct toysurface *
1139 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1140 uint32_t flags, struct rectangle *rectangle)
1142 struct shm_surface *surface;
1143 DBG_OBJ(wl_surface, "\n");
1145 surface = xmalloc(sizeof *surface);
1146 memset(surface, 0, sizeof *surface);
1151 surface->base.prepare = shm_surface_prepare;
1152 surface->base.swap = shm_surface_swap;
1153 surface->base.acquire = shm_surface_acquire;
1154 surface->base.release = shm_surface_release;
1155 surface->base.destroy = shm_surface_destroy;
1157 surface->display = display;
1158 surface->surface = wl_surface;
1159 surface->flags = flags;
1161 return &surface->base;
1165 * The following correspondences between file names and cursors was copied
1166 * from: https://bugs.kde.org/attachment.cgi?id=67313
1169 static const char *bottom_left_corners[] = {
1170 "bottom_left_corner",
1175 static const char *bottom_right_corners[] = {
1176 "bottom_right_corner",
1181 static const char *bottom_sides[] = {
1187 static const char *grabbings[] = {
1190 "208530c400c041818281048008011002"
1193 static const char *left_ptrs[] = {
1200 static const char *left_sides[] = {
1206 static const char *right_sides[] = {
1212 static const char *top_left_corners[] = {
1218 static const char *top_right_corners[] = {
1224 static const char *top_sides[] = {
1230 static const char *xterms[] = {
1236 static const char *hand1s[] = {
1240 "e29285e634086352946a0e7090d73106"
1243 static const char *watches[] = {
1246 "0426c94ea35c87780ff01dc239897213"
1249 struct cursor_alternatives {
1254 static const struct cursor_alternatives cursors[] = {
1255 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1256 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1257 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1258 {grabbings, ARRAY_LENGTH(grabbings)},
1259 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1260 {left_sides, ARRAY_LENGTH(left_sides)},
1261 {right_sides, ARRAY_LENGTH(right_sides)},
1262 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1263 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1264 {top_sides, ARRAY_LENGTH(top_sides)},
1265 {xterms, ARRAY_LENGTH(xterms)},
1266 {hand1s, ARRAY_LENGTH(hand1s)},
1267 {watches, ARRAY_LENGTH(watches)},
1271 create_cursors(struct display *display)
1273 struct weston_config *config;
1274 struct weston_config_section *s;
1278 struct wl_cursor *cursor;
1280 config = weston_config_parse("weston.ini");
1281 s = weston_config_get_section(config, "shell", NULL, NULL);
1282 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1283 weston_config_section_get_int(s, "cursor-size", &size, 32);
1284 weston_config_destroy(config);
1286 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1287 if (!display->cursor_theme) {
1288 fprintf(stderr, "could not load theme '%s'\n", theme);
1293 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1295 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1297 for (j = 0; !cursor && j < cursors[i].count; ++j)
1298 cursor = wl_cursor_theme_get_cursor(
1299 display->cursor_theme, cursors[i].names[j]);
1302 fprintf(stderr, "could not load cursor '%s'\n",
1303 cursors[i].names[0]);
1305 display->cursors[i] = cursor;
1310 destroy_cursors(struct display *display)
1312 wl_cursor_theme_destroy(display->cursor_theme);
1313 free(display->cursors);
1316 struct wl_cursor_image *
1317 display_get_pointer_image(struct display *display, int pointer)
1319 struct wl_cursor *cursor = display->cursors[pointer];
1321 return cursor ? cursor->images[0] : NULL;
1325 surface_flush(struct surface *surface)
1327 if (!surface->cairo_surface)
1330 if (surface->opaque_region) {
1331 wl_surface_set_opaque_region(surface->surface,
1332 surface->opaque_region);
1333 wl_region_destroy(surface->opaque_region);
1334 surface->opaque_region = NULL;
1337 if (surface->input_region) {
1338 wl_surface_set_input_region(surface->surface,
1339 surface->input_region);
1340 wl_region_destroy(surface->input_region);
1341 surface->input_region = NULL;
1344 if (surface->window->next_attach_serial > 0) {
1345 xdg_surface_ack_configure(surface->window->xdg_surface,
1346 surface->window->next_attach_serial);
1347 surface->window->next_attach_serial = 0;
1350 surface->toysurface->swap(surface->toysurface,
1351 surface->buffer_transform, surface->buffer_scale,
1352 &surface->server_allocation);
1354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
1359 window_has_focus(struct window *window)
1361 return window->focused;
1365 window_close(struct window *window)
1367 if (window->close_handler)
1368 window->close_handler(window->user_data);
1370 display_exit(window->display);
1374 window_get_display(struct window *window)
1376 return window->display;
1380 surface_create_surface(struct surface *surface, uint32_t flags)
1382 struct display *display = surface->window->display;
1383 struct rectangle allocation = surface->allocation;
1385 if (!surface->toysurface && display->dpy &&
1386 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1387 surface->toysurface =
1388 egl_window_surface_create(display,
1394 if (!surface->toysurface)
1395 surface->toysurface = shm_surface_create(display,
1397 flags, &allocation);
1399 surface->cairo_surface = surface->toysurface->prepare(
1400 surface->toysurface, 0, 0,
1401 allocation.width, allocation.height, flags,
1402 surface->buffer_transform, surface->buffer_scale);
1406 window_create_main_surface(struct window *window)
1408 struct surface *surface = window->main_surface;
1411 if (window->resizing)
1412 flags |= SURFACE_HINT_RESIZE;
1414 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1415 flags |= SURFACE_HINT_RGB565;
1417 surface_create_surface(surface, flags);
1421 window_get_buffer_transform(struct window *window)
1423 return window->main_surface->buffer_transform;
1427 window_set_buffer_transform(struct window *window,
1428 enum wl_output_transform transform)
1430 window->main_surface->buffer_transform = transform;
1431 wl_surface_set_buffer_transform(window->main_surface->surface,
1436 window_set_buffer_scale(struct window *window,
1439 window->main_surface->buffer_scale = scale;
1440 wl_surface_set_buffer_scale(window->main_surface->surface,
1445 window_get_buffer_scale(struct window *window)
1447 return window->main_surface->buffer_scale;
1451 window_get_output_scale(struct window *window)
1453 struct window_output *window_output;
1454 struct window_output *window_output_tmp;
1457 wl_list_for_each_safe(window_output, window_output_tmp,
1458 &window->window_output_list, link) {
1459 if (window_output->output->scale > scale)
1460 scale = window_output->output->scale;
1466 static void window_frame_destroy(struct window_frame *frame);
1469 surface_destroy(struct surface *surface)
1471 if (surface->frame_cb)
1472 wl_callback_destroy(surface->frame_cb);
1474 if (surface->input_region)
1475 wl_region_destroy(surface->input_region);
1477 if (surface->opaque_region)
1478 wl_region_destroy(surface->opaque_region);
1480 if (surface->subsurface)
1481 wl_subsurface_destroy(surface->subsurface);
1483 wl_surface_destroy(surface->surface);
1485 if (surface->toysurface)
1486 surface->toysurface->destroy(surface->toysurface);
1488 wl_list_remove(&surface->link);
1493 window_destroy(struct window *window)
1495 struct display *display = window->display;
1496 struct input *input;
1497 struct window_output *window_output;
1498 struct window_output *window_output_tmp;
1500 wl_list_remove(&window->redraw_task.link);
1502 wl_list_for_each(input, &display->input_list, link) {
1503 if (input->touch_focus == window)
1504 input->touch_focus = NULL;
1505 if (input->pointer_focus == window)
1506 input->pointer_focus = NULL;
1507 if (input->keyboard_focus == window)
1508 input->keyboard_focus = NULL;
1509 if (input->focus_widget &&
1510 input->focus_widget->window == window)
1511 input->focus_widget = NULL;
1514 wl_list_for_each_safe(window_output, window_output_tmp,
1515 &window->window_output_list, link) {
1516 free (window_output);
1520 window_frame_destroy(window->frame);
1522 if (window->xdg_surface)
1523 xdg_surface_destroy(window->xdg_surface);
1524 if (window->xdg_popup)
1525 xdg_popup_destroy(window->xdg_popup);
1527 surface_destroy(window->main_surface);
1529 wl_list_remove(&window->link);
1531 free(window->title);
1535 static struct widget *
1536 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1538 struct widget *child, *target;
1540 wl_list_for_each(child, &widget->child_list, link) {
1541 target = widget_find_widget(child, x, y);
1546 if (widget->allocation.x <= x &&
1547 x < widget->allocation.x + widget->allocation.width &&
1548 widget->allocation.y <= y &&
1549 y < widget->allocation.y + widget->allocation.height) {
1556 static struct widget *
1557 window_find_widget(struct window *window, int32_t x, int32_t y)
1559 struct surface *surface;
1560 struct widget *widget;
1562 wl_list_for_each(surface, &window->subsurface_list, link) {
1563 widget = widget_find_widget(surface->widget, x, y);
1571 static struct widget *
1572 widget_create(struct window *window, struct surface *surface, void *data)
1574 struct widget *widget;
1576 widget = xzalloc(sizeof *widget);
1577 widget->window = window;
1578 widget->surface = surface;
1579 widget->user_data = data;
1580 widget->allocation = surface->allocation;
1581 wl_list_init(&widget->child_list);
1583 widget->tooltip = NULL;
1584 widget->tooltip_count = 0;
1585 widget->default_cursor = CURSOR_LEFT_PTR;
1586 widget->use_cairo = 1;
1592 window_add_widget(struct window *window, void *data)
1594 struct widget *widget;
1596 widget = widget_create(window, window->main_surface, data);
1597 wl_list_init(&widget->link);
1598 window->main_surface->widget = widget;
1604 widget_add_widget(struct widget *parent, void *data)
1606 struct widget *widget;
1608 widget = widget_create(parent->window, parent->surface, data);
1609 wl_list_insert(parent->child_list.prev, &widget->link);
1615 widget_destroy(struct widget *widget)
1617 struct display *display = widget->window->display;
1618 struct surface *surface = widget->surface;
1619 struct input *input;
1621 /* Destroy the sub-surface along with the root widget */
1622 if (surface->widget == widget && surface->subsurface)
1623 surface_destroy(widget->surface);
1625 if (widget->tooltip)
1626 widget_destroy_tooltip(widget);
1628 wl_list_for_each(input, &display->input_list, link) {
1629 if (input->focus_widget == widget)
1630 input->focus_widget = NULL;
1633 wl_list_remove(&widget->link);
1638 widget_set_default_cursor(struct widget *widget, int cursor)
1640 widget->default_cursor = cursor;
1644 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1646 *allocation = widget->allocation;
1650 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1652 widget->allocation.width = width;
1653 widget->allocation.height = height;
1657 widget_set_allocation(struct widget *widget,
1658 int32_t x, int32_t y, int32_t width, int32_t height)
1660 widget->allocation.x = x;
1661 widget->allocation.y = y;
1662 widget_set_size(widget, width, height);
1666 widget_set_transparent(struct widget *widget, int transparent)
1668 widget->opaque = !transparent;
1672 widget_get_user_data(struct widget *widget)
1674 return widget->user_data;
1677 static cairo_surface_t *
1678 widget_get_cairo_surface(struct widget *widget)
1680 struct surface *surface = widget->surface;
1681 struct window *window = widget->window;
1683 assert(widget->use_cairo);
1685 if (!surface->cairo_surface) {
1686 if (surface == window->main_surface)
1687 window_create_main_surface(window);
1689 surface_create_surface(surface, 0);
1692 return surface->cairo_surface;
1696 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1698 struct surface *surface = widget->surface;
1701 enum wl_output_transform transform;
1702 int surface_width, surface_height;
1703 int translate_x, translate_y;
1706 surface_width = surface->allocation.width;
1707 surface_height = surface->allocation.height;
1709 transform = surface->buffer_transform;
1710 scale = surface->buffer_scale;
1712 switch (transform) {
1713 case WL_OUTPUT_TRANSFORM_FLIPPED:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1715 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1717 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1720 cairo_matrix_init_identity(&m);
1724 switch (transform) {
1725 case WL_OUTPUT_TRANSFORM_NORMAL:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED:
1733 translate_x = surface_width;
1736 case WL_OUTPUT_TRANSFORM_90:
1738 translate_x = surface_height;
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1743 translate_x = surface_height;
1744 translate_y = surface_width;
1746 case WL_OUTPUT_TRANSFORM_180:
1748 translate_x = surface_width;
1749 translate_y = surface_height;
1751 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1754 translate_y = surface_height;
1756 case WL_OUTPUT_TRANSFORM_270:
1757 angle = M_PI + M_PI_2;
1759 translate_y = surface_width;
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1762 angle = M_PI + M_PI_2;
1768 cairo_scale(cr, scale, scale);
1769 cairo_translate(cr, translate_x, translate_y);
1770 cairo_rotate(cr, angle);
1771 cairo_transform(cr, &m);
1775 widget_cairo_create(struct widget *widget)
1777 struct surface *surface = widget->surface;
1778 cairo_surface_t *cairo_surface;
1781 cairo_surface = widget_get_cairo_surface(widget);
1782 cr = cairo_create(cairo_surface);
1784 widget_cairo_update_transform(widget, cr);
1786 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1792 widget_get_wl_surface(struct widget *widget)
1794 return widget->surface->surface;
1797 struct wl_subsurface *
1798 widget_get_wl_subsurface(struct widget *widget)
1800 return widget->surface->subsurface;
1804 widget_get_last_time(struct widget *widget)
1806 return widget->surface->last_time;
1810 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1812 struct wl_compositor *comp = widget->window->display->compositor;
1813 struct surface *surface = widget->surface;
1815 if (!surface->input_region)
1816 surface->input_region = wl_compositor_create_region(comp);
1819 wl_region_add(surface->input_region,
1820 rect->x, rect->y, rect->width, rect->height);
1825 widget_set_resize_handler(struct widget *widget,
1826 widget_resize_handler_t handler)
1828 widget->resize_handler = handler;
1832 widget_set_redraw_handler(struct widget *widget,
1833 widget_redraw_handler_t handler)
1835 widget->redraw_handler = handler;
1839 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1841 widget->enter_handler = handler;
1845 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1847 widget->leave_handler = handler;
1851 widget_set_motion_handler(struct widget *widget,
1852 widget_motion_handler_t handler)
1854 widget->motion_handler = handler;
1858 widget_set_button_handler(struct widget *widget,
1859 widget_button_handler_t handler)
1861 widget->button_handler = handler;
1865 widget_set_touch_up_handler(struct widget *widget,
1866 widget_touch_up_handler_t handler)
1868 widget->touch_up_handler = handler;
1872 widget_set_touch_down_handler(struct widget *widget,
1873 widget_touch_down_handler_t handler)
1875 widget->touch_down_handler = handler;
1879 widget_set_touch_motion_handler(struct widget *widget,
1880 widget_touch_motion_handler_t handler)
1882 widget->touch_motion_handler = handler;
1886 widget_set_touch_frame_handler(struct widget *widget,
1887 widget_touch_frame_handler_t handler)
1889 widget->touch_frame_handler = handler;
1893 widget_set_touch_cancel_handler(struct widget *widget,
1894 widget_touch_cancel_handler_t handler)
1896 widget->touch_cancel_handler = handler;
1900 widget_set_axis_handler(struct widget *widget,
1901 widget_axis_handler_t handler)
1903 widget->axis_handler = handler;
1907 window_schedule_redraw_task(struct window *window);
1910 widget_schedule_redraw(struct widget *widget)
1912 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1913 widget->surface->redraw_needed = 1;
1914 window_schedule_redraw_task(widget->window);
1918 widget_set_use_cairo(struct widget *widget,
1921 widget->use_cairo = use_cairo;
1925 window_get_surface(struct window *window)
1927 cairo_surface_t *cairo_surface;
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1931 return cairo_surface_reference(cairo_surface);
1935 window_get_wl_surface(struct window *window)
1937 return window->main_surface->surface;
1941 tooltip_redraw_handler(struct widget *widget, void *data)
1944 const int32_t r = 3;
1945 struct tooltip *tooltip = data;
1946 int32_t width, height;
1948 cr = widget_cairo_create(widget);
1949 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1950 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1951 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1954 width = widget->allocation.width;
1955 height = widget->allocation.height;
1956 rounded_rect(cr, 0, 0, width, height, r);
1958 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1959 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1962 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1963 cairo_move_to(cr, 10, 16);
1964 cairo_show_text(cr, tooltip->entry);
1968 static cairo_text_extents_t
1969 get_text_extents(struct display *display, struct tooltip *tooltip)
1972 cairo_text_extents_t extents;
1974 /* Use the dummy_surface because tooltip's surface was not
1975 * created yet, and parent does not have a valid surface
1976 * outside repaint, either.
1978 cr = cairo_create(display->dummy_surface);
1979 cairo_text_extents(cr, tooltip->entry, &extents);
1986 window_create_tooltip(struct tooltip *tooltip)
1988 struct widget *parent = tooltip->parent;
1989 struct display *display = parent->window->display;
1990 const int offset_y = 27;
1991 const int margin = 3;
1992 cairo_text_extents_t extents;
1994 if (tooltip->widget)
1997 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1999 extents = get_text_extents(display, tooltip);
2000 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2001 widget_set_allocation(tooltip->widget,
2002 tooltip->x, tooltip->y + offset_y,
2003 extents.width + 20, 20 + margin * 2);
2009 widget_destroy_tooltip(struct widget *parent)
2011 struct tooltip *tooltip = parent->tooltip;
2013 parent->tooltip_count = 0;
2017 if (tooltip->widget) {
2018 widget_destroy(tooltip->widget);
2019 tooltip->widget = NULL;
2022 close(tooltip->tooltip_fd);
2023 free(tooltip->entry);
2025 parent->tooltip = NULL;
2029 tooltip_func(struct task *task, uint32_t events)
2031 struct tooltip *tooltip =
2032 container_of(task, struct tooltip, tooltip_task);
2035 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2037 window_create_tooltip(tooltip);
2040 #define TOOLTIP_TIMEOUT 500
2042 tooltip_timer_reset(struct tooltip *tooltip)
2044 struct itimerspec its;
2046 its.it_interval.tv_sec = 0;
2047 its.it_interval.tv_nsec = 0;
2048 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2049 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2050 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2051 fprintf(stderr, "could not set timerfd\n: %m");
2059 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2061 struct tooltip *tooltip = parent->tooltip;
2063 parent->tooltip_count++;
2067 tooltip_timer_reset(tooltip);
2071 /* the handler might be triggered too fast via input device motion, so
2072 * we need this check here to make sure tooltip is fully initialized */
2073 if (parent->tooltip_count > 1)
2076 tooltip = malloc(sizeof *tooltip);
2080 parent->tooltip = tooltip;
2081 tooltip->parent = parent;
2082 tooltip->widget = NULL;
2085 tooltip->entry = strdup(entry);
2086 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2087 if (tooltip->tooltip_fd < 0) {
2088 fprintf(stderr, "could not create timerfd\n: %m");
2092 tooltip->tooltip_task.run = tooltip_func;
2093 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2094 EPOLLIN, &tooltip->tooltip_task);
2095 tooltip_timer_reset(tooltip);
2101 workspace_manager_state(void *data,
2102 struct workspace_manager *workspace_manager,
2106 struct display *display = data;
2108 display->workspace = current;
2109 display->workspace_count = count;
2112 static const struct workspace_manager_listener workspace_manager_listener = {
2113 workspace_manager_state
2117 frame_resize_handler(struct widget *widget,
2118 int32_t width, int32_t height, void *data)
2120 struct window_frame *frame = data;
2121 struct widget *child = frame->child;
2122 struct rectangle interior;
2123 struct rectangle input;
2124 struct rectangle opaque;
2126 if (widget->window->fullscreen) {
2129 interior.width = width;
2130 interior.height = height;
2132 frame_resize(frame->frame, width, height);
2133 frame_interior(frame->frame, &interior.x, &interior.y,
2134 &interior.width, &interior.height);
2137 widget_set_allocation(child, interior.x, interior.y,
2138 interior.width, interior.height);
2140 if (child->resize_handler) {
2141 child->resize_handler(child, interior.width, interior.height,
2144 if (widget->window->fullscreen) {
2145 width = child->allocation.width;
2146 height = child->allocation.height;
2148 frame_resize_inside(frame->frame,
2149 child->allocation.width,
2150 child->allocation.height);
2151 width = frame_width(frame->frame);
2152 height = frame_height(frame->frame);
2156 widget_set_allocation(widget, 0, 0, width, height);
2158 widget->surface->input_region =
2159 wl_compositor_create_region(widget->window->display->compositor);
2160 if (!widget->window->fullscreen) {
2161 frame_input_rect(frame->frame, &input.x, &input.y,
2162 &input.width, &input.height);
2163 wl_region_add(widget->surface->input_region,
2164 input.x, input.y, input.width, input.height);
2166 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2169 widget_set_allocation(widget, 0, 0, width, height);
2171 if (child->opaque) {
2172 if (!widget->window->fullscreen) {
2173 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2174 &opaque.width, &opaque.height);
2176 wl_region_add(widget->surface->opaque_region,
2178 opaque.width, opaque.height);
2180 wl_region_add(widget->surface->opaque_region,
2181 0, 0, width, height);
2186 widget_schedule_redraw(widget);
2190 frame_redraw_handler(struct widget *widget, void *data)
2193 struct window_frame *frame = data;
2194 struct window *window = widget->window;
2196 if (window->fullscreen)
2199 cr = widget_cairo_create(widget);
2201 frame_repaint(frame->frame, cr);
2207 frame_get_pointer_image_for_location(struct window_frame *frame,
2208 enum theme_location location)
2210 struct window *window = frame->widget->window;
2213 return CURSOR_LEFT_PTR;
2216 case THEME_LOCATION_RESIZING_TOP:
2218 case THEME_LOCATION_RESIZING_BOTTOM:
2219 return CURSOR_BOTTOM;
2220 case THEME_LOCATION_RESIZING_LEFT:
2222 case THEME_LOCATION_RESIZING_RIGHT:
2223 return CURSOR_RIGHT;
2224 case THEME_LOCATION_RESIZING_TOP_LEFT:
2225 return CURSOR_TOP_LEFT;
2226 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2227 return CURSOR_TOP_RIGHT;
2228 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2229 return CURSOR_BOTTOM_LEFT;
2230 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2231 return CURSOR_BOTTOM_RIGHT;
2232 case THEME_LOCATION_EXTERIOR:
2233 case THEME_LOCATION_TITLEBAR:
2235 return CURSOR_LEFT_PTR;
2240 frame_menu_func(struct window *window,
2241 struct input *input, int index, void *data)
2243 struct display *display;
2247 window_close(window);
2249 case 1: /* move to workspace above */
2250 display = window->display;
2251 if (display->workspace > 0)
2252 workspace_manager_move_surface(
2253 display->workspace_manager,
2254 window->main_surface->surface,
2255 display->workspace - 1);
2257 case 2: /* move to workspace below */
2258 display = window->display;
2259 if (display->workspace < display->workspace_count - 1)
2260 workspace_manager_move_surface(
2261 display->workspace_manager,
2262 window->main_surface->surface,
2263 display->workspace + 1);
2265 case 3: /* fullscreen */
2266 /* we don't have a way to get out of fullscreen for now */
2267 if (window->fullscreen_handler)
2268 window->fullscreen_handler(window, window->user_data);
2274 window_show_frame_menu(struct window *window,
2275 struct input *input, uint32_t time)
2280 static const char *entries[] = {
2282 "Move to workspace above", "Move to workspace below",
2286 if (window->fullscreen_handler)
2287 count = ARRAY_LENGTH(entries);
2289 count = ARRAY_LENGTH(entries) - 1;
2291 input_get_position(input, &x, &y);
2292 window_show_menu(window->display, input, time, window,
2293 x - 10, y - 10, frame_menu_func, entries, count);
2297 frame_enter_handler(struct widget *widget,
2298 struct input *input, float x, float y, void *data)
2300 struct window_frame *frame = data;
2301 enum theme_location location;
2303 location = frame_pointer_enter(frame->frame, input, x, y);
2304 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2305 widget_schedule_redraw(frame->widget);
2307 return frame_get_pointer_image_for_location(data, location);
2311 frame_motion_handler(struct widget *widget,
2312 struct input *input, uint32_t time,
2313 float x, float y, void *data)
2315 struct window_frame *frame = data;
2316 enum theme_location location;
2318 location = frame_pointer_motion(frame->frame, input, x, y);
2319 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2320 widget_schedule_redraw(frame->widget);
2322 return frame_get_pointer_image_for_location(data, location);
2326 frame_leave_handler(struct widget *widget,
2327 struct input *input, void *data)
2329 struct window_frame *frame = data;
2331 frame_pointer_leave(frame->frame, input);
2332 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2333 widget_schedule_redraw(frame->widget);
2337 frame_handle_status(struct window_frame *frame, struct input *input,
2338 uint32_t time, enum theme_location location)
2340 struct window *window = frame->widget->window;
2343 status = frame_status(frame->frame);
2344 if (status & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2347 if (status & FRAME_STATUS_MINIMIZE) {
2348 window_set_minimized(window);
2349 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2352 if (status & FRAME_STATUS_MENU) {
2353 window_show_frame_menu(window, input, time);
2354 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2357 if (status & FRAME_STATUS_MAXIMIZE) {
2358 window_set_maximized(window, !window->maximized);
2359 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2362 if (status & FRAME_STATUS_CLOSE) {
2363 window_close(window);
2367 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2368 input_ungrab(input);
2369 xdg_surface_move(window->xdg_surface,
2370 input_get_seat(input),
2371 window->display->serial);
2373 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2376 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2377 input_ungrab(input);
2379 xdg_surface_resize(window->xdg_surface,
2380 input_get_seat(input),
2381 window->display->serial,
2384 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2389 frame_button_handler(struct widget *widget,
2390 struct input *input, uint32_t time,
2391 uint32_t button, enum wl_pointer_button_state state,
2395 struct window_frame *frame = data;
2396 enum theme_location location;
2398 location = frame_pointer_button(frame->frame, input, button, state);
2399 frame_handle_status(frame, input, time, location);
2403 frame_touch_down_handler(struct widget *widget, struct input *input,
2404 uint32_t serial, uint32_t time, int32_t id,
2405 float x, float y, void *data)
2407 struct window_frame *frame = data;
2409 frame_touch_down(frame->frame, input, id, x, y);
2410 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2414 frame_touch_up_handler(struct widget *widget,
2415 struct input *input, uint32_t serial, uint32_t time,
2416 int32_t id, void *data)
2418 struct window_frame *frame = data;
2420 frame_touch_up(frame->frame, input, id);
2421 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2425 window_frame_create(struct window *window, void *data)
2427 struct window_frame *frame;
2430 if (window->custom) {
2431 buttons = FRAME_BUTTON_NONE;
2433 buttons = FRAME_BUTTON_ALL;
2436 frame = xzalloc(sizeof *frame);
2437 frame->frame = frame_create(window->display->theme, 0, 0,
2438 buttons, window->title);
2440 frame->widget = window_add_widget(window, frame);
2441 frame->child = widget_add_widget(frame->widget, data);
2443 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2444 widget_set_resize_handler(frame->widget, frame_resize_handler);
2445 widget_set_enter_handler(frame->widget, frame_enter_handler);
2446 widget_set_leave_handler(frame->widget, frame_leave_handler);
2447 widget_set_motion_handler(frame->widget, frame_motion_handler);
2448 widget_set_button_handler(frame->widget, frame_button_handler);
2449 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2450 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2452 window->frame = frame;
2454 return frame->child;
2458 window_frame_set_child_size(struct widget *widget, int child_width,
2461 struct display *display = widget->window->display;
2462 struct theme *t = display->theme;
2463 int decoration_width, decoration_height;
2465 int margin = widget->window->maximized ? 0 : t->margin;
2467 if (!widget->window->fullscreen) {
2468 decoration_width = (t->width + margin) * 2;
2469 decoration_height = t->width +
2470 t->titlebar_height + margin * 2;
2472 width = child_width + decoration_width;
2473 height = child_height + decoration_height;
2475 width = child_width;
2476 height = child_height;
2479 window_schedule_resize(widget->window, width, height);
2483 window_frame_destroy(struct window_frame *frame)
2485 frame_destroy(frame->frame);
2487 /* frame->child must be destroyed by the application */
2488 widget_destroy(frame->widget);
2493 input_set_focus_widget(struct input *input, struct widget *focus,
2496 struct widget *old, *widget;
2499 if (focus == input->focus_widget)
2502 old = input->focus_widget;
2506 widget = input->grab;
2507 if (widget->leave_handler)
2508 widget->leave_handler(old, input, widget->user_data);
2509 input->focus_widget = NULL;
2515 widget = input->grab;
2516 input->focus_widget = focus;
2517 if (widget->enter_handler)
2518 cursor = widget->enter_handler(focus, input, x, y,
2521 cursor = widget->default_cursor;
2523 input_set_pointer_image(input, cursor);
2528 touch_grab(struct input *input, int32_t touch_id)
2530 input->touch_grab = 1;
2531 input->touch_grab_id = touch_id;
2535 touch_ungrab(struct input *input)
2537 struct touch_point *tp, *tmp;
2539 input->touch_grab = 0;
2541 wl_list_for_each_safe(tp, tmp,
2542 &input->touch_point_list, link) {
2543 if (tp->id != input->touch_grab_id)
2545 wl_list_remove(&tp->link);
2553 input_grab(struct input *input, struct widget *widget, uint32_t button)
2555 input->grab = widget;
2556 input->grab_button = button;
2558 input_set_focus_widget(input, widget, input->sx, input->sy);
2562 input_ungrab(struct input *input)
2564 struct widget *widget;
2567 if (input->pointer_focus) {
2568 widget = window_find_widget(input->pointer_focus,
2569 input->sx, input->sy);
2570 input_set_focus_widget(input, widget, input->sx, input->sy);
2575 input_remove_pointer_focus(struct input *input)
2577 struct window *window = input->pointer_focus;
2582 input_set_focus_widget(input, NULL, 0, 0);
2584 input->pointer_focus = NULL;
2585 input->current_cursor = CURSOR_UNSET;
2589 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2590 uint32_t serial, struct wl_surface *surface,
2591 wl_fixed_t sx_w, wl_fixed_t sy_w)
2593 struct input *input = data;
2594 struct window *window;
2595 struct widget *widget;
2596 float sx = wl_fixed_to_double(sx_w);
2597 float sy = wl_fixed_to_double(sy_w);
2600 /* enter event for a window we've just destroyed */
2604 window = wl_surface_get_user_data(surface);
2605 if (surface != window->main_surface->surface) {
2606 DBG("Ignoring input event from subsurface %p\n", surface);
2610 input->display->serial = serial;
2611 input->pointer_enter_serial = serial;
2612 input->pointer_focus = window;
2617 widget = window_find_widget(window, sx, sy);
2618 input_set_focus_widget(input, widget, sx, sy);
2622 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2623 uint32_t serial, struct wl_surface *surface)
2625 struct input *input = data;
2627 input->display->serial = serial;
2628 input_remove_pointer_focus(input);
2632 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2633 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2635 struct input *input = data;
2636 struct window *window = input->pointer_focus;
2637 struct widget *widget;
2639 float sx = wl_fixed_to_double(sx_w);
2640 float sy = wl_fixed_to_double(sy_w);
2648 /* when making the window smaller - e.g. after a unmaximise we might
2649 * still have a pending motion event that the compositor has picked
2650 * based on the old surface dimensions
2652 if (sx > window->main_surface->allocation.width ||
2653 sy > window->main_surface->allocation.height)
2656 if (!(input->grab && input->grab_button)) {
2657 widget = window_find_widget(window, sx, sy);
2658 input_set_focus_widget(input, widget, sx, sy);
2662 widget = input->grab;
2664 widget = input->focus_widget;
2666 if (widget->motion_handler)
2667 cursor = widget->motion_handler(input->focus_widget,
2668 input, time, sx, sy,
2671 cursor = widget->default_cursor;
2673 cursor = CURSOR_LEFT_PTR;
2675 input_set_pointer_image(input, cursor);
2679 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2680 uint32_t time, uint32_t button, uint32_t state_w)
2682 struct input *input = data;
2683 struct widget *widget;
2684 enum wl_pointer_button_state state = state_w;
2686 input->display->serial = serial;
2687 if (input->focus_widget && input->grab == NULL &&
2688 state == WL_POINTER_BUTTON_STATE_PRESSED)
2689 input_grab(input, input->focus_widget, button);
2691 widget = input->grab;
2692 if (widget && widget->button_handler)
2693 (*widget->button_handler)(widget,
2696 input->grab->user_data);
2698 if (input->grab && input->grab_button == button &&
2699 state == WL_POINTER_BUTTON_STATE_RELEASED)
2700 input_ungrab(input);
2704 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2705 uint32_t time, uint32_t axis, wl_fixed_t value)
2707 struct input *input = data;
2708 struct widget *widget;
2710 widget = input->focus_widget;
2712 widget = input->grab;
2713 if (widget && widget->axis_handler)
2714 (*widget->axis_handler)(widget,
2720 static const struct wl_pointer_listener pointer_listener = {
2721 pointer_handle_enter,
2722 pointer_handle_leave,
2723 pointer_handle_motion,
2724 pointer_handle_button,
2725 pointer_handle_axis,
2729 input_remove_keyboard_focus(struct input *input)
2731 struct window *window = input->keyboard_focus;
2732 struct itimerspec its;
2734 its.it_interval.tv_sec = 0;
2735 its.it_interval.tv_nsec = 0;
2736 its.it_value.tv_sec = 0;
2737 its.it_value.tv_nsec = 0;
2738 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2743 if (window->keyboard_focus_handler)
2744 (*window->keyboard_focus_handler)(window, NULL,
2747 input->keyboard_focus = NULL;
2751 keyboard_repeat_func(struct task *task, uint32_t events)
2753 struct input *input =
2754 container_of(task, struct input, repeat_task);
2755 struct window *window = input->keyboard_focus;
2758 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2759 /* If we change the timer between the fd becoming
2760 * readable and getting here, there'll be nothing to
2761 * read and we get EAGAIN. */
2764 if (window && window->key_handler) {
2765 (*window->key_handler)(window, input, input->repeat_time,
2766 input->repeat_key, input->repeat_sym,
2767 WL_KEYBOARD_KEY_STATE_PRESSED,
2773 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2774 uint32_t format, int fd, uint32_t size)
2776 struct input *input = data;
2777 struct xkb_keymap *keymap;
2778 struct xkb_state *state;
2786 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2791 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2792 if (map_str == MAP_FAILED) {
2797 keymap = xkb_map_new_from_string(input->display->xkb_context,
2799 XKB_KEYMAP_FORMAT_TEXT_V1,
2801 munmap(map_str, size);
2805 fprintf(stderr, "failed to compile keymap\n");
2809 state = xkb_state_new(keymap);
2811 fprintf(stderr, "failed to create XKB state\n");
2812 xkb_map_unref(keymap);
2816 xkb_keymap_unref(input->xkb.keymap);
2817 xkb_state_unref(input->xkb.state);
2818 input->xkb.keymap = keymap;
2819 input->xkb.state = state;
2821 input->xkb.control_mask =
2822 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2823 input->xkb.alt_mask =
2824 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2825 input->xkb.shift_mask =
2826 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2830 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2831 uint32_t serial, struct wl_surface *surface,
2832 struct wl_array *keys)
2834 struct input *input = data;
2835 struct window *window;
2837 input->display->serial = serial;
2838 input->keyboard_focus = wl_surface_get_user_data(surface);
2840 window = input->keyboard_focus;
2841 if (window->keyboard_focus_handler)
2842 (*window->keyboard_focus_handler)(window,
2843 input, window->user_data);
2847 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2848 uint32_t serial, struct wl_surface *surface)
2850 struct input *input = data;
2852 input->display->serial = serial;
2853 input_remove_keyboard_focus(input);
2857 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2858 uint32_t serial, uint32_t time, uint32_t key,
2861 struct input *input = data;
2862 struct window *window = input->keyboard_focus;
2863 uint32_t code, num_syms;
2864 enum wl_keyboard_key_state state = state_w;
2865 const xkb_keysym_t *syms;
2867 struct itimerspec its;
2869 input->display->serial = serial;
2871 if (!window || !input->xkb.state)
2874 /* We only use input grabs for pointer events for now, so just
2875 * ignore key presses if a grab is active. We expand the key
2876 * event delivery mechanism to route events to widgets to
2877 * properly handle key grabs. In the meantime, this prevents
2878 * key event devlivery while a grab is active. */
2879 if (input->grab && input->grab_button == 0)
2882 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2884 sym = XKB_KEY_NoSymbol;
2889 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2890 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2891 window_set_maximized(window, !window->maximized);
2892 } else if (sym == XKB_KEY_F11 &&
2893 window->fullscreen_handler &&
2894 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2895 window->fullscreen_handler(window, window->user_data);
2896 } else if (sym == XKB_KEY_F4 &&
2897 input->modifiers == MOD_ALT_MASK &&
2898 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2899 window_close(window);
2900 } else if (window->key_handler) {
2901 (*window->key_handler)(window, input, time, key,
2902 sym, state, window->user_data);
2905 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2906 key == input->repeat_key) {
2907 its.it_interval.tv_sec = 0;
2908 its.it_interval.tv_nsec = 0;
2909 its.it_value.tv_sec = 0;
2910 its.it_value.tv_nsec = 0;
2911 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2912 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2913 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2914 input->repeat_sym = sym;
2915 input->repeat_key = key;
2916 input->repeat_time = time;
2917 its.it_interval.tv_sec = 0;
2918 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2919 its.it_value.tv_sec = 0;
2920 its.it_value.tv_nsec = 400 * 1000 * 1000;
2921 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2926 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2927 uint32_t serial, uint32_t mods_depressed,
2928 uint32_t mods_latched, uint32_t mods_locked,
2931 struct input *input = data;
2932 xkb_mod_mask_t mask;
2934 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2935 if (!input->xkb.keymap)
2938 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2939 mods_locked, 0, 0, group);
2940 mask = xkb_state_serialize_mods(input->xkb.state,
2941 XKB_STATE_DEPRESSED |
2943 input->modifiers = 0;
2944 if (mask & input->xkb.control_mask)
2945 input->modifiers |= MOD_CONTROL_MASK;
2946 if (mask & input->xkb.alt_mask)
2947 input->modifiers |= MOD_ALT_MASK;
2948 if (mask & input->xkb.shift_mask)
2949 input->modifiers |= MOD_SHIFT_MASK;
2952 static const struct wl_keyboard_listener keyboard_listener = {
2953 keyboard_handle_keymap,
2954 keyboard_handle_enter,
2955 keyboard_handle_leave,
2956 keyboard_handle_key,
2957 keyboard_handle_modifiers,
2961 touch_handle_down(void *data, struct wl_touch *wl_touch,
2962 uint32_t serial, uint32_t time, struct wl_surface *surface,
2963 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2965 struct input *input = data;
2966 struct widget *widget;
2967 float sx = wl_fixed_to_double(x_w);
2968 float sy = wl_fixed_to_double(y_w);
2970 input->display->serial = serial;
2971 input->touch_focus = wl_surface_get_user_data(surface);
2972 if (!input->touch_focus) {
2973 DBG("Failed to find to touch focus for surface %p\n", surface);
2977 if (surface != input->touch_focus->main_surface->surface) {
2978 DBG("Ignoring input event from subsurface %p\n", surface);
2979 input->touch_focus = NULL;
2984 widget = input->grab;
2986 widget = window_find_widget(input->touch_focus,
2987 wl_fixed_to_double(x_w),
2988 wl_fixed_to_double(y_w));
2990 struct touch_point *tp = xmalloc(sizeof *tp);
2993 tp->widget = widget;
2996 wl_list_insert(&input->touch_point_list, &tp->link);
2998 if (widget->touch_down_handler)
2999 (*widget->touch_down_handler)(widget, input,
3008 touch_handle_up(void *data, struct wl_touch *wl_touch,
3009 uint32_t serial, uint32_t time, int32_t id)
3011 struct input *input = data;
3012 struct touch_point *tp, *tmp;
3014 if (!input->touch_focus) {
3015 DBG("No touch focus found for touch up event!\n");
3019 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3023 if (tp->widget->touch_up_handler)
3024 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3026 tp->widget->user_data);
3028 wl_list_remove(&tp->link);
3036 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3037 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3039 struct input *input = data;
3040 struct touch_point *tp;
3041 float sx = wl_fixed_to_double(x_w);
3042 float sy = wl_fixed_to_double(y_w);
3044 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3046 if (!input->touch_focus) {
3047 DBG("No touch focus found for touch motion event!\n");
3051 wl_list_for_each(tp, &input->touch_point_list, link) {
3057 if (tp->widget->touch_motion_handler)
3058 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3060 tp->widget->user_data);
3066 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3068 struct input *input = data;
3069 struct touch_point *tp, *tmp;
3071 DBG("touch_handle_frame\n");
3073 if (!input->touch_focus) {
3074 DBG("No touch focus found for touch frame event!\n");
3078 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3079 if (tp->widget->touch_frame_handler)
3080 (*tp->widget->touch_frame_handler)(tp->widget, input,
3081 tp->widget->user_data);
3086 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3088 struct input *input = data;
3089 struct touch_point *tp, *tmp;
3091 DBG("touch_handle_cancel\n");
3093 if (!input->touch_focus) {
3094 DBG("No touch focus found for touch cancel event!\n");
3098 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3099 if (tp->widget->touch_cancel_handler)
3100 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3101 tp->widget->user_data);
3103 wl_list_remove(&tp->link);
3108 static const struct wl_touch_listener touch_listener = {
3111 touch_handle_motion,
3113 touch_handle_cancel,
3117 seat_handle_capabilities(void *data, struct wl_seat *seat,
3118 enum wl_seat_capability caps)
3120 struct input *input = data;
3122 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3123 input->pointer = wl_seat_get_pointer(seat);
3124 wl_pointer_set_user_data(input->pointer, input);
3125 wl_pointer_add_listener(input->pointer, &pointer_listener,
3127 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3128 wl_pointer_destroy(input->pointer);
3129 input->pointer = NULL;
3132 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3133 input->keyboard = wl_seat_get_keyboard(seat);
3134 wl_keyboard_set_user_data(input->keyboard, input);
3135 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3137 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3138 wl_keyboard_destroy(input->keyboard);
3139 input->keyboard = NULL;
3142 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3143 input->touch = wl_seat_get_touch(seat);
3144 wl_touch_set_user_data(input->touch, input);
3145 wl_touch_add_listener(input->touch, &touch_listener, input);
3146 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3147 wl_touch_destroy(input->touch);
3148 input->touch = NULL;
3153 seat_handle_name(void *data, struct wl_seat *seat,
3159 static const struct wl_seat_listener seat_listener = {
3160 seat_handle_capabilities,
3165 input_get_position(struct input *input, int32_t *x, int32_t *y)
3172 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3174 struct touch_point *tp;
3176 wl_list_for_each(tp, &input->touch_point_list, link) {
3189 input_get_display(struct input *input)
3191 return input->display;
3195 input_get_seat(struct input *input)
3201 input_get_modifiers(struct input *input)
3203 return input->modifiers;
3207 input_get_focus_widget(struct input *input)
3209 return input->focus_widget;
3213 struct wl_data_offer *offer;
3214 struct input *input;
3215 struct wl_array types;
3218 struct task io_task;
3226 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3228 struct data_offer *offer = data;
3231 p = wl_array_add(&offer->types, sizeof *p);
3235 static const struct wl_data_offer_listener data_offer_listener = {
3240 data_offer_destroy(struct data_offer *offer)
3245 if (offer->refcount == 0) {
3246 wl_data_offer_destroy(offer->offer);
3247 for (p = offer->types.data; *p; p++)
3249 wl_array_release(&offer->types);
3255 data_device_data_offer(void *data,
3256 struct wl_data_device *data_device,
3257 struct wl_data_offer *_offer)
3259 struct data_offer *offer;
3261 offer = xmalloc(sizeof *offer);
3263 wl_array_init(&offer->types);
3264 offer->refcount = 1;
3265 offer->input = data;
3266 offer->offer = _offer;
3267 wl_data_offer_add_listener(offer->offer,
3268 &data_offer_listener, offer);
3272 data_device_enter(void *data, struct wl_data_device *data_device,
3273 uint32_t serial, struct wl_surface *surface,
3274 wl_fixed_t x_w, wl_fixed_t y_w,
3275 struct wl_data_offer *offer)
3277 struct input *input = data;
3278 struct window *window;
3280 float x = wl_fixed_to_double(x_w);
3281 float y = wl_fixed_to_double(y_w);
3284 window = wl_surface_get_user_data(surface);
3285 input->drag_enter_serial = serial;
3286 input->drag_focus = window,
3290 if (!input->touch_grab)
3291 input->pointer_enter_serial = serial;
3294 input->drag_offer = wl_data_offer_get_user_data(offer);
3296 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3299 types_data = input->drag_offer->types.data;
3301 input->drag_offer = NULL;
3305 if (window->data_handler)
3306 window->data_handler(window, input, x, y, types_data,
3311 data_device_leave(void *data, struct wl_data_device *data_device)
3313 struct input *input = data;
3315 if (input->drag_offer) {
3316 data_offer_destroy(input->drag_offer);
3317 input->drag_offer = NULL;
3322 data_device_motion(void *data, struct wl_data_device *data_device,
3323 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3325 struct input *input = data;
3326 struct window *window = input->drag_focus;
3327 float x = wl_fixed_to_double(x_w);
3328 float y = wl_fixed_to_double(y_w);
3334 if (input->drag_offer)
3335 types_data = input->drag_offer->types.data;
3339 if (window->data_handler)
3340 window->data_handler(window, input, x, y, types_data,
3345 data_device_drop(void *data, struct wl_data_device *data_device)
3347 struct input *input = data;
3348 struct window *window = input->drag_focus;
3354 if (window->drop_handler)
3355 window->drop_handler(window, input,
3356 x, y, window->user_data);
3358 if (input->touch_grab)
3359 touch_ungrab(input);
3363 data_device_selection(void *data,
3364 struct wl_data_device *wl_data_device,
3365 struct wl_data_offer *offer)
3367 struct input *input = data;
3370 if (input->selection_offer)
3371 data_offer_destroy(input->selection_offer);
3374 input->selection_offer = wl_data_offer_get_user_data(offer);
3375 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3378 input->selection_offer = NULL;
3382 static const struct wl_data_device_listener data_device_listener = {
3383 data_device_data_offer,
3388 data_device_selection
3392 input_set_pointer_image_index(struct input *input, int index)
3394 struct wl_buffer *buffer;
3395 struct wl_cursor *cursor;
3396 struct wl_cursor_image *image;
3398 if (!input->pointer)
3401 cursor = input->display->cursors[input->current_cursor];
3405 if (index >= (int) cursor->image_count) {
3406 fprintf(stderr, "cursor index out of range\n");
3410 image = cursor->images[index];
3411 buffer = wl_cursor_image_get_buffer(image);
3415 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3416 wl_surface_damage(input->pointer_surface, 0, 0,
3417 image->width, image->height);
3418 wl_surface_commit(input->pointer_surface);
3419 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3420 input->pointer_surface,
3421 image->hotspot_x, image->hotspot_y);
3424 static const struct wl_callback_listener pointer_surface_listener;
3427 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3430 struct input *input = data;
3431 struct wl_cursor *cursor;
3435 assert(callback == input->cursor_frame_cb);
3436 wl_callback_destroy(callback);
3437 input->cursor_frame_cb = NULL;
3440 if (!input->pointer)
3443 if (input->current_cursor == CURSOR_BLANK) {
3444 wl_pointer_set_cursor(input->pointer,
3445 input->pointer_enter_serial,
3450 if (input->current_cursor == CURSOR_UNSET)
3452 cursor = input->display->cursors[input->current_cursor];
3456 /* FIXME We don't have the current time on the first call so we set
3457 * the animation start to the time of the first frame callback. */
3459 input->cursor_anim_start = 0;
3460 else if (input->cursor_anim_start == 0)
3461 input->cursor_anim_start = time;
3463 if (time == 0 || input->cursor_anim_start == 0)
3466 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3468 if (cursor->image_count > 1) {
3469 input->cursor_frame_cb =
3470 wl_surface_frame(input->pointer_surface);
3471 wl_callback_add_listener(input->cursor_frame_cb,
3472 &pointer_surface_listener, input);
3475 input_set_pointer_image_index(input, i);
3478 static const struct wl_callback_listener pointer_surface_listener = {
3479 pointer_surface_frame_callback
3483 input_set_pointer_image(struct input *input, int pointer)
3487 if (!input->pointer)
3490 if (input->pointer_enter_serial > input->cursor_serial)
3493 if (!force && pointer == input->current_cursor)
3496 input->current_cursor = pointer;
3497 input->cursor_serial = input->pointer_enter_serial;
3498 if (!input->cursor_frame_cb)
3499 pointer_surface_frame_callback(input, NULL, 0);
3501 /* The current frame callback may be stuck if, for instance,
3502 * the set cursor request was processed by the server after
3503 * this client lost the focus. In this case the cursor surface
3504 * might not be mapped and the frame callback wouldn't ever
3505 * complete. Send a set_cursor and attach to try to map the
3506 * cursor surface again so that the callback will finish */
3507 input_set_pointer_image_index(input, 0);
3511 struct wl_data_device *
3512 input_get_data_device(struct input *input)
3514 return input->data_device;
3518 input_set_selection(struct input *input,
3519 struct wl_data_source *source, uint32_t time)
3521 if (input->data_device)
3522 wl_data_device_set_selection(input->data_device, source, time);
3526 input_accept(struct input *input, const char *type)
3528 wl_data_offer_accept(input->drag_offer->offer,
3529 input->drag_enter_serial, type);
3533 offer_io_func(struct task *task, uint32_t events)
3535 struct data_offer *offer =
3536 container_of(task, struct data_offer, io_task);
3540 len = read(offer->fd, buffer, sizeof buffer);
3541 offer->func(buffer, len,
3542 offer->x, offer->y, offer->user_data);
3546 data_offer_destroy(offer);
3551 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3552 data_func_t func, void *user_data)
3556 if (pipe2(p, O_CLOEXEC) == -1)
3559 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3562 offer->io_task.run = offer_io_func;
3566 offer->user_data = user_data;
3568 display_watch_fd(offer->input->display,
3569 offer->fd, EPOLLIN, &offer->io_task);
3573 input_receive_drag_data(struct input *input, const char *mime_type,
3574 data_func_t func, void *data)
3576 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3577 input->drag_offer->x = input->drag_x;
3578 input->drag_offer->y = input->drag_y;
3582 input_receive_drag_data_to_fd(struct input *input,
3583 const char *mime_type, int fd)
3585 if (input->drag_offer)
3586 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3592 input_receive_selection_data(struct input *input, const char *mime_type,
3593 data_func_t func, void *data)
3597 if (input->selection_offer == NULL)
3600 for (p = input->selection_offer->types.data; *p; p++)
3601 if (strcmp(mime_type, *p) == 0)
3607 data_offer_receive_data(input->selection_offer,
3608 mime_type, func, data);
3613 input_receive_selection_data_to_fd(struct input *input,
3614 const char *mime_type, int fd)
3616 if (input->selection_offer)
3617 wl_data_offer_receive(input->selection_offer->offer,
3624 window_move(struct window *window, struct input *input, uint32_t serial)
3626 if (!window->xdg_surface)
3629 xdg_surface_move(window->xdg_surface, input->seat, serial);
3633 surface_set_synchronized(struct surface *surface)
3635 if (!surface->subsurface)
3638 if (surface->synchronized)
3641 wl_subsurface_set_sync(surface->subsurface);
3642 surface->synchronized = 1;
3646 surface_set_synchronized_default(struct surface *surface)
3648 if (!surface->subsurface)
3651 if (surface->synchronized == surface->synchronized_default)
3654 if (surface->synchronized_default)
3655 wl_subsurface_set_sync(surface->subsurface);
3657 wl_subsurface_set_desync(surface->subsurface);
3659 surface->synchronized = surface->synchronized_default;
3663 surface_resize(struct surface *surface)
3665 struct widget *widget = surface->widget;
3666 struct wl_compositor *compositor = widget->window->display->compositor;
3668 if (surface->input_region) {
3669 wl_region_destroy(surface->input_region);
3670 surface->input_region = NULL;
3673 if (surface->opaque_region)
3674 wl_region_destroy(surface->opaque_region);
3676 surface->opaque_region = wl_compositor_create_region(compositor);
3678 if (widget->resize_handler)
3679 widget->resize_handler(widget,
3680 widget->allocation.width,
3681 widget->allocation.height,
3684 if (surface->subsurface &&
3685 (surface->allocation.x != widget->allocation.x ||
3686 surface->allocation.y != widget->allocation.y)) {
3687 wl_subsurface_set_position(surface->subsurface,
3688 widget->allocation.x,
3689 widget->allocation.y);
3691 if (surface->allocation.width != widget->allocation.width ||
3692 surface->allocation.height != widget->allocation.height) {
3693 window_schedule_redraw(widget->window);
3695 surface->allocation = widget->allocation;
3698 wl_region_add(surface->opaque_region, 0, 0,
3699 widget->allocation.width,
3700 widget->allocation.height);
3704 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3707 * This hack should be removed, when EGL respects
3708 * eglSwapInterval(0).
3710 * If this window has sub-surfaces, especially a free-running
3711 * EGL-widget, we need to post the parent surface once with
3712 * all the old state to guarantee, that the EGL-widget will
3713 * receive its frame callback soon. Otherwise, a forced call
3714 * to eglSwapBuffers may end up blocking, waiting for a frame
3715 * event that will never come, because we will commit the parent
3716 * surface with all new state only after eglSwapBuffers returns.
3718 * This assumes, that:
3719 * 1. When the EGL widget's resize hook is called, it pauses.
3720 * 2. When the EGL widget's redraw hook is called, it forces a
3721 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3722 * In a single threaded application condition 1 is a no-op.
3724 * XXX: This should actually be after the surface_resize() calls,
3725 * but cannot, because then it would commit the incomplete state
3726 * accumulated from the widget resize hooks.
3728 if (window->subsurface_list.next != &window->main_surface->link ||
3729 window->subsurface_list.prev != &window->main_surface->link)
3730 wl_surface_commit(window->main_surface->surface);
3734 window_do_resize(struct window *window)
3736 struct surface *surface;
3738 widget_set_allocation(window->main_surface->widget,
3739 window->pending_allocation.x,
3740 window->pending_allocation.y,
3741 window->pending_allocation.width,
3742 window->pending_allocation.height);
3744 surface_resize(window->main_surface);
3746 /* The main surface is in the list, too. Main surface's
3747 * resize_handler is responsible for calling widget_set_allocation()
3748 * on all sub-surface root widgets, so they will be resized
3751 wl_list_for_each(surface, &window->subsurface_list, link) {
3752 if (surface == window->main_surface)
3755 surface_set_synchronized(surface);
3756 surface_resize(surface);
3759 if (!window->fullscreen && !window->maximized)
3760 window->saved_allocation = window->pending_allocation;
3764 idle_resize(struct window *window)
3766 window->resize_needed = 0;
3767 window->redraw_needed = 1;
3769 DBG("from %dx%d to %dx%d\n",
3770 window->main_surface->server_allocation.width,
3771 window->main_surface->server_allocation.height,
3772 window->pending_allocation.width,
3773 window->pending_allocation.height);
3775 hack_prevent_EGL_sub_surface_deadlock(window);
3777 window_do_resize(window);
3781 undo_resize(struct window *window)
3783 window->pending_allocation.width =
3784 window->main_surface->server_allocation.width;
3785 window->pending_allocation.height =
3786 window->main_surface->server_allocation.height;
3788 DBG("back to %dx%d\n",
3789 window->main_surface->server_allocation.width,
3790 window->main_surface->server_allocation.height);
3792 window_do_resize(window);
3794 if (window->pending_allocation.width == 0 &&
3795 window->pending_allocation.height == 0) {
3796 fprintf(stderr, "Error: Could not draw a surface, "
3797 "most likely due to insufficient disk space in "
3798 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3804 window_schedule_resize(struct window *window, int width, int height)
3806 /* We should probably get these numbers from the theme. */
3807 const int min_width = 200, min_height = 200;
3809 window->pending_allocation.x = 0;
3810 window->pending_allocation.y = 0;
3811 window->pending_allocation.width = width;
3812 window->pending_allocation.height = height;
3814 if (window->min_allocation.width == 0) {
3815 if (width < min_width && window->frame)
3816 window->min_allocation.width = min_width;
3818 window->min_allocation.width = width;
3819 if (height < min_height && window->frame)
3820 window->min_allocation.height = min_height;
3822 window->min_allocation.height = height;
3825 if (window->pending_allocation.width < window->min_allocation.width)
3826 window->pending_allocation.width = window->min_allocation.width;
3827 if (window->pending_allocation.height < window->min_allocation.height)
3828 window->pending_allocation.height = window->min_allocation.height;
3830 window->resize_needed = 1;
3831 window_schedule_redraw(window);
3835 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3837 window_schedule_resize(widget->window, width, height);
3841 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3842 int32_t width, int32_t height,
3843 struct wl_array *states, uint32_t serial)
3845 struct window *window = data;
3848 window->maximized = 0;
3849 window->fullscreen = 0;
3850 window->resizing = 0;
3851 window->focused = 0;
3853 wl_array_for_each(p, states) {
3854 uint32_t state = *p;
3856 case XDG_SURFACE_STATE_MAXIMIZED:
3857 window->maximized = 1;
3859 case XDG_SURFACE_STATE_FULLSCREEN:
3860 window->fullscreen = 1;
3862 case XDG_SURFACE_STATE_RESIZING:
3863 window->resizing = 1;
3865 case XDG_SURFACE_STATE_ACTIVATED:
3866 window->focused = 1;
3874 if (window->frame) {
3875 if (window->maximized) {
3876 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3878 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3881 if (window->focused) {
3882 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3884 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3888 if (width > 0 && height > 0) {
3889 window_schedule_resize(window, width, height);
3891 window_schedule_resize(window,
3892 window->saved_allocation.width,
3893 window->saved_allocation.height);
3896 window->next_attach_serial = serial;
3898 if (window->state_changed_handler)
3899 window->state_changed_handler(window, window->user_data);
3903 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3905 struct window *window = data;
3906 window_close(window);
3909 static const struct xdg_surface_listener xdg_surface_listener = {
3910 handle_surface_configure,
3911 handle_surface_delete,
3915 window_sync_parent(struct window *window)
3917 struct wl_surface *parent_surface;
3919 if (!window->xdg_surface)
3923 parent_surface = window->parent->main_surface->surface;
3925 parent_surface = NULL;
3927 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3931 window_sync_margin(struct window *window)
3935 if (!window->xdg_surface)
3941 margin = frame_get_shadow_margin(window->frame->frame);
3943 /* Shadow size is the same on every side. */
3944 xdg_surface_set_margin(window->xdg_surface,
3952 window_flush(struct window *window)
3954 struct surface *surface;
3956 if (!window->custom) {
3957 if (window->xdg_surface) {
3958 window_sync_parent(window);
3959 window_sync_margin(window);
3963 wl_list_for_each(surface, &window->subsurface_list, link) {
3964 if (surface == window->main_surface)
3967 surface_flush(surface);
3970 surface_flush(window->main_surface);
3974 menu_destroy(struct menu *menu)
3976 widget_destroy(menu->widget);
3977 window_destroy(menu->window);
3978 frame_destroy(menu->frame);
3983 window_get_allocation(struct window *window,
3984 struct rectangle *allocation)
3986 *allocation = window->main_surface->allocation;
3990 widget_redraw(struct widget *widget)
3992 struct widget *child;
3994 if (widget->redraw_handler)
3995 widget->redraw_handler(widget, widget->user_data);
3996 wl_list_for_each(child, &widget->child_list, link)
3997 widget_redraw(child);
4001 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4003 struct surface *surface = data;
4005 assert(callback == surface->frame_cb);
4006 DBG_OBJ(callback, "done\n");
4007 wl_callback_destroy(callback);
4008 surface->frame_cb = NULL;
4010 surface->last_time = time;
4012 if (surface->redraw_needed || surface->window->redraw_needed) {
4013 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4014 window_schedule_redraw_task(surface->window);
4018 static const struct wl_callback_listener listener = {
4023 surface_redraw(struct surface *surface)
4025 DBG_OBJ(surface->surface, "begin\n");
4027 if (!surface->window->redraw_needed && !surface->redraw_needed)
4030 /* Whole-window redraw forces a redraw even if the previous has
4031 * not yet hit the screen.
4033 if (surface->frame_cb) {
4034 if (!surface->window->redraw_needed)
4037 DBG_OBJ(surface->frame_cb, "cancelled\n");
4038 wl_callback_destroy(surface->frame_cb);
4041 if (surface->widget->use_cairo &&
4042 !widget_get_cairo_surface(surface->widget)) {
4043 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4047 surface->frame_cb = wl_surface_frame(surface->surface);
4048 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4049 DBG_OBJ(surface->frame_cb, "new\n");
4051 surface->redraw_needed = 0;
4052 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4053 widget_redraw(surface->widget);
4054 DBG_OBJ(surface->surface, "done\n");
4059 idle_redraw(struct task *task, uint32_t events)
4061 struct window *window = container_of(task, struct window, redraw_task);
4062 struct surface *surface;
4066 DBG(" --------- \n");
4068 wl_list_init(&window->redraw_task.link);
4069 window->redraw_task_scheduled = 0;
4071 if (window->resize_needed) {
4072 /* throttle resizing to the main surface display */
4073 if (window->main_surface->frame_cb) {
4074 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4078 idle_resize(window);
4082 if (surface_redraw(window->main_surface) < 0) {
4084 * Only main_surface failure will cause us to undo the resize.
4085 * If sub-surfaces fail, they will just be broken with old
4090 wl_list_for_each(surface, &window->subsurface_list, link) {
4091 if (surface == window->main_surface)
4094 surface_redraw(surface);
4098 window->redraw_needed = 0;
4099 window_flush(window);
4101 wl_list_for_each(surface, &window->subsurface_list, link)
4102 surface_set_synchronized_default(surface);
4104 if (resized && failed) {
4105 /* Restore widget tree to correspond to what is on screen. */
4106 undo_resize(window);
4111 window_schedule_redraw_task(struct window *window)
4113 if (!window->redraw_task_scheduled) {
4114 window->redraw_task.run = idle_redraw;
4115 display_defer(window->display, &window->redraw_task);
4116 window->redraw_task_scheduled = 1;
4121 window_schedule_redraw(struct window *window)
4123 struct surface *surface;
4125 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4127 wl_list_for_each(surface, &window->subsurface_list, link)
4128 surface->redraw_needed = 1;
4130 window_schedule_redraw_task(window);
4134 window_is_fullscreen(struct window *window)
4136 return window->fullscreen;
4140 window_set_fullscreen(struct window *window, int fullscreen)
4142 if (!window->xdg_surface)
4145 if (window->fullscreen == fullscreen)
4149 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4151 xdg_surface_unset_fullscreen(window->xdg_surface);
4155 window_is_maximized(struct window *window)
4157 return window->maximized;
4161 window_set_maximized(struct window *window, int maximized)
4163 if (!window->xdg_surface)
4166 if (window->maximized == maximized)
4170 xdg_surface_set_maximized(window->xdg_surface);
4172 xdg_surface_unset_maximized(window->xdg_surface);
4176 window_is_resizing(struct window *window)
4178 return window->resizing;
4182 window_set_minimized(struct window *window)
4184 if (!window->xdg_surface)
4187 xdg_surface_set_minimized(window->xdg_surface);
4191 window_set_user_data(struct window *window, void *data)
4193 window->user_data = data;
4197 window_get_user_data(struct window *window)
4199 return window->user_data;
4203 window_set_key_handler(struct window *window,
4204 window_key_handler_t handler)
4206 window->key_handler = handler;
4210 window_set_keyboard_focus_handler(struct window *window,
4211 window_keyboard_focus_handler_t handler)
4213 window->keyboard_focus_handler = handler;
4217 window_set_data_handler(struct window *window, window_data_handler_t handler)
4219 window->data_handler = handler;
4223 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4225 window->drop_handler = handler;
4229 window_set_close_handler(struct window *window,
4230 window_close_handler_t handler)
4232 window->close_handler = handler;
4236 window_set_fullscreen_handler(struct window *window,
4237 window_fullscreen_handler_t handler)
4239 window->fullscreen_handler = handler;
4243 window_set_output_handler(struct window *window,
4244 window_output_handler_t handler)
4246 window->output_handler = handler;
4250 window_set_state_changed_handler(struct window *window,
4251 window_state_changed_handler_t handler)
4253 window->state_changed_handler = handler;
4257 window_set_title(struct window *window, const char *title)
4259 free(window->title);
4260 window->title = strdup(title);
4261 if (window->frame) {
4262 frame_set_title(window->frame->frame, title);
4263 widget_schedule_redraw(window->frame->widget);
4265 if (window->xdg_surface)
4266 xdg_surface_set_title(window->xdg_surface, title);
4270 window_get_title(struct window *window)
4272 return window->title;
4276 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4278 struct text_cursor_position *text_cursor_position =
4279 window->display->text_cursor_position;
4281 if (!text_cursor_position)
4284 text_cursor_position_notify(text_cursor_position,
4285 window->main_surface->surface,
4286 wl_fixed_from_int(x),
4287 wl_fixed_from_int(y));
4291 window_damage(struct window *window, int32_t x, int32_t y,
4292 int32_t width, int32_t height)
4294 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4298 surface_enter(void *data,
4299 struct wl_surface *wl_surface, struct wl_output *wl_output)
4301 struct window *window = data;
4302 struct output *output;
4303 struct output *output_found = NULL;
4304 struct window_output *window_output;
4306 wl_list_for_each(output, &window->display->output_list, link) {
4307 if (output->output == wl_output) {
4308 output_found = output;
4316 window_output = xmalloc(sizeof *window_output);
4317 window_output->output = output_found;
4319 wl_list_insert (&window->window_output_list, &window_output->link);
4321 if (window->output_handler)
4322 window->output_handler(window, output_found, 1,
4327 surface_leave(void *data,
4328 struct wl_surface *wl_surface, struct wl_output *output)
4330 struct window *window = data;
4331 struct window_output *window_output;
4332 struct window_output *window_output_found = NULL;
4334 wl_list_for_each(window_output, &window->window_output_list, link) {
4335 if (window_output->output->output == output) {
4336 window_output_found = window_output;
4341 if (window_output_found) {
4342 wl_list_remove(&window_output_found->link);
4344 if (window->output_handler)
4345 window->output_handler(window, window_output->output,
4346 0, window->user_data);
4348 free(window_output_found);
4352 static const struct wl_surface_listener surface_listener = {
4357 static struct surface *
4358 surface_create(struct window *window)
4360 struct display *display = window->display;
4361 struct surface *surface;
4363 surface = xmalloc(sizeof *surface);
4364 memset(surface, 0, sizeof *surface);
4368 surface->window = window;
4369 surface->surface = wl_compositor_create_surface(display->compositor);
4370 surface->buffer_scale = 1;
4371 wl_surface_add_listener(surface->surface, &surface_listener, window);
4373 wl_list_insert(&window->subsurface_list, &surface->link);
4378 static enum window_buffer_type
4379 get_preferred_buffer_type(struct display *display)
4381 #ifdef HAVE_CAIRO_EGL
4382 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4383 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4386 return WINDOW_BUFFER_TYPE_SHM;
4389 static struct window *
4390 window_create_internal(struct display *display, int custom)
4392 struct window *window;
4393 struct surface *surface;
4395 window = xzalloc(sizeof *window);
4396 wl_list_init(&window->subsurface_list);
4397 window->display = display;
4399 surface = surface_create(window);
4400 window->main_surface = surface;
4402 assert(custom || display->xdg_shell);
4404 window->custom = custom;
4405 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4407 surface->buffer_type = get_preferred_buffer_type(display);
4409 wl_surface_set_user_data(surface->surface, window);
4410 wl_list_insert(display->window_list.prev, &window->link);
4411 wl_list_init(&window->redraw_task.link);
4413 wl_list_init (&window->window_output_list);
4419 window_create(struct display *display)
4421 struct window *window;
4423 window = window_create_internal(display, 0);
4425 window->xdg_surface =
4426 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4427 window->main_surface->surface);
4428 fail_on_null(window->xdg_surface);
4430 xdg_surface_set_user_data(window->xdg_surface, window);
4431 xdg_surface_add_listener(window->xdg_surface,
4432 &xdg_surface_listener, window);
4438 window_create_custom(struct display *display)
4440 return window_create_internal(display, 1);
4444 window_set_parent(struct window *window,
4445 struct window *parent_window)
4447 window->parent = parent_window;
4448 window_sync_parent(window);
4452 window_get_parent(struct window *window)
4454 return window->parent;
4458 menu_set_item(struct menu *menu, int sy)
4460 int32_t x, y, width, height;
4463 frame_interior(menu->frame, &x, &y, &width, &height);
4464 next = (sy - y) / 20;
4465 if (menu->current != next) {
4466 menu->current = next;
4467 widget_schedule_redraw(menu->widget);
4472 menu_motion_handler(struct widget *widget,
4473 struct input *input, uint32_t time,
4474 float x, float y, void *data)
4476 struct menu *menu = data;
4478 if (widget == menu->widget)
4479 menu_set_item(data, y);
4481 return CURSOR_LEFT_PTR;
4485 menu_enter_handler(struct widget *widget,
4486 struct input *input, float x, float y, void *data)
4488 struct menu *menu = data;
4490 if (widget == menu->widget)
4491 menu_set_item(data, y);
4493 return CURSOR_LEFT_PTR;
4497 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4499 struct menu *menu = data;
4501 if (widget == menu->widget)
4502 menu_set_item(data, -200);
4506 menu_button_handler(struct widget *widget,
4507 struct input *input, uint32_t time,
4508 uint32_t button, enum wl_pointer_button_state state,
4512 struct menu *menu = data;
4514 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4515 (menu->release_count > 0 || time - menu->time > 500)) {
4516 /* Either relase after press-drag-release or
4517 * click-motion-click. */
4518 menu->func(menu->parent, input,
4519 menu->current, menu->parent->user_data);
4520 input_ungrab(input);
4522 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4523 menu->release_count++;
4528 menu_touch_up_handler(struct widget *widget,
4529 struct input *input,
4535 struct menu *menu = data;
4537 input_ungrab(input);
4542 menu_redraw_handler(struct widget *widget, void *data)
4545 struct menu *menu = data;
4546 int32_t x, y, width, height, i;
4548 cr = widget_cairo_create(widget);
4550 frame_repaint(menu->frame, cr);
4551 frame_interior(menu->frame, &x, &y, &width, &height);
4553 theme_set_background_source(menu->window->display->theme,
4554 cr, THEME_FRAME_ACTIVE);
4555 cairo_rectangle(cr, x, y, width, height);
4558 cairo_select_font_face(cr, "sans",
4559 CAIRO_FONT_SLANT_NORMAL,
4560 CAIRO_FONT_WEIGHT_NORMAL);
4561 cairo_set_font_size(cr, 12);
4563 for (i = 0; i < menu->count; i++) {
4564 if (i == menu->current) {
4565 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4566 cairo_rectangle(cr, x, y + i * 20, width, 20);
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]);
4572 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4573 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4574 cairo_show_text(cr, menu->entries[i]);
4582 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4584 struct window *window = data;
4585 struct menu *menu = window->main_surface->widget->user_data;
4587 input_ungrab(menu->input);
4591 static const struct xdg_popup_listener xdg_popup_listener = {
4592 handle_popup_popup_done,
4596 window_show_menu(struct display *display,
4597 struct input *input, uint32_t time, struct window *parent,
4598 int32_t x, int32_t y,
4599 menu_func_t func, const char **entries, int count)
4601 struct window *window;
4605 menu = malloc(sizeof *menu);
4609 window = window_create_internal(parent->display, 0);
4615 menu->window = window;
4616 menu->parent = parent;
4617 menu->widget = window_add_widget(menu->window, menu);
4618 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4619 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4620 menu->frame = frame_create(window->display->theme, 0, 0,
4621 FRAME_BUTTON_NONE, NULL);
4622 fail_on_null(menu->frame);
4623 menu->entries = entries;
4624 menu->count = count;
4625 menu->release_count = 0;
4629 menu->input = input;
4633 input_ungrab(input);
4635 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4636 widget_set_enter_handler(menu->widget, menu_enter_handler);
4637 widget_set_leave_handler(menu->widget, menu_leave_handler);
4638 widget_set_motion_handler(menu->widget, menu_motion_handler);
4639 widget_set_button_handler(menu->widget, menu_button_handler);
4640 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4642 input_grab(input, menu->widget, 0);
4643 frame_resize_inside(menu->frame, 200, count * 20);
4644 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4645 window_schedule_resize(window, frame_width(menu->frame),
4646 frame_height(menu->frame));
4648 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4650 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4651 window->main_surface->surface,
4652 parent->main_surface->surface,
4654 display_get_serial(window->display),
4658 fail_on_null(window->xdg_popup);
4660 xdg_popup_set_user_data(window->xdg_popup, window);
4661 xdg_popup_add_listener(window->xdg_popup,
4662 &xdg_popup_listener, window);
4666 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4668 window->main_surface->buffer_type = type;
4671 enum window_buffer_type
4672 window_get_buffer_type(struct window *window)
4674 return window->main_surface->buffer_type;
4678 window_set_preferred_format(struct window *window,
4679 enum preferred_format format)
4681 window->preferred_format = format;
4685 window_add_subsurface(struct window *window, void *data,
4686 enum subsurface_mode default_mode)
4688 struct widget *widget;
4689 struct surface *surface;
4690 struct wl_surface *parent;
4691 struct wl_subcompositor *subcompo = window->display->subcompositor;
4693 surface = surface_create(window);
4694 surface->buffer_type = window_get_buffer_type(window);
4695 widget = widget_create(window, surface, data);
4696 wl_list_init(&widget->link);
4697 surface->widget = widget;
4699 parent = window->main_surface->surface;
4700 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4703 surface->synchronized = 1;
4705 switch (default_mode) {
4706 case SUBSURFACE_SYNCHRONIZED:
4707 surface->synchronized_default = 1;
4709 case SUBSURFACE_DESYNCHRONIZED:
4710 surface->synchronized_default = 0;
4713 assert(!"bad enum subsurface_mode");
4716 window->resize_needed = 1;
4717 window_schedule_redraw(window);
4723 display_handle_geometry(void *data,
4724 struct wl_output *wl_output,
4727 int physical_height,
4733 struct output *output = data;
4735 output->allocation.x = x;
4736 output->allocation.y = y;
4737 output->transform = transform;
4741 output->make = strdup(make);
4744 free(output->model);
4745 output->model = strdup(model);
4749 display_handle_done(void *data,
4750 struct wl_output *wl_output)
4755 display_handle_scale(void *data,
4756 struct wl_output *wl_output,
4759 struct output *output = data;
4761 output->scale = scale;
4765 display_handle_mode(void *data,
4766 struct wl_output *wl_output,
4772 struct output *output = data;
4773 struct display *display = output->display;
4775 if (flags & WL_OUTPUT_MODE_CURRENT) {
4776 output->allocation.width = width;
4777 output->allocation.height = height;
4778 if (display->output_configure_handler)
4779 (*display->output_configure_handler)(
4780 output, display->user_data);
4784 static const struct wl_output_listener output_listener = {
4785 display_handle_geometry,
4786 display_handle_mode,
4787 display_handle_done,
4788 display_handle_scale
4792 display_add_output(struct display *d, uint32_t id)
4794 struct output *output;
4796 output = xzalloc(sizeof *output);
4797 output->display = d;
4800 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4801 output->server_output_id = id;
4802 wl_list_insert(d->output_list.prev, &output->link);
4804 wl_output_add_listener(output->output, &output_listener, output);
4808 output_destroy(struct output *output)
4810 if (output->destroy_handler)
4811 (*output->destroy_handler)(output, output->user_data);
4813 wl_output_destroy(output->output);
4814 wl_list_remove(&output->link);
4819 display_destroy_output(struct display *d, uint32_t id)
4821 struct output *output;
4823 wl_list_for_each(output, &d->output_list, link) {
4824 if (output->server_output_id == id) {
4825 output_destroy(output);
4832 display_set_global_handler(struct display *display,
4833 display_global_handler_t handler)
4835 struct global *global;
4837 display->global_handler = handler;
4841 wl_list_for_each(global, &display->global_list, link)
4842 display->global_handler(display,
4843 global->name, global->interface,
4844 global->version, display->user_data);
4848 display_set_global_handler_remove(struct display *display,
4849 display_global_handler_t remove_handler)
4851 display->global_handler_remove = remove_handler;
4852 if (!remove_handler)
4857 display_set_output_configure_handler(struct display *display,
4858 display_output_handler_t handler)
4860 struct output *output;
4862 display->output_configure_handler = handler;
4866 wl_list_for_each(output, &display->output_list, link) {
4867 if (output->allocation.width == 0 &&
4868 output->allocation.height == 0)
4871 (*display->output_configure_handler)(output,
4872 display->user_data);
4877 output_set_user_data(struct output *output, void *data)
4879 output->user_data = data;
4883 output_get_user_data(struct output *output)
4885 return output->user_data;
4889 output_set_destroy_handler(struct output *output,
4890 display_output_handler_t handler)
4892 output->destroy_handler = handler;
4893 /* FIXME: implement this, once we have way to remove outputs */
4897 output_get_allocation(struct output *output, struct rectangle *base)
4899 struct rectangle allocation = output->allocation;
4901 switch (output->transform) {
4902 case WL_OUTPUT_TRANSFORM_90:
4903 case WL_OUTPUT_TRANSFORM_270:
4904 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4905 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4906 /* Swap width and height */
4907 allocation.width = output->allocation.height;
4908 allocation.height = output->allocation.width;
4916 output_get_wl_output(struct output *output)
4918 return output->output;
4921 enum wl_output_transform
4922 output_get_transform(struct output *output)
4924 return output->transform;
4928 output_get_scale(struct output *output)
4930 return output->scale;
4934 output_get_make(struct output *output)
4936 return output->make;
4940 output_get_model(struct output *output)
4942 return output->model;
4946 fini_xkb(struct input *input)
4948 xkb_state_unref(input->xkb.state);
4949 xkb_map_unref(input->xkb.keymap);
4952 #define MIN(a,b) ((a) < (b) ? a : b)
4955 display_add_input(struct display *d, uint32_t id)
4957 struct input *input;
4959 input = xzalloc(sizeof *input);
4961 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4962 MIN(d->seat_version, 3));
4963 input->touch_focus = NULL;
4964 input->pointer_focus = NULL;
4965 input->keyboard_focus = NULL;
4966 wl_list_init(&input->touch_point_list);
4967 wl_list_insert(d->input_list.prev, &input->link);
4969 wl_seat_add_listener(input->seat, &seat_listener, input);
4970 wl_seat_set_user_data(input->seat, input);
4972 if (d->data_device_manager) {
4973 input->data_device =
4974 wl_data_device_manager_get_data_device(d->data_device_manager,
4976 wl_data_device_add_listener(input->data_device,
4977 &data_device_listener,
4981 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4983 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4984 TFD_CLOEXEC | TFD_NONBLOCK);
4985 input->repeat_task.run = keyboard_repeat_func;
4986 display_watch_fd(d, input->repeat_timer_fd,
4987 EPOLLIN, &input->repeat_task);
4991 input_destroy(struct input *input)
4993 input_remove_keyboard_focus(input);
4994 input_remove_pointer_focus(input);
4996 if (input->drag_offer)
4997 data_offer_destroy(input->drag_offer);
4999 if (input->selection_offer)
5000 data_offer_destroy(input->selection_offer);
5002 if (input->data_device)
5003 wl_data_device_destroy(input->data_device);
5005 if (input->display->seat_version >= 3) {
5007 wl_pointer_release(input->pointer);
5008 if (input->keyboard)
5009 wl_keyboard_release(input->keyboard);
5014 wl_surface_destroy(input->pointer_surface);
5016 wl_list_remove(&input->link);
5017 wl_seat_destroy(input->seat);
5018 close(input->repeat_timer_fd);
5023 init_workspace_manager(struct display *d, uint32_t id)
5025 d->workspace_manager =
5026 wl_registry_bind(d->registry, id,
5027 &workspace_manager_interface, 1);
5028 if (d->workspace_manager != NULL)
5029 workspace_manager_add_listener(d->workspace_manager,
5030 &workspace_manager_listener,
5035 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5037 struct display *d = data;
5039 if (format == WL_SHM_FORMAT_RGB565)
5043 struct wl_shm_listener shm_listener = {
5048 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5050 xdg_shell_pong(shell, serial);
5053 static const struct xdg_shell_listener xdg_shell_listener = {
5057 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
5058 #ifdef static_assert
5059 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5060 "Interface version doesn't match implementation version");
5064 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5065 const char *interface, uint32_t version)
5067 struct display *d = data;
5068 struct global *global;
5070 global = xmalloc(sizeof *global);
5072 global->interface = strdup(interface);
5073 global->version = version;
5074 wl_list_insert(d->global_list.prev, &global->link);
5076 if (strcmp(interface, "wl_compositor") == 0) {
5077 d->compositor = wl_registry_bind(registry, id,
5078 &wl_compositor_interface, 3);
5079 } else if (strcmp(interface, "wl_output") == 0) {
5080 display_add_output(d, id);
5081 } else if (strcmp(interface, "wl_seat") == 0) {
5082 d->seat_version = version;
5083 display_add_input(d, id);
5084 } else if (strcmp(interface, "wl_shm") == 0) {
5085 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5086 wl_shm_add_listener(d->shm, &shm_listener, d);
5087 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5088 d->data_device_manager =
5089 wl_registry_bind(registry, id,
5090 &wl_data_device_manager_interface, 1);
5091 } else if (strcmp(interface, "xdg_shell") == 0) {
5092 d->xdg_shell = wl_registry_bind(registry, id,
5093 &xdg_shell_interface, 1);
5094 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5095 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5096 } else if (strcmp(interface, "text_cursor_position") == 0) {
5097 d->text_cursor_position =
5098 wl_registry_bind(registry, id,
5099 &text_cursor_position_interface, 1);
5100 } else if (strcmp(interface, "workspace_manager") == 0) {
5101 init_workspace_manager(d, id);
5102 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5104 wl_registry_bind(registry, id,
5105 &wl_subcompositor_interface, 1);
5108 if (d->global_handler)
5109 d->global_handler(d, id, interface, version, d->user_data);
5113 registry_handle_global_remove(void *data, struct wl_registry *registry,
5116 struct display *d = data;
5117 struct global *global;
5120 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5121 if (global->name != name)
5124 if (strcmp(global->interface, "wl_output") == 0)
5125 display_destroy_output(d, name);
5127 /* XXX: Should destroy remaining bound globals */
5129 if (d->global_handler_remove)
5130 d->global_handler_remove(d, name, global->interface,
5131 global->version, d->user_data);
5133 wl_list_remove(&global->link);
5134 free(global->interface);
5140 display_bind(struct display *display, uint32_t name,
5141 const struct wl_interface *interface, uint32_t version)
5143 return wl_registry_bind(display->registry, name, interface, version);
5146 static const struct wl_registry_listener registry_listener = {
5147 registry_handle_global,
5148 registry_handle_global_remove
5151 #ifdef HAVE_CAIRO_EGL
5153 init_egl(struct display *d)
5155 EGLint major, minor;
5158 #ifdef USE_CAIRO_GLESV2
5159 # define GL_BIT EGL_OPENGL_ES2_BIT
5161 # define GL_BIT EGL_OPENGL_BIT
5164 static const EGLint argb_cfg_attribs[] = {
5165 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5171 EGL_RENDERABLE_TYPE, GL_BIT,
5175 #ifdef USE_CAIRO_GLESV2
5176 static const EGLint context_attribs[] = {
5177 EGL_CONTEXT_CLIENT_VERSION, 2,
5180 EGLint api = EGL_OPENGL_ES_API;
5182 EGLint *context_attribs = NULL;
5183 EGLint api = EGL_OPENGL_API;
5186 d->dpy = eglGetDisplay(d->display);
5187 if (!eglInitialize(d->dpy, &major, &minor)) {
5188 fprintf(stderr, "failed to initialize EGL\n");
5192 if (!eglBindAPI(api)) {
5193 fprintf(stderr, "failed to bind EGL client API\n");
5197 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5198 &d->argb_config, 1, &n) || n != 1) {
5199 fprintf(stderr, "failed to choose argb EGL config\n");
5203 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5204 EGL_NO_CONTEXT, context_attribs);
5205 if (d->argb_ctx == NULL) {
5206 fprintf(stderr, "failed to create EGL context\n");
5210 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5211 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5212 fprintf(stderr, "failed to get cairo EGL argb device\n");
5220 fini_egl(struct display *display)
5222 cairo_device_destroy(display->argb_device);
5224 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5227 eglTerminate(display->dpy);
5233 init_dummy_surface(struct display *display)
5238 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5240 display->dummy_surface =
5241 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5243 display->dummy_surface_data = data;
5247 handle_display_data(struct task *task, uint32_t events)
5249 struct display *display =
5250 container_of(task, struct display, display_task);
5251 struct epoll_event ep;
5254 display->display_fd_events = events;
5256 if (events & EPOLLERR || events & EPOLLHUP) {
5257 display_exit(display);
5261 if (events & EPOLLIN) {
5262 ret = wl_display_dispatch(display->display);
5264 display_exit(display);
5269 if (events & EPOLLOUT) {
5270 ret = wl_display_flush(display->display);
5272 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5273 ep.data.ptr = &display->display_task;
5274 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5275 display->display_fd, &ep);
5276 } else if (ret == -1 && errno != EAGAIN) {
5277 display_exit(display);
5284 log_handler(const char *format, va_list args)
5286 vfprintf(stderr, format, args);
5290 display_create(int *argc, char *argv[])
5294 wl_log_set_handler_client(log_handler);
5296 d = zalloc(sizeof *d);
5300 d->display = wl_display_connect(NULL);
5301 if (d->display == NULL) {
5302 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5307 d->xkb_context = xkb_context_new(0);
5308 if (d->xkb_context == NULL) {
5309 fprintf(stderr, "Failed to create XKB context\n");
5314 d->epoll_fd = os_epoll_create_cloexec();
5315 d->display_fd = wl_display_get_fd(d->display);
5316 d->display_task.run = handle_display_data;
5317 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5320 wl_list_init(&d->deferred_list);
5321 wl_list_init(&d->input_list);
5322 wl_list_init(&d->output_list);
5323 wl_list_init(&d->global_list);
5326 d->workspace_count = 1;
5328 d->registry = wl_display_get_registry(d->display);
5329 wl_registry_add_listener(d->registry, ®istry_listener, d);
5331 if (wl_display_dispatch(d->display) < 0) {
5332 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5336 #ifdef HAVE_CAIRO_EGL
5337 if (init_egl(d) < 0)
5338 fprintf(stderr, "EGL does not seem to work, "
5339 "falling back to software rendering and wl_shm.\n");
5344 d->theme = theme_create();
5346 wl_list_init(&d->window_list);
5348 init_dummy_surface(d);
5354 display_destroy_outputs(struct display *display)
5357 struct output *output;
5359 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5360 output_destroy(output);
5364 display_destroy_inputs(struct display *display)
5367 struct input *input;
5369 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5370 input_destroy(input);
5374 display_destroy(struct display *display)
5376 if (!wl_list_empty(&display->window_list))
5377 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5378 wl_list_length(&display->window_list));
5380 if (!wl_list_empty(&display->deferred_list))
5381 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5383 cairo_surface_destroy(display->dummy_surface);
5384 free(display->dummy_surface_data);
5386 display_destroy_outputs(display);
5387 display_destroy_inputs(display);
5389 xkb_context_unref(display->xkb_context);
5391 theme_destroy(display->theme);
5392 destroy_cursors(display);
5394 #ifdef HAVE_CAIRO_EGL
5395 if (display->argb_device)
5399 if (display->subcompositor)
5400 wl_subcompositor_destroy(display->subcompositor);
5402 if (display->xdg_shell)
5403 xdg_shell_destroy(display->xdg_shell);
5406 wl_shm_destroy(display->shm);
5408 if (display->data_device_manager)
5409 wl_data_device_manager_destroy(display->data_device_manager);
5411 wl_compositor_destroy(display->compositor);
5412 wl_registry_destroy(display->registry);
5414 close(display->epoll_fd);
5416 if (!(display->display_fd_events & EPOLLERR) &&
5417 !(display->display_fd_events & EPOLLHUP))
5418 wl_display_flush(display->display);
5420 wl_display_disconnect(display->display);
5425 display_set_user_data(struct display *display, void *data)
5427 display->user_data = data;
5431 display_get_user_data(struct display *display)
5433 return display->user_data;
5437 display_get_display(struct display *display)
5439 return display->display;
5443 display_has_subcompositor(struct display *display)
5445 if (display->subcompositor)
5448 wl_display_roundtrip(display->display);
5450 return display->subcompositor != NULL;
5454 display_get_cairo_device(struct display *display)
5456 return display->argb_device;
5460 display_get_output(struct display *display)
5462 return container_of(display->output_list.next, struct output, link);
5465 struct wl_compositor *
5466 display_get_compositor(struct display *display)
5468 return display->compositor;
5472 display_get_serial(struct display *display)
5474 return display->serial;
5478 display_get_egl_display(struct display *d)
5483 struct wl_data_source *
5484 display_create_data_source(struct display *display)
5486 if (display->data_device_manager)
5487 return wl_data_device_manager_create_data_source(display->data_device_manager);
5493 display_get_argb_egl_config(struct display *d)
5495 return d->argb_config;
5499 display_acquire_window_surface(struct display *display,
5500 struct window *window,
5503 struct surface *surface = window->main_surface;
5505 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5508 widget_get_cairo_surface(window->main_surface->widget);
5509 return surface->toysurface->acquire(surface->toysurface, ctx);
5513 display_release_window_surface(struct display *display,
5514 struct window *window)
5516 struct surface *surface = window->main_surface;
5518 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5521 surface->toysurface->release(surface->toysurface);
5525 display_defer(struct display *display, struct task *task)
5527 wl_list_insert(&display->deferred_list, &task->link);
5531 display_watch_fd(struct display *display,
5532 int fd, uint32_t events, struct task *task)
5534 struct epoll_event ep;
5538 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5542 display_unwatch_fd(struct display *display, int fd)
5544 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5548 display_run(struct display *display)
5551 struct epoll_event ep[16];
5554 display->running = 1;
5556 while (!wl_list_empty(&display->deferred_list)) {
5557 task = container_of(display->deferred_list.prev,
5559 wl_list_remove(&task->link);
5563 wl_display_dispatch_pending(display->display);
5565 if (!display->running)
5568 ret = wl_display_flush(display->display);
5569 if (ret < 0 && errno == EAGAIN) {
5571 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5572 ep[0].data.ptr = &display->display_task;
5574 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5575 display->display_fd, &ep[0]);
5576 } else if (ret < 0) {
5580 count = epoll_wait(display->epoll_fd,
5581 ep, ARRAY_LENGTH(ep), -1);
5582 for (i = 0; i < count; i++) {
5583 task = ep[i].data.ptr;
5584 task->run(task, ep[i].events);
5590 display_exit(struct display *display)
5592 display->running = 0;
5596 keysym_modifiers_add(struct wl_array *modifiers_map,
5599 size_t len = strlen(name) + 1;
5602 p = wl_array_add(modifiers_map, len);
5607 strncpy(p, name, len);
5610 static xkb_mod_index_t
5611 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5614 xkb_mod_index_t index = 0;
5615 char *p = modifiers_map->data;
5617 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5618 if (strcmp(p, name) == 0)
5625 return XKB_MOD_INVALID;
5629 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5632 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5634 if (index == XKB_MOD_INVALID)
5635 return XKB_MOD_INVALID;
5641 fail_on_null(void *p)
5644 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5654 return fail_on_null(malloc(s));
5660 return fail_on_null(zalloc(s));
5664 xstrdup(const char *s)
5666 return fail_on_null(strdup(s));
5670 xrealloc(char *p, size_t s)
5672 return fail_on_null(realloc(p, s));