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;
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
332 struct task repeat_task;
336 uint32_t repeat_time;
340 struct display *display;
341 struct wl_output *output;
342 uint32_t server_output_id;
343 struct rectangle allocation;
348 display_output_handler_t destroy_handler;
352 struct window_frame {
353 struct widget *widget;
354 struct widget *child;
359 struct window *window;
360 struct window *parent;
361 struct widget *widget;
364 const char **entries;
373 struct widget *parent;
374 struct window *window;
375 struct widget *widget;
377 struct task tooltip_task;
383 struct wl_shm_pool *pool;
390 CURSOR_DEFAULT = 100,
394 enum window_location {
396 WINDOW_RESIZING_TOP = 1,
397 WINDOW_RESIZING_BOTTOM = 2,
398 WINDOW_RESIZING_LEFT = 4,
399 WINDOW_RESIZING_TOP_LEFT = 5,
400 WINDOW_RESIZING_BOTTOM_LEFT = 6,
401 WINDOW_RESIZING_RIGHT = 8,
402 WINDOW_RESIZING_TOP_RIGHT = 9,
403 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
404 WINDOW_RESIZING_MASK = 15,
405 WINDOW_EXTERIOR = 16,
406 WINDOW_TITLEBAR = 17,
407 WINDOW_CLIENT_AREA = 18,
410 static const cairo_user_data_key_t shm_surface_data_key;
417 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
418 __attribute__ ((format (printf, 4, 5)));
421 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
426 gettimeofday(&tv, NULL);
427 fprintf(stderr, "%8ld.%03ld ",
428 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
431 fprintf(stderr, "%s@%d ",
432 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
434 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
435 fprintf(stderr, "%s ", func);
438 vfprintf(stderr, fmt, ap);
442 #define DBG(fmt, ...) \
443 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
445 #define DBG_OBJ(obj, fmt, ...) \
446 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
450 #define DBG(...) do {} while (0)
451 #define DBG_OBJ(...) do {} while (0)
456 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
460 switch (buffer_transform) {
461 case WL_OUTPUT_TRANSFORM_90:
462 case WL_OUTPUT_TRANSFORM_270:
463 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
464 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
473 *width *= buffer_scale;
474 *height *= buffer_scale;
478 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
482 switch (buffer_transform) {
483 case WL_OUTPUT_TRANSFORM_90:
484 case WL_OUTPUT_TRANSFORM_270:
485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
486 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
495 *width /= buffer_scale;
496 *height /= buffer_scale;
499 #ifdef HAVE_CAIRO_EGL
501 struct egl_window_surface {
502 struct toysurface base;
503 cairo_surface_t *cairo_surface;
504 struct display *display;
505 struct wl_surface *surface;
506 struct wl_egl_window *egl_window;
507 EGLSurface egl_surface;
510 static struct egl_window_surface *
511 to_egl_window_surface(struct toysurface *base)
513 return container_of(base, struct egl_window_surface, base);
516 static cairo_surface_t *
517 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
518 int32_t width, int32_t height, uint32_t flags,
519 enum wl_output_transform buffer_transform, int32_t buffer_scale)
521 struct egl_window_surface *surface = to_egl_window_surface(base);
523 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
525 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
526 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
528 return cairo_surface_reference(surface->cairo_surface);
532 egl_window_surface_swap(struct toysurface *base,
533 enum wl_output_transform buffer_transform, int32_t buffer_scale,
534 struct rectangle *server_allocation)
536 struct egl_window_surface *surface = to_egl_window_surface(base);
538 cairo_gl_surface_swapbuffers(surface->cairo_surface);
539 wl_egl_window_get_attached_size(surface->egl_window,
540 &server_allocation->width,
541 &server_allocation->height);
543 buffer_to_surface_size (buffer_transform, buffer_scale,
544 &server_allocation->width,
545 &server_allocation->height);
549 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
551 struct egl_window_surface *surface = to_egl_window_surface(base);
552 cairo_device_t *device;
554 device = cairo_surface_get_device(surface->cairo_surface);
559 if (device == surface->display->argb_device)
560 ctx = surface->display->argb_ctx;
565 cairo_device_flush(device);
566 cairo_device_acquire(device);
567 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
568 surface->egl_surface, ctx))
569 fprintf(stderr, "failed to make surface current\n");
575 egl_window_surface_release(struct toysurface *base)
577 struct egl_window_surface *surface = to_egl_window_surface(base);
578 cairo_device_t *device;
580 device = cairo_surface_get_device(surface->cairo_surface);
584 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
585 surface->display->argb_ctx))
586 fprintf(stderr, "failed to make context current\n");
588 cairo_device_release(device);
592 egl_window_surface_destroy(struct toysurface *base)
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 struct display *d = surface->display;
597 cairo_surface_destroy(surface->cairo_surface);
598 eglDestroySurface(d->dpy, surface->egl_surface);
599 wl_egl_window_destroy(surface->egl_window);
600 surface->surface = NULL;
605 static struct toysurface *
606 egl_window_surface_create(struct display *display,
607 struct wl_surface *wl_surface,
609 struct rectangle *rectangle)
611 struct egl_window_surface *surface;
613 if (display->dpy == EGL_NO_DISPLAY)
616 surface = calloc(1, sizeof *surface);
620 surface->base.prepare = egl_window_surface_prepare;
621 surface->base.swap = egl_window_surface_swap;
622 surface->base.acquire = egl_window_surface_acquire;
623 surface->base.release = egl_window_surface_release;
624 surface->base.destroy = egl_window_surface_destroy;
626 surface->display = display;
627 surface->surface = wl_surface;
629 surface->egl_window = wl_egl_window_create(surface->surface,
633 surface->egl_surface = eglCreateWindowSurface(display->dpy,
634 display->argb_config,
638 surface->cairo_surface =
639 cairo_gl_surface_create_for_egl(display->argb_device,
640 surface->egl_surface,
644 return &surface->base;
649 static struct toysurface *
650 egl_window_surface_create(struct display *display,
651 struct wl_surface *wl_surface,
653 struct rectangle *rectangle)
660 struct shm_surface_data {
661 struct wl_buffer *buffer;
662 struct shm_pool *pool;
666 display_get_buffer_for_surface(struct display *display,
667 cairo_surface_t *surface)
669 struct shm_surface_data *data;
671 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
677 shm_pool_destroy(struct shm_pool *pool);
680 shm_surface_data_destroy(void *p)
682 struct shm_surface_data *data = p;
684 wl_buffer_destroy(data->buffer);
686 shm_pool_destroy(data->pool);
691 static struct wl_shm_pool *
692 make_shm_pool(struct display *display, int size, void **data)
694 struct wl_shm_pool *pool;
697 fd = os_create_anonymous_file(size);
699 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
704 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
705 if (*data == MAP_FAILED) {
706 fprintf(stderr, "mmap failed: %m\n");
711 pool = wl_shm_create_pool(display->shm, fd, size);
718 static struct shm_pool *
719 shm_pool_create(struct display *display, size_t size)
721 struct shm_pool *pool = malloc(sizeof *pool);
726 pool->pool = make_shm_pool(display, size, &pool->data);
739 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
741 if (pool->used + size > pool->size)
744 *offset = pool->used;
747 return (char *) pool->data + *offset;
750 /* destroy the pool. this does not unmap the memory though */
752 shm_pool_destroy(struct shm_pool *pool)
754 munmap(pool->data, pool->size);
755 wl_shm_pool_destroy(pool->pool);
759 /* Start allocating from the beginning of the pool again */
761 shm_pool_reset(struct shm_pool *pool)
767 data_length_for_shm_surface(struct rectangle *rect)
771 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
773 return stride * rect->height;
776 static cairo_surface_t *
777 display_create_shm_surface_from_pool(struct display *display,
778 struct rectangle *rectangle,
779 uint32_t flags, struct shm_pool *pool)
781 struct shm_surface_data *data;
783 cairo_surface_t *surface;
784 cairo_format_t cairo_format;
785 int stride, length, offset;
788 data = malloc(sizeof *data);
792 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
793 cairo_format = CAIRO_FORMAT_RGB16_565;
795 cairo_format = CAIRO_FORMAT_ARGB32;
797 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
798 length = stride * rectangle->height;
800 map = shm_pool_allocate(pool, length, &offset);
807 surface = cairo_image_surface_create_for_data (map,
813 cairo_surface_set_user_data(surface, &shm_surface_data_key,
814 data, shm_surface_data_destroy);
816 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
817 format = WL_SHM_FORMAT_RGB565;
819 if (flags & SURFACE_OPAQUE)
820 format = WL_SHM_FORMAT_XRGB8888;
822 format = WL_SHM_FORMAT_ARGB8888;
825 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
833 static cairo_surface_t *
834 display_create_shm_surface(struct display *display,
835 struct rectangle *rectangle, uint32_t flags,
836 struct shm_pool *alternate_pool,
837 struct shm_surface_data **data_ret)
839 struct shm_surface_data *data;
840 struct shm_pool *pool;
841 cairo_surface_t *surface;
843 if (alternate_pool) {
844 shm_pool_reset(alternate_pool);
845 surface = display_create_shm_surface_from_pool(display,
850 data = cairo_surface_get_user_data(surface,
851 &shm_surface_data_key);
856 pool = shm_pool_create(display,
857 data_length_for_shm_surface(rectangle));
862 display_create_shm_surface_from_pool(display, rectangle,
866 shm_pool_destroy(pool);
870 /* make sure we destroy the pool when the surface is destroyed */
871 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
882 check_size(struct rectangle *rect)
884 if (rect->width && rect->height)
887 fprintf(stderr, "tried to create surface of "
888 "width: %d, height: %d\n", rect->width, rect->height);
893 display_create_surface(struct display *display,
894 struct wl_surface *surface,
895 struct rectangle *rectangle,
898 if (check_size(rectangle) < 0)
901 assert(flags & SURFACE_SHM);
902 return display_create_shm_surface(display, rectangle, flags,
906 struct shm_surface_leaf {
907 cairo_surface_t *cairo_surface;
908 /* 'data' is automatically destroyed, when 'cairo_surface' is */
909 struct shm_surface_data *data;
911 struct shm_pool *resize_pool;
916 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
918 if (leaf->cairo_surface)
919 cairo_surface_destroy(leaf->cairo_surface);
920 /* leaf->data already destroyed via cairo private */
922 if (leaf->resize_pool)
923 shm_pool_destroy(leaf->resize_pool);
925 memset(leaf, 0, sizeof *leaf);
931 struct toysurface base;
932 struct display *display;
933 struct wl_surface *surface;
937 struct shm_surface_leaf leaf[MAX_LEAVES];
938 struct shm_surface_leaf *current;
941 static struct shm_surface *
942 to_shm_surface(struct toysurface *base)
944 return container_of(base, struct shm_surface, base);
948 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
951 struct shm_surface_leaf *leaf;
952 char bufs[MAX_LEAVES + 1];
955 for (i = 0; i < MAX_LEAVES; i++) {
956 leaf = &surface->leaf[i];
960 else if (leaf->cairo_surface)
966 bufs[MAX_LEAVES] = '\0';
967 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
972 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
974 struct shm_surface *surface = data;
975 struct shm_surface_leaf *leaf;
979 shm_surface_buffer_state_debug(surface, "buffer_release before");
981 for (i = 0; i < MAX_LEAVES; i++) {
982 leaf = &surface->leaf[i];
983 if (leaf->data && leaf->data->buffer == buffer) {
988 assert(i < MAX_LEAVES && "unknown buffer released");
990 /* Leave one free leaf with storage, release others */
992 for (i = 0; i < MAX_LEAVES; i++) {
993 leaf = &surface->leaf[i];
995 if (!leaf->cairo_surface || leaf->busy)
1001 shm_surface_leaf_release(leaf);
1004 shm_surface_buffer_state_debug(surface, "buffer_release after");
1007 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1008 shm_surface_buffer_release
1011 static cairo_surface_t *
1012 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1013 int32_t width, int32_t height, uint32_t flags,
1014 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1016 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1017 struct shm_surface *surface = to_shm_surface(base);
1018 struct rectangle rect = { 0};
1019 struct shm_surface_leaf *leaf = NULL;
1025 /* pick a free buffer, preferrably one that already has storage */
1026 for (i = 0; i < MAX_LEAVES; i++) {
1027 if (surface->leaf[i].busy)
1030 if (!leaf || surface->leaf[i].cairo_surface)
1031 leaf = &surface->leaf[i];
1033 DBG_OBJ(surface->surface, "pick leaf %d\n",
1034 (int)(leaf - &surface->leaf[0]));
1037 fprintf(stderr, "%s: all buffers are held by the server.\n",
1043 if (!resize_hint && leaf->resize_pool) {
1044 cairo_surface_destroy(leaf->cairo_surface);
1045 leaf->cairo_surface = NULL;
1046 shm_pool_destroy(leaf->resize_pool);
1047 leaf->resize_pool = NULL;
1050 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1052 if (leaf->cairo_surface &&
1053 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1054 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1057 if (leaf->cairo_surface)
1058 cairo_surface_destroy(leaf->cairo_surface);
1060 #ifdef USE_RESIZE_POOL
1061 if (resize_hint && !leaf->resize_pool) {
1062 /* Create a big pool to allocate from, while continuously
1063 * resizing. Mmapping a new pool in the server
1064 * is relatively expensive, so reusing a pool performs
1065 * better, but may temporarily reserve unneeded memory.
1067 /* We should probably base this number on the output size. */
1068 leaf->resize_pool = shm_pool_create(surface->display,
1074 rect.height = height;
1076 leaf->cairo_surface =
1077 display_create_shm_surface(surface->display, &rect,
1081 wl_buffer_add_listener(leaf->data->buffer,
1082 &shm_surface_buffer_listener, surface);
1085 surface->current = leaf;
1087 return cairo_surface_reference(leaf->cairo_surface);
1091 shm_surface_swap(struct toysurface *base,
1092 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1093 struct rectangle *server_allocation)
1095 struct shm_surface *surface = to_shm_surface(base);
1096 struct shm_surface_leaf *leaf = surface->current;
1098 server_allocation->width =
1099 cairo_image_surface_get_width(leaf->cairo_surface);
1100 server_allocation->height =
1101 cairo_image_surface_get_height(leaf->cairo_surface);
1103 buffer_to_surface_size (buffer_transform, buffer_scale,
1104 &server_allocation->width,
1105 &server_allocation->height);
1107 wl_surface_attach(surface->surface, leaf->data->buffer,
1108 surface->dx, surface->dy);
1109 wl_surface_damage(surface->surface, 0, 0,
1110 server_allocation->width, server_allocation->height);
1111 wl_surface_commit(surface->surface);
1113 DBG_OBJ(surface->surface, "leaf %d busy\n",
1114 (int)(leaf - &surface->leaf[0]));
1117 surface->current = NULL;
1121 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1127 shm_surface_release(struct toysurface *base)
1132 shm_surface_destroy(struct toysurface *base)
1134 struct shm_surface *surface = to_shm_surface(base);
1137 for (i = 0; i < MAX_LEAVES; i++)
1138 shm_surface_leaf_release(&surface->leaf[i]);
1143 static struct toysurface *
1144 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1145 uint32_t flags, struct rectangle *rectangle)
1147 struct shm_surface *surface;
1148 DBG_OBJ(wl_surface, "\n");
1150 surface = xmalloc(sizeof *surface);
1151 memset(surface, 0, sizeof *surface);
1156 surface->base.prepare = shm_surface_prepare;
1157 surface->base.swap = shm_surface_swap;
1158 surface->base.acquire = shm_surface_acquire;
1159 surface->base.release = shm_surface_release;
1160 surface->base.destroy = shm_surface_destroy;
1162 surface->display = display;
1163 surface->surface = wl_surface;
1164 surface->flags = flags;
1166 return &surface->base;
1170 * The following correspondences between file names and cursors was copied
1171 * from: https://bugs.kde.org/attachment.cgi?id=67313
1174 static const char *bottom_left_corners[] = {
1175 "bottom_left_corner",
1180 static const char *bottom_right_corners[] = {
1181 "bottom_right_corner",
1186 static const char *bottom_sides[] = {
1192 static const char *grabbings[] = {
1195 "208530c400c041818281048008011002"
1198 static const char *left_ptrs[] = {
1205 static const char *left_sides[] = {
1211 static const char *right_sides[] = {
1217 static const char *top_left_corners[] = {
1223 static const char *top_right_corners[] = {
1229 static const char *top_sides[] = {
1235 static const char *xterms[] = {
1241 static const char *hand1s[] = {
1245 "e29285e634086352946a0e7090d73106"
1248 static const char *watches[] = {
1251 "0426c94ea35c87780ff01dc239897213"
1254 struct cursor_alternatives {
1259 static const struct cursor_alternatives cursors[] = {
1260 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1261 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1262 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1263 {grabbings, ARRAY_LENGTH(grabbings)},
1264 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1265 {left_sides, ARRAY_LENGTH(left_sides)},
1266 {right_sides, ARRAY_LENGTH(right_sides)},
1267 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1268 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1269 {top_sides, ARRAY_LENGTH(top_sides)},
1270 {xterms, ARRAY_LENGTH(xterms)},
1271 {hand1s, ARRAY_LENGTH(hand1s)},
1272 {watches, ARRAY_LENGTH(watches)},
1276 create_cursors(struct display *display)
1278 struct weston_config *config;
1279 struct weston_config_section *s;
1283 struct wl_cursor *cursor;
1285 config = weston_config_parse("weston.ini");
1286 s = weston_config_get_section(config, "shell", NULL, NULL);
1287 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1288 weston_config_section_get_int(s, "cursor-size", &size, 32);
1289 weston_config_destroy(config);
1291 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1294 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1296 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1298 for (j = 0; !cursor && j < cursors[i].count; ++j)
1299 cursor = wl_cursor_theme_get_cursor(
1300 display->cursor_theme, cursors[i].names[j]);
1303 fprintf(stderr, "could not load cursor '%s'\n",
1304 cursors[i].names[0]);
1306 display->cursors[i] = cursor;
1311 destroy_cursors(struct display *display)
1313 wl_cursor_theme_destroy(display->cursor_theme);
1314 free(display->cursors);
1317 struct wl_cursor_image *
1318 display_get_pointer_image(struct display *display, int pointer)
1320 struct wl_cursor *cursor = display->cursors[pointer];
1322 return cursor ? cursor->images[0] : NULL;
1326 surface_flush(struct surface *surface)
1328 if (!surface->cairo_surface)
1331 if (surface->opaque_region) {
1332 wl_surface_set_opaque_region(surface->surface,
1333 surface->opaque_region);
1334 wl_region_destroy(surface->opaque_region);
1335 surface->opaque_region = NULL;
1338 if (surface->input_region) {
1339 wl_surface_set_input_region(surface->surface,
1340 surface->input_region);
1341 wl_region_destroy(surface->input_region);
1342 surface->input_region = NULL;
1345 surface->toysurface->swap(surface->toysurface,
1346 surface->buffer_transform, surface->buffer_scale,
1347 &surface->server_allocation);
1349 cairo_surface_destroy(surface->cairo_surface);
1350 surface->cairo_surface = NULL;
1354 window_has_focus(struct window *window)
1356 return window->focus_count > 0;
1360 window_flush(struct window *window)
1362 struct surface *surface;
1364 if (window->type == TYPE_NONE) {
1365 window->type = TYPE_TOPLEVEL;
1366 if (window->shell_surface)
1367 wl_shell_surface_set_toplevel(window->shell_surface);
1370 wl_list_for_each(surface, &window->subsurface_list, link) {
1371 if (surface == window->main_surface)
1374 surface_flush(surface);
1377 surface_flush(window->main_surface);
1381 window_get_display(struct window *window)
1383 return window->display;
1387 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1389 struct display *display = surface->window->display;
1390 struct rectangle allocation = surface->allocation;
1392 if (!surface->toysurface && display->dpy &&
1393 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1394 surface->toysurface =
1395 egl_window_surface_create(display,
1401 if (!surface->toysurface)
1402 surface->toysurface = shm_surface_create(display,
1404 flags, &allocation);
1406 surface->cairo_surface = surface->toysurface->prepare(
1407 surface->toysurface, dx, dy,
1408 allocation.width, allocation.height, flags,
1409 surface->buffer_transform, surface->buffer_scale);
1413 window_create_main_surface(struct window *window)
1415 struct surface *surface = window->main_surface;
1420 if (window->resizing)
1421 flags |= SURFACE_HINT_RESIZE;
1423 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1424 flags |= SURFACE_HINT_RGB565;
1426 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1427 dx = surface->server_allocation.width -
1428 surface->allocation.width;
1430 if (window->resize_edges & WINDOW_RESIZING_TOP)
1431 dy = surface->server_allocation.height -
1432 surface->allocation.height;
1434 window->resize_edges = 0;
1436 surface_create_surface(surface, dx, dy, flags);
1440 window_get_buffer_transform(struct window *window)
1442 return window->main_surface->buffer_transform;
1446 window_set_buffer_transform(struct window *window,
1447 enum wl_output_transform transform)
1449 window->main_surface->buffer_transform = transform;
1450 wl_surface_set_buffer_transform(window->main_surface->surface,
1455 window_set_buffer_scale(struct window *window,
1458 window->main_surface->buffer_scale = scale;
1459 wl_surface_set_buffer_scale(window->main_surface->surface,
1464 window_get_buffer_scale(struct window *window)
1466 return window->main_surface->buffer_scale;
1470 window_get_output_scale(struct window *window)
1472 struct window_output *window_output;
1473 struct window_output *window_output_tmp;
1476 wl_list_for_each_safe(window_output, window_output_tmp,
1477 &window->window_output_list, link) {
1478 if (window_output->output->scale > scale)
1479 scale = window_output->output->scale;
1485 static void window_frame_destroy(struct window_frame *frame);
1488 surface_destroy(struct surface *surface)
1490 if (surface->frame_cb)
1491 wl_callback_destroy(surface->frame_cb);
1493 if (surface->input_region)
1494 wl_region_destroy(surface->input_region);
1496 if (surface->opaque_region)
1497 wl_region_destroy(surface->opaque_region);
1499 if (surface->subsurface)
1500 wl_subsurface_destroy(surface->subsurface);
1502 wl_surface_destroy(surface->surface);
1504 if (surface->toysurface)
1505 surface->toysurface->destroy(surface->toysurface);
1507 wl_list_remove(&surface->link);
1512 window_destroy(struct window *window)
1514 struct display *display = window->display;
1515 struct input *input;
1516 struct window_output *window_output;
1517 struct window_output *window_output_tmp;
1519 wl_list_remove(&window->redraw_task.link);
1521 wl_list_for_each(input, &display->input_list, link) {
1522 if (input->touch_focus == window)
1523 input->touch_focus = NULL;
1524 if (input->pointer_focus == window)
1525 input->pointer_focus = NULL;
1526 if (input->keyboard_focus == window)
1527 input->keyboard_focus = NULL;
1528 if (input->focus_widget &&
1529 input->focus_widget->window == window)
1530 input->focus_widget = NULL;
1533 wl_list_for_each_safe(window_output, window_output_tmp,
1534 &window->window_output_list, link) {
1535 free (window_output);
1539 window_frame_destroy(window->frame);
1541 if (window->shell_surface)
1542 wl_shell_surface_destroy(window->shell_surface);
1544 surface_destroy(window->main_surface);
1546 wl_list_remove(&window->link);
1548 free(window->title);
1552 static struct widget *
1553 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1555 struct widget *child, *target;
1557 wl_list_for_each(child, &widget->child_list, link) {
1558 target = widget_find_widget(child, x, y);
1563 if (widget->allocation.x <= x &&
1564 x < widget->allocation.x + widget->allocation.width &&
1565 widget->allocation.y <= y &&
1566 y < widget->allocation.y + widget->allocation.height) {
1573 static struct widget *
1574 window_find_widget(struct window *window, int32_t x, int32_t y)
1576 struct surface *surface;
1577 struct widget *widget;
1579 wl_list_for_each(surface, &window->subsurface_list, link) {
1580 widget = widget_find_widget(surface->widget, x, y);
1588 static struct widget *
1589 widget_create(struct window *window, struct surface *surface, void *data)
1591 struct widget *widget;
1593 widget = xzalloc(sizeof *widget);
1594 widget->window = window;
1595 widget->surface = surface;
1596 widget->user_data = data;
1597 widget->allocation = surface->allocation;
1598 wl_list_init(&widget->child_list);
1600 widget->tooltip = NULL;
1601 widget->tooltip_count = 0;
1602 widget->default_cursor = CURSOR_LEFT_PTR;
1608 window_add_widget(struct window *window, void *data)
1610 struct widget *widget;
1612 widget = widget_create(window, window->main_surface, data);
1613 wl_list_init(&widget->link);
1614 window->main_surface->widget = widget;
1620 widget_add_widget(struct widget *parent, void *data)
1622 struct widget *widget;
1624 widget = widget_create(parent->window, parent->surface, data);
1625 wl_list_insert(parent->child_list.prev, &widget->link);
1631 widget_destroy(struct widget *widget)
1633 struct display *display = widget->window->display;
1634 struct surface *surface = widget->surface;
1635 struct input *input;
1637 /* Destroy the sub-surface along with the root widget */
1638 if (surface->widget == widget && surface->subsurface)
1639 surface_destroy(widget->surface);
1641 if (widget->tooltip) {
1642 free(widget->tooltip);
1643 widget->tooltip = NULL;
1646 wl_list_for_each(input, &display->input_list, link) {
1647 if (input->focus_widget == widget)
1648 input->focus_widget = NULL;
1651 wl_list_remove(&widget->link);
1656 widget_set_default_cursor(struct widget *widget, int cursor)
1658 widget->default_cursor = cursor;
1662 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1664 *allocation = widget->allocation;
1668 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1670 widget->allocation.width = width;
1671 widget->allocation.height = height;
1675 widget_set_allocation(struct widget *widget,
1676 int32_t x, int32_t y, int32_t width, int32_t height)
1678 widget->allocation.x = x;
1679 widget->allocation.y = y;
1680 widget_set_size(widget, width, height);
1684 widget_set_transparent(struct widget *widget, int transparent)
1686 widget->opaque = !transparent;
1690 widget_get_user_data(struct widget *widget)
1692 return widget->user_data;
1695 static cairo_surface_t *
1696 widget_get_cairo_surface(struct widget *widget)
1698 struct surface *surface = widget->surface;
1699 struct window *window = widget->window;
1701 if (!surface->cairo_surface) {
1702 if (surface == window->main_surface)
1703 window_create_main_surface(window);
1705 surface_create_surface(surface, 0, 0, 0);
1708 return surface->cairo_surface;
1712 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1714 struct surface *surface = widget->surface;
1717 enum wl_output_transform transform;
1718 int surface_width, surface_height;
1719 int translate_x, translate_y;
1722 surface_width = surface->allocation.width;
1723 surface_height = surface->allocation.height;
1725 transform = surface->buffer_transform;
1726 scale = surface->buffer_scale;
1728 switch (transform) {
1729 case WL_OUTPUT_TRANSFORM_FLIPPED:
1730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1731 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1732 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1733 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1736 cairo_matrix_init_identity(&m);
1740 switch (transform) {
1741 case WL_OUTPUT_TRANSFORM_NORMAL:
1747 case WL_OUTPUT_TRANSFORM_FLIPPED:
1749 translate_x = surface_width;
1752 case WL_OUTPUT_TRANSFORM_90:
1754 translate_x = surface_height;
1757 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1759 translate_x = surface_height;
1760 translate_y = surface_width;
1762 case WL_OUTPUT_TRANSFORM_180:
1764 translate_x = surface_width;
1765 translate_y = surface_height;
1767 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1770 translate_y = surface_height;
1772 case WL_OUTPUT_TRANSFORM_270:
1773 angle = M_PI + M_PI_2;
1775 translate_y = surface_width;
1777 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1778 angle = M_PI + M_PI_2;
1784 cairo_scale(cr, scale, scale);
1785 cairo_translate(cr, translate_x, translate_y);
1786 cairo_rotate(cr, angle);
1787 cairo_transform(cr, &m);
1791 widget_cairo_create(struct widget *widget)
1793 struct surface *surface = widget->surface;
1794 cairo_surface_t *cairo_surface;
1797 cairo_surface = widget_get_cairo_surface(widget);
1798 cr = cairo_create(cairo_surface);
1800 widget_cairo_update_transform(widget, cr);
1802 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1808 widget_get_wl_surface(struct widget *widget)
1810 return widget->surface->surface;
1814 widget_get_last_time(struct widget *widget)
1816 return widget->surface->last_time;
1820 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1822 struct wl_compositor *comp = widget->window->display->compositor;
1823 struct surface *surface = widget->surface;
1825 if (!surface->input_region)
1826 surface->input_region = wl_compositor_create_region(comp);
1829 wl_region_add(surface->input_region,
1830 rect->x, rect->y, rect->width, rect->height);
1835 widget_set_resize_handler(struct widget *widget,
1836 widget_resize_handler_t handler)
1838 widget->resize_handler = handler;
1842 widget_set_redraw_handler(struct widget *widget,
1843 widget_redraw_handler_t handler)
1845 widget->redraw_handler = handler;
1849 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1851 widget->enter_handler = handler;
1855 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1857 widget->leave_handler = handler;
1861 widget_set_motion_handler(struct widget *widget,
1862 widget_motion_handler_t handler)
1864 widget->motion_handler = handler;
1868 widget_set_button_handler(struct widget *widget,
1869 widget_button_handler_t handler)
1871 widget->button_handler = handler;
1875 widget_set_touch_up_handler(struct widget *widget,
1876 widget_touch_up_handler_t handler)
1878 widget->touch_up_handler = handler;
1882 widget_set_touch_down_handler(struct widget *widget,
1883 widget_touch_down_handler_t handler)
1885 widget->touch_down_handler = handler;
1889 widget_set_touch_motion_handler(struct widget *widget,
1890 widget_touch_motion_handler_t handler)
1892 widget->touch_motion_handler = handler;
1896 widget_set_touch_frame_handler(struct widget *widget,
1897 widget_touch_frame_handler_t handler)
1899 widget->touch_frame_handler = handler;
1903 widget_set_touch_cancel_handler(struct widget *widget,
1904 widget_touch_cancel_handler_t handler)
1906 widget->touch_cancel_handler = handler;
1910 widget_set_axis_handler(struct widget *widget,
1911 widget_axis_handler_t handler)
1913 widget->axis_handler = handler;
1917 window_schedule_redraw_task(struct window *window);
1920 widget_schedule_redraw(struct widget *widget)
1922 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1923 widget->surface->redraw_needed = 1;
1924 window_schedule_redraw_task(widget->window);
1928 window_get_surface(struct window *window)
1930 cairo_surface_t *cairo_surface;
1932 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1934 return cairo_surface_reference(cairo_surface);
1938 window_get_wl_surface(struct window *window)
1940 return window->main_surface->surface;
1944 tooltip_redraw_handler(struct widget *widget, void *data)
1947 const int32_t r = 3;
1948 struct tooltip *tooltip = data;
1949 int32_t width, height;
1951 cr = widget_cairo_create(widget);
1952 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1953 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1956 width = widget->allocation.width;
1957 height = widget->allocation.height;
1958 rounded_rect(cr, 0, 0, width, height, r);
1960 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1961 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1964 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1965 cairo_move_to(cr, 10, 16);
1966 cairo_show_text(cr, tooltip->entry);
1970 static cairo_text_extents_t
1971 get_text_extents(struct tooltip *tooltip)
1974 cairo_text_extents_t extents;
1976 /* Use the dummy_surface because tooltip's surface was not
1977 * created yet, and parent does not have a valid surface
1978 * outside repaint, either.
1980 cr = cairo_create(tooltip->window->display->dummy_surface);
1981 cairo_text_extents(cr, tooltip->entry, &extents);
1988 window_create_tooltip(struct tooltip *tooltip)
1990 struct widget *parent = tooltip->parent;
1991 struct display *display = parent->window->display;
1992 struct window *window;
1993 const int offset_y = 27;
1994 const int margin = 3;
1995 cairo_text_extents_t extents;
1997 if (tooltip->widget)
2000 window = window_create_transient(display, parent->window, tooltip->x,
2001 tooltip->y + offset_y,
2002 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2006 tooltip->window = window;
2007 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2009 extents = get_text_extents(tooltip);
2010 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2011 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2017 widget_destroy_tooltip(struct widget *parent)
2019 struct tooltip *tooltip = parent->tooltip;
2021 parent->tooltip_count = 0;
2025 if (tooltip->widget) {
2026 widget_destroy(tooltip->widget);
2027 window_destroy(tooltip->window);
2028 tooltip->widget = NULL;
2029 tooltip->window = NULL;
2032 close(tooltip->tooltip_fd);
2033 free(tooltip->entry);
2035 parent->tooltip = NULL;
2039 tooltip_func(struct task *task, uint32_t events)
2041 struct tooltip *tooltip =
2042 container_of(task, struct tooltip, tooltip_task);
2045 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2047 window_create_tooltip(tooltip);
2050 #define TOOLTIP_TIMEOUT 500
2052 tooltip_timer_reset(struct tooltip *tooltip)
2054 struct itimerspec its;
2056 its.it_interval.tv_sec = 0;
2057 its.it_interval.tv_nsec = 0;
2058 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2059 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2060 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2061 fprintf(stderr, "could not set timerfd\n: %m");
2069 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2071 struct tooltip *tooltip = parent->tooltip;
2073 parent->tooltip_count++;
2077 tooltip_timer_reset(tooltip);
2081 /* the handler might be triggered too fast via input device motion, so
2082 * we need this check here to make sure tooltip is fully initialized */
2083 if (parent->tooltip_count > 1)
2086 tooltip = malloc(sizeof *tooltip);
2090 parent->tooltip = tooltip;
2091 tooltip->parent = parent;
2092 tooltip->widget = NULL;
2093 tooltip->window = NULL;
2096 tooltip->entry = strdup(entry);
2097 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2098 if (tooltip->tooltip_fd < 0) {
2099 fprintf(stderr, "could not create timerfd\n: %m");
2103 tooltip->tooltip_task.run = tooltip_func;
2104 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2105 EPOLLIN, &tooltip->tooltip_task);
2106 tooltip_timer_reset(tooltip);
2112 workspace_manager_state(void *data,
2113 struct workspace_manager *workspace_manager,
2117 struct display *display = data;
2119 display->workspace = current;
2120 display->workspace_count = count;
2123 static const struct workspace_manager_listener workspace_manager_listener = {
2124 workspace_manager_state
2128 frame_resize_handler(struct widget *widget,
2129 int32_t width, int32_t height, void *data)
2131 struct window_frame *frame = data;
2132 struct widget *child = frame->child;
2133 struct rectangle interior;
2134 struct rectangle input;
2135 struct rectangle opaque;
2137 if (widget->window->type == TYPE_FULLSCREEN) {
2140 interior.width = width;
2141 interior.height = height;
2143 if (widget->window->type == TYPE_MAXIMIZED) {
2144 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2146 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2149 frame_resize(frame->frame, width, height);
2150 frame_interior(frame->frame, &interior.x, &interior.y,
2151 &interior.width, &interior.height);
2154 widget_set_allocation(child, interior.x, interior.y,
2155 interior.width, interior.height);
2157 if (child->resize_handler) {
2158 child->resize_handler(child, interior.width, interior.height,
2161 if (widget->window->type == TYPE_FULLSCREEN) {
2162 width = child->allocation.width;
2163 height = child->allocation.height;
2165 frame_resize_inside(frame->frame,
2166 child->allocation.width,
2167 child->allocation.height);
2168 width = frame_width(frame->frame);
2169 height = frame_height(frame->frame);
2173 widget_set_allocation(widget, 0, 0, width, height);
2175 widget->surface->input_region =
2176 wl_compositor_create_region(widget->window->display->compositor);
2177 if (widget->window->type != TYPE_FULLSCREEN) {
2178 frame_input_rect(frame->frame, &input.x, &input.y,
2179 &input.width, &input.height);
2180 wl_region_add(widget->surface->input_region,
2181 input.x, input.y, input.width, input.height);
2183 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2186 widget_set_allocation(widget, 0, 0, width, height);
2188 if (child->opaque) {
2189 if (widget->window->type != TYPE_FULLSCREEN) {
2190 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2191 &opaque.width, &opaque.height);
2193 wl_region_add(widget->surface->opaque_region,
2195 opaque.width, opaque.height);
2197 wl_region_add(widget->surface->opaque_region,
2198 0, 0, width, height);
2203 widget_schedule_redraw(widget);
2207 frame_redraw_handler(struct widget *widget, void *data)
2210 struct window_frame *frame = data;
2211 struct window *window = widget->window;
2213 if (window->type == TYPE_FULLSCREEN)
2216 if (window->focus_count) {
2217 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2219 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2222 cr = widget_cairo_create(widget);
2224 frame_repaint(frame->frame, cr);
2230 frame_get_pointer_image_for_location(struct window_frame *frame,
2231 enum theme_location location)
2233 struct window *window = frame->widget->window;
2235 if (window->type != TYPE_TOPLEVEL)
2236 return CURSOR_LEFT_PTR;
2239 case THEME_LOCATION_RESIZING_TOP:
2241 case THEME_LOCATION_RESIZING_BOTTOM:
2242 return CURSOR_BOTTOM;
2243 case THEME_LOCATION_RESIZING_LEFT:
2245 case THEME_LOCATION_RESIZING_RIGHT:
2246 return CURSOR_RIGHT;
2247 case THEME_LOCATION_RESIZING_TOP_LEFT:
2248 return CURSOR_TOP_LEFT;
2249 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2250 return CURSOR_TOP_RIGHT;
2251 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2252 return CURSOR_BOTTOM_LEFT;
2253 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2254 return CURSOR_BOTTOM_RIGHT;
2255 case THEME_LOCATION_EXTERIOR:
2256 case THEME_LOCATION_TITLEBAR:
2258 return CURSOR_LEFT_PTR;
2263 frame_menu_func(struct window *window,
2264 struct input *input, int index, void *data)
2266 struct display *display;
2270 if (window->close_handler)
2271 window->close_handler(window->user_data);
2273 display_exit(window->display);
2275 case 1: /* move to workspace above */
2276 display = window->display;
2277 if (display->workspace > 0)
2278 workspace_manager_move_surface(
2279 display->workspace_manager,
2280 window->main_surface->surface,
2281 display->workspace - 1);
2283 case 2: /* move to workspace below */
2284 display = window->display;
2285 if (display->workspace < display->workspace_count - 1)
2286 workspace_manager_move_surface(
2287 display->workspace_manager,
2288 window->main_surface->surface,
2289 display->workspace + 1);
2291 case 3: /* fullscreen */
2292 /* we don't have a way to get out of fullscreen for now */
2293 if (window->fullscreen_handler)
2294 window->fullscreen_handler(window, window->user_data);
2300 window_show_frame_menu(struct window *window,
2301 struct input *input, uint32_t time)
2306 static const char *entries[] = {
2308 "Move to workspace above", "Move to workspace below",
2312 if (window->fullscreen_handler)
2313 count = ARRAY_LENGTH(entries);
2315 count = ARRAY_LENGTH(entries) - 1;
2317 input_get_position(input, &x, &y);
2318 window_show_menu(window->display, input, time, window,
2319 x - 10, y - 10, frame_menu_func, entries, count);
2323 frame_enter_handler(struct widget *widget,
2324 struct input *input, float x, float y, void *data)
2326 struct window_frame *frame = data;
2327 enum theme_location location;
2329 location = frame_pointer_enter(frame->frame, input, x, y);
2330 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2331 widget_schedule_redraw(frame->widget);
2333 return frame_get_pointer_image_for_location(data, location);
2337 frame_motion_handler(struct widget *widget,
2338 struct input *input, uint32_t time,
2339 float x, float y, void *data)
2341 struct window_frame *frame = data;
2342 enum theme_location location;
2344 location = frame_pointer_motion(frame->frame, input, x, y);
2345 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2346 widget_schedule_redraw(frame->widget);
2348 return frame_get_pointer_image_for_location(data, location);
2352 frame_leave_handler(struct widget *widget,
2353 struct input *input, void *data)
2355 struct window_frame *frame = data;
2357 frame_pointer_leave(frame->frame, input);
2358 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2359 widget_schedule_redraw(frame->widget);
2363 frame_handle_status(struct window_frame *frame, struct input *input,
2364 uint32_t time, enum theme_location location)
2366 struct window *window = frame->widget->window;
2369 status = frame_status(frame->frame);
2370 if (status & FRAME_STATUS_REPAINT)
2371 widget_schedule_redraw(frame->widget);
2373 if (status & FRAME_STATUS_MINIMIZE)
2374 fprintf(stderr,"Minimize stub\n");
2376 if (status & FRAME_STATUS_MENU) {
2377 window_show_frame_menu(window, input, time);
2378 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2381 if (status & FRAME_STATUS_MAXIMIZE) {
2382 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2386 if (status & FRAME_STATUS_CLOSE) {
2387 if (window->close_handler)
2388 window->close_handler(window->user_data);
2390 display_exit(window->display);
2394 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2395 input_ungrab(input);
2396 wl_shell_surface_move(window->shell_surface,
2397 input_get_seat(input),
2398 window->display->serial);
2400 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2403 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2404 input_ungrab(input);
2406 window->resizing = 1;
2407 wl_shell_surface_resize(window->shell_surface,
2408 input_get_seat(input),
2409 window->display->serial,
2412 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2417 frame_button_handler(struct widget *widget,
2418 struct input *input, uint32_t time,
2419 uint32_t button, enum wl_pointer_button_state state,
2423 struct window_frame *frame = data;
2424 enum theme_location location;
2426 location = frame_pointer_button(frame->frame, input, button, state);
2427 frame_handle_status(frame, input, time, location);
2431 frame_touch_down_handler(struct widget *widget, struct input *input,
2432 uint32_t serial, uint32_t time, int32_t id,
2433 float x, float y, void *data)
2435 struct window_frame *frame = data;
2437 frame_touch_down(frame->frame, input, id, x, y);
2438 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2442 frame_touch_up_handler(struct widget *widget,
2443 struct input *input, uint32_t serial, uint32_t time,
2444 int32_t id, void *data)
2446 struct window_frame *frame = data;
2448 frame_touch_up(frame->frame, input, id);
2449 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2453 window_frame_create(struct window *window, void *data)
2455 struct window_frame *frame;
2457 frame = xzalloc(sizeof *frame);
2458 frame->frame = frame_create(window->display->theme, 0, 0,
2459 FRAME_BUTTON_ALL, window->title);
2461 frame->widget = window_add_widget(window, frame);
2462 frame->child = widget_add_widget(frame->widget, data);
2464 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2465 widget_set_resize_handler(frame->widget, frame_resize_handler);
2466 widget_set_enter_handler(frame->widget, frame_enter_handler);
2467 widget_set_leave_handler(frame->widget, frame_leave_handler);
2468 widget_set_motion_handler(frame->widget, frame_motion_handler);
2469 widget_set_button_handler(frame->widget, frame_button_handler);
2470 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2471 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2473 window->frame = frame;
2475 return frame->child;
2479 window_frame_set_child_size(struct widget *widget, int child_width,
2482 struct display *display = widget->window->display;
2483 struct theme *t = display->theme;
2484 int decoration_width, decoration_height;
2486 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2488 if (widget->window->type != TYPE_FULLSCREEN) {
2489 decoration_width = (t->width + margin) * 2;
2490 decoration_height = t->width +
2491 t->titlebar_height + margin * 2;
2493 width = child_width + decoration_width;
2494 height = child_height + decoration_height;
2496 width = child_width;
2497 height = child_height;
2500 window_schedule_resize(widget->window, width, height);
2504 window_frame_destroy(struct window_frame *frame)
2506 frame_destroy(frame->frame);
2508 /* frame->child must be destroyed by the application */
2509 widget_destroy(frame->widget);
2514 input_set_focus_widget(struct input *input, struct widget *focus,
2517 struct widget *old, *widget;
2520 if (focus == input->focus_widget)
2523 old = input->focus_widget;
2527 widget = input->grab;
2528 if (widget->leave_handler)
2529 widget->leave_handler(old, input, widget->user_data);
2530 input->focus_widget = NULL;
2536 widget = input->grab;
2537 input->focus_widget = focus;
2538 if (widget->enter_handler)
2539 cursor = widget->enter_handler(focus, input, x, y,
2542 cursor = widget->default_cursor;
2544 input_set_pointer_image(input, cursor);
2549 input_grab(struct input *input, struct widget *widget, uint32_t button)
2551 input->grab = widget;
2552 input->grab_button = button;
2556 input_ungrab(struct input *input)
2558 struct widget *widget;
2561 if (input->pointer_focus) {
2562 widget = window_find_widget(input->pointer_focus,
2563 input->sx, input->sy);
2564 input_set_focus_widget(input, widget, input->sx, input->sy);
2569 input_remove_pointer_focus(struct input *input)
2571 struct window *window = input->pointer_focus;
2576 input_set_focus_widget(input, NULL, 0, 0);
2578 input->pointer_focus = NULL;
2579 input->current_cursor = CURSOR_UNSET;
2583 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2584 uint32_t serial, struct wl_surface *surface,
2585 wl_fixed_t sx_w, wl_fixed_t sy_w)
2587 struct input *input = data;
2588 struct window *window;
2589 struct widget *widget;
2590 float sx = wl_fixed_to_double(sx_w);
2591 float sy = wl_fixed_to_double(sy_w);
2594 /* enter event for a window we've just destroyed */
2598 input->display->serial = serial;
2599 input->pointer_enter_serial = serial;
2600 input->pointer_focus = wl_surface_get_user_data(surface);
2601 window = input->pointer_focus;
2603 if (window->resizing) {
2604 window->resizing = 0;
2605 /* Schedule a redraw to free the pool */
2606 window_schedule_redraw(window);
2612 widget = window_find_widget(window, sx, sy);
2613 input_set_focus_widget(input, widget, sx, sy);
2617 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2618 uint32_t serial, struct wl_surface *surface)
2620 struct input *input = data;
2622 input->display->serial = serial;
2623 input_remove_pointer_focus(input);
2627 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2628 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2630 struct input *input = data;
2631 struct window *window = input->pointer_focus;
2632 struct widget *widget;
2634 float sx = wl_fixed_to_double(sx_w);
2635 float sy = wl_fixed_to_double(sy_w);
2643 /* when making the window smaller - e.g. after a unmaximise we might
2644 * still have a pending motion event that the compositor has picked
2645 * based on the old surface dimensions
2647 if (sx > window->main_surface->allocation.width ||
2648 sy > window->main_surface->allocation.height)
2651 if (!(input->grab && input->grab_button)) {
2652 widget = window_find_widget(window, sx, sy);
2653 input_set_focus_widget(input, widget, sx, sy);
2657 widget = input->grab;
2659 widget = input->focus_widget;
2661 if (widget->motion_handler)
2662 cursor = widget->motion_handler(input->focus_widget,
2663 input, time, sx, sy,
2666 cursor = widget->default_cursor;
2668 cursor = CURSOR_LEFT_PTR;
2670 input_set_pointer_image(input, cursor);
2674 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2675 uint32_t time, uint32_t button, uint32_t state_w)
2677 struct input *input = data;
2678 struct widget *widget;
2679 enum wl_pointer_button_state state = state_w;
2681 input->display->serial = serial;
2682 if (input->focus_widget && input->grab == NULL &&
2683 state == WL_POINTER_BUTTON_STATE_PRESSED)
2684 input_grab(input, input->focus_widget, button);
2686 widget = input->grab;
2687 if (widget && widget->button_handler)
2688 (*widget->button_handler)(widget,
2691 input->grab->user_data);
2693 if (input->grab && input->grab_button == button &&
2694 state == WL_POINTER_BUTTON_STATE_RELEASED)
2695 input_ungrab(input);
2699 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2700 uint32_t time, uint32_t axis, wl_fixed_t value)
2702 struct input *input = data;
2703 struct widget *widget;
2705 widget = input->focus_widget;
2707 widget = input->grab;
2708 if (widget && widget->axis_handler)
2709 (*widget->axis_handler)(widget,
2715 static const struct wl_pointer_listener pointer_listener = {
2716 pointer_handle_enter,
2717 pointer_handle_leave,
2718 pointer_handle_motion,
2719 pointer_handle_button,
2720 pointer_handle_axis,
2724 input_remove_keyboard_focus(struct input *input)
2726 struct window *window = input->keyboard_focus;
2727 struct itimerspec its;
2729 its.it_interval.tv_sec = 0;
2730 its.it_interval.tv_nsec = 0;
2731 its.it_value.tv_sec = 0;
2732 its.it_value.tv_nsec = 0;
2733 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2738 window->focus_count--;
2739 if (window->keyboard_focus_handler)
2740 (*window->keyboard_focus_handler)(window, NULL,
2743 input->keyboard_focus = NULL;
2747 keyboard_repeat_func(struct task *task, uint32_t events)
2749 struct input *input =
2750 container_of(task, struct input, repeat_task);
2751 struct window *window = input->keyboard_focus;
2754 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2755 /* If we change the timer between the fd becoming
2756 * readable and getting here, there'll be nothing to
2757 * read and we get EAGAIN. */
2760 if (window && window->key_handler) {
2761 (*window->key_handler)(window, input, input->repeat_time,
2762 input->repeat_key, input->repeat_sym,
2763 WL_KEYBOARD_KEY_STATE_PRESSED,
2769 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2770 uint32_t format, int fd, uint32_t size)
2772 struct input *input = data;
2773 struct xkb_keymap *keymap;
2774 struct xkb_state *state;
2782 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2787 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2788 if (map_str == MAP_FAILED) {
2793 keymap = xkb_map_new_from_string(input->display->xkb_context,
2795 XKB_KEYMAP_FORMAT_TEXT_V1,
2797 munmap(map_str, size);
2801 fprintf(stderr, "failed to compile keymap\n");
2805 state = xkb_state_new(keymap);
2807 fprintf(stderr, "failed to create XKB state\n");
2808 xkb_map_unref(keymap);
2812 xkb_keymap_unref(input->xkb.keymap);
2813 xkb_state_unref(input->xkb.state);
2814 input->xkb.keymap = keymap;
2815 input->xkb.state = state;
2817 input->xkb.control_mask =
2818 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2819 input->xkb.alt_mask =
2820 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2821 input->xkb.shift_mask =
2822 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2826 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2827 uint32_t serial, struct wl_surface *surface,
2828 struct wl_array *keys)
2830 struct input *input = data;
2831 struct window *window;
2833 input->display->serial = serial;
2834 input->keyboard_focus = wl_surface_get_user_data(surface);
2836 window = input->keyboard_focus;
2837 window->focus_count++;
2838 if (window->keyboard_focus_handler)
2839 (*window->keyboard_focus_handler)(window,
2840 input, window->user_data);
2844 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2845 uint32_t serial, struct wl_surface *surface)
2847 struct input *input = data;
2849 input->display->serial = serial;
2850 input_remove_keyboard_focus(input);
2854 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2855 uint32_t serial, uint32_t time, uint32_t key,
2858 struct input *input = data;
2859 struct window *window = input->keyboard_focus;
2860 uint32_t code, num_syms;
2861 enum wl_keyboard_key_state state = state_w;
2862 const xkb_keysym_t *syms;
2864 struct itimerspec its;
2866 input->display->serial = serial;
2868 if (!window || !input->xkb.state)
2871 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2873 sym = XKB_KEY_NoSymbol;
2877 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2878 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2879 window_set_maximized(window,
2880 window->type != TYPE_MAXIMIZED);
2881 } else if (sym == XKB_KEY_F11 &&
2882 window->fullscreen_handler &&
2883 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2884 window->fullscreen_handler(window, window->user_data);
2885 } else if (sym == XKB_KEY_F4 &&
2886 input->modifiers == MOD_ALT_MASK &&
2887 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2888 if (window->close_handler)
2889 window->close_handler(window->user_data);
2891 display_exit(window->display);
2892 } else if (window->key_handler) {
2893 (*window->key_handler)(window, input, time, key,
2894 sym, state, window->user_data);
2897 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2898 key == input->repeat_key) {
2899 its.it_interval.tv_sec = 0;
2900 its.it_interval.tv_nsec = 0;
2901 its.it_value.tv_sec = 0;
2902 its.it_value.tv_nsec = 0;
2903 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2904 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2905 input->repeat_sym = sym;
2906 input->repeat_key = key;
2907 input->repeat_time = time;
2908 its.it_interval.tv_sec = 0;
2909 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2910 its.it_value.tv_sec = 0;
2911 its.it_value.tv_nsec = 400 * 1000 * 1000;
2912 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2917 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2918 uint32_t serial, uint32_t mods_depressed,
2919 uint32_t mods_latched, uint32_t mods_locked,
2922 struct input *input = data;
2923 xkb_mod_mask_t mask;
2925 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2926 if (!input->xkb.keymap)
2929 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2930 mods_locked, 0, 0, group);
2931 mask = xkb_state_serialize_mods(input->xkb.state,
2932 XKB_STATE_DEPRESSED |
2934 input->modifiers = 0;
2935 if (mask & input->xkb.control_mask)
2936 input->modifiers |= MOD_CONTROL_MASK;
2937 if (mask & input->xkb.alt_mask)
2938 input->modifiers |= MOD_ALT_MASK;
2939 if (mask & input->xkb.shift_mask)
2940 input->modifiers |= MOD_SHIFT_MASK;
2943 static const struct wl_keyboard_listener keyboard_listener = {
2944 keyboard_handle_keymap,
2945 keyboard_handle_enter,
2946 keyboard_handle_leave,
2947 keyboard_handle_key,
2948 keyboard_handle_modifiers,
2952 touch_handle_down(void *data, struct wl_touch *wl_touch,
2953 uint32_t serial, uint32_t time, struct wl_surface *surface,
2954 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2956 struct input *input = data;
2957 struct widget *widget;
2958 float sx = wl_fixed_to_double(x_w);
2959 float sy = wl_fixed_to_double(y_w);
2961 input->display->serial = serial;
2962 input->touch_focus = wl_surface_get_user_data(surface);
2963 if (!input->touch_focus) {
2964 DBG("Failed to find to touch focus for surface %p\n", surface);
2968 widget = window_find_widget(input->touch_focus,
2969 wl_fixed_to_double(x_w),
2970 wl_fixed_to_double(y_w));
2972 struct touch_point *tp = xmalloc(sizeof *tp);
2975 tp->widget = widget;
2976 wl_list_insert(&input->touch_point_list, &tp->link);
2978 if (widget->touch_down_handler)
2979 (*widget->touch_down_handler)(widget, input,
2988 touch_handle_up(void *data, struct wl_touch *wl_touch,
2989 uint32_t serial, uint32_t time, int32_t id)
2991 struct input *input = data;
2992 struct touch_point *tp, *tmp;
2994 if (!input->touch_focus) {
2995 DBG("No touch focus found for touch up event!\n");
2999 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3003 if (tp->widget->touch_up_handler)
3004 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3006 tp->widget->user_data);
3008 wl_list_remove(&tp->link);
3016 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3017 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3019 struct input *input = data;
3020 struct touch_point *tp;
3021 float sx = wl_fixed_to_double(x_w);
3022 float sy = wl_fixed_to_double(y_w);
3024 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3026 if (!input->touch_focus) {
3027 DBG("No touch focus found for touch motion event!\n");
3031 wl_list_for_each(tp, &input->touch_point_list, link) {
3035 if (tp->widget->touch_motion_handler)
3036 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3038 tp->widget->user_data);
3044 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3046 struct input *input = data;
3047 struct touch_point *tp, *tmp;
3049 DBG("touch_handle_frame\n");
3051 if (!input->touch_focus) {
3052 DBG("No touch focus found for touch frame event!\n");
3056 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3057 if (tp->widget->touch_frame_handler)
3058 (*tp->widget->touch_frame_handler)(tp->widget, input,
3059 tp->widget->user_data);
3061 wl_list_remove(&tp->link);
3067 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3069 struct input *input = data;
3070 struct touch_point *tp, *tmp;
3072 DBG("touch_handle_cancel\n");
3074 if (!input->touch_focus) {
3075 DBG("No touch focus found for touch cancel event!\n");
3079 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3080 if (tp->widget->touch_cancel_handler)
3081 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3082 tp->widget->user_data);
3084 wl_list_remove(&tp->link);
3089 static const struct wl_touch_listener touch_listener = {
3092 touch_handle_motion,
3094 touch_handle_cancel,
3098 seat_handle_capabilities(void *data, struct wl_seat *seat,
3099 enum wl_seat_capability caps)
3101 struct input *input = data;
3103 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3104 input->pointer = wl_seat_get_pointer(seat);
3105 wl_pointer_set_user_data(input->pointer, input);
3106 wl_pointer_add_listener(input->pointer, &pointer_listener,
3108 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3109 wl_pointer_destroy(input->pointer);
3110 input->pointer = NULL;
3113 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3114 input->keyboard = wl_seat_get_keyboard(seat);
3115 wl_keyboard_set_user_data(input->keyboard, input);
3116 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3118 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3119 wl_keyboard_destroy(input->keyboard);
3120 input->keyboard = NULL;
3123 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3124 input->touch = wl_seat_get_touch(seat);
3125 wl_touch_set_user_data(input->touch, input);
3126 wl_touch_add_listener(input->touch, &touch_listener, input);
3127 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3128 wl_touch_destroy(input->touch);
3129 input->touch = NULL;
3134 seat_handle_name(void *data, struct wl_seat *seat,
3140 static const struct wl_seat_listener seat_listener = {
3141 seat_handle_capabilities,
3146 input_get_position(struct input *input, int32_t *x, int32_t *y)
3153 input_get_display(struct input *input)
3155 return input->display;
3159 input_get_seat(struct input *input)
3165 input_get_modifiers(struct input *input)
3167 return input->modifiers;
3171 input_get_focus_widget(struct input *input)
3173 return input->focus_widget;
3177 struct wl_data_offer *offer;
3178 struct input *input;
3179 struct wl_array types;
3182 struct task io_task;
3190 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3192 struct data_offer *offer = data;
3195 p = wl_array_add(&offer->types, sizeof *p);
3199 static const struct wl_data_offer_listener data_offer_listener = {
3204 data_offer_destroy(struct data_offer *offer)
3209 if (offer->refcount == 0) {
3210 wl_data_offer_destroy(offer->offer);
3211 for (p = offer->types.data; *p; p++)
3213 wl_array_release(&offer->types);
3219 data_device_data_offer(void *data,
3220 struct wl_data_device *data_device,
3221 struct wl_data_offer *_offer)
3223 struct data_offer *offer;
3225 offer = xmalloc(sizeof *offer);
3227 wl_array_init(&offer->types);
3228 offer->refcount = 1;
3229 offer->input = data;
3230 offer->offer = _offer;
3231 wl_data_offer_add_listener(offer->offer,
3232 &data_offer_listener, offer);
3236 data_device_enter(void *data, struct wl_data_device *data_device,
3237 uint32_t serial, struct wl_surface *surface,
3238 wl_fixed_t x_w, wl_fixed_t y_w,
3239 struct wl_data_offer *offer)
3241 struct input *input = data;
3242 struct window *window;
3244 float x = wl_fixed_to_double(x_w);
3245 float y = wl_fixed_to_double(y_w);
3248 input->pointer_enter_serial = serial;
3249 window = wl_surface_get_user_data(surface);
3250 input->pointer_focus = window;
3255 input->drag_offer = wl_data_offer_get_user_data(offer);
3257 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3260 types_data = input->drag_offer->types.data;
3262 input->drag_offer = NULL;
3266 window = input->pointer_focus;
3267 if (window->data_handler)
3268 window->data_handler(window, input, x, y, types_data,
3273 data_device_leave(void *data, struct wl_data_device *data_device)
3275 struct input *input = data;
3277 if (input->drag_offer) {
3278 data_offer_destroy(input->drag_offer);
3279 input->drag_offer = NULL;
3284 data_device_motion(void *data, struct wl_data_device *data_device,
3285 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3287 struct input *input = data;
3288 struct window *window = input->pointer_focus;
3289 float x = wl_fixed_to_double(x_w);
3290 float y = wl_fixed_to_double(y_w);
3296 if (input->drag_offer)
3297 types_data = input->drag_offer->types.data;
3301 if (window->data_handler)
3302 window->data_handler(window, input, x, y, types_data,
3307 data_device_drop(void *data, struct wl_data_device *data_device)
3309 struct input *input = data;
3310 struct window *window = input->pointer_focus;
3312 if (window->drop_handler)
3313 window->drop_handler(window, input,
3314 input->sx, input->sy, window->user_data);
3318 data_device_selection(void *data,
3319 struct wl_data_device *wl_data_device,
3320 struct wl_data_offer *offer)
3322 struct input *input = data;
3325 if (input->selection_offer)
3326 data_offer_destroy(input->selection_offer);
3329 input->selection_offer = wl_data_offer_get_user_data(offer);
3330 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3333 input->selection_offer = NULL;
3337 static const struct wl_data_device_listener data_device_listener = {
3338 data_device_data_offer,
3343 data_device_selection
3347 input_set_pointer_image_index(struct input *input, int index)
3349 struct wl_buffer *buffer;
3350 struct wl_cursor *cursor;
3351 struct wl_cursor_image *image;
3353 if (!input->pointer)
3356 cursor = input->display->cursors[input->current_cursor];
3360 if (index >= (int) cursor->image_count) {
3361 fprintf(stderr, "cursor index out of range\n");
3365 image = cursor->images[index];
3366 buffer = wl_cursor_image_get_buffer(image);
3370 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3371 input->pointer_surface,
3372 image->hotspot_x, image->hotspot_y);
3373 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3374 wl_surface_damage(input->pointer_surface, 0, 0,
3375 image->width, image->height);
3376 wl_surface_commit(input->pointer_surface);
3379 static const struct wl_callback_listener pointer_surface_listener;
3382 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3385 struct input *input = data;
3386 struct wl_cursor *cursor;
3390 assert(callback == input->cursor_frame_cb);
3391 wl_callback_destroy(callback);
3392 input->cursor_frame_cb = NULL;
3395 if (!input->pointer)
3398 if (input->current_cursor == CURSOR_BLANK) {
3399 wl_pointer_set_cursor(input->pointer,
3400 input->pointer_enter_serial,
3405 if (input->current_cursor == CURSOR_UNSET)
3407 cursor = input->display->cursors[input->current_cursor];
3411 /* FIXME We don't have the current time on the first call so we set
3412 * the animation start to the time of the first frame callback. */
3414 input->cursor_anim_start = 0;
3415 else if (input->cursor_anim_start == 0)
3416 input->cursor_anim_start = time;
3418 if (time == 0 || input->cursor_anim_start == 0)
3421 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3423 if (cursor->image_count > 1) {
3424 input->cursor_frame_cb =
3425 wl_surface_frame(input->pointer_surface);
3426 wl_callback_add_listener(input->cursor_frame_cb,
3427 &pointer_surface_listener, input);
3430 input_set_pointer_image_index(input, i);
3433 static const struct wl_callback_listener pointer_surface_listener = {
3434 pointer_surface_frame_callback
3438 input_set_pointer_image(struct input *input, int pointer)
3442 if (!input->pointer)
3445 if (input->pointer_enter_serial > input->cursor_serial)
3448 if (!force && pointer == input->current_cursor)
3451 input->current_cursor = pointer;
3452 input->cursor_serial = input->pointer_enter_serial;
3453 if (!input->cursor_frame_cb)
3454 pointer_surface_frame_callback(input, NULL, 0);
3456 /* The current frame callback may be stuck if, for instance,
3457 * the set cursor request was processed by the server after
3458 * this client lost the focus. In this case the cursor surface
3459 * might not be mapped and the frame callback wouldn't ever
3460 * complete. Send a set_cursor and attach to try to map the
3461 * cursor surface again so that the callback will finish */
3462 input_set_pointer_image_index(input, 0);
3466 struct wl_data_device *
3467 input_get_data_device(struct input *input)
3469 return input->data_device;
3473 input_set_selection(struct input *input,
3474 struct wl_data_source *source, uint32_t time)
3476 wl_data_device_set_selection(input->data_device, source, time);
3480 input_accept(struct input *input, const char *type)
3482 wl_data_offer_accept(input->drag_offer->offer,
3483 input->pointer_enter_serial, type);
3487 offer_io_func(struct task *task, uint32_t events)
3489 struct data_offer *offer =
3490 container_of(task, struct data_offer, io_task);
3494 len = read(offer->fd, buffer, sizeof buffer);
3495 offer->func(buffer, len,
3496 offer->x, offer->y, offer->user_data);
3500 data_offer_destroy(offer);
3505 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3506 data_func_t func, void *user_data)
3510 if (pipe2(p, O_CLOEXEC) == -1)
3513 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3516 offer->io_task.run = offer_io_func;
3520 offer->user_data = user_data;
3522 display_watch_fd(offer->input->display,
3523 offer->fd, EPOLLIN, &offer->io_task);
3527 input_receive_drag_data(struct input *input, const char *mime_type,
3528 data_func_t func, void *data)
3530 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3531 input->drag_offer->x = input->sx;
3532 input->drag_offer->y = input->sy;
3536 input_receive_drag_data_to_fd(struct input *input,
3537 const char *mime_type, int fd)
3539 if (input->drag_offer)
3540 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3546 input_receive_selection_data(struct input *input, const char *mime_type,
3547 data_func_t func, void *data)
3551 if (input->selection_offer == NULL)
3554 for (p = input->selection_offer->types.data; *p; p++)
3555 if (strcmp(mime_type, *p) == 0)
3561 data_offer_receive_data(input->selection_offer,
3562 mime_type, func, data);
3567 input_receive_selection_data_to_fd(struct input *input,
3568 const char *mime_type, int fd)
3570 if (input->selection_offer)
3571 wl_data_offer_receive(input->selection_offer->offer,
3578 window_move(struct window *window, struct input *input, uint32_t serial)
3580 if (!window->shell_surface)
3583 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3587 surface_set_synchronized(struct surface *surface)
3589 if (!surface->subsurface)
3592 if (surface->synchronized)
3595 wl_subsurface_set_sync(surface->subsurface);
3596 surface->synchronized = 1;
3600 surface_set_synchronized_default(struct surface *surface)
3602 if (!surface->subsurface)
3605 if (surface->synchronized == surface->synchronized_default)
3608 if (surface->synchronized_default)
3609 wl_subsurface_set_sync(surface->subsurface);
3611 wl_subsurface_set_desync(surface->subsurface);
3613 surface->synchronized = surface->synchronized_default;
3617 surface_resize(struct surface *surface)
3619 struct widget *widget = surface->widget;
3620 struct wl_compositor *compositor = widget->window->display->compositor;
3622 if (surface->input_region) {
3623 wl_region_destroy(surface->input_region);
3624 surface->input_region = NULL;
3627 if (surface->opaque_region)
3628 wl_region_destroy(surface->opaque_region);
3630 surface->opaque_region = wl_compositor_create_region(compositor);
3632 if (widget->resize_handler)
3633 widget->resize_handler(widget,
3634 widget->allocation.width,
3635 widget->allocation.height,
3638 if (surface->subsurface &&
3639 (surface->allocation.x != widget->allocation.x ||
3640 surface->allocation.y != widget->allocation.y)) {
3641 wl_subsurface_set_position(surface->subsurface,
3642 widget->allocation.x,
3643 widget->allocation.y);
3645 if (surface->allocation.width != widget->allocation.width ||
3646 surface->allocation.height != widget->allocation.height) {
3647 window_schedule_redraw(widget->window);
3649 surface->allocation = widget->allocation;
3652 wl_region_add(surface->opaque_region, 0, 0,
3653 widget->allocation.width,
3654 widget->allocation.height);
3658 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3661 * This hack should be removed, when EGL respects
3662 * eglSwapInterval(0).
3664 * If this window has sub-surfaces, especially a free-running
3665 * EGL-widget, we need to post the parent surface once with
3666 * all the old state to guarantee, that the EGL-widget will
3667 * receive its frame callback soon. Otherwise, a forced call
3668 * to eglSwapBuffers may end up blocking, waiting for a frame
3669 * event that will never come, because we will commit the parent
3670 * surface with all new state only after eglSwapBuffers returns.
3672 * This assumes, that:
3673 * 1. When the EGL widget's resize hook is called, it pauses.
3674 * 2. When the EGL widget's redraw hook is called, it forces a
3675 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3676 * In a single threaded application condition 1 is a no-op.
3678 * XXX: This should actually be after the surface_resize() calls,
3679 * but cannot, because then it would commit the incomplete state
3680 * accumulated from the widget resize hooks.
3682 if (window->subsurface_list.next != &window->main_surface->link ||
3683 window->subsurface_list.prev != &window->main_surface->link)
3684 wl_surface_commit(window->main_surface->surface);
3688 idle_resize(struct window *window)
3690 struct surface *surface;
3692 window->resize_needed = 0;
3693 window->redraw_needed = 1;
3695 DBG("from %dx%d to %dx%d\n",
3696 window->main_surface->server_allocation.width,
3697 window->main_surface->server_allocation.height,
3698 window->pending_allocation.width,
3699 window->pending_allocation.height);
3701 hack_prevent_EGL_sub_surface_deadlock(window);
3703 widget_set_allocation(window->main_surface->widget,
3704 window->pending_allocation.x,
3705 window->pending_allocation.y,
3706 window->pending_allocation.width,
3707 window->pending_allocation.height);
3709 surface_resize(window->main_surface);
3711 /* The main surface is in the list, too. Main surface's
3712 * resize_handler is responsible for calling widget_set_allocation()
3713 * on all sub-surface root widgets, so they will be resized
3716 wl_list_for_each(surface, &window->subsurface_list, link) {
3717 if (surface == window->main_surface)
3720 surface_set_synchronized(surface);
3721 surface_resize(surface);
3726 window_schedule_resize(struct window *window, int width, int height)
3728 /* We should probably get these numbers from the theme. */
3729 const int min_width = 200, min_height = 200;
3731 window->pending_allocation.x = 0;
3732 window->pending_allocation.y = 0;
3733 window->pending_allocation.width = width;
3734 window->pending_allocation.height = height;
3736 if (window->min_allocation.width == 0) {
3737 if (width < min_width && window->frame)
3738 window->min_allocation.width = min_width;
3740 window->min_allocation.width = width;
3741 if (height < min_height && window->frame)
3742 window->min_allocation.height = min_height;
3744 window->min_allocation.height = height;
3747 if (window->pending_allocation.width < window->min_allocation.width)
3748 window->pending_allocation.width = window->min_allocation.width;
3749 if (window->pending_allocation.height < window->min_allocation.height)
3750 window->pending_allocation.height = window->min_allocation.height;
3752 window->resize_needed = 1;
3753 window_schedule_redraw(window);
3757 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3759 window_schedule_resize(widget->window, width, height);
3763 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3766 wl_shell_surface_pong(shell_surface, serial);
3770 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3771 uint32_t edges, int32_t width, int32_t height)
3773 struct window *window = data;
3775 window->resize_edges = edges;
3776 window_schedule_resize(window, width, height);
3780 menu_destroy(struct menu *menu)
3782 widget_destroy(menu->widget);
3783 window_destroy(menu->window);
3784 frame_destroy(menu->frame);
3789 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3791 struct window *window = data;
3792 struct menu *menu = window->main_surface->widget->user_data;
3794 /* FIXME: Need more context in this event, at least the input
3795 * device. Or just use wl_callback. And this really needs to
3796 * be a window vfunc that the menu can set. And we need the
3799 input_ungrab(menu->input);
3803 static const struct wl_shell_surface_listener shell_surface_listener = {
3810 window_get_allocation(struct window *window,
3811 struct rectangle *allocation)
3813 *allocation = window->main_surface->allocation;
3817 widget_redraw(struct widget *widget)
3819 struct widget *child;
3821 if (widget->redraw_handler)
3822 widget->redraw_handler(widget, widget->user_data);
3823 wl_list_for_each(child, &widget->child_list, link)
3824 widget_redraw(child);
3828 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3830 struct surface *surface = data;
3832 assert(callback == surface->frame_cb);
3833 DBG_OBJ(callback, "done\n");
3834 wl_callback_destroy(callback);
3835 surface->frame_cb = NULL;
3837 surface->last_time = time;
3839 if (surface->redraw_needed || surface->window->redraw_needed) {
3840 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3841 window_schedule_redraw_task(surface->window);
3845 static const struct wl_callback_listener listener = {
3850 surface_redraw(struct surface *surface)
3852 DBG_OBJ(surface->surface, "begin\n");
3854 if (!surface->window->redraw_needed && !surface->redraw_needed)
3857 /* Whole-window redraw forces a redraw even if the previous has
3858 * not yet hit the screen.
3860 if (surface->frame_cb) {
3861 if (!surface->window->redraw_needed)
3864 DBG_OBJ(surface->frame_cb, "cancelled\n");
3865 wl_callback_destroy(surface->frame_cb);
3868 surface->frame_cb = wl_surface_frame(surface->surface);
3869 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3870 DBG_OBJ(surface->frame_cb, "new\n");
3872 surface->redraw_needed = 0;
3873 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3874 widget_redraw(surface->widget);
3875 DBG_OBJ(surface->surface, "done\n");
3879 idle_redraw(struct task *task, uint32_t events)
3881 struct window *window = container_of(task, struct window, redraw_task);
3882 struct surface *surface;
3884 DBG(" --------- \n");
3886 wl_list_init(&window->redraw_task.link);
3887 window->redraw_task_scheduled = 0;
3889 if (window->resize_needed) {
3890 /* throttle resizing to the main surface display */
3891 if (window->main_surface->frame_cb) {
3892 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
3896 idle_resize(window);
3899 wl_list_for_each(surface, &window->subsurface_list, link)
3900 surface_redraw(surface);
3902 window->redraw_needed = 0;
3903 window_flush(window);
3905 wl_list_for_each(surface, &window->subsurface_list, link)
3906 surface_set_synchronized_default(surface);
3910 window_schedule_redraw_task(struct window *window)
3912 if (window->configure_requests)
3914 if (!window->redraw_task_scheduled) {
3915 window->redraw_task.run = idle_redraw;
3916 display_defer(window->display, &window->redraw_task);
3917 window->redraw_task_scheduled = 1;
3922 window_schedule_redraw(struct window *window)
3924 struct surface *surface;
3926 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3928 wl_list_for_each(surface, &window->subsurface_list, link)
3929 surface->redraw_needed = 1;
3931 window_schedule_redraw_task(window);
3935 window_is_fullscreen(struct window *window)
3937 return window->type == TYPE_FULLSCREEN;
3941 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3943 struct window *window = data;
3945 wl_callback_destroy(callback);
3946 window->configure_requests--;
3948 if (!window->configure_requests)
3949 window_schedule_redraw(window);
3952 static struct wl_callback_listener configure_request_listener = {
3953 configure_request_completed,
3957 window_defer_redraw_until_configure(struct window* window)
3959 struct wl_callback *callback;
3961 if (window->redraw_task_scheduled) {
3962 wl_list_remove(&window->redraw_task.link);
3963 window->redraw_task_scheduled = 0;
3966 callback = wl_display_sync(window->display->display);
3967 wl_callback_add_listener(callback, &configure_request_listener, window);
3968 window->configure_requests++;
3972 window_set_fullscreen(struct window *window, int fullscreen)
3974 if (!window->display->shell)
3977 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3981 window->saved_type = window->type;
3982 if (window->type == TYPE_TOPLEVEL) {
3983 window->saved_allocation = window->main_surface->allocation;
3985 window->type = TYPE_FULLSCREEN;
3986 wl_shell_surface_set_fullscreen(window->shell_surface,
3987 window->fullscreen_method,
3989 window_defer_redraw_until_configure (window);
3991 if (window->saved_type == TYPE_MAXIMIZED) {
3992 window_set_maximized(window, 1);
3994 window->type = TYPE_TOPLEVEL;
3995 wl_shell_surface_set_toplevel(window->shell_surface);
3996 window_schedule_resize(window,
3997 window->saved_allocation.width,
3998 window->saved_allocation.height);
4005 window_set_fullscreen_method(struct window *window,
4006 enum wl_shell_surface_fullscreen_method method)
4008 window->fullscreen_method = method;
4012 window_is_maximized(struct window *window)
4014 return window->type == TYPE_MAXIMIZED;
4018 window_set_maximized(struct window *window, int maximized)
4020 if (!window->display->shell)
4023 if ((window->type == TYPE_MAXIMIZED) == maximized)
4026 if (window->type == TYPE_TOPLEVEL) {
4027 window->saved_allocation = window->main_surface->allocation;
4028 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4029 window->type = TYPE_MAXIMIZED;
4030 window_defer_redraw_until_configure(window);
4031 } else if (window->type == TYPE_FULLSCREEN) {
4032 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4033 window->type = TYPE_MAXIMIZED;
4034 window_defer_redraw_until_configure(window);
4036 wl_shell_surface_set_toplevel(window->shell_surface);
4037 window->type = TYPE_TOPLEVEL;
4038 window_schedule_resize(window,
4039 window->saved_allocation.width,
4040 window->saved_allocation.height);
4045 window_set_user_data(struct window *window, void *data)
4047 window->user_data = data;
4051 window_get_user_data(struct window *window)
4053 return window->user_data;
4057 window_set_key_handler(struct window *window,
4058 window_key_handler_t handler)
4060 window->key_handler = handler;
4064 window_set_keyboard_focus_handler(struct window *window,
4065 window_keyboard_focus_handler_t handler)
4067 window->keyboard_focus_handler = handler;
4071 window_set_data_handler(struct window *window, window_data_handler_t handler)
4073 window->data_handler = handler;
4077 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4079 window->drop_handler = handler;
4083 window_set_close_handler(struct window *window,
4084 window_close_handler_t handler)
4086 window->close_handler = handler;
4090 window_set_fullscreen_handler(struct window *window,
4091 window_fullscreen_handler_t handler)
4093 window->fullscreen_handler = handler;
4097 window_set_output_handler(struct window *window,
4098 window_output_handler_t handler)
4100 window->output_handler = handler;
4104 window_set_title(struct window *window, const char *title)
4106 free(window->title);
4107 window->title = strdup(title);
4108 if (window->frame) {
4109 frame_set_title(window->frame->frame, title);
4110 widget_schedule_redraw(window->frame->widget);
4112 if (window->shell_surface)
4113 wl_shell_surface_set_title(window->shell_surface, title);
4117 window_get_title(struct window *window)
4119 return window->title;
4123 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4125 struct text_cursor_position *text_cursor_position =
4126 window->display->text_cursor_position;
4128 if (!text_cursor_position)
4131 text_cursor_position_notify(text_cursor_position,
4132 window->main_surface->surface,
4133 wl_fixed_from_int(x),
4134 wl_fixed_from_int(y));
4138 window_damage(struct window *window, int32_t x, int32_t y,
4139 int32_t width, int32_t height)
4141 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4145 surface_enter(void *data,
4146 struct wl_surface *wl_surface, struct wl_output *wl_output)
4148 struct window *window = data;
4149 struct output *output;
4150 struct output *output_found = NULL;
4151 struct window_output *window_output;
4153 wl_list_for_each(output, &window->display->output_list, link) {
4154 if (output->output == wl_output) {
4155 output_found = output;
4163 window_output = xmalloc(sizeof *window_output);
4164 window_output->output = output_found;
4166 wl_list_insert (&window->window_output_list, &window_output->link);
4168 if (window->output_handler)
4169 window->output_handler(window, output_found, 1,
4174 surface_leave(void *data,
4175 struct wl_surface *wl_surface, struct wl_output *output)
4177 struct window *window = data;
4178 struct window_output *window_output;
4179 struct window_output *window_output_found = NULL;
4181 wl_list_for_each(window_output, &window->window_output_list, link) {
4182 if (window_output->output->output == output) {
4183 window_output_found = window_output;
4188 if (window_output_found) {
4189 wl_list_remove(&window_output_found->link);
4191 if (window->output_handler)
4192 window->output_handler(window, window_output->output,
4193 0, window->user_data);
4195 free(window_output_found);
4199 static const struct wl_surface_listener surface_listener = {
4204 static struct surface *
4205 surface_create(struct window *window)
4207 struct display *display = window->display;
4208 struct surface *surface;
4210 surface = xmalloc(sizeof *surface);
4211 memset(surface, 0, sizeof *surface);
4215 surface->window = window;
4216 surface->surface = wl_compositor_create_surface(display->compositor);
4217 surface->buffer_scale = 1;
4218 wl_surface_add_listener(surface->surface, &surface_listener, window);
4220 wl_list_insert(&window->subsurface_list, &surface->link);
4225 static struct window *
4226 window_create_internal(struct display *display, int type)
4228 struct window *window;
4229 struct surface *surface;
4231 window = xzalloc(sizeof *window);
4232 wl_list_init(&window->subsurface_list);
4233 window->display = display;
4235 surface = surface_create(window);
4236 window->main_surface = surface;
4238 if (type != TYPE_CUSTOM && display->shell) {
4239 window->shell_surface =
4240 wl_shell_get_shell_surface(display->shell,
4242 fail_on_null(window->shell_surface);
4245 window->type = type;
4246 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4247 window->configure_requests = 0;
4248 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4250 if (display->argb_device)
4251 #ifdef HAVE_CAIRO_EGL
4252 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4254 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4257 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4259 wl_surface_set_user_data(surface->surface, window);
4260 wl_list_insert(display->window_list.prev, &window->link);
4261 wl_list_init(&window->redraw_task.link);
4263 if (window->shell_surface) {
4264 wl_shell_surface_set_user_data(window->shell_surface, window);
4265 wl_shell_surface_add_listener(window->shell_surface,
4266 &shell_surface_listener, window);
4269 wl_list_init (&window->window_output_list);
4275 window_create(struct display *display)
4277 return window_create_internal(display, TYPE_NONE);
4281 window_create_custom(struct display *display)
4283 return window_create_internal(display, TYPE_CUSTOM);
4287 window_create_transient(struct display *display, struct window *parent,
4288 int32_t x, int32_t y, uint32_t flags)
4290 struct window *window;
4292 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4298 wl_shell_surface_set_transient(
4299 window->shell_surface,
4300 parent->main_surface->surface,
4301 window->x, window->y, flags);
4307 menu_set_item(struct menu *menu, int sy)
4309 int32_t x, y, width, height;
4312 frame_interior(menu->frame, &x, &y, &width, &height);
4313 next = (sy - y) / 20;
4314 if (menu->current != next) {
4315 menu->current = next;
4316 widget_schedule_redraw(menu->widget);
4321 menu_motion_handler(struct widget *widget,
4322 struct input *input, uint32_t time,
4323 float x, float y, void *data)
4325 struct menu *menu = data;
4327 if (widget == menu->widget)
4328 menu_set_item(data, y);
4330 return CURSOR_LEFT_PTR;
4334 menu_enter_handler(struct widget *widget,
4335 struct input *input, float x, float y, void *data)
4337 struct menu *menu = data;
4339 if (widget == menu->widget)
4340 menu_set_item(data, y);
4342 return CURSOR_LEFT_PTR;
4346 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4348 struct menu *menu = data;
4350 if (widget == menu->widget)
4351 menu_set_item(data, -200);
4355 menu_button_handler(struct widget *widget,
4356 struct input *input, uint32_t time,
4357 uint32_t button, enum wl_pointer_button_state state,
4361 struct menu *menu = data;
4363 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4364 (menu->release_count > 0 || time - menu->time > 500)) {
4365 /* Either relase after press-drag-release or
4366 * click-motion-click. */
4367 menu->func(menu->parent, input,
4368 menu->current, menu->parent->user_data);
4369 input_ungrab(input);
4371 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4372 menu->release_count++;
4377 menu_redraw_handler(struct widget *widget, void *data)
4380 struct menu *menu = data;
4381 int32_t x, y, width, height, i;
4383 cr = widget_cairo_create(widget);
4385 frame_repaint(menu->frame, cr);
4386 frame_interior(menu->frame, &x, &y, &width, &height);
4388 theme_set_background_source(menu->window->display->theme,
4389 cr, THEME_FRAME_ACTIVE);
4390 cairo_rectangle(cr, x, y, width, height);
4393 cairo_select_font_face(cr, "sans",
4394 CAIRO_FONT_SLANT_NORMAL,
4395 CAIRO_FONT_WEIGHT_NORMAL);
4396 cairo_set_font_size(cr, 12);
4398 for (i = 0; i < menu->count; i++) {
4399 if (i == menu->current) {
4400 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4401 cairo_rectangle(cr, x, y + i * 20, width, 20);
4403 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4404 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4405 cairo_show_text(cr, menu->entries[i]);
4407 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4408 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4409 cairo_show_text(cr, menu->entries[i]);
4417 window_show_menu(struct display *display,
4418 struct input *input, uint32_t time, struct window *parent,
4419 int32_t x, int32_t y,
4420 menu_func_t func, const char **entries, int count)
4422 struct window *window;
4426 menu = malloc(sizeof *menu);
4430 window = window_create_internal(parent->display, TYPE_MENU);
4436 menu->window = window;
4437 menu->parent = parent;
4438 menu->widget = window_add_widget(menu->window, menu);
4439 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4440 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4441 menu->frame = frame_create(window->display->theme, 0, 0,
4442 FRAME_BUTTON_NONE, NULL);
4443 menu->entries = entries;
4444 menu->count = count;
4445 menu->release_count = 0;
4449 menu->input = input;
4450 window->type = TYPE_MENU;
4454 input_ungrab(input);
4456 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4457 widget_set_enter_handler(menu->widget, menu_enter_handler);
4458 widget_set_leave_handler(menu->widget, menu_leave_handler);
4459 widget_set_motion_handler(menu->widget, menu_motion_handler);
4460 widget_set_button_handler(menu->widget, menu_button_handler);
4462 input_grab(input, menu->widget, 0);
4463 frame_resize_inside(menu->frame, 200, count * 20);
4464 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4465 window_schedule_resize(window, frame_width(menu->frame),
4466 frame_height(menu->frame));
4468 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4469 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4470 display_get_serial(window->display),
4471 parent->main_surface->surface,
4472 window->x - ix, window->y - iy, 0);
4476 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4478 window->main_surface->buffer_type = type;
4482 window_set_preferred_format(struct window *window,
4483 enum preferred_format format)
4485 window->preferred_format = format;
4489 window_add_subsurface(struct window *window, void *data,
4490 enum subsurface_mode default_mode)
4492 struct widget *widget;
4493 struct surface *surface;
4494 struct wl_surface *parent;
4495 struct wl_subcompositor *subcompo = window->display->subcompositor;
4497 surface = surface_create(window);
4498 widget = widget_create(window, surface, data);
4499 wl_list_init(&widget->link);
4500 surface->widget = widget;
4502 parent = window->main_surface->surface;
4503 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4506 surface->synchronized = 1;
4508 switch (default_mode) {
4509 case SUBSURFACE_SYNCHRONIZED:
4510 surface->synchronized_default = 1;
4512 case SUBSURFACE_DESYNCHRONIZED:
4513 surface->synchronized_default = 0;
4516 assert(!"bad enum subsurface_mode");
4523 display_handle_geometry(void *data,
4524 struct wl_output *wl_output,
4527 int physical_height,
4533 struct output *output = data;
4535 output->allocation.x = x;
4536 output->allocation.y = y;
4537 output->transform = transform;
4541 display_handle_done(void *data,
4542 struct wl_output *wl_output)
4547 display_handle_scale(void *data,
4548 struct wl_output *wl_output,
4551 struct output *output = data;
4553 output->scale = scale;
4557 display_handle_mode(void *data,
4558 struct wl_output *wl_output,
4564 struct output *output = data;
4565 struct display *display = output->display;
4567 if (flags & WL_OUTPUT_MODE_CURRENT) {
4568 output->allocation.width = width;
4569 output->allocation.height = height;
4570 if (display->output_configure_handler)
4571 (*display->output_configure_handler)(
4572 output, display->user_data);
4576 static const struct wl_output_listener output_listener = {
4577 display_handle_geometry,
4578 display_handle_mode,
4579 display_handle_done,
4580 display_handle_scale
4584 display_add_output(struct display *d, uint32_t id)
4586 struct output *output;
4588 output = xzalloc(sizeof *output);
4589 output->display = d;
4592 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4593 output->server_output_id = id;
4594 wl_list_insert(d->output_list.prev, &output->link);
4596 wl_output_add_listener(output->output, &output_listener, output);
4600 output_destroy(struct output *output)
4602 if (output->destroy_handler)
4603 (*output->destroy_handler)(output, output->user_data);
4605 wl_output_destroy(output->output);
4606 wl_list_remove(&output->link);
4611 display_destroy_output(struct display *d, uint32_t id)
4613 struct output *output;
4615 wl_list_for_each(output, &d->output_list, link) {
4616 if (output->server_output_id == id) {
4617 output_destroy(output);
4624 display_set_global_handler(struct display *display,
4625 display_global_handler_t handler)
4627 struct global *global;
4629 display->global_handler = handler;
4633 wl_list_for_each(global, &display->global_list, link)
4634 display->global_handler(display,
4635 global->name, global->interface,
4636 global->version, display->user_data);
4640 display_set_global_handler_remove(struct display *display,
4641 display_global_handler_t remove_handler)
4643 display->global_handler_remove = remove_handler;
4644 if (!remove_handler)
4649 display_set_output_configure_handler(struct display *display,
4650 display_output_handler_t handler)
4652 struct output *output;
4654 display->output_configure_handler = handler;
4658 wl_list_for_each(output, &display->output_list, link) {
4659 if (output->allocation.width == 0 &&
4660 output->allocation.height == 0)
4663 (*display->output_configure_handler)(output,
4664 display->user_data);
4669 output_set_user_data(struct output *output, void *data)
4671 output->user_data = data;
4675 output_get_user_data(struct output *output)
4677 return output->user_data;
4681 output_set_destroy_handler(struct output *output,
4682 display_output_handler_t handler)
4684 output->destroy_handler = handler;
4685 /* FIXME: implement this, once we have way to remove outputs */
4689 output_get_allocation(struct output *output, struct rectangle *base)
4691 struct rectangle allocation = output->allocation;
4693 switch (output->transform) {
4694 case WL_OUTPUT_TRANSFORM_90:
4695 case WL_OUTPUT_TRANSFORM_270:
4696 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4697 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4698 /* Swap width and height */
4699 allocation.width = output->allocation.height;
4700 allocation.height = output->allocation.width;
4708 output_get_wl_output(struct output *output)
4710 return output->output;
4713 enum wl_output_transform
4714 output_get_transform(struct output *output)
4716 return output->transform;
4720 output_get_scale(struct output *output)
4722 return output->scale;
4726 fini_xkb(struct input *input)
4728 xkb_state_unref(input->xkb.state);
4729 xkb_map_unref(input->xkb.keymap);
4732 #define MIN(a,b) ((a) < (b) ? a : b)
4735 display_add_input(struct display *d, uint32_t id)
4737 struct input *input;
4739 input = xzalloc(sizeof *input);
4741 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4742 MIN(d->seat_version, 3));
4743 input->touch_focus = NULL;
4744 input->pointer_focus = NULL;
4745 input->keyboard_focus = NULL;
4746 wl_list_init(&input->touch_point_list);
4747 wl_list_insert(d->input_list.prev, &input->link);
4749 wl_seat_add_listener(input->seat, &seat_listener, input);
4750 wl_seat_set_user_data(input->seat, input);
4752 input->data_device =
4753 wl_data_device_manager_get_data_device(d->data_device_manager,
4755 wl_data_device_add_listener(input->data_device, &data_device_listener,
4758 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4760 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4761 TFD_CLOEXEC | TFD_NONBLOCK);
4762 input->repeat_task.run = keyboard_repeat_func;
4763 display_watch_fd(d, input->repeat_timer_fd,
4764 EPOLLIN, &input->repeat_task);
4768 input_destroy(struct input *input)
4770 input_remove_keyboard_focus(input);
4771 input_remove_pointer_focus(input);
4773 if (input->drag_offer)
4774 data_offer_destroy(input->drag_offer);
4776 if (input->selection_offer)
4777 data_offer_destroy(input->selection_offer);
4779 wl_data_device_destroy(input->data_device);
4781 if (input->display->seat_version >= 3) {
4783 wl_pointer_release(input->pointer);
4784 if (input->keyboard)
4785 wl_keyboard_release(input->keyboard);
4790 wl_surface_destroy(input->pointer_surface);
4792 wl_list_remove(&input->link);
4793 wl_seat_destroy(input->seat);
4794 close(input->repeat_timer_fd);
4799 init_workspace_manager(struct display *d, uint32_t id)
4801 d->workspace_manager =
4802 wl_registry_bind(d->registry, id,
4803 &workspace_manager_interface, 1);
4804 if (d->workspace_manager != NULL)
4805 workspace_manager_add_listener(d->workspace_manager,
4806 &workspace_manager_listener,
4811 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4813 struct display *d = data;
4815 if (format == WL_SHM_FORMAT_RGB565)
4819 struct wl_shm_listener shm_listener = {
4824 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4825 const char *interface, uint32_t version)
4827 struct display *d = data;
4828 struct global *global;
4830 global = xmalloc(sizeof *global);
4832 global->interface = strdup(interface);
4833 global->version = version;
4834 wl_list_insert(d->global_list.prev, &global->link);
4836 if (strcmp(interface, "wl_compositor") == 0) {
4837 d->compositor = wl_registry_bind(registry, id,
4838 &wl_compositor_interface, 3);
4839 } else if (strcmp(interface, "wl_output") == 0) {
4840 display_add_output(d, id);
4841 } else if (strcmp(interface, "wl_seat") == 0) {
4842 d->seat_version = version;
4843 display_add_input(d, id);
4844 } else if (strcmp(interface, "wl_shell") == 0) {
4845 d->shell = wl_registry_bind(registry,
4846 id, &wl_shell_interface, 1);
4847 } else if (strcmp(interface, "wl_shm") == 0) {
4848 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4849 wl_shm_add_listener(d->shm, &shm_listener, d);
4850 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4851 d->data_device_manager =
4852 wl_registry_bind(registry, id,
4853 &wl_data_device_manager_interface, 1);
4854 } else if (strcmp(interface, "text_cursor_position") == 0) {
4855 d->text_cursor_position =
4856 wl_registry_bind(registry, id,
4857 &text_cursor_position_interface, 1);
4858 } else if (strcmp(interface, "workspace_manager") == 0) {
4859 init_workspace_manager(d, id);
4860 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4862 wl_registry_bind(registry, id,
4863 &wl_subcompositor_interface, 1);
4866 if (d->global_handler)
4867 d->global_handler(d, id, interface, version, d->user_data);
4871 registry_handle_global_remove(void *data, struct wl_registry *registry,
4874 struct display *d = data;
4875 struct global *global;
4878 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4879 if (global->name != name)
4882 if (strcmp(global->interface, "wl_output") == 0)
4883 display_destroy_output(d, name);
4885 /* XXX: Should destroy remaining bound globals */
4887 if (d->global_handler_remove)
4888 d->global_handler_remove(d, name, global->interface,
4889 global->version, d->user_data);
4891 wl_list_remove(&global->link);
4892 free(global->interface);
4898 display_bind(struct display *display, uint32_t name,
4899 const struct wl_interface *interface, uint32_t version)
4901 return wl_registry_bind(display->registry, name, interface, version);
4904 static const struct wl_registry_listener registry_listener = {
4905 registry_handle_global,
4906 registry_handle_global_remove
4909 #ifdef HAVE_CAIRO_EGL
4911 init_egl(struct display *d)
4913 EGLint major, minor;
4916 #ifdef USE_CAIRO_GLESV2
4917 # define GL_BIT EGL_OPENGL_ES2_BIT
4919 # define GL_BIT EGL_OPENGL_BIT
4922 static const EGLint argb_cfg_attribs[] = {
4923 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4929 EGL_RENDERABLE_TYPE, GL_BIT,
4933 #ifdef USE_CAIRO_GLESV2
4934 static const EGLint context_attribs[] = {
4935 EGL_CONTEXT_CLIENT_VERSION, 2,
4938 EGLint api = EGL_OPENGL_ES_API;
4940 EGLint *context_attribs = NULL;
4941 EGLint api = EGL_OPENGL_API;
4944 d->dpy = eglGetDisplay(d->display);
4945 if (!eglInitialize(d->dpy, &major, &minor)) {
4946 fprintf(stderr, "failed to initialize EGL\n");
4950 if (!eglBindAPI(api)) {
4951 fprintf(stderr, "failed to bind EGL client API\n");
4955 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4956 &d->argb_config, 1, &n) || n != 1) {
4957 fprintf(stderr, "failed to choose argb EGL config\n");
4961 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
4962 EGL_NO_CONTEXT, context_attribs);
4963 if (d->argb_ctx == NULL) {
4964 fprintf(stderr, "failed to create EGL context\n");
4968 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4969 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4970 fprintf(stderr, "failed to get cairo EGL argb device\n");
4978 fini_egl(struct display *display)
4980 cairo_device_destroy(display->argb_device);
4982 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4985 eglTerminate(display->dpy);
4991 init_dummy_surface(struct display *display)
4996 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4998 display->dummy_surface =
4999 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5001 display->dummy_surface_data = data;
5005 handle_display_data(struct task *task, uint32_t events)
5007 struct display *display =
5008 container_of(task, struct display, display_task);
5009 struct epoll_event ep;
5012 display->display_fd_events = events;
5014 if (events & EPOLLERR || events & EPOLLHUP) {
5015 display_exit(display);
5019 if (events & EPOLLIN) {
5020 ret = wl_display_dispatch(display->display);
5022 display_exit(display);
5027 if (events & EPOLLOUT) {
5028 ret = wl_display_flush(display->display);
5030 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5031 ep.data.ptr = &display->display_task;
5032 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5033 display->display_fd, &ep);
5034 } else if (ret == -1 && errno != EAGAIN) {
5035 display_exit(display);
5042 log_handler(const char *format, va_list args)
5044 vfprintf(stderr, format, args);
5048 display_create(int *argc, char *argv[])
5052 wl_log_set_handler_client(log_handler);
5054 d = zalloc(sizeof *d);
5058 d->display = wl_display_connect(NULL);
5059 if (d->display == NULL) {
5060 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5065 d->xkb_context = xkb_context_new(0);
5066 if (d->xkb_context == NULL) {
5067 fprintf(stderr, "Failed to create XKB context\n");
5072 d->epoll_fd = os_epoll_create_cloexec();
5073 d->display_fd = wl_display_get_fd(d->display);
5074 d->display_task.run = handle_display_data;
5075 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5078 wl_list_init(&d->deferred_list);
5079 wl_list_init(&d->input_list);
5080 wl_list_init(&d->output_list);
5081 wl_list_init(&d->global_list);
5084 d->workspace_count = 1;
5086 d->registry = wl_display_get_registry(d->display);
5087 wl_registry_add_listener(d->registry, ®istry_listener, d);
5089 if (wl_display_dispatch(d->display) < 0) {
5090 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5094 #ifdef HAVE_CAIRO_EGL
5095 if (init_egl(d) < 0)
5096 fprintf(stderr, "EGL does not seem to work, "
5097 "falling back to software rendering and wl_shm.\n");
5102 d->theme = theme_create();
5104 wl_list_init(&d->window_list);
5106 init_dummy_surface(d);
5112 display_destroy_outputs(struct display *display)
5115 struct output *output;
5117 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5118 output_destroy(output);
5122 display_destroy_inputs(struct display *display)
5125 struct input *input;
5127 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5128 input_destroy(input);
5132 display_destroy(struct display *display)
5134 if (!wl_list_empty(&display->window_list))
5135 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5136 wl_list_length(&display->window_list));
5138 if (!wl_list_empty(&display->deferred_list))
5139 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5141 cairo_surface_destroy(display->dummy_surface);
5142 free(display->dummy_surface_data);
5144 display_destroy_outputs(display);
5145 display_destroy_inputs(display);
5147 xkb_context_unref(display->xkb_context);
5149 theme_destroy(display->theme);
5150 destroy_cursors(display);
5152 #ifdef HAVE_CAIRO_EGL
5153 if (display->argb_device)
5157 if (display->subcompositor)
5158 wl_subcompositor_destroy(display->subcompositor);
5161 wl_shell_destroy(display->shell);
5164 wl_shm_destroy(display->shm);
5166 if (display->data_device_manager)
5167 wl_data_device_manager_destroy(display->data_device_manager);
5169 wl_compositor_destroy(display->compositor);
5170 wl_registry_destroy(display->registry);
5172 close(display->epoll_fd);
5174 if (!(display->display_fd_events & EPOLLERR) &&
5175 !(display->display_fd_events & EPOLLHUP))
5176 wl_display_flush(display->display);
5178 wl_display_disconnect(display->display);
5183 display_set_user_data(struct display *display, void *data)
5185 display->user_data = data;
5189 display_get_user_data(struct display *display)
5191 return display->user_data;
5195 display_get_display(struct display *display)
5197 return display->display;
5201 display_has_subcompositor(struct display *display)
5203 if (display->subcompositor)
5206 wl_display_roundtrip(display->display);
5208 return display->subcompositor != NULL;
5212 display_get_cairo_device(struct display *display)
5214 return display->argb_device;
5218 display_get_output(struct display *display)
5220 return container_of(display->output_list.next, struct output, link);
5223 struct wl_compositor *
5224 display_get_compositor(struct display *display)
5226 return display->compositor;
5230 display_get_serial(struct display *display)
5232 return display->serial;
5236 display_get_egl_display(struct display *d)
5241 struct wl_data_source *
5242 display_create_data_source(struct display *display)
5244 return wl_data_device_manager_create_data_source(display->data_device_manager);
5248 display_get_argb_egl_config(struct display *d)
5250 return d->argb_config;
5254 display_acquire_window_surface(struct display *display,
5255 struct window *window,
5258 struct surface *surface = window->main_surface;
5260 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5263 widget_get_cairo_surface(window->main_surface->widget);
5264 return surface->toysurface->acquire(surface->toysurface, ctx);
5268 display_release_window_surface(struct display *display,
5269 struct window *window)
5271 struct surface *surface = window->main_surface;
5273 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5276 surface->toysurface->release(surface->toysurface);
5280 display_defer(struct display *display, struct task *task)
5282 wl_list_insert(&display->deferred_list, &task->link);
5286 display_watch_fd(struct display *display,
5287 int fd, uint32_t events, struct task *task)
5289 struct epoll_event ep;
5293 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5297 display_unwatch_fd(struct display *display, int fd)
5299 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5303 display_run(struct display *display)
5306 struct epoll_event ep[16];
5309 display->running = 1;
5311 while (!wl_list_empty(&display->deferred_list)) {
5312 task = container_of(display->deferred_list.prev,
5314 wl_list_remove(&task->link);
5318 wl_display_dispatch_pending(display->display);
5320 if (!display->running)
5323 ret = wl_display_flush(display->display);
5324 if (ret < 0 && errno == EAGAIN) {
5326 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5327 ep[0].data.ptr = &display->display_task;
5329 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5330 display->display_fd, &ep[0]);
5331 } else if (ret < 0) {
5335 count = epoll_wait(display->epoll_fd,
5336 ep, ARRAY_LENGTH(ep), -1);
5337 for (i = 0; i < count; i++) {
5338 task = ep[i].data.ptr;
5339 task->run(task, ep[i].events);
5345 display_exit(struct display *display)
5347 display->running = 0;
5351 keysym_modifiers_add(struct wl_array *modifiers_map,
5354 size_t len = strlen(name) + 1;
5357 p = wl_array_add(modifiers_map, len);
5362 strncpy(p, name, len);
5365 static xkb_mod_index_t
5366 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5369 xkb_mod_index_t index = 0;
5370 char *p = modifiers_map->data;
5372 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5373 if (strcmp(p, name) == 0)
5380 return XKB_MOD_INVALID;
5384 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5387 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5389 if (index == XKB_MOD_INVALID)
5390 return XKB_MOD_INVALID;
5396 fail_on_null(void *p)
5399 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5409 return fail_on_null(malloc(s));
5415 return fail_on_null(zalloc(s));
5419 xstrdup(const char *s)
5421 return fail_on_null(strdup(s));