2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "xdg-shell-client-protocol.h"
69 #include "text-cursor-position-client-protocol.h"
70 #include "workspaces-client-protocol.h"
71 #include "../shared/os-compatibility.h"
85 struct wl_display *display;
86 struct wl_registry *registry;
87 struct wl_compositor *compositor;
88 struct wl_subcompositor *subcompositor;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
93 struct xdg_shell *xdg_shell;
95 EGLConfig argb_config;
97 cairo_device_t *argb_device;
101 uint32_t display_fd_events;
102 struct task display_task;
105 struct wl_list deferred_list;
109 struct wl_list global_list;
110 struct wl_list window_list;
111 struct wl_list input_list;
112 struct wl_list output_list;
116 struct wl_cursor_theme *cursor_theme;
117 struct wl_cursor **cursors;
119 display_output_handler_t output_configure_handler;
120 display_global_handler_t global_handler;
121 display_global_handler_t global_handler_remove;
125 struct xkb_context *xkb_context;
128 uint32_t workspace_count;
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 void *dummy_surface_data;
138 struct window_output {
139 struct output *output;
145 * Prepare the surface for drawing. Makes sure there is a surface
146 * of the right size available for rendering, and returns it.
147 * dx,dy are the x,y of wl_surface.attach.
148 * width,height are the new buffer size.
149 * If flags has SURFACE_HINT_RESIZE set, the user is
150 * doing continuous resizing.
151 * Returns the Cairo surface to draw to.
153 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
154 int32_t width, int32_t height, uint32_t flags,
155 enum wl_output_transform buffer_transform, int32_t buffer_scale);
158 * Post the surface to the server, returning the server allocation
159 * rectangle. The Cairo surface from prepare() must be destroyed
160 * after calling this.
162 void (*swap)(struct toysurface *base,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale,
164 struct rectangle *server_allocation);
167 * Make the toysurface current with the given EGL context.
168 * Returns 0 on success, and negative of failure.
170 int (*acquire)(struct toysurface *base, EGLContext ctx);
173 * Release the toysurface from the EGL context, returning control
176 void (*release)(struct toysurface *base);
179 * Destroy the toysurface, including the Cairo surface, any
180 * backing storage, and the Wayland protocol objects.
182 void (*destroy)(struct toysurface *base);
186 struct window *window;
188 struct wl_surface *surface;
189 struct wl_subsurface *subsurface;
191 int synchronized_default;
192 struct toysurface *toysurface;
193 struct widget *widget;
195 struct wl_callback *frame_cb;
198 struct rectangle allocation;
199 struct rectangle server_allocation;
201 struct wl_region *input_region;
202 struct wl_region *opaque_region;
204 enum window_buffer_type buffer_type;
205 enum wl_output_transform buffer_transform;
206 int32_t buffer_scale;
208 cairo_surface_t *cairo_surface;
214 struct display *display;
215 struct wl_list window_output_list;
217 struct rectangle saved_allocation;
218 struct rectangle min_allocation;
219 struct rectangle pending_allocation;
222 int redraw_task_scheduled;
223 struct task redraw_task;
233 enum preferred_format preferred_format;
235 window_key_handler_t key_handler;
236 window_keyboard_focus_handler_t keyboard_focus_handler;
237 window_data_handler_t data_handler;
238 window_drop_handler_t drop_handler;
239 window_close_handler_t close_handler;
240 window_fullscreen_handler_t fullscreen_handler;
241 window_output_handler_t output_handler;
243 struct surface *main_surface;
244 struct xdg_surface *xdg_surface;
245 struct xdg_popup *xdg_popup;
247 struct window *transient_for;
249 struct window_frame *frame;
251 /* struct surface::link, contains also main_surface */
252 struct wl_list subsurface_list;
259 struct window *window;
260 struct surface *surface;
261 struct tooltip *tooltip;
262 struct wl_list child_list;
264 struct rectangle allocation;
265 widget_resize_handler_t resize_handler;
266 widget_redraw_handler_t redraw_handler;
267 widget_enter_handler_t enter_handler;
268 widget_leave_handler_t leave_handler;
269 widget_motion_handler_t motion_handler;
270 widget_button_handler_t button_handler;
271 widget_touch_down_handler_t touch_down_handler;
272 widget_touch_up_handler_t touch_up_handler;
273 widget_touch_motion_handler_t touch_motion_handler;
274 widget_touch_frame_handler_t touch_frame_handler;
275 widget_touch_cancel_handler_t touch_cancel_handler;
276 widget_axis_handler_t axis_handler;
281 /* If this is set to false then no cairo surface will be
282 * created before redrawing the surface. This is useful if the
283 * redraw handler is going to do completely custom rendering
284 * such as using EGL directly */
291 struct widget *widget;
296 struct display *display;
297 struct wl_seat *seat;
298 struct wl_pointer *pointer;
299 struct wl_keyboard *keyboard;
300 struct wl_touch *touch;
301 struct wl_list touch_point_list;
302 struct window *pointer_focus;
303 struct window *keyboard_focus;
304 struct window *touch_focus;
306 uint32_t cursor_anim_start;
307 struct wl_callback *cursor_frame_cb;
308 struct wl_surface *pointer_surface;
310 uint32_t pointer_enter_serial;
311 uint32_t cursor_serial;
315 struct widget *focus_widget;
317 uint32_t grab_button;
319 struct wl_data_device *data_device;
320 struct data_offer *drag_offer;
321 struct data_offer *selection_offer;
323 int32_t touch_grab_id;
324 float drag_x, drag_y;
325 struct window *drag_focus;
326 uint32_t drag_enter_serial;
329 struct xkb_keymap *keymap;
330 struct xkb_state *state;
331 xkb_mod_mask_t control_mask;
332 xkb_mod_mask_t alt_mask;
333 xkb_mod_mask_t shift_mask;
336 struct task repeat_task;
340 uint32_t repeat_time;
344 struct display *display;
345 struct wl_output *output;
346 uint32_t server_output_id;
347 struct rectangle allocation;
354 display_output_handler_t destroy_handler;
358 struct window_frame {
359 struct widget *widget;
360 struct widget *child;
365 struct window *window;
366 struct window *parent;
367 struct widget *widget;
370 const char **entries;
379 struct widget *parent;
380 struct widget *widget;
382 struct task tooltip_task;
388 struct wl_shm_pool *pool;
395 CURSOR_DEFAULT = 100,
399 static const cairo_user_data_key_t shm_surface_data_key;
406 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
407 __attribute__ ((format (printf, 4, 5)));
410 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
415 gettimeofday(&tv, NULL);
416 fprintf(stderr, "%8ld.%03ld ",
417 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
420 fprintf(stderr, "%s@%d ",
421 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
423 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
424 fprintf(stderr, "%s ", func);
427 vfprintf(stderr, fmt, ap);
431 #define DBG(fmt, ...) \
432 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
434 #define DBG_OBJ(obj, fmt, ...) \
435 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
439 #define DBG(...) do {} while (0)
440 #define DBG_OBJ(...) do {} while (0)
445 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
449 switch (buffer_transform) {
450 case WL_OUTPUT_TRANSFORM_90:
451 case WL_OUTPUT_TRANSFORM_270:
452 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
453 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
462 *width *= buffer_scale;
463 *height *= buffer_scale;
467 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
471 switch (buffer_transform) {
472 case WL_OUTPUT_TRANSFORM_90:
473 case WL_OUTPUT_TRANSFORM_270:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
484 *width /= buffer_scale;
485 *height /= buffer_scale;
488 #ifdef HAVE_CAIRO_EGL
490 struct egl_window_surface {
491 struct toysurface base;
492 cairo_surface_t *cairo_surface;
493 struct display *display;
494 struct wl_surface *surface;
495 struct wl_egl_window *egl_window;
496 EGLSurface egl_surface;
499 static struct egl_window_surface *
500 to_egl_window_surface(struct toysurface *base)
502 return container_of(base, struct egl_window_surface, base);
505 static cairo_surface_t *
506 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
507 int32_t width, int32_t height, uint32_t flags,
508 enum wl_output_transform buffer_transform, int32_t buffer_scale)
510 struct egl_window_surface *surface = to_egl_window_surface(base);
512 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
514 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
515 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
517 return cairo_surface_reference(surface->cairo_surface);
521 egl_window_surface_swap(struct toysurface *base,
522 enum wl_output_transform buffer_transform, int32_t buffer_scale,
523 struct rectangle *server_allocation)
525 struct egl_window_surface *surface = to_egl_window_surface(base);
527 cairo_gl_surface_swapbuffers(surface->cairo_surface);
528 wl_egl_window_get_attached_size(surface->egl_window,
529 &server_allocation->width,
530 &server_allocation->height);
532 buffer_to_surface_size (buffer_transform, buffer_scale,
533 &server_allocation->width,
534 &server_allocation->height);
538 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
540 struct egl_window_surface *surface = to_egl_window_surface(base);
541 cairo_device_t *device;
543 device = cairo_surface_get_device(surface->cairo_surface);
548 if (device == surface->display->argb_device)
549 ctx = surface->display->argb_ctx;
554 cairo_device_flush(device);
555 cairo_device_acquire(device);
556 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
557 surface->egl_surface, ctx))
558 fprintf(stderr, "failed to make surface current\n");
564 egl_window_surface_release(struct toysurface *base)
566 struct egl_window_surface *surface = to_egl_window_surface(base);
567 cairo_device_t *device;
569 device = cairo_surface_get_device(surface->cairo_surface);
573 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
574 surface->display->argb_ctx))
575 fprintf(stderr, "failed to make context current\n");
577 cairo_device_release(device);
581 egl_window_surface_destroy(struct toysurface *base)
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 struct display *d = surface->display;
586 cairo_surface_destroy(surface->cairo_surface);
587 eglDestroySurface(d->dpy, surface->egl_surface);
588 wl_egl_window_destroy(surface->egl_window);
589 surface->surface = NULL;
594 static struct toysurface *
595 egl_window_surface_create(struct display *display,
596 struct wl_surface *wl_surface,
598 struct rectangle *rectangle)
600 struct egl_window_surface *surface;
602 if (display->dpy == EGL_NO_DISPLAY)
605 surface = calloc(1, sizeof *surface);
609 surface->base.prepare = egl_window_surface_prepare;
610 surface->base.swap = egl_window_surface_swap;
611 surface->base.acquire = egl_window_surface_acquire;
612 surface->base.release = egl_window_surface_release;
613 surface->base.destroy = egl_window_surface_destroy;
615 surface->display = display;
616 surface->surface = wl_surface;
618 surface->egl_window = wl_egl_window_create(surface->surface,
622 surface->egl_surface = eglCreateWindowSurface(display->dpy,
623 display->argb_config,
627 surface->cairo_surface =
628 cairo_gl_surface_create_for_egl(display->argb_device,
629 surface->egl_surface,
633 return &surface->base;
638 static struct toysurface *
639 egl_window_surface_create(struct display *display,
640 struct wl_surface *wl_surface,
642 struct rectangle *rectangle)
649 struct shm_surface_data {
650 struct wl_buffer *buffer;
651 struct shm_pool *pool;
655 display_get_buffer_for_surface(struct display *display,
656 cairo_surface_t *surface)
658 struct shm_surface_data *data;
660 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
666 shm_pool_destroy(struct shm_pool *pool);
669 shm_surface_data_destroy(void *p)
671 struct shm_surface_data *data = p;
673 wl_buffer_destroy(data->buffer);
675 shm_pool_destroy(data->pool);
680 static struct wl_shm_pool *
681 make_shm_pool(struct display *display, int size, void **data)
683 struct wl_shm_pool *pool;
686 fd = os_create_anonymous_file(size);
688 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
693 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
694 if (*data == MAP_FAILED) {
695 fprintf(stderr, "mmap failed: %m\n");
700 pool = wl_shm_create_pool(display->shm, fd, size);
707 static struct shm_pool *
708 shm_pool_create(struct display *display, size_t size)
710 struct shm_pool *pool = malloc(sizeof *pool);
715 pool->pool = make_shm_pool(display, size, &pool->data);
728 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
730 if (pool->used + size > pool->size)
733 *offset = pool->used;
736 return (char *) pool->data + *offset;
739 /* destroy the pool. this does not unmap the memory though */
741 shm_pool_destroy(struct shm_pool *pool)
743 munmap(pool->data, pool->size);
744 wl_shm_pool_destroy(pool->pool);
748 /* Start allocating from the beginning of the pool again */
750 shm_pool_reset(struct shm_pool *pool)
756 data_length_for_shm_surface(struct rectangle *rect)
760 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
762 return stride * rect->height;
765 static cairo_surface_t *
766 display_create_shm_surface_from_pool(struct display *display,
767 struct rectangle *rectangle,
768 uint32_t flags, struct shm_pool *pool)
770 struct shm_surface_data *data;
772 cairo_surface_t *surface;
773 cairo_format_t cairo_format;
774 int stride, length, offset;
777 data = malloc(sizeof *data);
781 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
782 cairo_format = CAIRO_FORMAT_RGB16_565;
784 cairo_format = CAIRO_FORMAT_ARGB32;
786 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
787 length = stride * rectangle->height;
789 map = shm_pool_allocate(pool, length, &offset);
796 surface = cairo_image_surface_create_for_data (map,
802 cairo_surface_set_user_data(surface, &shm_surface_data_key,
803 data, shm_surface_data_destroy);
805 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
806 format = WL_SHM_FORMAT_RGB565;
808 if (flags & SURFACE_OPAQUE)
809 format = WL_SHM_FORMAT_XRGB8888;
811 format = WL_SHM_FORMAT_ARGB8888;
814 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
822 static cairo_surface_t *
823 display_create_shm_surface(struct display *display,
824 struct rectangle *rectangle, uint32_t flags,
825 struct shm_pool *alternate_pool,
826 struct shm_surface_data **data_ret)
828 struct shm_surface_data *data;
829 struct shm_pool *pool;
830 cairo_surface_t *surface;
832 if (alternate_pool) {
833 shm_pool_reset(alternate_pool);
834 surface = display_create_shm_surface_from_pool(display,
839 data = cairo_surface_get_user_data(surface,
840 &shm_surface_data_key);
845 pool = shm_pool_create(display,
846 data_length_for_shm_surface(rectangle));
851 display_create_shm_surface_from_pool(display, rectangle,
855 shm_pool_destroy(pool);
859 /* make sure we destroy the pool when the surface is destroyed */
860 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
871 check_size(struct rectangle *rect)
873 if (rect->width && rect->height)
876 fprintf(stderr, "tried to create surface of "
877 "width: %d, height: %d\n", rect->width, rect->height);
882 display_create_surface(struct display *display,
883 struct wl_surface *surface,
884 struct rectangle *rectangle,
887 if (check_size(rectangle) < 0)
890 assert(flags & SURFACE_SHM);
891 return display_create_shm_surface(display, rectangle, flags,
895 struct shm_surface_leaf {
896 cairo_surface_t *cairo_surface;
897 /* 'data' is automatically destroyed, when 'cairo_surface' is */
898 struct shm_surface_data *data;
900 struct shm_pool *resize_pool;
905 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
907 if (leaf->cairo_surface)
908 cairo_surface_destroy(leaf->cairo_surface);
909 /* leaf->data already destroyed via cairo private */
911 if (leaf->resize_pool)
912 shm_pool_destroy(leaf->resize_pool);
914 memset(leaf, 0, sizeof *leaf);
920 struct toysurface base;
921 struct display *display;
922 struct wl_surface *surface;
926 struct shm_surface_leaf leaf[MAX_LEAVES];
927 struct shm_surface_leaf *current;
930 static struct shm_surface *
931 to_shm_surface(struct toysurface *base)
933 return container_of(base, struct shm_surface, base);
937 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
940 struct shm_surface_leaf *leaf;
941 char bufs[MAX_LEAVES + 1];
944 for (i = 0; i < MAX_LEAVES; i++) {
945 leaf = &surface->leaf[i];
949 else if (leaf->cairo_surface)
955 bufs[MAX_LEAVES] = '\0';
956 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
961 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
963 struct shm_surface *surface = data;
964 struct shm_surface_leaf *leaf;
968 shm_surface_buffer_state_debug(surface, "buffer_release before");
970 for (i = 0; i < MAX_LEAVES; i++) {
971 leaf = &surface->leaf[i];
972 if (leaf->data && leaf->data->buffer == buffer) {
977 assert(i < MAX_LEAVES && "unknown buffer released");
979 /* Leave one free leaf with storage, release others */
981 for (i = 0; i < MAX_LEAVES; i++) {
982 leaf = &surface->leaf[i];
984 if (!leaf->cairo_surface || leaf->busy)
990 shm_surface_leaf_release(leaf);
993 shm_surface_buffer_state_debug(surface, "buffer_release after");
996 static const struct wl_buffer_listener shm_surface_buffer_listener = {
997 shm_surface_buffer_release
1000 static cairo_surface_t *
1001 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1002 int32_t width, int32_t height, uint32_t flags,
1003 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1005 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1006 struct shm_surface *surface = to_shm_surface(base);
1007 struct rectangle rect = { 0};
1008 struct shm_surface_leaf *leaf = NULL;
1014 /* pick a free buffer, preferrably one that already has storage */
1015 for (i = 0; i < MAX_LEAVES; i++) {
1016 if (surface->leaf[i].busy)
1019 if (!leaf || surface->leaf[i].cairo_surface)
1020 leaf = &surface->leaf[i];
1022 DBG_OBJ(surface->surface, "pick leaf %d\n",
1023 (int)(leaf - &surface->leaf[0]));
1026 fprintf(stderr, "%s: all buffers are held by the server.\n",
1032 if (!resize_hint && leaf->resize_pool) {
1033 cairo_surface_destroy(leaf->cairo_surface);
1034 leaf->cairo_surface = NULL;
1035 shm_pool_destroy(leaf->resize_pool);
1036 leaf->resize_pool = NULL;
1039 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1041 if (leaf->cairo_surface &&
1042 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1043 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1046 if (leaf->cairo_surface)
1047 cairo_surface_destroy(leaf->cairo_surface);
1049 #ifdef USE_RESIZE_POOL
1050 if (resize_hint && !leaf->resize_pool) {
1051 /* Create a big pool to allocate from, while continuously
1052 * resizing. Mmapping a new pool in the server
1053 * is relatively expensive, so reusing a pool performs
1054 * better, but may temporarily reserve unneeded memory.
1056 /* We should probably base this number on the output size. */
1057 leaf->resize_pool = shm_pool_create(surface->display,
1063 rect.height = height;
1065 leaf->cairo_surface =
1066 display_create_shm_surface(surface->display, &rect,
1070 if (!leaf->cairo_surface)
1073 wl_buffer_add_listener(leaf->data->buffer,
1074 &shm_surface_buffer_listener, surface);
1077 surface->current = leaf;
1079 return cairo_surface_reference(leaf->cairo_surface);
1083 shm_surface_swap(struct toysurface *base,
1084 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1085 struct rectangle *server_allocation)
1087 struct shm_surface *surface = to_shm_surface(base);
1088 struct shm_surface_leaf *leaf = surface->current;
1090 server_allocation->width =
1091 cairo_image_surface_get_width(leaf->cairo_surface);
1092 server_allocation->height =
1093 cairo_image_surface_get_height(leaf->cairo_surface);
1095 buffer_to_surface_size (buffer_transform, buffer_scale,
1096 &server_allocation->width,
1097 &server_allocation->height);
1099 wl_surface_attach(surface->surface, leaf->data->buffer,
1100 surface->dx, surface->dy);
1101 wl_surface_damage(surface->surface, 0, 0,
1102 server_allocation->width, server_allocation->height);
1103 wl_surface_commit(surface->surface);
1105 DBG_OBJ(surface->surface, "leaf %d busy\n",
1106 (int)(leaf - &surface->leaf[0]));
1109 surface->current = NULL;
1113 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1119 shm_surface_release(struct toysurface *base)
1124 shm_surface_destroy(struct toysurface *base)
1126 struct shm_surface *surface = to_shm_surface(base);
1129 for (i = 0; i < MAX_LEAVES; i++)
1130 shm_surface_leaf_release(&surface->leaf[i]);
1135 static struct toysurface *
1136 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1137 uint32_t flags, struct rectangle *rectangle)
1139 struct shm_surface *surface;
1140 DBG_OBJ(wl_surface, "\n");
1142 surface = xmalloc(sizeof *surface);
1143 memset(surface, 0, sizeof *surface);
1148 surface->base.prepare = shm_surface_prepare;
1149 surface->base.swap = shm_surface_swap;
1150 surface->base.acquire = shm_surface_acquire;
1151 surface->base.release = shm_surface_release;
1152 surface->base.destroy = shm_surface_destroy;
1154 surface->display = display;
1155 surface->surface = wl_surface;
1156 surface->flags = flags;
1158 return &surface->base;
1162 * The following correspondences between file names and cursors was copied
1163 * from: https://bugs.kde.org/attachment.cgi?id=67313
1166 static const char *bottom_left_corners[] = {
1167 "bottom_left_corner",
1172 static const char *bottom_right_corners[] = {
1173 "bottom_right_corner",
1178 static const char *bottom_sides[] = {
1184 static const char *grabbings[] = {
1187 "208530c400c041818281048008011002"
1190 static const char *left_ptrs[] = {
1197 static const char *left_sides[] = {
1203 static const char *right_sides[] = {
1209 static const char *top_left_corners[] = {
1215 static const char *top_right_corners[] = {
1221 static const char *top_sides[] = {
1227 static const char *xterms[] = {
1233 static const char *hand1s[] = {
1237 "e29285e634086352946a0e7090d73106"
1240 static const char *watches[] = {
1243 "0426c94ea35c87780ff01dc239897213"
1246 struct cursor_alternatives {
1251 static const struct cursor_alternatives cursors[] = {
1252 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1253 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1254 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1255 {grabbings, ARRAY_LENGTH(grabbings)},
1256 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1257 {left_sides, ARRAY_LENGTH(left_sides)},
1258 {right_sides, ARRAY_LENGTH(right_sides)},
1259 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1260 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1261 {top_sides, ARRAY_LENGTH(top_sides)},
1262 {xterms, ARRAY_LENGTH(xterms)},
1263 {hand1s, ARRAY_LENGTH(hand1s)},
1264 {watches, ARRAY_LENGTH(watches)},
1268 create_cursors(struct display *display)
1270 struct weston_config *config;
1271 struct weston_config_section *s;
1275 struct wl_cursor *cursor;
1277 config = weston_config_parse("weston.ini");
1278 s = weston_config_get_section(config, "shell", NULL, NULL);
1279 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1280 weston_config_section_get_int(s, "cursor-size", &size, 32);
1281 weston_config_destroy(config);
1283 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1284 if (!display->cursor_theme) {
1285 fprintf(stderr, "could not load theme '%s'\n", theme);
1290 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1292 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1294 for (j = 0; !cursor && j < cursors[i].count; ++j)
1295 cursor = wl_cursor_theme_get_cursor(
1296 display->cursor_theme, cursors[i].names[j]);
1299 fprintf(stderr, "could not load cursor '%s'\n",
1300 cursors[i].names[0]);
1302 display->cursors[i] = cursor;
1307 destroy_cursors(struct display *display)
1309 wl_cursor_theme_destroy(display->cursor_theme);
1310 free(display->cursors);
1313 struct wl_cursor_image *
1314 display_get_pointer_image(struct display *display, int pointer)
1316 struct wl_cursor *cursor = display->cursors[pointer];
1318 return cursor ? cursor->images[0] : NULL;
1322 surface_flush(struct surface *surface)
1324 if (!surface->cairo_surface)
1327 if (surface->opaque_region) {
1328 wl_surface_set_opaque_region(surface->surface,
1329 surface->opaque_region);
1330 wl_region_destroy(surface->opaque_region);
1331 surface->opaque_region = NULL;
1334 if (surface->input_region) {
1335 wl_surface_set_input_region(surface->surface,
1336 surface->input_region);
1337 wl_region_destroy(surface->input_region);
1338 surface->input_region = NULL;
1341 surface->toysurface->swap(surface->toysurface,
1342 surface->buffer_transform, surface->buffer_scale,
1343 &surface->server_allocation);
1345 cairo_surface_destroy(surface->cairo_surface);
1346 surface->cairo_surface = NULL;
1350 window_has_focus(struct window *window)
1352 return window->focused;
1356 window_close(struct window *window)
1358 if (window->close_handler)
1359 window->close_handler(window->user_data);
1361 display_exit(window->display);
1365 window_get_display(struct window *window)
1367 return window->display;
1371 surface_create_surface(struct surface *surface, uint32_t flags)
1373 struct display *display = surface->window->display;
1374 struct rectangle allocation = surface->allocation;
1376 if (!surface->toysurface && display->dpy &&
1377 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1378 surface->toysurface =
1379 egl_window_surface_create(display,
1385 if (!surface->toysurface)
1386 surface->toysurface = shm_surface_create(display,
1388 flags, &allocation);
1390 surface->cairo_surface = surface->toysurface->prepare(
1391 surface->toysurface, 0, 0,
1392 allocation.width, allocation.height, flags,
1393 surface->buffer_transform, surface->buffer_scale);
1397 window_create_main_surface(struct window *window)
1399 struct surface *surface = window->main_surface;
1402 if (window->resizing)
1403 flags |= SURFACE_HINT_RESIZE;
1405 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1406 flags |= SURFACE_HINT_RGB565;
1408 surface_create_surface(surface, flags);
1412 window_get_buffer_transform(struct window *window)
1414 return window->main_surface->buffer_transform;
1418 window_set_buffer_transform(struct window *window,
1419 enum wl_output_transform transform)
1421 window->main_surface->buffer_transform = transform;
1422 wl_surface_set_buffer_transform(window->main_surface->surface,
1427 window_set_buffer_scale(struct window *window,
1430 window->main_surface->buffer_scale = scale;
1431 wl_surface_set_buffer_scale(window->main_surface->surface,
1436 window_get_buffer_scale(struct window *window)
1438 return window->main_surface->buffer_scale;
1442 window_get_output_scale(struct window *window)
1444 struct window_output *window_output;
1445 struct window_output *window_output_tmp;
1448 wl_list_for_each_safe(window_output, window_output_tmp,
1449 &window->window_output_list, link) {
1450 if (window_output->output->scale > scale)
1451 scale = window_output->output->scale;
1457 static void window_frame_destroy(struct window_frame *frame);
1460 surface_destroy(struct surface *surface)
1462 if (surface->frame_cb)
1463 wl_callback_destroy(surface->frame_cb);
1465 if (surface->input_region)
1466 wl_region_destroy(surface->input_region);
1468 if (surface->opaque_region)
1469 wl_region_destroy(surface->opaque_region);
1471 if (surface->subsurface)
1472 wl_subsurface_destroy(surface->subsurface);
1474 wl_surface_destroy(surface->surface);
1476 if (surface->toysurface)
1477 surface->toysurface->destroy(surface->toysurface);
1479 wl_list_remove(&surface->link);
1484 window_destroy(struct window *window)
1486 struct display *display = window->display;
1487 struct input *input;
1488 struct window_output *window_output;
1489 struct window_output *window_output_tmp;
1491 wl_list_remove(&window->redraw_task.link);
1493 wl_list_for_each(input, &display->input_list, link) {
1494 if (input->touch_focus == window)
1495 input->touch_focus = NULL;
1496 if (input->pointer_focus == window)
1497 input->pointer_focus = NULL;
1498 if (input->keyboard_focus == window)
1499 input->keyboard_focus = NULL;
1500 if (input->focus_widget &&
1501 input->focus_widget->window == window)
1502 input->focus_widget = NULL;
1505 wl_list_for_each_safe(window_output, window_output_tmp,
1506 &window->window_output_list, link) {
1507 free (window_output);
1511 window_frame_destroy(window->frame);
1513 if (window->xdg_surface)
1514 xdg_surface_destroy(window->xdg_surface);
1515 if (window->xdg_popup)
1516 xdg_popup_destroy(window->xdg_popup);
1518 surface_destroy(window->main_surface);
1520 wl_list_remove(&window->link);
1522 free(window->title);
1526 static struct widget *
1527 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1529 struct widget *child, *target;
1531 wl_list_for_each(child, &widget->child_list, link) {
1532 target = widget_find_widget(child, x, y);
1537 if (widget->allocation.x <= x &&
1538 x < widget->allocation.x + widget->allocation.width &&
1539 widget->allocation.y <= y &&
1540 y < widget->allocation.y + widget->allocation.height) {
1547 static struct widget *
1548 window_find_widget(struct window *window, int32_t x, int32_t y)
1550 struct surface *surface;
1551 struct widget *widget;
1553 wl_list_for_each(surface, &window->subsurface_list, link) {
1554 widget = widget_find_widget(surface->widget, x, y);
1562 static struct widget *
1563 widget_create(struct window *window, struct surface *surface, void *data)
1565 struct widget *widget;
1567 widget = xzalloc(sizeof *widget);
1568 widget->window = window;
1569 widget->surface = surface;
1570 widget->user_data = data;
1571 widget->allocation = surface->allocation;
1572 wl_list_init(&widget->child_list);
1574 widget->tooltip = NULL;
1575 widget->tooltip_count = 0;
1576 widget->default_cursor = CURSOR_LEFT_PTR;
1577 widget->use_cairo = 1;
1583 window_add_widget(struct window *window, void *data)
1585 struct widget *widget;
1587 widget = widget_create(window, window->main_surface, data);
1588 wl_list_init(&widget->link);
1589 window->main_surface->widget = widget;
1595 widget_add_widget(struct widget *parent, void *data)
1597 struct widget *widget;
1599 widget = widget_create(parent->window, parent->surface, data);
1600 wl_list_insert(parent->child_list.prev, &widget->link);
1606 widget_destroy(struct widget *widget)
1608 struct display *display = widget->window->display;
1609 struct surface *surface = widget->surface;
1610 struct input *input;
1612 /* Destroy the sub-surface along with the root widget */
1613 if (surface->widget == widget && surface->subsurface)
1614 surface_destroy(widget->surface);
1616 if (widget->tooltip)
1617 widget_destroy_tooltip(widget);
1619 wl_list_for_each(input, &display->input_list, link) {
1620 if (input->focus_widget == widget)
1621 input->focus_widget = NULL;
1624 wl_list_remove(&widget->link);
1629 widget_set_default_cursor(struct widget *widget, int cursor)
1631 widget->default_cursor = cursor;
1635 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1637 *allocation = widget->allocation;
1641 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1643 widget->allocation.width = width;
1644 widget->allocation.height = height;
1648 widget_set_allocation(struct widget *widget,
1649 int32_t x, int32_t y, int32_t width, int32_t height)
1651 widget->allocation.x = x;
1652 widget->allocation.y = y;
1653 widget_set_size(widget, width, height);
1657 widget_set_transparent(struct widget *widget, int transparent)
1659 widget->opaque = !transparent;
1663 widget_get_user_data(struct widget *widget)
1665 return widget->user_data;
1668 static cairo_surface_t *
1669 widget_get_cairo_surface(struct widget *widget)
1671 struct surface *surface = widget->surface;
1672 struct window *window = widget->window;
1674 assert(widget->use_cairo);
1676 if (!surface->cairo_surface) {
1677 if (surface == window->main_surface)
1678 window_create_main_surface(window);
1680 surface_create_surface(surface, 0);
1683 return surface->cairo_surface;
1687 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1689 struct surface *surface = widget->surface;
1692 enum wl_output_transform transform;
1693 int surface_width, surface_height;
1694 int translate_x, translate_y;
1697 surface_width = surface->allocation.width;
1698 surface_height = surface->allocation.height;
1700 transform = surface->buffer_transform;
1701 scale = surface->buffer_scale;
1703 switch (transform) {
1704 case WL_OUTPUT_TRANSFORM_FLIPPED:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1706 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1707 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1708 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1711 cairo_matrix_init_identity(&m);
1715 switch (transform) {
1716 case WL_OUTPUT_TRANSFORM_NORMAL:
1722 case WL_OUTPUT_TRANSFORM_FLIPPED:
1724 translate_x = surface_width;
1727 case WL_OUTPUT_TRANSFORM_90:
1729 translate_x = surface_height;
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1734 translate_x = surface_height;
1735 translate_y = surface_width;
1737 case WL_OUTPUT_TRANSFORM_180:
1739 translate_x = surface_width;
1740 translate_y = surface_height;
1742 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1745 translate_y = surface_height;
1747 case WL_OUTPUT_TRANSFORM_270:
1748 angle = M_PI + M_PI_2;
1750 translate_y = surface_width;
1752 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1753 angle = M_PI + M_PI_2;
1759 cairo_scale(cr, scale, scale);
1760 cairo_translate(cr, translate_x, translate_y);
1761 cairo_rotate(cr, angle);
1762 cairo_transform(cr, &m);
1766 widget_cairo_create(struct widget *widget)
1768 struct surface *surface = widget->surface;
1769 cairo_surface_t *cairo_surface;
1772 cairo_surface = widget_get_cairo_surface(widget);
1773 cr = cairo_create(cairo_surface);
1775 widget_cairo_update_transform(widget, cr);
1777 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1783 widget_get_wl_surface(struct widget *widget)
1785 return widget->surface->surface;
1788 struct wl_subsurface *
1789 widget_get_wl_subsurface(struct widget *widget)
1791 return widget->surface->subsurface;
1795 widget_get_last_time(struct widget *widget)
1797 return widget->surface->last_time;
1801 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1803 struct wl_compositor *comp = widget->window->display->compositor;
1804 struct surface *surface = widget->surface;
1806 if (!surface->input_region)
1807 surface->input_region = wl_compositor_create_region(comp);
1810 wl_region_add(surface->input_region,
1811 rect->x, rect->y, rect->width, rect->height);
1816 widget_set_resize_handler(struct widget *widget,
1817 widget_resize_handler_t handler)
1819 widget->resize_handler = handler;
1823 widget_set_redraw_handler(struct widget *widget,
1824 widget_redraw_handler_t handler)
1826 widget->redraw_handler = handler;
1830 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1832 widget->enter_handler = handler;
1836 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1838 widget->leave_handler = handler;
1842 widget_set_motion_handler(struct widget *widget,
1843 widget_motion_handler_t handler)
1845 widget->motion_handler = handler;
1849 widget_set_button_handler(struct widget *widget,
1850 widget_button_handler_t handler)
1852 widget->button_handler = handler;
1856 widget_set_touch_up_handler(struct widget *widget,
1857 widget_touch_up_handler_t handler)
1859 widget->touch_up_handler = handler;
1863 widget_set_touch_down_handler(struct widget *widget,
1864 widget_touch_down_handler_t handler)
1866 widget->touch_down_handler = handler;
1870 widget_set_touch_motion_handler(struct widget *widget,
1871 widget_touch_motion_handler_t handler)
1873 widget->touch_motion_handler = handler;
1877 widget_set_touch_frame_handler(struct widget *widget,
1878 widget_touch_frame_handler_t handler)
1880 widget->touch_frame_handler = handler;
1884 widget_set_touch_cancel_handler(struct widget *widget,
1885 widget_touch_cancel_handler_t handler)
1887 widget->touch_cancel_handler = handler;
1891 widget_set_axis_handler(struct widget *widget,
1892 widget_axis_handler_t handler)
1894 widget->axis_handler = handler;
1898 window_schedule_redraw_task(struct window *window);
1901 widget_schedule_redraw(struct widget *widget)
1903 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1904 widget->surface->redraw_needed = 1;
1905 window_schedule_redraw_task(widget->window);
1909 widget_set_use_cairo(struct widget *widget,
1912 widget->use_cairo = use_cairo;
1916 window_get_surface(struct window *window)
1918 cairo_surface_t *cairo_surface;
1920 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1922 return cairo_surface_reference(cairo_surface);
1926 window_get_wl_surface(struct window *window)
1928 return window->main_surface->surface;
1932 tooltip_redraw_handler(struct widget *widget, void *data)
1935 const int32_t r = 3;
1936 struct tooltip *tooltip = data;
1937 int32_t width, height;
1939 cr = widget_cairo_create(widget);
1940 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1942 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1945 width = widget->allocation.width;
1946 height = widget->allocation.height;
1947 rounded_rect(cr, 0, 0, width, height, r);
1949 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1950 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1953 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1954 cairo_move_to(cr, 10, 16);
1955 cairo_show_text(cr, tooltip->entry);
1959 static cairo_text_extents_t
1960 get_text_extents(struct display *display, struct tooltip *tooltip)
1963 cairo_text_extents_t extents;
1965 /* Use the dummy_surface because tooltip's surface was not
1966 * created yet, and parent does not have a valid surface
1967 * outside repaint, either.
1969 cr = cairo_create(display->dummy_surface);
1970 cairo_text_extents(cr, tooltip->entry, &extents);
1977 window_create_tooltip(struct tooltip *tooltip)
1979 struct widget *parent = tooltip->parent;
1980 struct display *display = parent->window->display;
1981 const int offset_y = 27;
1982 const int margin = 3;
1983 cairo_text_extents_t extents;
1985 if (tooltip->widget)
1988 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1990 extents = get_text_extents(display, tooltip);
1991 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1992 widget_set_allocation(tooltip->widget,
1993 tooltip->x, tooltip->y + offset_y,
1994 extents.width + 20, 20 + margin * 2);
2000 widget_destroy_tooltip(struct widget *parent)
2002 struct tooltip *tooltip = parent->tooltip;
2004 parent->tooltip_count = 0;
2008 if (tooltip->widget) {
2009 widget_destroy(tooltip->widget);
2010 tooltip->widget = NULL;
2013 close(tooltip->tooltip_fd);
2014 free(tooltip->entry);
2016 parent->tooltip = NULL;
2020 tooltip_func(struct task *task, uint32_t events)
2022 struct tooltip *tooltip =
2023 container_of(task, struct tooltip, tooltip_task);
2026 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2028 window_create_tooltip(tooltip);
2031 #define TOOLTIP_TIMEOUT 500
2033 tooltip_timer_reset(struct tooltip *tooltip)
2035 struct itimerspec its;
2037 its.it_interval.tv_sec = 0;
2038 its.it_interval.tv_nsec = 0;
2039 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2040 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2041 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2042 fprintf(stderr, "could not set timerfd\n: %m");
2050 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2052 struct tooltip *tooltip = parent->tooltip;
2054 parent->tooltip_count++;
2058 tooltip_timer_reset(tooltip);
2062 /* the handler might be triggered too fast via input device motion, so
2063 * we need this check here to make sure tooltip is fully initialized */
2064 if (parent->tooltip_count > 1)
2067 tooltip = malloc(sizeof *tooltip);
2071 parent->tooltip = tooltip;
2072 tooltip->parent = parent;
2073 tooltip->widget = NULL;
2076 tooltip->entry = strdup(entry);
2077 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2078 if (tooltip->tooltip_fd < 0) {
2079 fprintf(stderr, "could not create timerfd\n: %m");
2083 tooltip->tooltip_task.run = tooltip_func;
2084 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2085 EPOLLIN, &tooltip->tooltip_task);
2086 tooltip_timer_reset(tooltip);
2092 workspace_manager_state(void *data,
2093 struct workspace_manager *workspace_manager,
2097 struct display *display = data;
2099 display->workspace = current;
2100 display->workspace_count = count;
2103 static const struct workspace_manager_listener workspace_manager_listener = {
2104 workspace_manager_state
2108 frame_resize_handler(struct widget *widget,
2109 int32_t width, int32_t height, void *data)
2111 struct window_frame *frame = data;
2112 struct widget *child = frame->child;
2113 struct rectangle interior;
2114 struct rectangle input;
2115 struct rectangle opaque;
2117 if (widget->window->fullscreen) {
2120 interior.width = width;
2121 interior.height = height;
2123 if (widget->window->maximized) {
2124 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2126 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2129 frame_resize(frame->frame, width, height);
2130 frame_interior(frame->frame, &interior.x, &interior.y,
2131 &interior.width, &interior.height);
2134 widget_set_allocation(child, interior.x, interior.y,
2135 interior.width, interior.height);
2137 if (child->resize_handler) {
2138 child->resize_handler(child, interior.width, interior.height,
2141 if (widget->window->fullscreen) {
2142 width = child->allocation.width;
2143 height = child->allocation.height;
2145 frame_resize_inside(frame->frame,
2146 child->allocation.width,
2147 child->allocation.height);
2148 width = frame_width(frame->frame);
2149 height = frame_height(frame->frame);
2153 widget_set_allocation(widget, 0, 0, width, height);
2155 widget->surface->input_region =
2156 wl_compositor_create_region(widget->window->display->compositor);
2157 if (!widget->window->fullscreen) {
2158 frame_input_rect(frame->frame, &input.x, &input.y,
2159 &input.width, &input.height);
2160 wl_region_add(widget->surface->input_region,
2161 input.x, input.y, input.width, input.height);
2163 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2166 widget_set_allocation(widget, 0, 0, width, height);
2168 if (child->opaque) {
2169 if (!widget->window->fullscreen) {
2170 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2171 &opaque.width, &opaque.height);
2173 wl_region_add(widget->surface->opaque_region,
2175 opaque.width, opaque.height);
2177 wl_region_add(widget->surface->opaque_region,
2178 0, 0, width, height);
2183 widget_schedule_redraw(widget);
2187 frame_redraw_handler(struct widget *widget, void *data)
2190 struct window_frame *frame = data;
2191 struct window *window = widget->window;
2193 if (window->fullscreen)
2196 if (window->focused) {
2197 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2199 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2202 cr = widget_cairo_create(widget);
2204 frame_repaint(frame->frame, cr);
2210 frame_get_pointer_image_for_location(struct window_frame *frame,
2211 enum theme_location location)
2213 struct window *window = frame->widget->window;
2216 return CURSOR_LEFT_PTR;
2219 case THEME_LOCATION_RESIZING_TOP:
2221 case THEME_LOCATION_RESIZING_BOTTOM:
2222 return CURSOR_BOTTOM;
2223 case THEME_LOCATION_RESIZING_LEFT:
2225 case THEME_LOCATION_RESIZING_RIGHT:
2226 return CURSOR_RIGHT;
2227 case THEME_LOCATION_RESIZING_TOP_LEFT:
2228 return CURSOR_TOP_LEFT;
2229 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2230 return CURSOR_TOP_RIGHT;
2231 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2232 return CURSOR_BOTTOM_LEFT;
2233 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2234 return CURSOR_BOTTOM_RIGHT;
2235 case THEME_LOCATION_EXTERIOR:
2236 case THEME_LOCATION_TITLEBAR:
2238 return CURSOR_LEFT_PTR;
2243 frame_menu_func(struct window *window,
2244 struct input *input, int index, void *data)
2246 struct display *display;
2250 window_close(window);
2252 case 1: /* move to workspace above */
2253 display = window->display;
2254 if (display->workspace > 0)
2255 workspace_manager_move_surface(
2256 display->workspace_manager,
2257 window->main_surface->surface,
2258 display->workspace - 1);
2260 case 2: /* move to workspace below */
2261 display = window->display;
2262 if (display->workspace < display->workspace_count - 1)
2263 workspace_manager_move_surface(
2264 display->workspace_manager,
2265 window->main_surface->surface,
2266 display->workspace + 1);
2268 case 3: /* fullscreen */
2269 /* we don't have a way to get out of fullscreen for now */
2270 if (window->fullscreen_handler)
2271 window->fullscreen_handler(window, window->user_data);
2277 window_show_frame_menu(struct window *window,
2278 struct input *input, uint32_t time)
2283 static const char *entries[] = {
2285 "Move to workspace above", "Move to workspace below",
2289 if (window->fullscreen_handler)
2290 count = ARRAY_LENGTH(entries);
2292 count = ARRAY_LENGTH(entries) - 1;
2294 input_get_position(input, &x, &y);
2295 window_show_menu(window->display, input, time, window,
2296 x - 10, y - 10, frame_menu_func, entries, count);
2300 frame_enter_handler(struct widget *widget,
2301 struct input *input, float x, float y, void *data)
2303 struct window_frame *frame = data;
2304 enum theme_location location;
2306 location = frame_pointer_enter(frame->frame, input, x, y);
2307 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2308 widget_schedule_redraw(frame->widget);
2310 return frame_get_pointer_image_for_location(data, location);
2314 frame_motion_handler(struct widget *widget,
2315 struct input *input, uint32_t time,
2316 float x, float y, void *data)
2318 struct window_frame *frame = data;
2319 enum theme_location location;
2321 location = frame_pointer_motion(frame->frame, input, x, y);
2322 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2323 widget_schedule_redraw(frame->widget);
2325 return frame_get_pointer_image_for_location(data, location);
2329 frame_leave_handler(struct widget *widget,
2330 struct input *input, void *data)
2332 struct window_frame *frame = data;
2334 frame_pointer_leave(frame->frame, input);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2340 frame_handle_status(struct window_frame *frame, struct input *input,
2341 uint32_t time, enum theme_location location)
2343 struct window *window = frame->widget->window;
2346 status = frame_status(frame->frame);
2347 if (status & FRAME_STATUS_REPAINT)
2348 widget_schedule_redraw(frame->widget);
2350 if (status & FRAME_STATUS_MINIMIZE) {
2351 window_set_minimized(window);
2352 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2355 if (status & FRAME_STATUS_MENU) {
2356 window_show_frame_menu(window, input, time);
2357 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2360 if (status & FRAME_STATUS_MAXIMIZE) {
2361 window_set_maximized(window, !window->maximized);
2362 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2365 if (status & FRAME_STATUS_CLOSE) {
2366 window_close(window);
2370 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2371 input_ungrab(input);
2372 xdg_surface_move(window->xdg_surface,
2373 input_get_seat(input),
2374 window->display->serial);
2376 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2379 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2380 input_ungrab(input);
2382 window->resizing = 1;
2383 xdg_surface_resize(window->xdg_surface,
2384 input_get_seat(input),
2385 window->display->serial,
2388 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2393 frame_button_handler(struct widget *widget,
2394 struct input *input, uint32_t time,
2395 uint32_t button, enum wl_pointer_button_state state,
2399 struct window_frame *frame = data;
2400 enum theme_location location;
2402 location = frame_pointer_button(frame->frame, input, button, state);
2403 frame_handle_status(frame, input, time, location);
2407 frame_touch_down_handler(struct widget *widget, struct input *input,
2408 uint32_t serial, uint32_t time, int32_t id,
2409 float x, float y, void *data)
2411 struct window_frame *frame = data;
2413 frame_touch_down(frame->frame, input, id, x, y);
2414 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2418 frame_touch_up_handler(struct widget *widget,
2419 struct input *input, uint32_t serial, uint32_t time,
2420 int32_t id, void *data)
2422 struct window_frame *frame = data;
2424 frame_touch_up(frame->frame, input, id);
2425 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2429 window_frame_create(struct window *window, void *data)
2431 struct window_frame *frame;
2434 if (window->custom) {
2435 buttons = FRAME_BUTTON_NONE;
2437 buttons = FRAME_BUTTON_ALL;
2440 frame = xzalloc(sizeof *frame);
2441 frame->frame = frame_create(window->display->theme, 0, 0,
2442 buttons, window->title);
2444 frame->widget = window_add_widget(window, frame);
2445 frame->child = widget_add_widget(frame->widget, data);
2447 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2448 widget_set_resize_handler(frame->widget, frame_resize_handler);
2449 widget_set_enter_handler(frame->widget, frame_enter_handler);
2450 widget_set_leave_handler(frame->widget, frame_leave_handler);
2451 widget_set_motion_handler(frame->widget, frame_motion_handler);
2452 widget_set_button_handler(frame->widget, frame_button_handler);
2453 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2454 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2456 window->frame = frame;
2458 return frame->child;
2462 window_frame_set_child_size(struct widget *widget, int child_width,
2465 struct display *display = widget->window->display;
2466 struct theme *t = display->theme;
2467 int decoration_width, decoration_height;
2469 int margin = widget->window->maximized ? 0 : t->margin;
2471 if (!widget->window->fullscreen) {
2472 decoration_width = (t->width + margin) * 2;
2473 decoration_height = t->width +
2474 t->titlebar_height + margin * 2;
2476 width = child_width + decoration_width;
2477 height = child_height + decoration_height;
2479 width = child_width;
2480 height = child_height;
2483 window_schedule_resize(widget->window, width, height);
2487 window_frame_destroy(struct window_frame *frame)
2489 frame_destroy(frame->frame);
2491 /* frame->child must be destroyed by the application */
2492 widget_destroy(frame->widget);
2497 input_set_focus_widget(struct input *input, struct widget *focus,
2500 struct widget *old, *widget;
2503 if (focus == input->focus_widget)
2506 old = input->focus_widget;
2510 widget = input->grab;
2511 if (widget->leave_handler)
2512 widget->leave_handler(old, input, widget->user_data);
2513 input->focus_widget = NULL;
2519 widget = input->grab;
2520 input->focus_widget = focus;
2521 if (widget->enter_handler)
2522 cursor = widget->enter_handler(focus, input, x, y,
2525 cursor = widget->default_cursor;
2527 input_set_pointer_image(input, cursor);
2532 touch_grab(struct input *input, int32_t touch_id)
2534 input->touch_grab = 1;
2535 input->touch_grab_id = touch_id;
2539 touch_ungrab(struct input *input)
2541 struct touch_point *tp, *tmp;
2543 input->touch_grab = 0;
2545 wl_list_for_each_safe(tp, tmp,
2546 &input->touch_point_list, link) {
2547 if (tp->id != input->touch_grab_id)
2549 wl_list_remove(&tp->link);
2557 input_grab(struct input *input, struct widget *widget, uint32_t button)
2559 input->grab = widget;
2560 input->grab_button = button;
2564 input_ungrab(struct input *input)
2566 struct widget *widget;
2569 if (input->pointer_focus) {
2570 widget = window_find_widget(input->pointer_focus,
2571 input->sx, input->sy);
2572 input_set_focus_widget(input, widget, input->sx, input->sy);
2577 input_remove_pointer_focus(struct input *input)
2579 struct window *window = input->pointer_focus;
2584 input_set_focus_widget(input, NULL, 0, 0);
2586 input->pointer_focus = NULL;
2587 input->current_cursor = CURSOR_UNSET;
2591 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2592 uint32_t serial, struct wl_surface *surface,
2593 wl_fixed_t sx_w, wl_fixed_t sy_w)
2595 struct input *input = data;
2596 struct window *window;
2597 struct widget *widget;
2598 float sx = wl_fixed_to_double(sx_w);
2599 float sy = wl_fixed_to_double(sy_w);
2602 /* enter event for a window we've just destroyed */
2606 input->display->serial = serial;
2607 input->pointer_enter_serial = serial;
2608 input->pointer_focus = wl_surface_get_user_data(surface);
2609 window = input->pointer_focus;
2611 if (window->resizing) {
2612 window->resizing = 0;
2613 /* Schedule a redraw to free the pool */
2614 window_schedule_redraw(window);
2620 widget = window_find_widget(window, sx, sy);
2621 input_set_focus_widget(input, widget, sx, sy);
2625 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2626 uint32_t serial, struct wl_surface *surface)
2628 struct input *input = data;
2630 input->display->serial = serial;
2631 input_remove_pointer_focus(input);
2635 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2636 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2638 struct input *input = data;
2639 struct window *window = input->pointer_focus;
2640 struct widget *widget;
2642 float sx = wl_fixed_to_double(sx_w);
2643 float sy = wl_fixed_to_double(sy_w);
2651 /* when making the window smaller - e.g. after a unmaximise we might
2652 * still have a pending motion event that the compositor has picked
2653 * based on the old surface dimensions
2655 if (sx > window->main_surface->allocation.width ||
2656 sy > window->main_surface->allocation.height)
2659 if (!(input->grab && input->grab_button)) {
2660 widget = window_find_widget(window, sx, sy);
2661 input_set_focus_widget(input, widget, sx, sy);
2665 widget = input->grab;
2667 widget = input->focus_widget;
2669 if (widget->motion_handler)
2670 cursor = widget->motion_handler(input->focus_widget,
2671 input, time, sx, sy,
2674 cursor = widget->default_cursor;
2676 cursor = CURSOR_LEFT_PTR;
2678 input_set_pointer_image(input, cursor);
2682 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2683 uint32_t time, uint32_t button, uint32_t state_w)
2685 struct input *input = data;
2686 struct widget *widget;
2687 enum wl_pointer_button_state state = state_w;
2689 input->display->serial = serial;
2690 if (input->focus_widget && input->grab == NULL &&
2691 state == WL_POINTER_BUTTON_STATE_PRESSED)
2692 input_grab(input, input->focus_widget, button);
2694 widget = input->grab;
2695 if (widget && widget->button_handler)
2696 (*widget->button_handler)(widget,
2699 input->grab->user_data);
2701 if (input->grab && input->grab_button == button &&
2702 state == WL_POINTER_BUTTON_STATE_RELEASED)
2703 input_ungrab(input);
2707 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2708 uint32_t time, uint32_t axis, wl_fixed_t value)
2710 struct input *input = data;
2711 struct widget *widget;
2713 widget = input->focus_widget;
2715 widget = input->grab;
2716 if (widget && widget->axis_handler)
2717 (*widget->axis_handler)(widget,
2723 static const struct wl_pointer_listener pointer_listener = {
2724 pointer_handle_enter,
2725 pointer_handle_leave,
2726 pointer_handle_motion,
2727 pointer_handle_button,
2728 pointer_handle_axis,
2732 input_remove_keyboard_focus(struct input *input)
2734 struct window *window = input->keyboard_focus;
2735 struct itimerspec its;
2737 its.it_interval.tv_sec = 0;
2738 its.it_interval.tv_nsec = 0;
2739 its.it_value.tv_sec = 0;
2740 its.it_value.tv_nsec = 0;
2741 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2746 if (window->keyboard_focus_handler)
2747 (*window->keyboard_focus_handler)(window, NULL,
2750 input->keyboard_focus = NULL;
2754 keyboard_repeat_func(struct task *task, uint32_t events)
2756 struct input *input =
2757 container_of(task, struct input, repeat_task);
2758 struct window *window = input->keyboard_focus;
2761 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2762 /* If we change the timer between the fd becoming
2763 * readable and getting here, there'll be nothing to
2764 * read and we get EAGAIN. */
2767 if (window && window->key_handler) {
2768 (*window->key_handler)(window, input, input->repeat_time,
2769 input->repeat_key, input->repeat_sym,
2770 WL_KEYBOARD_KEY_STATE_PRESSED,
2776 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2777 uint32_t format, int fd, uint32_t size)
2779 struct input *input = data;
2780 struct xkb_keymap *keymap;
2781 struct xkb_state *state;
2789 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2794 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2795 if (map_str == MAP_FAILED) {
2800 keymap = xkb_map_new_from_string(input->display->xkb_context,
2802 XKB_KEYMAP_FORMAT_TEXT_V1,
2804 munmap(map_str, size);
2808 fprintf(stderr, "failed to compile keymap\n");
2812 state = xkb_state_new(keymap);
2814 fprintf(stderr, "failed to create XKB state\n");
2815 xkb_map_unref(keymap);
2819 xkb_keymap_unref(input->xkb.keymap);
2820 xkb_state_unref(input->xkb.state);
2821 input->xkb.keymap = keymap;
2822 input->xkb.state = state;
2824 input->xkb.control_mask =
2825 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2826 input->xkb.alt_mask =
2827 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2828 input->xkb.shift_mask =
2829 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2833 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2834 uint32_t serial, struct wl_surface *surface,
2835 struct wl_array *keys)
2837 struct input *input = data;
2838 struct window *window;
2840 input->display->serial = serial;
2841 input->keyboard_focus = wl_surface_get_user_data(surface);
2843 window = input->keyboard_focus;
2844 if (window->keyboard_focus_handler)
2845 (*window->keyboard_focus_handler)(window,
2846 input, window->user_data);
2850 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2851 uint32_t serial, struct wl_surface *surface)
2853 struct input *input = data;
2855 input->display->serial = serial;
2856 input_remove_keyboard_focus(input);
2860 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2861 uint32_t serial, uint32_t time, uint32_t key,
2864 struct input *input = data;
2865 struct window *window = input->keyboard_focus;
2866 uint32_t code, num_syms;
2867 enum wl_keyboard_key_state state = state_w;
2868 const xkb_keysym_t *syms;
2870 struct itimerspec its;
2872 input->display->serial = serial;
2874 if (!window || !input->xkb.state)
2877 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2879 sym = XKB_KEY_NoSymbol;
2884 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2885 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2886 window_set_maximized(window, !window->maximized);
2887 } else if (sym == XKB_KEY_F11 &&
2888 window->fullscreen_handler &&
2889 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2890 window->fullscreen_handler(window, window->user_data);
2891 } else if (sym == XKB_KEY_F4 &&
2892 input->modifiers == MOD_ALT_MASK &&
2893 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2894 window_close(window);
2895 } else if (window->key_handler) {
2896 (*window->key_handler)(window, input, time, key,
2897 sym, state, window->user_data);
2900 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2901 key == input->repeat_key) {
2902 its.it_interval.tv_sec = 0;
2903 its.it_interval.tv_nsec = 0;
2904 its.it_value.tv_sec = 0;
2905 its.it_value.tv_nsec = 0;
2906 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2907 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2908 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2909 input->repeat_sym = sym;
2910 input->repeat_key = key;
2911 input->repeat_time = time;
2912 its.it_interval.tv_sec = 0;
2913 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2914 its.it_value.tv_sec = 0;
2915 its.it_value.tv_nsec = 400 * 1000 * 1000;
2916 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2921 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2922 uint32_t serial, uint32_t mods_depressed,
2923 uint32_t mods_latched, uint32_t mods_locked,
2926 struct input *input = data;
2927 xkb_mod_mask_t mask;
2929 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2930 if (!input->xkb.keymap)
2933 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2934 mods_locked, 0, 0, group);
2935 mask = xkb_state_serialize_mods(input->xkb.state,
2936 XKB_STATE_DEPRESSED |
2938 input->modifiers = 0;
2939 if (mask & input->xkb.control_mask)
2940 input->modifiers |= MOD_CONTROL_MASK;
2941 if (mask & input->xkb.alt_mask)
2942 input->modifiers |= MOD_ALT_MASK;
2943 if (mask & input->xkb.shift_mask)
2944 input->modifiers |= MOD_SHIFT_MASK;
2947 static const struct wl_keyboard_listener keyboard_listener = {
2948 keyboard_handle_keymap,
2949 keyboard_handle_enter,
2950 keyboard_handle_leave,
2951 keyboard_handle_key,
2952 keyboard_handle_modifiers,
2956 touch_handle_down(void *data, struct wl_touch *wl_touch,
2957 uint32_t serial, uint32_t time, struct wl_surface *surface,
2958 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2960 struct input *input = data;
2961 struct widget *widget;
2962 float sx = wl_fixed_to_double(x_w);
2963 float sy = wl_fixed_to_double(y_w);
2965 input->display->serial = serial;
2966 input->touch_focus = wl_surface_get_user_data(surface);
2967 if (!input->touch_focus) {
2968 DBG("Failed to find to touch focus for surface %p\n", surface);
2972 widget = window_find_widget(input->touch_focus,
2973 wl_fixed_to_double(x_w),
2974 wl_fixed_to_double(y_w));
2976 struct touch_point *tp = xmalloc(sizeof *tp);
2979 tp->widget = widget;
2982 wl_list_insert(&input->touch_point_list, &tp->link);
2984 if (widget->touch_down_handler)
2985 (*widget->touch_down_handler)(widget, input,
2994 touch_handle_up(void *data, struct wl_touch *wl_touch,
2995 uint32_t serial, uint32_t time, int32_t id)
2997 struct input *input = data;
2998 struct touch_point *tp, *tmp;
3000 if (!input->touch_focus) {
3001 DBG("No touch focus found for touch up event!\n");
3005 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3009 if (tp->widget->touch_up_handler)
3010 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3012 tp->widget->user_data);
3014 wl_list_remove(&tp->link);
3022 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3023 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3025 struct input *input = data;
3026 struct touch_point *tp;
3027 float sx = wl_fixed_to_double(x_w);
3028 float sy = wl_fixed_to_double(y_w);
3030 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3032 if (!input->touch_focus) {
3033 DBG("No touch focus found for touch motion event!\n");
3037 wl_list_for_each(tp, &input->touch_point_list, link) {
3043 if (tp->widget->touch_motion_handler)
3044 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3046 tp->widget->user_data);
3052 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3054 struct input *input = data;
3055 struct touch_point *tp, *tmp;
3057 DBG("touch_handle_frame\n");
3059 if (!input->touch_focus) {
3060 DBG("No touch focus found for touch frame event!\n");
3064 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3065 if (tp->widget->touch_frame_handler)
3066 (*tp->widget->touch_frame_handler)(tp->widget, input,
3067 tp->widget->user_data);
3072 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3074 struct input *input = data;
3075 struct touch_point *tp, *tmp;
3077 DBG("touch_handle_cancel\n");
3079 if (!input->touch_focus) {
3080 DBG("No touch focus found for touch cancel event!\n");
3084 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3085 if (tp->widget->touch_cancel_handler)
3086 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3087 tp->widget->user_data);
3089 wl_list_remove(&tp->link);
3094 static const struct wl_touch_listener touch_listener = {
3097 touch_handle_motion,
3099 touch_handle_cancel,
3103 seat_handle_capabilities(void *data, struct wl_seat *seat,
3104 enum wl_seat_capability caps)
3106 struct input *input = data;
3108 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3109 input->pointer = wl_seat_get_pointer(seat);
3110 wl_pointer_set_user_data(input->pointer, input);
3111 wl_pointer_add_listener(input->pointer, &pointer_listener,
3113 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3114 wl_pointer_destroy(input->pointer);
3115 input->pointer = NULL;
3118 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3119 input->keyboard = wl_seat_get_keyboard(seat);
3120 wl_keyboard_set_user_data(input->keyboard, input);
3121 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3123 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3124 wl_keyboard_destroy(input->keyboard);
3125 input->keyboard = NULL;
3128 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3129 input->touch = wl_seat_get_touch(seat);
3130 wl_touch_set_user_data(input->touch, input);
3131 wl_touch_add_listener(input->touch, &touch_listener, input);
3132 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3133 wl_touch_destroy(input->touch);
3134 input->touch = NULL;
3139 seat_handle_name(void *data, struct wl_seat *seat,
3145 static const struct wl_seat_listener seat_listener = {
3146 seat_handle_capabilities,
3151 input_get_position(struct input *input, int32_t *x, int32_t *y)
3158 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3160 struct touch_point *tp;
3162 wl_list_for_each(tp, &input->touch_point_list, link) {
3175 input_get_display(struct input *input)
3177 return input->display;
3181 input_get_seat(struct input *input)
3187 input_get_modifiers(struct input *input)
3189 return input->modifiers;
3193 input_get_focus_widget(struct input *input)
3195 return input->focus_widget;
3199 struct wl_data_offer *offer;
3200 struct input *input;
3201 struct wl_array types;
3204 struct task io_task;
3212 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3214 struct data_offer *offer = data;
3217 p = wl_array_add(&offer->types, sizeof *p);
3221 static const struct wl_data_offer_listener data_offer_listener = {
3226 data_offer_destroy(struct data_offer *offer)
3231 if (offer->refcount == 0) {
3232 wl_data_offer_destroy(offer->offer);
3233 for (p = offer->types.data; *p; p++)
3235 wl_array_release(&offer->types);
3241 data_device_data_offer(void *data,
3242 struct wl_data_device *data_device,
3243 struct wl_data_offer *_offer)
3245 struct data_offer *offer;
3247 offer = xmalloc(sizeof *offer);
3249 wl_array_init(&offer->types);
3250 offer->refcount = 1;
3251 offer->input = data;
3252 offer->offer = _offer;
3253 wl_data_offer_add_listener(offer->offer,
3254 &data_offer_listener, offer);
3258 data_device_enter(void *data, struct wl_data_device *data_device,
3259 uint32_t serial, struct wl_surface *surface,
3260 wl_fixed_t x_w, wl_fixed_t y_w,
3261 struct wl_data_offer *offer)
3263 struct input *input = data;
3264 struct window *window;
3266 float x = wl_fixed_to_double(x_w);
3267 float y = wl_fixed_to_double(y_w);
3270 window = wl_surface_get_user_data(surface);
3271 input->drag_enter_serial = serial;
3272 input->drag_focus = window,
3276 if (!input->touch_grab)
3277 input->pointer_enter_serial = serial;
3280 input->drag_offer = wl_data_offer_get_user_data(offer);
3282 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3285 types_data = input->drag_offer->types.data;
3287 input->drag_offer = NULL;
3291 if (window->data_handler)
3292 window->data_handler(window, input, x, y, types_data,
3297 data_device_leave(void *data, struct wl_data_device *data_device)
3299 struct input *input = data;
3301 if (input->drag_offer) {
3302 data_offer_destroy(input->drag_offer);
3303 input->drag_offer = NULL;
3308 data_device_motion(void *data, struct wl_data_device *data_device,
3309 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3311 struct input *input = data;
3312 struct window *window = input->drag_focus;
3313 float x = wl_fixed_to_double(x_w);
3314 float y = wl_fixed_to_double(y_w);
3320 if (input->drag_offer)
3321 types_data = input->drag_offer->types.data;
3325 if (window->data_handler)
3326 window->data_handler(window, input, x, y, types_data,
3331 data_device_drop(void *data, struct wl_data_device *data_device)
3333 struct input *input = data;
3334 struct window *window = input->drag_focus;
3340 if (window->drop_handler)
3341 window->drop_handler(window, input,
3342 x, y, window->user_data);
3344 if (input->touch_grab)
3345 touch_ungrab(input);
3349 data_device_selection(void *data,
3350 struct wl_data_device *wl_data_device,
3351 struct wl_data_offer *offer)
3353 struct input *input = data;
3356 if (input->selection_offer)
3357 data_offer_destroy(input->selection_offer);
3360 input->selection_offer = wl_data_offer_get_user_data(offer);
3361 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3364 input->selection_offer = NULL;
3368 static const struct wl_data_device_listener data_device_listener = {
3369 data_device_data_offer,
3374 data_device_selection
3378 input_set_pointer_image_index(struct input *input, int index)
3380 struct wl_buffer *buffer;
3381 struct wl_cursor *cursor;
3382 struct wl_cursor_image *image;
3384 if (!input->pointer)
3387 cursor = input->display->cursors[input->current_cursor];
3391 if (index >= (int) cursor->image_count) {
3392 fprintf(stderr, "cursor index out of range\n");
3396 image = cursor->images[index];
3397 buffer = wl_cursor_image_get_buffer(image);
3401 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3402 wl_surface_damage(input->pointer_surface, 0, 0,
3403 image->width, image->height);
3404 wl_surface_commit(input->pointer_surface);
3405 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3406 input->pointer_surface,
3407 image->hotspot_x, image->hotspot_y);
3410 static const struct wl_callback_listener pointer_surface_listener;
3413 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3416 struct input *input = data;
3417 struct wl_cursor *cursor;
3421 assert(callback == input->cursor_frame_cb);
3422 wl_callback_destroy(callback);
3423 input->cursor_frame_cb = NULL;
3426 if (!input->pointer)
3429 if (input->current_cursor == CURSOR_BLANK) {
3430 wl_pointer_set_cursor(input->pointer,
3431 input->pointer_enter_serial,
3436 if (input->current_cursor == CURSOR_UNSET)
3438 cursor = input->display->cursors[input->current_cursor];
3442 /* FIXME We don't have the current time on the first call so we set
3443 * the animation start to the time of the first frame callback. */
3445 input->cursor_anim_start = 0;
3446 else if (input->cursor_anim_start == 0)
3447 input->cursor_anim_start = time;
3449 if (time == 0 || input->cursor_anim_start == 0)
3452 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3454 if (cursor->image_count > 1) {
3455 input->cursor_frame_cb =
3456 wl_surface_frame(input->pointer_surface);
3457 wl_callback_add_listener(input->cursor_frame_cb,
3458 &pointer_surface_listener, input);
3461 input_set_pointer_image_index(input, i);
3464 static const struct wl_callback_listener pointer_surface_listener = {
3465 pointer_surface_frame_callback
3469 input_set_pointer_image(struct input *input, int pointer)
3473 if (!input->pointer)
3476 if (input->pointer_enter_serial > input->cursor_serial)
3479 if (!force && pointer == input->current_cursor)
3482 input->current_cursor = pointer;
3483 input->cursor_serial = input->pointer_enter_serial;
3484 if (!input->cursor_frame_cb)
3485 pointer_surface_frame_callback(input, NULL, 0);
3487 /* The current frame callback may be stuck if, for instance,
3488 * the set cursor request was processed by the server after
3489 * this client lost the focus. In this case the cursor surface
3490 * might not be mapped and the frame callback wouldn't ever
3491 * complete. Send a set_cursor and attach to try to map the
3492 * cursor surface again so that the callback will finish */
3493 input_set_pointer_image_index(input, 0);
3497 struct wl_data_device *
3498 input_get_data_device(struct input *input)
3500 return input->data_device;
3504 input_set_selection(struct input *input,
3505 struct wl_data_source *source, uint32_t time)
3507 if (input->data_device)
3508 wl_data_device_set_selection(input->data_device, source, time);
3512 input_accept(struct input *input, const char *type)
3514 wl_data_offer_accept(input->drag_offer->offer,
3515 input->drag_enter_serial, type);
3519 offer_io_func(struct task *task, uint32_t events)
3521 struct data_offer *offer =
3522 container_of(task, struct data_offer, io_task);
3526 len = read(offer->fd, buffer, sizeof buffer);
3527 offer->func(buffer, len,
3528 offer->x, offer->y, offer->user_data);
3532 data_offer_destroy(offer);
3537 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3538 data_func_t func, void *user_data)
3542 if (pipe2(p, O_CLOEXEC) == -1)
3545 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3548 offer->io_task.run = offer_io_func;
3552 offer->user_data = user_data;
3554 display_watch_fd(offer->input->display,
3555 offer->fd, EPOLLIN, &offer->io_task);
3559 input_receive_drag_data(struct input *input, const char *mime_type,
3560 data_func_t func, void *data)
3562 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3563 input->drag_offer->x = input->drag_x;
3564 input->drag_offer->y = input->drag_y;
3568 input_receive_drag_data_to_fd(struct input *input,
3569 const char *mime_type, int fd)
3571 if (input->drag_offer)
3572 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3578 input_receive_selection_data(struct input *input, const char *mime_type,
3579 data_func_t func, void *data)
3583 if (input->selection_offer == NULL)
3586 for (p = input->selection_offer->types.data; *p; p++)
3587 if (strcmp(mime_type, *p) == 0)
3593 data_offer_receive_data(input->selection_offer,
3594 mime_type, func, data);
3599 input_receive_selection_data_to_fd(struct input *input,
3600 const char *mime_type, int fd)
3602 if (input->selection_offer)
3603 wl_data_offer_receive(input->selection_offer->offer,
3610 window_move(struct window *window, struct input *input, uint32_t serial)
3612 if (!window->xdg_surface)
3615 xdg_surface_move(window->xdg_surface, input->seat, serial);
3619 surface_set_synchronized(struct surface *surface)
3621 if (!surface->subsurface)
3624 if (surface->synchronized)
3627 wl_subsurface_set_sync(surface->subsurface);
3628 surface->synchronized = 1;
3632 surface_set_synchronized_default(struct surface *surface)
3634 if (!surface->subsurface)
3637 if (surface->synchronized == surface->synchronized_default)
3640 if (surface->synchronized_default)
3641 wl_subsurface_set_sync(surface->subsurface);
3643 wl_subsurface_set_desync(surface->subsurface);
3645 surface->synchronized = surface->synchronized_default;
3649 surface_resize(struct surface *surface)
3651 struct widget *widget = surface->widget;
3652 struct wl_compositor *compositor = widget->window->display->compositor;
3654 if (surface->input_region) {
3655 wl_region_destroy(surface->input_region);
3656 surface->input_region = NULL;
3659 if (surface->opaque_region)
3660 wl_region_destroy(surface->opaque_region);
3662 surface->opaque_region = wl_compositor_create_region(compositor);
3664 if (widget->resize_handler)
3665 widget->resize_handler(widget,
3666 widget->allocation.width,
3667 widget->allocation.height,
3670 if (surface->subsurface &&
3671 (surface->allocation.x != widget->allocation.x ||
3672 surface->allocation.y != widget->allocation.y)) {
3673 wl_subsurface_set_position(surface->subsurface,
3674 widget->allocation.x,
3675 widget->allocation.y);
3677 if (surface->allocation.width != widget->allocation.width ||
3678 surface->allocation.height != widget->allocation.height) {
3679 window_schedule_redraw(widget->window);
3681 surface->allocation = widget->allocation;
3684 wl_region_add(surface->opaque_region, 0, 0,
3685 widget->allocation.width,
3686 widget->allocation.height);
3690 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3693 * This hack should be removed, when EGL respects
3694 * eglSwapInterval(0).
3696 * If this window has sub-surfaces, especially a free-running
3697 * EGL-widget, we need to post the parent surface once with
3698 * all the old state to guarantee, that the EGL-widget will
3699 * receive its frame callback soon. Otherwise, a forced call
3700 * to eglSwapBuffers may end up blocking, waiting for a frame
3701 * event that will never come, because we will commit the parent
3702 * surface with all new state only after eglSwapBuffers returns.
3704 * This assumes, that:
3705 * 1. When the EGL widget's resize hook is called, it pauses.
3706 * 2. When the EGL widget's redraw hook is called, it forces a
3707 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3708 * In a single threaded application condition 1 is a no-op.
3710 * XXX: This should actually be after the surface_resize() calls,
3711 * but cannot, because then it would commit the incomplete state
3712 * accumulated from the widget resize hooks.
3714 if (window->subsurface_list.next != &window->main_surface->link ||
3715 window->subsurface_list.prev != &window->main_surface->link)
3716 wl_surface_commit(window->main_surface->surface);
3720 window_do_resize(struct window *window)
3722 struct surface *surface;
3724 widget_set_allocation(window->main_surface->widget,
3725 window->pending_allocation.x,
3726 window->pending_allocation.y,
3727 window->pending_allocation.width,
3728 window->pending_allocation.height);
3730 surface_resize(window->main_surface);
3732 /* The main surface is in the list, too. Main surface's
3733 * resize_handler is responsible for calling widget_set_allocation()
3734 * on all sub-surface root widgets, so they will be resized
3737 wl_list_for_each(surface, &window->subsurface_list, link) {
3738 if (surface == window->main_surface)
3741 surface_set_synchronized(surface);
3742 surface_resize(surface);
3745 if (!window->fullscreen && !window->maximized)
3746 window->saved_allocation = window->pending_allocation;
3750 idle_resize(struct window *window)
3752 window->resize_needed = 0;
3753 window->redraw_needed = 1;
3755 DBG("from %dx%d to %dx%d\n",
3756 window->main_surface->server_allocation.width,
3757 window->main_surface->server_allocation.height,
3758 window->pending_allocation.width,
3759 window->pending_allocation.height);
3761 hack_prevent_EGL_sub_surface_deadlock(window);
3763 window_do_resize(window);
3767 undo_resize(struct window *window)
3769 window->pending_allocation.width =
3770 window->main_surface->server_allocation.width;
3771 window->pending_allocation.height =
3772 window->main_surface->server_allocation.height;
3774 DBG("back to %dx%d\n",
3775 window->main_surface->server_allocation.width,
3776 window->main_surface->server_allocation.height);
3778 window_do_resize(window);
3780 if (window->pending_allocation.width == 0 &&
3781 window->pending_allocation.height == 0) {
3782 fprintf(stderr, "Error: Could not draw a surface, "
3783 "most likely due to insufficient disk space in "
3784 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3790 window_schedule_resize(struct window *window, int width, int height)
3792 /* We should probably get these numbers from the theme. */
3793 const int min_width = 200, min_height = 200;
3795 window->pending_allocation.x = 0;
3796 window->pending_allocation.y = 0;
3797 window->pending_allocation.width = width;
3798 window->pending_allocation.height = height;
3800 if (window->min_allocation.width == 0) {
3801 if (width < min_width && window->frame)
3802 window->min_allocation.width = min_width;
3804 window->min_allocation.width = width;
3805 if (height < min_height && window->frame)
3806 window->min_allocation.height = min_height;
3808 window->min_allocation.height = height;
3811 if (window->pending_allocation.width < window->min_allocation.width)
3812 window->pending_allocation.width = window->min_allocation.width;
3813 if (window->pending_allocation.height < window->min_allocation.height)
3814 window->pending_allocation.height = window->min_allocation.height;
3816 window->resize_needed = 1;
3817 window_schedule_redraw(window);
3821 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3823 window_schedule_resize(widget->window, width, height);
3827 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3828 int32_t width, int32_t height)
3830 struct window *window = data;
3832 window_schedule_resize(window, width, height);
3836 handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
3841 struct window *window = data;
3844 case XDG_SURFACE_STATE_MAXIMIZED:
3845 window->maximized = value;
3847 case XDG_SURFACE_STATE_FULLSCREEN:
3848 window->fullscreen = value;
3852 if (!window->fullscreen && !window->maximized)
3853 window_schedule_resize(window,
3854 window->saved_allocation.width,
3855 window->saved_allocation.height);
3857 xdg_surface_ack_change_state(xdg_surface, state, value, serial);
3858 window_schedule_redraw(window);
3862 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
3864 struct window *window = data;
3865 window->focused = 1;
3869 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
3871 struct window *window = data;
3872 window->focused = 0;
3876 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3878 struct window *window = data;
3879 window_close(window);
3882 static const struct xdg_surface_listener xdg_surface_listener = {
3883 handle_surface_configure,
3884 handle_surface_change_state,
3885 handle_surface_activated,
3886 handle_surface_deactivated,
3887 handle_surface_delete,
3891 window_sync_transient_for(struct window *window)
3893 struct wl_surface *parent_surface;
3895 if (!window->xdg_surface)
3898 if (window->transient_for)
3899 parent_surface = window->transient_for->main_surface->surface;
3901 parent_surface = NULL;
3903 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3907 window_sync_margin(struct window *window)
3911 if (!window->xdg_surface)
3917 margin = frame_get_shadow_margin(window->frame->frame);
3919 /* Shadow size is the same on every side. */
3920 xdg_surface_set_margin(window->xdg_surface,
3928 window_flush(struct window *window)
3930 struct surface *surface;
3932 if (!window->custom) {
3933 if (window->xdg_surface) {
3934 window_sync_transient_for(window);
3935 window_sync_margin(window);
3939 wl_list_for_each(surface, &window->subsurface_list, link) {
3940 if (surface == window->main_surface)
3943 surface_flush(surface);
3946 surface_flush(window->main_surface);
3950 menu_destroy(struct menu *menu)
3952 widget_destroy(menu->widget);
3953 window_destroy(menu->window);
3954 frame_destroy(menu->frame);
3959 window_get_allocation(struct window *window,
3960 struct rectangle *allocation)
3962 *allocation = window->main_surface->allocation;
3966 widget_redraw(struct widget *widget)
3968 struct widget *child;
3970 if (widget->redraw_handler)
3971 widget->redraw_handler(widget, widget->user_data);
3972 wl_list_for_each(child, &widget->child_list, link)
3973 widget_redraw(child);
3977 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3979 struct surface *surface = data;
3981 assert(callback == surface->frame_cb);
3982 DBG_OBJ(callback, "done\n");
3983 wl_callback_destroy(callback);
3984 surface->frame_cb = NULL;
3986 surface->last_time = time;
3988 if (surface->redraw_needed || surface->window->redraw_needed) {
3989 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3990 window_schedule_redraw_task(surface->window);
3994 static const struct wl_callback_listener listener = {
3999 surface_redraw(struct surface *surface)
4001 DBG_OBJ(surface->surface, "begin\n");
4003 if (!surface->window->redraw_needed && !surface->redraw_needed)
4006 /* Whole-window redraw forces a redraw even if the previous has
4007 * not yet hit the screen.
4009 if (surface->frame_cb) {
4010 if (!surface->window->redraw_needed)
4013 DBG_OBJ(surface->frame_cb, "cancelled\n");
4014 wl_callback_destroy(surface->frame_cb);
4017 if (surface->widget->use_cairo &&
4018 !widget_get_cairo_surface(surface->widget)) {
4019 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4023 surface->frame_cb = wl_surface_frame(surface->surface);
4024 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4025 DBG_OBJ(surface->frame_cb, "new\n");
4027 surface->redraw_needed = 0;
4028 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4029 widget_redraw(surface->widget);
4030 DBG_OBJ(surface->surface, "done\n");
4035 idle_redraw(struct task *task, uint32_t events)
4037 struct window *window = container_of(task, struct window, redraw_task);
4038 struct surface *surface;
4042 DBG(" --------- \n");
4044 wl_list_init(&window->redraw_task.link);
4045 window->redraw_task_scheduled = 0;
4047 if (window->resize_needed) {
4048 /* throttle resizing to the main surface display */
4049 if (window->main_surface->frame_cb) {
4050 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4054 idle_resize(window);
4058 if (surface_redraw(window->main_surface) < 0) {
4060 * Only main_surface failure will cause us to undo the resize.
4061 * If sub-surfaces fail, they will just be broken with old
4066 wl_list_for_each(surface, &window->subsurface_list, link) {
4067 if (surface == window->main_surface)
4070 surface_redraw(surface);
4074 window->redraw_needed = 0;
4075 window_flush(window);
4077 wl_list_for_each(surface, &window->subsurface_list, link)
4078 surface_set_synchronized_default(surface);
4080 if (resized && failed) {
4081 /* Restore widget tree to correspond to what is on screen. */
4082 undo_resize(window);
4087 window_schedule_redraw_task(struct window *window)
4089 if (!window->redraw_task_scheduled) {
4090 window->redraw_task.run = idle_redraw;
4091 display_defer(window->display, &window->redraw_task);
4092 window->redraw_task_scheduled = 1;
4097 window_schedule_redraw(struct window *window)
4099 struct surface *surface;
4101 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4103 wl_list_for_each(surface, &window->subsurface_list, link)
4104 surface->redraw_needed = 1;
4106 window_schedule_redraw_task(window);
4110 window_is_fullscreen(struct window *window)
4112 return window->fullscreen;
4116 window_set_fullscreen(struct window *window, int fullscreen)
4118 if (!window->xdg_surface)
4121 if (window->fullscreen == fullscreen)
4124 xdg_surface_request_change_state(window->xdg_surface,
4125 XDG_SURFACE_STATE_FULLSCREEN,
4131 window_is_maximized(struct window *window)
4133 return window->maximized;
4137 window_set_maximized(struct window *window, int maximized)
4139 if (!window->xdg_surface)
4142 if (window->maximized == maximized)
4145 xdg_surface_request_change_state(window->xdg_surface,
4146 XDG_SURFACE_STATE_MAXIMIZED,
4152 window_set_minimized(struct window *window)
4154 if (!window->xdg_surface)
4157 xdg_surface_set_minimized(window->xdg_surface);
4161 window_set_user_data(struct window *window, void *data)
4163 window->user_data = data;
4167 window_get_user_data(struct window *window)
4169 return window->user_data;
4173 window_set_key_handler(struct window *window,
4174 window_key_handler_t handler)
4176 window->key_handler = handler;
4180 window_set_keyboard_focus_handler(struct window *window,
4181 window_keyboard_focus_handler_t handler)
4183 window->keyboard_focus_handler = handler;
4187 window_set_data_handler(struct window *window, window_data_handler_t handler)
4189 window->data_handler = handler;
4193 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4195 window->drop_handler = handler;
4199 window_set_close_handler(struct window *window,
4200 window_close_handler_t handler)
4202 window->close_handler = handler;
4206 window_set_fullscreen_handler(struct window *window,
4207 window_fullscreen_handler_t handler)
4209 window->fullscreen_handler = handler;
4213 window_set_output_handler(struct window *window,
4214 window_output_handler_t handler)
4216 window->output_handler = handler;
4220 window_set_title(struct window *window, const char *title)
4222 free(window->title);
4223 window->title = strdup(title);
4224 if (window->frame) {
4225 frame_set_title(window->frame->frame, title);
4226 widget_schedule_redraw(window->frame->widget);
4228 if (window->xdg_surface)
4229 xdg_surface_set_title(window->xdg_surface, title);
4233 window_get_title(struct window *window)
4235 return window->title;
4239 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4241 struct text_cursor_position *text_cursor_position =
4242 window->display->text_cursor_position;
4244 if (!text_cursor_position)
4247 text_cursor_position_notify(text_cursor_position,
4248 window->main_surface->surface,
4249 wl_fixed_from_int(x),
4250 wl_fixed_from_int(y));
4254 window_damage(struct window *window, int32_t x, int32_t y,
4255 int32_t width, int32_t height)
4257 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4261 surface_enter(void *data,
4262 struct wl_surface *wl_surface, struct wl_output *wl_output)
4264 struct window *window = data;
4265 struct output *output;
4266 struct output *output_found = NULL;
4267 struct window_output *window_output;
4269 wl_list_for_each(output, &window->display->output_list, link) {
4270 if (output->output == wl_output) {
4271 output_found = output;
4279 window_output = xmalloc(sizeof *window_output);
4280 window_output->output = output_found;
4282 wl_list_insert (&window->window_output_list, &window_output->link);
4284 if (window->output_handler)
4285 window->output_handler(window, output_found, 1,
4290 surface_leave(void *data,
4291 struct wl_surface *wl_surface, struct wl_output *output)
4293 struct window *window = data;
4294 struct window_output *window_output;
4295 struct window_output *window_output_found = NULL;
4297 wl_list_for_each(window_output, &window->window_output_list, link) {
4298 if (window_output->output->output == output) {
4299 window_output_found = window_output;
4304 if (window_output_found) {
4305 wl_list_remove(&window_output_found->link);
4307 if (window->output_handler)
4308 window->output_handler(window, window_output->output,
4309 0, window->user_data);
4311 free(window_output_found);
4315 static const struct wl_surface_listener surface_listener = {
4320 static struct surface *
4321 surface_create(struct window *window)
4323 struct display *display = window->display;
4324 struct surface *surface;
4326 surface = xmalloc(sizeof *surface);
4327 memset(surface, 0, sizeof *surface);
4331 surface->window = window;
4332 surface->surface = wl_compositor_create_surface(display->compositor);
4333 surface->buffer_scale = 1;
4334 wl_surface_add_listener(surface->surface, &surface_listener, window);
4336 wl_list_insert(&window->subsurface_list, &surface->link);
4341 static struct window *
4342 window_create_internal(struct display *display, int custom)
4344 struct window *window;
4345 struct surface *surface;
4347 window = xzalloc(sizeof *window);
4348 wl_list_init(&window->subsurface_list);
4349 window->display = display;
4351 surface = surface_create(window);
4352 window->main_surface = surface;
4354 assert(custom || display->xdg_shell);
4356 window->custom = custom;
4357 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4359 if (display->argb_device)
4360 #ifdef HAVE_CAIRO_EGL
4361 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4363 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4366 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4368 wl_surface_set_user_data(surface->surface, window);
4369 wl_list_insert(display->window_list.prev, &window->link);
4370 wl_list_init(&window->redraw_task.link);
4372 wl_list_init (&window->window_output_list);
4378 window_create(struct display *display)
4380 struct window *window;
4382 window = window_create_internal(display, 0);
4384 window->xdg_surface =
4385 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4386 window->main_surface->surface);
4387 fail_on_null(window->xdg_surface);
4389 xdg_surface_set_user_data(window->xdg_surface, window);
4390 xdg_surface_add_listener(window->xdg_surface,
4391 &xdg_surface_listener, window);
4397 window_create_custom(struct display *display)
4399 return window_create_internal(display, 1);
4403 window_set_transient_for(struct window *window,
4404 struct window *parent_window)
4406 window->transient_for = parent_window;
4407 window_sync_transient_for(window);
4411 window_get_transient_for(struct window *window)
4413 return window->transient_for;
4417 menu_set_item(struct menu *menu, int sy)
4419 int32_t x, y, width, height;
4422 frame_interior(menu->frame, &x, &y, &width, &height);
4423 next = (sy - y) / 20;
4424 if (menu->current != next) {
4425 menu->current = next;
4426 widget_schedule_redraw(menu->widget);
4431 menu_motion_handler(struct widget *widget,
4432 struct input *input, uint32_t time,
4433 float x, float y, void *data)
4435 struct menu *menu = data;
4437 if (widget == menu->widget)
4438 menu_set_item(data, y);
4440 return CURSOR_LEFT_PTR;
4444 menu_enter_handler(struct widget *widget,
4445 struct input *input, float x, float y, void *data)
4447 struct menu *menu = data;
4449 if (widget == menu->widget)
4450 menu_set_item(data, y);
4452 return CURSOR_LEFT_PTR;
4456 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4458 struct menu *menu = data;
4460 if (widget == menu->widget)
4461 menu_set_item(data, -200);
4465 menu_button_handler(struct widget *widget,
4466 struct input *input, uint32_t time,
4467 uint32_t button, enum wl_pointer_button_state state,
4471 struct menu *menu = data;
4473 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4474 (menu->release_count > 0 || time - menu->time > 500)) {
4475 /* Either relase after press-drag-release or
4476 * click-motion-click. */
4477 menu->func(menu->parent, input,
4478 menu->current, menu->parent->user_data);
4479 input_ungrab(input);
4481 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4482 menu->release_count++;
4487 menu_redraw_handler(struct widget *widget, void *data)
4490 struct menu *menu = data;
4491 int32_t x, y, width, height, i;
4493 cr = widget_cairo_create(widget);
4495 frame_repaint(menu->frame, cr);
4496 frame_interior(menu->frame, &x, &y, &width, &height);
4498 theme_set_background_source(menu->window->display->theme,
4499 cr, THEME_FRAME_ACTIVE);
4500 cairo_rectangle(cr, x, y, width, height);
4503 cairo_select_font_face(cr, "sans",
4504 CAIRO_FONT_SLANT_NORMAL,
4505 CAIRO_FONT_WEIGHT_NORMAL);
4506 cairo_set_font_size(cr, 12);
4508 for (i = 0; i < menu->count; i++) {
4509 if (i == menu->current) {
4510 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4511 cairo_rectangle(cr, x, y + i * 20, width, 20);
4513 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4514 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4515 cairo_show_text(cr, menu->entries[i]);
4517 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4518 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4519 cairo_show_text(cr, menu->entries[i]);
4527 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4529 struct window *window = data;
4530 struct menu *menu = window->main_surface->widget->user_data;
4532 input_ungrab(menu->input);
4536 static const struct xdg_popup_listener xdg_popup_listener = {
4537 handle_popup_popup_done,
4541 window_show_menu(struct display *display,
4542 struct input *input, uint32_t time, struct window *parent,
4543 int32_t x, int32_t y,
4544 menu_func_t func, const char **entries, int count)
4546 struct window *window;
4550 menu = malloc(sizeof *menu);
4554 window = window_create_internal(parent->display, 0);
4560 menu->window = window;
4561 menu->parent = parent;
4562 menu->widget = window_add_widget(menu->window, menu);
4563 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4564 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4565 menu->frame = frame_create(window->display->theme, 0, 0,
4566 FRAME_BUTTON_NONE, NULL);
4567 fail_on_null(menu->frame);
4568 menu->entries = entries;
4569 menu->count = count;
4570 menu->release_count = 0;
4574 menu->input = input;
4578 input_ungrab(input);
4580 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4581 widget_set_enter_handler(menu->widget, menu_enter_handler);
4582 widget_set_leave_handler(menu->widget, menu_leave_handler);
4583 widget_set_motion_handler(menu->widget, menu_motion_handler);
4584 widget_set_button_handler(menu->widget, menu_button_handler);
4586 input_grab(input, menu->widget, 0);
4587 frame_resize_inside(menu->frame, 200, count * 20);
4588 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4589 window_schedule_resize(window, frame_width(menu->frame),
4590 frame_height(menu->frame));
4592 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4594 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4595 window->main_surface->surface,
4596 parent->main_surface->surface,
4598 display_get_serial(window->display),
4602 fail_on_null(window->xdg_popup);
4604 xdg_popup_set_user_data(window->xdg_popup, window);
4605 xdg_popup_add_listener(window->xdg_popup,
4606 &xdg_popup_listener, window);
4610 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4612 window->main_surface->buffer_type = type;
4615 enum window_buffer_type
4616 window_get_buffer_type(struct window *window)
4618 return window->main_surface->buffer_type;
4622 window_set_preferred_format(struct window *window,
4623 enum preferred_format format)
4625 window->preferred_format = format;
4629 window_add_subsurface(struct window *window, void *data,
4630 enum subsurface_mode default_mode)
4632 struct widget *widget;
4633 struct surface *surface;
4634 struct wl_surface *parent;
4635 struct wl_subcompositor *subcompo = window->display->subcompositor;
4637 surface = surface_create(window);
4638 surface->buffer_type = window_get_buffer_type(window);
4639 widget = widget_create(window, surface, data);
4640 wl_list_init(&widget->link);
4641 surface->widget = widget;
4643 parent = window->main_surface->surface;
4644 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4647 surface->synchronized = 1;
4649 switch (default_mode) {
4650 case SUBSURFACE_SYNCHRONIZED:
4651 surface->synchronized_default = 1;
4653 case SUBSURFACE_DESYNCHRONIZED:
4654 surface->synchronized_default = 0;
4657 assert(!"bad enum subsurface_mode");
4660 window->resize_needed = 1;
4661 window_schedule_redraw(window);
4667 display_handle_geometry(void *data,
4668 struct wl_output *wl_output,
4671 int physical_height,
4677 struct output *output = data;
4679 output->allocation.x = x;
4680 output->allocation.y = y;
4681 output->transform = transform;
4685 output->make = strdup(make);
4688 free(output->model);
4689 output->model = strdup(model);
4693 display_handle_done(void *data,
4694 struct wl_output *wl_output)
4699 display_handle_scale(void *data,
4700 struct wl_output *wl_output,
4703 struct output *output = data;
4705 output->scale = scale;
4709 display_handle_mode(void *data,
4710 struct wl_output *wl_output,
4716 struct output *output = data;
4717 struct display *display = output->display;
4719 if (flags & WL_OUTPUT_MODE_CURRENT) {
4720 output->allocation.width = width;
4721 output->allocation.height = height;
4722 if (display->output_configure_handler)
4723 (*display->output_configure_handler)(
4724 output, display->user_data);
4728 static const struct wl_output_listener output_listener = {
4729 display_handle_geometry,
4730 display_handle_mode,
4731 display_handle_done,
4732 display_handle_scale
4736 display_add_output(struct display *d, uint32_t id)
4738 struct output *output;
4740 output = xzalloc(sizeof *output);
4741 output->display = d;
4744 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4745 output->server_output_id = id;
4746 wl_list_insert(d->output_list.prev, &output->link);
4748 wl_output_add_listener(output->output, &output_listener, output);
4752 output_destroy(struct output *output)
4754 if (output->destroy_handler)
4755 (*output->destroy_handler)(output, output->user_data);
4757 wl_output_destroy(output->output);
4758 wl_list_remove(&output->link);
4763 display_destroy_output(struct display *d, uint32_t id)
4765 struct output *output;
4767 wl_list_for_each(output, &d->output_list, link) {
4768 if (output->server_output_id == id) {
4769 output_destroy(output);
4776 display_set_global_handler(struct display *display,
4777 display_global_handler_t handler)
4779 struct global *global;
4781 display->global_handler = handler;
4785 wl_list_for_each(global, &display->global_list, link)
4786 display->global_handler(display,
4787 global->name, global->interface,
4788 global->version, display->user_data);
4792 display_set_global_handler_remove(struct display *display,
4793 display_global_handler_t remove_handler)
4795 display->global_handler_remove = remove_handler;
4796 if (!remove_handler)
4801 display_set_output_configure_handler(struct display *display,
4802 display_output_handler_t handler)
4804 struct output *output;
4806 display->output_configure_handler = handler;
4810 wl_list_for_each(output, &display->output_list, link) {
4811 if (output->allocation.width == 0 &&
4812 output->allocation.height == 0)
4815 (*display->output_configure_handler)(output,
4816 display->user_data);
4821 output_set_user_data(struct output *output, void *data)
4823 output->user_data = data;
4827 output_get_user_data(struct output *output)
4829 return output->user_data;
4833 output_set_destroy_handler(struct output *output,
4834 display_output_handler_t handler)
4836 output->destroy_handler = handler;
4837 /* FIXME: implement this, once we have way to remove outputs */
4841 output_get_allocation(struct output *output, struct rectangle *base)
4843 struct rectangle allocation = output->allocation;
4845 switch (output->transform) {
4846 case WL_OUTPUT_TRANSFORM_90:
4847 case WL_OUTPUT_TRANSFORM_270:
4848 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4849 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4850 /* Swap width and height */
4851 allocation.width = output->allocation.height;
4852 allocation.height = output->allocation.width;
4860 output_get_wl_output(struct output *output)
4862 return output->output;
4865 enum wl_output_transform
4866 output_get_transform(struct output *output)
4868 return output->transform;
4872 output_get_scale(struct output *output)
4874 return output->scale;
4878 output_get_make(struct output *output)
4880 return output->make;
4884 output_get_model(struct output *output)
4886 return output->model;
4890 fini_xkb(struct input *input)
4892 xkb_state_unref(input->xkb.state);
4893 xkb_map_unref(input->xkb.keymap);
4896 #define MIN(a,b) ((a) < (b) ? a : b)
4899 display_add_input(struct display *d, uint32_t id)
4901 struct input *input;
4903 input = xzalloc(sizeof *input);
4905 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4906 MIN(d->seat_version, 3));
4907 input->touch_focus = NULL;
4908 input->pointer_focus = NULL;
4909 input->keyboard_focus = NULL;
4910 wl_list_init(&input->touch_point_list);
4911 wl_list_insert(d->input_list.prev, &input->link);
4913 wl_seat_add_listener(input->seat, &seat_listener, input);
4914 wl_seat_set_user_data(input->seat, input);
4916 if (d->data_device_manager) {
4917 input->data_device =
4918 wl_data_device_manager_get_data_device(d->data_device_manager,
4920 wl_data_device_add_listener(input->data_device,
4921 &data_device_listener,
4925 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4927 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4928 TFD_CLOEXEC | TFD_NONBLOCK);
4929 input->repeat_task.run = keyboard_repeat_func;
4930 display_watch_fd(d, input->repeat_timer_fd,
4931 EPOLLIN, &input->repeat_task);
4935 input_destroy(struct input *input)
4937 input_remove_keyboard_focus(input);
4938 input_remove_pointer_focus(input);
4940 if (input->drag_offer)
4941 data_offer_destroy(input->drag_offer);
4943 if (input->selection_offer)
4944 data_offer_destroy(input->selection_offer);
4946 if (input->data_device)
4947 wl_data_device_destroy(input->data_device);
4949 if (input->display->seat_version >= 3) {
4951 wl_pointer_release(input->pointer);
4952 if (input->keyboard)
4953 wl_keyboard_release(input->keyboard);
4958 wl_surface_destroy(input->pointer_surface);
4960 wl_list_remove(&input->link);
4961 wl_seat_destroy(input->seat);
4962 close(input->repeat_timer_fd);
4967 init_workspace_manager(struct display *d, uint32_t id)
4969 d->workspace_manager =
4970 wl_registry_bind(d->registry, id,
4971 &workspace_manager_interface, 1);
4972 if (d->workspace_manager != NULL)
4973 workspace_manager_add_listener(d->workspace_manager,
4974 &workspace_manager_listener,
4979 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4981 struct display *d = data;
4983 if (format == WL_SHM_FORMAT_RGB565)
4987 struct wl_shm_listener shm_listener = {
4992 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4994 xdg_shell_pong(shell, serial);
4997 static const struct xdg_shell_listener xdg_shell_listener = {
5001 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
5002 #ifdef static_assert
5003 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5004 "Interface version doesn't match implementation version");
5008 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5009 const char *interface, uint32_t version)
5011 struct display *d = data;
5012 struct global *global;
5014 global = xmalloc(sizeof *global);
5016 global->interface = strdup(interface);
5017 global->version = version;
5018 wl_list_insert(d->global_list.prev, &global->link);
5020 if (strcmp(interface, "wl_compositor") == 0) {
5021 d->compositor = wl_registry_bind(registry, id,
5022 &wl_compositor_interface, 3);
5023 } else if (strcmp(interface, "wl_output") == 0) {
5024 display_add_output(d, id);
5025 } else if (strcmp(interface, "wl_seat") == 0) {
5026 d->seat_version = version;
5027 display_add_input(d, id);
5028 } else if (strcmp(interface, "wl_shm") == 0) {
5029 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5030 wl_shm_add_listener(d->shm, &shm_listener, d);
5031 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5032 d->data_device_manager =
5033 wl_registry_bind(registry, id,
5034 &wl_data_device_manager_interface, 1);
5035 } else if (strcmp(interface, "xdg_shell") == 0) {
5036 d->xdg_shell = wl_registry_bind(registry, id,
5037 &xdg_shell_interface, 1);
5038 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5039 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5040 } else if (strcmp(interface, "text_cursor_position") == 0) {
5041 d->text_cursor_position =
5042 wl_registry_bind(registry, id,
5043 &text_cursor_position_interface, 1);
5044 } else if (strcmp(interface, "workspace_manager") == 0) {
5045 init_workspace_manager(d, id);
5046 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5048 wl_registry_bind(registry, id,
5049 &wl_subcompositor_interface, 1);
5052 if (d->global_handler)
5053 d->global_handler(d, id, interface, version, d->user_data);
5057 registry_handle_global_remove(void *data, struct wl_registry *registry,
5060 struct display *d = data;
5061 struct global *global;
5064 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5065 if (global->name != name)
5068 if (strcmp(global->interface, "wl_output") == 0)
5069 display_destroy_output(d, name);
5071 /* XXX: Should destroy remaining bound globals */
5073 if (d->global_handler_remove)
5074 d->global_handler_remove(d, name, global->interface,
5075 global->version, d->user_data);
5077 wl_list_remove(&global->link);
5078 free(global->interface);
5084 display_bind(struct display *display, uint32_t name,
5085 const struct wl_interface *interface, uint32_t version)
5087 return wl_registry_bind(display->registry, name, interface, version);
5090 static const struct wl_registry_listener registry_listener = {
5091 registry_handle_global,
5092 registry_handle_global_remove
5095 #ifdef HAVE_CAIRO_EGL
5097 init_egl(struct display *d)
5099 EGLint major, minor;
5102 #ifdef USE_CAIRO_GLESV2
5103 # define GL_BIT EGL_OPENGL_ES2_BIT
5105 # define GL_BIT EGL_OPENGL_BIT
5108 static const EGLint argb_cfg_attribs[] = {
5109 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5115 EGL_RENDERABLE_TYPE, GL_BIT,
5119 #ifdef USE_CAIRO_GLESV2
5120 static const EGLint context_attribs[] = {
5121 EGL_CONTEXT_CLIENT_VERSION, 2,
5124 EGLint api = EGL_OPENGL_ES_API;
5126 EGLint *context_attribs = NULL;
5127 EGLint api = EGL_OPENGL_API;
5130 d->dpy = eglGetDisplay(d->display);
5131 if (!eglInitialize(d->dpy, &major, &minor)) {
5132 fprintf(stderr, "failed to initialize EGL\n");
5136 if (!eglBindAPI(api)) {
5137 fprintf(stderr, "failed to bind EGL client API\n");
5141 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5142 &d->argb_config, 1, &n) || n != 1) {
5143 fprintf(stderr, "failed to choose argb EGL config\n");
5147 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5148 EGL_NO_CONTEXT, context_attribs);
5149 if (d->argb_ctx == NULL) {
5150 fprintf(stderr, "failed to create EGL context\n");
5154 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5155 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5156 fprintf(stderr, "failed to get cairo EGL argb device\n");
5164 fini_egl(struct display *display)
5166 cairo_device_destroy(display->argb_device);
5168 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5171 eglTerminate(display->dpy);
5177 init_dummy_surface(struct display *display)
5182 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5184 display->dummy_surface =
5185 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5187 display->dummy_surface_data = data;
5191 handle_display_data(struct task *task, uint32_t events)
5193 struct display *display =
5194 container_of(task, struct display, display_task);
5195 struct epoll_event ep;
5198 display->display_fd_events = events;
5200 if (events & EPOLLERR || events & EPOLLHUP) {
5201 display_exit(display);
5205 if (events & EPOLLIN) {
5206 ret = wl_display_dispatch(display->display);
5208 display_exit(display);
5213 if (events & EPOLLOUT) {
5214 ret = wl_display_flush(display->display);
5216 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5217 ep.data.ptr = &display->display_task;
5218 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5219 display->display_fd, &ep);
5220 } else if (ret == -1 && errno != EAGAIN) {
5221 display_exit(display);
5228 log_handler(const char *format, va_list args)
5230 vfprintf(stderr, format, args);
5234 display_create(int *argc, char *argv[])
5238 wl_log_set_handler_client(log_handler);
5240 d = zalloc(sizeof *d);
5244 d->display = wl_display_connect(NULL);
5245 if (d->display == NULL) {
5246 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5251 d->xkb_context = xkb_context_new(0);
5252 if (d->xkb_context == NULL) {
5253 fprintf(stderr, "Failed to create XKB context\n");
5258 d->epoll_fd = os_epoll_create_cloexec();
5259 d->display_fd = wl_display_get_fd(d->display);
5260 d->display_task.run = handle_display_data;
5261 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5264 wl_list_init(&d->deferred_list);
5265 wl_list_init(&d->input_list);
5266 wl_list_init(&d->output_list);
5267 wl_list_init(&d->global_list);
5270 d->workspace_count = 1;
5272 d->registry = wl_display_get_registry(d->display);
5273 wl_registry_add_listener(d->registry, ®istry_listener, d);
5275 if (wl_display_dispatch(d->display) < 0) {
5276 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5280 #ifdef HAVE_CAIRO_EGL
5281 if (init_egl(d) < 0)
5282 fprintf(stderr, "EGL does not seem to work, "
5283 "falling back to software rendering and wl_shm.\n");
5288 d->theme = theme_create();
5290 wl_list_init(&d->window_list);
5292 init_dummy_surface(d);
5298 display_destroy_outputs(struct display *display)
5301 struct output *output;
5303 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5304 output_destroy(output);
5308 display_destroy_inputs(struct display *display)
5311 struct input *input;
5313 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5314 input_destroy(input);
5318 display_destroy(struct display *display)
5320 if (!wl_list_empty(&display->window_list))
5321 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5322 wl_list_length(&display->window_list));
5324 if (!wl_list_empty(&display->deferred_list))
5325 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5327 cairo_surface_destroy(display->dummy_surface);
5328 free(display->dummy_surface_data);
5330 display_destroy_outputs(display);
5331 display_destroy_inputs(display);
5333 xkb_context_unref(display->xkb_context);
5335 theme_destroy(display->theme);
5336 destroy_cursors(display);
5338 #ifdef HAVE_CAIRO_EGL
5339 if (display->argb_device)
5343 if (display->subcompositor)
5344 wl_subcompositor_destroy(display->subcompositor);
5346 if (display->xdg_shell)
5347 xdg_shell_destroy(display->xdg_shell);
5350 wl_shm_destroy(display->shm);
5352 if (display->data_device_manager)
5353 wl_data_device_manager_destroy(display->data_device_manager);
5355 wl_compositor_destroy(display->compositor);
5356 wl_registry_destroy(display->registry);
5358 close(display->epoll_fd);
5360 if (!(display->display_fd_events & EPOLLERR) &&
5361 !(display->display_fd_events & EPOLLHUP))
5362 wl_display_flush(display->display);
5364 wl_display_disconnect(display->display);
5369 display_set_user_data(struct display *display, void *data)
5371 display->user_data = data;
5375 display_get_user_data(struct display *display)
5377 return display->user_data;
5381 display_get_display(struct display *display)
5383 return display->display;
5387 display_has_subcompositor(struct display *display)
5389 if (display->subcompositor)
5392 wl_display_roundtrip(display->display);
5394 return display->subcompositor != NULL;
5398 display_get_cairo_device(struct display *display)
5400 return display->argb_device;
5404 display_get_output(struct display *display)
5406 return container_of(display->output_list.next, struct output, link);
5409 struct wl_compositor *
5410 display_get_compositor(struct display *display)
5412 return display->compositor;
5416 display_get_serial(struct display *display)
5418 return display->serial;
5422 display_get_egl_display(struct display *d)
5427 struct wl_data_source *
5428 display_create_data_source(struct display *display)
5430 if (display->data_device_manager)
5431 return wl_data_device_manager_create_data_source(display->data_device_manager);
5437 display_get_argb_egl_config(struct display *d)
5439 return d->argb_config;
5443 display_acquire_window_surface(struct display *display,
5444 struct window *window,
5447 struct surface *surface = window->main_surface;
5449 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5452 widget_get_cairo_surface(window->main_surface->widget);
5453 return surface->toysurface->acquire(surface->toysurface, ctx);
5457 display_release_window_surface(struct display *display,
5458 struct window *window)
5460 struct surface *surface = window->main_surface;
5462 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5465 surface->toysurface->release(surface->toysurface);
5469 display_defer(struct display *display, struct task *task)
5471 wl_list_insert(&display->deferred_list, &task->link);
5475 display_watch_fd(struct display *display,
5476 int fd, uint32_t events, struct task *task)
5478 struct epoll_event ep;
5482 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5486 display_unwatch_fd(struct display *display, int fd)
5488 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5492 display_run(struct display *display)
5495 struct epoll_event ep[16];
5498 display->running = 1;
5500 while (!wl_list_empty(&display->deferred_list)) {
5501 task = container_of(display->deferred_list.prev,
5503 wl_list_remove(&task->link);
5507 wl_display_dispatch_pending(display->display);
5509 if (!display->running)
5512 ret = wl_display_flush(display->display);
5513 if (ret < 0 && errno == EAGAIN) {
5515 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5516 ep[0].data.ptr = &display->display_task;
5518 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5519 display->display_fd, &ep[0]);
5520 } else if (ret < 0) {
5524 count = epoll_wait(display->epoll_fd,
5525 ep, ARRAY_LENGTH(ep), -1);
5526 for (i = 0; i < count; i++) {
5527 task = ep[i].data.ptr;
5528 task->run(task, ep[i].events);
5534 display_exit(struct display *display)
5536 display->running = 0;
5540 keysym_modifiers_add(struct wl_array *modifiers_map,
5543 size_t len = strlen(name) + 1;
5546 p = wl_array_add(modifiers_map, len);
5551 strncpy(p, name, len);
5554 static xkb_mod_index_t
5555 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5558 xkb_mod_index_t index = 0;
5559 char *p = modifiers_map->data;
5561 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5562 if (strcmp(p, name) == 0)
5569 return XKB_MOD_INVALID;
5573 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5576 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5578 if (index == XKB_MOD_INVALID)
5579 return XKB_MOD_INVALID;
5585 fail_on_null(void *p)
5588 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5598 return fail_on_null(malloc(s));
5604 return fail_on_null(zalloc(s));
5608 xstrdup(const char *s)
5610 return fail_on_null(strdup(s));
5614 xrealloc(char *p, size_t s)
5616 return fail_on_null(realloc(p, s));