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 "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_subcompositor *subcompositor;
88 struct wl_shell *shell;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
94 EGLConfig argb_config;
96 cairo_device_t *argb_device;
100 uint32_t display_fd_events;
101 struct task display_task;
104 struct wl_list deferred_list;
108 struct wl_list global_list;
109 struct wl_list window_list;
110 struct wl_list input_list;
111 struct wl_list output_list;
115 struct wl_cursor_theme *cursor_theme;
116 struct wl_cursor **cursors;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
120 display_global_handler_t global_handler_remove;
124 struct xkb_context *xkb_context;
127 uint32_t workspace_count;
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
147 struct window_output {
148 struct output *output;
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
157 * width,height are the new buffer size.
158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
160 * Returns the Cairo surface to draw to.
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
163 int32_t width, int32_t height, uint32_t flags,
164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
171 void (*swap)(struct toysurface *base,
172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
173 struct rectangle *server_allocation);
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
182 * Release the toysurface from the EGL context, returning control
185 void (*release)(struct toysurface *base);
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
191 void (*destroy)(struct toysurface *base);
195 struct window *window;
197 struct wl_surface *surface;
198 struct wl_subsurface *subsurface;
200 int synchronized_default;
201 struct toysurface *toysurface;
202 struct widget *widget;
204 struct wl_callback *frame_cb;
207 struct rectangle allocation;
208 struct rectangle server_allocation;
210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
215 int32_t buffer_scale;
217 cairo_surface_t *cairo_surface;
223 struct display *display;
224 struct wl_list window_output_list;
226 struct rectangle saved_allocation;
227 struct rectangle min_allocation;
228 struct rectangle pending_allocation;
232 int redraw_task_scheduled;
233 struct task redraw_task;
240 int fullscreen_method;
241 int configure_requests;
243 enum preferred_format preferred_format;
245 window_key_handler_t key_handler;
246 window_keyboard_focus_handler_t keyboard_focus_handler;
247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
249 window_close_handler_t close_handler;
250 window_fullscreen_handler_t fullscreen_handler;
251 window_output_handler_t output_handler;
253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
256 struct window_frame *frame;
258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
266 struct window *window;
267 struct surface *surface;
268 struct tooltip *tooltip;
269 struct wl_list child_list;
271 struct rectangle allocation;
272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
276 widget_motion_handler_t motion_handler;
277 widget_button_handler_t button_handler;
278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
283 widget_axis_handler_t axis_handler;
293 struct widget *widget;
298 struct display *display;
299 struct wl_seat *seat;
300 struct wl_pointer *pointer;
301 struct wl_keyboard *keyboard;
302 struct wl_touch *touch;
303 struct wl_list touch_point_list;
304 struct window *pointer_focus;
305 struct window *keyboard_focus;
306 struct window *touch_focus;
308 uint32_t cursor_anim_start;
309 struct wl_callback *cursor_frame_cb;
310 struct wl_surface *pointer_surface;
312 uint32_t pointer_enter_serial;
313 uint32_t cursor_serial;
317 struct widget *focus_widget;
319 uint32_t grab_button;
321 struct wl_data_device *data_device;
322 struct data_offer *drag_offer;
323 struct data_offer *selection_offer;
325 int32_t touch_grab_id;
326 float drag_x, drag_y;
327 struct window *drag_focus;
328 uint32_t drag_enter_serial;
331 struct xkb_keymap *keymap;
332 struct xkb_state *state;
333 xkb_mod_mask_t control_mask;
334 xkb_mod_mask_t alt_mask;
335 xkb_mod_mask_t shift_mask;
338 struct task repeat_task;
342 uint32_t repeat_time;
346 struct display *display;
347 struct wl_output *output;
348 uint32_t server_output_id;
349 struct rectangle allocation;
354 display_output_handler_t destroy_handler;
358 struct window_frame {
359 struct widget *widget;
360 struct widget *child;
365 struct window *window;
366 struct window *parent;
367 struct widget *widget;
370 const char **entries;
379 struct widget *parent;
380 struct window *window;
381 struct widget *widget;
383 struct task tooltip_task;
389 struct wl_shm_pool *pool;
396 CURSOR_DEFAULT = 100,
400 enum window_location {
402 WINDOW_RESIZING_TOP = 1,
403 WINDOW_RESIZING_BOTTOM = 2,
404 WINDOW_RESIZING_LEFT = 4,
405 WINDOW_RESIZING_TOP_LEFT = 5,
406 WINDOW_RESIZING_BOTTOM_LEFT = 6,
407 WINDOW_RESIZING_RIGHT = 8,
408 WINDOW_RESIZING_TOP_RIGHT = 9,
409 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
410 WINDOW_RESIZING_MASK = 15,
411 WINDOW_EXTERIOR = 16,
412 WINDOW_TITLEBAR = 17,
413 WINDOW_CLIENT_AREA = 18,
416 static const cairo_user_data_key_t shm_surface_data_key;
423 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
424 __attribute__ ((format (printf, 4, 5)));
427 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
432 gettimeofday(&tv, NULL);
433 fprintf(stderr, "%8ld.%03ld ",
434 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
437 fprintf(stderr, "%s@%d ",
438 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
440 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
441 fprintf(stderr, "%s ", func);
444 vfprintf(stderr, fmt, ap);
448 #define DBG(fmt, ...) \
449 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
451 #define DBG_OBJ(obj, fmt, ...) \
452 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
456 #define DBG(...) do {} while (0)
457 #define DBG_OBJ(...) do {} while (0)
462 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
466 switch (buffer_transform) {
467 case WL_OUTPUT_TRANSFORM_90:
468 case WL_OUTPUT_TRANSFORM_270:
469 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
470 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
479 *width *= buffer_scale;
480 *height *= buffer_scale;
484 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
488 switch (buffer_transform) {
489 case WL_OUTPUT_TRANSFORM_90:
490 case WL_OUTPUT_TRANSFORM_270:
491 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
492 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
501 *width /= buffer_scale;
502 *height /= buffer_scale;
505 #ifdef HAVE_CAIRO_EGL
507 struct egl_window_surface {
508 struct toysurface base;
509 cairo_surface_t *cairo_surface;
510 struct display *display;
511 struct wl_surface *surface;
512 struct wl_egl_window *egl_window;
513 EGLSurface egl_surface;
516 static struct egl_window_surface *
517 to_egl_window_surface(struct toysurface *base)
519 return container_of(base, struct egl_window_surface, base);
522 static cairo_surface_t *
523 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
524 int32_t width, int32_t height, uint32_t flags,
525 enum wl_output_transform buffer_transform, int32_t buffer_scale)
527 struct egl_window_surface *surface = to_egl_window_surface(base);
529 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
531 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
532 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
534 return cairo_surface_reference(surface->cairo_surface);
538 egl_window_surface_swap(struct toysurface *base,
539 enum wl_output_transform buffer_transform, int32_t buffer_scale,
540 struct rectangle *server_allocation)
542 struct egl_window_surface *surface = to_egl_window_surface(base);
544 cairo_gl_surface_swapbuffers(surface->cairo_surface);
545 wl_egl_window_get_attached_size(surface->egl_window,
546 &server_allocation->width,
547 &server_allocation->height);
549 buffer_to_surface_size (buffer_transform, buffer_scale,
550 &server_allocation->width,
551 &server_allocation->height);
555 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
557 struct egl_window_surface *surface = to_egl_window_surface(base);
558 cairo_device_t *device;
560 device = cairo_surface_get_device(surface->cairo_surface);
565 if (device == surface->display->argb_device)
566 ctx = surface->display->argb_ctx;
571 cairo_device_flush(device);
572 cairo_device_acquire(device);
573 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
574 surface->egl_surface, ctx))
575 fprintf(stderr, "failed to make surface current\n");
581 egl_window_surface_release(struct toysurface *base)
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 cairo_device_t *device;
586 device = cairo_surface_get_device(surface->cairo_surface);
590 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
591 surface->display->argb_ctx))
592 fprintf(stderr, "failed to make context current\n");
594 cairo_device_release(device);
598 egl_window_surface_destroy(struct toysurface *base)
600 struct egl_window_surface *surface = to_egl_window_surface(base);
601 struct display *d = surface->display;
603 cairo_surface_destroy(surface->cairo_surface);
604 eglDestroySurface(d->dpy, surface->egl_surface);
605 wl_egl_window_destroy(surface->egl_window);
606 surface->surface = NULL;
611 static struct toysurface *
612 egl_window_surface_create(struct display *display,
613 struct wl_surface *wl_surface,
615 struct rectangle *rectangle)
617 struct egl_window_surface *surface;
619 if (display->dpy == EGL_NO_DISPLAY)
622 surface = calloc(1, sizeof *surface);
626 surface->base.prepare = egl_window_surface_prepare;
627 surface->base.swap = egl_window_surface_swap;
628 surface->base.acquire = egl_window_surface_acquire;
629 surface->base.release = egl_window_surface_release;
630 surface->base.destroy = egl_window_surface_destroy;
632 surface->display = display;
633 surface->surface = wl_surface;
635 surface->egl_window = wl_egl_window_create(surface->surface,
639 surface->egl_surface = eglCreateWindowSurface(display->dpy,
640 display->argb_config,
644 surface->cairo_surface =
645 cairo_gl_surface_create_for_egl(display->argb_device,
646 surface->egl_surface,
650 return &surface->base;
655 static struct toysurface *
656 egl_window_surface_create(struct display *display,
657 struct wl_surface *wl_surface,
659 struct rectangle *rectangle)
666 struct shm_surface_data {
667 struct wl_buffer *buffer;
668 struct shm_pool *pool;
672 display_get_buffer_for_surface(struct display *display,
673 cairo_surface_t *surface)
675 struct shm_surface_data *data;
677 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
683 shm_pool_destroy(struct shm_pool *pool);
686 shm_surface_data_destroy(void *p)
688 struct shm_surface_data *data = p;
690 wl_buffer_destroy(data->buffer);
692 shm_pool_destroy(data->pool);
697 static struct wl_shm_pool *
698 make_shm_pool(struct display *display, int size, void **data)
700 struct wl_shm_pool *pool;
703 fd = os_create_anonymous_file(size);
705 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
710 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
711 if (*data == MAP_FAILED) {
712 fprintf(stderr, "mmap failed: %m\n");
717 pool = wl_shm_create_pool(display->shm, fd, size);
724 static struct shm_pool *
725 shm_pool_create(struct display *display, size_t size)
727 struct shm_pool *pool = malloc(sizeof *pool);
732 pool->pool = make_shm_pool(display, size, &pool->data);
745 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
747 if (pool->used + size > pool->size)
750 *offset = pool->used;
753 return (char *) pool->data + *offset;
756 /* destroy the pool. this does not unmap the memory though */
758 shm_pool_destroy(struct shm_pool *pool)
760 munmap(pool->data, pool->size);
761 wl_shm_pool_destroy(pool->pool);
765 /* Start allocating from the beginning of the pool again */
767 shm_pool_reset(struct shm_pool *pool)
773 data_length_for_shm_surface(struct rectangle *rect)
777 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
779 return stride * rect->height;
782 static cairo_surface_t *
783 display_create_shm_surface_from_pool(struct display *display,
784 struct rectangle *rectangle,
785 uint32_t flags, struct shm_pool *pool)
787 struct shm_surface_data *data;
789 cairo_surface_t *surface;
790 cairo_format_t cairo_format;
791 int stride, length, offset;
794 data = malloc(sizeof *data);
798 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
799 cairo_format = CAIRO_FORMAT_RGB16_565;
801 cairo_format = CAIRO_FORMAT_ARGB32;
803 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
804 length = stride * rectangle->height;
806 map = shm_pool_allocate(pool, length, &offset);
813 surface = cairo_image_surface_create_for_data (map,
819 cairo_surface_set_user_data(surface, &shm_surface_data_key,
820 data, shm_surface_data_destroy);
822 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
823 format = WL_SHM_FORMAT_RGB565;
825 if (flags & SURFACE_OPAQUE)
826 format = WL_SHM_FORMAT_XRGB8888;
828 format = WL_SHM_FORMAT_ARGB8888;
831 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
839 static cairo_surface_t *
840 display_create_shm_surface(struct display *display,
841 struct rectangle *rectangle, uint32_t flags,
842 struct shm_pool *alternate_pool,
843 struct shm_surface_data **data_ret)
845 struct shm_surface_data *data;
846 struct shm_pool *pool;
847 cairo_surface_t *surface;
849 if (alternate_pool) {
850 shm_pool_reset(alternate_pool);
851 surface = display_create_shm_surface_from_pool(display,
856 data = cairo_surface_get_user_data(surface,
857 &shm_surface_data_key);
862 pool = shm_pool_create(display,
863 data_length_for_shm_surface(rectangle));
868 display_create_shm_surface_from_pool(display, rectangle,
872 shm_pool_destroy(pool);
876 /* make sure we destroy the pool when the surface is destroyed */
877 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
888 check_size(struct rectangle *rect)
890 if (rect->width && rect->height)
893 fprintf(stderr, "tried to create surface of "
894 "width: %d, height: %d\n", rect->width, rect->height);
899 display_create_surface(struct display *display,
900 struct wl_surface *surface,
901 struct rectangle *rectangle,
904 if (check_size(rectangle) < 0)
907 assert(flags & SURFACE_SHM);
908 return display_create_shm_surface(display, rectangle, flags,
912 struct shm_surface_leaf {
913 cairo_surface_t *cairo_surface;
914 /* 'data' is automatically destroyed, when 'cairo_surface' is */
915 struct shm_surface_data *data;
917 struct shm_pool *resize_pool;
922 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
924 if (leaf->cairo_surface)
925 cairo_surface_destroy(leaf->cairo_surface);
926 /* leaf->data already destroyed via cairo private */
928 if (leaf->resize_pool)
929 shm_pool_destroy(leaf->resize_pool);
931 memset(leaf, 0, sizeof *leaf);
937 struct toysurface base;
938 struct display *display;
939 struct wl_surface *surface;
943 struct shm_surface_leaf leaf[MAX_LEAVES];
944 struct shm_surface_leaf *current;
947 static struct shm_surface *
948 to_shm_surface(struct toysurface *base)
950 return container_of(base, struct shm_surface, base);
954 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
957 struct shm_surface_leaf *leaf;
958 char bufs[MAX_LEAVES + 1];
961 for (i = 0; i < MAX_LEAVES; i++) {
962 leaf = &surface->leaf[i];
966 else if (leaf->cairo_surface)
972 bufs[MAX_LEAVES] = '\0';
973 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
978 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
980 struct shm_surface *surface = data;
981 struct shm_surface_leaf *leaf;
985 shm_surface_buffer_state_debug(surface, "buffer_release before");
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
989 if (leaf->data && leaf->data->buffer == buffer) {
994 assert(i < MAX_LEAVES && "unknown buffer released");
996 /* Leave one free leaf with storage, release others */
998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1001 if (!leaf->cairo_surface || leaf->busy)
1007 shm_surface_leaf_release(leaf);
1010 shm_surface_buffer_state_debug(surface, "buffer_release after");
1013 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1014 shm_surface_buffer_release
1017 static cairo_surface_t *
1018 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1019 int32_t width, int32_t height, uint32_t flags,
1020 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1022 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1023 struct shm_surface *surface = to_shm_surface(base);
1024 struct rectangle rect = { 0};
1025 struct shm_surface_leaf *leaf = NULL;
1031 /* pick a free buffer, preferrably one that already has storage */
1032 for (i = 0; i < MAX_LEAVES; i++) {
1033 if (surface->leaf[i].busy)
1036 if (!leaf || surface->leaf[i].cairo_surface)
1037 leaf = &surface->leaf[i];
1039 DBG_OBJ(surface->surface, "pick leaf %d\n",
1040 (int)(leaf - &surface->leaf[0]));
1043 fprintf(stderr, "%s: all buffers are held by the server.\n",
1049 if (!resize_hint && leaf->resize_pool) {
1050 cairo_surface_destroy(leaf->cairo_surface);
1051 leaf->cairo_surface = NULL;
1052 shm_pool_destroy(leaf->resize_pool);
1053 leaf->resize_pool = NULL;
1056 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1058 if (leaf->cairo_surface &&
1059 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1060 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1063 if (leaf->cairo_surface)
1064 cairo_surface_destroy(leaf->cairo_surface);
1066 #ifdef USE_RESIZE_POOL
1067 if (resize_hint && !leaf->resize_pool) {
1068 /* Create a big pool to allocate from, while continuously
1069 * resizing. Mmapping a new pool in the server
1070 * is relatively expensive, so reusing a pool performs
1071 * better, but may temporarily reserve unneeded memory.
1073 /* We should probably base this number on the output size. */
1074 leaf->resize_pool = shm_pool_create(surface->display,
1080 rect.height = height;
1082 leaf->cairo_surface =
1083 display_create_shm_surface(surface->display, &rect,
1087 if (!leaf->cairo_surface)
1090 wl_buffer_add_listener(leaf->data->buffer,
1091 &shm_surface_buffer_listener, surface);
1094 surface->current = leaf;
1096 return cairo_surface_reference(leaf->cairo_surface);
1100 shm_surface_swap(struct toysurface *base,
1101 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1102 struct rectangle *server_allocation)
1104 struct shm_surface *surface = to_shm_surface(base);
1105 struct shm_surface_leaf *leaf = surface->current;
1107 server_allocation->width =
1108 cairo_image_surface_get_width(leaf->cairo_surface);
1109 server_allocation->height =
1110 cairo_image_surface_get_height(leaf->cairo_surface);
1112 buffer_to_surface_size (buffer_transform, buffer_scale,
1113 &server_allocation->width,
1114 &server_allocation->height);
1116 wl_surface_attach(surface->surface, leaf->data->buffer,
1117 surface->dx, surface->dy);
1118 wl_surface_damage(surface->surface, 0, 0,
1119 server_allocation->width, server_allocation->height);
1120 wl_surface_commit(surface->surface);
1122 DBG_OBJ(surface->surface, "leaf %d busy\n",
1123 (int)(leaf - &surface->leaf[0]));
1126 surface->current = NULL;
1130 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1136 shm_surface_release(struct toysurface *base)
1141 shm_surface_destroy(struct toysurface *base)
1143 struct shm_surface *surface = to_shm_surface(base);
1146 for (i = 0; i < MAX_LEAVES; i++)
1147 shm_surface_leaf_release(&surface->leaf[i]);
1152 static struct toysurface *
1153 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1154 uint32_t flags, struct rectangle *rectangle)
1156 struct shm_surface *surface;
1157 DBG_OBJ(wl_surface, "\n");
1159 surface = xmalloc(sizeof *surface);
1160 memset(surface, 0, sizeof *surface);
1165 surface->base.prepare = shm_surface_prepare;
1166 surface->base.swap = shm_surface_swap;
1167 surface->base.acquire = shm_surface_acquire;
1168 surface->base.release = shm_surface_release;
1169 surface->base.destroy = shm_surface_destroy;
1171 surface->display = display;
1172 surface->surface = wl_surface;
1173 surface->flags = flags;
1175 return &surface->base;
1179 * The following correspondences between file names and cursors was copied
1180 * from: https://bugs.kde.org/attachment.cgi?id=67313
1183 static const char *bottom_left_corners[] = {
1184 "bottom_left_corner",
1189 static const char *bottom_right_corners[] = {
1190 "bottom_right_corner",
1195 static const char *bottom_sides[] = {
1201 static const char *grabbings[] = {
1204 "208530c400c041818281048008011002"
1207 static const char *left_ptrs[] = {
1214 static const char *left_sides[] = {
1220 static const char *right_sides[] = {
1226 static const char *top_left_corners[] = {
1232 static const char *top_right_corners[] = {
1238 static const char *top_sides[] = {
1244 static const char *xterms[] = {
1250 static const char *hand1s[] = {
1254 "e29285e634086352946a0e7090d73106"
1257 static const char *watches[] = {
1260 "0426c94ea35c87780ff01dc239897213"
1263 struct cursor_alternatives {
1268 static const struct cursor_alternatives cursors[] = {
1269 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1270 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1271 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1272 {grabbings, ARRAY_LENGTH(grabbings)},
1273 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1274 {left_sides, ARRAY_LENGTH(left_sides)},
1275 {right_sides, ARRAY_LENGTH(right_sides)},
1276 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1277 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1278 {top_sides, ARRAY_LENGTH(top_sides)},
1279 {xterms, ARRAY_LENGTH(xterms)},
1280 {hand1s, ARRAY_LENGTH(hand1s)},
1281 {watches, ARRAY_LENGTH(watches)},
1285 create_cursors(struct display *display)
1287 struct weston_config *config;
1288 struct weston_config_section *s;
1292 struct wl_cursor *cursor;
1294 config = weston_config_parse("weston.ini");
1295 s = weston_config_get_section(config, "shell", NULL, NULL);
1296 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1297 weston_config_section_get_int(s, "cursor-size", &size, 32);
1298 weston_config_destroy(config);
1300 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1303 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1305 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1307 for (j = 0; !cursor && j < cursors[i].count; ++j)
1308 cursor = wl_cursor_theme_get_cursor(
1309 display->cursor_theme, cursors[i].names[j]);
1312 fprintf(stderr, "could not load cursor '%s'\n",
1313 cursors[i].names[0]);
1315 display->cursors[i] = cursor;
1320 destroy_cursors(struct display *display)
1322 wl_cursor_theme_destroy(display->cursor_theme);
1323 free(display->cursors);
1326 struct wl_cursor_image *
1327 display_get_pointer_image(struct display *display, int pointer)
1329 struct wl_cursor *cursor = display->cursors[pointer];
1331 return cursor ? cursor->images[0] : NULL;
1335 surface_flush(struct surface *surface)
1337 if (!surface->cairo_surface)
1340 if (surface->opaque_region) {
1341 wl_surface_set_opaque_region(surface->surface,
1342 surface->opaque_region);
1343 wl_region_destroy(surface->opaque_region);
1344 surface->opaque_region = NULL;
1347 if (surface->input_region) {
1348 wl_surface_set_input_region(surface->surface,
1349 surface->input_region);
1350 wl_region_destroy(surface->input_region);
1351 surface->input_region = NULL;
1354 surface->toysurface->swap(surface->toysurface,
1355 surface->buffer_transform, surface->buffer_scale,
1356 &surface->server_allocation);
1358 cairo_surface_destroy(surface->cairo_surface);
1359 surface->cairo_surface = NULL;
1363 window_has_focus(struct window *window)
1365 return window->focus_count > 0;
1369 window_flush(struct window *window)
1371 struct surface *surface;
1373 if (window->type == TYPE_NONE) {
1374 window->type = TYPE_TOPLEVEL;
1375 if (window->shell_surface)
1376 wl_shell_surface_set_toplevel(window->shell_surface);
1379 wl_list_for_each(surface, &window->subsurface_list, link) {
1380 if (surface == window->main_surface)
1383 surface_flush(surface);
1386 surface_flush(window->main_surface);
1390 window_get_display(struct window *window)
1392 return window->display;
1396 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1398 struct display *display = surface->window->display;
1399 struct rectangle allocation = surface->allocation;
1401 if (!surface->toysurface && display->dpy &&
1402 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1403 surface->toysurface =
1404 egl_window_surface_create(display,
1410 if (!surface->toysurface)
1411 surface->toysurface = shm_surface_create(display,
1413 flags, &allocation);
1415 surface->cairo_surface = surface->toysurface->prepare(
1416 surface->toysurface, dx, dy,
1417 allocation.width, allocation.height, flags,
1418 surface->buffer_transform, surface->buffer_scale);
1422 window_create_main_surface(struct window *window)
1424 struct surface *surface = window->main_surface;
1429 if (window->resizing)
1430 flags |= SURFACE_HINT_RESIZE;
1432 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1433 flags |= SURFACE_HINT_RGB565;
1435 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1436 dx = surface->server_allocation.width -
1437 surface->allocation.width;
1439 if (window->resize_edges & WINDOW_RESIZING_TOP)
1440 dy = surface->server_allocation.height -
1441 surface->allocation.height;
1443 window->resize_edges = 0;
1445 surface_create_surface(surface, dx, dy, flags);
1449 window_get_buffer_transform(struct window *window)
1451 return window->main_surface->buffer_transform;
1455 window_set_buffer_transform(struct window *window,
1456 enum wl_output_transform transform)
1458 window->main_surface->buffer_transform = transform;
1459 wl_surface_set_buffer_transform(window->main_surface->surface,
1464 window_set_buffer_scale(struct window *window,
1467 window->main_surface->buffer_scale = scale;
1468 wl_surface_set_buffer_scale(window->main_surface->surface,
1473 window_get_buffer_scale(struct window *window)
1475 return window->main_surface->buffer_scale;
1479 window_get_output_scale(struct window *window)
1481 struct window_output *window_output;
1482 struct window_output *window_output_tmp;
1485 wl_list_for_each_safe(window_output, window_output_tmp,
1486 &window->window_output_list, link) {
1487 if (window_output->output->scale > scale)
1488 scale = window_output->output->scale;
1494 static void window_frame_destroy(struct window_frame *frame);
1497 surface_destroy(struct surface *surface)
1499 if (surface->frame_cb)
1500 wl_callback_destroy(surface->frame_cb);
1502 if (surface->input_region)
1503 wl_region_destroy(surface->input_region);
1505 if (surface->opaque_region)
1506 wl_region_destroy(surface->opaque_region);
1508 if (surface->subsurface)
1509 wl_subsurface_destroy(surface->subsurface);
1511 wl_surface_destroy(surface->surface);
1513 if (surface->toysurface)
1514 surface->toysurface->destroy(surface->toysurface);
1516 wl_list_remove(&surface->link);
1521 window_destroy(struct window *window)
1523 struct display *display = window->display;
1524 struct input *input;
1525 struct window_output *window_output;
1526 struct window_output *window_output_tmp;
1528 wl_list_remove(&window->redraw_task.link);
1530 wl_list_for_each(input, &display->input_list, link) {
1531 if (input->touch_focus == window)
1532 input->touch_focus = NULL;
1533 if (input->pointer_focus == window)
1534 input->pointer_focus = NULL;
1535 if (input->keyboard_focus == window)
1536 input->keyboard_focus = NULL;
1537 if (input->focus_widget &&
1538 input->focus_widget->window == window)
1539 input->focus_widget = NULL;
1542 wl_list_for_each_safe(window_output, window_output_tmp,
1543 &window->window_output_list, link) {
1544 free (window_output);
1548 window_frame_destroy(window->frame);
1550 if (window->shell_surface)
1551 wl_shell_surface_destroy(window->shell_surface);
1553 surface_destroy(window->main_surface);
1555 wl_list_remove(&window->link);
1557 free(window->title);
1561 static struct widget *
1562 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1564 struct widget *child, *target;
1566 wl_list_for_each(child, &widget->child_list, link) {
1567 target = widget_find_widget(child, x, y);
1572 if (widget->allocation.x <= x &&
1573 x < widget->allocation.x + widget->allocation.width &&
1574 widget->allocation.y <= y &&
1575 y < widget->allocation.y + widget->allocation.height) {
1582 static struct widget *
1583 window_find_widget(struct window *window, int32_t x, int32_t y)
1585 struct surface *surface;
1586 struct widget *widget;
1588 wl_list_for_each(surface, &window->subsurface_list, link) {
1589 widget = widget_find_widget(surface->widget, x, y);
1597 static struct widget *
1598 widget_create(struct window *window, struct surface *surface, void *data)
1600 struct widget *widget;
1602 widget = xzalloc(sizeof *widget);
1603 widget->window = window;
1604 widget->surface = surface;
1605 widget->user_data = data;
1606 widget->allocation = surface->allocation;
1607 wl_list_init(&widget->child_list);
1609 widget->tooltip = NULL;
1610 widget->tooltip_count = 0;
1611 widget->default_cursor = CURSOR_LEFT_PTR;
1617 window_add_widget(struct window *window, void *data)
1619 struct widget *widget;
1621 widget = widget_create(window, window->main_surface, data);
1622 wl_list_init(&widget->link);
1623 window->main_surface->widget = widget;
1629 widget_add_widget(struct widget *parent, void *data)
1631 struct widget *widget;
1633 widget = widget_create(parent->window, parent->surface, data);
1634 wl_list_insert(parent->child_list.prev, &widget->link);
1640 widget_destroy(struct widget *widget)
1642 struct display *display = widget->window->display;
1643 struct surface *surface = widget->surface;
1644 struct input *input;
1646 /* Destroy the sub-surface along with the root widget */
1647 if (surface->widget == widget && surface->subsurface)
1648 surface_destroy(widget->surface);
1650 if (widget->tooltip) {
1651 free(widget->tooltip);
1652 widget->tooltip = NULL;
1655 wl_list_for_each(input, &display->input_list, link) {
1656 if (input->focus_widget == widget)
1657 input->focus_widget = NULL;
1660 wl_list_remove(&widget->link);
1665 widget_set_default_cursor(struct widget *widget, int cursor)
1667 widget->default_cursor = cursor;
1671 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1673 *allocation = widget->allocation;
1677 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1679 widget->allocation.width = width;
1680 widget->allocation.height = height;
1684 widget_set_allocation(struct widget *widget,
1685 int32_t x, int32_t y, int32_t width, int32_t height)
1687 widget->allocation.x = x;
1688 widget->allocation.y = y;
1689 widget_set_size(widget, width, height);
1693 widget_set_transparent(struct widget *widget, int transparent)
1695 widget->opaque = !transparent;
1699 widget_get_user_data(struct widget *widget)
1701 return widget->user_data;
1704 static cairo_surface_t *
1705 widget_get_cairo_surface(struct widget *widget)
1707 struct surface *surface = widget->surface;
1708 struct window *window = widget->window;
1710 if (!surface->cairo_surface) {
1711 if (surface == window->main_surface)
1712 window_create_main_surface(window);
1714 surface_create_surface(surface, 0, 0, 0);
1717 return surface->cairo_surface;
1721 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1723 struct surface *surface = widget->surface;
1726 enum wl_output_transform transform;
1727 int surface_width, surface_height;
1728 int translate_x, translate_y;
1731 surface_width = surface->allocation.width;
1732 surface_height = surface->allocation.height;
1734 transform = surface->buffer_transform;
1735 scale = surface->buffer_scale;
1737 switch (transform) {
1738 case WL_OUTPUT_TRANSFORM_FLIPPED:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1741 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1742 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1745 cairo_matrix_init_identity(&m);
1749 switch (transform) {
1750 case WL_OUTPUT_TRANSFORM_NORMAL:
1756 case WL_OUTPUT_TRANSFORM_FLIPPED:
1758 translate_x = surface_width;
1761 case WL_OUTPUT_TRANSFORM_90:
1763 translate_x = surface_height;
1766 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1768 translate_x = surface_height;
1769 translate_y = surface_width;
1771 case WL_OUTPUT_TRANSFORM_180:
1773 translate_x = surface_width;
1774 translate_y = surface_height;
1776 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1779 translate_y = surface_height;
1781 case WL_OUTPUT_TRANSFORM_270:
1782 angle = M_PI + M_PI_2;
1784 translate_y = surface_width;
1786 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1787 angle = M_PI + M_PI_2;
1793 cairo_scale(cr, scale, scale);
1794 cairo_translate(cr, translate_x, translate_y);
1795 cairo_rotate(cr, angle);
1796 cairo_transform(cr, &m);
1800 widget_cairo_create(struct widget *widget)
1802 struct surface *surface = widget->surface;
1803 cairo_surface_t *cairo_surface;
1806 cairo_surface = widget_get_cairo_surface(widget);
1807 cr = cairo_create(cairo_surface);
1809 widget_cairo_update_transform(widget, cr);
1811 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1817 widget_get_wl_surface(struct widget *widget)
1819 return widget->surface->surface;
1822 struct wl_subsurface *
1823 widget_get_wl_subsurface(struct widget *widget)
1825 return widget->surface->subsurface;
1829 widget_get_last_time(struct widget *widget)
1831 return widget->surface->last_time;
1835 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1837 struct wl_compositor *comp = widget->window->display->compositor;
1838 struct surface *surface = widget->surface;
1840 if (!surface->input_region)
1841 surface->input_region = wl_compositor_create_region(comp);
1844 wl_region_add(surface->input_region,
1845 rect->x, rect->y, rect->width, rect->height);
1850 widget_set_resize_handler(struct widget *widget,
1851 widget_resize_handler_t handler)
1853 widget->resize_handler = handler;
1857 widget_set_redraw_handler(struct widget *widget,
1858 widget_redraw_handler_t handler)
1860 widget->redraw_handler = handler;
1864 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1866 widget->enter_handler = handler;
1870 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1872 widget->leave_handler = handler;
1876 widget_set_motion_handler(struct widget *widget,
1877 widget_motion_handler_t handler)
1879 widget->motion_handler = handler;
1883 widget_set_button_handler(struct widget *widget,
1884 widget_button_handler_t handler)
1886 widget->button_handler = handler;
1890 widget_set_touch_up_handler(struct widget *widget,
1891 widget_touch_up_handler_t handler)
1893 widget->touch_up_handler = handler;
1897 widget_set_touch_down_handler(struct widget *widget,
1898 widget_touch_down_handler_t handler)
1900 widget->touch_down_handler = handler;
1904 widget_set_touch_motion_handler(struct widget *widget,
1905 widget_touch_motion_handler_t handler)
1907 widget->touch_motion_handler = handler;
1911 widget_set_touch_frame_handler(struct widget *widget,
1912 widget_touch_frame_handler_t handler)
1914 widget->touch_frame_handler = handler;
1918 widget_set_touch_cancel_handler(struct widget *widget,
1919 widget_touch_cancel_handler_t handler)
1921 widget->touch_cancel_handler = handler;
1925 widget_set_axis_handler(struct widget *widget,
1926 widget_axis_handler_t handler)
1928 widget->axis_handler = handler;
1932 window_schedule_redraw_task(struct window *window);
1935 widget_schedule_redraw(struct widget *widget)
1937 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1938 widget->surface->redraw_needed = 1;
1939 window_schedule_redraw_task(widget->window);
1943 window_get_surface(struct window *window)
1945 cairo_surface_t *cairo_surface;
1947 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1949 return cairo_surface_reference(cairo_surface);
1953 window_get_wl_surface(struct window *window)
1955 return window->main_surface->surface;
1959 tooltip_redraw_handler(struct widget *widget, void *data)
1962 const int32_t r = 3;
1963 struct tooltip *tooltip = data;
1964 int32_t width, height;
1966 cr = widget_cairo_create(widget);
1967 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1968 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1971 width = widget->allocation.width;
1972 height = widget->allocation.height;
1973 rounded_rect(cr, 0, 0, width, height, r);
1975 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1976 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1979 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1980 cairo_move_to(cr, 10, 16);
1981 cairo_show_text(cr, tooltip->entry);
1985 static cairo_text_extents_t
1986 get_text_extents(struct tooltip *tooltip)
1989 cairo_text_extents_t extents;
1991 /* Use the dummy_surface because tooltip's surface was not
1992 * created yet, and parent does not have a valid surface
1993 * outside repaint, either.
1995 cr = cairo_create(tooltip->window->display->dummy_surface);
1996 cairo_text_extents(cr, tooltip->entry, &extents);
2003 window_create_tooltip(struct tooltip *tooltip)
2005 struct widget *parent = tooltip->parent;
2006 struct display *display = parent->window->display;
2007 struct window *window;
2008 const int offset_y = 27;
2009 const int margin = 3;
2010 cairo_text_extents_t extents;
2012 if (tooltip->widget)
2015 window = window_create_transient(display, parent->window, tooltip->x,
2016 tooltip->y + offset_y,
2017 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2021 tooltip->window = window;
2022 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2024 extents = get_text_extents(tooltip);
2025 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2026 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2032 widget_destroy_tooltip(struct widget *parent)
2034 struct tooltip *tooltip = parent->tooltip;
2036 parent->tooltip_count = 0;
2040 if (tooltip->widget) {
2041 widget_destroy(tooltip->widget);
2042 window_destroy(tooltip->window);
2043 tooltip->widget = NULL;
2044 tooltip->window = NULL;
2047 close(tooltip->tooltip_fd);
2048 free(tooltip->entry);
2050 parent->tooltip = NULL;
2054 tooltip_func(struct task *task, uint32_t events)
2056 struct tooltip *tooltip =
2057 container_of(task, struct tooltip, tooltip_task);
2060 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2062 window_create_tooltip(tooltip);
2065 #define TOOLTIP_TIMEOUT 500
2067 tooltip_timer_reset(struct tooltip *tooltip)
2069 struct itimerspec its;
2071 its.it_interval.tv_sec = 0;
2072 its.it_interval.tv_nsec = 0;
2073 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2074 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2075 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2076 fprintf(stderr, "could not set timerfd\n: %m");
2084 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2086 struct tooltip *tooltip = parent->tooltip;
2088 parent->tooltip_count++;
2092 tooltip_timer_reset(tooltip);
2096 /* the handler might be triggered too fast via input device motion, so
2097 * we need this check here to make sure tooltip is fully initialized */
2098 if (parent->tooltip_count > 1)
2101 tooltip = malloc(sizeof *tooltip);
2105 parent->tooltip = tooltip;
2106 tooltip->parent = parent;
2107 tooltip->widget = NULL;
2108 tooltip->window = NULL;
2111 tooltip->entry = strdup(entry);
2112 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2113 if (tooltip->tooltip_fd < 0) {
2114 fprintf(stderr, "could not create timerfd\n: %m");
2118 tooltip->tooltip_task.run = tooltip_func;
2119 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2120 EPOLLIN, &tooltip->tooltip_task);
2121 tooltip_timer_reset(tooltip);
2127 workspace_manager_state(void *data,
2128 struct workspace_manager *workspace_manager,
2132 struct display *display = data;
2134 display->workspace = current;
2135 display->workspace_count = count;
2138 static const struct workspace_manager_listener workspace_manager_listener = {
2139 workspace_manager_state
2143 frame_resize_handler(struct widget *widget,
2144 int32_t width, int32_t height, void *data)
2146 struct window_frame *frame = data;
2147 struct widget *child = frame->child;
2148 struct rectangle interior;
2149 struct rectangle input;
2150 struct rectangle opaque;
2152 if (widget->window->type == TYPE_FULLSCREEN) {
2155 interior.width = width;
2156 interior.height = height;
2158 if (widget->window->type == TYPE_MAXIMIZED) {
2159 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2161 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2164 frame_resize(frame->frame, width, height);
2165 frame_interior(frame->frame, &interior.x, &interior.y,
2166 &interior.width, &interior.height);
2169 widget_set_allocation(child, interior.x, interior.y,
2170 interior.width, interior.height);
2172 if (child->resize_handler) {
2173 child->resize_handler(child, interior.width, interior.height,
2176 if (widget->window->type == TYPE_FULLSCREEN) {
2177 width = child->allocation.width;
2178 height = child->allocation.height;
2180 frame_resize_inside(frame->frame,
2181 child->allocation.width,
2182 child->allocation.height);
2183 width = frame_width(frame->frame);
2184 height = frame_height(frame->frame);
2188 widget_set_allocation(widget, 0, 0, width, height);
2190 widget->surface->input_region =
2191 wl_compositor_create_region(widget->window->display->compositor);
2192 if (widget->window->type != TYPE_FULLSCREEN) {
2193 frame_input_rect(frame->frame, &input.x, &input.y,
2194 &input.width, &input.height);
2195 wl_region_add(widget->surface->input_region,
2196 input.x, input.y, input.width, input.height);
2198 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2201 widget_set_allocation(widget, 0, 0, width, height);
2203 if (child->opaque) {
2204 if (widget->window->type != TYPE_FULLSCREEN) {
2205 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2206 &opaque.width, &opaque.height);
2208 wl_region_add(widget->surface->opaque_region,
2210 opaque.width, opaque.height);
2212 wl_region_add(widget->surface->opaque_region,
2213 0, 0, width, height);
2218 widget_schedule_redraw(widget);
2222 frame_redraw_handler(struct widget *widget, void *data)
2225 struct window_frame *frame = data;
2226 struct window *window = widget->window;
2228 if (window->type == TYPE_FULLSCREEN)
2231 if (window->focus_count) {
2232 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2234 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2237 cr = widget_cairo_create(widget);
2239 frame_repaint(frame->frame, cr);
2245 frame_get_pointer_image_for_location(struct window_frame *frame,
2246 enum theme_location location)
2248 struct window *window = frame->widget->window;
2250 if (window->type != TYPE_TOPLEVEL)
2251 return CURSOR_LEFT_PTR;
2254 case THEME_LOCATION_RESIZING_TOP:
2256 case THEME_LOCATION_RESIZING_BOTTOM:
2257 return CURSOR_BOTTOM;
2258 case THEME_LOCATION_RESIZING_LEFT:
2260 case THEME_LOCATION_RESIZING_RIGHT:
2261 return CURSOR_RIGHT;
2262 case THEME_LOCATION_RESIZING_TOP_LEFT:
2263 return CURSOR_TOP_LEFT;
2264 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2265 return CURSOR_TOP_RIGHT;
2266 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2267 return CURSOR_BOTTOM_LEFT;
2268 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2269 return CURSOR_BOTTOM_RIGHT;
2270 case THEME_LOCATION_EXTERIOR:
2271 case THEME_LOCATION_TITLEBAR:
2273 return CURSOR_LEFT_PTR;
2278 frame_menu_func(struct window *window,
2279 struct input *input, int index, void *data)
2281 struct display *display;
2285 if (window->close_handler)
2286 window->close_handler(window->user_data);
2288 display_exit(window->display);
2290 case 1: /* move to workspace above */
2291 display = window->display;
2292 if (display->workspace > 0)
2293 workspace_manager_move_surface(
2294 display->workspace_manager,
2295 window->main_surface->surface,
2296 display->workspace - 1);
2298 case 2: /* move to workspace below */
2299 display = window->display;
2300 if (display->workspace < display->workspace_count - 1)
2301 workspace_manager_move_surface(
2302 display->workspace_manager,
2303 window->main_surface->surface,
2304 display->workspace + 1);
2306 case 3: /* fullscreen */
2307 /* we don't have a way to get out of fullscreen for now */
2308 if (window->fullscreen_handler)
2309 window->fullscreen_handler(window, window->user_data);
2315 window_show_frame_menu(struct window *window,
2316 struct input *input, uint32_t time)
2321 static const char *entries[] = {
2323 "Move to workspace above", "Move to workspace below",
2327 if (window->fullscreen_handler)
2328 count = ARRAY_LENGTH(entries);
2330 count = ARRAY_LENGTH(entries) - 1;
2332 input_get_position(input, &x, &y);
2333 window_show_menu(window->display, input, time, window,
2334 x - 10, y - 10, frame_menu_func, entries, count);
2338 frame_enter_handler(struct widget *widget,
2339 struct input *input, float x, float y, void *data)
2341 struct window_frame *frame = data;
2342 enum theme_location location;
2344 location = frame_pointer_enter(frame->frame, input, x, y);
2345 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2348 return frame_get_pointer_image_for_location(data, location);
2352 frame_motion_handler(struct widget *widget,
2353 struct input *input, uint32_t time,
2354 float x, float y, void *data)
2356 struct window_frame *frame = data;
2357 enum theme_location location;
2359 location = frame_pointer_motion(frame->frame, input, x, y);
2360 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2361 widget_schedule_redraw(frame->widget);
2363 return frame_get_pointer_image_for_location(data, location);
2367 frame_leave_handler(struct widget *widget,
2368 struct input *input, void *data)
2370 struct window_frame *frame = data;
2372 frame_pointer_leave(frame->frame, input);
2373 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2374 widget_schedule_redraw(frame->widget);
2378 frame_handle_status(struct window_frame *frame, struct input *input,
2379 uint32_t time, enum theme_location location)
2381 struct window *window = frame->widget->window;
2384 status = frame_status(frame->frame);
2385 if (status & FRAME_STATUS_REPAINT)
2386 widget_schedule_redraw(frame->widget);
2388 if (status & FRAME_STATUS_MINIMIZE)
2389 fprintf(stderr,"Minimize stub\n");
2391 if (status & FRAME_STATUS_MENU) {
2392 window_show_frame_menu(window, input, time);
2393 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2396 if (status & FRAME_STATUS_MAXIMIZE) {
2397 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2398 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2401 if (status & FRAME_STATUS_CLOSE) {
2402 if (window->close_handler)
2403 window->close_handler(window->user_data);
2405 display_exit(window->display);
2409 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2410 input_ungrab(input);
2411 wl_shell_surface_move(window->shell_surface,
2412 input_get_seat(input),
2413 window->display->serial);
2415 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2418 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2419 input_ungrab(input);
2421 window->resizing = 1;
2422 wl_shell_surface_resize(window->shell_surface,
2423 input_get_seat(input),
2424 window->display->serial,
2427 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2432 frame_button_handler(struct widget *widget,
2433 struct input *input, uint32_t time,
2434 uint32_t button, enum wl_pointer_button_state state,
2438 struct window_frame *frame = data;
2439 enum theme_location location;
2441 location = frame_pointer_button(frame->frame, input, button, state);
2442 frame_handle_status(frame, input, time, location);
2446 frame_touch_down_handler(struct widget *widget, struct input *input,
2447 uint32_t serial, uint32_t time, int32_t id,
2448 float x, float y, void *data)
2450 struct window_frame *frame = data;
2452 frame_touch_down(frame->frame, input, id, x, y);
2453 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2457 frame_touch_up_handler(struct widget *widget,
2458 struct input *input, uint32_t serial, uint32_t time,
2459 int32_t id, void *data)
2461 struct window_frame *frame = data;
2463 frame_touch_up(frame->frame, input, id);
2464 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2468 window_frame_create(struct window *window, void *data)
2470 struct window_frame *frame;
2473 if (window->type == TYPE_CUSTOM) {
2474 buttons = FRAME_BUTTON_NONE;
2476 buttons = FRAME_BUTTON_ALL;
2479 frame = xzalloc(sizeof *frame);
2480 frame->frame = frame_create(window->display->theme, 0, 0,
2481 buttons, window->title);
2483 frame->widget = window_add_widget(window, frame);
2484 frame->child = widget_add_widget(frame->widget, data);
2486 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2487 widget_set_resize_handler(frame->widget, frame_resize_handler);
2488 widget_set_enter_handler(frame->widget, frame_enter_handler);
2489 widget_set_leave_handler(frame->widget, frame_leave_handler);
2490 widget_set_motion_handler(frame->widget, frame_motion_handler);
2491 widget_set_button_handler(frame->widget, frame_button_handler);
2492 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2493 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2495 window->frame = frame;
2497 return frame->child;
2501 window_frame_set_child_size(struct widget *widget, int child_width,
2504 struct display *display = widget->window->display;
2505 struct theme *t = display->theme;
2506 int decoration_width, decoration_height;
2508 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2510 if (widget->window->type != TYPE_FULLSCREEN) {
2511 decoration_width = (t->width + margin) * 2;
2512 decoration_height = t->width +
2513 t->titlebar_height + margin * 2;
2515 width = child_width + decoration_width;
2516 height = child_height + decoration_height;
2518 width = child_width;
2519 height = child_height;
2522 window_schedule_resize(widget->window, width, height);
2526 window_frame_destroy(struct window_frame *frame)
2528 frame_destroy(frame->frame);
2530 /* frame->child must be destroyed by the application */
2531 widget_destroy(frame->widget);
2536 input_set_focus_widget(struct input *input, struct widget *focus,
2539 struct widget *old, *widget;
2542 if (focus == input->focus_widget)
2545 old = input->focus_widget;
2549 widget = input->grab;
2550 if (widget->leave_handler)
2551 widget->leave_handler(old, input, widget->user_data);
2552 input->focus_widget = NULL;
2558 widget = input->grab;
2559 input->focus_widget = focus;
2560 if (widget->enter_handler)
2561 cursor = widget->enter_handler(focus, input, x, y,
2564 cursor = widget->default_cursor;
2566 input_set_pointer_image(input, cursor);
2571 touch_grab(struct input *input, int32_t touch_id)
2573 input->touch_grab = 1;
2574 input->touch_grab_id = touch_id;
2578 touch_ungrab(struct input *input)
2580 struct touch_point *tp, *tmp;
2582 input->touch_grab = 0;
2584 wl_list_for_each_safe(tp, tmp,
2585 &input->touch_point_list, link) {
2586 if (tp->id != input->touch_grab_id)
2588 wl_list_remove(&tp->link);
2596 input_grab(struct input *input, struct widget *widget, uint32_t button)
2598 input->grab = widget;
2599 input->grab_button = button;
2603 input_ungrab(struct input *input)
2605 struct widget *widget;
2608 if (input->pointer_focus) {
2609 widget = window_find_widget(input->pointer_focus,
2610 input->sx, input->sy);
2611 input_set_focus_widget(input, widget, input->sx, input->sy);
2616 input_remove_pointer_focus(struct input *input)
2618 struct window *window = input->pointer_focus;
2623 input_set_focus_widget(input, NULL, 0, 0);
2625 input->pointer_focus = NULL;
2626 input->current_cursor = CURSOR_UNSET;
2630 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2631 uint32_t serial, struct wl_surface *surface,
2632 wl_fixed_t sx_w, wl_fixed_t sy_w)
2634 struct input *input = data;
2635 struct window *window;
2636 struct widget *widget;
2637 float sx = wl_fixed_to_double(sx_w);
2638 float sy = wl_fixed_to_double(sy_w);
2641 /* enter event for a window we've just destroyed */
2645 input->display->serial = serial;
2646 input->pointer_enter_serial = serial;
2647 input->pointer_focus = wl_surface_get_user_data(surface);
2648 window = input->pointer_focus;
2650 if (window->resizing) {
2651 window->resizing = 0;
2652 /* Schedule a redraw to free the pool */
2653 window_schedule_redraw(window);
2659 widget = window_find_widget(window, sx, sy);
2660 input_set_focus_widget(input, widget, sx, sy);
2664 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2665 uint32_t serial, struct wl_surface *surface)
2667 struct input *input = data;
2669 input->display->serial = serial;
2670 input_remove_pointer_focus(input);
2674 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2675 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2677 struct input *input = data;
2678 struct window *window = input->pointer_focus;
2679 struct widget *widget;
2681 float sx = wl_fixed_to_double(sx_w);
2682 float sy = wl_fixed_to_double(sy_w);
2690 /* when making the window smaller - e.g. after a unmaximise we might
2691 * still have a pending motion event that the compositor has picked
2692 * based on the old surface dimensions
2694 if (sx > window->main_surface->allocation.width ||
2695 sy > window->main_surface->allocation.height)
2698 if (!(input->grab && input->grab_button)) {
2699 widget = window_find_widget(window, sx, sy);
2700 input_set_focus_widget(input, widget, sx, sy);
2704 widget = input->grab;
2706 widget = input->focus_widget;
2708 if (widget->motion_handler)
2709 cursor = widget->motion_handler(input->focus_widget,
2710 input, time, sx, sy,
2713 cursor = widget->default_cursor;
2715 cursor = CURSOR_LEFT_PTR;
2717 input_set_pointer_image(input, cursor);
2721 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2722 uint32_t time, uint32_t button, uint32_t state_w)
2724 struct input *input = data;
2725 struct widget *widget;
2726 enum wl_pointer_button_state state = state_w;
2728 input->display->serial = serial;
2729 if (input->focus_widget && input->grab == NULL &&
2730 state == WL_POINTER_BUTTON_STATE_PRESSED)
2731 input_grab(input, input->focus_widget, button);
2733 widget = input->grab;
2734 if (widget && widget->button_handler)
2735 (*widget->button_handler)(widget,
2738 input->grab->user_data);
2740 if (input->grab && input->grab_button == button &&
2741 state == WL_POINTER_BUTTON_STATE_RELEASED)
2742 input_ungrab(input);
2746 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2747 uint32_t time, uint32_t axis, wl_fixed_t value)
2749 struct input *input = data;
2750 struct widget *widget;
2752 widget = input->focus_widget;
2754 widget = input->grab;
2755 if (widget && widget->axis_handler)
2756 (*widget->axis_handler)(widget,
2762 static const struct wl_pointer_listener pointer_listener = {
2763 pointer_handle_enter,
2764 pointer_handle_leave,
2765 pointer_handle_motion,
2766 pointer_handle_button,
2767 pointer_handle_axis,
2771 input_remove_keyboard_focus(struct input *input)
2773 struct window *window = input->keyboard_focus;
2774 struct itimerspec its;
2776 its.it_interval.tv_sec = 0;
2777 its.it_interval.tv_nsec = 0;
2778 its.it_value.tv_sec = 0;
2779 its.it_value.tv_nsec = 0;
2780 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2785 window->focus_count--;
2786 if (window->keyboard_focus_handler)
2787 (*window->keyboard_focus_handler)(window, NULL,
2790 input->keyboard_focus = NULL;
2794 keyboard_repeat_func(struct task *task, uint32_t events)
2796 struct input *input =
2797 container_of(task, struct input, repeat_task);
2798 struct window *window = input->keyboard_focus;
2801 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2802 /* If we change the timer between the fd becoming
2803 * readable and getting here, there'll be nothing to
2804 * read and we get EAGAIN. */
2807 if (window && window->key_handler) {
2808 (*window->key_handler)(window, input, input->repeat_time,
2809 input->repeat_key, input->repeat_sym,
2810 WL_KEYBOARD_KEY_STATE_PRESSED,
2816 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2817 uint32_t format, int fd, uint32_t size)
2819 struct input *input = data;
2820 struct xkb_keymap *keymap;
2821 struct xkb_state *state;
2829 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2834 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2835 if (map_str == MAP_FAILED) {
2840 keymap = xkb_map_new_from_string(input->display->xkb_context,
2842 XKB_KEYMAP_FORMAT_TEXT_V1,
2844 munmap(map_str, size);
2848 fprintf(stderr, "failed to compile keymap\n");
2852 state = xkb_state_new(keymap);
2854 fprintf(stderr, "failed to create XKB state\n");
2855 xkb_map_unref(keymap);
2859 xkb_keymap_unref(input->xkb.keymap);
2860 xkb_state_unref(input->xkb.state);
2861 input->xkb.keymap = keymap;
2862 input->xkb.state = state;
2864 input->xkb.control_mask =
2865 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2866 input->xkb.alt_mask =
2867 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2868 input->xkb.shift_mask =
2869 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2873 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2874 uint32_t serial, struct wl_surface *surface,
2875 struct wl_array *keys)
2877 struct input *input = data;
2878 struct window *window;
2880 input->display->serial = serial;
2881 input->keyboard_focus = wl_surface_get_user_data(surface);
2883 window = input->keyboard_focus;
2884 window->focus_count++;
2885 if (window->keyboard_focus_handler)
2886 (*window->keyboard_focus_handler)(window,
2887 input, window->user_data);
2891 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2892 uint32_t serial, struct wl_surface *surface)
2894 struct input *input = data;
2896 input->display->serial = serial;
2897 input_remove_keyboard_focus(input);
2901 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2902 uint32_t serial, uint32_t time, uint32_t key,
2905 struct input *input = data;
2906 struct window *window = input->keyboard_focus;
2907 uint32_t code, num_syms;
2908 enum wl_keyboard_key_state state = state_w;
2909 const xkb_keysym_t *syms;
2911 struct itimerspec its;
2913 input->display->serial = serial;
2915 if (!window || !input->xkb.state)
2918 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2920 sym = XKB_KEY_NoSymbol;
2924 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2925 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2926 window_set_maximized(window,
2927 window->type != TYPE_MAXIMIZED);
2928 } else if (sym == XKB_KEY_F11 &&
2929 window->fullscreen_handler &&
2930 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2931 window->fullscreen_handler(window, window->user_data);
2932 } else if (sym == XKB_KEY_F4 &&
2933 input->modifiers == MOD_ALT_MASK &&
2934 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 if (window->close_handler)
2936 window->close_handler(window->user_data);
2938 display_exit(window->display);
2939 } else if (window->key_handler) {
2940 (*window->key_handler)(window, input, time, key,
2941 sym, state, window->user_data);
2944 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2945 key == input->repeat_key) {
2946 its.it_interval.tv_sec = 0;
2947 its.it_interval.tv_nsec = 0;
2948 its.it_value.tv_sec = 0;
2949 its.it_value.tv_nsec = 0;
2950 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2951 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2952 input->repeat_sym = sym;
2953 input->repeat_key = key;
2954 input->repeat_time = time;
2955 its.it_interval.tv_sec = 0;
2956 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2957 its.it_value.tv_sec = 0;
2958 its.it_value.tv_nsec = 400 * 1000 * 1000;
2959 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2964 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2965 uint32_t serial, uint32_t mods_depressed,
2966 uint32_t mods_latched, uint32_t mods_locked,
2969 struct input *input = data;
2970 xkb_mod_mask_t mask;
2972 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2973 if (!input->xkb.keymap)
2976 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2977 mods_locked, 0, 0, group);
2978 mask = xkb_state_serialize_mods(input->xkb.state,
2979 XKB_STATE_DEPRESSED |
2981 input->modifiers = 0;
2982 if (mask & input->xkb.control_mask)
2983 input->modifiers |= MOD_CONTROL_MASK;
2984 if (mask & input->xkb.alt_mask)
2985 input->modifiers |= MOD_ALT_MASK;
2986 if (mask & input->xkb.shift_mask)
2987 input->modifiers |= MOD_SHIFT_MASK;
2990 static const struct wl_keyboard_listener keyboard_listener = {
2991 keyboard_handle_keymap,
2992 keyboard_handle_enter,
2993 keyboard_handle_leave,
2994 keyboard_handle_key,
2995 keyboard_handle_modifiers,
2999 touch_handle_down(void *data, struct wl_touch *wl_touch,
3000 uint32_t serial, uint32_t time, struct wl_surface *surface,
3001 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3003 struct input *input = data;
3004 struct widget *widget;
3005 float sx = wl_fixed_to_double(x_w);
3006 float sy = wl_fixed_to_double(y_w);
3008 input->display->serial = serial;
3009 input->touch_focus = wl_surface_get_user_data(surface);
3010 if (!input->touch_focus) {
3011 DBG("Failed to find to touch focus for surface %p\n", surface);
3015 widget = window_find_widget(input->touch_focus,
3016 wl_fixed_to_double(x_w),
3017 wl_fixed_to_double(y_w));
3019 struct touch_point *tp = xmalloc(sizeof *tp);
3022 tp->widget = widget;
3025 wl_list_insert(&input->touch_point_list, &tp->link);
3027 if (widget->touch_down_handler)
3028 (*widget->touch_down_handler)(widget, input,
3037 touch_handle_up(void *data, struct wl_touch *wl_touch,
3038 uint32_t serial, uint32_t time, int32_t id)
3040 struct input *input = data;
3041 struct touch_point *tp, *tmp;
3043 if (!input->touch_focus) {
3044 DBG("No touch focus found for touch up event!\n");
3048 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3052 if (tp->widget->touch_up_handler)
3053 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3055 tp->widget->user_data);
3057 wl_list_remove(&tp->link);
3065 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3066 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3068 struct input *input = data;
3069 struct touch_point *tp;
3070 float sx = wl_fixed_to_double(x_w);
3071 float sy = wl_fixed_to_double(y_w);
3073 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3075 if (!input->touch_focus) {
3076 DBG("No touch focus found for touch motion event!\n");
3080 wl_list_for_each(tp, &input->touch_point_list, link) {
3086 if (tp->widget->touch_motion_handler)
3087 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3089 tp->widget->user_data);
3095 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3097 struct input *input = data;
3098 struct touch_point *tp, *tmp;
3100 DBG("touch_handle_frame\n");
3102 if (!input->touch_focus) {
3103 DBG("No touch focus found for touch frame event!\n");
3107 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3108 if (tp->widget->touch_frame_handler)
3109 (*tp->widget->touch_frame_handler)(tp->widget, input,
3110 tp->widget->user_data);
3112 wl_list_remove(&tp->link);
3118 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3120 struct input *input = data;
3121 struct touch_point *tp, *tmp;
3123 DBG("touch_handle_cancel\n");
3125 if (!input->touch_focus) {
3126 DBG("No touch focus found for touch cancel event!\n");
3130 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3131 if (tp->widget->touch_cancel_handler)
3132 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3133 tp->widget->user_data);
3135 wl_list_remove(&tp->link);
3140 static const struct wl_touch_listener touch_listener = {
3143 touch_handle_motion,
3145 touch_handle_cancel,
3149 seat_handle_capabilities(void *data, struct wl_seat *seat,
3150 enum wl_seat_capability caps)
3152 struct input *input = data;
3154 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3155 input->pointer = wl_seat_get_pointer(seat);
3156 wl_pointer_set_user_data(input->pointer, input);
3157 wl_pointer_add_listener(input->pointer, &pointer_listener,
3159 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3160 wl_pointer_destroy(input->pointer);
3161 input->pointer = NULL;
3164 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3165 input->keyboard = wl_seat_get_keyboard(seat);
3166 wl_keyboard_set_user_data(input->keyboard, input);
3167 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3169 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3170 wl_keyboard_destroy(input->keyboard);
3171 input->keyboard = NULL;
3174 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3175 input->touch = wl_seat_get_touch(seat);
3176 wl_touch_set_user_data(input->touch, input);
3177 wl_touch_add_listener(input->touch, &touch_listener, input);
3178 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3179 wl_touch_destroy(input->touch);
3180 input->touch = NULL;
3185 seat_handle_name(void *data, struct wl_seat *seat,
3191 static const struct wl_seat_listener seat_listener = {
3192 seat_handle_capabilities,
3197 input_get_position(struct input *input, int32_t *x, int32_t *y)
3204 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3206 struct touch_point *tp;
3208 wl_list_for_each(tp, &input->touch_point_list, link) {
3221 input_get_display(struct input *input)
3223 return input->display;
3227 input_get_seat(struct input *input)
3233 input_get_modifiers(struct input *input)
3235 return input->modifiers;
3239 input_get_focus_widget(struct input *input)
3241 return input->focus_widget;
3245 struct wl_data_offer *offer;
3246 struct input *input;
3247 struct wl_array types;
3250 struct task io_task;
3258 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3260 struct data_offer *offer = data;
3263 p = wl_array_add(&offer->types, sizeof *p);
3267 static const struct wl_data_offer_listener data_offer_listener = {
3272 data_offer_destroy(struct data_offer *offer)
3277 if (offer->refcount == 0) {
3278 wl_data_offer_destroy(offer->offer);
3279 for (p = offer->types.data; *p; p++)
3281 wl_array_release(&offer->types);
3287 data_device_data_offer(void *data,
3288 struct wl_data_device *data_device,
3289 struct wl_data_offer *_offer)
3291 struct data_offer *offer;
3293 offer = xmalloc(sizeof *offer);
3295 wl_array_init(&offer->types);
3296 offer->refcount = 1;
3297 offer->input = data;
3298 offer->offer = _offer;
3299 wl_data_offer_add_listener(offer->offer,
3300 &data_offer_listener, offer);
3304 data_device_enter(void *data, struct wl_data_device *data_device,
3305 uint32_t serial, struct wl_surface *surface,
3306 wl_fixed_t x_w, wl_fixed_t y_w,
3307 struct wl_data_offer *offer)
3309 struct input *input = data;
3310 struct window *window;
3312 float x = wl_fixed_to_double(x_w);
3313 float y = wl_fixed_to_double(y_w);
3316 window = wl_surface_get_user_data(surface);
3317 input->drag_enter_serial = serial;
3318 input->drag_focus = window,
3322 if (!input->touch_grab)
3323 input->pointer_enter_serial = serial;
3326 input->drag_offer = wl_data_offer_get_user_data(offer);
3328 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3331 types_data = input->drag_offer->types.data;
3333 input->drag_offer = NULL;
3337 if (window->data_handler)
3338 window->data_handler(window, input, x, y, types_data,
3343 data_device_leave(void *data, struct wl_data_device *data_device)
3345 struct input *input = data;
3347 if (input->drag_offer) {
3348 data_offer_destroy(input->drag_offer);
3349 input->drag_offer = NULL;
3354 data_device_motion(void *data, struct wl_data_device *data_device,
3355 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3357 struct input *input = data;
3358 struct window *window = input->drag_focus;
3359 float x = wl_fixed_to_double(x_w);
3360 float y = wl_fixed_to_double(y_w);
3366 if (input->drag_offer)
3367 types_data = input->drag_offer->types.data;
3371 if (window->data_handler)
3372 window->data_handler(window, input, x, y, types_data,
3377 data_device_drop(void *data, struct wl_data_device *data_device)
3379 struct input *input = data;
3380 struct window *window = input->drag_focus;
3386 if (window->drop_handler)
3387 window->drop_handler(window, input,
3388 x, y, window->user_data);
3390 if (input->touch_grab)
3391 touch_ungrab(input);
3395 data_device_selection(void *data,
3396 struct wl_data_device *wl_data_device,
3397 struct wl_data_offer *offer)
3399 struct input *input = data;
3402 if (input->selection_offer)
3403 data_offer_destroy(input->selection_offer);
3406 input->selection_offer = wl_data_offer_get_user_data(offer);
3407 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3410 input->selection_offer = NULL;
3414 static const struct wl_data_device_listener data_device_listener = {
3415 data_device_data_offer,
3420 data_device_selection
3424 input_set_pointer_image_index(struct input *input, int index)
3426 struct wl_buffer *buffer;
3427 struct wl_cursor *cursor;
3428 struct wl_cursor_image *image;
3430 if (!input->pointer)
3433 cursor = input->display->cursors[input->current_cursor];
3437 if (index >= (int) cursor->image_count) {
3438 fprintf(stderr, "cursor index out of range\n");
3442 image = cursor->images[index];
3443 buffer = wl_cursor_image_get_buffer(image);
3447 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3448 input->pointer_surface,
3449 image->hotspot_x, image->hotspot_y);
3450 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3451 wl_surface_damage(input->pointer_surface, 0, 0,
3452 image->width, image->height);
3453 wl_surface_commit(input->pointer_surface);
3456 static const struct wl_callback_listener pointer_surface_listener;
3459 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3462 struct input *input = data;
3463 struct wl_cursor *cursor;
3467 assert(callback == input->cursor_frame_cb);
3468 wl_callback_destroy(callback);
3469 input->cursor_frame_cb = NULL;
3472 if (!input->pointer)
3475 if (input->current_cursor == CURSOR_BLANK) {
3476 wl_pointer_set_cursor(input->pointer,
3477 input->pointer_enter_serial,
3482 if (input->current_cursor == CURSOR_UNSET)
3484 cursor = input->display->cursors[input->current_cursor];
3488 /* FIXME We don't have the current time on the first call so we set
3489 * the animation start to the time of the first frame callback. */
3491 input->cursor_anim_start = 0;
3492 else if (input->cursor_anim_start == 0)
3493 input->cursor_anim_start = time;
3495 if (time == 0 || input->cursor_anim_start == 0)
3498 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3500 if (cursor->image_count > 1) {
3501 input->cursor_frame_cb =
3502 wl_surface_frame(input->pointer_surface);
3503 wl_callback_add_listener(input->cursor_frame_cb,
3504 &pointer_surface_listener, input);
3507 input_set_pointer_image_index(input, i);
3510 static const struct wl_callback_listener pointer_surface_listener = {
3511 pointer_surface_frame_callback
3515 input_set_pointer_image(struct input *input, int pointer)
3519 if (!input->pointer)
3522 if (input->pointer_enter_serial > input->cursor_serial)
3525 if (!force && pointer == input->current_cursor)
3528 input->current_cursor = pointer;
3529 input->cursor_serial = input->pointer_enter_serial;
3530 if (!input->cursor_frame_cb)
3531 pointer_surface_frame_callback(input, NULL, 0);
3533 /* The current frame callback may be stuck if, for instance,
3534 * the set cursor request was processed by the server after
3535 * this client lost the focus. In this case the cursor surface
3536 * might not be mapped and the frame callback wouldn't ever
3537 * complete. Send a set_cursor and attach to try to map the
3538 * cursor surface again so that the callback will finish */
3539 input_set_pointer_image_index(input, 0);
3543 struct wl_data_device *
3544 input_get_data_device(struct input *input)
3546 return input->data_device;
3550 input_set_selection(struct input *input,
3551 struct wl_data_source *source, uint32_t time)
3553 wl_data_device_set_selection(input->data_device, source, time);
3557 input_accept(struct input *input, const char *type)
3559 wl_data_offer_accept(input->drag_offer->offer,
3560 input->drag_enter_serial, type);
3564 offer_io_func(struct task *task, uint32_t events)
3566 struct data_offer *offer =
3567 container_of(task, struct data_offer, io_task);
3571 len = read(offer->fd, buffer, sizeof buffer);
3572 offer->func(buffer, len,
3573 offer->x, offer->y, offer->user_data);
3577 data_offer_destroy(offer);
3582 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3583 data_func_t func, void *user_data)
3587 if (pipe2(p, O_CLOEXEC) == -1)
3590 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3593 offer->io_task.run = offer_io_func;
3597 offer->user_data = user_data;
3599 display_watch_fd(offer->input->display,
3600 offer->fd, EPOLLIN, &offer->io_task);
3604 input_receive_drag_data(struct input *input, const char *mime_type,
3605 data_func_t func, void *data)
3607 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3608 input->drag_offer->x = input->drag_x;
3609 input->drag_offer->y = input->drag_y;
3613 input_receive_drag_data_to_fd(struct input *input,
3614 const char *mime_type, int fd)
3616 if (input->drag_offer)
3617 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3623 input_receive_selection_data(struct input *input, const char *mime_type,
3624 data_func_t func, void *data)
3628 if (input->selection_offer == NULL)
3631 for (p = input->selection_offer->types.data; *p; p++)
3632 if (strcmp(mime_type, *p) == 0)
3638 data_offer_receive_data(input->selection_offer,
3639 mime_type, func, data);
3644 input_receive_selection_data_to_fd(struct input *input,
3645 const char *mime_type, int fd)
3647 if (input->selection_offer)
3648 wl_data_offer_receive(input->selection_offer->offer,
3655 window_move(struct window *window, struct input *input, uint32_t serial)
3657 if (!window->shell_surface)
3660 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3664 surface_set_synchronized(struct surface *surface)
3666 if (!surface->subsurface)
3669 if (surface->synchronized)
3672 wl_subsurface_set_sync(surface->subsurface);
3673 surface->synchronized = 1;
3677 surface_set_synchronized_default(struct surface *surface)
3679 if (!surface->subsurface)
3682 if (surface->synchronized == surface->synchronized_default)
3685 if (surface->synchronized_default)
3686 wl_subsurface_set_sync(surface->subsurface);
3688 wl_subsurface_set_desync(surface->subsurface);
3690 surface->synchronized = surface->synchronized_default;
3694 surface_resize(struct surface *surface)
3696 struct widget *widget = surface->widget;
3697 struct wl_compositor *compositor = widget->window->display->compositor;
3699 if (surface->input_region) {
3700 wl_region_destroy(surface->input_region);
3701 surface->input_region = NULL;
3704 if (surface->opaque_region)
3705 wl_region_destroy(surface->opaque_region);
3707 surface->opaque_region = wl_compositor_create_region(compositor);
3709 if (widget->resize_handler)
3710 widget->resize_handler(widget,
3711 widget->allocation.width,
3712 widget->allocation.height,
3715 if (surface->subsurface &&
3716 (surface->allocation.x != widget->allocation.x ||
3717 surface->allocation.y != widget->allocation.y)) {
3718 wl_subsurface_set_position(surface->subsurface,
3719 widget->allocation.x,
3720 widget->allocation.y);
3722 if (surface->allocation.width != widget->allocation.width ||
3723 surface->allocation.height != widget->allocation.height) {
3724 window_schedule_redraw(widget->window);
3726 surface->allocation = widget->allocation;
3729 wl_region_add(surface->opaque_region, 0, 0,
3730 widget->allocation.width,
3731 widget->allocation.height);
3735 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3738 * This hack should be removed, when EGL respects
3739 * eglSwapInterval(0).
3741 * If this window has sub-surfaces, especially a free-running
3742 * EGL-widget, we need to post the parent surface once with
3743 * all the old state to guarantee, that the EGL-widget will
3744 * receive its frame callback soon. Otherwise, a forced call
3745 * to eglSwapBuffers may end up blocking, waiting for a frame
3746 * event that will never come, because we will commit the parent
3747 * surface with all new state only after eglSwapBuffers returns.
3749 * This assumes, that:
3750 * 1. When the EGL widget's resize hook is called, it pauses.
3751 * 2. When the EGL widget's redraw hook is called, it forces a
3752 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3753 * In a single threaded application condition 1 is a no-op.
3755 * XXX: This should actually be after the surface_resize() calls,
3756 * but cannot, because then it would commit the incomplete state
3757 * accumulated from the widget resize hooks.
3759 if (window->subsurface_list.next != &window->main_surface->link ||
3760 window->subsurface_list.prev != &window->main_surface->link)
3761 wl_surface_commit(window->main_surface->surface);
3765 window_do_resize(struct window *window)
3767 struct surface *surface;
3769 widget_set_allocation(window->main_surface->widget,
3770 window->pending_allocation.x,
3771 window->pending_allocation.y,
3772 window->pending_allocation.width,
3773 window->pending_allocation.height);
3775 surface_resize(window->main_surface);
3777 /* The main surface is in the list, too. Main surface's
3778 * resize_handler is responsible for calling widget_set_allocation()
3779 * on all sub-surface root widgets, so they will be resized
3782 wl_list_for_each(surface, &window->subsurface_list, link) {
3783 if (surface == window->main_surface)
3786 surface_set_synchronized(surface);
3787 surface_resize(surface);
3792 idle_resize(struct window *window)
3794 window->resize_needed = 0;
3795 window->redraw_needed = 1;
3797 DBG("from %dx%d to %dx%d\n",
3798 window->main_surface->server_allocation.width,
3799 window->main_surface->server_allocation.height,
3800 window->pending_allocation.width,
3801 window->pending_allocation.height);
3803 hack_prevent_EGL_sub_surface_deadlock(window);
3805 window_do_resize(window);
3809 undo_resize(struct window *window)
3811 window->pending_allocation.width =
3812 window->main_surface->server_allocation.width;
3813 window->pending_allocation.height =
3814 window->main_surface->server_allocation.height;
3816 DBG("back to %dx%d\n",
3817 window->main_surface->server_allocation.width,
3818 window->main_surface->server_allocation.height);
3820 window_do_resize(window);
3822 if (window->pending_allocation.width == 0 &&
3823 window->pending_allocation.height == 0) {
3824 fprintf(stderr, "Error: Could not draw a surface, "
3825 "most likely due to insufficient disk space in "
3826 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3832 window_schedule_resize(struct window *window, int width, int height)
3834 /* We should probably get these numbers from the theme. */
3835 const int min_width = 200, min_height = 200;
3837 window->pending_allocation.x = 0;
3838 window->pending_allocation.y = 0;
3839 window->pending_allocation.width = width;
3840 window->pending_allocation.height = height;
3842 if (window->min_allocation.width == 0) {
3843 if (width < min_width && window->frame)
3844 window->min_allocation.width = min_width;
3846 window->min_allocation.width = width;
3847 if (height < min_height && window->frame)
3848 window->min_allocation.height = min_height;
3850 window->min_allocation.height = height;
3853 if (window->pending_allocation.width < window->min_allocation.width)
3854 window->pending_allocation.width = window->min_allocation.width;
3855 if (window->pending_allocation.height < window->min_allocation.height)
3856 window->pending_allocation.height = window->min_allocation.height;
3858 window->resize_needed = 1;
3859 window_schedule_redraw(window);
3863 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3865 window_schedule_resize(widget->window, width, height);
3869 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3872 wl_shell_surface_pong(shell_surface, serial);
3876 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3877 uint32_t edges, int32_t width, int32_t height)
3879 struct window *window = data;
3881 window->resize_edges = edges;
3882 window_schedule_resize(window, width, height);
3886 menu_destroy(struct menu *menu)
3888 widget_destroy(menu->widget);
3889 window_destroy(menu->window);
3890 frame_destroy(menu->frame);
3895 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3897 struct window *window = data;
3898 struct menu *menu = window->main_surface->widget->user_data;
3900 /* FIXME: Need more context in this event, at least the input
3901 * device. Or just use wl_callback. And this really needs to
3902 * be a window vfunc that the menu can set. And we need the
3905 input_ungrab(menu->input);
3909 static const struct wl_shell_surface_listener shell_surface_listener = {
3916 window_get_allocation(struct window *window,
3917 struct rectangle *allocation)
3919 *allocation = window->main_surface->allocation;
3923 widget_redraw(struct widget *widget)
3925 struct widget *child;
3927 if (widget->redraw_handler)
3928 widget->redraw_handler(widget, widget->user_data);
3929 wl_list_for_each(child, &widget->child_list, link)
3930 widget_redraw(child);
3934 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3936 struct surface *surface = data;
3938 assert(callback == surface->frame_cb);
3939 DBG_OBJ(callback, "done\n");
3940 wl_callback_destroy(callback);
3941 surface->frame_cb = NULL;
3943 surface->last_time = time;
3945 if (surface->redraw_needed || surface->window->redraw_needed) {
3946 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3947 window_schedule_redraw_task(surface->window);
3951 static const struct wl_callback_listener listener = {
3956 surface_redraw(struct surface *surface)
3958 DBG_OBJ(surface->surface, "begin\n");
3960 if (!surface->window->redraw_needed && !surface->redraw_needed)
3963 /* Whole-window redraw forces a redraw even if the previous has
3964 * not yet hit the screen.
3966 if (surface->frame_cb) {
3967 if (!surface->window->redraw_needed)
3970 DBG_OBJ(surface->frame_cb, "cancelled\n");
3971 wl_callback_destroy(surface->frame_cb);
3974 if (!widget_get_cairo_surface(surface->widget)) {
3975 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3979 surface->frame_cb = wl_surface_frame(surface->surface);
3980 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3981 DBG_OBJ(surface->frame_cb, "new\n");
3983 surface->redraw_needed = 0;
3984 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3985 widget_redraw(surface->widget);
3986 DBG_OBJ(surface->surface, "done\n");
3991 idle_redraw(struct task *task, uint32_t events)
3993 struct window *window = container_of(task, struct window, redraw_task);
3994 struct surface *surface;
3998 DBG(" --------- \n");
4000 wl_list_init(&window->redraw_task.link);
4001 window->redraw_task_scheduled = 0;
4003 if (window->resize_needed) {
4004 /* throttle resizing to the main surface display */
4005 if (window->main_surface->frame_cb) {
4006 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4010 idle_resize(window);
4014 if (surface_redraw(window->main_surface) < 0) {
4016 * Only main_surface failure will cause us to undo the resize.
4017 * If sub-surfaces fail, they will just be broken with old
4022 wl_list_for_each(surface, &window->subsurface_list, link) {
4023 if (surface == window->main_surface)
4026 surface_redraw(surface);
4030 window->redraw_needed = 0;
4031 window_flush(window);
4033 wl_list_for_each(surface, &window->subsurface_list, link)
4034 surface_set_synchronized_default(surface);
4036 if (resized && failed) {
4037 /* Restore widget tree to correspond to what is on screen. */
4038 undo_resize(window);
4043 window_schedule_redraw_task(struct window *window)
4045 if (window->configure_requests)
4047 if (!window->redraw_task_scheduled) {
4048 window->redraw_task.run = idle_redraw;
4049 display_defer(window->display, &window->redraw_task);
4050 window->redraw_task_scheduled = 1;
4055 window_schedule_redraw(struct window *window)
4057 struct surface *surface;
4059 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4061 wl_list_for_each(surface, &window->subsurface_list, link)
4062 surface->redraw_needed = 1;
4064 window_schedule_redraw_task(window);
4068 window_is_fullscreen(struct window *window)
4070 return window->type == TYPE_FULLSCREEN;
4074 window_is_transient(struct window *window)
4076 return window->type == TYPE_TRANSIENT;
4080 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4082 struct window *window = data;
4084 wl_callback_destroy(callback);
4085 window->configure_requests--;
4087 if (!window->configure_requests)
4088 window_schedule_redraw(window);
4091 static struct wl_callback_listener configure_request_listener = {
4092 configure_request_completed,
4096 window_defer_redraw_until_configure(struct window* window)
4098 struct wl_callback *callback;
4100 if (window->redraw_task_scheduled) {
4101 wl_list_remove(&window->redraw_task.link);
4102 window->redraw_task_scheduled = 0;
4105 callback = wl_display_sync(window->display->display);
4106 wl_callback_add_listener(callback, &configure_request_listener, window);
4107 window->configure_requests++;
4111 window_set_fullscreen(struct window *window, int fullscreen)
4113 if (!window->display->shell)
4116 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4120 window->saved_type = window->type;
4121 if (window->type == TYPE_TOPLEVEL) {
4122 window->saved_allocation = window->main_surface->allocation;
4124 window->type = TYPE_FULLSCREEN;
4125 wl_shell_surface_set_fullscreen(window->shell_surface,
4126 window->fullscreen_method,
4128 window_defer_redraw_until_configure (window);
4130 if (window->saved_type == TYPE_MAXIMIZED) {
4131 window_set_maximized(window, 1);
4133 window->type = TYPE_TOPLEVEL;
4134 wl_shell_surface_set_toplevel(window->shell_surface);
4135 window_schedule_resize(window,
4136 window->saved_allocation.width,
4137 window->saved_allocation.height);
4144 window_set_fullscreen_method(struct window *window,
4145 enum wl_shell_surface_fullscreen_method method)
4147 window->fullscreen_method = method;
4151 window_is_maximized(struct window *window)
4153 return window->type == TYPE_MAXIMIZED;
4157 window_set_maximized(struct window *window, int maximized)
4159 if (!window->display->shell)
4162 if ((window->type == TYPE_MAXIMIZED) == maximized)
4165 if (window->type == TYPE_TOPLEVEL) {
4166 window->saved_allocation = window->main_surface->allocation;
4167 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4168 window->type = TYPE_MAXIMIZED;
4169 window_defer_redraw_until_configure(window);
4170 } else if (window->type == TYPE_FULLSCREEN) {
4171 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4172 window->type = TYPE_MAXIMIZED;
4173 window_defer_redraw_until_configure(window);
4175 wl_shell_surface_set_toplevel(window->shell_surface);
4176 window->type = TYPE_TOPLEVEL;
4177 window_schedule_resize(window,
4178 window->saved_allocation.width,
4179 window->saved_allocation.height);
4184 window_set_user_data(struct window *window, void *data)
4186 window->user_data = data;
4190 window_get_user_data(struct window *window)
4192 return window->user_data;
4196 window_set_key_handler(struct window *window,
4197 window_key_handler_t handler)
4199 window->key_handler = handler;
4203 window_set_keyboard_focus_handler(struct window *window,
4204 window_keyboard_focus_handler_t handler)
4206 window->keyboard_focus_handler = handler;
4210 window_set_data_handler(struct window *window, window_data_handler_t handler)
4212 window->data_handler = handler;
4216 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4218 window->drop_handler = handler;
4222 window_set_close_handler(struct window *window,
4223 window_close_handler_t handler)
4225 window->close_handler = handler;
4229 window_set_fullscreen_handler(struct window *window,
4230 window_fullscreen_handler_t handler)
4232 window->fullscreen_handler = handler;
4236 window_set_output_handler(struct window *window,
4237 window_output_handler_t handler)
4239 window->output_handler = handler;
4243 window_set_title(struct window *window, const char *title)
4245 free(window->title);
4246 window->title = strdup(title);
4247 if (window->frame) {
4248 frame_set_title(window->frame->frame, title);
4249 widget_schedule_redraw(window->frame->widget);
4251 if (window->shell_surface)
4252 wl_shell_surface_set_title(window->shell_surface, title);
4256 window_get_title(struct window *window)
4258 return window->title;
4262 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4264 struct text_cursor_position *text_cursor_position =
4265 window->display->text_cursor_position;
4267 if (!text_cursor_position)
4270 text_cursor_position_notify(text_cursor_position,
4271 window->main_surface->surface,
4272 wl_fixed_from_int(x),
4273 wl_fixed_from_int(y));
4277 window_damage(struct window *window, int32_t x, int32_t y,
4278 int32_t width, int32_t height)
4280 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4284 surface_enter(void *data,
4285 struct wl_surface *wl_surface, struct wl_output *wl_output)
4287 struct window *window = data;
4288 struct output *output;
4289 struct output *output_found = NULL;
4290 struct window_output *window_output;
4292 wl_list_for_each(output, &window->display->output_list, link) {
4293 if (output->output == wl_output) {
4294 output_found = output;
4302 window_output = xmalloc(sizeof *window_output);
4303 window_output->output = output_found;
4305 wl_list_insert (&window->window_output_list, &window_output->link);
4307 if (window->output_handler)
4308 window->output_handler(window, output_found, 1,
4313 surface_leave(void *data,
4314 struct wl_surface *wl_surface, struct wl_output *output)
4316 struct window *window = data;
4317 struct window_output *window_output;
4318 struct window_output *window_output_found = NULL;
4320 wl_list_for_each(window_output, &window->window_output_list, link) {
4321 if (window_output->output->output == output) {
4322 window_output_found = window_output;
4327 if (window_output_found) {
4328 wl_list_remove(&window_output_found->link);
4330 if (window->output_handler)
4331 window->output_handler(window, window_output->output,
4332 0, window->user_data);
4334 free(window_output_found);
4338 static const struct wl_surface_listener surface_listener = {
4343 static struct surface *
4344 surface_create(struct window *window)
4346 struct display *display = window->display;
4347 struct surface *surface;
4349 surface = xmalloc(sizeof *surface);
4350 memset(surface, 0, sizeof *surface);
4354 surface->window = window;
4355 surface->surface = wl_compositor_create_surface(display->compositor);
4356 surface->buffer_scale = 1;
4357 wl_surface_add_listener(surface->surface, &surface_listener, window);
4359 wl_list_insert(&window->subsurface_list, &surface->link);
4364 static struct window *
4365 window_create_internal(struct display *display, int type)
4367 struct window *window;
4368 struct surface *surface;
4370 window = xzalloc(sizeof *window);
4371 wl_list_init(&window->subsurface_list);
4372 window->display = display;
4374 surface = surface_create(window);
4375 window->main_surface = surface;
4377 if (type != TYPE_CUSTOM && display->shell) {
4378 window->shell_surface =
4379 wl_shell_get_shell_surface(display->shell,
4381 fail_on_null(window->shell_surface);
4384 window->type = type;
4385 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4386 window->configure_requests = 0;
4387 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4389 if (display->argb_device)
4390 #ifdef HAVE_CAIRO_EGL
4391 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4393 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4396 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4398 wl_surface_set_user_data(surface->surface, window);
4399 wl_list_insert(display->window_list.prev, &window->link);
4400 wl_list_init(&window->redraw_task.link);
4402 if (window->shell_surface) {
4403 wl_shell_surface_set_user_data(window->shell_surface, window);
4404 wl_shell_surface_add_listener(window->shell_surface,
4405 &shell_surface_listener, window);
4408 wl_list_init (&window->window_output_list);
4414 window_create(struct display *display)
4416 return window_create_internal(display, TYPE_NONE);
4420 window_create_custom(struct display *display)
4422 return window_create_internal(display, TYPE_CUSTOM);
4426 window_create_transient(struct display *display, struct window *parent,
4427 int32_t x, int32_t y, uint32_t flags)
4429 struct window *window;
4431 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4437 wl_shell_surface_set_transient(
4438 window->shell_surface,
4439 parent->main_surface->surface,
4440 window->x, window->y, flags);
4446 menu_set_item(struct menu *menu, int sy)
4448 int32_t x, y, width, height;
4451 frame_interior(menu->frame, &x, &y, &width, &height);
4452 next = (sy - y) / 20;
4453 if (menu->current != next) {
4454 menu->current = next;
4455 widget_schedule_redraw(menu->widget);
4460 menu_motion_handler(struct widget *widget,
4461 struct input *input, uint32_t time,
4462 float x, float y, void *data)
4464 struct menu *menu = data;
4466 if (widget == menu->widget)
4467 menu_set_item(data, y);
4469 return CURSOR_LEFT_PTR;
4473 menu_enter_handler(struct widget *widget,
4474 struct input *input, float x, float y, void *data)
4476 struct menu *menu = data;
4478 if (widget == menu->widget)
4479 menu_set_item(data, y);
4481 return CURSOR_LEFT_PTR;
4485 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4487 struct menu *menu = data;
4489 if (widget == menu->widget)
4490 menu_set_item(data, -200);
4494 menu_button_handler(struct widget *widget,
4495 struct input *input, uint32_t time,
4496 uint32_t button, enum wl_pointer_button_state state,
4500 struct menu *menu = data;
4502 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4503 (menu->release_count > 0 || time - menu->time > 500)) {
4504 /* Either relase after press-drag-release or
4505 * click-motion-click. */
4506 menu->func(menu->parent, input,
4507 menu->current, menu->parent->user_data);
4508 input_ungrab(input);
4510 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4511 menu->release_count++;
4516 menu_redraw_handler(struct widget *widget, void *data)
4519 struct menu *menu = data;
4520 int32_t x, y, width, height, i;
4522 cr = widget_cairo_create(widget);
4524 frame_repaint(menu->frame, cr);
4525 frame_interior(menu->frame, &x, &y, &width, &height);
4527 theme_set_background_source(menu->window->display->theme,
4528 cr, THEME_FRAME_ACTIVE);
4529 cairo_rectangle(cr, x, y, width, height);
4532 cairo_select_font_face(cr, "sans",
4533 CAIRO_FONT_SLANT_NORMAL,
4534 CAIRO_FONT_WEIGHT_NORMAL);
4535 cairo_set_font_size(cr, 12);
4537 for (i = 0; i < menu->count; i++) {
4538 if (i == menu->current) {
4539 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4540 cairo_rectangle(cr, x, y + i * 20, width, 20);
4542 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4543 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4544 cairo_show_text(cr, menu->entries[i]);
4546 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4547 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4548 cairo_show_text(cr, menu->entries[i]);
4556 window_show_menu(struct display *display,
4557 struct input *input, uint32_t time, struct window *parent,
4558 int32_t x, int32_t y,
4559 menu_func_t func, const char **entries, int count)
4561 struct window *window;
4565 menu = malloc(sizeof *menu);
4569 window = window_create_internal(parent->display, TYPE_MENU);
4575 menu->window = window;
4576 menu->parent = parent;
4577 menu->widget = window_add_widget(menu->window, menu);
4578 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4579 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4580 menu->frame = frame_create(window->display->theme, 0, 0,
4581 FRAME_BUTTON_NONE, NULL);
4582 menu->entries = entries;
4583 menu->count = count;
4584 menu->release_count = 0;
4588 menu->input = input;
4589 window->type = TYPE_MENU;
4593 input_ungrab(input);
4595 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4596 widget_set_enter_handler(menu->widget, menu_enter_handler);
4597 widget_set_leave_handler(menu->widget, menu_leave_handler);
4598 widget_set_motion_handler(menu->widget, menu_motion_handler);
4599 widget_set_button_handler(menu->widget, menu_button_handler);
4601 input_grab(input, menu->widget, 0);
4602 frame_resize_inside(menu->frame, 200, count * 20);
4603 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4604 window_schedule_resize(window, frame_width(menu->frame),
4605 frame_height(menu->frame));
4607 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4608 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4609 display_get_serial(window->display),
4610 parent->main_surface->surface,
4611 window->x - ix, window->y - iy, 0);
4615 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4617 window->main_surface->buffer_type = type;
4621 window_set_preferred_format(struct window *window,
4622 enum preferred_format format)
4624 window->preferred_format = format;
4628 window_add_subsurface(struct window *window, void *data,
4629 enum subsurface_mode default_mode)
4631 struct widget *widget;
4632 struct surface *surface;
4633 struct wl_surface *parent;
4634 struct wl_subcompositor *subcompo = window->display->subcompositor;
4636 surface = surface_create(window);
4637 widget = widget_create(window, surface, data);
4638 wl_list_init(&widget->link);
4639 surface->widget = widget;
4641 parent = window->main_surface->surface;
4642 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4645 surface->synchronized = 1;
4647 switch (default_mode) {
4648 case SUBSURFACE_SYNCHRONIZED:
4649 surface->synchronized_default = 1;
4651 case SUBSURFACE_DESYNCHRONIZED:
4652 surface->synchronized_default = 0;
4655 assert(!"bad enum subsurface_mode");
4662 display_handle_geometry(void *data,
4663 struct wl_output *wl_output,
4666 int physical_height,
4672 struct output *output = data;
4674 output->allocation.x = x;
4675 output->allocation.y = y;
4676 output->transform = transform;
4680 display_handle_done(void *data,
4681 struct wl_output *wl_output)
4686 display_handle_scale(void *data,
4687 struct wl_output *wl_output,
4690 struct output *output = data;
4692 output->scale = scale;
4696 display_handle_mode(void *data,
4697 struct wl_output *wl_output,
4703 struct output *output = data;
4704 struct display *display = output->display;
4706 if (flags & WL_OUTPUT_MODE_CURRENT) {
4707 output->allocation.width = width;
4708 output->allocation.height = height;
4709 if (display->output_configure_handler)
4710 (*display->output_configure_handler)(
4711 output, display->user_data);
4715 static const struct wl_output_listener output_listener = {
4716 display_handle_geometry,
4717 display_handle_mode,
4718 display_handle_done,
4719 display_handle_scale
4723 display_add_output(struct display *d, uint32_t id)
4725 struct output *output;
4727 output = xzalloc(sizeof *output);
4728 output->display = d;
4731 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4732 output->server_output_id = id;
4733 wl_list_insert(d->output_list.prev, &output->link);
4735 wl_output_add_listener(output->output, &output_listener, output);
4739 output_destroy(struct output *output)
4741 if (output->destroy_handler)
4742 (*output->destroy_handler)(output, output->user_data);
4744 wl_output_destroy(output->output);
4745 wl_list_remove(&output->link);
4750 display_destroy_output(struct display *d, uint32_t id)
4752 struct output *output;
4754 wl_list_for_each(output, &d->output_list, link) {
4755 if (output->server_output_id == id) {
4756 output_destroy(output);
4763 display_set_global_handler(struct display *display,
4764 display_global_handler_t handler)
4766 struct global *global;
4768 display->global_handler = handler;
4772 wl_list_for_each(global, &display->global_list, link)
4773 display->global_handler(display,
4774 global->name, global->interface,
4775 global->version, display->user_data);
4779 display_set_global_handler_remove(struct display *display,
4780 display_global_handler_t remove_handler)
4782 display->global_handler_remove = remove_handler;
4783 if (!remove_handler)
4788 display_set_output_configure_handler(struct display *display,
4789 display_output_handler_t handler)
4791 struct output *output;
4793 display->output_configure_handler = handler;
4797 wl_list_for_each(output, &display->output_list, link) {
4798 if (output->allocation.width == 0 &&
4799 output->allocation.height == 0)
4802 (*display->output_configure_handler)(output,
4803 display->user_data);
4808 output_set_user_data(struct output *output, void *data)
4810 output->user_data = data;
4814 output_get_user_data(struct output *output)
4816 return output->user_data;
4820 output_set_destroy_handler(struct output *output,
4821 display_output_handler_t handler)
4823 output->destroy_handler = handler;
4824 /* FIXME: implement this, once we have way to remove outputs */
4828 output_get_allocation(struct output *output, struct rectangle *base)
4830 struct rectangle allocation = output->allocation;
4832 switch (output->transform) {
4833 case WL_OUTPUT_TRANSFORM_90:
4834 case WL_OUTPUT_TRANSFORM_270:
4835 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4836 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4837 /* Swap width and height */
4838 allocation.width = output->allocation.height;
4839 allocation.height = output->allocation.width;
4847 output_get_wl_output(struct output *output)
4849 return output->output;
4852 enum wl_output_transform
4853 output_get_transform(struct output *output)
4855 return output->transform;
4859 output_get_scale(struct output *output)
4861 return output->scale;
4865 fini_xkb(struct input *input)
4867 xkb_state_unref(input->xkb.state);
4868 xkb_map_unref(input->xkb.keymap);
4871 #define MIN(a,b) ((a) < (b) ? a : b)
4874 display_add_input(struct display *d, uint32_t id)
4876 struct input *input;
4878 input = xzalloc(sizeof *input);
4880 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4881 MIN(d->seat_version, 3));
4882 input->touch_focus = NULL;
4883 input->pointer_focus = NULL;
4884 input->keyboard_focus = NULL;
4885 wl_list_init(&input->touch_point_list);
4886 wl_list_insert(d->input_list.prev, &input->link);
4888 wl_seat_add_listener(input->seat, &seat_listener, input);
4889 wl_seat_set_user_data(input->seat, input);
4891 input->data_device =
4892 wl_data_device_manager_get_data_device(d->data_device_manager,
4894 wl_data_device_add_listener(input->data_device, &data_device_listener,
4897 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4899 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4900 TFD_CLOEXEC | TFD_NONBLOCK);
4901 input->repeat_task.run = keyboard_repeat_func;
4902 display_watch_fd(d, input->repeat_timer_fd,
4903 EPOLLIN, &input->repeat_task);
4907 input_destroy(struct input *input)
4909 input_remove_keyboard_focus(input);
4910 input_remove_pointer_focus(input);
4912 if (input->drag_offer)
4913 data_offer_destroy(input->drag_offer);
4915 if (input->selection_offer)
4916 data_offer_destroy(input->selection_offer);
4918 wl_data_device_destroy(input->data_device);
4920 if (input->display->seat_version >= 3) {
4922 wl_pointer_release(input->pointer);
4923 if (input->keyboard)
4924 wl_keyboard_release(input->keyboard);
4929 wl_surface_destroy(input->pointer_surface);
4931 wl_list_remove(&input->link);
4932 wl_seat_destroy(input->seat);
4933 close(input->repeat_timer_fd);
4938 init_workspace_manager(struct display *d, uint32_t id)
4940 d->workspace_manager =
4941 wl_registry_bind(d->registry, id,
4942 &workspace_manager_interface, 1);
4943 if (d->workspace_manager != NULL)
4944 workspace_manager_add_listener(d->workspace_manager,
4945 &workspace_manager_listener,
4950 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4952 struct display *d = data;
4954 if (format == WL_SHM_FORMAT_RGB565)
4958 struct wl_shm_listener shm_listener = {
4963 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4964 const char *interface, uint32_t version)
4966 struct display *d = data;
4967 struct global *global;
4969 global = xmalloc(sizeof *global);
4971 global->interface = strdup(interface);
4972 global->version = version;
4973 wl_list_insert(d->global_list.prev, &global->link);
4975 if (strcmp(interface, "wl_compositor") == 0) {
4976 d->compositor = wl_registry_bind(registry, id,
4977 &wl_compositor_interface, 3);
4978 } else if (strcmp(interface, "wl_output") == 0) {
4979 display_add_output(d, id);
4980 } else if (strcmp(interface, "wl_seat") == 0) {
4981 d->seat_version = version;
4982 display_add_input(d, id);
4983 } else if (strcmp(interface, "wl_shell") == 0) {
4984 d->shell = wl_registry_bind(registry,
4985 id, &wl_shell_interface, 1);
4986 } else if (strcmp(interface, "wl_shm") == 0) {
4987 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4988 wl_shm_add_listener(d->shm, &shm_listener, d);
4989 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4990 d->data_device_manager =
4991 wl_registry_bind(registry, id,
4992 &wl_data_device_manager_interface, 1);
4993 } else if (strcmp(interface, "text_cursor_position") == 0) {
4994 d->text_cursor_position =
4995 wl_registry_bind(registry, id,
4996 &text_cursor_position_interface, 1);
4997 } else if (strcmp(interface, "workspace_manager") == 0) {
4998 init_workspace_manager(d, id);
4999 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5001 wl_registry_bind(registry, id,
5002 &wl_subcompositor_interface, 1);
5005 if (d->global_handler)
5006 d->global_handler(d, id, interface, version, d->user_data);
5010 registry_handle_global_remove(void *data, struct wl_registry *registry,
5013 struct display *d = data;
5014 struct global *global;
5017 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5018 if (global->name != name)
5021 if (strcmp(global->interface, "wl_output") == 0)
5022 display_destroy_output(d, name);
5024 /* XXX: Should destroy remaining bound globals */
5026 if (d->global_handler_remove)
5027 d->global_handler_remove(d, name, global->interface,
5028 global->version, d->user_data);
5030 wl_list_remove(&global->link);
5031 free(global->interface);
5037 display_bind(struct display *display, uint32_t name,
5038 const struct wl_interface *interface, uint32_t version)
5040 return wl_registry_bind(display->registry, name, interface, version);
5043 static const struct wl_registry_listener registry_listener = {
5044 registry_handle_global,
5045 registry_handle_global_remove
5048 #ifdef HAVE_CAIRO_EGL
5050 init_egl(struct display *d)
5052 EGLint major, minor;
5055 #ifdef USE_CAIRO_GLESV2
5056 # define GL_BIT EGL_OPENGL_ES2_BIT
5058 # define GL_BIT EGL_OPENGL_BIT
5061 static const EGLint argb_cfg_attribs[] = {
5062 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5068 EGL_RENDERABLE_TYPE, GL_BIT,
5072 #ifdef USE_CAIRO_GLESV2
5073 static const EGLint context_attribs[] = {
5074 EGL_CONTEXT_CLIENT_VERSION, 2,
5077 EGLint api = EGL_OPENGL_ES_API;
5079 EGLint *context_attribs = NULL;
5080 EGLint api = EGL_OPENGL_API;
5083 d->dpy = eglGetDisplay(d->display);
5084 if (!eglInitialize(d->dpy, &major, &minor)) {
5085 fprintf(stderr, "failed to initialize EGL\n");
5089 if (!eglBindAPI(api)) {
5090 fprintf(stderr, "failed to bind EGL client API\n");
5094 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5095 &d->argb_config, 1, &n) || n != 1) {
5096 fprintf(stderr, "failed to choose argb EGL config\n");
5100 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5101 EGL_NO_CONTEXT, context_attribs);
5102 if (d->argb_ctx == NULL) {
5103 fprintf(stderr, "failed to create EGL context\n");
5107 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5108 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5109 fprintf(stderr, "failed to get cairo EGL argb device\n");
5117 fini_egl(struct display *display)
5119 cairo_device_destroy(display->argb_device);
5121 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5124 eglTerminate(display->dpy);
5130 init_dummy_surface(struct display *display)
5135 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5137 display->dummy_surface =
5138 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5140 display->dummy_surface_data = data;
5144 handle_display_data(struct task *task, uint32_t events)
5146 struct display *display =
5147 container_of(task, struct display, display_task);
5148 struct epoll_event ep;
5151 display->display_fd_events = events;
5153 if (events & EPOLLERR || events & EPOLLHUP) {
5154 display_exit(display);
5158 if (events & EPOLLIN) {
5159 ret = wl_display_dispatch(display->display);
5161 display_exit(display);
5166 if (events & EPOLLOUT) {
5167 ret = wl_display_flush(display->display);
5169 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5170 ep.data.ptr = &display->display_task;
5171 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5172 display->display_fd, &ep);
5173 } else if (ret == -1 && errno != EAGAIN) {
5174 display_exit(display);
5181 log_handler(const char *format, va_list args)
5183 vfprintf(stderr, format, args);
5187 display_create(int *argc, char *argv[])
5191 wl_log_set_handler_client(log_handler);
5193 d = zalloc(sizeof *d);
5197 d->display = wl_display_connect(NULL);
5198 if (d->display == NULL) {
5199 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5204 d->xkb_context = xkb_context_new(0);
5205 if (d->xkb_context == NULL) {
5206 fprintf(stderr, "Failed to create XKB context\n");
5211 d->epoll_fd = os_epoll_create_cloexec();
5212 d->display_fd = wl_display_get_fd(d->display);
5213 d->display_task.run = handle_display_data;
5214 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5217 wl_list_init(&d->deferred_list);
5218 wl_list_init(&d->input_list);
5219 wl_list_init(&d->output_list);
5220 wl_list_init(&d->global_list);
5223 d->workspace_count = 1;
5225 d->registry = wl_display_get_registry(d->display);
5226 wl_registry_add_listener(d->registry, ®istry_listener, d);
5228 if (wl_display_dispatch(d->display) < 0) {
5229 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5233 #ifdef HAVE_CAIRO_EGL
5234 if (init_egl(d) < 0)
5235 fprintf(stderr, "EGL does not seem to work, "
5236 "falling back to software rendering and wl_shm.\n");
5241 d->theme = theme_create();
5243 wl_list_init(&d->window_list);
5245 init_dummy_surface(d);
5251 display_destroy_outputs(struct display *display)
5254 struct output *output;
5256 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5257 output_destroy(output);
5261 display_destroy_inputs(struct display *display)
5264 struct input *input;
5266 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5267 input_destroy(input);
5271 display_destroy(struct display *display)
5273 if (!wl_list_empty(&display->window_list))
5274 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5275 wl_list_length(&display->window_list));
5277 if (!wl_list_empty(&display->deferred_list))
5278 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5280 cairo_surface_destroy(display->dummy_surface);
5281 free(display->dummy_surface_data);
5283 display_destroy_outputs(display);
5284 display_destroy_inputs(display);
5286 xkb_context_unref(display->xkb_context);
5288 theme_destroy(display->theme);
5289 destroy_cursors(display);
5291 #ifdef HAVE_CAIRO_EGL
5292 if (display->argb_device)
5296 if (display->subcompositor)
5297 wl_subcompositor_destroy(display->subcompositor);
5300 wl_shell_destroy(display->shell);
5303 wl_shm_destroy(display->shm);
5305 if (display->data_device_manager)
5306 wl_data_device_manager_destroy(display->data_device_manager);
5308 wl_compositor_destroy(display->compositor);
5309 wl_registry_destroy(display->registry);
5311 close(display->epoll_fd);
5313 if (!(display->display_fd_events & EPOLLERR) &&
5314 !(display->display_fd_events & EPOLLHUP))
5315 wl_display_flush(display->display);
5317 wl_display_disconnect(display->display);
5322 display_set_user_data(struct display *display, void *data)
5324 display->user_data = data;
5328 display_get_user_data(struct display *display)
5330 return display->user_data;
5334 display_get_display(struct display *display)
5336 return display->display;
5340 display_has_subcompositor(struct display *display)
5342 if (display->subcompositor)
5345 wl_display_roundtrip(display->display);
5347 return display->subcompositor != NULL;
5351 display_get_cairo_device(struct display *display)
5353 return display->argb_device;
5357 display_get_output(struct display *display)
5359 return container_of(display->output_list.next, struct output, link);
5362 struct wl_compositor *
5363 display_get_compositor(struct display *display)
5365 return display->compositor;
5369 display_get_serial(struct display *display)
5371 return display->serial;
5375 display_get_egl_display(struct display *d)
5380 struct wl_data_source *
5381 display_create_data_source(struct display *display)
5383 return wl_data_device_manager_create_data_source(display->data_device_manager);
5387 display_get_argb_egl_config(struct display *d)
5389 return d->argb_config;
5393 display_acquire_window_surface(struct display *display,
5394 struct window *window,
5397 struct surface *surface = window->main_surface;
5399 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5402 widget_get_cairo_surface(window->main_surface->widget);
5403 return surface->toysurface->acquire(surface->toysurface, ctx);
5407 display_release_window_surface(struct display *display,
5408 struct window *window)
5410 struct surface *surface = window->main_surface;
5412 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5415 surface->toysurface->release(surface->toysurface);
5419 display_defer(struct display *display, struct task *task)
5421 wl_list_insert(&display->deferred_list, &task->link);
5425 display_watch_fd(struct display *display,
5426 int fd, uint32_t events, struct task *task)
5428 struct epoll_event ep;
5432 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5436 display_unwatch_fd(struct display *display, int fd)
5438 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5442 display_run(struct display *display)
5445 struct epoll_event ep[16];
5448 display->running = 1;
5450 while (!wl_list_empty(&display->deferred_list)) {
5451 task = container_of(display->deferred_list.prev,
5453 wl_list_remove(&task->link);
5457 wl_display_dispatch_pending(display->display);
5459 if (!display->running)
5462 ret = wl_display_flush(display->display);
5463 if (ret < 0 && errno == EAGAIN) {
5465 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5466 ep[0].data.ptr = &display->display_task;
5468 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5469 display->display_fd, &ep[0]);
5470 } else if (ret < 0) {
5474 count = epoll_wait(display->epoll_fd,
5475 ep, ARRAY_LENGTH(ep), -1);
5476 for (i = 0; i < count; i++) {
5477 task = ep[i].data.ptr;
5478 task->run(task, ep[i].events);
5484 display_exit(struct display *display)
5486 display->running = 0;
5490 keysym_modifiers_add(struct wl_array *modifiers_map,
5493 size_t len = strlen(name) + 1;
5496 p = wl_array_add(modifiers_map, len);
5501 strncpy(p, name, len);
5504 static xkb_mod_index_t
5505 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5508 xkb_mod_index_t index = 0;
5509 char *p = modifiers_map->data;
5511 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5512 if (strcmp(p, name) == 0)
5519 return XKB_MOD_INVALID;
5523 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5526 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5528 if (index == XKB_MOD_INVALID)
5529 return XKB_MOD_INVALID;
5535 fail_on_null(void *p)
5538 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5548 return fail_on_null(malloc(s));
5554 return fail_on_null(zalloc(s));
5558 xstrdup(const char *s)
5560 return fail_on_null(strdup(s));