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;
2938 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2939 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2940 window_set_maximized(window,
2941 window->type != TYPE_MAXIMIZED);
2942 } else if (sym == XKB_KEY_F11 &&
2943 window->fullscreen_handler &&
2944 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2945 window->fullscreen_handler(window, window->user_data);
2946 } else if (sym == XKB_KEY_F4 &&
2947 input->modifiers == MOD_ALT_MASK &&
2948 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2949 if (window->close_handler)
2950 window->close_handler(window->user_data);
2952 display_exit(window->display);
2953 } else if (window->key_handler) {
2954 (*window->key_handler)(window, input, time, key,
2955 sym, state, window->user_data);
2958 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2959 key == input->repeat_key) {
2960 its.it_interval.tv_sec = 0;
2961 its.it_interval.tv_nsec = 0;
2962 its.it_value.tv_sec = 0;
2963 its.it_value.tv_nsec = 0;
2964 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2965 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2966 input->repeat_sym = sym;
2967 input->repeat_key = key;
2968 input->repeat_time = time;
2969 its.it_interval.tv_sec = 0;
2970 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2971 its.it_value.tv_sec = 0;
2972 its.it_value.tv_nsec = 400 * 1000 * 1000;
2973 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2978 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2979 uint32_t serial, uint32_t mods_depressed,
2980 uint32_t mods_latched, uint32_t mods_locked,
2983 struct input *input = data;
2984 xkb_mod_mask_t mask;
2986 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2987 if (!input->xkb.keymap)
2990 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2991 mods_locked, 0, 0, group);
2992 mask = xkb_state_serialize_mods(input->xkb.state,
2993 XKB_STATE_DEPRESSED |
2995 input->modifiers = 0;
2996 if (mask & input->xkb.control_mask)
2997 input->modifiers |= MOD_CONTROL_MASK;
2998 if (mask & input->xkb.alt_mask)
2999 input->modifiers |= MOD_ALT_MASK;
3000 if (mask & input->xkb.shift_mask)
3001 input->modifiers |= MOD_SHIFT_MASK;
3004 static const struct wl_keyboard_listener keyboard_listener = {
3005 keyboard_handle_keymap,
3006 keyboard_handle_enter,
3007 keyboard_handle_leave,
3008 keyboard_handle_key,
3009 keyboard_handle_modifiers,
3013 touch_handle_down(void *data, struct wl_touch *wl_touch,
3014 uint32_t serial, uint32_t time, struct wl_surface *surface,
3015 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3017 struct input *input = data;
3018 struct widget *widget;
3019 float sx = wl_fixed_to_double(x_w);
3020 float sy = wl_fixed_to_double(y_w);
3022 input->display->serial = serial;
3023 input->touch_focus = wl_surface_get_user_data(surface);
3024 if (!input->touch_focus) {
3025 DBG("Failed to find to touch focus for surface %p\n", surface);
3029 widget = window_find_widget(input->touch_focus,
3030 wl_fixed_to_double(x_w),
3031 wl_fixed_to_double(y_w));
3033 struct touch_point *tp = xmalloc(sizeof *tp);
3036 tp->widget = widget;
3039 wl_list_insert(&input->touch_point_list, &tp->link);
3041 if (widget->touch_down_handler)
3042 (*widget->touch_down_handler)(widget, input,
3051 touch_handle_up(void *data, struct wl_touch *wl_touch,
3052 uint32_t serial, uint32_t time, int32_t id)
3054 struct input *input = data;
3055 struct touch_point *tp, *tmp;
3057 if (!input->touch_focus) {
3058 DBG("No touch focus found for touch up event!\n");
3062 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3066 if (tp->widget->touch_up_handler)
3067 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3069 tp->widget->user_data);
3071 wl_list_remove(&tp->link);
3079 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3080 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3082 struct input *input = data;
3083 struct touch_point *tp;
3084 float sx = wl_fixed_to_double(x_w);
3085 float sy = wl_fixed_to_double(y_w);
3087 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3089 if (!input->touch_focus) {
3090 DBG("No touch focus found for touch motion event!\n");
3094 wl_list_for_each(tp, &input->touch_point_list, link) {
3100 if (tp->widget->touch_motion_handler)
3101 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3103 tp->widget->user_data);
3109 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3111 struct input *input = data;
3112 struct touch_point *tp, *tmp;
3114 DBG("touch_handle_frame\n");
3116 if (!input->touch_focus) {
3117 DBG("No touch focus found for touch frame event!\n");
3121 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3122 if (tp->widget->touch_frame_handler)
3123 (*tp->widget->touch_frame_handler)(tp->widget, input,
3124 tp->widget->user_data);
3126 wl_list_remove(&tp->link);
3132 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3134 struct input *input = data;
3135 struct touch_point *tp, *tmp;
3137 DBG("touch_handle_cancel\n");
3139 if (!input->touch_focus) {
3140 DBG("No touch focus found for touch cancel event!\n");
3144 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3145 if (tp->widget->touch_cancel_handler)
3146 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3147 tp->widget->user_data);
3149 wl_list_remove(&tp->link);
3154 static const struct wl_touch_listener touch_listener = {
3157 touch_handle_motion,
3159 touch_handle_cancel,
3163 seat_handle_capabilities(void *data, struct wl_seat *seat,
3164 enum wl_seat_capability caps)
3166 struct input *input = data;
3168 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3169 input->pointer = wl_seat_get_pointer(seat);
3170 wl_pointer_set_user_data(input->pointer, input);
3171 wl_pointer_add_listener(input->pointer, &pointer_listener,
3173 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3174 wl_pointer_destroy(input->pointer);
3175 input->pointer = NULL;
3178 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3179 input->keyboard = wl_seat_get_keyboard(seat);
3180 wl_keyboard_set_user_data(input->keyboard, input);
3181 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3183 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3184 wl_keyboard_destroy(input->keyboard);
3185 input->keyboard = NULL;
3188 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3189 input->touch = wl_seat_get_touch(seat);
3190 wl_touch_set_user_data(input->touch, input);
3191 wl_touch_add_listener(input->touch, &touch_listener, input);
3192 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3193 wl_touch_destroy(input->touch);
3194 input->touch = NULL;
3199 seat_handle_name(void *data, struct wl_seat *seat,
3205 static const struct wl_seat_listener seat_listener = {
3206 seat_handle_capabilities,
3211 input_get_position(struct input *input, int32_t *x, int32_t *y)
3218 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3220 struct touch_point *tp;
3222 wl_list_for_each(tp, &input->touch_point_list, link) {
3235 input_get_display(struct input *input)
3237 return input->display;
3241 input_get_seat(struct input *input)
3247 input_get_modifiers(struct input *input)
3249 return input->modifiers;
3253 input_get_focus_widget(struct input *input)
3255 return input->focus_widget;
3259 struct wl_data_offer *offer;
3260 struct input *input;
3261 struct wl_array types;
3264 struct task io_task;
3272 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3274 struct data_offer *offer = data;
3277 p = wl_array_add(&offer->types, sizeof *p);
3281 static const struct wl_data_offer_listener data_offer_listener = {
3286 data_offer_destroy(struct data_offer *offer)
3291 if (offer->refcount == 0) {
3292 wl_data_offer_destroy(offer->offer);
3293 for (p = offer->types.data; *p; p++)
3295 wl_array_release(&offer->types);
3301 data_device_data_offer(void *data,
3302 struct wl_data_device *data_device,
3303 struct wl_data_offer *_offer)
3305 struct data_offer *offer;
3307 offer = xmalloc(sizeof *offer);
3309 wl_array_init(&offer->types);
3310 offer->refcount = 1;
3311 offer->input = data;
3312 offer->offer = _offer;
3313 wl_data_offer_add_listener(offer->offer,
3314 &data_offer_listener, offer);
3318 data_device_enter(void *data, struct wl_data_device *data_device,
3319 uint32_t serial, struct wl_surface *surface,
3320 wl_fixed_t x_w, wl_fixed_t y_w,
3321 struct wl_data_offer *offer)
3323 struct input *input = data;
3324 struct window *window;
3326 float x = wl_fixed_to_double(x_w);
3327 float y = wl_fixed_to_double(y_w);
3330 window = wl_surface_get_user_data(surface);
3331 input->drag_enter_serial = serial;
3332 input->drag_focus = window,
3336 if (!input->touch_grab)
3337 input->pointer_enter_serial = serial;
3340 input->drag_offer = wl_data_offer_get_user_data(offer);
3342 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3345 types_data = input->drag_offer->types.data;
3347 input->drag_offer = NULL;
3351 if (window->data_handler)
3352 window->data_handler(window, input, x, y, types_data,
3357 data_device_leave(void *data, struct wl_data_device *data_device)
3359 struct input *input = data;
3361 if (input->drag_offer) {
3362 data_offer_destroy(input->drag_offer);
3363 input->drag_offer = NULL;
3368 data_device_motion(void *data, struct wl_data_device *data_device,
3369 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3371 struct input *input = data;
3372 struct window *window = input->drag_focus;
3373 float x = wl_fixed_to_double(x_w);
3374 float y = wl_fixed_to_double(y_w);
3380 if (input->drag_offer)
3381 types_data = input->drag_offer->types.data;
3385 if (window->data_handler)
3386 window->data_handler(window, input, x, y, types_data,
3391 data_device_drop(void *data, struct wl_data_device *data_device)
3393 struct input *input = data;
3394 struct window *window = input->drag_focus;
3400 if (window->drop_handler)
3401 window->drop_handler(window, input,
3402 x, y, window->user_data);
3404 if (input->touch_grab)
3405 touch_ungrab(input);
3409 data_device_selection(void *data,
3410 struct wl_data_device *wl_data_device,
3411 struct wl_data_offer *offer)
3413 struct input *input = data;
3416 if (input->selection_offer)
3417 data_offer_destroy(input->selection_offer);
3420 input->selection_offer = wl_data_offer_get_user_data(offer);
3421 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3424 input->selection_offer = NULL;
3428 static const struct wl_data_device_listener data_device_listener = {
3429 data_device_data_offer,
3434 data_device_selection
3438 input_set_pointer_image_index(struct input *input, int index)
3440 struct wl_buffer *buffer;
3441 struct wl_cursor *cursor;
3442 struct wl_cursor_image *image;
3444 if (!input->pointer)
3447 cursor = input->display->cursors[input->current_cursor];
3451 if (index >= (int) cursor->image_count) {
3452 fprintf(stderr, "cursor index out of range\n");
3456 image = cursor->images[index];
3457 buffer = wl_cursor_image_get_buffer(image);
3461 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3462 wl_surface_damage(input->pointer_surface, 0, 0,
3463 image->width, image->height);
3464 wl_surface_commit(input->pointer_surface);
3465 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3466 input->pointer_surface,
3467 image->hotspot_x, image->hotspot_y);
3470 static const struct wl_callback_listener pointer_surface_listener;
3473 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3476 struct input *input = data;
3477 struct wl_cursor *cursor;
3481 assert(callback == input->cursor_frame_cb);
3482 wl_callback_destroy(callback);
3483 input->cursor_frame_cb = NULL;
3486 if (!input->pointer)
3489 if (input->current_cursor == CURSOR_BLANK) {
3490 wl_pointer_set_cursor(input->pointer,
3491 input->pointer_enter_serial,
3496 if (input->current_cursor == CURSOR_UNSET)
3498 cursor = input->display->cursors[input->current_cursor];
3502 /* FIXME We don't have the current time on the first call so we set
3503 * the animation start to the time of the first frame callback. */
3505 input->cursor_anim_start = 0;
3506 else if (input->cursor_anim_start == 0)
3507 input->cursor_anim_start = time;
3509 if (time == 0 || input->cursor_anim_start == 0)
3512 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3514 if (cursor->image_count > 1) {
3515 input->cursor_frame_cb =
3516 wl_surface_frame(input->pointer_surface);
3517 wl_callback_add_listener(input->cursor_frame_cb,
3518 &pointer_surface_listener, input);
3521 input_set_pointer_image_index(input, i);
3524 static const struct wl_callback_listener pointer_surface_listener = {
3525 pointer_surface_frame_callback
3529 input_set_pointer_image(struct input *input, int pointer)
3533 if (!input->pointer)
3536 if (input->pointer_enter_serial > input->cursor_serial)
3539 if (!force && pointer == input->current_cursor)
3542 input->current_cursor = pointer;
3543 input->cursor_serial = input->pointer_enter_serial;
3544 if (!input->cursor_frame_cb)
3545 pointer_surface_frame_callback(input, NULL, 0);
3547 /* The current frame callback may be stuck if, for instance,
3548 * the set cursor request was processed by the server after
3549 * this client lost the focus. In this case the cursor surface
3550 * might not be mapped and the frame callback wouldn't ever
3551 * complete. Send a set_cursor and attach to try to map the
3552 * cursor surface again so that the callback will finish */
3553 input_set_pointer_image_index(input, 0);
3557 struct wl_data_device *
3558 input_get_data_device(struct input *input)
3560 return input->data_device;
3564 input_set_selection(struct input *input,
3565 struct wl_data_source *source, uint32_t time)
3567 wl_data_device_set_selection(input->data_device, source, time);
3571 input_accept(struct input *input, const char *type)
3573 wl_data_offer_accept(input->drag_offer->offer,
3574 input->drag_enter_serial, type);
3578 offer_io_func(struct task *task, uint32_t events)
3580 struct data_offer *offer =
3581 container_of(task, struct data_offer, io_task);
3585 len = read(offer->fd, buffer, sizeof buffer);
3586 offer->func(buffer, len,
3587 offer->x, offer->y, offer->user_data);
3591 data_offer_destroy(offer);
3596 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3597 data_func_t func, void *user_data)
3601 if (pipe2(p, O_CLOEXEC) == -1)
3604 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3607 offer->io_task.run = offer_io_func;
3611 offer->user_data = user_data;
3613 display_watch_fd(offer->input->display,
3614 offer->fd, EPOLLIN, &offer->io_task);
3618 input_receive_drag_data(struct input *input, const char *mime_type,
3619 data_func_t func, void *data)
3621 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3622 input->drag_offer->x = input->drag_x;
3623 input->drag_offer->y = input->drag_y;
3627 input_receive_drag_data_to_fd(struct input *input,
3628 const char *mime_type, int fd)
3630 if (input->drag_offer)
3631 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3637 input_receive_selection_data(struct input *input, const char *mime_type,
3638 data_func_t func, void *data)
3642 if (input->selection_offer == NULL)
3645 for (p = input->selection_offer->types.data; *p; p++)
3646 if (strcmp(mime_type, *p) == 0)
3652 data_offer_receive_data(input->selection_offer,
3653 mime_type, func, data);
3658 input_receive_selection_data_to_fd(struct input *input,
3659 const char *mime_type, int fd)
3661 if (input->selection_offer)
3662 wl_data_offer_receive(input->selection_offer->offer,
3669 window_move(struct window *window, struct input *input, uint32_t serial)
3671 if (!window->shell_surface)
3674 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3678 surface_set_synchronized(struct surface *surface)
3680 if (!surface->subsurface)
3683 if (surface->synchronized)
3686 wl_subsurface_set_sync(surface->subsurface);
3687 surface->synchronized = 1;
3691 surface_set_synchronized_default(struct surface *surface)
3693 if (!surface->subsurface)
3696 if (surface->synchronized == surface->synchronized_default)
3699 if (surface->synchronized_default)
3700 wl_subsurface_set_sync(surface->subsurface);
3702 wl_subsurface_set_desync(surface->subsurface);
3704 surface->synchronized = surface->synchronized_default;
3708 surface_resize(struct surface *surface)
3710 struct widget *widget = surface->widget;
3711 struct wl_compositor *compositor = widget->window->display->compositor;
3713 if (surface->input_region) {
3714 wl_region_destroy(surface->input_region);
3715 surface->input_region = NULL;
3718 if (surface->opaque_region)
3719 wl_region_destroy(surface->opaque_region);
3721 surface->opaque_region = wl_compositor_create_region(compositor);
3723 if (widget->resize_handler)
3724 widget->resize_handler(widget,
3725 widget->allocation.width,
3726 widget->allocation.height,
3729 if (surface->subsurface &&
3730 (surface->allocation.x != widget->allocation.x ||
3731 surface->allocation.y != widget->allocation.y)) {
3732 wl_subsurface_set_position(surface->subsurface,
3733 widget->allocation.x,
3734 widget->allocation.y);
3736 if (surface->allocation.width != widget->allocation.width ||
3737 surface->allocation.height != widget->allocation.height) {
3738 window_schedule_redraw(widget->window);
3740 surface->allocation = widget->allocation;
3743 wl_region_add(surface->opaque_region, 0, 0,
3744 widget->allocation.width,
3745 widget->allocation.height);
3749 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3752 * This hack should be removed, when EGL respects
3753 * eglSwapInterval(0).
3755 * If this window has sub-surfaces, especially a free-running
3756 * EGL-widget, we need to post the parent surface once with
3757 * all the old state to guarantee, that the EGL-widget will
3758 * receive its frame callback soon. Otherwise, a forced call
3759 * to eglSwapBuffers may end up blocking, waiting for a frame
3760 * event that will never come, because we will commit the parent
3761 * surface with all new state only after eglSwapBuffers returns.
3763 * This assumes, that:
3764 * 1. When the EGL widget's resize hook is called, it pauses.
3765 * 2. When the EGL widget's redraw hook is called, it forces a
3766 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3767 * In a single threaded application condition 1 is a no-op.
3769 * XXX: This should actually be after the surface_resize() calls,
3770 * but cannot, because then it would commit the incomplete state
3771 * accumulated from the widget resize hooks.
3773 if (window->subsurface_list.next != &window->main_surface->link ||
3774 window->subsurface_list.prev != &window->main_surface->link)
3775 wl_surface_commit(window->main_surface->surface);
3779 window_do_resize(struct window *window)
3781 struct surface *surface;
3783 widget_set_allocation(window->main_surface->widget,
3784 window->pending_allocation.x,
3785 window->pending_allocation.y,
3786 window->pending_allocation.width,
3787 window->pending_allocation.height);
3789 surface_resize(window->main_surface);
3791 /* The main surface is in the list, too. Main surface's
3792 * resize_handler is responsible for calling widget_set_allocation()
3793 * on all sub-surface root widgets, so they will be resized
3796 wl_list_for_each(surface, &window->subsurface_list, link) {
3797 if (surface == window->main_surface)
3800 surface_set_synchronized(surface);
3801 surface_resize(surface);
3806 idle_resize(struct window *window)
3808 window->resize_needed = 0;
3809 window->redraw_needed = 1;
3811 DBG("from %dx%d to %dx%d\n",
3812 window->main_surface->server_allocation.width,
3813 window->main_surface->server_allocation.height,
3814 window->pending_allocation.width,
3815 window->pending_allocation.height);
3817 hack_prevent_EGL_sub_surface_deadlock(window);
3819 window_do_resize(window);
3823 undo_resize(struct window *window)
3825 window->pending_allocation.width =
3826 window->main_surface->server_allocation.width;
3827 window->pending_allocation.height =
3828 window->main_surface->server_allocation.height;
3830 DBG("back to %dx%d\n",
3831 window->main_surface->server_allocation.width,
3832 window->main_surface->server_allocation.height);
3834 window_do_resize(window);
3836 if (window->pending_allocation.width == 0 &&
3837 window->pending_allocation.height == 0) {
3838 fprintf(stderr, "Error: Could not draw a surface, "
3839 "most likely due to insufficient disk space in "
3840 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3846 window_schedule_resize(struct window *window, int width, int height)
3848 /* We should probably get these numbers from the theme. */
3849 const int min_width = 200, min_height = 200;
3851 window->pending_allocation.x = 0;
3852 window->pending_allocation.y = 0;
3853 window->pending_allocation.width = width;
3854 window->pending_allocation.height = height;
3856 if (window->min_allocation.width == 0) {
3857 if (width < min_width && window->frame)
3858 window->min_allocation.width = min_width;
3860 window->min_allocation.width = width;
3861 if (height < min_height && window->frame)
3862 window->min_allocation.height = min_height;
3864 window->min_allocation.height = height;
3867 if (window->pending_allocation.width < window->min_allocation.width)
3868 window->pending_allocation.width = window->min_allocation.width;
3869 if (window->pending_allocation.height < window->min_allocation.height)
3870 window->pending_allocation.height = window->min_allocation.height;
3872 window->resize_needed = 1;
3873 window_schedule_redraw(window);
3877 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3879 window_schedule_resize(widget->window, width, height);
3883 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3886 wl_shell_surface_pong(shell_surface, serial);
3890 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3891 uint32_t edges, int32_t width, int32_t height)
3893 struct window *window = data;
3895 window->resize_edges = edges;
3896 window_schedule_resize(window, width, height);
3900 menu_destroy(struct menu *menu)
3902 widget_destroy(menu->widget);
3903 window_destroy(menu->window);
3904 frame_destroy(menu->frame);
3909 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3911 struct window *window = data;
3912 struct menu *menu = window->main_surface->widget->user_data;
3914 /* FIXME: Need more context in this event, at least the input
3915 * device. Or just use wl_callback. And this really needs to
3916 * be a window vfunc that the menu can set. And we need the
3919 input_ungrab(menu->input);
3923 static const struct wl_shell_surface_listener shell_surface_listener = {
3930 window_get_allocation(struct window *window,
3931 struct rectangle *allocation)
3933 *allocation = window->main_surface->allocation;
3937 widget_redraw(struct widget *widget)
3939 struct widget *child;
3941 if (widget->redraw_handler)
3942 widget->redraw_handler(widget, widget->user_data);
3943 wl_list_for_each(child, &widget->child_list, link)
3944 widget_redraw(child);
3948 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3950 struct surface *surface = data;
3952 assert(callback == surface->frame_cb);
3953 DBG_OBJ(callback, "done\n");
3954 wl_callback_destroy(callback);
3955 surface->frame_cb = NULL;
3957 surface->last_time = time;
3959 if (surface->redraw_needed || surface->window->redraw_needed) {
3960 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3961 window_schedule_redraw_task(surface->window);
3965 static const struct wl_callback_listener listener = {
3970 surface_redraw(struct surface *surface)
3972 DBG_OBJ(surface->surface, "begin\n");
3974 if (!surface->window->redraw_needed && !surface->redraw_needed)
3977 /* Whole-window redraw forces a redraw even if the previous has
3978 * not yet hit the screen.
3980 if (surface->frame_cb) {
3981 if (!surface->window->redraw_needed)
3984 DBG_OBJ(surface->frame_cb, "cancelled\n");
3985 wl_callback_destroy(surface->frame_cb);
3988 if (surface->widget->use_cairo &&
3989 !widget_get_cairo_surface(surface->widget)) {
3990 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3994 surface->frame_cb = wl_surface_frame(surface->surface);
3995 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3996 DBG_OBJ(surface->frame_cb, "new\n");
3998 surface->redraw_needed = 0;
3999 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4000 widget_redraw(surface->widget);
4001 DBG_OBJ(surface->surface, "done\n");
4006 idle_redraw(struct task *task, uint32_t events)
4008 struct window *window = container_of(task, struct window, redraw_task);
4009 struct surface *surface;
4013 DBG(" --------- \n");
4015 wl_list_init(&window->redraw_task.link);
4016 window->redraw_task_scheduled = 0;
4018 if (window->resize_needed) {
4019 /* throttle resizing to the main surface display */
4020 if (window->main_surface->frame_cb) {
4021 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4025 idle_resize(window);
4029 if (surface_redraw(window->main_surface) < 0) {
4031 * Only main_surface failure will cause us to undo the resize.
4032 * If sub-surfaces fail, they will just be broken with old
4037 wl_list_for_each(surface, &window->subsurface_list, link) {
4038 if (surface == window->main_surface)
4041 surface_redraw(surface);
4045 window->redraw_needed = 0;
4046 window_flush(window);
4048 wl_list_for_each(surface, &window->subsurface_list, link)
4049 surface_set_synchronized_default(surface);
4051 if (resized && failed) {
4052 /* Restore widget tree to correspond to what is on screen. */
4053 undo_resize(window);
4058 window_schedule_redraw_task(struct window *window)
4060 if (window->configure_requests)
4062 if (!window->redraw_task_scheduled) {
4063 window->redraw_task.run = idle_redraw;
4064 display_defer(window->display, &window->redraw_task);
4065 window->redraw_task_scheduled = 1;
4070 window_schedule_redraw(struct window *window)
4072 struct surface *surface;
4074 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4076 wl_list_for_each(surface, &window->subsurface_list, link)
4077 surface->redraw_needed = 1;
4079 window_schedule_redraw_task(window);
4083 window_is_fullscreen(struct window *window)
4085 return window->type == TYPE_FULLSCREEN;
4089 window_is_transient(struct window *window)
4091 return window->type == TYPE_TRANSIENT;
4095 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4097 struct window *window = data;
4099 wl_callback_destroy(callback);
4100 window->configure_requests--;
4102 if (!window->configure_requests)
4103 window_schedule_redraw(window);
4106 static struct wl_callback_listener configure_request_listener = {
4107 configure_request_completed,
4111 window_defer_redraw_until_configure(struct window* window)
4113 struct wl_callback *callback;
4115 if (window->redraw_task_scheduled) {
4116 wl_list_remove(&window->redraw_task.link);
4117 window->redraw_task_scheduled = 0;
4120 callback = wl_display_sync(window->display->display);
4121 wl_callback_add_listener(callback, &configure_request_listener, window);
4122 window->configure_requests++;
4126 window_set_fullscreen(struct window *window, int fullscreen)
4128 if (!window->display->shell)
4131 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4135 window->saved_type = window->type;
4136 if (window->type == TYPE_TOPLEVEL) {
4137 window->saved_allocation = window->main_surface->allocation;
4139 window->type = TYPE_FULLSCREEN;
4140 wl_shell_surface_set_fullscreen(window->shell_surface,
4141 window->fullscreen_method,
4143 window_defer_redraw_until_configure (window);
4145 if (window->saved_type == TYPE_MAXIMIZED) {
4146 window_set_maximized(window, 1);
4148 window->type = TYPE_TOPLEVEL;
4149 wl_shell_surface_set_toplevel(window->shell_surface);
4150 window_schedule_resize(window,
4151 window->saved_allocation.width,
4152 window->saved_allocation.height);
4159 window_set_fullscreen_method(struct window *window,
4160 enum wl_shell_surface_fullscreen_method method)
4162 window->fullscreen_method = method;
4166 window_is_maximized(struct window *window)
4168 return window->type == TYPE_MAXIMIZED;
4172 window_set_maximized(struct window *window, int maximized)
4174 if (!window->display->shell)
4177 if ((window->type == TYPE_MAXIMIZED) == maximized)
4180 if (window->type == TYPE_TOPLEVEL) {
4181 window->saved_allocation = window->main_surface->allocation;
4182 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4183 window->type = TYPE_MAXIMIZED;
4184 window_defer_redraw_until_configure(window);
4185 } else if (window->type == TYPE_FULLSCREEN) {
4186 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4187 window->type = TYPE_MAXIMIZED;
4188 window_defer_redraw_until_configure(window);
4190 wl_shell_surface_set_toplevel(window->shell_surface);
4191 window->type = TYPE_TOPLEVEL;
4192 window_schedule_resize(window,
4193 window->saved_allocation.width,
4194 window->saved_allocation.height);
4199 window_set_user_data(struct window *window, void *data)
4201 window->user_data = data;
4205 window_get_user_data(struct window *window)
4207 return window->user_data;
4211 window_set_key_handler(struct window *window,
4212 window_key_handler_t handler)
4214 window->key_handler = handler;
4218 window_set_keyboard_focus_handler(struct window *window,
4219 window_keyboard_focus_handler_t handler)
4221 window->keyboard_focus_handler = handler;
4225 window_set_data_handler(struct window *window, window_data_handler_t handler)
4227 window->data_handler = handler;
4231 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4233 window->drop_handler = handler;
4237 window_set_close_handler(struct window *window,
4238 window_close_handler_t handler)
4240 window->close_handler = handler;
4244 window_set_fullscreen_handler(struct window *window,
4245 window_fullscreen_handler_t handler)
4247 window->fullscreen_handler = handler;
4251 window_set_output_handler(struct window *window,
4252 window_output_handler_t handler)
4254 window->output_handler = handler;
4258 window_set_title(struct window *window, const char *title)
4260 free(window->title);
4261 window->title = strdup(title);
4262 if (window->frame) {
4263 frame_set_title(window->frame->frame, title);
4264 widget_schedule_redraw(window->frame->widget);
4266 if (window->shell_surface)
4267 wl_shell_surface_set_title(window->shell_surface, title);
4271 window_get_title(struct window *window)
4273 return window->title;
4277 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4279 struct text_cursor_position *text_cursor_position =
4280 window->display->text_cursor_position;
4282 if (!text_cursor_position)
4285 text_cursor_position_notify(text_cursor_position,
4286 window->main_surface->surface,
4287 wl_fixed_from_int(x),
4288 wl_fixed_from_int(y));
4292 window_damage(struct window *window, int32_t x, int32_t y,
4293 int32_t width, int32_t height)
4295 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4299 surface_enter(void *data,
4300 struct wl_surface *wl_surface, struct wl_output *wl_output)
4302 struct window *window = data;
4303 struct output *output;
4304 struct output *output_found = NULL;
4305 struct window_output *window_output;
4307 wl_list_for_each(output, &window->display->output_list, link) {
4308 if (output->output == wl_output) {
4309 output_found = output;
4317 window_output = xmalloc(sizeof *window_output);
4318 window_output->output = output_found;
4320 wl_list_insert (&window->window_output_list, &window_output->link);
4322 if (window->output_handler)
4323 window->output_handler(window, output_found, 1,
4328 surface_leave(void *data,
4329 struct wl_surface *wl_surface, struct wl_output *output)
4331 struct window *window = data;
4332 struct window_output *window_output;
4333 struct window_output *window_output_found = NULL;
4335 wl_list_for_each(window_output, &window->window_output_list, link) {
4336 if (window_output->output->output == output) {
4337 window_output_found = window_output;
4342 if (window_output_found) {
4343 wl_list_remove(&window_output_found->link);
4345 if (window->output_handler)
4346 window->output_handler(window, window_output->output,
4347 0, window->user_data);
4349 free(window_output_found);
4353 static const struct wl_surface_listener surface_listener = {
4358 static struct surface *
4359 surface_create(struct window *window)
4361 struct display *display = window->display;
4362 struct surface *surface;
4364 surface = xmalloc(sizeof *surface);
4365 memset(surface, 0, sizeof *surface);
4369 surface->window = window;
4370 surface->surface = wl_compositor_create_surface(display->compositor);
4371 surface->buffer_scale = 1;
4372 wl_surface_add_listener(surface->surface, &surface_listener, window);
4374 wl_list_insert(&window->subsurface_list, &surface->link);
4379 static struct window *
4380 window_create_internal(struct display *display, int type)
4382 struct window *window;
4383 struct surface *surface;
4385 window = xzalloc(sizeof *window);
4386 wl_list_init(&window->subsurface_list);
4387 window->display = display;
4389 surface = surface_create(window);
4390 window->main_surface = surface;
4392 if (type != TYPE_CUSTOM && display->shell) {
4393 window->shell_surface =
4394 wl_shell_get_shell_surface(display->shell,
4396 fail_on_null(window->shell_surface);
4399 window->type = type;
4400 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4401 window->configure_requests = 0;
4402 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4404 if (display->argb_device)
4405 #ifdef HAVE_CAIRO_EGL
4406 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4408 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4411 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4413 wl_surface_set_user_data(surface->surface, window);
4414 wl_list_insert(display->window_list.prev, &window->link);
4415 wl_list_init(&window->redraw_task.link);
4417 if (window->shell_surface) {
4418 wl_shell_surface_set_user_data(window->shell_surface, window);
4419 wl_shell_surface_add_listener(window->shell_surface,
4420 &shell_surface_listener, window);
4423 wl_list_init (&window->window_output_list);
4429 window_create(struct display *display)
4431 return window_create_internal(display, TYPE_NONE);
4435 window_create_custom(struct display *display)
4437 return window_create_internal(display, TYPE_CUSTOM);
4441 window_create_transient(struct display *display, struct window *parent,
4442 int32_t x, int32_t y, uint32_t flags)
4444 struct window *window;
4446 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4452 wl_shell_surface_set_transient(
4453 window->shell_surface,
4454 parent->main_surface->surface,
4455 window->x, window->y, flags);
4461 menu_set_item(struct menu *menu, int sy)
4463 int32_t x, y, width, height;
4466 frame_interior(menu->frame, &x, &y, &width, &height);
4467 next = (sy - y) / 20;
4468 if (menu->current != next) {
4469 menu->current = next;
4470 widget_schedule_redraw(menu->widget);
4475 menu_motion_handler(struct widget *widget,
4476 struct input *input, uint32_t time,
4477 float x, float y, void *data)
4479 struct menu *menu = data;
4481 if (widget == menu->widget)
4482 menu_set_item(data, y);
4484 return CURSOR_LEFT_PTR;
4488 menu_enter_handler(struct widget *widget,
4489 struct input *input, float x, float y, void *data)
4491 struct menu *menu = data;
4493 if (widget == menu->widget)
4494 menu_set_item(data, y);
4496 return CURSOR_LEFT_PTR;
4500 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4502 struct menu *menu = data;
4504 if (widget == menu->widget)
4505 menu_set_item(data, -200);
4509 menu_button_handler(struct widget *widget,
4510 struct input *input, uint32_t time,
4511 uint32_t button, enum wl_pointer_button_state state,
4515 struct menu *menu = data;
4517 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4518 (menu->release_count > 0 || time - menu->time > 500)) {
4519 /* Either relase after press-drag-release or
4520 * click-motion-click. */
4521 menu->func(menu->parent, input,
4522 menu->current, menu->parent->user_data);
4523 input_ungrab(input);
4525 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4526 menu->release_count++;
4531 menu_redraw_handler(struct widget *widget, void *data)
4534 struct menu *menu = data;
4535 int32_t x, y, width, height, i;
4537 cr = widget_cairo_create(widget);
4539 frame_repaint(menu->frame, cr);
4540 frame_interior(menu->frame, &x, &y, &width, &height);
4542 theme_set_background_source(menu->window->display->theme,
4543 cr, THEME_FRAME_ACTIVE);
4544 cairo_rectangle(cr, x, y, width, height);
4547 cairo_select_font_face(cr, "sans",
4548 CAIRO_FONT_SLANT_NORMAL,
4549 CAIRO_FONT_WEIGHT_NORMAL);
4550 cairo_set_font_size(cr, 12);
4552 for (i = 0; i < menu->count; i++) {
4553 if (i == menu->current) {
4554 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4555 cairo_rectangle(cr, x, y + i * 20, width, 20);
4557 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4558 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4559 cairo_show_text(cr, menu->entries[i]);
4561 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4562 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4563 cairo_show_text(cr, menu->entries[i]);
4571 window_show_menu(struct display *display,
4572 struct input *input, uint32_t time, struct window *parent,
4573 int32_t x, int32_t y,
4574 menu_func_t func, const char **entries, int count)
4576 struct window *window;
4580 menu = malloc(sizeof *menu);
4584 window = window_create_internal(parent->display, TYPE_MENU);
4590 menu->window = window;
4591 menu->parent = parent;
4592 menu->widget = window_add_widget(menu->window, menu);
4593 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4594 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4595 menu->frame = frame_create(window->display->theme, 0, 0,
4596 FRAME_BUTTON_NONE, NULL);
4597 fail_on_null(menu->frame);
4598 menu->entries = entries;
4599 menu->count = count;
4600 menu->release_count = 0;
4604 menu->input = input;
4605 window->type = TYPE_MENU;
4609 input_ungrab(input);
4611 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4612 widget_set_enter_handler(menu->widget, menu_enter_handler);
4613 widget_set_leave_handler(menu->widget, menu_leave_handler);
4614 widget_set_motion_handler(menu->widget, menu_motion_handler);
4615 widget_set_button_handler(menu->widget, menu_button_handler);
4617 input_grab(input, menu->widget, 0);
4618 frame_resize_inside(menu->frame, 200, count * 20);
4619 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4620 window_schedule_resize(window, frame_width(menu->frame),
4621 frame_height(menu->frame));
4623 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4624 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4625 display_get_serial(window->display),
4626 parent->main_surface->surface,
4627 window->x - ix, window->y - iy, 0);
4631 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4633 window->main_surface->buffer_type = type;
4637 window_set_preferred_format(struct window *window,
4638 enum preferred_format format)
4640 window->preferred_format = format;
4644 window_add_subsurface(struct window *window, void *data,
4645 enum subsurface_mode default_mode)
4647 struct widget *widget;
4648 struct surface *surface;
4649 struct wl_surface *parent;
4650 struct wl_subcompositor *subcompo = window->display->subcompositor;
4652 surface = surface_create(window);
4653 widget = widget_create(window, surface, data);
4654 wl_list_init(&widget->link);
4655 surface->widget = widget;
4657 parent = window->main_surface->surface;
4658 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4661 surface->synchronized = 1;
4663 switch (default_mode) {
4664 case SUBSURFACE_SYNCHRONIZED:
4665 surface->synchronized_default = 1;
4667 case SUBSURFACE_DESYNCHRONIZED:
4668 surface->synchronized_default = 0;
4671 assert(!"bad enum subsurface_mode");
4674 window->resize_needed = 1;
4675 window_schedule_redraw(window);
4681 display_handle_geometry(void *data,
4682 struct wl_output *wl_output,
4685 int physical_height,
4691 struct output *output = data;
4693 output->allocation.x = x;
4694 output->allocation.y = y;
4695 output->transform = transform;
4699 display_handle_done(void *data,
4700 struct wl_output *wl_output)
4705 display_handle_scale(void *data,
4706 struct wl_output *wl_output,
4709 struct output *output = data;
4711 output->scale = scale;
4715 display_handle_mode(void *data,
4716 struct wl_output *wl_output,
4722 struct output *output = data;
4723 struct display *display = output->display;
4725 if (flags & WL_OUTPUT_MODE_CURRENT) {
4726 output->allocation.width = width;
4727 output->allocation.height = height;
4728 if (display->output_configure_handler)
4729 (*display->output_configure_handler)(
4730 output, display->user_data);
4734 static const struct wl_output_listener output_listener = {
4735 display_handle_geometry,
4736 display_handle_mode,
4737 display_handle_done,
4738 display_handle_scale
4742 display_add_output(struct display *d, uint32_t id)
4744 struct output *output;
4746 output = xzalloc(sizeof *output);
4747 output->display = d;
4750 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4751 output->server_output_id = id;
4752 wl_list_insert(d->output_list.prev, &output->link);
4754 wl_output_add_listener(output->output, &output_listener, output);
4758 output_destroy(struct output *output)
4760 if (output->destroy_handler)
4761 (*output->destroy_handler)(output, output->user_data);
4763 wl_output_destroy(output->output);
4764 wl_list_remove(&output->link);
4769 display_destroy_output(struct display *d, uint32_t id)
4771 struct output *output;
4773 wl_list_for_each(output, &d->output_list, link) {
4774 if (output->server_output_id == id) {
4775 output_destroy(output);
4782 display_set_global_handler(struct display *display,
4783 display_global_handler_t handler)
4785 struct global *global;
4787 display->global_handler = handler;
4791 wl_list_for_each(global, &display->global_list, link)
4792 display->global_handler(display,
4793 global->name, global->interface,
4794 global->version, display->user_data);
4798 display_set_global_handler_remove(struct display *display,
4799 display_global_handler_t remove_handler)
4801 display->global_handler_remove = remove_handler;
4802 if (!remove_handler)
4807 display_set_output_configure_handler(struct display *display,
4808 display_output_handler_t handler)
4810 struct output *output;
4812 display->output_configure_handler = handler;
4816 wl_list_for_each(output, &display->output_list, link) {
4817 if (output->allocation.width == 0 &&
4818 output->allocation.height == 0)
4821 (*display->output_configure_handler)(output,
4822 display->user_data);
4827 output_set_user_data(struct output *output, void *data)
4829 output->user_data = data;
4833 output_get_user_data(struct output *output)
4835 return output->user_data;
4839 output_set_destroy_handler(struct output *output,
4840 display_output_handler_t handler)
4842 output->destroy_handler = handler;
4843 /* FIXME: implement this, once we have way to remove outputs */
4847 output_get_allocation(struct output *output, struct rectangle *base)
4849 struct rectangle allocation = output->allocation;
4851 switch (output->transform) {
4852 case WL_OUTPUT_TRANSFORM_90:
4853 case WL_OUTPUT_TRANSFORM_270:
4854 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4855 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4856 /* Swap width and height */
4857 allocation.width = output->allocation.height;
4858 allocation.height = output->allocation.width;
4866 output_get_wl_output(struct output *output)
4868 return output->output;
4871 enum wl_output_transform
4872 output_get_transform(struct output *output)
4874 return output->transform;
4878 output_get_scale(struct output *output)
4880 return output->scale;
4884 fini_xkb(struct input *input)
4886 xkb_state_unref(input->xkb.state);
4887 xkb_map_unref(input->xkb.keymap);
4890 #define MIN(a,b) ((a) < (b) ? a : b)
4893 display_add_input(struct display *d, uint32_t id)
4895 struct input *input;
4897 input = xzalloc(sizeof *input);
4899 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4900 MIN(d->seat_version, 3));
4901 input->touch_focus = NULL;
4902 input->pointer_focus = NULL;
4903 input->keyboard_focus = NULL;
4904 wl_list_init(&input->touch_point_list);
4905 wl_list_insert(d->input_list.prev, &input->link);
4907 wl_seat_add_listener(input->seat, &seat_listener, input);
4908 wl_seat_set_user_data(input->seat, input);
4910 input->data_device =
4911 wl_data_device_manager_get_data_device(d->data_device_manager,
4913 wl_data_device_add_listener(input->data_device, &data_device_listener,
4916 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4918 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4919 TFD_CLOEXEC | TFD_NONBLOCK);
4920 input->repeat_task.run = keyboard_repeat_func;
4921 display_watch_fd(d, input->repeat_timer_fd,
4922 EPOLLIN, &input->repeat_task);
4926 input_destroy(struct input *input)
4928 input_remove_keyboard_focus(input);
4929 input_remove_pointer_focus(input);
4931 if (input->drag_offer)
4932 data_offer_destroy(input->drag_offer);
4934 if (input->selection_offer)
4935 data_offer_destroy(input->selection_offer);
4937 wl_data_device_destroy(input->data_device);
4939 if (input->display->seat_version >= 3) {
4941 wl_pointer_release(input->pointer);
4942 if (input->keyboard)
4943 wl_keyboard_release(input->keyboard);
4948 wl_surface_destroy(input->pointer_surface);
4950 wl_list_remove(&input->link);
4951 wl_seat_destroy(input->seat);
4952 close(input->repeat_timer_fd);
4957 init_workspace_manager(struct display *d, uint32_t id)
4959 d->workspace_manager =
4960 wl_registry_bind(d->registry, id,
4961 &workspace_manager_interface, 1);
4962 if (d->workspace_manager != NULL)
4963 workspace_manager_add_listener(d->workspace_manager,
4964 &workspace_manager_listener,
4969 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4971 struct display *d = data;
4973 if (format == WL_SHM_FORMAT_RGB565)
4977 struct wl_shm_listener shm_listener = {
4982 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4983 const char *interface, uint32_t version)
4985 struct display *d = data;
4986 struct global *global;
4988 global = xmalloc(sizeof *global);
4990 global->interface = strdup(interface);
4991 global->version = version;
4992 wl_list_insert(d->global_list.prev, &global->link);
4994 if (strcmp(interface, "wl_compositor") == 0) {
4995 d->compositor = wl_registry_bind(registry, id,
4996 &wl_compositor_interface, 3);
4997 } else if (strcmp(interface, "wl_output") == 0) {
4998 display_add_output(d, id);
4999 } else if (strcmp(interface, "wl_seat") == 0) {
5000 d->seat_version = version;
5001 display_add_input(d, id);
5002 } else if (strcmp(interface, "wl_shell") == 0) {
5003 d->shell = wl_registry_bind(registry,
5004 id, &wl_shell_interface, 1);
5005 } else if (strcmp(interface, "wl_shm") == 0) {
5006 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5007 wl_shm_add_listener(d->shm, &shm_listener, d);
5008 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5009 d->data_device_manager =
5010 wl_registry_bind(registry, id,
5011 &wl_data_device_manager_interface, 1);
5012 } else if (strcmp(interface, "text_cursor_position") == 0) {
5013 d->text_cursor_position =
5014 wl_registry_bind(registry, id,
5015 &text_cursor_position_interface, 1);
5016 } else if (strcmp(interface, "workspace_manager") == 0) {
5017 init_workspace_manager(d, id);
5018 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5020 wl_registry_bind(registry, id,
5021 &wl_subcompositor_interface, 1);
5024 if (d->global_handler)
5025 d->global_handler(d, id, interface, version, d->user_data);
5029 registry_handle_global_remove(void *data, struct wl_registry *registry,
5032 struct display *d = data;
5033 struct global *global;
5036 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5037 if (global->name != name)
5040 if (strcmp(global->interface, "wl_output") == 0)
5041 display_destroy_output(d, name);
5043 /* XXX: Should destroy remaining bound globals */
5045 if (d->global_handler_remove)
5046 d->global_handler_remove(d, name, global->interface,
5047 global->version, d->user_data);
5049 wl_list_remove(&global->link);
5050 free(global->interface);
5056 display_bind(struct display *display, uint32_t name,
5057 const struct wl_interface *interface, uint32_t version)
5059 return wl_registry_bind(display->registry, name, interface, version);
5062 static const struct wl_registry_listener registry_listener = {
5063 registry_handle_global,
5064 registry_handle_global_remove
5067 #ifdef HAVE_CAIRO_EGL
5069 init_egl(struct display *d)
5071 EGLint major, minor;
5074 #ifdef USE_CAIRO_GLESV2
5075 # define GL_BIT EGL_OPENGL_ES2_BIT
5077 # define GL_BIT EGL_OPENGL_BIT
5080 static const EGLint argb_cfg_attribs[] = {
5081 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5087 EGL_RENDERABLE_TYPE, GL_BIT,
5091 #ifdef USE_CAIRO_GLESV2
5092 static const EGLint context_attribs[] = {
5093 EGL_CONTEXT_CLIENT_VERSION, 2,
5096 EGLint api = EGL_OPENGL_ES_API;
5098 EGLint *context_attribs = NULL;
5099 EGLint api = EGL_OPENGL_API;
5102 d->dpy = eglGetDisplay(d->display);
5103 if (!eglInitialize(d->dpy, &major, &minor)) {
5104 fprintf(stderr, "failed to initialize EGL\n");
5108 if (!eglBindAPI(api)) {
5109 fprintf(stderr, "failed to bind EGL client API\n");
5113 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5114 &d->argb_config, 1, &n) || n != 1) {
5115 fprintf(stderr, "failed to choose argb EGL config\n");
5119 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5120 EGL_NO_CONTEXT, context_attribs);
5121 if (d->argb_ctx == NULL) {
5122 fprintf(stderr, "failed to create EGL context\n");
5126 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5127 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5128 fprintf(stderr, "failed to get cairo EGL argb device\n");
5136 fini_egl(struct display *display)
5138 cairo_device_destroy(display->argb_device);
5140 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5143 eglTerminate(display->dpy);
5149 init_dummy_surface(struct display *display)
5154 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5156 display->dummy_surface =
5157 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5159 display->dummy_surface_data = data;
5163 handle_display_data(struct task *task, uint32_t events)
5165 struct display *display =
5166 container_of(task, struct display, display_task);
5167 struct epoll_event ep;
5170 display->display_fd_events = events;
5172 if (events & EPOLLERR || events & EPOLLHUP) {
5173 display_exit(display);
5177 if (events & EPOLLIN) {
5178 ret = wl_display_dispatch(display->display);
5180 display_exit(display);
5185 if (events & EPOLLOUT) {
5186 ret = wl_display_flush(display->display);
5188 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5189 ep.data.ptr = &display->display_task;
5190 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5191 display->display_fd, &ep);
5192 } else if (ret == -1 && errno != EAGAIN) {
5193 display_exit(display);
5200 log_handler(const char *format, va_list args)
5202 vfprintf(stderr, format, args);
5206 display_create(int *argc, char *argv[])
5210 wl_log_set_handler_client(log_handler);
5212 d = zalloc(sizeof *d);
5216 d->display = wl_display_connect(NULL);
5217 if (d->display == NULL) {
5218 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5223 d->xkb_context = xkb_context_new(0);
5224 if (d->xkb_context == NULL) {
5225 fprintf(stderr, "Failed to create XKB context\n");
5230 d->epoll_fd = os_epoll_create_cloexec();
5231 d->display_fd = wl_display_get_fd(d->display);
5232 d->display_task.run = handle_display_data;
5233 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5236 wl_list_init(&d->deferred_list);
5237 wl_list_init(&d->input_list);
5238 wl_list_init(&d->output_list);
5239 wl_list_init(&d->global_list);
5242 d->workspace_count = 1;
5244 d->registry = wl_display_get_registry(d->display);
5245 wl_registry_add_listener(d->registry, ®istry_listener, d);
5247 if (wl_display_dispatch(d->display) < 0) {
5248 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5252 #ifdef HAVE_CAIRO_EGL
5253 if (init_egl(d) < 0)
5254 fprintf(stderr, "EGL does not seem to work, "
5255 "falling back to software rendering and wl_shm.\n");
5260 d->theme = theme_create();
5262 wl_list_init(&d->window_list);
5264 init_dummy_surface(d);
5270 display_destroy_outputs(struct display *display)
5273 struct output *output;
5275 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5276 output_destroy(output);
5280 display_destroy_inputs(struct display *display)
5283 struct input *input;
5285 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5286 input_destroy(input);
5290 display_destroy(struct display *display)
5292 if (!wl_list_empty(&display->window_list))
5293 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5294 wl_list_length(&display->window_list));
5296 if (!wl_list_empty(&display->deferred_list))
5297 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5299 cairo_surface_destroy(display->dummy_surface);
5300 free(display->dummy_surface_data);
5302 display_destroy_outputs(display);
5303 display_destroy_inputs(display);
5305 xkb_context_unref(display->xkb_context);
5307 theme_destroy(display->theme);
5308 destroy_cursors(display);
5310 #ifdef HAVE_CAIRO_EGL
5311 if (display->argb_device)
5315 if (display->subcompositor)
5316 wl_subcompositor_destroy(display->subcompositor);
5319 wl_shell_destroy(display->shell);
5322 wl_shm_destroy(display->shm);
5324 if (display->data_device_manager)
5325 wl_data_device_manager_destroy(display->data_device_manager);
5327 wl_compositor_destroy(display->compositor);
5328 wl_registry_destroy(display->registry);
5330 close(display->epoll_fd);
5332 if (!(display->display_fd_events & EPOLLERR) &&
5333 !(display->display_fd_events & EPOLLHUP))
5334 wl_display_flush(display->display);
5336 wl_display_disconnect(display->display);
5341 display_set_user_data(struct display *display, void *data)
5343 display->user_data = data;
5347 display_get_user_data(struct display *display)
5349 return display->user_data;
5353 display_get_display(struct display *display)
5355 return display->display;
5359 display_has_subcompositor(struct display *display)
5361 if (display->subcompositor)
5364 wl_display_roundtrip(display->display);
5366 return display->subcompositor != NULL;
5370 display_get_cairo_device(struct display *display)
5372 return display->argb_device;
5376 display_get_output(struct display *display)
5378 return container_of(display->output_list.next, struct output, link);
5381 struct wl_compositor *
5382 display_get_compositor(struct display *display)
5384 return display->compositor;
5388 display_get_serial(struct display *display)
5390 return display->serial;
5394 display_get_egl_display(struct display *d)
5399 struct wl_data_source *
5400 display_create_data_source(struct display *display)
5402 return wl_data_device_manager_create_data_source(display->data_device_manager);
5406 display_get_argb_egl_config(struct display *d)
5408 return d->argb_config;
5412 display_acquire_window_surface(struct display *display,
5413 struct window *window,
5416 struct surface *surface = window->main_surface;
5418 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5421 widget_get_cairo_surface(window->main_surface->widget);
5422 return surface->toysurface->acquire(surface->toysurface, ctx);
5426 display_release_window_surface(struct display *display,
5427 struct window *window)
5429 struct surface *surface = window->main_surface;
5431 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5434 surface->toysurface->release(surface->toysurface);
5438 display_defer(struct display *display, struct task *task)
5440 wl_list_insert(&display->deferred_list, &task->link);
5444 display_watch_fd(struct display *display,
5445 int fd, uint32_t events, struct task *task)
5447 struct epoll_event ep;
5451 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5455 display_unwatch_fd(struct display *display, int fd)
5457 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5461 display_run(struct display *display)
5464 struct epoll_event ep[16];
5467 display->running = 1;
5469 while (!wl_list_empty(&display->deferred_list)) {
5470 task = container_of(display->deferred_list.prev,
5472 wl_list_remove(&task->link);
5476 wl_display_dispatch_pending(display->display);
5478 if (!display->running)
5481 ret = wl_display_flush(display->display);
5482 if (ret < 0 && errno == EAGAIN) {
5484 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5485 ep[0].data.ptr = &display->display_task;
5487 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5488 display->display_fd, &ep[0]);
5489 } else if (ret < 0) {
5493 count = epoll_wait(display->epoll_fd,
5494 ep, ARRAY_LENGTH(ep), -1);
5495 for (i = 0; i < count; i++) {
5496 task = ep[i].data.ptr;
5497 task->run(task, ep[i].events);
5503 display_exit(struct display *display)
5505 display->running = 0;
5509 keysym_modifiers_add(struct wl_array *modifiers_map,
5512 size_t len = strlen(name) + 1;
5515 p = wl_array_add(modifiers_map, len);
5520 strncpy(p, name, len);
5523 static xkb_mod_index_t
5524 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5527 xkb_mod_index_t index = 0;
5528 char *p = modifiers_map->data;
5530 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5531 if (strcmp(p, name) == 0)
5538 return XKB_MOD_INVALID;
5542 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5545 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5547 if (index == XKB_MOD_INVALID)
5548 return XKB_MOD_INVALID;
5554 fail_on_null(void *p)
5557 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5567 return fail_on_null(malloc(s));
5573 return fail_on_null(zalloc(s));
5577 xstrdup(const char *s)
5579 return fail_on_null(strdup(s));
5583 xrealloc(char *p, size_t s)
5585 return fail_on_null(realloc(p, s));