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;
324 int32_t touch_grab_id;
325 float drag_x, drag_y;
326 struct window *drag_focus;
327 uint32_t drag_enter_serial;
330 struct xkb_keymap *keymap;
331 struct xkb_state *state;
332 xkb_mod_mask_t control_mask;
333 xkb_mod_mask_t alt_mask;
334 xkb_mod_mask_t shift_mask;
337 struct task repeat_task;
341 uint32_t repeat_time;
345 struct display *display;
346 struct wl_output *output;
347 uint32_t server_output_id;
348 struct rectangle allocation;
353 display_output_handler_t destroy_handler;
357 struct window_frame {
358 struct widget *widget;
359 struct widget *child;
364 struct window *window;
365 struct window *parent;
366 struct widget *widget;
369 const char **entries;
378 struct widget *parent;
379 struct window *window;
380 struct widget *widget;
382 struct task tooltip_task;
388 struct wl_shm_pool *pool;
395 CURSOR_DEFAULT = 100,
399 enum window_location {
401 WINDOW_RESIZING_TOP = 1,
402 WINDOW_RESIZING_BOTTOM = 2,
403 WINDOW_RESIZING_LEFT = 4,
404 WINDOW_RESIZING_TOP_LEFT = 5,
405 WINDOW_RESIZING_BOTTOM_LEFT = 6,
406 WINDOW_RESIZING_RIGHT = 8,
407 WINDOW_RESIZING_TOP_RIGHT = 9,
408 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
409 WINDOW_RESIZING_MASK = 15,
410 WINDOW_EXTERIOR = 16,
411 WINDOW_TITLEBAR = 17,
412 WINDOW_CLIENT_AREA = 18,
415 static const cairo_user_data_key_t shm_surface_data_key;
422 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
423 __attribute__ ((format (printf, 4, 5)));
426 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
431 gettimeofday(&tv, NULL);
432 fprintf(stderr, "%8ld.%03ld ",
433 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
436 fprintf(stderr, "%s@%d ",
437 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
439 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
440 fprintf(stderr, "%s ", func);
443 vfprintf(stderr, fmt, ap);
447 #define DBG(fmt, ...) \
448 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
450 #define DBG_OBJ(obj, fmt, ...) \
451 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
455 #define DBG(...) do {} while (0)
456 #define DBG_OBJ(...) do {} while (0)
461 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
465 switch (buffer_transform) {
466 case WL_OUTPUT_TRANSFORM_90:
467 case WL_OUTPUT_TRANSFORM_270:
468 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
469 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
478 *width *= buffer_scale;
479 *height *= buffer_scale;
483 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
487 switch (buffer_transform) {
488 case WL_OUTPUT_TRANSFORM_90:
489 case WL_OUTPUT_TRANSFORM_270:
490 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
491 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
500 *width /= buffer_scale;
501 *height /= buffer_scale;
504 #ifdef HAVE_CAIRO_EGL
506 struct egl_window_surface {
507 struct toysurface base;
508 cairo_surface_t *cairo_surface;
509 struct display *display;
510 struct wl_surface *surface;
511 struct wl_egl_window *egl_window;
512 EGLSurface egl_surface;
515 static struct egl_window_surface *
516 to_egl_window_surface(struct toysurface *base)
518 return container_of(base, struct egl_window_surface, base);
521 static cairo_surface_t *
522 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
523 int32_t width, int32_t height, uint32_t flags,
524 enum wl_output_transform buffer_transform, int32_t buffer_scale)
526 struct egl_window_surface *surface = to_egl_window_surface(base);
528 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
530 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
531 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
533 return cairo_surface_reference(surface->cairo_surface);
537 egl_window_surface_swap(struct toysurface *base,
538 enum wl_output_transform buffer_transform, int32_t buffer_scale,
539 struct rectangle *server_allocation)
541 struct egl_window_surface *surface = to_egl_window_surface(base);
543 cairo_gl_surface_swapbuffers(surface->cairo_surface);
544 wl_egl_window_get_attached_size(surface->egl_window,
545 &server_allocation->width,
546 &server_allocation->height);
548 buffer_to_surface_size (buffer_transform, buffer_scale,
549 &server_allocation->width,
550 &server_allocation->height);
554 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
556 struct egl_window_surface *surface = to_egl_window_surface(base);
557 cairo_device_t *device;
559 device = cairo_surface_get_device(surface->cairo_surface);
564 if (device == surface->display->argb_device)
565 ctx = surface->display->argb_ctx;
570 cairo_device_flush(device);
571 cairo_device_acquire(device);
572 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
573 surface->egl_surface, ctx))
574 fprintf(stderr, "failed to make surface current\n");
580 egl_window_surface_release(struct toysurface *base)
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583 cairo_device_t *device;
585 device = cairo_surface_get_device(surface->cairo_surface);
589 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
590 surface->display->argb_ctx))
591 fprintf(stderr, "failed to make context current\n");
593 cairo_device_release(device);
597 egl_window_surface_destroy(struct toysurface *base)
599 struct egl_window_surface *surface = to_egl_window_surface(base);
600 struct display *d = surface->display;
602 cairo_surface_destroy(surface->cairo_surface);
603 eglDestroySurface(d->dpy, surface->egl_surface);
604 wl_egl_window_destroy(surface->egl_window);
605 surface->surface = NULL;
610 static struct toysurface *
611 egl_window_surface_create(struct display *display,
612 struct wl_surface *wl_surface,
614 struct rectangle *rectangle)
616 struct egl_window_surface *surface;
618 if (display->dpy == EGL_NO_DISPLAY)
621 surface = calloc(1, sizeof *surface);
625 surface->base.prepare = egl_window_surface_prepare;
626 surface->base.swap = egl_window_surface_swap;
627 surface->base.acquire = egl_window_surface_acquire;
628 surface->base.release = egl_window_surface_release;
629 surface->base.destroy = egl_window_surface_destroy;
631 surface->display = display;
632 surface->surface = wl_surface;
634 surface->egl_window = wl_egl_window_create(surface->surface,
638 surface->egl_surface = eglCreateWindowSurface(display->dpy,
639 display->argb_config,
643 surface->cairo_surface =
644 cairo_gl_surface_create_for_egl(display->argb_device,
645 surface->egl_surface,
649 return &surface->base;
654 static struct toysurface *
655 egl_window_surface_create(struct display *display,
656 struct wl_surface *wl_surface,
658 struct rectangle *rectangle)
665 struct shm_surface_data {
666 struct wl_buffer *buffer;
667 struct shm_pool *pool;
671 display_get_buffer_for_surface(struct display *display,
672 cairo_surface_t *surface)
674 struct shm_surface_data *data;
676 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
682 shm_pool_destroy(struct shm_pool *pool);
685 shm_surface_data_destroy(void *p)
687 struct shm_surface_data *data = p;
689 wl_buffer_destroy(data->buffer);
691 shm_pool_destroy(data->pool);
696 static struct wl_shm_pool *
697 make_shm_pool(struct display *display, int size, void **data)
699 struct wl_shm_pool *pool;
702 fd = os_create_anonymous_file(size);
704 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
709 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
710 if (*data == MAP_FAILED) {
711 fprintf(stderr, "mmap failed: %m\n");
716 pool = wl_shm_create_pool(display->shm, fd, size);
723 static struct shm_pool *
724 shm_pool_create(struct display *display, size_t size)
726 struct shm_pool *pool = malloc(sizeof *pool);
731 pool->pool = make_shm_pool(display, size, &pool->data);
744 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
746 if (pool->used + size > pool->size)
749 *offset = pool->used;
752 return (char *) pool->data + *offset;
755 /* destroy the pool. this does not unmap the memory though */
757 shm_pool_destroy(struct shm_pool *pool)
759 munmap(pool->data, pool->size);
760 wl_shm_pool_destroy(pool->pool);
764 /* Start allocating from the beginning of the pool again */
766 shm_pool_reset(struct shm_pool *pool)
772 data_length_for_shm_surface(struct rectangle *rect)
776 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
778 return stride * rect->height;
781 static cairo_surface_t *
782 display_create_shm_surface_from_pool(struct display *display,
783 struct rectangle *rectangle,
784 uint32_t flags, struct shm_pool *pool)
786 struct shm_surface_data *data;
788 cairo_surface_t *surface;
789 cairo_format_t cairo_format;
790 int stride, length, offset;
793 data = malloc(sizeof *data);
797 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
798 cairo_format = CAIRO_FORMAT_RGB16_565;
800 cairo_format = CAIRO_FORMAT_ARGB32;
802 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
803 length = stride * rectangle->height;
805 map = shm_pool_allocate(pool, length, &offset);
812 surface = cairo_image_surface_create_for_data (map,
818 cairo_surface_set_user_data(surface, &shm_surface_data_key,
819 data, shm_surface_data_destroy);
821 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
822 format = WL_SHM_FORMAT_RGB565;
824 if (flags & SURFACE_OPAQUE)
825 format = WL_SHM_FORMAT_XRGB8888;
827 format = WL_SHM_FORMAT_ARGB8888;
830 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
838 static cairo_surface_t *
839 display_create_shm_surface(struct display *display,
840 struct rectangle *rectangle, uint32_t flags,
841 struct shm_pool *alternate_pool,
842 struct shm_surface_data **data_ret)
844 struct shm_surface_data *data;
845 struct shm_pool *pool;
846 cairo_surface_t *surface;
848 if (alternate_pool) {
849 shm_pool_reset(alternate_pool);
850 surface = display_create_shm_surface_from_pool(display,
855 data = cairo_surface_get_user_data(surface,
856 &shm_surface_data_key);
861 pool = shm_pool_create(display,
862 data_length_for_shm_surface(rectangle));
867 display_create_shm_surface_from_pool(display, rectangle,
871 shm_pool_destroy(pool);
875 /* make sure we destroy the pool when the surface is destroyed */
876 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
887 check_size(struct rectangle *rect)
889 if (rect->width && rect->height)
892 fprintf(stderr, "tried to create surface of "
893 "width: %d, height: %d\n", rect->width, rect->height);
898 display_create_surface(struct display *display,
899 struct wl_surface *surface,
900 struct rectangle *rectangle,
903 if (check_size(rectangle) < 0)
906 assert(flags & SURFACE_SHM);
907 return display_create_shm_surface(display, rectangle, flags,
911 struct shm_surface_leaf {
912 cairo_surface_t *cairo_surface;
913 /* 'data' is automatically destroyed, when 'cairo_surface' is */
914 struct shm_surface_data *data;
916 struct shm_pool *resize_pool;
921 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
923 if (leaf->cairo_surface)
924 cairo_surface_destroy(leaf->cairo_surface);
925 /* leaf->data already destroyed via cairo private */
927 if (leaf->resize_pool)
928 shm_pool_destroy(leaf->resize_pool);
930 memset(leaf, 0, sizeof *leaf);
936 struct toysurface base;
937 struct display *display;
938 struct wl_surface *surface;
942 struct shm_surface_leaf leaf[MAX_LEAVES];
943 struct shm_surface_leaf *current;
946 static struct shm_surface *
947 to_shm_surface(struct toysurface *base)
949 return container_of(base, struct shm_surface, base);
953 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
956 struct shm_surface_leaf *leaf;
957 char bufs[MAX_LEAVES + 1];
960 for (i = 0; i < MAX_LEAVES; i++) {
961 leaf = &surface->leaf[i];
965 else if (leaf->cairo_surface)
971 bufs[MAX_LEAVES] = '\0';
972 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
977 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
979 struct shm_surface *surface = data;
980 struct shm_surface_leaf *leaf;
984 shm_surface_buffer_state_debug(surface, "buffer_release before");
986 for (i = 0; i < MAX_LEAVES; i++) {
987 leaf = &surface->leaf[i];
988 if (leaf->data && leaf->data->buffer == buffer) {
993 assert(i < MAX_LEAVES && "unknown buffer released");
995 /* Leave one free leaf with storage, release others */
997 for (i = 0; i < MAX_LEAVES; i++) {
998 leaf = &surface->leaf[i];
1000 if (!leaf->cairo_surface || leaf->busy)
1006 shm_surface_leaf_release(leaf);
1009 shm_surface_buffer_state_debug(surface, "buffer_release after");
1012 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1013 shm_surface_buffer_release
1016 static cairo_surface_t *
1017 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1018 int32_t width, int32_t height, uint32_t flags,
1019 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1021 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1022 struct shm_surface *surface = to_shm_surface(base);
1023 struct rectangle rect = { 0};
1024 struct shm_surface_leaf *leaf = NULL;
1030 /* pick a free buffer, preferrably one that already has storage */
1031 for (i = 0; i < MAX_LEAVES; i++) {
1032 if (surface->leaf[i].busy)
1035 if (!leaf || surface->leaf[i].cairo_surface)
1036 leaf = &surface->leaf[i];
1038 DBG_OBJ(surface->surface, "pick leaf %d\n",
1039 (int)(leaf - &surface->leaf[0]));
1042 fprintf(stderr, "%s: all buffers are held by the server.\n",
1048 if (!resize_hint && leaf->resize_pool) {
1049 cairo_surface_destroy(leaf->cairo_surface);
1050 leaf->cairo_surface = NULL;
1051 shm_pool_destroy(leaf->resize_pool);
1052 leaf->resize_pool = NULL;
1055 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1057 if (leaf->cairo_surface &&
1058 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1059 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1062 if (leaf->cairo_surface)
1063 cairo_surface_destroy(leaf->cairo_surface);
1065 #ifdef USE_RESIZE_POOL
1066 if (resize_hint && !leaf->resize_pool) {
1067 /* Create a big pool to allocate from, while continuously
1068 * resizing. Mmapping a new pool in the server
1069 * is relatively expensive, so reusing a pool performs
1070 * better, but may temporarily reserve unneeded memory.
1072 /* We should probably base this number on the output size. */
1073 leaf->resize_pool = shm_pool_create(surface->display,
1079 rect.height = height;
1081 leaf->cairo_surface =
1082 display_create_shm_surface(surface->display, &rect,
1086 wl_buffer_add_listener(leaf->data->buffer,
1087 &shm_surface_buffer_listener, surface);
1090 surface->current = leaf;
1092 return cairo_surface_reference(leaf->cairo_surface);
1096 shm_surface_swap(struct toysurface *base,
1097 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1098 struct rectangle *server_allocation)
1100 struct shm_surface *surface = to_shm_surface(base);
1101 struct shm_surface_leaf *leaf = surface->current;
1103 server_allocation->width =
1104 cairo_image_surface_get_width(leaf->cairo_surface);
1105 server_allocation->height =
1106 cairo_image_surface_get_height(leaf->cairo_surface);
1108 buffer_to_surface_size (buffer_transform, buffer_scale,
1109 &server_allocation->width,
1110 &server_allocation->height);
1112 wl_surface_attach(surface->surface, leaf->data->buffer,
1113 surface->dx, surface->dy);
1114 wl_surface_damage(surface->surface, 0, 0,
1115 server_allocation->width, server_allocation->height);
1116 wl_surface_commit(surface->surface);
1118 DBG_OBJ(surface->surface, "leaf %d busy\n",
1119 (int)(leaf - &surface->leaf[0]));
1122 surface->current = NULL;
1126 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1132 shm_surface_release(struct toysurface *base)
1137 shm_surface_destroy(struct toysurface *base)
1139 struct shm_surface *surface = to_shm_surface(base);
1142 for (i = 0; i < MAX_LEAVES; i++)
1143 shm_surface_leaf_release(&surface->leaf[i]);
1148 static struct toysurface *
1149 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1150 uint32_t flags, struct rectangle *rectangle)
1152 struct shm_surface *surface;
1153 DBG_OBJ(wl_surface, "\n");
1155 surface = xmalloc(sizeof *surface);
1156 memset(surface, 0, sizeof *surface);
1161 surface->base.prepare = shm_surface_prepare;
1162 surface->base.swap = shm_surface_swap;
1163 surface->base.acquire = shm_surface_acquire;
1164 surface->base.release = shm_surface_release;
1165 surface->base.destroy = shm_surface_destroy;
1167 surface->display = display;
1168 surface->surface = wl_surface;
1169 surface->flags = flags;
1171 return &surface->base;
1175 * The following correspondences between file names and cursors was copied
1176 * from: https://bugs.kde.org/attachment.cgi?id=67313
1179 static const char *bottom_left_corners[] = {
1180 "bottom_left_corner",
1185 static const char *bottom_right_corners[] = {
1186 "bottom_right_corner",
1191 static const char *bottom_sides[] = {
1197 static const char *grabbings[] = {
1200 "208530c400c041818281048008011002"
1203 static const char *left_ptrs[] = {
1210 static const char *left_sides[] = {
1216 static const char *right_sides[] = {
1222 static const char *top_left_corners[] = {
1228 static const char *top_right_corners[] = {
1234 static const char *top_sides[] = {
1240 static const char *xterms[] = {
1246 static const char *hand1s[] = {
1250 "e29285e634086352946a0e7090d73106"
1253 static const char *watches[] = {
1256 "0426c94ea35c87780ff01dc239897213"
1259 struct cursor_alternatives {
1264 static const struct cursor_alternatives cursors[] = {
1265 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1266 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1267 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1268 {grabbings, ARRAY_LENGTH(grabbings)},
1269 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1270 {left_sides, ARRAY_LENGTH(left_sides)},
1271 {right_sides, ARRAY_LENGTH(right_sides)},
1272 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1273 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1274 {top_sides, ARRAY_LENGTH(top_sides)},
1275 {xterms, ARRAY_LENGTH(xterms)},
1276 {hand1s, ARRAY_LENGTH(hand1s)},
1277 {watches, ARRAY_LENGTH(watches)},
1281 create_cursors(struct display *display)
1283 struct weston_config *config;
1284 struct weston_config_section *s;
1288 struct wl_cursor *cursor;
1290 config = weston_config_parse("weston.ini");
1291 s = weston_config_get_section(config, "shell", NULL, NULL);
1292 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1293 weston_config_section_get_int(s, "cursor-size", &size, 32);
1294 weston_config_destroy(config);
1296 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1299 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1301 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1303 for (j = 0; !cursor && j < cursors[i].count; ++j)
1304 cursor = wl_cursor_theme_get_cursor(
1305 display->cursor_theme, cursors[i].names[j]);
1308 fprintf(stderr, "could not load cursor '%s'\n",
1309 cursors[i].names[0]);
1311 display->cursors[i] = cursor;
1316 destroy_cursors(struct display *display)
1318 wl_cursor_theme_destroy(display->cursor_theme);
1319 free(display->cursors);
1322 struct wl_cursor_image *
1323 display_get_pointer_image(struct display *display, int pointer)
1325 struct wl_cursor *cursor = display->cursors[pointer];
1327 return cursor ? cursor->images[0] : NULL;
1331 surface_flush(struct surface *surface)
1333 if (!surface->cairo_surface)
1336 if (surface->opaque_region) {
1337 wl_surface_set_opaque_region(surface->surface,
1338 surface->opaque_region);
1339 wl_region_destroy(surface->opaque_region);
1340 surface->opaque_region = NULL;
1343 if (surface->input_region) {
1344 wl_surface_set_input_region(surface->surface,
1345 surface->input_region);
1346 wl_region_destroy(surface->input_region);
1347 surface->input_region = NULL;
1350 surface->toysurface->swap(surface->toysurface,
1351 surface->buffer_transform, surface->buffer_scale,
1352 &surface->server_allocation);
1354 cairo_surface_destroy(surface->cairo_surface);
1355 surface->cairo_surface = NULL;
1359 window_has_focus(struct window *window)
1361 return window->focus_count > 0;
1365 window_flush(struct window *window)
1367 struct surface *surface;
1369 if (window->type == TYPE_NONE) {
1370 window->type = TYPE_TOPLEVEL;
1371 if (window->shell_surface)
1372 wl_shell_surface_set_toplevel(window->shell_surface);
1375 wl_list_for_each(surface, &window->subsurface_list, link) {
1376 if (surface == window->main_surface)
1379 surface_flush(surface);
1382 surface_flush(window->main_surface);
1386 window_get_display(struct window *window)
1388 return window->display;
1392 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1394 struct display *display = surface->window->display;
1395 struct rectangle allocation = surface->allocation;
1397 if (!surface->toysurface && display->dpy &&
1398 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1399 surface->toysurface =
1400 egl_window_surface_create(display,
1406 if (!surface->toysurface)
1407 surface->toysurface = shm_surface_create(display,
1409 flags, &allocation);
1411 surface->cairo_surface = surface->toysurface->prepare(
1412 surface->toysurface, dx, dy,
1413 allocation.width, allocation.height, flags,
1414 surface->buffer_transform, surface->buffer_scale);
1418 window_create_main_surface(struct window *window)
1420 struct surface *surface = window->main_surface;
1425 if (window->resizing)
1426 flags |= SURFACE_HINT_RESIZE;
1428 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1429 flags |= SURFACE_HINT_RGB565;
1431 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1432 dx = surface->server_allocation.width -
1433 surface->allocation.width;
1435 if (window->resize_edges & WINDOW_RESIZING_TOP)
1436 dy = surface->server_allocation.height -
1437 surface->allocation.height;
1439 window->resize_edges = 0;
1441 surface_create_surface(surface, dx, dy, flags);
1445 window_get_buffer_transform(struct window *window)
1447 return window->main_surface->buffer_transform;
1451 window_set_buffer_transform(struct window *window,
1452 enum wl_output_transform transform)
1454 window->main_surface->buffer_transform = transform;
1455 wl_surface_set_buffer_transform(window->main_surface->surface,
1460 window_set_buffer_scale(struct window *window,
1463 window->main_surface->buffer_scale = scale;
1464 wl_surface_set_buffer_scale(window->main_surface->surface,
1469 window_get_buffer_scale(struct window *window)
1471 return window->main_surface->buffer_scale;
1475 window_get_output_scale(struct window *window)
1477 struct window_output *window_output;
1478 struct window_output *window_output_tmp;
1481 wl_list_for_each_safe(window_output, window_output_tmp,
1482 &window->window_output_list, link) {
1483 if (window_output->output->scale > scale)
1484 scale = window_output->output->scale;
1490 static void window_frame_destroy(struct window_frame *frame);
1493 surface_destroy(struct surface *surface)
1495 if (surface->frame_cb)
1496 wl_callback_destroy(surface->frame_cb);
1498 if (surface->input_region)
1499 wl_region_destroy(surface->input_region);
1501 if (surface->opaque_region)
1502 wl_region_destroy(surface->opaque_region);
1504 if (surface->subsurface)
1505 wl_subsurface_destroy(surface->subsurface);
1507 wl_surface_destroy(surface->surface);
1509 if (surface->toysurface)
1510 surface->toysurface->destroy(surface->toysurface);
1512 wl_list_remove(&surface->link);
1517 window_destroy(struct window *window)
1519 struct display *display = window->display;
1520 struct input *input;
1521 struct window_output *window_output;
1522 struct window_output *window_output_tmp;
1524 wl_list_remove(&window->redraw_task.link);
1526 wl_list_for_each(input, &display->input_list, link) {
1527 if (input->touch_focus == window)
1528 input->touch_focus = NULL;
1529 if (input->pointer_focus == window)
1530 input->pointer_focus = NULL;
1531 if (input->keyboard_focus == window)
1532 input->keyboard_focus = NULL;
1533 if (input->focus_widget &&
1534 input->focus_widget->window == window)
1535 input->focus_widget = NULL;
1538 wl_list_for_each_safe(window_output, window_output_tmp,
1539 &window->window_output_list, link) {
1540 free (window_output);
1544 window_frame_destroy(window->frame);
1546 if (window->shell_surface)
1547 wl_shell_surface_destroy(window->shell_surface);
1549 surface_destroy(window->main_surface);
1551 wl_list_remove(&window->link);
1553 free(window->title);
1557 static struct widget *
1558 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1560 struct widget *child, *target;
1562 wl_list_for_each(child, &widget->child_list, link) {
1563 target = widget_find_widget(child, x, y);
1568 if (widget->allocation.x <= x &&
1569 x < widget->allocation.x + widget->allocation.width &&
1570 widget->allocation.y <= y &&
1571 y < widget->allocation.y + widget->allocation.height) {
1578 static struct widget *
1579 window_find_widget(struct window *window, int32_t x, int32_t y)
1581 struct surface *surface;
1582 struct widget *widget;
1584 wl_list_for_each(surface, &window->subsurface_list, link) {
1585 widget = widget_find_widget(surface->widget, x, y);
1593 static struct widget *
1594 widget_create(struct window *window, struct surface *surface, void *data)
1596 struct widget *widget;
1598 widget = xzalloc(sizeof *widget);
1599 widget->window = window;
1600 widget->surface = surface;
1601 widget->user_data = data;
1602 widget->allocation = surface->allocation;
1603 wl_list_init(&widget->child_list);
1605 widget->tooltip = NULL;
1606 widget->tooltip_count = 0;
1607 widget->default_cursor = CURSOR_LEFT_PTR;
1613 window_add_widget(struct window *window, void *data)
1615 struct widget *widget;
1617 widget = widget_create(window, window->main_surface, data);
1618 wl_list_init(&widget->link);
1619 window->main_surface->widget = widget;
1625 widget_add_widget(struct widget *parent, void *data)
1627 struct widget *widget;
1629 widget = widget_create(parent->window, parent->surface, data);
1630 wl_list_insert(parent->child_list.prev, &widget->link);
1636 widget_destroy(struct widget *widget)
1638 struct display *display = widget->window->display;
1639 struct surface *surface = widget->surface;
1640 struct input *input;
1642 /* Destroy the sub-surface along with the root widget */
1643 if (surface->widget == widget && surface->subsurface)
1644 surface_destroy(widget->surface);
1646 if (widget->tooltip) {
1647 free(widget->tooltip);
1648 widget->tooltip = NULL;
1651 wl_list_for_each(input, &display->input_list, link) {
1652 if (input->focus_widget == widget)
1653 input->focus_widget = NULL;
1656 wl_list_remove(&widget->link);
1661 widget_set_default_cursor(struct widget *widget, int cursor)
1663 widget->default_cursor = cursor;
1667 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1669 *allocation = widget->allocation;
1673 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1675 widget->allocation.width = width;
1676 widget->allocation.height = height;
1680 widget_set_allocation(struct widget *widget,
1681 int32_t x, int32_t y, int32_t width, int32_t height)
1683 widget->allocation.x = x;
1684 widget->allocation.y = y;
1685 widget_set_size(widget, width, height);
1689 widget_set_transparent(struct widget *widget, int transparent)
1691 widget->opaque = !transparent;
1695 widget_get_user_data(struct widget *widget)
1697 return widget->user_data;
1700 static cairo_surface_t *
1701 widget_get_cairo_surface(struct widget *widget)
1703 struct surface *surface = widget->surface;
1704 struct window *window = widget->window;
1706 if (!surface->cairo_surface) {
1707 if (surface == window->main_surface)
1708 window_create_main_surface(window);
1710 surface_create_surface(surface, 0, 0, 0);
1713 return surface->cairo_surface;
1717 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1719 struct surface *surface = widget->surface;
1722 enum wl_output_transform transform;
1723 int surface_width, surface_height;
1724 int translate_x, translate_y;
1727 surface_width = surface->allocation.width;
1728 surface_height = surface->allocation.height;
1730 transform = surface->buffer_transform;
1731 scale = surface->buffer_scale;
1733 switch (transform) {
1734 case WL_OUTPUT_TRANSFORM_FLIPPED:
1735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1736 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1737 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1738 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1741 cairo_matrix_init_identity(&m);
1745 switch (transform) {
1746 case WL_OUTPUT_TRANSFORM_NORMAL:
1752 case WL_OUTPUT_TRANSFORM_FLIPPED:
1754 translate_x = surface_width;
1757 case WL_OUTPUT_TRANSFORM_90:
1759 translate_x = surface_height;
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1764 translate_x = surface_height;
1765 translate_y = surface_width;
1767 case WL_OUTPUT_TRANSFORM_180:
1769 translate_x = surface_width;
1770 translate_y = surface_height;
1772 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1775 translate_y = surface_height;
1777 case WL_OUTPUT_TRANSFORM_270:
1778 angle = M_PI + M_PI_2;
1780 translate_y = surface_width;
1782 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1783 angle = M_PI + M_PI_2;
1789 cairo_scale(cr, scale, scale);
1790 cairo_translate(cr, translate_x, translate_y);
1791 cairo_rotate(cr, angle);
1792 cairo_transform(cr, &m);
1796 widget_cairo_create(struct widget *widget)
1798 struct surface *surface = widget->surface;
1799 cairo_surface_t *cairo_surface;
1802 cairo_surface = widget_get_cairo_surface(widget);
1803 cr = cairo_create(cairo_surface);
1805 widget_cairo_update_transform(widget, cr);
1807 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1813 widget_get_wl_surface(struct widget *widget)
1815 return widget->surface->surface;
1819 widget_get_last_time(struct widget *widget)
1821 return widget->surface->last_time;
1825 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1827 struct wl_compositor *comp = widget->window->display->compositor;
1828 struct surface *surface = widget->surface;
1830 if (!surface->input_region)
1831 surface->input_region = wl_compositor_create_region(comp);
1834 wl_region_add(surface->input_region,
1835 rect->x, rect->y, rect->width, rect->height);
1840 widget_set_resize_handler(struct widget *widget,
1841 widget_resize_handler_t handler)
1843 widget->resize_handler = handler;
1847 widget_set_redraw_handler(struct widget *widget,
1848 widget_redraw_handler_t handler)
1850 widget->redraw_handler = handler;
1854 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1856 widget->enter_handler = handler;
1860 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1862 widget->leave_handler = handler;
1866 widget_set_motion_handler(struct widget *widget,
1867 widget_motion_handler_t handler)
1869 widget->motion_handler = handler;
1873 widget_set_button_handler(struct widget *widget,
1874 widget_button_handler_t handler)
1876 widget->button_handler = handler;
1880 widget_set_touch_up_handler(struct widget *widget,
1881 widget_touch_up_handler_t handler)
1883 widget->touch_up_handler = handler;
1887 widget_set_touch_down_handler(struct widget *widget,
1888 widget_touch_down_handler_t handler)
1890 widget->touch_down_handler = handler;
1894 widget_set_touch_motion_handler(struct widget *widget,
1895 widget_touch_motion_handler_t handler)
1897 widget->touch_motion_handler = handler;
1901 widget_set_touch_frame_handler(struct widget *widget,
1902 widget_touch_frame_handler_t handler)
1904 widget->touch_frame_handler = handler;
1908 widget_set_touch_cancel_handler(struct widget *widget,
1909 widget_touch_cancel_handler_t handler)
1911 widget->touch_cancel_handler = handler;
1915 widget_set_axis_handler(struct widget *widget,
1916 widget_axis_handler_t handler)
1918 widget->axis_handler = handler;
1922 window_schedule_redraw_task(struct window *window);
1925 widget_schedule_redraw(struct widget *widget)
1927 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1928 widget->surface->redraw_needed = 1;
1929 window_schedule_redraw_task(widget->window);
1933 window_get_surface(struct window *window)
1935 cairo_surface_t *cairo_surface;
1937 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1939 return cairo_surface_reference(cairo_surface);
1943 window_get_wl_surface(struct window *window)
1945 return window->main_surface->surface;
1949 tooltip_redraw_handler(struct widget *widget, void *data)
1952 const int32_t r = 3;
1953 struct tooltip *tooltip = data;
1954 int32_t width, height;
1956 cr = widget_cairo_create(widget);
1957 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1958 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1961 width = widget->allocation.width;
1962 height = widget->allocation.height;
1963 rounded_rect(cr, 0, 0, width, height, r);
1965 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1966 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1969 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1970 cairo_move_to(cr, 10, 16);
1971 cairo_show_text(cr, tooltip->entry);
1975 static cairo_text_extents_t
1976 get_text_extents(struct tooltip *tooltip)
1979 cairo_text_extents_t extents;
1981 /* Use the dummy_surface because tooltip's surface was not
1982 * created yet, and parent does not have a valid surface
1983 * outside repaint, either.
1985 cr = cairo_create(tooltip->window->display->dummy_surface);
1986 cairo_text_extents(cr, tooltip->entry, &extents);
1993 window_create_tooltip(struct tooltip *tooltip)
1995 struct widget *parent = tooltip->parent;
1996 struct display *display = parent->window->display;
1997 struct window *window;
1998 const int offset_y = 27;
1999 const int margin = 3;
2000 cairo_text_extents_t extents;
2002 if (tooltip->widget)
2005 window = window_create_transient(display, parent->window, tooltip->x,
2006 tooltip->y + offset_y,
2007 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2011 tooltip->window = window;
2012 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2014 extents = get_text_extents(tooltip);
2015 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2016 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2022 widget_destroy_tooltip(struct widget *parent)
2024 struct tooltip *tooltip = parent->tooltip;
2026 parent->tooltip_count = 0;
2030 if (tooltip->widget) {
2031 widget_destroy(tooltip->widget);
2032 window_destroy(tooltip->window);
2033 tooltip->widget = NULL;
2034 tooltip->window = NULL;
2037 close(tooltip->tooltip_fd);
2038 free(tooltip->entry);
2040 parent->tooltip = NULL;
2044 tooltip_func(struct task *task, uint32_t events)
2046 struct tooltip *tooltip =
2047 container_of(task, struct tooltip, tooltip_task);
2050 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2052 window_create_tooltip(tooltip);
2055 #define TOOLTIP_TIMEOUT 500
2057 tooltip_timer_reset(struct tooltip *tooltip)
2059 struct itimerspec its;
2061 its.it_interval.tv_sec = 0;
2062 its.it_interval.tv_nsec = 0;
2063 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2064 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2065 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2066 fprintf(stderr, "could not set timerfd\n: %m");
2074 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2076 struct tooltip *tooltip = parent->tooltip;
2078 parent->tooltip_count++;
2082 tooltip_timer_reset(tooltip);
2086 /* the handler might be triggered too fast via input device motion, so
2087 * we need this check here to make sure tooltip is fully initialized */
2088 if (parent->tooltip_count > 1)
2091 tooltip = malloc(sizeof *tooltip);
2095 parent->tooltip = tooltip;
2096 tooltip->parent = parent;
2097 tooltip->widget = NULL;
2098 tooltip->window = NULL;
2101 tooltip->entry = strdup(entry);
2102 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2103 if (tooltip->tooltip_fd < 0) {
2104 fprintf(stderr, "could not create timerfd\n: %m");
2108 tooltip->tooltip_task.run = tooltip_func;
2109 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2110 EPOLLIN, &tooltip->tooltip_task);
2111 tooltip_timer_reset(tooltip);
2117 workspace_manager_state(void *data,
2118 struct workspace_manager *workspace_manager,
2122 struct display *display = data;
2124 display->workspace = current;
2125 display->workspace_count = count;
2128 static const struct workspace_manager_listener workspace_manager_listener = {
2129 workspace_manager_state
2133 frame_resize_handler(struct widget *widget,
2134 int32_t width, int32_t height, void *data)
2136 struct window_frame *frame = data;
2137 struct widget *child = frame->child;
2138 struct rectangle interior;
2139 struct rectangle input;
2140 struct rectangle opaque;
2142 if (widget->window->type == TYPE_FULLSCREEN) {
2145 interior.width = width;
2146 interior.height = height;
2148 if (widget->window->type == TYPE_MAXIMIZED) {
2149 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2151 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2154 frame_resize(frame->frame, width, height);
2155 frame_interior(frame->frame, &interior.x, &interior.y,
2156 &interior.width, &interior.height);
2159 widget_set_allocation(child, interior.x, interior.y,
2160 interior.width, interior.height);
2162 if (child->resize_handler) {
2163 child->resize_handler(child, interior.width, interior.height,
2166 if (widget->window->type == TYPE_FULLSCREEN) {
2167 width = child->allocation.width;
2168 height = child->allocation.height;
2170 frame_resize_inside(frame->frame,
2171 child->allocation.width,
2172 child->allocation.height);
2173 width = frame_width(frame->frame);
2174 height = frame_height(frame->frame);
2178 widget_set_allocation(widget, 0, 0, width, height);
2180 widget->surface->input_region =
2181 wl_compositor_create_region(widget->window->display->compositor);
2182 if (widget->window->type != TYPE_FULLSCREEN) {
2183 frame_input_rect(frame->frame, &input.x, &input.y,
2184 &input.width, &input.height);
2185 wl_region_add(widget->surface->input_region,
2186 input.x, input.y, input.width, input.height);
2188 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2191 widget_set_allocation(widget, 0, 0, width, height);
2193 if (child->opaque) {
2194 if (widget->window->type != TYPE_FULLSCREEN) {
2195 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2196 &opaque.width, &opaque.height);
2198 wl_region_add(widget->surface->opaque_region,
2200 opaque.width, opaque.height);
2202 wl_region_add(widget->surface->opaque_region,
2203 0, 0, width, height);
2208 widget_schedule_redraw(widget);
2212 frame_redraw_handler(struct widget *widget, void *data)
2215 struct window_frame *frame = data;
2216 struct window *window = widget->window;
2218 if (window->type == TYPE_FULLSCREEN)
2221 if (window->focus_count) {
2222 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2224 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2227 cr = widget_cairo_create(widget);
2229 frame_repaint(frame->frame, cr);
2235 frame_get_pointer_image_for_location(struct window_frame *frame,
2236 enum theme_location location)
2238 struct window *window = frame->widget->window;
2240 if (window->type != TYPE_TOPLEVEL)
2241 return CURSOR_LEFT_PTR;
2244 case THEME_LOCATION_RESIZING_TOP:
2246 case THEME_LOCATION_RESIZING_BOTTOM:
2247 return CURSOR_BOTTOM;
2248 case THEME_LOCATION_RESIZING_LEFT:
2250 case THEME_LOCATION_RESIZING_RIGHT:
2251 return CURSOR_RIGHT;
2252 case THEME_LOCATION_RESIZING_TOP_LEFT:
2253 return CURSOR_TOP_LEFT;
2254 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2255 return CURSOR_TOP_RIGHT;
2256 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2257 return CURSOR_BOTTOM_LEFT;
2258 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2259 return CURSOR_BOTTOM_RIGHT;
2260 case THEME_LOCATION_EXTERIOR:
2261 case THEME_LOCATION_TITLEBAR:
2263 return CURSOR_LEFT_PTR;
2268 frame_menu_func(struct window *window,
2269 struct input *input, int index, void *data)
2271 struct display *display;
2275 if (window->close_handler)
2276 window->close_handler(window->user_data);
2278 display_exit(window->display);
2280 case 1: /* move to workspace above */
2281 display = window->display;
2282 if (display->workspace > 0)
2283 workspace_manager_move_surface(
2284 display->workspace_manager,
2285 window->main_surface->surface,
2286 display->workspace - 1);
2288 case 2: /* move to workspace below */
2289 display = window->display;
2290 if (display->workspace < display->workspace_count - 1)
2291 workspace_manager_move_surface(
2292 display->workspace_manager,
2293 window->main_surface->surface,
2294 display->workspace + 1);
2296 case 3: /* fullscreen */
2297 /* we don't have a way to get out of fullscreen for now */
2298 if (window->fullscreen_handler)
2299 window->fullscreen_handler(window, window->user_data);
2305 window_show_frame_menu(struct window *window,
2306 struct input *input, uint32_t time)
2311 static const char *entries[] = {
2313 "Move to workspace above", "Move to workspace below",
2317 if (window->fullscreen_handler)
2318 count = ARRAY_LENGTH(entries);
2320 count = ARRAY_LENGTH(entries) - 1;
2322 input_get_position(input, &x, &y);
2323 window_show_menu(window->display, input, time, window,
2324 x - 10, y - 10, frame_menu_func, entries, count);
2328 frame_enter_handler(struct widget *widget,
2329 struct input *input, float x, float y, void *data)
2331 struct window_frame *frame = data;
2332 enum theme_location location;
2334 location = frame_pointer_enter(frame->frame, input, x, y);
2335 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2336 widget_schedule_redraw(frame->widget);
2338 return frame_get_pointer_image_for_location(data, location);
2342 frame_motion_handler(struct widget *widget,
2343 struct input *input, uint32_t time,
2344 float x, float y, void *data)
2346 struct window_frame *frame = data;
2347 enum theme_location location;
2349 location = frame_pointer_motion(frame->frame, input, x, y);
2350 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2351 widget_schedule_redraw(frame->widget);
2353 return frame_get_pointer_image_for_location(data, location);
2357 frame_leave_handler(struct widget *widget,
2358 struct input *input, void *data)
2360 struct window_frame *frame = data;
2362 frame_pointer_leave(frame->frame, input);
2363 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2364 widget_schedule_redraw(frame->widget);
2368 frame_handle_status(struct window_frame *frame, struct input *input,
2369 uint32_t time, enum theme_location location)
2371 struct window *window = frame->widget->window;
2374 status = frame_status(frame->frame);
2375 if (status & FRAME_STATUS_REPAINT)
2376 widget_schedule_redraw(frame->widget);
2378 if (status & FRAME_STATUS_MINIMIZE)
2379 fprintf(stderr,"Minimize stub\n");
2381 if (status & FRAME_STATUS_MENU) {
2382 window_show_frame_menu(window, input, time);
2383 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2386 if (status & FRAME_STATUS_MAXIMIZE) {
2387 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2388 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2391 if (status & FRAME_STATUS_CLOSE) {
2392 if (window->close_handler)
2393 window->close_handler(window->user_data);
2395 display_exit(window->display);
2399 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2400 input_ungrab(input);
2401 wl_shell_surface_move(window->shell_surface,
2402 input_get_seat(input),
2403 window->display->serial);
2405 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2408 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2409 input_ungrab(input);
2411 window->resizing = 1;
2412 wl_shell_surface_resize(window->shell_surface,
2413 input_get_seat(input),
2414 window->display->serial,
2417 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2422 frame_button_handler(struct widget *widget,
2423 struct input *input, uint32_t time,
2424 uint32_t button, enum wl_pointer_button_state state,
2428 struct window_frame *frame = data;
2429 enum theme_location location;
2431 location = frame_pointer_button(frame->frame, input, button, state);
2432 frame_handle_status(frame, input, time, location);
2436 frame_touch_down_handler(struct widget *widget, struct input *input,
2437 uint32_t serial, uint32_t time, int32_t id,
2438 float x, float y, void *data)
2440 struct window_frame *frame = data;
2442 frame_touch_down(frame->frame, input, id, x, y);
2443 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2447 frame_touch_up_handler(struct widget *widget,
2448 struct input *input, uint32_t serial, uint32_t time,
2449 int32_t id, void *data)
2451 struct window_frame *frame = data;
2453 frame_touch_up(frame->frame, input, id);
2454 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2458 window_frame_create(struct window *window, void *data)
2460 struct window_frame *frame;
2462 frame = xzalloc(sizeof *frame);
2463 frame->frame = frame_create(window->display->theme, 0, 0,
2464 FRAME_BUTTON_ALL, window->title);
2466 frame->widget = window_add_widget(window, frame);
2467 frame->child = widget_add_widget(frame->widget, data);
2469 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2470 widget_set_resize_handler(frame->widget, frame_resize_handler);
2471 widget_set_enter_handler(frame->widget, frame_enter_handler);
2472 widget_set_leave_handler(frame->widget, frame_leave_handler);
2473 widget_set_motion_handler(frame->widget, frame_motion_handler);
2474 widget_set_button_handler(frame->widget, frame_button_handler);
2475 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2476 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2478 window->frame = frame;
2480 return frame->child;
2484 window_frame_set_child_size(struct widget *widget, int child_width,
2487 struct display *display = widget->window->display;
2488 struct theme *t = display->theme;
2489 int decoration_width, decoration_height;
2491 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2493 if (widget->window->type != TYPE_FULLSCREEN) {
2494 decoration_width = (t->width + margin) * 2;
2495 decoration_height = t->width +
2496 t->titlebar_height + margin * 2;
2498 width = child_width + decoration_width;
2499 height = child_height + decoration_height;
2501 width = child_width;
2502 height = child_height;
2505 window_schedule_resize(widget->window, width, height);
2509 window_frame_destroy(struct window_frame *frame)
2511 frame_destroy(frame->frame);
2513 /* frame->child must be destroyed by the application */
2514 widget_destroy(frame->widget);
2519 input_set_focus_widget(struct input *input, struct widget *focus,
2522 struct widget *old, *widget;
2525 if (focus == input->focus_widget)
2528 old = input->focus_widget;
2532 widget = input->grab;
2533 if (widget->leave_handler)
2534 widget->leave_handler(old, input, widget->user_data);
2535 input->focus_widget = NULL;
2541 widget = input->grab;
2542 input->focus_widget = focus;
2543 if (widget->enter_handler)
2544 cursor = widget->enter_handler(focus, input, x, y,
2547 cursor = widget->default_cursor;
2549 input_set_pointer_image(input, cursor);
2554 touch_grab(struct input *input, int32_t touch_id)
2556 input->touch_grab = 1;
2557 input->touch_grab_id = touch_id;
2561 touch_ungrab(struct input *input)
2563 struct touch_point *tp, *tmp;
2565 input->touch_grab = 0;
2567 wl_list_for_each_safe(tp, tmp,
2568 &input->touch_point_list, link) {
2569 if (tp->id != input->touch_grab_id)
2571 wl_list_remove(&tp->link);
2579 input_grab(struct input *input, struct widget *widget, uint32_t button)
2581 input->grab = widget;
2582 input->grab_button = button;
2586 input_ungrab(struct input *input)
2588 struct widget *widget;
2591 if (input->pointer_focus) {
2592 widget = window_find_widget(input->pointer_focus,
2593 input->sx, input->sy);
2594 input_set_focus_widget(input, widget, input->sx, input->sy);
2599 input_remove_pointer_focus(struct input *input)
2601 struct window *window = input->pointer_focus;
2606 input_set_focus_widget(input, NULL, 0, 0);
2608 input->pointer_focus = NULL;
2609 input->current_cursor = CURSOR_UNSET;
2613 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2614 uint32_t serial, struct wl_surface *surface,
2615 wl_fixed_t sx_w, wl_fixed_t sy_w)
2617 struct input *input = data;
2618 struct window *window;
2619 struct widget *widget;
2620 float sx = wl_fixed_to_double(sx_w);
2621 float sy = wl_fixed_to_double(sy_w);
2624 /* enter event for a window we've just destroyed */
2628 input->display->serial = serial;
2629 input->pointer_enter_serial = serial;
2630 input->pointer_focus = wl_surface_get_user_data(surface);
2631 window = input->pointer_focus;
2633 if (window->resizing) {
2634 window->resizing = 0;
2635 /* Schedule a redraw to free the pool */
2636 window_schedule_redraw(window);
2642 widget = window_find_widget(window, sx, sy);
2643 input_set_focus_widget(input, widget, sx, sy);
2647 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2648 uint32_t serial, struct wl_surface *surface)
2650 struct input *input = data;
2652 input->display->serial = serial;
2653 input_remove_pointer_focus(input);
2657 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2658 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2660 struct input *input = data;
2661 struct window *window = input->pointer_focus;
2662 struct widget *widget;
2664 float sx = wl_fixed_to_double(sx_w);
2665 float sy = wl_fixed_to_double(sy_w);
2673 /* when making the window smaller - e.g. after a unmaximise we might
2674 * still have a pending motion event that the compositor has picked
2675 * based on the old surface dimensions
2677 if (sx > window->main_surface->allocation.width ||
2678 sy > window->main_surface->allocation.height)
2681 if (!(input->grab && input->grab_button)) {
2682 widget = window_find_widget(window, sx, sy);
2683 input_set_focus_widget(input, widget, sx, sy);
2687 widget = input->grab;
2689 widget = input->focus_widget;
2691 if (widget->motion_handler)
2692 cursor = widget->motion_handler(input->focus_widget,
2693 input, time, sx, sy,
2696 cursor = widget->default_cursor;
2698 cursor = CURSOR_LEFT_PTR;
2700 input_set_pointer_image(input, cursor);
2704 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2705 uint32_t time, uint32_t button, uint32_t state_w)
2707 struct input *input = data;
2708 struct widget *widget;
2709 enum wl_pointer_button_state state = state_w;
2711 input->display->serial = serial;
2712 if (input->focus_widget && input->grab == NULL &&
2713 state == WL_POINTER_BUTTON_STATE_PRESSED)
2714 input_grab(input, input->focus_widget, button);
2716 widget = input->grab;
2717 if (widget && widget->button_handler)
2718 (*widget->button_handler)(widget,
2721 input->grab->user_data);
2723 if (input->grab && input->grab_button == button &&
2724 state == WL_POINTER_BUTTON_STATE_RELEASED)
2725 input_ungrab(input);
2729 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2730 uint32_t time, uint32_t axis, wl_fixed_t value)
2732 struct input *input = data;
2733 struct widget *widget;
2735 widget = input->focus_widget;
2737 widget = input->grab;
2738 if (widget && widget->axis_handler)
2739 (*widget->axis_handler)(widget,
2745 static const struct wl_pointer_listener pointer_listener = {
2746 pointer_handle_enter,
2747 pointer_handle_leave,
2748 pointer_handle_motion,
2749 pointer_handle_button,
2750 pointer_handle_axis,
2754 input_remove_keyboard_focus(struct input *input)
2756 struct window *window = input->keyboard_focus;
2757 struct itimerspec its;
2759 its.it_interval.tv_sec = 0;
2760 its.it_interval.tv_nsec = 0;
2761 its.it_value.tv_sec = 0;
2762 its.it_value.tv_nsec = 0;
2763 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2768 window->focus_count--;
2769 if (window->keyboard_focus_handler)
2770 (*window->keyboard_focus_handler)(window, NULL,
2773 input->keyboard_focus = NULL;
2777 keyboard_repeat_func(struct task *task, uint32_t events)
2779 struct input *input =
2780 container_of(task, struct input, repeat_task);
2781 struct window *window = input->keyboard_focus;
2784 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2785 /* If we change the timer between the fd becoming
2786 * readable and getting here, there'll be nothing to
2787 * read and we get EAGAIN. */
2790 if (window && window->key_handler) {
2791 (*window->key_handler)(window, input, input->repeat_time,
2792 input->repeat_key, input->repeat_sym,
2793 WL_KEYBOARD_KEY_STATE_PRESSED,
2799 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2800 uint32_t format, int fd, uint32_t size)
2802 struct input *input = data;
2803 struct xkb_keymap *keymap;
2804 struct xkb_state *state;
2812 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2817 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2818 if (map_str == MAP_FAILED) {
2823 keymap = xkb_map_new_from_string(input->display->xkb_context,
2825 XKB_KEYMAP_FORMAT_TEXT_V1,
2827 munmap(map_str, size);
2831 fprintf(stderr, "failed to compile keymap\n");
2835 state = xkb_state_new(keymap);
2837 fprintf(stderr, "failed to create XKB state\n");
2838 xkb_map_unref(keymap);
2842 xkb_keymap_unref(input->xkb.keymap);
2843 xkb_state_unref(input->xkb.state);
2844 input->xkb.keymap = keymap;
2845 input->xkb.state = state;
2847 input->xkb.control_mask =
2848 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2849 input->xkb.alt_mask =
2850 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2851 input->xkb.shift_mask =
2852 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2856 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2857 uint32_t serial, struct wl_surface *surface,
2858 struct wl_array *keys)
2860 struct input *input = data;
2861 struct window *window;
2863 input->display->serial = serial;
2864 input->keyboard_focus = wl_surface_get_user_data(surface);
2866 window = input->keyboard_focus;
2867 window->focus_count++;
2868 if (window->keyboard_focus_handler)
2869 (*window->keyboard_focus_handler)(window,
2870 input, window->user_data);
2874 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2875 uint32_t serial, struct wl_surface *surface)
2877 struct input *input = data;
2879 input->display->serial = serial;
2880 input_remove_keyboard_focus(input);
2884 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2885 uint32_t serial, uint32_t time, uint32_t key,
2888 struct input *input = data;
2889 struct window *window = input->keyboard_focus;
2890 uint32_t code, num_syms;
2891 enum wl_keyboard_key_state state = state_w;
2892 const xkb_keysym_t *syms;
2894 struct itimerspec its;
2896 input->display->serial = serial;
2898 if (!window || !input->xkb.state)
2901 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2903 sym = XKB_KEY_NoSymbol;
2907 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2908 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2909 window_set_maximized(window,
2910 window->type != TYPE_MAXIMIZED);
2911 } else if (sym == XKB_KEY_F11 &&
2912 window->fullscreen_handler &&
2913 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2914 window->fullscreen_handler(window, window->user_data);
2915 } else if (sym == XKB_KEY_F4 &&
2916 input->modifiers == MOD_ALT_MASK &&
2917 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2918 if (window->close_handler)
2919 window->close_handler(window->user_data);
2921 display_exit(window->display);
2922 } else if (window->key_handler) {
2923 (*window->key_handler)(window, input, time, key,
2924 sym, state, window->user_data);
2927 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2928 key == input->repeat_key) {
2929 its.it_interval.tv_sec = 0;
2930 its.it_interval.tv_nsec = 0;
2931 its.it_value.tv_sec = 0;
2932 its.it_value.tv_nsec = 0;
2933 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2934 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2935 input->repeat_sym = sym;
2936 input->repeat_key = key;
2937 input->repeat_time = time;
2938 its.it_interval.tv_sec = 0;
2939 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2940 its.it_value.tv_sec = 0;
2941 its.it_value.tv_nsec = 400 * 1000 * 1000;
2942 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2947 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2948 uint32_t serial, uint32_t mods_depressed,
2949 uint32_t mods_latched, uint32_t mods_locked,
2952 struct input *input = data;
2953 xkb_mod_mask_t mask;
2955 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2956 if (!input->xkb.keymap)
2959 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2960 mods_locked, 0, 0, group);
2961 mask = xkb_state_serialize_mods(input->xkb.state,
2962 XKB_STATE_DEPRESSED |
2964 input->modifiers = 0;
2965 if (mask & input->xkb.control_mask)
2966 input->modifiers |= MOD_CONTROL_MASK;
2967 if (mask & input->xkb.alt_mask)
2968 input->modifiers |= MOD_ALT_MASK;
2969 if (mask & input->xkb.shift_mask)
2970 input->modifiers |= MOD_SHIFT_MASK;
2973 static const struct wl_keyboard_listener keyboard_listener = {
2974 keyboard_handle_keymap,
2975 keyboard_handle_enter,
2976 keyboard_handle_leave,
2977 keyboard_handle_key,
2978 keyboard_handle_modifiers,
2982 touch_handle_down(void *data, struct wl_touch *wl_touch,
2983 uint32_t serial, uint32_t time, struct wl_surface *surface,
2984 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2986 struct input *input = data;
2987 struct widget *widget;
2988 float sx = wl_fixed_to_double(x_w);
2989 float sy = wl_fixed_to_double(y_w);
2991 input->display->serial = serial;
2992 input->touch_focus = wl_surface_get_user_data(surface);
2993 if (!input->touch_focus) {
2994 DBG("Failed to find to touch focus for surface %p\n", surface);
2998 widget = window_find_widget(input->touch_focus,
2999 wl_fixed_to_double(x_w),
3000 wl_fixed_to_double(y_w));
3002 struct touch_point *tp = xmalloc(sizeof *tp);
3005 tp->widget = widget;
3006 wl_list_insert(&input->touch_point_list, &tp->link);
3008 if (widget->touch_down_handler)
3009 (*widget->touch_down_handler)(widget, input,
3018 touch_handle_up(void *data, struct wl_touch *wl_touch,
3019 uint32_t serial, uint32_t time, int32_t id)
3021 struct input *input = data;
3022 struct touch_point *tp, *tmp;
3024 if (!input->touch_focus) {
3025 DBG("No touch focus found for touch up event!\n");
3029 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3033 if (tp->widget->touch_up_handler)
3034 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3036 tp->widget->user_data);
3038 wl_list_remove(&tp->link);
3046 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3047 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3049 struct input *input = data;
3050 struct touch_point *tp;
3051 float sx = wl_fixed_to_double(x_w);
3052 float sy = wl_fixed_to_double(y_w);
3054 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3056 if (!input->touch_focus) {
3057 DBG("No touch focus found for touch motion event!\n");
3061 wl_list_for_each(tp, &input->touch_point_list, link) {
3065 if (tp->widget->touch_motion_handler)
3066 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3068 tp->widget->user_data);
3074 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3076 struct input *input = data;
3077 struct touch_point *tp, *tmp;
3079 DBG("touch_handle_frame\n");
3081 if (!input->touch_focus) {
3082 DBG("No touch focus found for touch frame event!\n");
3086 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3087 if (tp->widget->touch_frame_handler)
3088 (*tp->widget->touch_frame_handler)(tp->widget, input,
3089 tp->widget->user_data);
3091 wl_list_remove(&tp->link);
3097 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3099 struct input *input = data;
3100 struct touch_point *tp, *tmp;
3102 DBG("touch_handle_cancel\n");
3104 if (!input->touch_focus) {
3105 DBG("No touch focus found for touch cancel event!\n");
3109 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3110 if (tp->widget->touch_cancel_handler)
3111 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3112 tp->widget->user_data);
3114 wl_list_remove(&tp->link);
3119 static const struct wl_touch_listener touch_listener = {
3122 touch_handle_motion,
3124 touch_handle_cancel,
3128 seat_handle_capabilities(void *data, struct wl_seat *seat,
3129 enum wl_seat_capability caps)
3131 struct input *input = data;
3133 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3134 input->pointer = wl_seat_get_pointer(seat);
3135 wl_pointer_set_user_data(input->pointer, input);
3136 wl_pointer_add_listener(input->pointer, &pointer_listener,
3138 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3139 wl_pointer_destroy(input->pointer);
3140 input->pointer = NULL;
3143 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3144 input->keyboard = wl_seat_get_keyboard(seat);
3145 wl_keyboard_set_user_data(input->keyboard, input);
3146 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3148 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3149 wl_keyboard_destroy(input->keyboard);
3150 input->keyboard = NULL;
3153 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3154 input->touch = wl_seat_get_touch(seat);
3155 wl_touch_set_user_data(input->touch, input);
3156 wl_touch_add_listener(input->touch, &touch_listener, input);
3157 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3158 wl_touch_destroy(input->touch);
3159 input->touch = NULL;
3164 seat_handle_name(void *data, struct wl_seat *seat,
3170 static const struct wl_seat_listener seat_listener = {
3171 seat_handle_capabilities,
3176 input_get_position(struct input *input, int32_t *x, int32_t *y)
3183 input_get_display(struct input *input)
3185 return input->display;
3189 input_get_seat(struct input *input)
3195 input_get_modifiers(struct input *input)
3197 return input->modifiers;
3201 input_get_focus_widget(struct input *input)
3203 return input->focus_widget;
3207 struct wl_data_offer *offer;
3208 struct input *input;
3209 struct wl_array types;
3212 struct task io_task;
3220 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3222 struct data_offer *offer = data;
3225 p = wl_array_add(&offer->types, sizeof *p);
3229 static const struct wl_data_offer_listener data_offer_listener = {
3234 data_offer_destroy(struct data_offer *offer)
3239 if (offer->refcount == 0) {
3240 wl_data_offer_destroy(offer->offer);
3241 for (p = offer->types.data; *p; p++)
3243 wl_array_release(&offer->types);
3249 data_device_data_offer(void *data,
3250 struct wl_data_device *data_device,
3251 struct wl_data_offer *_offer)
3253 struct data_offer *offer;
3255 offer = xmalloc(sizeof *offer);
3257 wl_array_init(&offer->types);
3258 offer->refcount = 1;
3259 offer->input = data;
3260 offer->offer = _offer;
3261 wl_data_offer_add_listener(offer->offer,
3262 &data_offer_listener, offer);
3266 data_device_enter(void *data, struct wl_data_device *data_device,
3267 uint32_t serial, struct wl_surface *surface,
3268 wl_fixed_t x_w, wl_fixed_t y_w,
3269 struct wl_data_offer *offer)
3271 struct input *input = data;
3272 struct window *window;
3274 float x = wl_fixed_to_double(x_w);
3275 float y = wl_fixed_to_double(y_w);
3278 window = wl_surface_get_user_data(surface);
3279 input->drag_enter_serial = serial;
3280 input->drag_focus = window,
3284 if (!input->touch_grab)
3285 input->pointer_enter_serial = serial;
3288 input->drag_offer = wl_data_offer_get_user_data(offer);
3290 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3293 types_data = input->drag_offer->types.data;
3295 input->drag_offer = NULL;
3299 if (window->data_handler)
3300 window->data_handler(window, input, x, y, types_data,
3305 data_device_leave(void *data, struct wl_data_device *data_device)
3307 struct input *input = data;
3309 if (input->drag_offer) {
3310 data_offer_destroy(input->drag_offer);
3311 input->drag_offer = NULL;
3316 data_device_motion(void *data, struct wl_data_device *data_device,
3317 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3319 struct input *input = data;
3320 struct window *window = input->drag_focus;
3321 float x = wl_fixed_to_double(x_w);
3322 float y = wl_fixed_to_double(y_w);
3328 if (input->drag_offer)
3329 types_data = input->drag_offer->types.data;
3333 if (window->data_handler)
3334 window->data_handler(window, input, x, y, types_data,
3339 data_device_drop(void *data, struct wl_data_device *data_device)
3341 struct input *input = data;
3342 struct window *window = input->drag_focus;
3348 if (window->drop_handler)
3349 window->drop_handler(window, input,
3350 x, y, window->user_data);
3352 if (input->touch_grab)
3353 touch_ungrab(input);
3357 data_device_selection(void *data,
3358 struct wl_data_device *wl_data_device,
3359 struct wl_data_offer *offer)
3361 struct input *input = data;
3364 if (input->selection_offer)
3365 data_offer_destroy(input->selection_offer);
3368 input->selection_offer = wl_data_offer_get_user_data(offer);
3369 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3372 input->selection_offer = NULL;
3376 static const struct wl_data_device_listener data_device_listener = {
3377 data_device_data_offer,
3382 data_device_selection
3386 input_set_pointer_image_index(struct input *input, int index)
3388 struct wl_buffer *buffer;
3389 struct wl_cursor *cursor;
3390 struct wl_cursor_image *image;
3392 if (!input->pointer)
3395 cursor = input->display->cursors[input->current_cursor];
3399 if (index >= (int) cursor->image_count) {
3400 fprintf(stderr, "cursor index out of range\n");
3404 image = cursor->images[index];
3405 buffer = wl_cursor_image_get_buffer(image);
3409 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3410 input->pointer_surface,
3411 image->hotspot_x, image->hotspot_y);
3412 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3413 wl_surface_damage(input->pointer_surface, 0, 0,
3414 image->width, image->height);
3415 wl_surface_commit(input->pointer_surface);
3418 static const struct wl_callback_listener pointer_surface_listener;
3421 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3424 struct input *input = data;
3425 struct wl_cursor *cursor;
3429 assert(callback == input->cursor_frame_cb);
3430 wl_callback_destroy(callback);
3431 input->cursor_frame_cb = NULL;
3434 if (!input->pointer)
3437 if (input->current_cursor == CURSOR_BLANK) {
3438 wl_pointer_set_cursor(input->pointer,
3439 input->pointer_enter_serial,
3444 if (input->current_cursor == CURSOR_UNSET)
3446 cursor = input->display->cursors[input->current_cursor];
3450 /* FIXME We don't have the current time on the first call so we set
3451 * the animation start to the time of the first frame callback. */
3453 input->cursor_anim_start = 0;
3454 else if (input->cursor_anim_start == 0)
3455 input->cursor_anim_start = time;
3457 if (time == 0 || input->cursor_anim_start == 0)
3460 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3462 if (cursor->image_count > 1) {
3463 input->cursor_frame_cb =
3464 wl_surface_frame(input->pointer_surface);
3465 wl_callback_add_listener(input->cursor_frame_cb,
3466 &pointer_surface_listener, input);
3469 input_set_pointer_image_index(input, i);
3472 static const struct wl_callback_listener pointer_surface_listener = {
3473 pointer_surface_frame_callback
3477 input_set_pointer_image(struct input *input, int pointer)
3481 if (!input->pointer)
3484 if (input->pointer_enter_serial > input->cursor_serial)
3487 if (!force && pointer == input->current_cursor)
3490 input->current_cursor = pointer;
3491 input->cursor_serial = input->pointer_enter_serial;
3492 if (!input->cursor_frame_cb)
3493 pointer_surface_frame_callback(input, NULL, 0);
3495 /* The current frame callback may be stuck if, for instance,
3496 * the set cursor request was processed by the server after
3497 * this client lost the focus. In this case the cursor surface
3498 * might not be mapped and the frame callback wouldn't ever
3499 * complete. Send a set_cursor and attach to try to map the
3500 * cursor surface again so that the callback will finish */
3501 input_set_pointer_image_index(input, 0);
3505 struct wl_data_device *
3506 input_get_data_device(struct input *input)
3508 return input->data_device;
3512 input_set_selection(struct input *input,
3513 struct wl_data_source *source, uint32_t time)
3515 wl_data_device_set_selection(input->data_device, source, time);
3519 input_accept(struct input *input, const char *type)
3521 wl_data_offer_accept(input->drag_offer->offer,
3522 input->drag_enter_serial, type);
3526 offer_io_func(struct task *task, uint32_t events)
3528 struct data_offer *offer =
3529 container_of(task, struct data_offer, io_task);
3533 len = read(offer->fd, buffer, sizeof buffer);
3534 offer->func(buffer, len,
3535 offer->x, offer->y, offer->user_data);
3539 data_offer_destroy(offer);
3544 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3545 data_func_t func, void *user_data)
3549 if (pipe2(p, O_CLOEXEC) == -1)
3552 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3555 offer->io_task.run = offer_io_func;
3559 offer->user_data = user_data;
3561 display_watch_fd(offer->input->display,
3562 offer->fd, EPOLLIN, &offer->io_task);
3566 input_receive_drag_data(struct input *input, const char *mime_type,
3567 data_func_t func, void *data)
3569 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3570 input->drag_offer->x = input->drag_x;
3571 input->drag_offer->y = input->drag_y;
3575 input_receive_drag_data_to_fd(struct input *input,
3576 const char *mime_type, int fd)
3578 if (input->drag_offer)
3579 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3585 input_receive_selection_data(struct input *input, const char *mime_type,
3586 data_func_t func, void *data)
3590 if (input->selection_offer == NULL)
3593 for (p = input->selection_offer->types.data; *p; p++)
3594 if (strcmp(mime_type, *p) == 0)
3600 data_offer_receive_data(input->selection_offer,
3601 mime_type, func, data);
3606 input_receive_selection_data_to_fd(struct input *input,
3607 const char *mime_type, int fd)
3609 if (input->selection_offer)
3610 wl_data_offer_receive(input->selection_offer->offer,
3617 window_move(struct window *window, struct input *input, uint32_t serial)
3619 if (!window->shell_surface)
3622 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3626 surface_set_synchronized(struct surface *surface)
3628 if (!surface->subsurface)
3631 if (surface->synchronized)
3634 wl_subsurface_set_sync(surface->subsurface);
3635 surface->synchronized = 1;
3639 surface_set_synchronized_default(struct surface *surface)
3641 if (!surface->subsurface)
3644 if (surface->synchronized == surface->synchronized_default)
3647 if (surface->synchronized_default)
3648 wl_subsurface_set_sync(surface->subsurface);
3650 wl_subsurface_set_desync(surface->subsurface);
3652 surface->synchronized = surface->synchronized_default;
3656 surface_resize(struct surface *surface)
3658 struct widget *widget = surface->widget;
3659 struct wl_compositor *compositor = widget->window->display->compositor;
3661 if (surface->input_region) {
3662 wl_region_destroy(surface->input_region);
3663 surface->input_region = NULL;
3666 if (surface->opaque_region)
3667 wl_region_destroy(surface->opaque_region);
3669 surface->opaque_region = wl_compositor_create_region(compositor);
3671 if (widget->resize_handler)
3672 widget->resize_handler(widget,
3673 widget->allocation.width,
3674 widget->allocation.height,
3677 if (surface->subsurface &&
3678 (surface->allocation.x != widget->allocation.x ||
3679 surface->allocation.y != widget->allocation.y)) {
3680 wl_subsurface_set_position(surface->subsurface,
3681 widget->allocation.x,
3682 widget->allocation.y);
3684 if (surface->allocation.width != widget->allocation.width ||
3685 surface->allocation.height != widget->allocation.height) {
3686 window_schedule_redraw(widget->window);
3688 surface->allocation = widget->allocation;
3691 wl_region_add(surface->opaque_region, 0, 0,
3692 widget->allocation.width,
3693 widget->allocation.height);
3697 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3700 * This hack should be removed, when EGL respects
3701 * eglSwapInterval(0).
3703 * If this window has sub-surfaces, especially a free-running
3704 * EGL-widget, we need to post the parent surface once with
3705 * all the old state to guarantee, that the EGL-widget will
3706 * receive its frame callback soon. Otherwise, a forced call
3707 * to eglSwapBuffers may end up blocking, waiting for a frame
3708 * event that will never come, because we will commit the parent
3709 * surface with all new state only after eglSwapBuffers returns.
3711 * This assumes, that:
3712 * 1. When the EGL widget's resize hook is called, it pauses.
3713 * 2. When the EGL widget's redraw hook is called, it forces a
3714 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3715 * In a single threaded application condition 1 is a no-op.
3717 * XXX: This should actually be after the surface_resize() calls,
3718 * but cannot, because then it would commit the incomplete state
3719 * accumulated from the widget resize hooks.
3721 if (window->subsurface_list.next != &window->main_surface->link ||
3722 window->subsurface_list.prev != &window->main_surface->link)
3723 wl_surface_commit(window->main_surface->surface);
3727 idle_resize(struct window *window)
3729 struct surface *surface;
3731 window->resize_needed = 0;
3732 window->redraw_needed = 1;
3734 DBG("from %dx%d to %dx%d\n",
3735 window->main_surface->server_allocation.width,
3736 window->main_surface->server_allocation.height,
3737 window->pending_allocation.width,
3738 window->pending_allocation.height);
3740 hack_prevent_EGL_sub_surface_deadlock(window);
3742 widget_set_allocation(window->main_surface->widget,
3743 window->pending_allocation.x,
3744 window->pending_allocation.y,
3745 window->pending_allocation.width,
3746 window->pending_allocation.height);
3748 surface_resize(window->main_surface);
3750 /* The main surface is in the list, too. Main surface's
3751 * resize_handler is responsible for calling widget_set_allocation()
3752 * on all sub-surface root widgets, so they will be resized
3755 wl_list_for_each(surface, &window->subsurface_list, link) {
3756 if (surface == window->main_surface)
3759 surface_set_synchronized(surface);
3760 surface_resize(surface);
3765 window_schedule_resize(struct window *window, int width, int height)
3767 /* We should probably get these numbers from the theme. */
3768 const int min_width = 200, min_height = 200;
3770 window->pending_allocation.x = 0;
3771 window->pending_allocation.y = 0;
3772 window->pending_allocation.width = width;
3773 window->pending_allocation.height = height;
3775 if (window->min_allocation.width == 0) {
3776 if (width < min_width && window->frame)
3777 window->min_allocation.width = min_width;
3779 window->min_allocation.width = width;
3780 if (height < min_height && window->frame)
3781 window->min_allocation.height = min_height;
3783 window->min_allocation.height = height;
3786 if (window->pending_allocation.width < window->min_allocation.width)
3787 window->pending_allocation.width = window->min_allocation.width;
3788 if (window->pending_allocation.height < window->min_allocation.height)
3789 window->pending_allocation.height = window->min_allocation.height;
3791 window->resize_needed = 1;
3792 window_schedule_redraw(window);
3796 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3798 window_schedule_resize(widget->window, width, height);
3802 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3805 wl_shell_surface_pong(shell_surface, serial);
3809 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3810 uint32_t edges, int32_t width, int32_t height)
3812 struct window *window = data;
3814 window->resize_edges = edges;
3815 window_schedule_resize(window, width, height);
3819 menu_destroy(struct menu *menu)
3821 widget_destroy(menu->widget);
3822 window_destroy(menu->window);
3823 frame_destroy(menu->frame);
3828 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3830 struct window *window = data;
3831 struct menu *menu = window->main_surface->widget->user_data;
3833 /* FIXME: Need more context in this event, at least the input
3834 * device. Or just use wl_callback. And this really needs to
3835 * be a window vfunc that the menu can set. And we need the
3838 input_ungrab(menu->input);
3842 static const struct wl_shell_surface_listener shell_surface_listener = {
3849 window_get_allocation(struct window *window,
3850 struct rectangle *allocation)
3852 *allocation = window->main_surface->allocation;
3856 widget_redraw(struct widget *widget)
3858 struct widget *child;
3860 if (widget->redraw_handler)
3861 widget->redraw_handler(widget, widget->user_data);
3862 wl_list_for_each(child, &widget->child_list, link)
3863 widget_redraw(child);
3867 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3869 struct surface *surface = data;
3871 assert(callback == surface->frame_cb);
3872 DBG_OBJ(callback, "done\n");
3873 wl_callback_destroy(callback);
3874 surface->frame_cb = NULL;
3876 surface->last_time = time;
3878 if (surface->redraw_needed || surface->window->redraw_needed) {
3879 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3880 window_schedule_redraw_task(surface->window);
3884 static const struct wl_callback_listener listener = {
3889 surface_redraw(struct surface *surface)
3891 DBG_OBJ(surface->surface, "begin\n");
3893 if (!surface->window->redraw_needed && !surface->redraw_needed)
3896 /* Whole-window redraw forces a redraw even if the previous has
3897 * not yet hit the screen.
3899 if (surface->frame_cb) {
3900 if (!surface->window->redraw_needed)
3903 DBG_OBJ(surface->frame_cb, "cancelled\n");
3904 wl_callback_destroy(surface->frame_cb);
3907 surface->frame_cb = wl_surface_frame(surface->surface);
3908 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3909 DBG_OBJ(surface->frame_cb, "new\n");
3911 surface->redraw_needed = 0;
3912 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3913 widget_redraw(surface->widget);
3914 DBG_OBJ(surface->surface, "done\n");
3918 idle_redraw(struct task *task, uint32_t events)
3920 struct window *window = container_of(task, struct window, redraw_task);
3921 struct surface *surface;
3923 DBG(" --------- \n");
3925 wl_list_init(&window->redraw_task.link);
3926 window->redraw_task_scheduled = 0;
3928 if (window->resize_needed) {
3929 /* throttle resizing to the main surface display */
3930 if (window->main_surface->frame_cb) {
3931 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
3935 idle_resize(window);
3938 wl_list_for_each(surface, &window->subsurface_list, link)
3939 surface_redraw(surface);
3941 window->redraw_needed = 0;
3942 window_flush(window);
3944 wl_list_for_each(surface, &window->subsurface_list, link)
3945 surface_set_synchronized_default(surface);
3949 window_schedule_redraw_task(struct window *window)
3951 if (window->configure_requests)
3953 if (!window->redraw_task_scheduled) {
3954 window->redraw_task.run = idle_redraw;
3955 display_defer(window->display, &window->redraw_task);
3956 window->redraw_task_scheduled = 1;
3961 window_schedule_redraw(struct window *window)
3963 struct surface *surface;
3965 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3967 wl_list_for_each(surface, &window->subsurface_list, link)
3968 surface->redraw_needed = 1;
3970 window_schedule_redraw_task(window);
3974 window_is_fullscreen(struct window *window)
3976 return window->type == TYPE_FULLSCREEN;
3980 window_is_transient(struct window *window)
3982 return window->type == TYPE_TRANSIENT;
3986 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3988 struct window *window = data;
3990 wl_callback_destroy(callback);
3991 window->configure_requests--;
3993 if (!window->configure_requests)
3994 window_schedule_redraw(window);
3997 static struct wl_callback_listener configure_request_listener = {
3998 configure_request_completed,
4002 window_defer_redraw_until_configure(struct window* window)
4004 struct wl_callback *callback;
4006 if (window->redraw_task_scheduled) {
4007 wl_list_remove(&window->redraw_task.link);
4008 window->redraw_task_scheduled = 0;
4011 callback = wl_display_sync(window->display->display);
4012 wl_callback_add_listener(callback, &configure_request_listener, window);
4013 window->configure_requests++;
4017 window_set_fullscreen(struct window *window, int fullscreen)
4019 if (!window->display->shell)
4022 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4026 window->saved_type = window->type;
4027 if (window->type == TYPE_TOPLEVEL) {
4028 window->saved_allocation = window->main_surface->allocation;
4030 window->type = TYPE_FULLSCREEN;
4031 wl_shell_surface_set_fullscreen(window->shell_surface,
4032 window->fullscreen_method,
4034 window_defer_redraw_until_configure (window);
4036 if (window->saved_type == TYPE_MAXIMIZED) {
4037 window_set_maximized(window, 1);
4039 window->type = TYPE_TOPLEVEL;
4040 wl_shell_surface_set_toplevel(window->shell_surface);
4041 window_schedule_resize(window,
4042 window->saved_allocation.width,
4043 window->saved_allocation.height);
4050 window_set_fullscreen_method(struct window *window,
4051 enum wl_shell_surface_fullscreen_method method)
4053 window->fullscreen_method = method;
4057 window_is_maximized(struct window *window)
4059 return window->type == TYPE_MAXIMIZED;
4063 window_set_maximized(struct window *window, int maximized)
4065 if (!window->display->shell)
4068 if ((window->type == TYPE_MAXIMIZED) == maximized)
4071 if (window->type == TYPE_TOPLEVEL) {
4072 window->saved_allocation = window->main_surface->allocation;
4073 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4074 window->type = TYPE_MAXIMIZED;
4075 window_defer_redraw_until_configure(window);
4076 } else if (window->type == TYPE_FULLSCREEN) {
4077 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4078 window->type = TYPE_MAXIMIZED;
4079 window_defer_redraw_until_configure(window);
4081 wl_shell_surface_set_toplevel(window->shell_surface);
4082 window->type = TYPE_TOPLEVEL;
4083 window_schedule_resize(window,
4084 window->saved_allocation.width,
4085 window->saved_allocation.height);
4090 window_set_user_data(struct window *window, void *data)
4092 window->user_data = data;
4096 window_get_user_data(struct window *window)
4098 return window->user_data;
4102 window_set_key_handler(struct window *window,
4103 window_key_handler_t handler)
4105 window->key_handler = handler;
4109 window_set_keyboard_focus_handler(struct window *window,
4110 window_keyboard_focus_handler_t handler)
4112 window->keyboard_focus_handler = handler;
4116 window_set_data_handler(struct window *window, window_data_handler_t handler)
4118 window->data_handler = handler;
4122 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4124 window->drop_handler = handler;
4128 window_set_close_handler(struct window *window,
4129 window_close_handler_t handler)
4131 window->close_handler = handler;
4135 window_set_fullscreen_handler(struct window *window,
4136 window_fullscreen_handler_t handler)
4138 window->fullscreen_handler = handler;
4142 window_set_output_handler(struct window *window,
4143 window_output_handler_t handler)
4145 window->output_handler = handler;
4149 window_set_title(struct window *window, const char *title)
4151 free(window->title);
4152 window->title = strdup(title);
4153 if (window->frame) {
4154 frame_set_title(window->frame->frame, title);
4155 widget_schedule_redraw(window->frame->widget);
4157 if (window->shell_surface)
4158 wl_shell_surface_set_title(window->shell_surface, title);
4162 window_get_title(struct window *window)
4164 return window->title;
4168 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4170 struct text_cursor_position *text_cursor_position =
4171 window->display->text_cursor_position;
4173 if (!text_cursor_position)
4176 text_cursor_position_notify(text_cursor_position,
4177 window->main_surface->surface,
4178 wl_fixed_from_int(x),
4179 wl_fixed_from_int(y));
4183 window_damage(struct window *window, int32_t x, int32_t y,
4184 int32_t width, int32_t height)
4186 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4190 surface_enter(void *data,
4191 struct wl_surface *wl_surface, struct wl_output *wl_output)
4193 struct window *window = data;
4194 struct output *output;
4195 struct output *output_found = NULL;
4196 struct window_output *window_output;
4198 wl_list_for_each(output, &window->display->output_list, link) {
4199 if (output->output == wl_output) {
4200 output_found = output;
4208 window_output = xmalloc(sizeof *window_output);
4209 window_output->output = output_found;
4211 wl_list_insert (&window->window_output_list, &window_output->link);
4213 if (window->output_handler)
4214 window->output_handler(window, output_found, 1,
4219 surface_leave(void *data,
4220 struct wl_surface *wl_surface, struct wl_output *output)
4222 struct window *window = data;
4223 struct window_output *window_output;
4224 struct window_output *window_output_found = NULL;
4226 wl_list_for_each(window_output, &window->window_output_list, link) {
4227 if (window_output->output->output == output) {
4228 window_output_found = window_output;
4233 if (window_output_found) {
4234 wl_list_remove(&window_output_found->link);
4236 if (window->output_handler)
4237 window->output_handler(window, window_output->output,
4238 0, window->user_data);
4240 free(window_output_found);
4244 static const struct wl_surface_listener surface_listener = {
4249 static struct surface *
4250 surface_create(struct window *window)
4252 struct display *display = window->display;
4253 struct surface *surface;
4255 surface = xmalloc(sizeof *surface);
4256 memset(surface, 0, sizeof *surface);
4260 surface->window = window;
4261 surface->surface = wl_compositor_create_surface(display->compositor);
4262 surface->buffer_scale = 1;
4263 wl_surface_add_listener(surface->surface, &surface_listener, window);
4265 wl_list_insert(&window->subsurface_list, &surface->link);
4270 static struct window *
4271 window_create_internal(struct display *display, int type)
4273 struct window *window;
4274 struct surface *surface;
4276 window = xzalloc(sizeof *window);
4277 wl_list_init(&window->subsurface_list);
4278 window->display = display;
4280 surface = surface_create(window);
4281 window->main_surface = surface;
4283 if (type != TYPE_CUSTOM && display->shell) {
4284 window->shell_surface =
4285 wl_shell_get_shell_surface(display->shell,
4287 fail_on_null(window->shell_surface);
4290 window->type = type;
4291 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4292 window->configure_requests = 0;
4293 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4295 if (display->argb_device)
4296 #ifdef HAVE_CAIRO_EGL
4297 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4299 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4302 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4304 wl_surface_set_user_data(surface->surface, window);
4305 wl_list_insert(display->window_list.prev, &window->link);
4306 wl_list_init(&window->redraw_task.link);
4308 if (window->shell_surface) {
4309 wl_shell_surface_set_user_data(window->shell_surface, window);
4310 wl_shell_surface_add_listener(window->shell_surface,
4311 &shell_surface_listener, window);
4314 wl_list_init (&window->window_output_list);
4320 window_create(struct display *display)
4322 return window_create_internal(display, TYPE_NONE);
4326 window_create_custom(struct display *display)
4328 return window_create_internal(display, TYPE_CUSTOM);
4332 window_create_transient(struct display *display, struct window *parent,
4333 int32_t x, int32_t y, uint32_t flags)
4335 struct window *window;
4337 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4343 wl_shell_surface_set_transient(
4344 window->shell_surface,
4345 parent->main_surface->surface,
4346 window->x, window->y, flags);
4352 menu_set_item(struct menu *menu, int sy)
4354 int32_t x, y, width, height;
4357 frame_interior(menu->frame, &x, &y, &width, &height);
4358 next = (sy - y) / 20;
4359 if (menu->current != next) {
4360 menu->current = next;
4361 widget_schedule_redraw(menu->widget);
4366 menu_motion_handler(struct widget *widget,
4367 struct input *input, uint32_t time,
4368 float x, float y, void *data)
4370 struct menu *menu = data;
4372 if (widget == menu->widget)
4373 menu_set_item(data, y);
4375 return CURSOR_LEFT_PTR;
4379 menu_enter_handler(struct widget *widget,
4380 struct input *input, float x, float y, void *data)
4382 struct menu *menu = data;
4384 if (widget == menu->widget)
4385 menu_set_item(data, y);
4387 return CURSOR_LEFT_PTR;
4391 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4393 struct menu *menu = data;
4395 if (widget == menu->widget)
4396 menu_set_item(data, -200);
4400 menu_button_handler(struct widget *widget,
4401 struct input *input, uint32_t time,
4402 uint32_t button, enum wl_pointer_button_state state,
4406 struct menu *menu = data;
4408 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4409 (menu->release_count > 0 || time - menu->time > 500)) {
4410 /* Either relase after press-drag-release or
4411 * click-motion-click. */
4412 menu->func(menu->parent, input,
4413 menu->current, menu->parent->user_data);
4414 input_ungrab(input);
4416 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4417 menu->release_count++;
4422 menu_redraw_handler(struct widget *widget, void *data)
4425 struct menu *menu = data;
4426 int32_t x, y, width, height, i;
4428 cr = widget_cairo_create(widget);
4430 frame_repaint(menu->frame, cr);
4431 frame_interior(menu->frame, &x, &y, &width, &height);
4433 theme_set_background_source(menu->window->display->theme,
4434 cr, THEME_FRAME_ACTIVE);
4435 cairo_rectangle(cr, x, y, width, height);
4438 cairo_select_font_face(cr, "sans",
4439 CAIRO_FONT_SLANT_NORMAL,
4440 CAIRO_FONT_WEIGHT_NORMAL);
4441 cairo_set_font_size(cr, 12);
4443 for (i = 0; i < menu->count; i++) {
4444 if (i == menu->current) {
4445 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4446 cairo_rectangle(cr, x, y + i * 20, width, 20);
4448 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4449 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4450 cairo_show_text(cr, menu->entries[i]);
4452 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4453 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4454 cairo_show_text(cr, menu->entries[i]);
4462 window_show_menu(struct display *display,
4463 struct input *input, uint32_t time, struct window *parent,
4464 int32_t x, int32_t y,
4465 menu_func_t func, const char **entries, int count)
4467 struct window *window;
4471 menu = malloc(sizeof *menu);
4475 window = window_create_internal(parent->display, TYPE_MENU);
4481 menu->window = window;
4482 menu->parent = parent;
4483 menu->widget = window_add_widget(menu->window, menu);
4484 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4485 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4486 menu->frame = frame_create(window->display->theme, 0, 0,
4487 FRAME_BUTTON_NONE, NULL);
4488 menu->entries = entries;
4489 menu->count = count;
4490 menu->release_count = 0;
4494 menu->input = input;
4495 window->type = TYPE_MENU;
4499 input_ungrab(input);
4501 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4502 widget_set_enter_handler(menu->widget, menu_enter_handler);
4503 widget_set_leave_handler(menu->widget, menu_leave_handler);
4504 widget_set_motion_handler(menu->widget, menu_motion_handler);
4505 widget_set_button_handler(menu->widget, menu_button_handler);
4507 input_grab(input, menu->widget, 0);
4508 frame_resize_inside(menu->frame, 200, count * 20);
4509 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4510 window_schedule_resize(window, frame_width(menu->frame),
4511 frame_height(menu->frame));
4513 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4514 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4515 display_get_serial(window->display),
4516 parent->main_surface->surface,
4517 window->x - ix, window->y - iy, 0);
4521 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4523 window->main_surface->buffer_type = type;
4527 window_set_preferred_format(struct window *window,
4528 enum preferred_format format)
4530 window->preferred_format = format;
4534 window_add_subsurface(struct window *window, void *data,
4535 enum subsurface_mode default_mode)
4537 struct widget *widget;
4538 struct surface *surface;
4539 struct wl_surface *parent;
4540 struct wl_subcompositor *subcompo = window->display->subcompositor;
4542 surface = surface_create(window);
4543 widget = widget_create(window, surface, data);
4544 wl_list_init(&widget->link);
4545 surface->widget = widget;
4547 parent = window->main_surface->surface;
4548 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4551 surface->synchronized = 1;
4553 switch (default_mode) {
4554 case SUBSURFACE_SYNCHRONIZED:
4555 surface->synchronized_default = 1;
4557 case SUBSURFACE_DESYNCHRONIZED:
4558 surface->synchronized_default = 0;
4561 assert(!"bad enum subsurface_mode");
4568 display_handle_geometry(void *data,
4569 struct wl_output *wl_output,
4572 int physical_height,
4578 struct output *output = data;
4580 output->allocation.x = x;
4581 output->allocation.y = y;
4582 output->transform = transform;
4586 display_handle_done(void *data,
4587 struct wl_output *wl_output)
4592 display_handle_scale(void *data,
4593 struct wl_output *wl_output,
4596 struct output *output = data;
4598 output->scale = scale;
4602 display_handle_mode(void *data,
4603 struct wl_output *wl_output,
4609 struct output *output = data;
4610 struct display *display = output->display;
4612 if (flags & WL_OUTPUT_MODE_CURRENT) {
4613 output->allocation.width = width;
4614 output->allocation.height = height;
4615 if (display->output_configure_handler)
4616 (*display->output_configure_handler)(
4617 output, display->user_data);
4621 static const struct wl_output_listener output_listener = {
4622 display_handle_geometry,
4623 display_handle_mode,
4624 display_handle_done,
4625 display_handle_scale
4629 display_add_output(struct display *d, uint32_t id)
4631 struct output *output;
4633 output = xzalloc(sizeof *output);
4634 output->display = d;
4637 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4638 output->server_output_id = id;
4639 wl_list_insert(d->output_list.prev, &output->link);
4641 wl_output_add_listener(output->output, &output_listener, output);
4645 output_destroy(struct output *output)
4647 if (output->destroy_handler)
4648 (*output->destroy_handler)(output, output->user_data);
4650 wl_output_destroy(output->output);
4651 wl_list_remove(&output->link);
4656 display_destroy_output(struct display *d, uint32_t id)
4658 struct output *output;
4660 wl_list_for_each(output, &d->output_list, link) {
4661 if (output->server_output_id == id) {
4662 output_destroy(output);
4669 display_set_global_handler(struct display *display,
4670 display_global_handler_t handler)
4672 struct global *global;
4674 display->global_handler = handler;
4678 wl_list_for_each(global, &display->global_list, link)
4679 display->global_handler(display,
4680 global->name, global->interface,
4681 global->version, display->user_data);
4685 display_set_global_handler_remove(struct display *display,
4686 display_global_handler_t remove_handler)
4688 display->global_handler_remove = remove_handler;
4689 if (!remove_handler)
4694 display_set_output_configure_handler(struct display *display,
4695 display_output_handler_t handler)
4697 struct output *output;
4699 display->output_configure_handler = handler;
4703 wl_list_for_each(output, &display->output_list, link) {
4704 if (output->allocation.width == 0 &&
4705 output->allocation.height == 0)
4708 (*display->output_configure_handler)(output,
4709 display->user_data);
4714 output_set_user_data(struct output *output, void *data)
4716 output->user_data = data;
4720 output_get_user_data(struct output *output)
4722 return output->user_data;
4726 output_set_destroy_handler(struct output *output,
4727 display_output_handler_t handler)
4729 output->destroy_handler = handler;
4730 /* FIXME: implement this, once we have way to remove outputs */
4734 output_get_allocation(struct output *output, struct rectangle *base)
4736 struct rectangle allocation = output->allocation;
4738 switch (output->transform) {
4739 case WL_OUTPUT_TRANSFORM_90:
4740 case WL_OUTPUT_TRANSFORM_270:
4741 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4742 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4743 /* Swap width and height */
4744 allocation.width = output->allocation.height;
4745 allocation.height = output->allocation.width;
4753 output_get_wl_output(struct output *output)
4755 return output->output;
4758 enum wl_output_transform
4759 output_get_transform(struct output *output)
4761 return output->transform;
4765 output_get_scale(struct output *output)
4767 return output->scale;
4771 fini_xkb(struct input *input)
4773 xkb_state_unref(input->xkb.state);
4774 xkb_map_unref(input->xkb.keymap);
4777 #define MIN(a,b) ((a) < (b) ? a : b)
4780 display_add_input(struct display *d, uint32_t id)
4782 struct input *input;
4784 input = xzalloc(sizeof *input);
4786 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4787 MIN(d->seat_version, 3));
4788 input->touch_focus = NULL;
4789 input->pointer_focus = NULL;
4790 input->keyboard_focus = NULL;
4791 wl_list_init(&input->touch_point_list);
4792 wl_list_insert(d->input_list.prev, &input->link);
4794 wl_seat_add_listener(input->seat, &seat_listener, input);
4795 wl_seat_set_user_data(input->seat, input);
4797 input->data_device =
4798 wl_data_device_manager_get_data_device(d->data_device_manager,
4800 wl_data_device_add_listener(input->data_device, &data_device_listener,
4803 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4805 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4806 TFD_CLOEXEC | TFD_NONBLOCK);
4807 input->repeat_task.run = keyboard_repeat_func;
4808 display_watch_fd(d, input->repeat_timer_fd,
4809 EPOLLIN, &input->repeat_task);
4813 input_destroy(struct input *input)
4815 input_remove_keyboard_focus(input);
4816 input_remove_pointer_focus(input);
4818 if (input->drag_offer)
4819 data_offer_destroy(input->drag_offer);
4821 if (input->selection_offer)
4822 data_offer_destroy(input->selection_offer);
4824 wl_data_device_destroy(input->data_device);
4826 if (input->display->seat_version >= 3) {
4828 wl_pointer_release(input->pointer);
4829 if (input->keyboard)
4830 wl_keyboard_release(input->keyboard);
4835 wl_surface_destroy(input->pointer_surface);
4837 wl_list_remove(&input->link);
4838 wl_seat_destroy(input->seat);
4839 close(input->repeat_timer_fd);
4844 init_workspace_manager(struct display *d, uint32_t id)
4846 d->workspace_manager =
4847 wl_registry_bind(d->registry, id,
4848 &workspace_manager_interface, 1);
4849 if (d->workspace_manager != NULL)
4850 workspace_manager_add_listener(d->workspace_manager,
4851 &workspace_manager_listener,
4856 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4858 struct display *d = data;
4860 if (format == WL_SHM_FORMAT_RGB565)
4864 struct wl_shm_listener shm_listener = {
4869 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4870 const char *interface, uint32_t version)
4872 struct display *d = data;
4873 struct global *global;
4875 global = xmalloc(sizeof *global);
4877 global->interface = strdup(interface);
4878 global->version = version;
4879 wl_list_insert(d->global_list.prev, &global->link);
4881 if (strcmp(interface, "wl_compositor") == 0) {
4882 d->compositor = wl_registry_bind(registry, id,
4883 &wl_compositor_interface, 3);
4884 } else if (strcmp(interface, "wl_output") == 0) {
4885 display_add_output(d, id);
4886 } else if (strcmp(interface, "wl_seat") == 0) {
4887 d->seat_version = version;
4888 display_add_input(d, id);
4889 } else if (strcmp(interface, "wl_shell") == 0) {
4890 d->shell = wl_registry_bind(registry,
4891 id, &wl_shell_interface, 1);
4892 } else if (strcmp(interface, "wl_shm") == 0) {
4893 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4894 wl_shm_add_listener(d->shm, &shm_listener, d);
4895 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4896 d->data_device_manager =
4897 wl_registry_bind(registry, id,
4898 &wl_data_device_manager_interface, 1);
4899 } else if (strcmp(interface, "text_cursor_position") == 0) {
4900 d->text_cursor_position =
4901 wl_registry_bind(registry, id,
4902 &text_cursor_position_interface, 1);
4903 } else if (strcmp(interface, "workspace_manager") == 0) {
4904 init_workspace_manager(d, id);
4905 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4907 wl_registry_bind(registry, id,
4908 &wl_subcompositor_interface, 1);
4911 if (d->global_handler)
4912 d->global_handler(d, id, interface, version, d->user_data);
4916 registry_handle_global_remove(void *data, struct wl_registry *registry,
4919 struct display *d = data;
4920 struct global *global;
4923 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4924 if (global->name != name)
4927 if (strcmp(global->interface, "wl_output") == 0)
4928 display_destroy_output(d, name);
4930 /* XXX: Should destroy remaining bound globals */
4932 if (d->global_handler_remove)
4933 d->global_handler_remove(d, name, global->interface,
4934 global->version, d->user_data);
4936 wl_list_remove(&global->link);
4937 free(global->interface);
4943 display_bind(struct display *display, uint32_t name,
4944 const struct wl_interface *interface, uint32_t version)
4946 return wl_registry_bind(display->registry, name, interface, version);
4949 static const struct wl_registry_listener registry_listener = {
4950 registry_handle_global,
4951 registry_handle_global_remove
4954 #ifdef HAVE_CAIRO_EGL
4956 init_egl(struct display *d)
4958 EGLint major, minor;
4961 #ifdef USE_CAIRO_GLESV2
4962 # define GL_BIT EGL_OPENGL_ES2_BIT
4964 # define GL_BIT EGL_OPENGL_BIT
4967 static const EGLint argb_cfg_attribs[] = {
4968 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4974 EGL_RENDERABLE_TYPE, GL_BIT,
4978 #ifdef USE_CAIRO_GLESV2
4979 static const EGLint context_attribs[] = {
4980 EGL_CONTEXT_CLIENT_VERSION, 2,
4983 EGLint api = EGL_OPENGL_ES_API;
4985 EGLint *context_attribs = NULL;
4986 EGLint api = EGL_OPENGL_API;
4989 d->dpy = eglGetDisplay(d->display);
4990 if (!eglInitialize(d->dpy, &major, &minor)) {
4991 fprintf(stderr, "failed to initialize EGL\n");
4995 if (!eglBindAPI(api)) {
4996 fprintf(stderr, "failed to bind EGL client API\n");
5000 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5001 &d->argb_config, 1, &n) || n != 1) {
5002 fprintf(stderr, "failed to choose argb EGL config\n");
5006 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5007 EGL_NO_CONTEXT, context_attribs);
5008 if (d->argb_ctx == NULL) {
5009 fprintf(stderr, "failed to create EGL context\n");
5013 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5014 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5015 fprintf(stderr, "failed to get cairo EGL argb device\n");
5023 fini_egl(struct display *display)
5025 cairo_device_destroy(display->argb_device);
5027 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5030 eglTerminate(display->dpy);
5036 init_dummy_surface(struct display *display)
5041 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5043 display->dummy_surface =
5044 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5046 display->dummy_surface_data = data;
5050 handle_display_data(struct task *task, uint32_t events)
5052 struct display *display =
5053 container_of(task, struct display, display_task);
5054 struct epoll_event ep;
5057 display->display_fd_events = events;
5059 if (events & EPOLLERR || events & EPOLLHUP) {
5060 display_exit(display);
5064 if (events & EPOLLIN) {
5065 ret = wl_display_dispatch(display->display);
5067 display_exit(display);
5072 if (events & EPOLLOUT) {
5073 ret = wl_display_flush(display->display);
5075 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5076 ep.data.ptr = &display->display_task;
5077 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5078 display->display_fd, &ep);
5079 } else if (ret == -1 && errno != EAGAIN) {
5080 display_exit(display);
5087 log_handler(const char *format, va_list args)
5089 vfprintf(stderr, format, args);
5093 display_create(int *argc, char *argv[])
5097 wl_log_set_handler_client(log_handler);
5099 d = zalloc(sizeof *d);
5103 d->display = wl_display_connect(NULL);
5104 if (d->display == NULL) {
5105 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5110 d->xkb_context = xkb_context_new(0);
5111 if (d->xkb_context == NULL) {
5112 fprintf(stderr, "Failed to create XKB context\n");
5117 d->epoll_fd = os_epoll_create_cloexec();
5118 d->display_fd = wl_display_get_fd(d->display);
5119 d->display_task.run = handle_display_data;
5120 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5123 wl_list_init(&d->deferred_list);
5124 wl_list_init(&d->input_list);
5125 wl_list_init(&d->output_list);
5126 wl_list_init(&d->global_list);
5129 d->workspace_count = 1;
5131 d->registry = wl_display_get_registry(d->display);
5132 wl_registry_add_listener(d->registry, ®istry_listener, d);
5134 if (wl_display_dispatch(d->display) < 0) {
5135 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5139 #ifdef HAVE_CAIRO_EGL
5140 if (init_egl(d) < 0)
5141 fprintf(stderr, "EGL does not seem to work, "
5142 "falling back to software rendering and wl_shm.\n");
5147 d->theme = theme_create();
5149 wl_list_init(&d->window_list);
5151 init_dummy_surface(d);
5157 display_destroy_outputs(struct display *display)
5160 struct output *output;
5162 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5163 output_destroy(output);
5167 display_destroy_inputs(struct display *display)
5170 struct input *input;
5172 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5173 input_destroy(input);
5177 display_destroy(struct display *display)
5179 if (!wl_list_empty(&display->window_list))
5180 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5181 wl_list_length(&display->window_list));
5183 if (!wl_list_empty(&display->deferred_list))
5184 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5186 cairo_surface_destroy(display->dummy_surface);
5187 free(display->dummy_surface_data);
5189 display_destroy_outputs(display);
5190 display_destroy_inputs(display);
5192 xkb_context_unref(display->xkb_context);
5194 theme_destroy(display->theme);
5195 destroy_cursors(display);
5197 #ifdef HAVE_CAIRO_EGL
5198 if (display->argb_device)
5202 if (display->subcompositor)
5203 wl_subcompositor_destroy(display->subcompositor);
5206 wl_shell_destroy(display->shell);
5209 wl_shm_destroy(display->shm);
5211 if (display->data_device_manager)
5212 wl_data_device_manager_destroy(display->data_device_manager);
5214 wl_compositor_destroy(display->compositor);
5215 wl_registry_destroy(display->registry);
5217 close(display->epoll_fd);
5219 if (!(display->display_fd_events & EPOLLERR) &&
5220 !(display->display_fd_events & EPOLLHUP))
5221 wl_display_flush(display->display);
5223 wl_display_disconnect(display->display);
5228 display_set_user_data(struct display *display, void *data)
5230 display->user_data = data;
5234 display_get_user_data(struct display *display)
5236 return display->user_data;
5240 display_get_display(struct display *display)
5242 return display->display;
5246 display_has_subcompositor(struct display *display)
5248 if (display->subcompositor)
5251 wl_display_roundtrip(display->display);
5253 return display->subcompositor != NULL;
5257 display_get_cairo_device(struct display *display)
5259 return display->argb_device;
5263 display_get_output(struct display *display)
5265 return container_of(display->output_list.next, struct output, link);
5268 struct wl_compositor *
5269 display_get_compositor(struct display *display)
5271 return display->compositor;
5275 display_get_serial(struct display *display)
5277 return display->serial;
5281 display_get_egl_display(struct display *d)
5286 struct wl_data_source *
5287 display_create_data_source(struct display *display)
5289 return wl_data_device_manager_create_data_source(display->data_device_manager);
5293 display_get_argb_egl_config(struct display *d)
5295 return d->argb_config;
5299 display_acquire_window_surface(struct display *display,
5300 struct window *window,
5303 struct surface *surface = window->main_surface;
5305 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5308 widget_get_cairo_surface(window->main_surface->widget);
5309 return surface->toysurface->acquire(surface->toysurface, ctx);
5313 display_release_window_surface(struct display *display,
5314 struct window *window)
5316 struct surface *surface = window->main_surface;
5318 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5321 surface->toysurface->release(surface->toysurface);
5325 display_defer(struct display *display, struct task *task)
5327 wl_list_insert(&display->deferred_list, &task->link);
5331 display_watch_fd(struct display *display,
5332 int fd, uint32_t events, struct task *task)
5334 struct epoll_event ep;
5338 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5342 display_unwatch_fd(struct display *display, int fd)
5344 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5348 display_run(struct display *display)
5351 struct epoll_event ep[16];
5354 display->running = 1;
5356 while (!wl_list_empty(&display->deferred_list)) {
5357 task = container_of(display->deferred_list.prev,
5359 wl_list_remove(&task->link);
5363 wl_display_dispatch_pending(display->display);
5365 if (!display->running)
5368 ret = wl_display_flush(display->display);
5369 if (ret < 0 && errno == EAGAIN) {
5371 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5372 ep[0].data.ptr = &display->display_task;
5374 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5375 display->display_fd, &ep[0]);
5376 } else if (ret < 0) {
5380 count = epoll_wait(display->epoll_fd,
5381 ep, ARRAY_LENGTH(ep), -1);
5382 for (i = 0; i < count; i++) {
5383 task = ep[i].data.ptr;
5384 task->run(task, ep[i].events);
5390 display_exit(struct display *display)
5392 display->running = 0;
5396 keysym_modifiers_add(struct wl_array *modifiers_map,
5399 size_t len = strlen(name) + 1;
5402 p = wl_array_add(modifiers_map, len);
5407 strncpy(p, name, len);
5410 static xkb_mod_index_t
5411 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5414 xkb_mod_index_t index = 0;
5415 char *p = modifiers_map->data;
5417 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5418 if (strcmp(p, name) == 0)
5425 return XKB_MOD_INVALID;
5429 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5432 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5434 if (index == XKB_MOD_INVALID)
5435 return XKB_MOD_INVALID;
5441 fail_on_null(void *p)
5444 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5454 return fail_on_null(malloc(s));
5460 return fail_on_null(zalloc(s));
5464 xstrdup(const char *s)
5466 return fail_on_null(strdup(s));