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;
146 struct window_output {
147 struct output *output;
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
156 * width,height are the new buffer size.
157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
159 * Returns the Cairo surface to draw to.
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
162 int32_t width, int32_t height, uint32_t flags,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
170 void (*swap)(struct toysurface *base,
171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
172 struct rectangle *server_allocation);
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
181 * Release the toysurface from the EGL context, returning control
184 void (*release)(struct toysurface *base);
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
190 void (*destroy)(struct toysurface *base);
194 struct window *window;
196 struct wl_surface *surface;
197 struct wl_subsurface *subsurface;
199 int synchronized_default;
200 struct toysurface *toysurface;
201 struct widget *widget;
203 struct wl_callback *frame_cb;
206 struct rectangle allocation;
207 struct rectangle server_allocation;
209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
214 int32_t buffer_scale;
216 cairo_surface_t *cairo_surface;
222 struct display *display;
223 struct wl_list window_output_list;
225 struct rectangle saved_allocation;
226 struct rectangle min_allocation;
227 struct rectangle pending_allocation;
231 int redraw_task_scheduled;
232 struct task redraw_task;
239 int fullscreen_method;
240 int configure_requests;
242 enum preferred_format preferred_format;
244 window_key_handler_t key_handler;
245 window_keyboard_focus_handler_t keyboard_focus_handler;
246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
248 window_close_handler_t close_handler;
249 window_fullscreen_handler_t fullscreen_handler;
250 window_output_handler_t output_handler;
252 struct surface *main_surface;
253 struct wl_shell_surface *shell_surface;
255 struct window_frame *frame;
257 /* struct surface::link, contains also main_surface */
258 struct wl_list subsurface_list;
265 struct window *window;
266 struct surface *surface;
267 struct tooltip *tooltip;
268 struct wl_list child_list;
270 struct rectangle allocation;
271 widget_resize_handler_t resize_handler;
272 widget_redraw_handler_t redraw_handler;
273 widget_enter_handler_t enter_handler;
274 widget_leave_handler_t leave_handler;
275 widget_motion_handler_t motion_handler;
276 widget_button_handler_t button_handler;
277 widget_touch_down_handler_t touch_down_handler;
278 widget_touch_up_handler_t touch_up_handler;
279 widget_touch_motion_handler_t touch_motion_handler;
280 widget_touch_frame_handler_t touch_frame_handler;
281 widget_touch_cancel_handler_t touch_cancel_handler;
282 widget_axis_handler_t axis_handler;
287 /* If this is set to false then no cairo surface will be
288 * created before redrawing the surface. This is useful if the
289 * redraw handler is going to do completely custom rendering
290 * such as using EGL directly */
297 struct widget *widget;
302 struct display *display;
303 struct wl_seat *seat;
304 struct wl_pointer *pointer;
305 struct wl_keyboard *keyboard;
306 struct wl_touch *touch;
307 struct wl_list touch_point_list;
308 struct window *pointer_focus;
309 struct window *keyboard_focus;
310 struct window *touch_focus;
312 uint32_t cursor_anim_start;
313 struct wl_callback *cursor_frame_cb;
314 struct wl_surface *pointer_surface;
316 uint32_t pointer_enter_serial;
317 uint32_t cursor_serial;
321 struct widget *focus_widget;
323 uint32_t grab_button;
325 struct wl_data_device *data_device;
326 struct data_offer *drag_offer;
327 struct data_offer *selection_offer;
329 int32_t touch_grab_id;
330 float drag_x, drag_y;
331 struct window *drag_focus;
332 uint32_t drag_enter_serial;
335 struct xkb_keymap *keymap;
336 struct xkb_state *state;
337 xkb_mod_mask_t control_mask;
338 xkb_mod_mask_t alt_mask;
339 xkb_mod_mask_t shift_mask;
342 struct task repeat_task;
346 uint32_t repeat_time;
350 struct display *display;
351 struct wl_output *output;
352 uint32_t server_output_id;
353 struct rectangle allocation;
358 display_output_handler_t destroy_handler;
362 struct window_frame {
363 struct widget *widget;
364 struct widget *child;
369 struct window *window;
370 struct window *parent;
371 struct widget *widget;
374 const char **entries;
383 struct widget *parent;
384 struct widget *widget;
386 struct task tooltip_task;
392 struct wl_shm_pool *pool;
399 CURSOR_DEFAULT = 100,
403 enum window_location {
405 WINDOW_RESIZING_TOP = 1,
406 WINDOW_RESIZING_BOTTOM = 2,
407 WINDOW_RESIZING_LEFT = 4,
408 WINDOW_RESIZING_TOP_LEFT = 5,
409 WINDOW_RESIZING_BOTTOM_LEFT = 6,
410 WINDOW_RESIZING_RIGHT = 8,
411 WINDOW_RESIZING_TOP_RIGHT = 9,
412 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
413 WINDOW_RESIZING_MASK = 15,
414 WINDOW_EXTERIOR = 16,
415 WINDOW_TITLEBAR = 17,
416 WINDOW_CLIENT_AREA = 18,
419 static const cairo_user_data_key_t shm_surface_data_key;
426 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
427 __attribute__ ((format (printf, 4, 5)));
430 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435 gettimeofday(&tv, NULL);
436 fprintf(stderr, "%8ld.%03ld ",
437 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
440 fprintf(stderr, "%s@%d ",
441 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
443 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
444 fprintf(stderr, "%s ", func);
447 vfprintf(stderr, fmt, ap);
451 #define DBG(fmt, ...) \
452 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
454 #define DBG_OBJ(obj, fmt, ...) \
455 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
459 #define DBG(...) do {} while (0)
460 #define DBG_OBJ(...) do {} while (0)
465 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
469 switch (buffer_transform) {
470 case WL_OUTPUT_TRANSFORM_90:
471 case WL_OUTPUT_TRANSFORM_270:
472 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
473 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
482 *width *= buffer_scale;
483 *height *= buffer_scale;
487 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
491 switch (buffer_transform) {
492 case WL_OUTPUT_TRANSFORM_90:
493 case WL_OUTPUT_TRANSFORM_270:
494 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 *width /= buffer_scale;
505 *height /= buffer_scale;
508 #ifdef HAVE_CAIRO_EGL
510 struct egl_window_surface {
511 struct toysurface base;
512 cairo_surface_t *cairo_surface;
513 struct display *display;
514 struct wl_surface *surface;
515 struct wl_egl_window *egl_window;
516 EGLSurface egl_surface;
519 static struct egl_window_surface *
520 to_egl_window_surface(struct toysurface *base)
522 return container_of(base, struct egl_window_surface, base);
525 static cairo_surface_t *
526 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
527 int32_t width, int32_t height, uint32_t flags,
528 enum wl_output_transform buffer_transform, int32_t buffer_scale)
530 struct egl_window_surface *surface = to_egl_window_surface(base);
532 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
534 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
535 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
537 return cairo_surface_reference(surface->cairo_surface);
541 egl_window_surface_swap(struct toysurface *base,
542 enum wl_output_transform buffer_transform, int32_t buffer_scale,
543 struct rectangle *server_allocation)
545 struct egl_window_surface *surface = to_egl_window_surface(base);
547 cairo_gl_surface_swapbuffers(surface->cairo_surface);
548 wl_egl_window_get_attached_size(surface->egl_window,
549 &server_allocation->width,
550 &server_allocation->height);
552 buffer_to_surface_size (buffer_transform, buffer_scale,
553 &server_allocation->width,
554 &server_allocation->height);
558 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
560 struct egl_window_surface *surface = to_egl_window_surface(base);
561 cairo_device_t *device;
563 device = cairo_surface_get_device(surface->cairo_surface);
568 if (device == surface->display->argb_device)
569 ctx = surface->display->argb_ctx;
574 cairo_device_flush(device);
575 cairo_device_acquire(device);
576 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
577 surface->egl_surface, ctx))
578 fprintf(stderr, "failed to make surface current\n");
584 egl_window_surface_release(struct toysurface *base)
586 struct egl_window_surface *surface = to_egl_window_surface(base);
587 cairo_device_t *device;
589 device = cairo_surface_get_device(surface->cairo_surface);
593 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
594 surface->display->argb_ctx))
595 fprintf(stderr, "failed to make context current\n");
597 cairo_device_release(device);
601 egl_window_surface_destroy(struct toysurface *base)
603 struct egl_window_surface *surface = to_egl_window_surface(base);
604 struct display *d = surface->display;
606 cairo_surface_destroy(surface->cairo_surface);
607 eglDestroySurface(d->dpy, surface->egl_surface);
608 wl_egl_window_destroy(surface->egl_window);
609 surface->surface = NULL;
614 static struct toysurface *
615 egl_window_surface_create(struct display *display,
616 struct wl_surface *wl_surface,
618 struct rectangle *rectangle)
620 struct egl_window_surface *surface;
622 if (display->dpy == EGL_NO_DISPLAY)
625 surface = calloc(1, sizeof *surface);
629 surface->base.prepare = egl_window_surface_prepare;
630 surface->base.swap = egl_window_surface_swap;
631 surface->base.acquire = egl_window_surface_acquire;
632 surface->base.release = egl_window_surface_release;
633 surface->base.destroy = egl_window_surface_destroy;
635 surface->display = display;
636 surface->surface = wl_surface;
638 surface->egl_window = wl_egl_window_create(surface->surface,
642 surface->egl_surface = eglCreateWindowSurface(display->dpy,
643 display->argb_config,
647 surface->cairo_surface =
648 cairo_gl_surface_create_for_egl(display->argb_device,
649 surface->egl_surface,
653 return &surface->base;
658 static struct toysurface *
659 egl_window_surface_create(struct display *display,
660 struct wl_surface *wl_surface,
662 struct rectangle *rectangle)
669 struct shm_surface_data {
670 struct wl_buffer *buffer;
671 struct shm_pool *pool;
675 display_get_buffer_for_surface(struct display *display,
676 cairo_surface_t *surface)
678 struct shm_surface_data *data;
680 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
686 shm_pool_destroy(struct shm_pool *pool);
689 shm_surface_data_destroy(void *p)
691 struct shm_surface_data *data = p;
693 wl_buffer_destroy(data->buffer);
695 shm_pool_destroy(data->pool);
700 static struct wl_shm_pool *
701 make_shm_pool(struct display *display, int size, void **data)
703 struct wl_shm_pool *pool;
706 fd = os_create_anonymous_file(size);
708 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
713 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
714 if (*data == MAP_FAILED) {
715 fprintf(stderr, "mmap failed: %m\n");
720 pool = wl_shm_create_pool(display->shm, fd, size);
727 static struct shm_pool *
728 shm_pool_create(struct display *display, size_t size)
730 struct shm_pool *pool = malloc(sizeof *pool);
735 pool->pool = make_shm_pool(display, size, &pool->data);
748 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
750 if (pool->used + size > pool->size)
753 *offset = pool->used;
756 return (char *) pool->data + *offset;
759 /* destroy the pool. this does not unmap the memory though */
761 shm_pool_destroy(struct shm_pool *pool)
763 munmap(pool->data, pool->size);
764 wl_shm_pool_destroy(pool->pool);
768 /* Start allocating from the beginning of the pool again */
770 shm_pool_reset(struct shm_pool *pool)
776 data_length_for_shm_surface(struct rectangle *rect)
780 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
782 return stride * rect->height;
785 static cairo_surface_t *
786 display_create_shm_surface_from_pool(struct display *display,
787 struct rectangle *rectangle,
788 uint32_t flags, struct shm_pool *pool)
790 struct shm_surface_data *data;
792 cairo_surface_t *surface;
793 cairo_format_t cairo_format;
794 int stride, length, offset;
797 data = malloc(sizeof *data);
801 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
802 cairo_format = CAIRO_FORMAT_RGB16_565;
804 cairo_format = CAIRO_FORMAT_ARGB32;
806 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
807 length = stride * rectangle->height;
809 map = shm_pool_allocate(pool, length, &offset);
816 surface = cairo_image_surface_create_for_data (map,
822 cairo_surface_set_user_data(surface, &shm_surface_data_key,
823 data, shm_surface_data_destroy);
825 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
826 format = WL_SHM_FORMAT_RGB565;
828 if (flags & SURFACE_OPAQUE)
829 format = WL_SHM_FORMAT_XRGB8888;
831 format = WL_SHM_FORMAT_ARGB8888;
834 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
842 static cairo_surface_t *
843 display_create_shm_surface(struct display *display,
844 struct rectangle *rectangle, uint32_t flags,
845 struct shm_pool *alternate_pool,
846 struct shm_surface_data **data_ret)
848 struct shm_surface_data *data;
849 struct shm_pool *pool;
850 cairo_surface_t *surface;
852 if (alternate_pool) {
853 shm_pool_reset(alternate_pool);
854 surface = display_create_shm_surface_from_pool(display,
859 data = cairo_surface_get_user_data(surface,
860 &shm_surface_data_key);
865 pool = shm_pool_create(display,
866 data_length_for_shm_surface(rectangle));
871 display_create_shm_surface_from_pool(display, rectangle,
875 shm_pool_destroy(pool);
879 /* make sure we destroy the pool when the surface is destroyed */
880 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
891 check_size(struct rectangle *rect)
893 if (rect->width && rect->height)
896 fprintf(stderr, "tried to create surface of "
897 "width: %d, height: %d\n", rect->width, rect->height);
902 display_create_surface(struct display *display,
903 struct wl_surface *surface,
904 struct rectangle *rectangle,
907 if (check_size(rectangle) < 0)
910 assert(flags & SURFACE_SHM);
911 return display_create_shm_surface(display, rectangle, flags,
915 struct shm_surface_leaf {
916 cairo_surface_t *cairo_surface;
917 /* 'data' is automatically destroyed, when 'cairo_surface' is */
918 struct shm_surface_data *data;
920 struct shm_pool *resize_pool;
925 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
927 if (leaf->cairo_surface)
928 cairo_surface_destroy(leaf->cairo_surface);
929 /* leaf->data already destroyed via cairo private */
931 if (leaf->resize_pool)
932 shm_pool_destroy(leaf->resize_pool);
934 memset(leaf, 0, sizeof *leaf);
940 struct toysurface base;
941 struct display *display;
942 struct wl_surface *surface;
946 struct shm_surface_leaf leaf[MAX_LEAVES];
947 struct shm_surface_leaf *current;
950 static struct shm_surface *
951 to_shm_surface(struct toysurface *base)
953 return container_of(base, struct shm_surface, base);
957 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
960 struct shm_surface_leaf *leaf;
961 char bufs[MAX_LEAVES + 1];
964 for (i = 0; i < MAX_LEAVES; i++) {
965 leaf = &surface->leaf[i];
969 else if (leaf->cairo_surface)
975 bufs[MAX_LEAVES] = '\0';
976 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
981 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
983 struct shm_surface *surface = data;
984 struct shm_surface_leaf *leaf;
988 shm_surface_buffer_state_debug(surface, "buffer_release before");
990 for (i = 0; i < MAX_LEAVES; i++) {
991 leaf = &surface->leaf[i];
992 if (leaf->data && leaf->data->buffer == buffer) {
997 assert(i < MAX_LEAVES && "unknown buffer released");
999 /* Leave one free leaf with storage, release others */
1001 for (i = 0; i < MAX_LEAVES; i++) {
1002 leaf = &surface->leaf[i];
1004 if (!leaf->cairo_surface || leaf->busy)
1010 shm_surface_leaf_release(leaf);
1013 shm_surface_buffer_state_debug(surface, "buffer_release after");
1016 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1017 shm_surface_buffer_release
1020 static cairo_surface_t *
1021 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1022 int32_t width, int32_t height, uint32_t flags,
1023 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1025 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1026 struct shm_surface *surface = to_shm_surface(base);
1027 struct rectangle rect = { 0};
1028 struct shm_surface_leaf *leaf = NULL;
1034 /* pick a free buffer, preferrably one that already has storage */
1035 for (i = 0; i < MAX_LEAVES; i++) {
1036 if (surface->leaf[i].busy)
1039 if (!leaf || surface->leaf[i].cairo_surface)
1040 leaf = &surface->leaf[i];
1042 DBG_OBJ(surface->surface, "pick leaf %d\n",
1043 (int)(leaf - &surface->leaf[0]));
1046 fprintf(stderr, "%s: all buffers are held by the server.\n",
1052 if (!resize_hint && leaf->resize_pool) {
1053 cairo_surface_destroy(leaf->cairo_surface);
1054 leaf->cairo_surface = NULL;
1055 shm_pool_destroy(leaf->resize_pool);
1056 leaf->resize_pool = NULL;
1059 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1061 if (leaf->cairo_surface &&
1062 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1063 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1066 if (leaf->cairo_surface)
1067 cairo_surface_destroy(leaf->cairo_surface);
1069 #ifdef USE_RESIZE_POOL
1070 if (resize_hint && !leaf->resize_pool) {
1071 /* Create a big pool to allocate from, while continuously
1072 * resizing. Mmapping a new pool in the server
1073 * is relatively expensive, so reusing a pool performs
1074 * better, but may temporarily reserve unneeded memory.
1076 /* We should probably base this number on the output size. */
1077 leaf->resize_pool = shm_pool_create(surface->display,
1083 rect.height = height;
1085 leaf->cairo_surface =
1086 display_create_shm_surface(surface->display, &rect,
1090 if (!leaf->cairo_surface)
1093 wl_buffer_add_listener(leaf->data->buffer,
1094 &shm_surface_buffer_listener, surface);
1097 surface->current = leaf;
1099 return cairo_surface_reference(leaf->cairo_surface);
1103 shm_surface_swap(struct toysurface *base,
1104 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1105 struct rectangle *server_allocation)
1107 struct shm_surface *surface = to_shm_surface(base);
1108 struct shm_surface_leaf *leaf = surface->current;
1110 server_allocation->width =
1111 cairo_image_surface_get_width(leaf->cairo_surface);
1112 server_allocation->height =
1113 cairo_image_surface_get_height(leaf->cairo_surface);
1115 buffer_to_surface_size (buffer_transform, buffer_scale,
1116 &server_allocation->width,
1117 &server_allocation->height);
1119 wl_surface_attach(surface->surface, leaf->data->buffer,
1120 surface->dx, surface->dy);
1121 wl_surface_damage(surface->surface, 0, 0,
1122 server_allocation->width, server_allocation->height);
1123 wl_surface_commit(surface->surface);
1125 DBG_OBJ(surface->surface, "leaf %d busy\n",
1126 (int)(leaf - &surface->leaf[0]));
1129 surface->current = NULL;
1133 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1139 shm_surface_release(struct toysurface *base)
1144 shm_surface_destroy(struct toysurface *base)
1146 struct shm_surface *surface = to_shm_surface(base);
1149 for (i = 0; i < MAX_LEAVES; i++)
1150 shm_surface_leaf_release(&surface->leaf[i]);
1155 static struct toysurface *
1156 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1157 uint32_t flags, struct rectangle *rectangle)
1159 struct shm_surface *surface;
1160 DBG_OBJ(wl_surface, "\n");
1162 surface = xmalloc(sizeof *surface);
1163 memset(surface, 0, sizeof *surface);
1168 surface->base.prepare = shm_surface_prepare;
1169 surface->base.swap = shm_surface_swap;
1170 surface->base.acquire = shm_surface_acquire;
1171 surface->base.release = shm_surface_release;
1172 surface->base.destroy = shm_surface_destroy;
1174 surface->display = display;
1175 surface->surface = wl_surface;
1176 surface->flags = flags;
1178 return &surface->base;
1182 * The following correspondences between file names and cursors was copied
1183 * from: https://bugs.kde.org/attachment.cgi?id=67313
1186 static const char *bottom_left_corners[] = {
1187 "bottom_left_corner",
1192 static const char *bottom_right_corners[] = {
1193 "bottom_right_corner",
1198 static const char *bottom_sides[] = {
1204 static const char *grabbings[] = {
1207 "208530c400c041818281048008011002"
1210 static const char *left_ptrs[] = {
1217 static const char *left_sides[] = {
1223 static const char *right_sides[] = {
1229 static const char *top_left_corners[] = {
1235 static const char *top_right_corners[] = {
1241 static const char *top_sides[] = {
1247 static const char *xterms[] = {
1253 static const char *hand1s[] = {
1257 "e29285e634086352946a0e7090d73106"
1260 static const char *watches[] = {
1263 "0426c94ea35c87780ff01dc239897213"
1266 struct cursor_alternatives {
1271 static const struct cursor_alternatives cursors[] = {
1272 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1273 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1274 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1275 {grabbings, ARRAY_LENGTH(grabbings)},
1276 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1277 {left_sides, ARRAY_LENGTH(left_sides)},
1278 {right_sides, ARRAY_LENGTH(right_sides)},
1279 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1280 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1281 {top_sides, ARRAY_LENGTH(top_sides)},
1282 {xterms, ARRAY_LENGTH(xterms)},
1283 {hand1s, ARRAY_LENGTH(hand1s)},
1284 {watches, ARRAY_LENGTH(watches)},
1288 create_cursors(struct display *display)
1290 struct weston_config *config;
1291 struct weston_config_section *s;
1295 struct wl_cursor *cursor;
1297 config = weston_config_parse("weston.ini");
1298 s = weston_config_get_section(config, "shell", NULL, NULL);
1299 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1300 weston_config_section_get_int(s, "cursor-size", &size, 32);
1301 weston_config_destroy(config);
1303 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1306 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1308 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1310 for (j = 0; !cursor && j < cursors[i].count; ++j)
1311 cursor = wl_cursor_theme_get_cursor(
1312 display->cursor_theme, cursors[i].names[j]);
1315 fprintf(stderr, "could not load cursor '%s'\n",
1316 cursors[i].names[0]);
1318 display->cursors[i] = cursor;
1323 destroy_cursors(struct display *display)
1325 wl_cursor_theme_destroy(display->cursor_theme);
1326 free(display->cursors);
1329 struct wl_cursor_image *
1330 display_get_pointer_image(struct display *display, int pointer)
1332 struct wl_cursor *cursor = display->cursors[pointer];
1334 return cursor ? cursor->images[0] : NULL;
1338 surface_flush(struct surface *surface)
1340 if (!surface->cairo_surface)
1343 if (surface->opaque_region) {
1344 wl_surface_set_opaque_region(surface->surface,
1345 surface->opaque_region);
1346 wl_region_destroy(surface->opaque_region);
1347 surface->opaque_region = NULL;
1350 if (surface->input_region) {
1351 wl_surface_set_input_region(surface->surface,
1352 surface->input_region);
1353 wl_region_destroy(surface->input_region);
1354 surface->input_region = NULL;
1357 surface->toysurface->swap(surface->toysurface,
1358 surface->buffer_transform, surface->buffer_scale,
1359 &surface->server_allocation);
1361 cairo_surface_destroy(surface->cairo_surface);
1362 surface->cairo_surface = NULL;
1366 window_has_focus(struct window *window)
1368 return window->focus_count > 0;
1372 window_flush(struct window *window)
1374 struct surface *surface;
1376 if (window->type == TYPE_NONE) {
1377 window->type = TYPE_TOPLEVEL;
1378 if (window->shell_surface)
1379 wl_shell_surface_set_toplevel(window->shell_surface);
1382 wl_list_for_each(surface, &window->subsurface_list, link) {
1383 if (surface == window->main_surface)
1386 surface_flush(surface);
1389 surface_flush(window->main_surface);
1393 window_get_display(struct window *window)
1395 return window->display;
1399 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1401 struct display *display = surface->window->display;
1402 struct rectangle allocation = surface->allocation;
1404 if (!surface->toysurface && display->dpy &&
1405 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1406 surface->toysurface =
1407 egl_window_surface_create(display,
1413 if (!surface->toysurface)
1414 surface->toysurface = shm_surface_create(display,
1416 flags, &allocation);
1418 surface->cairo_surface = surface->toysurface->prepare(
1419 surface->toysurface, dx, dy,
1420 allocation.width, allocation.height, flags,
1421 surface->buffer_transform, surface->buffer_scale);
1425 window_create_main_surface(struct window *window)
1427 struct surface *surface = window->main_surface;
1432 if (window->resizing)
1433 flags |= SURFACE_HINT_RESIZE;
1435 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1436 flags |= SURFACE_HINT_RGB565;
1438 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1439 dx = surface->server_allocation.width -
1440 surface->allocation.width;
1442 if (window->resize_edges & WINDOW_RESIZING_TOP)
1443 dy = surface->server_allocation.height -
1444 surface->allocation.height;
1446 window->resize_edges = 0;
1448 surface_create_surface(surface, dx, dy, flags);
1452 window_get_buffer_transform(struct window *window)
1454 return window->main_surface->buffer_transform;
1458 window_set_buffer_transform(struct window *window,
1459 enum wl_output_transform transform)
1461 window->main_surface->buffer_transform = transform;
1462 wl_surface_set_buffer_transform(window->main_surface->surface,
1467 window_set_buffer_scale(struct window *window,
1470 window->main_surface->buffer_scale = scale;
1471 wl_surface_set_buffer_scale(window->main_surface->surface,
1476 window_get_buffer_scale(struct window *window)
1478 return window->main_surface->buffer_scale;
1482 window_get_output_scale(struct window *window)
1484 struct window_output *window_output;
1485 struct window_output *window_output_tmp;
1488 wl_list_for_each_safe(window_output, window_output_tmp,
1489 &window->window_output_list, link) {
1490 if (window_output->output->scale > scale)
1491 scale = window_output->output->scale;
1497 static void window_frame_destroy(struct window_frame *frame);
1500 surface_destroy(struct surface *surface)
1502 if (surface->frame_cb)
1503 wl_callback_destroy(surface->frame_cb);
1505 if (surface->input_region)
1506 wl_region_destroy(surface->input_region);
1508 if (surface->opaque_region)
1509 wl_region_destroy(surface->opaque_region);
1511 if (surface->subsurface)
1512 wl_subsurface_destroy(surface->subsurface);
1514 wl_surface_destroy(surface->surface);
1516 if (surface->toysurface)
1517 surface->toysurface->destroy(surface->toysurface);
1519 wl_list_remove(&surface->link);
1524 window_destroy(struct window *window)
1526 struct display *display = window->display;
1527 struct input *input;
1528 struct window_output *window_output;
1529 struct window_output *window_output_tmp;
1531 wl_list_remove(&window->redraw_task.link);
1533 wl_list_for_each(input, &display->input_list, link) {
1534 if (input->touch_focus == window)
1535 input->touch_focus = NULL;
1536 if (input->pointer_focus == window)
1537 input->pointer_focus = NULL;
1538 if (input->keyboard_focus == window)
1539 input->keyboard_focus = NULL;
1540 if (input->focus_widget &&
1541 input->focus_widget->window == window)
1542 input->focus_widget = NULL;
1545 wl_list_for_each_safe(window_output, window_output_tmp,
1546 &window->window_output_list, link) {
1547 free (window_output);
1551 window_frame_destroy(window->frame);
1553 if (window->shell_surface)
1554 wl_shell_surface_destroy(window->shell_surface);
1556 surface_destroy(window->main_surface);
1558 wl_list_remove(&window->link);
1560 free(window->title);
1564 static struct widget *
1565 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1567 struct widget *child, *target;
1569 wl_list_for_each(child, &widget->child_list, link) {
1570 target = widget_find_widget(child, x, y);
1575 if (widget->allocation.x <= x &&
1576 x < widget->allocation.x + widget->allocation.width &&
1577 widget->allocation.y <= y &&
1578 y < widget->allocation.y + widget->allocation.height) {
1585 static struct widget *
1586 window_find_widget(struct window *window, int32_t x, int32_t y)
1588 struct surface *surface;
1589 struct widget *widget;
1591 wl_list_for_each(surface, &window->subsurface_list, link) {
1592 widget = widget_find_widget(surface->widget, x, y);
1600 static struct widget *
1601 widget_create(struct window *window, struct surface *surface, void *data)
1603 struct widget *widget;
1605 widget = xzalloc(sizeof *widget);
1606 widget->window = window;
1607 widget->surface = surface;
1608 widget->user_data = data;
1609 widget->allocation = surface->allocation;
1610 wl_list_init(&widget->child_list);
1612 widget->tooltip = NULL;
1613 widget->tooltip_count = 0;
1614 widget->default_cursor = CURSOR_LEFT_PTR;
1615 widget->use_cairo = 1;
1621 window_add_widget(struct window *window, void *data)
1623 struct widget *widget;
1625 widget = widget_create(window, window->main_surface, data);
1626 wl_list_init(&widget->link);
1627 window->main_surface->widget = widget;
1633 widget_add_widget(struct widget *parent, void *data)
1635 struct widget *widget;
1637 widget = widget_create(parent->window, parent->surface, data);
1638 wl_list_insert(parent->child_list.prev, &widget->link);
1644 widget_destroy(struct widget *widget)
1646 struct display *display = widget->window->display;
1647 struct surface *surface = widget->surface;
1648 struct input *input;
1650 /* Destroy the sub-surface along with the root widget */
1651 if (surface->widget == widget && surface->subsurface)
1652 surface_destroy(widget->surface);
1654 if (widget->tooltip)
1655 widget_destroy_tooltip(widget);
1657 wl_list_for_each(input, &display->input_list, link) {
1658 if (input->focus_widget == widget)
1659 input->focus_widget = NULL;
1662 wl_list_remove(&widget->link);
1667 widget_set_default_cursor(struct widget *widget, int cursor)
1669 widget->default_cursor = cursor;
1673 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1675 *allocation = widget->allocation;
1679 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1681 widget->allocation.width = width;
1682 widget->allocation.height = height;
1686 widget_set_allocation(struct widget *widget,
1687 int32_t x, int32_t y, int32_t width, int32_t height)
1689 widget->allocation.x = x;
1690 widget->allocation.y = y;
1691 widget_set_size(widget, width, height);
1695 widget_set_transparent(struct widget *widget, int transparent)
1697 widget->opaque = !transparent;
1701 widget_get_user_data(struct widget *widget)
1703 return widget->user_data;
1706 static cairo_surface_t *
1707 widget_get_cairo_surface(struct widget *widget)
1709 struct surface *surface = widget->surface;
1710 struct window *window = widget->window;
1712 assert(widget->use_cairo);
1714 if (!surface->cairo_surface) {
1715 if (surface == window->main_surface)
1716 window_create_main_surface(window);
1718 surface_create_surface(surface, 0, 0, 0);
1721 return surface->cairo_surface;
1725 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1727 struct surface *surface = widget->surface;
1730 enum wl_output_transform transform;
1731 int surface_width, surface_height;
1732 int translate_x, translate_y;
1735 surface_width = surface->allocation.width;
1736 surface_height = surface->allocation.height;
1738 transform = surface->buffer_transform;
1739 scale = surface->buffer_scale;
1741 switch (transform) {
1742 case WL_OUTPUT_TRANSFORM_FLIPPED:
1743 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1746 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1749 cairo_matrix_init_identity(&m);
1753 switch (transform) {
1754 case WL_OUTPUT_TRANSFORM_NORMAL:
1760 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 translate_x = surface_width;
1765 case WL_OUTPUT_TRANSFORM_90:
1767 translate_x = surface_height;
1770 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1772 translate_x = surface_height;
1773 translate_y = surface_width;
1775 case WL_OUTPUT_TRANSFORM_180:
1777 translate_x = surface_width;
1778 translate_y = surface_height;
1780 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1783 translate_y = surface_height;
1785 case WL_OUTPUT_TRANSFORM_270:
1786 angle = M_PI + M_PI_2;
1788 translate_y = surface_width;
1790 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1791 angle = M_PI + M_PI_2;
1797 cairo_scale(cr, scale, scale);
1798 cairo_translate(cr, translate_x, translate_y);
1799 cairo_rotate(cr, angle);
1800 cairo_transform(cr, &m);
1804 widget_cairo_create(struct widget *widget)
1806 struct surface *surface = widget->surface;
1807 cairo_surface_t *cairo_surface;
1810 cairo_surface = widget_get_cairo_surface(widget);
1811 cr = cairo_create(cairo_surface);
1813 widget_cairo_update_transform(widget, cr);
1815 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1821 widget_get_wl_surface(struct widget *widget)
1823 return widget->surface->surface;
1826 struct wl_subsurface *
1827 widget_get_wl_subsurface(struct widget *widget)
1829 return widget->surface->subsurface;
1833 widget_get_last_time(struct widget *widget)
1835 return widget->surface->last_time;
1839 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1841 struct wl_compositor *comp = widget->window->display->compositor;
1842 struct surface *surface = widget->surface;
1844 if (!surface->input_region)
1845 surface->input_region = wl_compositor_create_region(comp);
1848 wl_region_add(surface->input_region,
1849 rect->x, rect->y, rect->width, rect->height);
1854 widget_set_resize_handler(struct widget *widget,
1855 widget_resize_handler_t handler)
1857 widget->resize_handler = handler;
1861 widget_set_redraw_handler(struct widget *widget,
1862 widget_redraw_handler_t handler)
1864 widget->redraw_handler = handler;
1868 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1870 widget->enter_handler = handler;
1874 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1876 widget->leave_handler = handler;
1880 widget_set_motion_handler(struct widget *widget,
1881 widget_motion_handler_t handler)
1883 widget->motion_handler = handler;
1887 widget_set_button_handler(struct widget *widget,
1888 widget_button_handler_t handler)
1890 widget->button_handler = handler;
1894 widget_set_touch_up_handler(struct widget *widget,
1895 widget_touch_up_handler_t handler)
1897 widget->touch_up_handler = handler;
1901 widget_set_touch_down_handler(struct widget *widget,
1902 widget_touch_down_handler_t handler)
1904 widget->touch_down_handler = handler;
1908 widget_set_touch_motion_handler(struct widget *widget,
1909 widget_touch_motion_handler_t handler)
1911 widget->touch_motion_handler = handler;
1915 widget_set_touch_frame_handler(struct widget *widget,
1916 widget_touch_frame_handler_t handler)
1918 widget->touch_frame_handler = handler;
1922 widget_set_touch_cancel_handler(struct widget *widget,
1923 widget_touch_cancel_handler_t handler)
1925 widget->touch_cancel_handler = handler;
1929 widget_set_axis_handler(struct widget *widget,
1930 widget_axis_handler_t handler)
1932 widget->axis_handler = handler;
1936 window_schedule_redraw_task(struct window *window);
1939 widget_schedule_redraw(struct widget *widget)
1941 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1942 widget->surface->redraw_needed = 1;
1943 window_schedule_redraw_task(widget->window);
1947 widget_set_use_cairo(struct widget *widget,
1950 widget->use_cairo = use_cairo;
1954 window_get_surface(struct window *window)
1956 cairo_surface_t *cairo_surface;
1958 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1960 return cairo_surface_reference(cairo_surface);
1964 window_get_wl_surface(struct window *window)
1966 return window->main_surface->surface;
1970 tooltip_redraw_handler(struct widget *widget, void *data)
1973 const int32_t r = 3;
1974 struct tooltip *tooltip = data;
1975 int32_t width, height;
1977 cr = widget_cairo_create(widget);
1978 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
1979 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1980 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1983 width = widget->allocation.width;
1984 height = widget->allocation.height;
1985 rounded_rect(cr, 0, 0, width, height, r);
1987 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1988 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1991 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1992 cairo_move_to(cr, 10, 16);
1993 cairo_show_text(cr, tooltip->entry);
1997 static cairo_text_extents_t
1998 get_text_extents(struct display *display, struct tooltip *tooltip)
2001 cairo_text_extents_t extents;
2003 /* Use the dummy_surface because tooltip's surface was not
2004 * created yet, and parent does not have a valid surface
2005 * outside repaint, either.
2007 cr = cairo_create(display->dummy_surface);
2008 cairo_text_extents(cr, tooltip->entry, &extents);
2015 window_create_tooltip(struct tooltip *tooltip)
2017 struct widget *parent = tooltip->parent;
2018 struct display *display = parent->window->display;
2019 const int offset_y = 27;
2020 const int margin = 3;
2021 cairo_text_extents_t extents;
2023 if (tooltip->widget)
2026 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
2028 extents = get_text_extents(display, tooltip);
2029 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2030 widget_set_allocation(tooltip->widget,
2031 tooltip->x, tooltip->y + offset_y,
2032 extents.width + 20, 20 + margin * 2);
2038 widget_destroy_tooltip(struct widget *parent)
2040 struct tooltip *tooltip = parent->tooltip;
2042 parent->tooltip_count = 0;
2046 if (tooltip->widget) {
2047 widget_destroy(tooltip->widget);
2048 tooltip->widget = NULL;
2051 close(tooltip->tooltip_fd);
2052 free(tooltip->entry);
2054 parent->tooltip = NULL;
2058 tooltip_func(struct task *task, uint32_t events)
2060 struct tooltip *tooltip =
2061 container_of(task, struct tooltip, tooltip_task);
2064 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2066 window_create_tooltip(tooltip);
2069 #define TOOLTIP_TIMEOUT 500
2071 tooltip_timer_reset(struct tooltip *tooltip)
2073 struct itimerspec its;
2075 its.it_interval.tv_sec = 0;
2076 its.it_interval.tv_nsec = 0;
2077 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2078 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2079 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2080 fprintf(stderr, "could not set timerfd\n: %m");
2088 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2090 struct tooltip *tooltip = parent->tooltip;
2092 parent->tooltip_count++;
2096 tooltip_timer_reset(tooltip);
2100 /* the handler might be triggered too fast via input device motion, so
2101 * we need this check here to make sure tooltip is fully initialized */
2102 if (parent->tooltip_count > 1)
2105 tooltip = malloc(sizeof *tooltip);
2109 parent->tooltip = tooltip;
2110 tooltip->parent = parent;
2111 tooltip->widget = NULL;
2114 tooltip->entry = strdup(entry);
2115 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2116 if (tooltip->tooltip_fd < 0) {
2117 fprintf(stderr, "could not create timerfd\n: %m");
2121 tooltip->tooltip_task.run = tooltip_func;
2122 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2123 EPOLLIN, &tooltip->tooltip_task);
2124 tooltip_timer_reset(tooltip);
2130 workspace_manager_state(void *data,
2131 struct workspace_manager *workspace_manager,
2135 struct display *display = data;
2137 display->workspace = current;
2138 display->workspace_count = count;
2141 static const struct workspace_manager_listener workspace_manager_listener = {
2142 workspace_manager_state
2146 frame_resize_handler(struct widget *widget,
2147 int32_t width, int32_t height, void *data)
2149 struct window_frame *frame = data;
2150 struct widget *child = frame->child;
2151 struct rectangle interior;
2152 struct rectangle input;
2153 struct rectangle opaque;
2155 if (widget->window->type == TYPE_FULLSCREEN) {
2158 interior.width = width;
2159 interior.height = height;
2161 if (widget->window->type == TYPE_MAXIMIZED) {
2162 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2164 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2167 frame_resize(frame->frame, width, height);
2168 frame_interior(frame->frame, &interior.x, &interior.y,
2169 &interior.width, &interior.height);
2172 widget_set_allocation(child, interior.x, interior.y,
2173 interior.width, interior.height);
2175 if (child->resize_handler) {
2176 child->resize_handler(child, interior.width, interior.height,
2179 if (widget->window->type == TYPE_FULLSCREEN) {
2180 width = child->allocation.width;
2181 height = child->allocation.height;
2183 frame_resize_inside(frame->frame,
2184 child->allocation.width,
2185 child->allocation.height);
2186 width = frame_width(frame->frame);
2187 height = frame_height(frame->frame);
2191 widget_set_allocation(widget, 0, 0, width, height);
2193 widget->surface->input_region =
2194 wl_compositor_create_region(widget->window->display->compositor);
2195 if (widget->window->type != TYPE_FULLSCREEN) {
2196 frame_input_rect(frame->frame, &input.x, &input.y,
2197 &input.width, &input.height);
2198 wl_region_add(widget->surface->input_region,
2199 input.x, input.y, input.width, input.height);
2201 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2204 widget_set_allocation(widget, 0, 0, width, height);
2206 if (child->opaque) {
2207 if (widget->window->type != TYPE_FULLSCREEN) {
2208 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2209 &opaque.width, &opaque.height);
2211 wl_region_add(widget->surface->opaque_region,
2213 opaque.width, opaque.height);
2215 wl_region_add(widget->surface->opaque_region,
2216 0, 0, width, height);
2221 widget_schedule_redraw(widget);
2225 frame_redraw_handler(struct widget *widget, void *data)
2228 struct window_frame *frame = data;
2229 struct window *window = widget->window;
2231 if (window->type == TYPE_FULLSCREEN)
2234 if (window->focus_count) {
2235 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2237 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2240 cr = widget_cairo_create(widget);
2242 frame_repaint(frame->frame, cr);
2248 frame_get_pointer_image_for_location(struct window_frame *frame,
2249 enum theme_location location)
2251 struct window *window = frame->widget->window;
2253 if (window->type != TYPE_TOPLEVEL)
2254 return CURSOR_LEFT_PTR;
2257 case THEME_LOCATION_RESIZING_TOP:
2259 case THEME_LOCATION_RESIZING_BOTTOM:
2260 return CURSOR_BOTTOM;
2261 case THEME_LOCATION_RESIZING_LEFT:
2263 case THEME_LOCATION_RESIZING_RIGHT:
2264 return CURSOR_RIGHT;
2265 case THEME_LOCATION_RESIZING_TOP_LEFT:
2266 return CURSOR_TOP_LEFT;
2267 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2268 return CURSOR_TOP_RIGHT;
2269 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2270 return CURSOR_BOTTOM_LEFT;
2271 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2272 return CURSOR_BOTTOM_RIGHT;
2273 case THEME_LOCATION_EXTERIOR:
2274 case THEME_LOCATION_TITLEBAR:
2276 return CURSOR_LEFT_PTR;
2281 frame_menu_func(struct window *window,
2282 struct input *input, int index, void *data)
2284 struct display *display;
2288 if (window->close_handler)
2289 window->close_handler(window->user_data);
2291 display_exit(window->display);
2293 case 1: /* move to workspace above */
2294 display = window->display;
2295 if (display->workspace > 0)
2296 workspace_manager_move_surface(
2297 display->workspace_manager,
2298 window->main_surface->surface,
2299 display->workspace - 1);
2301 case 2: /* move to workspace below */
2302 display = window->display;
2303 if (display->workspace < display->workspace_count - 1)
2304 workspace_manager_move_surface(
2305 display->workspace_manager,
2306 window->main_surface->surface,
2307 display->workspace + 1);
2309 case 3: /* fullscreen */
2310 /* we don't have a way to get out of fullscreen for now */
2311 if (window->fullscreen_handler)
2312 window->fullscreen_handler(window, window->user_data);
2318 window_show_frame_menu(struct window *window,
2319 struct input *input, uint32_t time)
2324 static const char *entries[] = {
2326 "Move to workspace above", "Move to workspace below",
2330 if (window->fullscreen_handler)
2331 count = ARRAY_LENGTH(entries);
2333 count = ARRAY_LENGTH(entries) - 1;
2335 input_get_position(input, &x, &y);
2336 window_show_menu(window->display, input, time, window,
2337 x - 10, y - 10, frame_menu_func, entries, count);
2341 frame_enter_handler(struct widget *widget,
2342 struct input *input, float x, float y, void *data)
2344 struct window_frame *frame = data;
2345 enum theme_location location;
2347 location = frame_pointer_enter(frame->frame, input, x, y);
2348 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2349 widget_schedule_redraw(frame->widget);
2351 return frame_get_pointer_image_for_location(data, location);
2355 frame_motion_handler(struct widget *widget,
2356 struct input *input, uint32_t time,
2357 float x, float y, void *data)
2359 struct window_frame *frame = data;
2360 enum theme_location location;
2362 location = frame_pointer_motion(frame->frame, input, x, y);
2363 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2364 widget_schedule_redraw(frame->widget);
2366 return frame_get_pointer_image_for_location(data, location);
2370 frame_leave_handler(struct widget *widget,
2371 struct input *input, void *data)
2373 struct window_frame *frame = data;
2375 frame_pointer_leave(frame->frame, input);
2376 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2377 widget_schedule_redraw(frame->widget);
2381 frame_handle_status(struct window_frame *frame, struct input *input,
2382 uint32_t time, enum theme_location location)
2384 struct window *window = frame->widget->window;
2387 status = frame_status(frame->frame);
2388 if (status & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2391 if (status & FRAME_STATUS_MINIMIZE)
2392 fprintf(stderr,"Minimize stub\n");
2394 if (status & FRAME_STATUS_MENU) {
2395 window_show_frame_menu(window, input, time);
2396 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2399 if (status & FRAME_STATUS_MAXIMIZE) {
2400 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2401 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2404 if (status & FRAME_STATUS_CLOSE) {
2405 if (window->close_handler)
2406 window->close_handler(window->user_data);
2408 display_exit(window->display);
2412 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2413 input_ungrab(input);
2414 wl_shell_surface_move(window->shell_surface,
2415 input_get_seat(input),
2416 window->display->serial);
2418 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2421 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2422 input_ungrab(input);
2424 window->resizing = 1;
2425 wl_shell_surface_resize(window->shell_surface,
2426 input_get_seat(input),
2427 window->display->serial,
2430 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2435 frame_button_handler(struct widget *widget,
2436 struct input *input, uint32_t time,
2437 uint32_t button, enum wl_pointer_button_state state,
2441 struct window_frame *frame = data;
2442 enum theme_location location;
2444 location = frame_pointer_button(frame->frame, input, button, state);
2445 frame_handle_status(frame, input, time, location);
2449 frame_touch_down_handler(struct widget *widget, struct input *input,
2450 uint32_t serial, uint32_t time, int32_t id,
2451 float x, float y, void *data)
2453 struct window_frame *frame = data;
2455 frame_touch_down(frame->frame, input, id, x, y);
2456 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2460 frame_touch_up_handler(struct widget *widget,
2461 struct input *input, uint32_t serial, uint32_t time,
2462 int32_t id, void *data)
2464 struct window_frame *frame = data;
2466 frame_touch_up(frame->frame, input, id);
2467 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2471 window_frame_create(struct window *window, void *data)
2473 struct window_frame *frame;
2476 if (window->type == TYPE_CUSTOM) {
2477 buttons = FRAME_BUTTON_NONE;
2479 buttons = FRAME_BUTTON_ALL;
2482 frame = xzalloc(sizeof *frame);
2483 frame->frame = frame_create(window->display->theme, 0, 0,
2484 buttons, window->title);
2486 frame->widget = window_add_widget(window, frame);
2487 frame->child = widget_add_widget(frame->widget, data);
2489 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2490 widget_set_resize_handler(frame->widget, frame_resize_handler);
2491 widget_set_enter_handler(frame->widget, frame_enter_handler);
2492 widget_set_leave_handler(frame->widget, frame_leave_handler);
2493 widget_set_motion_handler(frame->widget, frame_motion_handler);
2494 widget_set_button_handler(frame->widget, frame_button_handler);
2495 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2496 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2498 window->frame = frame;
2500 return frame->child;
2504 window_frame_set_child_size(struct widget *widget, int child_width,
2507 struct display *display = widget->window->display;
2508 struct theme *t = display->theme;
2509 int decoration_width, decoration_height;
2511 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2513 if (widget->window->type != TYPE_FULLSCREEN) {
2514 decoration_width = (t->width + margin) * 2;
2515 decoration_height = t->width +
2516 t->titlebar_height + margin * 2;
2518 width = child_width + decoration_width;
2519 height = child_height + decoration_height;
2521 width = child_width;
2522 height = child_height;
2525 window_schedule_resize(widget->window, width, height);
2529 window_frame_destroy(struct window_frame *frame)
2531 frame_destroy(frame->frame);
2533 /* frame->child must be destroyed by the application */
2534 widget_destroy(frame->widget);
2539 input_set_focus_widget(struct input *input, struct widget *focus,
2542 struct widget *old, *widget;
2545 if (focus == input->focus_widget)
2548 old = input->focus_widget;
2552 widget = input->grab;
2553 if (widget->leave_handler)
2554 widget->leave_handler(old, input, widget->user_data);
2555 input->focus_widget = NULL;
2561 widget = input->grab;
2562 input->focus_widget = focus;
2563 if (widget->enter_handler)
2564 cursor = widget->enter_handler(focus, input, x, y,
2567 cursor = widget->default_cursor;
2569 input_set_pointer_image(input, cursor);
2574 touch_grab(struct input *input, int32_t touch_id)
2576 input->touch_grab = 1;
2577 input->touch_grab_id = touch_id;
2581 touch_ungrab(struct input *input)
2583 struct touch_point *tp, *tmp;
2585 input->touch_grab = 0;
2587 wl_list_for_each_safe(tp, tmp,
2588 &input->touch_point_list, link) {
2589 if (tp->id != input->touch_grab_id)
2591 wl_list_remove(&tp->link);
2599 input_grab(struct input *input, struct widget *widget, uint32_t button)
2601 input->grab = widget;
2602 input->grab_button = button;
2606 input_ungrab(struct input *input)
2608 struct widget *widget;
2611 if (input->pointer_focus) {
2612 widget = window_find_widget(input->pointer_focus,
2613 input->sx, input->sy);
2614 input_set_focus_widget(input, widget, input->sx, input->sy);
2619 input_remove_pointer_focus(struct input *input)
2621 struct window *window = input->pointer_focus;
2626 input_set_focus_widget(input, NULL, 0, 0);
2628 input->pointer_focus = NULL;
2629 input->current_cursor = CURSOR_UNSET;
2633 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2634 uint32_t serial, struct wl_surface *surface,
2635 wl_fixed_t sx_w, wl_fixed_t sy_w)
2637 struct input *input = data;
2638 struct window *window;
2639 struct widget *widget;
2640 float sx = wl_fixed_to_double(sx_w);
2641 float sy = wl_fixed_to_double(sy_w);
2644 /* enter event for a window we've just destroyed */
2648 input->display->serial = serial;
2649 input->pointer_enter_serial = serial;
2650 input->pointer_focus = wl_surface_get_user_data(surface);
2651 window = input->pointer_focus;
2653 if (window->resizing) {
2654 window->resizing = 0;
2655 /* Schedule a redraw to free the pool */
2656 window_schedule_redraw(window);
2662 widget = window_find_widget(window, sx, sy);
2663 input_set_focus_widget(input, widget, sx, sy);
2667 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2668 uint32_t serial, struct wl_surface *surface)
2670 struct input *input = data;
2672 input->display->serial = serial;
2673 input_remove_pointer_focus(input);
2677 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2678 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2680 struct input *input = data;
2681 struct window *window = input->pointer_focus;
2682 struct widget *widget;
2684 float sx = wl_fixed_to_double(sx_w);
2685 float sy = wl_fixed_to_double(sy_w);
2693 /* when making the window smaller - e.g. after a unmaximise we might
2694 * still have a pending motion event that the compositor has picked
2695 * based on the old surface dimensions
2697 if (sx > window->main_surface->allocation.width ||
2698 sy > window->main_surface->allocation.height)
2701 if (!(input->grab && input->grab_button)) {
2702 widget = window_find_widget(window, sx, sy);
2703 input_set_focus_widget(input, widget, sx, sy);
2707 widget = input->grab;
2709 widget = input->focus_widget;
2711 if (widget->motion_handler)
2712 cursor = widget->motion_handler(input->focus_widget,
2713 input, time, sx, sy,
2716 cursor = widget->default_cursor;
2718 cursor = CURSOR_LEFT_PTR;
2720 input_set_pointer_image(input, cursor);
2724 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2725 uint32_t time, uint32_t button, uint32_t state_w)
2727 struct input *input = data;
2728 struct widget *widget;
2729 enum wl_pointer_button_state state = state_w;
2731 input->display->serial = serial;
2732 if (input->focus_widget && input->grab == NULL &&
2733 state == WL_POINTER_BUTTON_STATE_PRESSED)
2734 input_grab(input, input->focus_widget, button);
2736 widget = input->grab;
2737 if (widget && widget->button_handler)
2738 (*widget->button_handler)(widget,
2741 input->grab->user_data);
2743 if (input->grab && input->grab_button == button &&
2744 state == WL_POINTER_BUTTON_STATE_RELEASED)
2745 input_ungrab(input);
2749 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2750 uint32_t time, uint32_t axis, wl_fixed_t value)
2752 struct input *input = data;
2753 struct widget *widget;
2755 widget = input->focus_widget;
2757 widget = input->grab;
2758 if (widget && widget->axis_handler)
2759 (*widget->axis_handler)(widget,
2765 static const struct wl_pointer_listener pointer_listener = {
2766 pointer_handle_enter,
2767 pointer_handle_leave,
2768 pointer_handle_motion,
2769 pointer_handle_button,
2770 pointer_handle_axis,
2774 input_remove_keyboard_focus(struct input *input)
2776 struct window *window = input->keyboard_focus;
2777 struct itimerspec its;
2779 its.it_interval.tv_sec = 0;
2780 its.it_interval.tv_nsec = 0;
2781 its.it_value.tv_sec = 0;
2782 its.it_value.tv_nsec = 0;
2783 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2788 window->focus_count--;
2789 if (window->keyboard_focus_handler)
2790 (*window->keyboard_focus_handler)(window, NULL,
2793 input->keyboard_focus = NULL;
2797 keyboard_repeat_func(struct task *task, uint32_t events)
2799 struct input *input =
2800 container_of(task, struct input, repeat_task);
2801 struct window *window = input->keyboard_focus;
2804 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2805 /* If we change the timer between the fd becoming
2806 * readable and getting here, there'll be nothing to
2807 * read and we get EAGAIN. */
2810 if (window && window->key_handler) {
2811 (*window->key_handler)(window, input, input->repeat_time,
2812 input->repeat_key, input->repeat_sym,
2813 WL_KEYBOARD_KEY_STATE_PRESSED,
2819 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2820 uint32_t format, int fd, uint32_t size)
2822 struct input *input = data;
2823 struct xkb_keymap *keymap;
2824 struct xkb_state *state;
2832 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2837 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2838 if (map_str == MAP_FAILED) {
2843 keymap = xkb_map_new_from_string(input->display->xkb_context,
2845 XKB_KEYMAP_FORMAT_TEXT_V1,
2847 munmap(map_str, size);
2851 fprintf(stderr, "failed to compile keymap\n");
2855 state = xkb_state_new(keymap);
2857 fprintf(stderr, "failed to create XKB state\n");
2858 xkb_map_unref(keymap);
2862 xkb_keymap_unref(input->xkb.keymap);
2863 xkb_state_unref(input->xkb.state);
2864 input->xkb.keymap = keymap;
2865 input->xkb.state = state;
2867 input->xkb.control_mask =
2868 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2869 input->xkb.alt_mask =
2870 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2871 input->xkb.shift_mask =
2872 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2876 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2877 uint32_t serial, struct wl_surface *surface,
2878 struct wl_array *keys)
2880 struct input *input = data;
2881 struct window *window;
2883 input->display->serial = serial;
2884 input->keyboard_focus = wl_surface_get_user_data(surface);
2886 window = input->keyboard_focus;
2887 window->focus_count++;
2888 if (window->keyboard_focus_handler)
2889 (*window->keyboard_focus_handler)(window,
2890 input, window->user_data);
2894 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2895 uint32_t serial, struct wl_surface *surface)
2897 struct input *input = data;
2899 input->display->serial = serial;
2900 input_remove_keyboard_focus(input);
2904 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2905 uint32_t serial, uint32_t time, uint32_t key,
2908 struct input *input = data;
2909 struct window *window = input->keyboard_focus;
2910 uint32_t code, num_syms;
2911 enum wl_keyboard_key_state state = state_w;
2912 const xkb_keysym_t *syms;
2914 struct itimerspec its;
2916 input->display->serial = serial;
2918 if (!window || !input->xkb.state)
2921 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2923 sym = XKB_KEY_NoSymbol;
2928 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2929 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2930 window_set_maximized(window,
2931 window->type != TYPE_MAXIMIZED);
2932 } else if (sym == XKB_KEY_F11 &&
2933 window->fullscreen_handler &&
2934 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 window->fullscreen_handler(window, window->user_data);
2936 } else if (sym == XKB_KEY_F4 &&
2937 input->modifiers == MOD_ALT_MASK &&
2938 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2939 if (window->close_handler)
2940 window->close_handler(window->user_data);
2942 display_exit(window->display);
2943 } else if (window->key_handler) {
2944 (*window->key_handler)(window, input, time, key,
2945 sym, state, window->user_data);
2948 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2949 key == input->repeat_key) {
2950 its.it_interval.tv_sec = 0;
2951 its.it_interval.tv_nsec = 0;
2952 its.it_value.tv_sec = 0;
2953 its.it_value.tv_nsec = 0;
2954 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2955 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2956 input->repeat_sym = sym;
2957 input->repeat_key = key;
2958 input->repeat_time = time;
2959 its.it_interval.tv_sec = 0;
2960 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2961 its.it_value.tv_sec = 0;
2962 its.it_value.tv_nsec = 400 * 1000 * 1000;
2963 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2968 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2969 uint32_t serial, uint32_t mods_depressed,
2970 uint32_t mods_latched, uint32_t mods_locked,
2973 struct input *input = data;
2974 xkb_mod_mask_t mask;
2976 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2977 if (!input->xkb.keymap)
2980 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2981 mods_locked, 0, 0, group);
2982 mask = xkb_state_serialize_mods(input->xkb.state,
2983 XKB_STATE_DEPRESSED |
2985 input->modifiers = 0;
2986 if (mask & input->xkb.control_mask)
2987 input->modifiers |= MOD_CONTROL_MASK;
2988 if (mask & input->xkb.alt_mask)
2989 input->modifiers |= MOD_ALT_MASK;
2990 if (mask & input->xkb.shift_mask)
2991 input->modifiers |= MOD_SHIFT_MASK;
2994 static const struct wl_keyboard_listener keyboard_listener = {
2995 keyboard_handle_keymap,
2996 keyboard_handle_enter,
2997 keyboard_handle_leave,
2998 keyboard_handle_key,
2999 keyboard_handle_modifiers,
3003 touch_handle_down(void *data, struct wl_touch *wl_touch,
3004 uint32_t serial, uint32_t time, struct wl_surface *surface,
3005 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3007 struct input *input = data;
3008 struct widget *widget;
3009 float sx = wl_fixed_to_double(x_w);
3010 float sy = wl_fixed_to_double(y_w);
3012 input->display->serial = serial;
3013 input->touch_focus = wl_surface_get_user_data(surface);
3014 if (!input->touch_focus) {
3015 DBG("Failed to find to touch focus for surface %p\n", surface);
3019 widget = window_find_widget(input->touch_focus,
3020 wl_fixed_to_double(x_w),
3021 wl_fixed_to_double(y_w));
3023 struct touch_point *tp = xmalloc(sizeof *tp);
3026 tp->widget = widget;
3029 wl_list_insert(&input->touch_point_list, &tp->link);
3031 if (widget->touch_down_handler)
3032 (*widget->touch_down_handler)(widget, input,
3041 touch_handle_up(void *data, struct wl_touch *wl_touch,
3042 uint32_t serial, uint32_t time, int32_t id)
3044 struct input *input = data;
3045 struct touch_point *tp, *tmp;
3047 if (!input->touch_focus) {
3048 DBG("No touch focus found for touch up event!\n");
3052 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3056 if (tp->widget->touch_up_handler)
3057 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3059 tp->widget->user_data);
3061 wl_list_remove(&tp->link);
3069 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3070 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3072 struct input *input = data;
3073 struct touch_point *tp;
3074 float sx = wl_fixed_to_double(x_w);
3075 float sy = wl_fixed_to_double(y_w);
3077 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3079 if (!input->touch_focus) {
3080 DBG("No touch focus found for touch motion event!\n");
3084 wl_list_for_each(tp, &input->touch_point_list, link) {
3090 if (tp->widget->touch_motion_handler)
3091 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3093 tp->widget->user_data);
3099 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3101 struct input *input = data;
3102 struct touch_point *tp, *tmp;
3104 DBG("touch_handle_frame\n");
3106 if (!input->touch_focus) {
3107 DBG("No touch focus found for touch frame event!\n");
3111 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3112 if (tp->widget->touch_frame_handler)
3113 (*tp->widget->touch_frame_handler)(tp->widget, input,
3114 tp->widget->user_data);
3116 wl_list_remove(&tp->link);
3122 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3124 struct input *input = data;
3125 struct touch_point *tp, *tmp;
3127 DBG("touch_handle_cancel\n");
3129 if (!input->touch_focus) {
3130 DBG("No touch focus found for touch cancel event!\n");
3134 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3135 if (tp->widget->touch_cancel_handler)
3136 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3137 tp->widget->user_data);
3139 wl_list_remove(&tp->link);
3144 static const struct wl_touch_listener touch_listener = {
3147 touch_handle_motion,
3149 touch_handle_cancel,
3153 seat_handle_capabilities(void *data, struct wl_seat *seat,
3154 enum wl_seat_capability caps)
3156 struct input *input = data;
3158 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3159 input->pointer = wl_seat_get_pointer(seat);
3160 wl_pointer_set_user_data(input->pointer, input);
3161 wl_pointer_add_listener(input->pointer, &pointer_listener,
3163 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3164 wl_pointer_destroy(input->pointer);
3165 input->pointer = NULL;
3168 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3169 input->keyboard = wl_seat_get_keyboard(seat);
3170 wl_keyboard_set_user_data(input->keyboard, input);
3171 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3173 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3174 wl_keyboard_destroy(input->keyboard);
3175 input->keyboard = NULL;
3178 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3179 input->touch = wl_seat_get_touch(seat);
3180 wl_touch_set_user_data(input->touch, input);
3181 wl_touch_add_listener(input->touch, &touch_listener, input);
3182 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3183 wl_touch_destroy(input->touch);
3184 input->touch = NULL;
3189 seat_handle_name(void *data, struct wl_seat *seat,
3195 static const struct wl_seat_listener seat_listener = {
3196 seat_handle_capabilities,
3201 input_get_position(struct input *input, int32_t *x, int32_t *y)
3208 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3210 struct touch_point *tp;
3212 wl_list_for_each(tp, &input->touch_point_list, link) {
3225 input_get_display(struct input *input)
3227 return input->display;
3231 input_get_seat(struct input *input)
3237 input_get_modifiers(struct input *input)
3239 return input->modifiers;
3243 input_get_focus_widget(struct input *input)
3245 return input->focus_widget;
3249 struct wl_data_offer *offer;
3250 struct input *input;
3251 struct wl_array types;
3254 struct task io_task;
3262 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3264 struct data_offer *offer = data;
3267 p = wl_array_add(&offer->types, sizeof *p);
3271 static const struct wl_data_offer_listener data_offer_listener = {
3276 data_offer_destroy(struct data_offer *offer)
3281 if (offer->refcount == 0) {
3282 wl_data_offer_destroy(offer->offer);
3283 for (p = offer->types.data; *p; p++)
3285 wl_array_release(&offer->types);
3291 data_device_data_offer(void *data,
3292 struct wl_data_device *data_device,
3293 struct wl_data_offer *_offer)
3295 struct data_offer *offer;
3297 offer = xmalloc(sizeof *offer);
3299 wl_array_init(&offer->types);
3300 offer->refcount = 1;
3301 offer->input = data;
3302 offer->offer = _offer;
3303 wl_data_offer_add_listener(offer->offer,
3304 &data_offer_listener, offer);
3308 data_device_enter(void *data, struct wl_data_device *data_device,
3309 uint32_t serial, struct wl_surface *surface,
3310 wl_fixed_t x_w, wl_fixed_t y_w,
3311 struct wl_data_offer *offer)
3313 struct input *input = data;
3314 struct window *window;
3316 float x = wl_fixed_to_double(x_w);
3317 float y = wl_fixed_to_double(y_w);
3320 window = wl_surface_get_user_data(surface);
3321 input->drag_enter_serial = serial;
3322 input->drag_focus = window,
3326 if (!input->touch_grab)
3327 input->pointer_enter_serial = serial;
3330 input->drag_offer = wl_data_offer_get_user_data(offer);
3332 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3335 types_data = input->drag_offer->types.data;
3337 input->drag_offer = NULL;
3341 if (window->data_handler)
3342 window->data_handler(window, input, x, y, types_data,
3347 data_device_leave(void *data, struct wl_data_device *data_device)
3349 struct input *input = data;
3351 if (input->drag_offer) {
3352 data_offer_destroy(input->drag_offer);
3353 input->drag_offer = NULL;
3358 data_device_motion(void *data, struct wl_data_device *data_device,
3359 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3361 struct input *input = data;
3362 struct window *window = input->drag_focus;
3363 float x = wl_fixed_to_double(x_w);
3364 float y = wl_fixed_to_double(y_w);
3370 if (input->drag_offer)
3371 types_data = input->drag_offer->types.data;
3375 if (window->data_handler)
3376 window->data_handler(window, input, x, y, types_data,
3381 data_device_drop(void *data, struct wl_data_device *data_device)
3383 struct input *input = data;
3384 struct window *window = input->drag_focus;
3390 if (window->drop_handler)
3391 window->drop_handler(window, input,
3392 x, y, window->user_data);
3394 if (input->touch_grab)
3395 touch_ungrab(input);
3399 data_device_selection(void *data,
3400 struct wl_data_device *wl_data_device,
3401 struct wl_data_offer *offer)
3403 struct input *input = data;
3406 if (input->selection_offer)
3407 data_offer_destroy(input->selection_offer);
3410 input->selection_offer = wl_data_offer_get_user_data(offer);
3411 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3414 input->selection_offer = NULL;
3418 static const struct wl_data_device_listener data_device_listener = {
3419 data_device_data_offer,
3424 data_device_selection
3428 input_set_pointer_image_index(struct input *input, int index)
3430 struct wl_buffer *buffer;
3431 struct wl_cursor *cursor;
3432 struct wl_cursor_image *image;
3434 if (!input->pointer)
3437 cursor = input->display->cursors[input->current_cursor];
3441 if (index >= (int) cursor->image_count) {
3442 fprintf(stderr, "cursor index out of range\n");
3446 image = cursor->images[index];
3447 buffer = wl_cursor_image_get_buffer(image);
3451 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3452 wl_surface_damage(input->pointer_surface, 0, 0,
3453 image->width, image->height);
3454 wl_surface_commit(input->pointer_surface);
3455 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3456 input->pointer_surface,
3457 image->hotspot_x, image->hotspot_y);
3460 static const struct wl_callback_listener pointer_surface_listener;
3463 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3466 struct input *input = data;
3467 struct wl_cursor *cursor;
3471 assert(callback == input->cursor_frame_cb);
3472 wl_callback_destroy(callback);
3473 input->cursor_frame_cb = NULL;
3476 if (!input->pointer)
3479 if (input->current_cursor == CURSOR_BLANK) {
3480 wl_pointer_set_cursor(input->pointer,
3481 input->pointer_enter_serial,
3486 if (input->current_cursor == CURSOR_UNSET)
3488 cursor = input->display->cursors[input->current_cursor];
3492 /* FIXME We don't have the current time on the first call so we set
3493 * the animation start to the time of the first frame callback. */
3495 input->cursor_anim_start = 0;
3496 else if (input->cursor_anim_start == 0)
3497 input->cursor_anim_start = time;
3499 if (time == 0 || input->cursor_anim_start == 0)
3502 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3504 if (cursor->image_count > 1) {
3505 input->cursor_frame_cb =
3506 wl_surface_frame(input->pointer_surface);
3507 wl_callback_add_listener(input->cursor_frame_cb,
3508 &pointer_surface_listener, input);
3511 input_set_pointer_image_index(input, i);
3514 static const struct wl_callback_listener pointer_surface_listener = {
3515 pointer_surface_frame_callback
3519 input_set_pointer_image(struct input *input, int pointer)
3523 if (!input->pointer)
3526 if (input->pointer_enter_serial > input->cursor_serial)
3529 if (!force && pointer == input->current_cursor)
3532 input->current_cursor = pointer;
3533 input->cursor_serial = input->pointer_enter_serial;
3534 if (!input->cursor_frame_cb)
3535 pointer_surface_frame_callback(input, NULL, 0);
3537 /* The current frame callback may be stuck if, for instance,
3538 * the set cursor request was processed by the server after
3539 * this client lost the focus. In this case the cursor surface
3540 * might not be mapped and the frame callback wouldn't ever
3541 * complete. Send a set_cursor and attach to try to map the
3542 * cursor surface again so that the callback will finish */
3543 input_set_pointer_image_index(input, 0);
3547 struct wl_data_device *
3548 input_get_data_device(struct input *input)
3550 return input->data_device;
3554 input_set_selection(struct input *input,
3555 struct wl_data_source *source, uint32_t time)
3557 wl_data_device_set_selection(input->data_device, source, time);
3561 input_accept(struct input *input, const char *type)
3563 wl_data_offer_accept(input->drag_offer->offer,
3564 input->drag_enter_serial, type);
3568 offer_io_func(struct task *task, uint32_t events)
3570 struct data_offer *offer =
3571 container_of(task, struct data_offer, io_task);
3575 len = read(offer->fd, buffer, sizeof buffer);
3576 offer->func(buffer, len,
3577 offer->x, offer->y, offer->user_data);
3581 data_offer_destroy(offer);
3586 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3587 data_func_t func, void *user_data)
3591 if (pipe2(p, O_CLOEXEC) == -1)
3594 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3597 offer->io_task.run = offer_io_func;
3601 offer->user_data = user_data;
3603 display_watch_fd(offer->input->display,
3604 offer->fd, EPOLLIN, &offer->io_task);
3608 input_receive_drag_data(struct input *input, const char *mime_type,
3609 data_func_t func, void *data)
3611 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3612 input->drag_offer->x = input->drag_x;
3613 input->drag_offer->y = input->drag_y;
3617 input_receive_drag_data_to_fd(struct input *input,
3618 const char *mime_type, int fd)
3620 if (input->drag_offer)
3621 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3627 input_receive_selection_data(struct input *input, const char *mime_type,
3628 data_func_t func, void *data)
3632 if (input->selection_offer == NULL)
3635 for (p = input->selection_offer->types.data; *p; p++)
3636 if (strcmp(mime_type, *p) == 0)
3642 data_offer_receive_data(input->selection_offer,
3643 mime_type, func, data);
3648 input_receive_selection_data_to_fd(struct input *input,
3649 const char *mime_type, int fd)
3651 if (input->selection_offer)
3652 wl_data_offer_receive(input->selection_offer->offer,
3659 window_move(struct window *window, struct input *input, uint32_t serial)
3661 if (!window->shell_surface)
3664 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3668 surface_set_synchronized(struct surface *surface)
3670 if (!surface->subsurface)
3673 if (surface->synchronized)
3676 wl_subsurface_set_sync(surface->subsurface);
3677 surface->synchronized = 1;
3681 surface_set_synchronized_default(struct surface *surface)
3683 if (!surface->subsurface)
3686 if (surface->synchronized == surface->synchronized_default)
3689 if (surface->synchronized_default)
3690 wl_subsurface_set_sync(surface->subsurface);
3692 wl_subsurface_set_desync(surface->subsurface);
3694 surface->synchronized = surface->synchronized_default;
3698 surface_resize(struct surface *surface)
3700 struct widget *widget = surface->widget;
3701 struct wl_compositor *compositor = widget->window->display->compositor;
3703 if (surface->input_region) {
3704 wl_region_destroy(surface->input_region);
3705 surface->input_region = NULL;
3708 if (surface->opaque_region)
3709 wl_region_destroy(surface->opaque_region);
3711 surface->opaque_region = wl_compositor_create_region(compositor);
3713 if (widget->resize_handler)
3714 widget->resize_handler(widget,
3715 widget->allocation.width,
3716 widget->allocation.height,
3719 if (surface->subsurface &&
3720 (surface->allocation.x != widget->allocation.x ||
3721 surface->allocation.y != widget->allocation.y)) {
3722 wl_subsurface_set_position(surface->subsurface,
3723 widget->allocation.x,
3724 widget->allocation.y);
3726 if (surface->allocation.width != widget->allocation.width ||
3727 surface->allocation.height != widget->allocation.height) {
3728 window_schedule_redraw(widget->window);
3730 surface->allocation = widget->allocation;
3733 wl_region_add(surface->opaque_region, 0, 0,
3734 widget->allocation.width,
3735 widget->allocation.height);
3739 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3742 * This hack should be removed, when EGL respects
3743 * eglSwapInterval(0).
3745 * If this window has sub-surfaces, especially a free-running
3746 * EGL-widget, we need to post the parent surface once with
3747 * all the old state to guarantee, that the EGL-widget will
3748 * receive its frame callback soon. Otherwise, a forced call
3749 * to eglSwapBuffers may end up blocking, waiting for a frame
3750 * event that will never come, because we will commit the parent
3751 * surface with all new state only after eglSwapBuffers returns.
3753 * This assumes, that:
3754 * 1. When the EGL widget's resize hook is called, it pauses.
3755 * 2. When the EGL widget's redraw hook is called, it forces a
3756 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3757 * In a single threaded application condition 1 is a no-op.
3759 * XXX: This should actually be after the surface_resize() calls,
3760 * but cannot, because then it would commit the incomplete state
3761 * accumulated from the widget resize hooks.
3763 if (window->subsurface_list.next != &window->main_surface->link ||
3764 window->subsurface_list.prev != &window->main_surface->link)
3765 wl_surface_commit(window->main_surface->surface);
3769 window_do_resize(struct window *window)
3771 struct surface *surface;
3773 widget_set_allocation(window->main_surface->widget,
3774 window->pending_allocation.x,
3775 window->pending_allocation.y,
3776 window->pending_allocation.width,
3777 window->pending_allocation.height);
3779 surface_resize(window->main_surface);
3781 /* The main surface is in the list, too. Main surface's
3782 * resize_handler is responsible for calling widget_set_allocation()
3783 * on all sub-surface root widgets, so they will be resized
3786 wl_list_for_each(surface, &window->subsurface_list, link) {
3787 if (surface == window->main_surface)
3790 surface_set_synchronized(surface);
3791 surface_resize(surface);
3796 idle_resize(struct window *window)
3798 window->resize_needed = 0;
3799 window->redraw_needed = 1;
3801 DBG("from %dx%d to %dx%d\n",
3802 window->main_surface->server_allocation.width,
3803 window->main_surface->server_allocation.height,
3804 window->pending_allocation.width,
3805 window->pending_allocation.height);
3807 hack_prevent_EGL_sub_surface_deadlock(window);
3809 window_do_resize(window);
3813 undo_resize(struct window *window)
3815 window->pending_allocation.width =
3816 window->main_surface->server_allocation.width;
3817 window->pending_allocation.height =
3818 window->main_surface->server_allocation.height;
3820 DBG("back to %dx%d\n",
3821 window->main_surface->server_allocation.width,
3822 window->main_surface->server_allocation.height);
3824 window_do_resize(window);
3826 if (window->pending_allocation.width == 0 &&
3827 window->pending_allocation.height == 0) {
3828 fprintf(stderr, "Error: Could not draw a surface, "
3829 "most likely due to insufficient disk space in "
3830 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3836 window_schedule_resize(struct window *window, int width, int height)
3838 /* We should probably get these numbers from the theme. */
3839 const int min_width = 200, min_height = 200;
3841 window->pending_allocation.x = 0;
3842 window->pending_allocation.y = 0;
3843 window->pending_allocation.width = width;
3844 window->pending_allocation.height = height;
3846 if (window->min_allocation.width == 0) {
3847 if (width < min_width && window->frame)
3848 window->min_allocation.width = min_width;
3850 window->min_allocation.width = width;
3851 if (height < min_height && window->frame)
3852 window->min_allocation.height = min_height;
3854 window->min_allocation.height = height;
3857 if (window->pending_allocation.width < window->min_allocation.width)
3858 window->pending_allocation.width = window->min_allocation.width;
3859 if (window->pending_allocation.height < window->min_allocation.height)
3860 window->pending_allocation.height = window->min_allocation.height;
3862 window->resize_needed = 1;
3863 window_schedule_redraw(window);
3867 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3869 window_schedule_resize(widget->window, width, height);
3873 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3876 wl_shell_surface_pong(shell_surface, serial);
3880 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3881 uint32_t edges, int32_t width, int32_t height)
3883 struct window *window = data;
3885 window->resize_edges = edges;
3886 window_schedule_resize(window, width, height);
3890 menu_destroy(struct menu *menu)
3892 widget_destroy(menu->widget);
3893 window_destroy(menu->window);
3894 frame_destroy(menu->frame);
3899 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3901 struct window *window = data;
3902 struct menu *menu = window->main_surface->widget->user_data;
3904 /* FIXME: Need more context in this event, at least the input
3905 * device. Or just use wl_callback. And this really needs to
3906 * be a window vfunc that the menu can set. And we need the
3909 input_ungrab(menu->input);
3913 static const struct wl_shell_surface_listener shell_surface_listener = {
3920 window_get_allocation(struct window *window,
3921 struct rectangle *allocation)
3923 *allocation = window->main_surface->allocation;
3927 widget_redraw(struct widget *widget)
3929 struct widget *child;
3931 if (widget->redraw_handler)
3932 widget->redraw_handler(widget, widget->user_data);
3933 wl_list_for_each(child, &widget->child_list, link)
3934 widget_redraw(child);
3938 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3940 struct surface *surface = data;
3942 assert(callback == surface->frame_cb);
3943 DBG_OBJ(callback, "done\n");
3944 wl_callback_destroy(callback);
3945 surface->frame_cb = NULL;
3947 surface->last_time = time;
3949 if (surface->redraw_needed || surface->window->redraw_needed) {
3950 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3951 window_schedule_redraw_task(surface->window);
3955 static const struct wl_callback_listener listener = {
3960 surface_redraw(struct surface *surface)
3962 DBG_OBJ(surface->surface, "begin\n");
3964 if (!surface->window->redraw_needed && !surface->redraw_needed)
3967 /* Whole-window redraw forces a redraw even if the previous has
3968 * not yet hit the screen.
3970 if (surface->frame_cb) {
3971 if (!surface->window->redraw_needed)
3974 DBG_OBJ(surface->frame_cb, "cancelled\n");
3975 wl_callback_destroy(surface->frame_cb);
3978 if (surface->widget->use_cairo &&
3979 !widget_get_cairo_surface(surface->widget)) {
3980 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3984 surface->frame_cb = wl_surface_frame(surface->surface);
3985 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3986 DBG_OBJ(surface->frame_cb, "new\n");
3988 surface->redraw_needed = 0;
3989 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3990 widget_redraw(surface->widget);
3991 DBG_OBJ(surface->surface, "done\n");
3996 idle_redraw(struct task *task, uint32_t events)
3998 struct window *window = container_of(task, struct window, redraw_task);
3999 struct surface *surface;
4003 DBG(" --------- \n");
4005 wl_list_init(&window->redraw_task.link);
4006 window->redraw_task_scheduled = 0;
4008 if (window->resize_needed) {
4009 /* throttle resizing to the main surface display */
4010 if (window->main_surface->frame_cb) {
4011 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4015 idle_resize(window);
4019 if (surface_redraw(window->main_surface) < 0) {
4021 * Only main_surface failure will cause us to undo the resize.
4022 * If sub-surfaces fail, they will just be broken with old
4027 wl_list_for_each(surface, &window->subsurface_list, link) {
4028 if (surface == window->main_surface)
4031 surface_redraw(surface);
4035 window->redraw_needed = 0;
4036 window_flush(window);
4038 wl_list_for_each(surface, &window->subsurface_list, link)
4039 surface_set_synchronized_default(surface);
4041 if (resized && failed) {
4042 /* Restore widget tree to correspond to what is on screen. */
4043 undo_resize(window);
4048 window_schedule_redraw_task(struct window *window)
4050 if (window->configure_requests)
4052 if (!window->redraw_task_scheduled) {
4053 window->redraw_task.run = idle_redraw;
4054 display_defer(window->display, &window->redraw_task);
4055 window->redraw_task_scheduled = 1;
4060 window_schedule_redraw(struct window *window)
4062 struct surface *surface;
4064 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4066 wl_list_for_each(surface, &window->subsurface_list, link)
4067 surface->redraw_needed = 1;
4069 window_schedule_redraw_task(window);
4073 window_is_fullscreen(struct window *window)
4075 return window->type == TYPE_FULLSCREEN;
4079 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4081 struct window *window = data;
4083 wl_callback_destroy(callback);
4084 window->configure_requests--;
4086 if (!window->configure_requests)
4087 window_schedule_redraw(window);
4090 static struct wl_callback_listener configure_request_listener = {
4091 configure_request_completed,
4095 window_defer_redraw_until_configure(struct window* window)
4097 struct wl_callback *callback;
4099 if (window->redraw_task_scheduled) {
4100 wl_list_remove(&window->redraw_task.link);
4101 window->redraw_task_scheduled = 0;
4104 callback = wl_display_sync(window->display->display);
4105 wl_callback_add_listener(callback, &configure_request_listener, window);
4106 window->configure_requests++;
4110 window_set_fullscreen(struct window *window, int fullscreen)
4112 if (!window->display->shell)
4115 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4119 window->saved_type = window->type;
4120 if (window->type == TYPE_TOPLEVEL) {
4121 window->saved_allocation = window->main_surface->allocation;
4123 window->type = TYPE_FULLSCREEN;
4124 wl_shell_surface_set_fullscreen(window->shell_surface,
4125 window->fullscreen_method,
4127 window_defer_redraw_until_configure (window);
4129 if (window->saved_type == TYPE_MAXIMIZED) {
4130 window_set_maximized(window, 1);
4132 window->type = TYPE_TOPLEVEL;
4133 wl_shell_surface_set_toplevel(window->shell_surface);
4134 window_schedule_resize(window,
4135 window->saved_allocation.width,
4136 window->saved_allocation.height);
4143 window_set_fullscreen_method(struct window *window,
4144 enum wl_shell_surface_fullscreen_method method)
4146 window->fullscreen_method = method;
4150 window_is_maximized(struct window *window)
4152 return window->type == TYPE_MAXIMIZED;
4156 window_set_maximized(struct window *window, int maximized)
4158 if (!window->display->shell)
4161 if ((window->type == TYPE_MAXIMIZED) == maximized)
4164 if (window->type == TYPE_TOPLEVEL) {
4165 window->saved_allocation = window->main_surface->allocation;
4166 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4167 window->type = TYPE_MAXIMIZED;
4168 window_defer_redraw_until_configure(window);
4169 } else if (window->type == TYPE_FULLSCREEN) {
4170 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4171 window->type = TYPE_MAXIMIZED;
4172 window_defer_redraw_until_configure(window);
4174 wl_shell_surface_set_toplevel(window->shell_surface);
4175 window->type = TYPE_TOPLEVEL;
4176 window_schedule_resize(window,
4177 window->saved_allocation.width,
4178 window->saved_allocation.height);
4183 window_set_user_data(struct window *window, void *data)
4185 window->user_data = data;
4189 window_get_user_data(struct window *window)
4191 return window->user_data;
4195 window_set_key_handler(struct window *window,
4196 window_key_handler_t handler)
4198 window->key_handler = handler;
4202 window_set_keyboard_focus_handler(struct window *window,
4203 window_keyboard_focus_handler_t handler)
4205 window->keyboard_focus_handler = handler;
4209 window_set_data_handler(struct window *window, window_data_handler_t handler)
4211 window->data_handler = handler;
4215 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4217 window->drop_handler = handler;
4221 window_set_close_handler(struct window *window,
4222 window_close_handler_t handler)
4224 window->close_handler = handler;
4228 window_set_fullscreen_handler(struct window *window,
4229 window_fullscreen_handler_t handler)
4231 window->fullscreen_handler = handler;
4235 window_set_output_handler(struct window *window,
4236 window_output_handler_t handler)
4238 window->output_handler = handler;
4242 window_set_title(struct window *window, const char *title)
4244 free(window->title);
4245 window->title = strdup(title);
4246 if (window->frame) {
4247 frame_set_title(window->frame->frame, title);
4248 widget_schedule_redraw(window->frame->widget);
4250 if (window->shell_surface)
4251 wl_shell_surface_set_title(window->shell_surface, title);
4255 window_get_title(struct window *window)
4257 return window->title;
4261 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4263 struct text_cursor_position *text_cursor_position =
4264 window->display->text_cursor_position;
4266 if (!text_cursor_position)
4269 text_cursor_position_notify(text_cursor_position,
4270 window->main_surface->surface,
4271 wl_fixed_from_int(x),
4272 wl_fixed_from_int(y));
4276 window_damage(struct window *window, int32_t x, int32_t y,
4277 int32_t width, int32_t height)
4279 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4283 surface_enter(void *data,
4284 struct wl_surface *wl_surface, struct wl_output *wl_output)
4286 struct window *window = data;
4287 struct output *output;
4288 struct output *output_found = NULL;
4289 struct window_output *window_output;
4291 wl_list_for_each(output, &window->display->output_list, link) {
4292 if (output->output == wl_output) {
4293 output_found = output;
4301 window_output = xmalloc(sizeof *window_output);
4302 window_output->output = output_found;
4304 wl_list_insert (&window->window_output_list, &window_output->link);
4306 if (window->output_handler)
4307 window->output_handler(window, output_found, 1,
4312 surface_leave(void *data,
4313 struct wl_surface *wl_surface, struct wl_output *output)
4315 struct window *window = data;
4316 struct window_output *window_output;
4317 struct window_output *window_output_found = NULL;
4319 wl_list_for_each(window_output, &window->window_output_list, link) {
4320 if (window_output->output->output == output) {
4321 window_output_found = window_output;
4326 if (window_output_found) {
4327 wl_list_remove(&window_output_found->link);
4329 if (window->output_handler)
4330 window->output_handler(window, window_output->output,
4331 0, window->user_data);
4333 free(window_output_found);
4337 static const struct wl_surface_listener surface_listener = {
4342 static struct surface *
4343 surface_create(struct window *window)
4345 struct display *display = window->display;
4346 struct surface *surface;
4348 surface = xmalloc(sizeof *surface);
4349 memset(surface, 0, sizeof *surface);
4353 surface->window = window;
4354 surface->surface = wl_compositor_create_surface(display->compositor);
4355 surface->buffer_scale = 1;
4356 wl_surface_add_listener(surface->surface, &surface_listener, window);
4358 wl_list_insert(&window->subsurface_list, &surface->link);
4363 static struct window *
4364 window_create_internal(struct display *display, int type)
4366 struct window *window;
4367 struct surface *surface;
4369 window = xzalloc(sizeof *window);
4370 wl_list_init(&window->subsurface_list);
4371 window->display = display;
4373 surface = surface_create(window);
4374 window->main_surface = surface;
4376 if (type != TYPE_CUSTOM && display->shell) {
4377 window->shell_surface =
4378 wl_shell_get_shell_surface(display->shell,
4380 fail_on_null(window->shell_surface);
4383 window->type = type;
4384 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4385 window->configure_requests = 0;
4386 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4388 if (display->argb_device)
4389 #ifdef HAVE_CAIRO_EGL
4390 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4392 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4395 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4397 wl_surface_set_user_data(surface->surface, window);
4398 wl_list_insert(display->window_list.prev, &window->link);
4399 wl_list_init(&window->redraw_task.link);
4401 if (window->shell_surface) {
4402 wl_shell_surface_set_user_data(window->shell_surface, window);
4403 wl_shell_surface_add_listener(window->shell_surface,
4404 &shell_surface_listener, window);
4407 wl_list_init (&window->window_output_list);
4413 window_create(struct display *display)
4415 return window_create_internal(display, TYPE_NONE);
4419 window_create_custom(struct display *display)
4421 return window_create_internal(display, TYPE_CUSTOM);
4425 menu_set_item(struct menu *menu, int sy)
4427 int32_t x, y, width, height;
4430 frame_interior(menu->frame, &x, &y, &width, &height);
4431 next = (sy - y) / 20;
4432 if (menu->current != next) {
4433 menu->current = next;
4434 widget_schedule_redraw(menu->widget);
4439 menu_motion_handler(struct widget *widget,
4440 struct input *input, uint32_t time,
4441 float x, float y, void *data)
4443 struct menu *menu = data;
4445 if (widget == menu->widget)
4446 menu_set_item(data, y);
4448 return CURSOR_LEFT_PTR;
4452 menu_enter_handler(struct widget *widget,
4453 struct input *input, float x, float y, void *data)
4455 struct menu *menu = data;
4457 if (widget == menu->widget)
4458 menu_set_item(data, y);
4460 return CURSOR_LEFT_PTR;
4464 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4466 struct menu *menu = data;
4468 if (widget == menu->widget)
4469 menu_set_item(data, -200);
4473 menu_button_handler(struct widget *widget,
4474 struct input *input, uint32_t time,
4475 uint32_t button, enum wl_pointer_button_state state,
4479 struct menu *menu = data;
4481 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4482 (menu->release_count > 0 || time - menu->time > 500)) {
4483 /* Either relase after press-drag-release or
4484 * click-motion-click. */
4485 menu->func(menu->parent, input,
4486 menu->current, menu->parent->user_data);
4487 input_ungrab(input);
4489 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4490 menu->release_count++;
4495 menu_redraw_handler(struct widget *widget, void *data)
4498 struct menu *menu = data;
4499 int32_t x, y, width, height, i;
4501 cr = widget_cairo_create(widget);
4503 frame_repaint(menu->frame, cr);
4504 frame_interior(menu->frame, &x, &y, &width, &height);
4506 theme_set_background_source(menu->window->display->theme,
4507 cr, THEME_FRAME_ACTIVE);
4508 cairo_rectangle(cr, x, y, width, height);
4511 cairo_select_font_face(cr, "sans",
4512 CAIRO_FONT_SLANT_NORMAL,
4513 CAIRO_FONT_WEIGHT_NORMAL);
4514 cairo_set_font_size(cr, 12);
4516 for (i = 0; i < menu->count; i++) {
4517 if (i == menu->current) {
4518 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4519 cairo_rectangle(cr, x, y + i * 20, width, 20);
4521 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4522 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4523 cairo_show_text(cr, menu->entries[i]);
4525 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4526 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4527 cairo_show_text(cr, menu->entries[i]);
4535 window_show_menu(struct display *display,
4536 struct input *input, uint32_t time, struct window *parent,
4537 int32_t x, int32_t y,
4538 menu_func_t func, const char **entries, int count)
4540 struct window *window;
4544 menu = malloc(sizeof *menu);
4548 window = window_create_internal(parent->display, TYPE_MENU);
4554 menu->window = window;
4555 menu->parent = parent;
4556 menu->widget = window_add_widget(menu->window, menu);
4557 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4558 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4559 menu->frame = frame_create(window->display->theme, 0, 0,
4560 FRAME_BUTTON_NONE, NULL);
4561 fail_on_null(menu->frame);
4562 menu->entries = entries;
4563 menu->count = count;
4564 menu->release_count = 0;
4568 menu->input = input;
4569 window->type = TYPE_MENU;
4573 input_ungrab(input);
4575 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4576 widget_set_enter_handler(menu->widget, menu_enter_handler);
4577 widget_set_leave_handler(menu->widget, menu_leave_handler);
4578 widget_set_motion_handler(menu->widget, menu_motion_handler);
4579 widget_set_button_handler(menu->widget, menu_button_handler);
4581 input_grab(input, menu->widget, 0);
4582 frame_resize_inside(menu->frame, 200, count * 20);
4583 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4584 window_schedule_resize(window, frame_width(menu->frame),
4585 frame_height(menu->frame));
4587 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4588 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4589 display_get_serial(window->display),
4590 parent->main_surface->surface,
4591 window->x - ix, window->y - iy, 0);
4595 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4597 window->main_surface->buffer_type = type;
4601 window_set_preferred_format(struct window *window,
4602 enum preferred_format format)
4604 window->preferred_format = format;
4608 window_add_subsurface(struct window *window, void *data,
4609 enum subsurface_mode default_mode)
4611 struct widget *widget;
4612 struct surface *surface;
4613 struct wl_surface *parent;
4614 struct wl_subcompositor *subcompo = window->display->subcompositor;
4616 surface = surface_create(window);
4617 widget = widget_create(window, surface, data);
4618 wl_list_init(&widget->link);
4619 surface->widget = widget;
4621 parent = window->main_surface->surface;
4622 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4625 surface->synchronized = 1;
4627 switch (default_mode) {
4628 case SUBSURFACE_SYNCHRONIZED:
4629 surface->synchronized_default = 1;
4631 case SUBSURFACE_DESYNCHRONIZED:
4632 surface->synchronized_default = 0;
4635 assert(!"bad enum subsurface_mode");
4638 window->resize_needed = 1;
4639 window_schedule_redraw(window);
4645 display_handle_geometry(void *data,
4646 struct wl_output *wl_output,
4649 int physical_height,
4655 struct output *output = data;
4657 output->allocation.x = x;
4658 output->allocation.y = y;
4659 output->transform = transform;
4663 display_handle_done(void *data,
4664 struct wl_output *wl_output)
4669 display_handle_scale(void *data,
4670 struct wl_output *wl_output,
4673 struct output *output = data;
4675 output->scale = scale;
4679 display_handle_mode(void *data,
4680 struct wl_output *wl_output,
4686 struct output *output = data;
4687 struct display *display = output->display;
4689 if (flags & WL_OUTPUT_MODE_CURRENT) {
4690 output->allocation.width = width;
4691 output->allocation.height = height;
4692 if (display->output_configure_handler)
4693 (*display->output_configure_handler)(
4694 output, display->user_data);
4698 static const struct wl_output_listener output_listener = {
4699 display_handle_geometry,
4700 display_handle_mode,
4701 display_handle_done,
4702 display_handle_scale
4706 display_add_output(struct display *d, uint32_t id)
4708 struct output *output;
4710 output = xzalloc(sizeof *output);
4711 output->display = d;
4714 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4715 output->server_output_id = id;
4716 wl_list_insert(d->output_list.prev, &output->link);
4718 wl_output_add_listener(output->output, &output_listener, output);
4722 output_destroy(struct output *output)
4724 if (output->destroy_handler)
4725 (*output->destroy_handler)(output, output->user_data);
4727 wl_output_destroy(output->output);
4728 wl_list_remove(&output->link);
4733 display_destroy_output(struct display *d, uint32_t id)
4735 struct output *output;
4737 wl_list_for_each(output, &d->output_list, link) {
4738 if (output->server_output_id == id) {
4739 output_destroy(output);
4746 display_set_global_handler(struct display *display,
4747 display_global_handler_t handler)
4749 struct global *global;
4751 display->global_handler = handler;
4755 wl_list_for_each(global, &display->global_list, link)
4756 display->global_handler(display,
4757 global->name, global->interface,
4758 global->version, display->user_data);
4762 display_set_global_handler_remove(struct display *display,
4763 display_global_handler_t remove_handler)
4765 display->global_handler_remove = remove_handler;
4766 if (!remove_handler)
4771 display_set_output_configure_handler(struct display *display,
4772 display_output_handler_t handler)
4774 struct output *output;
4776 display->output_configure_handler = handler;
4780 wl_list_for_each(output, &display->output_list, link) {
4781 if (output->allocation.width == 0 &&
4782 output->allocation.height == 0)
4785 (*display->output_configure_handler)(output,
4786 display->user_data);
4791 output_set_user_data(struct output *output, void *data)
4793 output->user_data = data;
4797 output_get_user_data(struct output *output)
4799 return output->user_data;
4803 output_set_destroy_handler(struct output *output,
4804 display_output_handler_t handler)
4806 output->destroy_handler = handler;
4807 /* FIXME: implement this, once we have way to remove outputs */
4811 output_get_allocation(struct output *output, struct rectangle *base)
4813 struct rectangle allocation = output->allocation;
4815 switch (output->transform) {
4816 case WL_OUTPUT_TRANSFORM_90:
4817 case WL_OUTPUT_TRANSFORM_270:
4818 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4819 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4820 /* Swap width and height */
4821 allocation.width = output->allocation.height;
4822 allocation.height = output->allocation.width;
4830 output_get_wl_output(struct output *output)
4832 return output->output;
4835 enum wl_output_transform
4836 output_get_transform(struct output *output)
4838 return output->transform;
4842 output_get_scale(struct output *output)
4844 return output->scale;
4848 fini_xkb(struct input *input)
4850 xkb_state_unref(input->xkb.state);
4851 xkb_map_unref(input->xkb.keymap);
4854 #define MIN(a,b) ((a) < (b) ? a : b)
4857 display_add_input(struct display *d, uint32_t id)
4859 struct input *input;
4861 input = xzalloc(sizeof *input);
4863 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4864 MIN(d->seat_version, 3));
4865 input->touch_focus = NULL;
4866 input->pointer_focus = NULL;
4867 input->keyboard_focus = NULL;
4868 wl_list_init(&input->touch_point_list);
4869 wl_list_insert(d->input_list.prev, &input->link);
4871 wl_seat_add_listener(input->seat, &seat_listener, input);
4872 wl_seat_set_user_data(input->seat, input);
4874 input->data_device =
4875 wl_data_device_manager_get_data_device(d->data_device_manager,
4877 wl_data_device_add_listener(input->data_device, &data_device_listener,
4880 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4882 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4883 TFD_CLOEXEC | TFD_NONBLOCK);
4884 input->repeat_task.run = keyboard_repeat_func;
4885 display_watch_fd(d, input->repeat_timer_fd,
4886 EPOLLIN, &input->repeat_task);
4890 input_destroy(struct input *input)
4892 input_remove_keyboard_focus(input);
4893 input_remove_pointer_focus(input);
4895 if (input->drag_offer)
4896 data_offer_destroy(input->drag_offer);
4898 if (input->selection_offer)
4899 data_offer_destroy(input->selection_offer);
4901 wl_data_device_destroy(input->data_device);
4903 if (input->display->seat_version >= 3) {
4905 wl_pointer_release(input->pointer);
4906 if (input->keyboard)
4907 wl_keyboard_release(input->keyboard);
4912 wl_surface_destroy(input->pointer_surface);
4914 wl_list_remove(&input->link);
4915 wl_seat_destroy(input->seat);
4916 close(input->repeat_timer_fd);
4921 init_workspace_manager(struct display *d, uint32_t id)
4923 d->workspace_manager =
4924 wl_registry_bind(d->registry, id,
4925 &workspace_manager_interface, 1);
4926 if (d->workspace_manager != NULL)
4927 workspace_manager_add_listener(d->workspace_manager,
4928 &workspace_manager_listener,
4933 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4935 struct display *d = data;
4937 if (format == WL_SHM_FORMAT_RGB565)
4941 struct wl_shm_listener shm_listener = {
4946 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4947 const char *interface, uint32_t version)
4949 struct display *d = data;
4950 struct global *global;
4952 global = xmalloc(sizeof *global);
4954 global->interface = strdup(interface);
4955 global->version = version;
4956 wl_list_insert(d->global_list.prev, &global->link);
4958 if (strcmp(interface, "wl_compositor") == 0) {
4959 d->compositor = wl_registry_bind(registry, id,
4960 &wl_compositor_interface, 3);
4961 } else if (strcmp(interface, "wl_output") == 0) {
4962 display_add_output(d, id);
4963 } else if (strcmp(interface, "wl_seat") == 0) {
4964 d->seat_version = version;
4965 display_add_input(d, id);
4966 } else if (strcmp(interface, "wl_shell") == 0) {
4967 d->shell = wl_registry_bind(registry,
4968 id, &wl_shell_interface, 1);
4969 } else if (strcmp(interface, "wl_shm") == 0) {
4970 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4971 wl_shm_add_listener(d->shm, &shm_listener, d);
4972 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4973 d->data_device_manager =
4974 wl_registry_bind(registry, id,
4975 &wl_data_device_manager_interface, 1);
4976 } else if (strcmp(interface, "text_cursor_position") == 0) {
4977 d->text_cursor_position =
4978 wl_registry_bind(registry, id,
4979 &text_cursor_position_interface, 1);
4980 } else if (strcmp(interface, "workspace_manager") == 0) {
4981 init_workspace_manager(d, id);
4982 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4984 wl_registry_bind(registry, id,
4985 &wl_subcompositor_interface, 1);
4988 if (d->global_handler)
4989 d->global_handler(d, id, interface, version, d->user_data);
4993 registry_handle_global_remove(void *data, struct wl_registry *registry,
4996 struct display *d = data;
4997 struct global *global;
5000 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5001 if (global->name != name)
5004 if (strcmp(global->interface, "wl_output") == 0)
5005 display_destroy_output(d, name);
5007 /* XXX: Should destroy remaining bound globals */
5009 if (d->global_handler_remove)
5010 d->global_handler_remove(d, name, global->interface,
5011 global->version, d->user_data);
5013 wl_list_remove(&global->link);
5014 free(global->interface);
5020 display_bind(struct display *display, uint32_t name,
5021 const struct wl_interface *interface, uint32_t version)
5023 return wl_registry_bind(display->registry, name, interface, version);
5026 static const struct wl_registry_listener registry_listener = {
5027 registry_handle_global,
5028 registry_handle_global_remove
5031 #ifdef HAVE_CAIRO_EGL
5033 init_egl(struct display *d)
5035 EGLint major, minor;
5038 #ifdef USE_CAIRO_GLESV2
5039 # define GL_BIT EGL_OPENGL_ES2_BIT
5041 # define GL_BIT EGL_OPENGL_BIT
5044 static const EGLint argb_cfg_attribs[] = {
5045 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5051 EGL_RENDERABLE_TYPE, GL_BIT,
5055 #ifdef USE_CAIRO_GLESV2
5056 static const EGLint context_attribs[] = {
5057 EGL_CONTEXT_CLIENT_VERSION, 2,
5060 EGLint api = EGL_OPENGL_ES_API;
5062 EGLint *context_attribs = NULL;
5063 EGLint api = EGL_OPENGL_API;
5066 d->dpy = eglGetDisplay(d->display);
5067 if (!eglInitialize(d->dpy, &major, &minor)) {
5068 fprintf(stderr, "failed to initialize EGL\n");
5072 if (!eglBindAPI(api)) {
5073 fprintf(stderr, "failed to bind EGL client API\n");
5077 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5078 &d->argb_config, 1, &n) || n != 1) {
5079 fprintf(stderr, "failed to choose argb EGL config\n");
5083 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5084 EGL_NO_CONTEXT, context_attribs);
5085 if (d->argb_ctx == NULL) {
5086 fprintf(stderr, "failed to create EGL context\n");
5090 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5091 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5092 fprintf(stderr, "failed to get cairo EGL argb device\n");
5100 fini_egl(struct display *display)
5102 cairo_device_destroy(display->argb_device);
5104 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5107 eglTerminate(display->dpy);
5113 init_dummy_surface(struct display *display)
5118 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5120 display->dummy_surface =
5121 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5123 display->dummy_surface_data = data;
5127 handle_display_data(struct task *task, uint32_t events)
5129 struct display *display =
5130 container_of(task, struct display, display_task);
5131 struct epoll_event ep;
5134 display->display_fd_events = events;
5136 if (events & EPOLLERR || events & EPOLLHUP) {
5137 display_exit(display);
5141 if (events & EPOLLIN) {
5142 ret = wl_display_dispatch(display->display);
5144 display_exit(display);
5149 if (events & EPOLLOUT) {
5150 ret = wl_display_flush(display->display);
5152 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5153 ep.data.ptr = &display->display_task;
5154 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5155 display->display_fd, &ep);
5156 } else if (ret == -1 && errno != EAGAIN) {
5157 display_exit(display);
5164 log_handler(const char *format, va_list args)
5166 vfprintf(stderr, format, args);
5170 display_create(int *argc, char *argv[])
5174 wl_log_set_handler_client(log_handler);
5176 d = zalloc(sizeof *d);
5180 d->display = wl_display_connect(NULL);
5181 if (d->display == NULL) {
5182 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5187 d->xkb_context = xkb_context_new(0);
5188 if (d->xkb_context == NULL) {
5189 fprintf(stderr, "Failed to create XKB context\n");
5194 d->epoll_fd = os_epoll_create_cloexec();
5195 d->display_fd = wl_display_get_fd(d->display);
5196 d->display_task.run = handle_display_data;
5197 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5200 wl_list_init(&d->deferred_list);
5201 wl_list_init(&d->input_list);
5202 wl_list_init(&d->output_list);
5203 wl_list_init(&d->global_list);
5206 d->workspace_count = 1;
5208 d->registry = wl_display_get_registry(d->display);
5209 wl_registry_add_listener(d->registry, ®istry_listener, d);
5211 if (wl_display_dispatch(d->display) < 0) {
5212 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5216 #ifdef HAVE_CAIRO_EGL
5217 if (init_egl(d) < 0)
5218 fprintf(stderr, "EGL does not seem to work, "
5219 "falling back to software rendering and wl_shm.\n");
5224 d->theme = theme_create();
5226 wl_list_init(&d->window_list);
5228 init_dummy_surface(d);
5234 display_destroy_outputs(struct display *display)
5237 struct output *output;
5239 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5240 output_destroy(output);
5244 display_destroy_inputs(struct display *display)
5247 struct input *input;
5249 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5250 input_destroy(input);
5254 display_destroy(struct display *display)
5256 if (!wl_list_empty(&display->window_list))
5257 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5258 wl_list_length(&display->window_list));
5260 if (!wl_list_empty(&display->deferred_list))
5261 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5263 cairo_surface_destroy(display->dummy_surface);
5264 free(display->dummy_surface_data);
5266 display_destroy_outputs(display);
5267 display_destroy_inputs(display);
5269 xkb_context_unref(display->xkb_context);
5271 theme_destroy(display->theme);
5272 destroy_cursors(display);
5274 #ifdef HAVE_CAIRO_EGL
5275 if (display->argb_device)
5279 if (display->subcompositor)
5280 wl_subcompositor_destroy(display->subcompositor);
5283 wl_shell_destroy(display->shell);
5286 wl_shm_destroy(display->shm);
5288 if (display->data_device_manager)
5289 wl_data_device_manager_destroy(display->data_device_manager);
5291 wl_compositor_destroy(display->compositor);
5292 wl_registry_destroy(display->registry);
5294 close(display->epoll_fd);
5296 if (!(display->display_fd_events & EPOLLERR) &&
5297 !(display->display_fd_events & EPOLLHUP))
5298 wl_display_flush(display->display);
5300 wl_display_disconnect(display->display);
5305 display_set_user_data(struct display *display, void *data)
5307 display->user_data = data;
5311 display_get_user_data(struct display *display)
5313 return display->user_data;
5317 display_get_display(struct display *display)
5319 return display->display;
5323 display_has_subcompositor(struct display *display)
5325 if (display->subcompositor)
5328 wl_display_roundtrip(display->display);
5330 return display->subcompositor != NULL;
5334 display_get_cairo_device(struct display *display)
5336 return display->argb_device;
5340 display_get_output(struct display *display)
5342 return container_of(display->output_list.next, struct output, link);
5345 struct wl_compositor *
5346 display_get_compositor(struct display *display)
5348 return display->compositor;
5352 display_get_serial(struct display *display)
5354 return display->serial;
5358 display_get_egl_display(struct display *d)
5363 struct wl_data_source *
5364 display_create_data_source(struct display *display)
5366 return wl_data_device_manager_create_data_source(display->data_device_manager);
5370 display_get_argb_egl_config(struct display *d)
5372 return d->argb_config;
5376 display_acquire_window_surface(struct display *display,
5377 struct window *window,
5380 struct surface *surface = window->main_surface;
5382 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5385 widget_get_cairo_surface(window->main_surface->widget);
5386 return surface->toysurface->acquire(surface->toysurface, ctx);
5390 display_release_window_surface(struct display *display,
5391 struct window *window)
5393 struct surface *surface = window->main_surface;
5395 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5398 surface->toysurface->release(surface->toysurface);
5402 display_defer(struct display *display, struct task *task)
5404 wl_list_insert(&display->deferred_list, &task->link);
5408 display_watch_fd(struct display *display,
5409 int fd, uint32_t events, struct task *task)
5411 struct epoll_event ep;
5415 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5419 display_unwatch_fd(struct display *display, int fd)
5421 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5425 display_run(struct display *display)
5428 struct epoll_event ep[16];
5431 display->running = 1;
5433 while (!wl_list_empty(&display->deferred_list)) {
5434 task = container_of(display->deferred_list.prev,
5436 wl_list_remove(&task->link);
5440 wl_display_dispatch_pending(display->display);
5442 if (!display->running)
5445 ret = wl_display_flush(display->display);
5446 if (ret < 0 && errno == EAGAIN) {
5448 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5449 ep[0].data.ptr = &display->display_task;
5451 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5452 display->display_fd, &ep[0]);
5453 } else if (ret < 0) {
5457 count = epoll_wait(display->epoll_fd,
5458 ep, ARRAY_LENGTH(ep), -1);
5459 for (i = 0; i < count; i++) {
5460 task = ep[i].data.ptr;
5461 task->run(task, ep[i].events);
5467 display_exit(struct display *display)
5469 display->running = 0;
5473 keysym_modifiers_add(struct wl_array *modifiers_map,
5476 size_t len = strlen(name) + 1;
5479 p = wl_array_add(modifiers_map, len);
5484 strncpy(p, name, len);
5487 static xkb_mod_index_t
5488 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5491 xkb_mod_index_t index = 0;
5492 char *p = modifiers_map->data;
5494 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5495 if (strcmp(p, name) == 0)
5502 return XKB_MOD_INVALID;
5506 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5509 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5511 if (index == XKB_MOD_INVALID)
5512 return XKB_MOD_INVALID;
5518 fail_on_null(void *p)
5521 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5531 return fail_on_null(malloc(s));
5537 return fail_on_null(zalloc(s));
5541 xstrdup(const char *s)
5543 return fail_on_null(strdup(s));
5547 xrealloc(char *p, size_t s)
5549 return fail_on_null(realloc(p, s));