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;
223 int redraw_task_scheduled;
224 struct task redraw_task;
234 enum preferred_format preferred_format;
236 window_key_handler_t key_handler;
237 window_keyboard_focus_handler_t keyboard_focus_handler;
238 window_data_handler_t data_handler;
239 window_drop_handler_t drop_handler;
240 window_close_handler_t close_handler;
241 window_fullscreen_handler_t fullscreen_handler;
242 window_output_handler_t output_handler;
244 struct surface *main_surface;
245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
248 struct window *transient_for;
250 struct window_frame *frame;
252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
260 struct window *window;
261 struct surface *surface;
262 struct tooltip *tooltip;
263 struct wl_list child_list;
265 struct rectangle allocation;
266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
270 widget_motion_handler_t motion_handler;
271 widget_button_handler_t button_handler;
272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
277 widget_axis_handler_t axis_handler;
282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
337 struct task repeat_task;
341 uint32_t repeat_time;
345 struct display *display;
346 struct wl_output *output;
347 uint32_t server_output_id;
348 struct rectangle allocation;
353 display_output_handler_t destroy_handler;
357 struct window_frame {
358 struct widget *widget;
359 struct widget *child;
364 struct window *window;
365 struct window *parent;
366 struct widget *widget;
369 const char **entries;
378 struct widget *parent;
379 struct widget *widget;
381 struct task tooltip_task;
387 struct wl_shm_pool *pool;
394 CURSOR_DEFAULT = 100,
398 static const cairo_user_data_key_t shm_surface_data_key;
405 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
406 __attribute__ ((format (printf, 4, 5)));
409 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
414 gettimeofday(&tv, NULL);
415 fprintf(stderr, "%8ld.%03ld ",
416 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
419 fprintf(stderr, "%s@%d ",
420 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
422 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
423 fprintf(stderr, "%s ", func);
426 vfprintf(stderr, fmt, ap);
430 #define DBG(fmt, ...) \
431 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
433 #define DBG_OBJ(obj, fmt, ...) \
434 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
438 #define DBG(...) do {} while (0)
439 #define DBG_OBJ(...) do {} while (0)
444 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
448 switch (buffer_transform) {
449 case WL_OUTPUT_TRANSFORM_90:
450 case WL_OUTPUT_TRANSFORM_270:
451 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
452 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
461 *width *= buffer_scale;
462 *height *= buffer_scale;
466 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
470 switch (buffer_transform) {
471 case WL_OUTPUT_TRANSFORM_90:
472 case WL_OUTPUT_TRANSFORM_270:
473 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
483 *width /= buffer_scale;
484 *height /= buffer_scale;
487 #ifdef HAVE_CAIRO_EGL
489 struct egl_window_surface {
490 struct toysurface base;
491 cairo_surface_t *cairo_surface;
492 struct display *display;
493 struct wl_surface *surface;
494 struct wl_egl_window *egl_window;
495 EGLSurface egl_surface;
498 static struct egl_window_surface *
499 to_egl_window_surface(struct toysurface *base)
501 return container_of(base, struct egl_window_surface, base);
504 static cairo_surface_t *
505 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
506 int32_t width, int32_t height, uint32_t flags,
507 enum wl_output_transform buffer_transform, int32_t buffer_scale)
509 struct egl_window_surface *surface = to_egl_window_surface(base);
511 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
513 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
514 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
516 return cairo_surface_reference(surface->cairo_surface);
520 egl_window_surface_swap(struct toysurface *base,
521 enum wl_output_transform buffer_transform, int32_t buffer_scale,
522 struct rectangle *server_allocation)
524 struct egl_window_surface *surface = to_egl_window_surface(base);
526 cairo_gl_surface_swapbuffers(surface->cairo_surface);
527 wl_egl_window_get_attached_size(surface->egl_window,
528 &server_allocation->width,
529 &server_allocation->height);
531 buffer_to_surface_size (buffer_transform, buffer_scale,
532 &server_allocation->width,
533 &server_allocation->height);
537 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
539 struct egl_window_surface *surface = to_egl_window_surface(base);
540 cairo_device_t *device;
542 device = cairo_surface_get_device(surface->cairo_surface);
547 if (device == surface->display->argb_device)
548 ctx = surface->display->argb_ctx;
553 cairo_device_flush(device);
554 cairo_device_acquire(device);
555 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
556 surface->egl_surface, ctx))
557 fprintf(stderr, "failed to make surface current\n");
563 egl_window_surface_release(struct toysurface *base)
565 struct egl_window_surface *surface = to_egl_window_surface(base);
566 cairo_device_t *device;
568 device = cairo_surface_get_device(surface->cairo_surface);
572 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
573 surface->display->argb_ctx))
574 fprintf(stderr, "failed to make context current\n");
576 cairo_device_release(device);
580 egl_window_surface_destroy(struct toysurface *base)
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583 struct display *d = surface->display;
585 cairo_surface_destroy(surface->cairo_surface);
586 eglDestroySurface(d->dpy, surface->egl_surface);
587 wl_egl_window_destroy(surface->egl_window);
588 surface->surface = NULL;
593 static struct toysurface *
594 egl_window_surface_create(struct display *display,
595 struct wl_surface *wl_surface,
597 struct rectangle *rectangle)
599 struct egl_window_surface *surface;
601 if (display->dpy == EGL_NO_DISPLAY)
604 surface = calloc(1, sizeof *surface);
608 surface->base.prepare = egl_window_surface_prepare;
609 surface->base.swap = egl_window_surface_swap;
610 surface->base.acquire = egl_window_surface_acquire;
611 surface->base.release = egl_window_surface_release;
612 surface->base.destroy = egl_window_surface_destroy;
614 surface->display = display;
615 surface->surface = wl_surface;
617 surface->egl_window = wl_egl_window_create(surface->surface,
621 surface->egl_surface = eglCreateWindowSurface(display->dpy,
622 display->argb_config,
626 surface->cairo_surface =
627 cairo_gl_surface_create_for_egl(display->argb_device,
628 surface->egl_surface,
632 return &surface->base;
637 static struct toysurface *
638 egl_window_surface_create(struct display *display,
639 struct wl_surface *wl_surface,
641 struct rectangle *rectangle)
648 struct shm_surface_data {
649 struct wl_buffer *buffer;
650 struct shm_pool *pool;
654 display_get_buffer_for_surface(struct display *display,
655 cairo_surface_t *surface)
657 struct shm_surface_data *data;
659 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
665 shm_pool_destroy(struct shm_pool *pool);
668 shm_surface_data_destroy(void *p)
670 struct shm_surface_data *data = p;
672 wl_buffer_destroy(data->buffer);
674 shm_pool_destroy(data->pool);
679 static struct wl_shm_pool *
680 make_shm_pool(struct display *display, int size, void **data)
682 struct wl_shm_pool *pool;
685 fd = os_create_anonymous_file(size);
687 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
692 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
693 if (*data == MAP_FAILED) {
694 fprintf(stderr, "mmap failed: %m\n");
699 pool = wl_shm_create_pool(display->shm, fd, size);
706 static struct shm_pool *
707 shm_pool_create(struct display *display, size_t size)
709 struct shm_pool *pool = malloc(sizeof *pool);
714 pool->pool = make_shm_pool(display, size, &pool->data);
727 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
729 if (pool->used + size > pool->size)
732 *offset = pool->used;
735 return (char *) pool->data + *offset;
738 /* destroy the pool. this does not unmap the memory though */
740 shm_pool_destroy(struct shm_pool *pool)
742 munmap(pool->data, pool->size);
743 wl_shm_pool_destroy(pool->pool);
747 /* Start allocating from the beginning of the pool again */
749 shm_pool_reset(struct shm_pool *pool)
755 data_length_for_shm_surface(struct rectangle *rect)
759 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
761 return stride * rect->height;
764 static cairo_surface_t *
765 display_create_shm_surface_from_pool(struct display *display,
766 struct rectangle *rectangle,
767 uint32_t flags, struct shm_pool *pool)
769 struct shm_surface_data *data;
771 cairo_surface_t *surface;
772 cairo_format_t cairo_format;
773 int stride, length, offset;
776 data = malloc(sizeof *data);
780 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
781 cairo_format = CAIRO_FORMAT_RGB16_565;
783 cairo_format = CAIRO_FORMAT_ARGB32;
785 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
786 length = stride * rectangle->height;
788 map = shm_pool_allocate(pool, length, &offset);
795 surface = cairo_image_surface_create_for_data (map,
801 cairo_surface_set_user_data(surface, &shm_surface_data_key,
802 data, shm_surface_data_destroy);
804 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
805 format = WL_SHM_FORMAT_RGB565;
807 if (flags & SURFACE_OPAQUE)
808 format = WL_SHM_FORMAT_XRGB8888;
810 format = WL_SHM_FORMAT_ARGB8888;
813 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
821 static cairo_surface_t *
822 display_create_shm_surface(struct display *display,
823 struct rectangle *rectangle, uint32_t flags,
824 struct shm_pool *alternate_pool,
825 struct shm_surface_data **data_ret)
827 struct shm_surface_data *data;
828 struct shm_pool *pool;
829 cairo_surface_t *surface;
831 if (alternate_pool) {
832 shm_pool_reset(alternate_pool);
833 surface = display_create_shm_surface_from_pool(display,
838 data = cairo_surface_get_user_data(surface,
839 &shm_surface_data_key);
844 pool = shm_pool_create(display,
845 data_length_for_shm_surface(rectangle));
850 display_create_shm_surface_from_pool(display, rectangle,
854 shm_pool_destroy(pool);
858 /* make sure we destroy the pool when the surface is destroyed */
859 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
870 check_size(struct rectangle *rect)
872 if (rect->width && rect->height)
875 fprintf(stderr, "tried to create surface of "
876 "width: %d, height: %d\n", rect->width, rect->height);
881 display_create_surface(struct display *display,
882 struct wl_surface *surface,
883 struct rectangle *rectangle,
886 if (check_size(rectangle) < 0)
889 assert(flags & SURFACE_SHM);
890 return display_create_shm_surface(display, rectangle, flags,
894 struct shm_surface_leaf {
895 cairo_surface_t *cairo_surface;
896 /* 'data' is automatically destroyed, when 'cairo_surface' is */
897 struct shm_surface_data *data;
899 struct shm_pool *resize_pool;
904 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
906 if (leaf->cairo_surface)
907 cairo_surface_destroy(leaf->cairo_surface);
908 /* leaf->data already destroyed via cairo private */
910 if (leaf->resize_pool)
911 shm_pool_destroy(leaf->resize_pool);
913 memset(leaf, 0, sizeof *leaf);
919 struct toysurface base;
920 struct display *display;
921 struct wl_surface *surface;
925 struct shm_surface_leaf leaf[MAX_LEAVES];
926 struct shm_surface_leaf *current;
929 static struct shm_surface *
930 to_shm_surface(struct toysurface *base)
932 return container_of(base, struct shm_surface, base);
936 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
939 struct shm_surface_leaf *leaf;
940 char bufs[MAX_LEAVES + 1];
943 for (i = 0; i < MAX_LEAVES; i++) {
944 leaf = &surface->leaf[i];
948 else if (leaf->cairo_surface)
954 bufs[MAX_LEAVES] = '\0';
955 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
960 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
962 struct shm_surface *surface = data;
963 struct shm_surface_leaf *leaf;
967 shm_surface_buffer_state_debug(surface, "buffer_release before");
969 for (i = 0; i < MAX_LEAVES; i++) {
970 leaf = &surface->leaf[i];
971 if (leaf->data && leaf->data->buffer == buffer) {
976 assert(i < MAX_LEAVES && "unknown buffer released");
978 /* Leave one free leaf with storage, release others */
980 for (i = 0; i < MAX_LEAVES; i++) {
981 leaf = &surface->leaf[i];
983 if (!leaf->cairo_surface || leaf->busy)
989 shm_surface_leaf_release(leaf);
992 shm_surface_buffer_state_debug(surface, "buffer_release after");
995 static const struct wl_buffer_listener shm_surface_buffer_listener = {
996 shm_surface_buffer_release
999 static cairo_surface_t *
1000 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1001 int32_t width, int32_t height, uint32_t flags,
1002 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1004 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1005 struct shm_surface *surface = to_shm_surface(base);
1006 struct rectangle rect = { 0};
1007 struct shm_surface_leaf *leaf = NULL;
1013 /* pick a free buffer, preferrably one that already has storage */
1014 for (i = 0; i < MAX_LEAVES; i++) {
1015 if (surface->leaf[i].busy)
1018 if (!leaf || surface->leaf[i].cairo_surface)
1019 leaf = &surface->leaf[i];
1021 DBG_OBJ(surface->surface, "pick leaf %d\n",
1022 (int)(leaf - &surface->leaf[0]));
1025 fprintf(stderr, "%s: all buffers are held by the server.\n",
1031 if (!resize_hint && leaf->resize_pool) {
1032 cairo_surface_destroy(leaf->cairo_surface);
1033 leaf->cairo_surface = NULL;
1034 shm_pool_destroy(leaf->resize_pool);
1035 leaf->resize_pool = NULL;
1038 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1040 if (leaf->cairo_surface &&
1041 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1042 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1045 if (leaf->cairo_surface)
1046 cairo_surface_destroy(leaf->cairo_surface);
1048 #ifdef USE_RESIZE_POOL
1049 if (resize_hint && !leaf->resize_pool) {
1050 /* Create a big pool to allocate from, while continuously
1051 * resizing. Mmapping a new pool in the server
1052 * is relatively expensive, so reusing a pool performs
1053 * better, but may temporarily reserve unneeded memory.
1055 /* We should probably base this number on the output size. */
1056 leaf->resize_pool = shm_pool_create(surface->display,
1062 rect.height = height;
1064 leaf->cairo_surface =
1065 display_create_shm_surface(surface->display, &rect,
1069 if (!leaf->cairo_surface)
1072 wl_buffer_add_listener(leaf->data->buffer,
1073 &shm_surface_buffer_listener, surface);
1076 surface->current = leaf;
1078 return cairo_surface_reference(leaf->cairo_surface);
1082 shm_surface_swap(struct toysurface *base,
1083 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1084 struct rectangle *server_allocation)
1086 struct shm_surface *surface = to_shm_surface(base);
1087 struct shm_surface_leaf *leaf = surface->current;
1089 server_allocation->width =
1090 cairo_image_surface_get_width(leaf->cairo_surface);
1091 server_allocation->height =
1092 cairo_image_surface_get_height(leaf->cairo_surface);
1094 buffer_to_surface_size (buffer_transform, buffer_scale,
1095 &server_allocation->width,
1096 &server_allocation->height);
1098 wl_surface_attach(surface->surface, leaf->data->buffer,
1099 surface->dx, surface->dy);
1100 wl_surface_damage(surface->surface, 0, 0,
1101 server_allocation->width, server_allocation->height);
1102 wl_surface_commit(surface->surface);
1104 DBG_OBJ(surface->surface, "leaf %d busy\n",
1105 (int)(leaf - &surface->leaf[0]));
1108 surface->current = NULL;
1112 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1118 shm_surface_release(struct toysurface *base)
1123 shm_surface_destroy(struct toysurface *base)
1125 struct shm_surface *surface = to_shm_surface(base);
1128 for (i = 0; i < MAX_LEAVES; i++)
1129 shm_surface_leaf_release(&surface->leaf[i]);
1134 static struct toysurface *
1135 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1136 uint32_t flags, struct rectangle *rectangle)
1138 struct shm_surface *surface;
1139 DBG_OBJ(wl_surface, "\n");
1141 surface = xmalloc(sizeof *surface);
1142 memset(surface, 0, sizeof *surface);
1147 surface->base.prepare = shm_surface_prepare;
1148 surface->base.swap = shm_surface_swap;
1149 surface->base.acquire = shm_surface_acquire;
1150 surface->base.release = shm_surface_release;
1151 surface->base.destroy = shm_surface_destroy;
1153 surface->display = display;
1154 surface->surface = wl_surface;
1155 surface->flags = flags;
1157 return &surface->base;
1161 * The following correspondences between file names and cursors was copied
1162 * from: https://bugs.kde.org/attachment.cgi?id=67313
1165 static const char *bottom_left_corners[] = {
1166 "bottom_left_corner",
1171 static const char *bottom_right_corners[] = {
1172 "bottom_right_corner",
1177 static const char *bottom_sides[] = {
1183 static const char *grabbings[] = {
1186 "208530c400c041818281048008011002"
1189 static const char *left_ptrs[] = {
1196 static const char *left_sides[] = {
1202 static const char *right_sides[] = {
1208 static const char *top_left_corners[] = {
1214 static const char *top_right_corners[] = {
1220 static const char *top_sides[] = {
1226 static const char *xterms[] = {
1232 static const char *hand1s[] = {
1236 "e29285e634086352946a0e7090d73106"
1239 static const char *watches[] = {
1242 "0426c94ea35c87780ff01dc239897213"
1245 struct cursor_alternatives {
1250 static const struct cursor_alternatives cursors[] = {
1251 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1252 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1253 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1254 {grabbings, ARRAY_LENGTH(grabbings)},
1255 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1256 {left_sides, ARRAY_LENGTH(left_sides)},
1257 {right_sides, ARRAY_LENGTH(right_sides)},
1258 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1259 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1260 {top_sides, ARRAY_LENGTH(top_sides)},
1261 {xterms, ARRAY_LENGTH(xterms)},
1262 {hand1s, ARRAY_LENGTH(hand1s)},
1263 {watches, ARRAY_LENGTH(watches)},
1267 create_cursors(struct display *display)
1269 struct weston_config *config;
1270 struct weston_config_section *s;
1274 struct wl_cursor *cursor;
1276 config = weston_config_parse("weston.ini");
1277 s = weston_config_get_section(config, "shell", NULL, NULL);
1278 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1279 weston_config_section_get_int(s, "cursor-size", &size, 32);
1280 weston_config_destroy(config);
1282 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1285 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1287 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1289 for (j = 0; !cursor && j < cursors[i].count; ++j)
1290 cursor = wl_cursor_theme_get_cursor(
1291 display->cursor_theme, cursors[i].names[j]);
1294 fprintf(stderr, "could not load cursor '%s'\n",
1295 cursors[i].names[0]);
1297 display->cursors[i] = cursor;
1302 destroy_cursors(struct display *display)
1304 wl_cursor_theme_destroy(display->cursor_theme);
1305 free(display->cursors);
1308 struct wl_cursor_image *
1309 display_get_pointer_image(struct display *display, int pointer)
1311 struct wl_cursor *cursor = display->cursors[pointer];
1313 return cursor ? cursor->images[0] : NULL;
1317 surface_flush(struct surface *surface)
1319 if (!surface->cairo_surface)
1322 if (surface->opaque_region) {
1323 wl_surface_set_opaque_region(surface->surface,
1324 surface->opaque_region);
1325 wl_region_destroy(surface->opaque_region);
1326 surface->opaque_region = NULL;
1329 if (surface->input_region) {
1330 wl_surface_set_input_region(surface->surface,
1331 surface->input_region);
1332 wl_region_destroy(surface->input_region);
1333 surface->input_region = NULL;
1336 surface->toysurface->swap(surface->toysurface,
1337 surface->buffer_transform, surface->buffer_scale,
1338 &surface->server_allocation);
1340 cairo_surface_destroy(surface->cairo_surface);
1341 surface->cairo_surface = NULL;
1345 window_has_focus(struct window *window)
1347 return window->focused;
1351 window_close(struct window *window)
1353 if (window->close_handler)
1354 window->close_handler(window->user_data);
1356 display_exit(window->display);
1360 window_get_display(struct window *window)
1362 return window->display;
1366 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1368 struct display *display = surface->window->display;
1369 struct rectangle allocation = surface->allocation;
1371 if (!surface->toysurface && display->dpy &&
1372 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1373 surface->toysurface =
1374 egl_window_surface_create(display,
1380 if (!surface->toysurface)
1381 surface->toysurface = shm_surface_create(display,
1383 flags, &allocation);
1385 surface->cairo_surface = surface->toysurface->prepare(
1386 surface->toysurface, dx, dy,
1387 allocation.width, allocation.height, flags,
1388 surface->buffer_transform, surface->buffer_scale);
1392 window_create_main_surface(struct window *window)
1394 struct surface *surface = window->main_surface;
1399 if (window->resizing)
1400 flags |= SURFACE_HINT_RESIZE;
1402 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1403 flags |= SURFACE_HINT_RGB565;
1405 if (window->resize_edges & THEME_LOCATION_RESIZING_LEFT)
1406 dx = surface->server_allocation.width -
1407 surface->allocation.width;
1409 if (window->resize_edges & THEME_LOCATION_RESIZING_TOP)
1410 dy = surface->server_allocation.height -
1411 surface->allocation.height;
1413 window->resize_edges = 0;
1415 surface_create_surface(surface, dx, dy, flags);
1419 window_get_buffer_transform(struct window *window)
1421 return window->main_surface->buffer_transform;
1425 window_set_buffer_transform(struct window *window,
1426 enum wl_output_transform transform)
1428 window->main_surface->buffer_transform = transform;
1429 wl_surface_set_buffer_transform(window->main_surface->surface,
1434 window_set_buffer_scale(struct window *window,
1437 window->main_surface->buffer_scale = scale;
1438 wl_surface_set_buffer_scale(window->main_surface->surface,
1443 window_get_buffer_scale(struct window *window)
1445 return window->main_surface->buffer_scale;
1449 window_get_output_scale(struct window *window)
1451 struct window_output *window_output;
1452 struct window_output *window_output_tmp;
1455 wl_list_for_each_safe(window_output, window_output_tmp,
1456 &window->window_output_list, link) {
1457 if (window_output->output->scale > scale)
1458 scale = window_output->output->scale;
1464 static void window_frame_destroy(struct window_frame *frame);
1467 surface_destroy(struct surface *surface)
1469 if (surface->frame_cb)
1470 wl_callback_destroy(surface->frame_cb);
1472 if (surface->input_region)
1473 wl_region_destroy(surface->input_region);
1475 if (surface->opaque_region)
1476 wl_region_destroy(surface->opaque_region);
1478 if (surface->subsurface)
1479 wl_subsurface_destroy(surface->subsurface);
1481 wl_surface_destroy(surface->surface);
1483 if (surface->toysurface)
1484 surface->toysurface->destroy(surface->toysurface);
1486 wl_list_remove(&surface->link);
1491 window_destroy(struct window *window)
1493 struct display *display = window->display;
1494 struct input *input;
1495 struct window_output *window_output;
1496 struct window_output *window_output_tmp;
1498 wl_list_remove(&window->redraw_task.link);
1500 wl_list_for_each(input, &display->input_list, link) {
1501 if (input->touch_focus == window)
1502 input->touch_focus = NULL;
1503 if (input->pointer_focus == window)
1504 input->pointer_focus = NULL;
1505 if (input->keyboard_focus == window)
1506 input->keyboard_focus = NULL;
1507 if (input->focus_widget &&
1508 input->focus_widget->window == window)
1509 input->focus_widget = NULL;
1512 wl_list_for_each_safe(window_output, window_output_tmp,
1513 &window->window_output_list, link) {
1514 free (window_output);
1518 window_frame_destroy(window->frame);
1520 if (window->xdg_surface)
1521 xdg_surface_destroy(window->xdg_surface);
1522 if (window->xdg_popup)
1523 xdg_popup_destroy(window->xdg_popup);
1525 surface_destroy(window->main_surface);
1527 wl_list_remove(&window->link);
1529 free(window->title);
1533 static struct widget *
1534 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1536 struct widget *child, *target;
1538 wl_list_for_each(child, &widget->child_list, link) {
1539 target = widget_find_widget(child, x, y);
1544 if (widget->allocation.x <= x &&
1545 x < widget->allocation.x + widget->allocation.width &&
1546 widget->allocation.y <= y &&
1547 y < widget->allocation.y + widget->allocation.height) {
1554 static struct widget *
1555 window_find_widget(struct window *window, int32_t x, int32_t y)
1557 struct surface *surface;
1558 struct widget *widget;
1560 wl_list_for_each(surface, &window->subsurface_list, link) {
1561 widget = widget_find_widget(surface->widget, x, y);
1569 static struct widget *
1570 widget_create(struct window *window, struct surface *surface, void *data)
1572 struct widget *widget;
1574 widget = xzalloc(sizeof *widget);
1575 widget->window = window;
1576 widget->surface = surface;
1577 widget->user_data = data;
1578 widget->allocation = surface->allocation;
1579 wl_list_init(&widget->child_list);
1581 widget->tooltip = NULL;
1582 widget->tooltip_count = 0;
1583 widget->default_cursor = CURSOR_LEFT_PTR;
1584 widget->use_cairo = 1;
1590 window_add_widget(struct window *window, void *data)
1592 struct widget *widget;
1594 widget = widget_create(window, window->main_surface, data);
1595 wl_list_init(&widget->link);
1596 window->main_surface->widget = widget;
1602 widget_add_widget(struct widget *parent, void *data)
1604 struct widget *widget;
1606 widget = widget_create(parent->window, parent->surface, data);
1607 wl_list_insert(parent->child_list.prev, &widget->link);
1613 widget_destroy(struct widget *widget)
1615 struct display *display = widget->window->display;
1616 struct surface *surface = widget->surface;
1617 struct input *input;
1619 /* Destroy the sub-surface along with the root widget */
1620 if (surface->widget == widget && surface->subsurface)
1621 surface_destroy(widget->surface);
1623 if (widget->tooltip)
1624 widget_destroy_tooltip(widget);
1626 wl_list_for_each(input, &display->input_list, link) {
1627 if (input->focus_widget == widget)
1628 input->focus_widget = NULL;
1631 wl_list_remove(&widget->link);
1636 widget_set_default_cursor(struct widget *widget, int cursor)
1638 widget->default_cursor = cursor;
1642 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1644 *allocation = widget->allocation;
1648 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1650 widget->allocation.width = width;
1651 widget->allocation.height = height;
1655 widget_set_allocation(struct widget *widget,
1656 int32_t x, int32_t y, int32_t width, int32_t height)
1658 widget->allocation.x = x;
1659 widget->allocation.y = y;
1660 widget_set_size(widget, width, height);
1664 widget_set_transparent(struct widget *widget, int transparent)
1666 widget->opaque = !transparent;
1670 widget_get_user_data(struct widget *widget)
1672 return widget->user_data;
1675 static cairo_surface_t *
1676 widget_get_cairo_surface(struct widget *widget)
1678 struct surface *surface = widget->surface;
1679 struct window *window = widget->window;
1681 assert(widget->use_cairo);
1683 if (!surface->cairo_surface) {
1684 if (surface == window->main_surface)
1685 window_create_main_surface(window);
1687 surface_create_surface(surface, 0, 0, 0);
1690 return surface->cairo_surface;
1694 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1696 struct surface *surface = widget->surface;
1699 enum wl_output_transform transform;
1700 int surface_width, surface_height;
1701 int translate_x, translate_y;
1704 surface_width = surface->allocation.width;
1705 surface_height = surface->allocation.height;
1707 transform = surface->buffer_transform;
1708 scale = surface->buffer_scale;
1710 switch (transform) {
1711 case WL_OUTPUT_TRANSFORM_FLIPPED:
1712 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1713 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1714 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1715 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1718 cairo_matrix_init_identity(&m);
1722 switch (transform) {
1723 case WL_OUTPUT_TRANSFORM_NORMAL:
1729 case WL_OUTPUT_TRANSFORM_FLIPPED:
1731 translate_x = surface_width;
1734 case WL_OUTPUT_TRANSFORM_90:
1736 translate_x = surface_height;
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1741 translate_x = surface_height;
1742 translate_y = surface_width;
1744 case WL_OUTPUT_TRANSFORM_180:
1746 translate_x = surface_width;
1747 translate_y = surface_height;
1749 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1752 translate_y = surface_height;
1754 case WL_OUTPUT_TRANSFORM_270:
1755 angle = M_PI + M_PI_2;
1757 translate_y = surface_width;
1759 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1760 angle = M_PI + M_PI_2;
1766 cairo_scale(cr, scale, scale);
1767 cairo_translate(cr, translate_x, translate_y);
1768 cairo_rotate(cr, angle);
1769 cairo_transform(cr, &m);
1773 widget_cairo_create(struct widget *widget)
1775 struct surface *surface = widget->surface;
1776 cairo_surface_t *cairo_surface;
1779 cairo_surface = widget_get_cairo_surface(widget);
1780 cr = cairo_create(cairo_surface);
1782 widget_cairo_update_transform(widget, cr);
1784 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1790 widget_get_wl_surface(struct widget *widget)
1792 return widget->surface->surface;
1795 struct wl_subsurface *
1796 widget_get_wl_subsurface(struct widget *widget)
1798 return widget->surface->subsurface;
1802 widget_get_last_time(struct widget *widget)
1804 return widget->surface->last_time;
1808 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1810 struct wl_compositor *comp = widget->window->display->compositor;
1811 struct surface *surface = widget->surface;
1813 if (!surface->input_region)
1814 surface->input_region = wl_compositor_create_region(comp);
1817 wl_region_add(surface->input_region,
1818 rect->x, rect->y, rect->width, rect->height);
1823 widget_set_resize_handler(struct widget *widget,
1824 widget_resize_handler_t handler)
1826 widget->resize_handler = handler;
1830 widget_set_redraw_handler(struct widget *widget,
1831 widget_redraw_handler_t handler)
1833 widget->redraw_handler = handler;
1837 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1839 widget->enter_handler = handler;
1843 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1845 widget->leave_handler = handler;
1849 widget_set_motion_handler(struct widget *widget,
1850 widget_motion_handler_t handler)
1852 widget->motion_handler = handler;
1856 widget_set_button_handler(struct widget *widget,
1857 widget_button_handler_t handler)
1859 widget->button_handler = handler;
1863 widget_set_touch_up_handler(struct widget *widget,
1864 widget_touch_up_handler_t handler)
1866 widget->touch_up_handler = handler;
1870 widget_set_touch_down_handler(struct widget *widget,
1871 widget_touch_down_handler_t handler)
1873 widget->touch_down_handler = handler;
1877 widget_set_touch_motion_handler(struct widget *widget,
1878 widget_touch_motion_handler_t handler)
1880 widget->touch_motion_handler = handler;
1884 widget_set_touch_frame_handler(struct widget *widget,
1885 widget_touch_frame_handler_t handler)
1887 widget->touch_frame_handler = handler;
1891 widget_set_touch_cancel_handler(struct widget *widget,
1892 widget_touch_cancel_handler_t handler)
1894 widget->touch_cancel_handler = handler;
1898 widget_set_axis_handler(struct widget *widget,
1899 widget_axis_handler_t handler)
1901 widget->axis_handler = handler;
1905 window_schedule_redraw_task(struct window *window);
1908 widget_schedule_redraw(struct widget *widget)
1910 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1911 widget->surface->redraw_needed = 1;
1912 window_schedule_redraw_task(widget->window);
1916 widget_set_use_cairo(struct widget *widget,
1919 widget->use_cairo = use_cairo;
1923 window_get_surface(struct window *window)
1925 cairo_surface_t *cairo_surface;
1927 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1929 return cairo_surface_reference(cairo_surface);
1933 window_get_wl_surface(struct window *window)
1935 return window->main_surface->surface;
1939 tooltip_redraw_handler(struct widget *widget, void *data)
1942 const int32_t r = 3;
1943 struct tooltip *tooltip = data;
1944 int32_t width, height;
1946 cr = widget_cairo_create(widget);
1947 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1948 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1949 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1952 width = widget->allocation.width;
1953 height = widget->allocation.height;
1954 rounded_rect(cr, 0, 0, width, height, r);
1956 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1957 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1960 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1961 cairo_move_to(cr, 10, 16);
1962 cairo_show_text(cr, tooltip->entry);
1966 static cairo_text_extents_t
1967 get_text_extents(struct display *display, struct tooltip *tooltip)
1970 cairo_text_extents_t extents;
1972 /* Use the dummy_surface because tooltip's surface was not
1973 * created yet, and parent does not have a valid surface
1974 * outside repaint, either.
1976 cr = cairo_create(display->dummy_surface);
1977 cairo_text_extents(cr, tooltip->entry, &extents);
1984 window_create_tooltip(struct tooltip *tooltip)
1986 struct widget *parent = tooltip->parent;
1987 struct display *display = parent->window->display;
1988 const int offset_y = 27;
1989 const int margin = 3;
1990 cairo_text_extents_t extents;
1992 if (tooltip->widget)
1995 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1997 extents = get_text_extents(display, tooltip);
1998 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1999 widget_set_allocation(tooltip->widget,
2000 tooltip->x, tooltip->y + offset_y,
2001 extents.width + 20, 20 + margin * 2);
2007 widget_destroy_tooltip(struct widget *parent)
2009 struct tooltip *tooltip = parent->tooltip;
2011 parent->tooltip_count = 0;
2015 if (tooltip->widget) {
2016 widget_destroy(tooltip->widget);
2017 tooltip->widget = NULL;
2020 close(tooltip->tooltip_fd);
2021 free(tooltip->entry);
2023 parent->tooltip = NULL;
2027 tooltip_func(struct task *task, uint32_t events)
2029 struct tooltip *tooltip =
2030 container_of(task, struct tooltip, tooltip_task);
2033 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2035 window_create_tooltip(tooltip);
2038 #define TOOLTIP_TIMEOUT 500
2040 tooltip_timer_reset(struct tooltip *tooltip)
2042 struct itimerspec its;
2044 its.it_interval.tv_sec = 0;
2045 its.it_interval.tv_nsec = 0;
2046 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2047 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2048 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2049 fprintf(stderr, "could not set timerfd\n: %m");
2057 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2059 struct tooltip *tooltip = parent->tooltip;
2061 parent->tooltip_count++;
2065 tooltip_timer_reset(tooltip);
2069 /* the handler might be triggered too fast via input device motion, so
2070 * we need this check here to make sure tooltip is fully initialized */
2071 if (parent->tooltip_count > 1)
2074 tooltip = malloc(sizeof *tooltip);
2078 parent->tooltip = tooltip;
2079 tooltip->parent = parent;
2080 tooltip->widget = NULL;
2083 tooltip->entry = strdup(entry);
2084 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2085 if (tooltip->tooltip_fd < 0) {
2086 fprintf(stderr, "could not create timerfd\n: %m");
2090 tooltip->tooltip_task.run = tooltip_func;
2091 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2092 EPOLLIN, &tooltip->tooltip_task);
2093 tooltip_timer_reset(tooltip);
2099 workspace_manager_state(void *data,
2100 struct workspace_manager *workspace_manager,
2104 struct display *display = data;
2106 display->workspace = current;
2107 display->workspace_count = count;
2110 static const struct workspace_manager_listener workspace_manager_listener = {
2111 workspace_manager_state
2115 frame_resize_handler(struct widget *widget,
2116 int32_t width, int32_t height, void *data)
2118 struct window_frame *frame = data;
2119 struct widget *child = frame->child;
2120 struct rectangle interior;
2121 struct rectangle input;
2122 struct rectangle opaque;
2124 if (widget->window->fullscreen) {
2127 interior.width = width;
2128 interior.height = height;
2130 if (widget->window->maximized) {
2131 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2133 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2136 frame_resize(frame->frame, width, height);
2137 frame_interior(frame->frame, &interior.x, &interior.y,
2138 &interior.width, &interior.height);
2141 widget_set_allocation(child, interior.x, interior.y,
2142 interior.width, interior.height);
2144 if (child->resize_handler) {
2145 child->resize_handler(child, interior.width, interior.height,
2148 if (widget->window->fullscreen) {
2149 width = child->allocation.width;
2150 height = child->allocation.height;
2152 frame_resize_inside(frame->frame,
2153 child->allocation.width,
2154 child->allocation.height);
2155 width = frame_width(frame->frame);
2156 height = frame_height(frame->frame);
2160 widget_set_allocation(widget, 0, 0, width, height);
2162 widget->surface->input_region =
2163 wl_compositor_create_region(widget->window->display->compositor);
2164 if (!widget->window->fullscreen) {
2165 frame_input_rect(frame->frame, &input.x, &input.y,
2166 &input.width, &input.height);
2167 wl_region_add(widget->surface->input_region,
2168 input.x, input.y, input.width, input.height);
2170 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2173 widget_set_allocation(widget, 0, 0, width, height);
2175 if (child->opaque) {
2176 if (!widget->window->fullscreen) {
2177 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2178 &opaque.width, &opaque.height);
2180 wl_region_add(widget->surface->opaque_region,
2182 opaque.width, opaque.height);
2184 wl_region_add(widget->surface->opaque_region,
2185 0, 0, width, height);
2190 widget_schedule_redraw(widget);
2194 frame_redraw_handler(struct widget *widget, void *data)
2197 struct window_frame *frame = data;
2198 struct window *window = widget->window;
2200 if (window->fullscreen)
2203 if (window->focused) {
2204 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2206 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2209 cr = widget_cairo_create(widget);
2211 frame_repaint(frame->frame, cr);
2217 frame_get_pointer_image_for_location(struct window_frame *frame,
2218 enum theme_location location)
2220 struct window *window = frame->widget->window;
2223 return CURSOR_LEFT_PTR;
2226 case THEME_LOCATION_RESIZING_TOP:
2228 case THEME_LOCATION_RESIZING_BOTTOM:
2229 return CURSOR_BOTTOM;
2230 case THEME_LOCATION_RESIZING_LEFT:
2232 case THEME_LOCATION_RESIZING_RIGHT:
2233 return CURSOR_RIGHT;
2234 case THEME_LOCATION_RESIZING_TOP_LEFT:
2235 return CURSOR_TOP_LEFT;
2236 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2237 return CURSOR_TOP_RIGHT;
2238 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2239 return CURSOR_BOTTOM_LEFT;
2240 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2241 return CURSOR_BOTTOM_RIGHT;
2242 case THEME_LOCATION_EXTERIOR:
2243 case THEME_LOCATION_TITLEBAR:
2245 return CURSOR_LEFT_PTR;
2250 frame_menu_func(struct window *window,
2251 struct input *input, int index, void *data)
2253 struct display *display;
2257 window_close(window);
2259 case 1: /* move to workspace above */
2260 display = window->display;
2261 if (display->workspace > 0)
2262 workspace_manager_move_surface(
2263 display->workspace_manager,
2264 window->main_surface->surface,
2265 display->workspace - 1);
2267 case 2: /* move to workspace below */
2268 display = window->display;
2269 if (display->workspace < display->workspace_count - 1)
2270 workspace_manager_move_surface(
2271 display->workspace_manager,
2272 window->main_surface->surface,
2273 display->workspace + 1);
2275 case 3: /* fullscreen */
2276 /* we don't have a way to get out of fullscreen for now */
2277 if (window->fullscreen_handler)
2278 window->fullscreen_handler(window, window->user_data);
2284 window_show_frame_menu(struct window *window,
2285 struct input *input, uint32_t time)
2290 static const char *entries[] = {
2292 "Move to workspace above", "Move to workspace below",
2296 if (window->fullscreen_handler)
2297 count = ARRAY_LENGTH(entries);
2299 count = ARRAY_LENGTH(entries) - 1;
2301 input_get_position(input, &x, &y);
2302 window_show_menu(window->display, input, time, window,
2303 x - 10, y - 10, frame_menu_func, entries, count);
2307 frame_enter_handler(struct widget *widget,
2308 struct input *input, float x, float y, void *data)
2310 struct window_frame *frame = data;
2311 enum theme_location location;
2313 location = frame_pointer_enter(frame->frame, input, x, y);
2314 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2315 widget_schedule_redraw(frame->widget);
2317 return frame_get_pointer_image_for_location(data, location);
2321 frame_motion_handler(struct widget *widget,
2322 struct input *input, uint32_t time,
2323 float x, float y, void *data)
2325 struct window_frame *frame = data;
2326 enum theme_location location;
2328 location = frame_pointer_motion(frame->frame, input, x, y);
2329 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2330 widget_schedule_redraw(frame->widget);
2332 return frame_get_pointer_image_for_location(data, location);
2336 frame_leave_handler(struct widget *widget,
2337 struct input *input, void *data)
2339 struct window_frame *frame = data;
2341 frame_pointer_leave(frame->frame, input);
2342 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2343 widget_schedule_redraw(frame->widget);
2347 frame_handle_status(struct window_frame *frame, struct input *input,
2348 uint32_t time, enum theme_location location)
2350 struct window *window = frame->widget->window;
2353 status = frame_status(frame->frame);
2354 if (status & FRAME_STATUS_REPAINT)
2355 widget_schedule_redraw(frame->widget);
2357 if (status & FRAME_STATUS_MINIMIZE) {
2358 window_set_minimized(window);
2359 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2362 if (status & FRAME_STATUS_MENU) {
2363 window_show_frame_menu(window, input, time);
2364 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2367 if (status & FRAME_STATUS_MAXIMIZE) {
2368 window_set_maximized(window, !window->maximized);
2369 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2372 if (status & FRAME_STATUS_CLOSE) {
2373 window_close(window);
2377 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2378 input_ungrab(input);
2379 xdg_surface_move(window->xdg_surface,
2380 input_get_seat(input),
2381 window->display->serial);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2386 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2387 input_ungrab(input);
2389 window->resizing = 1;
2390 xdg_surface_resize(window->xdg_surface,
2391 input_get_seat(input),
2392 window->display->serial,
2395 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2400 frame_button_handler(struct widget *widget,
2401 struct input *input, uint32_t time,
2402 uint32_t button, enum wl_pointer_button_state state,
2406 struct window_frame *frame = data;
2407 enum theme_location location;
2409 location = frame_pointer_button(frame->frame, input, button, state);
2410 frame_handle_status(frame, input, time, location);
2414 frame_touch_down_handler(struct widget *widget, struct input *input,
2415 uint32_t serial, uint32_t time, int32_t id,
2416 float x, float y, void *data)
2418 struct window_frame *frame = data;
2420 frame_touch_down(frame->frame, input, id, x, y);
2421 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2425 frame_touch_up_handler(struct widget *widget,
2426 struct input *input, uint32_t serial, uint32_t time,
2427 int32_t id, void *data)
2429 struct window_frame *frame = data;
2431 frame_touch_up(frame->frame, input, id);
2432 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2436 window_frame_create(struct window *window, void *data)
2438 struct window_frame *frame;
2441 if (window->custom) {
2442 buttons = FRAME_BUTTON_NONE;
2444 buttons = FRAME_BUTTON_ALL;
2447 frame = xzalloc(sizeof *frame);
2448 frame->frame = frame_create(window->display->theme, 0, 0,
2449 buttons, window->title);
2451 frame->widget = window_add_widget(window, frame);
2452 frame->child = widget_add_widget(frame->widget, data);
2454 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2455 widget_set_resize_handler(frame->widget, frame_resize_handler);
2456 widget_set_enter_handler(frame->widget, frame_enter_handler);
2457 widget_set_leave_handler(frame->widget, frame_leave_handler);
2458 widget_set_motion_handler(frame->widget, frame_motion_handler);
2459 widget_set_button_handler(frame->widget, frame_button_handler);
2460 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2461 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2463 window->frame = frame;
2465 return frame->child;
2469 window_frame_set_child_size(struct widget *widget, int child_width,
2472 struct display *display = widget->window->display;
2473 struct theme *t = display->theme;
2474 int decoration_width, decoration_height;
2476 int margin = widget->window->maximized ? 0 : t->margin;
2478 if (!widget->window->fullscreen) {
2479 decoration_width = (t->width + margin) * 2;
2480 decoration_height = t->width +
2481 t->titlebar_height + margin * 2;
2483 width = child_width + decoration_width;
2484 height = child_height + decoration_height;
2486 width = child_width;
2487 height = child_height;
2490 window_schedule_resize(widget->window, width, height);
2494 window_frame_destroy(struct window_frame *frame)
2496 frame_destroy(frame->frame);
2498 /* frame->child must be destroyed by the application */
2499 widget_destroy(frame->widget);
2504 input_set_focus_widget(struct input *input, struct widget *focus,
2507 struct widget *old, *widget;
2510 if (focus == input->focus_widget)
2513 old = input->focus_widget;
2517 widget = input->grab;
2518 if (widget->leave_handler)
2519 widget->leave_handler(old, input, widget->user_data);
2520 input->focus_widget = NULL;
2526 widget = input->grab;
2527 input->focus_widget = focus;
2528 if (widget->enter_handler)
2529 cursor = widget->enter_handler(focus, input, x, y,
2532 cursor = widget->default_cursor;
2534 input_set_pointer_image(input, cursor);
2539 touch_grab(struct input *input, int32_t touch_id)
2541 input->touch_grab = 1;
2542 input->touch_grab_id = touch_id;
2546 touch_ungrab(struct input *input)
2548 struct touch_point *tp, *tmp;
2550 input->touch_grab = 0;
2552 wl_list_for_each_safe(tp, tmp,
2553 &input->touch_point_list, link) {
2554 if (tp->id != input->touch_grab_id)
2556 wl_list_remove(&tp->link);
2564 input_grab(struct input *input, struct widget *widget, uint32_t button)
2566 input->grab = widget;
2567 input->grab_button = button;
2571 input_ungrab(struct input *input)
2573 struct widget *widget;
2576 if (input->pointer_focus) {
2577 widget = window_find_widget(input->pointer_focus,
2578 input->sx, input->sy);
2579 input_set_focus_widget(input, widget, input->sx, input->sy);
2584 input_remove_pointer_focus(struct input *input)
2586 struct window *window = input->pointer_focus;
2591 input_set_focus_widget(input, NULL, 0, 0);
2593 input->pointer_focus = NULL;
2594 input->current_cursor = CURSOR_UNSET;
2598 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2599 uint32_t serial, struct wl_surface *surface,
2600 wl_fixed_t sx_w, wl_fixed_t sy_w)
2602 struct input *input = data;
2603 struct window *window;
2604 struct widget *widget;
2605 float sx = wl_fixed_to_double(sx_w);
2606 float sy = wl_fixed_to_double(sy_w);
2609 /* enter event for a window we've just destroyed */
2613 input->display->serial = serial;
2614 input->pointer_enter_serial = serial;
2615 input->pointer_focus = wl_surface_get_user_data(surface);
2616 window = input->pointer_focus;
2618 if (window->resizing) {
2619 window->resizing = 0;
2620 /* Schedule a redraw to free the pool */
2621 window_schedule_redraw(window);
2627 widget = window_find_widget(window, sx, sy);
2628 input_set_focus_widget(input, widget, sx, sy);
2632 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2633 uint32_t serial, struct wl_surface *surface)
2635 struct input *input = data;
2637 input->display->serial = serial;
2638 input_remove_pointer_focus(input);
2642 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2643 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2645 struct input *input = data;
2646 struct window *window = input->pointer_focus;
2647 struct widget *widget;
2649 float sx = wl_fixed_to_double(sx_w);
2650 float sy = wl_fixed_to_double(sy_w);
2658 /* when making the window smaller - e.g. after a unmaximise we might
2659 * still have a pending motion event that the compositor has picked
2660 * based on the old surface dimensions
2662 if (sx > window->main_surface->allocation.width ||
2663 sy > window->main_surface->allocation.height)
2666 if (!(input->grab && input->grab_button)) {
2667 widget = window_find_widget(window, sx, sy);
2668 input_set_focus_widget(input, widget, sx, sy);
2672 widget = input->grab;
2674 widget = input->focus_widget;
2676 if (widget->motion_handler)
2677 cursor = widget->motion_handler(input->focus_widget,
2678 input, time, sx, sy,
2681 cursor = widget->default_cursor;
2683 cursor = CURSOR_LEFT_PTR;
2685 input_set_pointer_image(input, cursor);
2689 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2690 uint32_t time, uint32_t button, uint32_t state_w)
2692 struct input *input = data;
2693 struct widget *widget;
2694 enum wl_pointer_button_state state = state_w;
2696 input->display->serial = serial;
2697 if (input->focus_widget && input->grab == NULL &&
2698 state == WL_POINTER_BUTTON_STATE_PRESSED)
2699 input_grab(input, input->focus_widget, button);
2701 widget = input->grab;
2702 if (widget && widget->button_handler)
2703 (*widget->button_handler)(widget,
2706 input->grab->user_data);
2708 if (input->grab && input->grab_button == button &&
2709 state == WL_POINTER_BUTTON_STATE_RELEASED)
2710 input_ungrab(input);
2714 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2715 uint32_t time, uint32_t axis, wl_fixed_t value)
2717 struct input *input = data;
2718 struct widget *widget;
2720 widget = input->focus_widget;
2722 widget = input->grab;
2723 if (widget && widget->axis_handler)
2724 (*widget->axis_handler)(widget,
2730 static const struct wl_pointer_listener pointer_listener = {
2731 pointer_handle_enter,
2732 pointer_handle_leave,
2733 pointer_handle_motion,
2734 pointer_handle_button,
2735 pointer_handle_axis,
2739 input_remove_keyboard_focus(struct input *input)
2741 struct window *window = input->keyboard_focus;
2742 struct itimerspec its;
2744 its.it_interval.tv_sec = 0;
2745 its.it_interval.tv_nsec = 0;
2746 its.it_value.tv_sec = 0;
2747 its.it_value.tv_nsec = 0;
2748 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2753 if (window->keyboard_focus_handler)
2754 (*window->keyboard_focus_handler)(window, NULL,
2757 input->keyboard_focus = NULL;
2761 keyboard_repeat_func(struct task *task, uint32_t events)
2763 struct input *input =
2764 container_of(task, struct input, repeat_task);
2765 struct window *window = input->keyboard_focus;
2768 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2769 /* If we change the timer between the fd becoming
2770 * readable and getting here, there'll be nothing to
2771 * read and we get EAGAIN. */
2774 if (window && window->key_handler) {
2775 (*window->key_handler)(window, input, input->repeat_time,
2776 input->repeat_key, input->repeat_sym,
2777 WL_KEYBOARD_KEY_STATE_PRESSED,
2783 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2784 uint32_t format, int fd, uint32_t size)
2786 struct input *input = data;
2787 struct xkb_keymap *keymap;
2788 struct xkb_state *state;
2796 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2801 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2802 if (map_str == MAP_FAILED) {
2807 keymap = xkb_map_new_from_string(input->display->xkb_context,
2809 XKB_KEYMAP_FORMAT_TEXT_V1,
2811 munmap(map_str, size);
2815 fprintf(stderr, "failed to compile keymap\n");
2819 state = xkb_state_new(keymap);
2821 fprintf(stderr, "failed to create XKB state\n");
2822 xkb_map_unref(keymap);
2826 xkb_keymap_unref(input->xkb.keymap);
2827 xkb_state_unref(input->xkb.state);
2828 input->xkb.keymap = keymap;
2829 input->xkb.state = state;
2831 input->xkb.control_mask =
2832 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2833 input->xkb.alt_mask =
2834 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2835 input->xkb.shift_mask =
2836 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2840 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2841 uint32_t serial, struct wl_surface *surface,
2842 struct wl_array *keys)
2844 struct input *input = data;
2845 struct window *window;
2847 input->display->serial = serial;
2848 input->keyboard_focus = wl_surface_get_user_data(surface);
2850 window = input->keyboard_focus;
2851 if (window->keyboard_focus_handler)
2852 (*window->keyboard_focus_handler)(window,
2853 input, window->user_data);
2857 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2858 uint32_t serial, struct wl_surface *surface)
2860 struct input *input = data;
2862 input->display->serial = serial;
2863 input_remove_keyboard_focus(input);
2867 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2868 uint32_t serial, uint32_t time, uint32_t key,
2871 struct input *input = data;
2872 struct window *window = input->keyboard_focus;
2873 uint32_t code, num_syms;
2874 enum wl_keyboard_key_state state = state_w;
2875 const xkb_keysym_t *syms;
2877 struct itimerspec its;
2879 input->display->serial = serial;
2881 if (!window || !input->xkb.state)
2884 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2886 sym = XKB_KEY_NoSymbol;
2891 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2892 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2893 window_set_maximized(window, !window->maximized);
2894 } else if (sym == XKB_KEY_F11 &&
2895 window->fullscreen_handler &&
2896 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2897 window->fullscreen_handler(window, window->user_data);
2898 } else if (sym == XKB_KEY_F4 &&
2899 input->modifiers == MOD_ALT_MASK &&
2900 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2901 window_close(window);
2902 } else if (window->key_handler) {
2903 (*window->key_handler)(window, input, time, key,
2904 sym, state, window->user_data);
2907 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2908 key == input->repeat_key) {
2909 its.it_interval.tv_sec = 0;
2910 its.it_interval.tv_nsec = 0;
2911 its.it_value.tv_sec = 0;
2912 its.it_value.tv_nsec = 0;
2913 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2914 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2915 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2916 input->repeat_sym = sym;
2917 input->repeat_key = key;
2918 input->repeat_time = time;
2919 its.it_interval.tv_sec = 0;
2920 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2921 its.it_value.tv_sec = 0;
2922 its.it_value.tv_nsec = 400 * 1000 * 1000;
2923 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2928 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2929 uint32_t serial, uint32_t mods_depressed,
2930 uint32_t mods_latched, uint32_t mods_locked,
2933 struct input *input = data;
2934 xkb_mod_mask_t mask;
2936 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2937 if (!input->xkb.keymap)
2940 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2941 mods_locked, 0, 0, group);
2942 mask = xkb_state_serialize_mods(input->xkb.state,
2943 XKB_STATE_DEPRESSED |
2945 input->modifiers = 0;
2946 if (mask & input->xkb.control_mask)
2947 input->modifiers |= MOD_CONTROL_MASK;
2948 if (mask & input->xkb.alt_mask)
2949 input->modifiers |= MOD_ALT_MASK;
2950 if (mask & input->xkb.shift_mask)
2951 input->modifiers |= MOD_SHIFT_MASK;
2954 static const struct wl_keyboard_listener keyboard_listener = {
2955 keyboard_handle_keymap,
2956 keyboard_handle_enter,
2957 keyboard_handle_leave,
2958 keyboard_handle_key,
2959 keyboard_handle_modifiers,
2963 touch_handle_down(void *data, struct wl_touch *wl_touch,
2964 uint32_t serial, uint32_t time, struct wl_surface *surface,
2965 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2967 struct input *input = data;
2968 struct widget *widget;
2969 float sx = wl_fixed_to_double(x_w);
2970 float sy = wl_fixed_to_double(y_w);
2972 input->display->serial = serial;
2973 input->touch_focus = wl_surface_get_user_data(surface);
2974 if (!input->touch_focus) {
2975 DBG("Failed to find to touch focus for surface %p\n", surface);
2979 widget = window_find_widget(input->touch_focus,
2980 wl_fixed_to_double(x_w),
2981 wl_fixed_to_double(y_w));
2983 struct touch_point *tp = xmalloc(sizeof *tp);
2986 tp->widget = widget;
2989 wl_list_insert(&input->touch_point_list, &tp->link);
2991 if (widget->touch_down_handler)
2992 (*widget->touch_down_handler)(widget, input,
3001 touch_handle_up(void *data, struct wl_touch *wl_touch,
3002 uint32_t serial, uint32_t time, int32_t id)
3004 struct input *input = data;
3005 struct touch_point *tp, *tmp;
3007 if (!input->touch_focus) {
3008 DBG("No touch focus found for touch up event!\n");
3012 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3016 if (tp->widget->touch_up_handler)
3017 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3019 tp->widget->user_data);
3021 wl_list_remove(&tp->link);
3029 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3030 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3032 struct input *input = data;
3033 struct touch_point *tp;
3034 float sx = wl_fixed_to_double(x_w);
3035 float sy = wl_fixed_to_double(y_w);
3037 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3039 if (!input->touch_focus) {
3040 DBG("No touch focus found for touch motion event!\n");
3044 wl_list_for_each(tp, &input->touch_point_list, link) {
3050 if (tp->widget->touch_motion_handler)
3051 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3053 tp->widget->user_data);
3059 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3061 struct input *input = data;
3062 struct touch_point *tp, *tmp;
3064 DBG("touch_handle_frame\n");
3066 if (!input->touch_focus) {
3067 DBG("No touch focus found for touch frame event!\n");
3071 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3072 if (tp->widget->touch_frame_handler)
3073 (*tp->widget->touch_frame_handler)(tp->widget, input,
3074 tp->widget->user_data);
3076 wl_list_remove(&tp->link);
3082 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3084 struct input *input = data;
3085 struct touch_point *tp, *tmp;
3087 DBG("touch_handle_cancel\n");
3089 if (!input->touch_focus) {
3090 DBG("No touch focus found for touch cancel event!\n");
3094 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3095 if (tp->widget->touch_cancel_handler)
3096 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3097 tp->widget->user_data);
3099 wl_list_remove(&tp->link);
3104 static const struct wl_touch_listener touch_listener = {
3107 touch_handle_motion,
3109 touch_handle_cancel,
3113 seat_handle_capabilities(void *data, struct wl_seat *seat,
3114 enum wl_seat_capability caps)
3116 struct input *input = data;
3118 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3119 input->pointer = wl_seat_get_pointer(seat);
3120 wl_pointer_set_user_data(input->pointer, input);
3121 wl_pointer_add_listener(input->pointer, &pointer_listener,
3123 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3124 wl_pointer_destroy(input->pointer);
3125 input->pointer = NULL;
3128 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3129 input->keyboard = wl_seat_get_keyboard(seat);
3130 wl_keyboard_set_user_data(input->keyboard, input);
3131 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3133 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3134 wl_keyboard_destroy(input->keyboard);
3135 input->keyboard = NULL;
3138 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3139 input->touch = wl_seat_get_touch(seat);
3140 wl_touch_set_user_data(input->touch, input);
3141 wl_touch_add_listener(input->touch, &touch_listener, input);
3142 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3143 wl_touch_destroy(input->touch);
3144 input->touch = NULL;
3149 seat_handle_name(void *data, struct wl_seat *seat,
3155 static const struct wl_seat_listener seat_listener = {
3156 seat_handle_capabilities,
3161 input_get_position(struct input *input, int32_t *x, int32_t *y)
3168 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3170 struct touch_point *tp;
3172 wl_list_for_each(tp, &input->touch_point_list, link) {
3185 input_get_display(struct input *input)
3187 return input->display;
3191 input_get_seat(struct input *input)
3197 input_get_modifiers(struct input *input)
3199 return input->modifiers;
3203 input_get_focus_widget(struct input *input)
3205 return input->focus_widget;
3209 struct wl_data_offer *offer;
3210 struct input *input;
3211 struct wl_array types;
3214 struct task io_task;
3222 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3224 struct data_offer *offer = data;
3227 p = wl_array_add(&offer->types, sizeof *p);
3231 static const struct wl_data_offer_listener data_offer_listener = {
3236 data_offer_destroy(struct data_offer *offer)
3241 if (offer->refcount == 0) {
3242 wl_data_offer_destroy(offer->offer);
3243 for (p = offer->types.data; *p; p++)
3245 wl_array_release(&offer->types);
3251 data_device_data_offer(void *data,
3252 struct wl_data_device *data_device,
3253 struct wl_data_offer *_offer)
3255 struct data_offer *offer;
3257 offer = xmalloc(sizeof *offer);
3259 wl_array_init(&offer->types);
3260 offer->refcount = 1;
3261 offer->input = data;
3262 offer->offer = _offer;
3263 wl_data_offer_add_listener(offer->offer,
3264 &data_offer_listener, offer);
3268 data_device_enter(void *data, struct wl_data_device *data_device,
3269 uint32_t serial, struct wl_surface *surface,
3270 wl_fixed_t x_w, wl_fixed_t y_w,
3271 struct wl_data_offer *offer)
3273 struct input *input = data;
3274 struct window *window;
3276 float x = wl_fixed_to_double(x_w);
3277 float y = wl_fixed_to_double(y_w);
3280 window = wl_surface_get_user_data(surface);
3281 input->drag_enter_serial = serial;
3282 input->drag_focus = window,
3286 if (!input->touch_grab)
3287 input->pointer_enter_serial = serial;
3290 input->drag_offer = wl_data_offer_get_user_data(offer);
3292 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3295 types_data = input->drag_offer->types.data;
3297 input->drag_offer = NULL;
3301 if (window->data_handler)
3302 window->data_handler(window, input, x, y, types_data,
3307 data_device_leave(void *data, struct wl_data_device *data_device)
3309 struct input *input = data;
3311 if (input->drag_offer) {
3312 data_offer_destroy(input->drag_offer);
3313 input->drag_offer = NULL;
3318 data_device_motion(void *data, struct wl_data_device *data_device,
3319 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3321 struct input *input = data;
3322 struct window *window = input->drag_focus;
3323 float x = wl_fixed_to_double(x_w);
3324 float y = wl_fixed_to_double(y_w);
3330 if (input->drag_offer)
3331 types_data = input->drag_offer->types.data;
3335 if (window->data_handler)
3336 window->data_handler(window, input, x, y, types_data,
3341 data_device_drop(void *data, struct wl_data_device *data_device)
3343 struct input *input = data;
3344 struct window *window = input->drag_focus;
3350 if (window->drop_handler)
3351 window->drop_handler(window, input,
3352 x, y, window->user_data);
3354 if (input->touch_grab)
3355 touch_ungrab(input);
3359 data_device_selection(void *data,
3360 struct wl_data_device *wl_data_device,
3361 struct wl_data_offer *offer)
3363 struct input *input = data;
3366 if (input->selection_offer)
3367 data_offer_destroy(input->selection_offer);
3370 input->selection_offer = wl_data_offer_get_user_data(offer);
3371 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3374 input->selection_offer = NULL;
3378 static const struct wl_data_device_listener data_device_listener = {
3379 data_device_data_offer,
3384 data_device_selection
3388 input_set_pointer_image_index(struct input *input, int index)
3390 struct wl_buffer *buffer;
3391 struct wl_cursor *cursor;
3392 struct wl_cursor_image *image;
3394 if (!input->pointer)
3397 cursor = input->display->cursors[input->current_cursor];
3401 if (index >= (int) cursor->image_count) {
3402 fprintf(stderr, "cursor index out of range\n");
3406 image = cursor->images[index];
3407 buffer = wl_cursor_image_get_buffer(image);
3411 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3412 wl_surface_damage(input->pointer_surface, 0, 0,
3413 image->width, image->height);
3414 wl_surface_commit(input->pointer_surface);
3415 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3416 input->pointer_surface,
3417 image->hotspot_x, image->hotspot_y);
3420 static const struct wl_callback_listener pointer_surface_listener;
3423 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3426 struct input *input = data;
3427 struct wl_cursor *cursor;
3431 assert(callback == input->cursor_frame_cb);
3432 wl_callback_destroy(callback);
3433 input->cursor_frame_cb = NULL;
3436 if (!input->pointer)
3439 if (input->current_cursor == CURSOR_BLANK) {
3440 wl_pointer_set_cursor(input->pointer,
3441 input->pointer_enter_serial,
3446 if (input->current_cursor == CURSOR_UNSET)
3448 cursor = input->display->cursors[input->current_cursor];
3452 /* FIXME We don't have the current time on the first call so we set
3453 * the animation start to the time of the first frame callback. */
3455 input->cursor_anim_start = 0;
3456 else if (input->cursor_anim_start == 0)
3457 input->cursor_anim_start = time;
3459 if (time == 0 || input->cursor_anim_start == 0)
3462 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3464 if (cursor->image_count > 1) {
3465 input->cursor_frame_cb =
3466 wl_surface_frame(input->pointer_surface);
3467 wl_callback_add_listener(input->cursor_frame_cb,
3468 &pointer_surface_listener, input);
3471 input_set_pointer_image_index(input, i);
3474 static const struct wl_callback_listener pointer_surface_listener = {
3475 pointer_surface_frame_callback
3479 input_set_pointer_image(struct input *input, int pointer)
3483 if (!input->pointer)
3486 if (input->pointer_enter_serial > input->cursor_serial)
3489 if (!force && pointer == input->current_cursor)
3492 input->current_cursor = pointer;
3493 input->cursor_serial = input->pointer_enter_serial;
3494 if (!input->cursor_frame_cb)
3495 pointer_surface_frame_callback(input, NULL, 0);
3497 /* The current frame callback may be stuck if, for instance,
3498 * the set cursor request was processed by the server after
3499 * this client lost the focus. In this case the cursor surface
3500 * might not be mapped and the frame callback wouldn't ever
3501 * complete. Send a set_cursor and attach to try to map the
3502 * cursor surface again so that the callback will finish */
3503 input_set_pointer_image_index(input, 0);
3507 struct wl_data_device *
3508 input_get_data_device(struct input *input)
3510 return input->data_device;
3514 input_set_selection(struct input *input,
3515 struct wl_data_source *source, uint32_t time)
3517 wl_data_device_set_selection(input->data_device, source, time);
3521 input_accept(struct input *input, const char *type)
3523 wl_data_offer_accept(input->drag_offer->offer,
3524 input->drag_enter_serial, type);
3528 offer_io_func(struct task *task, uint32_t events)
3530 struct data_offer *offer =
3531 container_of(task, struct data_offer, io_task);
3535 len = read(offer->fd, buffer, sizeof buffer);
3536 offer->func(buffer, len,
3537 offer->x, offer->y, offer->user_data);
3541 data_offer_destroy(offer);
3546 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3547 data_func_t func, void *user_data)
3551 if (pipe2(p, O_CLOEXEC) == -1)
3554 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3557 offer->io_task.run = offer_io_func;
3561 offer->user_data = user_data;
3563 display_watch_fd(offer->input->display,
3564 offer->fd, EPOLLIN, &offer->io_task);
3568 input_receive_drag_data(struct input *input, const char *mime_type,
3569 data_func_t func, void *data)
3571 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3572 input->drag_offer->x = input->drag_x;
3573 input->drag_offer->y = input->drag_y;
3577 input_receive_drag_data_to_fd(struct input *input,
3578 const char *mime_type, int fd)
3580 if (input->drag_offer)
3581 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3587 input_receive_selection_data(struct input *input, const char *mime_type,
3588 data_func_t func, void *data)
3592 if (input->selection_offer == NULL)
3595 for (p = input->selection_offer->types.data; *p; p++)
3596 if (strcmp(mime_type, *p) == 0)
3602 data_offer_receive_data(input->selection_offer,
3603 mime_type, func, data);
3608 input_receive_selection_data_to_fd(struct input *input,
3609 const char *mime_type, int fd)
3611 if (input->selection_offer)
3612 wl_data_offer_receive(input->selection_offer->offer,
3619 window_move(struct window *window, struct input *input, uint32_t serial)
3621 if (!window->xdg_surface)
3624 xdg_surface_move(window->xdg_surface, input->seat, serial);
3628 surface_set_synchronized(struct surface *surface)
3630 if (!surface->subsurface)
3633 if (surface->synchronized)
3636 wl_subsurface_set_sync(surface->subsurface);
3637 surface->synchronized = 1;
3641 surface_set_synchronized_default(struct surface *surface)
3643 if (!surface->subsurface)
3646 if (surface->synchronized == surface->synchronized_default)
3649 if (surface->synchronized_default)
3650 wl_subsurface_set_sync(surface->subsurface);
3652 wl_subsurface_set_desync(surface->subsurface);
3654 surface->synchronized = surface->synchronized_default;
3658 surface_resize(struct surface *surface)
3660 struct widget *widget = surface->widget;
3661 struct wl_compositor *compositor = widget->window->display->compositor;
3663 if (surface->input_region) {
3664 wl_region_destroy(surface->input_region);
3665 surface->input_region = NULL;
3668 if (surface->opaque_region)
3669 wl_region_destroy(surface->opaque_region);
3671 surface->opaque_region = wl_compositor_create_region(compositor);
3673 if (widget->resize_handler)
3674 widget->resize_handler(widget,
3675 widget->allocation.width,
3676 widget->allocation.height,
3679 if (surface->subsurface &&
3680 (surface->allocation.x != widget->allocation.x ||
3681 surface->allocation.y != widget->allocation.y)) {
3682 wl_subsurface_set_position(surface->subsurface,
3683 widget->allocation.x,
3684 widget->allocation.y);
3686 if (surface->allocation.width != widget->allocation.width ||
3687 surface->allocation.height != widget->allocation.height) {
3688 window_schedule_redraw(widget->window);
3690 surface->allocation = widget->allocation;
3693 wl_region_add(surface->opaque_region, 0, 0,
3694 widget->allocation.width,
3695 widget->allocation.height);
3699 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3702 * This hack should be removed, when EGL respects
3703 * eglSwapInterval(0).
3705 * If this window has sub-surfaces, especially a free-running
3706 * EGL-widget, we need to post the parent surface once with
3707 * all the old state to guarantee, that the EGL-widget will
3708 * receive its frame callback soon. Otherwise, a forced call
3709 * to eglSwapBuffers may end up blocking, waiting for a frame
3710 * event that will never come, because we will commit the parent
3711 * surface with all new state only after eglSwapBuffers returns.
3713 * This assumes, that:
3714 * 1. When the EGL widget's resize hook is called, it pauses.
3715 * 2. When the EGL widget's redraw hook is called, it forces a
3716 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3717 * In a single threaded application condition 1 is a no-op.
3719 * XXX: This should actually be after the surface_resize() calls,
3720 * but cannot, because then it would commit the incomplete state
3721 * accumulated from the widget resize hooks.
3723 if (window->subsurface_list.next != &window->main_surface->link ||
3724 window->subsurface_list.prev != &window->main_surface->link)
3725 wl_surface_commit(window->main_surface->surface);
3729 window_do_resize(struct window *window)
3731 struct surface *surface;
3733 widget_set_allocation(window->main_surface->widget,
3734 window->pending_allocation.x,
3735 window->pending_allocation.y,
3736 window->pending_allocation.width,
3737 window->pending_allocation.height);
3739 surface_resize(window->main_surface);
3741 /* The main surface is in the list, too. Main surface's
3742 * resize_handler is responsible for calling widget_set_allocation()
3743 * on all sub-surface root widgets, so they will be resized
3746 wl_list_for_each(surface, &window->subsurface_list, link) {
3747 if (surface == window->main_surface)
3750 surface_set_synchronized(surface);
3751 surface_resize(surface);
3756 idle_resize(struct window *window)
3758 window->resize_needed = 0;
3759 window->redraw_needed = 1;
3761 DBG("from %dx%d to %dx%d\n",
3762 window->main_surface->server_allocation.width,
3763 window->main_surface->server_allocation.height,
3764 window->pending_allocation.width,
3765 window->pending_allocation.height);
3767 hack_prevent_EGL_sub_surface_deadlock(window);
3769 window_do_resize(window);
3773 undo_resize(struct window *window)
3775 window->pending_allocation.width =
3776 window->main_surface->server_allocation.width;
3777 window->pending_allocation.height =
3778 window->main_surface->server_allocation.height;
3780 DBG("back to %dx%d\n",
3781 window->main_surface->server_allocation.width,
3782 window->main_surface->server_allocation.height);
3784 window_do_resize(window);
3786 if (window->pending_allocation.width == 0 &&
3787 window->pending_allocation.height == 0) {
3788 fprintf(stderr, "Error: Could not draw a surface, "
3789 "most likely due to insufficient disk space in "
3790 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3796 window_schedule_resize(struct window *window, int width, int height)
3798 /* We should probably get these numbers from the theme. */
3799 const int min_width = 200, min_height = 200;
3801 window->pending_allocation.x = 0;
3802 window->pending_allocation.y = 0;
3803 window->pending_allocation.width = width;
3804 window->pending_allocation.height = height;
3806 if (window->min_allocation.width == 0) {
3807 if (width < min_width && window->frame)
3808 window->min_allocation.width = min_width;
3810 window->min_allocation.width = width;
3811 if (height < min_height && window->frame)
3812 window->min_allocation.height = min_height;
3814 window->min_allocation.height = height;
3817 if (window->pending_allocation.width < window->min_allocation.width)
3818 window->pending_allocation.width = window->min_allocation.width;
3819 if (window->pending_allocation.height < window->min_allocation.height)
3820 window->pending_allocation.height = window->min_allocation.height;
3822 window->resize_needed = 1;
3823 window_schedule_redraw(window);
3827 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3829 window_schedule_resize(widget->window, width, height);
3833 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3834 int32_t width, int32_t height)
3836 struct window *window = data;
3838 window_schedule_resize(window, width, height);
3842 handle_surface_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
3844 struct window *window = data;
3845 window_set_maximized(window, 1);
3849 handle_surface_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
3851 struct window *window = data;
3852 window_set_maximized(window, 0);
3856 handle_surface_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
3858 struct window *window = data;
3859 window_set_fullscreen(window, 1);
3863 handle_surface_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
3865 struct window *window = data;
3866 window_set_fullscreen(window, 0);
3870 handle_surface_focused_set(void *data, struct xdg_surface *xdg_surface)
3872 struct window *window = data;
3873 window->focused = 1;
3877 handle_surface_focused_unset(void *data, struct xdg_surface *xdg_surface)
3879 struct window *window = data;
3880 window->focused = 0;
3884 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3886 struct window *window = data;
3887 window_close(window);
3890 static const struct xdg_surface_listener xdg_surface_listener = {
3891 handle_surface_configure,
3892 handle_surface_request_set_maximized,
3893 handle_surface_request_unset_maximized,
3894 handle_surface_request_set_fullscreen,
3895 handle_surface_request_unset_fullscreen,
3896 handle_surface_focused_set,
3897 handle_surface_focused_unset,
3898 handle_surface_delete,
3902 window_sync_transient_for(struct window *window)
3904 struct wl_surface *parent_surface;
3906 if (!window->xdg_surface)
3909 if (window->transient_for)
3910 parent_surface = window->transient_for->main_surface->surface;
3912 parent_surface = NULL;
3914 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3918 window_sync_margin(struct window *window)
3922 if (!window->xdg_surface)
3928 margin = frame_get_shadow_margin(window->frame->frame);
3930 /* Shadow size is the same on every side. */
3931 xdg_surface_set_margin(window->xdg_surface,
3939 window_flush(struct window *window)
3941 struct surface *surface;
3943 if (!window->custom) {
3944 if (!window->xdg_popup && !window->xdg_surface) {
3945 window->xdg_surface = xdg_shell_get_xdg_surface(window->display->xdg_shell,
3946 window->main_surface->surface);
3947 fail_on_null(window->xdg_surface);
3949 xdg_surface_set_user_data(window->xdg_surface, window);
3950 xdg_surface_add_listener(window->xdg_surface,
3951 &xdg_surface_listener, window);
3953 window_sync_transient_for(window);
3954 window_sync_margin(window);
3958 wl_list_for_each(surface, &window->subsurface_list, link) {
3959 if (surface == window->main_surface)
3962 surface_flush(surface);
3965 surface_flush(window->main_surface);
3969 menu_destroy(struct menu *menu)
3971 widget_destroy(menu->widget);
3972 window_destroy(menu->window);
3973 frame_destroy(menu->frame);
3978 window_get_allocation(struct window *window,
3979 struct rectangle *allocation)
3981 *allocation = window->main_surface->allocation;
3985 widget_redraw(struct widget *widget)
3987 struct widget *child;
3989 if (widget->redraw_handler)
3990 widget->redraw_handler(widget, widget->user_data);
3991 wl_list_for_each(child, &widget->child_list, link)
3992 widget_redraw(child);
3996 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3998 struct surface *surface = data;
4000 assert(callback == surface->frame_cb);
4001 DBG_OBJ(callback, "done\n");
4002 wl_callback_destroy(callback);
4003 surface->frame_cb = NULL;
4005 surface->last_time = time;
4007 if (surface->redraw_needed || surface->window->redraw_needed) {
4008 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4009 window_schedule_redraw_task(surface->window);
4013 static const struct wl_callback_listener listener = {
4018 surface_redraw(struct surface *surface)
4020 DBG_OBJ(surface->surface, "begin\n");
4022 if (!surface->window->redraw_needed && !surface->redraw_needed)
4025 /* Whole-window redraw forces a redraw even if the previous has
4026 * not yet hit the screen.
4028 if (surface->frame_cb) {
4029 if (!surface->window->redraw_needed)
4032 DBG_OBJ(surface->frame_cb, "cancelled\n");
4033 wl_callback_destroy(surface->frame_cb);
4036 if (surface->widget->use_cairo &&
4037 !widget_get_cairo_surface(surface->widget)) {
4038 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4042 surface->frame_cb = wl_surface_frame(surface->surface);
4043 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4044 DBG_OBJ(surface->frame_cb, "new\n");
4046 surface->redraw_needed = 0;
4047 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4048 widget_redraw(surface->widget);
4049 DBG_OBJ(surface->surface, "done\n");
4054 idle_redraw(struct task *task, uint32_t events)
4056 struct window *window = container_of(task, struct window, redraw_task);
4057 struct surface *surface;
4061 DBG(" --------- \n");
4063 wl_list_init(&window->redraw_task.link);
4064 window->redraw_task_scheduled = 0;
4066 if (window->resize_needed) {
4067 /* throttle resizing to the main surface display */
4068 if (window->main_surface->frame_cb) {
4069 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4073 idle_resize(window);
4077 if (surface_redraw(window->main_surface) < 0) {
4079 * Only main_surface failure will cause us to undo the resize.
4080 * If sub-surfaces fail, they will just be broken with old
4085 wl_list_for_each(surface, &window->subsurface_list, link) {
4086 if (surface == window->main_surface)
4089 surface_redraw(surface);
4093 window->redraw_needed = 0;
4094 window_flush(window);
4096 wl_list_for_each(surface, &window->subsurface_list, link)
4097 surface_set_synchronized_default(surface);
4099 if (resized && failed) {
4100 /* Restore widget tree to correspond to what is on screen. */
4101 undo_resize(window);
4106 window_schedule_redraw_task(struct window *window)
4108 if (!window->redraw_task_scheduled) {
4109 window->redraw_task.run = idle_redraw;
4110 display_defer(window->display, &window->redraw_task);
4111 window->redraw_task_scheduled = 1;
4116 window_schedule_redraw(struct window *window)
4118 struct surface *surface;
4120 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4122 wl_list_for_each(surface, &window->subsurface_list, link)
4123 surface->redraw_needed = 1;
4125 window_schedule_redraw_task(window);
4129 configure_sync_callback(void *data,
4130 struct wl_callback *callback, uint32_t time)
4132 struct window *window = data;
4134 DBG("scheduling redraw from maximize sync callback\n");
4136 wl_callback_destroy(callback);
4138 window->redraw_task_scheduled = 0;
4139 window_schedule_redraw_task(window);
4142 static struct wl_callback_listener configure_sync_callback_listener = {
4143 configure_sync_callback,
4147 window_delay_redraw(struct window *window)
4149 struct wl_callback *callback;
4151 DBG("delay scheduled redraw for maximize configure\n");
4152 if (window->redraw_task_scheduled)
4153 wl_list_remove(&window->redraw_task.link);
4155 window->redraw_task_scheduled = 1;
4156 callback = wl_display_sync(window->display->display);
4157 wl_callback_add_listener(callback,
4158 &configure_sync_callback_listener, window);
4162 window_is_fullscreen(struct window *window)
4164 return window->fullscreen;
4168 window_set_fullscreen(struct window *window, int fullscreen)
4170 if (!window->xdg_surface)
4173 if (window->fullscreen == fullscreen)
4176 window->fullscreen = fullscreen;
4177 if (window->fullscreen)
4178 xdg_surface_set_fullscreen(window->xdg_surface);
4180 xdg_surface_unset_fullscreen(window->xdg_surface);
4182 window_delay_redraw(window);
4186 window_is_maximized(struct window *window)
4188 return window->maximized;
4192 window_set_maximized(struct window *window, int maximized)
4194 if (!window->xdg_surface)
4197 if (window->maximized == maximized)
4200 window->maximized = maximized;
4201 if (window->maximized)
4202 xdg_surface_set_maximized(window->xdg_surface);
4204 xdg_surface_unset_maximized(window->xdg_surface);
4206 window_delay_redraw(window);
4210 window_set_minimized(struct window *window)
4212 if (!window->xdg_surface)
4215 xdg_surface_set_minimized(window->xdg_surface);
4219 window_set_user_data(struct window *window, void *data)
4221 window->user_data = data;
4225 window_get_user_data(struct window *window)
4227 return window->user_data;
4231 window_set_key_handler(struct window *window,
4232 window_key_handler_t handler)
4234 window->key_handler = handler;
4238 window_set_keyboard_focus_handler(struct window *window,
4239 window_keyboard_focus_handler_t handler)
4241 window->keyboard_focus_handler = handler;
4245 window_set_data_handler(struct window *window, window_data_handler_t handler)
4247 window->data_handler = handler;
4251 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4253 window->drop_handler = handler;
4257 window_set_close_handler(struct window *window,
4258 window_close_handler_t handler)
4260 window->close_handler = handler;
4264 window_set_fullscreen_handler(struct window *window,
4265 window_fullscreen_handler_t handler)
4267 window->fullscreen_handler = handler;
4271 window_set_output_handler(struct window *window,
4272 window_output_handler_t handler)
4274 window->output_handler = handler;
4278 window_set_title(struct window *window, const char *title)
4280 free(window->title);
4281 window->title = strdup(title);
4282 if (window->frame) {
4283 frame_set_title(window->frame->frame, title);
4284 widget_schedule_redraw(window->frame->widget);
4286 if (window->xdg_surface)
4287 xdg_surface_set_title(window->xdg_surface, title);
4291 window_get_title(struct window *window)
4293 return window->title;
4297 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4299 struct text_cursor_position *text_cursor_position =
4300 window->display->text_cursor_position;
4302 if (!text_cursor_position)
4305 text_cursor_position_notify(text_cursor_position,
4306 window->main_surface->surface,
4307 wl_fixed_from_int(x),
4308 wl_fixed_from_int(y));
4312 window_damage(struct window *window, int32_t x, int32_t y,
4313 int32_t width, int32_t height)
4315 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4319 surface_enter(void *data,
4320 struct wl_surface *wl_surface, struct wl_output *wl_output)
4322 struct window *window = data;
4323 struct output *output;
4324 struct output *output_found = NULL;
4325 struct window_output *window_output;
4327 wl_list_for_each(output, &window->display->output_list, link) {
4328 if (output->output == wl_output) {
4329 output_found = output;
4337 window_output = xmalloc(sizeof *window_output);
4338 window_output->output = output_found;
4340 wl_list_insert (&window->window_output_list, &window_output->link);
4342 if (window->output_handler)
4343 window->output_handler(window, output_found, 1,
4348 surface_leave(void *data,
4349 struct wl_surface *wl_surface, struct wl_output *output)
4351 struct window *window = data;
4352 struct window_output *window_output;
4353 struct window_output *window_output_found = NULL;
4355 wl_list_for_each(window_output, &window->window_output_list, link) {
4356 if (window_output->output->output == output) {
4357 window_output_found = window_output;
4362 if (window_output_found) {
4363 wl_list_remove(&window_output_found->link);
4365 if (window->output_handler)
4366 window->output_handler(window, window_output->output,
4367 0, window->user_data);
4369 free(window_output_found);
4373 static const struct wl_surface_listener surface_listener = {
4378 static struct surface *
4379 surface_create(struct window *window)
4381 struct display *display = window->display;
4382 struct surface *surface;
4384 surface = xmalloc(sizeof *surface);
4385 memset(surface, 0, sizeof *surface);
4389 surface->window = window;
4390 surface->surface = wl_compositor_create_surface(display->compositor);
4391 surface->buffer_scale = 1;
4392 wl_surface_add_listener(surface->surface, &surface_listener, window);
4394 wl_list_insert(&window->subsurface_list, &surface->link);
4399 static struct window *
4400 window_create_internal(struct display *display, int custom)
4402 struct window *window;
4403 struct surface *surface;
4405 window = xzalloc(sizeof *window);
4406 wl_list_init(&window->subsurface_list);
4407 window->display = display;
4409 surface = surface_create(window);
4410 window->main_surface = surface;
4412 fail_on_null(display->xdg_shell);
4414 window->custom = custom;
4415 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4417 if (display->argb_device)
4418 #ifdef HAVE_CAIRO_EGL
4419 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4421 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4424 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4426 wl_surface_set_user_data(surface->surface, window);
4427 wl_list_insert(display->window_list.prev, &window->link);
4428 wl_list_init(&window->redraw_task.link);
4430 wl_list_init (&window->window_output_list);
4436 window_create(struct display *display)
4438 return window_create_internal(display, 0);
4442 window_create_custom(struct display *display)
4444 return window_create_internal(display, 1);
4448 window_set_transient_for(struct window *window,
4449 struct window *parent_window)
4451 window->transient_for = parent_window;
4452 window_sync_transient_for(window);
4456 window_get_transient_for(struct window *window)
4458 return window->transient_for;
4462 menu_set_item(struct menu *menu, int sy)
4464 int32_t x, y, width, height;
4467 frame_interior(menu->frame, &x, &y, &width, &height);
4468 next = (sy - y) / 20;
4469 if (menu->current != next) {
4470 menu->current = next;
4471 widget_schedule_redraw(menu->widget);
4476 menu_motion_handler(struct widget *widget,
4477 struct input *input, uint32_t time,
4478 float x, float y, void *data)
4480 struct menu *menu = data;
4482 if (widget == menu->widget)
4483 menu_set_item(data, y);
4485 return CURSOR_LEFT_PTR;
4489 menu_enter_handler(struct widget *widget,
4490 struct input *input, float x, float y, void *data)
4492 struct menu *menu = data;
4494 if (widget == menu->widget)
4495 menu_set_item(data, y);
4497 return CURSOR_LEFT_PTR;
4501 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4503 struct menu *menu = data;
4505 if (widget == menu->widget)
4506 menu_set_item(data, -200);
4510 menu_button_handler(struct widget *widget,
4511 struct input *input, uint32_t time,
4512 uint32_t button, enum wl_pointer_button_state state,
4516 struct menu *menu = data;
4518 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4519 (menu->release_count > 0 || time - menu->time > 500)) {
4520 /* Either relase after press-drag-release or
4521 * click-motion-click. */
4522 menu->func(menu->parent, input,
4523 menu->current, menu->parent->user_data);
4524 input_ungrab(input);
4526 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4527 menu->release_count++;
4532 menu_redraw_handler(struct widget *widget, void *data)
4535 struct menu *menu = data;
4536 int32_t x, y, width, height, i;
4538 cr = widget_cairo_create(widget);
4540 frame_repaint(menu->frame, cr);
4541 frame_interior(menu->frame, &x, &y, &width, &height);
4543 theme_set_background_source(menu->window->display->theme,
4544 cr, THEME_FRAME_ACTIVE);
4545 cairo_rectangle(cr, x, y, width, height);
4548 cairo_select_font_face(cr, "sans",
4549 CAIRO_FONT_SLANT_NORMAL,
4550 CAIRO_FONT_WEIGHT_NORMAL);
4551 cairo_set_font_size(cr, 12);
4553 for (i = 0; i < menu->count; i++) {
4554 if (i == menu->current) {
4555 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4556 cairo_rectangle(cr, x, y + i * 20, width, 20);
4558 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4559 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4560 cairo_show_text(cr, menu->entries[i]);
4562 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4563 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4564 cairo_show_text(cr, menu->entries[i]);
4572 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4574 struct window *window = data;
4575 struct menu *menu = window->main_surface->widget->user_data;
4577 input_ungrab(menu->input);
4581 static const struct xdg_popup_listener xdg_popup_listener = {
4582 handle_popup_popup_done,
4586 window_show_menu(struct display *display,
4587 struct input *input, uint32_t time, struct window *parent,
4588 int32_t x, int32_t y,
4589 menu_func_t func, const char **entries, int count)
4591 struct window *window;
4595 menu = malloc(sizeof *menu);
4599 window = window_create_internal(parent->display, 0);
4605 menu->window = window;
4606 menu->parent = parent;
4607 menu->widget = window_add_widget(menu->window, menu);
4608 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4609 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4610 menu->frame = frame_create(window->display->theme, 0, 0,
4611 FRAME_BUTTON_NONE, NULL);
4612 fail_on_null(menu->frame);
4613 menu->entries = entries;
4614 menu->count = count;
4615 menu->release_count = 0;
4619 menu->input = input;
4623 input_ungrab(input);
4625 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4626 widget_set_enter_handler(menu->widget, menu_enter_handler);
4627 widget_set_leave_handler(menu->widget, menu_leave_handler);
4628 widget_set_motion_handler(menu->widget, menu_motion_handler);
4629 widget_set_button_handler(menu->widget, menu_button_handler);
4631 input_grab(input, menu->widget, 0);
4632 frame_resize_inside(menu->frame, 200, count * 20);
4633 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4634 window_schedule_resize(window, frame_width(menu->frame),
4635 frame_height(menu->frame));
4637 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4639 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4640 window->main_surface->surface,
4641 parent->main_surface->surface,
4643 display_get_serial(window->display),
4647 fail_on_null(window->xdg_popup);
4649 xdg_popup_set_user_data(window->xdg_popup, window);
4650 xdg_popup_add_listener(window->xdg_popup,
4651 &xdg_popup_listener, window);
4655 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4657 window->main_surface->buffer_type = type;
4661 window_set_preferred_format(struct window *window,
4662 enum preferred_format format)
4664 window->preferred_format = format;
4668 window_add_subsurface(struct window *window, void *data,
4669 enum subsurface_mode default_mode)
4671 struct widget *widget;
4672 struct surface *surface;
4673 struct wl_surface *parent;
4674 struct wl_subcompositor *subcompo = window->display->subcompositor;
4676 surface = surface_create(window);
4677 widget = widget_create(window, surface, data);
4678 wl_list_init(&widget->link);
4679 surface->widget = widget;
4681 parent = window->main_surface->surface;
4682 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4685 surface->synchronized = 1;
4687 switch (default_mode) {
4688 case SUBSURFACE_SYNCHRONIZED:
4689 surface->synchronized_default = 1;
4691 case SUBSURFACE_DESYNCHRONIZED:
4692 surface->synchronized_default = 0;
4695 assert(!"bad enum subsurface_mode");
4698 window->resize_needed = 1;
4699 window_schedule_redraw(window);
4705 display_handle_geometry(void *data,
4706 struct wl_output *wl_output,
4709 int physical_height,
4715 struct output *output = data;
4717 output->allocation.x = x;
4718 output->allocation.y = y;
4719 output->transform = transform;
4723 display_handle_done(void *data,
4724 struct wl_output *wl_output)
4729 display_handle_scale(void *data,
4730 struct wl_output *wl_output,
4733 struct output *output = data;
4735 output->scale = scale;
4739 display_handle_mode(void *data,
4740 struct wl_output *wl_output,
4746 struct output *output = data;
4747 struct display *display = output->display;
4749 if (flags & WL_OUTPUT_MODE_CURRENT) {
4750 output->allocation.width = width;
4751 output->allocation.height = height;
4752 if (display->output_configure_handler)
4753 (*display->output_configure_handler)(
4754 output, display->user_data);
4758 static const struct wl_output_listener output_listener = {
4759 display_handle_geometry,
4760 display_handle_mode,
4761 display_handle_done,
4762 display_handle_scale
4766 display_add_output(struct display *d, uint32_t id)
4768 struct output *output;
4770 output = xzalloc(sizeof *output);
4771 output->display = d;
4774 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4775 output->server_output_id = id;
4776 wl_list_insert(d->output_list.prev, &output->link);
4778 wl_output_add_listener(output->output, &output_listener, output);
4782 output_destroy(struct output *output)
4784 if (output->destroy_handler)
4785 (*output->destroy_handler)(output, output->user_data);
4787 wl_output_destroy(output->output);
4788 wl_list_remove(&output->link);
4793 display_destroy_output(struct display *d, uint32_t id)
4795 struct output *output;
4797 wl_list_for_each(output, &d->output_list, link) {
4798 if (output->server_output_id == id) {
4799 output_destroy(output);
4806 display_set_global_handler(struct display *display,
4807 display_global_handler_t handler)
4809 struct global *global;
4811 display->global_handler = handler;
4815 wl_list_for_each(global, &display->global_list, link)
4816 display->global_handler(display,
4817 global->name, global->interface,
4818 global->version, display->user_data);
4822 display_set_global_handler_remove(struct display *display,
4823 display_global_handler_t remove_handler)
4825 display->global_handler_remove = remove_handler;
4826 if (!remove_handler)
4831 display_set_output_configure_handler(struct display *display,
4832 display_output_handler_t handler)
4834 struct output *output;
4836 display->output_configure_handler = handler;
4840 wl_list_for_each(output, &display->output_list, link) {
4841 if (output->allocation.width == 0 &&
4842 output->allocation.height == 0)
4845 (*display->output_configure_handler)(output,
4846 display->user_data);
4851 output_set_user_data(struct output *output, void *data)
4853 output->user_data = data;
4857 output_get_user_data(struct output *output)
4859 return output->user_data;
4863 output_set_destroy_handler(struct output *output,
4864 display_output_handler_t handler)
4866 output->destroy_handler = handler;
4867 /* FIXME: implement this, once we have way to remove outputs */
4871 output_get_allocation(struct output *output, struct rectangle *base)
4873 struct rectangle allocation = output->allocation;
4875 switch (output->transform) {
4876 case WL_OUTPUT_TRANSFORM_90:
4877 case WL_OUTPUT_TRANSFORM_270:
4878 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4879 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4880 /* Swap width and height */
4881 allocation.width = output->allocation.height;
4882 allocation.height = output->allocation.width;
4890 output_get_wl_output(struct output *output)
4892 return output->output;
4895 enum wl_output_transform
4896 output_get_transform(struct output *output)
4898 return output->transform;
4902 output_get_scale(struct output *output)
4904 return output->scale;
4908 fini_xkb(struct input *input)
4910 xkb_state_unref(input->xkb.state);
4911 xkb_map_unref(input->xkb.keymap);
4914 #define MIN(a,b) ((a) < (b) ? a : b)
4917 display_add_input(struct display *d, uint32_t id)
4919 struct input *input;
4921 input = xzalloc(sizeof *input);
4923 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4924 MIN(d->seat_version, 3));
4925 input->touch_focus = NULL;
4926 input->pointer_focus = NULL;
4927 input->keyboard_focus = NULL;
4928 wl_list_init(&input->touch_point_list);
4929 wl_list_insert(d->input_list.prev, &input->link);
4931 wl_seat_add_listener(input->seat, &seat_listener, input);
4932 wl_seat_set_user_data(input->seat, input);
4934 input->data_device =
4935 wl_data_device_manager_get_data_device(d->data_device_manager,
4937 wl_data_device_add_listener(input->data_device, &data_device_listener,
4940 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4942 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4943 TFD_CLOEXEC | TFD_NONBLOCK);
4944 input->repeat_task.run = keyboard_repeat_func;
4945 display_watch_fd(d, input->repeat_timer_fd,
4946 EPOLLIN, &input->repeat_task);
4950 input_destroy(struct input *input)
4952 input_remove_keyboard_focus(input);
4953 input_remove_pointer_focus(input);
4955 if (input->drag_offer)
4956 data_offer_destroy(input->drag_offer);
4958 if (input->selection_offer)
4959 data_offer_destroy(input->selection_offer);
4961 wl_data_device_destroy(input->data_device);
4963 if (input->display->seat_version >= 3) {
4965 wl_pointer_release(input->pointer);
4966 if (input->keyboard)
4967 wl_keyboard_release(input->keyboard);
4972 wl_surface_destroy(input->pointer_surface);
4974 wl_list_remove(&input->link);
4975 wl_seat_destroy(input->seat);
4976 close(input->repeat_timer_fd);
4981 init_workspace_manager(struct display *d, uint32_t id)
4983 d->workspace_manager =
4984 wl_registry_bind(d->registry, id,
4985 &workspace_manager_interface, 1);
4986 if (d->workspace_manager != NULL)
4987 workspace_manager_add_listener(d->workspace_manager,
4988 &workspace_manager_listener,
4993 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4995 struct display *d = data;
4997 if (format == WL_SHM_FORMAT_RGB565)
5001 struct wl_shm_listener shm_listener = {
5006 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5008 xdg_shell_pong(shell, serial);
5011 static const struct xdg_shell_listener xdg_shell_listener = {
5015 #define XDG_VERSION 2 /* The version of xdg-shell that we implement */
5016 #ifdef static_assert
5017 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5018 "Interface version doesn't match implementation version");
5022 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5023 const char *interface, uint32_t version)
5025 struct display *d = data;
5026 struct global *global;
5028 global = xmalloc(sizeof *global);
5030 global->interface = strdup(interface);
5031 global->version = version;
5032 wl_list_insert(d->global_list.prev, &global->link);
5034 if (strcmp(interface, "wl_compositor") == 0) {
5035 d->compositor = wl_registry_bind(registry, id,
5036 &wl_compositor_interface, 3);
5037 } else if (strcmp(interface, "wl_output") == 0) {
5038 display_add_output(d, id);
5039 } else if (strcmp(interface, "wl_seat") == 0) {
5040 d->seat_version = version;
5041 display_add_input(d, id);
5042 } else if (strcmp(interface, "wl_shm") == 0) {
5043 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5044 wl_shm_add_listener(d->shm, &shm_listener, d);
5045 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5046 d->data_device_manager =
5047 wl_registry_bind(registry, id,
5048 &wl_data_device_manager_interface, 1);
5049 } else if (strcmp(interface, "xdg_shell") == 0) {
5050 d->xdg_shell = wl_registry_bind(registry, id,
5051 &xdg_shell_interface, 1);
5052 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5053 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5054 } else if (strcmp(interface, "text_cursor_position") == 0) {
5055 d->text_cursor_position =
5056 wl_registry_bind(registry, id,
5057 &text_cursor_position_interface, 1);
5058 } else if (strcmp(interface, "workspace_manager") == 0) {
5059 init_workspace_manager(d, id);
5060 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5062 wl_registry_bind(registry, id,
5063 &wl_subcompositor_interface, 1);
5066 if (d->global_handler)
5067 d->global_handler(d, id, interface, version, d->user_data);
5071 registry_handle_global_remove(void *data, struct wl_registry *registry,
5074 struct display *d = data;
5075 struct global *global;
5078 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5079 if (global->name != name)
5082 if (strcmp(global->interface, "wl_output") == 0)
5083 display_destroy_output(d, name);
5085 /* XXX: Should destroy remaining bound globals */
5087 if (d->global_handler_remove)
5088 d->global_handler_remove(d, name, global->interface,
5089 global->version, d->user_data);
5091 wl_list_remove(&global->link);
5092 free(global->interface);
5098 display_bind(struct display *display, uint32_t name,
5099 const struct wl_interface *interface, uint32_t version)
5101 return wl_registry_bind(display->registry, name, interface, version);
5104 static const struct wl_registry_listener registry_listener = {
5105 registry_handle_global,
5106 registry_handle_global_remove
5109 #ifdef HAVE_CAIRO_EGL
5111 init_egl(struct display *d)
5113 EGLint major, minor;
5116 #ifdef USE_CAIRO_GLESV2
5117 # define GL_BIT EGL_OPENGL_ES2_BIT
5119 # define GL_BIT EGL_OPENGL_BIT
5122 static const EGLint argb_cfg_attribs[] = {
5123 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5129 EGL_RENDERABLE_TYPE, GL_BIT,
5133 #ifdef USE_CAIRO_GLESV2
5134 static const EGLint context_attribs[] = {
5135 EGL_CONTEXT_CLIENT_VERSION, 2,
5138 EGLint api = EGL_OPENGL_ES_API;
5140 EGLint *context_attribs = NULL;
5141 EGLint api = EGL_OPENGL_API;
5144 d->dpy = eglGetDisplay(d->display);
5145 if (!eglInitialize(d->dpy, &major, &minor)) {
5146 fprintf(stderr, "failed to initialize EGL\n");
5150 if (!eglBindAPI(api)) {
5151 fprintf(stderr, "failed to bind EGL client API\n");
5155 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5156 &d->argb_config, 1, &n) || n != 1) {
5157 fprintf(stderr, "failed to choose argb EGL config\n");
5161 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5162 EGL_NO_CONTEXT, context_attribs);
5163 if (d->argb_ctx == NULL) {
5164 fprintf(stderr, "failed to create EGL context\n");
5168 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5169 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5170 fprintf(stderr, "failed to get cairo EGL argb device\n");
5178 fini_egl(struct display *display)
5180 cairo_device_destroy(display->argb_device);
5182 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5185 eglTerminate(display->dpy);
5191 init_dummy_surface(struct display *display)
5196 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5198 display->dummy_surface =
5199 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5201 display->dummy_surface_data = data;
5205 handle_display_data(struct task *task, uint32_t events)
5207 struct display *display =
5208 container_of(task, struct display, display_task);
5209 struct epoll_event ep;
5212 display->display_fd_events = events;
5214 if (events & EPOLLERR || events & EPOLLHUP) {
5215 display_exit(display);
5219 if (events & EPOLLIN) {
5220 ret = wl_display_dispatch(display->display);
5222 display_exit(display);
5227 if (events & EPOLLOUT) {
5228 ret = wl_display_flush(display->display);
5230 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5231 ep.data.ptr = &display->display_task;
5232 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5233 display->display_fd, &ep);
5234 } else if (ret == -1 && errno != EAGAIN) {
5235 display_exit(display);
5242 log_handler(const char *format, va_list args)
5244 vfprintf(stderr, format, args);
5248 display_create(int *argc, char *argv[])
5252 wl_log_set_handler_client(log_handler);
5254 d = zalloc(sizeof *d);
5258 d->display = wl_display_connect(NULL);
5259 if (d->display == NULL) {
5260 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5265 d->xkb_context = xkb_context_new(0);
5266 if (d->xkb_context == NULL) {
5267 fprintf(stderr, "Failed to create XKB context\n");
5272 d->epoll_fd = os_epoll_create_cloexec();
5273 d->display_fd = wl_display_get_fd(d->display);
5274 d->display_task.run = handle_display_data;
5275 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5278 wl_list_init(&d->deferred_list);
5279 wl_list_init(&d->input_list);
5280 wl_list_init(&d->output_list);
5281 wl_list_init(&d->global_list);
5284 d->workspace_count = 1;
5286 d->registry = wl_display_get_registry(d->display);
5287 wl_registry_add_listener(d->registry, ®istry_listener, d);
5289 if (wl_display_dispatch(d->display) < 0) {
5290 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5294 #ifdef HAVE_CAIRO_EGL
5295 if (init_egl(d) < 0)
5296 fprintf(stderr, "EGL does not seem to work, "
5297 "falling back to software rendering and wl_shm.\n");
5302 d->theme = theme_create();
5304 wl_list_init(&d->window_list);
5306 init_dummy_surface(d);
5312 display_destroy_outputs(struct display *display)
5315 struct output *output;
5317 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5318 output_destroy(output);
5322 display_destroy_inputs(struct display *display)
5325 struct input *input;
5327 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5328 input_destroy(input);
5332 display_destroy(struct display *display)
5334 if (!wl_list_empty(&display->window_list))
5335 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5336 wl_list_length(&display->window_list));
5338 if (!wl_list_empty(&display->deferred_list))
5339 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5341 cairo_surface_destroy(display->dummy_surface);
5342 free(display->dummy_surface_data);
5344 display_destroy_outputs(display);
5345 display_destroy_inputs(display);
5347 xkb_context_unref(display->xkb_context);
5349 theme_destroy(display->theme);
5350 destroy_cursors(display);
5352 #ifdef HAVE_CAIRO_EGL
5353 if (display->argb_device)
5357 if (display->subcompositor)
5358 wl_subcompositor_destroy(display->subcompositor);
5360 if (display->xdg_shell)
5361 xdg_shell_destroy(display->xdg_shell);
5364 wl_shm_destroy(display->shm);
5366 if (display->data_device_manager)
5367 wl_data_device_manager_destroy(display->data_device_manager);
5369 wl_compositor_destroy(display->compositor);
5370 wl_registry_destroy(display->registry);
5372 close(display->epoll_fd);
5374 if (!(display->display_fd_events & EPOLLERR) &&
5375 !(display->display_fd_events & EPOLLHUP))
5376 wl_display_flush(display->display);
5378 wl_display_disconnect(display->display);
5383 display_set_user_data(struct display *display, void *data)
5385 display->user_data = data;
5389 display_get_user_data(struct display *display)
5391 return display->user_data;
5395 display_get_display(struct display *display)
5397 return display->display;
5401 display_has_subcompositor(struct display *display)
5403 if (display->subcompositor)
5406 wl_display_roundtrip(display->display);
5408 return display->subcompositor != NULL;
5412 display_get_cairo_device(struct display *display)
5414 return display->argb_device;
5418 display_get_output(struct display *display)
5420 return container_of(display->output_list.next, struct output, link);
5423 struct wl_compositor *
5424 display_get_compositor(struct display *display)
5426 return display->compositor;
5430 display_get_serial(struct display *display)
5432 return display->serial;
5436 display_get_egl_display(struct display *d)
5441 struct wl_data_source *
5442 display_create_data_source(struct display *display)
5444 return wl_data_device_manager_create_data_source(display->data_device_manager);
5448 display_get_argb_egl_config(struct display *d)
5450 return d->argb_config;
5454 display_acquire_window_surface(struct display *display,
5455 struct window *window,
5458 struct surface *surface = window->main_surface;
5460 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5463 widget_get_cairo_surface(window->main_surface->widget);
5464 return surface->toysurface->acquire(surface->toysurface, ctx);
5468 display_release_window_surface(struct display *display,
5469 struct window *window)
5471 struct surface *surface = window->main_surface;
5473 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5476 surface->toysurface->release(surface->toysurface);
5480 display_defer(struct display *display, struct task *task)
5482 wl_list_insert(&display->deferred_list, &task->link);
5486 display_watch_fd(struct display *display,
5487 int fd, uint32_t events, struct task *task)
5489 struct epoll_event ep;
5493 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5497 display_unwatch_fd(struct display *display, int fd)
5499 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5503 display_run(struct display *display)
5506 struct epoll_event ep[16];
5509 display->running = 1;
5511 while (!wl_list_empty(&display->deferred_list)) {
5512 task = container_of(display->deferred_list.prev,
5514 wl_list_remove(&task->link);
5518 wl_display_dispatch_pending(display->display);
5520 if (!display->running)
5523 ret = wl_display_flush(display->display);
5524 if (ret < 0 && errno == EAGAIN) {
5526 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5527 ep[0].data.ptr = &display->display_task;
5529 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5530 display->display_fd, &ep[0]);
5531 } else if (ret < 0) {
5535 count = epoll_wait(display->epoll_fd,
5536 ep, ARRAY_LENGTH(ep), -1);
5537 for (i = 0; i < count; i++) {
5538 task = ep[i].data.ptr;
5539 task->run(task, ep[i].events);
5545 display_exit(struct display *display)
5547 display->running = 0;
5551 keysym_modifiers_add(struct wl_array *modifiers_map,
5554 size_t len = strlen(name) + 1;
5557 p = wl_array_add(modifiers_map, len);
5562 strncpy(p, name, len);
5565 static xkb_mod_index_t
5566 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5569 xkb_mod_index_t index = 0;
5570 char *p = modifiers_map->data;
5572 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5573 if (strcmp(p, name) == 0)
5580 return XKB_MOD_INVALID;
5584 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5587 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5589 if (index == XKB_MOD_INVALID)
5590 return XKB_MOD_INVALID;
5596 fail_on_null(void *p)
5599 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5609 return fail_on_null(malloc(s));
5615 return fail_on_null(zalloc(s));
5619 xstrdup(const char *s)
5621 return fail_on_null(strdup(s));
5625 xrealloc(char *p, size_t s)
5627 return fail_on_null(realloc(p, s));