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;
123 struct xkb_context *xkb_context;
126 uint32_t workspace_count;
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
146 struct window_output {
147 struct output *output;
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
156 * width,height are the new buffer size.
157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
159 * Returns the Cairo surface to draw to.
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
162 int32_t width, int32_t height, uint32_t flags,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
170 void (*swap)(struct toysurface *base,
171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
172 struct rectangle *server_allocation);
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
181 * Release the toysurface from the EGL context, returning control
184 void (*release)(struct toysurface *base);
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
190 void (*destroy)(struct toysurface *base);
194 struct window *window;
196 struct wl_surface *surface;
197 struct wl_subsurface *subsurface;
199 int synchronized_default;
200 struct toysurface *toysurface;
201 struct widget *widget;
203 struct wl_callback *frame_cb;
206 struct rectangle allocation;
207 struct rectangle server_allocation;
209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
214 int32_t buffer_scale;
216 cairo_surface_t *cairo_surface;
222 struct display *display;
223 struct window *parent;
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;
258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
266 struct window *window;
267 struct surface *surface;
268 struct tooltip *tooltip;
269 struct wl_list child_list;
271 struct rectangle allocation;
272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
276 widget_motion_handler_t motion_handler;
277 widget_button_handler_t button_handler;
278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
283 widget_axis_handler_t axis_handler;
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
332 struct task repeat_task;
336 uint32_t repeat_time;
340 struct display *display;
341 struct wl_output *output;
342 struct rectangle allocation;
347 display_output_handler_t destroy_handler;
351 enum frame_button_action {
352 FRAME_BUTTON_NULL = 0,
353 FRAME_BUTTON_ICON = 1,
354 FRAME_BUTTON_CLOSE = 2,
355 FRAME_BUTTON_MINIMIZE = 3,
356 FRAME_BUTTON_MAXIMIZE = 4,
359 enum frame_button_pointer {
360 FRAME_BUTTON_DEFAULT = 0,
361 FRAME_BUTTON_OVER = 1,
362 FRAME_BUTTON_ACTIVE = 2,
365 enum frame_button_align {
366 FRAME_BUTTON_RIGHT = 0,
367 FRAME_BUTTON_LEFT = 1,
370 enum frame_button_decoration {
371 FRAME_BUTTON_NONE = 0,
372 FRAME_BUTTON_FANCY = 1,
375 struct frame_button {
376 struct widget *widget;
378 cairo_surface_t *icon;
379 enum frame_button_action type;
380 enum frame_button_pointer state;
381 struct wl_list link; /* buttons_list */
382 enum frame_button_align align;
383 enum frame_button_decoration decoration;
387 struct widget *widget;
388 struct widget *child;
389 struct wl_list buttons_list;
393 struct window *window;
394 struct widget *widget;
396 const char **entries;
405 struct widget *parent;
406 struct window *window;
407 struct widget *widget;
409 struct task tooltip_task;
415 struct wl_shm_pool *pool;
422 CURSOR_DEFAULT = 100,
426 enum window_location {
428 WINDOW_RESIZING_TOP = 1,
429 WINDOW_RESIZING_BOTTOM = 2,
430 WINDOW_RESIZING_LEFT = 4,
431 WINDOW_RESIZING_TOP_LEFT = 5,
432 WINDOW_RESIZING_BOTTOM_LEFT = 6,
433 WINDOW_RESIZING_RIGHT = 8,
434 WINDOW_RESIZING_TOP_RIGHT = 9,
435 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
436 WINDOW_RESIZING_MASK = 15,
437 WINDOW_EXTERIOR = 16,
438 WINDOW_TITLEBAR = 17,
439 WINDOW_CLIENT_AREA = 18,
442 static const cairo_user_data_key_t shm_surface_data_key;
449 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
450 __attribute__ ((format (printf, 4, 5)));
453 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
458 gettimeofday(&tv, NULL);
459 fprintf(stderr, "%8ld.%03ld ",
460 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
463 fprintf(stderr, "%s@%d ",
464 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
466 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
467 fprintf(stderr, "%s ", func);
470 vfprintf(stderr, fmt, ap);
474 #define DBG(fmt, ...) \
475 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
477 #define DBG_OBJ(obj, fmt, ...) \
478 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
482 #define DBG(...) do {} while (0)
483 #define DBG_OBJ(...) do {} while (0)
488 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
492 switch (buffer_transform) {
493 case WL_OUTPUT_TRANSFORM_90:
494 case WL_OUTPUT_TRANSFORM_270:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
496 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
505 *width *= buffer_scale;
506 *height *= buffer_scale;
510 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
514 switch (buffer_transform) {
515 case WL_OUTPUT_TRANSFORM_90:
516 case WL_OUTPUT_TRANSFORM_270:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
518 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
527 *width /= buffer_scale;
528 *height /= buffer_scale;
531 #ifdef HAVE_CAIRO_EGL
533 struct egl_window_surface {
534 struct toysurface base;
535 cairo_surface_t *cairo_surface;
536 struct display *display;
537 struct wl_surface *surface;
538 struct wl_egl_window *egl_window;
539 EGLSurface egl_surface;
542 static struct egl_window_surface *
543 to_egl_window_surface(struct toysurface *base)
545 return container_of(base, struct egl_window_surface, base);
548 static cairo_surface_t *
549 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
550 int32_t width, int32_t height, uint32_t flags,
551 enum wl_output_transform buffer_transform, int32_t buffer_scale)
553 struct egl_window_surface *surface = to_egl_window_surface(base);
555 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
557 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
558 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
560 return cairo_surface_reference(surface->cairo_surface);
564 egl_window_surface_swap(struct toysurface *base,
565 enum wl_output_transform buffer_transform, int32_t buffer_scale,
566 struct rectangle *server_allocation)
568 struct egl_window_surface *surface = to_egl_window_surface(base);
570 cairo_gl_surface_swapbuffers(surface->cairo_surface);
571 wl_egl_window_get_attached_size(surface->egl_window,
572 &server_allocation->width,
573 &server_allocation->height);
575 buffer_to_surface_size (buffer_transform, buffer_scale,
576 &server_allocation->width,
577 &server_allocation->height);
581 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
583 struct egl_window_surface *surface = to_egl_window_surface(base);
584 cairo_device_t *device;
586 device = cairo_surface_get_device(surface->cairo_surface);
591 if (device == surface->display->argb_device)
592 ctx = surface->display->argb_ctx;
597 cairo_device_flush(device);
598 cairo_device_acquire(device);
599 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
600 surface->egl_surface, ctx))
601 fprintf(stderr, "failed to make surface current\n");
607 egl_window_surface_release(struct toysurface *base)
609 struct egl_window_surface *surface = to_egl_window_surface(base);
610 cairo_device_t *device;
612 device = cairo_surface_get_device(surface->cairo_surface);
616 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
617 surface->display->argb_ctx))
618 fprintf(stderr, "failed to make context current\n");
620 cairo_device_release(device);
624 egl_window_surface_destroy(struct toysurface *base)
626 struct egl_window_surface *surface = to_egl_window_surface(base);
627 struct display *d = surface->display;
629 cairo_surface_destroy(surface->cairo_surface);
630 eglDestroySurface(d->dpy, surface->egl_surface);
631 wl_egl_window_destroy(surface->egl_window);
632 surface->surface = NULL;
637 static struct toysurface *
638 egl_window_surface_create(struct display *display,
639 struct wl_surface *wl_surface,
641 struct rectangle *rectangle)
643 struct egl_window_surface *surface;
645 if (display->dpy == EGL_NO_DISPLAY)
648 surface = calloc(1, sizeof *surface);
652 surface->base.prepare = egl_window_surface_prepare;
653 surface->base.swap = egl_window_surface_swap;
654 surface->base.acquire = egl_window_surface_acquire;
655 surface->base.release = egl_window_surface_release;
656 surface->base.destroy = egl_window_surface_destroy;
658 surface->display = display;
659 surface->surface = wl_surface;
661 surface->egl_window = wl_egl_window_create(surface->surface,
665 surface->egl_surface = eglCreateWindowSurface(display->dpy,
666 display->argb_config,
670 surface->cairo_surface =
671 cairo_gl_surface_create_for_egl(display->argb_device,
672 surface->egl_surface,
676 return &surface->base;
681 static struct toysurface *
682 egl_window_surface_create(struct display *display,
683 struct wl_surface *wl_surface,
685 struct rectangle *rectangle)
692 struct shm_surface_data {
693 struct wl_buffer *buffer;
694 struct shm_pool *pool;
698 display_get_buffer_for_surface(struct display *display,
699 cairo_surface_t *surface)
701 struct shm_surface_data *data;
703 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
709 shm_pool_destroy(struct shm_pool *pool);
712 shm_surface_data_destroy(void *p)
714 struct shm_surface_data *data = p;
716 wl_buffer_destroy(data->buffer);
718 shm_pool_destroy(data->pool);
723 static struct wl_shm_pool *
724 make_shm_pool(struct display *display, int size, void **data)
726 struct wl_shm_pool *pool;
729 fd = os_create_anonymous_file(size);
731 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
736 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
737 if (*data == MAP_FAILED) {
738 fprintf(stderr, "mmap failed: %m\n");
743 pool = wl_shm_create_pool(display->shm, fd, size);
750 static struct shm_pool *
751 shm_pool_create(struct display *display, size_t size)
753 struct shm_pool *pool = malloc(sizeof *pool);
758 pool->pool = make_shm_pool(display, size, &pool->data);
771 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
773 if (pool->used + size > pool->size)
776 *offset = pool->used;
779 return (char *) pool->data + *offset;
782 /* destroy the pool. this does not unmap the memory though */
784 shm_pool_destroy(struct shm_pool *pool)
786 munmap(pool->data, pool->size);
787 wl_shm_pool_destroy(pool->pool);
791 /* Start allocating from the beginning of the pool again */
793 shm_pool_reset(struct shm_pool *pool)
799 data_length_for_shm_surface(struct rectangle *rect)
803 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
805 return stride * rect->height;
808 static cairo_surface_t *
809 display_create_shm_surface_from_pool(struct display *display,
810 struct rectangle *rectangle,
811 uint32_t flags, struct shm_pool *pool)
813 struct shm_surface_data *data;
815 cairo_surface_t *surface;
816 cairo_format_t cairo_format;
817 int stride, length, offset;
820 data = malloc(sizeof *data);
824 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
825 cairo_format = CAIRO_FORMAT_RGB16_565;
827 cairo_format = CAIRO_FORMAT_ARGB32;
829 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
830 length = stride * rectangle->height;
832 map = shm_pool_allocate(pool, length, &offset);
839 surface = cairo_image_surface_create_for_data (map,
845 cairo_surface_set_user_data(surface, &shm_surface_data_key,
846 data, shm_surface_data_destroy);
848 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
849 format = WL_SHM_FORMAT_RGB565;
851 if (flags & SURFACE_OPAQUE)
852 format = WL_SHM_FORMAT_XRGB8888;
854 format = WL_SHM_FORMAT_ARGB8888;
857 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
865 static cairo_surface_t *
866 display_create_shm_surface(struct display *display,
867 struct rectangle *rectangle, uint32_t flags,
868 struct shm_pool *alternate_pool,
869 struct shm_surface_data **data_ret)
871 struct shm_surface_data *data;
872 struct shm_pool *pool;
873 cairo_surface_t *surface;
875 if (alternate_pool) {
876 shm_pool_reset(alternate_pool);
877 surface = display_create_shm_surface_from_pool(display,
882 data = cairo_surface_get_user_data(surface,
883 &shm_surface_data_key);
888 pool = shm_pool_create(display,
889 data_length_for_shm_surface(rectangle));
894 display_create_shm_surface_from_pool(display, rectangle,
898 shm_pool_destroy(pool);
902 /* make sure we destroy the pool when the surface is destroyed */
903 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
914 check_size(struct rectangle *rect)
916 if (rect->width && rect->height)
919 fprintf(stderr, "tried to create surface of "
920 "width: %d, height: %d\n", rect->width, rect->height);
925 display_create_surface(struct display *display,
926 struct wl_surface *surface,
927 struct rectangle *rectangle,
930 if (check_size(rectangle) < 0)
933 assert(flags & SURFACE_SHM);
934 return display_create_shm_surface(display, rectangle, flags,
938 struct shm_surface_leaf {
939 cairo_surface_t *cairo_surface;
940 /* 'data' is automatically destroyed, when 'cairo_surface' is */
941 struct shm_surface_data *data;
943 struct shm_pool *resize_pool;
948 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
950 if (leaf->cairo_surface)
951 cairo_surface_destroy(leaf->cairo_surface);
952 /* leaf->data already destroyed via cairo private */
954 if (leaf->resize_pool)
955 shm_pool_destroy(leaf->resize_pool);
957 memset(leaf, 0, sizeof *leaf);
963 struct toysurface base;
964 struct display *display;
965 struct wl_surface *surface;
969 struct shm_surface_leaf leaf[MAX_LEAVES];
970 struct shm_surface_leaf *current;
973 static struct shm_surface *
974 to_shm_surface(struct toysurface *base)
976 return container_of(base, struct shm_surface, base);
980 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
983 struct shm_surface_leaf *leaf;
984 char bufs[MAX_LEAVES + 1];
987 for (i = 0; i < MAX_LEAVES; i++) {
988 leaf = &surface->leaf[i];
992 else if (leaf->cairo_surface)
998 bufs[MAX_LEAVES] = '\0';
999 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1004 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1006 struct shm_surface *surface = data;
1007 struct shm_surface_leaf *leaf;
1011 shm_surface_buffer_state_debug(surface, "buffer_release before");
1013 for (i = 0; i < MAX_LEAVES; i++) {
1014 leaf = &surface->leaf[i];
1015 if (leaf->data && leaf->data->buffer == buffer) {
1020 assert(i < MAX_LEAVES && "unknown buffer released");
1022 /* Leave one free leaf with storage, release others */
1024 for (i = 0; i < MAX_LEAVES; i++) {
1025 leaf = &surface->leaf[i];
1027 if (!leaf->cairo_surface || leaf->busy)
1033 shm_surface_leaf_release(leaf);
1036 shm_surface_buffer_state_debug(surface, "buffer_release after");
1039 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1040 shm_surface_buffer_release
1043 static cairo_surface_t *
1044 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1045 int32_t width, int32_t height, uint32_t flags,
1046 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1048 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1049 struct shm_surface *surface = to_shm_surface(base);
1050 struct rectangle rect = { 0};
1051 struct shm_surface_leaf *leaf = NULL;
1057 /* pick a free buffer, preferrably one that already has storage */
1058 for (i = 0; i < MAX_LEAVES; i++) {
1059 if (surface->leaf[i].busy)
1062 if (!leaf || surface->leaf[i].cairo_surface)
1063 leaf = &surface->leaf[i];
1065 DBG_OBJ(surface->surface, "pick leaf %d\n",
1066 (int)(leaf - &surface->leaf[0]));
1069 fprintf(stderr, "%s: all buffers are held by the server.\n",
1075 if (!resize_hint && leaf->resize_pool) {
1076 cairo_surface_destroy(leaf->cairo_surface);
1077 leaf->cairo_surface = NULL;
1078 shm_pool_destroy(leaf->resize_pool);
1079 leaf->resize_pool = NULL;
1082 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1084 if (leaf->cairo_surface &&
1085 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1086 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1089 if (leaf->cairo_surface)
1090 cairo_surface_destroy(leaf->cairo_surface);
1092 #ifdef USE_RESIZE_POOL
1093 if (resize_hint && !leaf->resize_pool) {
1094 /* Create a big pool to allocate from, while continuously
1095 * resizing. Mmapping a new pool in the server
1096 * is relatively expensive, so reusing a pool performs
1097 * better, but may temporarily reserve unneeded memory.
1099 /* We should probably base this number on the output size. */
1100 leaf->resize_pool = shm_pool_create(surface->display,
1106 rect.height = height;
1108 leaf->cairo_surface =
1109 display_create_shm_surface(surface->display, &rect,
1113 wl_buffer_add_listener(leaf->data->buffer,
1114 &shm_surface_buffer_listener, surface);
1117 surface->current = leaf;
1119 return cairo_surface_reference(leaf->cairo_surface);
1123 shm_surface_swap(struct toysurface *base,
1124 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1125 struct rectangle *server_allocation)
1127 struct shm_surface *surface = to_shm_surface(base);
1128 struct shm_surface_leaf *leaf = surface->current;
1130 server_allocation->width =
1131 cairo_image_surface_get_width(leaf->cairo_surface);
1132 server_allocation->height =
1133 cairo_image_surface_get_height(leaf->cairo_surface);
1135 buffer_to_surface_size (buffer_transform, buffer_scale,
1136 &server_allocation->width,
1137 &server_allocation->height);
1139 wl_surface_attach(surface->surface, leaf->data->buffer,
1140 surface->dx, surface->dy);
1141 wl_surface_damage(surface->surface, 0, 0,
1142 server_allocation->width, server_allocation->height);
1143 wl_surface_commit(surface->surface);
1145 DBG_OBJ(surface->surface, "leaf %d busy\n",
1146 (int)(leaf - &surface->leaf[0]));
1149 surface->current = NULL;
1153 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1159 shm_surface_release(struct toysurface *base)
1164 shm_surface_destroy(struct toysurface *base)
1166 struct shm_surface *surface = to_shm_surface(base);
1169 for (i = 0; i < MAX_LEAVES; i++)
1170 shm_surface_leaf_release(&surface->leaf[i]);
1175 static struct toysurface *
1176 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1177 uint32_t flags, struct rectangle *rectangle)
1179 struct shm_surface *surface;
1180 DBG_OBJ(wl_surface, "\n");
1182 surface = xmalloc(sizeof *surface);
1183 memset(surface, 0, sizeof *surface);
1188 surface->base.prepare = shm_surface_prepare;
1189 surface->base.swap = shm_surface_swap;
1190 surface->base.acquire = shm_surface_acquire;
1191 surface->base.release = shm_surface_release;
1192 surface->base.destroy = shm_surface_destroy;
1194 surface->display = display;
1195 surface->surface = wl_surface;
1196 surface->flags = flags;
1198 return &surface->base;
1202 * The following correspondences between file names and cursors was copied
1203 * from: https://bugs.kde.org/attachment.cgi?id=67313
1206 static const char *bottom_left_corners[] = {
1207 "bottom_left_corner",
1212 static const char *bottom_right_corners[] = {
1213 "bottom_right_corner",
1218 static const char *bottom_sides[] = {
1224 static const char *grabbings[] = {
1227 "208530c400c041818281048008011002"
1230 static const char *left_ptrs[] = {
1237 static const char *left_sides[] = {
1243 static const char *right_sides[] = {
1249 static const char *top_left_corners[] = {
1255 static const char *top_right_corners[] = {
1261 static const char *top_sides[] = {
1267 static const char *xterms[] = {
1273 static const char *hand1s[] = {
1277 "e29285e634086352946a0e7090d73106"
1280 static const char *watches[] = {
1283 "0426c94ea35c87780ff01dc239897213"
1286 struct cursor_alternatives {
1291 static const struct cursor_alternatives cursors[] = {
1292 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1293 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1294 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1295 {grabbings, ARRAY_LENGTH(grabbings)},
1296 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1297 {left_sides, ARRAY_LENGTH(left_sides)},
1298 {right_sides, ARRAY_LENGTH(right_sides)},
1299 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1300 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1301 {top_sides, ARRAY_LENGTH(top_sides)},
1302 {xterms, ARRAY_LENGTH(xterms)},
1303 {hand1s, ARRAY_LENGTH(hand1s)},
1304 {watches, ARRAY_LENGTH(watches)},
1308 create_cursors(struct display *display)
1310 struct weston_config *config;
1311 struct weston_config_section *s;
1315 struct wl_cursor *cursor;
1317 config = weston_config_parse("weston.ini");
1318 s = weston_config_get_section(config, "shell", NULL, NULL);
1319 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1320 weston_config_section_get_int(s, "cursor-size", &size, 32);
1321 weston_config_destroy(config);
1323 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1325 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1327 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1329 for (j = 0; !cursor && j < cursors[i].count; ++j)
1330 cursor = wl_cursor_theme_get_cursor(
1331 display->cursor_theme, cursors[i].names[j]);
1334 fprintf(stderr, "could not load cursor '%s'\n",
1335 cursors[i].names[0]);
1337 display->cursors[i] = cursor;
1342 destroy_cursors(struct display *display)
1344 wl_cursor_theme_destroy(display->cursor_theme);
1345 free(display->cursors);
1348 struct wl_cursor_image *
1349 display_get_pointer_image(struct display *display, int pointer)
1351 struct wl_cursor *cursor = display->cursors[pointer];
1353 return cursor ? cursor->images[0] : NULL;
1357 surface_flush(struct surface *surface)
1359 if (!surface->cairo_surface)
1362 if (surface->opaque_region) {
1363 wl_surface_set_opaque_region(surface->surface,
1364 surface->opaque_region);
1365 wl_region_destroy(surface->opaque_region);
1366 surface->opaque_region = NULL;
1369 if (surface->input_region) {
1370 wl_surface_set_input_region(surface->surface,
1371 surface->input_region);
1372 wl_region_destroy(surface->input_region);
1373 surface->input_region = NULL;
1376 surface->toysurface->swap(surface->toysurface,
1377 surface->buffer_transform, surface->buffer_scale,
1378 &surface->server_allocation);
1380 cairo_surface_destroy(surface->cairo_surface);
1381 surface->cairo_surface = NULL;
1385 window_has_focus(struct window *window)
1387 return window->focus_count > 0;
1391 window_flush(struct window *window)
1393 struct surface *surface;
1395 if (window->type == TYPE_NONE) {
1396 window->type = TYPE_TOPLEVEL;
1397 if (window->shell_surface)
1398 wl_shell_surface_set_toplevel(window->shell_surface);
1401 wl_list_for_each(surface, &window->subsurface_list, link) {
1402 if (surface == window->main_surface)
1405 surface_flush(surface);
1408 surface_flush(window->main_surface);
1412 window_get_display(struct window *window)
1414 return window->display;
1418 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1420 struct display *display = surface->window->display;
1421 struct rectangle allocation = surface->allocation;
1423 if (!surface->toysurface && display->dpy &&
1424 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1425 surface->toysurface =
1426 egl_window_surface_create(display,
1432 if (!surface->toysurface)
1433 surface->toysurface = shm_surface_create(display,
1435 flags, &allocation);
1437 surface->cairo_surface = surface->toysurface->prepare(
1438 surface->toysurface, dx, dy,
1439 allocation.width, allocation.height, flags,
1440 surface->buffer_transform, surface->buffer_scale);
1444 window_create_main_surface(struct window *window)
1446 struct surface *surface = window->main_surface;
1451 if (window->resizing)
1452 flags |= SURFACE_HINT_RESIZE;
1454 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1455 flags |= SURFACE_HINT_RGB565;
1457 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1458 dx = surface->server_allocation.width -
1459 surface->allocation.width;
1461 if (window->resize_edges & WINDOW_RESIZING_TOP)
1462 dy = surface->server_allocation.height -
1463 surface->allocation.height;
1465 window->resize_edges = 0;
1467 surface_create_surface(surface, dx, dy, flags);
1471 window_get_buffer_transform(struct window *window)
1473 return window->main_surface->buffer_transform;
1477 window_set_buffer_transform(struct window *window,
1478 enum wl_output_transform transform)
1480 window->main_surface->buffer_transform = transform;
1481 wl_surface_set_buffer_transform(window->main_surface->surface,
1486 window_set_buffer_scale(struct window *window,
1489 window->main_surface->buffer_scale = scale;
1490 wl_surface_set_buffer_scale(window->main_surface->surface,
1495 window_get_buffer_scale(struct window *window)
1497 return window->main_surface->buffer_scale;
1501 window_get_output_scale(struct window *window)
1503 struct window_output *window_output;
1504 struct window_output *window_output_tmp;
1507 wl_list_for_each_safe(window_output, window_output_tmp,
1508 &window->window_output_list, link) {
1509 if (window_output->output->scale > scale)
1510 scale = window_output->output->scale;
1516 static void frame_destroy(struct frame *frame);
1519 surface_destroy(struct surface *surface)
1521 if (surface->frame_cb)
1522 wl_callback_destroy(surface->frame_cb);
1524 if (surface->input_region)
1525 wl_region_destroy(surface->input_region);
1527 if (surface->opaque_region)
1528 wl_region_destroy(surface->opaque_region);
1530 if (surface->subsurface)
1531 wl_subsurface_destroy(surface->subsurface);
1533 wl_surface_destroy(surface->surface);
1535 if (surface->toysurface)
1536 surface->toysurface->destroy(surface->toysurface);
1538 wl_list_remove(&surface->link);
1543 window_destroy(struct window *window)
1545 struct display *display = window->display;
1546 struct input *input;
1547 struct window_output *window_output;
1548 struct window_output *window_output_tmp;
1550 wl_list_remove(&window->redraw_task.link);
1552 wl_list_for_each(input, &display->input_list, link) {
1553 if (input->touch_focus == window)
1554 input->touch_focus = NULL;
1555 if (input->pointer_focus == window)
1556 input->pointer_focus = NULL;
1557 if (input->keyboard_focus == window)
1558 input->keyboard_focus = NULL;
1559 if (input->focus_widget &&
1560 input->focus_widget->window == window)
1561 input->focus_widget = NULL;
1564 wl_list_for_each_safe(window_output, window_output_tmp,
1565 &window->window_output_list, link) {
1566 free (window_output);
1570 frame_destroy(window->frame);
1572 if (window->shell_surface)
1573 wl_shell_surface_destroy(window->shell_surface);
1575 surface_destroy(window->main_surface);
1577 wl_list_remove(&window->link);
1579 free(window->title);
1583 static struct widget *
1584 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1586 struct widget *child, *target;
1588 wl_list_for_each(child, &widget->child_list, link) {
1589 target = widget_find_widget(child, x, y);
1594 if (widget->allocation.x <= x &&
1595 x < widget->allocation.x + widget->allocation.width &&
1596 widget->allocation.y <= y &&
1597 y < widget->allocation.y + widget->allocation.height) {
1604 static struct widget *
1605 window_find_widget(struct window *window, int32_t x, int32_t y)
1607 struct surface *surface;
1608 struct widget *widget;
1610 wl_list_for_each(surface, &window->subsurface_list, link) {
1611 widget = widget_find_widget(surface->widget, x, y);
1619 static struct widget *
1620 widget_create(struct window *window, struct surface *surface, void *data)
1622 struct widget *widget;
1624 widget = xzalloc(sizeof *widget);
1625 widget->window = window;
1626 widget->surface = surface;
1627 widget->user_data = data;
1628 widget->allocation = surface->allocation;
1629 wl_list_init(&widget->child_list);
1631 widget->tooltip = NULL;
1632 widget->tooltip_count = 0;
1633 widget->default_cursor = CURSOR_LEFT_PTR;
1639 window_add_widget(struct window *window, void *data)
1641 struct widget *widget;
1643 widget = widget_create(window, window->main_surface, data);
1644 wl_list_init(&widget->link);
1645 window->main_surface->widget = widget;
1651 widget_add_widget(struct widget *parent, void *data)
1653 struct widget *widget;
1655 widget = widget_create(parent->window, parent->surface, data);
1656 wl_list_insert(parent->child_list.prev, &widget->link);
1662 widget_destroy(struct widget *widget)
1664 struct display *display = widget->window->display;
1665 struct surface *surface = widget->surface;
1666 struct input *input;
1668 /* Destroy the sub-surface along with the root widget */
1669 if (surface->widget == widget && surface->subsurface)
1670 surface_destroy(widget->surface);
1672 if (widget->tooltip) {
1673 free(widget->tooltip);
1674 widget->tooltip = NULL;
1677 wl_list_for_each(input, &display->input_list, link) {
1678 if (input->focus_widget == widget)
1679 input->focus_widget = NULL;
1682 wl_list_remove(&widget->link);
1687 widget_set_default_cursor(struct widget *widget, int cursor)
1689 widget->default_cursor = cursor;
1693 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1695 *allocation = widget->allocation;
1699 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1701 widget->allocation.width = width;
1702 widget->allocation.height = height;
1706 widget_set_allocation(struct widget *widget,
1707 int32_t x, int32_t y, int32_t width, int32_t height)
1709 widget->allocation.x = x;
1710 widget->allocation.y = y;
1711 widget_set_size(widget, width, height);
1715 widget_set_transparent(struct widget *widget, int transparent)
1717 widget->opaque = !transparent;
1721 widget_get_user_data(struct widget *widget)
1723 return widget->user_data;
1726 static cairo_surface_t *
1727 widget_get_cairo_surface(struct widget *widget)
1729 struct surface *surface = widget->surface;
1730 struct window *window = widget->window;
1732 if (!surface->cairo_surface) {
1733 if (surface == window->main_surface)
1734 window_create_main_surface(window);
1736 surface_create_surface(surface, 0, 0, 0);
1739 return surface->cairo_surface;
1743 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1745 struct surface *surface = widget->surface;
1748 enum wl_output_transform transform;
1749 int surface_width, surface_height;
1750 int translate_x, translate_y;
1753 surface_width = surface->allocation.width;
1754 surface_height = surface->allocation.height;
1756 transform = surface->buffer_transform;
1757 scale = surface->buffer_scale;
1759 switch (transform) {
1760 case WL_OUTPUT_TRANSFORM_FLIPPED:
1761 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1764 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1767 cairo_matrix_init_identity(&m);
1771 switch (transform) {
1772 case WL_OUTPUT_TRANSFORM_NORMAL:
1778 case WL_OUTPUT_TRANSFORM_FLIPPED:
1780 translate_x = surface_width;
1783 case WL_OUTPUT_TRANSFORM_90:
1785 translate_x = surface_height;
1788 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1790 translate_x = surface_height;
1791 translate_y = surface_width;
1793 case WL_OUTPUT_TRANSFORM_180:
1795 translate_x = surface_width;
1796 translate_y = surface_height;
1798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1801 translate_y = surface_height;
1803 case WL_OUTPUT_TRANSFORM_270:
1804 angle = M_PI + M_PI_2;
1806 translate_y = surface_width;
1808 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1809 angle = M_PI + M_PI_2;
1815 cairo_scale(cr, scale, scale);
1816 cairo_translate(cr, translate_x, translate_y);
1817 cairo_rotate(cr, angle);
1818 cairo_transform(cr, &m);
1822 widget_cairo_create(struct widget *widget)
1824 struct surface *surface = widget->surface;
1825 cairo_surface_t *cairo_surface;
1828 cairo_surface = widget_get_cairo_surface(widget);
1829 cr = cairo_create(cairo_surface);
1831 widget_cairo_update_transform(widget, cr);
1833 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1839 widget_get_wl_surface(struct widget *widget)
1841 return widget->surface->surface;
1845 widget_get_last_time(struct widget *widget)
1847 return widget->surface->last_time;
1851 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1853 struct wl_compositor *comp = widget->window->display->compositor;
1854 struct surface *surface = widget->surface;
1856 if (!surface->input_region)
1857 surface->input_region = wl_compositor_create_region(comp);
1860 wl_region_add(surface->input_region,
1861 rect->x, rect->y, rect->width, rect->height);
1866 widget_set_resize_handler(struct widget *widget,
1867 widget_resize_handler_t handler)
1869 widget->resize_handler = handler;
1873 widget_set_redraw_handler(struct widget *widget,
1874 widget_redraw_handler_t handler)
1876 widget->redraw_handler = handler;
1880 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1882 widget->enter_handler = handler;
1886 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1888 widget->leave_handler = handler;
1892 widget_set_motion_handler(struct widget *widget,
1893 widget_motion_handler_t handler)
1895 widget->motion_handler = handler;
1899 widget_set_button_handler(struct widget *widget,
1900 widget_button_handler_t handler)
1902 widget->button_handler = handler;
1906 widget_set_touch_up_handler(struct widget *widget,
1907 widget_touch_up_handler_t handler)
1909 widget->touch_up_handler = handler;
1913 widget_set_touch_down_handler(struct widget *widget,
1914 widget_touch_down_handler_t handler)
1916 widget->touch_down_handler = handler;
1920 widget_set_touch_motion_handler(struct widget *widget,
1921 widget_touch_motion_handler_t handler)
1923 widget->touch_motion_handler = handler;
1927 widget_set_touch_frame_handler(struct widget *widget,
1928 widget_touch_frame_handler_t handler)
1930 widget->touch_frame_handler = handler;
1934 widget_set_touch_cancel_handler(struct widget *widget,
1935 widget_touch_cancel_handler_t handler)
1937 widget->touch_cancel_handler = handler;
1941 widget_set_axis_handler(struct widget *widget,
1942 widget_axis_handler_t handler)
1944 widget->axis_handler = handler;
1948 window_schedule_redraw_task(struct window *window);
1951 widget_schedule_redraw(struct widget *widget)
1953 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1954 widget->surface->redraw_needed = 1;
1955 window_schedule_redraw_task(widget->window);
1959 window_get_surface(struct window *window)
1961 cairo_surface_t *cairo_surface;
1963 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1965 return cairo_surface_reference(cairo_surface);
1969 window_get_wl_surface(struct window *window)
1971 return window->main_surface->surface;
1974 struct wl_shell_surface *
1975 window_get_wl_shell_surface(struct window *window)
1977 return window->shell_surface;
1981 tooltip_redraw_handler(struct widget *widget, void *data)
1984 const int32_t r = 3;
1985 struct tooltip *tooltip = data;
1986 int32_t width, height;
1988 cr = widget_cairo_create(widget);
1989 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1990 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1993 width = widget->allocation.width;
1994 height = widget->allocation.height;
1995 rounded_rect(cr, 0, 0, width, height, r);
1997 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1998 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2001 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2002 cairo_move_to(cr, 10, 16);
2003 cairo_show_text(cr, tooltip->entry);
2007 static cairo_text_extents_t
2008 get_text_extents(struct tooltip *tooltip)
2011 cairo_text_extents_t extents;
2013 /* Use the dummy_surface because tooltip's surface was not
2014 * created yet, and parent does not have a valid surface
2015 * outside repaint, either.
2017 cr = cairo_create(tooltip->window->display->dummy_surface);
2018 cairo_text_extents(cr, tooltip->entry, &extents);
2025 window_create_tooltip(struct tooltip *tooltip)
2027 struct widget *parent = tooltip->parent;
2028 struct display *display = parent->window->display;
2029 struct window *window;
2030 const int offset_y = 27;
2031 const int margin = 3;
2032 cairo_text_extents_t extents;
2034 if (tooltip->widget)
2037 window = window_create_transient(display, parent->window, tooltip->x,
2038 tooltip->y + offset_y,
2039 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2043 tooltip->window = window;
2044 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2046 extents = get_text_extents(tooltip);
2047 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2048 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2054 widget_destroy_tooltip(struct widget *parent)
2056 struct tooltip *tooltip = parent->tooltip;
2058 parent->tooltip_count = 0;
2062 if (tooltip->widget) {
2063 widget_destroy(tooltip->widget);
2064 window_destroy(tooltip->window);
2065 tooltip->widget = NULL;
2066 tooltip->window = NULL;
2069 close(tooltip->tooltip_fd);
2070 free(tooltip->entry);
2072 parent->tooltip = NULL;
2076 tooltip_func(struct task *task, uint32_t events)
2078 struct tooltip *tooltip =
2079 container_of(task, struct tooltip, tooltip_task);
2082 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2084 window_create_tooltip(tooltip);
2087 #define TOOLTIP_TIMEOUT 500
2089 tooltip_timer_reset(struct tooltip *tooltip)
2091 struct itimerspec its;
2093 its.it_interval.tv_sec = 0;
2094 its.it_interval.tv_nsec = 0;
2095 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2096 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2097 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2098 fprintf(stderr, "could not set timerfd\n: %m");
2106 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2108 struct tooltip *tooltip = parent->tooltip;
2110 parent->tooltip_count++;
2114 tooltip_timer_reset(tooltip);
2118 /* the handler might be triggered too fast via input device motion, so
2119 * we need this check here to make sure tooltip is fully initialized */
2120 if (parent->tooltip_count > 1)
2123 tooltip = malloc(sizeof *tooltip);
2127 parent->tooltip = tooltip;
2128 tooltip->parent = parent;
2129 tooltip->widget = NULL;
2130 tooltip->window = NULL;
2133 tooltip->entry = strdup(entry);
2134 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2135 if (tooltip->tooltip_fd < 0) {
2136 fprintf(stderr, "could not create timerfd\n: %m");
2140 tooltip->tooltip_task.run = tooltip_func;
2141 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2142 EPOLLIN, &tooltip->tooltip_task);
2143 tooltip_timer_reset(tooltip);
2149 workspace_manager_state(void *data,
2150 struct workspace_manager *workspace_manager,
2154 struct display *display = data;
2156 display->workspace = current;
2157 display->workspace_count = count;
2160 static const struct workspace_manager_listener workspace_manager_listener = {
2161 workspace_manager_state
2165 frame_resize_handler(struct widget *widget,
2166 int32_t width, int32_t height, void *data)
2168 struct frame *frame = data;
2169 struct widget *child = frame->child;
2170 struct rectangle allocation;
2171 struct display *display = widget->window->display;
2172 struct surface *surface = widget->surface;
2173 struct frame_button * button;
2174 struct theme *t = display->theme;
2175 int x_l, x_r, y, w, h;
2176 int decoration_width, decoration_height;
2177 int opaque_margin, shadow_margin;
2179 switch (widget->window->type) {
2180 case TYPE_FULLSCREEN:
2181 decoration_width = 0;
2182 decoration_height = 0;
2186 allocation.width = width;
2187 allocation.height = height;
2190 wl_list_for_each(button, &frame->buttons_list, link)
2191 button->widget->opaque = 1;
2193 case TYPE_MAXIMIZED:
2194 decoration_width = t->width * 2;
2195 decoration_height = t->width + t->titlebar_height;
2197 allocation.x = t->width;
2198 allocation.y = t->titlebar_height;
2199 allocation.width = width - decoration_width;
2200 allocation.height = height - decoration_height;
2204 wl_list_for_each(button, &frame->buttons_list, link)
2205 button->widget->opaque = 0;
2208 decoration_width = (t->width + t->margin) * 2;
2209 decoration_height = t->width +
2210 t->titlebar_height + t->margin * 2;
2212 allocation.x = t->width + t->margin;
2213 allocation.y = t->titlebar_height + t->margin;
2214 allocation.width = width - decoration_width;
2215 allocation.height = height - decoration_height;
2217 opaque_margin = t->margin + t->frame_radius;
2219 wl_list_for_each(button, &frame->buttons_list, link)
2220 button->widget->opaque = 0;
2224 widget_set_allocation(child, allocation.x, allocation.y,
2225 allocation.width, allocation.height);
2227 if (child->resize_handler)
2228 child->resize_handler(child,
2233 width = child->allocation.width + decoration_width;
2234 height = child->allocation.height + decoration_height;
2236 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2238 surface->input_region =
2239 wl_compositor_create_region(display->compositor);
2240 if (widget->window->type != TYPE_FULLSCREEN) {
2241 wl_region_add(surface->input_region,
2242 shadow_margin, shadow_margin,
2243 width - 2 * shadow_margin,
2244 height - 2 * shadow_margin);
2246 wl_region_add(surface->input_region, 0, 0, width, height);
2249 widget_set_allocation(widget, 0, 0, width, height);
2252 wl_region_add(surface->opaque_region,
2253 opaque_margin, opaque_margin,
2254 widget->allocation.width - 2 * opaque_margin,
2255 widget->allocation.height - 2 * opaque_margin);
2257 /* frame internal buttons */
2258 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2259 x_l = t->width + shadow_margin;
2260 y = t->width + shadow_margin;
2261 wl_list_for_each(button, &frame->buttons_list, link) {
2262 const int button_padding = 4;
2263 w = cairo_image_surface_get_width(button->icon);
2264 h = cairo_image_surface_get_height(button->icon);
2266 if (button->decoration == FRAME_BUTTON_FANCY)
2269 if (button->align == FRAME_BUTTON_LEFT) {
2270 widget_set_allocation(button->widget,
2271 x_l, y , w + 1, h + 1);
2273 x_l += button_padding;
2276 widget_set_allocation(button->widget,
2277 x_r, y , w + 1, h + 1);
2278 x_r -= button_padding;
2284 frame_button_enter_handler(struct widget *widget,
2285 struct input *input, float x, float y, void *data)
2287 struct frame_button *frame_button = data;
2289 widget_schedule_redraw(frame_button->widget);
2290 frame_button->state = FRAME_BUTTON_OVER;
2292 return CURSOR_LEFT_PTR;
2296 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2298 struct frame_button *frame_button = data;
2300 widget_schedule_redraw(frame_button->widget);
2301 frame_button->state = FRAME_BUTTON_DEFAULT;
2305 frame_button_button_handler(struct widget *widget,
2306 struct input *input, uint32_t time,
2308 enum wl_pointer_button_state state, void *data)
2310 struct frame_button *frame_button = data;
2311 struct window *window = widget->window;
2312 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
2314 if (button != BTN_LEFT)
2318 case WL_POINTER_BUTTON_STATE_PRESSED:
2319 frame_button->state = FRAME_BUTTON_ACTIVE;
2320 widget_schedule_redraw(frame_button->widget);
2322 if (frame_button->type == FRAME_BUTTON_ICON)
2323 window_show_frame_menu(window, input, time);
2325 case WL_POINTER_BUTTON_STATE_RELEASED:
2326 frame_button->state = FRAME_BUTTON_DEFAULT;
2327 widget_schedule_redraw(frame_button->widget);
2334 switch (frame_button->type) {
2335 case FRAME_BUTTON_CLOSE:
2336 if (window->close_handler)
2337 window->close_handler(window->parent,
2340 display_exit(window->display);
2342 case FRAME_BUTTON_MINIMIZE:
2343 fprintf(stderr,"Minimize stub\n");
2345 case FRAME_BUTTON_MAXIMIZE:
2346 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2349 /* Unknown operation */
2355 frame_button_touch_down_handler(struct widget *widget, struct input *input,
2356 uint32_t serial, uint32_t time, int32_t id,
2357 float x, float y, void *data)
2359 struct frame_button *frame_button = data;
2360 struct window *window = widget->window;
2362 switch (frame_button->type) {
2363 case FRAME_BUTTON_CLOSE:
2364 if (window->close_handler)
2365 window->close_handler(window->parent,
2368 display_exit(window->display);
2370 case FRAME_BUTTON_MINIMIZE:
2371 fprintf(stderr,"Minimize stub\n");
2373 case FRAME_BUTTON_MAXIMIZE:
2374 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2377 /* Unknown operation */
2384 frame_button_motion_handler(struct widget *widget,
2385 struct input *input, uint32_t time,
2386 float x, float y, void *data)
2388 struct frame_button *frame_button = data;
2389 enum frame_button_pointer previous_button_state = frame_button->state;
2391 /* only track state for a pressed button */
2392 if (input->grab != widget)
2393 return CURSOR_LEFT_PTR;
2395 if (x > widget->allocation.x &&
2396 x < (widget->allocation.x + widget->allocation.width) &&
2397 y > widget->allocation.y &&
2398 y < (widget->allocation.y + widget->allocation.height)) {
2399 frame_button->state = FRAME_BUTTON_ACTIVE;
2401 frame_button->state = FRAME_BUTTON_DEFAULT;
2404 if (frame_button->state != previous_button_state)
2405 widget_schedule_redraw(frame_button->widget);
2407 return CURSOR_LEFT_PTR;
2411 frame_button_redraw_handler(struct widget *widget, void *data)
2413 struct frame_button *frame_button = data;
2415 int width, height, x, y;
2417 x = widget->allocation.x;
2418 y = widget->allocation.y;
2419 width = widget->allocation.width;
2420 height = widget->allocation.height;
2429 cr = widget_cairo_create(widget);
2431 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2432 cairo_set_line_width(cr, 1);
2434 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2435 cairo_rectangle (cr, x, y, 25, 16);
2437 cairo_stroke_preserve(cr);
2439 switch (frame_button->state) {
2440 case FRAME_BUTTON_DEFAULT:
2441 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2443 case FRAME_BUTTON_OVER:
2444 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2446 case FRAME_BUTTON_ACTIVE:
2447 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2456 cairo_set_source_surface(cr, frame_button->icon, x, y);
2462 static struct widget *
2463 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2464 enum frame_button_align align, enum frame_button_decoration style)
2466 struct frame_button *frame_button;
2467 const char *icon = data;
2469 frame_button = xzalloc (sizeof *frame_button);
2470 frame_button->icon = cairo_image_surface_create_from_png(icon);
2471 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2472 frame_button->frame = frame;
2473 frame_button->type = type;
2474 frame_button->align = align;
2475 frame_button->decoration = style;
2477 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2479 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2480 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2481 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2482 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
2483 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
2484 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
2485 return frame_button->widget;
2489 frame_button_destroy(struct frame_button *frame_button)
2491 widget_destroy(frame_button->widget);
2492 wl_list_remove(&frame_button->link);
2493 cairo_surface_destroy(frame_button->icon);
2500 frame_redraw_handler(struct widget *widget, void *data)
2503 struct window *window = widget->window;
2504 struct theme *t = window->display->theme;
2507 if (window->type == TYPE_FULLSCREEN)
2510 cr = widget_cairo_create(widget);
2512 if (window->focus_count)
2513 flags |= THEME_FRAME_ACTIVE;
2514 if (window->type == TYPE_MAXIMIZED)
2515 flags |= THEME_FRAME_MAXIMIZED;
2516 theme_render_frame(t, cr, widget->allocation.width,
2517 widget->allocation.height, window->title, flags);
2523 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
2525 struct theme *t = frame->widget->window->display->theme;
2526 struct window *window = frame->widget->window;
2529 if (window->type != TYPE_TOPLEVEL)
2530 return CURSOR_LEFT_PTR;
2532 location = theme_get_location(t, input->sx, input->sy,
2533 frame->widget->allocation.width,
2534 frame->widget->allocation.height,
2535 window->type == TYPE_MAXIMIZED ?
2536 THEME_FRAME_MAXIMIZED : 0);
2539 case THEME_LOCATION_RESIZING_TOP:
2541 case THEME_LOCATION_RESIZING_BOTTOM:
2542 return CURSOR_BOTTOM;
2543 case THEME_LOCATION_RESIZING_LEFT:
2545 case THEME_LOCATION_RESIZING_RIGHT:
2546 return CURSOR_RIGHT;
2547 case THEME_LOCATION_RESIZING_TOP_LEFT:
2548 return CURSOR_TOP_LEFT;
2549 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2550 return CURSOR_TOP_RIGHT;
2551 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2552 return CURSOR_BOTTOM_LEFT;
2553 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2554 return CURSOR_BOTTOM_RIGHT;
2555 case THEME_LOCATION_EXTERIOR:
2556 case THEME_LOCATION_TITLEBAR:
2558 return CURSOR_LEFT_PTR;
2563 frame_menu_func(struct window *window, int index, void *data)
2565 struct display *display;
2569 if (window->close_handler)
2570 window->close_handler(window->parent,
2573 display_exit(window->display);
2575 case 1: /* move to workspace above */
2576 display = window->display;
2577 if (display->workspace > 0)
2578 workspace_manager_move_surface(
2579 display->workspace_manager,
2580 window->main_surface->surface,
2581 display->workspace - 1);
2583 case 2: /* move to workspace below */
2584 display = window->display;
2585 if (display->workspace < display->workspace_count - 1)
2586 workspace_manager_move_surface(
2587 display->workspace_manager,
2588 window->main_surface->surface,
2589 display->workspace + 1);
2591 case 3: /* fullscreen */
2592 /* we don't have a way to get out of fullscreen for now */
2593 if (window->fullscreen_handler)
2594 window->fullscreen_handler(window, window->user_data);
2600 window_show_frame_menu(struct window *window,
2601 struct input *input, uint32_t time)
2606 static const char *entries[] = {
2608 "Move to workspace above", "Move to workspace below",
2612 if (window->fullscreen_handler)
2613 count = ARRAY_LENGTH(entries);
2615 count = ARRAY_LENGTH(entries) - 1;
2617 input_get_position(input, &x, &y);
2618 window_show_menu(window->display, input, time, window,
2619 x - 10, y - 10, frame_menu_func, entries, count);
2623 frame_enter_handler(struct widget *widget,
2624 struct input *input, float x, float y, void *data)
2626 return frame_get_pointer_image_for_location(data, input);
2630 frame_motion_handler(struct widget *widget,
2631 struct input *input, uint32_t time,
2632 float x, float y, void *data)
2634 return frame_get_pointer_image_for_location(data, input);
2638 frame_button_handler(struct widget *widget,
2639 struct input *input, uint32_t time,
2640 uint32_t button, enum wl_pointer_button_state state,
2644 struct frame *frame = data;
2645 struct window *window = widget->window;
2646 struct display *display = window->display;
2649 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
2652 location = theme_get_location(display->theme, input->sx, input->sy,
2653 frame->widget->allocation.width,
2654 frame->widget->allocation.height,
2655 window->type == TYPE_MAXIMIZED ?
2656 THEME_FRAME_MAXIMIZED : 0);
2658 if (window->display->shell && button == BTN_LEFT &&
2659 window->type == TYPE_TOPLEVEL) {
2661 case THEME_LOCATION_TITLEBAR:
2662 if (!window->shell_surface)
2664 input_ungrab(input);
2665 wl_shell_surface_move(window->shell_surface,
2666 input_get_seat(input),
2669 case THEME_LOCATION_RESIZING_TOP:
2670 case THEME_LOCATION_RESIZING_BOTTOM:
2671 case THEME_LOCATION_RESIZING_LEFT:
2672 case THEME_LOCATION_RESIZING_RIGHT:
2673 case THEME_LOCATION_RESIZING_TOP_LEFT:
2674 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2675 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2676 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2677 if (!window->shell_surface)
2679 input_ungrab(input);
2681 window->resizing = 1;
2682 wl_shell_surface_resize(window->shell_surface,
2683 input_get_seat(input),
2684 display->serial, location);
2687 } else if (button == BTN_RIGHT &&
2688 (window->type == TYPE_TOPLEVEL ||
2689 window->type == TYPE_MAXIMIZED)) {
2690 window_show_frame_menu(window, input, time);
2695 frame_touch_down_handler(struct widget *widget, struct input *input,
2696 uint32_t serial, uint32_t time, int32_t id,
2697 float x, float y, void *data)
2699 struct window *window = widget->window;
2700 struct display *display = window->display;
2702 wl_shell_surface_move(window->shell_surface,
2703 input_get_seat(input),
2708 frame_create(struct window *window, void *data)
2710 struct frame *frame;
2712 frame = xzalloc(sizeof *frame);
2713 frame->widget = window_add_widget(window, frame);
2714 frame->child = widget_add_widget(frame->widget, data);
2716 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2717 widget_set_resize_handler(frame->widget, frame_resize_handler);
2718 widget_set_enter_handler(frame->widget, frame_enter_handler);
2719 widget_set_motion_handler(frame->widget, frame_motion_handler);
2720 widget_set_button_handler(frame->widget, frame_button_handler);
2721 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2723 /* Create empty list for frame buttons */
2724 wl_list_init(&frame->buttons_list);
2726 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2727 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2729 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2730 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2732 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2733 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2735 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2736 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2738 window->frame = frame;
2740 return frame->child;
2744 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2746 struct display *display = widget->window->display;
2747 struct theme *t = display->theme;
2748 int decoration_width, decoration_height;
2750 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2752 if (widget->window->type != TYPE_FULLSCREEN) {
2753 decoration_width = (t->width + margin) * 2;
2754 decoration_height = t->width +
2755 t->titlebar_height + margin * 2;
2757 width = child_width + decoration_width;
2758 height = child_height + decoration_height;
2760 width = child_width;
2761 height = child_height;
2764 window_schedule_resize(widget->window, width, height);
2768 frame_destroy(struct frame *frame)
2770 struct frame_button *button, *tmp;
2772 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2773 frame_button_destroy(button);
2775 /* frame->child must be destroyed by the application */
2776 widget_destroy(frame->widget);
2781 input_set_focus_widget(struct input *input, struct widget *focus,
2784 struct widget *old, *widget;
2787 if (focus == input->focus_widget)
2790 old = input->focus_widget;
2794 widget = input->grab;
2795 if (widget->leave_handler)
2796 widget->leave_handler(old, input, widget->user_data);
2797 input->focus_widget = NULL;
2803 widget = input->grab;
2804 input->focus_widget = focus;
2805 if (widget->enter_handler)
2806 cursor = widget->enter_handler(focus, input, x, y,
2809 cursor = widget->default_cursor;
2811 input_set_pointer_image(input, cursor);
2816 input_grab(struct input *input, struct widget *widget, uint32_t button)
2818 input->grab = widget;
2819 input->grab_button = button;
2823 input_ungrab(struct input *input)
2825 struct widget *widget;
2828 if (input->pointer_focus) {
2829 widget = window_find_widget(input->pointer_focus,
2830 input->sx, input->sy);
2831 input_set_focus_widget(input, widget, input->sx, input->sy);
2836 input_remove_pointer_focus(struct input *input)
2838 struct window *window = input->pointer_focus;
2843 input_set_focus_widget(input, NULL, 0, 0);
2845 input->pointer_focus = NULL;
2846 input->current_cursor = CURSOR_UNSET;
2850 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2851 uint32_t serial, struct wl_surface *surface,
2852 wl_fixed_t sx_w, wl_fixed_t sy_w)
2854 struct input *input = data;
2855 struct window *window;
2856 struct widget *widget;
2857 float sx = wl_fixed_to_double(sx_w);
2858 float sy = wl_fixed_to_double(sy_w);
2861 /* enter event for a window we've just destroyed */
2865 input->display->serial = serial;
2866 input->pointer_enter_serial = serial;
2867 input->pointer_focus = wl_surface_get_user_data(surface);
2868 window = input->pointer_focus;
2870 if (window->resizing) {
2871 window->resizing = 0;
2872 /* Schedule a redraw to free the pool */
2873 window_schedule_redraw(window);
2879 widget = window_find_widget(window, sx, sy);
2880 input_set_focus_widget(input, widget, sx, sy);
2884 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2885 uint32_t serial, struct wl_surface *surface)
2887 struct input *input = data;
2889 input->display->serial = serial;
2890 input_remove_pointer_focus(input);
2894 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2895 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2897 struct input *input = data;
2898 struct window *window = input->pointer_focus;
2899 struct widget *widget;
2901 float sx = wl_fixed_to_double(sx_w);
2902 float sy = wl_fixed_to_double(sy_w);
2910 /* when making the window smaller - e.g. after a unmaximise we might
2911 * still have a pending motion event that the compositor has picked
2912 * based on the old surface dimensions
2914 if (sx > window->main_surface->allocation.width ||
2915 sy > window->main_surface->allocation.height)
2918 if (!(input->grab && input->grab_button)) {
2919 widget = window_find_widget(window, sx, sy);
2920 input_set_focus_widget(input, widget, sx, sy);
2924 widget = input->grab;
2926 widget = input->focus_widget;
2928 if (widget->motion_handler)
2929 cursor = widget->motion_handler(input->focus_widget,
2930 input, time, sx, sy,
2933 cursor = widget->default_cursor;
2935 cursor = CURSOR_LEFT_PTR;
2937 input_set_pointer_image(input, cursor);
2941 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2942 uint32_t time, uint32_t button, uint32_t state_w)
2944 struct input *input = data;
2945 struct widget *widget;
2946 enum wl_pointer_button_state state = state_w;
2948 input->display->serial = serial;
2949 if (input->focus_widget && input->grab == NULL &&
2950 state == WL_POINTER_BUTTON_STATE_PRESSED)
2951 input_grab(input, input->focus_widget, button);
2953 widget = input->grab;
2954 if (widget && widget->button_handler)
2955 (*widget->button_handler)(widget,
2958 input->grab->user_data);
2960 if (input->grab && input->grab_button == button &&
2961 state == WL_POINTER_BUTTON_STATE_RELEASED)
2962 input_ungrab(input);
2966 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2967 uint32_t time, uint32_t axis, wl_fixed_t value)
2969 struct input *input = data;
2970 struct widget *widget;
2972 widget = input->focus_widget;
2974 widget = input->grab;
2975 if (widget && widget->axis_handler)
2976 (*widget->axis_handler)(widget,
2982 static const struct wl_pointer_listener pointer_listener = {
2983 pointer_handle_enter,
2984 pointer_handle_leave,
2985 pointer_handle_motion,
2986 pointer_handle_button,
2987 pointer_handle_axis,
2991 input_remove_keyboard_focus(struct input *input)
2993 struct window *window = input->keyboard_focus;
2994 struct itimerspec its;
2996 its.it_interval.tv_sec = 0;
2997 its.it_interval.tv_nsec = 0;
2998 its.it_value.tv_sec = 0;
2999 its.it_value.tv_nsec = 0;
3000 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3005 window->focus_count--;
3006 if (window->keyboard_focus_handler)
3007 (*window->keyboard_focus_handler)(window, NULL,
3010 input->keyboard_focus = NULL;
3014 keyboard_repeat_func(struct task *task, uint32_t events)
3016 struct input *input =
3017 container_of(task, struct input, repeat_task);
3018 struct window *window = input->keyboard_focus;
3021 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3022 /* If we change the timer between the fd becoming
3023 * readable and getting here, there'll be nothing to
3024 * read and we get EAGAIN. */
3027 if (window && window->key_handler) {
3028 (*window->key_handler)(window, input, input->repeat_time,
3029 input->repeat_key, input->repeat_sym,
3030 WL_KEYBOARD_KEY_STATE_PRESSED,
3036 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3037 uint32_t format, int fd, uint32_t size)
3039 struct input *input = data;
3047 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3052 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3053 if (map_str == MAP_FAILED) {
3058 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3060 XKB_KEYMAP_FORMAT_TEXT_V1,
3062 munmap(map_str, size);
3065 if (!input->xkb.keymap) {
3066 fprintf(stderr, "failed to compile keymap\n");
3070 input->xkb.state = xkb_state_new(input->xkb.keymap);
3071 if (!input->xkb.state) {
3072 fprintf(stderr, "failed to create XKB state\n");
3073 xkb_map_unref(input->xkb.keymap);
3074 input->xkb.keymap = NULL;
3078 input->xkb.control_mask =
3079 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3080 input->xkb.alt_mask =
3081 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3082 input->xkb.shift_mask =
3083 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3087 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3088 uint32_t serial, struct wl_surface *surface,
3089 struct wl_array *keys)
3091 struct input *input = data;
3092 struct window *window;
3094 input->display->serial = serial;
3095 input->keyboard_focus = wl_surface_get_user_data(surface);
3097 window = input->keyboard_focus;
3098 window->focus_count++;
3099 if (window->keyboard_focus_handler)
3100 (*window->keyboard_focus_handler)(window,
3101 input, window->user_data);
3105 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3106 uint32_t serial, struct wl_surface *surface)
3108 struct input *input = data;
3110 input->display->serial = serial;
3111 input_remove_keyboard_focus(input);
3115 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
3116 uint32_t serial, uint32_t time, uint32_t key,
3119 struct input *input = data;
3120 struct window *window = input->keyboard_focus;
3121 uint32_t code, num_syms;
3122 enum wl_keyboard_key_state state = state_w;
3123 const xkb_keysym_t *syms;
3125 struct itimerspec its;
3127 input->display->serial = serial;
3129 if (!window || !input->xkb.state)
3132 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
3134 sym = XKB_KEY_NoSymbol;
3138 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
3139 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3140 window_set_maximized(window,
3141 window->type != TYPE_MAXIMIZED);
3142 } else if (sym == XKB_KEY_F11 &&
3143 window->fullscreen_handler &&
3144 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3145 window->fullscreen_handler(window, window->user_data);
3146 } else if (sym == XKB_KEY_F4 &&
3147 input->modifiers == MOD_ALT_MASK &&
3148 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3149 if (window->close_handler)
3150 window->close_handler(window->parent,
3153 display_exit(window->display);
3154 } else if (window->key_handler) {
3155 (*window->key_handler)(window, input, time, key,
3156 sym, state, window->user_data);
3159 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3160 key == input->repeat_key) {
3161 its.it_interval.tv_sec = 0;
3162 its.it_interval.tv_nsec = 0;
3163 its.it_value.tv_sec = 0;
3164 its.it_value.tv_nsec = 0;
3165 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3166 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3167 input->repeat_sym = sym;
3168 input->repeat_key = key;
3169 input->repeat_time = time;
3170 its.it_interval.tv_sec = 0;
3171 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3172 its.it_value.tv_sec = 0;
3173 its.it_value.tv_nsec = 400 * 1000 * 1000;
3174 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3179 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3180 uint32_t serial, uint32_t mods_depressed,
3181 uint32_t mods_latched, uint32_t mods_locked,
3184 struct input *input = data;
3185 xkb_mod_mask_t mask;
3187 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3188 if (!input->xkb.keymap)
3191 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3192 mods_locked, 0, 0, group);
3193 mask = xkb_state_serialize_mods(input->xkb.state,
3194 XKB_STATE_DEPRESSED |
3196 input->modifiers = 0;
3197 if (mask & input->xkb.control_mask)
3198 input->modifiers |= MOD_CONTROL_MASK;
3199 if (mask & input->xkb.alt_mask)
3200 input->modifiers |= MOD_ALT_MASK;
3201 if (mask & input->xkb.shift_mask)
3202 input->modifiers |= MOD_SHIFT_MASK;
3205 static const struct wl_keyboard_listener keyboard_listener = {
3206 keyboard_handle_keymap,
3207 keyboard_handle_enter,
3208 keyboard_handle_leave,
3209 keyboard_handle_key,
3210 keyboard_handle_modifiers,
3214 touch_handle_down(void *data, struct wl_touch *wl_touch,
3215 uint32_t serial, uint32_t time, struct wl_surface *surface,
3216 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3218 struct input *input = data;
3219 struct widget *widget;
3220 float sx = wl_fixed_to_double(x_w);
3221 float sy = wl_fixed_to_double(y_w);
3223 input->display->serial = serial;
3224 input->touch_focus = wl_surface_get_user_data(surface);
3225 if (!input->touch_focus) {
3226 DBG("Failed to find to touch focus for surface %p\n", surface);
3230 widget = window_find_widget(input->touch_focus,
3231 wl_fixed_to_double(x_w),
3232 wl_fixed_to_double(y_w));
3234 struct touch_point *tp = xmalloc(sizeof *tp);
3237 tp->widget = widget;
3238 wl_list_insert(&input->touch_point_list, &tp->link);
3240 if (widget->touch_down_handler)
3241 (*widget->touch_down_handler)(widget, input,
3250 touch_handle_up(void *data, struct wl_touch *wl_touch,
3251 uint32_t serial, uint32_t time, int32_t id)
3253 struct input *input = data;
3254 struct touch_point *tp, *tmp;
3256 if (!input->touch_focus) {
3257 DBG("No touch focus found for touch up event!\n");
3261 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3265 if (tp->widget->touch_up_handler)
3266 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3268 tp->widget->user_data);
3270 wl_list_remove(&tp->link);
3278 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3279 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3281 struct input *input = data;
3282 struct touch_point *tp;
3283 float sx = wl_fixed_to_double(x_w);
3284 float sy = wl_fixed_to_double(y_w);
3286 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3288 if (!input->touch_focus) {
3289 DBG("No touch focus found for touch motion event!\n");
3293 wl_list_for_each(tp, &input->touch_point_list, link) {
3297 if (tp->widget->touch_motion_handler)
3298 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3300 tp->widget->user_data);
3306 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3308 struct input *input = data;
3309 struct touch_point *tp, *tmp;
3311 DBG("touch_handle_frame\n");
3313 if (!input->touch_focus) {
3314 DBG("No touch focus found for touch frame event!\n");
3318 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3319 if (tp->widget->touch_frame_handler)
3320 (*tp->widget->touch_frame_handler)(tp->widget, input,
3321 tp->widget->user_data);
3323 wl_list_remove(&tp->link);
3329 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3331 struct input *input = data;
3332 struct touch_point *tp, *tmp;
3334 DBG("touch_handle_cancel\n");
3336 if (!input->touch_focus) {
3337 DBG("No touch focus found for touch cancel event!\n");
3341 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3342 if (tp->widget->touch_cancel_handler)
3343 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3344 tp->widget->user_data);
3346 wl_list_remove(&tp->link);
3351 static const struct wl_touch_listener touch_listener = {
3354 touch_handle_motion,
3356 touch_handle_cancel,
3360 seat_handle_capabilities(void *data, struct wl_seat *seat,
3361 enum wl_seat_capability caps)
3363 struct input *input = data;
3365 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3366 input->pointer = wl_seat_get_pointer(seat);
3367 wl_pointer_set_user_data(input->pointer, input);
3368 wl_pointer_add_listener(input->pointer, &pointer_listener,
3370 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3371 wl_pointer_destroy(input->pointer);
3372 input->pointer = NULL;
3375 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3376 input->keyboard = wl_seat_get_keyboard(seat);
3377 wl_keyboard_set_user_data(input->keyboard, input);
3378 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3380 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3381 wl_keyboard_destroy(input->keyboard);
3382 input->keyboard = NULL;
3385 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3386 input->touch = wl_seat_get_touch(seat);
3387 wl_touch_set_user_data(input->touch, input);
3388 wl_touch_add_listener(input->touch, &touch_listener, input);
3389 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3390 wl_touch_destroy(input->touch);
3391 input->touch = NULL;
3396 seat_handle_name(void *data, struct wl_seat *seat,
3402 static const struct wl_seat_listener seat_listener = {
3403 seat_handle_capabilities,
3408 input_get_position(struct input *input, int32_t *x, int32_t *y)
3415 input_get_display(struct input *input)
3417 return input->display;
3421 input_get_seat(struct input *input)
3427 input_get_modifiers(struct input *input)
3429 return input->modifiers;
3433 input_get_focus_widget(struct input *input)
3435 return input->focus_widget;
3439 struct wl_data_offer *offer;
3440 struct input *input;
3441 struct wl_array types;
3444 struct task io_task;
3452 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3454 struct data_offer *offer = data;
3457 p = wl_array_add(&offer->types, sizeof *p);
3461 static const struct wl_data_offer_listener data_offer_listener = {
3466 data_offer_destroy(struct data_offer *offer)
3471 if (offer->refcount == 0) {
3472 wl_data_offer_destroy(offer->offer);
3473 for (p = offer->types.data; *p; p++)
3475 wl_array_release(&offer->types);
3481 data_device_data_offer(void *data,
3482 struct wl_data_device *data_device,
3483 struct wl_data_offer *_offer)
3485 struct data_offer *offer;
3487 offer = xmalloc(sizeof *offer);
3489 wl_array_init(&offer->types);
3490 offer->refcount = 1;
3491 offer->input = data;
3492 offer->offer = _offer;
3493 wl_data_offer_add_listener(offer->offer,
3494 &data_offer_listener, offer);
3498 data_device_enter(void *data, struct wl_data_device *data_device,
3499 uint32_t serial, struct wl_surface *surface,
3500 wl_fixed_t x_w, wl_fixed_t y_w,
3501 struct wl_data_offer *offer)
3503 struct input *input = data;
3504 struct window *window;
3506 float x = wl_fixed_to_double(x_w);
3507 float y = wl_fixed_to_double(y_w);
3510 input->pointer_enter_serial = serial;
3511 window = wl_surface_get_user_data(surface);
3512 input->pointer_focus = window;
3515 input->drag_offer = wl_data_offer_get_user_data(offer);
3517 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3520 types_data = input->drag_offer->types.data;
3522 input->drag_offer = NULL;
3526 window = input->pointer_focus;
3527 if (window->data_handler)
3528 window->data_handler(window, input, x, y, types_data,
3533 data_device_leave(void *data, struct wl_data_device *data_device)
3535 struct input *input = data;
3537 if (input->drag_offer) {
3538 data_offer_destroy(input->drag_offer);
3539 input->drag_offer = NULL;
3544 data_device_motion(void *data, struct wl_data_device *data_device,
3545 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3547 struct input *input = data;
3548 struct window *window = input->pointer_focus;
3549 float x = wl_fixed_to_double(x_w);
3550 float y = wl_fixed_to_double(y_w);
3556 if (input->drag_offer)
3557 types_data = input->drag_offer->types.data;
3561 if (window->data_handler)
3562 window->data_handler(window, input, x, y, types_data,
3567 data_device_drop(void *data, struct wl_data_device *data_device)
3569 struct input *input = data;
3570 struct window *window = input->pointer_focus;
3572 if (window->drop_handler)
3573 window->drop_handler(window, input,
3574 input->sx, input->sy, window->user_data);
3578 data_device_selection(void *data,
3579 struct wl_data_device *wl_data_device,
3580 struct wl_data_offer *offer)
3582 struct input *input = data;
3585 if (input->selection_offer)
3586 data_offer_destroy(input->selection_offer);
3589 input->selection_offer = wl_data_offer_get_user_data(offer);
3590 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3593 input->selection_offer = NULL;
3597 static const struct wl_data_device_listener data_device_listener = {
3598 data_device_data_offer,
3603 data_device_selection
3607 input_set_pointer_image_index(struct input *input, int index)
3609 struct wl_buffer *buffer;
3610 struct wl_cursor *cursor;
3611 struct wl_cursor_image *image;
3613 if (!input->pointer)
3616 cursor = input->display->cursors[input->current_cursor];
3620 if (index >= (int) cursor->image_count) {
3621 fprintf(stderr, "cursor index out of range\n");
3625 image = cursor->images[index];
3626 buffer = wl_cursor_image_get_buffer(image);
3630 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3631 input->pointer_surface,
3632 image->hotspot_x, image->hotspot_y);
3633 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3634 wl_surface_damage(input->pointer_surface, 0, 0,
3635 image->width, image->height);
3636 wl_surface_commit(input->pointer_surface);
3639 static const struct wl_callback_listener pointer_surface_listener;
3642 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3645 struct input *input = data;
3646 struct wl_cursor *cursor;
3650 assert(callback == input->cursor_frame_cb);
3651 wl_callback_destroy(callback);
3652 input->cursor_frame_cb = NULL;
3655 if (!input->pointer)
3658 if (input->current_cursor == CURSOR_BLANK) {
3659 wl_pointer_set_cursor(input->pointer,
3660 input->pointer_enter_serial,
3665 if (input->current_cursor == CURSOR_UNSET)
3667 cursor = input->display->cursors[input->current_cursor];
3671 /* FIXME We don't have the current time on the first call so we set
3672 * the animation start to the time of the first frame callback. */
3674 input->cursor_anim_start = 0;
3675 else if (input->cursor_anim_start == 0)
3676 input->cursor_anim_start = time;
3678 if (time == 0 || input->cursor_anim_start == 0)
3681 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3683 if (cursor->image_count > 1) {
3684 input->cursor_frame_cb =
3685 wl_surface_frame(input->pointer_surface);
3686 wl_callback_add_listener(input->cursor_frame_cb,
3687 &pointer_surface_listener, input);
3690 input_set_pointer_image_index(input, i);
3693 static const struct wl_callback_listener pointer_surface_listener = {
3694 pointer_surface_frame_callback
3698 input_set_pointer_image(struct input *input, int pointer)
3702 if (!input->pointer)
3705 if (input->pointer_enter_serial > input->cursor_serial)
3708 if (!force && pointer == input->current_cursor)
3711 input->current_cursor = pointer;
3712 input->cursor_serial = input->pointer_enter_serial;
3713 if (!input->cursor_frame_cb)
3714 pointer_surface_frame_callback(input, NULL, 0);
3716 /* The current frame callback may be stuck if, for instance,
3717 * the set cursor request was processed by the server after
3718 * this client lost the focus. In this case the cursor surface
3719 * might not be mapped and the frame callback wouldn't ever
3720 * complete. Send a set_cursor and attach to try to map the
3721 * cursor surface again so that the callback will finish */
3722 input_set_pointer_image_index(input, 0);
3726 struct wl_data_device *
3727 input_get_data_device(struct input *input)
3729 return input->data_device;
3733 input_set_selection(struct input *input,
3734 struct wl_data_source *source, uint32_t time)
3736 wl_data_device_set_selection(input->data_device, source, time);
3740 input_accept(struct input *input, const char *type)
3742 wl_data_offer_accept(input->drag_offer->offer,
3743 input->pointer_enter_serial, type);
3747 offer_io_func(struct task *task, uint32_t events)
3749 struct data_offer *offer =
3750 container_of(task, struct data_offer, io_task);
3754 len = read(offer->fd, buffer, sizeof buffer);
3755 offer->func(buffer, len,
3756 offer->x, offer->y, offer->user_data);
3760 data_offer_destroy(offer);
3765 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3766 data_func_t func, void *user_data)
3770 if (pipe2(p, O_CLOEXEC) == -1)
3773 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3776 offer->io_task.run = offer_io_func;
3780 offer->user_data = user_data;
3782 display_watch_fd(offer->input->display,
3783 offer->fd, EPOLLIN, &offer->io_task);
3787 input_receive_drag_data(struct input *input, const char *mime_type,
3788 data_func_t func, void *data)
3790 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3791 input->drag_offer->x = input->sx;
3792 input->drag_offer->y = input->sy;
3796 input_receive_drag_data_to_fd(struct input *input,
3797 const char *mime_type, int fd)
3799 if (input->drag_offer)
3800 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3806 input_receive_selection_data(struct input *input, const char *mime_type,
3807 data_func_t func, void *data)
3811 if (input->selection_offer == NULL)
3814 for (p = input->selection_offer->types.data; *p; p++)
3815 if (strcmp(mime_type, *p) == 0)
3821 data_offer_receive_data(input->selection_offer,
3822 mime_type, func, data);
3827 input_receive_selection_data_to_fd(struct input *input,
3828 const char *mime_type, int fd)
3830 if (input->selection_offer)
3831 wl_data_offer_receive(input->selection_offer->offer,
3838 window_move(struct window *window, struct input *input, uint32_t serial)
3840 if (!window->shell_surface)
3843 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3847 window_touch_move(struct window *window, struct input *input, uint32_t serial)
3849 if (!window->shell_surface)
3852 wl_shell_surface_move(window->shell_surface, input->seat,
3853 window->display->serial);
3857 surface_set_synchronized(struct surface *surface)
3859 if (!surface->subsurface)
3862 if (surface->synchronized)
3865 wl_subsurface_set_sync(surface->subsurface);
3866 surface->synchronized = 1;
3870 surface_set_synchronized_default(struct surface *surface)
3872 if (!surface->subsurface)
3875 if (surface->synchronized == surface->synchronized_default)
3878 if (surface->synchronized_default)
3879 wl_subsurface_set_sync(surface->subsurface);
3881 wl_subsurface_set_desync(surface->subsurface);
3883 surface->synchronized = surface->synchronized_default;
3887 surface_resize(struct surface *surface)
3889 struct widget *widget = surface->widget;
3890 struct wl_compositor *compositor = widget->window->display->compositor;
3892 if (surface->input_region) {
3893 wl_region_destroy(surface->input_region);
3894 surface->input_region = NULL;
3897 if (surface->opaque_region)
3898 wl_region_destroy(surface->opaque_region);
3900 surface->opaque_region = wl_compositor_create_region(compositor);
3902 if (widget->resize_handler)
3903 widget->resize_handler(widget,
3904 widget->allocation.width,
3905 widget->allocation.height,
3908 if (surface->subsurface &&
3909 (surface->allocation.x != widget->allocation.x ||
3910 surface->allocation.y != widget->allocation.y)) {
3911 wl_subsurface_set_position(surface->subsurface,
3912 widget->allocation.x,
3913 widget->allocation.y);
3915 if (surface->allocation.width != widget->allocation.width ||
3916 surface->allocation.height != widget->allocation.height) {
3917 window_schedule_redraw(widget->window);
3919 surface->allocation = widget->allocation;
3922 wl_region_add(surface->opaque_region, 0, 0,
3923 widget->allocation.width,
3924 widget->allocation.height);
3928 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3931 * This hack should be removed, when EGL respects
3932 * eglSwapInterval(0).
3934 * If this window has sub-surfaces, especially a free-running
3935 * EGL-widget, we need to post the parent surface once with
3936 * all the old state to guarantee, that the EGL-widget will
3937 * receive its frame callback soon. Otherwise, a forced call
3938 * to eglSwapBuffers may end up blocking, waiting for a frame
3939 * event that will never come, because we will commit the parent
3940 * surface with all new state only after eglSwapBuffers returns.
3942 * This assumes, that:
3943 * 1. When the EGL widget's resize hook is called, it pauses.
3944 * 2. When the EGL widget's redraw hook is called, it forces a
3945 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3946 * In a single threaded application condition 1 is a no-op.
3948 * XXX: This should actually be after the surface_resize() calls,
3949 * but cannot, because then it would commit the incomplete state
3950 * accumulated from the widget resize hooks.
3952 if (window->subsurface_list.next != &window->main_surface->link ||
3953 window->subsurface_list.prev != &window->main_surface->link)
3954 wl_surface_commit(window->main_surface->surface);
3958 idle_resize(struct window *window)
3960 struct surface *surface;
3962 window->resize_needed = 0;
3963 window->redraw_needed = 1;
3965 DBG("from %dx%d to %dx%d\n",
3966 window->main_surface->server_allocation.width,
3967 window->main_surface->server_allocation.height,
3968 window->pending_allocation.width,
3969 window->pending_allocation.height);
3971 hack_prevent_EGL_sub_surface_deadlock(window);
3973 widget_set_allocation(window->main_surface->widget,
3974 window->pending_allocation.x,
3975 window->pending_allocation.y,
3976 window->pending_allocation.width,
3977 window->pending_allocation.height);
3979 surface_resize(window->main_surface);
3981 /* The main surface is in the list, too. Main surface's
3982 * resize_handler is responsible for calling widget_set_allocation()
3983 * on all sub-surface root widgets, so they will be resized
3986 wl_list_for_each(surface, &window->subsurface_list, link) {
3987 if (surface == window->main_surface)
3990 surface_set_synchronized(surface);
3991 surface_resize(surface);
3996 window_schedule_resize(struct window *window, int width, int height)
3998 window->pending_allocation.x = 0;
3999 window->pending_allocation.y = 0;
4000 window->pending_allocation.width = width;
4001 window->pending_allocation.height = height;
4003 if (window->min_allocation.width == 0)
4004 window->min_allocation = window->pending_allocation;
4005 if (window->pending_allocation.width < window->min_allocation.width)
4006 window->pending_allocation.width = window->min_allocation.width;
4007 if (window->pending_allocation.height < window->min_allocation.height)
4008 window->pending_allocation.height = window->min_allocation.height;
4010 window->resize_needed = 1;
4011 window_schedule_redraw(window);
4015 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
4017 window_schedule_resize(widget->window, width, height);
4021 handle_ping(void *data, struct wl_shell_surface *shell_surface,
4024 wl_shell_surface_pong(shell_surface, serial);
4028 handle_configure(void *data, struct wl_shell_surface *shell_surface,
4029 uint32_t edges, int32_t width, int32_t height)
4031 struct window *window = data;
4033 window->resize_edges = edges;
4034 window_schedule_resize(window, width, height);
4038 menu_destroy(struct menu *menu)
4040 widget_destroy(menu->widget);
4041 window_destroy(menu->window);
4046 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4048 struct window *window = data;
4049 struct menu *menu = window->main_surface->widget->user_data;
4051 /* FIXME: Need more context in this event, at least the input
4052 * device. Or just use wl_callback. And this really needs to
4053 * be a window vfunc that the menu can set. And we need the
4056 input_ungrab(menu->input);
4060 static const struct wl_shell_surface_listener shell_surface_listener = {
4067 window_get_allocation(struct window *window,
4068 struct rectangle *allocation)
4070 *allocation = window->main_surface->allocation;
4074 widget_redraw(struct widget *widget)
4076 struct widget *child;
4078 if (widget->redraw_handler)
4079 widget->redraw_handler(widget, widget->user_data);
4080 wl_list_for_each(child, &widget->child_list, link)
4081 widget_redraw(child);
4085 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4087 struct surface *surface = data;
4089 assert(callback == surface->frame_cb);
4090 DBG_OBJ(callback, "done\n");
4091 wl_callback_destroy(callback);
4092 surface->frame_cb = NULL;
4094 surface->last_time = time;
4096 if (surface->redraw_needed || surface->window->redraw_needed) {
4097 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4098 window_schedule_redraw_task(surface->window);
4102 static const struct wl_callback_listener listener = {
4107 surface_redraw(struct surface *surface)
4109 DBG_OBJ(surface->surface, "begin\n");
4111 if (!surface->window->redraw_needed && !surface->redraw_needed)
4114 /* Whole-window redraw forces a redraw even if the previous has
4115 * not yet hit the screen.
4117 if (surface->frame_cb) {
4118 if (!surface->window->redraw_needed)
4121 DBG_OBJ(surface->frame_cb, "cancelled\n");
4122 wl_callback_destroy(surface->frame_cb);
4125 surface->frame_cb = wl_surface_frame(surface->surface);
4126 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4127 DBG_OBJ(surface->frame_cb, "new\n");
4129 surface->redraw_needed = 0;
4130 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4131 widget_redraw(surface->widget);
4132 DBG_OBJ(surface->surface, "done\n");
4136 idle_redraw(struct task *task, uint32_t events)
4138 struct window *window = container_of(task, struct window, redraw_task);
4139 struct surface *surface;
4141 DBG(" --------- \n");
4143 wl_list_init(&window->redraw_task.link);
4144 window->redraw_task_scheduled = 0;
4146 if (window->resize_needed) {
4147 /* throttle resizing to the main surface display */
4148 if (window->main_surface->frame_cb) {
4149 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4153 idle_resize(window);
4156 wl_list_for_each(surface, &window->subsurface_list, link)
4157 surface_redraw(surface);
4159 window->redraw_needed = 0;
4160 window_flush(window);
4162 wl_list_for_each(surface, &window->subsurface_list, link)
4163 surface_set_synchronized_default(surface);
4167 window_schedule_redraw_task(struct window *window)
4169 if (window->configure_requests)
4171 if (!window->redraw_task_scheduled) {
4172 window->redraw_task.run = idle_redraw;
4173 display_defer(window->display, &window->redraw_task);
4174 window->redraw_task_scheduled = 1;
4179 window_schedule_redraw(struct window *window)
4181 struct surface *surface;
4183 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4185 wl_list_for_each(surface, &window->subsurface_list, link)
4186 surface->redraw_needed = 1;
4188 window_schedule_redraw_task(window);
4192 window_is_fullscreen(struct window *window)
4194 return window->type == TYPE_FULLSCREEN;
4198 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4200 struct window *window = data;
4202 wl_callback_destroy(callback);
4203 window->configure_requests--;
4205 if (!window->configure_requests)
4206 window_schedule_redraw(window);
4209 static struct wl_callback_listener configure_request_listener = {
4210 configure_request_completed,
4214 window_defer_redraw_until_configure(struct window* window)
4216 struct wl_callback *callback;
4218 if (window->redraw_task_scheduled) {
4219 wl_list_remove(&window->redraw_task.link);
4220 window->redraw_task_scheduled = 0;
4223 callback = wl_display_sync(window->display->display);
4224 wl_callback_add_listener(callback, &configure_request_listener, window);
4225 window->configure_requests++;
4229 window_set_fullscreen(struct window *window, int fullscreen)
4231 if (!window->display->shell)
4234 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4238 window->saved_type = window->type;
4239 if (window->type == TYPE_TOPLEVEL) {
4240 window->saved_allocation = window->main_surface->allocation;
4242 window->type = TYPE_FULLSCREEN;
4243 wl_shell_surface_set_fullscreen(window->shell_surface,
4244 window->fullscreen_method,
4246 window_defer_redraw_until_configure (window);
4248 if (window->saved_type == TYPE_MAXIMIZED) {
4249 window_set_maximized(window, 1);
4251 window->type = TYPE_TOPLEVEL;
4252 wl_shell_surface_set_toplevel(window->shell_surface);
4253 window_schedule_resize(window,
4254 window->saved_allocation.width,
4255 window->saved_allocation.height);
4262 window_set_fullscreen_method(struct window *window,
4263 enum wl_shell_surface_fullscreen_method method)
4265 window->fullscreen_method = method;
4269 window_is_maximized(struct window *window)
4271 return window->type == TYPE_MAXIMIZED;
4275 window_set_maximized(struct window *window, int maximized)
4277 if (!window->display->shell)
4280 if ((window->type == TYPE_MAXIMIZED) == maximized)
4283 if (window->type == TYPE_TOPLEVEL) {
4284 window->saved_allocation = window->main_surface->allocation;
4285 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4286 window->type = TYPE_MAXIMIZED;
4287 window_defer_redraw_until_configure(window);
4288 } else if (window->type == TYPE_FULLSCREEN) {
4289 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4290 window->type = TYPE_MAXIMIZED;
4291 window_defer_redraw_until_configure(window);
4293 wl_shell_surface_set_toplevel(window->shell_surface);
4294 window->type = TYPE_TOPLEVEL;
4295 window_schedule_resize(window,
4296 window->saved_allocation.width,
4297 window->saved_allocation.height);
4302 window_set_user_data(struct window *window, void *data)
4304 window->user_data = data;
4308 window_get_user_data(struct window *window)
4310 return window->user_data;
4314 window_set_key_handler(struct window *window,
4315 window_key_handler_t handler)
4317 window->key_handler = handler;
4321 window_set_keyboard_focus_handler(struct window *window,
4322 window_keyboard_focus_handler_t handler)
4324 window->keyboard_focus_handler = handler;
4328 window_set_data_handler(struct window *window, window_data_handler_t handler)
4330 window->data_handler = handler;
4334 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4336 window->drop_handler = handler;
4340 window_set_close_handler(struct window *window,
4341 window_close_handler_t handler)
4343 window->close_handler = handler;
4347 window_set_fullscreen_handler(struct window *window,
4348 window_fullscreen_handler_t handler)
4350 window->fullscreen_handler = handler;
4354 window_set_output_handler(struct window *window,
4355 window_output_handler_t handler)
4357 window->output_handler = handler;
4361 window_set_title(struct window *window, const char *title)
4363 free(window->title);
4364 window->title = strdup(title);
4365 if (window->shell_surface)
4366 wl_shell_surface_set_title(window->shell_surface, title);
4370 window_get_title(struct window *window)
4372 return window->title;
4376 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4378 struct text_cursor_position *text_cursor_position =
4379 window->display->text_cursor_position;
4381 if (!text_cursor_position)
4384 text_cursor_position_notify(text_cursor_position,
4385 window->main_surface->surface,
4386 wl_fixed_from_int(x),
4387 wl_fixed_from_int(y));
4391 window_damage(struct window *window, int32_t x, int32_t y,
4392 int32_t width, int32_t height)
4394 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4398 surface_enter(void *data,
4399 struct wl_surface *wl_surface, struct wl_output *wl_output)
4401 struct window *window = data;
4402 struct output *output;
4403 struct output *output_found = NULL;
4404 struct window_output *window_output;
4406 wl_list_for_each(output, &window->display->output_list, link) {
4407 if (output->output == wl_output) {
4408 output_found = output;
4416 window_output = xmalloc(sizeof *window_output);
4417 window_output->output = output_found;
4419 wl_list_insert (&window->window_output_list, &window_output->link);
4421 if (window->output_handler)
4422 window->output_handler(window, output_found, 1,
4427 surface_leave(void *data,
4428 struct wl_surface *wl_surface, struct wl_output *output)
4430 struct window *window = data;
4431 struct window_output *window_output;
4432 struct window_output *window_output_found = NULL;
4434 wl_list_for_each(window_output, &window->window_output_list, link) {
4435 if (window_output->output->output == output) {
4436 window_output_found = window_output;
4441 if (window_output_found) {
4442 wl_list_remove(&window_output_found->link);
4444 if (window->output_handler)
4445 window->output_handler(window, window_output->output,
4446 0, window->user_data);
4448 free(window_output_found);
4452 static const struct wl_surface_listener surface_listener = {
4457 static struct surface *
4458 surface_create(struct window *window)
4460 struct display *display = window->display;
4461 struct surface *surface;
4463 surface = xmalloc(sizeof *surface);
4464 memset(surface, 0, sizeof *surface);
4468 surface->window = window;
4469 surface->surface = wl_compositor_create_surface(display->compositor);
4470 surface->buffer_scale = 1;
4471 wl_surface_add_listener(surface->surface, &surface_listener, window);
4473 wl_list_insert(&window->subsurface_list, &surface->link);
4478 static struct window *
4479 window_create_internal(struct display *display,
4480 struct window *parent, int type)
4482 struct window *window;
4483 struct surface *surface;
4485 window = xzalloc(sizeof *window);
4486 wl_list_init(&window->subsurface_list);
4487 window->display = display;
4488 window->parent = parent;
4490 surface = surface_create(window);
4491 window->main_surface = surface;
4493 if (type != TYPE_CUSTOM && display->shell) {
4494 window->shell_surface =
4495 wl_shell_get_shell_surface(display->shell,
4497 fail_on_null(window->shell_surface);
4500 window->type = type;
4501 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4502 window->configure_requests = 0;
4503 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4505 if (display->argb_device)
4506 #ifdef HAVE_CAIRO_EGL
4507 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4509 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4512 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4514 wl_surface_set_user_data(surface->surface, window);
4515 wl_list_insert(display->window_list.prev, &window->link);
4516 wl_list_init(&window->redraw_task.link);
4518 if (window->shell_surface) {
4519 wl_shell_surface_set_user_data(window->shell_surface, window);
4520 wl_shell_surface_add_listener(window->shell_surface,
4521 &shell_surface_listener, window);
4524 wl_list_init (&window->window_output_list);
4530 window_create(struct display *display)
4532 return window_create_internal(display, NULL, TYPE_NONE);
4536 window_create_custom(struct display *display)
4538 return window_create_internal(display, NULL, TYPE_CUSTOM);
4542 window_create_transient(struct display *display, struct window *parent,
4543 int32_t x, int32_t y, uint32_t flags)
4545 struct window *window;
4547 window = window_create_internal(parent->display,
4548 parent, TYPE_TRANSIENT);
4554 wl_shell_surface_set_transient(
4555 window->shell_surface,
4556 window->parent->main_surface->surface,
4557 window->x, window->y, flags);
4563 menu_set_item(struct menu *menu, int sy)
4567 next = (sy - 8) / 20;
4568 if (menu->current != next) {
4569 menu->current = next;
4570 widget_schedule_redraw(menu->widget);
4575 menu_motion_handler(struct widget *widget,
4576 struct input *input, uint32_t time,
4577 float x, float y, void *data)
4579 struct menu *menu = data;
4581 if (widget == menu->widget)
4582 menu_set_item(data, y);
4584 return CURSOR_LEFT_PTR;
4588 menu_enter_handler(struct widget *widget,
4589 struct input *input, float x, float y, void *data)
4591 struct menu *menu = data;
4593 if (widget == menu->widget)
4594 menu_set_item(data, y);
4596 return CURSOR_LEFT_PTR;
4600 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4602 struct menu *menu = data;
4604 if (widget == menu->widget)
4605 menu_set_item(data, -200);
4609 menu_button_handler(struct widget *widget,
4610 struct input *input, uint32_t time,
4611 uint32_t button, enum wl_pointer_button_state state,
4615 struct menu *menu = data;
4617 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4618 (menu->release_count > 0 || time - menu->time > 500)) {
4619 /* Either relase after press-drag-release or
4620 * click-motion-click. */
4621 menu->func(menu->window->parent,
4622 menu->current, menu->window->parent->user_data);
4623 input_ungrab(input);
4625 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4626 menu->release_count++;
4631 menu_redraw_handler(struct widget *widget, void *data)
4634 const int32_t r = 3, margin = 3;
4635 struct menu *menu = data;
4636 int32_t width, height, i;
4638 cr = widget_cairo_create(widget);
4639 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4640 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4643 width = widget->allocation.width;
4644 height = widget->allocation.height;
4645 rounded_rect(cr, 0, 0, width, height, r);
4647 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
4648 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4651 for (i = 0; i < menu->count; i++) {
4652 if (i == menu->current) {
4653 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4654 cairo_rectangle(cr, margin, i * 20 + margin,
4655 width - 2 * margin, 20);
4657 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4658 cairo_move_to(cr, 10, i * 20 + 16);
4659 cairo_show_text(cr, menu->entries[i]);
4661 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4662 cairo_move_to(cr, 10, i * 20 + 16);
4663 cairo_show_text(cr, menu->entries[i]);
4671 window_show_menu(struct display *display,
4672 struct input *input, uint32_t time, struct window *parent,
4673 int32_t x, int32_t y,
4674 menu_func_t func, const char **entries, int count)
4676 struct window *window;
4678 const int32_t margin = 3;
4680 menu = malloc(sizeof *menu);
4684 window = window_create_internal(parent->display, parent, TYPE_MENU);
4690 menu->window = window;
4691 menu->widget = window_add_widget(menu->window, menu);
4692 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4693 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4694 menu->entries = entries;
4695 menu->count = count;
4696 menu->release_count = 0;
4700 menu->input = input;
4701 window->type = TYPE_MENU;
4705 input_ungrab(input);
4706 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4707 display_get_serial(window->display),
4708 window->parent->main_surface->surface,
4709 window->x, window->y, 0);
4711 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4712 widget_set_enter_handler(menu->widget, menu_enter_handler);
4713 widget_set_leave_handler(menu->widget, menu_leave_handler);
4714 widget_set_motion_handler(menu->widget, menu_motion_handler);
4715 widget_set_button_handler(menu->widget, menu_button_handler);
4717 input_grab(input, menu->widget, 0);
4718 window_schedule_resize(window, 200, count * 20 + margin * 2);
4722 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4724 window->main_surface->buffer_type = type;
4728 window_set_preferred_format(struct window *window,
4729 enum preferred_format format)
4731 window->preferred_format = format;
4735 window_add_subsurface(struct window *window, void *data,
4736 enum subsurface_mode default_mode)
4738 struct widget *widget;
4739 struct surface *surface;
4740 struct wl_surface *parent;
4741 struct wl_subcompositor *subcompo = window->display->subcompositor;
4743 surface = surface_create(window);
4744 widget = widget_create(window, surface, data);
4745 wl_list_init(&widget->link);
4746 surface->widget = widget;
4748 parent = window->main_surface->surface;
4749 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4752 surface->synchronized = 1;
4754 switch (default_mode) {
4755 case SUBSURFACE_SYNCHRONIZED:
4756 surface->synchronized_default = 1;
4758 case SUBSURFACE_DESYNCHRONIZED:
4759 surface->synchronized_default = 0;
4762 assert(!"bad enum subsurface_mode");
4769 display_handle_geometry(void *data,
4770 struct wl_output *wl_output,
4773 int physical_height,
4779 struct output *output = data;
4781 output->allocation.x = x;
4782 output->allocation.y = y;
4783 output->transform = transform;
4787 display_handle_done(void *data,
4788 struct wl_output *wl_output)
4793 display_handle_scale(void *data,
4794 struct wl_output *wl_output,
4797 struct output *output = data;
4799 output->scale = scale;
4803 display_handle_mode(void *data,
4804 struct wl_output *wl_output,
4810 struct output *output = data;
4811 struct display *display = output->display;
4813 if (flags & WL_OUTPUT_MODE_CURRENT) {
4814 output->allocation.width = width;
4815 output->allocation.height = height;
4816 if (display->output_configure_handler)
4817 (*display->output_configure_handler)(
4818 output, display->user_data);
4822 static const struct wl_output_listener output_listener = {
4823 display_handle_geometry,
4824 display_handle_mode,
4825 display_handle_done,
4826 display_handle_scale
4830 display_add_output(struct display *d, uint32_t id)
4832 struct output *output;
4834 output = xzalloc(sizeof *output);
4835 output->display = d;
4838 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4839 wl_list_insert(d->output_list.prev, &output->link);
4841 wl_output_add_listener(output->output, &output_listener, output);
4845 output_destroy(struct output *output)
4847 if (output->destroy_handler)
4848 (*output->destroy_handler)(output, output->user_data);
4850 wl_output_destroy(output->output);
4851 wl_list_remove(&output->link);
4856 display_set_global_handler(struct display *display,
4857 display_global_handler_t handler)
4859 struct global *global;
4861 display->global_handler = handler;
4865 wl_list_for_each(global, &display->global_list, link)
4866 display->global_handler(display,
4867 global->name, global->interface,
4868 global->version, display->user_data);
4872 display_set_output_configure_handler(struct display *display,
4873 display_output_handler_t handler)
4875 struct output *output;
4877 display->output_configure_handler = handler;
4881 wl_list_for_each(output, &display->output_list, link) {
4882 if (output->allocation.width == 0 &&
4883 output->allocation.height == 0)
4886 (*display->output_configure_handler)(output,
4887 display->user_data);
4892 output_set_user_data(struct output *output, void *data)
4894 output->user_data = data;
4898 output_get_user_data(struct output *output)
4900 return output->user_data;
4904 output_set_destroy_handler(struct output *output,
4905 display_output_handler_t handler)
4907 output->destroy_handler = handler;
4908 /* FIXME: implement this, once we have way to remove outputs */
4912 output_get_allocation(struct output *output, struct rectangle *base)
4914 struct rectangle allocation = output->allocation;
4916 switch (output->transform) {
4917 case WL_OUTPUT_TRANSFORM_90:
4918 case WL_OUTPUT_TRANSFORM_270:
4919 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4920 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4921 /* Swap width and height */
4922 allocation.width = output->allocation.height;
4923 allocation.height = output->allocation.width;
4931 output_get_wl_output(struct output *output)
4933 return output->output;
4936 enum wl_output_transform
4937 output_get_transform(struct output *output)
4939 return output->transform;
4943 output_get_scale(struct output *output)
4945 return output->scale;
4949 fini_xkb(struct input *input)
4951 xkb_state_unref(input->xkb.state);
4952 xkb_map_unref(input->xkb.keymap);
4955 #define MAX(a,b) ((a) > (b) ? a : b)
4958 display_add_input(struct display *d, uint32_t id)
4960 struct input *input;
4962 input = xzalloc(sizeof *input);
4964 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4965 MAX(d->seat_version, 3));
4966 input->touch_focus = NULL;
4967 input->pointer_focus = NULL;
4968 input->keyboard_focus = NULL;
4969 wl_list_init(&input->touch_point_list);
4970 wl_list_insert(d->input_list.prev, &input->link);
4972 wl_seat_add_listener(input->seat, &seat_listener, input);
4973 wl_seat_set_user_data(input->seat, input);
4975 input->data_device =
4976 wl_data_device_manager_get_data_device(d->data_device_manager,
4978 wl_data_device_add_listener(input->data_device, &data_device_listener,
4981 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4983 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4984 TFD_CLOEXEC | TFD_NONBLOCK);
4985 input->repeat_task.run = keyboard_repeat_func;
4986 display_watch_fd(d, input->repeat_timer_fd,
4987 EPOLLIN, &input->repeat_task);
4991 input_destroy(struct input *input)
4993 input_remove_keyboard_focus(input);
4994 input_remove_pointer_focus(input);
4996 if (input->drag_offer)
4997 data_offer_destroy(input->drag_offer);
4999 if (input->selection_offer)
5000 data_offer_destroy(input->selection_offer);
5002 wl_data_device_destroy(input->data_device);
5004 if (input->display->seat_version >= 3) {
5006 wl_pointer_release(input->pointer);
5007 if (input->keyboard)
5008 wl_keyboard_release(input->keyboard);
5013 wl_surface_destroy(input->pointer_surface);
5015 wl_list_remove(&input->link);
5016 wl_seat_destroy(input->seat);
5017 close(input->repeat_timer_fd);
5022 init_workspace_manager(struct display *d, uint32_t id)
5024 d->workspace_manager =
5025 wl_registry_bind(d->registry, id,
5026 &workspace_manager_interface, 1);
5027 if (d->workspace_manager != NULL)
5028 workspace_manager_add_listener(d->workspace_manager,
5029 &workspace_manager_listener,
5034 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5036 struct display *d = data;
5038 if (format == WL_SHM_FORMAT_RGB565)
5042 struct wl_shm_listener shm_listener = {
5047 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5048 const char *interface, uint32_t version)
5050 struct display *d = data;
5051 struct global *global;
5053 global = xmalloc(sizeof *global);
5055 global->interface = strdup(interface);
5056 global->version = version;
5057 wl_list_insert(d->global_list.prev, &global->link);
5059 if (strcmp(interface, "wl_compositor") == 0) {
5060 d->compositor = wl_registry_bind(registry, id,
5061 &wl_compositor_interface, 3);
5062 } else if (strcmp(interface, "wl_output") == 0) {
5063 display_add_output(d, id);
5064 } else if (strcmp(interface, "wl_seat") == 0) {
5065 d->seat_version = version;
5066 display_add_input(d, id);
5067 } else if (strcmp(interface, "wl_shell") == 0) {
5068 d->shell = wl_registry_bind(registry,
5069 id, &wl_shell_interface, 1);
5070 } else if (strcmp(interface, "wl_shm") == 0) {
5071 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5072 wl_shm_add_listener(d->shm, &shm_listener, d);
5073 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5074 d->data_device_manager =
5075 wl_registry_bind(registry, id,
5076 &wl_data_device_manager_interface, 1);
5077 } else if (strcmp(interface, "text_cursor_position") == 0) {
5078 d->text_cursor_position =
5079 wl_registry_bind(registry, id,
5080 &text_cursor_position_interface, 1);
5081 } else if (strcmp(interface, "workspace_manager") == 0) {
5082 init_workspace_manager(d, id);
5083 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5085 wl_registry_bind(registry, id,
5086 &wl_subcompositor_interface, 1);
5089 if (d->global_handler)
5090 d->global_handler(d, id, interface, version, d->user_data);
5094 registry_handle_global_remove(void *data, struct wl_registry *registry,
5097 struct display *d = data;
5098 struct global *global;
5101 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5102 if (global->name != name)
5105 /* XXX: Should destroy bound globals, and call
5106 * the counterpart of display::global_handler
5108 wl_list_remove(&global->link);
5109 free(global->interface);
5115 display_bind(struct display *display, uint32_t name,
5116 const struct wl_interface *interface, uint32_t version)
5118 return wl_registry_bind(display->registry, name, interface, version);
5121 static const struct wl_registry_listener registry_listener = {
5122 registry_handle_global,
5123 registry_handle_global_remove
5126 #ifdef HAVE_CAIRO_EGL
5128 init_egl(struct display *d)
5130 EGLint major, minor;
5133 #ifdef USE_CAIRO_GLESV2
5134 # define GL_BIT EGL_OPENGL_ES2_BIT
5136 # define GL_BIT EGL_OPENGL_BIT
5139 static const EGLint argb_cfg_attribs[] = {
5140 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5146 EGL_RENDERABLE_TYPE, GL_BIT,
5150 #ifdef USE_CAIRO_GLESV2
5151 static const EGLint context_attribs[] = {
5152 EGL_CONTEXT_CLIENT_VERSION, 2,
5155 EGLint api = EGL_OPENGL_ES_API;
5157 EGLint *context_attribs = NULL;
5158 EGLint api = EGL_OPENGL_API;
5161 d->dpy = eglGetDisplay(d->display);
5162 if (!eglInitialize(d->dpy, &major, &minor)) {
5163 fprintf(stderr, "failed to initialize EGL\n");
5167 if (!eglBindAPI(api)) {
5168 fprintf(stderr, "failed to bind EGL client API\n");
5172 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5173 &d->argb_config, 1, &n) || n != 1) {
5174 fprintf(stderr, "failed to choose argb EGL config\n");
5178 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5179 EGL_NO_CONTEXT, context_attribs);
5180 if (d->argb_ctx == NULL) {
5181 fprintf(stderr, "failed to create EGL context\n");
5185 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5186 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5187 fprintf(stderr, "failed to get cairo EGL argb device\n");
5195 fini_egl(struct display *display)
5197 cairo_device_destroy(display->argb_device);
5199 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5202 eglTerminate(display->dpy);
5208 init_dummy_surface(struct display *display)
5213 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5215 display->dummy_surface =
5216 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5218 display->dummy_surface_data = data;
5222 handle_display_data(struct task *task, uint32_t events)
5224 struct display *display =
5225 container_of(task, struct display, display_task);
5226 struct epoll_event ep;
5229 display->display_fd_events = events;
5231 if (events & EPOLLERR || events & EPOLLHUP) {
5232 display_exit(display);
5236 if (events & EPOLLIN) {
5237 ret = wl_display_dispatch(display->display);
5239 display_exit(display);
5244 if (events & EPOLLOUT) {
5245 ret = wl_display_flush(display->display);
5247 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5248 ep.data.ptr = &display->display_task;
5249 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5250 display->display_fd, &ep);
5251 } else if (ret == -1 && errno != EAGAIN) {
5252 display_exit(display);
5259 log_handler(const char *format, va_list args)
5261 vfprintf(stderr, format, args);
5265 display_create(int *argc, char *argv[])
5269 wl_log_set_handler_client(log_handler);
5271 d = zalloc(sizeof *d);
5275 d->display = wl_display_connect(NULL);
5276 if (d->display == NULL) {
5277 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5282 d->xkb_context = xkb_context_new(0);
5283 if (d->xkb_context == NULL) {
5284 fprintf(stderr, "Failed to create XKB context\n");
5289 d->epoll_fd = os_epoll_create_cloexec();
5290 d->display_fd = wl_display_get_fd(d->display);
5291 d->display_task.run = handle_display_data;
5292 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5295 wl_list_init(&d->deferred_list);
5296 wl_list_init(&d->input_list);
5297 wl_list_init(&d->output_list);
5298 wl_list_init(&d->global_list);
5301 d->workspace_count = 1;
5303 d->registry = wl_display_get_registry(d->display);
5304 wl_registry_add_listener(d->registry, ®istry_listener, d);
5306 if (wl_display_dispatch(d->display) < 0) {
5307 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5311 #ifdef HAVE_CAIRO_EGL
5312 if (init_egl(d) < 0)
5313 fprintf(stderr, "EGL does not seem to work, "
5314 "falling back to software rendering and wl_shm.\n");
5319 d->theme = theme_create();
5321 wl_list_init(&d->window_list);
5323 init_dummy_surface(d);
5329 display_destroy_outputs(struct display *display)
5332 struct output *output;
5334 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5335 output_destroy(output);
5339 display_destroy_inputs(struct display *display)
5342 struct input *input;
5344 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5345 input_destroy(input);
5349 display_destroy(struct display *display)
5351 if (!wl_list_empty(&display->window_list))
5352 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5353 wl_list_length(&display->window_list));
5355 if (!wl_list_empty(&display->deferred_list))
5356 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5358 cairo_surface_destroy(display->dummy_surface);
5359 free(display->dummy_surface_data);
5361 display_destroy_outputs(display);
5362 display_destroy_inputs(display);
5364 xkb_context_unref(display->xkb_context);
5366 theme_destroy(display->theme);
5367 destroy_cursors(display);
5369 #ifdef HAVE_CAIRO_EGL
5370 if (display->argb_device)
5374 if (display->subcompositor)
5375 wl_subcompositor_destroy(display->subcompositor);
5378 wl_shell_destroy(display->shell);
5381 wl_shm_destroy(display->shm);
5383 if (display->data_device_manager)
5384 wl_data_device_manager_destroy(display->data_device_manager);
5386 wl_compositor_destroy(display->compositor);
5387 wl_registry_destroy(display->registry);
5389 close(display->epoll_fd);
5391 if (!(display->display_fd_events & EPOLLERR) &&
5392 !(display->display_fd_events & EPOLLHUP))
5393 wl_display_flush(display->display);
5395 wl_display_disconnect(display->display);
5400 display_set_user_data(struct display *display, void *data)
5402 display->user_data = data;
5406 display_get_user_data(struct display *display)
5408 return display->user_data;
5412 display_get_display(struct display *display)
5414 return display->display;
5418 display_has_subcompositor(struct display *display)
5420 if (display->subcompositor)
5423 wl_display_roundtrip(display->display);
5425 return display->subcompositor != NULL;
5429 display_get_cairo_device(struct display *display)
5431 return display->argb_device;
5435 display_get_output(struct display *display)
5437 return container_of(display->output_list.next, struct output, link);
5440 struct wl_compositor *
5441 display_get_compositor(struct display *display)
5443 return display->compositor;
5447 display_get_serial(struct display *display)
5449 return display->serial;
5453 display_get_egl_display(struct display *d)
5458 struct wl_data_source *
5459 display_create_data_source(struct display *display)
5461 return wl_data_device_manager_create_data_source(display->data_device_manager);
5465 display_get_argb_egl_config(struct display *d)
5467 return d->argb_config;
5471 display_get_shell(struct display *display)
5473 return display->shell;
5477 display_acquire_window_surface(struct display *display,
5478 struct window *window,
5481 struct surface *surface = window->main_surface;
5483 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5486 widget_get_cairo_surface(window->main_surface->widget);
5487 return surface->toysurface->acquire(surface->toysurface, ctx);
5491 display_release_window_surface(struct display *display,
5492 struct window *window)
5494 struct surface *surface = window->main_surface;
5496 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5499 surface->toysurface->release(surface->toysurface);
5503 display_defer(struct display *display, struct task *task)
5505 wl_list_insert(&display->deferred_list, &task->link);
5509 display_watch_fd(struct display *display,
5510 int fd, uint32_t events, struct task *task)
5512 struct epoll_event ep;
5516 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5520 display_unwatch_fd(struct display *display, int fd)
5522 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5526 display_run(struct display *display)
5529 struct epoll_event ep[16];
5532 display->running = 1;
5534 while (!wl_list_empty(&display->deferred_list)) {
5535 task = container_of(display->deferred_list.prev,
5537 wl_list_remove(&task->link);
5541 wl_display_dispatch_pending(display->display);
5543 if (!display->running)
5546 ret = wl_display_flush(display->display);
5547 if (ret < 0 && errno == EAGAIN) {
5549 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5550 ep[0].data.ptr = &display->display_task;
5552 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5553 display->display_fd, &ep[0]);
5554 } else if (ret < 0) {
5558 count = epoll_wait(display->epoll_fd,
5559 ep, ARRAY_LENGTH(ep), -1);
5560 for (i = 0; i < count; i++) {
5561 task = ep[i].data.ptr;
5562 task->run(task, ep[i].events);
5568 display_exit(struct display *display)
5570 display->running = 0;
5574 keysym_modifiers_add(struct wl_array *modifiers_map,
5577 size_t len = strlen(name) + 1;
5580 p = wl_array_add(modifiers_map, len);
5585 strncpy(p, name, len);
5588 static xkb_mod_index_t
5589 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5592 xkb_mod_index_t index = 0;
5593 char *p = modifiers_map->data;
5595 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5596 if (strcmp(p, name) == 0)
5603 return XKB_MOD_INVALID;
5607 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5610 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5612 if (index == XKB_MOD_INVALID)
5613 return XKB_MOD_INVALID;
5619 fail_on_null(void *p)
5622 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5632 return fail_on_null(malloc(s));
5638 return fail_on_null(zalloc(s));
5642 xstrdup(const char *s)
5644 return fail_on_null(strdup(s));