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);
1284 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1286 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1288 for (j = 0; !cursor && j < cursors[i].count; ++j)
1289 cursor = wl_cursor_theme_get_cursor(
1290 display->cursor_theme, cursors[i].names[j]);
1293 fprintf(stderr, "could not load cursor '%s'\n",
1294 cursors[i].names[0]);
1296 display->cursors[i] = cursor;
1301 destroy_cursors(struct display *display)
1303 wl_cursor_theme_destroy(display->cursor_theme);
1304 free(display->cursors);
1307 struct wl_cursor_image *
1308 display_get_pointer_image(struct display *display, int pointer)
1310 struct wl_cursor *cursor = display->cursors[pointer];
1312 return cursor ? cursor->images[0] : NULL;
1316 surface_flush(struct surface *surface)
1318 if (!surface->cairo_surface)
1321 if (surface->opaque_region) {
1322 wl_surface_set_opaque_region(surface->surface,
1323 surface->opaque_region);
1324 wl_region_destroy(surface->opaque_region);
1325 surface->opaque_region = NULL;
1328 if (surface->input_region) {
1329 wl_surface_set_input_region(surface->surface,
1330 surface->input_region);
1331 wl_region_destroy(surface->input_region);
1332 surface->input_region = NULL;
1335 surface->toysurface->swap(surface->toysurface,
1336 surface->buffer_transform, surface->buffer_scale,
1337 &surface->server_allocation);
1339 cairo_surface_destroy(surface->cairo_surface);
1340 surface->cairo_surface = NULL;
1344 window_has_focus(struct window *window)
1346 return window->focused;
1350 window_close(struct window *window)
1352 if (window->close_handler)
1353 window->close_handler(window->user_data);
1355 display_exit(window->display);
1359 window_get_display(struct window *window)
1361 return window->display;
1365 surface_create_surface(struct surface *surface, uint32_t flags)
1367 struct display *display = surface->window->display;
1368 struct rectangle allocation = surface->allocation;
1370 if (!surface->toysurface && display->dpy &&
1371 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1372 surface->toysurface =
1373 egl_window_surface_create(display,
1379 if (!surface->toysurface)
1380 surface->toysurface = shm_surface_create(display,
1382 flags, &allocation);
1384 surface->cairo_surface = surface->toysurface->prepare(
1385 surface->toysurface, 0, 0,
1386 allocation.width, allocation.height, flags,
1387 surface->buffer_transform, surface->buffer_scale);
1391 window_create_main_surface(struct window *window)
1393 struct surface *surface = window->main_surface;
1396 if (window->resizing)
1397 flags |= SURFACE_HINT_RESIZE;
1399 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1400 flags |= SURFACE_HINT_RGB565;
1402 surface_create_surface(surface, flags);
1406 window_get_buffer_transform(struct window *window)
1408 return window->main_surface->buffer_transform;
1412 window_set_buffer_transform(struct window *window,
1413 enum wl_output_transform transform)
1415 window->main_surface->buffer_transform = transform;
1416 wl_surface_set_buffer_transform(window->main_surface->surface,
1421 window_set_buffer_scale(struct window *window,
1424 window->main_surface->buffer_scale = scale;
1425 wl_surface_set_buffer_scale(window->main_surface->surface,
1430 window_get_buffer_scale(struct window *window)
1432 return window->main_surface->buffer_scale;
1436 window_get_output_scale(struct window *window)
1438 struct window_output *window_output;
1439 struct window_output *window_output_tmp;
1442 wl_list_for_each_safe(window_output, window_output_tmp,
1443 &window->window_output_list, link) {
1444 if (window_output->output->scale > scale)
1445 scale = window_output->output->scale;
1451 static void window_frame_destroy(struct window_frame *frame);
1454 surface_destroy(struct surface *surface)
1456 if (surface->frame_cb)
1457 wl_callback_destroy(surface->frame_cb);
1459 if (surface->input_region)
1460 wl_region_destroy(surface->input_region);
1462 if (surface->opaque_region)
1463 wl_region_destroy(surface->opaque_region);
1465 if (surface->subsurface)
1466 wl_subsurface_destroy(surface->subsurface);
1468 wl_surface_destroy(surface->surface);
1470 if (surface->toysurface)
1471 surface->toysurface->destroy(surface->toysurface);
1473 wl_list_remove(&surface->link);
1478 window_destroy(struct window *window)
1480 struct display *display = window->display;
1481 struct input *input;
1482 struct window_output *window_output;
1483 struct window_output *window_output_tmp;
1485 wl_list_remove(&window->redraw_task.link);
1487 wl_list_for_each(input, &display->input_list, link) {
1488 if (input->touch_focus == window)
1489 input->touch_focus = NULL;
1490 if (input->pointer_focus == window)
1491 input->pointer_focus = NULL;
1492 if (input->keyboard_focus == window)
1493 input->keyboard_focus = NULL;
1494 if (input->focus_widget &&
1495 input->focus_widget->window == window)
1496 input->focus_widget = NULL;
1499 wl_list_for_each_safe(window_output, window_output_tmp,
1500 &window->window_output_list, link) {
1501 free (window_output);
1505 window_frame_destroy(window->frame);
1507 if (window->xdg_surface)
1508 xdg_surface_destroy(window->xdg_surface);
1509 if (window->xdg_popup)
1510 xdg_popup_destroy(window->xdg_popup);
1512 surface_destroy(window->main_surface);
1514 wl_list_remove(&window->link);
1516 free(window->title);
1520 static struct widget *
1521 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1523 struct widget *child, *target;
1525 wl_list_for_each(child, &widget->child_list, link) {
1526 target = widget_find_widget(child, x, y);
1531 if (widget->allocation.x <= x &&
1532 x < widget->allocation.x + widget->allocation.width &&
1533 widget->allocation.y <= y &&
1534 y < widget->allocation.y + widget->allocation.height) {
1541 static struct widget *
1542 window_find_widget(struct window *window, int32_t x, int32_t y)
1544 struct surface *surface;
1545 struct widget *widget;
1547 wl_list_for_each(surface, &window->subsurface_list, link) {
1548 widget = widget_find_widget(surface->widget, x, y);
1556 static struct widget *
1557 widget_create(struct window *window, struct surface *surface, void *data)
1559 struct widget *widget;
1561 widget = xzalloc(sizeof *widget);
1562 widget->window = window;
1563 widget->surface = surface;
1564 widget->user_data = data;
1565 widget->allocation = surface->allocation;
1566 wl_list_init(&widget->child_list);
1568 widget->tooltip = NULL;
1569 widget->tooltip_count = 0;
1570 widget->default_cursor = CURSOR_LEFT_PTR;
1571 widget->use_cairo = 1;
1577 window_add_widget(struct window *window, void *data)
1579 struct widget *widget;
1581 widget = widget_create(window, window->main_surface, data);
1582 wl_list_init(&widget->link);
1583 window->main_surface->widget = widget;
1589 widget_add_widget(struct widget *parent, void *data)
1591 struct widget *widget;
1593 widget = widget_create(parent->window, parent->surface, data);
1594 wl_list_insert(parent->child_list.prev, &widget->link);
1600 widget_destroy(struct widget *widget)
1602 struct display *display = widget->window->display;
1603 struct surface *surface = widget->surface;
1604 struct input *input;
1606 /* Destroy the sub-surface along with the root widget */
1607 if (surface->widget == widget && surface->subsurface)
1608 surface_destroy(widget->surface);
1610 if (widget->tooltip)
1611 widget_destroy_tooltip(widget);
1613 wl_list_for_each(input, &display->input_list, link) {
1614 if (input->focus_widget == widget)
1615 input->focus_widget = NULL;
1618 wl_list_remove(&widget->link);
1623 widget_set_default_cursor(struct widget *widget, int cursor)
1625 widget->default_cursor = cursor;
1629 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1631 *allocation = widget->allocation;
1635 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1637 widget->allocation.width = width;
1638 widget->allocation.height = height;
1642 widget_set_allocation(struct widget *widget,
1643 int32_t x, int32_t y, int32_t width, int32_t height)
1645 widget->allocation.x = x;
1646 widget->allocation.y = y;
1647 widget_set_size(widget, width, height);
1651 widget_set_transparent(struct widget *widget, int transparent)
1653 widget->opaque = !transparent;
1657 widget_get_user_data(struct widget *widget)
1659 return widget->user_data;
1662 static cairo_surface_t *
1663 widget_get_cairo_surface(struct widget *widget)
1665 struct surface *surface = widget->surface;
1666 struct window *window = widget->window;
1668 assert(widget->use_cairo);
1670 if (!surface->cairo_surface) {
1671 if (surface == window->main_surface)
1672 window_create_main_surface(window);
1674 surface_create_surface(surface, 0);
1677 return surface->cairo_surface;
1681 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1683 struct surface *surface = widget->surface;
1686 enum wl_output_transform transform;
1687 int surface_width, surface_height;
1688 int translate_x, translate_y;
1691 surface_width = surface->allocation.width;
1692 surface_height = surface->allocation.height;
1694 transform = surface->buffer_transform;
1695 scale = surface->buffer_scale;
1697 switch (transform) {
1698 case WL_OUTPUT_TRANSFORM_FLIPPED:
1699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1702 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1705 cairo_matrix_init_identity(&m);
1709 switch (transform) {
1710 case WL_OUTPUT_TRANSFORM_NORMAL:
1716 case WL_OUTPUT_TRANSFORM_FLIPPED:
1718 translate_x = surface_width;
1721 case WL_OUTPUT_TRANSFORM_90:
1723 translate_x = surface_height;
1726 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1728 translate_x = surface_height;
1729 translate_y = surface_width;
1731 case WL_OUTPUT_TRANSFORM_180:
1733 translate_x = surface_width;
1734 translate_y = surface_height;
1736 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1739 translate_y = surface_height;
1741 case WL_OUTPUT_TRANSFORM_270:
1742 angle = M_PI + M_PI_2;
1744 translate_y = surface_width;
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1747 angle = M_PI + M_PI_2;
1753 cairo_scale(cr, scale, scale);
1754 cairo_translate(cr, translate_x, translate_y);
1755 cairo_rotate(cr, angle);
1756 cairo_transform(cr, &m);
1760 widget_cairo_create(struct widget *widget)
1762 struct surface *surface = widget->surface;
1763 cairo_surface_t *cairo_surface;
1766 cairo_surface = widget_get_cairo_surface(widget);
1767 cr = cairo_create(cairo_surface);
1769 widget_cairo_update_transform(widget, cr);
1771 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1777 widget_get_wl_surface(struct widget *widget)
1779 return widget->surface->surface;
1782 struct wl_subsurface *
1783 widget_get_wl_subsurface(struct widget *widget)
1785 return widget->surface->subsurface;
1789 widget_get_last_time(struct widget *widget)
1791 return widget->surface->last_time;
1795 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1797 struct wl_compositor *comp = widget->window->display->compositor;
1798 struct surface *surface = widget->surface;
1800 if (!surface->input_region)
1801 surface->input_region = wl_compositor_create_region(comp);
1804 wl_region_add(surface->input_region,
1805 rect->x, rect->y, rect->width, rect->height);
1810 widget_set_resize_handler(struct widget *widget,
1811 widget_resize_handler_t handler)
1813 widget->resize_handler = handler;
1817 widget_set_redraw_handler(struct widget *widget,
1818 widget_redraw_handler_t handler)
1820 widget->redraw_handler = handler;
1824 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1826 widget->enter_handler = handler;
1830 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1832 widget->leave_handler = handler;
1836 widget_set_motion_handler(struct widget *widget,
1837 widget_motion_handler_t handler)
1839 widget->motion_handler = handler;
1843 widget_set_button_handler(struct widget *widget,
1844 widget_button_handler_t handler)
1846 widget->button_handler = handler;
1850 widget_set_touch_up_handler(struct widget *widget,
1851 widget_touch_up_handler_t handler)
1853 widget->touch_up_handler = handler;
1857 widget_set_touch_down_handler(struct widget *widget,
1858 widget_touch_down_handler_t handler)
1860 widget->touch_down_handler = handler;
1864 widget_set_touch_motion_handler(struct widget *widget,
1865 widget_touch_motion_handler_t handler)
1867 widget->touch_motion_handler = handler;
1871 widget_set_touch_frame_handler(struct widget *widget,
1872 widget_touch_frame_handler_t handler)
1874 widget->touch_frame_handler = handler;
1878 widget_set_touch_cancel_handler(struct widget *widget,
1879 widget_touch_cancel_handler_t handler)
1881 widget->touch_cancel_handler = handler;
1885 widget_set_axis_handler(struct widget *widget,
1886 widget_axis_handler_t handler)
1888 widget->axis_handler = handler;
1892 window_schedule_redraw_task(struct window *window);
1895 widget_schedule_redraw(struct widget *widget)
1897 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1898 widget->surface->redraw_needed = 1;
1899 window_schedule_redraw_task(widget->window);
1903 widget_set_use_cairo(struct widget *widget,
1906 widget->use_cairo = use_cairo;
1910 window_get_surface(struct window *window)
1912 cairo_surface_t *cairo_surface;
1914 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1916 return cairo_surface_reference(cairo_surface);
1920 window_get_wl_surface(struct window *window)
1922 return window->main_surface->surface;
1926 tooltip_redraw_handler(struct widget *widget, void *data)
1929 const int32_t r = 3;
1930 struct tooltip *tooltip = data;
1931 int32_t width, height;
1933 cr = widget_cairo_create(widget);
1934 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1935 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1936 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1939 width = widget->allocation.width;
1940 height = widget->allocation.height;
1941 rounded_rect(cr, 0, 0, width, height, r);
1943 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1944 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1947 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1948 cairo_move_to(cr, 10, 16);
1949 cairo_show_text(cr, tooltip->entry);
1953 static cairo_text_extents_t
1954 get_text_extents(struct display *display, struct tooltip *tooltip)
1957 cairo_text_extents_t extents;
1959 /* Use the dummy_surface because tooltip's surface was not
1960 * created yet, and parent does not have a valid surface
1961 * outside repaint, either.
1963 cr = cairo_create(display->dummy_surface);
1964 cairo_text_extents(cr, tooltip->entry, &extents);
1971 window_create_tooltip(struct tooltip *tooltip)
1973 struct widget *parent = tooltip->parent;
1974 struct display *display = parent->window->display;
1975 const int offset_y = 27;
1976 const int margin = 3;
1977 cairo_text_extents_t extents;
1979 if (tooltip->widget)
1982 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1984 extents = get_text_extents(display, tooltip);
1985 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1986 widget_set_allocation(tooltip->widget,
1987 tooltip->x, tooltip->y + offset_y,
1988 extents.width + 20, 20 + margin * 2);
1994 widget_destroy_tooltip(struct widget *parent)
1996 struct tooltip *tooltip = parent->tooltip;
1998 parent->tooltip_count = 0;
2002 if (tooltip->widget) {
2003 widget_destroy(tooltip->widget);
2004 tooltip->widget = NULL;
2007 close(tooltip->tooltip_fd);
2008 free(tooltip->entry);
2010 parent->tooltip = NULL;
2014 tooltip_func(struct task *task, uint32_t events)
2016 struct tooltip *tooltip =
2017 container_of(task, struct tooltip, tooltip_task);
2020 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2022 window_create_tooltip(tooltip);
2025 #define TOOLTIP_TIMEOUT 500
2027 tooltip_timer_reset(struct tooltip *tooltip)
2029 struct itimerspec its;
2031 its.it_interval.tv_sec = 0;
2032 its.it_interval.tv_nsec = 0;
2033 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2034 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2035 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2036 fprintf(stderr, "could not set timerfd\n: %m");
2044 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2046 struct tooltip *tooltip = parent->tooltip;
2048 parent->tooltip_count++;
2052 tooltip_timer_reset(tooltip);
2056 /* the handler might be triggered too fast via input device motion, so
2057 * we need this check here to make sure tooltip is fully initialized */
2058 if (parent->tooltip_count > 1)
2061 tooltip = malloc(sizeof *tooltip);
2065 parent->tooltip = tooltip;
2066 tooltip->parent = parent;
2067 tooltip->widget = NULL;
2070 tooltip->entry = strdup(entry);
2071 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2072 if (tooltip->tooltip_fd < 0) {
2073 fprintf(stderr, "could not create timerfd\n: %m");
2077 tooltip->tooltip_task.run = tooltip_func;
2078 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2079 EPOLLIN, &tooltip->tooltip_task);
2080 tooltip_timer_reset(tooltip);
2086 workspace_manager_state(void *data,
2087 struct workspace_manager *workspace_manager,
2091 struct display *display = data;
2093 display->workspace = current;
2094 display->workspace_count = count;
2097 static const struct workspace_manager_listener workspace_manager_listener = {
2098 workspace_manager_state
2102 frame_resize_handler(struct widget *widget,
2103 int32_t width, int32_t height, void *data)
2105 struct window_frame *frame = data;
2106 struct widget *child = frame->child;
2107 struct rectangle interior;
2108 struct rectangle input;
2109 struct rectangle opaque;
2111 if (widget->window->fullscreen) {
2114 interior.width = width;
2115 interior.height = height;
2117 if (widget->window->maximized) {
2118 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2120 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2123 frame_resize(frame->frame, width, height);
2124 frame_interior(frame->frame, &interior.x, &interior.y,
2125 &interior.width, &interior.height);
2128 widget_set_allocation(child, interior.x, interior.y,
2129 interior.width, interior.height);
2131 if (child->resize_handler) {
2132 child->resize_handler(child, interior.width, interior.height,
2135 if (widget->window->fullscreen) {
2136 width = child->allocation.width;
2137 height = child->allocation.height;
2139 frame_resize_inside(frame->frame,
2140 child->allocation.width,
2141 child->allocation.height);
2142 width = frame_width(frame->frame);
2143 height = frame_height(frame->frame);
2147 widget_set_allocation(widget, 0, 0, width, height);
2149 widget->surface->input_region =
2150 wl_compositor_create_region(widget->window->display->compositor);
2151 if (!widget->window->fullscreen) {
2152 frame_input_rect(frame->frame, &input.x, &input.y,
2153 &input.width, &input.height);
2154 wl_region_add(widget->surface->input_region,
2155 input.x, input.y, input.width, input.height);
2157 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2160 widget_set_allocation(widget, 0, 0, width, height);
2162 if (child->opaque) {
2163 if (!widget->window->fullscreen) {
2164 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2165 &opaque.width, &opaque.height);
2167 wl_region_add(widget->surface->opaque_region,
2169 opaque.width, opaque.height);
2171 wl_region_add(widget->surface->opaque_region,
2172 0, 0, width, height);
2177 widget_schedule_redraw(widget);
2181 frame_redraw_handler(struct widget *widget, void *data)
2184 struct window_frame *frame = data;
2185 struct window *window = widget->window;
2187 if (window->fullscreen)
2190 if (window->focused) {
2191 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2193 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2196 cr = widget_cairo_create(widget);
2198 frame_repaint(frame->frame, cr);
2204 frame_get_pointer_image_for_location(struct window_frame *frame,
2205 enum theme_location location)
2207 struct window *window = frame->widget->window;
2210 return CURSOR_LEFT_PTR;
2213 case THEME_LOCATION_RESIZING_TOP:
2215 case THEME_LOCATION_RESIZING_BOTTOM:
2216 return CURSOR_BOTTOM;
2217 case THEME_LOCATION_RESIZING_LEFT:
2219 case THEME_LOCATION_RESIZING_RIGHT:
2220 return CURSOR_RIGHT;
2221 case THEME_LOCATION_RESIZING_TOP_LEFT:
2222 return CURSOR_TOP_LEFT;
2223 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2224 return CURSOR_TOP_RIGHT;
2225 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2226 return CURSOR_BOTTOM_LEFT;
2227 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2228 return CURSOR_BOTTOM_RIGHT;
2229 case THEME_LOCATION_EXTERIOR:
2230 case THEME_LOCATION_TITLEBAR:
2232 return CURSOR_LEFT_PTR;
2237 frame_menu_func(struct window *window,
2238 struct input *input, int index, void *data)
2240 struct display *display;
2244 window_close(window);
2246 case 1: /* move to workspace above */
2247 display = window->display;
2248 if (display->workspace > 0)
2249 workspace_manager_move_surface(
2250 display->workspace_manager,
2251 window->main_surface->surface,
2252 display->workspace - 1);
2254 case 2: /* move to workspace below */
2255 display = window->display;
2256 if (display->workspace < display->workspace_count - 1)
2257 workspace_manager_move_surface(
2258 display->workspace_manager,
2259 window->main_surface->surface,
2260 display->workspace + 1);
2262 case 3: /* fullscreen */
2263 /* we don't have a way to get out of fullscreen for now */
2264 if (window->fullscreen_handler)
2265 window->fullscreen_handler(window, window->user_data);
2271 window_show_frame_menu(struct window *window,
2272 struct input *input, uint32_t time)
2277 static const char *entries[] = {
2279 "Move to workspace above", "Move to workspace below",
2283 if (window->fullscreen_handler)
2284 count = ARRAY_LENGTH(entries);
2286 count = ARRAY_LENGTH(entries) - 1;
2288 input_get_position(input, &x, &y);
2289 window_show_menu(window->display, input, time, window,
2290 x - 10, y - 10, frame_menu_func, entries, count);
2294 frame_enter_handler(struct widget *widget,
2295 struct input *input, float x, float y, void *data)
2297 struct window_frame *frame = data;
2298 enum theme_location location;
2300 location = frame_pointer_enter(frame->frame, input, x, y);
2301 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2302 widget_schedule_redraw(frame->widget);
2304 return frame_get_pointer_image_for_location(data, location);
2308 frame_motion_handler(struct widget *widget,
2309 struct input *input, uint32_t time,
2310 float x, float y, void *data)
2312 struct window_frame *frame = data;
2313 enum theme_location location;
2315 location = frame_pointer_motion(frame->frame, input, x, y);
2316 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2317 widget_schedule_redraw(frame->widget);
2319 return frame_get_pointer_image_for_location(data, location);
2323 frame_leave_handler(struct widget *widget,
2324 struct input *input, void *data)
2326 struct window_frame *frame = data;
2328 frame_pointer_leave(frame->frame, input);
2329 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2330 widget_schedule_redraw(frame->widget);
2334 frame_handle_status(struct window_frame *frame, struct input *input,
2335 uint32_t time, enum theme_location location)
2337 struct window *window = frame->widget->window;
2340 status = frame_status(frame->frame);
2341 if (status & FRAME_STATUS_REPAINT)
2342 widget_schedule_redraw(frame->widget);
2344 if (status & FRAME_STATUS_MINIMIZE) {
2345 window_set_minimized(window);
2346 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2349 if (status & FRAME_STATUS_MENU) {
2350 window_show_frame_menu(window, input, time);
2351 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2354 if (status & FRAME_STATUS_MAXIMIZE) {
2355 window_set_maximized(window, !window->maximized);
2356 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2359 if (status & FRAME_STATUS_CLOSE) {
2360 window_close(window);
2364 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2365 input_ungrab(input);
2366 xdg_surface_move(window->xdg_surface,
2367 input_get_seat(input),
2368 window->display->serial);
2370 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2373 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2374 input_ungrab(input);
2376 window->resizing = 1;
2377 xdg_surface_resize(window->xdg_surface,
2378 input_get_seat(input),
2379 window->display->serial,
2382 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2387 frame_button_handler(struct widget *widget,
2388 struct input *input, uint32_t time,
2389 uint32_t button, enum wl_pointer_button_state state,
2393 struct window_frame *frame = data;
2394 enum theme_location location;
2396 location = frame_pointer_button(frame->frame, input, button, state);
2397 frame_handle_status(frame, input, time, location);
2401 frame_touch_down_handler(struct widget *widget, struct input *input,
2402 uint32_t serial, uint32_t time, int32_t id,
2403 float x, float y, void *data)
2405 struct window_frame *frame = data;
2407 frame_touch_down(frame->frame, input, id, x, y);
2408 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2412 frame_touch_up_handler(struct widget *widget,
2413 struct input *input, uint32_t serial, uint32_t time,
2414 int32_t id, void *data)
2416 struct window_frame *frame = data;
2418 frame_touch_up(frame->frame, input, id);
2419 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2423 window_frame_create(struct window *window, void *data)
2425 struct window_frame *frame;
2428 if (window->custom) {
2429 buttons = FRAME_BUTTON_NONE;
2431 buttons = FRAME_BUTTON_ALL;
2434 frame = xzalloc(sizeof *frame);
2435 frame->frame = frame_create(window->display->theme, 0, 0,
2436 buttons, window->title);
2438 frame->widget = window_add_widget(window, frame);
2439 frame->child = widget_add_widget(frame->widget, data);
2441 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2442 widget_set_resize_handler(frame->widget, frame_resize_handler);
2443 widget_set_enter_handler(frame->widget, frame_enter_handler);
2444 widget_set_leave_handler(frame->widget, frame_leave_handler);
2445 widget_set_motion_handler(frame->widget, frame_motion_handler);
2446 widget_set_button_handler(frame->widget, frame_button_handler);
2447 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2448 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2450 window->frame = frame;
2452 return frame->child;
2456 window_frame_set_child_size(struct widget *widget, int child_width,
2459 struct display *display = widget->window->display;
2460 struct theme *t = display->theme;
2461 int decoration_width, decoration_height;
2463 int margin = widget->window->maximized ? 0 : t->margin;
2465 if (!widget->window->fullscreen) {
2466 decoration_width = (t->width + margin) * 2;
2467 decoration_height = t->width +
2468 t->titlebar_height + margin * 2;
2470 width = child_width + decoration_width;
2471 height = child_height + decoration_height;
2473 width = child_width;
2474 height = child_height;
2477 window_schedule_resize(widget->window, width, height);
2481 window_frame_destroy(struct window_frame *frame)
2483 frame_destroy(frame->frame);
2485 /* frame->child must be destroyed by the application */
2486 widget_destroy(frame->widget);
2491 input_set_focus_widget(struct input *input, struct widget *focus,
2494 struct widget *old, *widget;
2497 if (focus == input->focus_widget)
2500 old = input->focus_widget;
2504 widget = input->grab;
2505 if (widget->leave_handler)
2506 widget->leave_handler(old, input, widget->user_data);
2507 input->focus_widget = NULL;
2513 widget = input->grab;
2514 input->focus_widget = focus;
2515 if (widget->enter_handler)
2516 cursor = widget->enter_handler(focus, input, x, y,
2519 cursor = widget->default_cursor;
2521 input_set_pointer_image(input, cursor);
2526 touch_grab(struct input *input, int32_t touch_id)
2528 input->touch_grab = 1;
2529 input->touch_grab_id = touch_id;
2533 touch_ungrab(struct input *input)
2535 struct touch_point *tp, *tmp;
2537 input->touch_grab = 0;
2539 wl_list_for_each_safe(tp, tmp,
2540 &input->touch_point_list, link) {
2541 if (tp->id != input->touch_grab_id)
2543 wl_list_remove(&tp->link);
2551 input_grab(struct input *input, struct widget *widget, uint32_t button)
2553 input->grab = widget;
2554 input->grab_button = button;
2558 input_ungrab(struct input *input)
2560 struct widget *widget;
2563 if (input->pointer_focus) {
2564 widget = window_find_widget(input->pointer_focus,
2565 input->sx, input->sy);
2566 input_set_focus_widget(input, widget, input->sx, input->sy);
2571 input_remove_pointer_focus(struct input *input)
2573 struct window *window = input->pointer_focus;
2578 input_set_focus_widget(input, NULL, 0, 0);
2580 input->pointer_focus = NULL;
2581 input->current_cursor = CURSOR_UNSET;
2585 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2586 uint32_t serial, struct wl_surface *surface,
2587 wl_fixed_t sx_w, wl_fixed_t sy_w)
2589 struct input *input = data;
2590 struct window *window;
2591 struct widget *widget;
2592 float sx = wl_fixed_to_double(sx_w);
2593 float sy = wl_fixed_to_double(sy_w);
2596 /* enter event for a window we've just destroyed */
2600 input->display->serial = serial;
2601 input->pointer_enter_serial = serial;
2602 input->pointer_focus = wl_surface_get_user_data(surface);
2603 window = input->pointer_focus;
2605 if (window->resizing) {
2606 window->resizing = 0;
2607 /* Schedule a redraw to free the pool */
2608 window_schedule_redraw(window);
2614 widget = window_find_widget(window, sx, sy);
2615 input_set_focus_widget(input, widget, sx, sy);
2619 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2620 uint32_t serial, struct wl_surface *surface)
2622 struct input *input = data;
2624 input->display->serial = serial;
2625 input_remove_pointer_focus(input);
2629 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2630 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2632 struct input *input = data;
2633 struct window *window = input->pointer_focus;
2634 struct widget *widget;
2636 float sx = wl_fixed_to_double(sx_w);
2637 float sy = wl_fixed_to_double(sy_w);
2645 /* when making the window smaller - e.g. after a unmaximise we might
2646 * still have a pending motion event that the compositor has picked
2647 * based on the old surface dimensions
2649 if (sx > window->main_surface->allocation.width ||
2650 sy > window->main_surface->allocation.height)
2653 if (!(input->grab && input->grab_button)) {
2654 widget = window_find_widget(window, sx, sy);
2655 input_set_focus_widget(input, widget, sx, sy);
2659 widget = input->grab;
2661 widget = input->focus_widget;
2663 if (widget->motion_handler)
2664 cursor = widget->motion_handler(input->focus_widget,
2665 input, time, sx, sy,
2668 cursor = widget->default_cursor;
2670 cursor = CURSOR_LEFT_PTR;
2672 input_set_pointer_image(input, cursor);
2676 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2677 uint32_t time, uint32_t button, uint32_t state_w)
2679 struct input *input = data;
2680 struct widget *widget;
2681 enum wl_pointer_button_state state = state_w;
2683 input->display->serial = serial;
2684 if (input->focus_widget && input->grab == NULL &&
2685 state == WL_POINTER_BUTTON_STATE_PRESSED)
2686 input_grab(input, input->focus_widget, button);
2688 widget = input->grab;
2689 if (widget && widget->button_handler)
2690 (*widget->button_handler)(widget,
2693 input->grab->user_data);
2695 if (input->grab && input->grab_button == button &&
2696 state == WL_POINTER_BUTTON_STATE_RELEASED)
2697 input_ungrab(input);
2701 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2702 uint32_t time, uint32_t axis, wl_fixed_t value)
2704 struct input *input = data;
2705 struct widget *widget;
2707 widget = input->focus_widget;
2709 widget = input->grab;
2710 if (widget && widget->axis_handler)
2711 (*widget->axis_handler)(widget,
2717 static const struct wl_pointer_listener pointer_listener = {
2718 pointer_handle_enter,
2719 pointer_handle_leave,
2720 pointer_handle_motion,
2721 pointer_handle_button,
2722 pointer_handle_axis,
2726 input_remove_keyboard_focus(struct input *input)
2728 struct window *window = input->keyboard_focus;
2729 struct itimerspec its;
2731 its.it_interval.tv_sec = 0;
2732 its.it_interval.tv_nsec = 0;
2733 its.it_value.tv_sec = 0;
2734 its.it_value.tv_nsec = 0;
2735 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2740 if (window->keyboard_focus_handler)
2741 (*window->keyboard_focus_handler)(window, NULL,
2744 input->keyboard_focus = NULL;
2748 keyboard_repeat_func(struct task *task, uint32_t events)
2750 struct input *input =
2751 container_of(task, struct input, repeat_task);
2752 struct window *window = input->keyboard_focus;
2755 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2756 /* If we change the timer between the fd becoming
2757 * readable and getting here, there'll be nothing to
2758 * read and we get EAGAIN. */
2761 if (window && window->key_handler) {
2762 (*window->key_handler)(window, input, input->repeat_time,
2763 input->repeat_key, input->repeat_sym,
2764 WL_KEYBOARD_KEY_STATE_PRESSED,
2770 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2771 uint32_t format, int fd, uint32_t size)
2773 struct input *input = data;
2774 struct xkb_keymap *keymap;
2775 struct xkb_state *state;
2783 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2788 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2789 if (map_str == MAP_FAILED) {
2794 keymap = xkb_map_new_from_string(input->display->xkb_context,
2796 XKB_KEYMAP_FORMAT_TEXT_V1,
2798 munmap(map_str, size);
2802 fprintf(stderr, "failed to compile keymap\n");
2806 state = xkb_state_new(keymap);
2808 fprintf(stderr, "failed to create XKB state\n");
2809 xkb_map_unref(keymap);
2813 xkb_keymap_unref(input->xkb.keymap);
2814 xkb_state_unref(input->xkb.state);
2815 input->xkb.keymap = keymap;
2816 input->xkb.state = state;
2818 input->xkb.control_mask =
2819 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2820 input->xkb.alt_mask =
2821 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2822 input->xkb.shift_mask =
2823 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2827 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2828 uint32_t serial, struct wl_surface *surface,
2829 struct wl_array *keys)
2831 struct input *input = data;
2832 struct window *window;
2834 input->display->serial = serial;
2835 input->keyboard_focus = wl_surface_get_user_data(surface);
2837 window = input->keyboard_focus;
2838 if (window->keyboard_focus_handler)
2839 (*window->keyboard_focus_handler)(window,
2840 input, window->user_data);
2844 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2845 uint32_t serial, struct wl_surface *surface)
2847 struct input *input = data;
2849 input->display->serial = serial;
2850 input_remove_keyboard_focus(input);
2854 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2855 uint32_t serial, uint32_t time, uint32_t key,
2858 struct input *input = data;
2859 struct window *window = input->keyboard_focus;
2860 uint32_t code, num_syms;
2861 enum wl_keyboard_key_state state = state_w;
2862 const xkb_keysym_t *syms;
2864 struct itimerspec its;
2866 input->display->serial = serial;
2868 if (!window || !input->xkb.state)
2871 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2873 sym = XKB_KEY_NoSymbol;
2878 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2879 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2880 window_set_maximized(window, !window->maximized);
2881 } else if (sym == XKB_KEY_F11 &&
2882 window->fullscreen_handler &&
2883 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2884 window->fullscreen_handler(window, window->user_data);
2885 } else if (sym == XKB_KEY_F4 &&
2886 input->modifiers == MOD_ALT_MASK &&
2887 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2888 window_close(window);
2889 } else if (window->key_handler) {
2890 (*window->key_handler)(window, input, time, key,
2891 sym, state, window->user_data);
2894 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2895 key == input->repeat_key) {
2896 its.it_interval.tv_sec = 0;
2897 its.it_interval.tv_nsec = 0;
2898 its.it_value.tv_sec = 0;
2899 its.it_value.tv_nsec = 0;
2900 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2901 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2902 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2903 input->repeat_sym = sym;
2904 input->repeat_key = key;
2905 input->repeat_time = time;
2906 its.it_interval.tv_sec = 0;
2907 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2908 its.it_value.tv_sec = 0;
2909 its.it_value.tv_nsec = 400 * 1000 * 1000;
2910 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2915 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2916 uint32_t serial, uint32_t mods_depressed,
2917 uint32_t mods_latched, uint32_t mods_locked,
2920 struct input *input = data;
2921 xkb_mod_mask_t mask;
2923 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2924 if (!input->xkb.keymap)
2927 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2928 mods_locked, 0, 0, group);
2929 mask = xkb_state_serialize_mods(input->xkb.state,
2930 XKB_STATE_DEPRESSED |
2932 input->modifiers = 0;
2933 if (mask & input->xkb.control_mask)
2934 input->modifiers |= MOD_CONTROL_MASK;
2935 if (mask & input->xkb.alt_mask)
2936 input->modifiers |= MOD_ALT_MASK;
2937 if (mask & input->xkb.shift_mask)
2938 input->modifiers |= MOD_SHIFT_MASK;
2941 static const struct wl_keyboard_listener keyboard_listener = {
2942 keyboard_handle_keymap,
2943 keyboard_handle_enter,
2944 keyboard_handle_leave,
2945 keyboard_handle_key,
2946 keyboard_handle_modifiers,
2950 touch_handle_down(void *data, struct wl_touch *wl_touch,
2951 uint32_t serial, uint32_t time, struct wl_surface *surface,
2952 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2954 struct input *input = data;
2955 struct widget *widget;
2956 float sx = wl_fixed_to_double(x_w);
2957 float sy = wl_fixed_to_double(y_w);
2959 input->display->serial = serial;
2960 input->touch_focus = wl_surface_get_user_data(surface);
2961 if (!input->touch_focus) {
2962 DBG("Failed to find to touch focus for surface %p\n", surface);
2966 widget = window_find_widget(input->touch_focus,
2967 wl_fixed_to_double(x_w),
2968 wl_fixed_to_double(y_w));
2970 struct touch_point *tp = xmalloc(sizeof *tp);
2973 tp->widget = widget;
2976 wl_list_insert(&input->touch_point_list, &tp->link);
2978 if (widget->touch_down_handler)
2979 (*widget->touch_down_handler)(widget, input,
2988 touch_handle_up(void *data, struct wl_touch *wl_touch,
2989 uint32_t serial, uint32_t time, int32_t id)
2991 struct input *input = data;
2992 struct touch_point *tp, *tmp;
2994 if (!input->touch_focus) {
2995 DBG("No touch focus found for touch up event!\n");
2999 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3003 if (tp->widget->touch_up_handler)
3004 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3006 tp->widget->user_data);
3008 wl_list_remove(&tp->link);
3016 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3017 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3019 struct input *input = data;
3020 struct touch_point *tp;
3021 float sx = wl_fixed_to_double(x_w);
3022 float sy = wl_fixed_to_double(y_w);
3024 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3026 if (!input->touch_focus) {
3027 DBG("No touch focus found for touch motion event!\n");
3031 wl_list_for_each(tp, &input->touch_point_list, link) {
3037 if (tp->widget->touch_motion_handler)
3038 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3040 tp->widget->user_data);
3046 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3048 struct input *input = data;
3049 struct touch_point *tp, *tmp;
3051 DBG("touch_handle_frame\n");
3053 if (!input->touch_focus) {
3054 DBG("No touch focus found for touch frame event!\n");
3058 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3059 if (tp->widget->touch_frame_handler)
3060 (*tp->widget->touch_frame_handler)(tp->widget, input,
3061 tp->widget->user_data);
3063 wl_list_remove(&tp->link);
3069 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3071 struct input *input = data;
3072 struct touch_point *tp, *tmp;
3074 DBG("touch_handle_cancel\n");
3076 if (!input->touch_focus) {
3077 DBG("No touch focus found for touch cancel event!\n");
3081 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3082 if (tp->widget->touch_cancel_handler)
3083 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3084 tp->widget->user_data);
3086 wl_list_remove(&tp->link);
3091 static const struct wl_touch_listener touch_listener = {
3094 touch_handle_motion,
3096 touch_handle_cancel,
3100 seat_handle_capabilities(void *data, struct wl_seat *seat,
3101 enum wl_seat_capability caps)
3103 struct input *input = data;
3105 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3106 input->pointer = wl_seat_get_pointer(seat);
3107 wl_pointer_set_user_data(input->pointer, input);
3108 wl_pointer_add_listener(input->pointer, &pointer_listener,
3110 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3111 wl_pointer_destroy(input->pointer);
3112 input->pointer = NULL;
3115 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3116 input->keyboard = wl_seat_get_keyboard(seat);
3117 wl_keyboard_set_user_data(input->keyboard, input);
3118 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3120 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3121 wl_keyboard_destroy(input->keyboard);
3122 input->keyboard = NULL;
3125 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3126 input->touch = wl_seat_get_touch(seat);
3127 wl_touch_set_user_data(input->touch, input);
3128 wl_touch_add_listener(input->touch, &touch_listener, input);
3129 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3130 wl_touch_destroy(input->touch);
3131 input->touch = NULL;
3136 seat_handle_name(void *data, struct wl_seat *seat,
3142 static const struct wl_seat_listener seat_listener = {
3143 seat_handle_capabilities,
3148 input_get_position(struct input *input, int32_t *x, int32_t *y)
3155 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3157 struct touch_point *tp;
3159 wl_list_for_each(tp, &input->touch_point_list, link) {
3172 input_get_display(struct input *input)
3174 return input->display;
3178 input_get_seat(struct input *input)
3184 input_get_modifiers(struct input *input)
3186 return input->modifiers;
3190 input_get_focus_widget(struct input *input)
3192 return input->focus_widget;
3196 struct wl_data_offer *offer;
3197 struct input *input;
3198 struct wl_array types;
3201 struct task io_task;
3209 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3211 struct data_offer *offer = data;
3214 p = wl_array_add(&offer->types, sizeof *p);
3218 static const struct wl_data_offer_listener data_offer_listener = {
3223 data_offer_destroy(struct data_offer *offer)
3228 if (offer->refcount == 0) {
3229 wl_data_offer_destroy(offer->offer);
3230 for (p = offer->types.data; *p; p++)
3232 wl_array_release(&offer->types);
3238 data_device_data_offer(void *data,
3239 struct wl_data_device *data_device,
3240 struct wl_data_offer *_offer)
3242 struct data_offer *offer;
3244 offer = xmalloc(sizeof *offer);
3246 wl_array_init(&offer->types);
3247 offer->refcount = 1;
3248 offer->input = data;
3249 offer->offer = _offer;
3250 wl_data_offer_add_listener(offer->offer,
3251 &data_offer_listener, offer);
3255 data_device_enter(void *data, struct wl_data_device *data_device,
3256 uint32_t serial, struct wl_surface *surface,
3257 wl_fixed_t x_w, wl_fixed_t y_w,
3258 struct wl_data_offer *offer)
3260 struct input *input = data;
3261 struct window *window;
3263 float x = wl_fixed_to_double(x_w);
3264 float y = wl_fixed_to_double(y_w);
3267 window = wl_surface_get_user_data(surface);
3268 input->drag_enter_serial = serial;
3269 input->drag_focus = window,
3273 if (!input->touch_grab)
3274 input->pointer_enter_serial = serial;
3277 input->drag_offer = wl_data_offer_get_user_data(offer);
3279 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3282 types_data = input->drag_offer->types.data;
3284 input->drag_offer = NULL;
3288 if (window->data_handler)
3289 window->data_handler(window, input, x, y, types_data,
3294 data_device_leave(void *data, struct wl_data_device *data_device)
3296 struct input *input = data;
3298 if (input->drag_offer) {
3299 data_offer_destroy(input->drag_offer);
3300 input->drag_offer = NULL;
3305 data_device_motion(void *data, struct wl_data_device *data_device,
3306 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3308 struct input *input = data;
3309 struct window *window = input->drag_focus;
3310 float x = wl_fixed_to_double(x_w);
3311 float y = wl_fixed_to_double(y_w);
3317 if (input->drag_offer)
3318 types_data = input->drag_offer->types.data;
3322 if (window->data_handler)
3323 window->data_handler(window, input, x, y, types_data,
3328 data_device_drop(void *data, struct wl_data_device *data_device)
3330 struct input *input = data;
3331 struct window *window = input->drag_focus;
3337 if (window->drop_handler)
3338 window->drop_handler(window, input,
3339 x, y, window->user_data);
3341 if (input->touch_grab)
3342 touch_ungrab(input);
3346 data_device_selection(void *data,
3347 struct wl_data_device *wl_data_device,
3348 struct wl_data_offer *offer)
3350 struct input *input = data;
3353 if (input->selection_offer)
3354 data_offer_destroy(input->selection_offer);
3357 input->selection_offer = wl_data_offer_get_user_data(offer);
3358 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3361 input->selection_offer = NULL;
3365 static const struct wl_data_device_listener data_device_listener = {
3366 data_device_data_offer,
3371 data_device_selection
3375 input_set_pointer_image_index(struct input *input, int index)
3377 struct wl_buffer *buffer;
3378 struct wl_cursor *cursor;
3379 struct wl_cursor_image *image;
3381 if (!input->pointer)
3384 cursor = input->display->cursors[input->current_cursor];
3388 if (index >= (int) cursor->image_count) {
3389 fprintf(stderr, "cursor index out of range\n");
3393 image = cursor->images[index];
3394 buffer = wl_cursor_image_get_buffer(image);
3398 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3399 wl_surface_damage(input->pointer_surface, 0, 0,
3400 image->width, image->height);
3401 wl_surface_commit(input->pointer_surface);
3402 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3403 input->pointer_surface,
3404 image->hotspot_x, image->hotspot_y);
3407 static const struct wl_callback_listener pointer_surface_listener;
3410 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3413 struct input *input = data;
3414 struct wl_cursor *cursor;
3418 assert(callback == input->cursor_frame_cb);
3419 wl_callback_destroy(callback);
3420 input->cursor_frame_cb = NULL;
3423 if (!input->pointer)
3426 if (input->current_cursor == CURSOR_BLANK) {
3427 wl_pointer_set_cursor(input->pointer,
3428 input->pointer_enter_serial,
3433 if (input->current_cursor == CURSOR_UNSET)
3435 cursor = input->display->cursors[input->current_cursor];
3439 /* FIXME We don't have the current time on the first call so we set
3440 * the animation start to the time of the first frame callback. */
3442 input->cursor_anim_start = 0;
3443 else if (input->cursor_anim_start == 0)
3444 input->cursor_anim_start = time;
3446 if (time == 0 || input->cursor_anim_start == 0)
3449 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3451 if (cursor->image_count > 1) {
3452 input->cursor_frame_cb =
3453 wl_surface_frame(input->pointer_surface);
3454 wl_callback_add_listener(input->cursor_frame_cb,
3455 &pointer_surface_listener, input);
3458 input_set_pointer_image_index(input, i);
3461 static const struct wl_callback_listener pointer_surface_listener = {
3462 pointer_surface_frame_callback
3466 input_set_pointer_image(struct input *input, int pointer)
3470 if (!input->pointer)
3473 if (input->pointer_enter_serial > input->cursor_serial)
3476 if (!force && pointer == input->current_cursor)
3479 input->current_cursor = pointer;
3480 input->cursor_serial = input->pointer_enter_serial;
3481 if (!input->cursor_frame_cb)
3482 pointer_surface_frame_callback(input, NULL, 0);
3484 /* The current frame callback may be stuck if, for instance,
3485 * the set cursor request was processed by the server after
3486 * this client lost the focus. In this case the cursor surface
3487 * might not be mapped and the frame callback wouldn't ever
3488 * complete. Send a set_cursor and attach to try to map the
3489 * cursor surface again so that the callback will finish */
3490 input_set_pointer_image_index(input, 0);
3494 struct wl_data_device *
3495 input_get_data_device(struct input *input)
3497 return input->data_device;
3501 input_set_selection(struct input *input,
3502 struct wl_data_source *source, uint32_t time)
3504 wl_data_device_set_selection(input->data_device, source, time);
3508 input_accept(struct input *input, const char *type)
3510 wl_data_offer_accept(input->drag_offer->offer,
3511 input->drag_enter_serial, type);
3515 offer_io_func(struct task *task, uint32_t events)
3517 struct data_offer *offer =
3518 container_of(task, struct data_offer, io_task);
3522 len = read(offer->fd, buffer, sizeof buffer);
3523 offer->func(buffer, len,
3524 offer->x, offer->y, offer->user_data);
3528 data_offer_destroy(offer);
3533 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3534 data_func_t func, void *user_data)
3538 if (pipe2(p, O_CLOEXEC) == -1)
3541 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3544 offer->io_task.run = offer_io_func;
3548 offer->user_data = user_data;
3550 display_watch_fd(offer->input->display,
3551 offer->fd, EPOLLIN, &offer->io_task);
3555 input_receive_drag_data(struct input *input, const char *mime_type,
3556 data_func_t func, void *data)
3558 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3559 input->drag_offer->x = input->drag_x;
3560 input->drag_offer->y = input->drag_y;
3564 input_receive_drag_data_to_fd(struct input *input,
3565 const char *mime_type, int fd)
3567 if (input->drag_offer)
3568 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3574 input_receive_selection_data(struct input *input, const char *mime_type,
3575 data_func_t func, void *data)
3579 if (input->selection_offer == NULL)
3582 for (p = input->selection_offer->types.data; *p; p++)
3583 if (strcmp(mime_type, *p) == 0)
3589 data_offer_receive_data(input->selection_offer,
3590 mime_type, func, data);
3595 input_receive_selection_data_to_fd(struct input *input,
3596 const char *mime_type, int fd)
3598 if (input->selection_offer)
3599 wl_data_offer_receive(input->selection_offer->offer,
3606 window_move(struct window *window, struct input *input, uint32_t serial)
3608 if (!window->xdg_surface)
3611 xdg_surface_move(window->xdg_surface, input->seat, serial);
3615 surface_set_synchronized(struct surface *surface)
3617 if (!surface->subsurface)
3620 if (surface->synchronized)
3623 wl_subsurface_set_sync(surface->subsurface);
3624 surface->synchronized = 1;
3628 surface_set_synchronized_default(struct surface *surface)
3630 if (!surface->subsurface)
3633 if (surface->synchronized == surface->synchronized_default)
3636 if (surface->synchronized_default)
3637 wl_subsurface_set_sync(surface->subsurface);
3639 wl_subsurface_set_desync(surface->subsurface);
3641 surface->synchronized = surface->synchronized_default;
3645 surface_resize(struct surface *surface)
3647 struct widget *widget = surface->widget;
3648 struct wl_compositor *compositor = widget->window->display->compositor;
3650 if (surface->input_region) {
3651 wl_region_destroy(surface->input_region);
3652 surface->input_region = NULL;
3655 if (surface->opaque_region)
3656 wl_region_destroy(surface->opaque_region);
3658 surface->opaque_region = wl_compositor_create_region(compositor);
3660 if (widget->resize_handler)
3661 widget->resize_handler(widget,
3662 widget->allocation.width,
3663 widget->allocation.height,
3666 if (surface->subsurface &&
3667 (surface->allocation.x != widget->allocation.x ||
3668 surface->allocation.y != widget->allocation.y)) {
3669 wl_subsurface_set_position(surface->subsurface,
3670 widget->allocation.x,
3671 widget->allocation.y);
3673 if (surface->allocation.width != widget->allocation.width ||
3674 surface->allocation.height != widget->allocation.height) {
3675 window_schedule_redraw(widget->window);
3677 surface->allocation = widget->allocation;
3680 wl_region_add(surface->opaque_region, 0, 0,
3681 widget->allocation.width,
3682 widget->allocation.height);
3686 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3689 * This hack should be removed, when EGL respects
3690 * eglSwapInterval(0).
3692 * If this window has sub-surfaces, especially a free-running
3693 * EGL-widget, we need to post the parent surface once with
3694 * all the old state to guarantee, that the EGL-widget will
3695 * receive its frame callback soon. Otherwise, a forced call
3696 * to eglSwapBuffers may end up blocking, waiting for a frame
3697 * event that will never come, because we will commit the parent
3698 * surface with all new state only after eglSwapBuffers returns.
3700 * This assumes, that:
3701 * 1. When the EGL widget's resize hook is called, it pauses.
3702 * 2. When the EGL widget's redraw hook is called, it forces a
3703 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3704 * In a single threaded application condition 1 is a no-op.
3706 * XXX: This should actually be after the surface_resize() calls,
3707 * but cannot, because then it would commit the incomplete state
3708 * accumulated from the widget resize hooks.
3710 if (window->subsurface_list.next != &window->main_surface->link ||
3711 window->subsurface_list.prev != &window->main_surface->link)
3712 wl_surface_commit(window->main_surface->surface);
3716 window_do_resize(struct window *window)
3718 struct surface *surface;
3720 widget_set_allocation(window->main_surface->widget,
3721 window->pending_allocation.x,
3722 window->pending_allocation.y,
3723 window->pending_allocation.width,
3724 window->pending_allocation.height);
3726 surface_resize(window->main_surface);
3728 /* The main surface is in the list, too. Main surface's
3729 * resize_handler is responsible for calling widget_set_allocation()
3730 * on all sub-surface root widgets, so they will be resized
3733 wl_list_for_each(surface, &window->subsurface_list, link) {
3734 if (surface == window->main_surface)
3737 surface_set_synchronized(surface);
3738 surface_resize(surface);
3741 if (!window->fullscreen && !window->maximized)
3742 window->saved_allocation = window->pending_allocation;
3746 idle_resize(struct window *window)
3748 window->resize_needed = 0;
3749 window->redraw_needed = 1;
3751 DBG("from %dx%d to %dx%d\n",
3752 window->main_surface->server_allocation.width,
3753 window->main_surface->server_allocation.height,
3754 window->pending_allocation.width,
3755 window->pending_allocation.height);
3757 hack_prevent_EGL_sub_surface_deadlock(window);
3759 window_do_resize(window);
3763 undo_resize(struct window *window)
3765 window->pending_allocation.width =
3766 window->main_surface->server_allocation.width;
3767 window->pending_allocation.height =
3768 window->main_surface->server_allocation.height;
3770 DBG("back to %dx%d\n",
3771 window->main_surface->server_allocation.width,
3772 window->main_surface->server_allocation.height);
3774 window_do_resize(window);
3776 if (window->pending_allocation.width == 0 &&
3777 window->pending_allocation.height == 0) {
3778 fprintf(stderr, "Error: Could not draw a surface, "
3779 "most likely due to insufficient disk space in "
3780 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3786 window_schedule_resize(struct window *window, int width, int height)
3788 /* We should probably get these numbers from the theme. */
3789 const int min_width = 200, min_height = 200;
3791 window->pending_allocation.x = 0;
3792 window->pending_allocation.y = 0;
3793 window->pending_allocation.width = width;
3794 window->pending_allocation.height = height;
3796 if (window->min_allocation.width == 0) {
3797 if (width < min_width && window->frame)
3798 window->min_allocation.width = min_width;
3800 window->min_allocation.width = width;
3801 if (height < min_height && window->frame)
3802 window->min_allocation.height = min_height;
3804 window->min_allocation.height = height;
3807 if (window->pending_allocation.width < window->min_allocation.width)
3808 window->pending_allocation.width = window->min_allocation.width;
3809 if (window->pending_allocation.height < window->min_allocation.height)
3810 window->pending_allocation.height = window->min_allocation.height;
3812 window->resize_needed = 1;
3813 window_schedule_redraw(window);
3817 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3819 window_schedule_resize(widget->window, width, height);
3823 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3824 int32_t width, int32_t height)
3826 struct window *window = data;
3828 window_schedule_resize(window, width, height);
3832 handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
3837 struct window *window = data;
3840 case XDG_SURFACE_STATE_MAXIMIZED:
3841 window->maximized = value;
3843 case XDG_SURFACE_STATE_FULLSCREEN:
3844 window->fullscreen = value;
3848 if (!window->fullscreen && !window->maximized)
3849 window_schedule_resize(window,
3850 window->saved_allocation.width,
3851 window->saved_allocation.height);
3853 xdg_surface_ack_change_state(xdg_surface, state, value, serial);
3854 window_schedule_redraw(window);
3858 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
3860 struct window *window = data;
3861 window->focused = 1;
3865 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
3867 struct window *window = data;
3868 window->focused = 0;
3872 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3874 struct window *window = data;
3875 window_close(window);
3878 static const struct xdg_surface_listener xdg_surface_listener = {
3879 handle_surface_configure,
3880 handle_surface_change_state,
3881 handle_surface_activated,
3882 handle_surface_deactivated,
3883 handle_surface_delete,
3887 window_sync_transient_for(struct window *window)
3889 struct wl_surface *parent_surface;
3891 if (!window->xdg_surface)
3894 if (window->transient_for)
3895 parent_surface = window->transient_for->main_surface->surface;
3897 parent_surface = NULL;
3899 xdg_surface_set_transient_for(window->xdg_surface, parent_surface);
3903 window_sync_margin(struct window *window)
3907 if (!window->xdg_surface)
3913 margin = frame_get_shadow_margin(window->frame->frame);
3915 /* Shadow size is the same on every side. */
3916 xdg_surface_set_margin(window->xdg_surface,
3924 window_flush(struct window *window)
3926 struct surface *surface;
3928 if (!window->custom) {
3929 if (!window->xdg_popup && !window->xdg_surface) {
3930 window->xdg_surface = xdg_shell_get_xdg_surface(window->display->xdg_shell,
3931 window->main_surface->surface);
3932 fail_on_null(window->xdg_surface);
3934 xdg_surface_set_user_data(window->xdg_surface, window);
3935 xdg_surface_add_listener(window->xdg_surface,
3936 &xdg_surface_listener, window);
3938 window_sync_transient_for(window);
3939 window_sync_margin(window);
3943 wl_list_for_each(surface, &window->subsurface_list, link) {
3944 if (surface == window->main_surface)
3947 surface_flush(surface);
3950 surface_flush(window->main_surface);
3954 menu_destroy(struct menu *menu)
3956 widget_destroy(menu->widget);
3957 window_destroy(menu->window);
3958 frame_destroy(menu->frame);
3963 window_get_allocation(struct window *window,
3964 struct rectangle *allocation)
3966 *allocation = window->main_surface->allocation;
3970 widget_redraw(struct widget *widget)
3972 struct widget *child;
3974 if (widget->redraw_handler)
3975 widget->redraw_handler(widget, widget->user_data);
3976 wl_list_for_each(child, &widget->child_list, link)
3977 widget_redraw(child);
3981 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3983 struct surface *surface = data;
3985 assert(callback == surface->frame_cb);
3986 DBG_OBJ(callback, "done\n");
3987 wl_callback_destroy(callback);
3988 surface->frame_cb = NULL;
3990 surface->last_time = time;
3992 if (surface->redraw_needed || surface->window->redraw_needed) {
3993 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3994 window_schedule_redraw_task(surface->window);
3998 static const struct wl_callback_listener listener = {
4003 surface_redraw(struct surface *surface)
4005 DBG_OBJ(surface->surface, "begin\n");
4007 if (!surface->window->redraw_needed && !surface->redraw_needed)
4010 /* Whole-window redraw forces a redraw even if the previous has
4011 * not yet hit the screen.
4013 if (surface->frame_cb) {
4014 if (!surface->window->redraw_needed)
4017 DBG_OBJ(surface->frame_cb, "cancelled\n");
4018 wl_callback_destroy(surface->frame_cb);
4021 if (surface->widget->use_cairo &&
4022 !widget_get_cairo_surface(surface->widget)) {
4023 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4027 surface->frame_cb = wl_surface_frame(surface->surface);
4028 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4029 DBG_OBJ(surface->frame_cb, "new\n");
4031 surface->redraw_needed = 0;
4032 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4033 widget_redraw(surface->widget);
4034 DBG_OBJ(surface->surface, "done\n");
4039 idle_redraw(struct task *task, uint32_t events)
4041 struct window *window = container_of(task, struct window, redraw_task);
4042 struct surface *surface;
4046 DBG(" --------- \n");
4048 wl_list_init(&window->redraw_task.link);
4049 window->redraw_task_scheduled = 0;
4051 if (window->resize_needed) {
4052 /* throttle resizing to the main surface display */
4053 if (window->main_surface->frame_cb) {
4054 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4058 idle_resize(window);
4062 if (surface_redraw(window->main_surface) < 0) {
4064 * Only main_surface failure will cause us to undo the resize.
4065 * If sub-surfaces fail, they will just be broken with old
4070 wl_list_for_each(surface, &window->subsurface_list, link) {
4071 if (surface == window->main_surface)
4074 surface_redraw(surface);
4078 window->redraw_needed = 0;
4079 window_flush(window);
4081 wl_list_for_each(surface, &window->subsurface_list, link)
4082 surface_set_synchronized_default(surface);
4084 if (resized && failed) {
4085 /* Restore widget tree to correspond to what is on screen. */
4086 undo_resize(window);
4091 window_schedule_redraw_task(struct window *window)
4093 if (!window->redraw_task_scheduled) {
4094 window->redraw_task.run = idle_redraw;
4095 display_defer(window->display, &window->redraw_task);
4096 window->redraw_task_scheduled = 1;
4101 window_schedule_redraw(struct window *window)
4103 struct surface *surface;
4105 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4107 wl_list_for_each(surface, &window->subsurface_list, link)
4108 surface->redraw_needed = 1;
4110 window_schedule_redraw_task(window);
4114 window_is_fullscreen(struct window *window)
4116 return window->fullscreen;
4120 window_set_fullscreen(struct window *window, int fullscreen)
4122 if (!window->xdg_surface)
4125 if (window->fullscreen == fullscreen)
4128 xdg_surface_request_change_state(window->xdg_surface,
4129 XDG_SURFACE_STATE_FULLSCREEN,
4135 window_is_maximized(struct window *window)
4137 return window->maximized;
4141 window_set_maximized(struct window *window, int maximized)
4143 if (!window->xdg_surface)
4146 if (window->maximized == maximized)
4149 xdg_surface_request_change_state(window->xdg_surface,
4150 XDG_SURFACE_STATE_MAXIMIZED,
4156 window_set_minimized(struct window *window)
4158 if (!window->xdg_surface)
4161 xdg_surface_set_minimized(window->xdg_surface);
4165 window_set_user_data(struct window *window, void *data)
4167 window->user_data = data;
4171 window_get_user_data(struct window *window)
4173 return window->user_data;
4177 window_set_key_handler(struct window *window,
4178 window_key_handler_t handler)
4180 window->key_handler = handler;
4184 window_set_keyboard_focus_handler(struct window *window,
4185 window_keyboard_focus_handler_t handler)
4187 window->keyboard_focus_handler = handler;
4191 window_set_data_handler(struct window *window, window_data_handler_t handler)
4193 window->data_handler = handler;
4197 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4199 window->drop_handler = handler;
4203 window_set_close_handler(struct window *window,
4204 window_close_handler_t handler)
4206 window->close_handler = handler;
4210 window_set_fullscreen_handler(struct window *window,
4211 window_fullscreen_handler_t handler)
4213 window->fullscreen_handler = handler;
4217 window_set_output_handler(struct window *window,
4218 window_output_handler_t handler)
4220 window->output_handler = handler;
4224 window_set_title(struct window *window, const char *title)
4226 free(window->title);
4227 window->title = strdup(title);
4228 if (window->frame) {
4229 frame_set_title(window->frame->frame, title);
4230 widget_schedule_redraw(window->frame->widget);
4232 if (window->xdg_surface)
4233 xdg_surface_set_title(window->xdg_surface, title);
4237 window_get_title(struct window *window)
4239 return window->title;
4243 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4245 struct text_cursor_position *text_cursor_position =
4246 window->display->text_cursor_position;
4248 if (!text_cursor_position)
4251 text_cursor_position_notify(text_cursor_position,
4252 window->main_surface->surface,
4253 wl_fixed_from_int(x),
4254 wl_fixed_from_int(y));
4258 window_damage(struct window *window, int32_t x, int32_t y,
4259 int32_t width, int32_t height)
4261 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4265 surface_enter(void *data,
4266 struct wl_surface *wl_surface, struct wl_output *wl_output)
4268 struct window *window = data;
4269 struct output *output;
4270 struct output *output_found = NULL;
4271 struct window_output *window_output;
4273 wl_list_for_each(output, &window->display->output_list, link) {
4274 if (output->output == wl_output) {
4275 output_found = output;
4283 window_output = xmalloc(sizeof *window_output);
4284 window_output->output = output_found;
4286 wl_list_insert (&window->window_output_list, &window_output->link);
4288 if (window->output_handler)
4289 window->output_handler(window, output_found, 1,
4294 surface_leave(void *data,
4295 struct wl_surface *wl_surface, struct wl_output *output)
4297 struct window *window = data;
4298 struct window_output *window_output;
4299 struct window_output *window_output_found = NULL;
4301 wl_list_for_each(window_output, &window->window_output_list, link) {
4302 if (window_output->output->output == output) {
4303 window_output_found = window_output;
4308 if (window_output_found) {
4309 wl_list_remove(&window_output_found->link);
4311 if (window->output_handler)
4312 window->output_handler(window, window_output->output,
4313 0, window->user_data);
4315 free(window_output_found);
4319 static const struct wl_surface_listener surface_listener = {
4324 static struct surface *
4325 surface_create(struct window *window)
4327 struct display *display = window->display;
4328 struct surface *surface;
4330 surface = xmalloc(sizeof *surface);
4331 memset(surface, 0, sizeof *surface);
4335 surface->window = window;
4336 surface->surface = wl_compositor_create_surface(display->compositor);
4337 surface->buffer_scale = 1;
4338 wl_surface_add_listener(surface->surface, &surface_listener, window);
4340 wl_list_insert(&window->subsurface_list, &surface->link);
4345 static struct window *
4346 window_create_internal(struct display *display, int custom)
4348 struct window *window;
4349 struct surface *surface;
4351 window = xzalloc(sizeof *window);
4352 wl_list_init(&window->subsurface_list);
4353 window->display = display;
4355 surface = surface_create(window);
4356 window->main_surface = surface;
4358 fail_on_null(display->xdg_shell);
4360 window->custom = custom;
4361 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4363 if (display->argb_device)
4364 #ifdef HAVE_CAIRO_EGL
4365 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4367 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4370 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4372 wl_surface_set_user_data(surface->surface, window);
4373 wl_list_insert(display->window_list.prev, &window->link);
4374 wl_list_init(&window->redraw_task.link);
4376 wl_list_init (&window->window_output_list);
4382 window_create(struct display *display)
4384 return window_create_internal(display, 0);
4388 window_create_custom(struct display *display)
4390 return window_create_internal(display, 1);
4394 window_set_transient_for(struct window *window,
4395 struct window *parent_window)
4397 window->transient_for = parent_window;
4398 window_sync_transient_for(window);
4402 window_get_transient_for(struct window *window)
4404 return window->transient_for;
4408 menu_set_item(struct menu *menu, int sy)
4410 int32_t x, y, width, height;
4413 frame_interior(menu->frame, &x, &y, &width, &height);
4414 next = (sy - y) / 20;
4415 if (menu->current != next) {
4416 menu->current = next;
4417 widget_schedule_redraw(menu->widget);
4422 menu_motion_handler(struct widget *widget,
4423 struct input *input, uint32_t time,
4424 float x, float y, void *data)
4426 struct menu *menu = data;
4428 if (widget == menu->widget)
4429 menu_set_item(data, y);
4431 return CURSOR_LEFT_PTR;
4435 menu_enter_handler(struct widget *widget,
4436 struct input *input, float x, float y, void *data)
4438 struct menu *menu = data;
4440 if (widget == menu->widget)
4441 menu_set_item(data, y);
4443 return CURSOR_LEFT_PTR;
4447 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4449 struct menu *menu = data;
4451 if (widget == menu->widget)
4452 menu_set_item(data, -200);
4456 menu_button_handler(struct widget *widget,
4457 struct input *input, uint32_t time,
4458 uint32_t button, enum wl_pointer_button_state state,
4462 struct menu *menu = data;
4464 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4465 (menu->release_count > 0 || time - menu->time > 500)) {
4466 /* Either relase after press-drag-release or
4467 * click-motion-click. */
4468 menu->func(menu->parent, input,
4469 menu->current, menu->parent->user_data);
4470 input_ungrab(input);
4472 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4473 menu->release_count++;
4478 menu_redraw_handler(struct widget *widget, void *data)
4481 struct menu *menu = data;
4482 int32_t x, y, width, height, i;
4484 cr = widget_cairo_create(widget);
4486 frame_repaint(menu->frame, cr);
4487 frame_interior(menu->frame, &x, &y, &width, &height);
4489 theme_set_background_source(menu->window->display->theme,
4490 cr, THEME_FRAME_ACTIVE);
4491 cairo_rectangle(cr, x, y, width, height);
4494 cairo_select_font_face(cr, "sans",
4495 CAIRO_FONT_SLANT_NORMAL,
4496 CAIRO_FONT_WEIGHT_NORMAL);
4497 cairo_set_font_size(cr, 12);
4499 for (i = 0; i < menu->count; i++) {
4500 if (i == menu->current) {
4501 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4502 cairo_rectangle(cr, x, y + i * 20, width, 20);
4504 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4505 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4506 cairo_show_text(cr, menu->entries[i]);
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]);
4518 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4520 struct window *window = data;
4521 struct menu *menu = window->main_surface->widget->user_data;
4523 input_ungrab(menu->input);
4527 static const struct xdg_popup_listener xdg_popup_listener = {
4528 handle_popup_popup_done,
4532 window_show_menu(struct display *display,
4533 struct input *input, uint32_t time, struct window *parent,
4534 int32_t x, int32_t y,
4535 menu_func_t func, const char **entries, int count)
4537 struct window *window;
4541 menu = malloc(sizeof *menu);
4545 window = window_create_internal(parent->display, 0);
4551 menu->window = window;
4552 menu->parent = parent;
4553 menu->widget = window_add_widget(menu->window, menu);
4554 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4555 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4556 menu->frame = frame_create(window->display->theme, 0, 0,
4557 FRAME_BUTTON_NONE, NULL);
4558 fail_on_null(menu->frame);
4559 menu->entries = entries;
4560 menu->count = count;
4561 menu->release_count = 0;
4565 menu->input = input;
4569 input_ungrab(input);
4571 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4572 widget_set_enter_handler(menu->widget, menu_enter_handler);
4573 widget_set_leave_handler(menu->widget, menu_leave_handler);
4574 widget_set_motion_handler(menu->widget, menu_motion_handler);
4575 widget_set_button_handler(menu->widget, menu_button_handler);
4577 input_grab(input, menu->widget, 0);
4578 frame_resize_inside(menu->frame, 200, count * 20);
4579 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4580 window_schedule_resize(window, frame_width(menu->frame),
4581 frame_height(menu->frame));
4583 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4585 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4586 window->main_surface->surface,
4587 parent->main_surface->surface,
4589 display_get_serial(window->display),
4593 fail_on_null(window->xdg_popup);
4595 xdg_popup_set_user_data(window->xdg_popup, window);
4596 xdg_popup_add_listener(window->xdg_popup,
4597 &xdg_popup_listener, window);
4601 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4603 window->main_surface->buffer_type = type;
4607 window_set_preferred_format(struct window *window,
4608 enum preferred_format format)
4610 window->preferred_format = format;
4614 window_add_subsurface(struct window *window, void *data,
4615 enum subsurface_mode default_mode)
4617 struct widget *widget;
4618 struct surface *surface;
4619 struct wl_surface *parent;
4620 struct wl_subcompositor *subcompo = window->display->subcompositor;
4622 surface = surface_create(window);
4623 widget = widget_create(window, surface, data);
4624 wl_list_init(&widget->link);
4625 surface->widget = widget;
4627 parent = window->main_surface->surface;
4628 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4631 surface->synchronized = 1;
4633 switch (default_mode) {
4634 case SUBSURFACE_SYNCHRONIZED:
4635 surface->synchronized_default = 1;
4637 case SUBSURFACE_DESYNCHRONIZED:
4638 surface->synchronized_default = 0;
4641 assert(!"bad enum subsurface_mode");
4644 window->resize_needed = 1;
4645 window_schedule_redraw(window);
4651 display_handle_geometry(void *data,
4652 struct wl_output *wl_output,
4655 int physical_height,
4661 struct output *output = data;
4663 output->allocation.x = x;
4664 output->allocation.y = y;
4665 output->transform = transform;
4669 display_handle_done(void *data,
4670 struct wl_output *wl_output)
4675 display_handle_scale(void *data,
4676 struct wl_output *wl_output,
4679 struct output *output = data;
4681 output->scale = scale;
4685 display_handle_mode(void *data,
4686 struct wl_output *wl_output,
4692 struct output *output = data;
4693 struct display *display = output->display;
4695 if (flags & WL_OUTPUT_MODE_CURRENT) {
4696 output->allocation.width = width;
4697 output->allocation.height = height;
4698 if (display->output_configure_handler)
4699 (*display->output_configure_handler)(
4700 output, display->user_data);
4704 static const struct wl_output_listener output_listener = {
4705 display_handle_geometry,
4706 display_handle_mode,
4707 display_handle_done,
4708 display_handle_scale
4712 display_add_output(struct display *d, uint32_t id)
4714 struct output *output;
4716 output = xzalloc(sizeof *output);
4717 output->display = d;
4720 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4721 output->server_output_id = id;
4722 wl_list_insert(d->output_list.prev, &output->link);
4724 wl_output_add_listener(output->output, &output_listener, output);
4728 output_destroy(struct output *output)
4730 if (output->destroy_handler)
4731 (*output->destroy_handler)(output, output->user_data);
4733 wl_output_destroy(output->output);
4734 wl_list_remove(&output->link);
4739 display_destroy_output(struct display *d, uint32_t id)
4741 struct output *output;
4743 wl_list_for_each(output, &d->output_list, link) {
4744 if (output->server_output_id == id) {
4745 output_destroy(output);
4752 display_set_global_handler(struct display *display,
4753 display_global_handler_t handler)
4755 struct global *global;
4757 display->global_handler = handler;
4761 wl_list_for_each(global, &display->global_list, link)
4762 display->global_handler(display,
4763 global->name, global->interface,
4764 global->version, display->user_data);
4768 display_set_global_handler_remove(struct display *display,
4769 display_global_handler_t remove_handler)
4771 display->global_handler_remove = remove_handler;
4772 if (!remove_handler)
4777 display_set_output_configure_handler(struct display *display,
4778 display_output_handler_t handler)
4780 struct output *output;
4782 display->output_configure_handler = handler;
4786 wl_list_for_each(output, &display->output_list, link) {
4787 if (output->allocation.width == 0 &&
4788 output->allocation.height == 0)
4791 (*display->output_configure_handler)(output,
4792 display->user_data);
4797 output_set_user_data(struct output *output, void *data)
4799 output->user_data = data;
4803 output_get_user_data(struct output *output)
4805 return output->user_data;
4809 output_set_destroy_handler(struct output *output,
4810 display_output_handler_t handler)
4812 output->destroy_handler = handler;
4813 /* FIXME: implement this, once we have way to remove outputs */
4817 output_get_allocation(struct output *output, struct rectangle *base)
4819 struct rectangle allocation = output->allocation;
4821 switch (output->transform) {
4822 case WL_OUTPUT_TRANSFORM_90:
4823 case WL_OUTPUT_TRANSFORM_270:
4824 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4825 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4826 /* Swap width and height */
4827 allocation.width = output->allocation.height;
4828 allocation.height = output->allocation.width;
4836 output_get_wl_output(struct output *output)
4838 return output->output;
4841 enum wl_output_transform
4842 output_get_transform(struct output *output)
4844 return output->transform;
4848 output_get_scale(struct output *output)
4850 return output->scale;
4854 fini_xkb(struct input *input)
4856 xkb_state_unref(input->xkb.state);
4857 xkb_map_unref(input->xkb.keymap);
4860 #define MIN(a,b) ((a) < (b) ? a : b)
4863 display_add_input(struct display *d, uint32_t id)
4865 struct input *input;
4867 input = xzalloc(sizeof *input);
4869 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4870 MIN(d->seat_version, 3));
4871 input->touch_focus = NULL;
4872 input->pointer_focus = NULL;
4873 input->keyboard_focus = NULL;
4874 wl_list_init(&input->touch_point_list);
4875 wl_list_insert(d->input_list.prev, &input->link);
4877 wl_seat_add_listener(input->seat, &seat_listener, input);
4878 wl_seat_set_user_data(input->seat, input);
4880 input->data_device =
4881 wl_data_device_manager_get_data_device(d->data_device_manager,
4883 wl_data_device_add_listener(input->data_device, &data_device_listener,
4886 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4888 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4889 TFD_CLOEXEC | TFD_NONBLOCK);
4890 input->repeat_task.run = keyboard_repeat_func;
4891 display_watch_fd(d, input->repeat_timer_fd,
4892 EPOLLIN, &input->repeat_task);
4896 input_destroy(struct input *input)
4898 input_remove_keyboard_focus(input);
4899 input_remove_pointer_focus(input);
4901 if (input->drag_offer)
4902 data_offer_destroy(input->drag_offer);
4904 if (input->selection_offer)
4905 data_offer_destroy(input->selection_offer);
4907 wl_data_device_destroy(input->data_device);
4909 if (input->display->seat_version >= 3) {
4911 wl_pointer_release(input->pointer);
4912 if (input->keyboard)
4913 wl_keyboard_release(input->keyboard);
4918 wl_surface_destroy(input->pointer_surface);
4920 wl_list_remove(&input->link);
4921 wl_seat_destroy(input->seat);
4922 close(input->repeat_timer_fd);
4927 init_workspace_manager(struct display *d, uint32_t id)
4929 d->workspace_manager =
4930 wl_registry_bind(d->registry, id,
4931 &workspace_manager_interface, 1);
4932 if (d->workspace_manager != NULL)
4933 workspace_manager_add_listener(d->workspace_manager,
4934 &workspace_manager_listener,
4939 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4941 struct display *d = data;
4943 if (format == WL_SHM_FORMAT_RGB565)
4947 struct wl_shm_listener shm_listener = {
4952 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
4954 xdg_shell_pong(shell, serial);
4957 static const struct xdg_shell_listener xdg_shell_listener = {
4961 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
4962 #ifdef static_assert
4963 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
4964 "Interface version doesn't match implementation version");
4968 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4969 const char *interface, uint32_t version)
4971 struct display *d = data;
4972 struct global *global;
4974 global = xmalloc(sizeof *global);
4976 global->interface = strdup(interface);
4977 global->version = version;
4978 wl_list_insert(d->global_list.prev, &global->link);
4980 if (strcmp(interface, "wl_compositor") == 0) {
4981 d->compositor = wl_registry_bind(registry, id,
4982 &wl_compositor_interface, 3);
4983 } else if (strcmp(interface, "wl_output") == 0) {
4984 display_add_output(d, id);
4985 } else if (strcmp(interface, "wl_seat") == 0) {
4986 d->seat_version = version;
4987 display_add_input(d, id);
4988 } else if (strcmp(interface, "wl_shm") == 0) {
4989 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4990 wl_shm_add_listener(d->shm, &shm_listener, d);
4991 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4992 d->data_device_manager =
4993 wl_registry_bind(registry, id,
4994 &wl_data_device_manager_interface, 1);
4995 } else if (strcmp(interface, "xdg_shell") == 0) {
4996 d->xdg_shell = wl_registry_bind(registry, id,
4997 &xdg_shell_interface, 1);
4998 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
4999 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5000 } else if (strcmp(interface, "text_cursor_position") == 0) {
5001 d->text_cursor_position =
5002 wl_registry_bind(registry, id,
5003 &text_cursor_position_interface, 1);
5004 } else if (strcmp(interface, "workspace_manager") == 0) {
5005 init_workspace_manager(d, id);
5006 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5008 wl_registry_bind(registry, id,
5009 &wl_subcompositor_interface, 1);
5012 if (d->global_handler)
5013 d->global_handler(d, id, interface, version, d->user_data);
5017 registry_handle_global_remove(void *data, struct wl_registry *registry,
5020 struct display *d = data;
5021 struct global *global;
5024 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5025 if (global->name != name)
5028 if (strcmp(global->interface, "wl_output") == 0)
5029 display_destroy_output(d, name);
5031 /* XXX: Should destroy remaining bound globals */
5033 if (d->global_handler_remove)
5034 d->global_handler_remove(d, name, global->interface,
5035 global->version, d->user_data);
5037 wl_list_remove(&global->link);
5038 free(global->interface);
5044 display_bind(struct display *display, uint32_t name,
5045 const struct wl_interface *interface, uint32_t version)
5047 return wl_registry_bind(display->registry, name, interface, version);
5050 static const struct wl_registry_listener registry_listener = {
5051 registry_handle_global,
5052 registry_handle_global_remove
5055 #ifdef HAVE_CAIRO_EGL
5057 init_egl(struct display *d)
5059 EGLint major, minor;
5062 #ifdef USE_CAIRO_GLESV2
5063 # define GL_BIT EGL_OPENGL_ES2_BIT
5065 # define GL_BIT EGL_OPENGL_BIT
5068 static const EGLint argb_cfg_attribs[] = {
5069 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5075 EGL_RENDERABLE_TYPE, GL_BIT,
5079 #ifdef USE_CAIRO_GLESV2
5080 static const EGLint context_attribs[] = {
5081 EGL_CONTEXT_CLIENT_VERSION, 2,
5084 EGLint api = EGL_OPENGL_ES_API;
5086 EGLint *context_attribs = NULL;
5087 EGLint api = EGL_OPENGL_API;
5090 d->dpy = eglGetDisplay(d->display);
5091 if (!eglInitialize(d->dpy, &major, &minor)) {
5092 fprintf(stderr, "failed to initialize EGL\n");
5096 if (!eglBindAPI(api)) {
5097 fprintf(stderr, "failed to bind EGL client API\n");
5101 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5102 &d->argb_config, 1, &n) || n != 1) {
5103 fprintf(stderr, "failed to choose argb EGL config\n");
5107 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5108 EGL_NO_CONTEXT, context_attribs);
5109 if (d->argb_ctx == NULL) {
5110 fprintf(stderr, "failed to create EGL context\n");
5114 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5115 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5116 fprintf(stderr, "failed to get cairo EGL argb device\n");
5124 fini_egl(struct display *display)
5126 cairo_device_destroy(display->argb_device);
5128 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5131 eglTerminate(display->dpy);
5137 init_dummy_surface(struct display *display)
5142 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5144 display->dummy_surface =
5145 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5147 display->dummy_surface_data = data;
5151 handle_display_data(struct task *task, uint32_t events)
5153 struct display *display =
5154 container_of(task, struct display, display_task);
5155 struct epoll_event ep;
5158 display->display_fd_events = events;
5160 if (events & EPOLLERR || events & EPOLLHUP) {
5161 display_exit(display);
5165 if (events & EPOLLIN) {
5166 ret = wl_display_dispatch(display->display);
5168 display_exit(display);
5173 if (events & EPOLLOUT) {
5174 ret = wl_display_flush(display->display);
5176 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5177 ep.data.ptr = &display->display_task;
5178 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5179 display->display_fd, &ep);
5180 } else if (ret == -1 && errno != EAGAIN) {
5181 display_exit(display);
5188 log_handler(const char *format, va_list args)
5190 vfprintf(stderr, format, args);
5194 display_create(int *argc, char *argv[])
5198 wl_log_set_handler_client(log_handler);
5200 d = zalloc(sizeof *d);
5204 d->display = wl_display_connect(NULL);
5205 if (d->display == NULL) {
5206 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5211 d->xkb_context = xkb_context_new(0);
5212 if (d->xkb_context == NULL) {
5213 fprintf(stderr, "Failed to create XKB context\n");
5218 d->epoll_fd = os_epoll_create_cloexec();
5219 d->display_fd = wl_display_get_fd(d->display);
5220 d->display_task.run = handle_display_data;
5221 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5224 wl_list_init(&d->deferred_list);
5225 wl_list_init(&d->input_list);
5226 wl_list_init(&d->output_list);
5227 wl_list_init(&d->global_list);
5230 d->workspace_count = 1;
5232 d->registry = wl_display_get_registry(d->display);
5233 wl_registry_add_listener(d->registry, ®istry_listener, d);
5235 if (wl_display_dispatch(d->display) < 0) {
5236 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5240 #ifdef HAVE_CAIRO_EGL
5241 if (init_egl(d) < 0)
5242 fprintf(stderr, "EGL does not seem to work, "
5243 "falling back to software rendering and wl_shm.\n");
5248 d->theme = theme_create();
5250 wl_list_init(&d->window_list);
5252 init_dummy_surface(d);
5258 display_destroy_outputs(struct display *display)
5261 struct output *output;
5263 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5264 output_destroy(output);
5268 display_destroy_inputs(struct display *display)
5271 struct input *input;
5273 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5274 input_destroy(input);
5278 display_destroy(struct display *display)
5280 if (!wl_list_empty(&display->window_list))
5281 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5282 wl_list_length(&display->window_list));
5284 if (!wl_list_empty(&display->deferred_list))
5285 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5287 cairo_surface_destroy(display->dummy_surface);
5288 free(display->dummy_surface_data);
5290 display_destroy_outputs(display);
5291 display_destroy_inputs(display);
5293 xkb_context_unref(display->xkb_context);
5295 theme_destroy(display->theme);
5296 destroy_cursors(display);
5298 #ifdef HAVE_CAIRO_EGL
5299 if (display->argb_device)
5303 if (display->subcompositor)
5304 wl_subcompositor_destroy(display->subcompositor);
5306 if (display->xdg_shell)
5307 xdg_shell_destroy(display->xdg_shell);
5310 wl_shm_destroy(display->shm);
5312 if (display->data_device_manager)
5313 wl_data_device_manager_destroy(display->data_device_manager);
5315 wl_compositor_destroy(display->compositor);
5316 wl_registry_destroy(display->registry);
5318 close(display->epoll_fd);
5320 if (!(display->display_fd_events & EPOLLERR) &&
5321 !(display->display_fd_events & EPOLLHUP))
5322 wl_display_flush(display->display);
5324 wl_display_disconnect(display->display);
5329 display_set_user_data(struct display *display, void *data)
5331 display->user_data = data;
5335 display_get_user_data(struct display *display)
5337 return display->user_data;
5341 display_get_display(struct display *display)
5343 return display->display;
5347 display_has_subcompositor(struct display *display)
5349 if (display->subcompositor)
5352 wl_display_roundtrip(display->display);
5354 return display->subcompositor != NULL;
5358 display_get_cairo_device(struct display *display)
5360 return display->argb_device;
5364 display_get_output(struct display *display)
5366 return container_of(display->output_list.next, struct output, link);
5369 struct wl_compositor *
5370 display_get_compositor(struct display *display)
5372 return display->compositor;
5376 display_get_serial(struct display *display)
5378 return display->serial;
5382 display_get_egl_display(struct display *d)
5387 struct wl_data_source *
5388 display_create_data_source(struct display *display)
5390 return wl_data_device_manager_create_data_source(display->data_device_manager);
5394 display_get_argb_egl_config(struct display *d)
5396 return d->argb_config;
5400 display_acquire_window_surface(struct display *display,
5401 struct window *window,
5404 struct surface *surface = window->main_surface;
5406 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5409 widget_get_cairo_surface(window->main_surface->widget);
5410 return surface->toysurface->acquire(surface->toysurface, ctx);
5414 display_release_window_surface(struct display *display,
5415 struct window *window)
5417 struct surface *surface = window->main_surface;
5419 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5422 surface->toysurface->release(surface->toysurface);
5426 display_defer(struct display *display, struct task *task)
5428 wl_list_insert(&display->deferred_list, &task->link);
5432 display_watch_fd(struct display *display,
5433 int fd, uint32_t events, struct task *task)
5435 struct epoll_event ep;
5439 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5443 display_unwatch_fd(struct display *display, int fd)
5445 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5449 display_run(struct display *display)
5452 struct epoll_event ep[16];
5455 display->running = 1;
5457 while (!wl_list_empty(&display->deferred_list)) {
5458 task = container_of(display->deferred_list.prev,
5460 wl_list_remove(&task->link);
5464 wl_display_dispatch_pending(display->display);
5466 if (!display->running)
5469 ret = wl_display_flush(display->display);
5470 if (ret < 0 && errno == EAGAIN) {
5472 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5473 ep[0].data.ptr = &display->display_task;
5475 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5476 display->display_fd, &ep[0]);
5477 } else if (ret < 0) {
5481 count = epoll_wait(display->epoll_fd,
5482 ep, ARRAY_LENGTH(ep), -1);
5483 for (i = 0; i < count; i++) {
5484 task = ep[i].data.ptr;
5485 task->run(task, ep[i].events);
5491 display_exit(struct display *display)
5493 display->running = 0;
5497 keysym_modifiers_add(struct wl_array *modifiers_map,
5500 size_t len = strlen(name) + 1;
5503 p = wl_array_add(modifiers_map, len);
5508 strncpy(p, name, len);
5511 static xkb_mod_index_t
5512 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5515 xkb_mod_index_t index = 0;
5516 char *p = modifiers_map->data;
5518 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5519 if (strcmp(p, name) == 0)
5526 return XKB_MOD_INVALID;
5530 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5533 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5535 if (index == XKB_MOD_INVALID)
5536 return XKB_MOD_INVALID;
5542 fail_on_null(void *p)
5545 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5555 return fail_on_null(malloc(s));
5561 return fail_on_null(zalloc(s));
5565 xstrdup(const char *s)
5567 return fail_on_null(strdup(s));
5571 xrealloc(char *p, size_t s)
5573 return fail_on_null(realloc(p, s));