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 free(widget->tooltip);
1658 widget->tooltip = NULL;
1661 wl_list_for_each(input, &display->input_list, link) {
1662 if (input->focus_widget == widget)
1663 input->focus_widget = NULL;
1666 wl_list_remove(&widget->link);
1671 widget_set_default_cursor(struct widget *widget, int cursor)
1673 widget->default_cursor = cursor;
1677 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1679 *allocation = widget->allocation;
1683 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1685 widget->allocation.width = width;
1686 widget->allocation.height = height;
1690 widget_set_allocation(struct widget *widget,
1691 int32_t x, int32_t y, int32_t width, int32_t height)
1693 widget->allocation.x = x;
1694 widget->allocation.y = y;
1695 widget_set_size(widget, width, height);
1699 widget_set_transparent(struct widget *widget, int transparent)
1701 widget->opaque = !transparent;
1705 widget_get_user_data(struct widget *widget)
1707 return widget->user_data;
1710 static cairo_surface_t *
1711 widget_get_cairo_surface(struct widget *widget)
1713 struct surface *surface = widget->surface;
1714 struct window *window = widget->window;
1716 assert(widget->use_cairo);
1718 if (!surface->cairo_surface) {
1719 if (surface == window->main_surface)
1720 window_create_main_surface(window);
1722 surface_create_surface(surface, 0, 0, 0);
1725 return surface->cairo_surface;
1729 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1731 struct surface *surface = widget->surface;
1734 enum wl_output_transform transform;
1735 int surface_width, surface_height;
1736 int translate_x, translate_y;
1739 surface_width = surface->allocation.width;
1740 surface_height = surface->allocation.height;
1742 transform = surface->buffer_transform;
1743 scale = surface->buffer_scale;
1745 switch (transform) {
1746 case WL_OUTPUT_TRANSFORM_FLIPPED:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1749 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1750 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1753 cairo_matrix_init_identity(&m);
1757 switch (transform) {
1758 case WL_OUTPUT_TRANSFORM_NORMAL:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED:
1766 translate_x = surface_width;
1769 case WL_OUTPUT_TRANSFORM_90:
1771 translate_x = surface_height;
1774 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1776 translate_x = surface_height;
1777 translate_y = surface_width;
1779 case WL_OUTPUT_TRANSFORM_180:
1781 translate_x = surface_width;
1782 translate_y = surface_height;
1784 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1787 translate_y = surface_height;
1789 case WL_OUTPUT_TRANSFORM_270:
1790 angle = M_PI + M_PI_2;
1792 translate_y = surface_width;
1794 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1795 angle = M_PI + M_PI_2;
1801 cairo_scale(cr, scale, scale);
1802 cairo_translate(cr, translate_x, translate_y);
1803 cairo_rotate(cr, angle);
1804 cairo_transform(cr, &m);
1808 widget_cairo_create(struct widget *widget)
1810 struct surface *surface = widget->surface;
1811 cairo_surface_t *cairo_surface;
1814 cairo_surface = widget_get_cairo_surface(widget);
1815 cr = cairo_create(cairo_surface);
1817 widget_cairo_update_transform(widget, cr);
1819 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1825 widget_get_wl_surface(struct widget *widget)
1827 return widget->surface->surface;
1830 struct wl_subsurface *
1831 widget_get_wl_subsurface(struct widget *widget)
1833 return widget->surface->subsurface;
1837 widget_get_last_time(struct widget *widget)
1839 return widget->surface->last_time;
1843 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1845 struct wl_compositor *comp = widget->window->display->compositor;
1846 struct surface *surface = widget->surface;
1848 if (!surface->input_region)
1849 surface->input_region = wl_compositor_create_region(comp);
1852 wl_region_add(surface->input_region,
1853 rect->x, rect->y, rect->width, rect->height);
1858 widget_set_resize_handler(struct widget *widget,
1859 widget_resize_handler_t handler)
1861 widget->resize_handler = handler;
1865 widget_set_redraw_handler(struct widget *widget,
1866 widget_redraw_handler_t handler)
1868 widget->redraw_handler = handler;
1872 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1874 widget->enter_handler = handler;
1878 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1880 widget->leave_handler = handler;
1884 widget_set_motion_handler(struct widget *widget,
1885 widget_motion_handler_t handler)
1887 widget->motion_handler = handler;
1891 widget_set_button_handler(struct widget *widget,
1892 widget_button_handler_t handler)
1894 widget->button_handler = handler;
1898 widget_set_touch_up_handler(struct widget *widget,
1899 widget_touch_up_handler_t handler)
1901 widget->touch_up_handler = handler;
1905 widget_set_touch_down_handler(struct widget *widget,
1906 widget_touch_down_handler_t handler)
1908 widget->touch_down_handler = handler;
1912 widget_set_touch_motion_handler(struct widget *widget,
1913 widget_touch_motion_handler_t handler)
1915 widget->touch_motion_handler = handler;
1919 widget_set_touch_frame_handler(struct widget *widget,
1920 widget_touch_frame_handler_t handler)
1922 widget->touch_frame_handler = handler;
1926 widget_set_touch_cancel_handler(struct widget *widget,
1927 widget_touch_cancel_handler_t handler)
1929 widget->touch_cancel_handler = handler;
1933 widget_set_axis_handler(struct widget *widget,
1934 widget_axis_handler_t handler)
1936 widget->axis_handler = handler;
1940 window_schedule_redraw_task(struct window *window);
1943 widget_schedule_redraw(struct widget *widget)
1945 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1946 widget->surface->redraw_needed = 1;
1947 window_schedule_redraw_task(widget->window);
1951 widget_set_use_cairo(struct widget *widget,
1954 widget->use_cairo = use_cairo;
1958 window_get_surface(struct window *window)
1960 cairo_surface_t *cairo_surface;
1962 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1964 return cairo_surface_reference(cairo_surface);
1968 window_get_wl_surface(struct window *window)
1970 return window->main_surface->surface;
1974 tooltip_redraw_handler(struct widget *widget, void *data)
1977 const int32_t r = 3;
1978 struct tooltip *tooltip = data;
1979 int32_t width, height;
1981 cr = widget_cairo_create(widget);
1982 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1983 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1986 width = widget->allocation.width;
1987 height = widget->allocation.height;
1988 rounded_rect(cr, 0, 0, width, height, r);
1990 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1994 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1995 cairo_move_to(cr, 10, 16);
1996 cairo_show_text(cr, tooltip->entry);
2000 static cairo_text_extents_t
2001 get_text_extents(struct tooltip *tooltip)
2004 cairo_text_extents_t extents;
2006 /* Use the dummy_surface because tooltip's surface was not
2007 * created yet, and parent does not have a valid surface
2008 * outside repaint, either.
2010 cr = cairo_create(tooltip->window->display->dummy_surface);
2011 cairo_text_extents(cr, tooltip->entry, &extents);
2018 window_create_tooltip(struct tooltip *tooltip)
2020 struct widget *parent = tooltip->parent;
2021 struct display *display = parent->window->display;
2022 struct window *window;
2023 const int offset_y = 27;
2024 const int margin = 3;
2025 cairo_text_extents_t extents;
2027 if (tooltip->widget)
2030 window = window_create_transient(display, parent->window, tooltip->x,
2031 tooltip->y + offset_y,
2032 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2036 tooltip->window = window;
2037 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2039 extents = get_text_extents(tooltip);
2040 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2041 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2047 widget_destroy_tooltip(struct widget *parent)
2049 struct tooltip *tooltip = parent->tooltip;
2051 parent->tooltip_count = 0;
2055 if (tooltip->widget) {
2056 widget_destroy(tooltip->widget);
2057 window_destroy(tooltip->window);
2058 tooltip->widget = NULL;
2059 tooltip->window = NULL;
2062 close(tooltip->tooltip_fd);
2063 free(tooltip->entry);
2065 parent->tooltip = NULL;
2069 tooltip_func(struct task *task, uint32_t events)
2071 struct tooltip *tooltip =
2072 container_of(task, struct tooltip, tooltip_task);
2075 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2077 window_create_tooltip(tooltip);
2080 #define TOOLTIP_TIMEOUT 500
2082 tooltip_timer_reset(struct tooltip *tooltip)
2084 struct itimerspec its;
2086 its.it_interval.tv_sec = 0;
2087 its.it_interval.tv_nsec = 0;
2088 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2089 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2090 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2091 fprintf(stderr, "could not set timerfd\n: %m");
2099 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2101 struct tooltip *tooltip = parent->tooltip;
2103 parent->tooltip_count++;
2107 tooltip_timer_reset(tooltip);
2111 /* the handler might be triggered too fast via input device motion, so
2112 * we need this check here to make sure tooltip is fully initialized */
2113 if (parent->tooltip_count > 1)
2116 tooltip = malloc(sizeof *tooltip);
2120 parent->tooltip = tooltip;
2121 tooltip->parent = parent;
2122 tooltip->widget = NULL;
2123 tooltip->window = NULL;
2126 tooltip->entry = strdup(entry);
2127 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2128 if (tooltip->tooltip_fd < 0) {
2129 fprintf(stderr, "could not create timerfd\n: %m");
2133 tooltip->tooltip_task.run = tooltip_func;
2134 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2135 EPOLLIN, &tooltip->tooltip_task);
2136 tooltip_timer_reset(tooltip);
2142 workspace_manager_state(void *data,
2143 struct workspace_manager *workspace_manager,
2147 struct display *display = data;
2149 display->workspace = current;
2150 display->workspace_count = count;
2153 static const struct workspace_manager_listener workspace_manager_listener = {
2154 workspace_manager_state
2158 frame_resize_handler(struct widget *widget,
2159 int32_t width, int32_t height, void *data)
2161 struct window_frame *frame = data;
2162 struct widget *child = frame->child;
2163 struct rectangle interior;
2164 struct rectangle input;
2165 struct rectangle opaque;
2167 if (widget->window->type == TYPE_FULLSCREEN) {
2170 interior.width = width;
2171 interior.height = height;
2173 if (widget->window->type == TYPE_MAXIMIZED) {
2174 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2176 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2179 frame_resize(frame->frame, width, height);
2180 frame_interior(frame->frame, &interior.x, &interior.y,
2181 &interior.width, &interior.height);
2184 widget_set_allocation(child, interior.x, interior.y,
2185 interior.width, interior.height);
2187 if (child->resize_handler) {
2188 child->resize_handler(child, interior.width, interior.height,
2191 if (widget->window->type == TYPE_FULLSCREEN) {
2192 width = child->allocation.width;
2193 height = child->allocation.height;
2195 frame_resize_inside(frame->frame,
2196 child->allocation.width,
2197 child->allocation.height);
2198 width = frame_width(frame->frame);
2199 height = frame_height(frame->frame);
2203 widget_set_allocation(widget, 0, 0, width, height);
2205 widget->surface->input_region =
2206 wl_compositor_create_region(widget->window->display->compositor);
2207 if (widget->window->type != TYPE_FULLSCREEN) {
2208 frame_input_rect(frame->frame, &input.x, &input.y,
2209 &input.width, &input.height);
2210 wl_region_add(widget->surface->input_region,
2211 input.x, input.y, input.width, input.height);
2213 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2216 widget_set_allocation(widget, 0, 0, width, height);
2218 if (child->opaque) {
2219 if (widget->window->type != TYPE_FULLSCREEN) {
2220 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2221 &opaque.width, &opaque.height);
2223 wl_region_add(widget->surface->opaque_region,
2225 opaque.width, opaque.height);
2227 wl_region_add(widget->surface->opaque_region,
2228 0, 0, width, height);
2233 widget_schedule_redraw(widget);
2237 frame_redraw_handler(struct widget *widget, void *data)
2240 struct window_frame *frame = data;
2241 struct window *window = widget->window;
2243 if (window->type == TYPE_FULLSCREEN)
2246 if (window->focus_count) {
2247 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2249 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2252 cr = widget_cairo_create(widget);
2254 frame_repaint(frame->frame, cr);
2260 frame_get_pointer_image_for_location(struct window_frame *frame,
2261 enum theme_location location)
2263 struct window *window = frame->widget->window;
2265 if (window->type != TYPE_TOPLEVEL)
2266 return CURSOR_LEFT_PTR;
2269 case THEME_LOCATION_RESIZING_TOP:
2271 case THEME_LOCATION_RESIZING_BOTTOM:
2272 return CURSOR_BOTTOM;
2273 case THEME_LOCATION_RESIZING_LEFT:
2275 case THEME_LOCATION_RESIZING_RIGHT:
2276 return CURSOR_RIGHT;
2277 case THEME_LOCATION_RESIZING_TOP_LEFT:
2278 return CURSOR_TOP_LEFT;
2279 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2280 return CURSOR_TOP_RIGHT;
2281 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2282 return CURSOR_BOTTOM_LEFT;
2283 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2284 return CURSOR_BOTTOM_RIGHT;
2285 case THEME_LOCATION_EXTERIOR:
2286 case THEME_LOCATION_TITLEBAR:
2288 return CURSOR_LEFT_PTR;
2293 frame_menu_func(struct window *window,
2294 struct input *input, int index, void *data)
2296 struct display *display;
2300 if (window->close_handler)
2301 window->close_handler(window->user_data);
2303 display_exit(window->display);
2305 case 1: /* move to workspace above */
2306 display = window->display;
2307 if (display->workspace > 0)
2308 workspace_manager_move_surface(
2309 display->workspace_manager,
2310 window->main_surface->surface,
2311 display->workspace - 1);
2313 case 2: /* move to workspace below */
2314 display = window->display;
2315 if (display->workspace < display->workspace_count - 1)
2316 workspace_manager_move_surface(
2317 display->workspace_manager,
2318 window->main_surface->surface,
2319 display->workspace + 1);
2321 case 3: /* fullscreen */
2322 /* we don't have a way to get out of fullscreen for now */
2323 if (window->fullscreen_handler)
2324 window->fullscreen_handler(window, window->user_data);
2330 window_show_frame_menu(struct window *window,
2331 struct input *input, uint32_t time)
2336 static const char *entries[] = {
2338 "Move to workspace above", "Move to workspace below",
2342 if (window->fullscreen_handler)
2343 count = ARRAY_LENGTH(entries);
2345 count = ARRAY_LENGTH(entries) - 1;
2347 input_get_position(input, &x, &y);
2348 window_show_menu(window->display, input, time, window,
2349 x - 10, y - 10, frame_menu_func, entries, count);
2353 frame_enter_handler(struct widget *widget,
2354 struct input *input, float x, float y, void *data)
2356 struct window_frame *frame = data;
2357 enum theme_location location;
2359 location = frame_pointer_enter(frame->frame, input, x, y);
2360 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2361 widget_schedule_redraw(frame->widget);
2363 return frame_get_pointer_image_for_location(data, location);
2367 frame_motion_handler(struct widget *widget,
2368 struct input *input, uint32_t time,
2369 float x, float y, void *data)
2371 struct window_frame *frame = data;
2372 enum theme_location location;
2374 location = frame_pointer_motion(frame->frame, input, x, y);
2375 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2376 widget_schedule_redraw(frame->widget);
2378 return frame_get_pointer_image_for_location(data, location);
2382 frame_leave_handler(struct widget *widget,
2383 struct input *input, void *data)
2385 struct window_frame *frame = data;
2387 frame_pointer_leave(frame->frame, input);
2388 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2389 widget_schedule_redraw(frame->widget);
2393 frame_handle_status(struct window_frame *frame, struct input *input,
2394 uint32_t time, enum theme_location location)
2396 struct window *window = frame->widget->window;
2399 status = frame_status(frame->frame);
2400 if (status & FRAME_STATUS_REPAINT)
2401 widget_schedule_redraw(frame->widget);
2403 if (status & FRAME_STATUS_MINIMIZE)
2404 fprintf(stderr,"Minimize stub\n");
2406 if (status & FRAME_STATUS_MENU) {
2407 window_show_frame_menu(window, input, time);
2408 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2411 if (status & FRAME_STATUS_MAXIMIZE) {
2412 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2413 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2416 if (status & FRAME_STATUS_CLOSE) {
2417 if (window->close_handler)
2418 window->close_handler(window->user_data);
2420 display_exit(window->display);
2424 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2425 input_ungrab(input);
2426 wl_shell_surface_move(window->shell_surface,
2427 input_get_seat(input),
2428 window->display->serial);
2430 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2433 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2434 input_ungrab(input);
2436 window->resizing = 1;
2437 wl_shell_surface_resize(window->shell_surface,
2438 input_get_seat(input),
2439 window->display->serial,
2442 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2447 frame_button_handler(struct widget *widget,
2448 struct input *input, uint32_t time,
2449 uint32_t button, enum wl_pointer_button_state state,
2453 struct window_frame *frame = data;
2454 enum theme_location location;
2456 location = frame_pointer_button(frame->frame, input, button, state);
2457 frame_handle_status(frame, input, time, location);
2461 frame_touch_down_handler(struct widget *widget, struct input *input,
2462 uint32_t serial, uint32_t time, int32_t id,
2463 float x, float y, void *data)
2465 struct window_frame *frame = data;
2467 frame_touch_down(frame->frame, input, id, x, y);
2468 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2472 frame_touch_up_handler(struct widget *widget,
2473 struct input *input, uint32_t serial, uint32_t time,
2474 int32_t id, void *data)
2476 struct window_frame *frame = data;
2478 frame_touch_up(frame->frame, input, id);
2479 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2483 window_frame_create(struct window *window, void *data)
2485 struct window_frame *frame;
2488 if (window->type == TYPE_CUSTOM) {
2489 buttons = FRAME_BUTTON_NONE;
2491 buttons = FRAME_BUTTON_ALL;
2494 frame = xzalloc(sizeof *frame);
2495 frame->frame = frame_create(window->display->theme, 0, 0,
2496 buttons, window->title);
2498 frame->widget = window_add_widget(window, frame);
2499 frame->child = widget_add_widget(frame->widget, data);
2501 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2502 widget_set_resize_handler(frame->widget, frame_resize_handler);
2503 widget_set_enter_handler(frame->widget, frame_enter_handler);
2504 widget_set_leave_handler(frame->widget, frame_leave_handler);
2505 widget_set_motion_handler(frame->widget, frame_motion_handler);
2506 widget_set_button_handler(frame->widget, frame_button_handler);
2507 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2508 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2510 window->frame = frame;
2512 return frame->child;
2516 window_frame_set_child_size(struct widget *widget, int child_width,
2519 struct display *display = widget->window->display;
2520 struct theme *t = display->theme;
2521 int decoration_width, decoration_height;
2523 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2525 if (widget->window->type != TYPE_FULLSCREEN) {
2526 decoration_width = (t->width + margin) * 2;
2527 decoration_height = t->width +
2528 t->titlebar_height + margin * 2;
2530 width = child_width + decoration_width;
2531 height = child_height + decoration_height;
2533 width = child_width;
2534 height = child_height;
2537 window_schedule_resize(widget->window, width, height);
2541 window_frame_destroy(struct window_frame *frame)
2543 frame_destroy(frame->frame);
2545 /* frame->child must be destroyed by the application */
2546 widget_destroy(frame->widget);
2551 input_set_focus_widget(struct input *input, struct widget *focus,
2554 struct widget *old, *widget;
2557 if (focus == input->focus_widget)
2560 old = input->focus_widget;
2564 widget = input->grab;
2565 if (widget->leave_handler)
2566 widget->leave_handler(old, input, widget->user_data);
2567 input->focus_widget = NULL;
2573 widget = input->grab;
2574 input->focus_widget = focus;
2575 if (widget->enter_handler)
2576 cursor = widget->enter_handler(focus, input, x, y,
2579 cursor = widget->default_cursor;
2581 input_set_pointer_image(input, cursor);
2586 touch_grab(struct input *input, int32_t touch_id)
2588 input->touch_grab = 1;
2589 input->touch_grab_id = touch_id;
2593 touch_ungrab(struct input *input)
2595 struct touch_point *tp, *tmp;
2597 input->touch_grab = 0;
2599 wl_list_for_each_safe(tp, tmp,
2600 &input->touch_point_list, link) {
2601 if (tp->id != input->touch_grab_id)
2603 wl_list_remove(&tp->link);
2611 input_grab(struct input *input, struct widget *widget, uint32_t button)
2613 input->grab = widget;
2614 input->grab_button = button;
2618 input_ungrab(struct input *input)
2620 struct widget *widget;
2623 if (input->pointer_focus) {
2624 widget = window_find_widget(input->pointer_focus,
2625 input->sx, input->sy);
2626 input_set_focus_widget(input, widget, input->sx, input->sy);
2631 input_remove_pointer_focus(struct input *input)
2633 struct window *window = input->pointer_focus;
2638 input_set_focus_widget(input, NULL, 0, 0);
2640 input->pointer_focus = NULL;
2641 input->current_cursor = CURSOR_UNSET;
2645 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2646 uint32_t serial, struct wl_surface *surface,
2647 wl_fixed_t sx_w, wl_fixed_t sy_w)
2649 struct input *input = data;
2650 struct window *window;
2651 struct widget *widget;
2652 float sx = wl_fixed_to_double(sx_w);
2653 float sy = wl_fixed_to_double(sy_w);
2656 /* enter event for a window we've just destroyed */
2660 input->display->serial = serial;
2661 input->pointer_enter_serial = serial;
2662 input->pointer_focus = wl_surface_get_user_data(surface);
2663 window = input->pointer_focus;
2665 if (window->resizing) {
2666 window->resizing = 0;
2667 /* Schedule a redraw to free the pool */
2668 window_schedule_redraw(window);
2674 widget = window_find_widget(window, sx, sy);
2675 input_set_focus_widget(input, widget, sx, sy);
2679 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2680 uint32_t serial, struct wl_surface *surface)
2682 struct input *input = data;
2684 input->display->serial = serial;
2685 input_remove_pointer_focus(input);
2689 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2690 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2692 struct input *input = data;
2693 struct window *window = input->pointer_focus;
2694 struct widget *widget;
2696 float sx = wl_fixed_to_double(sx_w);
2697 float sy = wl_fixed_to_double(sy_w);
2705 /* when making the window smaller - e.g. after a unmaximise we might
2706 * still have a pending motion event that the compositor has picked
2707 * based on the old surface dimensions
2709 if (sx > window->main_surface->allocation.width ||
2710 sy > window->main_surface->allocation.height)
2713 if (!(input->grab && input->grab_button)) {
2714 widget = window_find_widget(window, sx, sy);
2715 input_set_focus_widget(input, widget, sx, sy);
2719 widget = input->grab;
2721 widget = input->focus_widget;
2723 if (widget->motion_handler)
2724 cursor = widget->motion_handler(input->focus_widget,
2725 input, time, sx, sy,
2728 cursor = widget->default_cursor;
2730 cursor = CURSOR_LEFT_PTR;
2732 input_set_pointer_image(input, cursor);
2736 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2737 uint32_t time, uint32_t button, uint32_t state_w)
2739 struct input *input = data;
2740 struct widget *widget;
2741 enum wl_pointer_button_state state = state_w;
2743 input->display->serial = serial;
2744 if (input->focus_widget && input->grab == NULL &&
2745 state == WL_POINTER_BUTTON_STATE_PRESSED)
2746 input_grab(input, input->focus_widget, button);
2748 widget = input->grab;
2749 if (widget && widget->button_handler)
2750 (*widget->button_handler)(widget,
2753 input->grab->user_data);
2755 if (input->grab && input->grab_button == button &&
2756 state == WL_POINTER_BUTTON_STATE_RELEASED)
2757 input_ungrab(input);
2761 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2762 uint32_t time, uint32_t axis, wl_fixed_t value)
2764 struct input *input = data;
2765 struct widget *widget;
2767 widget = input->focus_widget;
2769 widget = input->grab;
2770 if (widget && widget->axis_handler)
2771 (*widget->axis_handler)(widget,
2777 static const struct wl_pointer_listener pointer_listener = {
2778 pointer_handle_enter,
2779 pointer_handle_leave,
2780 pointer_handle_motion,
2781 pointer_handle_button,
2782 pointer_handle_axis,
2786 input_remove_keyboard_focus(struct input *input)
2788 struct window *window = input->keyboard_focus;
2789 struct itimerspec its;
2791 its.it_interval.tv_sec = 0;
2792 its.it_interval.tv_nsec = 0;
2793 its.it_value.tv_sec = 0;
2794 its.it_value.tv_nsec = 0;
2795 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2800 window->focus_count--;
2801 if (window->keyboard_focus_handler)
2802 (*window->keyboard_focus_handler)(window, NULL,
2805 input->keyboard_focus = NULL;
2809 keyboard_repeat_func(struct task *task, uint32_t events)
2811 struct input *input =
2812 container_of(task, struct input, repeat_task);
2813 struct window *window = input->keyboard_focus;
2816 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2817 /* If we change the timer between the fd becoming
2818 * readable and getting here, there'll be nothing to
2819 * read and we get EAGAIN. */
2822 if (window && window->key_handler) {
2823 (*window->key_handler)(window, input, input->repeat_time,
2824 input->repeat_key, input->repeat_sym,
2825 WL_KEYBOARD_KEY_STATE_PRESSED,
2831 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2832 uint32_t format, int fd, uint32_t size)
2834 struct input *input = data;
2835 struct xkb_keymap *keymap;
2836 struct xkb_state *state;
2844 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2849 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2850 if (map_str == MAP_FAILED) {
2855 keymap = xkb_map_new_from_string(input->display->xkb_context,
2857 XKB_KEYMAP_FORMAT_TEXT_V1,
2859 munmap(map_str, size);
2863 fprintf(stderr, "failed to compile keymap\n");
2867 state = xkb_state_new(keymap);
2869 fprintf(stderr, "failed to create XKB state\n");
2870 xkb_map_unref(keymap);
2874 xkb_keymap_unref(input->xkb.keymap);
2875 xkb_state_unref(input->xkb.state);
2876 input->xkb.keymap = keymap;
2877 input->xkb.state = state;
2879 input->xkb.control_mask =
2880 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2881 input->xkb.alt_mask =
2882 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2883 input->xkb.shift_mask =
2884 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2888 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2889 uint32_t serial, struct wl_surface *surface,
2890 struct wl_array *keys)
2892 struct input *input = data;
2893 struct window *window;
2895 input->display->serial = serial;
2896 input->keyboard_focus = wl_surface_get_user_data(surface);
2898 window = input->keyboard_focus;
2899 window->focus_count++;
2900 if (window->keyboard_focus_handler)
2901 (*window->keyboard_focus_handler)(window,
2902 input, window->user_data);
2906 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2907 uint32_t serial, struct wl_surface *surface)
2909 struct input *input = data;
2911 input->display->serial = serial;
2912 input_remove_keyboard_focus(input);
2916 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2917 uint32_t serial, uint32_t time, uint32_t key,
2920 struct input *input = data;
2921 struct window *window = input->keyboard_focus;
2922 uint32_t code, num_syms;
2923 enum wl_keyboard_key_state state = state_w;
2924 const xkb_keysym_t *syms;
2926 struct itimerspec its;
2928 input->display->serial = serial;
2930 if (!window || !input->xkb.state)
2933 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2935 sym = XKB_KEY_NoSymbol;
2939 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2940 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2941 window_set_maximized(window,
2942 window->type != TYPE_MAXIMIZED);
2943 } else if (sym == XKB_KEY_F11 &&
2944 window->fullscreen_handler &&
2945 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2946 window->fullscreen_handler(window, window->user_data);
2947 } else if (sym == XKB_KEY_F4 &&
2948 input->modifiers == MOD_ALT_MASK &&
2949 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2950 if (window->close_handler)
2951 window->close_handler(window->user_data);
2953 display_exit(window->display);
2954 } else if (window->key_handler) {
2955 (*window->key_handler)(window, input, time, key,
2956 sym, state, window->user_data);
2959 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2960 key == input->repeat_key) {
2961 its.it_interval.tv_sec = 0;
2962 its.it_interval.tv_nsec = 0;
2963 its.it_value.tv_sec = 0;
2964 its.it_value.tv_nsec = 0;
2965 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2966 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2967 input->repeat_sym = sym;
2968 input->repeat_key = key;
2969 input->repeat_time = time;
2970 its.it_interval.tv_sec = 0;
2971 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2972 its.it_value.tv_sec = 0;
2973 its.it_value.tv_nsec = 400 * 1000 * 1000;
2974 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2979 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2980 uint32_t serial, uint32_t mods_depressed,
2981 uint32_t mods_latched, uint32_t mods_locked,
2984 struct input *input = data;
2985 xkb_mod_mask_t mask;
2987 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2988 if (!input->xkb.keymap)
2991 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2992 mods_locked, 0, 0, group);
2993 mask = xkb_state_serialize_mods(input->xkb.state,
2994 XKB_STATE_DEPRESSED |
2996 input->modifiers = 0;
2997 if (mask & input->xkb.control_mask)
2998 input->modifiers |= MOD_CONTROL_MASK;
2999 if (mask & input->xkb.alt_mask)
3000 input->modifiers |= MOD_ALT_MASK;
3001 if (mask & input->xkb.shift_mask)
3002 input->modifiers |= MOD_SHIFT_MASK;
3005 static const struct wl_keyboard_listener keyboard_listener = {
3006 keyboard_handle_keymap,
3007 keyboard_handle_enter,
3008 keyboard_handle_leave,
3009 keyboard_handle_key,
3010 keyboard_handle_modifiers,
3014 touch_handle_down(void *data, struct wl_touch *wl_touch,
3015 uint32_t serial, uint32_t time, struct wl_surface *surface,
3016 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3018 struct input *input = data;
3019 struct widget *widget;
3020 float sx = wl_fixed_to_double(x_w);
3021 float sy = wl_fixed_to_double(y_w);
3023 input->display->serial = serial;
3024 input->touch_focus = wl_surface_get_user_data(surface);
3025 if (!input->touch_focus) {
3026 DBG("Failed to find to touch focus for surface %p\n", surface);
3030 widget = window_find_widget(input->touch_focus,
3031 wl_fixed_to_double(x_w),
3032 wl_fixed_to_double(y_w));
3034 struct touch_point *tp = xmalloc(sizeof *tp);
3037 tp->widget = widget;
3040 wl_list_insert(&input->touch_point_list, &tp->link);
3042 if (widget->touch_down_handler)
3043 (*widget->touch_down_handler)(widget, input,
3052 touch_handle_up(void *data, struct wl_touch *wl_touch,
3053 uint32_t serial, uint32_t time, int32_t id)
3055 struct input *input = data;
3056 struct touch_point *tp, *tmp;
3058 if (!input->touch_focus) {
3059 DBG("No touch focus found for touch up event!\n");
3063 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3067 if (tp->widget->touch_up_handler)
3068 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3070 tp->widget->user_data);
3072 wl_list_remove(&tp->link);
3080 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3081 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3083 struct input *input = data;
3084 struct touch_point *tp;
3085 float sx = wl_fixed_to_double(x_w);
3086 float sy = wl_fixed_to_double(y_w);
3088 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3090 if (!input->touch_focus) {
3091 DBG("No touch focus found for touch motion event!\n");
3095 wl_list_for_each(tp, &input->touch_point_list, link) {
3101 if (tp->widget->touch_motion_handler)
3102 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3104 tp->widget->user_data);
3110 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3112 struct input *input = data;
3113 struct touch_point *tp, *tmp;
3115 DBG("touch_handle_frame\n");
3117 if (!input->touch_focus) {
3118 DBG("No touch focus found for touch frame event!\n");
3122 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3123 if (tp->widget->touch_frame_handler)
3124 (*tp->widget->touch_frame_handler)(tp->widget, input,
3125 tp->widget->user_data);
3127 wl_list_remove(&tp->link);
3133 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3135 struct input *input = data;
3136 struct touch_point *tp, *tmp;
3138 DBG("touch_handle_cancel\n");
3140 if (!input->touch_focus) {
3141 DBG("No touch focus found for touch cancel event!\n");
3145 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3146 if (tp->widget->touch_cancel_handler)
3147 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3148 tp->widget->user_data);
3150 wl_list_remove(&tp->link);
3155 static const struct wl_touch_listener touch_listener = {
3158 touch_handle_motion,
3160 touch_handle_cancel,
3164 seat_handle_capabilities(void *data, struct wl_seat *seat,
3165 enum wl_seat_capability caps)
3167 struct input *input = data;
3169 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3170 input->pointer = wl_seat_get_pointer(seat);
3171 wl_pointer_set_user_data(input->pointer, input);
3172 wl_pointer_add_listener(input->pointer, &pointer_listener,
3174 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3175 wl_pointer_destroy(input->pointer);
3176 input->pointer = NULL;
3179 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3180 input->keyboard = wl_seat_get_keyboard(seat);
3181 wl_keyboard_set_user_data(input->keyboard, input);
3182 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3184 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3185 wl_keyboard_destroy(input->keyboard);
3186 input->keyboard = NULL;
3189 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3190 input->touch = wl_seat_get_touch(seat);
3191 wl_touch_set_user_data(input->touch, input);
3192 wl_touch_add_listener(input->touch, &touch_listener, input);
3193 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3194 wl_touch_destroy(input->touch);
3195 input->touch = NULL;
3200 seat_handle_name(void *data, struct wl_seat *seat,
3206 static const struct wl_seat_listener seat_listener = {
3207 seat_handle_capabilities,
3212 input_get_position(struct input *input, int32_t *x, int32_t *y)
3219 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3221 struct touch_point *tp;
3223 wl_list_for_each(tp, &input->touch_point_list, link) {
3236 input_get_display(struct input *input)
3238 return input->display;
3242 input_get_seat(struct input *input)
3248 input_get_modifiers(struct input *input)
3250 return input->modifiers;
3254 input_get_focus_widget(struct input *input)
3256 return input->focus_widget;
3260 struct wl_data_offer *offer;
3261 struct input *input;
3262 struct wl_array types;
3265 struct task io_task;
3273 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3275 struct data_offer *offer = data;
3278 p = wl_array_add(&offer->types, sizeof *p);
3282 static const struct wl_data_offer_listener data_offer_listener = {
3287 data_offer_destroy(struct data_offer *offer)
3292 if (offer->refcount == 0) {
3293 wl_data_offer_destroy(offer->offer);
3294 for (p = offer->types.data; *p; p++)
3296 wl_array_release(&offer->types);
3302 data_device_data_offer(void *data,
3303 struct wl_data_device *data_device,
3304 struct wl_data_offer *_offer)
3306 struct data_offer *offer;
3308 offer = xmalloc(sizeof *offer);
3310 wl_array_init(&offer->types);
3311 offer->refcount = 1;
3312 offer->input = data;
3313 offer->offer = _offer;
3314 wl_data_offer_add_listener(offer->offer,
3315 &data_offer_listener, offer);
3319 data_device_enter(void *data, struct wl_data_device *data_device,
3320 uint32_t serial, struct wl_surface *surface,
3321 wl_fixed_t x_w, wl_fixed_t y_w,
3322 struct wl_data_offer *offer)
3324 struct input *input = data;
3325 struct window *window;
3327 float x = wl_fixed_to_double(x_w);
3328 float y = wl_fixed_to_double(y_w);
3331 window = wl_surface_get_user_data(surface);
3332 input->drag_enter_serial = serial;
3333 input->drag_focus = window,
3337 if (!input->touch_grab)
3338 input->pointer_enter_serial = serial;
3341 input->drag_offer = wl_data_offer_get_user_data(offer);
3343 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3346 types_data = input->drag_offer->types.data;
3348 input->drag_offer = NULL;
3352 if (window->data_handler)
3353 window->data_handler(window, input, x, y, types_data,
3358 data_device_leave(void *data, struct wl_data_device *data_device)
3360 struct input *input = data;
3362 if (input->drag_offer) {
3363 data_offer_destroy(input->drag_offer);
3364 input->drag_offer = NULL;
3369 data_device_motion(void *data, struct wl_data_device *data_device,
3370 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3372 struct input *input = data;
3373 struct window *window = input->drag_focus;
3374 float x = wl_fixed_to_double(x_w);
3375 float y = wl_fixed_to_double(y_w);
3381 if (input->drag_offer)
3382 types_data = input->drag_offer->types.data;
3386 if (window->data_handler)
3387 window->data_handler(window, input, x, y, types_data,
3392 data_device_drop(void *data, struct wl_data_device *data_device)
3394 struct input *input = data;
3395 struct window *window = input->drag_focus;
3401 if (window->drop_handler)
3402 window->drop_handler(window, input,
3403 x, y, window->user_data);
3405 if (input->touch_grab)
3406 touch_ungrab(input);
3410 data_device_selection(void *data,
3411 struct wl_data_device *wl_data_device,
3412 struct wl_data_offer *offer)
3414 struct input *input = data;
3417 if (input->selection_offer)
3418 data_offer_destroy(input->selection_offer);
3421 input->selection_offer = wl_data_offer_get_user_data(offer);
3422 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3425 input->selection_offer = NULL;
3429 static const struct wl_data_device_listener data_device_listener = {
3430 data_device_data_offer,
3435 data_device_selection
3439 input_set_pointer_image_index(struct input *input, int index)
3441 struct wl_buffer *buffer;
3442 struct wl_cursor *cursor;
3443 struct wl_cursor_image *image;
3445 if (!input->pointer)
3448 cursor = input->display->cursors[input->current_cursor];
3452 if (index >= (int) cursor->image_count) {
3453 fprintf(stderr, "cursor index out of range\n");
3457 image = cursor->images[index];
3458 buffer = wl_cursor_image_get_buffer(image);
3462 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3463 input->pointer_surface,
3464 image->hotspot_x, image->hotspot_y);
3465 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3466 wl_surface_damage(input->pointer_surface, 0, 0,
3467 image->width, image->height);
3468 wl_surface_commit(input->pointer_surface);
3471 static const struct wl_callback_listener pointer_surface_listener;
3474 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3477 struct input *input = data;
3478 struct wl_cursor *cursor;
3482 assert(callback == input->cursor_frame_cb);
3483 wl_callback_destroy(callback);
3484 input->cursor_frame_cb = NULL;
3487 if (!input->pointer)
3490 if (input->current_cursor == CURSOR_BLANK) {
3491 wl_pointer_set_cursor(input->pointer,
3492 input->pointer_enter_serial,
3497 if (input->current_cursor == CURSOR_UNSET)
3499 cursor = input->display->cursors[input->current_cursor];
3503 /* FIXME We don't have the current time on the first call so we set
3504 * the animation start to the time of the first frame callback. */
3506 input->cursor_anim_start = 0;
3507 else if (input->cursor_anim_start == 0)
3508 input->cursor_anim_start = time;
3510 if (time == 0 || input->cursor_anim_start == 0)
3513 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3515 if (cursor->image_count > 1) {
3516 input->cursor_frame_cb =
3517 wl_surface_frame(input->pointer_surface);
3518 wl_callback_add_listener(input->cursor_frame_cb,
3519 &pointer_surface_listener, input);
3522 input_set_pointer_image_index(input, i);
3525 static const struct wl_callback_listener pointer_surface_listener = {
3526 pointer_surface_frame_callback
3530 input_set_pointer_image(struct input *input, int pointer)
3534 if (!input->pointer)
3537 if (input->pointer_enter_serial > input->cursor_serial)
3540 if (!force && pointer == input->current_cursor)
3543 input->current_cursor = pointer;
3544 input->cursor_serial = input->pointer_enter_serial;
3545 if (!input->cursor_frame_cb)
3546 pointer_surface_frame_callback(input, NULL, 0);
3548 /* The current frame callback may be stuck if, for instance,
3549 * the set cursor request was processed by the server after
3550 * this client lost the focus. In this case the cursor surface
3551 * might not be mapped and the frame callback wouldn't ever
3552 * complete. Send a set_cursor and attach to try to map the
3553 * cursor surface again so that the callback will finish */
3554 input_set_pointer_image_index(input, 0);
3558 struct wl_data_device *
3559 input_get_data_device(struct input *input)
3561 return input->data_device;
3565 input_set_selection(struct input *input,
3566 struct wl_data_source *source, uint32_t time)
3568 wl_data_device_set_selection(input->data_device, source, time);
3572 input_accept(struct input *input, const char *type)
3574 wl_data_offer_accept(input->drag_offer->offer,
3575 input->drag_enter_serial, type);
3579 offer_io_func(struct task *task, uint32_t events)
3581 struct data_offer *offer =
3582 container_of(task, struct data_offer, io_task);
3586 len = read(offer->fd, buffer, sizeof buffer);
3587 offer->func(buffer, len,
3588 offer->x, offer->y, offer->user_data);
3592 data_offer_destroy(offer);
3597 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3598 data_func_t func, void *user_data)
3602 if (pipe2(p, O_CLOEXEC) == -1)
3605 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3608 offer->io_task.run = offer_io_func;
3612 offer->user_data = user_data;
3614 display_watch_fd(offer->input->display,
3615 offer->fd, EPOLLIN, &offer->io_task);
3619 input_receive_drag_data(struct input *input, const char *mime_type,
3620 data_func_t func, void *data)
3622 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3623 input->drag_offer->x = input->drag_x;
3624 input->drag_offer->y = input->drag_y;
3628 input_receive_drag_data_to_fd(struct input *input,
3629 const char *mime_type, int fd)
3631 if (input->drag_offer)
3632 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3638 input_receive_selection_data(struct input *input, const char *mime_type,
3639 data_func_t func, void *data)
3643 if (input->selection_offer == NULL)
3646 for (p = input->selection_offer->types.data; *p; p++)
3647 if (strcmp(mime_type, *p) == 0)
3653 data_offer_receive_data(input->selection_offer,
3654 mime_type, func, data);
3659 input_receive_selection_data_to_fd(struct input *input,
3660 const char *mime_type, int fd)
3662 if (input->selection_offer)
3663 wl_data_offer_receive(input->selection_offer->offer,
3670 window_move(struct window *window, struct input *input, uint32_t serial)
3672 if (!window->shell_surface)
3675 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3679 surface_set_synchronized(struct surface *surface)
3681 if (!surface->subsurface)
3684 if (surface->synchronized)
3687 wl_subsurface_set_sync(surface->subsurface);
3688 surface->synchronized = 1;
3692 surface_set_synchronized_default(struct surface *surface)
3694 if (!surface->subsurface)
3697 if (surface->synchronized == surface->synchronized_default)
3700 if (surface->synchronized_default)
3701 wl_subsurface_set_sync(surface->subsurface);
3703 wl_subsurface_set_desync(surface->subsurface);
3705 surface->synchronized = surface->synchronized_default;
3709 surface_resize(struct surface *surface)
3711 struct widget *widget = surface->widget;
3712 struct wl_compositor *compositor = widget->window->display->compositor;
3714 if (surface->input_region) {
3715 wl_region_destroy(surface->input_region);
3716 surface->input_region = NULL;
3719 if (surface->opaque_region)
3720 wl_region_destroy(surface->opaque_region);
3722 surface->opaque_region = wl_compositor_create_region(compositor);
3724 if (widget->resize_handler)
3725 widget->resize_handler(widget,
3726 widget->allocation.width,
3727 widget->allocation.height,
3730 if (surface->subsurface &&
3731 (surface->allocation.x != widget->allocation.x ||
3732 surface->allocation.y != widget->allocation.y)) {
3733 wl_subsurface_set_position(surface->subsurface,
3734 widget->allocation.x,
3735 widget->allocation.y);
3737 if (surface->allocation.width != widget->allocation.width ||
3738 surface->allocation.height != widget->allocation.height) {
3739 window_schedule_redraw(widget->window);
3741 surface->allocation = widget->allocation;
3744 wl_region_add(surface->opaque_region, 0, 0,
3745 widget->allocation.width,
3746 widget->allocation.height);
3750 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3753 * This hack should be removed, when EGL respects
3754 * eglSwapInterval(0).
3756 * If this window has sub-surfaces, especially a free-running
3757 * EGL-widget, we need to post the parent surface once with
3758 * all the old state to guarantee, that the EGL-widget will
3759 * receive its frame callback soon. Otherwise, a forced call
3760 * to eglSwapBuffers may end up blocking, waiting for a frame
3761 * event that will never come, because we will commit the parent
3762 * surface with all new state only after eglSwapBuffers returns.
3764 * This assumes, that:
3765 * 1. When the EGL widget's resize hook is called, it pauses.
3766 * 2. When the EGL widget's redraw hook is called, it forces a
3767 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3768 * In a single threaded application condition 1 is a no-op.
3770 * XXX: This should actually be after the surface_resize() calls,
3771 * but cannot, because then it would commit the incomplete state
3772 * accumulated from the widget resize hooks.
3774 if (window->subsurface_list.next != &window->main_surface->link ||
3775 window->subsurface_list.prev != &window->main_surface->link)
3776 wl_surface_commit(window->main_surface->surface);
3780 window_do_resize(struct window *window)
3782 struct surface *surface;
3784 widget_set_allocation(window->main_surface->widget,
3785 window->pending_allocation.x,
3786 window->pending_allocation.y,
3787 window->pending_allocation.width,
3788 window->pending_allocation.height);
3790 surface_resize(window->main_surface);
3792 /* The main surface is in the list, too. Main surface's
3793 * resize_handler is responsible for calling widget_set_allocation()
3794 * on all sub-surface root widgets, so they will be resized
3797 wl_list_for_each(surface, &window->subsurface_list, link) {
3798 if (surface == window->main_surface)
3801 surface_set_synchronized(surface);
3802 surface_resize(surface);
3807 idle_resize(struct window *window)
3809 window->resize_needed = 0;
3810 window->redraw_needed = 1;
3812 DBG("from %dx%d to %dx%d\n",
3813 window->main_surface->server_allocation.width,
3814 window->main_surface->server_allocation.height,
3815 window->pending_allocation.width,
3816 window->pending_allocation.height);
3818 hack_prevent_EGL_sub_surface_deadlock(window);
3820 window_do_resize(window);
3824 undo_resize(struct window *window)
3826 window->pending_allocation.width =
3827 window->main_surface->server_allocation.width;
3828 window->pending_allocation.height =
3829 window->main_surface->server_allocation.height;
3831 DBG("back to %dx%d\n",
3832 window->main_surface->server_allocation.width,
3833 window->main_surface->server_allocation.height);
3835 window_do_resize(window);
3837 if (window->pending_allocation.width == 0 &&
3838 window->pending_allocation.height == 0) {
3839 fprintf(stderr, "Error: Could not draw a surface, "
3840 "most likely due to insufficient disk space in "
3841 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3847 window_schedule_resize(struct window *window, int width, int height)
3849 /* We should probably get these numbers from the theme. */
3850 const int min_width = 200, min_height = 200;
3852 window->pending_allocation.x = 0;
3853 window->pending_allocation.y = 0;
3854 window->pending_allocation.width = width;
3855 window->pending_allocation.height = height;
3857 if (window->min_allocation.width == 0) {
3858 if (width < min_width && window->frame)
3859 window->min_allocation.width = min_width;
3861 window->min_allocation.width = width;
3862 if (height < min_height && window->frame)
3863 window->min_allocation.height = min_height;
3865 window->min_allocation.height = height;
3868 if (window->pending_allocation.width < window->min_allocation.width)
3869 window->pending_allocation.width = window->min_allocation.width;
3870 if (window->pending_allocation.height < window->min_allocation.height)
3871 window->pending_allocation.height = window->min_allocation.height;
3873 window->resize_needed = 1;
3874 window_schedule_redraw(window);
3878 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3880 window_schedule_resize(widget->window, width, height);
3884 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3887 wl_shell_surface_pong(shell_surface, serial);
3891 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3892 uint32_t edges, int32_t width, int32_t height)
3894 struct window *window = data;
3896 window->resize_edges = edges;
3897 window_schedule_resize(window, width, height);
3901 menu_destroy(struct menu *menu)
3903 widget_destroy(menu->widget);
3904 window_destroy(menu->window);
3905 frame_destroy(menu->frame);
3910 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3912 struct window *window = data;
3913 struct menu *menu = window->main_surface->widget->user_data;
3915 /* FIXME: Need more context in this event, at least the input
3916 * device. Or just use wl_callback. And this really needs to
3917 * be a window vfunc that the menu can set. And we need the
3920 input_ungrab(menu->input);
3924 static const struct wl_shell_surface_listener shell_surface_listener = {
3931 window_get_allocation(struct window *window,
3932 struct rectangle *allocation)
3934 *allocation = window->main_surface->allocation;
3938 widget_redraw(struct widget *widget)
3940 struct widget *child;
3942 if (widget->redraw_handler)
3943 widget->redraw_handler(widget, widget->user_data);
3944 wl_list_for_each(child, &widget->child_list, link)
3945 widget_redraw(child);
3949 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3951 struct surface *surface = data;
3953 assert(callback == surface->frame_cb);
3954 DBG_OBJ(callback, "done\n");
3955 wl_callback_destroy(callback);
3956 surface->frame_cb = NULL;
3958 surface->last_time = time;
3960 if (surface->redraw_needed || surface->window->redraw_needed) {
3961 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3962 window_schedule_redraw_task(surface->window);
3966 static const struct wl_callback_listener listener = {
3971 surface_redraw(struct surface *surface)
3973 DBG_OBJ(surface->surface, "begin\n");
3975 if (!surface->window->redraw_needed && !surface->redraw_needed)
3978 /* Whole-window redraw forces a redraw even if the previous has
3979 * not yet hit the screen.
3981 if (surface->frame_cb) {
3982 if (!surface->window->redraw_needed)
3985 DBG_OBJ(surface->frame_cb, "cancelled\n");
3986 wl_callback_destroy(surface->frame_cb);
3989 if (surface->widget->use_cairo &&
3990 !widget_get_cairo_surface(surface->widget)) {
3991 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3995 surface->frame_cb = wl_surface_frame(surface->surface);
3996 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3997 DBG_OBJ(surface->frame_cb, "new\n");
3999 surface->redraw_needed = 0;
4000 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4001 widget_redraw(surface->widget);
4002 DBG_OBJ(surface->surface, "done\n");
4007 idle_redraw(struct task *task, uint32_t events)
4009 struct window *window = container_of(task, struct window, redraw_task);
4010 struct surface *surface;
4014 DBG(" --------- \n");
4016 wl_list_init(&window->redraw_task.link);
4017 window->redraw_task_scheduled = 0;
4019 if (window->resize_needed) {
4020 /* throttle resizing to the main surface display */
4021 if (window->main_surface->frame_cb) {
4022 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4026 idle_resize(window);
4030 if (surface_redraw(window->main_surface) < 0) {
4032 * Only main_surface failure will cause us to undo the resize.
4033 * If sub-surfaces fail, they will just be broken with old
4038 wl_list_for_each(surface, &window->subsurface_list, link) {
4039 if (surface == window->main_surface)
4042 surface_redraw(surface);
4046 window->redraw_needed = 0;
4047 window_flush(window);
4049 wl_list_for_each(surface, &window->subsurface_list, link)
4050 surface_set_synchronized_default(surface);
4052 if (resized && failed) {
4053 /* Restore widget tree to correspond to what is on screen. */
4054 undo_resize(window);
4059 window_schedule_redraw_task(struct window *window)
4061 if (window->configure_requests)
4063 if (!window->redraw_task_scheduled) {
4064 window->redraw_task.run = idle_redraw;
4065 display_defer(window->display, &window->redraw_task);
4066 window->redraw_task_scheduled = 1;
4071 window_schedule_redraw(struct window *window)
4073 struct surface *surface;
4075 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4077 wl_list_for_each(surface, &window->subsurface_list, link)
4078 surface->redraw_needed = 1;
4080 window_schedule_redraw_task(window);
4084 window_is_fullscreen(struct window *window)
4086 return window->type == TYPE_FULLSCREEN;
4090 window_is_transient(struct window *window)
4092 return window->type == TYPE_TRANSIENT;
4096 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4098 struct window *window = data;
4100 wl_callback_destroy(callback);
4101 window->configure_requests--;
4103 if (!window->configure_requests)
4104 window_schedule_redraw(window);
4107 static struct wl_callback_listener configure_request_listener = {
4108 configure_request_completed,
4112 window_defer_redraw_until_configure(struct window* window)
4114 struct wl_callback *callback;
4116 if (window->redraw_task_scheduled) {
4117 wl_list_remove(&window->redraw_task.link);
4118 window->redraw_task_scheduled = 0;
4121 callback = wl_display_sync(window->display->display);
4122 wl_callback_add_listener(callback, &configure_request_listener, window);
4123 window->configure_requests++;
4127 window_set_fullscreen(struct window *window, int fullscreen)
4129 if (!window->display->shell)
4132 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4136 window->saved_type = window->type;
4137 if (window->type == TYPE_TOPLEVEL) {
4138 window->saved_allocation = window->main_surface->allocation;
4140 window->type = TYPE_FULLSCREEN;
4141 wl_shell_surface_set_fullscreen(window->shell_surface,
4142 window->fullscreen_method,
4144 window_defer_redraw_until_configure (window);
4146 if (window->saved_type == TYPE_MAXIMIZED) {
4147 window_set_maximized(window, 1);
4149 window->type = TYPE_TOPLEVEL;
4150 wl_shell_surface_set_toplevel(window->shell_surface);
4151 window_schedule_resize(window,
4152 window->saved_allocation.width,
4153 window->saved_allocation.height);
4160 window_set_fullscreen_method(struct window *window,
4161 enum wl_shell_surface_fullscreen_method method)
4163 window->fullscreen_method = method;
4167 window_is_maximized(struct window *window)
4169 return window->type == TYPE_MAXIMIZED;
4173 window_set_maximized(struct window *window, int maximized)
4175 if (!window->display->shell)
4178 if ((window->type == TYPE_MAXIMIZED) == maximized)
4181 if (window->type == TYPE_TOPLEVEL) {
4182 window->saved_allocation = window->main_surface->allocation;
4183 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4184 window->type = TYPE_MAXIMIZED;
4185 window_defer_redraw_until_configure(window);
4186 } else if (window->type == TYPE_FULLSCREEN) {
4187 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4188 window->type = TYPE_MAXIMIZED;
4189 window_defer_redraw_until_configure(window);
4191 wl_shell_surface_set_toplevel(window->shell_surface);
4192 window->type = TYPE_TOPLEVEL;
4193 window_schedule_resize(window,
4194 window->saved_allocation.width,
4195 window->saved_allocation.height);
4200 window_set_user_data(struct window *window, void *data)
4202 window->user_data = data;
4206 window_get_user_data(struct window *window)
4208 return window->user_data;
4212 window_set_key_handler(struct window *window,
4213 window_key_handler_t handler)
4215 window->key_handler = handler;
4219 window_set_keyboard_focus_handler(struct window *window,
4220 window_keyboard_focus_handler_t handler)
4222 window->keyboard_focus_handler = handler;
4226 window_set_data_handler(struct window *window, window_data_handler_t handler)
4228 window->data_handler = handler;
4232 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4234 window->drop_handler = handler;
4238 window_set_close_handler(struct window *window,
4239 window_close_handler_t handler)
4241 window->close_handler = handler;
4245 window_set_fullscreen_handler(struct window *window,
4246 window_fullscreen_handler_t handler)
4248 window->fullscreen_handler = handler;
4252 window_set_output_handler(struct window *window,
4253 window_output_handler_t handler)
4255 window->output_handler = handler;
4259 window_set_title(struct window *window, const char *title)
4261 free(window->title);
4262 window->title = strdup(title);
4263 if (window->frame) {
4264 frame_set_title(window->frame->frame, title);
4265 widget_schedule_redraw(window->frame->widget);
4267 if (window->shell_surface)
4268 wl_shell_surface_set_title(window->shell_surface, title);
4272 window_get_title(struct window *window)
4274 return window->title;
4278 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4280 struct text_cursor_position *text_cursor_position =
4281 window->display->text_cursor_position;
4283 if (!text_cursor_position)
4286 text_cursor_position_notify(text_cursor_position,
4287 window->main_surface->surface,
4288 wl_fixed_from_int(x),
4289 wl_fixed_from_int(y));
4293 window_damage(struct window *window, int32_t x, int32_t y,
4294 int32_t width, int32_t height)
4296 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4300 surface_enter(void *data,
4301 struct wl_surface *wl_surface, struct wl_output *wl_output)
4303 struct window *window = data;
4304 struct output *output;
4305 struct output *output_found = NULL;
4306 struct window_output *window_output;
4308 wl_list_for_each(output, &window->display->output_list, link) {
4309 if (output->output == wl_output) {
4310 output_found = output;
4318 window_output = xmalloc(sizeof *window_output);
4319 window_output->output = output_found;
4321 wl_list_insert (&window->window_output_list, &window_output->link);
4323 if (window->output_handler)
4324 window->output_handler(window, output_found, 1,
4329 surface_leave(void *data,
4330 struct wl_surface *wl_surface, struct wl_output *output)
4332 struct window *window = data;
4333 struct window_output *window_output;
4334 struct window_output *window_output_found = NULL;
4336 wl_list_for_each(window_output, &window->window_output_list, link) {
4337 if (window_output->output->output == output) {
4338 window_output_found = window_output;
4343 if (window_output_found) {
4344 wl_list_remove(&window_output_found->link);
4346 if (window->output_handler)
4347 window->output_handler(window, window_output->output,
4348 0, window->user_data);
4350 free(window_output_found);
4354 static const struct wl_surface_listener surface_listener = {
4359 static struct surface *
4360 surface_create(struct window *window)
4362 struct display *display = window->display;
4363 struct surface *surface;
4365 surface = xmalloc(sizeof *surface);
4366 memset(surface, 0, sizeof *surface);
4370 surface->window = window;
4371 surface->surface = wl_compositor_create_surface(display->compositor);
4372 surface->buffer_scale = 1;
4373 wl_surface_add_listener(surface->surface, &surface_listener, window);
4375 wl_list_insert(&window->subsurface_list, &surface->link);
4380 static struct window *
4381 window_create_internal(struct display *display, int type)
4383 struct window *window;
4384 struct surface *surface;
4386 window = xzalloc(sizeof *window);
4387 wl_list_init(&window->subsurface_list);
4388 window->display = display;
4390 surface = surface_create(window);
4391 window->main_surface = surface;
4393 if (type != TYPE_CUSTOM && display->shell) {
4394 window->shell_surface =
4395 wl_shell_get_shell_surface(display->shell,
4397 fail_on_null(window->shell_surface);
4400 window->type = type;
4401 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4402 window->configure_requests = 0;
4403 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4405 if (display->argb_device)
4406 #ifdef HAVE_CAIRO_EGL
4407 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4409 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4412 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4414 wl_surface_set_user_data(surface->surface, window);
4415 wl_list_insert(display->window_list.prev, &window->link);
4416 wl_list_init(&window->redraw_task.link);
4418 if (window->shell_surface) {
4419 wl_shell_surface_set_user_data(window->shell_surface, window);
4420 wl_shell_surface_add_listener(window->shell_surface,
4421 &shell_surface_listener, window);
4424 wl_list_init (&window->window_output_list);
4430 window_create(struct display *display)
4432 return window_create_internal(display, TYPE_NONE);
4436 window_create_custom(struct display *display)
4438 return window_create_internal(display, TYPE_CUSTOM);
4442 window_create_transient(struct display *display, struct window *parent,
4443 int32_t x, int32_t y, uint32_t flags)
4445 struct window *window;
4447 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4453 wl_shell_surface_set_transient(
4454 window->shell_surface,
4455 parent->main_surface->surface,
4456 window->x, window->y, flags);
4462 menu_set_item(struct menu *menu, int sy)
4464 int32_t x, y, width, height;
4467 frame_interior(menu->frame, &x, &y, &width, &height);
4468 next = (sy - y) / 20;
4469 if (menu->current != next) {
4470 menu->current = next;
4471 widget_schedule_redraw(menu->widget);
4476 menu_motion_handler(struct widget *widget,
4477 struct input *input, uint32_t time,
4478 float x, float y, void *data)
4480 struct menu *menu = data;
4482 if (widget == menu->widget)
4483 menu_set_item(data, y);
4485 return CURSOR_LEFT_PTR;
4489 menu_enter_handler(struct widget *widget,
4490 struct input *input, float x, float y, void *data)
4492 struct menu *menu = data;
4494 if (widget == menu->widget)
4495 menu_set_item(data, y);
4497 return CURSOR_LEFT_PTR;
4501 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4503 struct menu *menu = data;
4505 if (widget == menu->widget)
4506 menu_set_item(data, -200);
4510 menu_button_handler(struct widget *widget,
4511 struct input *input, uint32_t time,
4512 uint32_t button, enum wl_pointer_button_state state,
4516 struct menu *menu = data;
4518 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4519 (menu->release_count > 0 || time - menu->time > 500)) {
4520 /* Either relase after press-drag-release or
4521 * click-motion-click. */
4522 menu->func(menu->parent, input,
4523 menu->current, menu->parent->user_data);
4524 input_ungrab(input);
4526 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4527 menu->release_count++;
4532 menu_redraw_handler(struct widget *widget, void *data)
4535 struct menu *menu = data;
4536 int32_t x, y, width, height, i;
4538 cr = widget_cairo_create(widget);
4540 frame_repaint(menu->frame, cr);
4541 frame_interior(menu->frame, &x, &y, &width, &height);
4543 theme_set_background_source(menu->window->display->theme,
4544 cr, THEME_FRAME_ACTIVE);
4545 cairo_rectangle(cr, x, y, width, height);
4548 cairo_select_font_face(cr, "sans",
4549 CAIRO_FONT_SLANT_NORMAL,
4550 CAIRO_FONT_WEIGHT_NORMAL);
4551 cairo_set_font_size(cr, 12);
4553 for (i = 0; i < menu->count; i++) {
4554 if (i == menu->current) {
4555 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4556 cairo_rectangle(cr, x, y + i * 20, width, 20);
4558 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4559 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4560 cairo_show_text(cr, menu->entries[i]);
4562 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4563 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4564 cairo_show_text(cr, menu->entries[i]);
4572 window_show_menu(struct display *display,
4573 struct input *input, uint32_t time, struct window *parent,
4574 int32_t x, int32_t y,
4575 menu_func_t func, const char **entries, int count)
4577 struct window *window;
4581 menu = malloc(sizeof *menu);
4585 window = window_create_internal(parent->display, TYPE_MENU);
4591 menu->window = window;
4592 menu->parent = parent;
4593 menu->widget = window_add_widget(menu->window, menu);
4594 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4595 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4596 menu->frame = frame_create(window->display->theme, 0, 0,
4597 FRAME_BUTTON_NONE, NULL);
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");
4678 display_handle_geometry(void *data,
4679 struct wl_output *wl_output,
4682 int physical_height,
4688 struct output *output = data;
4690 output->allocation.x = x;
4691 output->allocation.y = y;
4692 output->transform = transform;
4696 display_handle_done(void *data,
4697 struct wl_output *wl_output)
4702 display_handle_scale(void *data,
4703 struct wl_output *wl_output,
4706 struct output *output = data;
4708 output->scale = scale;
4712 display_handle_mode(void *data,
4713 struct wl_output *wl_output,
4719 struct output *output = data;
4720 struct display *display = output->display;
4722 if (flags & WL_OUTPUT_MODE_CURRENT) {
4723 output->allocation.width = width;
4724 output->allocation.height = height;
4725 if (display->output_configure_handler)
4726 (*display->output_configure_handler)(
4727 output, display->user_data);
4731 static const struct wl_output_listener output_listener = {
4732 display_handle_geometry,
4733 display_handle_mode,
4734 display_handle_done,
4735 display_handle_scale
4739 display_add_output(struct display *d, uint32_t id)
4741 struct output *output;
4743 output = xzalloc(sizeof *output);
4744 output->display = d;
4747 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4748 output->server_output_id = id;
4749 wl_list_insert(d->output_list.prev, &output->link);
4751 wl_output_add_listener(output->output, &output_listener, output);
4755 output_destroy(struct output *output)
4757 if (output->destroy_handler)
4758 (*output->destroy_handler)(output, output->user_data);
4760 wl_output_destroy(output->output);
4761 wl_list_remove(&output->link);
4766 display_destroy_output(struct display *d, uint32_t id)
4768 struct output *output;
4770 wl_list_for_each(output, &d->output_list, link) {
4771 if (output->server_output_id == id) {
4772 output_destroy(output);
4779 display_set_global_handler(struct display *display,
4780 display_global_handler_t handler)
4782 struct global *global;
4784 display->global_handler = handler;
4788 wl_list_for_each(global, &display->global_list, link)
4789 display->global_handler(display,
4790 global->name, global->interface,
4791 global->version, display->user_data);
4795 display_set_global_handler_remove(struct display *display,
4796 display_global_handler_t remove_handler)
4798 display->global_handler_remove = remove_handler;
4799 if (!remove_handler)
4804 display_set_output_configure_handler(struct display *display,
4805 display_output_handler_t handler)
4807 struct output *output;
4809 display->output_configure_handler = handler;
4813 wl_list_for_each(output, &display->output_list, link) {
4814 if (output->allocation.width == 0 &&
4815 output->allocation.height == 0)
4818 (*display->output_configure_handler)(output,
4819 display->user_data);
4824 output_set_user_data(struct output *output, void *data)
4826 output->user_data = data;
4830 output_get_user_data(struct output *output)
4832 return output->user_data;
4836 output_set_destroy_handler(struct output *output,
4837 display_output_handler_t handler)
4839 output->destroy_handler = handler;
4840 /* FIXME: implement this, once we have way to remove outputs */
4844 output_get_allocation(struct output *output, struct rectangle *base)
4846 struct rectangle allocation = output->allocation;
4848 switch (output->transform) {
4849 case WL_OUTPUT_TRANSFORM_90:
4850 case WL_OUTPUT_TRANSFORM_270:
4851 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4852 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4853 /* Swap width and height */
4854 allocation.width = output->allocation.height;
4855 allocation.height = output->allocation.width;
4863 output_get_wl_output(struct output *output)
4865 return output->output;
4868 enum wl_output_transform
4869 output_get_transform(struct output *output)
4871 return output->transform;
4875 output_get_scale(struct output *output)
4877 return output->scale;
4881 fini_xkb(struct input *input)
4883 xkb_state_unref(input->xkb.state);
4884 xkb_map_unref(input->xkb.keymap);
4887 #define MIN(a,b) ((a) < (b) ? a : b)
4890 display_add_input(struct display *d, uint32_t id)
4892 struct input *input;
4894 input = xzalloc(sizeof *input);
4896 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4897 MIN(d->seat_version, 3));
4898 input->touch_focus = NULL;
4899 input->pointer_focus = NULL;
4900 input->keyboard_focus = NULL;
4901 wl_list_init(&input->touch_point_list);
4902 wl_list_insert(d->input_list.prev, &input->link);
4904 wl_seat_add_listener(input->seat, &seat_listener, input);
4905 wl_seat_set_user_data(input->seat, input);
4907 input->data_device =
4908 wl_data_device_manager_get_data_device(d->data_device_manager,
4910 wl_data_device_add_listener(input->data_device, &data_device_listener,
4913 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4915 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4916 TFD_CLOEXEC | TFD_NONBLOCK);
4917 input->repeat_task.run = keyboard_repeat_func;
4918 display_watch_fd(d, input->repeat_timer_fd,
4919 EPOLLIN, &input->repeat_task);
4923 input_destroy(struct input *input)
4925 input_remove_keyboard_focus(input);
4926 input_remove_pointer_focus(input);
4928 if (input->drag_offer)
4929 data_offer_destroy(input->drag_offer);
4931 if (input->selection_offer)
4932 data_offer_destroy(input->selection_offer);
4934 wl_data_device_destroy(input->data_device);
4936 if (input->display->seat_version >= 3) {
4938 wl_pointer_release(input->pointer);
4939 if (input->keyboard)
4940 wl_keyboard_release(input->keyboard);
4945 wl_surface_destroy(input->pointer_surface);
4947 wl_list_remove(&input->link);
4948 wl_seat_destroy(input->seat);
4949 close(input->repeat_timer_fd);
4954 init_workspace_manager(struct display *d, uint32_t id)
4956 d->workspace_manager =
4957 wl_registry_bind(d->registry, id,
4958 &workspace_manager_interface, 1);
4959 if (d->workspace_manager != NULL)
4960 workspace_manager_add_listener(d->workspace_manager,
4961 &workspace_manager_listener,
4966 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4968 struct display *d = data;
4970 if (format == WL_SHM_FORMAT_RGB565)
4974 struct wl_shm_listener shm_listener = {
4979 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4980 const char *interface, uint32_t version)
4982 struct display *d = data;
4983 struct global *global;
4985 global = xmalloc(sizeof *global);
4987 global->interface = strdup(interface);
4988 global->version = version;
4989 wl_list_insert(d->global_list.prev, &global->link);
4991 if (strcmp(interface, "wl_compositor") == 0) {
4992 d->compositor = wl_registry_bind(registry, id,
4993 &wl_compositor_interface, 3);
4994 } else if (strcmp(interface, "wl_output") == 0) {
4995 display_add_output(d, id);
4996 } else if (strcmp(interface, "wl_seat") == 0) {
4997 d->seat_version = version;
4998 display_add_input(d, id);
4999 } else if (strcmp(interface, "wl_shell") == 0) {
5000 d->shell = wl_registry_bind(registry,
5001 id, &wl_shell_interface, 1);
5002 } else if (strcmp(interface, "wl_shm") == 0) {
5003 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5004 wl_shm_add_listener(d->shm, &shm_listener, d);
5005 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5006 d->data_device_manager =
5007 wl_registry_bind(registry, id,
5008 &wl_data_device_manager_interface, 1);
5009 } else if (strcmp(interface, "text_cursor_position") == 0) {
5010 d->text_cursor_position =
5011 wl_registry_bind(registry, id,
5012 &text_cursor_position_interface, 1);
5013 } else if (strcmp(interface, "workspace_manager") == 0) {
5014 init_workspace_manager(d, id);
5015 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5017 wl_registry_bind(registry, id,
5018 &wl_subcompositor_interface, 1);
5021 if (d->global_handler)
5022 d->global_handler(d, id, interface, version, d->user_data);
5026 registry_handle_global_remove(void *data, struct wl_registry *registry,
5029 struct display *d = data;
5030 struct global *global;
5033 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5034 if (global->name != name)
5037 if (strcmp(global->interface, "wl_output") == 0)
5038 display_destroy_output(d, name);
5040 /* XXX: Should destroy remaining bound globals */
5042 if (d->global_handler_remove)
5043 d->global_handler_remove(d, name, global->interface,
5044 global->version, d->user_data);
5046 wl_list_remove(&global->link);
5047 free(global->interface);
5053 display_bind(struct display *display, uint32_t name,
5054 const struct wl_interface *interface, uint32_t version)
5056 return wl_registry_bind(display->registry, name, interface, version);
5059 static const struct wl_registry_listener registry_listener = {
5060 registry_handle_global,
5061 registry_handle_global_remove
5064 #ifdef HAVE_CAIRO_EGL
5066 init_egl(struct display *d)
5068 EGLint major, minor;
5071 #ifdef USE_CAIRO_GLESV2
5072 # define GL_BIT EGL_OPENGL_ES2_BIT
5074 # define GL_BIT EGL_OPENGL_BIT
5077 static const EGLint argb_cfg_attribs[] = {
5078 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5084 EGL_RENDERABLE_TYPE, GL_BIT,
5088 #ifdef USE_CAIRO_GLESV2
5089 static const EGLint context_attribs[] = {
5090 EGL_CONTEXT_CLIENT_VERSION, 2,
5093 EGLint api = EGL_OPENGL_ES_API;
5095 EGLint *context_attribs = NULL;
5096 EGLint api = EGL_OPENGL_API;
5099 d->dpy = eglGetDisplay(d->display);
5100 if (!eglInitialize(d->dpy, &major, &minor)) {
5101 fprintf(stderr, "failed to initialize EGL\n");
5105 if (!eglBindAPI(api)) {
5106 fprintf(stderr, "failed to bind EGL client API\n");
5110 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5111 &d->argb_config, 1, &n) || n != 1) {
5112 fprintf(stderr, "failed to choose argb EGL config\n");
5116 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5117 EGL_NO_CONTEXT, context_attribs);
5118 if (d->argb_ctx == NULL) {
5119 fprintf(stderr, "failed to create EGL context\n");
5123 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5124 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5125 fprintf(stderr, "failed to get cairo EGL argb device\n");
5133 fini_egl(struct display *display)
5135 cairo_device_destroy(display->argb_device);
5137 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5140 eglTerminate(display->dpy);
5146 init_dummy_surface(struct display *display)
5151 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5153 display->dummy_surface =
5154 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5156 display->dummy_surface_data = data;
5160 handle_display_data(struct task *task, uint32_t events)
5162 struct display *display =
5163 container_of(task, struct display, display_task);
5164 struct epoll_event ep;
5167 display->display_fd_events = events;
5169 if (events & EPOLLERR || events & EPOLLHUP) {
5170 display_exit(display);
5174 if (events & EPOLLIN) {
5175 ret = wl_display_dispatch(display->display);
5177 display_exit(display);
5182 if (events & EPOLLOUT) {
5183 ret = wl_display_flush(display->display);
5185 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5186 ep.data.ptr = &display->display_task;
5187 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5188 display->display_fd, &ep);
5189 } else if (ret == -1 && errno != EAGAIN) {
5190 display_exit(display);
5197 log_handler(const char *format, va_list args)
5199 vfprintf(stderr, format, args);
5203 display_create(int *argc, char *argv[])
5207 wl_log_set_handler_client(log_handler);
5209 d = zalloc(sizeof *d);
5213 d->display = wl_display_connect(NULL);
5214 if (d->display == NULL) {
5215 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5220 d->xkb_context = xkb_context_new(0);
5221 if (d->xkb_context == NULL) {
5222 fprintf(stderr, "Failed to create XKB context\n");
5227 d->epoll_fd = os_epoll_create_cloexec();
5228 d->display_fd = wl_display_get_fd(d->display);
5229 d->display_task.run = handle_display_data;
5230 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5233 wl_list_init(&d->deferred_list);
5234 wl_list_init(&d->input_list);
5235 wl_list_init(&d->output_list);
5236 wl_list_init(&d->global_list);
5239 d->workspace_count = 1;
5241 d->registry = wl_display_get_registry(d->display);
5242 wl_registry_add_listener(d->registry, ®istry_listener, d);
5244 if (wl_display_dispatch(d->display) < 0) {
5245 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5249 #ifdef HAVE_CAIRO_EGL
5250 if (init_egl(d) < 0)
5251 fprintf(stderr, "EGL does not seem to work, "
5252 "falling back to software rendering and wl_shm.\n");
5257 d->theme = theme_create();
5259 wl_list_init(&d->window_list);
5261 init_dummy_surface(d);
5267 display_destroy_outputs(struct display *display)
5270 struct output *output;
5272 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5273 output_destroy(output);
5277 display_destroy_inputs(struct display *display)
5280 struct input *input;
5282 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5283 input_destroy(input);
5287 display_destroy(struct display *display)
5289 if (!wl_list_empty(&display->window_list))
5290 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5291 wl_list_length(&display->window_list));
5293 if (!wl_list_empty(&display->deferred_list))
5294 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5296 cairo_surface_destroy(display->dummy_surface);
5297 free(display->dummy_surface_data);
5299 display_destroy_outputs(display);
5300 display_destroy_inputs(display);
5302 xkb_context_unref(display->xkb_context);
5304 theme_destroy(display->theme);
5305 destroy_cursors(display);
5307 #ifdef HAVE_CAIRO_EGL
5308 if (display->argb_device)
5312 if (display->subcompositor)
5313 wl_subcompositor_destroy(display->subcompositor);
5316 wl_shell_destroy(display->shell);
5319 wl_shm_destroy(display->shm);
5321 if (display->data_device_manager)
5322 wl_data_device_manager_destroy(display->data_device_manager);
5324 wl_compositor_destroy(display->compositor);
5325 wl_registry_destroy(display->registry);
5327 close(display->epoll_fd);
5329 if (!(display->display_fd_events & EPOLLERR) &&
5330 !(display->display_fd_events & EPOLLHUP))
5331 wl_display_flush(display->display);
5333 wl_display_disconnect(display->display);
5338 display_set_user_data(struct display *display, void *data)
5340 display->user_data = data;
5344 display_get_user_data(struct display *display)
5346 return display->user_data;
5350 display_get_display(struct display *display)
5352 return display->display;
5356 display_has_subcompositor(struct display *display)
5358 if (display->subcompositor)
5361 wl_display_roundtrip(display->display);
5363 return display->subcompositor != NULL;
5367 display_get_cairo_device(struct display *display)
5369 return display->argb_device;
5373 display_get_output(struct display *display)
5375 return container_of(display->output_list.next, struct output, link);
5378 struct wl_compositor *
5379 display_get_compositor(struct display *display)
5381 return display->compositor;
5385 display_get_serial(struct display *display)
5387 return display->serial;
5391 display_get_egl_display(struct display *d)
5396 struct wl_data_source *
5397 display_create_data_source(struct display *display)
5399 return wl_data_device_manager_create_data_source(display->data_device_manager);
5403 display_get_argb_egl_config(struct display *d)
5405 return d->argb_config;
5409 display_acquire_window_surface(struct display *display,
5410 struct window *window,
5413 struct surface *surface = window->main_surface;
5415 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5418 widget_get_cairo_surface(window->main_surface->widget);
5419 return surface->toysurface->acquire(surface->toysurface, ctx);
5423 display_release_window_surface(struct display *display,
5424 struct window *window)
5426 struct surface *surface = window->main_surface;
5428 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5431 surface->toysurface->release(surface->toysurface);
5435 display_defer(struct display *display, struct task *task)
5437 wl_list_insert(&display->deferred_list, &task->link);
5441 display_watch_fd(struct display *display,
5442 int fd, uint32_t events, struct task *task)
5444 struct epoll_event ep;
5448 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5452 display_unwatch_fd(struct display *display, int fd)
5454 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5458 display_run(struct display *display)
5461 struct epoll_event ep[16];
5464 display->running = 1;
5466 while (!wl_list_empty(&display->deferred_list)) {
5467 task = container_of(display->deferred_list.prev,
5469 wl_list_remove(&task->link);
5473 wl_display_dispatch_pending(display->display);
5475 if (!display->running)
5478 ret = wl_display_flush(display->display);
5479 if (ret < 0 && errno == EAGAIN) {
5481 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5482 ep[0].data.ptr = &display->display_task;
5484 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5485 display->display_fd, &ep[0]);
5486 } else if (ret < 0) {
5490 count = epoll_wait(display->epoll_fd,
5491 ep, ARRAY_LENGTH(ep), -1);
5492 for (i = 0; i < count; i++) {
5493 task = ep[i].data.ptr;
5494 task->run(task, ep[i].events);
5500 display_exit(struct display *display)
5502 display->running = 0;
5506 keysym_modifiers_add(struct wl_array *modifiers_map,
5509 size_t len = strlen(name) + 1;
5512 p = wl_array_add(modifiers_map, len);
5517 strncpy(p, name, len);
5520 static xkb_mod_index_t
5521 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5524 xkb_mod_index_t index = 0;
5525 char *p = modifiers_map->data;
5527 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5528 if (strcmp(p, name) == 0)
5535 return XKB_MOD_INVALID;
5539 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5542 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5544 if (index == XKB_MOD_INVALID)
5545 return XKB_MOD_INVALID;
5551 fail_on_null(void *p)
5554 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5564 return fail_on_null(malloc(s));
5570 return fail_on_null(zalloc(s));
5574 xstrdup(const char *s)
5576 return fail_on_null(strdup(s));
5580 xrealloc(char *p, size_t s)
5582 return fail_on_null(realloc(p, s));