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;
242 window_state_changed_handler_t state_changed_handler;
244 struct surface *main_surface;
245 struct xdg_surface *xdg_surface;
246 struct xdg_popup *xdg_popup;
248 struct window *parent;
250 struct window_frame *frame;
252 /* struct surface::link, contains also main_surface */
253 struct wl_list subsurface_list;
260 struct window *window;
261 struct surface *surface;
262 struct tooltip *tooltip;
263 struct wl_list child_list;
265 struct rectangle allocation;
266 widget_resize_handler_t resize_handler;
267 widget_redraw_handler_t redraw_handler;
268 widget_enter_handler_t enter_handler;
269 widget_leave_handler_t leave_handler;
270 widget_motion_handler_t motion_handler;
271 widget_button_handler_t button_handler;
272 widget_touch_down_handler_t touch_down_handler;
273 widget_touch_up_handler_t touch_up_handler;
274 widget_touch_motion_handler_t touch_motion_handler;
275 widget_touch_frame_handler_t touch_frame_handler;
276 widget_touch_cancel_handler_t touch_cancel_handler;
277 widget_axis_handler_t axis_handler;
282 /* If this is set to false then no cairo surface will be
283 * created before redrawing the surface. This is useful if the
284 * redraw handler is going to do completely custom rendering
285 * such as using EGL directly */
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
337 int32_t repeat_rate_sec;
338 int32_t repeat_rate_nsec;
339 int32_t repeat_delay_sec;
340 int32_t repeat_delay_nsec;
342 struct task repeat_task;
346 uint32_t repeat_time;
350 struct display *display;
351 struct wl_output *output;
352 uint32_t server_output_id;
353 struct rectangle allocation;
360 display_output_handler_t destroy_handler;
364 struct window_frame {
365 struct widget *widget;
366 struct widget *child;
372 struct window *window;
373 struct widget *widget;
376 const char **entries;
385 struct widget *parent;
386 struct widget *widget;
388 struct task tooltip_task;
394 struct wl_shm_pool *pool;
401 CURSOR_DEFAULT = 100,
405 static const cairo_user_data_key_t shm_surface_data_key;
412 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
413 __attribute__ ((format (printf, 4, 5)));
416 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
421 gettimeofday(&tv, NULL);
422 fprintf(stderr, "%8ld.%03ld ",
423 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
426 fprintf(stderr, "%s@%d ",
427 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
429 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
430 fprintf(stderr, "%s ", func);
433 vfprintf(stderr, fmt, ap);
437 #define DBG(fmt, ...) \
438 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
440 #define DBG_OBJ(obj, fmt, ...) \
441 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
445 #define DBG(...) do {} while (0)
446 #define DBG_OBJ(...) do {} while (0)
451 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
455 switch (buffer_transform) {
456 case WL_OUTPUT_TRANSFORM_90:
457 case WL_OUTPUT_TRANSFORM_270:
458 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
459 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
468 *width *= buffer_scale;
469 *height *= buffer_scale;
473 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
490 *width /= buffer_scale;
491 *height /= buffer_scale;
494 #ifdef HAVE_CAIRO_EGL
496 struct egl_window_surface {
497 struct toysurface base;
498 cairo_surface_t *cairo_surface;
499 struct display *display;
500 struct wl_surface *surface;
501 struct wl_egl_window *egl_window;
502 EGLSurface egl_surface;
505 static struct egl_window_surface *
506 to_egl_window_surface(struct toysurface *base)
508 return container_of(base, struct egl_window_surface, base);
511 static cairo_surface_t *
512 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
513 int32_t width, int32_t height, uint32_t flags,
514 enum wl_output_transform buffer_transform, int32_t buffer_scale)
516 struct egl_window_surface *surface = to_egl_window_surface(base);
518 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
520 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
521 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
523 return cairo_surface_reference(surface->cairo_surface);
527 egl_window_surface_swap(struct toysurface *base,
528 enum wl_output_transform buffer_transform, int32_t buffer_scale,
529 struct rectangle *server_allocation)
531 struct egl_window_surface *surface = to_egl_window_surface(base);
533 cairo_gl_surface_swapbuffers(surface->cairo_surface);
534 wl_egl_window_get_attached_size(surface->egl_window,
535 &server_allocation->width,
536 &server_allocation->height);
538 buffer_to_surface_size (buffer_transform, buffer_scale,
539 &server_allocation->width,
540 &server_allocation->height);
544 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
546 struct egl_window_surface *surface = to_egl_window_surface(base);
547 cairo_device_t *device;
549 device = cairo_surface_get_device(surface->cairo_surface);
554 if (device == surface->display->argb_device)
555 ctx = surface->display->argb_ctx;
560 cairo_device_flush(device);
561 cairo_device_acquire(device);
562 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
563 surface->egl_surface, ctx))
564 fprintf(stderr, "failed to make surface current\n");
570 egl_window_surface_release(struct toysurface *base)
572 struct egl_window_surface *surface = to_egl_window_surface(base);
573 cairo_device_t *device;
575 device = cairo_surface_get_device(surface->cairo_surface);
579 if (!eglMakeCurrent(surface->display->dpy,
580 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
581 fprintf(stderr, "failed to make context current\n");
583 cairo_device_release(device);
587 egl_window_surface_destroy(struct toysurface *base)
589 struct egl_window_surface *surface = to_egl_window_surface(base);
590 struct display *d = surface->display;
592 cairo_surface_destroy(surface->cairo_surface);
593 eglDestroySurface(d->dpy, surface->egl_surface);
594 wl_egl_window_destroy(surface->egl_window);
595 surface->surface = NULL;
600 static struct toysurface *
601 egl_window_surface_create(struct display *display,
602 struct wl_surface *wl_surface,
604 struct rectangle *rectangle)
606 struct egl_window_surface *surface;
608 if (display->dpy == EGL_NO_DISPLAY)
611 surface = calloc(1, sizeof *surface);
615 surface->base.prepare = egl_window_surface_prepare;
616 surface->base.swap = egl_window_surface_swap;
617 surface->base.acquire = egl_window_surface_acquire;
618 surface->base.release = egl_window_surface_release;
619 surface->base.destroy = egl_window_surface_destroy;
621 surface->display = display;
622 surface->surface = wl_surface;
624 surface->egl_window = wl_egl_window_create(surface->surface,
628 surface->egl_surface = eglCreateWindowSurface(display->dpy,
629 display->argb_config,
633 surface->cairo_surface =
634 cairo_gl_surface_create_for_egl(display->argb_device,
635 surface->egl_surface,
639 return &surface->base;
644 static struct toysurface *
645 egl_window_surface_create(struct display *display,
646 struct wl_surface *wl_surface,
648 struct rectangle *rectangle)
655 struct shm_surface_data {
656 struct wl_buffer *buffer;
657 struct shm_pool *pool;
661 display_get_buffer_for_surface(struct display *display,
662 cairo_surface_t *surface)
664 struct shm_surface_data *data;
666 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
672 shm_pool_destroy(struct shm_pool *pool);
675 shm_surface_data_destroy(void *p)
677 struct shm_surface_data *data = p;
679 wl_buffer_destroy(data->buffer);
681 shm_pool_destroy(data->pool);
686 static struct wl_shm_pool *
687 make_shm_pool(struct display *display, int size, void **data)
689 struct wl_shm_pool *pool;
692 fd = os_create_anonymous_file(size);
694 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
699 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
700 if (*data == MAP_FAILED) {
701 fprintf(stderr, "mmap failed: %m\n");
706 pool = wl_shm_create_pool(display->shm, fd, size);
713 static struct shm_pool *
714 shm_pool_create(struct display *display, size_t size)
716 struct shm_pool *pool = malloc(sizeof *pool);
721 pool->pool = make_shm_pool(display, size, &pool->data);
734 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
736 if (pool->used + size > pool->size)
739 *offset = pool->used;
742 return (char *) pool->data + *offset;
745 /* destroy the pool. this does not unmap the memory though */
747 shm_pool_destroy(struct shm_pool *pool)
749 munmap(pool->data, pool->size);
750 wl_shm_pool_destroy(pool->pool);
754 /* Start allocating from the beginning of the pool again */
756 shm_pool_reset(struct shm_pool *pool)
762 data_length_for_shm_surface(struct rectangle *rect)
766 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
768 return stride * rect->height;
771 static cairo_surface_t *
772 display_create_shm_surface_from_pool(struct display *display,
773 struct rectangle *rectangle,
774 uint32_t flags, struct shm_pool *pool)
776 struct shm_surface_data *data;
778 cairo_surface_t *surface;
779 cairo_format_t cairo_format;
780 int stride, length, offset;
783 data = malloc(sizeof *data);
787 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
788 cairo_format = CAIRO_FORMAT_RGB16_565;
790 cairo_format = CAIRO_FORMAT_ARGB32;
792 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
793 length = stride * rectangle->height;
795 map = shm_pool_allocate(pool, length, &offset);
802 surface = cairo_image_surface_create_for_data (map,
808 cairo_surface_set_user_data(surface, &shm_surface_data_key,
809 data, shm_surface_data_destroy);
811 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
812 format = WL_SHM_FORMAT_RGB565;
814 if (flags & SURFACE_OPAQUE)
815 format = WL_SHM_FORMAT_XRGB8888;
817 format = WL_SHM_FORMAT_ARGB8888;
820 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
828 static cairo_surface_t *
829 display_create_shm_surface(struct display *display,
830 struct rectangle *rectangle, uint32_t flags,
831 struct shm_pool *alternate_pool,
832 struct shm_surface_data **data_ret)
834 struct shm_surface_data *data;
835 struct shm_pool *pool;
836 cairo_surface_t *surface;
838 if (alternate_pool) {
839 shm_pool_reset(alternate_pool);
840 surface = display_create_shm_surface_from_pool(display,
845 data = cairo_surface_get_user_data(surface,
846 &shm_surface_data_key);
851 pool = shm_pool_create(display,
852 data_length_for_shm_surface(rectangle));
857 display_create_shm_surface_from_pool(display, rectangle,
861 shm_pool_destroy(pool);
865 /* make sure we destroy the pool when the surface is destroyed */
866 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
877 check_size(struct rectangle *rect)
879 if (rect->width && rect->height)
882 fprintf(stderr, "tried to create surface of "
883 "width: %d, height: %d\n", rect->width, rect->height);
888 display_create_surface(struct display *display,
889 struct wl_surface *surface,
890 struct rectangle *rectangle,
893 if (check_size(rectangle) < 0)
896 assert(flags & SURFACE_SHM);
897 return display_create_shm_surface(display, rectangle, flags,
901 struct shm_surface_leaf {
902 cairo_surface_t *cairo_surface;
903 /* 'data' is automatically destroyed, when 'cairo_surface' is */
904 struct shm_surface_data *data;
906 struct shm_pool *resize_pool;
911 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
913 if (leaf->cairo_surface)
914 cairo_surface_destroy(leaf->cairo_surface);
915 /* leaf->data already destroyed via cairo private */
917 if (leaf->resize_pool)
918 shm_pool_destroy(leaf->resize_pool);
920 memset(leaf, 0, sizeof *leaf);
926 struct toysurface base;
927 struct display *display;
928 struct wl_surface *surface;
932 struct shm_surface_leaf leaf[MAX_LEAVES];
933 struct shm_surface_leaf *current;
936 static struct shm_surface *
937 to_shm_surface(struct toysurface *base)
939 return container_of(base, struct shm_surface, base);
943 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
946 struct shm_surface_leaf *leaf;
947 char bufs[MAX_LEAVES + 1];
950 for (i = 0; i < MAX_LEAVES; i++) {
951 leaf = &surface->leaf[i];
955 else if (leaf->cairo_surface)
961 bufs[MAX_LEAVES] = '\0';
962 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
967 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
969 struct shm_surface *surface = data;
970 struct shm_surface_leaf *leaf;
974 shm_surface_buffer_state_debug(surface, "buffer_release before");
976 for (i = 0; i < MAX_LEAVES; i++) {
977 leaf = &surface->leaf[i];
978 if (leaf->data && leaf->data->buffer == buffer) {
983 assert(i < MAX_LEAVES && "unknown buffer released");
985 /* Leave one free leaf with storage, release others */
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
990 if (!leaf->cairo_surface || leaf->busy)
996 shm_surface_leaf_release(leaf);
999 shm_surface_buffer_state_debug(surface, "buffer_release after");
1002 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1003 shm_surface_buffer_release
1006 static cairo_surface_t *
1007 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1008 int32_t width, int32_t height, uint32_t flags,
1009 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1011 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1012 struct shm_surface *surface = to_shm_surface(base);
1013 struct rectangle rect = { 0};
1014 struct shm_surface_leaf *leaf = NULL;
1020 /* pick a free buffer, preferrably one that already has storage */
1021 for (i = 0; i < MAX_LEAVES; i++) {
1022 if (surface->leaf[i].busy)
1025 if (!leaf || surface->leaf[i].cairo_surface)
1026 leaf = &surface->leaf[i];
1028 DBG_OBJ(surface->surface, "pick leaf %d\n",
1029 (int)(leaf - &surface->leaf[0]));
1032 fprintf(stderr, "%s: all buffers are held by the server.\n",
1038 if (!resize_hint && leaf->resize_pool) {
1039 cairo_surface_destroy(leaf->cairo_surface);
1040 leaf->cairo_surface = NULL;
1041 shm_pool_destroy(leaf->resize_pool);
1042 leaf->resize_pool = NULL;
1045 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1047 if (leaf->cairo_surface &&
1048 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1049 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1052 if (leaf->cairo_surface)
1053 cairo_surface_destroy(leaf->cairo_surface);
1055 #ifdef USE_RESIZE_POOL
1056 if (resize_hint && !leaf->resize_pool) {
1057 /* Create a big pool to allocate from, while continuously
1058 * resizing. Mmapping a new pool in the server
1059 * is relatively expensive, so reusing a pool performs
1060 * better, but may temporarily reserve unneeded memory.
1062 /* We should probably base this number on the output size. */
1063 leaf->resize_pool = shm_pool_create(surface->display,
1069 rect.height = height;
1071 leaf->cairo_surface =
1072 display_create_shm_surface(surface->display, &rect,
1076 if (!leaf->cairo_surface)
1079 wl_buffer_add_listener(leaf->data->buffer,
1080 &shm_surface_buffer_listener, surface);
1083 surface->current = leaf;
1085 return cairo_surface_reference(leaf->cairo_surface);
1089 shm_surface_swap(struct toysurface *base,
1090 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1091 struct rectangle *server_allocation)
1093 struct shm_surface *surface = to_shm_surface(base);
1094 struct shm_surface_leaf *leaf = surface->current;
1096 server_allocation->width =
1097 cairo_image_surface_get_width(leaf->cairo_surface);
1098 server_allocation->height =
1099 cairo_image_surface_get_height(leaf->cairo_surface);
1101 buffer_to_surface_size (buffer_transform, buffer_scale,
1102 &server_allocation->width,
1103 &server_allocation->height);
1105 wl_surface_attach(surface->surface, leaf->data->buffer,
1106 surface->dx, surface->dy);
1107 wl_surface_damage(surface->surface, 0, 0,
1108 server_allocation->width, server_allocation->height);
1109 wl_surface_commit(surface->surface);
1111 DBG_OBJ(surface->surface, "leaf %d busy\n",
1112 (int)(leaf - &surface->leaf[0]));
1115 surface->current = NULL;
1119 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1125 shm_surface_release(struct toysurface *base)
1130 shm_surface_destroy(struct toysurface *base)
1132 struct shm_surface *surface = to_shm_surface(base);
1135 for (i = 0; i < MAX_LEAVES; i++)
1136 shm_surface_leaf_release(&surface->leaf[i]);
1141 static struct toysurface *
1142 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1143 uint32_t flags, struct rectangle *rectangle)
1145 struct shm_surface *surface;
1146 DBG_OBJ(wl_surface, "\n");
1148 surface = xmalloc(sizeof *surface);
1149 memset(surface, 0, sizeof *surface);
1154 surface->base.prepare = shm_surface_prepare;
1155 surface->base.swap = shm_surface_swap;
1156 surface->base.acquire = shm_surface_acquire;
1157 surface->base.release = shm_surface_release;
1158 surface->base.destroy = shm_surface_destroy;
1160 surface->display = display;
1161 surface->surface = wl_surface;
1162 surface->flags = flags;
1164 return &surface->base;
1168 * The following correspondences between file names and cursors was copied
1169 * from: https://bugs.kde.org/attachment.cgi?id=67313
1172 static const char *bottom_left_corners[] = {
1173 "bottom_left_corner",
1178 static const char *bottom_right_corners[] = {
1179 "bottom_right_corner",
1184 static const char *bottom_sides[] = {
1190 static const char *grabbings[] = {
1193 "208530c400c041818281048008011002"
1196 static const char *left_ptrs[] = {
1203 static const char *left_sides[] = {
1209 static const char *right_sides[] = {
1215 static const char *top_left_corners[] = {
1221 static const char *top_right_corners[] = {
1227 static const char *top_sides[] = {
1233 static const char *xterms[] = {
1239 static const char *hand1s[] = {
1243 "e29285e634086352946a0e7090d73106"
1246 static const char *watches[] = {
1249 "0426c94ea35c87780ff01dc239897213"
1252 struct cursor_alternatives {
1257 static const struct cursor_alternatives cursors[] = {
1258 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1259 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1260 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1261 {grabbings, ARRAY_LENGTH(grabbings)},
1262 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1263 {left_sides, ARRAY_LENGTH(left_sides)},
1264 {right_sides, ARRAY_LENGTH(right_sides)},
1265 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1266 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1267 {top_sides, ARRAY_LENGTH(top_sides)},
1268 {xterms, ARRAY_LENGTH(xterms)},
1269 {hand1s, ARRAY_LENGTH(hand1s)},
1270 {watches, ARRAY_LENGTH(watches)},
1274 create_cursors(struct display *display)
1276 struct weston_config *config;
1277 struct weston_config_section *s;
1281 struct wl_cursor *cursor;
1283 config = weston_config_parse("weston.ini");
1284 s = weston_config_get_section(config, "shell", NULL, NULL);
1285 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1286 weston_config_section_get_int(s, "cursor-size", &size, 32);
1287 weston_config_destroy(config);
1289 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1290 if (!display->cursor_theme) {
1291 fprintf(stderr, "could not load theme '%s'\n", theme);
1296 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1298 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1300 for (j = 0; !cursor && j < cursors[i].count; ++j)
1301 cursor = wl_cursor_theme_get_cursor(
1302 display->cursor_theme, cursors[i].names[j]);
1305 fprintf(stderr, "could not load cursor '%s'\n",
1306 cursors[i].names[0]);
1308 display->cursors[i] = cursor;
1313 destroy_cursors(struct display *display)
1315 wl_cursor_theme_destroy(display->cursor_theme);
1316 free(display->cursors);
1319 struct wl_cursor_image *
1320 display_get_pointer_image(struct display *display, int pointer)
1322 struct wl_cursor *cursor = display->cursors[pointer];
1324 return cursor ? cursor->images[0] : NULL;
1328 surface_flush(struct surface *surface)
1330 if (!surface->cairo_surface)
1333 if (surface->opaque_region) {
1334 wl_surface_set_opaque_region(surface->surface,
1335 surface->opaque_region);
1336 wl_region_destroy(surface->opaque_region);
1337 surface->opaque_region = NULL;
1340 if (surface->input_region) {
1341 wl_surface_set_input_region(surface->surface,
1342 surface->input_region);
1343 wl_region_destroy(surface->input_region);
1344 surface->input_region = NULL;
1347 surface->toysurface->swap(surface->toysurface,
1348 surface->buffer_transform, surface->buffer_scale,
1349 &surface->server_allocation);
1351 cairo_surface_destroy(surface->cairo_surface);
1352 surface->cairo_surface = NULL;
1356 window_has_focus(struct window *window)
1358 return window->focused;
1362 window_close(struct window *window)
1364 if (window->close_handler)
1365 window->close_handler(window->user_data);
1367 display_exit(window->display);
1371 window_get_display(struct window *window)
1373 return window->display;
1377 surface_create_surface(struct surface *surface, uint32_t flags)
1379 struct display *display = surface->window->display;
1380 struct rectangle allocation = surface->allocation;
1382 if (!surface->toysurface && display->dpy &&
1383 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1384 surface->toysurface =
1385 egl_window_surface_create(display,
1391 if (!surface->toysurface)
1392 surface->toysurface = shm_surface_create(display,
1394 flags, &allocation);
1396 surface->cairo_surface = surface->toysurface->prepare(
1397 surface->toysurface, 0, 0,
1398 allocation.width, allocation.height, flags,
1399 surface->buffer_transform, surface->buffer_scale);
1403 window_create_main_surface(struct window *window)
1405 struct surface *surface = window->main_surface;
1408 if (window->resizing)
1409 flags |= SURFACE_HINT_RESIZE;
1411 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1412 flags |= SURFACE_HINT_RGB565;
1414 surface_create_surface(surface, flags);
1418 window_get_buffer_transform(struct window *window)
1420 return window->main_surface->buffer_transform;
1424 window_set_buffer_transform(struct window *window,
1425 enum wl_output_transform transform)
1427 window->main_surface->buffer_transform = transform;
1428 wl_surface_set_buffer_transform(window->main_surface->surface,
1433 window_set_buffer_scale(struct window *window,
1436 window->main_surface->buffer_scale = scale;
1437 wl_surface_set_buffer_scale(window->main_surface->surface,
1442 window_get_buffer_scale(struct window *window)
1444 return window->main_surface->buffer_scale;
1448 window_get_output_scale(struct window *window)
1450 struct window_output *window_output;
1451 struct window_output *window_output_tmp;
1454 wl_list_for_each_safe(window_output, window_output_tmp,
1455 &window->window_output_list, link) {
1456 if (window_output->output->scale > scale)
1457 scale = window_output->output->scale;
1463 static void window_frame_destroy(struct window_frame *frame);
1466 surface_destroy(struct surface *surface)
1468 if (surface->frame_cb)
1469 wl_callback_destroy(surface->frame_cb);
1471 if (surface->input_region)
1472 wl_region_destroy(surface->input_region);
1474 if (surface->opaque_region)
1475 wl_region_destroy(surface->opaque_region);
1477 if (surface->subsurface)
1478 wl_subsurface_destroy(surface->subsurface);
1480 wl_surface_destroy(surface->surface);
1482 if (surface->toysurface)
1483 surface->toysurface->destroy(surface->toysurface);
1485 wl_list_remove(&surface->link);
1490 window_destroy(struct window *window)
1492 struct display *display = window->display;
1493 struct input *input;
1494 struct window_output *window_output;
1495 struct window_output *window_output_tmp;
1497 wl_list_remove(&window->redraw_task.link);
1499 wl_list_for_each(input, &display->input_list, link) {
1500 if (input->touch_focus == window)
1501 input->touch_focus = NULL;
1502 if (input->pointer_focus == window)
1503 input->pointer_focus = NULL;
1504 if (input->keyboard_focus == window)
1505 input->keyboard_focus = NULL;
1506 if (input->focus_widget &&
1507 input->focus_widget->window == window)
1508 input->focus_widget = NULL;
1511 wl_list_for_each_safe(window_output, window_output_tmp,
1512 &window->window_output_list, link) {
1513 free (window_output);
1517 window_frame_destroy(window->frame);
1519 if (window->xdg_surface)
1520 xdg_surface_destroy(window->xdg_surface);
1521 if (window->xdg_popup)
1522 xdg_popup_destroy(window->xdg_popup);
1524 surface_destroy(window->main_surface);
1526 wl_list_remove(&window->link);
1528 free(window->title);
1532 static struct widget *
1533 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1535 struct widget *child, *target;
1537 wl_list_for_each(child, &widget->child_list, link) {
1538 target = widget_find_widget(child, x, y);
1543 if (widget->allocation.x <= x &&
1544 x < widget->allocation.x + widget->allocation.width &&
1545 widget->allocation.y <= y &&
1546 y < widget->allocation.y + widget->allocation.height) {
1553 static struct widget *
1554 window_find_widget(struct window *window, int32_t x, int32_t y)
1556 struct surface *surface;
1557 struct widget *widget;
1559 wl_list_for_each(surface, &window->subsurface_list, link) {
1560 widget = widget_find_widget(surface->widget, x, y);
1568 static struct widget *
1569 widget_create(struct window *window, struct surface *surface, void *data)
1571 struct widget *widget;
1573 widget = xzalloc(sizeof *widget);
1574 widget->window = window;
1575 widget->surface = surface;
1576 widget->user_data = data;
1577 widget->allocation = surface->allocation;
1578 wl_list_init(&widget->child_list);
1580 widget->tooltip = NULL;
1581 widget->tooltip_count = 0;
1582 widget->default_cursor = CURSOR_LEFT_PTR;
1583 widget->use_cairo = 1;
1589 window_add_widget(struct window *window, void *data)
1591 struct widget *widget;
1593 widget = widget_create(window, window->main_surface, data);
1594 wl_list_init(&widget->link);
1595 window->main_surface->widget = widget;
1601 widget_add_widget(struct widget *parent, void *data)
1603 struct widget *widget;
1605 widget = widget_create(parent->window, parent->surface, data);
1606 wl_list_insert(parent->child_list.prev, &widget->link);
1612 widget_destroy(struct widget *widget)
1614 struct display *display = widget->window->display;
1615 struct surface *surface = widget->surface;
1616 struct input *input;
1618 /* Destroy the sub-surface along with the root widget */
1619 if (surface->widget == widget && surface->subsurface)
1620 surface_destroy(widget->surface);
1622 if (widget->tooltip)
1623 widget_destroy_tooltip(widget);
1625 wl_list_for_each(input, &display->input_list, link) {
1626 if (input->focus_widget == widget)
1627 input->focus_widget = NULL;
1630 wl_list_remove(&widget->link);
1635 widget_set_default_cursor(struct widget *widget, int cursor)
1637 widget->default_cursor = cursor;
1641 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1643 *allocation = widget->allocation;
1647 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1649 widget->allocation.width = width;
1650 widget->allocation.height = height;
1654 widget_set_allocation(struct widget *widget,
1655 int32_t x, int32_t y, int32_t width, int32_t height)
1657 widget->allocation.x = x;
1658 widget->allocation.y = y;
1659 widget_set_size(widget, width, height);
1663 widget_set_transparent(struct widget *widget, int transparent)
1665 widget->opaque = !transparent;
1669 widget_get_user_data(struct widget *widget)
1671 return widget->user_data;
1674 static cairo_surface_t *
1675 widget_get_cairo_surface(struct widget *widget)
1677 struct surface *surface = widget->surface;
1678 struct window *window = widget->window;
1680 assert(widget->use_cairo);
1682 if (!surface->cairo_surface) {
1683 if (surface == window->main_surface)
1684 window_create_main_surface(window);
1686 surface_create_surface(surface, 0);
1689 return surface->cairo_surface;
1693 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1695 struct surface *surface = widget->surface;
1698 enum wl_output_transform transform;
1699 int surface_width, surface_height;
1700 int translate_x, translate_y;
1703 surface_width = surface->allocation.width;
1704 surface_height = surface->allocation.height;
1706 transform = surface->buffer_transform;
1707 scale = surface->buffer_scale;
1709 switch (transform) {
1710 case WL_OUTPUT_TRANSFORM_FLIPPED:
1711 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1713 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1714 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1717 cairo_matrix_init_identity(&m);
1721 switch (transform) {
1722 case WL_OUTPUT_TRANSFORM_NORMAL:
1728 case WL_OUTPUT_TRANSFORM_FLIPPED:
1730 translate_x = surface_width;
1733 case WL_OUTPUT_TRANSFORM_90:
1735 translate_x = surface_height;
1738 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1740 translate_x = surface_height;
1741 translate_y = surface_width;
1743 case WL_OUTPUT_TRANSFORM_180:
1745 translate_x = surface_width;
1746 translate_y = surface_height;
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1751 translate_y = surface_height;
1753 case WL_OUTPUT_TRANSFORM_270:
1754 angle = M_PI + M_PI_2;
1756 translate_y = surface_width;
1758 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1759 angle = M_PI + M_PI_2;
1765 cairo_scale(cr, scale, scale);
1766 cairo_translate(cr, translate_x, translate_y);
1767 cairo_rotate(cr, angle);
1768 cairo_transform(cr, &m);
1772 widget_cairo_create(struct widget *widget)
1774 struct surface *surface = widget->surface;
1775 cairo_surface_t *cairo_surface;
1778 cairo_surface = widget_get_cairo_surface(widget);
1779 cr = cairo_create(cairo_surface);
1781 widget_cairo_update_transform(widget, cr);
1783 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1789 widget_get_wl_surface(struct widget *widget)
1791 return widget->surface->surface;
1794 struct wl_subsurface *
1795 widget_get_wl_subsurface(struct widget *widget)
1797 return widget->surface->subsurface;
1801 widget_get_last_time(struct widget *widget)
1803 return widget->surface->last_time;
1807 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1809 struct wl_compositor *comp = widget->window->display->compositor;
1810 struct surface *surface = widget->surface;
1812 if (!surface->input_region)
1813 surface->input_region = wl_compositor_create_region(comp);
1816 wl_region_add(surface->input_region,
1817 rect->x, rect->y, rect->width, rect->height);
1822 widget_set_resize_handler(struct widget *widget,
1823 widget_resize_handler_t handler)
1825 widget->resize_handler = handler;
1829 widget_set_redraw_handler(struct widget *widget,
1830 widget_redraw_handler_t handler)
1832 widget->redraw_handler = handler;
1836 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1838 widget->enter_handler = handler;
1842 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1844 widget->leave_handler = handler;
1848 widget_set_motion_handler(struct widget *widget,
1849 widget_motion_handler_t handler)
1851 widget->motion_handler = handler;
1855 widget_set_button_handler(struct widget *widget,
1856 widget_button_handler_t handler)
1858 widget->button_handler = handler;
1862 widget_set_touch_up_handler(struct widget *widget,
1863 widget_touch_up_handler_t handler)
1865 widget->touch_up_handler = handler;
1869 widget_set_touch_down_handler(struct widget *widget,
1870 widget_touch_down_handler_t handler)
1872 widget->touch_down_handler = handler;
1876 widget_set_touch_motion_handler(struct widget *widget,
1877 widget_touch_motion_handler_t handler)
1879 widget->touch_motion_handler = handler;
1883 widget_set_touch_frame_handler(struct widget *widget,
1884 widget_touch_frame_handler_t handler)
1886 widget->touch_frame_handler = handler;
1890 widget_set_touch_cancel_handler(struct widget *widget,
1891 widget_touch_cancel_handler_t handler)
1893 widget->touch_cancel_handler = handler;
1897 widget_set_axis_handler(struct widget *widget,
1898 widget_axis_handler_t handler)
1900 widget->axis_handler = handler;
1904 window_schedule_redraw_task(struct window *window);
1907 widget_schedule_redraw(struct widget *widget)
1909 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1910 widget->surface->redraw_needed = 1;
1911 window_schedule_redraw_task(widget->window);
1915 widget_set_use_cairo(struct widget *widget,
1918 widget->use_cairo = use_cairo;
1922 window_get_surface(struct window *window)
1924 cairo_surface_t *cairo_surface;
1926 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1928 return cairo_surface_reference(cairo_surface);
1932 window_get_wl_surface(struct window *window)
1934 return window->main_surface->surface;
1938 tooltip_redraw_handler(struct widget *widget, void *data)
1941 const int32_t r = 3;
1942 struct tooltip *tooltip = data;
1943 int32_t width, height;
1945 cr = widget_cairo_create(widget);
1946 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1947 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1948 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1951 width = widget->allocation.width;
1952 height = widget->allocation.height;
1953 rounded_rect(cr, 0, 0, width, height, r);
1955 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1956 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1959 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1960 cairo_move_to(cr, 10, 16);
1961 cairo_show_text(cr, tooltip->entry);
1965 static cairo_text_extents_t
1966 get_text_extents(struct display *display, struct tooltip *tooltip)
1969 cairo_text_extents_t extents;
1971 /* Use the dummy_surface because tooltip's surface was not
1972 * created yet, and parent does not have a valid surface
1973 * outside repaint, either.
1975 cr = cairo_create(display->dummy_surface);
1976 cairo_text_extents(cr, tooltip->entry, &extents);
1983 window_create_tooltip(struct tooltip *tooltip)
1985 struct widget *parent = tooltip->parent;
1986 struct display *display = parent->window->display;
1987 const int offset_y = 27;
1988 const int margin = 3;
1989 cairo_text_extents_t extents;
1991 if (tooltip->widget)
1994 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
1996 extents = get_text_extents(display, tooltip);
1997 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1998 widget_set_allocation(tooltip->widget,
1999 tooltip->x, tooltip->y + offset_y,
2000 extents.width + 20, 20 + margin * 2);
2006 widget_destroy_tooltip(struct widget *parent)
2008 struct tooltip *tooltip = parent->tooltip;
2010 parent->tooltip_count = 0;
2014 if (tooltip->widget) {
2015 widget_destroy(tooltip->widget);
2016 tooltip->widget = NULL;
2019 close(tooltip->tooltip_fd);
2020 free(tooltip->entry);
2022 parent->tooltip = NULL;
2026 tooltip_func(struct task *task, uint32_t events)
2028 struct tooltip *tooltip =
2029 container_of(task, struct tooltip, tooltip_task);
2032 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2034 window_create_tooltip(tooltip);
2037 #define TOOLTIP_TIMEOUT 500
2039 tooltip_timer_reset(struct tooltip *tooltip)
2041 struct itimerspec its;
2043 its.it_interval.tv_sec = 0;
2044 its.it_interval.tv_nsec = 0;
2045 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2046 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2047 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2048 fprintf(stderr, "could not set timerfd\n: %m");
2056 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2058 struct tooltip *tooltip = parent->tooltip;
2060 parent->tooltip_count++;
2064 tooltip_timer_reset(tooltip);
2068 /* the handler might be triggered too fast via input device motion, so
2069 * we need this check here to make sure tooltip is fully initialized */
2070 if (parent->tooltip_count > 1)
2073 tooltip = malloc(sizeof *tooltip);
2077 parent->tooltip = tooltip;
2078 tooltip->parent = parent;
2079 tooltip->widget = NULL;
2082 tooltip->entry = strdup(entry);
2083 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2084 if (tooltip->tooltip_fd < 0) {
2085 fprintf(stderr, "could not create timerfd\n: %m");
2089 tooltip->tooltip_task.run = tooltip_func;
2090 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2091 EPOLLIN, &tooltip->tooltip_task);
2092 tooltip_timer_reset(tooltip);
2098 workspace_manager_state(void *data,
2099 struct workspace_manager *workspace_manager,
2103 struct display *display = data;
2105 display->workspace = current;
2106 display->workspace_count = count;
2109 static const struct workspace_manager_listener workspace_manager_listener = {
2110 workspace_manager_state
2114 frame_resize_handler(struct widget *widget,
2115 int32_t width, int32_t height, void *data)
2117 struct window_frame *frame = data;
2118 struct widget *child = frame->child;
2119 struct rectangle interior;
2120 struct rectangle input;
2121 struct rectangle opaque;
2123 if (widget->window->fullscreen) {
2126 interior.width = width;
2127 interior.height = height;
2129 frame_resize(frame->frame, width, height);
2130 frame_interior(frame->frame, &interior.x, &interior.y,
2131 &interior.width, &interior.height);
2134 widget_set_allocation(child, interior.x, interior.y,
2135 interior.width, interior.height);
2137 if (child->resize_handler) {
2138 child->resize_handler(child, interior.width, interior.height,
2141 if (widget->window->fullscreen) {
2142 width = child->allocation.width;
2143 height = child->allocation.height;
2145 frame_resize_inside(frame->frame,
2146 child->allocation.width,
2147 child->allocation.height);
2148 width = frame_width(frame->frame);
2149 height = frame_height(frame->frame);
2153 widget_set_allocation(widget, 0, 0, width, height);
2155 widget->surface->input_region =
2156 wl_compositor_create_region(widget->window->display->compositor);
2157 if (!widget->window->fullscreen) {
2158 frame_input_rect(frame->frame, &input.x, &input.y,
2159 &input.width, &input.height);
2160 wl_region_add(widget->surface->input_region,
2161 input.x, input.y, input.width, input.height);
2163 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2166 widget_set_allocation(widget, 0, 0, width, height);
2168 if (child->opaque) {
2169 if (!widget->window->fullscreen) {
2170 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2171 &opaque.width, &opaque.height);
2173 wl_region_add(widget->surface->opaque_region,
2175 opaque.width, opaque.height);
2177 wl_region_add(widget->surface->opaque_region,
2178 0, 0, width, height);
2183 widget_schedule_redraw(widget);
2187 frame_redraw_handler(struct widget *widget, void *data)
2190 struct window_frame *frame = data;
2191 struct window *window = widget->window;
2193 if (window->fullscreen)
2196 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 window_show_frame_menu(struct window *window,
2238 struct input *input, uint32_t time)
2242 if (window->xdg_surface) {
2243 input_get_position(input, &x, &y);
2244 xdg_surface_show_window_menu(window->xdg_surface,
2245 input_get_seat(input),
2246 window->display->serial,
2252 frame_enter_handler(struct widget *widget,
2253 struct input *input, float x, float y, void *data)
2255 struct window_frame *frame = data;
2256 enum theme_location location;
2258 location = frame_pointer_enter(frame->frame, input, x, y);
2259 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2260 widget_schedule_redraw(frame->widget);
2262 return frame_get_pointer_image_for_location(data, location);
2266 frame_motion_handler(struct widget *widget,
2267 struct input *input, uint32_t time,
2268 float x, float y, void *data)
2270 struct window_frame *frame = data;
2271 enum theme_location location;
2273 location = frame_pointer_motion(frame->frame, input, x, y);
2274 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2275 widget_schedule_redraw(frame->widget);
2277 return frame_get_pointer_image_for_location(data, location);
2281 frame_leave_handler(struct widget *widget,
2282 struct input *input, void *data)
2284 struct window_frame *frame = data;
2286 frame_pointer_leave(frame->frame, input);
2287 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2288 widget_schedule_redraw(frame->widget);
2292 frame_handle_status(struct window_frame *frame, struct input *input,
2293 uint32_t time, enum theme_location location)
2295 struct window *window = frame->widget->window;
2298 status = frame_status(frame->frame);
2299 if (status & FRAME_STATUS_REPAINT)
2300 widget_schedule_redraw(frame->widget);
2302 if (status & FRAME_STATUS_MINIMIZE) {
2303 window_set_minimized(window);
2304 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2307 if (status & FRAME_STATUS_MENU) {
2308 window_show_frame_menu(window, input, time);
2309 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2312 if (status & FRAME_STATUS_MAXIMIZE) {
2313 window_set_maximized(window, !window->maximized);
2314 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2317 if (status & FRAME_STATUS_CLOSE) {
2318 window_close(window);
2322 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2323 input_ungrab(input);
2324 xdg_surface_move(window->xdg_surface,
2325 input_get_seat(input),
2326 window->display->serial);
2328 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2331 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2332 input_ungrab(input);
2334 xdg_surface_resize(window->xdg_surface,
2335 input_get_seat(input),
2336 window->display->serial,
2339 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2344 frame_button_handler(struct widget *widget,
2345 struct input *input, uint32_t time,
2346 uint32_t button, enum wl_pointer_button_state state,
2350 struct window_frame *frame = data;
2351 enum theme_location location;
2353 location = frame_pointer_button(frame->frame, input, button, state);
2354 frame_handle_status(frame, input, time, location);
2358 frame_touch_down_handler(struct widget *widget, struct input *input,
2359 uint32_t serial, uint32_t time, int32_t id,
2360 float x, float y, void *data)
2362 struct window_frame *frame = data;
2364 frame_touch_down(frame->frame, input, id, x, y);
2365 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2369 frame_touch_up_handler(struct widget *widget,
2370 struct input *input, uint32_t serial, uint32_t time,
2371 int32_t id, void *data)
2373 struct window_frame *frame = data;
2375 frame_touch_up(frame->frame, input, id);
2376 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2380 window_frame_create(struct window *window, void *data)
2382 struct window_frame *frame;
2385 if (window->custom) {
2386 buttons = FRAME_BUTTON_NONE;
2388 buttons = FRAME_BUTTON_ALL;
2391 frame = xzalloc(sizeof *frame);
2392 frame->frame = frame_create(window->display->theme, 0, 0,
2393 buttons, window->title);
2395 frame->widget = window_add_widget(window, frame);
2396 frame->child = widget_add_widget(frame->widget, data);
2398 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2399 widget_set_resize_handler(frame->widget, frame_resize_handler);
2400 widget_set_enter_handler(frame->widget, frame_enter_handler);
2401 widget_set_leave_handler(frame->widget, frame_leave_handler);
2402 widget_set_motion_handler(frame->widget, frame_motion_handler);
2403 widget_set_button_handler(frame->widget, frame_button_handler);
2404 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2405 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2407 window->frame = frame;
2409 return frame->child;
2413 window_frame_set_child_size(struct widget *widget, int child_width,
2416 struct display *display = widget->window->display;
2417 struct theme *t = display->theme;
2418 int decoration_width, decoration_height;
2420 int margin = widget->window->maximized ? 0 : t->margin;
2422 if (!widget->window->fullscreen) {
2423 decoration_width = (t->width + margin) * 2;
2424 decoration_height = t->width +
2425 t->titlebar_height + margin * 2;
2427 width = child_width + decoration_width;
2428 height = child_height + decoration_height;
2430 width = child_width;
2431 height = child_height;
2434 window_schedule_resize(widget->window, width, height);
2438 window_frame_destroy(struct window_frame *frame)
2440 frame_destroy(frame->frame);
2442 /* frame->child must be destroyed by the application */
2443 widget_destroy(frame->widget);
2448 input_set_focus_widget(struct input *input, struct widget *focus,
2451 struct widget *old, *widget;
2454 if (focus == input->focus_widget)
2457 old = input->focus_widget;
2461 widget = input->grab;
2462 if (widget->leave_handler)
2463 widget->leave_handler(old, input, widget->user_data);
2464 input->focus_widget = NULL;
2470 widget = input->grab;
2471 input->focus_widget = focus;
2472 if (widget->enter_handler)
2473 cursor = widget->enter_handler(focus, input, x, y,
2476 cursor = widget->default_cursor;
2478 input_set_pointer_image(input, cursor);
2483 touch_grab(struct input *input, int32_t touch_id)
2485 input->touch_grab = 1;
2486 input->touch_grab_id = touch_id;
2490 touch_ungrab(struct input *input)
2492 struct touch_point *tp, *tmp;
2494 input->touch_grab = 0;
2496 wl_list_for_each_safe(tp, tmp,
2497 &input->touch_point_list, link) {
2498 if (tp->id != input->touch_grab_id)
2500 wl_list_remove(&tp->link);
2508 input_grab(struct input *input, struct widget *widget, uint32_t button)
2510 input->grab = widget;
2511 input->grab_button = button;
2513 input_set_focus_widget(input, widget, input->sx, input->sy);
2517 input_ungrab(struct input *input)
2519 struct widget *widget;
2522 if (input->pointer_focus) {
2523 widget = window_find_widget(input->pointer_focus,
2524 input->sx, input->sy);
2525 input_set_focus_widget(input, widget, input->sx, input->sy);
2530 input_remove_pointer_focus(struct input *input)
2532 struct window *window = input->pointer_focus;
2537 input_set_focus_widget(input, NULL, 0, 0);
2539 input->pointer_focus = NULL;
2540 input->current_cursor = CURSOR_UNSET;
2544 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2545 uint32_t serial, struct wl_surface *surface,
2546 wl_fixed_t sx_w, wl_fixed_t sy_w)
2548 struct input *input = data;
2549 struct window *window;
2550 struct widget *widget;
2551 float sx = wl_fixed_to_double(sx_w);
2552 float sy = wl_fixed_to_double(sy_w);
2555 /* enter event for a window we've just destroyed */
2559 window = wl_surface_get_user_data(surface);
2560 if (surface != window->main_surface->surface) {
2561 DBG("Ignoring input event from subsurface %p\n", surface);
2565 input->display->serial = serial;
2566 input->pointer_enter_serial = serial;
2567 input->pointer_focus = window;
2572 widget = window_find_widget(window, sx, sy);
2573 input_set_focus_widget(input, widget, sx, sy);
2577 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2578 uint32_t serial, struct wl_surface *surface)
2580 struct input *input = data;
2582 input->display->serial = serial;
2583 input_remove_pointer_focus(input);
2587 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2588 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2590 struct input *input = data;
2591 struct window *window = input->pointer_focus;
2592 struct widget *widget;
2594 float sx = wl_fixed_to_double(sx_w);
2595 float sy = wl_fixed_to_double(sy_w);
2603 /* when making the window smaller - e.g. after a unmaximise we might
2604 * still have a pending motion event that the compositor has picked
2605 * based on the old surface dimensions
2607 if (sx > window->main_surface->allocation.width ||
2608 sy > window->main_surface->allocation.height)
2611 if (!(input->grab && input->grab_button)) {
2612 widget = window_find_widget(window, sx, sy);
2613 input_set_focus_widget(input, widget, sx, sy);
2617 widget = input->grab;
2619 widget = input->focus_widget;
2621 if (widget->motion_handler)
2622 cursor = widget->motion_handler(input->focus_widget,
2623 input, time, sx, sy,
2626 cursor = widget->default_cursor;
2628 cursor = CURSOR_LEFT_PTR;
2630 input_set_pointer_image(input, cursor);
2634 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2635 uint32_t time, uint32_t button, uint32_t state_w)
2637 struct input *input = data;
2638 struct widget *widget;
2639 enum wl_pointer_button_state state = state_w;
2641 input->display->serial = serial;
2642 if (input->focus_widget && input->grab == NULL &&
2643 state == WL_POINTER_BUTTON_STATE_PRESSED)
2644 input_grab(input, input->focus_widget, button);
2646 widget = input->grab;
2647 if (widget && widget->button_handler)
2648 (*widget->button_handler)(widget,
2651 input->grab->user_data);
2653 if (input->grab && input->grab_button == button &&
2654 state == WL_POINTER_BUTTON_STATE_RELEASED)
2655 input_ungrab(input);
2659 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2660 uint32_t time, uint32_t axis, wl_fixed_t value)
2662 struct input *input = data;
2663 struct widget *widget;
2665 widget = input->focus_widget;
2667 widget = input->grab;
2668 if (widget && widget->axis_handler)
2669 (*widget->axis_handler)(widget,
2675 static const struct wl_pointer_listener pointer_listener = {
2676 pointer_handle_enter,
2677 pointer_handle_leave,
2678 pointer_handle_motion,
2679 pointer_handle_button,
2680 pointer_handle_axis,
2684 input_remove_keyboard_focus(struct input *input)
2686 struct window *window = input->keyboard_focus;
2687 struct itimerspec its;
2689 its.it_interval.tv_sec = 0;
2690 its.it_interval.tv_nsec = 0;
2691 its.it_value.tv_sec = 0;
2692 its.it_value.tv_nsec = 0;
2693 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2698 if (window->keyboard_focus_handler)
2699 (*window->keyboard_focus_handler)(window, NULL,
2702 input->keyboard_focus = NULL;
2706 keyboard_repeat_func(struct task *task, uint32_t events)
2708 struct input *input =
2709 container_of(task, struct input, repeat_task);
2710 struct window *window = input->keyboard_focus;
2713 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2714 /* If we change the timer between the fd becoming
2715 * readable and getting here, there'll be nothing to
2716 * read and we get EAGAIN. */
2719 if (window && window->key_handler) {
2720 (*window->key_handler)(window, input, input->repeat_time,
2721 input->repeat_key, input->repeat_sym,
2722 WL_KEYBOARD_KEY_STATE_PRESSED,
2728 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2729 uint32_t format, int fd, uint32_t size)
2731 struct input *input = data;
2732 struct xkb_keymap *keymap;
2733 struct xkb_state *state;
2741 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2746 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2747 if (map_str == MAP_FAILED) {
2752 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2754 XKB_KEYMAP_FORMAT_TEXT_V1,
2756 munmap(map_str, size);
2760 fprintf(stderr, "failed to compile keymap\n");
2764 state = xkb_state_new(keymap);
2766 fprintf(stderr, "failed to create XKB state\n");
2767 xkb_keymap_unref(keymap);
2771 xkb_keymap_unref(input->xkb.keymap);
2772 xkb_state_unref(input->xkb.state);
2773 input->xkb.keymap = keymap;
2774 input->xkb.state = state;
2776 input->xkb.control_mask =
2777 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
2778 input->xkb.alt_mask =
2779 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
2780 input->xkb.shift_mask =
2781 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
2785 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2786 uint32_t serial, struct wl_surface *surface,
2787 struct wl_array *keys)
2789 struct input *input = data;
2790 struct window *window;
2792 input->display->serial = serial;
2793 input->keyboard_focus = wl_surface_get_user_data(surface);
2795 window = input->keyboard_focus;
2796 if (window->keyboard_focus_handler)
2797 (*window->keyboard_focus_handler)(window,
2798 input, window->user_data);
2802 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2803 uint32_t serial, struct wl_surface *surface)
2805 struct input *input = data;
2807 input->display->serial = serial;
2808 input_remove_keyboard_focus(input);
2812 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2813 uint32_t serial, uint32_t time, uint32_t key,
2816 struct input *input = data;
2817 struct window *window = input->keyboard_focus;
2818 uint32_t code, num_syms;
2819 enum wl_keyboard_key_state state = state_w;
2820 const xkb_keysym_t *syms;
2822 struct itimerspec its;
2824 input->display->serial = serial;
2826 if (!window || !input->xkb.state)
2829 /* We only use input grabs for pointer events for now, so just
2830 * ignore key presses if a grab is active. We expand the key
2831 * event delivery mechanism to route events to widgets to
2832 * properly handle key grabs. In the meantime, this prevents
2833 * key event devlivery while a grab is active. */
2834 if (input->grab && input->grab_button == 0)
2837 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
2839 sym = XKB_KEY_NoSymbol;
2844 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2845 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2846 window_set_maximized(window, !window->maximized);
2847 } else if (sym == XKB_KEY_F11 &&
2848 window->fullscreen_handler &&
2849 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2850 window->fullscreen_handler(window, window->user_data);
2851 } else if (sym == XKB_KEY_F4 &&
2852 input->modifiers == MOD_ALT_MASK &&
2853 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2854 window_close(window);
2855 } else if (window->key_handler) {
2856 (*window->key_handler)(window, input, time, key,
2857 sym, state, window->user_data);
2860 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2861 key == input->repeat_key) {
2862 its.it_interval.tv_sec = 0;
2863 its.it_interval.tv_nsec = 0;
2864 its.it_value.tv_sec = 0;
2865 its.it_value.tv_nsec = 0;
2866 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2867 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
2868 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
2869 input->repeat_sym = sym;
2870 input->repeat_key = key;
2871 input->repeat_time = time;
2872 its.it_interval.tv_sec = input->repeat_rate_sec;
2873 its.it_interval.tv_nsec = input->repeat_rate_nsec;
2874 its.it_value.tv_sec = input->repeat_delay_sec;
2875 its.it_value.tv_nsec = input->repeat_delay_nsec;
2876 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2881 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2882 uint32_t serial, uint32_t mods_depressed,
2883 uint32_t mods_latched, uint32_t mods_locked,
2886 struct input *input = data;
2887 xkb_mod_mask_t mask;
2889 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2890 if (!input->xkb.keymap)
2893 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2894 mods_locked, 0, 0, group);
2895 mask = xkb_state_serialize_mods(input->xkb.state,
2896 XKB_STATE_MODS_DEPRESSED |
2897 XKB_STATE_MODS_LATCHED);
2898 input->modifiers = 0;
2899 if (mask & input->xkb.control_mask)
2900 input->modifiers |= MOD_CONTROL_MASK;
2901 if (mask & input->xkb.alt_mask)
2902 input->modifiers |= MOD_ALT_MASK;
2903 if (mask & input->xkb.shift_mask)
2904 input->modifiers |= MOD_SHIFT_MASK;
2908 set_repeat_info(struct input *input, int32_t rate, int32_t delay)
2910 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
2911 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
2913 /* a rate of zero disables any repeating, regardless of the delay's
2919 input->repeat_rate_sec = 1;
2921 input->repeat_rate_nsec = 1000000000 / rate;
2923 input->repeat_delay_sec = delay / 1000;
2924 delay -= (input->repeat_delay_sec * 1000);
2925 input->repeat_delay_nsec = delay * 1000 * 1000;
2929 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
2930 int32_t rate, int32_t delay)
2932 struct input *input = data;
2934 set_repeat_info(input, rate, delay);
2937 static const struct wl_keyboard_listener keyboard_listener = {
2938 keyboard_handle_keymap,
2939 keyboard_handle_enter,
2940 keyboard_handle_leave,
2941 keyboard_handle_key,
2942 keyboard_handle_modifiers,
2943 keyboard_handle_repeat_info
2948 touch_handle_down(void *data, struct wl_touch *wl_touch,
2949 uint32_t serial, uint32_t time, struct wl_surface *surface,
2950 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2952 struct input *input = data;
2953 struct widget *widget;
2954 float sx = wl_fixed_to_double(x_w);
2955 float sy = wl_fixed_to_double(y_w);
2957 input->display->serial = serial;
2958 input->touch_focus = wl_surface_get_user_data(surface);
2959 if (!input->touch_focus) {
2960 DBG("Failed to find to touch focus for surface %p\n", surface);
2964 if (surface != input->touch_focus->main_surface->surface) {
2965 DBG("Ignoring input event from subsurface %p\n", surface);
2966 input->touch_focus = NULL;
2971 widget = input->grab;
2973 widget = window_find_widget(input->touch_focus,
2974 wl_fixed_to_double(x_w),
2975 wl_fixed_to_double(y_w));
2977 struct touch_point *tp = xmalloc(sizeof *tp);
2980 tp->widget = widget;
2983 wl_list_insert(&input->touch_point_list, &tp->link);
2985 if (widget->touch_down_handler)
2986 (*widget->touch_down_handler)(widget, input,
2995 touch_handle_up(void *data, struct wl_touch *wl_touch,
2996 uint32_t serial, uint32_t time, int32_t id)
2998 struct input *input = data;
2999 struct touch_point *tp, *tmp;
3001 if (!input->touch_focus) {
3002 DBG("No touch focus found for touch up event!\n");
3006 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3010 if (tp->widget->touch_up_handler)
3011 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3013 tp->widget->user_data);
3015 wl_list_remove(&tp->link);
3023 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3024 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3026 struct input *input = data;
3027 struct touch_point *tp;
3028 float sx = wl_fixed_to_double(x_w);
3029 float sy = wl_fixed_to_double(y_w);
3031 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3033 if (!input->touch_focus) {
3034 DBG("No touch focus found for touch motion event!\n");
3038 wl_list_for_each(tp, &input->touch_point_list, link) {
3044 if (tp->widget->touch_motion_handler)
3045 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3047 tp->widget->user_data);
3053 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3055 struct input *input = data;
3056 struct touch_point *tp, *tmp;
3058 DBG("touch_handle_frame\n");
3060 if (!input->touch_focus) {
3061 DBG("No touch focus found for touch frame event!\n");
3065 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3066 if (tp->widget->touch_frame_handler)
3067 (*tp->widget->touch_frame_handler)(tp->widget, input,
3068 tp->widget->user_data);
3073 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3075 struct input *input = data;
3076 struct touch_point *tp, *tmp;
3078 DBG("touch_handle_cancel\n");
3080 if (!input->touch_focus) {
3081 DBG("No touch focus found for touch cancel event!\n");
3085 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_cancel_handler)
3087 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3088 tp->widget->user_data);
3090 wl_list_remove(&tp->link);
3095 static const struct wl_touch_listener touch_listener = {
3098 touch_handle_motion,
3100 touch_handle_cancel,
3104 seat_handle_capabilities(void *data, struct wl_seat *seat,
3105 enum wl_seat_capability caps)
3107 struct input *input = data;
3109 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3110 input->pointer = wl_seat_get_pointer(seat);
3111 wl_pointer_set_user_data(input->pointer, input);
3112 wl_pointer_add_listener(input->pointer, &pointer_listener,
3114 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3115 wl_pointer_destroy(input->pointer);
3116 input->pointer = NULL;
3119 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3120 input->keyboard = wl_seat_get_keyboard(seat);
3121 wl_keyboard_set_user_data(input->keyboard, input);
3122 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3124 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3125 wl_keyboard_destroy(input->keyboard);
3126 input->keyboard = NULL;
3129 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3130 input->touch = wl_seat_get_touch(seat);
3131 wl_touch_set_user_data(input->touch, input);
3132 wl_touch_add_listener(input->touch, &touch_listener, input);
3133 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3134 wl_touch_destroy(input->touch);
3135 input->touch = NULL;
3140 seat_handle_name(void *data, struct wl_seat *seat,
3146 static const struct wl_seat_listener seat_listener = {
3147 seat_handle_capabilities,
3152 input_get_position(struct input *input, int32_t *x, int32_t *y)
3159 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3161 struct touch_point *tp;
3163 wl_list_for_each(tp, &input->touch_point_list, link) {
3176 input_get_display(struct input *input)
3178 return input->display;
3182 input_get_seat(struct input *input)
3188 input_get_modifiers(struct input *input)
3190 return input->modifiers;
3194 input_get_focus_widget(struct input *input)
3196 return input->focus_widget;
3200 struct wl_data_offer *offer;
3201 struct input *input;
3202 struct wl_array types;
3205 struct task io_task;
3213 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3215 struct data_offer *offer = data;
3218 p = wl_array_add(&offer->types, sizeof *p);
3222 static const struct wl_data_offer_listener data_offer_listener = {
3227 data_offer_destroy(struct data_offer *offer)
3232 if (offer->refcount == 0) {
3233 wl_data_offer_destroy(offer->offer);
3234 for (p = offer->types.data; *p; p++)
3236 wl_array_release(&offer->types);
3242 data_device_data_offer(void *data,
3243 struct wl_data_device *data_device,
3244 struct wl_data_offer *_offer)
3246 struct data_offer *offer;
3248 offer = xmalloc(sizeof *offer);
3250 wl_array_init(&offer->types);
3251 offer->refcount = 1;
3252 offer->input = data;
3253 offer->offer = _offer;
3254 wl_data_offer_add_listener(offer->offer,
3255 &data_offer_listener, offer);
3259 data_device_enter(void *data, struct wl_data_device *data_device,
3260 uint32_t serial, struct wl_surface *surface,
3261 wl_fixed_t x_w, wl_fixed_t y_w,
3262 struct wl_data_offer *offer)
3264 struct input *input = data;
3265 struct window *window;
3267 float x = wl_fixed_to_double(x_w);
3268 float y = wl_fixed_to_double(y_w);
3271 window = wl_surface_get_user_data(surface);
3272 input->drag_enter_serial = serial;
3273 input->drag_focus = window,
3277 if (!input->touch_grab)
3278 input->pointer_enter_serial = serial;
3281 input->drag_offer = wl_data_offer_get_user_data(offer);
3283 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3286 types_data = input->drag_offer->types.data;
3288 input->drag_offer = NULL;
3292 if (window->data_handler)
3293 window->data_handler(window, input, x, y, types_data,
3298 data_device_leave(void *data, struct wl_data_device *data_device)
3300 struct input *input = data;
3302 if (input->drag_offer) {
3303 data_offer_destroy(input->drag_offer);
3304 input->drag_offer = NULL;
3309 data_device_motion(void *data, struct wl_data_device *data_device,
3310 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3312 struct input *input = data;
3313 struct window *window = input->drag_focus;
3314 float x = wl_fixed_to_double(x_w);
3315 float y = wl_fixed_to_double(y_w);
3321 if (input->drag_offer)
3322 types_data = input->drag_offer->types.data;
3326 if (window->data_handler)
3327 window->data_handler(window, input, x, y, types_data,
3332 data_device_drop(void *data, struct wl_data_device *data_device)
3334 struct input *input = data;
3335 struct window *window = input->drag_focus;
3341 if (window->drop_handler)
3342 window->drop_handler(window, input,
3343 x, y, window->user_data);
3345 if (input->touch_grab)
3346 touch_ungrab(input);
3350 data_device_selection(void *data,
3351 struct wl_data_device *wl_data_device,
3352 struct wl_data_offer *offer)
3354 struct input *input = data;
3357 if (input->selection_offer)
3358 data_offer_destroy(input->selection_offer);
3361 input->selection_offer = wl_data_offer_get_user_data(offer);
3362 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3365 input->selection_offer = NULL;
3369 static const struct wl_data_device_listener data_device_listener = {
3370 data_device_data_offer,
3375 data_device_selection
3379 input_set_pointer_image_index(struct input *input, int index)
3381 struct wl_buffer *buffer;
3382 struct wl_cursor *cursor;
3383 struct wl_cursor_image *image;
3385 if (!input->pointer)
3388 cursor = input->display->cursors[input->current_cursor];
3392 if (index >= (int) cursor->image_count) {
3393 fprintf(stderr, "cursor index out of range\n");
3397 image = cursor->images[index];
3398 buffer = wl_cursor_image_get_buffer(image);
3402 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3403 wl_surface_damage(input->pointer_surface, 0, 0,
3404 image->width, image->height);
3405 wl_surface_commit(input->pointer_surface);
3406 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3407 input->pointer_surface,
3408 image->hotspot_x, image->hotspot_y);
3411 static const struct wl_callback_listener pointer_surface_listener;
3414 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3417 struct input *input = data;
3418 struct wl_cursor *cursor;
3422 assert(callback == input->cursor_frame_cb);
3423 wl_callback_destroy(callback);
3424 input->cursor_frame_cb = NULL;
3427 if (!input->pointer)
3430 if (input->current_cursor == CURSOR_BLANK) {
3431 wl_pointer_set_cursor(input->pointer,
3432 input->pointer_enter_serial,
3437 if (input->current_cursor == CURSOR_UNSET)
3439 cursor = input->display->cursors[input->current_cursor];
3443 /* FIXME We don't have the current time on the first call so we set
3444 * the animation start to the time of the first frame callback. */
3446 input->cursor_anim_start = 0;
3447 else if (input->cursor_anim_start == 0)
3448 input->cursor_anim_start = time;
3450 if (time == 0 || input->cursor_anim_start == 0)
3453 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3455 if (cursor->image_count > 1) {
3456 input->cursor_frame_cb =
3457 wl_surface_frame(input->pointer_surface);
3458 wl_callback_add_listener(input->cursor_frame_cb,
3459 &pointer_surface_listener, input);
3462 input_set_pointer_image_index(input, i);
3465 static const struct wl_callback_listener pointer_surface_listener = {
3466 pointer_surface_frame_callback
3470 input_set_pointer_image(struct input *input, int pointer)
3474 if (!input->pointer)
3477 if (input->pointer_enter_serial > input->cursor_serial)
3480 if (!force && pointer == input->current_cursor)
3483 input->current_cursor = pointer;
3484 input->cursor_serial = input->pointer_enter_serial;
3485 if (!input->cursor_frame_cb)
3486 pointer_surface_frame_callback(input, NULL, 0);
3488 /* The current frame callback may be stuck if, for instance,
3489 * the set cursor request was processed by the server after
3490 * this client lost the focus. In this case the cursor surface
3491 * might not be mapped and the frame callback wouldn't ever
3492 * complete. Send a set_cursor and attach to try to map the
3493 * cursor surface again so that the callback will finish */
3494 input_set_pointer_image_index(input, 0);
3498 struct wl_data_device *
3499 input_get_data_device(struct input *input)
3501 return input->data_device;
3505 input_set_selection(struct input *input,
3506 struct wl_data_source *source, uint32_t time)
3508 if (input->data_device)
3509 wl_data_device_set_selection(input->data_device, source, time);
3513 input_accept(struct input *input, const char *type)
3515 wl_data_offer_accept(input->drag_offer->offer,
3516 input->drag_enter_serial, type);
3520 offer_io_func(struct task *task, uint32_t events)
3522 struct data_offer *offer =
3523 container_of(task, struct data_offer, io_task);
3527 len = read(offer->fd, buffer, sizeof buffer);
3528 offer->func(buffer, len,
3529 offer->x, offer->y, offer->user_data);
3533 data_offer_destroy(offer);
3538 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3539 data_func_t func, void *user_data)
3543 if (pipe2(p, O_CLOEXEC) == -1)
3546 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3549 offer->io_task.run = offer_io_func;
3553 offer->user_data = user_data;
3555 display_watch_fd(offer->input->display,
3556 offer->fd, EPOLLIN, &offer->io_task);
3560 input_receive_drag_data(struct input *input, const char *mime_type,
3561 data_func_t func, void *data)
3563 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3564 input->drag_offer->x = input->drag_x;
3565 input->drag_offer->y = input->drag_y;
3569 input_receive_drag_data_to_fd(struct input *input,
3570 const char *mime_type, int fd)
3572 if (input->drag_offer)
3573 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3579 input_receive_selection_data(struct input *input, const char *mime_type,
3580 data_func_t func, void *data)
3584 if (input->selection_offer == NULL)
3587 for (p = input->selection_offer->types.data; *p; p++)
3588 if (strcmp(mime_type, *p) == 0)
3594 data_offer_receive_data(input->selection_offer,
3595 mime_type, func, data);
3600 input_receive_selection_data_to_fd(struct input *input,
3601 const char *mime_type, int fd)
3603 if (input->selection_offer)
3604 wl_data_offer_receive(input->selection_offer->offer,
3611 window_move(struct window *window, struct input *input, uint32_t serial)
3613 if (!window->xdg_surface)
3616 xdg_surface_move(window->xdg_surface, input->seat, serial);
3620 surface_set_synchronized(struct surface *surface)
3622 if (!surface->subsurface)
3625 if (surface->synchronized)
3628 wl_subsurface_set_sync(surface->subsurface);
3629 surface->synchronized = 1;
3633 surface_set_synchronized_default(struct surface *surface)
3635 if (!surface->subsurface)
3638 if (surface->synchronized == surface->synchronized_default)
3641 if (surface->synchronized_default)
3642 wl_subsurface_set_sync(surface->subsurface);
3644 wl_subsurface_set_desync(surface->subsurface);
3646 surface->synchronized = surface->synchronized_default;
3650 surface_resize(struct surface *surface)
3652 struct widget *widget = surface->widget;
3653 struct wl_compositor *compositor = widget->window->display->compositor;
3655 if (surface->input_region) {
3656 wl_region_destroy(surface->input_region);
3657 surface->input_region = NULL;
3660 if (surface->opaque_region)
3661 wl_region_destroy(surface->opaque_region);
3663 surface->opaque_region = wl_compositor_create_region(compositor);
3665 if (widget->resize_handler)
3666 widget->resize_handler(widget,
3667 widget->allocation.width,
3668 widget->allocation.height,
3671 if (surface->subsurface &&
3672 (surface->allocation.x != widget->allocation.x ||
3673 surface->allocation.y != widget->allocation.y)) {
3674 wl_subsurface_set_position(surface->subsurface,
3675 widget->allocation.x,
3676 widget->allocation.y);
3678 if (surface->allocation.width != widget->allocation.width ||
3679 surface->allocation.height != widget->allocation.height) {
3680 window_schedule_redraw(widget->window);
3682 surface->allocation = widget->allocation;
3685 wl_region_add(surface->opaque_region, 0, 0,
3686 widget->allocation.width,
3687 widget->allocation.height);
3691 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3694 * This hack should be removed, when EGL respects
3695 * eglSwapInterval(0).
3697 * If this window has sub-surfaces, especially a free-running
3698 * EGL-widget, we need to post the parent surface once with
3699 * all the old state to guarantee, that the EGL-widget will
3700 * receive its frame callback soon. Otherwise, a forced call
3701 * to eglSwapBuffers may end up blocking, waiting for a frame
3702 * event that will never come, because we will commit the parent
3703 * surface with all new state only after eglSwapBuffers returns.
3705 * This assumes, that:
3706 * 1. When the EGL widget's resize hook is called, it pauses.
3707 * 2. When the EGL widget's redraw hook is called, it forces a
3708 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3709 * In a single threaded application condition 1 is a no-op.
3711 * XXX: This should actually be after the surface_resize() calls,
3712 * but cannot, because then it would commit the incomplete state
3713 * accumulated from the widget resize hooks.
3715 if (window->subsurface_list.next != &window->main_surface->link ||
3716 window->subsurface_list.prev != &window->main_surface->link)
3717 wl_surface_commit(window->main_surface->surface);
3721 window_do_resize(struct window *window)
3723 struct surface *surface;
3725 widget_set_allocation(window->main_surface->widget,
3726 window->pending_allocation.x,
3727 window->pending_allocation.y,
3728 window->pending_allocation.width,
3729 window->pending_allocation.height);
3731 surface_resize(window->main_surface);
3733 /* The main surface is in the list, too. Main surface's
3734 * resize_handler is responsible for calling widget_set_allocation()
3735 * on all sub-surface root widgets, so they will be resized
3738 wl_list_for_each(surface, &window->subsurface_list, link) {
3739 if (surface == window->main_surface)
3742 surface_set_synchronized(surface);
3743 surface_resize(surface);
3746 if (!window->fullscreen && !window->maximized)
3747 window->saved_allocation = window->pending_allocation;
3751 idle_resize(struct window *window)
3753 window->resize_needed = 0;
3754 window->redraw_needed = 1;
3756 DBG("from %dx%d to %dx%d\n",
3757 window->main_surface->server_allocation.width,
3758 window->main_surface->server_allocation.height,
3759 window->pending_allocation.width,
3760 window->pending_allocation.height);
3762 hack_prevent_EGL_sub_surface_deadlock(window);
3764 window_do_resize(window);
3768 undo_resize(struct window *window)
3770 window->pending_allocation.width =
3771 window->main_surface->server_allocation.width;
3772 window->pending_allocation.height =
3773 window->main_surface->server_allocation.height;
3775 DBG("back to %dx%d\n",
3776 window->main_surface->server_allocation.width,
3777 window->main_surface->server_allocation.height);
3779 window_do_resize(window);
3781 if (window->pending_allocation.width == 0 &&
3782 window->pending_allocation.height == 0) {
3783 fprintf(stderr, "Error: Could not draw a surface, "
3784 "most likely due to insufficient disk space in "
3785 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3791 window_schedule_resize(struct window *window, int width, int height)
3793 /* We should probably get these numbers from the theme. */
3794 const int min_width = 200, min_height = 200;
3796 window->pending_allocation.x = 0;
3797 window->pending_allocation.y = 0;
3798 window->pending_allocation.width = width;
3799 window->pending_allocation.height = height;
3801 if (window->min_allocation.width == 0) {
3802 if (width < min_width && window->frame)
3803 window->min_allocation.width = min_width;
3805 window->min_allocation.width = width;
3806 if (height < min_height && window->frame)
3807 window->min_allocation.height = min_height;
3809 window->min_allocation.height = height;
3812 if (window->pending_allocation.width < window->min_allocation.width)
3813 window->pending_allocation.width = window->min_allocation.width;
3814 if (window->pending_allocation.height < window->min_allocation.height)
3815 window->pending_allocation.height = window->min_allocation.height;
3817 window->resize_needed = 1;
3818 window_schedule_redraw(window);
3822 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3824 window_schedule_resize(widget->window, width, height);
3828 window_get_shadow_margin(struct window *window)
3830 if (window->frame && !window->fullscreen)
3831 return frame_get_shadow_margin(window->frame->frame);
3837 handle_surface_configure(void *data, struct xdg_surface *xdg_surface,
3838 int32_t width, int32_t height,
3839 struct wl_array *states, uint32_t serial)
3841 struct window *window = data;
3844 window->maximized = 0;
3845 window->fullscreen = 0;
3846 window->resizing = 0;
3847 window->focused = 0;
3849 wl_array_for_each(p, states) {
3850 uint32_t state = *p;
3852 case XDG_SURFACE_STATE_MAXIMIZED:
3853 window->maximized = 1;
3855 case XDG_SURFACE_STATE_FULLSCREEN:
3856 window->fullscreen = 1;
3858 case XDG_SURFACE_STATE_RESIZING:
3859 window->resizing = 1;
3861 case XDG_SURFACE_STATE_ACTIVATED:
3862 window->focused = 1;
3870 if (window->frame) {
3871 if (window->maximized) {
3872 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3874 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
3877 if (window->focused) {
3878 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3880 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
3884 if (width > 0 && height > 0) {
3885 /* The width / height params are for window geometry,
3886 * but window_schedule_resize takes allocation. Add
3887 * on the shadow margin to get the difference. */
3888 int margin = window_get_shadow_margin(window);
3890 window_schedule_resize(window,
3892 height + margin * 2);
3894 window_schedule_resize(window,
3895 window->saved_allocation.width,
3896 window->saved_allocation.height);
3899 xdg_surface_ack_configure(window->xdg_surface, serial);
3901 if (window->state_changed_handler)
3902 window->state_changed_handler(window, window->user_data);
3906 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
3908 struct window *window = data;
3909 window_close(window);
3912 static const struct xdg_surface_listener xdg_surface_listener = {
3913 handle_surface_configure,
3914 handle_surface_delete,
3918 window_sync_parent(struct window *window)
3920 struct wl_surface *parent_surface;
3922 if (!window->xdg_surface)
3926 parent_surface = window->parent->main_surface->surface;
3928 parent_surface = NULL;
3930 xdg_surface_set_parent(window->xdg_surface, parent_surface);
3934 window_get_geometry(struct window *window, struct rectangle *geometry)
3936 if (window->frame && !window->fullscreen)
3937 frame_input_rect(window->frame->frame,
3943 window_get_allocation(window, geometry);
3947 window_sync_geometry(struct window *window)
3949 struct rectangle geometry;
3951 if (!window->xdg_surface)
3954 window_get_geometry(window, &geometry);
3956 xdg_surface_set_window_geometry(window->xdg_surface,
3964 window_flush(struct window *window)
3966 struct surface *surface;
3968 if (!window->custom) {
3969 if (window->xdg_surface) {
3970 window_sync_parent(window);
3971 window_sync_geometry(window);
3975 wl_list_for_each(surface, &window->subsurface_list, link) {
3976 if (surface == window->main_surface)
3979 surface_flush(surface);
3982 surface_flush(window->main_surface);
3986 menu_destroy(struct menu *menu)
3988 widget_destroy(menu->widget);
3989 window_destroy(menu->window);
3990 frame_destroy(menu->frame);
3995 window_get_allocation(struct window *window,
3996 struct rectangle *allocation)
3998 *allocation = window->main_surface->allocation;
4002 widget_redraw(struct widget *widget)
4004 struct widget *child;
4006 if (widget->redraw_handler)
4007 widget->redraw_handler(widget, widget->user_data);
4008 wl_list_for_each(child, &widget->child_list, link)
4009 widget_redraw(child);
4013 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4015 struct surface *surface = data;
4017 assert(callback == surface->frame_cb);
4018 DBG_OBJ(callback, "done\n");
4019 wl_callback_destroy(callback);
4020 surface->frame_cb = NULL;
4022 surface->last_time = time;
4024 if (surface->redraw_needed || surface->window->redraw_needed) {
4025 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4026 window_schedule_redraw_task(surface->window);
4030 static const struct wl_callback_listener listener = {
4035 surface_redraw(struct surface *surface)
4037 DBG_OBJ(surface->surface, "begin\n");
4039 if (!surface->window->redraw_needed && !surface->redraw_needed)
4042 /* Whole-window redraw forces a redraw even if the previous has
4043 * not yet hit the screen.
4045 if (surface->frame_cb) {
4046 if (!surface->window->redraw_needed)
4049 DBG_OBJ(surface->frame_cb, "cancelled\n");
4050 wl_callback_destroy(surface->frame_cb);
4053 if (surface->widget->use_cairo &&
4054 !widget_get_cairo_surface(surface->widget)) {
4055 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4059 surface->frame_cb = wl_surface_frame(surface->surface);
4060 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4061 DBG_OBJ(surface->frame_cb, "new\n");
4063 surface->redraw_needed = 0;
4064 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4065 widget_redraw(surface->widget);
4066 DBG_OBJ(surface->surface, "done\n");
4071 idle_redraw(struct task *task, uint32_t events)
4073 struct window *window = container_of(task, struct window, redraw_task);
4074 struct surface *surface;
4078 DBG(" --------- \n");
4080 wl_list_init(&window->redraw_task.link);
4081 window->redraw_task_scheduled = 0;
4083 if (window->resize_needed) {
4084 /* throttle resizing to the main surface display */
4085 if (window->main_surface->frame_cb) {
4086 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4090 idle_resize(window);
4094 if (surface_redraw(window->main_surface) < 0) {
4096 * Only main_surface failure will cause us to undo the resize.
4097 * If sub-surfaces fail, they will just be broken with old
4102 wl_list_for_each(surface, &window->subsurface_list, link) {
4103 if (surface == window->main_surface)
4106 surface_redraw(surface);
4110 window->redraw_needed = 0;
4111 window_flush(window);
4113 wl_list_for_each(surface, &window->subsurface_list, link)
4114 surface_set_synchronized_default(surface);
4116 if (resized && failed) {
4117 /* Restore widget tree to correspond to what is on screen. */
4118 undo_resize(window);
4123 window_schedule_redraw_task(struct window *window)
4125 if (!window->redraw_task_scheduled) {
4126 window->redraw_task.run = idle_redraw;
4127 display_defer(window->display, &window->redraw_task);
4128 window->redraw_task_scheduled = 1;
4133 window_schedule_redraw(struct window *window)
4135 struct surface *surface;
4137 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4139 wl_list_for_each(surface, &window->subsurface_list, link)
4140 surface->redraw_needed = 1;
4142 window_schedule_redraw_task(window);
4146 window_is_fullscreen(struct window *window)
4148 return window->fullscreen;
4152 window_set_fullscreen(struct window *window, int fullscreen)
4154 if (!window->xdg_surface)
4157 if (window->fullscreen == fullscreen)
4161 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4163 xdg_surface_unset_fullscreen(window->xdg_surface);
4167 window_is_maximized(struct window *window)
4169 return window->maximized;
4173 window_set_maximized(struct window *window, int maximized)
4175 if (!window->xdg_surface)
4178 if (window->maximized == maximized)
4182 xdg_surface_set_maximized(window->xdg_surface);
4184 xdg_surface_unset_maximized(window->xdg_surface);
4188 window_is_resizing(struct window *window)
4190 return window->resizing;
4194 window_set_minimized(struct window *window)
4196 if (!window->xdg_surface)
4199 xdg_surface_set_minimized(window->xdg_surface);
4203 window_set_user_data(struct window *window, void *data)
4205 window->user_data = data;
4209 window_get_user_data(struct window *window)
4211 return window->user_data;
4215 window_set_key_handler(struct window *window,
4216 window_key_handler_t handler)
4218 window->key_handler = handler;
4222 window_set_keyboard_focus_handler(struct window *window,
4223 window_keyboard_focus_handler_t handler)
4225 window->keyboard_focus_handler = handler;
4229 window_set_data_handler(struct window *window, window_data_handler_t handler)
4231 window->data_handler = handler;
4235 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4237 window->drop_handler = handler;
4241 window_set_close_handler(struct window *window,
4242 window_close_handler_t handler)
4244 window->close_handler = handler;
4248 window_set_fullscreen_handler(struct window *window,
4249 window_fullscreen_handler_t handler)
4251 window->fullscreen_handler = handler;
4255 window_set_output_handler(struct window *window,
4256 window_output_handler_t handler)
4258 window->output_handler = handler;
4262 window_set_state_changed_handler(struct window *window,
4263 window_state_changed_handler_t handler)
4265 window->state_changed_handler = handler;
4269 window_set_title(struct window *window, const char *title)
4271 free(window->title);
4272 window->title = strdup(title);
4273 if (window->frame) {
4274 frame_set_title(window->frame->frame, title);
4275 widget_schedule_redraw(window->frame->widget);
4277 if (window->xdg_surface)
4278 xdg_surface_set_title(window->xdg_surface, title);
4282 window_get_title(struct window *window)
4284 return window->title;
4288 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4290 struct text_cursor_position *text_cursor_position =
4291 window->display->text_cursor_position;
4293 if (!text_cursor_position)
4296 text_cursor_position_notify(text_cursor_position,
4297 window->main_surface->surface,
4298 wl_fixed_from_int(x),
4299 wl_fixed_from_int(y));
4303 window_damage(struct window *window, int32_t x, int32_t y,
4304 int32_t width, int32_t height)
4306 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4310 surface_enter(void *data,
4311 struct wl_surface *wl_surface, struct wl_output *wl_output)
4313 struct window *window = data;
4314 struct output *output;
4315 struct output *output_found = NULL;
4316 struct window_output *window_output;
4318 wl_list_for_each(output, &window->display->output_list, link) {
4319 if (output->output == wl_output) {
4320 output_found = output;
4328 window_output = xmalloc(sizeof *window_output);
4329 window_output->output = output_found;
4331 wl_list_insert (&window->window_output_list, &window_output->link);
4333 if (window->output_handler)
4334 window->output_handler(window, output_found, 1,
4339 surface_leave(void *data,
4340 struct wl_surface *wl_surface, struct wl_output *output)
4342 struct window *window = data;
4343 struct window_output *window_output;
4344 struct window_output *window_output_found = NULL;
4346 wl_list_for_each(window_output, &window->window_output_list, link) {
4347 if (window_output->output->output == output) {
4348 window_output_found = window_output;
4353 if (window_output_found) {
4354 wl_list_remove(&window_output_found->link);
4356 if (window->output_handler)
4357 window->output_handler(window, window_output->output,
4358 0, window->user_data);
4360 free(window_output_found);
4364 static const struct wl_surface_listener surface_listener = {
4369 static struct surface *
4370 surface_create(struct window *window)
4372 struct display *display = window->display;
4373 struct surface *surface;
4375 surface = xmalloc(sizeof *surface);
4376 memset(surface, 0, sizeof *surface);
4380 surface->window = window;
4381 surface->surface = wl_compositor_create_surface(display->compositor);
4382 surface->buffer_scale = 1;
4383 wl_surface_add_listener(surface->surface, &surface_listener, window);
4385 wl_list_insert(&window->subsurface_list, &surface->link);
4390 static enum window_buffer_type
4391 get_preferred_buffer_type(struct display *display)
4393 #ifdef HAVE_CAIRO_EGL
4394 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4395 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4398 return WINDOW_BUFFER_TYPE_SHM;
4401 static struct window *
4402 window_create_internal(struct display *display, int custom)
4404 struct window *window;
4405 struct surface *surface;
4407 window = xzalloc(sizeof *window);
4408 wl_list_init(&window->subsurface_list);
4409 window->display = display;
4411 surface = surface_create(window);
4412 window->main_surface = surface;
4414 assert(custom || display->xdg_shell);
4416 window->custom = custom;
4417 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4419 surface->buffer_type = get_preferred_buffer_type(display);
4421 wl_surface_set_user_data(surface->surface, window);
4422 wl_list_insert(display->window_list.prev, &window->link);
4423 wl_list_init(&window->redraw_task.link);
4425 wl_list_init (&window->window_output_list);
4431 window_create(struct display *display)
4433 struct window *window;
4435 window = window_create_internal(display, 0);
4437 window->xdg_surface =
4438 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4439 window->main_surface->surface);
4440 fail_on_null(window->xdg_surface);
4442 xdg_surface_set_user_data(window->xdg_surface, window);
4443 xdg_surface_add_listener(window->xdg_surface,
4444 &xdg_surface_listener, window);
4450 window_create_custom(struct display *display)
4452 return window_create_internal(display, 1);
4456 window_set_parent(struct window *window,
4457 struct window *parent_window)
4459 window->parent = parent_window;
4460 window_sync_parent(window);
4464 window_get_parent(struct window *window)
4466 return window->parent;
4470 menu_set_item(struct menu *menu, int sy)
4472 int32_t x, y, width, height;
4475 frame_interior(menu->frame, &x, &y, &width, &height);
4476 next = (sy - y) / 20;
4477 if (menu->current != next) {
4478 menu->current = next;
4479 widget_schedule_redraw(menu->widget);
4484 menu_motion_handler(struct widget *widget,
4485 struct input *input, uint32_t time,
4486 float x, float y, void *data)
4488 struct menu *menu = data;
4490 if (widget == menu->widget)
4491 menu_set_item(data, y);
4493 return CURSOR_LEFT_PTR;
4497 menu_enter_handler(struct widget *widget,
4498 struct input *input, float x, float y, void *data)
4500 struct menu *menu = data;
4502 if (widget == menu->widget)
4503 menu_set_item(data, y);
4505 return CURSOR_LEFT_PTR;
4509 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4511 struct menu *menu = data;
4513 if (widget == menu->widget)
4514 menu_set_item(data, -200);
4518 menu_button_handler(struct widget *widget,
4519 struct input *input, uint32_t time,
4520 uint32_t button, enum wl_pointer_button_state state,
4524 struct menu *menu = data;
4526 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4527 (menu->release_count > 0 || time - menu->time > 500)) {
4528 /* Either relase after press-drag-release or
4529 * click-motion-click. */
4530 menu->func(menu->user_data, input, menu->current);
4531 input_ungrab(input);
4533 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4534 menu->release_count++;
4539 menu_touch_up_handler(struct widget *widget,
4540 struct input *input,
4546 struct menu *menu = data;
4548 input_ungrab(input);
4553 menu_redraw_handler(struct widget *widget, void *data)
4556 struct menu *menu = data;
4557 int32_t x, y, width, height, i;
4559 cr = widget_cairo_create(widget);
4561 frame_repaint(menu->frame, cr);
4562 frame_interior(menu->frame, &x, &y, &width, &height);
4564 theme_set_background_source(menu->window->display->theme,
4565 cr, THEME_FRAME_ACTIVE);
4566 cairo_rectangle(cr, x, y, width, height);
4569 cairo_select_font_face(cr, "sans",
4570 CAIRO_FONT_SLANT_NORMAL,
4571 CAIRO_FONT_WEIGHT_NORMAL);
4572 cairo_set_font_size(cr, 12);
4574 for (i = 0; i < menu->count; i++) {
4575 if (i == menu->current) {
4576 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4577 cairo_rectangle(cr, x, y + i * 20, width, 20);
4579 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4580 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4581 cairo_show_text(cr, menu->entries[i]);
4583 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4584 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4585 cairo_show_text(cr, menu->entries[i]);
4593 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4595 struct window *window = data;
4596 struct menu *menu = window->main_surface->widget->user_data;
4598 input_ungrab(menu->input);
4602 static const struct xdg_popup_listener xdg_popup_listener = {
4603 handle_popup_popup_done,
4606 static struct menu *
4607 create_menu(struct display *display,
4608 struct input *input, uint32_t time,
4609 menu_func_t func, const char **entries, int count,
4612 struct window *window;
4615 menu = malloc(sizeof *menu);
4619 window = window_create_internal(display, 0);
4625 menu->window = window;
4626 menu->user_data = user_data;
4627 menu->widget = window_add_widget(menu->window, menu);
4628 menu->frame = frame_create(window->display->theme, 0, 0,
4629 FRAME_BUTTON_NONE, NULL);
4630 fail_on_null(menu->frame);
4631 menu->entries = entries;
4632 menu->count = count;
4633 menu->release_count = 0;
4637 menu->input = input;
4639 input_ungrab(input);
4641 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4642 widget_set_enter_handler(menu->widget, menu_enter_handler);
4643 widget_set_leave_handler(menu->widget, menu_leave_handler);
4644 widget_set_motion_handler(menu->widget, menu_motion_handler);
4645 widget_set_button_handler(menu->widget, menu_button_handler);
4646 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4648 input_grab(input, menu->widget, 0);
4649 frame_resize_inside(menu->frame, 200, count * 20);
4650 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4651 window_schedule_resize(window, frame_width(menu->frame),
4652 frame_height(menu->frame));
4658 window_create_menu(struct display *display,
4659 struct input *input, uint32_t time,
4660 menu_func_t func, const char **entries, int count,
4664 menu = create_menu(display, input, time, func, entries, count, user_data);
4669 return menu->window;
4673 window_show_menu(struct display *display,
4674 struct input *input, uint32_t time, struct window *parent,
4675 int32_t x, int32_t y,
4676 menu_func_t func, const char **entries, int count)
4679 struct window *window;
4682 menu = create_menu(display, input, time, func, entries, count, parent);
4687 window = menu->window;
4689 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4690 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4695 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4697 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4698 window->main_surface->surface,
4699 parent->main_surface->surface,
4701 display_get_serial(window->display),
4705 fail_on_null(window->xdg_popup);
4707 xdg_popup_set_user_data(window->xdg_popup, window);
4708 xdg_popup_add_listener(window->xdg_popup,
4709 &xdg_popup_listener, window);
4713 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4715 window->main_surface->buffer_type = type;
4718 enum window_buffer_type
4719 window_get_buffer_type(struct window *window)
4721 return window->main_surface->buffer_type;
4725 window_set_preferred_format(struct window *window,
4726 enum preferred_format format)
4728 window->preferred_format = format;
4732 window_add_subsurface(struct window *window, void *data,
4733 enum subsurface_mode default_mode)
4735 struct widget *widget;
4736 struct surface *surface;
4737 struct wl_surface *parent;
4738 struct wl_subcompositor *subcompo = window->display->subcompositor;
4740 surface = surface_create(window);
4741 surface->buffer_type = window_get_buffer_type(window);
4742 widget = widget_create(window, surface, data);
4743 wl_list_init(&widget->link);
4744 surface->widget = widget;
4746 parent = window->main_surface->surface;
4747 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4750 surface->synchronized = 1;
4752 switch (default_mode) {
4753 case SUBSURFACE_SYNCHRONIZED:
4754 surface->synchronized_default = 1;
4756 case SUBSURFACE_DESYNCHRONIZED:
4757 surface->synchronized_default = 0;
4760 assert(!"bad enum subsurface_mode");
4763 window->resize_needed = 1;
4764 window_schedule_redraw(window);
4770 display_handle_geometry(void *data,
4771 struct wl_output *wl_output,
4774 int physical_height,
4780 struct output *output = data;
4782 output->allocation.x = x;
4783 output->allocation.y = y;
4784 output->transform = transform;
4788 output->make = strdup(make);
4791 free(output->model);
4792 output->model = strdup(model);
4796 display_handle_done(void *data,
4797 struct wl_output *wl_output)
4802 display_handle_scale(void *data,
4803 struct wl_output *wl_output,
4806 struct output *output = data;
4808 output->scale = scale;
4812 display_handle_mode(void *data,
4813 struct wl_output *wl_output,
4819 struct output *output = data;
4820 struct display *display = output->display;
4822 if (flags & WL_OUTPUT_MODE_CURRENT) {
4823 output->allocation.width = width;
4824 output->allocation.height = height;
4825 if (display->output_configure_handler)
4826 (*display->output_configure_handler)(
4827 output, display->user_data);
4831 static const struct wl_output_listener output_listener = {
4832 display_handle_geometry,
4833 display_handle_mode,
4834 display_handle_done,
4835 display_handle_scale
4839 display_add_output(struct display *d, uint32_t id)
4841 struct output *output;
4843 output = xzalloc(sizeof *output);
4844 output->display = d;
4847 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4848 output->server_output_id = id;
4849 wl_list_insert(d->output_list.prev, &output->link);
4851 wl_output_add_listener(output->output, &output_listener, output);
4855 output_destroy(struct output *output)
4857 if (output->destroy_handler)
4858 (*output->destroy_handler)(output, output->user_data);
4860 wl_output_destroy(output->output);
4861 wl_list_remove(&output->link);
4866 display_destroy_output(struct display *d, uint32_t id)
4868 struct output *output;
4870 wl_list_for_each(output, &d->output_list, link) {
4871 if (output->server_output_id == id) {
4872 output_destroy(output);
4879 display_set_global_handler(struct display *display,
4880 display_global_handler_t handler)
4882 struct global *global;
4884 display->global_handler = handler;
4888 wl_list_for_each(global, &display->global_list, link)
4889 display->global_handler(display,
4890 global->name, global->interface,
4891 global->version, display->user_data);
4895 display_set_global_handler_remove(struct display *display,
4896 display_global_handler_t remove_handler)
4898 display->global_handler_remove = remove_handler;
4899 if (!remove_handler)
4904 display_set_output_configure_handler(struct display *display,
4905 display_output_handler_t handler)
4907 struct output *output;
4909 display->output_configure_handler = handler;
4913 wl_list_for_each(output, &display->output_list, link) {
4914 if (output->allocation.width == 0 &&
4915 output->allocation.height == 0)
4918 (*display->output_configure_handler)(output,
4919 display->user_data);
4924 output_set_user_data(struct output *output, void *data)
4926 output->user_data = data;
4930 output_get_user_data(struct output *output)
4932 return output->user_data;
4936 output_set_destroy_handler(struct output *output,
4937 display_output_handler_t handler)
4939 output->destroy_handler = handler;
4940 /* FIXME: implement this, once we have way to remove outputs */
4944 output_get_allocation(struct output *output, struct rectangle *base)
4946 struct rectangle allocation = output->allocation;
4948 switch (output->transform) {
4949 case WL_OUTPUT_TRANSFORM_90:
4950 case WL_OUTPUT_TRANSFORM_270:
4951 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4952 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4953 /* Swap width and height */
4954 allocation.width = output->allocation.height;
4955 allocation.height = output->allocation.width;
4963 output_get_wl_output(struct output *output)
4965 return output->output;
4968 enum wl_output_transform
4969 output_get_transform(struct output *output)
4971 return output->transform;
4975 output_get_scale(struct output *output)
4977 return output->scale;
4981 output_get_make(struct output *output)
4983 return output->make;
4987 output_get_model(struct output *output)
4989 return output->model;
4993 fini_xkb(struct input *input)
4995 xkb_state_unref(input->xkb.state);
4996 xkb_keymap_unref(input->xkb.keymap);
4999 #define MIN(a,b) ((a) < (b) ? a : b)
5002 display_add_input(struct display *d, uint32_t id)
5004 struct input *input;
5006 input = xzalloc(sizeof *input);
5008 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
5009 MIN(d->seat_version, 4));
5010 input->touch_focus = NULL;
5011 input->pointer_focus = NULL;
5012 input->keyboard_focus = NULL;
5013 wl_list_init(&input->touch_point_list);
5014 wl_list_insert(d->input_list.prev, &input->link);
5016 wl_seat_add_listener(input->seat, &seat_listener, input);
5017 wl_seat_set_user_data(input->seat, input);
5019 if (d->data_device_manager) {
5020 input->data_device =
5021 wl_data_device_manager_get_data_device(d->data_device_manager,
5023 wl_data_device_add_listener(input->data_device,
5024 &data_device_listener,
5028 input->pointer_surface = wl_compositor_create_surface(d->compositor);
5030 set_repeat_info(input, 40, 400);
5032 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5033 TFD_CLOEXEC | TFD_NONBLOCK);
5034 input->repeat_task.run = keyboard_repeat_func;
5035 display_watch_fd(d, input->repeat_timer_fd,
5036 EPOLLIN, &input->repeat_task);
5040 input_destroy(struct input *input)
5042 input_remove_keyboard_focus(input);
5043 input_remove_pointer_focus(input);
5045 if (input->drag_offer)
5046 data_offer_destroy(input->drag_offer);
5048 if (input->selection_offer)
5049 data_offer_destroy(input->selection_offer);
5051 if (input->data_device)
5052 wl_data_device_destroy(input->data_device);
5054 if (input->display->seat_version >= 3) {
5056 wl_pointer_release(input->pointer);
5057 if (input->keyboard)
5058 wl_keyboard_release(input->keyboard);
5063 wl_surface_destroy(input->pointer_surface);
5065 wl_list_remove(&input->link);
5066 wl_seat_destroy(input->seat);
5067 close(input->repeat_timer_fd);
5072 init_workspace_manager(struct display *d, uint32_t id)
5074 d->workspace_manager =
5075 wl_registry_bind(d->registry, id,
5076 &workspace_manager_interface, 1);
5077 if (d->workspace_manager != NULL)
5078 workspace_manager_add_listener(d->workspace_manager,
5079 &workspace_manager_listener,
5084 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5086 struct display *d = data;
5088 if (format == WL_SHM_FORMAT_RGB565)
5092 struct wl_shm_listener shm_listener = {
5097 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5099 xdg_shell_pong(shell, serial);
5102 static const struct xdg_shell_listener xdg_shell_listener = {
5106 #define XDG_VERSION 4 /* The version of xdg-shell that we implement */
5107 #ifdef static_assert
5108 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5109 "Interface version doesn't match implementation version");
5113 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5114 const char *interface, uint32_t version)
5116 struct display *d = data;
5117 struct global *global;
5119 global = xmalloc(sizeof *global);
5121 global->interface = strdup(interface);
5122 global->version = version;
5123 wl_list_insert(d->global_list.prev, &global->link);
5125 if (strcmp(interface, "wl_compositor") == 0) {
5126 d->compositor = wl_registry_bind(registry, id,
5127 &wl_compositor_interface, 3);
5128 } else if (strcmp(interface, "wl_output") == 0) {
5129 display_add_output(d, id);
5130 } else if (strcmp(interface, "wl_seat") == 0) {
5131 d->seat_version = version;
5132 display_add_input(d, id);
5133 } else if (strcmp(interface, "wl_shm") == 0) {
5134 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5135 wl_shm_add_listener(d->shm, &shm_listener, d);
5136 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5137 d->data_device_manager =
5138 wl_registry_bind(registry, id,
5139 &wl_data_device_manager_interface, 1);
5140 } else if (strcmp(interface, "xdg_shell") == 0) {
5141 d->xdg_shell = wl_registry_bind(registry, id,
5142 &xdg_shell_interface, 1);
5143 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5144 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5145 } else if (strcmp(interface, "text_cursor_position") == 0) {
5146 d->text_cursor_position =
5147 wl_registry_bind(registry, id,
5148 &text_cursor_position_interface, 1);
5149 } else if (strcmp(interface, "workspace_manager") == 0) {
5150 init_workspace_manager(d, id);
5151 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5153 wl_registry_bind(registry, id,
5154 &wl_subcompositor_interface, 1);
5157 if (d->global_handler)
5158 d->global_handler(d, id, interface, version, d->user_data);
5162 registry_handle_global_remove(void *data, struct wl_registry *registry,
5165 struct display *d = data;
5166 struct global *global;
5169 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5170 if (global->name != name)
5173 if (strcmp(global->interface, "wl_output") == 0)
5174 display_destroy_output(d, name);
5176 /* XXX: Should destroy remaining bound globals */
5178 if (d->global_handler_remove)
5179 d->global_handler_remove(d, name, global->interface,
5180 global->version, d->user_data);
5182 wl_list_remove(&global->link);
5183 free(global->interface);
5189 display_bind(struct display *display, uint32_t name,
5190 const struct wl_interface *interface, uint32_t version)
5192 return wl_registry_bind(display->registry, name, interface, version);
5195 static const struct wl_registry_listener registry_listener = {
5196 registry_handle_global,
5197 registry_handle_global_remove
5200 #ifdef HAVE_CAIRO_EGL
5202 init_egl(struct display *d)
5204 EGLint major, minor;
5207 #ifdef USE_CAIRO_GLESV2
5208 # define GL_BIT EGL_OPENGL_ES2_BIT
5210 # define GL_BIT EGL_OPENGL_BIT
5213 static const EGLint argb_cfg_attribs[] = {
5214 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5220 EGL_RENDERABLE_TYPE, GL_BIT,
5224 #ifdef USE_CAIRO_GLESV2
5225 static const EGLint context_attribs[] = {
5226 EGL_CONTEXT_CLIENT_VERSION, 2,
5229 EGLint api = EGL_OPENGL_ES_API;
5231 EGLint *context_attribs = NULL;
5232 EGLint api = EGL_OPENGL_API;
5235 d->dpy = eglGetDisplay(d->display);
5236 if (!eglInitialize(d->dpy, &major, &minor)) {
5237 fprintf(stderr, "failed to initialize EGL\n");
5241 if (!eglBindAPI(api)) {
5242 fprintf(stderr, "failed to bind EGL client API\n");
5246 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5247 &d->argb_config, 1, &n) || n != 1) {
5248 fprintf(stderr, "failed to choose argb EGL config\n");
5252 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5253 EGL_NO_CONTEXT, context_attribs);
5254 if (d->argb_ctx == NULL) {
5255 fprintf(stderr, "failed to create EGL context\n");
5259 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5260 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5261 fprintf(stderr, "failed to get cairo EGL argb device\n");
5269 fini_egl(struct display *display)
5271 cairo_device_destroy(display->argb_device);
5273 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5276 eglTerminate(display->dpy);
5282 init_dummy_surface(struct display *display)
5287 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5289 display->dummy_surface =
5290 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5292 display->dummy_surface_data = data;
5296 handle_display_data(struct task *task, uint32_t events)
5298 struct display *display =
5299 container_of(task, struct display, display_task);
5300 struct epoll_event ep;
5303 display->display_fd_events = events;
5305 if (events & EPOLLERR || events & EPOLLHUP) {
5306 display_exit(display);
5310 if (events & EPOLLIN) {
5311 ret = wl_display_dispatch(display->display);
5313 display_exit(display);
5318 if (events & EPOLLOUT) {
5319 ret = wl_display_flush(display->display);
5321 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5322 ep.data.ptr = &display->display_task;
5323 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5324 display->display_fd, &ep);
5325 } else if (ret == -1 && errno != EAGAIN) {
5326 display_exit(display);
5333 log_handler(const char *format, va_list args)
5335 vfprintf(stderr, format, args);
5339 display_create(int *argc, char *argv[])
5343 wl_log_set_handler_client(log_handler);
5345 d = zalloc(sizeof *d);
5349 d->display = wl_display_connect(NULL);
5350 if (d->display == NULL) {
5351 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5356 d->xkb_context = xkb_context_new(0);
5357 if (d->xkb_context == NULL) {
5358 fprintf(stderr, "Failed to create XKB context\n");
5363 d->epoll_fd = os_epoll_create_cloexec();
5364 d->display_fd = wl_display_get_fd(d->display);
5365 d->display_task.run = handle_display_data;
5366 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5369 wl_list_init(&d->deferred_list);
5370 wl_list_init(&d->input_list);
5371 wl_list_init(&d->output_list);
5372 wl_list_init(&d->global_list);
5375 d->workspace_count = 1;
5377 d->registry = wl_display_get_registry(d->display);
5378 wl_registry_add_listener(d->registry, ®istry_listener, d);
5380 if (wl_display_dispatch(d->display) < 0) {
5381 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5385 #ifdef HAVE_CAIRO_EGL
5386 if (init_egl(d) < 0)
5387 fprintf(stderr, "EGL does not seem to work, "
5388 "falling back to software rendering and wl_shm.\n");
5393 d->theme = theme_create();
5395 wl_list_init(&d->window_list);
5397 init_dummy_surface(d);
5403 display_destroy_outputs(struct display *display)
5406 struct output *output;
5408 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5409 output_destroy(output);
5413 display_destroy_inputs(struct display *display)
5416 struct input *input;
5418 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5419 input_destroy(input);
5423 display_destroy(struct display *display)
5425 if (!wl_list_empty(&display->window_list))
5426 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5427 wl_list_length(&display->window_list));
5429 if (!wl_list_empty(&display->deferred_list))
5430 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5432 cairo_surface_destroy(display->dummy_surface);
5433 free(display->dummy_surface_data);
5435 display_destroy_outputs(display);
5436 display_destroy_inputs(display);
5438 xkb_context_unref(display->xkb_context);
5440 theme_destroy(display->theme);
5441 destroy_cursors(display);
5443 #ifdef HAVE_CAIRO_EGL
5444 if (display->argb_device)
5448 if (display->subcompositor)
5449 wl_subcompositor_destroy(display->subcompositor);
5451 if (display->xdg_shell)
5452 xdg_shell_destroy(display->xdg_shell);
5455 wl_shm_destroy(display->shm);
5457 if (display->data_device_manager)
5458 wl_data_device_manager_destroy(display->data_device_manager);
5460 wl_compositor_destroy(display->compositor);
5461 wl_registry_destroy(display->registry);
5463 close(display->epoll_fd);
5465 if (!(display->display_fd_events & EPOLLERR) &&
5466 !(display->display_fd_events & EPOLLHUP))
5467 wl_display_flush(display->display);
5469 wl_display_disconnect(display->display);
5474 display_set_user_data(struct display *display, void *data)
5476 display->user_data = data;
5480 display_get_user_data(struct display *display)
5482 return display->user_data;
5486 display_get_display(struct display *display)
5488 return display->display;
5492 display_has_subcompositor(struct display *display)
5494 if (display->subcompositor)
5497 wl_display_roundtrip(display->display);
5499 return display->subcompositor != NULL;
5503 display_get_cairo_device(struct display *display)
5505 return display->argb_device;
5509 display_get_output(struct display *display)
5511 return container_of(display->output_list.next, struct output, link);
5514 struct wl_compositor *
5515 display_get_compositor(struct display *display)
5517 return display->compositor;
5521 display_get_serial(struct display *display)
5523 return display->serial;
5527 display_get_egl_display(struct display *d)
5532 struct wl_data_source *
5533 display_create_data_source(struct display *display)
5535 if (display->data_device_manager)
5536 return wl_data_device_manager_create_data_source(display->data_device_manager);
5542 display_get_argb_egl_config(struct display *d)
5544 return d->argb_config;
5548 display_acquire_window_surface(struct display *display,
5549 struct window *window,
5552 struct surface *surface = window->main_surface;
5554 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5557 widget_get_cairo_surface(window->main_surface->widget);
5558 return surface->toysurface->acquire(surface->toysurface, ctx);
5562 display_release_window_surface(struct display *display,
5563 struct window *window)
5565 struct surface *surface = window->main_surface;
5567 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5570 surface->toysurface->release(surface->toysurface);
5574 display_defer(struct display *display, struct task *task)
5576 wl_list_insert(&display->deferred_list, &task->link);
5580 display_watch_fd(struct display *display,
5581 int fd, uint32_t events, struct task *task)
5583 struct epoll_event ep;
5587 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5591 display_unwatch_fd(struct display *display, int fd)
5593 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5597 display_run(struct display *display)
5600 struct epoll_event ep[16];
5603 display->running = 1;
5605 while (!wl_list_empty(&display->deferred_list)) {
5606 task = container_of(display->deferred_list.prev,
5608 wl_list_remove(&task->link);
5612 wl_display_dispatch_pending(display->display);
5614 if (!display->running)
5617 ret = wl_display_flush(display->display);
5618 if (ret < 0 && errno == EAGAIN) {
5620 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5621 ep[0].data.ptr = &display->display_task;
5623 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5624 display->display_fd, &ep[0]);
5625 } else if (ret < 0) {
5629 count = epoll_wait(display->epoll_fd,
5630 ep, ARRAY_LENGTH(ep), -1);
5631 for (i = 0; i < count; i++) {
5632 task = ep[i].data.ptr;
5633 task->run(task, ep[i].events);
5639 display_exit(struct display *display)
5641 display->running = 0;
5645 keysym_modifiers_add(struct wl_array *modifiers_map,
5648 size_t len = strlen(name) + 1;
5651 p = wl_array_add(modifiers_map, len);
5656 strncpy(p, name, len);
5659 static xkb_mod_index_t
5660 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5663 xkb_mod_index_t index = 0;
5664 char *p = modifiers_map->data;
5666 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5667 if (strcmp(p, name) == 0)
5674 return XKB_MOD_INVALID;
5678 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5681 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5683 if (index == XKB_MOD_INVALID)
5684 return XKB_MOD_INVALID;
5690 fail_on_null(void *p)
5693 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5703 return fail_on_null(malloc(s));
5709 return fail_on_null(zalloc(s));
5713 xstrdup(const char *s)
5715 return fail_on_null(strdup(s));
5719 xrealloc(char *p, size_t s)
5721 return fail_on_null(realloc(p, s));