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;
352 display_output_handler_t destroy_handler;
356 struct window_frame {
357 struct widget *widget;
358 struct widget *child;
363 struct window *window;
364 struct window *parent;
365 struct widget *widget;
368 const char **entries;
377 struct widget *parent;
378 struct widget *widget;
380 struct task tooltip_task;
386 struct wl_shm_pool *pool;
393 CURSOR_DEFAULT = 100,
397 static const cairo_user_data_key_t shm_surface_data_key;
404 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
405 __attribute__ ((format (printf, 4, 5)));
408 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
413 gettimeofday(&tv, NULL);
414 fprintf(stderr, "%8ld.%03ld ",
415 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
418 fprintf(stderr, "%s@%d ",
419 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
421 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
422 fprintf(stderr, "%s ", func);
425 vfprintf(stderr, fmt, ap);
429 #define DBG(fmt, ...) \
430 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
432 #define DBG_OBJ(obj, fmt, ...) \
433 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
437 #define DBG(...) do {} while (0)
438 #define DBG_OBJ(...) do {} while (0)
443 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
447 switch (buffer_transform) {
448 case WL_OUTPUT_TRANSFORM_90:
449 case WL_OUTPUT_TRANSFORM_270:
450 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
451 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
460 *width *= buffer_scale;
461 *height *= buffer_scale;
465 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
469 switch (buffer_transform) {
470 case WL_OUTPUT_TRANSFORM_90:
471 case WL_OUTPUT_TRANSFORM_270:
472 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
473 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 *width /= buffer_scale;
483 *height /= buffer_scale;
486 #ifdef HAVE_CAIRO_EGL
488 struct egl_window_surface {
489 struct toysurface base;
490 cairo_surface_t *cairo_surface;
491 struct display *display;
492 struct wl_surface *surface;
493 struct wl_egl_window *egl_window;
494 EGLSurface egl_surface;
497 static struct egl_window_surface *
498 to_egl_window_surface(struct toysurface *base)
500 return container_of(base, struct egl_window_surface, base);
503 static cairo_surface_t *
504 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
505 int32_t width, int32_t height, uint32_t flags,
506 enum wl_output_transform buffer_transform, int32_t buffer_scale)
508 struct egl_window_surface *surface = to_egl_window_surface(base);
510 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
512 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
513 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
515 return cairo_surface_reference(surface->cairo_surface);
519 egl_window_surface_swap(struct toysurface *base,
520 enum wl_output_transform buffer_transform, int32_t buffer_scale,
521 struct rectangle *server_allocation)
523 struct egl_window_surface *surface = to_egl_window_surface(base);
525 cairo_gl_surface_swapbuffers(surface->cairo_surface);
526 wl_egl_window_get_attached_size(surface->egl_window,
527 &server_allocation->width,
528 &server_allocation->height);
530 buffer_to_surface_size (buffer_transform, buffer_scale,
531 &server_allocation->width,
532 &server_allocation->height);
536 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
538 struct egl_window_surface *surface = to_egl_window_surface(base);
539 cairo_device_t *device;
541 device = cairo_surface_get_device(surface->cairo_surface);
546 if (device == surface->display->argb_device)
547 ctx = surface->display->argb_ctx;
552 cairo_device_flush(device);
553 cairo_device_acquire(device);
554 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
555 surface->egl_surface, ctx))
556 fprintf(stderr, "failed to make surface current\n");
562 egl_window_surface_release(struct toysurface *base)
564 struct egl_window_surface *surface = to_egl_window_surface(base);
565 cairo_device_t *device;
567 device = cairo_surface_get_device(surface->cairo_surface);
571 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
572 surface->display->argb_ctx))
573 fprintf(stderr, "failed to make context current\n");
575 cairo_device_release(device);
579 egl_window_surface_destroy(struct toysurface *base)
581 struct egl_window_surface *surface = to_egl_window_surface(base);
582 struct display *d = surface->display;
584 cairo_surface_destroy(surface->cairo_surface);
585 eglDestroySurface(d->dpy, surface->egl_surface);
586 wl_egl_window_destroy(surface->egl_window);
587 surface->surface = NULL;
592 static struct toysurface *
593 egl_window_surface_create(struct display *display,
594 struct wl_surface *wl_surface,
596 struct rectangle *rectangle)
598 struct egl_window_surface *surface;
600 if (display->dpy == EGL_NO_DISPLAY)
603 surface = calloc(1, sizeof *surface);
607 surface->base.prepare = egl_window_surface_prepare;
608 surface->base.swap = egl_window_surface_swap;
609 surface->base.acquire = egl_window_surface_acquire;
610 surface->base.release = egl_window_surface_release;
611 surface->base.destroy = egl_window_surface_destroy;
613 surface->display = display;
614 surface->surface = wl_surface;
616 surface->egl_window = wl_egl_window_create(surface->surface,
620 surface->egl_surface = eglCreateWindowSurface(display->dpy,
621 display->argb_config,
625 surface->cairo_surface =
626 cairo_gl_surface_create_for_egl(display->argb_device,
627 surface->egl_surface,
631 return &surface->base;
636 static struct toysurface *
637 egl_window_surface_create(struct display *display,
638 struct wl_surface *wl_surface,
640 struct rectangle *rectangle)
647 struct shm_surface_data {
648 struct wl_buffer *buffer;
649 struct shm_pool *pool;
653 display_get_buffer_for_surface(struct display *display,
654 cairo_surface_t *surface)
656 struct shm_surface_data *data;
658 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
664 shm_pool_destroy(struct shm_pool *pool);
667 shm_surface_data_destroy(void *p)
669 struct shm_surface_data *data = p;
671 wl_buffer_destroy(data->buffer);
673 shm_pool_destroy(data->pool);
678 static struct wl_shm_pool *
679 make_shm_pool(struct display *display, int size, void **data)
681 struct wl_shm_pool *pool;
684 fd = os_create_anonymous_file(size);
686 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
691 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
692 if (*data == MAP_FAILED) {
693 fprintf(stderr, "mmap failed: %m\n");
698 pool = wl_shm_create_pool(display->shm, fd, size);
705 static struct shm_pool *
706 shm_pool_create(struct display *display, size_t size)
708 struct shm_pool *pool = malloc(sizeof *pool);
713 pool->pool = make_shm_pool(display, size, &pool->data);
726 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
728 if (pool->used + size > pool->size)
731 *offset = pool->used;
734 return (char *) pool->data + *offset;
737 /* destroy the pool. this does not unmap the memory though */
739 shm_pool_destroy(struct shm_pool *pool)
741 munmap(pool->data, pool->size);
742 wl_shm_pool_destroy(pool->pool);
746 /* Start allocating from the beginning of the pool again */
748 shm_pool_reset(struct shm_pool *pool)
754 data_length_for_shm_surface(struct rectangle *rect)
758 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
760 return stride * rect->height;
763 static cairo_surface_t *
764 display_create_shm_surface_from_pool(struct display *display,
765 struct rectangle *rectangle,
766 uint32_t flags, struct shm_pool *pool)
768 struct shm_surface_data *data;
770 cairo_surface_t *surface;
771 cairo_format_t cairo_format;
772 int stride, length, offset;
775 data = malloc(sizeof *data);
779 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
780 cairo_format = CAIRO_FORMAT_RGB16_565;
782 cairo_format = CAIRO_FORMAT_ARGB32;
784 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
785 length = stride * rectangle->height;
787 map = shm_pool_allocate(pool, length, &offset);
794 surface = cairo_image_surface_create_for_data (map,
800 cairo_surface_set_user_data(surface, &shm_surface_data_key,
801 data, shm_surface_data_destroy);
803 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
804 format = WL_SHM_FORMAT_RGB565;
806 if (flags & SURFACE_OPAQUE)
807 format = WL_SHM_FORMAT_XRGB8888;
809 format = WL_SHM_FORMAT_ARGB8888;
812 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
820 static cairo_surface_t *
821 display_create_shm_surface(struct display *display,
822 struct rectangle *rectangle, uint32_t flags,
823 struct shm_pool *alternate_pool,
824 struct shm_surface_data **data_ret)
826 struct shm_surface_data *data;
827 struct shm_pool *pool;
828 cairo_surface_t *surface;
830 if (alternate_pool) {
831 shm_pool_reset(alternate_pool);
832 surface = display_create_shm_surface_from_pool(display,
837 data = cairo_surface_get_user_data(surface,
838 &shm_surface_data_key);
843 pool = shm_pool_create(display,
844 data_length_for_shm_surface(rectangle));
849 display_create_shm_surface_from_pool(display, rectangle,
853 shm_pool_destroy(pool);
857 /* make sure we destroy the pool when the surface is destroyed */
858 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
869 check_size(struct rectangle *rect)
871 if (rect->width && rect->height)
874 fprintf(stderr, "tried to create surface of "
875 "width: %d, height: %d\n", rect->width, rect->height);
880 display_create_surface(struct display *display,
881 struct wl_surface *surface,
882 struct rectangle *rectangle,
885 if (check_size(rectangle) < 0)
888 assert(flags & SURFACE_SHM);
889 return display_create_shm_surface(display, rectangle, flags,
893 struct shm_surface_leaf {
894 cairo_surface_t *cairo_surface;
895 /* 'data' is automatically destroyed, when 'cairo_surface' is */
896 struct shm_surface_data *data;
898 struct shm_pool *resize_pool;
903 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
905 if (leaf->cairo_surface)
906 cairo_surface_destroy(leaf->cairo_surface);
907 /* leaf->data already destroyed via cairo private */
909 if (leaf->resize_pool)
910 shm_pool_destroy(leaf->resize_pool);
912 memset(leaf, 0, sizeof *leaf);
918 struct toysurface base;
919 struct display *display;
920 struct wl_surface *surface;
924 struct shm_surface_leaf leaf[MAX_LEAVES];
925 struct shm_surface_leaf *current;
928 static struct shm_surface *
929 to_shm_surface(struct toysurface *base)
931 return container_of(base, struct shm_surface, base);
935 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
938 struct shm_surface_leaf *leaf;
939 char bufs[MAX_LEAVES + 1];
942 for (i = 0; i < MAX_LEAVES; i++) {
943 leaf = &surface->leaf[i];
947 else if (leaf->cairo_surface)
953 bufs[MAX_LEAVES] = '\0';
954 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
959 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
961 struct shm_surface *surface = data;
962 struct shm_surface_leaf *leaf;
966 shm_surface_buffer_state_debug(surface, "buffer_release before");
968 for (i = 0; i < MAX_LEAVES; i++) {
969 leaf = &surface->leaf[i];
970 if (leaf->data && leaf->data->buffer == buffer) {
975 assert(i < MAX_LEAVES && "unknown buffer released");
977 /* Leave one free leaf with storage, release others */
979 for (i = 0; i < MAX_LEAVES; i++) {
980 leaf = &surface->leaf[i];
982 if (!leaf->cairo_surface || leaf->busy)
988 shm_surface_leaf_release(leaf);
991 shm_surface_buffer_state_debug(surface, "buffer_release after");
994 static const struct wl_buffer_listener shm_surface_buffer_listener = {
995 shm_surface_buffer_release
998 static cairo_surface_t *
999 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1000 int32_t width, int32_t height, uint32_t flags,
1001 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1003 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1004 struct shm_surface *surface = to_shm_surface(base);
1005 struct rectangle rect = { 0};
1006 struct shm_surface_leaf *leaf = NULL;
1012 /* pick a free buffer, preferrably one that already has storage */
1013 for (i = 0; i < MAX_LEAVES; i++) {
1014 if (surface->leaf[i].busy)
1017 if (!leaf || surface->leaf[i].cairo_surface)
1018 leaf = &surface->leaf[i];
1020 DBG_OBJ(surface->surface, "pick leaf %d\n",
1021 (int)(leaf - &surface->leaf[0]));
1024 fprintf(stderr, "%s: all buffers are held by the server.\n",
1030 if (!resize_hint && leaf->resize_pool) {
1031 cairo_surface_destroy(leaf->cairo_surface);
1032 leaf->cairo_surface = NULL;
1033 shm_pool_destroy(leaf->resize_pool);
1034 leaf->resize_pool = NULL;
1037 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1039 if (leaf->cairo_surface &&
1040 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1041 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1044 if (leaf->cairo_surface)
1045 cairo_surface_destroy(leaf->cairo_surface);
1047 #ifdef USE_RESIZE_POOL
1048 if (resize_hint && !leaf->resize_pool) {
1049 /* Create a big pool to allocate from, while continuously
1050 * resizing. Mmapping a new pool in the server
1051 * is relatively expensive, so reusing a pool performs
1052 * better, but may temporarily reserve unneeded memory.
1054 /* We should probably base this number on the output size. */
1055 leaf->resize_pool = shm_pool_create(surface->display,
1061 rect.height = height;
1063 leaf->cairo_surface =
1064 display_create_shm_surface(surface->display, &rect,
1068 if (!leaf->cairo_surface)
1071 wl_buffer_add_listener(leaf->data->buffer,
1072 &shm_surface_buffer_listener, surface);
1075 surface->current = leaf;
1077 return cairo_surface_reference(leaf->cairo_surface);
1081 shm_surface_swap(struct toysurface *base,
1082 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1083 struct rectangle *server_allocation)
1085 struct shm_surface *surface = to_shm_surface(base);
1086 struct shm_surface_leaf *leaf = surface->current;
1088 server_allocation->width =
1089 cairo_image_surface_get_width(leaf->cairo_surface);
1090 server_allocation->height =
1091 cairo_image_surface_get_height(leaf->cairo_surface);
1093 buffer_to_surface_size (buffer_transform, buffer_scale,
1094 &server_allocation->width,
1095 &server_allocation->height);
1097 wl_surface_attach(surface->surface, leaf->data->buffer,
1098 surface->dx, surface->dy);
1099 wl_surface_damage(surface->surface, 0, 0,
1100 server_allocation->width, server_allocation->height);
1101 wl_surface_commit(surface->surface);
1103 DBG_OBJ(surface->surface, "leaf %d busy\n",
1104 (int)(leaf - &surface->leaf[0]));
1107 surface->current = NULL;
1111 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1117 shm_surface_release(struct toysurface *base)
1122 shm_surface_destroy(struct toysurface *base)
1124 struct shm_surface *surface = to_shm_surface(base);
1127 for (i = 0; i < MAX_LEAVES; i++)
1128 shm_surface_leaf_release(&surface->leaf[i]);
1133 static struct toysurface *
1134 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1135 uint32_t flags, struct rectangle *rectangle)
1137 struct shm_surface *surface;
1138 DBG_OBJ(wl_surface, "\n");
1140 surface = xmalloc(sizeof *surface);
1141 memset(surface, 0, sizeof *surface);
1146 surface->base.prepare = shm_surface_prepare;
1147 surface->base.swap = shm_surface_swap;
1148 surface->base.acquire = shm_surface_acquire;
1149 surface->base.release = shm_surface_release;
1150 surface->base.destroy = shm_surface_destroy;
1152 surface->display = display;
1153 surface->surface = wl_surface;
1154 surface->flags = flags;
1156 return &surface->base;
1160 * The following correspondences between file names and cursors was copied
1161 * from: https://bugs.kde.org/attachment.cgi?id=67313
1164 static const char *bottom_left_corners[] = {
1165 "bottom_left_corner",
1170 static const char *bottom_right_corners[] = {
1171 "bottom_right_corner",
1176 static const char *bottom_sides[] = {
1182 static const char *grabbings[] = {
1185 "208530c400c041818281048008011002"
1188 static const char *left_ptrs[] = {
1195 static const char *left_sides[] = {
1201 static const char *right_sides[] = {
1207 static const char *top_left_corners[] = {
1213 static const char *top_right_corners[] = {
1219 static const char *top_sides[] = {
1225 static const char *xterms[] = {
1231 static const char *hand1s[] = {
1235 "e29285e634086352946a0e7090d73106"
1238 static const char *watches[] = {
1241 "0426c94ea35c87780ff01dc239897213"
1244 struct cursor_alternatives {
1249 static const struct cursor_alternatives cursors[] = {
1250 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1251 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1252 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1253 {grabbings, ARRAY_LENGTH(grabbings)},
1254 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1255 {left_sides, ARRAY_LENGTH(left_sides)},
1256 {right_sides, ARRAY_LENGTH(right_sides)},
1257 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1258 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1259 {top_sides, ARRAY_LENGTH(top_sides)},
1260 {xterms, ARRAY_LENGTH(xterms)},
1261 {hand1s, ARRAY_LENGTH(hand1s)},
1262 {watches, ARRAY_LENGTH(watches)},
1266 create_cursors(struct display *display)
1268 struct weston_config *config;
1269 struct weston_config_section *s;
1273 struct wl_cursor *cursor;
1275 config = weston_config_parse("weston.ini");
1276 s = weston_config_get_section(config, "shell", NULL, NULL);
1277 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1278 weston_config_section_get_int(s, "cursor-size", &size, 32);
1279 weston_config_destroy(config);
1281 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1282 if (!display->cursor_theme) {
1283 fprintf(stderr, "could not load theme '%s'\n", theme);
1288 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1290 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1292 for (j = 0; !cursor && j < cursors[i].count; ++j)
1293 cursor = wl_cursor_theme_get_cursor(
1294 display->cursor_theme, cursors[i].names[j]);
1297 fprintf(stderr, "could not load cursor '%s'\n",
1298 cursors[i].names[0]);
1300 display->cursors[i] = cursor;
1305 destroy_cursors(struct display *display)
1307 wl_cursor_theme_destroy(display->cursor_theme);
1308 free(display->cursors);
1311 struct wl_cursor_image *
1312 display_get_pointer_image(struct display *display, int pointer)
1314 struct wl_cursor *cursor = display->cursors[pointer];
1316 return cursor ? cursor->images[0] : NULL;
1320 surface_flush(struct surface *surface)
1322 if (!surface->cairo_surface)
1325 if (surface->opaque_region) {
1326 wl_surface_set_opaque_region(surface->surface,
1327 surface->opaque_region);
1328 wl_region_destroy(surface->opaque_region);
1329 surface->opaque_region = NULL;
1332 if (surface->input_region) {
1333 wl_surface_set_input_region(surface->surface,
1334 surface->input_region);
1335 wl_region_destroy(surface->input_region);
1336 surface->input_region = NULL;
1339 surface->toysurface->swap(surface->toysurface,
1340 surface->buffer_transform, surface->buffer_scale,
1341 &surface->server_allocation);
1343 cairo_surface_destroy(surface->cairo_surface);
1344 surface->cairo_surface = NULL;
1348 window_has_focus(struct window *window)
1350 return window->focused;
1354 window_close(struct window *window)
1356 if (window->close_handler)
1357 window->close_handler(window->user_data);
1359 display_exit(window->display);
1363 window_get_display(struct window *window)
1365 return window->display;
1369 surface_create_surface(struct surface *surface, uint32_t flags)
1371 struct display *display = surface->window->display;
1372 struct rectangle allocation = surface->allocation;
1374 if (!surface->toysurface && display->dpy &&
1375 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1376 surface->toysurface =
1377 egl_window_surface_create(display,
1383 if (!surface->toysurface)
1384 surface->toysurface = shm_surface_create(display,
1386 flags, &allocation);
1388 surface->cairo_surface = surface->toysurface->prepare(
1389 surface->toysurface, 0, 0,
1390 allocation.width, allocation.height, flags,
1391 surface->buffer_transform, surface->buffer_scale);
1395 window_create_main_surface(struct window *window)
1397 struct surface *surface = window->main_surface;
1400 if (window->resizing)
1401 flags |= SURFACE_HINT_RESIZE;
1403 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1404 flags |= SURFACE_HINT_RGB565;
1406 surface_create_surface(surface, flags);
1410 window_get_buffer_transform(struct window *window)
1412 return window->main_surface->buffer_transform;
1416 window_set_buffer_transform(struct window *window,
1417 enum wl_output_transform transform)
1419 window->main_surface->buffer_transform = transform;
1420 wl_surface_set_buffer_transform(window->main_surface->surface,
1425 window_set_buffer_scale(struct window *window,
1428 window->main_surface->buffer_scale = scale;
1429 wl_surface_set_buffer_scale(window->main_surface->surface,
1434 window_get_buffer_scale(struct window *window)
1436 return window->main_surface->buffer_scale;
1440 window_get_output_scale(struct window *window)
1442 struct window_output *window_output;
1443 struct window_output *window_output_tmp;
1446 wl_list_for_each_safe(window_output, window_output_tmp,
1447 &window->window_output_list, link) {
1448 if (window_output->output->scale > scale)
1449 scale = window_output->output->scale;
1455 static void window_frame_destroy(struct window_frame *frame);
1458 surface_destroy(struct surface *surface)
1460 if (surface->frame_cb)
1461 wl_callback_destroy(surface->frame_cb);
1463 if (surface->input_region)
1464 wl_region_destroy(surface->input_region);
1466 if (surface->opaque_region)
1467 wl_region_destroy(surface->opaque_region);
1469 if (surface->subsurface)
1470 wl_subsurface_destroy(surface->subsurface);
1472 wl_surface_destroy(surface->surface);
1474 if (surface->toysurface)
1475 surface->toysurface->destroy(surface->toysurface);
1477 wl_list_remove(&surface->link);
1482 window_destroy(struct window *window)
1484 struct display *display = window->display;
1485 struct input *input;
1486 struct window_output *window_output;
1487 struct window_output *window_output_tmp;
1489 wl_list_remove(&window->redraw_task.link);
1491 wl_list_for_each(input, &display->input_list, link) {
1492 if (input->touch_focus == window)
1493 input->touch_focus = NULL;
1494 if (input->pointer_focus == window)
1495 input->pointer_focus = NULL;
1496 if (input->keyboard_focus == window)
1497 input->keyboard_focus = NULL;
1498 if (input->focus_widget &&
1499 input->focus_widget->window == window)
1500 input->focus_widget = NULL;
1503 wl_list_for_each_safe(window_output, window_output_tmp,
1504 &window->window_output_list, link) {
1505 free (window_output);
1509 window_frame_destroy(window->frame);
1511 if (window->xdg_surface)
1512 xdg_surface_destroy(window->xdg_surface);
1513 if (window->xdg_popup)
1514 xdg_popup_destroy(window->xdg_popup);
1516 surface_destroy(window->main_surface);
1518 wl_list_remove(&window->link);
1520 free(window->title);
1524 static struct widget *
1525 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1527 struct widget *child, *target;
1529 wl_list_for_each(child, &widget->child_list, link) {
1530 target = widget_find_widget(child, x, y);
1535 if (widget->allocation.x <= x &&
1536 x < widget->allocation.x + widget->allocation.width &&
1537 widget->allocation.y <= y &&
1538 y < widget->allocation.y + widget->allocation.height) {
1545 static struct widget *
1546 window_find_widget(struct window *window, int32_t x, int32_t y)
1548 struct surface *surface;
1549 struct widget *widget;
1551 wl_list_for_each(surface, &window->subsurface_list, link) {
1552 widget = widget_find_widget(surface->widget, x, y);
1560 static struct widget *
1561 widget_create(struct window *window, struct surface *surface, void *data)
1563 struct widget *widget;
1565 widget = xzalloc(sizeof *widget);
1566 widget->window = window;
1567 widget->surface = surface;
1568 widget->user_data = data;
1569 widget->allocation = surface->allocation;
1570 wl_list_init(&widget->child_list);
1572 widget->tooltip = NULL;
1573 widget->tooltip_count = 0;
1574 widget->default_cursor = CURSOR_LEFT_PTR;
1575 widget->use_cairo = 1;
1581 window_add_widget(struct window *window, void *data)
1583 struct widget *widget;
1585 widget = widget_create(window, window->main_surface, data);
1586 wl_list_init(&widget->link);
1587 window->main_surface->widget = widget;
1593 widget_add_widget(struct widget *parent, void *data)
1595 struct widget *widget;
1597 widget = widget_create(parent->window, parent->surface, data);
1598 wl_list_insert(parent->child_list.prev, &widget->link);
1604 widget_destroy(struct widget *widget)
1606 struct display *display = widget->window->display;
1607 struct surface *surface = widget->surface;
1608 struct input *input;
1610 /* Destroy the sub-surface along with the root widget */
1611 if (surface->widget == widget && surface->subsurface)
1612 surface_destroy(widget->surface);
1614 if (widget->tooltip)
1615 widget_destroy_tooltip(widget);
1617 wl_list_for_each(input, &display->input_list, link) {
1618 if (input->focus_widget == widget)
1619 input->focus_widget = NULL;
1622 wl_list_remove(&widget->link);
1627 widget_set_default_cursor(struct widget *widget, int cursor)
1629 widget->default_cursor = cursor;
1633 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1635 *allocation = widget->allocation;
1639 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1641 widget->allocation.width = width;
1642 widget->allocation.height = height;
1646 widget_set_allocation(struct widget *widget,
1647 int32_t x, int32_t y, int32_t width, int32_t height)
1649 widget->allocation.x = x;
1650 widget->allocation.y = y;
1651 widget_set_size(widget, width, height);
1655 widget_set_transparent(struct widget *widget, int transparent)
1657 widget->opaque = !transparent;
1661 widget_get_user_data(struct widget *widget)
1663 return widget->user_data;
1666 static cairo_surface_t *
1667 widget_get_cairo_surface(struct widget *widget)
1669 struct surface *surface = widget->surface;
1670 struct window *window = widget->window;
1672 assert(widget->use_cairo);
1674 if (!surface->cairo_surface) {
1675 if (surface == window->main_surface)
1676 window_create_main_surface(window);
1678 surface_create_surface(surface, 0);
1681 return surface->cairo_surface;
1685 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1687 struct surface *surface = widget->surface;
1690 enum wl_output_transform transform;
1691 int surface_width, surface_height;
1692 int translate_x, translate_y;
1695 surface_width = surface->allocation.width;
1696 surface_height = surface->allocation.height;
1698 transform = surface->buffer_transform;
1699 scale = surface->buffer_scale;
1701 switch (transform) {
1702 case WL_OUTPUT_TRANSFORM_FLIPPED:
1703 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1704 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1705 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1706 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1709 cairo_matrix_init_identity(&m);
1713 switch (transform) {
1714 case WL_OUTPUT_TRANSFORM_NORMAL:
1720 case WL_OUTPUT_TRANSFORM_FLIPPED:
1722 translate_x = surface_width;
1725 case WL_OUTPUT_TRANSFORM_90:
1727 translate_x = surface_height;
1730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1732 translate_x = surface_height;
1733 translate_y = surface_width;
1735 case WL_OUTPUT_TRANSFORM_180:
1737 translate_x = surface_width;
1738 translate_y = surface_height;
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1743 translate_y = surface_height;
1745 case WL_OUTPUT_TRANSFORM_270:
1746 angle = M_PI + M_PI_2;
1748 translate_y = surface_width;
1750 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1751 angle = M_PI + M_PI_2;
1757 cairo_scale(cr, scale, scale);
1758 cairo_translate(cr, translate_x, translate_y);
1759 cairo_rotate(cr, angle);
1760 cairo_transform(cr, &m);
1764 widget_cairo_create(struct widget *widget)
1766 struct surface *surface = widget->surface;
1767 cairo_surface_t *cairo_surface;
1770 cairo_surface = widget_get_cairo_surface(widget);
1771 cr = cairo_create(cairo_surface);
1773 widget_cairo_update_transform(widget, cr);
1775 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1781 widget_get_wl_surface(struct widget *widget)
1783 return widget->surface->surface;
1786 struct wl_subsurface *
1787 widget_get_wl_subsurface(struct widget *widget)
1789 return widget->surface->subsurface;
1793 widget_get_last_time(struct widget *widget)
1795 return widget->surface->last_time;
1799 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1801 struct wl_compositor *comp = widget->window->display->compositor;
1802 struct surface *surface = widget->surface;
1804 if (!surface->input_region)
1805 surface->input_region = wl_compositor_create_region(comp);
1808 wl_region_add(surface->input_region,
1809 rect->x, rect->y, rect->width, rect->height);
1814 widget_set_resize_handler(struct widget *widget,
1815 widget_resize_handler_t handler)
1817 widget->resize_handler = handler;
1821 widget_set_redraw_handler(struct widget *widget,
1822 widget_redraw_handler_t handler)
1824 widget->redraw_handler = handler;
1828 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1830 widget->enter_handler = handler;
1834 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1836 widget->leave_handler = handler;
1840 widget_set_motion_handler(struct widget *widget,
1841 widget_motion_handler_t handler)
1843 widget->motion_handler = handler;
1847 widget_set_button_handler(struct widget *widget,
1848 widget_button_handler_t handler)
1850 widget->button_handler = handler;
1854 widget_set_touch_up_handler(struct widget *widget,
1855 widget_touch_up_handler_t handler)
1857 widget->touch_up_handler = handler;
1861 widget_set_touch_down_handler(struct widget *widget,
1862 widget_touch_down_handler_t handler)
1864 widget->touch_down_handler = handler;
1868 widget_set_touch_motion_handler(struct widget *widget,
1869 widget_touch_motion_handler_t handler)
1871 widget->touch_motion_handler = handler;
1875 widget_set_touch_frame_handler(struct widget *widget,
1876 widget_touch_frame_handler_t handler)
1878 widget->touch_frame_handler = handler;
1882 widget_set_touch_cancel_handler(struct widget *widget,
1883 widget_touch_cancel_handler_t handler)
1885 widget->touch_cancel_handler = handler;
1889 widget_set_axis_handler(struct widget *widget,
1890 widget_axis_handler_t handler)
1892 widget->axis_handler = handler;
1896 window_schedule_redraw_task(struct window *window);
1899 widget_schedule_redraw(struct widget *widget)
1901 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1902 widget->surface->redraw_needed = 1;
1903 window_schedule_redraw_task(widget->window);
1907 widget_set_use_cairo(struct widget *widget,
1910 widget->use_cairo = use_cairo;
1914 window_get_surface(struct window *window)
1916 cairo_surface_t *cairo_surface;
1918 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1920 return cairo_surface_reference(cairo_surface);
1924 window_get_wl_surface(struct window *window)
1926 return window->main_surface->surface;
1930 tooltip_redraw_handler(struct widget *widget, void *data)
1933 const int32_t r = 3;
1934 struct tooltip *tooltip = data;
1935 int32_t width, height;
1937 cr = widget_cairo_create(widget);
1938 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1939 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1940 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1943 width = widget->allocation.width;
1944 height = widget->allocation.height;
1945 rounded_rect(cr, 0, 0, width, height, r);
1947 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1948 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1951 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1952 cairo_move_to(cr, 10, 16);
1953 cairo_show_text(cr, tooltip->entry);
1957 static cairo_text_extents_t
1958 get_text_extents(struct display *display, struct tooltip *tooltip)
1961 cairo_text_extents_t extents;
1963 /* Use the dummy_surface because tooltip's surface was not
1964 * created yet, and parent does not have a valid surface
1965 * outside repaint, either.
1967 cr = cairo_create(display->dummy_surface);
1968 cairo_text_extents(cr, tooltip->entry, &extents);
1975 window_create_tooltip(struct tooltip *tooltip)
1977 struct widget *parent = tooltip->parent;
1978 struct display *display = parent->window->display;
1979 const int offset_y = 27;
1980 const int margin = 3;
1981 cairo_text_extents_t extents;
1983 if (tooltip->widget)
1986 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1988 extents = get_text_extents(display, tooltip);
1989 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1990 widget_set_allocation(tooltip->widget,
1991 tooltip->x, tooltip->y + offset_y,
1992 extents.width + 20, 20 + margin * 2);
1998 widget_destroy_tooltip(struct widget *parent)
2000 struct tooltip *tooltip = parent->tooltip;
2002 parent->tooltip_count = 0;
2006 if (tooltip->widget) {
2007 widget_destroy(tooltip->widget);
2008 tooltip->widget = NULL;
2011 close(tooltip->tooltip_fd);
2012 free(tooltip->entry);
2014 parent->tooltip = NULL;
2018 tooltip_func(struct task *task, uint32_t events)
2020 struct tooltip *tooltip =
2021 container_of(task, struct tooltip, tooltip_task);
2024 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2026 window_create_tooltip(tooltip);
2029 #define TOOLTIP_TIMEOUT 500
2031 tooltip_timer_reset(struct tooltip *tooltip)
2033 struct itimerspec its;
2035 its.it_interval.tv_sec = 0;
2036 its.it_interval.tv_nsec = 0;
2037 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2038 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2039 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2040 fprintf(stderr, "could not set timerfd\n: %m");
2048 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2050 struct tooltip *tooltip = parent->tooltip;
2052 parent->tooltip_count++;
2056 tooltip_timer_reset(tooltip);
2060 /* the handler might be triggered too fast via input device motion, so
2061 * we need this check here to make sure tooltip is fully initialized */
2062 if (parent->tooltip_count > 1)
2065 tooltip = malloc(sizeof *tooltip);
2069 parent->tooltip = tooltip;
2070 tooltip->parent = parent;
2071 tooltip->widget = NULL;
2074 tooltip->entry = strdup(entry);
2075 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2076 if (tooltip->tooltip_fd < 0) {
2077 fprintf(stderr, "could not create timerfd\n: %m");
2081 tooltip->tooltip_task.run = tooltip_func;
2082 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2083 EPOLLIN, &tooltip->tooltip_task);
2084 tooltip_timer_reset(tooltip);
2090 workspace_manager_state(void *data,
2091 struct workspace_manager *workspace_manager,
2095 struct display *display = data;
2097 display->workspace = current;
2098 display->workspace_count = count;
2101 static const struct workspace_manager_listener workspace_manager_listener = {
2102 workspace_manager_state
2106 frame_resize_handler(struct widget *widget,
2107 int32_t width, int32_t height, void *data)
2109 struct window_frame *frame = data;
2110 struct widget *child = frame->child;
2111 struct rectangle interior;
2112 struct rectangle input;
2113 struct rectangle opaque;
2115 if (widget->window->fullscreen) {
2118 interior.width = width;
2119 interior.height = height;
2121 if (widget->window->maximized) {
2122 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2124 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2127 frame_resize(frame->frame, width, height);
2128 frame_interior(frame->frame, &interior.x, &interior.y,
2129 &interior.width, &interior.height);
2132 widget_set_allocation(child, interior.x, interior.y,
2133 interior.width, interior.height);
2135 if (child->resize_handler) {
2136 child->resize_handler(child, interior.width, interior.height,
2139 if (widget->window->fullscreen) {
2140 width = child->allocation.width;
2141 height = child->allocation.height;
2143 frame_resize_inside(frame->frame,
2144 child->allocation.width,
2145 child->allocation.height);
2146 width = frame_width(frame->frame);
2147 height = frame_height(frame->frame);
2151 widget_set_allocation(widget, 0, 0, width, height);
2153 widget->surface->input_region =
2154 wl_compositor_create_region(widget->window->display->compositor);
2155 if (!widget->window->fullscreen) {
2156 frame_input_rect(frame->frame, &input.x, &input.y,
2157 &input.width, &input.height);
2158 wl_region_add(widget->surface->input_region,
2159 input.x, input.y, input.width, input.height);
2161 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2164 widget_set_allocation(widget, 0, 0, width, height);
2166 if (child->opaque) {
2167 if (!widget->window->fullscreen) {
2168 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2169 &opaque.width, &opaque.height);
2171 wl_region_add(widget->surface->opaque_region,
2173 opaque.width, opaque.height);
2175 wl_region_add(widget->surface->opaque_region,
2176 0, 0, width, height);
2181 widget_schedule_redraw(widget);
2185 frame_redraw_handler(struct widget *widget, void *data)
2188 struct window_frame *frame = data;
2189 struct window *window = widget->window;
2191 if (window->fullscreen)
2194 if (window->focused) {
2195 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2197 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2200 cr = widget_cairo_create(widget);
2202 frame_repaint(frame->frame, cr);
2208 frame_get_pointer_image_for_location(struct window_frame *frame,
2209 enum theme_location location)
2211 struct window *window = frame->widget->window;
2214 return CURSOR_LEFT_PTR;
2217 case THEME_LOCATION_RESIZING_TOP:
2219 case THEME_LOCATION_RESIZING_BOTTOM:
2220 return CURSOR_BOTTOM;
2221 case THEME_LOCATION_RESIZING_LEFT:
2223 case THEME_LOCATION_RESIZING_RIGHT:
2224 return CURSOR_RIGHT;
2225 case THEME_LOCATION_RESIZING_TOP_LEFT:
2226 return CURSOR_TOP_LEFT;
2227 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2228 return CURSOR_TOP_RIGHT;
2229 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2230 return CURSOR_BOTTOM_LEFT;
2231 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2232 return CURSOR_BOTTOM_RIGHT;
2233 case THEME_LOCATION_EXTERIOR:
2234 case THEME_LOCATION_TITLEBAR:
2236 return CURSOR_LEFT_PTR;
2241 frame_menu_func(struct window *window,
2242 struct input *input, int index, void *data)
2244 struct display *display;
2248 window_close(window);
2250 case 1: /* move to workspace above */
2251 display = window->display;
2252 if (display->workspace > 0)
2253 workspace_manager_move_surface(
2254 display->workspace_manager,
2255 window->main_surface->surface,
2256 display->workspace - 1);
2258 case 2: /* move to workspace below */
2259 display = window->display;
2260 if (display->workspace < display->workspace_count - 1)
2261 workspace_manager_move_surface(
2262 display->workspace_manager,
2263 window->main_surface->surface,
2264 display->workspace + 1);
2266 case 3: /* fullscreen */
2267 /* we don't have a way to get out of fullscreen for now */
2268 if (window->fullscreen_handler)
2269 window->fullscreen_handler(window, window->user_data);
2275 window_show_frame_menu(struct window *window,
2276 struct input *input, uint32_t time)
2281 static const char *entries[] = {
2283 "Move to workspace above", "Move to workspace below",
2287 if (window->fullscreen_handler)
2288 count = ARRAY_LENGTH(entries);
2290 count = ARRAY_LENGTH(entries) - 1;
2292 input_get_position(input, &x, &y);
2293 window_show_menu(window->display, input, time, window,
2294 x - 10, y - 10, frame_menu_func, entries, count);
2298 frame_enter_handler(struct widget *widget,
2299 struct input *input, float x, float y, void *data)
2301 struct window_frame *frame = data;
2302 enum theme_location location;
2304 location = frame_pointer_enter(frame->frame, input, x, y);
2305 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2306 widget_schedule_redraw(frame->widget);
2308 return frame_get_pointer_image_for_location(data, location);
2312 frame_motion_handler(struct widget *widget,
2313 struct input *input, uint32_t time,
2314 float x, float y, void *data)
2316 struct window_frame *frame = data;
2317 enum theme_location location;
2319 location = frame_pointer_motion(frame->frame, input, x, y);
2320 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2321 widget_schedule_redraw(frame->widget);
2323 return frame_get_pointer_image_for_location(data, location);
2327 frame_leave_handler(struct widget *widget,
2328 struct input *input, void *data)
2330 struct window_frame *frame = data;
2332 frame_pointer_leave(frame->frame, input);
2333 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2334 widget_schedule_redraw(frame->widget);
2338 frame_handle_status(struct window_frame *frame, struct input *input,
2339 uint32_t time, enum theme_location location)
2341 struct window *window = frame->widget->window;
2344 status = frame_status(frame->frame);
2345 if (status & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2348 if (status & FRAME_STATUS_MINIMIZE) {
2349 window_set_minimized(window);
2350 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2353 if (status & FRAME_STATUS_MENU) {
2354 window_show_frame_menu(window, input, time);
2355 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2358 if (status & FRAME_STATUS_MAXIMIZE) {
2359 window_set_maximized(window, !window->maximized);
2360 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2363 if (status & FRAME_STATUS_CLOSE) {
2364 window_close(window);
2368 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2369 input_ungrab(input);
2370 xdg_surface_move(window->xdg_surface,
2371 input_get_seat(input),
2372 window->display->serial);
2374 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2377 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2378 input_ungrab(input);
2380 window->resizing = 1;
2381 xdg_surface_resize(window->xdg_surface,
2382 input_get_seat(input),
2383 window->display->serial,
2386 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2391 frame_button_handler(struct widget *widget,
2392 struct input *input, uint32_t time,
2393 uint32_t button, enum wl_pointer_button_state state,
2397 struct window_frame *frame = data;
2398 enum theme_location location;
2400 location = frame_pointer_button(frame->frame, input, button, state);
2401 frame_handle_status(frame, input, time, location);
2405 frame_touch_down_handler(struct widget *widget, struct input *input,
2406 uint32_t serial, uint32_t time, int32_t id,
2407 float x, float y, void *data)
2409 struct window_frame *frame = data;
2411 frame_touch_down(frame->frame, input, id, x, y);
2412 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2416 frame_touch_up_handler(struct widget *widget,
2417 struct input *input, uint32_t serial, uint32_t time,
2418 int32_t id, void *data)
2420 struct window_frame *frame = data;
2422 frame_touch_up(frame->frame, input, id);
2423 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2427 window_frame_create(struct window *window, void *data)
2429 struct window_frame *frame;
2432 if (window->custom) {
2433 buttons = FRAME_BUTTON_NONE;
2435 buttons = FRAME_BUTTON_ALL;
2438 frame = xzalloc(sizeof *frame);
2439 frame->frame = frame_create(window->display->theme, 0, 0,
2440 buttons, window->title);
2442 frame->widget = window_add_widget(window, frame);
2443 frame->child = widget_add_widget(frame->widget, data);
2445 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2446 widget_set_resize_handler(frame->widget, frame_resize_handler);
2447 widget_set_enter_handler(frame->widget, frame_enter_handler);
2448 widget_set_leave_handler(frame->widget, frame_leave_handler);
2449 widget_set_motion_handler(frame->widget, frame_motion_handler);
2450 widget_set_button_handler(frame->widget, frame_button_handler);
2451 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2452 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2454 window->frame = frame;
2456 return frame->child;
2460 window_frame_set_child_size(struct widget *widget, int child_width,
2463 struct display *display = widget->window->display;
2464 struct theme *t = display->theme;
2465 int decoration_width, decoration_height;
2467 int margin = widget->window->maximized ? 0 : t->margin;
2469 if (!widget->window->fullscreen) {
2470 decoration_width = (t->width + margin) * 2;
2471 decoration_height = t->width +
2472 t->titlebar_height + margin * 2;
2474 width = child_width + decoration_width;
2475 height = child_height + decoration_height;
2477 width = child_width;
2478 height = child_height;
2481 window_schedule_resize(widget->window, width, height);
2485 window_frame_destroy(struct window_frame *frame)
2487 frame_destroy(frame->frame);
2489 /* frame->child must be destroyed by the application */
2490 widget_destroy(frame->widget);
2495 input_set_focus_widget(struct input *input, struct widget *focus,
2498 struct widget *old, *widget;
2501 if (focus == input->focus_widget)
2504 old = input->focus_widget;
2508 widget = input->grab;
2509 if (widget->leave_handler)
2510 widget->leave_handler(old, input, widget->user_data);
2511 input->focus_widget = NULL;
2517 widget = input->grab;
2518 input->focus_widget = focus;
2519 if (widget->enter_handler)
2520 cursor = widget->enter_handler(focus, input, x, y,
2523 cursor = widget->default_cursor;
2525 input_set_pointer_image(input, cursor);
2530 touch_grab(struct input *input, int32_t touch_id)
2532 input->touch_grab = 1;
2533 input->touch_grab_id = touch_id;
2537 touch_ungrab(struct input *input)
2539 struct touch_point *tp, *tmp;
2541 input->touch_grab = 0;
2543 wl_list_for_each_safe(tp, tmp,
2544 &input->touch_point_list, link) {
2545 if (tp->id != input->touch_grab_id)
2547 wl_list_remove(&tp->link);
2555 input_grab(struct input *input, struct widget *widget, uint32_t button)
2557 input->grab = widget;
2558 input->grab_button = button;
2562 input_ungrab(struct input *input)
2564 struct widget *widget;
2567 if (input->pointer_focus) {
2568 widget = window_find_widget(input->pointer_focus,
2569 input->sx, input->sy);
2570 input_set_focus_widget(input, widget, input->sx, input->sy);
2575 input_remove_pointer_focus(struct input *input)
2577 struct window *window = input->pointer_focus;
2582 input_set_focus_widget(input, NULL, 0, 0);
2584 input->pointer_focus = NULL;
2585 input->current_cursor = CURSOR_UNSET;
2589 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2590 uint32_t serial, struct wl_surface *surface,
2591 wl_fixed_t sx_w, wl_fixed_t sy_w)
2593 struct input *input = data;
2594 struct window *window;
2595 struct widget *widget;
2596 float sx = wl_fixed_to_double(sx_w);
2597 float sy = wl_fixed_to_double(sy_w);
2600 /* enter event for a window we've just destroyed */
2604 input->display->serial = serial;
2605 input->pointer_enter_serial = serial;
2606 input->pointer_focus = wl_surface_get_user_data(surface);
2607 window = input->pointer_focus;
2609 if (window->resizing) {
2610 window->resizing = 0;
2611 /* Schedule a redraw to free the pool */
2612 window_schedule_redraw(window);
2618 widget = window_find_widget(window, sx, sy);
2619 input_set_focus_widget(input, widget, sx, sy);
2623 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2624 uint32_t serial, struct wl_surface *surface)
2626 struct input *input = data;
2628 input->display->serial = serial;
2629 input_remove_pointer_focus(input);
2633 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2634 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2636 struct input *input = data;
2637 struct window *window = input->pointer_focus;
2638 struct widget *widget;
2640 float sx = wl_fixed_to_double(sx_w);
2641 float sy = wl_fixed_to_double(sy_w);
2649 /* when making the window smaller - e.g. after a unmaximise we might
2650 * still have a pending motion event that the compositor has picked
2651 * based on the old surface dimensions
2653 if (sx > window->main_surface->allocation.width ||
2654 sy > window->main_surface->allocation.height)
2657 if (!(input->grab && input->grab_button)) {
2658 widget = window_find_widget(window, sx, sy);
2659 input_set_focus_widget(input, widget, sx, sy);
2663 widget = input->grab;
2665 widget = input->focus_widget;
2667 if (widget->motion_handler)
2668 cursor = widget->motion_handler(input->focus_widget,
2669 input, time, sx, sy,
2672 cursor = widget->default_cursor;
2674 cursor = CURSOR_LEFT_PTR;
2676 input_set_pointer_image(input, cursor);
2680 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2681 uint32_t time, uint32_t button, uint32_t state_w)
2683 struct input *input = data;
2684 struct widget *widget;
2685 enum wl_pointer_button_state state = state_w;
2687 input->display->serial = serial;
2688 if (input->focus_widget && input->grab == NULL &&
2689 state == WL_POINTER_BUTTON_STATE_PRESSED)
2690 input_grab(input, input->focus_widget, button);
2692 widget = input->grab;
2693 if (widget && widget->button_handler)
2694 (*widget->button_handler)(widget,
2697 input->grab->user_data);
2699 if (input->grab && input->grab_button == button &&
2700 state == WL_POINTER_BUTTON_STATE_RELEASED)
2701 input_ungrab(input);
2705 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2706 uint32_t time, uint32_t axis, wl_fixed_t value)
2708 struct input *input = data;
2709 struct widget *widget;
2711 widget = input->focus_widget;
2713 widget = input->grab;
2714 if (widget && widget->axis_handler)
2715 (*widget->axis_handler)(widget,
2721 static const struct wl_pointer_listener pointer_listener = {
2722 pointer_handle_enter,
2723 pointer_handle_leave,
2724 pointer_handle_motion,
2725 pointer_handle_button,
2726 pointer_handle_axis,
2730 input_remove_keyboard_focus(struct input *input)
2732 struct window *window = input->keyboard_focus;
2733 struct itimerspec its;
2735 its.it_interval.tv_sec = 0;
2736 its.it_interval.tv_nsec = 0;
2737 its.it_value.tv_sec = 0;
2738 its.it_value.tv_nsec = 0;
2739 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2744 if (window->keyboard_focus_handler)
2745 (*window->keyboard_focus_handler)(window, NULL,
2748 input->keyboard_focus = NULL;
2752 keyboard_repeat_func(struct task *task, uint32_t events)
2754 struct input *input =
2755 container_of(task, struct input, repeat_task);
2756 struct window *window = input->keyboard_focus;
2759 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2760 /* If we change the timer between the fd becoming
2761 * readable and getting here, there'll be nothing to
2762 * read and we get EAGAIN. */
2765 if (window && window->key_handler) {
2766 (*window->key_handler)(window, input, input->repeat_time,
2767 input->repeat_key, input->repeat_sym,
2768 WL_KEYBOARD_KEY_STATE_PRESSED,
2774 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2775 uint32_t format, int fd, uint32_t size)
2777 struct input *input = data;
2778 struct xkb_keymap *keymap;
2779 struct xkb_state *state;
2787 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2792 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2793 if (map_str == MAP_FAILED) {
2798 keymap = xkb_map_new_from_string(input->display->xkb_context,
2800 XKB_KEYMAP_FORMAT_TEXT_V1,
2802 munmap(map_str, size);
2806 fprintf(stderr, "failed to compile keymap\n");
2810 state = xkb_state_new(keymap);
2812 fprintf(stderr, "failed to create XKB state\n");
2813 xkb_map_unref(keymap);
2817 xkb_keymap_unref(input->xkb.keymap);
2818 xkb_state_unref(input->xkb.state);
2819 input->xkb.keymap = keymap;
2820 input->xkb.state = state;
2822 input->xkb.control_mask =
2823 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2824 input->xkb.alt_mask =
2825 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2826 input->xkb.shift_mask =
2827 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2831 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2832 uint32_t serial, struct wl_surface *surface,
2833 struct wl_array *keys)
2835 struct input *input = data;
2836 struct window *window;
2838 input->display->serial = serial;
2839 input->keyboard_focus = wl_surface_get_user_data(surface);
2841 window = input->keyboard_focus;
2842 if (window->keyboard_focus_handler)
2843 (*window->keyboard_focus_handler)(window,
2844 input, window->user_data);
2848 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2849 uint32_t serial, struct wl_surface *surface)
2851 struct input *input = data;
2853 input->display->serial = serial;
2854 input_remove_keyboard_focus(input);
2858 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2859 uint32_t serial, uint32_t time, uint32_t key,
2862 struct input *input = data;
2863 struct window *window = input->keyboard_focus;
2864 uint32_t code, num_syms;
2865 enum wl_keyboard_key_state state = state_w;
2866 const xkb_keysym_t *syms;
2868 struct itimerspec its;
2870 input->display->serial = serial;
2872 if (!window || !input->xkb.state)
2875 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2877 sym = XKB_KEY_NoSymbol;
2882 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2883 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2884 window_set_maximized(window, !window->maximized);
2885 } else if (sym == XKB_KEY_F11 &&
2886 window->fullscreen_handler &&
2887 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2888 window->fullscreen_handler(window, window->user_data);
2889 } else if (sym == XKB_KEY_F4 &&
2890 input->modifiers == MOD_ALT_MASK &&
2891 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2892 window_close(window);
2893 } else if (window->key_handler) {
2894 (*window->key_handler)(window, input, time, key,
2895 sym, state, window->user_data);
2898 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2899 key == input->repeat_key) {
2900 its.it_interval.tv_sec = 0;
2901 its.it_interval.tv_nsec = 0;
2902 its.it_value.tv_sec = 0;
2903 its.it_value.tv_nsec = 0;
2904 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2905 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2906 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2907 input->repeat_sym = sym;
2908 input->repeat_key = key;
2909 input->repeat_time = time;
2910 its.it_interval.tv_sec = 0;
2911 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2912 its.it_value.tv_sec = 0;
2913 its.it_value.tv_nsec = 400 * 1000 * 1000;
2914 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2919 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2920 uint32_t serial, uint32_t mods_depressed,
2921 uint32_t mods_latched, uint32_t mods_locked,
2924 struct input *input = data;
2925 xkb_mod_mask_t mask;
2927 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2928 if (!input->xkb.keymap)
2931 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2932 mods_locked, 0, 0, group);
2933 mask = xkb_state_serialize_mods(input->xkb.state,
2934 XKB_STATE_DEPRESSED |
2936 input->modifiers = 0;
2937 if (mask & input->xkb.control_mask)
2938 input->modifiers |= MOD_CONTROL_MASK;
2939 if (mask & input->xkb.alt_mask)
2940 input->modifiers |= MOD_ALT_MASK;
2941 if (mask & input->xkb.shift_mask)
2942 input->modifiers |= MOD_SHIFT_MASK;
2945 static const struct wl_keyboard_listener keyboard_listener = {
2946 keyboard_handle_keymap,
2947 keyboard_handle_enter,
2948 keyboard_handle_leave,
2949 keyboard_handle_key,
2950 keyboard_handle_modifiers,
2954 touch_handle_down(void *data, struct wl_touch *wl_touch,
2955 uint32_t serial, uint32_t time, struct wl_surface *surface,
2956 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2958 struct input *input = data;
2959 struct widget *widget;
2960 float sx = wl_fixed_to_double(x_w);
2961 float sy = wl_fixed_to_double(y_w);
2963 input->display->serial = serial;
2964 input->touch_focus = wl_surface_get_user_data(surface);
2965 if (!input->touch_focus) {
2966 DBG("Failed to find to touch focus for surface %p\n", surface);
2970 widget = window_find_widget(input->touch_focus,
2971 wl_fixed_to_double(x_w),
2972 wl_fixed_to_double(y_w));
2974 struct touch_point *tp = xmalloc(sizeof *tp);
2977 tp->widget = widget;
2980 wl_list_insert(&input->touch_point_list, &tp->link);
2982 if (widget->touch_down_handler)
2983 (*widget->touch_down_handler)(widget, input,
2992 touch_handle_up(void *data, struct wl_touch *wl_touch,
2993 uint32_t serial, uint32_t time, int32_t id)
2995 struct input *input = data;
2996 struct touch_point *tp, *tmp;
2998 if (!input->touch_focus) {
2999 DBG("No touch focus found for touch up event!\n");
3003 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3007 if (tp->widget->touch_up_handler)
3008 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3010 tp->widget->user_data);
3012 wl_list_remove(&tp->link);
3020 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3021 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3023 struct input *input = data;
3024 struct touch_point *tp;
3025 float sx = wl_fixed_to_double(x_w);
3026 float sy = wl_fixed_to_double(y_w);
3028 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3030 if (!input->touch_focus) {
3031 DBG("No touch focus found for touch motion event!\n");
3035 wl_list_for_each(tp, &input->touch_point_list, link) {
3041 if (tp->widget->touch_motion_handler)
3042 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3044 tp->widget->user_data);
3050 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3052 struct input *input = data;
3053 struct touch_point *tp, *tmp;
3055 DBG("touch_handle_frame\n");
3057 if (!input->touch_focus) {
3058 DBG("No touch focus found for touch frame event!\n");
3062 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3063 if (tp->widget->touch_frame_handler)
3064 (*tp->widget->touch_frame_handler)(tp->widget, input,
3065 tp->widget->user_data);
3067 wl_list_remove(&tp->link);
3073 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3075 struct input *input = data;
3076 struct touch_point *tp, *tmp;
3078 DBG("touch_handle_cancel\n");
3080 if (!input->touch_focus) {
3081 DBG("No touch focus found for touch cancel event!\n");
3085 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_cancel_handler)
3087 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3088 tp->widget->user_data);
3090 wl_list_remove(&tp->link);
3095 static const struct wl_touch_listener touch_listener = {
3098 touch_handle_motion,
3100 touch_handle_cancel,
3104 seat_handle_capabilities(void *data, struct wl_seat *seat,
3105 enum wl_seat_capability caps)
3107 struct input *input = data;
3109 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3110 input->pointer = wl_seat_get_pointer(seat);
3111 wl_pointer_set_user_data(input->pointer, input);
3112 wl_pointer_add_listener(input->pointer, &pointer_listener,
3114 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3115 wl_pointer_destroy(input->pointer);
3116 input->pointer = NULL;
3119 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3120 input->keyboard = wl_seat_get_keyboard(seat);
3121 wl_keyboard_set_user_data(input->keyboard, input);
3122 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3124 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3125 wl_keyboard_destroy(input->keyboard);
3126 input->keyboard = NULL;
3129 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3130 input->touch = wl_seat_get_touch(seat);
3131 wl_touch_set_user_data(input->touch, input);
3132 wl_touch_add_listener(input->touch, &touch_listener, input);
3133 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3134 wl_touch_destroy(input->touch);
3135 input->touch = NULL;
3140 seat_handle_name(void *data, struct wl_seat *seat,
3146 static const struct wl_seat_listener seat_listener = {
3147 seat_handle_capabilities,
3152 input_get_position(struct input *input, int32_t *x, int32_t *y)
3159 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3161 struct touch_point *tp;
3163 wl_list_for_each(tp, &input->touch_point_list, link) {
3176 input_get_display(struct input *input)
3178 return input->display;
3182 input_get_seat(struct input *input)
3188 input_get_modifiers(struct input *input)
3190 return input->modifiers;
3194 input_get_focus_widget(struct input *input)
3196 return input->focus_widget;
3200 struct wl_data_offer *offer;
3201 struct input *input;
3202 struct wl_array types;
3205 struct task io_task;
3213 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3215 struct data_offer *offer = data;
3218 p = wl_array_add(&offer->types, sizeof *p);
3222 static const struct wl_data_offer_listener data_offer_listener = {
3227 data_offer_destroy(struct data_offer *offer)
3232 if (offer->refcount == 0) {
3233 wl_data_offer_destroy(offer->offer);
3234 for (p = offer->types.data; *p; p++)
3236 wl_array_release(&offer->types);
3242 data_device_data_offer(void *data,
3243 struct wl_data_device *data_device,
3244 struct wl_data_offer *_offer)
3246 struct data_offer *offer;
3248 offer = xmalloc(sizeof *offer);
3250 wl_array_init(&offer->types);
3251 offer->refcount = 1;
3252 offer->input = data;
3253 offer->offer = _offer;
3254 wl_data_offer_add_listener(offer->offer,
3255 &data_offer_listener, offer);
3259 data_device_enter(void *data, struct wl_data_device *data_device,
3260 uint32_t serial, struct wl_surface *surface,
3261 wl_fixed_t x_w, wl_fixed_t y_w,
3262 struct wl_data_offer *offer)
3264 struct input *input = data;
3265 struct window *window;
3267 float x = wl_fixed_to_double(x_w);
3268 float y = wl_fixed_to_double(y_w);
3271 window = wl_surface_get_user_data(surface);
3272 input->drag_enter_serial = serial;
3273 input->drag_focus = window,
3277 if (!input->touch_grab)
3278 input->pointer_enter_serial = serial;
3281 input->drag_offer = wl_data_offer_get_user_data(offer);
3283 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3286 types_data = input->drag_offer->types.data;
3288 input->drag_offer = NULL;
3292 if (window->data_handler)
3293 window->data_handler(window, input, x, y, types_data,
3298 data_device_leave(void *data, struct wl_data_device *data_device)
3300 struct input *input = data;
3302 if (input->drag_offer) {
3303 data_offer_destroy(input->drag_offer);
3304 input->drag_offer = NULL;
3309 data_device_motion(void *data, struct wl_data_device *data_device,
3310 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3312 struct input *input = data;
3313 struct window *window = input->drag_focus;
3314 float x = wl_fixed_to_double(x_w);
3315 float y = wl_fixed_to_double(y_w);
3321 if (input->drag_offer)
3322 types_data = input->drag_offer->types.data;
3326 if (window->data_handler)
3327 window->data_handler(window, input, x, y, types_data,
3332 data_device_drop(void *data, struct wl_data_device *data_device)
3334 struct input *input = data;
3335 struct window *window = input->drag_focus;
3341 if (window->drop_handler)
3342 window->drop_handler(window, input,
3343 x, y, window->user_data);
3345 if (input->touch_grab)
3346 touch_ungrab(input);
3350 data_device_selection(void *data,
3351 struct wl_data_device *wl_data_device,
3352 struct wl_data_offer *offer)
3354 struct input *input = data;
3357 if (input->selection_offer)
3358 data_offer_destroy(input->selection_offer);
3361 input->selection_offer = wl_data_offer_get_user_data(offer);
3362 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3365 input->selection_offer = NULL;
3369 static const struct wl_data_device_listener data_device_listener = {
3370 data_device_data_offer,
3375 data_device_selection
3379 input_set_pointer_image_index(struct input *input, int index)
3381 struct wl_buffer *buffer;
3382 struct wl_cursor *cursor;
3383 struct wl_cursor_image *image;
3385 if (!input->pointer)
3388 cursor = input->display->cursors[input->current_cursor];
3392 if (index >= (int) cursor->image_count) {
3393 fprintf(stderr, "cursor index out of range\n");
3397 image = cursor->images[index];
3398 buffer = wl_cursor_image_get_buffer(image);
3402 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3403 wl_surface_damage(input->pointer_surface, 0, 0,
3404 image->width, image->height);
3405 wl_surface_commit(input->pointer_surface);
3406 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3407 input->pointer_surface,
3408 image->hotspot_x, image->hotspot_y);
3411 static const struct wl_callback_listener pointer_surface_listener;
3414 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3417 struct input *input = data;
3418 struct wl_cursor *cursor;
3422 assert(callback == input->cursor_frame_cb);
3423 wl_callback_destroy(callback);
3424 input->cursor_frame_cb = NULL;
3427 if (!input->pointer)
3430 if (input->current_cursor == CURSOR_BLANK) {
3431 wl_pointer_set_cursor(input->pointer,
3432 input->pointer_enter_serial,
3437 if (input->current_cursor == CURSOR_UNSET)
3439 cursor = input->display->cursors[input->current_cursor];
3443 /* FIXME We don't have the current time on the first call so we set
3444 * the animation start to the time of the first frame callback. */
3446 input->cursor_anim_start = 0;
3447 else if (input->cursor_anim_start == 0)
3448 input->cursor_anim_start = time;
3450 if (time == 0 || input->cursor_anim_start == 0)
3453 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3455 if (cursor->image_count > 1) {
3456 input->cursor_frame_cb =
3457 wl_surface_frame(input->pointer_surface);
3458 wl_callback_add_listener(input->cursor_frame_cb,
3459 &pointer_surface_listener, input);
3462 input_set_pointer_image_index(input, i);
3465 static const struct wl_callback_listener pointer_surface_listener = {
3466 pointer_surface_frame_callback
3470 input_set_pointer_image(struct input *input, int pointer)
3474 if (!input->pointer)
3477 if (input->pointer_enter_serial > input->cursor_serial)
3480 if (!force && pointer == input->current_cursor)
3483 input->current_cursor = pointer;
3484 input->cursor_serial = input->pointer_enter_serial;
3485 if (!input->cursor_frame_cb)
3486 pointer_surface_frame_callback(input, NULL, 0);
3488 /* The current frame callback may be stuck if, for instance,
3489 * the set cursor request was processed by the server after
3490 * this client lost the focus. In this case the cursor surface
3491 * might not be mapped and the frame callback wouldn't ever
3492 * complete. Send a set_cursor and attach to try to map the
3493 * cursor surface again so that the callback will finish */
3494 input_set_pointer_image_index(input, 0);
3498 struct wl_data_device *
3499 input_get_data_device(struct input *input)
3501 return input->data_device;
3505 input_set_selection(struct input *input,
3506 struct wl_data_source *source, uint32_t time)
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_popup && !window->xdg_surface) {
3934 window->xdg_surface = xdg_shell_get_xdg_surface(window->display->xdg_shell,
3935 window->main_surface->surface);
3936 fail_on_null(window->xdg_surface);
3938 xdg_surface_set_user_data(window->xdg_surface, window);
3939 xdg_surface_add_listener(window->xdg_surface,
3940 &xdg_surface_listener, window);
3942 window_sync_transient_for(window);
3943 window_sync_margin(window);
3947 wl_list_for_each(surface, &window->subsurface_list, link) {
3948 if (surface == window->main_surface)
3951 surface_flush(surface);
3954 surface_flush(window->main_surface);
3958 menu_destroy(struct menu *menu)
3960 widget_destroy(menu->widget);
3961 window_destroy(menu->window);
3962 frame_destroy(menu->frame);
3967 window_get_allocation(struct window *window,
3968 struct rectangle *allocation)
3970 *allocation = window->main_surface->allocation;
3974 widget_redraw(struct widget *widget)
3976 struct widget *child;
3978 if (widget->redraw_handler)
3979 widget->redraw_handler(widget, widget->user_data);
3980 wl_list_for_each(child, &widget->child_list, link)
3981 widget_redraw(child);
3985 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3987 struct surface *surface = data;
3989 assert(callback == surface->frame_cb);
3990 DBG_OBJ(callback, "done\n");
3991 wl_callback_destroy(callback);
3992 surface->frame_cb = NULL;
3994 surface->last_time = time;
3996 if (surface->redraw_needed || surface->window->redraw_needed) {
3997 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3998 window_schedule_redraw_task(surface->window);
4002 static const struct wl_callback_listener listener = {
4007 surface_redraw(struct surface *surface)
4009 DBG_OBJ(surface->surface, "begin\n");
4011 if (!surface->window->redraw_needed && !surface->redraw_needed)
4014 /* Whole-window redraw forces a redraw even if the previous has
4015 * not yet hit the screen.
4017 if (surface->frame_cb) {
4018 if (!surface->window->redraw_needed)
4021 DBG_OBJ(surface->frame_cb, "cancelled\n");
4022 wl_callback_destroy(surface->frame_cb);
4025 if (surface->widget->use_cairo &&
4026 !widget_get_cairo_surface(surface->widget)) {
4027 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4031 surface->frame_cb = wl_surface_frame(surface->surface);
4032 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4033 DBG_OBJ(surface->frame_cb, "new\n");
4035 surface->redraw_needed = 0;
4036 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4037 widget_redraw(surface->widget);
4038 DBG_OBJ(surface->surface, "done\n");
4043 idle_redraw(struct task *task, uint32_t events)
4045 struct window *window = container_of(task, struct window, redraw_task);
4046 struct surface *surface;
4050 DBG(" --------- \n");
4052 wl_list_init(&window->redraw_task.link);
4053 window->redraw_task_scheduled = 0;
4055 if (window->resize_needed) {
4056 /* throttle resizing to the main surface display */
4057 if (window->main_surface->frame_cb) {
4058 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4062 idle_resize(window);
4066 if (surface_redraw(window->main_surface) < 0) {
4068 * Only main_surface failure will cause us to undo the resize.
4069 * If sub-surfaces fail, they will just be broken with old
4074 wl_list_for_each(surface, &window->subsurface_list, link) {
4075 if (surface == window->main_surface)
4078 surface_redraw(surface);
4082 window->redraw_needed = 0;
4083 window_flush(window);
4085 wl_list_for_each(surface, &window->subsurface_list, link)
4086 surface_set_synchronized_default(surface);
4088 if (resized && failed) {
4089 /* Restore widget tree to correspond to what is on screen. */
4090 undo_resize(window);
4095 window_schedule_redraw_task(struct window *window)
4097 if (!window->redraw_task_scheduled) {
4098 window->redraw_task.run = idle_redraw;
4099 display_defer(window->display, &window->redraw_task);
4100 window->redraw_task_scheduled = 1;
4105 window_schedule_redraw(struct window *window)
4107 struct surface *surface;
4109 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4111 wl_list_for_each(surface, &window->subsurface_list, link)
4112 surface->redraw_needed = 1;
4114 window_schedule_redraw_task(window);
4118 window_is_fullscreen(struct window *window)
4120 return window->fullscreen;
4124 window_set_fullscreen(struct window *window, int fullscreen)
4126 if (!window->xdg_surface)
4129 if (window->fullscreen == fullscreen)
4132 xdg_surface_request_change_state(window->xdg_surface,
4133 XDG_SURFACE_STATE_FULLSCREEN,
4139 window_is_maximized(struct window *window)
4141 return window->maximized;
4145 window_set_maximized(struct window *window, int maximized)
4147 if (!window->xdg_surface)
4150 if (window->maximized == maximized)
4153 xdg_surface_request_change_state(window->xdg_surface,
4154 XDG_SURFACE_STATE_MAXIMIZED,
4160 window_set_minimized(struct window *window)
4162 if (!window->xdg_surface)
4165 xdg_surface_set_minimized(window->xdg_surface);
4169 window_set_user_data(struct window *window, void *data)
4171 window->user_data = data;
4175 window_get_user_data(struct window *window)
4177 return window->user_data;
4181 window_set_key_handler(struct window *window,
4182 window_key_handler_t handler)
4184 window->key_handler = handler;
4188 window_set_keyboard_focus_handler(struct window *window,
4189 window_keyboard_focus_handler_t handler)
4191 window->keyboard_focus_handler = handler;
4195 window_set_data_handler(struct window *window, window_data_handler_t handler)
4197 window->data_handler = handler;
4201 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4203 window->drop_handler = handler;
4207 window_set_close_handler(struct window *window,
4208 window_close_handler_t handler)
4210 window->close_handler = handler;
4214 window_set_fullscreen_handler(struct window *window,
4215 window_fullscreen_handler_t handler)
4217 window->fullscreen_handler = handler;
4221 window_set_output_handler(struct window *window,
4222 window_output_handler_t handler)
4224 window->output_handler = handler;
4228 window_set_title(struct window *window, const char *title)
4230 free(window->title);
4231 window->title = strdup(title);
4232 if (window->frame) {
4233 frame_set_title(window->frame->frame, title);
4234 widget_schedule_redraw(window->frame->widget);
4236 if (window->xdg_surface)
4237 xdg_surface_set_title(window->xdg_surface, title);
4241 window_get_title(struct window *window)
4243 return window->title;
4247 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4249 struct text_cursor_position *text_cursor_position =
4250 window->display->text_cursor_position;
4252 if (!text_cursor_position)
4255 text_cursor_position_notify(text_cursor_position,
4256 window->main_surface->surface,
4257 wl_fixed_from_int(x),
4258 wl_fixed_from_int(y));
4262 window_damage(struct window *window, int32_t x, int32_t y,
4263 int32_t width, int32_t height)
4265 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4269 surface_enter(void *data,
4270 struct wl_surface *wl_surface, struct wl_output *wl_output)
4272 struct window *window = data;
4273 struct output *output;
4274 struct output *output_found = NULL;
4275 struct window_output *window_output;
4277 wl_list_for_each(output, &window->display->output_list, link) {
4278 if (output->output == wl_output) {
4279 output_found = output;
4287 window_output = xmalloc(sizeof *window_output);
4288 window_output->output = output_found;
4290 wl_list_insert (&window->window_output_list, &window_output->link);
4292 if (window->output_handler)
4293 window->output_handler(window, output_found, 1,
4298 surface_leave(void *data,
4299 struct wl_surface *wl_surface, struct wl_output *output)
4301 struct window *window = data;
4302 struct window_output *window_output;
4303 struct window_output *window_output_found = NULL;
4305 wl_list_for_each(window_output, &window->window_output_list, link) {
4306 if (window_output->output->output == output) {
4307 window_output_found = window_output;
4312 if (window_output_found) {
4313 wl_list_remove(&window_output_found->link);
4315 if (window->output_handler)
4316 window->output_handler(window, window_output->output,
4317 0, window->user_data);
4319 free(window_output_found);
4323 static const struct wl_surface_listener surface_listener = {
4328 static struct surface *
4329 surface_create(struct window *window)
4331 struct display *display = window->display;
4332 struct surface *surface;
4334 surface = xmalloc(sizeof *surface);
4335 memset(surface, 0, sizeof *surface);
4339 surface->window = window;
4340 surface->surface = wl_compositor_create_surface(display->compositor);
4341 surface->buffer_scale = 1;
4342 wl_surface_add_listener(surface->surface, &surface_listener, window);
4344 wl_list_insert(&window->subsurface_list, &surface->link);
4349 static struct window *
4350 window_create_internal(struct display *display, int custom)
4352 struct window *window;
4353 struct surface *surface;
4355 window = xzalloc(sizeof *window);
4356 wl_list_init(&window->subsurface_list);
4357 window->display = display;
4359 surface = surface_create(window);
4360 window->main_surface = surface;
4362 fail_on_null(display->xdg_shell);
4364 window->custom = custom;
4365 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4367 if (display->argb_device)
4368 #ifdef HAVE_CAIRO_EGL
4369 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4371 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4374 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4376 wl_surface_set_user_data(surface->surface, window);
4377 wl_list_insert(display->window_list.prev, &window->link);
4378 wl_list_init(&window->redraw_task.link);
4380 wl_list_init (&window->window_output_list);
4386 window_create(struct display *display)
4388 return window_create_internal(display, 0);
4392 window_create_custom(struct display *display)
4394 return window_create_internal(display, 1);
4398 window_set_transient_for(struct window *window,
4399 struct window *parent_window)
4401 window->transient_for = parent_window;
4402 window_sync_transient_for(window);
4406 window_get_transient_for(struct window *window)
4408 return window->transient_for;
4412 menu_set_item(struct menu *menu, int sy)
4414 int32_t x, y, width, height;
4417 frame_interior(menu->frame, &x, &y, &width, &height);
4418 next = (sy - y) / 20;
4419 if (menu->current != next) {
4420 menu->current = next;
4421 widget_schedule_redraw(menu->widget);
4426 menu_motion_handler(struct widget *widget,
4427 struct input *input, uint32_t time,
4428 float x, float y, void *data)
4430 struct menu *menu = data;
4432 if (widget == menu->widget)
4433 menu_set_item(data, y);
4435 return CURSOR_LEFT_PTR;
4439 menu_enter_handler(struct widget *widget,
4440 struct input *input, float x, float y, void *data)
4442 struct menu *menu = data;
4444 if (widget == menu->widget)
4445 menu_set_item(data, y);
4447 return CURSOR_LEFT_PTR;
4451 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4453 struct menu *menu = data;
4455 if (widget == menu->widget)
4456 menu_set_item(data, -200);
4460 menu_button_handler(struct widget *widget,
4461 struct input *input, uint32_t time,
4462 uint32_t button, enum wl_pointer_button_state state,
4466 struct menu *menu = data;
4468 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4469 (menu->release_count > 0 || time - menu->time > 500)) {
4470 /* Either relase after press-drag-release or
4471 * click-motion-click. */
4472 menu->func(menu->parent, input,
4473 menu->current, menu->parent->user_data);
4474 input_ungrab(input);
4476 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4477 menu->release_count++;
4482 menu_redraw_handler(struct widget *widget, void *data)
4485 struct menu *menu = data;
4486 int32_t x, y, width, height, i;
4488 cr = widget_cairo_create(widget);
4490 frame_repaint(menu->frame, cr);
4491 frame_interior(menu->frame, &x, &y, &width, &height);
4493 theme_set_background_source(menu->window->display->theme,
4494 cr, THEME_FRAME_ACTIVE);
4495 cairo_rectangle(cr, x, y, width, height);
4498 cairo_select_font_face(cr, "sans",
4499 CAIRO_FONT_SLANT_NORMAL,
4500 CAIRO_FONT_WEIGHT_NORMAL);
4501 cairo_set_font_size(cr, 12);
4503 for (i = 0; i < menu->count; i++) {
4504 if (i == menu->current) {
4505 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4506 cairo_rectangle(cr, x, y + i * 20, width, 20);
4508 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4509 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4510 cairo_show_text(cr, menu->entries[i]);
4512 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4513 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4514 cairo_show_text(cr, menu->entries[i]);
4522 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4524 struct window *window = data;
4525 struct menu *menu = window->main_surface->widget->user_data;
4527 input_ungrab(menu->input);
4531 static const struct xdg_popup_listener xdg_popup_listener = {
4532 handle_popup_popup_done,
4536 window_show_menu(struct display *display,
4537 struct input *input, uint32_t time, struct window *parent,
4538 int32_t x, int32_t y,
4539 menu_func_t func, const char **entries, int count)
4541 struct window *window;
4545 menu = malloc(sizeof *menu);
4549 window = window_create_internal(parent->display, 0);
4555 menu->window = window;
4556 menu->parent = parent;
4557 menu->widget = window_add_widget(menu->window, menu);
4558 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4559 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4560 menu->frame = frame_create(window->display->theme, 0, 0,
4561 FRAME_BUTTON_NONE, NULL);
4562 fail_on_null(menu->frame);
4563 menu->entries = entries;
4564 menu->count = count;
4565 menu->release_count = 0;
4569 menu->input = input;
4573 input_ungrab(input);
4575 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4576 widget_set_enter_handler(menu->widget, menu_enter_handler);
4577 widget_set_leave_handler(menu->widget, menu_leave_handler);
4578 widget_set_motion_handler(menu->widget, menu_motion_handler);
4579 widget_set_button_handler(menu->widget, menu_button_handler);
4581 input_grab(input, menu->widget, 0);
4582 frame_resize_inside(menu->frame, 200, count * 20);
4583 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4584 window_schedule_resize(window, frame_width(menu->frame),
4585 frame_height(menu->frame));
4587 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4589 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4590 window->main_surface->surface,
4591 parent->main_surface->surface,
4593 display_get_serial(window->display),
4597 fail_on_null(window->xdg_popup);
4599 xdg_popup_set_user_data(window->xdg_popup, window);
4600 xdg_popup_add_listener(window->xdg_popup,
4601 &xdg_popup_listener, window);
4605 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4607 window->main_surface->buffer_type = type;
4611 window_set_preferred_format(struct window *window,
4612 enum preferred_format format)
4614 window->preferred_format = format;
4618 window_add_subsurface(struct window *window, void *data,
4619 enum subsurface_mode default_mode)
4621 struct widget *widget;
4622 struct surface *surface;
4623 struct wl_surface *parent;
4624 struct wl_subcompositor *subcompo = window->display->subcompositor;
4626 surface = surface_create(window);
4627 widget = widget_create(window, surface, data);
4628 wl_list_init(&widget->link);
4629 surface->widget = widget;
4631 parent = window->main_surface->surface;
4632 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4635 surface->synchronized = 1;
4637 switch (default_mode) {
4638 case SUBSURFACE_SYNCHRONIZED:
4639 surface->synchronized_default = 1;
4641 case SUBSURFACE_DESYNCHRONIZED:
4642 surface->synchronized_default = 0;
4645 assert(!"bad enum subsurface_mode");
4648 window->resize_needed = 1;
4649 window_schedule_redraw(window);
4655 display_handle_geometry(void *data,
4656 struct wl_output *wl_output,
4659 int physical_height,
4665 struct output *output = data;
4667 output->allocation.x = x;
4668 output->allocation.y = y;
4669 output->transform = transform;
4673 display_handle_done(void *data,
4674 struct wl_output *wl_output)
4679 display_handle_scale(void *data,
4680 struct wl_output *wl_output,
4683 struct output *output = data;
4685 output->scale = scale;
4689 display_handle_mode(void *data,
4690 struct wl_output *wl_output,
4696 struct output *output = data;
4697 struct display *display = output->display;
4699 if (flags & WL_OUTPUT_MODE_CURRENT) {
4700 output->allocation.width = width;
4701 output->allocation.height = height;
4702 if (display->output_configure_handler)
4703 (*display->output_configure_handler)(
4704 output, display->user_data);
4708 static const struct wl_output_listener output_listener = {
4709 display_handle_geometry,
4710 display_handle_mode,
4711 display_handle_done,
4712 display_handle_scale
4716 display_add_output(struct display *d, uint32_t id)
4718 struct output *output;
4720 output = xzalloc(sizeof *output);
4721 output->display = d;
4724 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4725 output->server_output_id = id;
4726 wl_list_insert(d->output_list.prev, &output->link);
4728 wl_output_add_listener(output->output, &output_listener, output);
4732 output_destroy(struct output *output)
4734 if (output->destroy_handler)
4735 (*output->destroy_handler)(output, output->user_data);
4737 wl_output_destroy(output->output);
4738 wl_list_remove(&output->link);
4743 display_destroy_output(struct display *d, uint32_t id)
4745 struct output *output;
4747 wl_list_for_each(output, &d->output_list, link) {
4748 if (output->server_output_id == id) {
4749 output_destroy(output);
4756 display_set_global_handler(struct display *display,
4757 display_global_handler_t handler)
4759 struct global *global;
4761 display->global_handler = handler;
4765 wl_list_for_each(global, &display->global_list, link)
4766 display->global_handler(display,
4767 global->name, global->interface,
4768 global->version, display->user_data);
4772 display_set_global_handler_remove(struct display *display,
4773 display_global_handler_t remove_handler)
4775 display->global_handler_remove = remove_handler;
4776 if (!remove_handler)
4781 display_set_output_configure_handler(struct display *display,
4782 display_output_handler_t handler)
4784 struct output *output;
4786 display->output_configure_handler = handler;
4790 wl_list_for_each(output, &display->output_list, link) {
4791 if (output->allocation.width == 0 &&
4792 output->allocation.height == 0)
4795 (*display->output_configure_handler)(output,
4796 display->user_data);
4801 output_set_user_data(struct output *output, void *data)
4803 output->user_data = data;
4807 output_get_user_data(struct output *output)
4809 return output->user_data;
4813 output_set_destroy_handler(struct output *output,
4814 display_output_handler_t handler)
4816 output->destroy_handler = handler;
4817 /* FIXME: implement this, once we have way to remove outputs */
4821 output_get_allocation(struct output *output, struct rectangle *base)
4823 struct rectangle allocation = output->allocation;
4825 switch (output->transform) {
4826 case WL_OUTPUT_TRANSFORM_90:
4827 case WL_OUTPUT_TRANSFORM_270:
4828 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4829 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4830 /* Swap width and height */
4831 allocation.width = output->allocation.height;
4832 allocation.height = output->allocation.width;
4840 output_get_wl_output(struct output *output)
4842 return output->output;
4845 enum wl_output_transform
4846 output_get_transform(struct output *output)
4848 return output->transform;
4852 output_get_scale(struct output *output)
4854 return output->scale;
4858 fini_xkb(struct input *input)
4860 xkb_state_unref(input->xkb.state);
4861 xkb_map_unref(input->xkb.keymap);
4864 #define MIN(a,b) ((a) < (b) ? a : b)
4867 display_add_input(struct display *d, uint32_t id)
4869 struct input *input;
4871 input = xzalloc(sizeof *input);
4873 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4874 MIN(d->seat_version, 3));
4875 input->touch_focus = NULL;
4876 input->pointer_focus = NULL;
4877 input->keyboard_focus = NULL;
4878 wl_list_init(&input->touch_point_list);
4879 wl_list_insert(d->input_list.prev, &input->link);
4881 wl_seat_add_listener(input->seat, &seat_listener, input);
4882 wl_seat_set_user_data(input->seat, input);
4884 input->data_device =
4885 wl_data_device_manager_get_data_device(d->data_device_manager,
4887 wl_data_device_add_listener(input->data_device, &data_device_listener,
4890 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4892 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4893 TFD_CLOEXEC | TFD_NONBLOCK);
4894 input->repeat_task.run = keyboard_repeat_func;
4895 display_watch_fd(d, input->repeat_timer_fd,
4896 EPOLLIN, &input->repeat_task);
4900 input_destroy(struct input *input)
4902 input_remove_keyboard_focus(input);
4903 input_remove_pointer_focus(input);
4905 if (input->drag_offer)
4906 data_offer_destroy(input->drag_offer);
4908 if (input->selection_offer)
4909 data_offer_destroy(input->selection_offer);
4911 wl_data_device_destroy(input->data_device);
4913 if (input->display->seat_version >= 3) {
4915 wl_pointer_release(input->pointer);
4916 if (input->keyboard)
4917 wl_keyboard_release(input->keyboard);
4922 wl_surface_destroy(input->pointer_surface);
4924 wl_list_remove(&input->link);
4925 wl_seat_destroy(input->seat);
4926 close(input->repeat_timer_fd);
4931 init_workspace_manager(struct display *d, uint32_t id)
4933 d->workspace_manager =
4934 wl_registry_bind(d->registry, id,
4935 &workspace_manager_interface, 1);
4936 if (d->workspace_manager != NULL)
4937 workspace_manager_add_listener(d->workspace_manager,
4938 &workspace_manager_listener,
4943 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4945 struct display *d = data;
4947 if (format == WL_SHM_FORMAT_RGB565)
4951 struct wl_shm_listener shm_listener = {
4956 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4958 xdg_shell_pong(shell, serial);
4961 static const struct xdg_shell_listener xdg_shell_listener = {
4965 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
4966 #ifdef static_assert
4967 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
4968 "Interface version doesn't match implementation version");
4972 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4973 const char *interface, uint32_t version)
4975 struct display *d = data;
4976 struct global *global;
4978 global = xmalloc(sizeof *global);
4980 global->interface = strdup(interface);
4981 global->version = version;
4982 wl_list_insert(d->global_list.prev, &global->link);
4984 if (strcmp(interface, "wl_compositor") == 0) {
4985 d->compositor = wl_registry_bind(registry, id,
4986 &wl_compositor_interface, 3);
4987 } else if (strcmp(interface, "wl_output") == 0) {
4988 display_add_output(d, id);
4989 } else if (strcmp(interface, "wl_seat") == 0) {
4990 d->seat_version = version;
4991 display_add_input(d, id);
4992 } else if (strcmp(interface, "wl_shm") == 0) {
4993 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4994 wl_shm_add_listener(d->shm, &shm_listener, d);
4995 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4996 d->data_device_manager =
4997 wl_registry_bind(registry, id,
4998 &wl_data_device_manager_interface, 1);
4999 } else if (strcmp(interface, "xdg_shell") == 0) {
5000 d->xdg_shell = wl_registry_bind(registry, id,
5001 &xdg_shell_interface, 1);
5002 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5003 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5004 } else if (strcmp(interface, "text_cursor_position") == 0) {
5005 d->text_cursor_position =
5006 wl_registry_bind(registry, id,
5007 &text_cursor_position_interface, 1);
5008 } else if (strcmp(interface, "workspace_manager") == 0) {
5009 init_workspace_manager(d, id);
5010 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5012 wl_registry_bind(registry, id,
5013 &wl_subcompositor_interface, 1);
5016 if (d->global_handler)
5017 d->global_handler(d, id, interface, version, d->user_data);
5021 registry_handle_global_remove(void *data, struct wl_registry *registry,
5024 struct display *d = data;
5025 struct global *global;
5028 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5029 if (global->name != name)
5032 if (strcmp(global->interface, "wl_output") == 0)
5033 display_destroy_output(d, name);
5035 /* XXX: Should destroy remaining bound globals */
5037 if (d->global_handler_remove)
5038 d->global_handler_remove(d, name, global->interface,
5039 global->version, d->user_data);
5041 wl_list_remove(&global->link);
5042 free(global->interface);
5048 display_bind(struct display *display, uint32_t name,
5049 const struct wl_interface *interface, uint32_t version)
5051 return wl_registry_bind(display->registry, name, interface, version);
5054 static const struct wl_registry_listener registry_listener = {
5055 registry_handle_global,
5056 registry_handle_global_remove
5059 #ifdef HAVE_CAIRO_EGL
5061 init_egl(struct display *d)
5063 EGLint major, minor;
5066 #ifdef USE_CAIRO_GLESV2
5067 # define GL_BIT EGL_OPENGL_ES2_BIT
5069 # define GL_BIT EGL_OPENGL_BIT
5072 static const EGLint argb_cfg_attribs[] = {
5073 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5079 EGL_RENDERABLE_TYPE, GL_BIT,
5083 #ifdef USE_CAIRO_GLESV2
5084 static const EGLint context_attribs[] = {
5085 EGL_CONTEXT_CLIENT_VERSION, 2,
5088 EGLint api = EGL_OPENGL_ES_API;
5090 EGLint *context_attribs = NULL;
5091 EGLint api = EGL_OPENGL_API;
5094 d->dpy = eglGetDisplay(d->display);
5095 if (!eglInitialize(d->dpy, &major, &minor)) {
5096 fprintf(stderr, "failed to initialize EGL\n");
5100 if (!eglBindAPI(api)) {
5101 fprintf(stderr, "failed to bind EGL client API\n");
5105 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5106 &d->argb_config, 1, &n) || n != 1) {
5107 fprintf(stderr, "failed to choose argb EGL config\n");
5111 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5112 EGL_NO_CONTEXT, context_attribs);
5113 if (d->argb_ctx == NULL) {
5114 fprintf(stderr, "failed to create EGL context\n");
5118 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5119 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5120 fprintf(stderr, "failed to get cairo EGL argb device\n");
5128 fini_egl(struct display *display)
5130 cairo_device_destroy(display->argb_device);
5132 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5135 eglTerminate(display->dpy);
5141 init_dummy_surface(struct display *display)
5146 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5148 display->dummy_surface =
5149 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5151 display->dummy_surface_data = data;
5155 handle_display_data(struct task *task, uint32_t events)
5157 struct display *display =
5158 container_of(task, struct display, display_task);
5159 struct epoll_event ep;
5162 display->display_fd_events = events;
5164 if (events & EPOLLERR || events & EPOLLHUP) {
5165 display_exit(display);
5169 if (events & EPOLLIN) {
5170 ret = wl_display_dispatch(display->display);
5172 display_exit(display);
5177 if (events & EPOLLOUT) {
5178 ret = wl_display_flush(display->display);
5180 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5181 ep.data.ptr = &display->display_task;
5182 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5183 display->display_fd, &ep);
5184 } else if (ret == -1 && errno != EAGAIN) {
5185 display_exit(display);
5192 log_handler(const char *format, va_list args)
5194 vfprintf(stderr, format, args);
5198 display_create(int *argc, char *argv[])
5202 wl_log_set_handler_client(log_handler);
5204 d = zalloc(sizeof *d);
5208 d->display = wl_display_connect(NULL);
5209 if (d->display == NULL) {
5210 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5215 d->xkb_context = xkb_context_new(0);
5216 if (d->xkb_context == NULL) {
5217 fprintf(stderr, "Failed to create XKB context\n");
5222 d->epoll_fd = os_epoll_create_cloexec();
5223 d->display_fd = wl_display_get_fd(d->display);
5224 d->display_task.run = handle_display_data;
5225 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5228 wl_list_init(&d->deferred_list);
5229 wl_list_init(&d->input_list);
5230 wl_list_init(&d->output_list);
5231 wl_list_init(&d->global_list);
5234 d->workspace_count = 1;
5236 d->registry = wl_display_get_registry(d->display);
5237 wl_registry_add_listener(d->registry, ®istry_listener, d);
5239 if (wl_display_dispatch(d->display) < 0) {
5240 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5244 #ifdef HAVE_CAIRO_EGL
5245 if (init_egl(d) < 0)
5246 fprintf(stderr, "EGL does not seem to work, "
5247 "falling back to software rendering and wl_shm.\n");
5252 d->theme = theme_create();
5254 wl_list_init(&d->window_list);
5256 init_dummy_surface(d);
5262 display_destroy_outputs(struct display *display)
5265 struct output *output;
5267 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5268 output_destroy(output);
5272 display_destroy_inputs(struct display *display)
5275 struct input *input;
5277 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5278 input_destroy(input);
5282 display_destroy(struct display *display)
5284 if (!wl_list_empty(&display->window_list))
5285 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5286 wl_list_length(&display->window_list));
5288 if (!wl_list_empty(&display->deferred_list))
5289 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5291 cairo_surface_destroy(display->dummy_surface);
5292 free(display->dummy_surface_data);
5294 display_destroy_outputs(display);
5295 display_destroy_inputs(display);
5297 xkb_context_unref(display->xkb_context);
5299 theme_destroy(display->theme);
5300 destroy_cursors(display);
5302 #ifdef HAVE_CAIRO_EGL
5303 if (display->argb_device)
5307 if (display->subcompositor)
5308 wl_subcompositor_destroy(display->subcompositor);
5310 if (display->xdg_shell)
5311 xdg_shell_destroy(display->xdg_shell);
5314 wl_shm_destroy(display->shm);
5316 if (display->data_device_manager)
5317 wl_data_device_manager_destroy(display->data_device_manager);
5319 wl_compositor_destroy(display->compositor);
5320 wl_registry_destroy(display->registry);
5322 close(display->epoll_fd);
5324 if (!(display->display_fd_events & EPOLLERR) &&
5325 !(display->display_fd_events & EPOLLHUP))
5326 wl_display_flush(display->display);
5328 wl_display_disconnect(display->display);
5333 display_set_user_data(struct display *display, void *data)
5335 display->user_data = data;
5339 display_get_user_data(struct display *display)
5341 return display->user_data;
5345 display_get_display(struct display *display)
5347 return display->display;
5351 display_has_subcompositor(struct display *display)
5353 if (display->subcompositor)
5356 wl_display_roundtrip(display->display);
5358 return display->subcompositor != NULL;
5362 display_get_cairo_device(struct display *display)
5364 return display->argb_device;
5368 display_get_output(struct display *display)
5370 return container_of(display->output_list.next, struct output, link);
5373 struct wl_compositor *
5374 display_get_compositor(struct display *display)
5376 return display->compositor;
5380 display_get_serial(struct display *display)
5382 return display->serial;
5386 display_get_egl_display(struct display *d)
5391 struct wl_data_source *
5392 display_create_data_source(struct display *display)
5394 return wl_data_device_manager_create_data_source(display->data_device_manager);
5398 display_get_argb_egl_config(struct display *d)
5400 return d->argb_config;
5404 display_acquire_window_surface(struct display *display,
5405 struct window *window,
5408 struct surface *surface = window->main_surface;
5410 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5413 widget_get_cairo_surface(window->main_surface->widget);
5414 return surface->toysurface->acquire(surface->toysurface, ctx);
5418 display_release_window_surface(struct display *display,
5419 struct window *window)
5421 struct surface *surface = window->main_surface;
5423 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5426 surface->toysurface->release(surface->toysurface);
5430 display_defer(struct display *display, struct task *task)
5432 wl_list_insert(&display->deferred_list, &task->link);
5436 display_watch_fd(struct display *display,
5437 int fd, uint32_t events, struct task *task)
5439 struct epoll_event ep;
5443 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5447 display_unwatch_fd(struct display *display, int fd)
5449 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5453 display_run(struct display *display)
5456 struct epoll_event ep[16];
5459 display->running = 1;
5461 while (!wl_list_empty(&display->deferred_list)) {
5462 task = container_of(display->deferred_list.prev,
5464 wl_list_remove(&task->link);
5468 wl_display_dispatch_pending(display->display);
5470 if (!display->running)
5473 ret = wl_display_flush(display->display);
5474 if (ret < 0 && errno == EAGAIN) {
5476 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5477 ep[0].data.ptr = &display->display_task;
5479 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5480 display->display_fd, &ep[0]);
5481 } else if (ret < 0) {
5485 count = epoll_wait(display->epoll_fd,
5486 ep, ARRAY_LENGTH(ep), -1);
5487 for (i = 0; i < count; i++) {
5488 task = ep[i].data.ptr;
5489 task->run(task, ep[i].events);
5495 display_exit(struct display *display)
5497 display->running = 0;
5501 keysym_modifiers_add(struct wl_array *modifiers_map,
5504 size_t len = strlen(name) + 1;
5507 p = wl_array_add(modifiers_map, len);
5512 strncpy(p, name, len);
5515 static xkb_mod_index_t
5516 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5519 xkb_mod_index_t index = 0;
5520 char *p = modifiers_map->data;
5522 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5523 if (strcmp(p, name) == 0)
5530 return XKB_MOD_INVALID;
5534 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5537 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5539 if (index == XKB_MOD_INVALID)
5540 return XKB_MOD_INVALID;
5546 fail_on_null(void *p)
5549 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5559 return fail_on_null(malloc(s));
5565 return fail_on_null(zalloc(s));
5569 xstrdup(const char *s)
5571 return fail_on_null(strdup(s));
5575 xrealloc(char *p, size_t s)
5577 return fail_on_null(realloc(p, s));