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 if (!leaf->cairo_surface)
1089 wl_buffer_add_listener(leaf->data->buffer,
1090 &shm_surface_buffer_listener, surface);
1093 surface->current = leaf;
1095 return cairo_surface_reference(leaf->cairo_surface);
1099 shm_surface_swap(struct toysurface *base,
1100 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1101 struct rectangle *server_allocation)
1103 struct shm_surface *surface = to_shm_surface(base);
1104 struct shm_surface_leaf *leaf = surface->current;
1106 server_allocation->width =
1107 cairo_image_surface_get_width(leaf->cairo_surface);
1108 server_allocation->height =
1109 cairo_image_surface_get_height(leaf->cairo_surface);
1111 buffer_to_surface_size (buffer_transform, buffer_scale,
1112 &server_allocation->width,
1113 &server_allocation->height);
1115 wl_surface_attach(surface->surface, leaf->data->buffer,
1116 surface->dx, surface->dy);
1117 wl_surface_damage(surface->surface, 0, 0,
1118 server_allocation->width, server_allocation->height);
1119 wl_surface_commit(surface->surface);
1121 DBG_OBJ(surface->surface, "leaf %d busy\n",
1122 (int)(leaf - &surface->leaf[0]));
1125 surface->current = NULL;
1129 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1135 shm_surface_release(struct toysurface *base)
1140 shm_surface_destroy(struct toysurface *base)
1142 struct shm_surface *surface = to_shm_surface(base);
1145 for (i = 0; i < MAX_LEAVES; i++)
1146 shm_surface_leaf_release(&surface->leaf[i]);
1151 static struct toysurface *
1152 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1153 uint32_t flags, struct rectangle *rectangle)
1155 struct shm_surface *surface;
1156 DBG_OBJ(wl_surface, "\n");
1158 surface = xmalloc(sizeof *surface);
1159 memset(surface, 0, sizeof *surface);
1164 surface->base.prepare = shm_surface_prepare;
1165 surface->base.swap = shm_surface_swap;
1166 surface->base.acquire = shm_surface_acquire;
1167 surface->base.release = shm_surface_release;
1168 surface->base.destroy = shm_surface_destroy;
1170 surface->display = display;
1171 surface->surface = wl_surface;
1172 surface->flags = flags;
1174 return &surface->base;
1178 * The following correspondences between file names and cursors was copied
1179 * from: https://bugs.kde.org/attachment.cgi?id=67313
1182 static const char *bottom_left_corners[] = {
1183 "bottom_left_corner",
1188 static const char *bottom_right_corners[] = {
1189 "bottom_right_corner",
1194 static const char *bottom_sides[] = {
1200 static const char *grabbings[] = {
1203 "208530c400c041818281048008011002"
1206 static const char *left_ptrs[] = {
1213 static const char *left_sides[] = {
1219 static const char *right_sides[] = {
1225 static const char *top_left_corners[] = {
1231 static const char *top_right_corners[] = {
1237 static const char *top_sides[] = {
1243 static const char *xterms[] = {
1249 static const char *hand1s[] = {
1253 "e29285e634086352946a0e7090d73106"
1256 static const char *watches[] = {
1259 "0426c94ea35c87780ff01dc239897213"
1262 struct cursor_alternatives {
1267 static const struct cursor_alternatives cursors[] = {
1268 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1269 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1270 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1271 {grabbings, ARRAY_LENGTH(grabbings)},
1272 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1273 {left_sides, ARRAY_LENGTH(left_sides)},
1274 {right_sides, ARRAY_LENGTH(right_sides)},
1275 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1276 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1277 {top_sides, ARRAY_LENGTH(top_sides)},
1278 {xterms, ARRAY_LENGTH(xterms)},
1279 {hand1s, ARRAY_LENGTH(hand1s)},
1280 {watches, ARRAY_LENGTH(watches)},
1284 create_cursors(struct display *display)
1286 struct weston_config *config;
1287 struct weston_config_section *s;
1291 struct wl_cursor *cursor;
1293 config = weston_config_parse("weston.ini");
1294 s = weston_config_get_section(config, "shell", NULL, NULL);
1295 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1296 weston_config_section_get_int(s, "cursor-size", &size, 32);
1297 weston_config_destroy(config);
1299 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1302 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1304 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1306 for (j = 0; !cursor && j < cursors[i].count; ++j)
1307 cursor = wl_cursor_theme_get_cursor(
1308 display->cursor_theme, cursors[i].names[j]);
1311 fprintf(stderr, "could not load cursor '%s'\n",
1312 cursors[i].names[0]);
1314 display->cursors[i] = cursor;
1319 destroy_cursors(struct display *display)
1321 wl_cursor_theme_destroy(display->cursor_theme);
1322 free(display->cursors);
1325 struct wl_cursor_image *
1326 display_get_pointer_image(struct display *display, int pointer)
1328 struct wl_cursor *cursor = display->cursors[pointer];
1330 return cursor ? cursor->images[0] : NULL;
1334 surface_flush(struct surface *surface)
1336 if (!surface->cairo_surface)
1339 if (surface->opaque_region) {
1340 wl_surface_set_opaque_region(surface->surface,
1341 surface->opaque_region);
1342 wl_region_destroy(surface->opaque_region);
1343 surface->opaque_region = NULL;
1346 if (surface->input_region) {
1347 wl_surface_set_input_region(surface->surface,
1348 surface->input_region);
1349 wl_region_destroy(surface->input_region);
1350 surface->input_region = NULL;
1353 surface->toysurface->swap(surface->toysurface,
1354 surface->buffer_transform, surface->buffer_scale,
1355 &surface->server_allocation);
1357 cairo_surface_destroy(surface->cairo_surface);
1358 surface->cairo_surface = NULL;
1362 window_has_focus(struct window *window)
1364 return window->focus_count > 0;
1368 window_flush(struct window *window)
1370 struct surface *surface;
1372 if (window->type == TYPE_NONE) {
1373 window->type = TYPE_TOPLEVEL;
1374 if (window->shell_surface)
1375 wl_shell_surface_set_toplevel(window->shell_surface);
1378 wl_list_for_each(surface, &window->subsurface_list, link) {
1379 if (surface == window->main_surface)
1382 surface_flush(surface);
1385 surface_flush(window->main_surface);
1389 window_get_display(struct window *window)
1391 return window->display;
1395 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1397 struct display *display = surface->window->display;
1398 struct rectangle allocation = surface->allocation;
1400 if (!surface->toysurface && display->dpy &&
1401 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1402 surface->toysurface =
1403 egl_window_surface_create(display,
1409 if (!surface->toysurface)
1410 surface->toysurface = shm_surface_create(display,
1412 flags, &allocation);
1414 surface->cairo_surface = surface->toysurface->prepare(
1415 surface->toysurface, dx, dy,
1416 allocation.width, allocation.height, flags,
1417 surface->buffer_transform, surface->buffer_scale);
1421 window_create_main_surface(struct window *window)
1423 struct surface *surface = window->main_surface;
1428 if (window->resizing)
1429 flags |= SURFACE_HINT_RESIZE;
1431 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1432 flags |= SURFACE_HINT_RGB565;
1434 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1435 dx = surface->server_allocation.width -
1436 surface->allocation.width;
1438 if (window->resize_edges & WINDOW_RESIZING_TOP)
1439 dy = surface->server_allocation.height -
1440 surface->allocation.height;
1442 window->resize_edges = 0;
1444 surface_create_surface(surface, dx, dy, flags);
1448 window_get_buffer_transform(struct window *window)
1450 return window->main_surface->buffer_transform;
1454 window_set_buffer_transform(struct window *window,
1455 enum wl_output_transform transform)
1457 window->main_surface->buffer_transform = transform;
1458 wl_surface_set_buffer_transform(window->main_surface->surface,
1463 window_set_buffer_scale(struct window *window,
1466 window->main_surface->buffer_scale = scale;
1467 wl_surface_set_buffer_scale(window->main_surface->surface,
1472 window_get_buffer_scale(struct window *window)
1474 return window->main_surface->buffer_scale;
1478 window_get_output_scale(struct window *window)
1480 struct window_output *window_output;
1481 struct window_output *window_output_tmp;
1484 wl_list_for_each_safe(window_output, window_output_tmp,
1485 &window->window_output_list, link) {
1486 if (window_output->output->scale > scale)
1487 scale = window_output->output->scale;
1493 static void window_frame_destroy(struct window_frame *frame);
1496 surface_destroy(struct surface *surface)
1498 if (surface->frame_cb)
1499 wl_callback_destroy(surface->frame_cb);
1501 if (surface->input_region)
1502 wl_region_destroy(surface->input_region);
1504 if (surface->opaque_region)
1505 wl_region_destroy(surface->opaque_region);
1507 if (surface->subsurface)
1508 wl_subsurface_destroy(surface->subsurface);
1510 wl_surface_destroy(surface->surface);
1512 if (surface->toysurface)
1513 surface->toysurface->destroy(surface->toysurface);
1515 wl_list_remove(&surface->link);
1520 window_destroy(struct window *window)
1522 struct display *display = window->display;
1523 struct input *input;
1524 struct window_output *window_output;
1525 struct window_output *window_output_tmp;
1527 wl_list_remove(&window->redraw_task.link);
1529 wl_list_for_each(input, &display->input_list, link) {
1530 if (input->touch_focus == window)
1531 input->touch_focus = NULL;
1532 if (input->pointer_focus == window)
1533 input->pointer_focus = NULL;
1534 if (input->keyboard_focus == window)
1535 input->keyboard_focus = NULL;
1536 if (input->focus_widget &&
1537 input->focus_widget->window == window)
1538 input->focus_widget = NULL;
1541 wl_list_for_each_safe(window_output, window_output_tmp,
1542 &window->window_output_list, link) {
1543 free (window_output);
1547 window_frame_destroy(window->frame);
1549 if (window->shell_surface)
1550 wl_shell_surface_destroy(window->shell_surface);
1552 surface_destroy(window->main_surface);
1554 wl_list_remove(&window->link);
1556 free(window->title);
1560 static struct widget *
1561 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1563 struct widget *child, *target;
1565 wl_list_for_each(child, &widget->child_list, link) {
1566 target = widget_find_widget(child, x, y);
1571 if (widget->allocation.x <= x &&
1572 x < widget->allocation.x + widget->allocation.width &&
1573 widget->allocation.y <= y &&
1574 y < widget->allocation.y + widget->allocation.height) {
1581 static struct widget *
1582 window_find_widget(struct window *window, int32_t x, int32_t y)
1584 struct surface *surface;
1585 struct widget *widget;
1587 wl_list_for_each(surface, &window->subsurface_list, link) {
1588 widget = widget_find_widget(surface->widget, x, y);
1596 static struct widget *
1597 widget_create(struct window *window, struct surface *surface, void *data)
1599 struct widget *widget;
1601 widget = xzalloc(sizeof *widget);
1602 widget->window = window;
1603 widget->surface = surface;
1604 widget->user_data = data;
1605 widget->allocation = surface->allocation;
1606 wl_list_init(&widget->child_list);
1608 widget->tooltip = NULL;
1609 widget->tooltip_count = 0;
1610 widget->default_cursor = CURSOR_LEFT_PTR;
1616 window_add_widget(struct window *window, void *data)
1618 struct widget *widget;
1620 widget = widget_create(window, window->main_surface, data);
1621 wl_list_init(&widget->link);
1622 window->main_surface->widget = widget;
1628 widget_add_widget(struct widget *parent, void *data)
1630 struct widget *widget;
1632 widget = widget_create(parent->window, parent->surface, data);
1633 wl_list_insert(parent->child_list.prev, &widget->link);
1639 widget_destroy(struct widget *widget)
1641 struct display *display = widget->window->display;
1642 struct surface *surface = widget->surface;
1643 struct input *input;
1645 /* Destroy the sub-surface along with the root widget */
1646 if (surface->widget == widget && surface->subsurface)
1647 surface_destroy(widget->surface);
1649 if (widget->tooltip) {
1650 free(widget->tooltip);
1651 widget->tooltip = NULL;
1654 wl_list_for_each(input, &display->input_list, link) {
1655 if (input->focus_widget == widget)
1656 input->focus_widget = NULL;
1659 wl_list_remove(&widget->link);
1664 widget_set_default_cursor(struct widget *widget, int cursor)
1666 widget->default_cursor = cursor;
1670 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1672 *allocation = widget->allocation;
1676 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1678 widget->allocation.width = width;
1679 widget->allocation.height = height;
1683 widget_set_allocation(struct widget *widget,
1684 int32_t x, int32_t y, int32_t width, int32_t height)
1686 widget->allocation.x = x;
1687 widget->allocation.y = y;
1688 widget_set_size(widget, width, height);
1692 widget_set_transparent(struct widget *widget, int transparent)
1694 widget->opaque = !transparent;
1698 widget_get_user_data(struct widget *widget)
1700 return widget->user_data;
1703 static cairo_surface_t *
1704 widget_get_cairo_surface(struct widget *widget)
1706 struct surface *surface = widget->surface;
1707 struct window *window = widget->window;
1709 if (!surface->cairo_surface) {
1710 if (surface == window->main_surface)
1711 window_create_main_surface(window);
1713 surface_create_surface(surface, 0, 0, 0);
1716 return surface->cairo_surface;
1720 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1722 struct surface *surface = widget->surface;
1725 enum wl_output_transform transform;
1726 int surface_width, surface_height;
1727 int translate_x, translate_y;
1730 surface_width = surface->allocation.width;
1731 surface_height = surface->allocation.height;
1733 transform = surface->buffer_transform;
1734 scale = surface->buffer_scale;
1736 switch (transform) {
1737 case WL_OUTPUT_TRANSFORM_FLIPPED:
1738 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1739 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1740 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1741 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1744 cairo_matrix_init_identity(&m);
1748 switch (transform) {
1749 case WL_OUTPUT_TRANSFORM_NORMAL:
1755 case WL_OUTPUT_TRANSFORM_FLIPPED:
1757 translate_x = surface_width;
1760 case WL_OUTPUT_TRANSFORM_90:
1762 translate_x = surface_height;
1765 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1767 translate_x = surface_height;
1768 translate_y = surface_width;
1770 case WL_OUTPUT_TRANSFORM_180:
1772 translate_x = surface_width;
1773 translate_y = surface_height;
1775 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1778 translate_y = surface_height;
1780 case WL_OUTPUT_TRANSFORM_270:
1781 angle = M_PI + M_PI_2;
1783 translate_y = surface_width;
1785 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1786 angle = M_PI + M_PI_2;
1792 cairo_scale(cr, scale, scale);
1793 cairo_translate(cr, translate_x, translate_y);
1794 cairo_rotate(cr, angle);
1795 cairo_transform(cr, &m);
1799 widget_cairo_create(struct widget *widget)
1801 struct surface *surface = widget->surface;
1802 cairo_surface_t *cairo_surface;
1805 cairo_surface = widget_get_cairo_surface(widget);
1806 cr = cairo_create(cairo_surface);
1808 widget_cairo_update_transform(widget, cr);
1810 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1816 widget_get_wl_surface(struct widget *widget)
1818 return widget->surface->surface;
1821 struct wl_subsurface *
1822 widget_get_wl_subsurface(struct widget *widget)
1824 return widget->surface->subsurface;
1828 widget_get_last_time(struct widget *widget)
1830 return widget->surface->last_time;
1834 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1836 struct wl_compositor *comp = widget->window->display->compositor;
1837 struct surface *surface = widget->surface;
1839 if (!surface->input_region)
1840 surface->input_region = wl_compositor_create_region(comp);
1843 wl_region_add(surface->input_region,
1844 rect->x, rect->y, rect->width, rect->height);
1849 widget_set_resize_handler(struct widget *widget,
1850 widget_resize_handler_t handler)
1852 widget->resize_handler = handler;
1856 widget_set_redraw_handler(struct widget *widget,
1857 widget_redraw_handler_t handler)
1859 widget->redraw_handler = handler;
1863 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1865 widget->enter_handler = handler;
1869 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1871 widget->leave_handler = handler;
1875 widget_set_motion_handler(struct widget *widget,
1876 widget_motion_handler_t handler)
1878 widget->motion_handler = handler;
1882 widget_set_button_handler(struct widget *widget,
1883 widget_button_handler_t handler)
1885 widget->button_handler = handler;
1889 widget_set_touch_up_handler(struct widget *widget,
1890 widget_touch_up_handler_t handler)
1892 widget->touch_up_handler = handler;
1896 widget_set_touch_down_handler(struct widget *widget,
1897 widget_touch_down_handler_t handler)
1899 widget->touch_down_handler = handler;
1903 widget_set_touch_motion_handler(struct widget *widget,
1904 widget_touch_motion_handler_t handler)
1906 widget->touch_motion_handler = handler;
1910 widget_set_touch_frame_handler(struct widget *widget,
1911 widget_touch_frame_handler_t handler)
1913 widget->touch_frame_handler = handler;
1917 widget_set_touch_cancel_handler(struct widget *widget,
1918 widget_touch_cancel_handler_t handler)
1920 widget->touch_cancel_handler = handler;
1924 widget_set_axis_handler(struct widget *widget,
1925 widget_axis_handler_t handler)
1927 widget->axis_handler = handler;
1931 window_schedule_redraw_task(struct window *window);
1934 widget_schedule_redraw(struct widget *widget)
1936 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1937 widget->surface->redraw_needed = 1;
1938 window_schedule_redraw_task(widget->window);
1942 window_get_surface(struct window *window)
1944 cairo_surface_t *cairo_surface;
1946 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1948 return cairo_surface_reference(cairo_surface);
1952 window_get_wl_surface(struct window *window)
1954 return window->main_surface->surface;
1958 tooltip_redraw_handler(struct widget *widget, void *data)
1961 const int32_t r = 3;
1962 struct tooltip *tooltip = data;
1963 int32_t width, height;
1965 cr = widget_cairo_create(widget);
1966 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1967 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1970 width = widget->allocation.width;
1971 height = widget->allocation.height;
1972 rounded_rect(cr, 0, 0, width, height, r);
1974 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1975 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1978 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1979 cairo_move_to(cr, 10, 16);
1980 cairo_show_text(cr, tooltip->entry);
1984 static cairo_text_extents_t
1985 get_text_extents(struct tooltip *tooltip)
1988 cairo_text_extents_t extents;
1990 /* Use the dummy_surface because tooltip's surface was not
1991 * created yet, and parent does not have a valid surface
1992 * outside repaint, either.
1994 cr = cairo_create(tooltip->window->display->dummy_surface);
1995 cairo_text_extents(cr, tooltip->entry, &extents);
2002 window_create_tooltip(struct tooltip *tooltip)
2004 struct widget *parent = tooltip->parent;
2005 struct display *display = parent->window->display;
2006 struct window *window;
2007 const int offset_y = 27;
2008 const int margin = 3;
2009 cairo_text_extents_t extents;
2011 if (tooltip->widget)
2014 window = window_create_transient(display, parent->window, tooltip->x,
2015 tooltip->y + offset_y,
2016 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2020 tooltip->window = window;
2021 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2023 extents = get_text_extents(tooltip);
2024 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2025 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2031 widget_destroy_tooltip(struct widget *parent)
2033 struct tooltip *tooltip = parent->tooltip;
2035 parent->tooltip_count = 0;
2039 if (tooltip->widget) {
2040 widget_destroy(tooltip->widget);
2041 window_destroy(tooltip->window);
2042 tooltip->widget = NULL;
2043 tooltip->window = NULL;
2046 close(tooltip->tooltip_fd);
2047 free(tooltip->entry);
2049 parent->tooltip = NULL;
2053 tooltip_func(struct task *task, uint32_t events)
2055 struct tooltip *tooltip =
2056 container_of(task, struct tooltip, tooltip_task);
2059 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2061 window_create_tooltip(tooltip);
2064 #define TOOLTIP_TIMEOUT 500
2066 tooltip_timer_reset(struct tooltip *tooltip)
2068 struct itimerspec its;
2070 its.it_interval.tv_sec = 0;
2071 its.it_interval.tv_nsec = 0;
2072 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2073 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2074 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2075 fprintf(stderr, "could not set timerfd\n: %m");
2083 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2085 struct tooltip *tooltip = parent->tooltip;
2087 parent->tooltip_count++;
2091 tooltip_timer_reset(tooltip);
2095 /* the handler might be triggered too fast via input device motion, so
2096 * we need this check here to make sure tooltip is fully initialized */
2097 if (parent->tooltip_count > 1)
2100 tooltip = malloc(sizeof *tooltip);
2104 parent->tooltip = tooltip;
2105 tooltip->parent = parent;
2106 tooltip->widget = NULL;
2107 tooltip->window = NULL;
2110 tooltip->entry = strdup(entry);
2111 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2112 if (tooltip->tooltip_fd < 0) {
2113 fprintf(stderr, "could not create timerfd\n: %m");
2117 tooltip->tooltip_task.run = tooltip_func;
2118 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2119 EPOLLIN, &tooltip->tooltip_task);
2120 tooltip_timer_reset(tooltip);
2126 workspace_manager_state(void *data,
2127 struct workspace_manager *workspace_manager,
2131 struct display *display = data;
2133 display->workspace = current;
2134 display->workspace_count = count;
2137 static const struct workspace_manager_listener workspace_manager_listener = {
2138 workspace_manager_state
2142 frame_resize_handler(struct widget *widget,
2143 int32_t width, int32_t height, void *data)
2145 struct window_frame *frame = data;
2146 struct widget *child = frame->child;
2147 struct rectangle interior;
2148 struct rectangle input;
2149 struct rectangle opaque;
2151 if (widget->window->type == TYPE_FULLSCREEN) {
2154 interior.width = width;
2155 interior.height = height;
2157 if (widget->window->type == TYPE_MAXIMIZED) {
2158 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2160 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2163 frame_resize(frame->frame, width, height);
2164 frame_interior(frame->frame, &interior.x, &interior.y,
2165 &interior.width, &interior.height);
2168 widget_set_allocation(child, interior.x, interior.y,
2169 interior.width, interior.height);
2171 if (child->resize_handler) {
2172 child->resize_handler(child, interior.width, interior.height,
2175 if (widget->window->type == TYPE_FULLSCREEN) {
2176 width = child->allocation.width;
2177 height = child->allocation.height;
2179 frame_resize_inside(frame->frame,
2180 child->allocation.width,
2181 child->allocation.height);
2182 width = frame_width(frame->frame);
2183 height = frame_height(frame->frame);
2187 widget_set_allocation(widget, 0, 0, width, height);
2189 widget->surface->input_region =
2190 wl_compositor_create_region(widget->window->display->compositor);
2191 if (widget->window->type != TYPE_FULLSCREEN) {
2192 frame_input_rect(frame->frame, &input.x, &input.y,
2193 &input.width, &input.height);
2194 wl_region_add(widget->surface->input_region,
2195 input.x, input.y, input.width, input.height);
2197 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2200 widget_set_allocation(widget, 0, 0, width, height);
2202 if (child->opaque) {
2203 if (widget->window->type != TYPE_FULLSCREEN) {
2204 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2205 &opaque.width, &opaque.height);
2207 wl_region_add(widget->surface->opaque_region,
2209 opaque.width, opaque.height);
2211 wl_region_add(widget->surface->opaque_region,
2212 0, 0, width, height);
2217 widget_schedule_redraw(widget);
2221 frame_redraw_handler(struct widget *widget, void *data)
2224 struct window_frame *frame = data;
2225 struct window *window = widget->window;
2227 if (window->type == TYPE_FULLSCREEN)
2230 if (window->focus_count) {
2231 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2233 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2236 cr = widget_cairo_create(widget);
2238 frame_repaint(frame->frame, cr);
2244 frame_get_pointer_image_for_location(struct window_frame *frame,
2245 enum theme_location location)
2247 struct window *window = frame->widget->window;
2249 if (window->type != TYPE_TOPLEVEL)
2250 return CURSOR_LEFT_PTR;
2253 case THEME_LOCATION_RESIZING_TOP:
2255 case THEME_LOCATION_RESIZING_BOTTOM:
2256 return CURSOR_BOTTOM;
2257 case THEME_LOCATION_RESIZING_LEFT:
2259 case THEME_LOCATION_RESIZING_RIGHT:
2260 return CURSOR_RIGHT;
2261 case THEME_LOCATION_RESIZING_TOP_LEFT:
2262 return CURSOR_TOP_LEFT;
2263 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2264 return CURSOR_TOP_RIGHT;
2265 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2266 return CURSOR_BOTTOM_LEFT;
2267 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2268 return CURSOR_BOTTOM_RIGHT;
2269 case THEME_LOCATION_EXTERIOR:
2270 case THEME_LOCATION_TITLEBAR:
2272 return CURSOR_LEFT_PTR;
2277 frame_menu_func(struct window *window,
2278 struct input *input, int index, void *data)
2280 struct display *display;
2284 if (window->close_handler)
2285 window->close_handler(window->user_data);
2287 display_exit(window->display);
2289 case 1: /* move to workspace above */
2290 display = window->display;
2291 if (display->workspace > 0)
2292 workspace_manager_move_surface(
2293 display->workspace_manager,
2294 window->main_surface->surface,
2295 display->workspace - 1);
2297 case 2: /* move to workspace below */
2298 display = window->display;
2299 if (display->workspace < display->workspace_count - 1)
2300 workspace_manager_move_surface(
2301 display->workspace_manager,
2302 window->main_surface->surface,
2303 display->workspace + 1);
2305 case 3: /* fullscreen */
2306 /* we don't have a way to get out of fullscreen for now */
2307 if (window->fullscreen_handler)
2308 window->fullscreen_handler(window, window->user_data);
2314 window_show_frame_menu(struct window *window,
2315 struct input *input, uint32_t time)
2320 static const char *entries[] = {
2322 "Move to workspace above", "Move to workspace below",
2326 if (window->fullscreen_handler)
2327 count = ARRAY_LENGTH(entries);
2329 count = ARRAY_LENGTH(entries) - 1;
2331 input_get_position(input, &x, &y);
2332 window_show_menu(window->display, input, time, window,
2333 x - 10, y - 10, frame_menu_func, entries, count);
2337 frame_enter_handler(struct widget *widget,
2338 struct input *input, float x, float y, void *data)
2340 struct window_frame *frame = data;
2341 enum theme_location location;
2343 location = frame_pointer_enter(frame->frame, input, x, y);
2344 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2345 widget_schedule_redraw(frame->widget);
2347 return frame_get_pointer_image_for_location(data, location);
2351 frame_motion_handler(struct widget *widget,
2352 struct input *input, uint32_t time,
2353 float x, float y, void *data)
2355 struct window_frame *frame = data;
2356 enum theme_location location;
2358 location = frame_pointer_motion(frame->frame, input, x, y);
2359 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2360 widget_schedule_redraw(frame->widget);
2362 return frame_get_pointer_image_for_location(data, location);
2366 frame_leave_handler(struct widget *widget,
2367 struct input *input, void *data)
2369 struct window_frame *frame = data;
2371 frame_pointer_leave(frame->frame, input);
2372 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2373 widget_schedule_redraw(frame->widget);
2377 frame_handle_status(struct window_frame *frame, struct input *input,
2378 uint32_t time, enum theme_location location)
2380 struct window *window = frame->widget->window;
2383 status = frame_status(frame->frame);
2384 if (status & FRAME_STATUS_REPAINT)
2385 widget_schedule_redraw(frame->widget);
2387 if (status & FRAME_STATUS_MINIMIZE)
2388 fprintf(stderr,"Minimize stub\n");
2390 if (status & FRAME_STATUS_MENU) {
2391 window_show_frame_menu(window, input, time);
2392 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2395 if (status & FRAME_STATUS_MAXIMIZE) {
2396 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2397 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2400 if (status & FRAME_STATUS_CLOSE) {
2401 if (window->close_handler)
2402 window->close_handler(window->user_data);
2404 display_exit(window->display);
2408 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2409 input_ungrab(input);
2410 wl_shell_surface_move(window->shell_surface,
2411 input_get_seat(input),
2412 window->display->serial);
2414 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2417 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2418 input_ungrab(input);
2420 window->resizing = 1;
2421 wl_shell_surface_resize(window->shell_surface,
2422 input_get_seat(input),
2423 window->display->serial,
2426 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2431 frame_button_handler(struct widget *widget,
2432 struct input *input, uint32_t time,
2433 uint32_t button, enum wl_pointer_button_state state,
2437 struct window_frame *frame = data;
2438 enum theme_location location;
2440 location = frame_pointer_button(frame->frame, input, button, state);
2441 frame_handle_status(frame, input, time, location);
2445 frame_touch_down_handler(struct widget *widget, struct input *input,
2446 uint32_t serial, uint32_t time, int32_t id,
2447 float x, float y, void *data)
2449 struct window_frame *frame = data;
2451 frame_touch_down(frame->frame, input, id, x, y);
2452 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2456 frame_touch_up_handler(struct widget *widget,
2457 struct input *input, uint32_t serial, uint32_t time,
2458 int32_t id, void *data)
2460 struct window_frame *frame = data;
2462 frame_touch_up(frame->frame, input, id);
2463 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2467 window_frame_create(struct window *window, void *data)
2469 struct window_frame *frame;
2471 frame = xzalloc(sizeof *frame);
2472 frame->frame = frame_create(window->display->theme, 0, 0,
2473 FRAME_BUTTON_ALL, window->title);
2475 frame->widget = window_add_widget(window, frame);
2476 frame->child = widget_add_widget(frame->widget, data);
2478 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2479 widget_set_resize_handler(frame->widget, frame_resize_handler);
2480 widget_set_enter_handler(frame->widget, frame_enter_handler);
2481 widget_set_leave_handler(frame->widget, frame_leave_handler);
2482 widget_set_motion_handler(frame->widget, frame_motion_handler);
2483 widget_set_button_handler(frame->widget, frame_button_handler);
2484 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2485 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2487 window->frame = frame;
2489 return frame->child;
2493 window_frame_set_child_size(struct widget *widget, int child_width,
2496 struct display *display = widget->window->display;
2497 struct theme *t = display->theme;
2498 int decoration_width, decoration_height;
2500 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2502 if (widget->window->type != TYPE_FULLSCREEN) {
2503 decoration_width = (t->width + margin) * 2;
2504 decoration_height = t->width +
2505 t->titlebar_height + margin * 2;
2507 width = child_width + decoration_width;
2508 height = child_height + decoration_height;
2510 width = child_width;
2511 height = child_height;
2514 window_schedule_resize(widget->window, width, height);
2518 window_frame_destroy(struct window_frame *frame)
2520 frame_destroy(frame->frame);
2522 /* frame->child must be destroyed by the application */
2523 widget_destroy(frame->widget);
2528 input_set_focus_widget(struct input *input, struct widget *focus,
2531 struct widget *old, *widget;
2534 if (focus == input->focus_widget)
2537 old = input->focus_widget;
2541 widget = input->grab;
2542 if (widget->leave_handler)
2543 widget->leave_handler(old, input, widget->user_data);
2544 input->focus_widget = NULL;
2550 widget = input->grab;
2551 input->focus_widget = focus;
2552 if (widget->enter_handler)
2553 cursor = widget->enter_handler(focus, input, x, y,
2556 cursor = widget->default_cursor;
2558 input_set_pointer_image(input, cursor);
2563 touch_grab(struct input *input, int32_t touch_id)
2565 input->touch_grab = 1;
2566 input->touch_grab_id = touch_id;
2570 touch_ungrab(struct input *input)
2572 struct touch_point *tp, *tmp;
2574 input->touch_grab = 0;
2576 wl_list_for_each_safe(tp, tmp,
2577 &input->touch_point_list, link) {
2578 if (tp->id != input->touch_grab_id)
2580 wl_list_remove(&tp->link);
2588 input_grab(struct input *input, struct widget *widget, uint32_t button)
2590 input->grab = widget;
2591 input->grab_button = button;
2595 input_ungrab(struct input *input)
2597 struct widget *widget;
2600 if (input->pointer_focus) {
2601 widget = window_find_widget(input->pointer_focus,
2602 input->sx, input->sy);
2603 input_set_focus_widget(input, widget, input->sx, input->sy);
2608 input_remove_pointer_focus(struct input *input)
2610 struct window *window = input->pointer_focus;
2615 input_set_focus_widget(input, NULL, 0, 0);
2617 input->pointer_focus = NULL;
2618 input->current_cursor = CURSOR_UNSET;
2622 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2623 uint32_t serial, struct wl_surface *surface,
2624 wl_fixed_t sx_w, wl_fixed_t sy_w)
2626 struct input *input = data;
2627 struct window *window;
2628 struct widget *widget;
2629 float sx = wl_fixed_to_double(sx_w);
2630 float sy = wl_fixed_to_double(sy_w);
2633 /* enter event for a window we've just destroyed */
2637 input->display->serial = serial;
2638 input->pointer_enter_serial = serial;
2639 input->pointer_focus = wl_surface_get_user_data(surface);
2640 window = input->pointer_focus;
2642 if (window->resizing) {
2643 window->resizing = 0;
2644 /* Schedule a redraw to free the pool */
2645 window_schedule_redraw(window);
2651 widget = window_find_widget(window, sx, sy);
2652 input_set_focus_widget(input, widget, sx, sy);
2656 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2657 uint32_t serial, struct wl_surface *surface)
2659 struct input *input = data;
2661 input->display->serial = serial;
2662 input_remove_pointer_focus(input);
2666 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2667 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2669 struct input *input = data;
2670 struct window *window = input->pointer_focus;
2671 struct widget *widget;
2673 float sx = wl_fixed_to_double(sx_w);
2674 float sy = wl_fixed_to_double(sy_w);
2682 /* when making the window smaller - e.g. after a unmaximise we might
2683 * still have a pending motion event that the compositor has picked
2684 * based on the old surface dimensions
2686 if (sx > window->main_surface->allocation.width ||
2687 sy > window->main_surface->allocation.height)
2690 if (!(input->grab && input->grab_button)) {
2691 widget = window_find_widget(window, sx, sy);
2692 input_set_focus_widget(input, widget, sx, sy);
2696 widget = input->grab;
2698 widget = input->focus_widget;
2700 if (widget->motion_handler)
2701 cursor = widget->motion_handler(input->focus_widget,
2702 input, time, sx, sy,
2705 cursor = widget->default_cursor;
2707 cursor = CURSOR_LEFT_PTR;
2709 input_set_pointer_image(input, cursor);
2713 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2714 uint32_t time, uint32_t button, uint32_t state_w)
2716 struct input *input = data;
2717 struct widget *widget;
2718 enum wl_pointer_button_state state = state_w;
2720 input->display->serial = serial;
2721 if (input->focus_widget && input->grab == NULL &&
2722 state == WL_POINTER_BUTTON_STATE_PRESSED)
2723 input_grab(input, input->focus_widget, button);
2725 widget = input->grab;
2726 if (widget && widget->button_handler)
2727 (*widget->button_handler)(widget,
2730 input->grab->user_data);
2732 if (input->grab && input->grab_button == button &&
2733 state == WL_POINTER_BUTTON_STATE_RELEASED)
2734 input_ungrab(input);
2738 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2739 uint32_t time, uint32_t axis, wl_fixed_t value)
2741 struct input *input = data;
2742 struct widget *widget;
2744 widget = input->focus_widget;
2746 widget = input->grab;
2747 if (widget && widget->axis_handler)
2748 (*widget->axis_handler)(widget,
2754 static const struct wl_pointer_listener pointer_listener = {
2755 pointer_handle_enter,
2756 pointer_handle_leave,
2757 pointer_handle_motion,
2758 pointer_handle_button,
2759 pointer_handle_axis,
2763 input_remove_keyboard_focus(struct input *input)
2765 struct window *window = input->keyboard_focus;
2766 struct itimerspec its;
2768 its.it_interval.tv_sec = 0;
2769 its.it_interval.tv_nsec = 0;
2770 its.it_value.tv_sec = 0;
2771 its.it_value.tv_nsec = 0;
2772 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2777 window->focus_count--;
2778 if (window->keyboard_focus_handler)
2779 (*window->keyboard_focus_handler)(window, NULL,
2782 input->keyboard_focus = NULL;
2786 keyboard_repeat_func(struct task *task, uint32_t events)
2788 struct input *input =
2789 container_of(task, struct input, repeat_task);
2790 struct window *window = input->keyboard_focus;
2793 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2794 /* If we change the timer between the fd becoming
2795 * readable and getting here, there'll be nothing to
2796 * read and we get EAGAIN. */
2799 if (window && window->key_handler) {
2800 (*window->key_handler)(window, input, input->repeat_time,
2801 input->repeat_key, input->repeat_sym,
2802 WL_KEYBOARD_KEY_STATE_PRESSED,
2808 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2809 uint32_t format, int fd, uint32_t size)
2811 struct input *input = data;
2812 struct xkb_keymap *keymap;
2813 struct xkb_state *state;
2821 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2826 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2827 if (map_str == MAP_FAILED) {
2832 keymap = xkb_map_new_from_string(input->display->xkb_context,
2834 XKB_KEYMAP_FORMAT_TEXT_V1,
2836 munmap(map_str, size);
2840 fprintf(stderr, "failed to compile keymap\n");
2844 state = xkb_state_new(keymap);
2846 fprintf(stderr, "failed to create XKB state\n");
2847 xkb_map_unref(keymap);
2851 xkb_keymap_unref(input->xkb.keymap);
2852 xkb_state_unref(input->xkb.state);
2853 input->xkb.keymap = keymap;
2854 input->xkb.state = state;
2856 input->xkb.control_mask =
2857 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2858 input->xkb.alt_mask =
2859 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2860 input->xkb.shift_mask =
2861 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2865 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2866 uint32_t serial, struct wl_surface *surface,
2867 struct wl_array *keys)
2869 struct input *input = data;
2870 struct window *window;
2872 input->display->serial = serial;
2873 input->keyboard_focus = wl_surface_get_user_data(surface);
2875 window = input->keyboard_focus;
2876 window->focus_count++;
2877 if (window->keyboard_focus_handler)
2878 (*window->keyboard_focus_handler)(window,
2879 input, window->user_data);
2883 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2884 uint32_t serial, struct wl_surface *surface)
2886 struct input *input = data;
2888 input->display->serial = serial;
2889 input_remove_keyboard_focus(input);
2893 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2894 uint32_t serial, uint32_t time, uint32_t key,
2897 struct input *input = data;
2898 struct window *window = input->keyboard_focus;
2899 uint32_t code, num_syms;
2900 enum wl_keyboard_key_state state = state_w;
2901 const xkb_keysym_t *syms;
2903 struct itimerspec its;
2905 input->display->serial = serial;
2907 if (!window || !input->xkb.state)
2910 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2912 sym = XKB_KEY_NoSymbol;
2916 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2917 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2918 window_set_maximized(window,
2919 window->type != TYPE_MAXIMIZED);
2920 } else if (sym == XKB_KEY_F11 &&
2921 window->fullscreen_handler &&
2922 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2923 window->fullscreen_handler(window, window->user_data);
2924 } else if (sym == XKB_KEY_F4 &&
2925 input->modifiers == MOD_ALT_MASK &&
2926 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2927 if (window->close_handler)
2928 window->close_handler(window->user_data);
2930 display_exit(window->display);
2931 } else if (window->key_handler) {
2932 (*window->key_handler)(window, input, time, key,
2933 sym, state, window->user_data);
2936 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2937 key == input->repeat_key) {
2938 its.it_interval.tv_sec = 0;
2939 its.it_interval.tv_nsec = 0;
2940 its.it_value.tv_sec = 0;
2941 its.it_value.tv_nsec = 0;
2942 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2943 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2944 input->repeat_sym = sym;
2945 input->repeat_key = key;
2946 input->repeat_time = time;
2947 its.it_interval.tv_sec = 0;
2948 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2949 its.it_value.tv_sec = 0;
2950 its.it_value.tv_nsec = 400 * 1000 * 1000;
2951 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2956 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2957 uint32_t serial, uint32_t mods_depressed,
2958 uint32_t mods_latched, uint32_t mods_locked,
2961 struct input *input = data;
2962 xkb_mod_mask_t mask;
2964 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2965 if (!input->xkb.keymap)
2968 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2969 mods_locked, 0, 0, group);
2970 mask = xkb_state_serialize_mods(input->xkb.state,
2971 XKB_STATE_DEPRESSED |
2973 input->modifiers = 0;
2974 if (mask & input->xkb.control_mask)
2975 input->modifiers |= MOD_CONTROL_MASK;
2976 if (mask & input->xkb.alt_mask)
2977 input->modifiers |= MOD_ALT_MASK;
2978 if (mask & input->xkb.shift_mask)
2979 input->modifiers |= MOD_SHIFT_MASK;
2982 static const struct wl_keyboard_listener keyboard_listener = {
2983 keyboard_handle_keymap,
2984 keyboard_handle_enter,
2985 keyboard_handle_leave,
2986 keyboard_handle_key,
2987 keyboard_handle_modifiers,
2991 touch_handle_down(void *data, struct wl_touch *wl_touch,
2992 uint32_t serial, uint32_t time, struct wl_surface *surface,
2993 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2995 struct input *input = data;
2996 struct widget *widget;
2997 float sx = wl_fixed_to_double(x_w);
2998 float sy = wl_fixed_to_double(y_w);
3000 input->display->serial = serial;
3001 input->touch_focus = wl_surface_get_user_data(surface);
3002 if (!input->touch_focus) {
3003 DBG("Failed to find to touch focus for surface %p\n", surface);
3007 widget = window_find_widget(input->touch_focus,
3008 wl_fixed_to_double(x_w),
3009 wl_fixed_to_double(y_w));
3011 struct touch_point *tp = xmalloc(sizeof *tp);
3014 tp->widget = widget;
3015 wl_list_insert(&input->touch_point_list, &tp->link);
3017 if (widget->touch_down_handler)
3018 (*widget->touch_down_handler)(widget, input,
3027 touch_handle_up(void *data, struct wl_touch *wl_touch,
3028 uint32_t serial, uint32_t time, int32_t id)
3030 struct input *input = data;
3031 struct touch_point *tp, *tmp;
3033 if (!input->touch_focus) {
3034 DBG("No touch focus found for touch up event!\n");
3038 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3042 if (tp->widget->touch_up_handler)
3043 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3045 tp->widget->user_data);
3047 wl_list_remove(&tp->link);
3055 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3056 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3058 struct input *input = data;
3059 struct touch_point *tp;
3060 float sx = wl_fixed_to_double(x_w);
3061 float sy = wl_fixed_to_double(y_w);
3063 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3065 if (!input->touch_focus) {
3066 DBG("No touch focus found for touch motion event!\n");
3070 wl_list_for_each(tp, &input->touch_point_list, link) {
3074 if (tp->widget->touch_motion_handler)
3075 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3077 tp->widget->user_data);
3083 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3085 struct input *input = data;
3086 struct touch_point *tp, *tmp;
3088 DBG("touch_handle_frame\n");
3090 if (!input->touch_focus) {
3091 DBG("No touch focus found for touch frame event!\n");
3095 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3096 if (tp->widget->touch_frame_handler)
3097 (*tp->widget->touch_frame_handler)(tp->widget, input,
3098 tp->widget->user_data);
3100 wl_list_remove(&tp->link);
3106 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3108 struct input *input = data;
3109 struct touch_point *tp, *tmp;
3111 DBG("touch_handle_cancel\n");
3113 if (!input->touch_focus) {
3114 DBG("No touch focus found for touch cancel event!\n");
3118 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3119 if (tp->widget->touch_cancel_handler)
3120 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3121 tp->widget->user_data);
3123 wl_list_remove(&tp->link);
3128 static const struct wl_touch_listener touch_listener = {
3131 touch_handle_motion,
3133 touch_handle_cancel,
3137 seat_handle_capabilities(void *data, struct wl_seat *seat,
3138 enum wl_seat_capability caps)
3140 struct input *input = data;
3142 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3143 input->pointer = wl_seat_get_pointer(seat);
3144 wl_pointer_set_user_data(input->pointer, input);
3145 wl_pointer_add_listener(input->pointer, &pointer_listener,
3147 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3148 wl_pointer_destroy(input->pointer);
3149 input->pointer = NULL;
3152 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3153 input->keyboard = wl_seat_get_keyboard(seat);
3154 wl_keyboard_set_user_data(input->keyboard, input);
3155 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3157 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3158 wl_keyboard_destroy(input->keyboard);
3159 input->keyboard = NULL;
3162 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3163 input->touch = wl_seat_get_touch(seat);
3164 wl_touch_set_user_data(input->touch, input);
3165 wl_touch_add_listener(input->touch, &touch_listener, input);
3166 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3167 wl_touch_destroy(input->touch);
3168 input->touch = NULL;
3173 seat_handle_name(void *data, struct wl_seat *seat,
3179 static const struct wl_seat_listener seat_listener = {
3180 seat_handle_capabilities,
3185 input_get_position(struct input *input, int32_t *x, int32_t *y)
3192 input_get_display(struct input *input)
3194 return input->display;
3198 input_get_seat(struct input *input)
3204 input_get_modifiers(struct input *input)
3206 return input->modifiers;
3210 input_get_focus_widget(struct input *input)
3212 return input->focus_widget;
3216 struct wl_data_offer *offer;
3217 struct input *input;
3218 struct wl_array types;
3221 struct task io_task;
3229 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3231 struct data_offer *offer = data;
3234 p = wl_array_add(&offer->types, sizeof *p);
3238 static const struct wl_data_offer_listener data_offer_listener = {
3243 data_offer_destroy(struct data_offer *offer)
3248 if (offer->refcount == 0) {
3249 wl_data_offer_destroy(offer->offer);
3250 for (p = offer->types.data; *p; p++)
3252 wl_array_release(&offer->types);
3258 data_device_data_offer(void *data,
3259 struct wl_data_device *data_device,
3260 struct wl_data_offer *_offer)
3262 struct data_offer *offer;
3264 offer = xmalloc(sizeof *offer);
3266 wl_array_init(&offer->types);
3267 offer->refcount = 1;
3268 offer->input = data;
3269 offer->offer = _offer;
3270 wl_data_offer_add_listener(offer->offer,
3271 &data_offer_listener, offer);
3275 data_device_enter(void *data, struct wl_data_device *data_device,
3276 uint32_t serial, struct wl_surface *surface,
3277 wl_fixed_t x_w, wl_fixed_t y_w,
3278 struct wl_data_offer *offer)
3280 struct input *input = data;
3281 struct window *window;
3283 float x = wl_fixed_to_double(x_w);
3284 float y = wl_fixed_to_double(y_w);
3287 window = wl_surface_get_user_data(surface);
3288 input->drag_enter_serial = serial;
3289 input->drag_focus = window,
3293 if (!input->touch_grab)
3294 input->pointer_enter_serial = serial;
3297 input->drag_offer = wl_data_offer_get_user_data(offer);
3299 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3302 types_data = input->drag_offer->types.data;
3304 input->drag_offer = NULL;
3308 if (window->data_handler)
3309 window->data_handler(window, input, x, y, types_data,
3314 data_device_leave(void *data, struct wl_data_device *data_device)
3316 struct input *input = data;
3318 if (input->drag_offer) {
3319 data_offer_destroy(input->drag_offer);
3320 input->drag_offer = NULL;
3325 data_device_motion(void *data, struct wl_data_device *data_device,
3326 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3328 struct input *input = data;
3329 struct window *window = input->drag_focus;
3330 float x = wl_fixed_to_double(x_w);
3331 float y = wl_fixed_to_double(y_w);
3337 if (input->drag_offer)
3338 types_data = input->drag_offer->types.data;
3342 if (window->data_handler)
3343 window->data_handler(window, input, x, y, types_data,
3348 data_device_drop(void *data, struct wl_data_device *data_device)
3350 struct input *input = data;
3351 struct window *window = input->drag_focus;
3357 if (window->drop_handler)
3358 window->drop_handler(window, input,
3359 x, y, window->user_data);
3361 if (input->touch_grab)
3362 touch_ungrab(input);
3366 data_device_selection(void *data,
3367 struct wl_data_device *wl_data_device,
3368 struct wl_data_offer *offer)
3370 struct input *input = data;
3373 if (input->selection_offer)
3374 data_offer_destroy(input->selection_offer);
3377 input->selection_offer = wl_data_offer_get_user_data(offer);
3378 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3381 input->selection_offer = NULL;
3385 static const struct wl_data_device_listener data_device_listener = {
3386 data_device_data_offer,
3391 data_device_selection
3395 input_set_pointer_image_index(struct input *input, int index)
3397 struct wl_buffer *buffer;
3398 struct wl_cursor *cursor;
3399 struct wl_cursor_image *image;
3401 if (!input->pointer)
3404 cursor = input->display->cursors[input->current_cursor];
3408 if (index >= (int) cursor->image_count) {
3409 fprintf(stderr, "cursor index out of range\n");
3413 image = cursor->images[index];
3414 buffer = wl_cursor_image_get_buffer(image);
3418 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3419 input->pointer_surface,
3420 image->hotspot_x, image->hotspot_y);
3421 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3422 wl_surface_damage(input->pointer_surface, 0, 0,
3423 image->width, image->height);
3424 wl_surface_commit(input->pointer_surface);
3427 static const struct wl_callback_listener pointer_surface_listener;
3430 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3433 struct input *input = data;
3434 struct wl_cursor *cursor;
3438 assert(callback == input->cursor_frame_cb);
3439 wl_callback_destroy(callback);
3440 input->cursor_frame_cb = NULL;
3443 if (!input->pointer)
3446 if (input->current_cursor == CURSOR_BLANK) {
3447 wl_pointer_set_cursor(input->pointer,
3448 input->pointer_enter_serial,
3453 if (input->current_cursor == CURSOR_UNSET)
3455 cursor = input->display->cursors[input->current_cursor];
3459 /* FIXME We don't have the current time on the first call so we set
3460 * the animation start to the time of the first frame callback. */
3462 input->cursor_anim_start = 0;
3463 else if (input->cursor_anim_start == 0)
3464 input->cursor_anim_start = time;
3466 if (time == 0 || input->cursor_anim_start == 0)
3469 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3471 if (cursor->image_count > 1) {
3472 input->cursor_frame_cb =
3473 wl_surface_frame(input->pointer_surface);
3474 wl_callback_add_listener(input->cursor_frame_cb,
3475 &pointer_surface_listener, input);
3478 input_set_pointer_image_index(input, i);
3481 static const struct wl_callback_listener pointer_surface_listener = {
3482 pointer_surface_frame_callback
3486 input_set_pointer_image(struct input *input, int pointer)
3490 if (!input->pointer)
3493 if (input->pointer_enter_serial > input->cursor_serial)
3496 if (!force && pointer == input->current_cursor)
3499 input->current_cursor = pointer;
3500 input->cursor_serial = input->pointer_enter_serial;
3501 if (!input->cursor_frame_cb)
3502 pointer_surface_frame_callback(input, NULL, 0);
3504 /* The current frame callback may be stuck if, for instance,
3505 * the set cursor request was processed by the server after
3506 * this client lost the focus. In this case the cursor surface
3507 * might not be mapped and the frame callback wouldn't ever
3508 * complete. Send a set_cursor and attach to try to map the
3509 * cursor surface again so that the callback will finish */
3510 input_set_pointer_image_index(input, 0);
3514 struct wl_data_device *
3515 input_get_data_device(struct input *input)
3517 return input->data_device;
3521 input_set_selection(struct input *input,
3522 struct wl_data_source *source, uint32_t time)
3524 wl_data_device_set_selection(input->data_device, source, time);
3528 input_accept(struct input *input, const char *type)
3530 wl_data_offer_accept(input->drag_offer->offer,
3531 input->drag_enter_serial, type);
3535 offer_io_func(struct task *task, uint32_t events)
3537 struct data_offer *offer =
3538 container_of(task, struct data_offer, io_task);
3542 len = read(offer->fd, buffer, sizeof buffer);
3543 offer->func(buffer, len,
3544 offer->x, offer->y, offer->user_data);
3548 data_offer_destroy(offer);
3553 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3554 data_func_t func, void *user_data)
3558 if (pipe2(p, O_CLOEXEC) == -1)
3561 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3564 offer->io_task.run = offer_io_func;
3568 offer->user_data = user_data;
3570 display_watch_fd(offer->input->display,
3571 offer->fd, EPOLLIN, &offer->io_task);
3575 input_receive_drag_data(struct input *input, const char *mime_type,
3576 data_func_t func, void *data)
3578 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3579 input->drag_offer->x = input->drag_x;
3580 input->drag_offer->y = input->drag_y;
3584 input_receive_drag_data_to_fd(struct input *input,
3585 const char *mime_type, int fd)
3587 if (input->drag_offer)
3588 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3594 input_receive_selection_data(struct input *input, const char *mime_type,
3595 data_func_t func, void *data)
3599 if (input->selection_offer == NULL)
3602 for (p = input->selection_offer->types.data; *p; p++)
3603 if (strcmp(mime_type, *p) == 0)
3609 data_offer_receive_data(input->selection_offer,
3610 mime_type, func, data);
3615 input_receive_selection_data_to_fd(struct input *input,
3616 const char *mime_type, int fd)
3618 if (input->selection_offer)
3619 wl_data_offer_receive(input->selection_offer->offer,
3626 window_move(struct window *window, struct input *input, uint32_t serial)
3628 if (!window->shell_surface)
3631 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3635 surface_set_synchronized(struct surface *surface)
3637 if (!surface->subsurface)
3640 if (surface->synchronized)
3643 wl_subsurface_set_sync(surface->subsurface);
3644 surface->synchronized = 1;
3648 surface_set_synchronized_default(struct surface *surface)
3650 if (!surface->subsurface)
3653 if (surface->synchronized == surface->synchronized_default)
3656 if (surface->synchronized_default)
3657 wl_subsurface_set_sync(surface->subsurface);
3659 wl_subsurface_set_desync(surface->subsurface);
3661 surface->synchronized = surface->synchronized_default;
3665 surface_resize(struct surface *surface)
3667 struct widget *widget = surface->widget;
3668 struct wl_compositor *compositor = widget->window->display->compositor;
3670 if (surface->input_region) {
3671 wl_region_destroy(surface->input_region);
3672 surface->input_region = NULL;
3675 if (surface->opaque_region)
3676 wl_region_destroy(surface->opaque_region);
3678 surface->opaque_region = wl_compositor_create_region(compositor);
3680 if (widget->resize_handler)
3681 widget->resize_handler(widget,
3682 widget->allocation.width,
3683 widget->allocation.height,
3686 if (surface->subsurface &&
3687 (surface->allocation.x != widget->allocation.x ||
3688 surface->allocation.y != widget->allocation.y)) {
3689 wl_subsurface_set_position(surface->subsurface,
3690 widget->allocation.x,
3691 widget->allocation.y);
3693 if (surface->allocation.width != widget->allocation.width ||
3694 surface->allocation.height != widget->allocation.height) {
3695 window_schedule_redraw(widget->window);
3697 surface->allocation = widget->allocation;
3700 wl_region_add(surface->opaque_region, 0, 0,
3701 widget->allocation.width,
3702 widget->allocation.height);
3706 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3709 * This hack should be removed, when EGL respects
3710 * eglSwapInterval(0).
3712 * If this window has sub-surfaces, especially a free-running
3713 * EGL-widget, we need to post the parent surface once with
3714 * all the old state to guarantee, that the EGL-widget will
3715 * receive its frame callback soon. Otherwise, a forced call
3716 * to eglSwapBuffers may end up blocking, waiting for a frame
3717 * event that will never come, because we will commit the parent
3718 * surface with all new state only after eglSwapBuffers returns.
3720 * This assumes, that:
3721 * 1. When the EGL widget's resize hook is called, it pauses.
3722 * 2. When the EGL widget's redraw hook is called, it forces a
3723 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3724 * In a single threaded application condition 1 is a no-op.
3726 * XXX: This should actually be after the surface_resize() calls,
3727 * but cannot, because then it would commit the incomplete state
3728 * accumulated from the widget resize hooks.
3730 if (window->subsurface_list.next != &window->main_surface->link ||
3731 window->subsurface_list.prev != &window->main_surface->link)
3732 wl_surface_commit(window->main_surface->surface);
3736 window_do_resize(struct window *window)
3738 struct surface *surface;
3740 widget_set_allocation(window->main_surface->widget,
3741 window->pending_allocation.x,
3742 window->pending_allocation.y,
3743 window->pending_allocation.width,
3744 window->pending_allocation.height);
3746 surface_resize(window->main_surface);
3748 /* The main surface is in the list, too. Main surface's
3749 * resize_handler is responsible for calling widget_set_allocation()
3750 * on all sub-surface root widgets, so they will be resized
3753 wl_list_for_each(surface, &window->subsurface_list, link) {
3754 if (surface == window->main_surface)
3757 surface_set_synchronized(surface);
3758 surface_resize(surface);
3763 idle_resize(struct window *window)
3765 window->resize_needed = 0;
3766 window->redraw_needed = 1;
3768 DBG("from %dx%d to %dx%d\n",
3769 window->main_surface->server_allocation.width,
3770 window->main_surface->server_allocation.height,
3771 window->pending_allocation.width,
3772 window->pending_allocation.height);
3774 hack_prevent_EGL_sub_surface_deadlock(window);
3776 window_do_resize(window);
3780 undo_resize(struct window *window)
3782 window->pending_allocation.width =
3783 window->main_surface->server_allocation.width;
3784 window->pending_allocation.height =
3785 window->main_surface->server_allocation.height;
3787 DBG("back to %dx%d\n",
3788 window->main_surface->server_allocation.width,
3789 window->main_surface->server_allocation.height);
3791 window_do_resize(window);
3793 if (window->pending_allocation.width == 0 &&
3794 window->pending_allocation.height == 0) {
3795 fprintf(stderr, "Error: Could not draw a surface, "
3796 "most likely due to insufficient disk space in "
3797 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3803 window_schedule_resize(struct window *window, int width, int height)
3805 /* We should probably get these numbers from the theme. */
3806 const int min_width = 200, min_height = 200;
3808 window->pending_allocation.x = 0;
3809 window->pending_allocation.y = 0;
3810 window->pending_allocation.width = width;
3811 window->pending_allocation.height = height;
3813 if (window->min_allocation.width == 0) {
3814 if (width < min_width && window->frame)
3815 window->min_allocation.width = min_width;
3817 window->min_allocation.width = width;
3818 if (height < min_height && window->frame)
3819 window->min_allocation.height = min_height;
3821 window->min_allocation.height = height;
3824 if (window->pending_allocation.width < window->min_allocation.width)
3825 window->pending_allocation.width = window->min_allocation.width;
3826 if (window->pending_allocation.height < window->min_allocation.height)
3827 window->pending_allocation.height = window->min_allocation.height;
3829 window->resize_needed = 1;
3830 window_schedule_redraw(window);
3834 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3836 window_schedule_resize(widget->window, width, height);
3840 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3843 wl_shell_surface_pong(shell_surface, serial);
3847 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3848 uint32_t edges, int32_t width, int32_t height)
3850 struct window *window = data;
3852 window->resize_edges = edges;
3853 window_schedule_resize(window, width, height);
3857 menu_destroy(struct menu *menu)
3859 widget_destroy(menu->widget);
3860 window_destroy(menu->window);
3861 frame_destroy(menu->frame);
3866 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3868 struct window *window = data;
3869 struct menu *menu = window->main_surface->widget->user_data;
3871 /* FIXME: Need more context in this event, at least the input
3872 * device. Or just use wl_callback. And this really needs to
3873 * be a window vfunc that the menu can set. And we need the
3876 input_ungrab(menu->input);
3880 static const struct wl_shell_surface_listener shell_surface_listener = {
3887 window_get_allocation(struct window *window,
3888 struct rectangle *allocation)
3890 *allocation = window->main_surface->allocation;
3894 widget_redraw(struct widget *widget)
3896 struct widget *child;
3898 if (widget->redraw_handler)
3899 widget->redraw_handler(widget, widget->user_data);
3900 wl_list_for_each(child, &widget->child_list, link)
3901 widget_redraw(child);
3905 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3907 struct surface *surface = data;
3909 assert(callback == surface->frame_cb);
3910 DBG_OBJ(callback, "done\n");
3911 wl_callback_destroy(callback);
3912 surface->frame_cb = NULL;
3914 surface->last_time = time;
3916 if (surface->redraw_needed || surface->window->redraw_needed) {
3917 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3918 window_schedule_redraw_task(surface->window);
3922 static const struct wl_callback_listener listener = {
3927 surface_redraw(struct surface *surface)
3929 DBG_OBJ(surface->surface, "begin\n");
3931 if (!surface->window->redraw_needed && !surface->redraw_needed)
3934 /* Whole-window redraw forces a redraw even if the previous has
3935 * not yet hit the screen.
3937 if (surface->frame_cb) {
3938 if (!surface->window->redraw_needed)
3941 DBG_OBJ(surface->frame_cb, "cancelled\n");
3942 wl_callback_destroy(surface->frame_cb);
3945 if (!widget_get_cairo_surface(surface->widget)) {
3946 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
3950 surface->frame_cb = wl_surface_frame(surface->surface);
3951 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3952 DBG_OBJ(surface->frame_cb, "new\n");
3954 surface->redraw_needed = 0;
3955 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3956 widget_redraw(surface->widget);
3957 DBG_OBJ(surface->surface, "done\n");
3962 idle_redraw(struct task *task, uint32_t events)
3964 struct window *window = container_of(task, struct window, redraw_task);
3965 struct surface *surface;
3969 DBG(" --------- \n");
3971 wl_list_init(&window->redraw_task.link);
3972 window->redraw_task_scheduled = 0;
3974 if (window->resize_needed) {
3975 /* throttle resizing to the main surface display */
3976 if (window->main_surface->frame_cb) {
3977 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
3981 idle_resize(window);
3985 if (surface_redraw(window->main_surface) < 0) {
3987 * Only main_surface failure will cause us to undo the resize.
3988 * If sub-surfaces fail, they will just be broken with old
3993 wl_list_for_each(surface, &window->subsurface_list, link) {
3994 if (surface == window->main_surface)
3997 surface_redraw(surface);
4001 window->redraw_needed = 0;
4002 window_flush(window);
4004 wl_list_for_each(surface, &window->subsurface_list, link)
4005 surface_set_synchronized_default(surface);
4007 if (resized && failed) {
4008 /* Restore widget tree to correspond to what is on screen. */
4009 undo_resize(window);
4014 window_schedule_redraw_task(struct window *window)
4016 if (window->configure_requests)
4018 if (!window->redraw_task_scheduled) {
4019 window->redraw_task.run = idle_redraw;
4020 display_defer(window->display, &window->redraw_task);
4021 window->redraw_task_scheduled = 1;
4026 window_schedule_redraw(struct window *window)
4028 struct surface *surface;
4030 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4032 wl_list_for_each(surface, &window->subsurface_list, link)
4033 surface->redraw_needed = 1;
4035 window_schedule_redraw_task(window);
4039 window_is_fullscreen(struct window *window)
4041 return window->type == TYPE_FULLSCREEN;
4045 window_is_transient(struct window *window)
4047 return window->type == TYPE_TRANSIENT;
4051 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4053 struct window *window = data;
4055 wl_callback_destroy(callback);
4056 window->configure_requests--;
4058 if (!window->configure_requests)
4059 window_schedule_redraw(window);
4062 static struct wl_callback_listener configure_request_listener = {
4063 configure_request_completed,
4067 window_defer_redraw_until_configure(struct window* window)
4069 struct wl_callback *callback;
4071 if (window->redraw_task_scheduled) {
4072 wl_list_remove(&window->redraw_task.link);
4073 window->redraw_task_scheduled = 0;
4076 callback = wl_display_sync(window->display->display);
4077 wl_callback_add_listener(callback, &configure_request_listener, window);
4078 window->configure_requests++;
4082 window_set_fullscreen(struct window *window, int fullscreen)
4084 if (!window->display->shell)
4087 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4091 window->saved_type = window->type;
4092 if (window->type == TYPE_TOPLEVEL) {
4093 window->saved_allocation = window->main_surface->allocation;
4095 window->type = TYPE_FULLSCREEN;
4096 wl_shell_surface_set_fullscreen(window->shell_surface,
4097 window->fullscreen_method,
4099 window_defer_redraw_until_configure (window);
4101 if (window->saved_type == TYPE_MAXIMIZED) {
4102 window_set_maximized(window, 1);
4104 window->type = TYPE_TOPLEVEL;
4105 wl_shell_surface_set_toplevel(window->shell_surface);
4106 window_schedule_resize(window,
4107 window->saved_allocation.width,
4108 window->saved_allocation.height);
4115 window_set_fullscreen_method(struct window *window,
4116 enum wl_shell_surface_fullscreen_method method)
4118 window->fullscreen_method = method;
4122 window_is_maximized(struct window *window)
4124 return window->type == TYPE_MAXIMIZED;
4128 window_set_maximized(struct window *window, int maximized)
4130 if (!window->display->shell)
4133 if ((window->type == TYPE_MAXIMIZED) == maximized)
4136 if (window->type == TYPE_TOPLEVEL) {
4137 window->saved_allocation = window->main_surface->allocation;
4138 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4139 window->type = TYPE_MAXIMIZED;
4140 window_defer_redraw_until_configure(window);
4141 } else if (window->type == TYPE_FULLSCREEN) {
4142 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4143 window->type = TYPE_MAXIMIZED;
4144 window_defer_redraw_until_configure(window);
4146 wl_shell_surface_set_toplevel(window->shell_surface);
4147 window->type = TYPE_TOPLEVEL;
4148 window_schedule_resize(window,
4149 window->saved_allocation.width,
4150 window->saved_allocation.height);
4155 window_set_user_data(struct window *window, void *data)
4157 window->user_data = data;
4161 window_get_user_data(struct window *window)
4163 return window->user_data;
4167 window_set_key_handler(struct window *window,
4168 window_key_handler_t handler)
4170 window->key_handler = handler;
4174 window_set_keyboard_focus_handler(struct window *window,
4175 window_keyboard_focus_handler_t handler)
4177 window->keyboard_focus_handler = handler;
4181 window_set_data_handler(struct window *window, window_data_handler_t handler)
4183 window->data_handler = handler;
4187 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4189 window->drop_handler = handler;
4193 window_set_close_handler(struct window *window,
4194 window_close_handler_t handler)
4196 window->close_handler = handler;
4200 window_set_fullscreen_handler(struct window *window,
4201 window_fullscreen_handler_t handler)
4203 window->fullscreen_handler = handler;
4207 window_set_output_handler(struct window *window,
4208 window_output_handler_t handler)
4210 window->output_handler = handler;
4214 window_set_title(struct window *window, const char *title)
4216 free(window->title);
4217 window->title = strdup(title);
4218 if (window->frame) {
4219 frame_set_title(window->frame->frame, title);
4220 widget_schedule_redraw(window->frame->widget);
4222 if (window->shell_surface)
4223 wl_shell_surface_set_title(window->shell_surface, title);
4227 window_get_title(struct window *window)
4229 return window->title;
4233 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4235 struct text_cursor_position *text_cursor_position =
4236 window->display->text_cursor_position;
4238 if (!text_cursor_position)
4241 text_cursor_position_notify(text_cursor_position,
4242 window->main_surface->surface,
4243 wl_fixed_from_int(x),
4244 wl_fixed_from_int(y));
4248 window_damage(struct window *window, int32_t x, int32_t y,
4249 int32_t width, int32_t height)
4251 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4255 surface_enter(void *data,
4256 struct wl_surface *wl_surface, struct wl_output *wl_output)
4258 struct window *window = data;
4259 struct output *output;
4260 struct output *output_found = NULL;
4261 struct window_output *window_output;
4263 wl_list_for_each(output, &window->display->output_list, link) {
4264 if (output->output == wl_output) {
4265 output_found = output;
4273 window_output = xmalloc(sizeof *window_output);
4274 window_output->output = output_found;
4276 wl_list_insert (&window->window_output_list, &window_output->link);
4278 if (window->output_handler)
4279 window->output_handler(window, output_found, 1,
4284 surface_leave(void *data,
4285 struct wl_surface *wl_surface, struct wl_output *output)
4287 struct window *window = data;
4288 struct window_output *window_output;
4289 struct window_output *window_output_found = NULL;
4291 wl_list_for_each(window_output, &window->window_output_list, link) {
4292 if (window_output->output->output == output) {
4293 window_output_found = window_output;
4298 if (window_output_found) {
4299 wl_list_remove(&window_output_found->link);
4301 if (window->output_handler)
4302 window->output_handler(window, window_output->output,
4303 0, window->user_data);
4305 free(window_output_found);
4309 static const struct wl_surface_listener surface_listener = {
4314 static struct surface *
4315 surface_create(struct window *window)
4317 struct display *display = window->display;
4318 struct surface *surface;
4320 surface = xmalloc(sizeof *surface);
4321 memset(surface, 0, sizeof *surface);
4325 surface->window = window;
4326 surface->surface = wl_compositor_create_surface(display->compositor);
4327 surface->buffer_scale = 1;
4328 wl_surface_add_listener(surface->surface, &surface_listener, window);
4330 wl_list_insert(&window->subsurface_list, &surface->link);
4335 static struct window *
4336 window_create_internal(struct display *display, int type)
4338 struct window *window;
4339 struct surface *surface;
4341 window = xzalloc(sizeof *window);
4342 wl_list_init(&window->subsurface_list);
4343 window->display = display;
4345 surface = surface_create(window);
4346 window->main_surface = surface;
4348 if (type != TYPE_CUSTOM && display->shell) {
4349 window->shell_surface =
4350 wl_shell_get_shell_surface(display->shell,
4352 fail_on_null(window->shell_surface);
4355 window->type = type;
4356 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4357 window->configure_requests = 0;
4358 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4360 if (display->argb_device)
4361 #ifdef HAVE_CAIRO_EGL
4362 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4364 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4367 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4369 wl_surface_set_user_data(surface->surface, window);
4370 wl_list_insert(display->window_list.prev, &window->link);
4371 wl_list_init(&window->redraw_task.link);
4373 if (window->shell_surface) {
4374 wl_shell_surface_set_user_data(window->shell_surface, window);
4375 wl_shell_surface_add_listener(window->shell_surface,
4376 &shell_surface_listener, window);
4379 wl_list_init (&window->window_output_list);
4385 window_create(struct display *display)
4387 return window_create_internal(display, TYPE_NONE);
4391 window_create_custom(struct display *display)
4393 return window_create_internal(display, TYPE_CUSTOM);
4397 window_create_transient(struct display *display, struct window *parent,
4398 int32_t x, int32_t y, uint32_t flags)
4400 struct window *window;
4402 window = window_create_internal(parent->display, TYPE_TRANSIENT);
4408 wl_shell_surface_set_transient(
4409 window->shell_surface,
4410 parent->main_surface->surface,
4411 window->x, window->y, flags);
4417 menu_set_item(struct menu *menu, int sy)
4419 int32_t x, y, width, height;
4422 frame_interior(menu->frame, &x, &y, &width, &height);
4423 next = (sy - y) / 20;
4424 if (menu->current != next) {
4425 menu->current = next;
4426 widget_schedule_redraw(menu->widget);
4431 menu_motion_handler(struct widget *widget,
4432 struct input *input, uint32_t time,
4433 float x, float y, void *data)
4435 struct menu *menu = data;
4437 if (widget == menu->widget)
4438 menu_set_item(data, y);
4440 return CURSOR_LEFT_PTR;
4444 menu_enter_handler(struct widget *widget,
4445 struct input *input, float x, float y, void *data)
4447 struct menu *menu = data;
4449 if (widget == menu->widget)
4450 menu_set_item(data, y);
4452 return CURSOR_LEFT_PTR;
4456 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4458 struct menu *menu = data;
4460 if (widget == menu->widget)
4461 menu_set_item(data, -200);
4465 menu_button_handler(struct widget *widget,
4466 struct input *input, uint32_t time,
4467 uint32_t button, enum wl_pointer_button_state state,
4471 struct menu *menu = data;
4473 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4474 (menu->release_count > 0 || time - menu->time > 500)) {
4475 /* Either relase after press-drag-release or
4476 * click-motion-click. */
4477 menu->func(menu->parent, input,
4478 menu->current, menu->parent->user_data);
4479 input_ungrab(input);
4481 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4482 menu->release_count++;
4487 menu_redraw_handler(struct widget *widget, void *data)
4490 struct menu *menu = data;
4491 int32_t x, y, width, height, i;
4493 cr = widget_cairo_create(widget);
4495 frame_repaint(menu->frame, cr);
4496 frame_interior(menu->frame, &x, &y, &width, &height);
4498 theme_set_background_source(menu->window->display->theme,
4499 cr, THEME_FRAME_ACTIVE);
4500 cairo_rectangle(cr, x, y, width, height);
4503 cairo_select_font_face(cr, "sans",
4504 CAIRO_FONT_SLANT_NORMAL,
4505 CAIRO_FONT_WEIGHT_NORMAL);
4506 cairo_set_font_size(cr, 12);
4508 for (i = 0; i < menu->count; i++) {
4509 if (i == menu->current) {
4510 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4511 cairo_rectangle(cr, x, y + i * 20, width, 20);
4513 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4514 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4515 cairo_show_text(cr, menu->entries[i]);
4517 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4518 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4519 cairo_show_text(cr, menu->entries[i]);
4527 window_show_menu(struct display *display,
4528 struct input *input, uint32_t time, struct window *parent,
4529 int32_t x, int32_t y,
4530 menu_func_t func, const char **entries, int count)
4532 struct window *window;
4536 menu = malloc(sizeof *menu);
4540 window = window_create_internal(parent->display, TYPE_MENU);
4546 menu->window = window;
4547 menu->parent = parent;
4548 menu->widget = window_add_widget(menu->window, menu);
4549 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4550 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4551 menu->frame = frame_create(window->display->theme, 0, 0,
4552 FRAME_BUTTON_NONE, NULL);
4553 menu->entries = entries;
4554 menu->count = count;
4555 menu->release_count = 0;
4559 menu->input = input;
4560 window->type = TYPE_MENU;
4564 input_ungrab(input);
4566 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4567 widget_set_enter_handler(menu->widget, menu_enter_handler);
4568 widget_set_leave_handler(menu->widget, menu_leave_handler);
4569 widget_set_motion_handler(menu->widget, menu_motion_handler);
4570 widget_set_button_handler(menu->widget, menu_button_handler);
4572 input_grab(input, menu->widget, 0);
4573 frame_resize_inside(menu->frame, 200, count * 20);
4574 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4575 window_schedule_resize(window, frame_width(menu->frame),
4576 frame_height(menu->frame));
4578 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4579 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4580 display_get_serial(window->display),
4581 parent->main_surface->surface,
4582 window->x - ix, window->y - iy, 0);
4586 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4588 window->main_surface->buffer_type = type;
4592 window_set_preferred_format(struct window *window,
4593 enum preferred_format format)
4595 window->preferred_format = format;
4599 window_add_subsurface(struct window *window, void *data,
4600 enum subsurface_mode default_mode)
4602 struct widget *widget;
4603 struct surface *surface;
4604 struct wl_surface *parent;
4605 struct wl_subcompositor *subcompo = window->display->subcompositor;
4607 surface = surface_create(window);
4608 widget = widget_create(window, surface, data);
4609 wl_list_init(&widget->link);
4610 surface->widget = widget;
4612 parent = window->main_surface->surface;
4613 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4616 surface->synchronized = 1;
4618 switch (default_mode) {
4619 case SUBSURFACE_SYNCHRONIZED:
4620 surface->synchronized_default = 1;
4622 case SUBSURFACE_DESYNCHRONIZED:
4623 surface->synchronized_default = 0;
4626 assert(!"bad enum subsurface_mode");
4633 display_handle_geometry(void *data,
4634 struct wl_output *wl_output,
4637 int physical_height,
4643 struct output *output = data;
4645 output->allocation.x = x;
4646 output->allocation.y = y;
4647 output->transform = transform;
4651 display_handle_done(void *data,
4652 struct wl_output *wl_output)
4657 display_handle_scale(void *data,
4658 struct wl_output *wl_output,
4661 struct output *output = data;
4663 output->scale = scale;
4667 display_handle_mode(void *data,
4668 struct wl_output *wl_output,
4674 struct output *output = data;
4675 struct display *display = output->display;
4677 if (flags & WL_OUTPUT_MODE_CURRENT) {
4678 output->allocation.width = width;
4679 output->allocation.height = height;
4680 if (display->output_configure_handler)
4681 (*display->output_configure_handler)(
4682 output, display->user_data);
4686 static const struct wl_output_listener output_listener = {
4687 display_handle_geometry,
4688 display_handle_mode,
4689 display_handle_done,
4690 display_handle_scale
4694 display_add_output(struct display *d, uint32_t id)
4696 struct output *output;
4698 output = xzalloc(sizeof *output);
4699 output->display = d;
4702 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4703 output->server_output_id = id;
4704 wl_list_insert(d->output_list.prev, &output->link);
4706 wl_output_add_listener(output->output, &output_listener, output);
4710 output_destroy(struct output *output)
4712 if (output->destroy_handler)
4713 (*output->destroy_handler)(output, output->user_data);
4715 wl_output_destroy(output->output);
4716 wl_list_remove(&output->link);
4721 display_destroy_output(struct display *d, uint32_t id)
4723 struct output *output;
4725 wl_list_for_each(output, &d->output_list, link) {
4726 if (output->server_output_id == id) {
4727 output_destroy(output);
4734 display_set_global_handler(struct display *display,
4735 display_global_handler_t handler)
4737 struct global *global;
4739 display->global_handler = handler;
4743 wl_list_for_each(global, &display->global_list, link)
4744 display->global_handler(display,
4745 global->name, global->interface,
4746 global->version, display->user_data);
4750 display_set_global_handler_remove(struct display *display,
4751 display_global_handler_t remove_handler)
4753 display->global_handler_remove = remove_handler;
4754 if (!remove_handler)
4759 display_set_output_configure_handler(struct display *display,
4760 display_output_handler_t handler)
4762 struct output *output;
4764 display->output_configure_handler = handler;
4768 wl_list_for_each(output, &display->output_list, link) {
4769 if (output->allocation.width == 0 &&
4770 output->allocation.height == 0)
4773 (*display->output_configure_handler)(output,
4774 display->user_data);
4779 output_set_user_data(struct output *output, void *data)
4781 output->user_data = data;
4785 output_get_user_data(struct output *output)
4787 return output->user_data;
4791 output_set_destroy_handler(struct output *output,
4792 display_output_handler_t handler)
4794 output->destroy_handler = handler;
4795 /* FIXME: implement this, once we have way to remove outputs */
4799 output_get_allocation(struct output *output, struct rectangle *base)
4801 struct rectangle allocation = output->allocation;
4803 switch (output->transform) {
4804 case WL_OUTPUT_TRANSFORM_90:
4805 case WL_OUTPUT_TRANSFORM_270:
4806 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4807 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4808 /* Swap width and height */
4809 allocation.width = output->allocation.height;
4810 allocation.height = output->allocation.width;
4818 output_get_wl_output(struct output *output)
4820 return output->output;
4823 enum wl_output_transform
4824 output_get_transform(struct output *output)
4826 return output->transform;
4830 output_get_scale(struct output *output)
4832 return output->scale;
4836 fini_xkb(struct input *input)
4838 xkb_state_unref(input->xkb.state);
4839 xkb_map_unref(input->xkb.keymap);
4842 #define MIN(a,b) ((a) < (b) ? a : b)
4845 display_add_input(struct display *d, uint32_t id)
4847 struct input *input;
4849 input = xzalloc(sizeof *input);
4851 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4852 MIN(d->seat_version, 3));
4853 input->touch_focus = NULL;
4854 input->pointer_focus = NULL;
4855 input->keyboard_focus = NULL;
4856 wl_list_init(&input->touch_point_list);
4857 wl_list_insert(d->input_list.prev, &input->link);
4859 wl_seat_add_listener(input->seat, &seat_listener, input);
4860 wl_seat_set_user_data(input->seat, input);
4862 input->data_device =
4863 wl_data_device_manager_get_data_device(d->data_device_manager,
4865 wl_data_device_add_listener(input->data_device, &data_device_listener,
4868 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4870 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4871 TFD_CLOEXEC | TFD_NONBLOCK);
4872 input->repeat_task.run = keyboard_repeat_func;
4873 display_watch_fd(d, input->repeat_timer_fd,
4874 EPOLLIN, &input->repeat_task);
4878 input_destroy(struct input *input)
4880 input_remove_keyboard_focus(input);
4881 input_remove_pointer_focus(input);
4883 if (input->drag_offer)
4884 data_offer_destroy(input->drag_offer);
4886 if (input->selection_offer)
4887 data_offer_destroy(input->selection_offer);
4889 wl_data_device_destroy(input->data_device);
4891 if (input->display->seat_version >= 3) {
4893 wl_pointer_release(input->pointer);
4894 if (input->keyboard)
4895 wl_keyboard_release(input->keyboard);
4900 wl_surface_destroy(input->pointer_surface);
4902 wl_list_remove(&input->link);
4903 wl_seat_destroy(input->seat);
4904 close(input->repeat_timer_fd);
4909 init_workspace_manager(struct display *d, uint32_t id)
4911 d->workspace_manager =
4912 wl_registry_bind(d->registry, id,
4913 &workspace_manager_interface, 1);
4914 if (d->workspace_manager != NULL)
4915 workspace_manager_add_listener(d->workspace_manager,
4916 &workspace_manager_listener,
4921 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4923 struct display *d = data;
4925 if (format == WL_SHM_FORMAT_RGB565)
4929 struct wl_shm_listener shm_listener = {
4934 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4935 const char *interface, uint32_t version)
4937 struct display *d = data;
4938 struct global *global;
4940 global = xmalloc(sizeof *global);
4942 global->interface = strdup(interface);
4943 global->version = version;
4944 wl_list_insert(d->global_list.prev, &global->link);
4946 if (strcmp(interface, "wl_compositor") == 0) {
4947 d->compositor = wl_registry_bind(registry, id,
4948 &wl_compositor_interface, 3);
4949 } else if (strcmp(interface, "wl_output") == 0) {
4950 display_add_output(d, id);
4951 } else if (strcmp(interface, "wl_seat") == 0) {
4952 d->seat_version = version;
4953 display_add_input(d, id);
4954 } else if (strcmp(interface, "wl_shell") == 0) {
4955 d->shell = wl_registry_bind(registry,
4956 id, &wl_shell_interface, 1);
4957 } else if (strcmp(interface, "wl_shm") == 0) {
4958 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4959 wl_shm_add_listener(d->shm, &shm_listener, d);
4960 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4961 d->data_device_manager =
4962 wl_registry_bind(registry, id,
4963 &wl_data_device_manager_interface, 1);
4964 } else if (strcmp(interface, "text_cursor_position") == 0) {
4965 d->text_cursor_position =
4966 wl_registry_bind(registry, id,
4967 &text_cursor_position_interface, 1);
4968 } else if (strcmp(interface, "workspace_manager") == 0) {
4969 init_workspace_manager(d, id);
4970 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4972 wl_registry_bind(registry, id,
4973 &wl_subcompositor_interface, 1);
4976 if (d->global_handler)
4977 d->global_handler(d, id, interface, version, d->user_data);
4981 registry_handle_global_remove(void *data, struct wl_registry *registry,
4984 struct display *d = data;
4985 struct global *global;
4988 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4989 if (global->name != name)
4992 if (strcmp(global->interface, "wl_output") == 0)
4993 display_destroy_output(d, name);
4995 /* XXX: Should destroy remaining bound globals */
4997 if (d->global_handler_remove)
4998 d->global_handler_remove(d, name, global->interface,
4999 global->version, d->user_data);
5001 wl_list_remove(&global->link);
5002 free(global->interface);
5008 display_bind(struct display *display, uint32_t name,
5009 const struct wl_interface *interface, uint32_t version)
5011 return wl_registry_bind(display->registry, name, interface, version);
5014 static const struct wl_registry_listener registry_listener = {
5015 registry_handle_global,
5016 registry_handle_global_remove
5019 #ifdef HAVE_CAIRO_EGL
5021 init_egl(struct display *d)
5023 EGLint major, minor;
5026 #ifdef USE_CAIRO_GLESV2
5027 # define GL_BIT EGL_OPENGL_ES2_BIT
5029 # define GL_BIT EGL_OPENGL_BIT
5032 static const EGLint argb_cfg_attribs[] = {
5033 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5039 EGL_RENDERABLE_TYPE, GL_BIT,
5043 #ifdef USE_CAIRO_GLESV2
5044 static const EGLint context_attribs[] = {
5045 EGL_CONTEXT_CLIENT_VERSION, 2,
5048 EGLint api = EGL_OPENGL_ES_API;
5050 EGLint *context_attribs = NULL;
5051 EGLint api = EGL_OPENGL_API;
5054 d->dpy = eglGetDisplay(d->display);
5055 if (!eglInitialize(d->dpy, &major, &minor)) {
5056 fprintf(stderr, "failed to initialize EGL\n");
5060 if (!eglBindAPI(api)) {
5061 fprintf(stderr, "failed to bind EGL client API\n");
5065 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5066 &d->argb_config, 1, &n) || n != 1) {
5067 fprintf(stderr, "failed to choose argb EGL config\n");
5071 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5072 EGL_NO_CONTEXT, context_attribs);
5073 if (d->argb_ctx == NULL) {
5074 fprintf(stderr, "failed to create EGL context\n");
5078 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5079 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5080 fprintf(stderr, "failed to get cairo EGL argb device\n");
5088 fini_egl(struct display *display)
5090 cairo_device_destroy(display->argb_device);
5092 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5095 eglTerminate(display->dpy);
5101 init_dummy_surface(struct display *display)
5106 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5108 display->dummy_surface =
5109 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5111 display->dummy_surface_data = data;
5115 handle_display_data(struct task *task, uint32_t events)
5117 struct display *display =
5118 container_of(task, struct display, display_task);
5119 struct epoll_event ep;
5122 display->display_fd_events = events;
5124 if (events & EPOLLERR || events & EPOLLHUP) {
5125 display_exit(display);
5129 if (events & EPOLLIN) {
5130 ret = wl_display_dispatch(display->display);
5132 display_exit(display);
5137 if (events & EPOLLOUT) {
5138 ret = wl_display_flush(display->display);
5140 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5141 ep.data.ptr = &display->display_task;
5142 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5143 display->display_fd, &ep);
5144 } else if (ret == -1 && errno != EAGAIN) {
5145 display_exit(display);
5152 log_handler(const char *format, va_list args)
5154 vfprintf(stderr, format, args);
5158 display_create(int *argc, char *argv[])
5162 wl_log_set_handler_client(log_handler);
5164 d = zalloc(sizeof *d);
5168 d->display = wl_display_connect(NULL);
5169 if (d->display == NULL) {
5170 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5175 d->xkb_context = xkb_context_new(0);
5176 if (d->xkb_context == NULL) {
5177 fprintf(stderr, "Failed to create XKB context\n");
5182 d->epoll_fd = os_epoll_create_cloexec();
5183 d->display_fd = wl_display_get_fd(d->display);
5184 d->display_task.run = handle_display_data;
5185 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5188 wl_list_init(&d->deferred_list);
5189 wl_list_init(&d->input_list);
5190 wl_list_init(&d->output_list);
5191 wl_list_init(&d->global_list);
5194 d->workspace_count = 1;
5196 d->registry = wl_display_get_registry(d->display);
5197 wl_registry_add_listener(d->registry, ®istry_listener, d);
5199 if (wl_display_dispatch(d->display) < 0) {
5200 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5204 #ifdef HAVE_CAIRO_EGL
5205 if (init_egl(d) < 0)
5206 fprintf(stderr, "EGL does not seem to work, "
5207 "falling back to software rendering and wl_shm.\n");
5212 d->theme = theme_create();
5214 wl_list_init(&d->window_list);
5216 init_dummy_surface(d);
5222 display_destroy_outputs(struct display *display)
5225 struct output *output;
5227 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5228 output_destroy(output);
5232 display_destroy_inputs(struct display *display)
5235 struct input *input;
5237 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5238 input_destroy(input);
5242 display_destroy(struct display *display)
5244 if (!wl_list_empty(&display->window_list))
5245 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5246 wl_list_length(&display->window_list));
5248 if (!wl_list_empty(&display->deferred_list))
5249 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5251 cairo_surface_destroy(display->dummy_surface);
5252 free(display->dummy_surface_data);
5254 display_destroy_outputs(display);
5255 display_destroy_inputs(display);
5257 xkb_context_unref(display->xkb_context);
5259 theme_destroy(display->theme);
5260 destroy_cursors(display);
5262 #ifdef HAVE_CAIRO_EGL
5263 if (display->argb_device)
5267 if (display->subcompositor)
5268 wl_subcompositor_destroy(display->subcompositor);
5271 wl_shell_destroy(display->shell);
5274 wl_shm_destroy(display->shm);
5276 if (display->data_device_manager)
5277 wl_data_device_manager_destroy(display->data_device_manager);
5279 wl_compositor_destroy(display->compositor);
5280 wl_registry_destroy(display->registry);
5282 close(display->epoll_fd);
5284 if (!(display->display_fd_events & EPOLLERR) &&
5285 !(display->display_fd_events & EPOLLHUP))
5286 wl_display_flush(display->display);
5288 wl_display_disconnect(display->display);
5293 display_set_user_data(struct display *display, void *data)
5295 display->user_data = data;
5299 display_get_user_data(struct display *display)
5301 return display->user_data;
5305 display_get_display(struct display *display)
5307 return display->display;
5311 display_has_subcompositor(struct display *display)
5313 if (display->subcompositor)
5316 wl_display_roundtrip(display->display);
5318 return display->subcompositor != NULL;
5322 display_get_cairo_device(struct display *display)
5324 return display->argb_device;
5328 display_get_output(struct display *display)
5330 return container_of(display->output_list.next, struct output, link);
5333 struct wl_compositor *
5334 display_get_compositor(struct display *display)
5336 return display->compositor;
5340 display_get_serial(struct display *display)
5342 return display->serial;
5346 display_get_egl_display(struct display *d)
5351 struct wl_data_source *
5352 display_create_data_source(struct display *display)
5354 return wl_data_device_manager_create_data_source(display->data_device_manager);
5358 display_get_argb_egl_config(struct display *d)
5360 return d->argb_config;
5364 display_acquire_window_surface(struct display *display,
5365 struct window *window,
5368 struct surface *surface = window->main_surface;
5370 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5373 widget_get_cairo_surface(window->main_surface->widget);
5374 return surface->toysurface->acquire(surface->toysurface, ctx);
5378 display_release_window_surface(struct display *display,
5379 struct window *window)
5381 struct surface *surface = window->main_surface;
5383 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5386 surface->toysurface->release(surface->toysurface);
5390 display_defer(struct display *display, struct task *task)
5392 wl_list_insert(&display->deferred_list, &task->link);
5396 display_watch_fd(struct display *display,
5397 int fd, uint32_t events, struct task *task)
5399 struct epoll_event ep;
5403 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5407 display_unwatch_fd(struct display *display, int fd)
5409 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5413 display_run(struct display *display)
5416 struct epoll_event ep[16];
5419 display->running = 1;
5421 while (!wl_list_empty(&display->deferred_list)) {
5422 task = container_of(display->deferred_list.prev,
5424 wl_list_remove(&task->link);
5428 wl_display_dispatch_pending(display->display);
5430 if (!display->running)
5433 ret = wl_display_flush(display->display);
5434 if (ret < 0 && errno == EAGAIN) {
5436 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5437 ep[0].data.ptr = &display->display_task;
5439 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5440 display->display_fd, &ep[0]);
5441 } else if (ret < 0) {
5445 count = epoll_wait(display->epoll_fd,
5446 ep, ARRAY_LENGTH(ep), -1);
5447 for (i = 0; i < count; i++) {
5448 task = ep[i].data.ptr;
5449 task->run(task, ep[i].events);
5455 display_exit(struct display *display)
5457 display->running = 0;
5461 keysym_modifiers_add(struct wl_array *modifiers_map,
5464 size_t len = strlen(name) + 1;
5467 p = wl_array_add(modifiers_map, len);
5472 strncpy(p, name, len);
5475 static xkb_mod_index_t
5476 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5479 xkb_mod_index_t index = 0;
5480 char *p = modifiers_map->data;
5482 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5483 if (strcmp(p, name) == 0)
5490 return XKB_MOD_INVALID;
5494 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5497 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5499 if (index == XKB_MOD_INVALID)
5500 return XKB_MOD_INVALID;
5506 fail_on_null(void *p)
5509 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5519 return fail_on_null(malloc(s));
5525 return fail_on_null(zalloc(s));
5529 xstrdup(const char *s)
5531 return fail_on_null(strdup(s));