2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_subcompositor *subcompositor;
88 struct wl_shell *shell;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
94 EGLConfig argb_config;
96 cairo_device_t *argb_device;
100 uint32_t display_fd_events;
101 struct task display_task;
104 struct wl_list deferred_list;
108 struct wl_list global_list;
109 struct wl_list window_list;
110 struct wl_list input_list;
111 struct wl_list output_list;
115 struct wl_cursor_theme *cursor_theme;
116 struct wl_cursor **cursors;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
120 display_global_handler_t global_handler_remove;
124 struct xkb_context *xkb_context;
127 uint32_t workspace_count;
129 /* A hack to get text extents for tooltips */
130 cairo_surface_t *dummy_surface;
131 void *dummy_surface_data;
147 struct window_output {
148 struct output *output;
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
157 * width,height are the new buffer size.
158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
160 * Returns the Cairo surface to draw to.
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
163 int32_t width, int32_t height, uint32_t flags,
164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
171 void (*swap)(struct toysurface *base,
172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
173 struct rectangle *server_allocation);
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
182 * Release the toysurface from the EGL context, returning control
185 void (*release)(struct toysurface *base);
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
191 void (*destroy)(struct toysurface *base);
195 struct window *window;
197 struct wl_surface *surface;
198 struct wl_subsurface *subsurface;
200 int synchronized_default;
201 struct toysurface *toysurface;
202 struct widget *widget;
204 struct wl_callback *frame_cb;
207 struct rectangle allocation;
208 struct rectangle server_allocation;
210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
215 int32_t buffer_scale;
217 cairo_surface_t *cairo_surface;
223 struct display *display;
224 struct wl_list window_output_list;
226 struct rectangle saved_allocation;
227 struct rectangle min_allocation;
228 struct rectangle pending_allocation;
232 int redraw_task_scheduled;
233 struct task redraw_task;
240 int fullscreen_method;
241 int configure_requests;
243 enum preferred_format preferred_format;
245 window_key_handler_t key_handler;
246 window_keyboard_focus_handler_t keyboard_focus_handler;
247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
249 window_close_handler_t close_handler;
250 window_fullscreen_handler_t fullscreen_handler;
251 window_output_handler_t output_handler;
253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
256 struct window_frame *frame;
258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
266 struct window *window;
267 struct surface *surface;
268 struct tooltip *tooltip;
269 struct wl_list child_list;
271 struct rectangle allocation;
272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
276 widget_motion_handler_t motion_handler;
277 widget_button_handler_t button_handler;
278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
283 widget_axis_handler_t axis_handler;
288 /* If this is set to false then no cairo surface will be
289 * created before redrawing the surface. This is useful if the
290 * redraw handler is going to do completely custom rendering
291 * such as using EGL directly */
298 struct widget *widget;
303 struct display *display;
304 struct wl_seat *seat;
305 struct wl_pointer *pointer;
306 struct wl_keyboard *keyboard;
307 struct wl_touch *touch;
308 struct wl_list touch_point_list;
309 struct window *pointer_focus;
310 struct window *keyboard_focus;
311 struct window *touch_focus;
313 uint32_t cursor_anim_start;
314 struct wl_callback *cursor_frame_cb;
315 struct wl_surface *pointer_surface;
317 uint32_t pointer_enter_serial;
318 uint32_t cursor_serial;
322 struct widget *focus_widget;
324 uint32_t grab_button;
326 struct wl_data_device *data_device;
327 struct data_offer *drag_offer;
328 struct data_offer *selection_offer;
330 int32_t touch_grab_id;
331 float drag_x, drag_y;
332 struct window *drag_focus;
333 uint32_t drag_enter_serial;
336 struct xkb_keymap *keymap;
337 struct xkb_state *state;
338 xkb_mod_mask_t control_mask;
339 xkb_mod_mask_t alt_mask;
340 xkb_mod_mask_t shift_mask;
343 struct task repeat_task;
347 uint32_t repeat_time;
351 struct display *display;
352 struct wl_output *output;
353 uint32_t server_output_id;
354 struct rectangle allocation;
359 display_output_handler_t destroy_handler;
363 struct window_frame {
364 struct widget *widget;
365 struct widget *child;
370 struct window *window;
371 struct window *parent;
372 struct widget *widget;
375 const char **entries;
384 struct widget *parent;
385 struct window *window;
386 struct widget *widget;
388 struct task tooltip_task;
394 struct wl_shm_pool *pool;
401 CURSOR_DEFAULT = 100,
405 enum window_location {
407 WINDOW_RESIZING_TOP = 1,
408 WINDOW_RESIZING_BOTTOM = 2,
409 WINDOW_RESIZING_LEFT = 4,
410 WINDOW_RESIZING_TOP_LEFT = 5,
411 WINDOW_RESIZING_BOTTOM_LEFT = 6,
412 WINDOW_RESIZING_RIGHT = 8,
413 WINDOW_RESIZING_TOP_RIGHT = 9,
414 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
415 WINDOW_RESIZING_MASK = 15,
416 WINDOW_EXTERIOR = 16,
417 WINDOW_TITLEBAR = 17,
418 WINDOW_CLIENT_AREA = 18,
421 static const cairo_user_data_key_t shm_surface_data_key;
428 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
429 __attribute__ ((format (printf, 4, 5)));
432 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
437 gettimeofday(&tv, NULL);
438 fprintf(stderr, "%8ld.%03ld ",
439 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
442 fprintf(stderr, "%s@%d ",
443 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
445 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
446 fprintf(stderr, "%s ", func);
449 vfprintf(stderr, fmt, ap);
453 #define DBG(fmt, ...) \
454 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
456 #define DBG_OBJ(obj, fmt, ...) \
457 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
461 #define DBG(...) do {} while (0)
462 #define DBG_OBJ(...) do {} while (0)
467 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
471 switch (buffer_transform) {
472 case WL_OUTPUT_TRANSFORM_90:
473 case WL_OUTPUT_TRANSFORM_270:
474 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
484 *width *= buffer_scale;
485 *height *= buffer_scale;
489 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
493 switch (buffer_transform) {
494 case WL_OUTPUT_TRANSFORM_90:
495 case WL_OUTPUT_TRANSFORM_270:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
506 *width /= buffer_scale;
507 *height /= buffer_scale;
510 #ifdef HAVE_CAIRO_EGL
512 struct egl_window_surface {
513 struct toysurface base;
514 cairo_surface_t *cairo_surface;
515 struct display *display;
516 struct wl_surface *surface;
517 struct wl_egl_window *egl_window;
518 EGLSurface egl_surface;
521 static struct egl_window_surface *
522 to_egl_window_surface(struct toysurface *base)
524 return container_of(base, struct egl_window_surface, base);
527 static cairo_surface_t *
528 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
529 int32_t width, int32_t height, uint32_t flags,
530 enum wl_output_transform buffer_transform, int32_t buffer_scale)
532 struct egl_window_surface *surface = to_egl_window_surface(base);
534 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
536 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
537 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
539 return cairo_surface_reference(surface->cairo_surface);
543 egl_window_surface_swap(struct toysurface *base,
544 enum wl_output_transform buffer_transform, int32_t buffer_scale,
545 struct rectangle *server_allocation)
547 struct egl_window_surface *surface = to_egl_window_surface(base);
549 cairo_gl_surface_swapbuffers(surface->cairo_surface);
550 wl_egl_window_get_attached_size(surface->egl_window,
551 &server_allocation->width,
552 &server_allocation->height);
554 buffer_to_surface_size (buffer_transform, buffer_scale,
555 &server_allocation->width,
556 &server_allocation->height);
560 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
562 struct egl_window_surface *surface = to_egl_window_surface(base);
563 cairo_device_t *device;
565 device = cairo_surface_get_device(surface->cairo_surface);
570 if (device == surface->display->argb_device)
571 ctx = surface->display->argb_ctx;
576 cairo_device_flush(device);
577 cairo_device_acquire(device);
578 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
579 surface->egl_surface, ctx))
580 fprintf(stderr, "failed to make surface current\n");
586 egl_window_surface_release(struct toysurface *base)
588 struct egl_window_surface *surface = to_egl_window_surface(base);
589 cairo_device_t *device;
591 device = cairo_surface_get_device(surface->cairo_surface);
595 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
596 surface->display->argb_ctx))
597 fprintf(stderr, "failed to make context current\n");
599 cairo_device_release(device);
603 egl_window_surface_destroy(struct toysurface *base)
605 struct egl_window_surface *surface = to_egl_window_surface(base);
606 struct display *d = surface->display;
608 cairo_surface_destroy(surface->cairo_surface);
609 eglDestroySurface(d->dpy, surface->egl_surface);
610 wl_egl_window_destroy(surface->egl_window);
611 surface->surface = NULL;
616 static struct toysurface *
617 egl_window_surface_create(struct display *display,
618 struct wl_surface *wl_surface,
620 struct rectangle *rectangle)
622 struct egl_window_surface *surface;
624 if (display->dpy == EGL_NO_DISPLAY)
627 surface = calloc(1, sizeof *surface);
631 surface->base.prepare = egl_window_surface_prepare;
632 surface->base.swap = egl_window_surface_swap;
633 surface->base.acquire = egl_window_surface_acquire;
634 surface->base.release = egl_window_surface_release;
635 surface->base.destroy = egl_window_surface_destroy;
637 surface->display = display;
638 surface->surface = wl_surface;
640 surface->egl_window = wl_egl_window_create(surface->surface,
644 surface->egl_surface = eglCreateWindowSurface(display->dpy,
645 display->argb_config,
649 surface->cairo_surface =
650 cairo_gl_surface_create_for_egl(display->argb_device,
651 surface->egl_surface,
655 return &surface->base;
660 static struct toysurface *
661 egl_window_surface_create(struct display *display,
662 struct wl_surface *wl_surface,
664 struct rectangle *rectangle)
671 struct shm_surface_data {
672 struct wl_buffer *buffer;
673 struct shm_pool *pool;
677 display_get_buffer_for_surface(struct display *display,
678 cairo_surface_t *surface)
680 struct shm_surface_data *data;
682 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
688 shm_pool_destroy(struct shm_pool *pool);
691 shm_surface_data_destroy(void *p)
693 struct shm_surface_data *data = p;
695 wl_buffer_destroy(data->buffer);
697 shm_pool_destroy(data->pool);
702 static struct wl_shm_pool *
703 make_shm_pool(struct display *display, int size, void **data)
705 struct wl_shm_pool *pool;
708 fd = os_create_anonymous_file(size);
710 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
715 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
716 if (*data == MAP_FAILED) {
717 fprintf(stderr, "mmap failed: %m\n");
722 pool = wl_shm_create_pool(display->shm, fd, size);
729 static struct shm_pool *
730 shm_pool_create(struct display *display, size_t size)
732 struct shm_pool *pool = malloc(sizeof *pool);
737 pool->pool = make_shm_pool(display, size, &pool->data);
750 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
752 if (pool->used + size > pool->size)
755 *offset = pool->used;
758 return (char *) pool->data + *offset;
761 /* destroy the pool. this does not unmap the memory though */
763 shm_pool_destroy(struct shm_pool *pool)
765 munmap(pool->data, pool->size);
766 wl_shm_pool_destroy(pool->pool);
770 /* Start allocating from the beginning of the pool again */
772 shm_pool_reset(struct shm_pool *pool)
778 data_length_for_shm_surface(struct rectangle *rect)
782 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
784 return stride * rect->height;
787 static cairo_surface_t *
788 display_create_shm_surface_from_pool(struct display *display,
789 struct rectangle *rectangle,
790 uint32_t flags, struct shm_pool *pool)
792 struct shm_surface_data *data;
794 cairo_surface_t *surface;
795 cairo_format_t cairo_format;
796 int stride, length, offset;
799 data = malloc(sizeof *data);
803 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
804 cairo_format = CAIRO_FORMAT_RGB16_565;
806 cairo_format = CAIRO_FORMAT_ARGB32;
808 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
809 length = stride * rectangle->height;
811 map = shm_pool_allocate(pool, length, &offset);
818 surface = cairo_image_surface_create_for_data (map,
824 cairo_surface_set_user_data(surface, &shm_surface_data_key,
825 data, shm_surface_data_destroy);
827 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
828 format = WL_SHM_FORMAT_RGB565;
830 if (flags & SURFACE_OPAQUE)
831 format = WL_SHM_FORMAT_XRGB8888;
833 format = WL_SHM_FORMAT_ARGB8888;
836 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
844 static cairo_surface_t *
845 display_create_shm_surface(struct display *display,
846 struct rectangle *rectangle, uint32_t flags,
847 struct shm_pool *alternate_pool,
848 struct shm_surface_data **data_ret)
850 struct shm_surface_data *data;
851 struct shm_pool *pool;
852 cairo_surface_t *surface;
854 if (alternate_pool) {
855 shm_pool_reset(alternate_pool);
856 surface = display_create_shm_surface_from_pool(display,
861 data = cairo_surface_get_user_data(surface,
862 &shm_surface_data_key);
867 pool = shm_pool_create(display,
868 data_length_for_shm_surface(rectangle));
873 display_create_shm_surface_from_pool(display, rectangle,
877 shm_pool_destroy(pool);
881 /* make sure we destroy the pool when the surface is destroyed */
882 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
893 check_size(struct rectangle *rect)
895 if (rect->width && rect->height)
898 fprintf(stderr, "tried to create surface of "
899 "width: %d, height: %d\n", rect->width, rect->height);
904 display_create_surface(struct display *display,
905 struct wl_surface *surface,
906 struct rectangle *rectangle,
909 if (check_size(rectangle) < 0)
912 assert(flags & SURFACE_SHM);
913 return display_create_shm_surface(display, rectangle, flags,
917 struct shm_surface_leaf {
918 cairo_surface_t *cairo_surface;
919 /* 'data' is automatically destroyed, when 'cairo_surface' is */
920 struct shm_surface_data *data;
922 struct shm_pool *resize_pool;
927 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
929 if (leaf->cairo_surface)
930 cairo_surface_destroy(leaf->cairo_surface);
931 /* leaf->data already destroyed via cairo private */
933 if (leaf->resize_pool)
934 shm_pool_destroy(leaf->resize_pool);
936 memset(leaf, 0, sizeof *leaf);
942 struct toysurface base;
943 struct display *display;
944 struct wl_surface *surface;
948 struct shm_surface_leaf leaf[MAX_LEAVES];
949 struct shm_surface_leaf *current;
952 static struct shm_surface *
953 to_shm_surface(struct toysurface *base)
955 return container_of(base, struct shm_surface, base);
959 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
962 struct shm_surface_leaf *leaf;
963 char bufs[MAX_LEAVES + 1];
966 for (i = 0; i < MAX_LEAVES; i++) {
967 leaf = &surface->leaf[i];
971 else if (leaf->cairo_surface)
977 bufs[MAX_LEAVES] = '\0';
978 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
983 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
985 struct shm_surface *surface = data;
986 struct shm_surface_leaf *leaf;
990 shm_surface_buffer_state_debug(surface, "buffer_release before");
992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
994 if (leaf->data && leaf->data->buffer == buffer) {
999 assert(i < MAX_LEAVES && "unknown buffer released");
1001 /* Leave one free leaf with storage, release others */
1003 for (i = 0; i < MAX_LEAVES; i++) {
1004 leaf = &surface->leaf[i];
1006 if (!leaf->cairo_surface || leaf->busy)
1012 shm_surface_leaf_release(leaf);
1015 shm_surface_buffer_state_debug(surface, "buffer_release after");
1018 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1019 shm_surface_buffer_release
1022 static cairo_surface_t *
1023 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1024 int32_t width, int32_t height, uint32_t flags,
1025 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1027 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1028 struct shm_surface *surface = to_shm_surface(base);
1029 struct rectangle rect = { 0};
1030 struct shm_surface_leaf *leaf = NULL;
1036 /* pick a free buffer, preferrably one that already has storage */
1037 for (i = 0; i < MAX_LEAVES; i++) {
1038 if (surface->leaf[i].busy)
1041 if (!leaf || surface->leaf[i].cairo_surface)
1042 leaf = &surface->leaf[i];
1044 DBG_OBJ(surface->surface, "pick leaf %d\n",
1045 (int)(leaf - &surface->leaf[0]));
1048 fprintf(stderr, "%s: all buffers are held by the server.\n",
1054 if (!resize_hint && leaf->resize_pool) {
1055 cairo_surface_destroy(leaf->cairo_surface);
1056 leaf->cairo_surface = NULL;
1057 shm_pool_destroy(leaf->resize_pool);
1058 leaf->resize_pool = NULL;
1061 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1063 if (leaf->cairo_surface &&
1064 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1065 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1068 if (leaf->cairo_surface)
1069 cairo_surface_destroy(leaf->cairo_surface);
1071 #ifdef USE_RESIZE_POOL
1072 if (resize_hint && !leaf->resize_pool) {
1073 /* Create a big pool to allocate from, while continuously
1074 * resizing. Mmapping a new pool in the server
1075 * is relatively expensive, so reusing a pool performs
1076 * better, but may temporarily reserve unneeded memory.
1078 /* We should probably base this number on the output size. */
1079 leaf->resize_pool = shm_pool_create(surface->display,
1085 rect.height = height;
1087 leaf->cairo_surface =
1088 display_create_shm_surface(surface->display, &rect,
1092 if (!leaf->cairo_surface)
1095 wl_buffer_add_listener(leaf->data->buffer,
1096 &shm_surface_buffer_listener, surface);
1099 surface->current = leaf;
1101 return cairo_surface_reference(leaf->cairo_surface);
1105 shm_surface_swap(struct toysurface *base,
1106 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1107 struct rectangle *server_allocation)
1109 struct shm_surface *surface = to_shm_surface(base);
1110 struct shm_surface_leaf *leaf = surface->current;
1112 server_allocation->width =
1113 cairo_image_surface_get_width(leaf->cairo_surface);
1114 server_allocation->height =
1115 cairo_image_surface_get_height(leaf->cairo_surface);
1117 buffer_to_surface_size (buffer_transform, buffer_scale,
1118 &server_allocation->width,
1119 &server_allocation->height);
1121 wl_surface_attach(surface->surface, leaf->data->buffer,
1122 surface->dx, surface->dy);
1123 wl_surface_damage(surface->surface, 0, 0,
1124 server_allocation->width, server_allocation->height);
1125 wl_surface_commit(surface->surface);
1127 DBG_OBJ(surface->surface, "leaf %d busy\n",
1128 (int)(leaf - &surface->leaf[0]));
1131 surface->current = NULL;
1135 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1141 shm_surface_release(struct toysurface *base)
1146 shm_surface_destroy(struct toysurface *base)
1148 struct shm_surface *surface = to_shm_surface(base);
1151 for (i = 0; i < MAX_LEAVES; i++)
1152 shm_surface_leaf_release(&surface->leaf[i]);
1157 static struct toysurface *
1158 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1159 uint32_t flags, struct rectangle *rectangle)
1161 struct shm_surface *surface;
1162 DBG_OBJ(wl_surface, "\n");
1164 surface = xmalloc(sizeof *surface);
1165 memset(surface, 0, sizeof *surface);
1170 surface->base.prepare = shm_surface_prepare;
1171 surface->base.swap = shm_surface_swap;
1172 surface->base.acquire = shm_surface_acquire;
1173 surface->base.release = shm_surface_release;
1174 surface->base.destroy = shm_surface_destroy;
1176 surface->display = display;
1177 surface->surface = wl_surface;
1178 surface->flags = flags;
1180 return &surface->base;
1184 * The following correspondences between file names and cursors was copied
1185 * from: https://bugs.kde.org/attachment.cgi?id=67313
1188 static const char *bottom_left_corners[] = {
1189 "bottom_left_corner",
1194 static const char *bottom_right_corners[] = {
1195 "bottom_right_corner",
1200 static const char *bottom_sides[] = {
1206 static const char *grabbings[] = {
1209 "208530c400c041818281048008011002"
1212 static const char *left_ptrs[] = {
1219 static const char *left_sides[] = {
1225 static const char *right_sides[] = {
1231 static const char *top_left_corners[] = {
1237 static const char *top_right_corners[] = {
1243 static const char *top_sides[] = {
1249 static const char *xterms[] = {
1255 static const char *hand1s[] = {
1259 "e29285e634086352946a0e7090d73106"
1262 static const char *watches[] = {
1265 "0426c94ea35c87780ff01dc239897213"
1268 struct cursor_alternatives {
1273 static const struct cursor_alternatives cursors[] = {
1274 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1275 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1276 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1277 {grabbings, ARRAY_LENGTH(grabbings)},
1278 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1279 {left_sides, ARRAY_LENGTH(left_sides)},
1280 {right_sides, ARRAY_LENGTH(right_sides)},
1281 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1282 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1283 {top_sides, ARRAY_LENGTH(top_sides)},
1284 {xterms, ARRAY_LENGTH(xterms)},
1285 {hand1s, ARRAY_LENGTH(hand1s)},
1286 {watches, ARRAY_LENGTH(watches)},
1290 create_cursors(struct display *display)
1292 struct weston_config *config;
1293 struct weston_config_section *s;
1297 struct wl_cursor *cursor;
1299 config = weston_config_parse("weston.ini");
1300 s = weston_config_get_section(config, "shell", NULL, NULL);
1301 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1302 weston_config_section_get_int(s, "cursor-size", &size, 32);
1303 weston_config_destroy(config);
1305 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1308 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1310 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1312 for (j = 0; !cursor && j < cursors[i].count; ++j)
1313 cursor = wl_cursor_theme_get_cursor(
1314 display->cursor_theme, cursors[i].names[j]);
1317 fprintf(stderr, "could not load cursor '%s'\n",
1318 cursors[i].names[0]);
1320 display->cursors[i] = cursor;
1325 destroy_cursors(struct display *display)
1327 wl_cursor_theme_destroy(display->cursor_theme);
1328 free(display->cursors);
1331 struct wl_cursor_image *
1332 display_get_pointer_image(struct display *display, int pointer)
1334 struct wl_cursor *cursor = display->cursors[pointer];
1336 return cursor ? cursor->images[0] : NULL;
1340 surface_flush(struct surface *surface)
1342 if (!surface->cairo_surface)
1345 if (surface->opaque_region) {
1346 wl_surface_set_opaque_region(surface->surface,
1347 surface->opaque_region);
1348 wl_region_destroy(surface->opaque_region);
1349 surface->opaque_region = NULL;
1352 if (surface->input_region) {
1353 wl_surface_set_input_region(surface->surface,
1354 surface->input_region);
1355 wl_region_destroy(surface->input_region);
1356 surface->input_region = NULL;
1359 surface->toysurface->swap(surface->toysurface,
1360 surface->buffer_transform, surface->buffer_scale,
1361 &surface->server_allocation);
1363 cairo_surface_destroy(surface->cairo_surface);
1364 surface->cairo_surface = NULL;
1368 window_has_focus(struct window *window)
1370 return window->focus_count > 0;
1374 window_flush(struct window *window)
1376 struct surface *surface;
1378 if (window->type == TYPE_NONE) {
1379 window->type = TYPE_TOPLEVEL;
1380 if (window->shell_surface)
1381 wl_shell_surface_set_toplevel(window->shell_surface);
1384 wl_list_for_each(surface, &window->subsurface_list, link) {
1385 if (surface == window->main_surface)
1388 surface_flush(surface);
1391 surface_flush(window->main_surface);
1395 window_get_display(struct window *window)
1397 return window->display;
1401 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1403 struct display *display = surface->window->display;
1404 struct rectangle allocation = surface->allocation;
1406 if (!surface->toysurface && display->dpy &&
1407 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1408 surface->toysurface =
1409 egl_window_surface_create(display,
1415 if (!surface->toysurface)
1416 surface->toysurface = shm_surface_create(display,
1418 flags, &allocation);
1420 surface->cairo_surface = surface->toysurface->prepare(
1421 surface->toysurface, dx, dy,
1422 allocation.width, allocation.height, flags,
1423 surface->buffer_transform, surface->buffer_scale);
1427 window_create_main_surface(struct window *window)
1429 struct surface *surface = window->main_surface;
1434 if (window->resizing)
1435 flags |= SURFACE_HINT_RESIZE;
1437 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1438 flags |= SURFACE_HINT_RGB565;
1440 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1441 dx = surface->server_allocation.width -
1442 surface->allocation.width;
1444 if (window->resize_edges & WINDOW_RESIZING_TOP)
1445 dy = surface->server_allocation.height -
1446 surface->allocation.height;
1448 window->resize_edges = 0;
1450 surface_create_surface(surface, dx, dy, flags);
1454 window_get_buffer_transform(struct window *window)
1456 return window->main_surface->buffer_transform;
1460 window_set_buffer_transform(struct window *window,
1461 enum wl_output_transform transform)
1463 window->main_surface->buffer_transform = transform;
1464 wl_surface_set_buffer_transform(window->main_surface->surface,
1469 window_set_buffer_scale(struct window *window,
1472 window->main_surface->buffer_scale = scale;
1473 wl_surface_set_buffer_scale(window->main_surface->surface,
1478 window_get_buffer_scale(struct window *window)
1480 return window->main_surface->buffer_scale;
1484 window_get_output_scale(struct window *window)
1486 struct window_output *window_output;
1487 struct window_output *window_output_tmp;
1490 wl_list_for_each_safe(window_output, window_output_tmp,
1491 &window->window_output_list, link) {
1492 if (window_output->output->scale > scale)
1493 scale = window_output->output->scale;
1499 static void window_frame_destroy(struct window_frame *frame);
1502 surface_destroy(struct surface *surface)
1504 if (surface->frame_cb)
1505 wl_callback_destroy(surface->frame_cb);
1507 if (surface->input_region)
1508 wl_region_destroy(surface->input_region);
1510 if (surface->opaque_region)
1511 wl_region_destroy(surface->opaque_region);
1513 if (surface->subsurface)
1514 wl_subsurface_destroy(surface->subsurface);
1516 wl_surface_destroy(surface->surface);
1518 if (surface->toysurface)
1519 surface->toysurface->destroy(surface->toysurface);
1521 wl_list_remove(&surface->link);
1526 window_destroy(struct window *window)
1528 struct display *display = window->display;
1529 struct input *input;
1530 struct window_output *window_output;
1531 struct window_output *window_output_tmp;
1533 wl_list_remove(&window->redraw_task.link);
1535 wl_list_for_each(input, &display->input_list, link) {
1536 if (input->touch_focus == window)
1537 input->touch_focus = NULL;
1538 if (input->pointer_focus == window)
1539 input->pointer_focus = NULL;
1540 if (input->keyboard_focus == window)
1541 input->keyboard_focus = NULL;
1542 if (input->focus_widget &&
1543 input->focus_widget->window == window)
1544 input->focus_widget = NULL;
1547 wl_list_for_each_safe(window_output, window_output_tmp,
1548 &window->window_output_list, link) {
1549 free (window_output);
1553 window_frame_destroy(window->frame);
1555 if (window->shell_surface)
1556 wl_shell_surface_destroy(window->shell_surface);
1558 surface_destroy(window->main_surface);
1560 wl_list_remove(&window->link);
1562 free(window->title);
1566 static struct widget *
1567 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1569 struct widget *child, *target;
1571 wl_list_for_each(child, &widget->child_list, link) {
1572 target = widget_find_widget(child, x, y);
1577 if (widget->allocation.x <= x &&
1578 x < widget->allocation.x + widget->allocation.width &&
1579 widget->allocation.y <= y &&
1580 y < widget->allocation.y + widget->allocation.height) {
1587 static struct widget *
1588 window_find_widget(struct window *window, int32_t x, int32_t y)
1590 struct surface *surface;
1591 struct widget *widget;
1593 wl_list_for_each(surface, &window->subsurface_list, link) {
1594 widget = widget_find_widget(surface->widget, x, y);
1602 static struct widget *
1603 widget_create(struct window *window, struct surface *surface, void *data)
1605 struct widget *widget;
1607 widget = xzalloc(sizeof *widget);
1608 widget->window = window;
1609 widget->surface = surface;
1610 widget->user_data = data;
1611 widget->allocation = surface->allocation;
1612 wl_list_init(&widget->child_list);
1614 widget->tooltip = NULL;
1615 widget->tooltip_count = 0;
1616 widget->default_cursor = CURSOR_LEFT_PTR;
1617 widget->use_cairo = 1;
1623 window_add_widget(struct window *window, void *data)
1625 struct widget *widget;
1627 widget = widget_create(window, window->main_surface, data);
1628 wl_list_init(&widget->link);
1629 window->main_surface->widget = widget;
1635 widget_add_widget(struct widget *parent, void *data)
1637 struct widget *widget;
1639 widget = widget_create(parent->window, parent->surface, data);
1640 wl_list_insert(parent->child_list.prev, &widget->link);
1646 widget_destroy(struct widget *widget)
1648 struct display *display = widget->window->display;
1649 struct surface *surface = widget->surface;
1650 struct input *input;
1652 /* Destroy the sub-surface along with the root widget */
1653 if (surface->widget == widget && surface->subsurface)
1654 surface_destroy(widget->surface);
1656 if (widget->tooltip)
1657 widget_destroy_tooltip(widget);
1659 wl_list_for_each(input, &display->input_list, link) {
1660 if (input->focus_widget == widget)
1661 input->focus_widget = NULL;
1664 wl_list_remove(&widget->link);
1669 widget_set_default_cursor(struct widget *widget, int cursor)
1671 widget->default_cursor = cursor;
1675 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1677 *allocation = widget->allocation;
1681 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1683 widget->allocation.width = width;
1684 widget->allocation.height = height;
1688 widget_set_allocation(struct widget *widget,
1689 int32_t x, int32_t y, int32_t width, int32_t height)
1691 widget->allocation.x = x;
1692 widget->allocation.y = y;
1693 widget_set_size(widget, width, height);
1697 widget_set_transparent(struct widget *widget, int transparent)
1699 widget->opaque = !transparent;
1703 widget_get_user_data(struct widget *widget)
1705 return widget->user_data;
1708 static cairo_surface_t *
1709 widget_get_cairo_surface(struct widget *widget)
1711 struct surface *surface = widget->surface;
1712 struct window *window = widget->window;
1714 assert(widget->use_cairo);
1716 if (!surface->cairo_surface) {
1717 if (surface == window->main_surface)
1718 window_create_main_surface(window);
1720 surface_create_surface(surface, 0, 0, 0);
1723 return surface->cairo_surface;
1727 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1729 struct surface *surface = widget->surface;
1732 enum wl_output_transform transform;
1733 int surface_width, surface_height;
1734 int translate_x, translate_y;
1737 surface_width = surface->allocation.width;
1738 surface_height = surface->allocation.height;
1740 transform = surface->buffer_transform;
1741 scale = surface->buffer_scale;
1743 switch (transform) {
1744 case WL_OUTPUT_TRANSFORM_FLIPPED:
1745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1746 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1748 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1751 cairo_matrix_init_identity(&m);
1755 switch (transform) {
1756 case WL_OUTPUT_TRANSFORM_NORMAL:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED:
1764 translate_x = surface_width;
1767 case WL_OUTPUT_TRANSFORM_90:
1769 translate_x = surface_height;
1772 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1774 translate_x = surface_height;
1775 translate_y = surface_width;
1777 case WL_OUTPUT_TRANSFORM_180:
1779 translate_x = surface_width;
1780 translate_y = surface_height;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1785 translate_y = surface_height;
1787 case WL_OUTPUT_TRANSFORM_270:
1788 angle = M_PI + M_PI_2;
1790 translate_y = surface_width;
1792 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1793 angle = M_PI + M_PI_2;
1799 cairo_scale(cr, scale, scale);
1800 cairo_translate(cr, translate_x, translate_y);
1801 cairo_rotate(cr, angle);
1802 cairo_transform(cr, &m);
1806 widget_cairo_create(struct widget *widget)
1808 struct surface *surface = widget->surface;
1809 cairo_surface_t *cairo_surface;
1812 cairo_surface = widget_get_cairo_surface(widget);
1813 cr = cairo_create(cairo_surface);
1815 widget_cairo_update_transform(widget, cr);
1817 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1823 widget_get_wl_surface(struct widget *widget)
1825 return widget->surface->surface;
1828 struct wl_subsurface *
1829 widget_get_wl_subsurface(struct widget *widget)
1831 return widget->surface->subsurface;
1835 widget_get_last_time(struct widget *widget)
1837 return widget->surface->last_time;
1841 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1843 struct wl_compositor *comp = widget->window->display->compositor;
1844 struct surface *surface = widget->surface;
1846 if (!surface->input_region)
1847 surface->input_region = wl_compositor_create_region(comp);
1850 wl_region_add(surface->input_region,
1851 rect->x, rect->y, rect->width, rect->height);
1856 widget_set_resize_handler(struct widget *widget,
1857 widget_resize_handler_t handler)
1859 widget->resize_handler = handler;
1863 widget_set_redraw_handler(struct widget *widget,
1864 widget_redraw_handler_t handler)
1866 widget->redraw_handler = handler;
1870 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1872 widget->enter_handler = handler;
1876 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1878 widget->leave_handler = handler;
1882 widget_set_motion_handler(struct widget *widget,
1883 widget_motion_handler_t handler)
1885 widget->motion_handler = handler;
1889 widget_set_button_handler(struct widget *widget,
1890 widget_button_handler_t handler)
1892 widget->button_handler = handler;
1896 widget_set_touch_up_handler(struct widget *widget,
1897 widget_touch_up_handler_t handler)
1899 widget->touch_up_handler = handler;
1903 widget_set_touch_down_handler(struct widget *widget,
1904 widget_touch_down_handler_t handler)
1906 widget->touch_down_handler = handler;
1910 widget_set_touch_motion_handler(struct widget *widget,
1911 widget_touch_motion_handler_t handler)
1913 widget->touch_motion_handler = handler;
1917 widget_set_touch_frame_handler(struct widget *widget,
1918 widget_touch_frame_handler_t handler)
1920 widget->touch_frame_handler = handler;
1924 widget_set_touch_cancel_handler(struct widget *widget,
1925 widget_touch_cancel_handler_t handler)
1927 widget->touch_cancel_handler = handler;
1931 widget_set_axis_handler(struct widget *widget,
1932 widget_axis_handler_t handler)
1934 widget->axis_handler = handler;
1938 window_schedule_redraw_task(struct window *window);
1941 widget_schedule_redraw(struct widget *widget)
1943 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1944 widget->surface->redraw_needed = 1;
1945 window_schedule_redraw_task(widget->window);
1949 widget_set_use_cairo(struct widget *widget,
1952 widget->use_cairo = use_cairo;
1956 window_get_surface(struct window *window)
1958 cairo_surface_t *cairo_surface;
1960 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1962 return cairo_surface_reference(cairo_surface);
1966 window_get_wl_surface(struct window *window)
1968 return window->main_surface->surface;
1972 tooltip_redraw_handler(struct widget *widget, void *data)
1975 const int32_t r = 3;
1976 struct tooltip *tooltip = data;
1977 int32_t width, height;
1979 cr = widget_cairo_create(widget);
1980 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1981 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1984 width = widget->allocation.width;
1985 height = widget->allocation.height;
1986 rounded_rect(cr, 0, 0, width, height, r);
1988 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1989 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1992 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1993 cairo_move_to(cr, 10, 16);
1994 cairo_show_text(cr, tooltip->entry);
1998 static cairo_text_extents_t
1999 get_text_extents(struct tooltip *tooltip)
2002 cairo_text_extents_t extents;
2004 /* Use the dummy_surface because tooltip's surface was not
2005 * created yet, and parent does not have a valid surface
2006 * outside repaint, either.
2008 cr = cairo_create(tooltip->window->display->dummy_surface);
2009 cairo_text_extents(cr, tooltip->entry, &extents);
2016 window_create_tooltip(struct tooltip *tooltip)
2018 struct widget *parent = tooltip->parent;
2019 struct display *display = parent->window->display;
2020 struct window *window;
2021 const int offset_y = 27;
2022 const int margin = 3;
2023 cairo_text_extents_t extents;
2025 if (tooltip->widget)
2028 window = window_create_transient(display, parent->window, tooltip->x,
2029 tooltip->y + offset_y,
2030 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2034 tooltip->window = window;
2035 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2037 extents = get_text_extents(tooltip);
2038 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2039 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2045 widget_destroy_tooltip(struct widget *parent)
2047 struct tooltip *tooltip = parent->tooltip;
2049 parent->tooltip_count = 0;
2053 if (tooltip->widget) {
2054 widget_destroy(tooltip->widget);
2055 window_destroy(tooltip->window);
2056 tooltip->widget = NULL;
2057 tooltip->window = NULL;
2060 close(tooltip->tooltip_fd);
2061 free(tooltip->entry);
2063 parent->tooltip = NULL;
2067 tooltip_func(struct task *task, uint32_t events)
2069 struct tooltip *tooltip =
2070 container_of(task, struct tooltip, tooltip_task);
2073 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2075 window_create_tooltip(tooltip);
2078 #define TOOLTIP_TIMEOUT 500
2080 tooltip_timer_reset(struct tooltip *tooltip)
2082 struct itimerspec its;
2084 its.it_interval.tv_sec = 0;
2085 its.it_interval.tv_nsec = 0;
2086 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2087 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2088 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2089 fprintf(stderr, "could not set timerfd\n: %m");
2097 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2099 struct tooltip *tooltip = parent->tooltip;
2101 parent->tooltip_count++;
2105 tooltip_timer_reset(tooltip);
2109 /* the handler might be triggered too fast via input device motion, so
2110 * we need this check here to make sure tooltip is fully initialized */
2111 if (parent->tooltip_count > 1)
2114 tooltip = malloc(sizeof *tooltip);
2118 parent->tooltip = tooltip;
2119 tooltip->parent = parent;
2120 tooltip->widget = NULL;
2121 tooltip->window = NULL;
2124 tooltip->entry = strdup(entry);
2125 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2126 if (tooltip->tooltip_fd < 0) {
2127 fprintf(stderr, "could not create timerfd\n: %m");
2131 tooltip->tooltip_task.run = tooltip_func;
2132 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2133 EPOLLIN, &tooltip->tooltip_task);
2134 tooltip_timer_reset(tooltip);
2140 workspace_manager_state(void *data,
2141 struct workspace_manager *workspace_manager,
2145 struct display *display = data;
2147 display->workspace = current;
2148 display->workspace_count = count;
2151 static const struct workspace_manager_listener workspace_manager_listener = {
2152 workspace_manager_state
2156 frame_resize_handler(struct widget *widget,
2157 int32_t width, int32_t height, void *data)
2159 struct window_frame *frame = data;
2160 struct widget *child = frame->child;
2161 struct rectangle interior;
2162 struct rectangle input;
2163 struct rectangle opaque;
2165 if (widget->window->type == TYPE_FULLSCREEN) {
2168 interior.width = width;
2169 interior.height = height;
2171 if (widget->window->type == TYPE_MAXIMIZED) {
2172 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2174 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2177 frame_resize(frame->frame, width, height);
2178 frame_interior(frame->frame, &interior.x, &interior.y,
2179 &interior.width, &interior.height);
2182 widget_set_allocation(child, interior.x, interior.y,
2183 interior.width, interior.height);
2185 if (child->resize_handler) {
2186 child->resize_handler(child, interior.width, interior.height,
2189 if (widget->window->type == TYPE_FULLSCREEN) {
2190 width = child->allocation.width;
2191 height = child->allocation.height;
2193 frame_resize_inside(frame->frame,
2194 child->allocation.width,
2195 child->allocation.height);
2196 width = frame_width(frame->frame);
2197 height = frame_height(frame->frame);
2201 widget_set_allocation(widget, 0, 0, width, height);
2203 widget->surface->input_region =
2204 wl_compositor_create_region(widget->window->display->compositor);
2205 if (widget->window->type != TYPE_FULLSCREEN) {
2206 frame_input_rect(frame->frame, &input.x, &input.y,
2207 &input.width, &input.height);
2208 wl_region_add(widget->surface->input_region,
2209 input.x, input.y, input.width, input.height);
2211 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2214 widget_set_allocation(widget, 0, 0, width, height);
2216 if (child->opaque) {
2217 if (widget->window->type != TYPE_FULLSCREEN) {
2218 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2219 &opaque.width, &opaque.height);
2221 wl_region_add(widget->surface->opaque_region,
2223 opaque.width, opaque.height);
2225 wl_region_add(widget->surface->opaque_region,
2226 0, 0, width, height);
2231 widget_schedule_redraw(widget);
2235 frame_redraw_handler(struct widget *widget, void *data)
2238 struct window_frame *frame = data;
2239 struct window *window = widget->window;
2241 if (window->type == TYPE_FULLSCREEN)
2244 if (window->focus_count) {
2245 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2247 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2250 cr = widget_cairo_create(widget);
2252 frame_repaint(frame->frame, cr);
2258 frame_get_pointer_image_for_location(struct window_frame *frame,
2259 enum theme_location location)
2261 struct window *window = frame->widget->window;
2263 if (window->type != TYPE_TOPLEVEL)
2264 return CURSOR_LEFT_PTR;
2267 case THEME_LOCATION_RESIZING_TOP:
2269 case THEME_LOCATION_RESIZING_BOTTOM:
2270 return CURSOR_BOTTOM;
2271 case THEME_LOCATION_RESIZING_LEFT:
2273 case THEME_LOCATION_RESIZING_RIGHT:
2274 return CURSOR_RIGHT;
2275 case THEME_LOCATION_RESIZING_TOP_LEFT:
2276 return CURSOR_TOP_LEFT;
2277 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2278 return CURSOR_TOP_RIGHT;
2279 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2280 return CURSOR_BOTTOM_LEFT;
2281 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2282 return CURSOR_BOTTOM_RIGHT;
2283 case THEME_LOCATION_EXTERIOR:
2284 case THEME_LOCATION_TITLEBAR:
2286 return CURSOR_LEFT_PTR;
2291 frame_menu_func(struct window *window,
2292 struct input *input, int index, void *data)
2294 struct display *display;
2298 if (window->close_handler)
2299 window->close_handler(window->user_data);
2301 display_exit(window->display);
2303 case 1: /* move to workspace above */
2304 display = window->display;
2305 if (display->workspace > 0)
2306 workspace_manager_move_surface(
2307 display->workspace_manager,
2308 window->main_surface->surface,
2309 display->workspace - 1);
2311 case 2: /* move to workspace below */
2312 display = window->display;
2313 if (display->workspace < display->workspace_count - 1)
2314 workspace_manager_move_surface(
2315 display->workspace_manager,
2316 window->main_surface->surface,
2317 display->workspace + 1);
2319 case 3: /* fullscreen */
2320 /* we don't have a way to get out of fullscreen for now */
2321 if (window->fullscreen_handler)
2322 window->fullscreen_handler(window, window->user_data);
2328 window_show_frame_menu(struct window *window,
2329 struct input *input, uint32_t time)
2334 static const char *entries[] = {
2336 "Move to workspace above", "Move to workspace below",
2340 if (window->fullscreen_handler)
2341 count = ARRAY_LENGTH(entries);
2343 count = ARRAY_LENGTH(entries) - 1;
2345 input_get_position(input, &x, &y);
2346 window_show_menu(window->display, input, time, window,
2347 x - 10, y - 10, frame_menu_func, entries, count);
2351 frame_enter_handler(struct widget *widget,
2352 struct input *input, float x, float y, void *data)
2354 struct window_frame *frame = data;
2355 enum theme_location location;
2357 location = frame_pointer_enter(frame->frame, input, x, y);
2358 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2359 widget_schedule_redraw(frame->widget);
2361 return frame_get_pointer_image_for_location(data, location);
2365 frame_motion_handler(struct widget *widget,
2366 struct input *input, uint32_t time,
2367 float x, float y, void *data)
2369 struct window_frame *frame = data;
2370 enum theme_location location;
2372 location = frame_pointer_motion(frame->frame, input, x, y);
2373 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2374 widget_schedule_redraw(frame->widget);
2376 return frame_get_pointer_image_for_location(data, location);
2380 frame_leave_handler(struct widget *widget,
2381 struct input *input, void *data)
2383 struct window_frame *frame = data;
2385 frame_pointer_leave(frame->frame, input);
2386 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2387 widget_schedule_redraw(frame->widget);
2391 frame_handle_status(struct window_frame *frame, struct input *input,
2392 uint32_t time, enum theme_location location)
2394 struct window *window = frame->widget->window;
2397 status = frame_status(frame->frame);
2398 if (status & FRAME_STATUS_REPAINT)
2399 widget_schedule_redraw(frame->widget);
2401 if (status & FRAME_STATUS_MINIMIZE)
2402 fprintf(stderr,"Minimize stub\n");
2404 if (status & FRAME_STATUS_MENU) {
2405 window_show_frame_menu(window, input, time);
2406 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2409 if (status & FRAME_STATUS_MAXIMIZE) {
2410 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2411 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2414 if (status & FRAME_STATUS_CLOSE) {
2415 if (window->close_handler)
2416 window->close_handler(window->user_data);
2418 display_exit(window->display);
2422 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2423 input_ungrab(input);
2424 wl_shell_surface_move(window->shell_surface,
2425 input_get_seat(input),
2426 window->display->serial);
2428 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2431 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2432 input_ungrab(input);
2434 window->resizing = 1;
2435 wl_shell_surface_resize(window->shell_surface,
2436 input_get_seat(input),
2437 window->display->serial,
2440 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2445 frame_button_handler(struct widget *widget,
2446 struct input *input, uint32_t time,
2447 uint32_t button, enum wl_pointer_button_state state,
2451 struct window_frame *frame = data;
2452 enum theme_location location;
2454 location = frame_pointer_button(frame->frame, input, button, state);
2455 frame_handle_status(frame, input, time, location);
2459 frame_touch_down_handler(struct widget *widget, struct input *input,
2460 uint32_t serial, uint32_t time, int32_t id,
2461 float x, float y, void *data)
2463 struct window_frame *frame = data;
2465 frame_touch_down(frame->frame, input, id, x, y);
2466 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2470 frame_touch_up_handler(struct widget *widget,
2471 struct input *input, uint32_t serial, uint32_t time,
2472 int32_t id, void *data)
2474 struct window_frame *frame = data;
2476 frame_touch_up(frame->frame, input, id);
2477 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2481 window_frame_create(struct window *window, void *data)
2483 struct window_frame *frame;
2486 if (window->type == TYPE_CUSTOM) {
2487 buttons = FRAME_BUTTON_NONE;
2489 buttons = FRAME_BUTTON_ALL;
2492 frame = xzalloc(sizeof *frame);
2493 frame->frame = frame_create(window->display->theme, 0, 0,
2494 buttons, window->title);
2496 frame->widget = window_add_widget(window, frame);
2497 frame->child = widget_add_widget(frame->widget, data);
2499 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2500 widget_set_resize_handler(frame->widget, frame_resize_handler);
2501 widget_set_enter_handler(frame->widget, frame_enter_handler);
2502 widget_set_leave_handler(frame->widget, frame_leave_handler);
2503 widget_set_motion_handler(frame->widget, frame_motion_handler);
2504 widget_set_button_handler(frame->widget, frame_button_handler);
2505 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2506 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2508 window->frame = frame;
2510 return frame->child;
2514 window_frame_set_child_size(struct widget *widget, int child_width,
2517 struct display *display = widget->window->display;
2518 struct theme *t = display->theme;
2519 int decoration_width, decoration_height;
2521 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2523 if (widget->window->type != TYPE_FULLSCREEN) {
2524 decoration_width = (t->width + margin) * 2;
2525 decoration_height = t->width +
2526 t->titlebar_height + margin * 2;
2528 width = child_width + decoration_width;
2529 height = child_height + decoration_height;
2531 width = child_width;
2532 height = child_height;
2535 window_schedule_resize(widget->window, width, height);
2539 window_frame_destroy(struct window_frame *frame)
2541 frame_destroy(frame->frame);
2543 /* frame->child must be destroyed by the application */
2544 widget_destroy(frame->widget);
2549 input_set_focus_widget(struct input *input, struct widget *focus,
2552 struct widget *old, *widget;
2555 if (focus == input->focus_widget)
2558 old = input->focus_widget;
2562 widget = input->grab;
2563 if (widget->leave_handler)
2564 widget->leave_handler(old, input, widget->user_data);
2565 input->focus_widget = NULL;
2571 widget = input->grab;
2572 input->focus_widget = focus;
2573 if (widget->enter_handler)
2574 cursor = widget->enter_handler(focus, input, x, y,
2577 cursor = widget->default_cursor;
2579 input_set_pointer_image(input, cursor);
2584 touch_grab(struct input *input, int32_t touch_id)
2586 input->touch_grab = 1;
2587 input->touch_grab_id = touch_id;
2591 touch_ungrab(struct input *input)
2593 struct touch_point *tp, *tmp;
2595 input->touch_grab = 0;
2597 wl_list_for_each_safe(tp, tmp,
2598 &input->touch_point_list, link) {
2599 if (tp->id != input->touch_grab_id)
2601 wl_list_remove(&tp->link);
2609 input_grab(struct input *input, struct widget *widget, uint32_t button)
2611 input->grab = widget;
2612 input->grab_button = button;
2616 input_ungrab(struct input *input)
2618 struct widget *widget;
2621 if (input->pointer_focus) {
2622 widget = window_find_widget(input->pointer_focus,
2623 input->sx, input->sy);
2624 input_set_focus_widget(input, widget, input->sx, input->sy);
2629 input_remove_pointer_focus(struct input *input)
2631 struct window *window = input->pointer_focus;
2636 input_set_focus_widget(input, NULL, 0, 0);
2638 input->pointer_focus = NULL;
2639 input->current_cursor = CURSOR_UNSET;
2643 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2644 uint32_t serial, struct wl_surface *surface,
2645 wl_fixed_t sx_w, wl_fixed_t sy_w)
2647 struct input *input = data;
2648 struct window *window;
2649 struct widget *widget;
2650 float sx = wl_fixed_to_double(sx_w);
2651 float sy = wl_fixed_to_double(sy_w);
2654 /* enter event for a window we've just destroyed */
2658 input->display->serial = serial;
2659 input->pointer_enter_serial = serial;
2660 input->pointer_focus = wl_surface_get_user_data(surface);
2661 window = input->pointer_focus;
2663 if (window->resizing) {
2664 window->resizing = 0;
2665 /* Schedule a redraw to free the pool */
2666 window_schedule_redraw(window);
2672 widget = window_find_widget(window, sx, sy);
2673 input_set_focus_widget(input, widget, sx, sy);
2677 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2678 uint32_t serial, struct wl_surface *surface)
2680 struct input *input = data;
2682 input->display->serial = serial;
2683 input_remove_pointer_focus(input);
2687 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2688 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2690 struct input *input = data;
2691 struct window *window = input->pointer_focus;
2692 struct widget *widget;
2694 float sx = wl_fixed_to_double(sx_w);
2695 float sy = wl_fixed_to_double(sy_w);
2703 /* when making the window smaller - e.g. after a unmaximise we might
2704 * still have a pending motion event that the compositor has picked
2705 * based on the old surface dimensions
2707 if (sx > window->main_surface->allocation.width ||
2708 sy > window->main_surface->allocation.height)
2711 if (!(input->grab && input->grab_button)) {
2712 widget = window_find_widget(window, sx, sy);
2713 input_set_focus_widget(input, widget, sx, sy);
2717 widget = input->grab;
2719 widget = input->focus_widget;
2721 if (widget->motion_handler)
2722 cursor = widget->motion_handler(input->focus_widget,
2723 input, time, sx, sy,
2726 cursor = widget->default_cursor;
2728 cursor = CURSOR_LEFT_PTR;
2730 input_set_pointer_image(input, cursor);
2734 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2735 uint32_t time, uint32_t button, uint32_t state_w)
2737 struct input *input = data;
2738 struct widget *widget;
2739 enum wl_pointer_button_state state = state_w;
2741 input->display->serial = serial;
2742 if (input->focus_widget && input->grab == NULL &&
2743 state == WL_POINTER_BUTTON_STATE_PRESSED)
2744 input_grab(input, input->focus_widget, button);
2746 widget = input->grab;
2747 if (widget && widget->button_handler)
2748 (*widget->button_handler)(widget,
2751 input->grab->user_data);
2753 if (input->grab && input->grab_button == button &&
2754 state == WL_POINTER_BUTTON_STATE_RELEASED)
2755 input_ungrab(input);
2759 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2760 uint32_t time, uint32_t axis, wl_fixed_t value)
2762 struct input *input = data;
2763 struct widget *widget;
2765 widget = input->focus_widget;
2767 widget = input->grab;
2768 if (widget && widget->axis_handler)
2769 (*widget->axis_handler)(widget,
2775 static const struct wl_pointer_listener pointer_listener = {
2776 pointer_handle_enter,
2777 pointer_handle_leave,
2778 pointer_handle_motion,
2779 pointer_handle_button,
2780 pointer_handle_axis,
2784 input_remove_keyboard_focus(struct input *input)
2786 struct window *window = input->keyboard_focus;
2787 struct itimerspec its;
2789 its.it_interval.tv_sec = 0;
2790 its.it_interval.tv_nsec = 0;
2791 its.it_value.tv_sec = 0;
2792 its.it_value.tv_nsec = 0;
2793 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2798 window->focus_count--;
2799 if (window->keyboard_focus_handler)
2800 (*window->keyboard_focus_handler)(window, NULL,
2803 input->keyboard_focus = NULL;
2807 keyboard_repeat_func(struct task *task, uint32_t events)
2809 struct input *input =
2810 container_of(task, struct input, repeat_task);
2811 struct window *window = input->keyboard_focus;
2814 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2815 /* If we change the timer between the fd becoming
2816 * readable and getting here, there'll be nothing to
2817 * read and we get EAGAIN. */
2820 if (window && window->key_handler) {
2821 (*window->key_handler)(window, input, input->repeat_time,
2822 input->repeat_key, input->repeat_sym,
2823 WL_KEYBOARD_KEY_STATE_PRESSED,
2829 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2830 uint32_t format, int fd, uint32_t size)
2832 struct input *input = data;
2833 struct xkb_keymap *keymap;
2834 struct xkb_state *state;
2842 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2847 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2848 if (map_str == MAP_FAILED) {
2853 keymap = xkb_map_new_from_string(input->display->xkb_context,
2855 XKB_KEYMAP_FORMAT_TEXT_V1,
2857 munmap(map_str, size);
2861 fprintf(stderr, "failed to compile keymap\n");
2865 state = xkb_state_new(keymap);
2867 fprintf(stderr, "failed to create XKB state\n");
2868 xkb_map_unref(keymap);
2872 xkb_keymap_unref(input->xkb.keymap);
2873 xkb_state_unref(input->xkb.state);
2874 input->xkb.keymap = keymap;
2875 input->xkb.state = state;
2877 input->xkb.control_mask =
2878 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2879 input->xkb.alt_mask =
2880 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2881 input->xkb.shift_mask =
2882 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2886 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2887 uint32_t serial, struct wl_surface *surface,
2888 struct wl_array *keys)
2890 struct input *input = data;
2891 struct window *window;
2893 input->display->serial = serial;
2894 input->keyboard_focus = wl_surface_get_user_data(surface);
2896 window = input->keyboard_focus;
2897 window->focus_count++;
2898 if (window->keyboard_focus_handler)
2899 (*window->keyboard_focus_handler)(window,
2900 input, window->user_data);
2904 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2905 uint32_t serial, struct wl_surface *surface)
2907 struct input *input = data;
2909 input->display->serial = serial;
2910 input_remove_keyboard_focus(input);
2914 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2915 uint32_t serial, uint32_t time, uint32_t key,
2918 struct input *input = data;
2919 struct window *window = input->keyboard_focus;
2920 uint32_t code, num_syms;
2921 enum wl_keyboard_key_state state = state_w;
2922 const xkb_keysym_t *syms;
2924 struct itimerspec its;
2926 input->display->serial = serial;
2928 if (!window || !input->xkb.state)
2931 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2933 sym = XKB_KEY_NoSymbol;
2937 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2938 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2939 window_set_maximized(window,
2940 window->type != TYPE_MAXIMIZED);
2941 } else if (sym == XKB_KEY_F11 &&
2942 window->fullscreen_handler &&
2943 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2944 window->fullscreen_handler(window, window->user_data);
2945 } else if (sym == XKB_KEY_F4 &&
2946 input->modifiers == MOD_ALT_MASK &&
2947 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2948 if (window->close_handler)
2949 window->close_handler(window->user_data);
2951 display_exit(window->display);
2952 } else if (window->key_handler) {
2953 (*window->key_handler)(window, input, time, key,
2954 sym, state, window->user_data);
2957 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2958 key == input->repeat_key) {
2959 its.it_interval.tv_sec = 0;
2960 its.it_interval.tv_nsec = 0;
2961 its.it_value.tv_sec = 0;
2962 its.it_value.tv_nsec = 0;
2963 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2964 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2965 input->repeat_sym = sym;
2966 input->repeat_key = key;
2967 input->repeat_time = time;
2968 its.it_interval.tv_sec = 0;
2969 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2970 its.it_value.tv_sec = 0;
2971 its.it_value.tv_nsec = 400 * 1000 * 1000;
2972 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2977 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2978 uint32_t serial, uint32_t mods_depressed,
2979 uint32_t mods_latched, uint32_t mods_locked,
2982 struct input *input = data;
2983 xkb_mod_mask_t mask;
2985 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2986 if (!input->xkb.keymap)
2989 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2990 mods_locked, 0, 0, group);
2991 mask = xkb_state_serialize_mods(input->xkb.state,
2992 XKB_STATE_DEPRESSED |
2994 input->modifiers = 0;
2995 if (mask & input->xkb.control_mask)
2996 input->modifiers |= MOD_CONTROL_MASK;
2997 if (mask & input->xkb.alt_mask)
2998 input->modifiers |= MOD_ALT_MASK;
2999 if (mask & input->xkb.shift_mask)
3000 input->modifiers |= MOD_SHIFT_MASK;
3003 static const struct wl_keyboard_listener keyboard_listener = {
3004 keyboard_handle_keymap,
3005 keyboard_handle_enter,
3006 keyboard_handle_leave,
3007 keyboard_handle_key,
3008 keyboard_handle_modifiers,
3012 touch_handle_down(void *data, struct wl_touch *wl_touch,
3013 uint32_t serial, uint32_t time, struct wl_surface *surface,
3014 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3016 struct input *input = data;
3017 struct widget *widget;
3018 float sx = wl_fixed_to_double(x_w);
3019 float sy = wl_fixed_to_double(y_w);
3021 input->display->serial = serial;
3022 input->touch_focus = wl_surface_get_user_data(surface);
3023 if (!input->touch_focus) {
3024 DBG("Failed to find to touch focus for surface %p\n", surface);
3028 widget = window_find_widget(input->touch_focus,
3029 wl_fixed_to_double(x_w),
3030 wl_fixed_to_double(y_w));
3032 struct touch_point *tp = xmalloc(sizeof *tp);
3035 tp->widget = widget;
3038 wl_list_insert(&input->touch_point_list, &tp->link);
3040 if (widget->touch_down_handler)
3041 (*widget->touch_down_handler)(widget, input,
3050 touch_handle_up(void *data, struct wl_touch *wl_touch,
3051 uint32_t serial, uint32_t time, int32_t id)
3053 struct input *input = data;
3054 struct touch_point *tp, *tmp;
3056 if (!input->touch_focus) {
3057 DBG("No touch focus found for touch up event!\n");
3061 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3065 if (tp->widget->touch_up_handler)
3066 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3068 tp->widget->user_data);
3070 wl_list_remove(&tp->link);
3078 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3079 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3081 struct input *input = data;
3082 struct touch_point *tp;
3083 float sx = wl_fixed_to_double(x_w);
3084 float sy = wl_fixed_to_double(y_w);
3086 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3088 if (!input->touch_focus) {
3089 DBG("No touch focus found for touch motion event!\n");
3093 wl_list_for_each(tp, &input->touch_point_list, link) {
3099 if (tp->widget->touch_motion_handler)
3100 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3102 tp->widget->user_data);
3108 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3110 struct input *input = data;
3111 struct touch_point *tp, *tmp;
3113 DBG("touch_handle_frame\n");
3115 if (!input->touch_focus) {
3116 DBG("No touch focus found for touch frame event!\n");
3120 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3121 if (tp->widget->touch_frame_handler)
3122 (*tp->widget->touch_frame_handler)(tp->widget, input,
3123 tp->widget->user_data);
3125 wl_list_remove(&tp->link);
3131 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3133 struct input *input = data;
3134 struct touch_point *tp, *tmp;
3136 DBG("touch_handle_cancel\n");
3138 if (!input->touch_focus) {
3139 DBG("No touch focus found for touch cancel event!\n");
3143 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3144 if (tp->widget->touch_cancel_handler)
3145 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3146 tp->widget->user_data);
3148 wl_list_remove(&tp->link);
3153 static const struct wl_touch_listener touch_listener = {
3156 touch_handle_motion,
3158 touch_handle_cancel,
3162 seat_handle_capabilities(void *data, struct wl_seat *seat,
3163 enum wl_seat_capability caps)
3165 struct input *input = data;
3167 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3168 input->pointer = wl_seat_get_pointer(seat);
3169 wl_pointer_set_user_data(input->pointer, input);
3170 wl_pointer_add_listener(input->pointer, &pointer_listener,
3172 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3173 wl_pointer_destroy(input->pointer);
3174 input->pointer = NULL;
3177 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3178 input->keyboard = wl_seat_get_keyboard(seat);
3179 wl_keyboard_set_user_data(input->keyboard, input);
3180 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3182 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3183 wl_keyboard_destroy(input->keyboard);
3184 input->keyboard = NULL;
3187 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3188 input->touch = wl_seat_get_touch(seat);
3189 wl_touch_set_user_data(input->touch, input);
3190 wl_touch_add_listener(input->touch, &touch_listener, input);
3191 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3192 wl_touch_destroy(input->touch);
3193 input->touch = NULL;
3198 seat_handle_name(void *data, struct wl_seat *seat,
3204 static const struct wl_seat_listener seat_listener = {
3205 seat_handle_capabilities,
3210 input_get_position(struct input *input, int32_t *x, int32_t *y)
3217 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3219 struct touch_point *tp;
3221 wl_list_for_each(tp, &input->touch_point_list, link) {
3234 input_get_display(struct input *input)
3236 return input->display;
3240 input_get_seat(struct input *input)
3246 input_get_modifiers(struct input *input)
3248 return input->modifiers;
3252 input_get_focus_widget(struct input *input)
3254 return input->focus_widget;
3258 struct wl_data_offer *offer;
3259 struct input *input;
3260 struct wl_array types;
3263 struct task io_task;
3271 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3273 struct data_offer *offer = data;
3276 p = wl_array_add(&offer->types, sizeof *p);
3280 static const struct wl_data_offer_listener data_offer_listener = {
3285 data_offer_destroy(struct data_offer *offer)
3290 if (offer->refcount == 0) {
3291 wl_data_offer_destroy(offer->offer);
3292 for (p = offer->types.data; *p; p++)
3294 wl_array_release(&offer->types);
3300 data_device_data_offer(void *data,
3301 struct wl_data_device *data_device,
3302 struct wl_data_offer *_offer)
3304 struct data_offer *offer;
3306 offer = xmalloc(sizeof *offer);
3308 wl_array_init(&offer->types);
3309 offer->refcount = 1;
3310 offer->input = data;
3311 offer->offer = _offer;
3312 wl_data_offer_add_listener(offer->offer,
3313 &data_offer_listener, offer);
3317 data_device_enter(void *data, struct wl_data_device *data_device,
3318 uint32_t serial, struct wl_surface *surface,
3319 wl_fixed_t x_w, wl_fixed_t y_w,
3320 struct wl_data_offer *offer)
3322 struct input *input = data;
3323 struct window *window;
3325 float x = wl_fixed_to_double(x_w);
3326 float y = wl_fixed_to_double(y_w);
3329 window = wl_surface_get_user_data(surface);
3330 input->drag_enter_serial = serial;
3331 input->drag_focus = window,
3335 if (!input->touch_grab)
3336 input->pointer_enter_serial = serial;
3339 input->drag_offer = wl_data_offer_get_user_data(offer);
3341 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3344 types_data = input->drag_offer->types.data;
3346 input->drag_offer = NULL;
3350 if (window->data_handler)
3351 window->data_handler(window, input, x, y, types_data,
3356 data_device_leave(void *data, struct wl_data_device *data_device)
3358 struct input *input = data;
3360 if (input->drag_offer) {
3361 data_offer_destroy(input->drag_offer);
3362 input->drag_offer = NULL;
3367 data_device_motion(void *data, struct wl_data_device *data_device,
3368 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3370 struct input *input = data;
3371 struct window *window = input->drag_focus;
3372 float x = wl_fixed_to_double(x_w);
3373 float y = wl_fixed_to_double(y_w);
3379 if (input->drag_offer)
3380 types_data = input->drag_offer->types.data;
3384 if (window->data_handler)
3385 window->data_handler(window, input, x, y, types_data,
3390 data_device_drop(void *data, struct wl_data_device *data_device)
3392 struct input *input = data;
3393 struct window *window = input->drag_focus;
3399 if (window->drop_handler)
3400 window->drop_handler(window, input,
3401 x, y, window->user_data);
3403 if (input->touch_grab)
3404 touch_ungrab(input);
3408 data_device_selection(void *data,
3409 struct wl_data_device *wl_data_device,
3410 struct wl_data_offer *offer)
3412 struct input *input = data;
3415 if (input->selection_offer)
3416 data_offer_destroy(input->selection_offer);
3419 input->selection_offer = wl_data_offer_get_user_data(offer);
3420 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3423 input->selection_offer = NULL;
3427 static const struct wl_data_device_listener data_device_listener = {
3428 data_device_data_offer,
3433 data_device_selection
3437 input_set_pointer_image_index(struct input *input, int index)
3439 struct wl_buffer *buffer;
3440 struct wl_cursor *cursor;
3441 struct wl_cursor_image *image;
3443 if (!input->pointer)
3446 cursor = input->display->cursors[input->current_cursor];
3450 if (index >= (int) cursor->image_count) {
3451 fprintf(stderr, "cursor index out of range\n");
3455 image = cursor->images[index];
3456 buffer = wl_cursor_image_get_buffer(image);
3460 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3461 input->pointer_surface,
3462 image->hotspot_x, image->hotspot_y);
3463 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3464 wl_surface_damage(input->pointer_surface, 0, 0,
3465 image->width, image->height);
3466 wl_surface_commit(input->pointer_surface);
3469 static const struct wl_callback_listener pointer_surface_listener;
3472 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3475 struct input *input = data;
3476 struct wl_cursor *cursor;
3480 assert(callback == input->cursor_frame_cb);
3481 wl_callback_destroy(callback);
3482 input->cursor_frame_cb = NULL;
3485 if (!input->pointer)
3488 if (input->current_cursor == CURSOR_BLANK) {
3489 wl_pointer_set_cursor(input->pointer,
3490 input->pointer_enter_serial,
3495 if (input->current_cursor == CURSOR_UNSET)
3497 cursor = input->display->cursors[input->current_cursor];
3501 /* FIXME We don't have the current time on the first call so we set
3502 * the animation start to the time of the first frame callback. */
3504 input->cursor_anim_start = 0;
3505 else if (input->cursor_anim_start == 0)
3506 input->cursor_anim_start = time;
3508 if (time == 0 || input->cursor_anim_start == 0)
3511 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3513 if (cursor->image_count > 1) {
3514 input->cursor_frame_cb =
3515 wl_surface_frame(input->pointer_surface);
3516 wl_callback_add_listener(input->cursor_frame_cb,
3517 &pointer_surface_listener, input);
3520 input_set_pointer_image_index(input, i);
3523 static const struct wl_callback_listener pointer_surface_listener = {
3524 pointer_surface_frame_callback
3528 input_set_pointer_image(struct input *input, int pointer)
3532 if (!input->pointer)
3535 if (input->pointer_enter_serial > input->cursor_serial)
3538 if (!force && pointer == input->current_cursor)
3541 input->current_cursor = pointer;
3542 input->cursor_serial = input->pointer_enter_serial;
3543 if (!input->cursor_frame_cb)
3544 pointer_surface_frame_callback(input, NULL, 0);
3546 /* The current frame callback may be stuck if, for instance,
3547 * the set cursor request was processed by the server after
3548 * this client lost the focus. In this case the cursor surface
3549 * might not be mapped and the frame callback wouldn't ever
3550 * complete. Send a set_cursor and attach to try to map the
3551 * cursor surface again so that the callback will finish */
3552 input_set_pointer_image_index(input, 0);
3556 struct wl_data_device *
3557 input_get_data_device(struct input *input)
3559 return input->data_device;
3563 input_set_selection(struct input *input,
3564 struct wl_data_source *source, uint32_t time)
3566 wl_data_device_set_selection(input->data_device, source, time);
3570 input_accept(struct input *input, const char *type)
3572 wl_data_offer_accept(input->drag_offer->offer,
3573 input->drag_enter_serial, type);
3577 offer_io_func(struct task *task, uint32_t events)
3579 struct data_offer *offer =
3580 container_of(task, struct data_offer, io_task);
3584 len = read(offer->fd, buffer, sizeof buffer);
3585 offer->func(buffer, len,
3586 offer->x, offer->y, offer->user_data);
3590 data_offer_destroy(offer);
3595 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3596 data_func_t func, void *user_data)
3600 if (pipe2(p, O_CLOEXEC) == -1)
3603 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3606 offer->io_task.run = offer_io_func;
3610 offer->user_data = user_data;
3612 display_watch_fd(offer->input->display,
3613 offer->fd, EPOLLIN, &offer->io_task);
3617 input_receive_drag_data(struct input *input, const char *mime_type,
3618 data_func_t func, void *data)
3620 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3621 input->drag_offer->x = input->drag_x;
3622 input->drag_offer->y = input->drag_y;
3626 input_receive_drag_data_to_fd(struct input *input,
3627 const char *mime_type, int fd)
3629 if (input->drag_offer)
3630 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3636 input_receive_selection_data(struct input *input, const char *mime_type,
3637 data_func_t func, void *data)
3641 if (input->selection_offer == NULL)
3644 for (p = input->selection_offer->types.data; *p; p++)
3645 if (strcmp(mime_type, *p) == 0)
3651 data_offer_receive_data(input->selection_offer,
3652 mime_type, func, data);
3657 input_receive_selection_data_to_fd(struct input *input,
3658 const char *mime_type, int fd)
3660 if (input->selection_offer)
3661 wl_data_offer_receive(input->selection_offer->offer,
3668 window_move(struct window *window, struct input *input, uint32_t serial)
3670 if (!window->shell_surface)
3673 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3677 surface_set_synchronized(struct surface *surface)
3679 if (!surface->subsurface)
3682 if (surface->synchronized)
3685 wl_subsurface_set_sync(surface->subsurface);
3686 surface->synchronized = 1;
3690 surface_set_synchronized_default(struct surface *surface)
3692 if (!surface->subsurface)
3695 if (surface->synchronized == surface->synchronized_default)
3698 if (surface->synchronized_default)
3699 wl_subsurface_set_sync(surface->subsurface);
3701 wl_subsurface_set_desync(surface->subsurface);
3703 surface->synchronized = surface->synchronized_default;
3707 surface_resize(struct surface *surface)
3709 struct widget *widget = surface->widget;
3710 struct wl_compositor *compositor = widget->window->display->compositor;
3712 if (surface->input_region) {
3713 wl_region_destroy(surface->input_region);
3714 surface->input_region = NULL;
3717 if (surface->opaque_region)
3718 wl_region_destroy(surface->opaque_region);
3720 surface->opaque_region = wl_compositor_create_region(compositor);
3722 if (widget->resize_handler)
3723 widget->resize_handler(widget,
3724 widget->allocation.width,
3725 widget->allocation.height,
3728 if (surface->subsurface &&
3729 (surface->allocation.x != widget->allocation.x ||
3730 surface->allocation.y != widget->allocation.y)) {
3731 wl_subsurface_set_position(surface->subsurface,
3732 widget->allocation.x,
3733 widget->allocation.y);
3735 if (surface->allocation.width != widget->allocation.width ||
3736 surface->allocation.height != widget->allocation.height) {
3737 window_schedule_redraw(widget->window);
3739 surface->allocation = widget->allocation;
3742 wl_region_add(surface->opaque_region, 0, 0,
3743 widget->allocation.width,
3744 widget->allocation.height);
3748 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3751 * This hack should be removed, when EGL respects
3752 * eglSwapInterval(0).
3754 * If this window has sub-surfaces, especially a free-running
3755 * EGL-widget, we need to post the parent surface once with
3756 * all the old state to guarantee, that the EGL-widget will
3757 * receive its frame callback soon. Otherwise, a forced call
3758 * to eglSwapBuffers may end up blocking, waiting for a frame
3759 * event that will never come, because we will commit the parent
3760 * surface with all new state only after eglSwapBuffers returns.
3762 * This assumes, that:
3763 * 1. When the EGL widget's resize hook is called, it pauses.
3764 * 2. When the EGL widget's redraw hook is called, it forces a
3765 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3766 * In a single threaded application condition 1 is a no-op.
3768 * XXX: This should actually be after the surface_resize() calls,
3769 * but cannot, because then it would commit the incomplete state
3770 * accumulated from the widget resize hooks.
3772 if (window->subsurface_list.next != &window->main_surface->link ||
3773 window->subsurface_list.prev != &window->main_surface->link)
3774 wl_surface_commit(window->main_surface->surface);
3778 window_do_resize(struct window *window)
3780 struct surface *surface;
3782 widget_set_allocation(window->main_surface->widget,
3783 window->pending_allocation.x,
3784 window->pending_allocation.y,
3785 window->pending_allocation.width,
3786 window->pending_allocation.height);
3788 surface_resize(window->main_surface);
3790 /* The main surface is in the list, too. Main surface's
3791 * resize_handler is responsible for calling widget_set_allocation()
3792 * on all sub-surface root widgets, so they will be resized
3795 wl_list_for_each(surface, &window->subsurface_list, link) {
3796 if (surface == window->main_surface)
3799 surface_set_synchronized(surface);
3800 surface_resize(surface);
3805 idle_resize(struct window *window)
3807 window->resize_needed = 0;
3808 window->redraw_needed = 1;
3810 DBG("from %dx%d to %dx%d\n",
3811 window->main_surface->server_allocation.width,
3812 window->main_surface->server_allocation.height,
3813 window->pending_allocation.width,
3814 window->pending_allocation.height);
3816 hack_prevent_EGL_sub_surface_deadlock(window);
3818 window_do_resize(window);
3822 undo_resize(struct window *window)
3824 window->pending_allocation.width =
3825 window->main_surface->server_allocation.width;
3826 window->pending_allocation.height =
3827 window->main_surface->server_allocation.height;
3829 DBG("back to %dx%d\n",
3830 window->main_surface->server_allocation.width,
3831 window->main_surface->server_allocation.height);
3833 window_do_resize(window);
3835 if (window->pending_allocation.width == 0 &&
3836 window->pending_allocation.height == 0) {
3837 fprintf(stderr, "Error: Could not draw a surface, "
3838 "most likely due to insufficient disk space in "
3839 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3845 window_schedule_resize(struct window *window, int width, int height)
3847 /* We should probably get these numbers from the theme. */
3848 const int min_width = 200, min_height = 200;
3850 window->pending_allocation.x = 0;
3851 window->pending_allocation.y = 0;
3852 window->pending_allocation.width = width;
3853 window->pending_allocation.height = height;
3855 if (window->min_allocation.width == 0) {
3856 if (width < min_width && window->frame)
3857 window->min_allocation.width = min_width;
3859 window->min_allocation.width = width;
3860 if (height < min_height && window->frame)
3861 window->min_allocation.height = min_height;
3863 window->min_allocation.height = height;
3866 if (window->pending_allocation.width < window->min_allocation.width)
3867 window->pending_allocation.width = window->min_allocation.width;
3868 if (window->pending_allocation.height < window->min_allocation.height)
3869 window->pending_allocation.height = window->min_allocation.height;
3871 window->resize_needed = 1;
3872 window_schedule_redraw(window);
3876 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3878 window_schedule_resize(widget->window, width, height);
3882 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3885 wl_shell_surface_pong(shell_surface, serial);
3889 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3890 uint32_t edges, int32_t width, int32_t height)
3892 struct window *window = data;
3894 window->resize_edges = edges;
3895 window_schedule_resize(window, width, height);
3899 menu_destroy(struct menu *menu)
3901 widget_destroy(menu->widget);
3902 window_destroy(menu->window);
3903 frame_destroy(menu->frame);
3908 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3910 struct window *window = data;
3911 struct menu *menu = window->main_surface->widget->user_data;
3913 /* FIXME: Need more context in this event, at least the input
3914 * device. Or just use wl_callback. And this really needs to
3915 * be a window vfunc that the menu can set. And we need the
3918 input_ungrab(menu->input);
3922 static const struct wl_shell_surface_listener shell_surface_listener = {
3929 window_get_allocation(struct window *window,
3930 struct rectangle *allocation)
3932 *allocation = window->main_surface->allocation;
3936 widget_redraw(struct widget *widget)
3938 struct widget *child;
3940 if (widget->redraw_handler)
3941 widget->redraw_handler(widget, widget->user_data);
3942 wl_list_for_each(child, &widget->child_list, link)
3943 widget_redraw(child);
3947 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3949 struct surface *surface = data;
3951 assert(callback == surface->frame_cb);
3952 DBG_OBJ(callback, "done\n");
3953 wl_callback_destroy(callback);
3954 surface->frame_cb = NULL;
3956 surface->last_time = time;
3958 if (surface->redraw_needed || surface->window->redraw_needed) {
3959 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3960 window_schedule_redraw_task(surface->window);
3964 static const struct wl_callback_listener listener = {
3969 surface_redraw(struct surface *surface)
3971 DBG_OBJ(surface->surface, "begin\n");
3973 if (!surface->window->redraw_needed && !surface->redraw_needed)
3976 /* Whole-window redraw forces a redraw even if the previous has
3977 * not yet hit the screen.
3979 if (surface->frame_cb) {
3980 if (!surface->window->redraw_needed)
3983 DBG_OBJ(surface->frame_cb, "cancelled\n");
3984 wl_callback_destroy(surface->frame_cb);
3987 if (surface->widget->use_cairo &&
3988 !widget_get_cairo_surface(surface->widget)) {
3989 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3993 surface->frame_cb = wl_surface_frame(surface->surface);
3994 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3995 DBG_OBJ(surface->frame_cb, "new\n");
3997 surface->redraw_needed = 0;
3998 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3999 widget_redraw(surface->widget);
4000 DBG_OBJ(surface->surface, "done\n");
4005 idle_redraw(struct task *task, uint32_t events)
4007 struct window *window = container_of(task, struct window, redraw_task);
4008 struct surface *surface;
4012 DBG(" --------- \n");
4014 wl_list_init(&window->redraw_task.link);
4015 window->redraw_task_scheduled = 0;
4017 if (window->resize_needed) {
4018 /* throttle resizing to the main surface display */
4019 if (window->main_surface->frame_cb) {
4020 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4024 idle_resize(window);
4028 if (surface_redraw(window->main_surface) < 0) {
4030 * Only main_surface failure will cause us to undo the resize.
4031 * If sub-surfaces fail, they will just be broken with old
4036 wl_list_for_each(surface, &window->subsurface_list, link) {
4037 if (surface == window->main_surface)
4040 surface_redraw(surface);
4044 window->redraw_needed = 0;
4045 window_flush(window);
4047 wl_list_for_each(surface, &window->subsurface_list, link)
4048 surface_set_synchronized_default(surface);
4050 if (resized && failed) {
4051 /* Restore widget tree to correspond to what is on screen. */
4052 undo_resize(window);
4057 window_schedule_redraw_task(struct window *window)
4059 if (window->configure_requests)
4061 if (!window->redraw_task_scheduled) {
4062 window->redraw_task.run = idle_redraw;
4063 display_defer(window->display, &window->redraw_task);
4064 window->redraw_task_scheduled = 1;
4069 window_schedule_redraw(struct window *window)
4071 struct surface *surface;
4073 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4075 wl_list_for_each(surface, &window->subsurface_list, link)
4076 surface->redraw_needed = 1;
4078 window_schedule_redraw_task(window);
4082 window_is_fullscreen(struct window *window)
4084 return window->type == TYPE_FULLSCREEN;
4088 window_is_transient(struct window *window)
4090 return window->type == TYPE_TRANSIENT;
4094 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4096 struct window *window = data;
4098 wl_callback_destroy(callback);
4099 window->configure_requests--;
4101 if (!window->configure_requests)
4102 window_schedule_redraw(window);
4105 static struct wl_callback_listener configure_request_listener = {
4106 configure_request_completed,
4110 window_defer_redraw_until_configure(struct window* window)
4112 struct wl_callback *callback;
4114 if (window->redraw_task_scheduled) {
4115 wl_list_remove(&window->redraw_task.link);
4116 window->redraw_task_scheduled = 0;
4119 callback = wl_display_sync(window->display->display);
4120 wl_callback_add_listener(callback, &configure_request_listener, window);
4121 window->configure_requests++;
4125 window_set_fullscreen(struct window *window, int fullscreen)
4127 if (!window->display->shell)
4130 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4134 window->saved_type = window->type;
4135 if (window->type == TYPE_TOPLEVEL) {
4136 window->saved_allocation = window->main_surface->allocation;
4138 window->type = TYPE_FULLSCREEN;
4139 wl_shell_surface_set_fullscreen(window->shell_surface,
4140 window->fullscreen_method,
4142 window_defer_redraw_until_configure (window);
4144 if (window->saved_type == TYPE_MAXIMIZED) {
4145 window_set_maximized(window, 1);
4147 window->type = TYPE_TOPLEVEL;
4148 wl_shell_surface_set_toplevel(window->shell_surface);
4149 window_schedule_resize(window,
4150 window->saved_allocation.width,
4151 window->saved_allocation.height);
4158 window_set_fullscreen_method(struct window *window,
4159 enum wl_shell_surface_fullscreen_method method)
4161 window->fullscreen_method = method;
4165 window_is_maximized(struct window *window)
4167 return window->type == TYPE_MAXIMIZED;
4171 window_set_maximized(struct window *window, int maximized)
4173 if (!window->display->shell)
4176 if ((window->type == TYPE_MAXIMIZED) == maximized)
4179 if (window->type == TYPE_TOPLEVEL) {
4180 window->saved_allocation = window->main_surface->allocation;
4181 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4182 window->type = TYPE_MAXIMIZED;
4183 window_defer_redraw_until_configure(window);
4184 } else if (window->type == TYPE_FULLSCREEN) {
4185 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4186 window->type = TYPE_MAXIMIZED;
4187 window_defer_redraw_until_configure(window);
4189 wl_shell_surface_set_toplevel(window->shell_surface);
4190 window->type = TYPE_TOPLEVEL;
4191 window_schedule_resize(window,
4192 window->saved_allocation.width,
4193 window->saved_allocation.height);
4198 window_set_user_data(struct window *window, void *data)
4200 window->user_data = data;
4204 window_get_user_data(struct window *window)
4206 return window->user_data;
4210 window_set_key_handler(struct window *window,
4211 window_key_handler_t handler)
4213 window->key_handler = handler;
4217 window_set_keyboard_focus_handler(struct window *window,
4218 window_keyboard_focus_handler_t handler)
4220 window->keyboard_focus_handler = handler;
4224 window_set_data_handler(struct window *window, window_data_handler_t handler)
4226 window->data_handler = handler;
4230 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4232 window->drop_handler = handler;
4236 window_set_close_handler(struct window *window,
4237 window_close_handler_t handler)
4239 window->close_handler = handler;
4243 window_set_fullscreen_handler(struct window *window,
4244 window_fullscreen_handler_t handler)
4246 window->fullscreen_handler = handler;
4250 window_set_output_handler(struct window *window,
4251 window_output_handler_t handler)
4253 window->output_handler = handler;
4257 window_set_title(struct window *window, const char *title)
4259 free(window->title);
4260 window->title = strdup(title);
4261 if (window->frame) {
4262 frame_set_title(window->frame->frame, title);
4263 widget_schedule_redraw(window->frame->widget);
4265 if (window->shell_surface)
4266 wl_shell_surface_set_title(window->shell_surface, title);
4270 window_get_title(struct window *window)
4272 return window->title;
4276 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4278 struct text_cursor_position *text_cursor_position =
4279 window->display->text_cursor_position;
4281 if (!text_cursor_position)
4284 text_cursor_position_notify(text_cursor_position,
4285 window->main_surface->surface,
4286 wl_fixed_from_int(x),
4287 wl_fixed_from_int(y));
4291 window_damage(struct window *window, int32_t x, int32_t y,
4292 int32_t width, int32_t height)
4294 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4298 surface_enter(void *data,
4299 struct wl_surface *wl_surface, struct wl_output *wl_output)
4301 struct window *window = data;
4302 struct output *output;
4303 struct output *output_found = NULL;
4304 struct window_output *window_output;
4306 wl_list_for_each(output, &window->display->output_list, link) {
4307 if (output->output == wl_output) {
4308 output_found = output;
4316 window_output = xmalloc(sizeof *window_output);
4317 window_output->output = output_found;
4319 wl_list_insert (&window->window_output_list, &window_output->link);
4321 if (window->output_handler)
4322 window->output_handler(window, output_found, 1,
4327 surface_leave(void *data,
4328 struct wl_surface *wl_surface, struct wl_output *output)
4330 struct window *window = data;
4331 struct window_output *window_output;
4332 struct window_output *window_output_found = NULL;
4334 wl_list_for_each(window_output, &window->window_output_list, link) {
4335 if (window_output->output->output == output) {
4336 window_output_found = window_output;
4341 if (window_output_found) {
4342 wl_list_remove(&window_output_found->link);
4344 if (window->output_handler)
4345 window->output_handler(window, window_output->output,
4346 0, window->user_data);
4348 free(window_output_found);
4352 static const struct wl_surface_listener surface_listener = {
4357 static struct surface *
4358 surface_create(struct window *window)
4360 struct display *display = window->display;
4361 struct surface *surface;
4363 surface = xmalloc(sizeof *surface);
4364 memset(surface, 0, sizeof *surface);
4368 surface->window = window;
4369 surface->surface = wl_compositor_create_surface(display->compositor);
4370 surface->buffer_scale = 1;
4371 wl_surface_add_listener(surface->surface, &surface_listener, window);
4373 wl_list_insert(&window->subsurface_list, &surface->link);
4378 static struct window *
4379 window_create_internal(struct display *display, int type)
4381 struct window *window;
4382 struct surface *surface;
4384 window = xzalloc(sizeof *window);
4385 wl_list_init(&window->subsurface_list);
4386 window->display = display;
4388 surface = surface_create(window);
4389 window->main_surface = surface;
4391 if (type != TYPE_CUSTOM && display->shell) {
4392 window->shell_surface =
4393 wl_shell_get_shell_surface(display->shell,
4395 fail_on_null(window->shell_surface);
4398 window->type = type;
4399 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4400 window->configure_requests = 0;
4401 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4403 if (display->argb_device)
4404 #ifdef HAVE_CAIRO_EGL
4405 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4407 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4410 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4412 wl_surface_set_user_data(surface->surface, window);
4413 wl_list_insert(display->window_list.prev, &window->link);
4414 wl_list_init(&window->redraw_task.link);
4416 if (window->shell_surface) {
4417 wl_shell_surface_set_user_data(window->shell_surface, window);
4418 wl_shell_surface_add_listener(window->shell_surface,
4419 &shell_surface_listener, window);
4422 wl_list_init (&window->window_output_list);
4428 window_create(struct display *display)
4430 return window_create_internal(display, TYPE_NONE);
4434 window_create_custom(struct display *display)
4436 return window_create_internal(display, TYPE_CUSTOM);
4440 window_create_transient(struct display *display, struct window *parent,
4441 int32_t x, int32_t y, uint32_t flags)
4443 struct window *window;
4445 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4451 wl_shell_surface_set_transient(
4452 window->shell_surface,
4453 parent->main_surface->surface,
4454 window->x, window->y, flags);
4460 menu_set_item(struct menu *menu, int sy)
4462 int32_t x, y, width, height;
4465 frame_interior(menu->frame, &x, &y, &width, &height);
4466 next = (sy - y) / 20;
4467 if (menu->current != next) {
4468 menu->current = next;
4469 widget_schedule_redraw(menu->widget);
4474 menu_motion_handler(struct widget *widget,
4475 struct input *input, uint32_t time,
4476 float x, float y, void *data)
4478 struct menu *menu = data;
4480 if (widget == menu->widget)
4481 menu_set_item(data, y);
4483 return CURSOR_LEFT_PTR;
4487 menu_enter_handler(struct widget *widget,
4488 struct input *input, float x, float y, void *data)
4490 struct menu *menu = data;
4492 if (widget == menu->widget)
4493 menu_set_item(data, y);
4495 return CURSOR_LEFT_PTR;
4499 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4501 struct menu *menu = data;
4503 if (widget == menu->widget)
4504 menu_set_item(data, -200);
4508 menu_button_handler(struct widget *widget,
4509 struct input *input, uint32_t time,
4510 uint32_t button, enum wl_pointer_button_state state,
4514 struct menu *menu = data;
4516 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4517 (menu->release_count > 0 || time - menu->time > 500)) {
4518 /* Either relase after press-drag-release or
4519 * click-motion-click. */
4520 menu->func(menu->parent, input,
4521 menu->current, menu->parent->user_data);
4522 input_ungrab(input);
4524 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4525 menu->release_count++;
4530 menu_redraw_handler(struct widget *widget, void *data)
4533 struct menu *menu = data;
4534 int32_t x, y, width, height, i;
4536 cr = widget_cairo_create(widget);
4538 frame_repaint(menu->frame, cr);
4539 frame_interior(menu->frame, &x, &y, &width, &height);
4541 theme_set_background_source(menu->window->display->theme,
4542 cr, THEME_FRAME_ACTIVE);
4543 cairo_rectangle(cr, x, y, width, height);
4546 cairo_select_font_face(cr, "sans",
4547 CAIRO_FONT_SLANT_NORMAL,
4548 CAIRO_FONT_WEIGHT_NORMAL);
4549 cairo_set_font_size(cr, 12);
4551 for (i = 0; i < menu->count; i++) {
4552 if (i == menu->current) {
4553 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4554 cairo_rectangle(cr, x, y + i * 20, width, 20);
4556 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4557 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4558 cairo_show_text(cr, menu->entries[i]);
4560 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4561 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4562 cairo_show_text(cr, menu->entries[i]);
4570 window_show_menu(struct display *display,
4571 struct input *input, uint32_t time, struct window *parent,
4572 int32_t x, int32_t y,
4573 menu_func_t func, const char **entries, int count)
4575 struct window *window;
4579 menu = malloc(sizeof *menu);
4583 window = window_create_internal(parent->display, TYPE_MENU);
4589 menu->window = window;
4590 menu->parent = parent;
4591 menu->widget = window_add_widget(menu->window, menu);
4592 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4593 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4594 menu->frame = frame_create(window->display->theme, 0, 0,
4595 FRAME_BUTTON_NONE, NULL);
4596 menu->entries = entries;
4597 menu->count = count;
4598 menu->release_count = 0;
4602 menu->input = input;
4603 window->type = TYPE_MENU;
4607 input_ungrab(input);
4609 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4610 widget_set_enter_handler(menu->widget, menu_enter_handler);
4611 widget_set_leave_handler(menu->widget, menu_leave_handler);
4612 widget_set_motion_handler(menu->widget, menu_motion_handler);
4613 widget_set_button_handler(menu->widget, menu_button_handler);
4615 input_grab(input, menu->widget, 0);
4616 frame_resize_inside(menu->frame, 200, count * 20);
4617 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4618 window_schedule_resize(window, frame_width(menu->frame),
4619 frame_height(menu->frame));
4621 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4622 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4623 display_get_serial(window->display),
4624 parent->main_surface->surface,
4625 window->x - ix, window->y - iy, 0);
4629 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4631 window->main_surface->buffer_type = type;
4635 window_set_preferred_format(struct window *window,
4636 enum preferred_format format)
4638 window->preferred_format = format;
4642 window_add_subsurface(struct window *window, void *data,
4643 enum subsurface_mode default_mode)
4645 struct widget *widget;
4646 struct surface *surface;
4647 struct wl_surface *parent;
4648 struct wl_subcompositor *subcompo = window->display->subcompositor;
4650 surface = surface_create(window);
4651 widget = widget_create(window, surface, data);
4652 wl_list_init(&widget->link);
4653 surface->widget = widget;
4655 parent = window->main_surface->surface;
4656 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4659 surface->synchronized = 1;
4661 switch (default_mode) {
4662 case SUBSURFACE_SYNCHRONIZED:
4663 surface->synchronized_default = 1;
4665 case SUBSURFACE_DESYNCHRONIZED:
4666 surface->synchronized_default = 0;
4669 assert(!"bad enum subsurface_mode");
4676 display_handle_geometry(void *data,
4677 struct wl_output *wl_output,
4680 int physical_height,
4686 struct output *output = data;
4688 output->allocation.x = x;
4689 output->allocation.y = y;
4690 output->transform = transform;
4694 display_handle_done(void *data,
4695 struct wl_output *wl_output)
4700 display_handle_scale(void *data,
4701 struct wl_output *wl_output,
4704 struct output *output = data;
4706 output->scale = scale;
4710 display_handle_mode(void *data,
4711 struct wl_output *wl_output,
4717 struct output *output = data;
4718 struct display *display = output->display;
4720 if (flags & WL_OUTPUT_MODE_CURRENT) {
4721 output->allocation.width = width;
4722 output->allocation.height = height;
4723 if (display->output_configure_handler)
4724 (*display->output_configure_handler)(
4725 output, display->user_data);
4729 static const struct wl_output_listener output_listener = {
4730 display_handle_geometry,
4731 display_handle_mode,
4732 display_handle_done,
4733 display_handle_scale
4737 display_add_output(struct display *d, uint32_t id)
4739 struct output *output;
4741 output = xzalloc(sizeof *output);
4742 output->display = d;
4745 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4746 output->server_output_id = id;
4747 wl_list_insert(d->output_list.prev, &output->link);
4749 wl_output_add_listener(output->output, &output_listener, output);
4753 output_destroy(struct output *output)
4755 if (output->destroy_handler)
4756 (*output->destroy_handler)(output, output->user_data);
4758 wl_output_destroy(output->output);
4759 wl_list_remove(&output->link);
4764 display_destroy_output(struct display *d, uint32_t id)
4766 struct output *output;
4768 wl_list_for_each(output, &d->output_list, link) {
4769 if (output->server_output_id == id) {
4770 output_destroy(output);
4777 display_set_global_handler(struct display *display,
4778 display_global_handler_t handler)
4780 struct global *global;
4782 display->global_handler = handler;
4786 wl_list_for_each(global, &display->global_list, link)
4787 display->global_handler(display,
4788 global->name, global->interface,
4789 global->version, display->user_data);
4793 display_set_global_handler_remove(struct display *display,
4794 display_global_handler_t remove_handler)
4796 display->global_handler_remove = remove_handler;
4797 if (!remove_handler)
4802 display_set_output_configure_handler(struct display *display,
4803 display_output_handler_t handler)
4805 struct output *output;
4807 display->output_configure_handler = handler;
4811 wl_list_for_each(output, &display->output_list, link) {
4812 if (output->allocation.width == 0 &&
4813 output->allocation.height == 0)
4816 (*display->output_configure_handler)(output,
4817 display->user_data);
4822 output_set_user_data(struct output *output, void *data)
4824 output->user_data = data;
4828 output_get_user_data(struct output *output)
4830 return output->user_data;
4834 output_set_destroy_handler(struct output *output,
4835 display_output_handler_t handler)
4837 output->destroy_handler = handler;
4838 /* FIXME: implement this, once we have way to remove outputs */
4842 output_get_allocation(struct output *output, struct rectangle *base)
4844 struct rectangle allocation = output->allocation;
4846 switch (output->transform) {
4847 case WL_OUTPUT_TRANSFORM_90:
4848 case WL_OUTPUT_TRANSFORM_270:
4849 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4850 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4851 /* Swap width and height */
4852 allocation.width = output->allocation.height;
4853 allocation.height = output->allocation.width;
4861 output_get_wl_output(struct output *output)
4863 return output->output;
4866 enum wl_output_transform
4867 output_get_transform(struct output *output)
4869 return output->transform;
4873 output_get_scale(struct output *output)
4875 return output->scale;
4879 fini_xkb(struct input *input)
4881 xkb_state_unref(input->xkb.state);
4882 xkb_map_unref(input->xkb.keymap);
4885 #define MIN(a,b) ((a) < (b) ? a : b)
4888 display_add_input(struct display *d, uint32_t id)
4890 struct input *input;
4892 input = xzalloc(sizeof *input);
4894 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4895 MIN(d->seat_version, 3));
4896 input->touch_focus = NULL;
4897 input->pointer_focus = NULL;
4898 input->keyboard_focus = NULL;
4899 wl_list_init(&input->touch_point_list);
4900 wl_list_insert(d->input_list.prev, &input->link);
4902 wl_seat_add_listener(input->seat, &seat_listener, input);
4903 wl_seat_set_user_data(input->seat, input);
4905 input->data_device =
4906 wl_data_device_manager_get_data_device(d->data_device_manager,
4908 wl_data_device_add_listener(input->data_device, &data_device_listener,
4911 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4913 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4914 TFD_CLOEXEC | TFD_NONBLOCK);
4915 input->repeat_task.run = keyboard_repeat_func;
4916 display_watch_fd(d, input->repeat_timer_fd,
4917 EPOLLIN, &input->repeat_task);
4921 input_destroy(struct input *input)
4923 input_remove_keyboard_focus(input);
4924 input_remove_pointer_focus(input);
4926 if (input->drag_offer)
4927 data_offer_destroy(input->drag_offer);
4929 if (input->selection_offer)
4930 data_offer_destroy(input->selection_offer);
4932 wl_data_device_destroy(input->data_device);
4934 if (input->display->seat_version >= 3) {
4936 wl_pointer_release(input->pointer);
4937 if (input->keyboard)
4938 wl_keyboard_release(input->keyboard);
4943 wl_surface_destroy(input->pointer_surface);
4945 wl_list_remove(&input->link);
4946 wl_seat_destroy(input->seat);
4947 close(input->repeat_timer_fd);
4952 init_workspace_manager(struct display *d, uint32_t id)
4954 d->workspace_manager =
4955 wl_registry_bind(d->registry, id,
4956 &workspace_manager_interface, 1);
4957 if (d->workspace_manager != NULL)
4958 workspace_manager_add_listener(d->workspace_manager,
4959 &workspace_manager_listener,
4964 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4966 struct display *d = data;
4968 if (format == WL_SHM_FORMAT_RGB565)
4972 struct wl_shm_listener shm_listener = {
4977 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4978 const char *interface, uint32_t version)
4980 struct display *d = data;
4981 struct global *global;
4983 global = xmalloc(sizeof *global);
4985 global->interface = strdup(interface);
4986 global->version = version;
4987 wl_list_insert(d->global_list.prev, &global->link);
4989 if (strcmp(interface, "wl_compositor") == 0) {
4990 d->compositor = wl_registry_bind(registry, id,
4991 &wl_compositor_interface, 3);
4992 } else if (strcmp(interface, "wl_output") == 0) {
4993 display_add_output(d, id);
4994 } else if (strcmp(interface, "wl_seat") == 0) {
4995 d->seat_version = version;
4996 display_add_input(d, id);
4997 } else if (strcmp(interface, "wl_shell") == 0) {
4998 d->shell = wl_registry_bind(registry,
4999 id, &wl_shell_interface, 1);
5000 } else if (strcmp(interface, "wl_shm") == 0) {
5001 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5002 wl_shm_add_listener(d->shm, &shm_listener, d);
5003 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5004 d->data_device_manager =
5005 wl_registry_bind(registry, id,
5006 &wl_data_device_manager_interface, 1);
5007 } else if (strcmp(interface, "text_cursor_position") == 0) {
5008 d->text_cursor_position =
5009 wl_registry_bind(registry, id,
5010 &text_cursor_position_interface, 1);
5011 } else if (strcmp(interface, "workspace_manager") == 0) {
5012 init_workspace_manager(d, id);
5013 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5015 wl_registry_bind(registry, id,
5016 &wl_subcompositor_interface, 1);
5019 if (d->global_handler)
5020 d->global_handler(d, id, interface, version, d->user_data);
5024 registry_handle_global_remove(void *data, struct wl_registry *registry,
5027 struct display *d = data;
5028 struct global *global;
5031 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5032 if (global->name != name)
5035 if (strcmp(global->interface, "wl_output") == 0)
5036 display_destroy_output(d, name);
5038 /* XXX: Should destroy remaining bound globals */
5040 if (d->global_handler_remove)
5041 d->global_handler_remove(d, name, global->interface,
5042 global->version, d->user_data);
5044 wl_list_remove(&global->link);
5045 free(global->interface);
5051 display_bind(struct display *display, uint32_t name,
5052 const struct wl_interface *interface, uint32_t version)
5054 return wl_registry_bind(display->registry, name, interface, version);
5057 static const struct wl_registry_listener registry_listener = {
5058 registry_handle_global,
5059 registry_handle_global_remove
5062 #ifdef HAVE_CAIRO_EGL
5064 init_egl(struct display *d)
5066 EGLint major, minor;
5069 #ifdef USE_CAIRO_GLESV2
5070 # define GL_BIT EGL_OPENGL_ES2_BIT
5072 # define GL_BIT EGL_OPENGL_BIT
5075 static const EGLint argb_cfg_attribs[] = {
5076 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5082 EGL_RENDERABLE_TYPE, GL_BIT,
5086 #ifdef USE_CAIRO_GLESV2
5087 static const EGLint context_attribs[] = {
5088 EGL_CONTEXT_CLIENT_VERSION, 2,
5091 EGLint api = EGL_OPENGL_ES_API;
5093 EGLint *context_attribs = NULL;
5094 EGLint api = EGL_OPENGL_API;
5097 d->dpy = eglGetDisplay(d->display);
5098 if (!eglInitialize(d->dpy, &major, &minor)) {
5099 fprintf(stderr, "failed to initialize EGL\n");
5103 if (!eglBindAPI(api)) {
5104 fprintf(stderr, "failed to bind EGL client API\n");
5108 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5109 &d->argb_config, 1, &n) || n != 1) {
5110 fprintf(stderr, "failed to choose argb EGL config\n");
5114 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5115 EGL_NO_CONTEXT, context_attribs);
5116 if (d->argb_ctx == NULL) {
5117 fprintf(stderr, "failed to create EGL context\n");
5121 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5122 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5123 fprintf(stderr, "failed to get cairo EGL argb device\n");
5131 fini_egl(struct display *display)
5133 cairo_device_destroy(display->argb_device);
5135 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5138 eglTerminate(display->dpy);
5144 init_dummy_surface(struct display *display)
5149 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5151 display->dummy_surface =
5152 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5154 display->dummy_surface_data = data;
5158 handle_display_data(struct task *task, uint32_t events)
5160 struct display *display =
5161 container_of(task, struct display, display_task);
5162 struct epoll_event ep;
5165 display->display_fd_events = events;
5167 if (events & EPOLLERR || events & EPOLLHUP) {
5168 display_exit(display);
5172 if (events & EPOLLIN) {
5173 ret = wl_display_dispatch(display->display);
5175 display_exit(display);
5180 if (events & EPOLLOUT) {
5181 ret = wl_display_flush(display->display);
5183 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5184 ep.data.ptr = &display->display_task;
5185 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5186 display->display_fd, &ep);
5187 } else if (ret == -1 && errno != EAGAIN) {
5188 display_exit(display);
5195 log_handler(const char *format, va_list args)
5197 vfprintf(stderr, format, args);
5201 display_create(int *argc, char *argv[])
5205 wl_log_set_handler_client(log_handler);
5207 d = zalloc(sizeof *d);
5211 d->display = wl_display_connect(NULL);
5212 if (d->display == NULL) {
5213 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5218 d->xkb_context = xkb_context_new(0);
5219 if (d->xkb_context == NULL) {
5220 fprintf(stderr, "Failed to create XKB context\n");
5225 d->epoll_fd = os_epoll_create_cloexec();
5226 d->display_fd = wl_display_get_fd(d->display);
5227 d->display_task.run = handle_display_data;
5228 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5231 wl_list_init(&d->deferred_list);
5232 wl_list_init(&d->input_list);
5233 wl_list_init(&d->output_list);
5234 wl_list_init(&d->global_list);
5237 d->workspace_count = 1;
5239 d->registry = wl_display_get_registry(d->display);
5240 wl_registry_add_listener(d->registry, ®istry_listener, d);
5242 if (wl_display_dispatch(d->display) < 0) {
5243 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5247 #ifdef HAVE_CAIRO_EGL
5248 if (init_egl(d) < 0)
5249 fprintf(stderr, "EGL does not seem to work, "
5250 "falling back to software rendering and wl_shm.\n");
5255 d->theme = theme_create();
5257 wl_list_init(&d->window_list);
5259 init_dummy_surface(d);
5265 display_destroy_outputs(struct display *display)
5268 struct output *output;
5270 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5271 output_destroy(output);
5275 display_destroy_inputs(struct display *display)
5278 struct input *input;
5280 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5281 input_destroy(input);
5285 display_destroy(struct display *display)
5287 if (!wl_list_empty(&display->window_list))
5288 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5289 wl_list_length(&display->window_list));
5291 if (!wl_list_empty(&display->deferred_list))
5292 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5294 cairo_surface_destroy(display->dummy_surface);
5295 free(display->dummy_surface_data);
5297 display_destroy_outputs(display);
5298 display_destroy_inputs(display);
5300 xkb_context_unref(display->xkb_context);
5302 theme_destroy(display->theme);
5303 destroy_cursors(display);
5305 #ifdef HAVE_CAIRO_EGL
5306 if (display->argb_device)
5310 if (display->subcompositor)
5311 wl_subcompositor_destroy(display->subcompositor);
5314 wl_shell_destroy(display->shell);
5317 wl_shm_destroy(display->shm);
5319 if (display->data_device_manager)
5320 wl_data_device_manager_destroy(display->data_device_manager);
5322 wl_compositor_destroy(display->compositor);
5323 wl_registry_destroy(display->registry);
5325 close(display->epoll_fd);
5327 if (!(display->display_fd_events & EPOLLERR) &&
5328 !(display->display_fd_events & EPOLLHUP))
5329 wl_display_flush(display->display);
5331 wl_display_disconnect(display->display);
5336 display_set_user_data(struct display *display, void *data)
5338 display->user_data = data;
5342 display_get_user_data(struct display *display)
5344 return display->user_data;
5348 display_get_display(struct display *display)
5350 return display->display;
5354 display_has_subcompositor(struct display *display)
5356 if (display->subcompositor)
5359 wl_display_roundtrip(display->display);
5361 return display->subcompositor != NULL;
5365 display_get_cairo_device(struct display *display)
5367 return display->argb_device;
5371 display_get_output(struct display *display)
5373 return container_of(display->output_list.next, struct output, link);
5376 struct wl_compositor *
5377 display_get_compositor(struct display *display)
5379 return display->compositor;
5383 display_get_serial(struct display *display)
5385 return display->serial;
5389 display_get_egl_display(struct display *d)
5394 struct wl_data_source *
5395 display_create_data_source(struct display *display)
5397 return wl_data_device_manager_create_data_source(display->data_device_manager);
5401 display_get_argb_egl_config(struct display *d)
5403 return d->argb_config;
5407 display_acquire_window_surface(struct display *display,
5408 struct window *window,
5411 struct surface *surface = window->main_surface;
5413 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5416 widget_get_cairo_surface(window->main_surface->widget);
5417 return surface->toysurface->acquire(surface->toysurface, ctx);
5421 display_release_window_surface(struct display *display,
5422 struct window *window)
5424 struct surface *surface = window->main_surface;
5426 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5429 surface->toysurface->release(surface->toysurface);
5433 display_defer(struct display *display, struct task *task)
5435 wl_list_insert(&display->deferred_list, &task->link);
5439 display_watch_fd(struct display *display,
5440 int fd, uint32_t events, struct task *task)
5442 struct epoll_event ep;
5446 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5450 display_unwatch_fd(struct display *display, int fd)
5452 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5456 display_run(struct display *display)
5459 struct epoll_event ep[16];
5462 display->running = 1;
5464 while (!wl_list_empty(&display->deferred_list)) {
5465 task = container_of(display->deferred_list.prev,
5467 wl_list_remove(&task->link);
5471 wl_display_dispatch_pending(display->display);
5473 if (!display->running)
5476 ret = wl_display_flush(display->display);
5477 if (ret < 0 && errno == EAGAIN) {
5479 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5480 ep[0].data.ptr = &display->display_task;
5482 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5483 display->display_fd, &ep[0]);
5484 } else if (ret < 0) {
5488 count = epoll_wait(display->epoll_fd,
5489 ep, ARRAY_LENGTH(ep), -1);
5490 for (i = 0; i < count; i++) {
5491 task = ep[i].data.ptr;
5492 task->run(task, ep[i].events);
5498 display_exit(struct display *display)
5500 display->running = 0;
5504 keysym_modifiers_add(struct wl_array *modifiers_map,
5507 size_t len = strlen(name) + 1;
5510 p = wl_array_add(modifiers_map, len);
5515 strncpy(p, name, len);
5518 static xkb_mod_index_t
5519 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5522 xkb_mod_index_t index = 0;
5523 char *p = modifiers_map->data;
5525 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5526 if (strcmp(p, name) == 0)
5533 return XKB_MOD_INVALID;
5537 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5540 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5542 if (index == XKB_MOD_INVALID)
5543 return XKB_MOD_INVALID;
5549 fail_on_null(void *p)
5552 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5562 return fail_on_null(malloc(s));
5568 return fail_on_null(zalloc(s));
5572 xstrdup(const char *s)
5574 return fail_on_null(strdup(s));
5578 xrealloc(char *p, size_t s)
5580 return fail_on_null(realloc(p, s));