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;
145 struct window_output {
146 struct output *output;
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new buffer size.
156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
158 * Returns the Cairo surface to draw to.
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
161 int32_t width, int32_t height, uint32_t flags,
162 enum wl_output_transform buffer_transform, int32_t buffer_scale);
165 * Post the surface to the server, returning the server allocation
166 * rectangle. The Cairo surface from prepare() must be destroyed
167 * after calling this.
169 void (*swap)(struct toysurface *base,
170 enum wl_output_transform buffer_transform, int32_t buffer_scale,
171 struct rectangle *server_allocation);
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
180 * Release the toysurface from the EGL context, returning control
183 void (*release)(struct toysurface *base);
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
189 void (*destroy)(struct toysurface *base);
193 struct window *window;
195 struct wl_surface *surface;
196 struct wl_subsurface *subsurface;
198 int synchronized_default;
199 struct toysurface *toysurface;
200 struct widget *widget;
202 struct wl_callback *frame_cb;
205 struct rectangle allocation;
206 struct rectangle server_allocation;
208 struct wl_region *input_region;
209 struct wl_region *opaque_region;
211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
213 int32_t buffer_scale;
215 cairo_surface_t *cairo_surface;
221 struct display *display;
222 struct window *parent;
223 struct wl_list window_output_list;
225 struct rectangle saved_allocation;
226 struct rectangle min_allocation;
227 struct rectangle pending_allocation;
231 int redraw_task_scheduled;
232 struct task redraw_task;
239 int fullscreen_method;
240 int configure_requests;
242 enum preferred_format preferred_format;
244 window_key_handler_t key_handler;
245 window_keyboard_focus_handler_t keyboard_focus_handler;
246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
248 window_close_handler_t close_handler;
249 window_fullscreen_handler_t fullscreen_handler;
250 window_output_handler_t output_handler;
252 struct surface *main_surface;
253 struct wl_shell_surface *shell_surface;
257 /* struct surface::link, contains also main_surface */
258 struct wl_list subsurface_list;
265 struct window *window;
266 struct surface *surface;
267 struct tooltip *tooltip;
268 struct wl_list child_list;
270 struct rectangle allocation;
271 widget_resize_handler_t resize_handler;
272 widget_redraw_handler_t redraw_handler;
273 widget_enter_handler_t enter_handler;
274 widget_leave_handler_t leave_handler;
275 widget_motion_handler_t motion_handler;
276 widget_button_handler_t button_handler;
277 widget_touch_down_handler_t touch_down_handler;
278 widget_touch_up_handler_t touch_up_handler;
279 widget_touch_motion_handler_t touch_motion_handler;
280 widget_touch_frame_handler_t touch_frame_handler;
281 widget_touch_cancel_handler_t touch_cancel_handler;
282 widget_axis_handler_t axis_handler;
291 struct widget *widget;
296 struct display *display;
297 struct wl_seat *seat;
298 struct wl_pointer *pointer;
299 struct wl_keyboard *keyboard;
300 struct wl_touch *touch;
301 struct wl_list touch_point_list;
302 struct window *pointer_focus;
303 struct window *keyboard_focus;
304 struct window *touch_focus;
306 uint32_t cursor_anim_start;
307 struct wl_callback *cursor_frame_cb;
308 struct wl_surface *pointer_surface;
310 uint32_t pointer_enter_serial;
311 uint32_t cursor_serial;
315 struct widget *focus_widget;
317 uint32_t grab_button;
319 struct wl_data_device *data_device;
320 struct data_offer *drag_offer;
321 struct data_offer *selection_offer;
324 struct xkb_keymap *keymap;
325 struct xkb_state *state;
326 xkb_mod_mask_t control_mask;
327 xkb_mod_mask_t alt_mask;
328 xkb_mod_mask_t shift_mask;
331 struct task repeat_task;
335 uint32_t repeat_time;
339 struct display *display;
340 struct wl_output *output;
341 struct rectangle allocation;
346 display_output_handler_t destroy_handler;
350 enum frame_button_action {
351 FRAME_BUTTON_NULL = 0,
352 FRAME_BUTTON_ICON = 1,
353 FRAME_BUTTON_CLOSE = 2,
354 FRAME_BUTTON_MINIMIZE = 3,
355 FRAME_BUTTON_MAXIMIZE = 4,
358 enum frame_button_pointer {
359 FRAME_BUTTON_DEFAULT = 0,
360 FRAME_BUTTON_OVER = 1,
361 FRAME_BUTTON_ACTIVE = 2,
364 enum frame_button_align {
365 FRAME_BUTTON_RIGHT = 0,
366 FRAME_BUTTON_LEFT = 1,
369 enum frame_button_decoration {
370 FRAME_BUTTON_NONE = 0,
371 FRAME_BUTTON_FANCY = 1,
374 struct frame_button {
375 struct widget *widget;
377 cairo_surface_t *icon;
378 enum frame_button_action type;
379 enum frame_button_pointer state;
380 struct wl_list link; /* buttons_list */
381 enum frame_button_align align;
382 enum frame_button_decoration decoration;
386 struct widget *widget;
387 struct widget *child;
388 struct wl_list buttons_list;
392 struct window *window;
393 struct widget *widget;
395 const char **entries;
404 struct widget *parent;
405 struct window *window;
406 struct widget *widget;
408 struct task tooltip_task;
414 struct wl_shm_pool *pool;
421 CURSOR_DEFAULT = 100,
425 enum window_location {
427 WINDOW_RESIZING_TOP = 1,
428 WINDOW_RESIZING_BOTTOM = 2,
429 WINDOW_RESIZING_LEFT = 4,
430 WINDOW_RESIZING_TOP_LEFT = 5,
431 WINDOW_RESIZING_BOTTOM_LEFT = 6,
432 WINDOW_RESIZING_RIGHT = 8,
433 WINDOW_RESIZING_TOP_RIGHT = 9,
434 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
435 WINDOW_RESIZING_MASK = 15,
436 WINDOW_EXTERIOR = 16,
437 WINDOW_TITLEBAR = 17,
438 WINDOW_CLIENT_AREA = 18,
441 static const cairo_user_data_key_t shm_surface_data_key;
448 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
449 __attribute__ ((format (printf, 4, 5)));
452 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
457 gettimeofday(&tv, NULL);
458 fprintf(stderr, "%8ld.%03ld ",
459 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
462 fprintf(stderr, "%s@%d ",
463 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
465 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
466 fprintf(stderr, "%s ", func);
469 vfprintf(stderr, fmt, ap);
473 #define DBG(fmt, ...) \
474 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
476 #define DBG_OBJ(obj, fmt, ...) \
477 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
481 #define DBG(...) do {} while (0)
482 #define DBG_OBJ(...) do {} while (0)
487 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
491 switch (buffer_transform) {
492 case WL_OUTPUT_TRANSFORM_90:
493 case WL_OUTPUT_TRANSFORM_270:
494 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
495 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 *width *= buffer_scale;
505 *height *= buffer_scale;
509 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
513 switch (buffer_transform) {
514 case WL_OUTPUT_TRANSFORM_90:
515 case WL_OUTPUT_TRANSFORM_270:
516 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
517 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
526 *width /= buffer_scale;
527 *height /= buffer_scale;
530 #ifdef HAVE_CAIRO_EGL
532 struct egl_window_surface {
533 struct toysurface base;
534 cairo_surface_t *cairo_surface;
535 struct display *display;
536 struct wl_surface *surface;
537 struct wl_egl_window *egl_window;
538 EGLSurface egl_surface;
541 static struct egl_window_surface *
542 to_egl_window_surface(struct toysurface *base)
544 return container_of(base, struct egl_window_surface, base);
547 static cairo_surface_t *
548 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
549 int32_t width, int32_t height, uint32_t flags,
550 enum wl_output_transform buffer_transform, int32_t buffer_scale)
552 struct egl_window_surface *surface = to_egl_window_surface(base);
554 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
556 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
557 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
559 return cairo_surface_reference(surface->cairo_surface);
563 egl_window_surface_swap(struct toysurface *base,
564 enum wl_output_transform buffer_transform, int32_t buffer_scale,
565 struct rectangle *server_allocation)
567 struct egl_window_surface *surface = to_egl_window_surface(base);
569 cairo_gl_surface_swapbuffers(surface->cairo_surface);
570 wl_egl_window_get_attached_size(surface->egl_window,
571 &server_allocation->width,
572 &server_allocation->height);
574 buffer_to_surface_size (buffer_transform, buffer_scale,
575 &server_allocation->width,
576 &server_allocation->height);
580 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
582 struct egl_window_surface *surface = to_egl_window_surface(base);
583 cairo_device_t *device;
585 device = cairo_surface_get_device(surface->cairo_surface);
590 if (device == surface->display->argb_device)
591 ctx = surface->display->argb_ctx;
596 cairo_device_flush(device);
597 cairo_device_acquire(device);
598 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
599 surface->egl_surface, ctx))
600 fprintf(stderr, "failed to make surface current\n");
606 egl_window_surface_release(struct toysurface *base)
608 struct egl_window_surface *surface = to_egl_window_surface(base);
609 cairo_device_t *device;
611 device = cairo_surface_get_device(surface->cairo_surface);
615 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
616 surface->display->argb_ctx))
617 fprintf(stderr, "failed to make context current\n");
619 cairo_device_release(device);
623 egl_window_surface_destroy(struct toysurface *base)
625 struct egl_window_surface *surface = to_egl_window_surface(base);
626 struct display *d = surface->display;
628 cairo_surface_destroy(surface->cairo_surface);
629 eglDestroySurface(d->dpy, surface->egl_surface);
630 wl_egl_window_destroy(surface->egl_window);
631 surface->surface = NULL;
636 static struct toysurface *
637 egl_window_surface_create(struct display *display,
638 struct wl_surface *wl_surface,
640 struct rectangle *rectangle)
642 struct egl_window_surface *surface;
644 if (display->dpy == EGL_NO_DISPLAY)
647 surface = calloc(1, sizeof *surface);
651 surface->base.prepare = egl_window_surface_prepare;
652 surface->base.swap = egl_window_surface_swap;
653 surface->base.acquire = egl_window_surface_acquire;
654 surface->base.release = egl_window_surface_release;
655 surface->base.destroy = egl_window_surface_destroy;
657 surface->display = display;
658 surface->surface = wl_surface;
660 surface->egl_window = wl_egl_window_create(surface->surface,
664 surface->egl_surface = eglCreateWindowSurface(display->dpy,
665 display->argb_config,
669 surface->cairo_surface =
670 cairo_gl_surface_create_for_egl(display->argb_device,
671 surface->egl_surface,
675 return &surface->base;
680 static struct toysurface *
681 egl_window_surface_create(struct display *display,
682 struct wl_surface *wl_surface,
684 struct rectangle *rectangle)
691 struct shm_surface_data {
692 struct wl_buffer *buffer;
693 struct shm_pool *pool;
697 display_get_buffer_for_surface(struct display *display,
698 cairo_surface_t *surface)
700 struct shm_surface_data *data;
702 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
708 shm_pool_destroy(struct shm_pool *pool);
711 shm_surface_data_destroy(void *p)
713 struct shm_surface_data *data = p;
715 wl_buffer_destroy(data->buffer);
717 shm_pool_destroy(data->pool);
722 static struct wl_shm_pool *
723 make_shm_pool(struct display *display, int size, void **data)
725 struct wl_shm_pool *pool;
728 fd = os_create_anonymous_file(size);
730 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
735 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
736 if (*data == MAP_FAILED) {
737 fprintf(stderr, "mmap failed: %m\n");
742 pool = wl_shm_create_pool(display->shm, fd, size);
749 static struct shm_pool *
750 shm_pool_create(struct display *display, size_t size)
752 struct shm_pool *pool = malloc(sizeof *pool);
757 pool->pool = make_shm_pool(display, size, &pool->data);
770 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
772 if (pool->used + size > pool->size)
775 *offset = pool->used;
778 return (char *) pool->data + *offset;
781 /* destroy the pool. this does not unmap the memory though */
783 shm_pool_destroy(struct shm_pool *pool)
785 munmap(pool->data, pool->size);
786 wl_shm_pool_destroy(pool->pool);
790 /* Start allocating from the beginning of the pool again */
792 shm_pool_reset(struct shm_pool *pool)
798 data_length_for_shm_surface(struct rectangle *rect)
802 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
804 return stride * rect->height;
807 static cairo_surface_t *
808 display_create_shm_surface_from_pool(struct display *display,
809 struct rectangle *rectangle,
810 uint32_t flags, struct shm_pool *pool)
812 struct shm_surface_data *data;
814 cairo_surface_t *surface;
815 cairo_format_t cairo_format;
816 int stride, length, offset;
819 data = malloc(sizeof *data);
823 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
824 cairo_format = CAIRO_FORMAT_RGB16_565;
826 cairo_format = CAIRO_FORMAT_ARGB32;
828 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
829 length = stride * rectangle->height;
831 map = shm_pool_allocate(pool, length, &offset);
838 surface = cairo_image_surface_create_for_data (map,
844 cairo_surface_set_user_data(surface, &shm_surface_data_key,
845 data, shm_surface_data_destroy);
847 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
848 format = WL_SHM_FORMAT_RGB565;
850 if (flags & SURFACE_OPAQUE)
851 format = WL_SHM_FORMAT_XRGB8888;
853 format = WL_SHM_FORMAT_ARGB8888;
856 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
864 static cairo_surface_t *
865 display_create_shm_surface(struct display *display,
866 struct rectangle *rectangle, uint32_t flags,
867 struct shm_pool *alternate_pool,
868 struct shm_surface_data **data_ret)
870 struct shm_surface_data *data;
871 struct shm_pool *pool;
872 cairo_surface_t *surface;
874 if (alternate_pool) {
875 shm_pool_reset(alternate_pool);
876 surface = display_create_shm_surface_from_pool(display,
881 data = cairo_surface_get_user_data(surface,
882 &shm_surface_data_key);
887 pool = shm_pool_create(display,
888 data_length_for_shm_surface(rectangle));
893 display_create_shm_surface_from_pool(display, rectangle,
897 shm_pool_destroy(pool);
901 /* make sure we destroy the pool when the surface is destroyed */
902 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
913 check_size(struct rectangle *rect)
915 if (rect->width && rect->height)
918 fprintf(stderr, "tried to create surface of "
919 "width: %d, height: %d\n", rect->width, rect->height);
924 display_create_surface(struct display *display,
925 struct wl_surface *surface,
926 struct rectangle *rectangle,
929 if (check_size(rectangle) < 0)
932 assert(flags & SURFACE_SHM);
933 return display_create_shm_surface(display, rectangle, flags,
937 struct shm_surface_leaf {
938 cairo_surface_t *cairo_surface;
939 /* 'data' is automatically destroyed, when 'cairo_surface' is */
940 struct shm_surface_data *data;
942 struct shm_pool *resize_pool;
947 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
949 if (leaf->cairo_surface)
950 cairo_surface_destroy(leaf->cairo_surface);
951 /* leaf->data already destroyed via cairo private */
953 if (leaf->resize_pool)
954 shm_pool_destroy(leaf->resize_pool);
956 memset(leaf, 0, sizeof *leaf);
962 struct toysurface base;
963 struct display *display;
964 struct wl_surface *surface;
968 struct shm_surface_leaf leaf[MAX_LEAVES];
969 struct shm_surface_leaf *current;
972 static struct shm_surface *
973 to_shm_surface(struct toysurface *base)
975 return container_of(base, struct shm_surface, base);
979 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
982 struct shm_surface_leaf *leaf;
983 char bufs[MAX_LEAVES + 1];
986 for (i = 0; i < MAX_LEAVES; i++) {
987 leaf = &surface->leaf[i];
991 else if (leaf->cairo_surface)
997 bufs[MAX_LEAVES] = '\0';
998 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
1003 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
1005 struct shm_surface *surface = data;
1006 struct shm_surface_leaf *leaf;
1010 shm_surface_buffer_state_debug(surface, "buffer_release before");
1012 for (i = 0; i < MAX_LEAVES; i++) {
1013 leaf = &surface->leaf[i];
1014 if (leaf->data && leaf->data->buffer == buffer) {
1019 assert(i < MAX_LEAVES && "unknown buffer released");
1021 /* Leave one free leaf with storage, release others */
1023 for (i = 0; i < MAX_LEAVES; i++) {
1024 leaf = &surface->leaf[i];
1026 if (!leaf->cairo_surface || leaf->busy)
1032 shm_surface_leaf_release(leaf);
1035 shm_surface_buffer_state_debug(surface, "buffer_release after");
1038 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1039 shm_surface_buffer_release
1042 static cairo_surface_t *
1043 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1044 int32_t width, int32_t height, uint32_t flags,
1045 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1047 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1048 struct shm_surface *surface = to_shm_surface(base);
1049 struct rectangle rect = { 0};
1050 struct shm_surface_leaf *leaf = NULL;
1056 /* pick a free buffer, preferrably one that already has storage */
1057 for (i = 0; i < MAX_LEAVES; i++) {
1058 if (surface->leaf[i].busy)
1061 if (!leaf || surface->leaf[i].cairo_surface)
1062 leaf = &surface->leaf[i];
1064 DBG_OBJ(surface->surface, "pick leaf %d\n",
1065 (int)(leaf - &surface->leaf[0]));
1068 fprintf(stderr, "%s: all buffers are held by the server.\n",
1074 if (!resize_hint && leaf->resize_pool) {
1075 cairo_surface_destroy(leaf->cairo_surface);
1076 leaf->cairo_surface = NULL;
1077 shm_pool_destroy(leaf->resize_pool);
1078 leaf->resize_pool = NULL;
1081 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1083 if (leaf->cairo_surface &&
1084 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1085 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1088 if (leaf->cairo_surface)
1089 cairo_surface_destroy(leaf->cairo_surface);
1091 #ifdef USE_RESIZE_POOL
1092 if (resize_hint && !leaf->resize_pool) {
1093 /* Create a big pool to allocate from, while continuously
1094 * resizing. Mmapping a new pool in the server
1095 * is relatively expensive, so reusing a pool performs
1096 * better, but may temporarily reserve unneeded memory.
1098 /* We should probably base this number on the output size. */
1099 leaf->resize_pool = shm_pool_create(surface->display,
1105 rect.height = height;
1107 leaf->cairo_surface =
1108 display_create_shm_surface(surface->display, &rect,
1112 wl_buffer_add_listener(leaf->data->buffer,
1113 &shm_surface_buffer_listener, surface);
1116 surface->current = leaf;
1118 return cairo_surface_reference(leaf->cairo_surface);
1122 shm_surface_swap(struct toysurface *base,
1123 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1124 struct rectangle *server_allocation)
1126 struct shm_surface *surface = to_shm_surface(base);
1127 struct shm_surface_leaf *leaf = surface->current;
1129 server_allocation->width =
1130 cairo_image_surface_get_width(leaf->cairo_surface);
1131 server_allocation->height =
1132 cairo_image_surface_get_height(leaf->cairo_surface);
1134 buffer_to_surface_size (buffer_transform, buffer_scale,
1135 &server_allocation->width,
1136 &server_allocation->height);
1138 wl_surface_attach(surface->surface, leaf->data->buffer,
1139 surface->dx, surface->dy);
1140 wl_surface_damage(surface->surface, 0, 0,
1141 server_allocation->width, server_allocation->height);
1142 wl_surface_commit(surface->surface);
1144 DBG_OBJ(surface->surface, "leaf %d busy\n",
1145 (int)(leaf - &surface->leaf[0]));
1148 surface->current = NULL;
1152 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1158 shm_surface_release(struct toysurface *base)
1163 shm_surface_destroy(struct toysurface *base)
1165 struct shm_surface *surface = to_shm_surface(base);
1168 for (i = 0; i < MAX_LEAVES; i++)
1169 shm_surface_leaf_release(&surface->leaf[i]);
1174 static struct toysurface *
1175 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1176 uint32_t flags, struct rectangle *rectangle)
1178 struct shm_surface *surface;
1179 DBG_OBJ(wl_surface, "\n");
1181 surface = xmalloc(sizeof *surface);
1182 memset(surface, 0, sizeof *surface);
1187 surface->base.prepare = shm_surface_prepare;
1188 surface->base.swap = shm_surface_swap;
1189 surface->base.acquire = shm_surface_acquire;
1190 surface->base.release = shm_surface_release;
1191 surface->base.destroy = shm_surface_destroy;
1193 surface->display = display;
1194 surface->surface = wl_surface;
1195 surface->flags = flags;
1197 return &surface->base;
1201 * The following correspondences between file names and cursors was copied
1202 * from: https://bugs.kde.org/attachment.cgi?id=67313
1205 static const char *bottom_left_corners[] = {
1206 "bottom_left_corner",
1211 static const char *bottom_right_corners[] = {
1212 "bottom_right_corner",
1217 static const char *bottom_sides[] = {
1223 static const char *grabbings[] = {
1226 "208530c400c041818281048008011002"
1229 static const char *left_ptrs[] = {
1236 static const char *left_sides[] = {
1242 static const char *right_sides[] = {
1248 static const char *top_left_corners[] = {
1254 static const char *top_right_corners[] = {
1260 static const char *top_sides[] = {
1266 static const char *xterms[] = {
1272 static const char *hand1s[] = {
1276 "e29285e634086352946a0e7090d73106"
1279 static const char *watches[] = {
1282 "0426c94ea35c87780ff01dc239897213"
1285 struct cursor_alternatives {
1290 static const struct cursor_alternatives cursors[] = {
1291 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1292 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1293 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1294 {grabbings, ARRAY_LENGTH(grabbings)},
1295 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1296 {left_sides, ARRAY_LENGTH(left_sides)},
1297 {right_sides, ARRAY_LENGTH(right_sides)},
1298 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1299 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1300 {top_sides, ARRAY_LENGTH(top_sides)},
1301 {xterms, ARRAY_LENGTH(xterms)},
1302 {hand1s, ARRAY_LENGTH(hand1s)},
1303 {watches, ARRAY_LENGTH(watches)},
1307 create_cursors(struct display *display)
1311 unsigned int size = 32;
1313 struct wl_cursor *cursor;
1314 struct config_key shell_keys[] = {
1315 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1316 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
1318 struct config_section cs[] = {
1319 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1322 config_fd = open_config_file("weston.ini");
1323 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1326 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1328 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1330 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1332 for (j = 0; !cursor && j < cursors[i].count; ++j)
1333 cursor = wl_cursor_theme_get_cursor(
1334 display->cursor_theme, cursors[i].names[j]);
1337 fprintf(stderr, "could not load cursor '%s'\n",
1338 cursors[i].names[0]);
1340 display->cursors[i] = cursor;
1345 destroy_cursors(struct display *display)
1347 wl_cursor_theme_destroy(display->cursor_theme);
1348 free(display->cursors);
1351 struct wl_cursor_image *
1352 display_get_pointer_image(struct display *display, int pointer)
1354 struct wl_cursor *cursor = display->cursors[pointer];
1356 return cursor ? cursor->images[0] : NULL;
1360 surface_flush(struct surface *surface)
1362 if (!surface->cairo_surface)
1365 if (surface->opaque_region) {
1366 wl_surface_set_opaque_region(surface->surface,
1367 surface->opaque_region);
1368 wl_region_destroy(surface->opaque_region);
1369 surface->opaque_region = NULL;
1372 if (surface->input_region) {
1373 wl_surface_set_input_region(surface->surface,
1374 surface->input_region);
1375 wl_region_destroy(surface->input_region);
1376 surface->input_region = NULL;
1379 surface->toysurface->swap(surface->toysurface,
1380 surface->buffer_transform, surface->buffer_scale,
1381 &surface->server_allocation);
1383 cairo_surface_destroy(surface->cairo_surface);
1384 surface->cairo_surface = NULL;
1388 window_has_focus(struct window *window)
1390 return window->focus_count > 0;
1394 window_flush(struct window *window)
1396 struct surface *surface;
1398 if (window->type == TYPE_NONE) {
1399 window->type = TYPE_TOPLEVEL;
1400 if (window->shell_surface)
1401 wl_shell_surface_set_toplevel(window->shell_surface);
1404 wl_list_for_each(surface, &window->subsurface_list, link) {
1405 if (surface == window->main_surface)
1408 surface_flush(surface);
1411 surface_flush(window->main_surface);
1415 window_get_display(struct window *window)
1417 return window->display;
1421 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1423 struct display *display = surface->window->display;
1424 struct rectangle allocation = surface->allocation;
1426 if (!surface->toysurface && display->dpy &&
1427 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1428 surface->toysurface =
1429 egl_window_surface_create(display,
1435 if (!surface->toysurface)
1436 surface->toysurface = shm_surface_create(display,
1438 flags, &allocation);
1440 surface->cairo_surface = surface->toysurface->prepare(
1441 surface->toysurface, dx, dy,
1442 allocation.width, allocation.height, flags,
1443 surface->buffer_transform, surface->buffer_scale);
1447 window_create_main_surface(struct window *window)
1449 struct surface *surface = window->main_surface;
1454 if (window->resizing)
1455 flags |= SURFACE_HINT_RESIZE;
1457 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1458 flags |= SURFACE_HINT_RGB565;
1460 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1461 dx = surface->server_allocation.width -
1462 surface->allocation.width;
1464 if (window->resize_edges & WINDOW_RESIZING_TOP)
1465 dy = surface->server_allocation.height -
1466 surface->allocation.height;
1468 window->resize_edges = 0;
1470 surface_create_surface(surface, dx, dy, flags);
1474 window_get_buffer_transform(struct window *window)
1476 return window->main_surface->buffer_transform;
1480 window_set_buffer_transform(struct window *window,
1481 enum wl_output_transform transform)
1483 window->main_surface->buffer_transform = transform;
1484 wl_surface_set_buffer_transform(window->main_surface->surface,
1489 window_set_buffer_scale(struct window *window,
1492 window->main_surface->buffer_scale = scale;
1493 wl_surface_set_buffer_scale(window->main_surface->surface,
1498 window_get_buffer_scale(struct window *window)
1500 return window->main_surface->buffer_scale;
1504 window_get_output_scale(struct window *window)
1506 struct window_output *window_output;
1507 struct window_output *window_output_tmp;
1510 wl_list_for_each_safe(window_output, window_output_tmp,
1511 &window->window_output_list, link) {
1512 if (window_output->output->scale > scale)
1513 scale = window_output->output->scale;
1519 static void frame_destroy(struct frame *frame);
1522 surface_destroy(struct surface *surface)
1524 if (surface->frame_cb)
1525 wl_callback_destroy(surface->frame_cb);
1527 if (surface->input_region)
1528 wl_region_destroy(surface->input_region);
1530 if (surface->opaque_region)
1531 wl_region_destroy(surface->opaque_region);
1533 if (surface->subsurface)
1534 wl_subsurface_destroy(surface->subsurface);
1536 wl_surface_destroy(surface->surface);
1538 if (surface->toysurface)
1539 surface->toysurface->destroy(surface->toysurface);
1541 wl_list_remove(&surface->link);
1546 window_destroy(struct window *window)
1548 struct display *display = window->display;
1549 struct input *input;
1550 struct window_output *window_output;
1551 struct window_output *window_output_tmp;
1553 wl_list_remove(&window->redraw_task.link);
1555 wl_list_for_each(input, &display->input_list, link) {
1556 if (input->pointer_focus == window)
1557 input->pointer_focus = NULL;
1558 if (input->keyboard_focus == window)
1559 input->keyboard_focus = NULL;
1560 if (input->focus_widget &&
1561 input->focus_widget->window == window)
1562 input->focus_widget = NULL;
1565 wl_list_for_each_safe(window_output, window_output_tmp,
1566 &window->window_output_list, link) {
1567 free (window_output);
1571 frame_destroy(window->frame);
1573 if (window->shell_surface)
1574 wl_shell_surface_destroy(window->shell_surface);
1576 surface_destroy(window->main_surface);
1578 wl_list_remove(&window->link);
1580 free(window->title);
1584 static struct widget *
1585 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1587 struct widget *child, *target;
1589 wl_list_for_each(child, &widget->child_list, link) {
1590 target = widget_find_widget(child, x, y);
1595 if (widget->allocation.x <= x &&
1596 x < widget->allocation.x + widget->allocation.width &&
1597 widget->allocation.y <= y &&
1598 y < widget->allocation.y + widget->allocation.height) {
1605 static struct widget *
1606 window_find_widget(struct window *window, int32_t x, int32_t y)
1608 struct surface *surface;
1609 struct widget *widget;
1611 wl_list_for_each(surface, &window->subsurface_list, link) {
1612 widget = widget_find_widget(surface->widget, x, y);
1620 static struct widget *
1621 widget_create(struct window *window, struct surface *surface, void *data)
1623 struct widget *widget;
1625 widget = xzalloc(sizeof *widget);
1626 widget->window = window;
1627 widget->surface = surface;
1628 widget->user_data = data;
1629 widget->allocation = surface->allocation;
1630 wl_list_init(&widget->child_list);
1632 widget->tooltip = NULL;
1633 widget->tooltip_count = 0;
1634 widget->default_cursor = CURSOR_LEFT_PTR;
1640 window_add_widget(struct window *window, void *data)
1642 struct widget *widget;
1644 widget = widget_create(window, window->main_surface, data);
1645 wl_list_init(&widget->link);
1646 window->main_surface->widget = widget;
1652 widget_add_widget(struct widget *parent, void *data)
1654 struct widget *widget;
1656 widget = widget_create(parent->window, parent->surface, data);
1657 wl_list_insert(parent->child_list.prev, &widget->link);
1663 widget_destroy(struct widget *widget)
1665 struct display *display = widget->window->display;
1666 struct surface *surface = widget->surface;
1667 struct input *input;
1669 /* Destroy the sub-surface along with the root widget */
1670 if (surface->widget == widget && surface->subsurface)
1671 surface_destroy(widget->surface);
1673 if (widget->tooltip) {
1674 free(widget->tooltip);
1675 widget->tooltip = NULL;
1678 wl_list_for_each(input, &display->input_list, link) {
1679 if (input->focus_widget == widget)
1680 input->focus_widget = NULL;
1683 wl_list_remove(&widget->link);
1688 widget_set_default_cursor(struct widget *widget, int cursor)
1690 widget->default_cursor = cursor;
1694 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1696 *allocation = widget->allocation;
1700 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1702 widget->allocation.width = width;
1703 widget->allocation.height = height;
1707 widget_set_allocation(struct widget *widget,
1708 int32_t x, int32_t y, int32_t width, int32_t height)
1710 widget->allocation.x = x;
1711 widget->allocation.y = y;
1712 widget_set_size(widget, width, height);
1716 widget_set_transparent(struct widget *widget, int transparent)
1718 widget->opaque = !transparent;
1722 widget_get_user_data(struct widget *widget)
1724 return widget->user_data;
1727 static cairo_surface_t *
1728 widget_get_cairo_surface(struct widget *widget)
1730 struct surface *surface = widget->surface;
1731 struct window *window = widget->window;
1733 if (!surface->cairo_surface) {
1734 if (surface == window->main_surface)
1735 window_create_main_surface(window);
1737 surface_create_surface(surface, 0, 0, 0);
1740 return surface->cairo_surface;
1744 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1746 struct surface *surface = widget->surface;
1749 enum wl_output_transform transform;
1750 int surface_width, surface_height;
1751 int translate_x, translate_y;
1754 surface_width = surface->allocation.width;
1755 surface_height = surface->allocation.height;
1757 transform = surface->buffer_transform;
1758 scale = surface->buffer_scale;
1760 switch (transform) {
1761 case WL_OUTPUT_TRANSFORM_FLIPPED:
1762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1765 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1768 cairo_matrix_init_identity(&m);
1772 switch (transform) {
1773 case WL_OUTPUT_TRANSFORM_NORMAL:
1779 case WL_OUTPUT_TRANSFORM_FLIPPED:
1781 translate_x = surface_width;
1784 case WL_OUTPUT_TRANSFORM_90:
1786 translate_x = surface_height;
1789 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1791 translate_x = surface_height;
1792 translate_y = surface_width;
1794 case WL_OUTPUT_TRANSFORM_180:
1796 translate_x = surface_width;
1797 translate_y = surface_height;
1799 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1802 translate_y = surface_height;
1804 case WL_OUTPUT_TRANSFORM_270:
1805 angle = M_PI + M_PI_2;
1807 translate_y = surface_width;
1809 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1810 angle = M_PI + M_PI_2;
1816 cairo_scale(cr, scale, scale);
1817 cairo_translate(cr, translate_x, translate_y);
1818 cairo_rotate(cr, angle);
1819 cairo_transform(cr, &m);
1823 widget_cairo_create(struct widget *widget)
1825 struct surface *surface = widget->surface;
1826 cairo_surface_t *cairo_surface;
1829 cairo_surface = widget_get_cairo_surface(widget);
1830 cr = cairo_create(cairo_surface);
1832 widget_cairo_update_transform(widget, cr);
1834 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1840 widget_get_wl_surface(struct widget *widget)
1842 return widget->surface->surface;
1846 widget_get_last_time(struct widget *widget)
1848 return widget->surface->last_time;
1852 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1854 struct wl_compositor *comp = widget->window->display->compositor;
1855 struct surface *surface = widget->surface;
1857 if (!surface->input_region)
1858 surface->input_region = wl_compositor_create_region(comp);
1861 wl_region_add(surface->input_region,
1862 rect->x, rect->y, rect->width, rect->height);
1867 widget_set_resize_handler(struct widget *widget,
1868 widget_resize_handler_t handler)
1870 widget->resize_handler = handler;
1874 widget_set_redraw_handler(struct widget *widget,
1875 widget_redraw_handler_t handler)
1877 widget->redraw_handler = handler;
1881 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1883 widget->enter_handler = handler;
1887 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1889 widget->leave_handler = handler;
1893 widget_set_motion_handler(struct widget *widget,
1894 widget_motion_handler_t handler)
1896 widget->motion_handler = handler;
1900 widget_set_button_handler(struct widget *widget,
1901 widget_button_handler_t handler)
1903 widget->button_handler = handler;
1907 widget_set_touch_up_handler(struct widget *widget,
1908 widget_touch_up_handler_t handler)
1910 widget->touch_up_handler = handler;
1914 widget_set_touch_down_handler(struct widget *widget,
1915 widget_touch_down_handler_t handler)
1917 widget->touch_down_handler = handler;
1921 widget_set_touch_motion_handler(struct widget *widget,
1922 widget_touch_motion_handler_t handler)
1924 widget->touch_motion_handler = handler;
1928 widget_set_touch_frame_handler(struct widget *widget,
1929 widget_touch_frame_handler_t handler)
1931 widget->touch_frame_handler = handler;
1935 widget_set_touch_cancel_handler(struct widget *widget,
1936 widget_touch_cancel_handler_t handler)
1938 widget->touch_cancel_handler = handler;
1942 widget_set_axis_handler(struct widget *widget,
1943 widget_axis_handler_t handler)
1945 widget->axis_handler = handler;
1949 window_schedule_redraw_task(struct window *window);
1952 widget_schedule_redraw(struct widget *widget)
1954 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1955 widget->surface->redraw_needed = 1;
1956 window_schedule_redraw_task(widget->window);
1960 window_get_surface(struct window *window)
1962 cairo_surface_t *cairo_surface;
1964 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1966 return cairo_surface_reference(cairo_surface);
1970 window_get_wl_surface(struct window *window)
1972 return window->main_surface->surface;
1975 struct wl_shell_surface *
1976 window_get_wl_shell_surface(struct window *window)
1978 return window->shell_surface;
1982 tooltip_redraw_handler(struct widget *widget, void *data)
1985 const int32_t r = 3;
1986 struct tooltip *tooltip = data;
1987 int32_t width, height;
1989 cr = widget_cairo_create(widget);
1990 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1991 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1994 width = widget->allocation.width;
1995 height = widget->allocation.height;
1996 rounded_rect(cr, 0, 0, width, height, r);
1998 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1999 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2002 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2003 cairo_move_to(cr, 10, 16);
2004 cairo_show_text(cr, tooltip->entry);
2008 static cairo_text_extents_t
2009 get_text_extents(struct tooltip *tooltip)
2012 cairo_text_extents_t extents;
2014 /* Use the dummy_surface because tooltip's surface was not
2015 * created yet, and parent does not have a valid surface
2016 * outside repaint, either.
2018 cr = cairo_create(tooltip->window->display->dummy_surface);
2019 cairo_text_extents(cr, tooltip->entry, &extents);
2026 window_create_tooltip(struct tooltip *tooltip)
2028 struct widget *parent = tooltip->parent;
2029 struct display *display = parent->window->display;
2030 struct window *window;
2031 const int offset_y = 27;
2032 const int margin = 3;
2033 cairo_text_extents_t extents;
2035 if (tooltip->widget)
2038 window = window_create_transient(display, parent->window, tooltip->x,
2039 tooltip->y + offset_y,
2040 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2044 tooltip->window = window;
2045 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2047 extents = get_text_extents(tooltip);
2048 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2049 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2055 widget_destroy_tooltip(struct widget *parent)
2057 struct tooltip *tooltip = parent->tooltip;
2059 parent->tooltip_count = 0;
2063 if (tooltip->widget) {
2064 widget_destroy(tooltip->widget);
2065 window_destroy(tooltip->window);
2066 tooltip->widget = NULL;
2067 tooltip->window = NULL;
2070 close(tooltip->tooltip_fd);
2071 free(tooltip->entry);
2073 parent->tooltip = NULL;
2077 tooltip_func(struct task *task, uint32_t events)
2079 struct tooltip *tooltip =
2080 container_of(task, struct tooltip, tooltip_task);
2083 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2085 window_create_tooltip(tooltip);
2088 #define TOOLTIP_TIMEOUT 500
2090 tooltip_timer_reset(struct tooltip *tooltip)
2092 struct itimerspec its;
2094 its.it_interval.tv_sec = 0;
2095 its.it_interval.tv_nsec = 0;
2096 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2097 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2098 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2099 fprintf(stderr, "could not set timerfd\n: %m");
2107 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2109 struct tooltip *tooltip = parent->tooltip;
2111 parent->tooltip_count++;
2115 tooltip_timer_reset(tooltip);
2119 /* the handler might be triggered too fast via input device motion, so
2120 * we need this check here to make sure tooltip is fully initialized */
2121 if (parent->tooltip_count > 1)
2124 tooltip = malloc(sizeof *tooltip);
2128 parent->tooltip = tooltip;
2129 tooltip->parent = parent;
2130 tooltip->widget = NULL;
2131 tooltip->window = NULL;
2134 tooltip->entry = strdup(entry);
2135 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2136 if (tooltip->tooltip_fd < 0) {
2137 fprintf(stderr, "could not create timerfd\n: %m");
2141 tooltip->tooltip_task.run = tooltip_func;
2142 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2143 EPOLLIN, &tooltip->tooltip_task);
2144 tooltip_timer_reset(tooltip);
2150 workspace_manager_state(void *data,
2151 struct workspace_manager *workspace_manager,
2155 struct display *display = data;
2157 display->workspace = current;
2158 display->workspace_count = count;
2161 static const struct workspace_manager_listener workspace_manager_listener = {
2162 workspace_manager_state
2166 frame_resize_handler(struct widget *widget,
2167 int32_t width, int32_t height, void *data)
2169 struct frame *frame = data;
2170 struct widget *child = frame->child;
2171 struct rectangle allocation;
2172 struct display *display = widget->window->display;
2173 struct surface *surface = widget->surface;
2174 struct frame_button * button;
2175 struct theme *t = display->theme;
2176 int x_l, x_r, y, w, h;
2177 int decoration_width, decoration_height;
2178 int opaque_margin, shadow_margin;
2180 switch (widget->window->type) {
2181 case TYPE_FULLSCREEN:
2182 decoration_width = 0;
2183 decoration_height = 0;
2187 allocation.width = width;
2188 allocation.height = height;
2191 wl_list_for_each(button, &frame->buttons_list, link)
2192 button->widget->opaque = 1;
2194 case TYPE_MAXIMIZED:
2195 decoration_width = t->width * 2;
2196 decoration_height = t->width + t->titlebar_height;
2198 allocation.x = t->width;
2199 allocation.y = t->titlebar_height;
2200 allocation.width = width - decoration_width;
2201 allocation.height = height - decoration_height;
2205 wl_list_for_each(button, &frame->buttons_list, link)
2206 button->widget->opaque = 0;
2209 decoration_width = (t->width + t->margin) * 2;
2210 decoration_height = t->width +
2211 t->titlebar_height + t->margin * 2;
2213 allocation.x = t->width + t->margin;
2214 allocation.y = t->titlebar_height + t->margin;
2215 allocation.width = width - decoration_width;
2216 allocation.height = height - decoration_height;
2218 opaque_margin = t->margin + t->frame_radius;
2220 wl_list_for_each(button, &frame->buttons_list, link)
2221 button->widget->opaque = 0;
2225 widget_set_allocation(child, allocation.x, allocation.y,
2226 allocation.width, allocation.height);
2228 if (child->resize_handler)
2229 child->resize_handler(child,
2234 width = child->allocation.width + decoration_width;
2235 height = child->allocation.height + decoration_height;
2237 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2239 surface->input_region =
2240 wl_compositor_create_region(display->compositor);
2241 if (widget->window->type != TYPE_FULLSCREEN) {
2242 wl_region_add(surface->input_region,
2243 shadow_margin, shadow_margin,
2244 width - 2 * shadow_margin,
2245 height - 2 * shadow_margin);
2247 wl_region_add(surface->input_region, 0, 0, width, height);
2250 widget_set_allocation(widget, 0, 0, width, height);
2253 wl_region_add(surface->opaque_region,
2254 opaque_margin, opaque_margin,
2255 widget->allocation.width - 2 * opaque_margin,
2256 widget->allocation.height - 2 * opaque_margin);
2258 /* frame internal buttons */
2259 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2260 x_l = t->width + shadow_margin;
2261 y = t->width + shadow_margin;
2262 wl_list_for_each(button, &frame->buttons_list, link) {
2263 const int button_padding = 4;
2264 w = cairo_image_surface_get_width(button->icon);
2265 h = cairo_image_surface_get_height(button->icon);
2267 if (button->decoration == FRAME_BUTTON_FANCY)
2270 if (button->align == FRAME_BUTTON_LEFT) {
2271 widget_set_allocation(button->widget,
2272 x_l, y , w + 1, h + 1);
2274 x_l += button_padding;
2277 widget_set_allocation(button->widget,
2278 x_r, y , w + 1, h + 1);
2279 x_r -= button_padding;
2285 frame_button_enter_handler(struct widget *widget,
2286 struct input *input, float x, float y, void *data)
2288 struct frame_button *frame_button = data;
2290 widget_schedule_redraw(frame_button->widget);
2291 frame_button->state = FRAME_BUTTON_OVER;
2293 return CURSOR_LEFT_PTR;
2297 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2299 struct frame_button *frame_button = data;
2301 widget_schedule_redraw(frame_button->widget);
2302 frame_button->state = FRAME_BUTTON_DEFAULT;
2306 frame_button_button_handler(struct widget *widget,
2307 struct input *input, uint32_t time,
2309 enum wl_pointer_button_state state, void *data)
2311 struct frame_button *frame_button = data;
2312 struct window *window = widget->window;
2313 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
2315 if (button != BTN_LEFT)
2319 case WL_POINTER_BUTTON_STATE_PRESSED:
2320 frame_button->state = FRAME_BUTTON_ACTIVE;
2321 widget_schedule_redraw(frame_button->widget);
2323 if (frame_button->type == FRAME_BUTTON_ICON)
2324 window_show_frame_menu(window, input, time);
2326 case WL_POINTER_BUTTON_STATE_RELEASED:
2327 frame_button->state = FRAME_BUTTON_DEFAULT;
2328 widget_schedule_redraw(frame_button->widget);
2335 switch (frame_button->type) {
2336 case FRAME_BUTTON_CLOSE:
2337 if (window->close_handler)
2338 window->close_handler(window->parent,
2341 display_exit(window->display);
2343 case FRAME_BUTTON_MINIMIZE:
2344 fprintf(stderr,"Minimize stub\n");
2346 case FRAME_BUTTON_MAXIMIZE:
2347 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2350 /* Unknown operation */
2356 frame_button_touch_down_handler(struct widget *widget, uint32_t serial,
2357 uint32_t time, int32_t id,
2358 float x, float y, void *data)
2360 struct frame_button *frame_button = data;
2361 struct window *window = widget->window;
2363 switch (frame_button->type) {
2364 case FRAME_BUTTON_CLOSE:
2365 if (window->close_handler)
2366 window->close_handler(window->parent,
2369 display_exit(window->display);
2371 case FRAME_BUTTON_MINIMIZE:
2372 fprintf(stderr,"Minimize stub\n");
2374 case FRAME_BUTTON_MAXIMIZE:
2375 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2378 /* Unknown operation */
2385 frame_button_motion_handler(struct widget *widget,
2386 struct input *input, uint32_t time,
2387 float x, float y, void *data)
2389 struct frame_button *frame_button = data;
2390 enum frame_button_pointer previous_button_state = frame_button->state;
2392 /* only track state for a pressed button */
2393 if (input->grab != widget)
2394 return CURSOR_LEFT_PTR;
2396 if (x > widget->allocation.x &&
2397 x < (widget->allocation.x + widget->allocation.width) &&
2398 y > widget->allocation.y &&
2399 y < (widget->allocation.y + widget->allocation.height)) {
2400 frame_button->state = FRAME_BUTTON_ACTIVE;
2402 frame_button->state = FRAME_BUTTON_DEFAULT;
2405 if (frame_button->state != previous_button_state)
2406 widget_schedule_redraw(frame_button->widget);
2408 return CURSOR_LEFT_PTR;
2412 frame_button_redraw_handler(struct widget *widget, void *data)
2414 struct frame_button *frame_button = data;
2416 int width, height, x, y;
2418 x = widget->allocation.x;
2419 y = widget->allocation.y;
2420 width = widget->allocation.width;
2421 height = widget->allocation.height;
2430 cr = widget_cairo_create(widget);
2432 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2433 cairo_set_line_width(cr, 1);
2435 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2436 cairo_rectangle (cr, x, y, 25, 16);
2438 cairo_stroke_preserve(cr);
2440 switch (frame_button->state) {
2441 case FRAME_BUTTON_DEFAULT:
2442 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2444 case FRAME_BUTTON_OVER:
2445 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2447 case FRAME_BUTTON_ACTIVE:
2448 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2457 cairo_set_source_surface(cr, frame_button->icon, x, y);
2463 static struct widget *
2464 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2465 enum frame_button_align align, enum frame_button_decoration style)
2467 struct frame_button *frame_button;
2468 const char *icon = data;
2470 frame_button = xzalloc (sizeof *frame_button);
2471 frame_button->icon = cairo_image_surface_create_from_png(icon);
2472 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2473 frame_button->frame = frame;
2474 frame_button->type = type;
2475 frame_button->align = align;
2476 frame_button->decoration = style;
2478 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2480 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2481 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2482 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2483 widget_set_touch_down_handler(frame_button->widget, frame_button_touch_down_handler);
2484 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
2485 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
2486 return frame_button->widget;
2490 frame_button_destroy(struct frame_button *frame_button)
2492 widget_destroy(frame_button->widget);
2493 wl_list_remove(&frame_button->link);
2494 cairo_surface_destroy(frame_button->icon);
2501 frame_redraw_handler(struct widget *widget, void *data)
2504 struct window *window = widget->window;
2505 struct theme *t = window->display->theme;
2508 if (window->type == TYPE_FULLSCREEN)
2511 cr = widget_cairo_create(widget);
2513 if (window->focus_count)
2514 flags |= THEME_FRAME_ACTIVE;
2515 if (window->type == TYPE_MAXIMIZED)
2516 flags |= THEME_FRAME_MAXIMIZED;
2517 theme_render_frame(t, cr, widget->allocation.width,
2518 widget->allocation.height, window->title, flags);
2524 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
2526 struct theme *t = frame->widget->window->display->theme;
2527 struct window *window = frame->widget->window;
2530 if (window->type != TYPE_TOPLEVEL)
2531 return CURSOR_LEFT_PTR;
2533 location = theme_get_location(t, input->sx, input->sy,
2534 frame->widget->allocation.width,
2535 frame->widget->allocation.height,
2536 window->type == TYPE_MAXIMIZED ?
2537 THEME_FRAME_MAXIMIZED : 0);
2540 case THEME_LOCATION_RESIZING_TOP:
2542 case THEME_LOCATION_RESIZING_BOTTOM:
2543 return CURSOR_BOTTOM;
2544 case THEME_LOCATION_RESIZING_LEFT:
2546 case THEME_LOCATION_RESIZING_RIGHT:
2547 return CURSOR_RIGHT;
2548 case THEME_LOCATION_RESIZING_TOP_LEFT:
2549 return CURSOR_TOP_LEFT;
2550 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2551 return CURSOR_TOP_RIGHT;
2552 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2553 return CURSOR_BOTTOM_LEFT;
2554 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2555 return CURSOR_BOTTOM_RIGHT;
2556 case THEME_LOCATION_EXTERIOR:
2557 case THEME_LOCATION_TITLEBAR:
2559 return CURSOR_LEFT_PTR;
2564 frame_menu_func(struct window *window, int index, void *data)
2566 struct display *display;
2570 if (window->close_handler)
2571 window->close_handler(window->parent,
2574 display_exit(window->display);
2576 case 1: /* move to workspace above */
2577 display = window->display;
2578 if (display->workspace > 0)
2579 workspace_manager_move_surface(
2580 display->workspace_manager,
2581 window->main_surface->surface,
2582 display->workspace - 1);
2584 case 2: /* move to workspace below */
2585 display = window->display;
2586 if (display->workspace < display->workspace_count - 1)
2587 workspace_manager_move_surface(
2588 display->workspace_manager,
2589 window->main_surface->surface,
2590 display->workspace + 1);
2592 case 3: /* fullscreen */
2593 /* we don't have a way to get out of fullscreen for now */
2594 if (window->fullscreen_handler)
2595 window->fullscreen_handler(window, window->user_data);
2601 window_show_frame_menu(struct window *window,
2602 struct input *input, uint32_t time)
2607 static const char *entries[] = {
2609 "Move to workspace above", "Move to workspace below",
2613 if (window->fullscreen_handler)
2614 count = ARRAY_LENGTH(entries);
2616 count = ARRAY_LENGTH(entries) - 1;
2618 input_get_position(input, &x, &y);
2619 window_show_menu(window->display, input, time, window,
2620 x - 10, y - 10, frame_menu_func, entries, count);
2624 frame_enter_handler(struct widget *widget,
2625 struct input *input, float x, float y, void *data)
2627 return frame_get_pointer_image_for_location(data, input);
2631 frame_motion_handler(struct widget *widget,
2632 struct input *input, uint32_t time,
2633 float x, float y, void *data)
2635 return frame_get_pointer_image_for_location(data, input);
2639 frame_button_handler(struct widget *widget,
2640 struct input *input, uint32_t time,
2641 uint32_t button, enum wl_pointer_button_state state,
2645 struct frame *frame = data;
2646 struct window *window = widget->window;
2647 struct display *display = window->display;
2650 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
2653 location = theme_get_location(display->theme, input->sx, input->sy,
2654 frame->widget->allocation.width,
2655 frame->widget->allocation.height,
2656 window->type == TYPE_MAXIMIZED ?
2657 THEME_FRAME_MAXIMIZED : 0);
2659 if (window->display->shell && button == BTN_LEFT &&
2660 window->type == TYPE_TOPLEVEL) {
2662 case THEME_LOCATION_TITLEBAR:
2663 if (!window->shell_surface)
2665 input_ungrab(input);
2666 wl_shell_surface_move(window->shell_surface,
2667 input_get_seat(input),
2670 case THEME_LOCATION_RESIZING_TOP:
2671 case THEME_LOCATION_RESIZING_BOTTOM:
2672 case THEME_LOCATION_RESIZING_LEFT:
2673 case THEME_LOCATION_RESIZING_RIGHT:
2674 case THEME_LOCATION_RESIZING_TOP_LEFT:
2675 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2676 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2677 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2678 if (!window->shell_surface)
2680 input_ungrab(input);
2682 window->resizing = 1;
2683 wl_shell_surface_resize(window->shell_surface,
2684 input_get_seat(input),
2685 display->serial, location);
2688 } else if (button == BTN_RIGHT &&
2689 (window->type == TYPE_TOPLEVEL ||
2690 window->type == TYPE_MAXIMIZED)) {
2691 window_show_frame_menu(window, input, time);
2696 frame_create(struct window *window, void *data)
2698 struct frame *frame;
2700 frame = xzalloc(sizeof *frame);
2701 frame->widget = window_add_widget(window, frame);
2702 frame->child = widget_add_widget(frame->widget, data);
2704 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2705 widget_set_resize_handler(frame->widget, frame_resize_handler);
2706 widget_set_enter_handler(frame->widget, frame_enter_handler);
2707 widget_set_motion_handler(frame->widget, frame_motion_handler);
2708 widget_set_button_handler(frame->widget, frame_button_handler);
2710 /* Create empty list for frame buttons */
2711 wl_list_init(&frame->buttons_list);
2713 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2714 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2716 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2717 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2719 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2720 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2722 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2723 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2725 window->frame = frame;
2727 return frame->child;
2731 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2733 struct display *display = widget->window->display;
2734 struct theme *t = display->theme;
2735 int decoration_width, decoration_height;
2737 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2739 if (widget->window->type != TYPE_FULLSCREEN) {
2740 decoration_width = (t->width + margin) * 2;
2741 decoration_height = t->width +
2742 t->titlebar_height + margin * 2;
2744 width = child_width + decoration_width;
2745 height = child_height + decoration_height;
2747 width = child_width;
2748 height = child_height;
2751 window_schedule_resize(widget->window, width, height);
2755 frame_destroy(struct frame *frame)
2757 struct frame_button *button, *tmp;
2759 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2760 frame_button_destroy(button);
2762 /* frame->child must be destroyed by the application */
2763 widget_destroy(frame->widget);
2768 input_set_focus_widget(struct input *input, struct widget *focus,
2771 struct widget *old, *widget;
2774 if (focus == input->focus_widget)
2777 old = input->focus_widget;
2781 widget = input->grab;
2782 if (widget->leave_handler)
2783 widget->leave_handler(old, input, widget->user_data);
2784 input->focus_widget = NULL;
2790 widget = input->grab;
2791 input->focus_widget = focus;
2792 if (widget->enter_handler)
2793 cursor = widget->enter_handler(focus, input, x, y,
2796 cursor = widget->default_cursor;
2798 input_set_pointer_image(input, cursor);
2803 input_grab(struct input *input, struct widget *widget, uint32_t button)
2805 input->grab = widget;
2806 input->grab_button = button;
2810 input_ungrab(struct input *input)
2812 struct widget *widget;
2815 if (input->pointer_focus) {
2816 widget = window_find_widget(input->pointer_focus,
2817 input->sx, input->sy);
2818 input_set_focus_widget(input, widget, input->sx, input->sy);
2823 input_remove_pointer_focus(struct input *input)
2825 struct window *window = input->pointer_focus;
2830 input_set_focus_widget(input, NULL, 0, 0);
2832 input->pointer_focus = NULL;
2833 input->current_cursor = CURSOR_UNSET;
2837 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2838 uint32_t serial, struct wl_surface *surface,
2839 wl_fixed_t sx_w, wl_fixed_t sy_w)
2841 struct input *input = data;
2842 struct window *window;
2843 struct widget *widget;
2844 float sx = wl_fixed_to_double(sx_w);
2845 float sy = wl_fixed_to_double(sy_w);
2848 /* enter event for a window we've just destroyed */
2852 input->display->serial = serial;
2853 input->pointer_enter_serial = serial;
2854 input->pointer_focus = wl_surface_get_user_data(surface);
2855 window = input->pointer_focus;
2857 if (window->resizing) {
2858 window->resizing = 0;
2859 /* Schedule a redraw to free the pool */
2860 window_schedule_redraw(window);
2866 widget = window_find_widget(window, sx, sy);
2867 input_set_focus_widget(input, widget, sx, sy);
2871 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2872 uint32_t serial, struct wl_surface *surface)
2874 struct input *input = data;
2876 input->display->serial = serial;
2877 input_remove_pointer_focus(input);
2881 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2882 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2884 struct input *input = data;
2885 struct window *window = input->pointer_focus;
2886 struct widget *widget;
2888 float sx = wl_fixed_to_double(sx_w);
2889 float sy = wl_fixed_to_double(sy_w);
2891 /* when making the window smaller - e.g. after a unmaximise we might
2892 * still have a pending motion event that the compositor has picked
2893 * based on the old surface dimensions
2895 if (sx > window->main_surface->allocation.width ||
2896 sy > window->main_surface->allocation.height)
2905 if (!(input->grab && input->grab_button)) {
2906 widget = window_find_widget(window, sx, sy);
2907 input_set_focus_widget(input, widget, sx, sy);
2911 widget = input->grab;
2913 widget = input->focus_widget;
2915 if (widget->motion_handler)
2916 cursor = widget->motion_handler(input->focus_widget,
2917 input, time, sx, sy,
2920 cursor = widget->default_cursor;
2922 cursor = CURSOR_LEFT_PTR;
2924 input_set_pointer_image(input, cursor);
2928 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2929 uint32_t time, uint32_t button, uint32_t state_w)
2931 struct input *input = data;
2932 struct widget *widget;
2933 enum wl_pointer_button_state state = state_w;
2935 input->display->serial = serial;
2936 if (input->focus_widget && input->grab == NULL &&
2937 state == WL_POINTER_BUTTON_STATE_PRESSED)
2938 input_grab(input, input->focus_widget, button);
2940 widget = input->grab;
2941 if (widget && widget->button_handler)
2942 (*widget->button_handler)(widget,
2945 input->grab->user_data);
2947 if (input->grab && input->grab_button == button &&
2948 state == WL_POINTER_BUTTON_STATE_RELEASED)
2949 input_ungrab(input);
2953 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2954 uint32_t time, uint32_t axis, wl_fixed_t value)
2956 struct input *input = data;
2957 struct widget *widget;
2959 widget = input->focus_widget;
2961 widget = input->grab;
2962 if (widget && widget->axis_handler)
2963 (*widget->axis_handler)(widget,
2969 static const struct wl_pointer_listener pointer_listener = {
2970 pointer_handle_enter,
2971 pointer_handle_leave,
2972 pointer_handle_motion,
2973 pointer_handle_button,
2974 pointer_handle_axis,
2978 input_remove_keyboard_focus(struct input *input)
2980 struct window *window = input->keyboard_focus;
2981 struct itimerspec its;
2983 its.it_interval.tv_sec = 0;
2984 its.it_interval.tv_nsec = 0;
2985 its.it_value.tv_sec = 0;
2986 its.it_value.tv_nsec = 0;
2987 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2992 window->focus_count--;
2993 if (window->keyboard_focus_handler)
2994 (*window->keyboard_focus_handler)(window, NULL,
2997 input->keyboard_focus = NULL;
3001 keyboard_repeat_func(struct task *task, uint32_t events)
3003 struct input *input =
3004 container_of(task, struct input, repeat_task);
3005 struct window *window = input->keyboard_focus;
3008 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
3009 /* If we change the timer between the fd becoming
3010 * readable and getting here, there'll be nothing to
3011 * read and we get EAGAIN. */
3014 if (window && window->key_handler) {
3015 (*window->key_handler)(window, input, input->repeat_time,
3016 input->repeat_key, input->repeat_sym,
3017 WL_KEYBOARD_KEY_STATE_PRESSED,
3023 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
3024 uint32_t format, int fd, uint32_t size)
3026 struct input *input = data;
3034 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
3039 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
3040 if (map_str == MAP_FAILED) {
3045 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
3047 XKB_KEYMAP_FORMAT_TEXT_V1,
3049 munmap(map_str, size);
3052 if (!input->xkb.keymap) {
3053 fprintf(stderr, "failed to compile keymap\n");
3057 input->xkb.state = xkb_state_new(input->xkb.keymap);
3058 if (!input->xkb.state) {
3059 fprintf(stderr, "failed to create XKB state\n");
3060 xkb_map_unref(input->xkb.keymap);
3061 input->xkb.keymap = NULL;
3065 input->xkb.control_mask =
3066 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
3067 input->xkb.alt_mask =
3068 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
3069 input->xkb.shift_mask =
3070 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
3074 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
3075 uint32_t serial, struct wl_surface *surface,
3076 struct wl_array *keys)
3078 struct input *input = data;
3079 struct window *window;
3081 input->display->serial = serial;
3082 input->keyboard_focus = wl_surface_get_user_data(surface);
3084 window = input->keyboard_focus;
3085 window->focus_count++;
3086 if (window->keyboard_focus_handler)
3087 (*window->keyboard_focus_handler)(window,
3088 input, window->user_data);
3092 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3093 uint32_t serial, struct wl_surface *surface)
3095 struct input *input = data;
3097 input->display->serial = serial;
3098 input_remove_keyboard_focus(input);
3102 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
3103 uint32_t serial, uint32_t time, uint32_t key,
3106 struct input *input = data;
3107 struct window *window = input->keyboard_focus;
3108 uint32_t code, num_syms;
3109 enum wl_keyboard_key_state state = state_w;
3110 const xkb_keysym_t *syms;
3112 struct itimerspec its;
3114 input->display->serial = serial;
3116 if (!window || !input->xkb.state)
3119 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
3121 sym = XKB_KEY_NoSymbol;
3125 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
3126 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3127 window_set_maximized(window,
3128 window->type != TYPE_MAXIMIZED);
3129 } else if (sym == XKB_KEY_F11 &&
3130 window->fullscreen_handler &&
3131 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3132 window->fullscreen_handler(window, window->user_data);
3133 } else if (sym == XKB_KEY_F4 &&
3134 input->modifiers == MOD_ALT_MASK &&
3135 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3136 if (window->close_handler)
3137 window->close_handler(window->parent,
3140 display_exit(window->display);
3141 } else if (window->key_handler) {
3142 (*window->key_handler)(window, input, time, key,
3143 sym, state, window->user_data);
3146 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3147 key == input->repeat_key) {
3148 its.it_interval.tv_sec = 0;
3149 its.it_interval.tv_nsec = 0;
3150 its.it_value.tv_sec = 0;
3151 its.it_value.tv_nsec = 0;
3152 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3153 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3154 input->repeat_sym = sym;
3155 input->repeat_key = key;
3156 input->repeat_time = time;
3157 its.it_interval.tv_sec = 0;
3158 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3159 its.it_value.tv_sec = 0;
3160 its.it_value.tv_nsec = 400 * 1000 * 1000;
3161 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3166 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3167 uint32_t serial, uint32_t mods_depressed,
3168 uint32_t mods_latched, uint32_t mods_locked,
3171 struct input *input = data;
3172 xkb_mod_mask_t mask;
3174 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3175 if (!input->xkb.keymap)
3178 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3179 mods_locked, 0, 0, group);
3180 mask = xkb_state_serialize_mods(input->xkb.state,
3181 XKB_STATE_DEPRESSED |
3183 input->modifiers = 0;
3184 if (mask & input->xkb.control_mask)
3185 input->modifiers |= MOD_CONTROL_MASK;
3186 if (mask & input->xkb.alt_mask)
3187 input->modifiers |= MOD_ALT_MASK;
3188 if (mask & input->xkb.shift_mask)
3189 input->modifiers |= MOD_SHIFT_MASK;
3192 static const struct wl_keyboard_listener keyboard_listener = {
3193 keyboard_handle_keymap,
3194 keyboard_handle_enter,
3195 keyboard_handle_leave,
3196 keyboard_handle_key,
3197 keyboard_handle_modifiers,
3201 touch_handle_down(void *data, struct wl_touch *wl_touch,
3202 uint32_t serial, uint32_t time, struct wl_surface *surface,
3203 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3205 struct input *input = data;
3206 struct widget *widget;
3207 float sx = wl_fixed_to_double(x_w);
3208 float sy = wl_fixed_to_double(y_w);
3210 DBG("touch_handle_down: %i %i\n", id, wl_list_length(&input->touch_point_list));
3212 input->touch_focus = wl_surface_get_user_data(surface);
3213 if (!input->touch_focus) {
3214 DBG("Failed to find to touch focus for surface %p\n", surface);
3218 widget = window_find_widget(input->touch_focus,
3219 wl_fixed_to_double(x_w),
3220 wl_fixed_to_double(y_w));
3222 struct touch_point *tp = xmalloc(sizeof *tp);
3225 tp->widget = widget;
3226 wl_list_insert(&input->touch_point_list, &tp->link);
3228 if (widget->touch_down_handler)
3229 (*widget->touch_down_handler)(widget, serial,
3238 touch_handle_up(void *data, struct wl_touch *wl_touch,
3239 uint32_t serial, uint32_t time, int32_t id)
3241 struct input *input = data;
3242 struct touch_point *tp, *tmp;
3244 DBG("touch_handle_up: %i %i\n", id, wl_list_length(&input->touch_point_list));
3246 if (!input->touch_focus) {
3247 DBG("No touch focus found for touch up event!\n");
3251 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3255 if (tp->widget->touch_up_handler)
3256 (*tp->widget->touch_up_handler)(tp->widget, serial,
3258 tp->widget->user_data);
3260 wl_list_remove(&tp->link);
3268 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3269 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3271 struct input *input = data;
3272 struct touch_point *tp;
3273 float sx = wl_fixed_to_double(x_w);
3274 float sy = wl_fixed_to_double(y_w);
3276 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3278 if (!input->touch_focus) {
3279 DBG("No touch focus found for touch motion event!\n");
3283 wl_list_for_each(tp, &input->touch_point_list, link) {
3287 if (tp->widget->touch_motion_handler)
3288 (*tp->widget->touch_motion_handler)(tp->widget, time,
3290 tp->widget->user_data);
3296 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3298 struct input *input = data;
3299 struct touch_point *tp, *tmp;
3301 DBG("touch_handle_frame\n");
3303 if (!input->touch_focus) {
3304 DBG("No touch focus found for touch frame event!\n");
3308 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3309 if (tp->widget->touch_frame_handler)
3310 (*tp->widget->touch_frame_handler)(tp->widget, tp->widget->user_data);
3312 wl_list_remove(&tp->link);
3318 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3320 struct input *input = data;
3321 struct touch_point *tp, *tmp;
3323 DBG("touch_handle_cancel\n");
3325 if (!input->touch_focus) {
3326 DBG("No touch focus found for touch cancel event!\n");
3330 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3331 if (tp->widget->touch_cancel_handler)
3332 (*tp->widget->touch_cancel_handler)(tp->widget, tp->widget->user_data);
3334 wl_list_remove(&tp->link);
3339 static const struct wl_touch_listener touch_listener = {
3342 touch_handle_motion,
3344 touch_handle_cancel,
3348 seat_handle_capabilities(void *data, struct wl_seat *seat,
3349 enum wl_seat_capability caps)
3351 struct input *input = data;
3353 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3354 input->pointer = wl_seat_get_pointer(seat);
3355 wl_pointer_set_user_data(input->pointer, input);
3356 wl_pointer_add_listener(input->pointer, &pointer_listener,
3358 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3359 wl_pointer_destroy(input->pointer);
3360 input->pointer = NULL;
3363 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3364 input->keyboard = wl_seat_get_keyboard(seat);
3365 wl_keyboard_set_user_data(input->keyboard, input);
3366 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3368 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3369 wl_keyboard_destroy(input->keyboard);
3370 input->keyboard = NULL;
3373 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3374 input->touch = wl_seat_get_touch(seat);
3375 wl_touch_set_user_data(input->touch, input);
3376 wl_touch_add_listener(input->touch, &touch_listener, input);
3377 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3378 wl_touch_destroy(input->touch);
3379 input->touch = NULL;
3383 static const struct wl_seat_listener seat_listener = {
3384 seat_handle_capabilities,
3388 input_get_position(struct input *input, int32_t *x, int32_t *y)
3395 input_get_display(struct input *input)
3397 return input->display;
3401 input_get_seat(struct input *input)
3407 input_get_modifiers(struct input *input)
3409 return input->modifiers;
3413 input_get_focus_widget(struct input *input)
3415 return input->focus_widget;
3419 struct wl_data_offer *offer;
3420 struct input *input;
3421 struct wl_array types;
3424 struct task io_task;
3432 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3434 struct data_offer *offer = data;
3437 p = wl_array_add(&offer->types, sizeof *p);
3441 static const struct wl_data_offer_listener data_offer_listener = {
3446 data_offer_destroy(struct data_offer *offer)
3451 if (offer->refcount == 0) {
3452 wl_data_offer_destroy(offer->offer);
3453 for (p = offer->types.data; *p; p++)
3455 wl_array_release(&offer->types);
3461 data_device_data_offer(void *data,
3462 struct wl_data_device *data_device,
3463 struct wl_data_offer *_offer)
3465 struct data_offer *offer;
3467 offer = xmalloc(sizeof *offer);
3469 wl_array_init(&offer->types);
3470 offer->refcount = 1;
3471 offer->input = data;
3472 offer->offer = _offer;
3473 wl_data_offer_add_listener(offer->offer,
3474 &data_offer_listener, offer);
3478 data_device_enter(void *data, struct wl_data_device *data_device,
3479 uint32_t serial, struct wl_surface *surface,
3480 wl_fixed_t x_w, wl_fixed_t y_w,
3481 struct wl_data_offer *offer)
3483 struct input *input = data;
3484 struct window *window;
3486 float x = wl_fixed_to_double(x_w);
3487 float y = wl_fixed_to_double(y_w);
3490 input->pointer_enter_serial = serial;
3491 window = wl_surface_get_user_data(surface);
3492 input->pointer_focus = window;
3495 input->drag_offer = wl_data_offer_get_user_data(offer);
3497 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3500 types_data = input->drag_offer->types.data;
3502 input->drag_offer = NULL;
3506 window = input->pointer_focus;
3507 if (window->data_handler)
3508 window->data_handler(window, input, x, y, types_data,
3513 data_device_leave(void *data, struct wl_data_device *data_device)
3515 struct input *input = data;
3517 if (input->drag_offer) {
3518 data_offer_destroy(input->drag_offer);
3519 input->drag_offer = NULL;
3524 data_device_motion(void *data, struct wl_data_device *data_device,
3525 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3527 struct input *input = data;
3528 struct window *window = input->pointer_focus;
3529 float x = wl_fixed_to_double(x_w);
3530 float y = wl_fixed_to_double(y_w);
3536 if (input->drag_offer)
3537 types_data = input->drag_offer->types.data;
3541 if (window->data_handler)
3542 window->data_handler(window, input, x, y, types_data,
3547 data_device_drop(void *data, struct wl_data_device *data_device)
3549 struct input *input = data;
3550 struct window *window = input->pointer_focus;
3552 if (window->drop_handler)
3553 window->drop_handler(window, input,
3554 input->sx, input->sy, window->user_data);
3558 data_device_selection(void *data,
3559 struct wl_data_device *wl_data_device,
3560 struct wl_data_offer *offer)
3562 struct input *input = data;
3565 if (input->selection_offer)
3566 data_offer_destroy(input->selection_offer);
3569 input->selection_offer = wl_data_offer_get_user_data(offer);
3570 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3573 input->selection_offer = NULL;
3577 static const struct wl_data_device_listener data_device_listener = {
3578 data_device_data_offer,
3583 data_device_selection
3587 input_set_pointer_image_index(struct input *input, int index)
3589 struct wl_buffer *buffer;
3590 struct wl_cursor *cursor;
3591 struct wl_cursor_image *image;
3593 if (!input->pointer)
3596 cursor = input->display->cursors[input->current_cursor];
3600 if (index >= (int) cursor->image_count) {
3601 fprintf(stderr, "cursor index out of range\n");
3605 image = cursor->images[index];
3606 buffer = wl_cursor_image_get_buffer(image);
3610 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3611 input->pointer_surface,
3612 image->hotspot_x, image->hotspot_y);
3613 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3614 wl_surface_damage(input->pointer_surface, 0, 0,
3615 image->width, image->height);
3616 wl_surface_commit(input->pointer_surface);
3619 static const struct wl_callback_listener pointer_surface_listener;
3622 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3625 struct input *input = data;
3626 struct wl_cursor *cursor;
3630 assert(callback == input->cursor_frame_cb);
3631 wl_callback_destroy(callback);
3632 input->cursor_frame_cb = NULL;
3635 if (!input->pointer)
3638 if (input->current_cursor == CURSOR_BLANK) {
3639 wl_pointer_set_cursor(input->pointer,
3640 input->pointer_enter_serial,
3645 if (input->current_cursor == CURSOR_UNSET)
3647 cursor = input->display->cursors[input->current_cursor];
3651 /* FIXME We don't have the current time on the first call so we set
3652 * the animation start to the time of the first frame callback. */
3654 input->cursor_anim_start = 0;
3655 else if (input->cursor_anim_start == 0)
3656 input->cursor_anim_start = time;
3658 if (time == 0 || input->cursor_anim_start == 0)
3661 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3663 if (cursor->image_count > 1) {
3664 input->cursor_frame_cb =
3665 wl_surface_frame(input->pointer_surface);
3666 wl_callback_add_listener(input->cursor_frame_cb,
3667 &pointer_surface_listener, input);
3670 input_set_pointer_image_index(input, i);
3673 static const struct wl_callback_listener pointer_surface_listener = {
3674 pointer_surface_frame_callback
3678 input_set_pointer_image(struct input *input, int pointer)
3682 if (!input->pointer)
3685 if (input->pointer_enter_serial > input->cursor_serial)
3688 if (!force && pointer == input->current_cursor)
3691 input->current_cursor = pointer;
3692 input->cursor_serial = input->pointer_enter_serial;
3693 if (!input->cursor_frame_cb)
3694 pointer_surface_frame_callback(input, NULL, 0);
3696 /* The current frame callback may be stuck if, for instance,
3697 * the set cursor request was processed by the server after
3698 * this client lost the focus. In this case the cursor surface
3699 * might not be mapped and the frame callback wouldn't ever
3700 * complete. Send a set_cursor and attach to try to map the
3701 * cursor surface again so that the callback will finish */
3702 input_set_pointer_image_index(input, 0);
3706 struct wl_data_device *
3707 input_get_data_device(struct input *input)
3709 return input->data_device;
3713 input_set_selection(struct input *input,
3714 struct wl_data_source *source, uint32_t time)
3716 wl_data_device_set_selection(input->data_device, source, time);
3720 input_accept(struct input *input, const char *type)
3722 wl_data_offer_accept(input->drag_offer->offer,
3723 input->pointer_enter_serial, type);
3727 offer_io_func(struct task *task, uint32_t events)
3729 struct data_offer *offer =
3730 container_of(task, struct data_offer, io_task);
3734 len = read(offer->fd, buffer, sizeof buffer);
3735 offer->func(buffer, len,
3736 offer->x, offer->y, offer->user_data);
3740 data_offer_destroy(offer);
3745 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3746 data_func_t func, void *user_data)
3750 if (pipe2(p, O_CLOEXEC) == -1)
3753 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3756 offer->io_task.run = offer_io_func;
3760 offer->user_data = user_data;
3762 display_watch_fd(offer->input->display,
3763 offer->fd, EPOLLIN, &offer->io_task);
3767 input_receive_drag_data(struct input *input, const char *mime_type,
3768 data_func_t func, void *data)
3770 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3771 input->drag_offer->x = input->sx;
3772 input->drag_offer->y = input->sy;
3776 input_receive_selection_data(struct input *input, const char *mime_type,
3777 data_func_t func, void *data)
3781 if (input->selection_offer == NULL)
3784 for (p = input->selection_offer->types.data; *p; p++)
3785 if (strcmp(mime_type, *p) == 0)
3791 data_offer_receive_data(input->selection_offer,
3792 mime_type, func, data);
3797 input_receive_selection_data_to_fd(struct input *input,
3798 const char *mime_type, int fd)
3800 if (input->selection_offer)
3801 wl_data_offer_receive(input->selection_offer->offer,
3808 window_move(struct window *window, struct input *input, uint32_t serial)
3810 if (!window->shell_surface)
3813 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3817 surface_set_synchronized(struct surface *surface)
3819 if (!surface->subsurface)
3822 if (surface->synchronized)
3825 wl_subsurface_set_sync(surface->subsurface);
3826 surface->synchronized = 1;
3830 surface_set_synchronized_default(struct surface *surface)
3832 if (!surface->subsurface)
3835 if (surface->synchronized == surface->synchronized_default)
3838 if (surface->synchronized_default)
3839 wl_subsurface_set_sync(surface->subsurface);
3841 wl_subsurface_set_desync(surface->subsurface);
3843 surface->synchronized = surface->synchronized_default;
3847 surface_resize(struct surface *surface)
3849 struct widget *widget = surface->widget;
3850 struct wl_compositor *compositor = widget->window->display->compositor;
3852 if (surface->input_region) {
3853 wl_region_destroy(surface->input_region);
3854 surface->input_region = NULL;
3857 if (surface->opaque_region)
3858 wl_region_destroy(surface->opaque_region);
3860 surface->opaque_region = wl_compositor_create_region(compositor);
3862 if (widget->resize_handler)
3863 widget->resize_handler(widget,
3864 widget->allocation.width,
3865 widget->allocation.height,
3868 if (surface->subsurface &&
3869 (surface->allocation.x != widget->allocation.x ||
3870 surface->allocation.y != widget->allocation.y)) {
3871 wl_subsurface_set_position(surface->subsurface,
3872 widget->allocation.x,
3873 widget->allocation.y);
3875 if (surface->allocation.width != widget->allocation.width ||
3876 surface->allocation.height != widget->allocation.height) {
3877 window_schedule_redraw(widget->window);
3879 surface->allocation = widget->allocation;
3882 wl_region_add(surface->opaque_region, 0, 0,
3883 widget->allocation.width,
3884 widget->allocation.height);
3888 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3891 * This hack should be removed, when EGL respects
3892 * eglSwapInterval(0).
3894 * If this window has sub-surfaces, especially a free-running
3895 * EGL-widget, we need to post the parent surface once with
3896 * all the old state to guarantee, that the EGL-widget will
3897 * receive its frame callback soon. Otherwise, a forced call
3898 * to eglSwapBuffers may end up blocking, waiting for a frame
3899 * event that will never come, because we will commit the parent
3900 * surface with all new state only after eglSwapBuffers returns.
3902 * This assumes, that:
3903 * 1. When the EGL widget's resize hook is called, it pauses.
3904 * 2. When the EGL widget's redraw hook is called, it forces a
3905 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3906 * In a single threaded application condition 1 is a no-op.
3908 * XXX: This should actually be after the surface_resize() calls,
3909 * but cannot, because then it would commit the incomplete state
3910 * accumulated from the widget resize hooks.
3912 if (window->subsurface_list.next != &window->main_surface->link ||
3913 window->subsurface_list.prev != &window->main_surface->link)
3914 wl_surface_commit(window->main_surface->surface);
3918 idle_resize(struct window *window)
3920 struct surface *surface;
3922 window->resize_needed = 0;
3923 window->redraw_needed = 1;
3925 DBG("from %dx%d to %dx%d\n",
3926 window->main_surface->server_allocation.width,
3927 window->main_surface->server_allocation.height,
3928 window->pending_allocation.width,
3929 window->pending_allocation.height);
3931 hack_prevent_EGL_sub_surface_deadlock(window);
3933 widget_set_allocation(window->main_surface->widget,
3934 window->pending_allocation.x,
3935 window->pending_allocation.y,
3936 window->pending_allocation.width,
3937 window->pending_allocation.height);
3939 surface_resize(window->main_surface);
3941 /* The main surface is in the list, too. Main surface's
3942 * resize_handler is responsible for calling widget_set_allocation()
3943 * on all sub-surface root widgets, so they will be resized
3946 wl_list_for_each(surface, &window->subsurface_list, link) {
3947 if (surface == window->main_surface)
3950 surface_set_synchronized(surface);
3951 surface_resize(surface);
3956 window_schedule_resize(struct window *window, int width, int height)
3958 window->pending_allocation.x = 0;
3959 window->pending_allocation.y = 0;
3960 window->pending_allocation.width = width;
3961 window->pending_allocation.height = height;
3963 if (window->min_allocation.width == 0)
3964 window->min_allocation = window->pending_allocation;
3965 if (window->pending_allocation.width < window->min_allocation.width)
3966 window->pending_allocation.width = window->min_allocation.width;
3967 if (window->pending_allocation.height < window->min_allocation.height)
3968 window->pending_allocation.height = window->min_allocation.height;
3970 window->resize_needed = 1;
3971 window_schedule_redraw(window);
3975 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3977 window_schedule_resize(widget->window, width, height);
3981 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3984 wl_shell_surface_pong(shell_surface, serial);
3988 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3989 uint32_t edges, int32_t width, int32_t height)
3991 struct window *window = data;
3993 window->resize_edges = edges;
3994 window_schedule_resize(window, width, height);
3998 menu_destroy(struct menu *menu)
4000 widget_destroy(menu->widget);
4001 window_destroy(menu->window);
4006 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
4008 struct window *window = data;
4009 struct menu *menu = window->main_surface->widget->user_data;
4011 /* FIXME: Need more context in this event, at least the input
4012 * device. Or just use wl_callback. And this really needs to
4013 * be a window vfunc that the menu can set. And we need the
4016 input_ungrab(menu->input);
4020 static const struct wl_shell_surface_listener shell_surface_listener = {
4027 window_get_allocation(struct window *window,
4028 struct rectangle *allocation)
4030 *allocation = window->main_surface->allocation;
4034 widget_redraw(struct widget *widget)
4036 struct widget *child;
4038 if (widget->redraw_handler)
4039 widget->redraw_handler(widget, widget->user_data);
4040 wl_list_for_each(child, &widget->child_list, link)
4041 widget_redraw(child);
4045 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4047 struct surface *surface = data;
4049 assert(callback == surface->frame_cb);
4050 DBG_OBJ(callback, "done\n");
4051 wl_callback_destroy(callback);
4052 surface->frame_cb = NULL;
4054 surface->last_time = time;
4056 if (surface->redraw_needed || surface->window->redraw_needed) {
4057 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4058 window_schedule_redraw_task(surface->window);
4062 static const struct wl_callback_listener listener = {
4067 surface_redraw(struct surface *surface)
4069 DBG_OBJ(surface->surface, "begin\n");
4071 if (!surface->window->redraw_needed && !surface->redraw_needed)
4074 /* Whole-window redraw forces a redraw even if the previous has
4075 * not yet hit the screen.
4077 if (surface->frame_cb) {
4078 if (!surface->window->redraw_needed)
4081 DBG_OBJ(surface->frame_cb, "cancelled\n");
4082 wl_callback_destroy(surface->frame_cb);
4085 surface->frame_cb = wl_surface_frame(surface->surface);
4086 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4087 DBG_OBJ(surface->frame_cb, "new\n");
4089 surface->redraw_needed = 0;
4090 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4091 widget_redraw(surface->widget);
4092 DBG_OBJ(surface->surface, "done\n");
4096 idle_redraw(struct task *task, uint32_t events)
4098 struct window *window = container_of(task, struct window, redraw_task);
4099 struct surface *surface;
4101 DBG(" --------- \n");
4103 wl_list_init(&window->redraw_task.link);
4104 window->redraw_task_scheduled = 0;
4106 if (window->resize_needed) {
4107 /* throttle resizing to the main surface display */
4108 if (window->main_surface->frame_cb) {
4109 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4113 idle_resize(window);
4116 wl_list_for_each(surface, &window->subsurface_list, link)
4117 surface_redraw(surface);
4119 window->redraw_needed = 0;
4120 window_flush(window);
4122 wl_list_for_each(surface, &window->subsurface_list, link)
4123 surface_set_synchronized_default(surface);
4127 window_schedule_redraw_task(struct window *window)
4129 if (window->configure_requests)
4131 if (!window->redraw_task_scheduled) {
4132 window->redraw_task.run = idle_redraw;
4133 display_defer(window->display, &window->redraw_task);
4134 window->redraw_task_scheduled = 1;
4139 window_schedule_redraw(struct window *window)
4141 struct surface *surface;
4143 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4145 wl_list_for_each(surface, &window->subsurface_list, link)
4146 surface->redraw_needed = 1;
4148 window_schedule_redraw_task(window);
4152 window_is_fullscreen(struct window *window)
4154 return window->type == TYPE_FULLSCREEN;
4158 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
4160 struct window *window = data;
4162 wl_callback_destroy(callback);
4163 window->configure_requests--;
4165 if (!window->configure_requests)
4166 window_schedule_redraw(window);
4169 static struct wl_callback_listener configure_request_listener = {
4170 configure_request_completed,
4174 window_defer_redraw_until_configure(struct window* window)
4176 struct wl_callback *callback;
4178 if (window->redraw_task_scheduled) {
4179 wl_list_remove(&window->redraw_task.link);
4180 window->redraw_task_scheduled = 0;
4183 callback = wl_display_sync(window->display->display);
4184 wl_callback_add_listener(callback, &configure_request_listener, window);
4185 window->configure_requests++;
4189 window_set_fullscreen(struct window *window, int fullscreen)
4191 if (!window->display->shell)
4194 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
4198 window->saved_type = window->type;
4199 if (window->type == TYPE_TOPLEVEL) {
4200 window->saved_allocation = window->main_surface->allocation;
4202 window->type = TYPE_FULLSCREEN;
4203 wl_shell_surface_set_fullscreen(window->shell_surface,
4204 window->fullscreen_method,
4206 window_defer_redraw_until_configure (window);
4208 if (window->saved_type == TYPE_MAXIMIZED) {
4209 window_set_maximized(window, 1);
4211 window->type = TYPE_TOPLEVEL;
4212 wl_shell_surface_set_toplevel(window->shell_surface);
4213 window_schedule_resize(window,
4214 window->saved_allocation.width,
4215 window->saved_allocation.height);
4222 window_set_fullscreen_method(struct window *window,
4223 enum wl_shell_surface_fullscreen_method method)
4225 window->fullscreen_method = method;
4229 window_is_maximized(struct window *window)
4231 return window->type == TYPE_MAXIMIZED;
4235 window_set_maximized(struct window *window, int maximized)
4237 if (!window->display->shell)
4240 if ((window->type == TYPE_MAXIMIZED) == maximized)
4243 if (window->type == TYPE_TOPLEVEL) {
4244 window->saved_allocation = window->main_surface->allocation;
4245 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4246 window->type = TYPE_MAXIMIZED;
4247 window_defer_redraw_until_configure(window);
4248 } else if (window->type == TYPE_FULLSCREEN) {
4249 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4250 window->type = TYPE_MAXIMIZED;
4251 window_defer_redraw_until_configure(window);
4253 wl_shell_surface_set_toplevel(window->shell_surface);
4254 window->type = TYPE_TOPLEVEL;
4255 window_schedule_resize(window,
4256 window->saved_allocation.width,
4257 window->saved_allocation.height);
4262 window_set_user_data(struct window *window, void *data)
4264 window->user_data = data;
4268 window_get_user_data(struct window *window)
4270 return window->user_data;
4274 window_set_key_handler(struct window *window,
4275 window_key_handler_t handler)
4277 window->key_handler = handler;
4281 window_set_keyboard_focus_handler(struct window *window,
4282 window_keyboard_focus_handler_t handler)
4284 window->keyboard_focus_handler = handler;
4288 window_set_data_handler(struct window *window, window_data_handler_t handler)
4290 window->data_handler = handler;
4294 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4296 window->drop_handler = handler;
4300 window_set_close_handler(struct window *window,
4301 window_close_handler_t handler)
4303 window->close_handler = handler;
4307 window_set_fullscreen_handler(struct window *window,
4308 window_fullscreen_handler_t handler)
4310 window->fullscreen_handler = handler;
4314 window_set_output_handler(struct window *window,
4315 window_output_handler_t handler)
4317 window->output_handler = handler;
4321 window_set_title(struct window *window, const char *title)
4323 free(window->title);
4324 window->title = strdup(title);
4325 if (window->shell_surface)
4326 wl_shell_surface_set_title(window->shell_surface, title);
4330 window_get_title(struct window *window)
4332 return window->title;
4336 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4338 struct text_cursor_position *text_cursor_position =
4339 window->display->text_cursor_position;
4341 if (!text_cursor_position)
4344 text_cursor_position_notify(text_cursor_position,
4345 window->main_surface->surface,
4346 wl_fixed_from_int(x),
4347 wl_fixed_from_int(y));
4351 window_damage(struct window *window, int32_t x, int32_t y,
4352 int32_t width, int32_t height)
4354 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4358 surface_enter(void *data,
4359 struct wl_surface *wl_surface, struct wl_output *wl_output)
4361 struct window *window = data;
4362 struct output *output;
4363 struct output *output_found = NULL;
4364 struct window_output *window_output;
4366 wl_list_for_each(output, &window->display->output_list, link) {
4367 if (output->output == wl_output) {
4368 output_found = output;
4376 window_output = xmalloc(sizeof *window_output);
4377 window_output->output = output_found;
4379 wl_list_insert (&window->window_output_list, &window_output->link);
4381 if (window->output_handler)
4382 window->output_handler(window, output_found, 1,
4387 surface_leave(void *data,
4388 struct wl_surface *wl_surface, struct wl_output *output)
4390 struct window *window = data;
4391 struct window_output *window_output;
4392 struct window_output *window_output_found = NULL;
4394 wl_list_for_each(window_output, &window->window_output_list, link) {
4395 if (window_output->output->output == output) {
4396 window_output_found = window_output;
4401 if (window_output_found) {
4402 wl_list_remove(&window_output_found->link);
4404 if (window->output_handler)
4405 window->output_handler(window, window_output->output,
4406 0, window->user_data);
4408 free(window_output_found);
4412 static const struct wl_surface_listener surface_listener = {
4417 static struct surface *
4418 surface_create(struct window *window)
4420 struct display *display = window->display;
4421 struct surface *surface;
4423 surface = xmalloc(sizeof *surface);
4424 memset(surface, 0, sizeof *surface);
4428 surface->window = window;
4429 surface->surface = wl_compositor_create_surface(display->compositor);
4430 surface->buffer_scale = 1;
4431 wl_surface_add_listener(surface->surface, &surface_listener, window);
4433 wl_list_insert(&window->subsurface_list, &surface->link);
4438 static struct window *
4439 window_create_internal(struct display *display,
4440 struct window *parent, int type)
4442 struct window *window;
4443 struct surface *surface;
4445 window = xzalloc(sizeof *window);
4446 wl_list_init(&window->subsurface_list);
4447 window->display = display;
4448 window->parent = parent;
4450 surface = surface_create(window);
4451 window->main_surface = surface;
4453 if (type != TYPE_CUSTOM && display->shell) {
4454 window->shell_surface =
4455 wl_shell_get_shell_surface(display->shell,
4457 fail_on_null(window->shell_surface);
4460 window->type = type;
4461 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4462 window->configure_requests = 0;
4463 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4465 if (display->argb_device)
4466 #ifdef HAVE_CAIRO_EGL
4467 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4469 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4472 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4474 wl_surface_set_user_data(surface->surface, window);
4475 wl_list_insert(display->window_list.prev, &window->link);
4476 wl_list_init(&window->redraw_task.link);
4478 if (window->shell_surface) {
4479 wl_shell_surface_set_user_data(window->shell_surface, window);
4480 wl_shell_surface_add_listener(window->shell_surface,
4481 &shell_surface_listener, window);
4484 wl_list_init (&window->window_output_list);
4490 window_create(struct display *display)
4492 return window_create_internal(display, NULL, TYPE_NONE);
4496 window_create_custom(struct display *display)
4498 return window_create_internal(display, NULL, TYPE_CUSTOM);
4502 window_create_transient(struct display *display, struct window *parent,
4503 int32_t x, int32_t y, uint32_t flags)
4505 struct window *window;
4507 window = window_create_internal(parent->display,
4508 parent, TYPE_TRANSIENT);
4514 wl_shell_surface_set_transient(
4515 window->shell_surface,
4516 window->parent->main_surface->surface,
4517 window->x, window->y, flags);
4523 menu_set_item(struct menu *menu, int sy)
4527 next = (sy - 8) / 20;
4528 if (menu->current != next) {
4529 menu->current = next;
4530 widget_schedule_redraw(menu->widget);
4535 menu_motion_handler(struct widget *widget,
4536 struct input *input, uint32_t time,
4537 float x, float y, void *data)
4539 struct menu *menu = data;
4541 if (widget == menu->widget)
4542 menu_set_item(data, y);
4544 return CURSOR_LEFT_PTR;
4548 menu_enter_handler(struct widget *widget,
4549 struct input *input, float x, float y, void *data)
4551 struct menu *menu = data;
4553 if (widget == menu->widget)
4554 menu_set_item(data, y);
4556 return CURSOR_LEFT_PTR;
4560 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4562 struct menu *menu = data;
4564 if (widget == menu->widget)
4565 menu_set_item(data, -200);
4569 menu_button_handler(struct widget *widget,
4570 struct input *input, uint32_t time,
4571 uint32_t button, enum wl_pointer_button_state state,
4575 struct menu *menu = data;
4577 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4578 (menu->release_count > 0 || time - menu->time > 500)) {
4579 /* Either relase after press-drag-release or
4580 * click-motion-click. */
4581 menu->func(menu->window->parent,
4582 menu->current, menu->window->parent->user_data);
4583 input_ungrab(input);
4585 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4586 menu->release_count++;
4591 menu_redraw_handler(struct widget *widget, void *data)
4594 const int32_t r = 3, margin = 3;
4595 struct menu *menu = data;
4596 int32_t width, height, i;
4598 cr = widget_cairo_create(widget);
4599 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4600 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4603 width = widget->allocation.width;
4604 height = widget->allocation.height;
4605 rounded_rect(cr, 0, 0, width, height, r);
4607 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
4608 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4611 for (i = 0; i < menu->count; i++) {
4612 if (i == menu->current) {
4613 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4614 cairo_rectangle(cr, margin, i * 20 + margin,
4615 width - 2 * margin, 20);
4617 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4618 cairo_move_to(cr, 10, i * 20 + 16);
4619 cairo_show_text(cr, menu->entries[i]);
4621 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4622 cairo_move_to(cr, 10, i * 20 + 16);
4623 cairo_show_text(cr, menu->entries[i]);
4631 window_show_menu(struct display *display,
4632 struct input *input, uint32_t time, struct window *parent,
4633 int32_t x, int32_t y,
4634 menu_func_t func, const char **entries, int count)
4636 struct window *window;
4638 const int32_t margin = 3;
4640 menu = malloc(sizeof *menu);
4644 window = window_create_internal(parent->display, parent, TYPE_MENU);
4650 menu->window = window;
4651 menu->widget = window_add_widget(menu->window, menu);
4652 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4653 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4654 menu->entries = entries;
4655 menu->count = count;
4656 menu->release_count = 0;
4660 menu->input = input;
4661 window->type = TYPE_MENU;
4665 input_ungrab(input);
4666 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4667 display_get_serial(window->display),
4668 window->parent->main_surface->surface,
4669 window->x, window->y, 0);
4671 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4672 widget_set_enter_handler(menu->widget, menu_enter_handler);
4673 widget_set_leave_handler(menu->widget, menu_leave_handler);
4674 widget_set_motion_handler(menu->widget, menu_motion_handler);
4675 widget_set_button_handler(menu->widget, menu_button_handler);
4677 input_grab(input, menu->widget, 0);
4678 window_schedule_resize(window, 200, count * 20 + margin * 2);
4682 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4684 window->main_surface->buffer_type = type;
4688 window_set_preferred_format(struct window *window,
4689 enum preferred_format format)
4691 window->preferred_format = format;
4695 window_add_subsurface(struct window *window, void *data,
4696 enum subsurface_mode default_mode)
4698 struct widget *widget;
4699 struct surface *surface;
4700 struct wl_surface *parent;
4701 struct wl_subcompositor *subcompo = window->display->subcompositor;
4703 surface = surface_create(window);
4704 widget = widget_create(window, surface, data);
4705 wl_list_init(&widget->link);
4706 surface->widget = widget;
4708 parent = window->main_surface->surface;
4709 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4712 surface->synchronized = 1;
4714 switch (default_mode) {
4715 case SUBSURFACE_SYNCHRONIZED:
4716 surface->synchronized_default = 1;
4718 case SUBSURFACE_DESYNCHRONIZED:
4719 surface->synchronized_default = 0;
4722 assert(!"bad enum subsurface_mode");
4729 display_handle_geometry(void *data,
4730 struct wl_output *wl_output,
4733 int physical_height,
4739 struct output *output = data;
4741 output->allocation.x = x;
4742 output->allocation.y = y;
4743 output->transform = transform;
4747 display_handle_done(void *data,
4748 struct wl_output *wl_output)
4753 display_handle_scale(void *data,
4754 struct wl_output *wl_output,
4757 struct output *output = data;
4759 output->scale = scale;
4763 display_handle_mode(void *data,
4764 struct wl_output *wl_output,
4770 struct output *output = data;
4771 struct display *display = output->display;
4773 if (flags & WL_OUTPUT_MODE_CURRENT) {
4774 output->allocation.width = width;
4775 output->allocation.height = height;
4776 if (display->output_configure_handler)
4777 (*display->output_configure_handler)(
4778 output, display->user_data);
4782 static const struct wl_output_listener output_listener = {
4783 display_handle_geometry,
4784 display_handle_mode,
4785 display_handle_done,
4786 display_handle_scale
4790 display_add_output(struct display *d, uint32_t id)
4792 struct output *output;
4794 output = xzalloc(sizeof *output);
4795 output->display = d;
4798 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4799 wl_list_insert(d->output_list.prev, &output->link);
4801 wl_output_add_listener(output->output, &output_listener, output);
4805 output_destroy(struct output *output)
4807 if (output->destroy_handler)
4808 (*output->destroy_handler)(output, output->user_data);
4810 wl_output_destroy(output->output);
4811 wl_list_remove(&output->link);
4816 display_set_global_handler(struct display *display,
4817 display_global_handler_t handler)
4819 struct global *global;
4821 display->global_handler = handler;
4825 wl_list_for_each(global, &display->global_list, link)
4826 display->global_handler(display,
4827 global->name, global->interface,
4828 global->version, display->user_data);
4832 display_set_output_configure_handler(struct display *display,
4833 display_output_handler_t handler)
4835 struct output *output;
4837 display->output_configure_handler = handler;
4841 wl_list_for_each(output, &display->output_list, link) {
4842 if (output->allocation.width == 0 &&
4843 output->allocation.height == 0)
4846 (*display->output_configure_handler)(output,
4847 display->user_data);
4852 output_set_user_data(struct output *output, void *data)
4854 output->user_data = data;
4858 output_get_user_data(struct output *output)
4860 return output->user_data;
4864 output_set_destroy_handler(struct output *output,
4865 display_output_handler_t handler)
4867 output->destroy_handler = handler;
4868 /* FIXME: implement this, once we have way to remove outputs */
4872 output_get_allocation(struct output *output, struct rectangle *base)
4874 struct rectangle allocation = output->allocation;
4876 switch (output->transform) {
4877 case WL_OUTPUT_TRANSFORM_90:
4878 case WL_OUTPUT_TRANSFORM_270:
4879 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4880 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4881 /* Swap width and height */
4882 allocation.width = output->allocation.height;
4883 allocation.height = output->allocation.width;
4891 output_get_wl_output(struct output *output)
4893 return output->output;
4896 enum wl_output_transform
4897 output_get_transform(struct output *output)
4899 return output->transform;
4903 output_get_scale(struct output *output)
4905 return output->scale;
4909 fini_xkb(struct input *input)
4911 xkb_state_unref(input->xkb.state);
4912 xkb_map_unref(input->xkb.keymap);
4916 display_add_input(struct display *d, uint32_t id)
4918 struct input *input;
4920 input = xzalloc(sizeof *input);
4922 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
4923 input->pointer_focus = NULL;
4924 input->keyboard_focus = NULL;
4925 wl_list_init(&input->touch_point_list);
4926 wl_list_insert(d->input_list.prev, &input->link);
4928 wl_seat_add_listener(input->seat, &seat_listener, input);
4929 wl_seat_set_user_data(input->seat, input);
4931 input->data_device =
4932 wl_data_device_manager_get_data_device(d->data_device_manager,
4934 wl_data_device_add_listener(input->data_device, &data_device_listener,
4937 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4939 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4940 TFD_CLOEXEC | TFD_NONBLOCK);
4941 input->repeat_task.run = keyboard_repeat_func;
4942 display_watch_fd(d, input->repeat_timer_fd,
4943 EPOLLIN, &input->repeat_task);
4947 input_destroy(struct input *input)
4949 input_remove_keyboard_focus(input);
4950 input_remove_pointer_focus(input);
4952 if (input->drag_offer)
4953 data_offer_destroy(input->drag_offer);
4955 if (input->selection_offer)
4956 data_offer_destroy(input->selection_offer);
4958 wl_data_device_destroy(input->data_device);
4961 wl_surface_destroy(input->pointer_surface);
4963 wl_list_remove(&input->link);
4964 wl_seat_destroy(input->seat);
4965 close(input->repeat_timer_fd);
4970 init_workspace_manager(struct display *d, uint32_t id)
4972 d->workspace_manager =
4973 wl_registry_bind(d->registry, id,
4974 &workspace_manager_interface, 1);
4975 if (d->workspace_manager != NULL)
4976 workspace_manager_add_listener(d->workspace_manager,
4977 &workspace_manager_listener,
4982 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4984 struct display *d = data;
4986 if (format == WL_SHM_FORMAT_RGB565)
4990 struct wl_shm_listener shm_listener = {
4995 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4996 const char *interface, uint32_t version)
4998 struct display *d = data;
4999 struct global *global;
5001 global = xmalloc(sizeof *global);
5003 global->interface = strdup(interface);
5004 global->version = version;
5005 wl_list_insert(d->global_list.prev, &global->link);
5007 if (strcmp(interface, "wl_compositor") == 0) {
5008 d->compositor = wl_registry_bind(registry, id,
5009 &wl_compositor_interface, 3);
5010 } else if (strcmp(interface, "wl_output") == 0) {
5011 display_add_output(d, id);
5012 } else if (strcmp(interface, "wl_seat") == 0) {
5013 display_add_input(d, id);
5014 } else if (strcmp(interface, "wl_shell") == 0) {
5015 d->shell = wl_registry_bind(registry,
5016 id, &wl_shell_interface, 1);
5017 } else if (strcmp(interface, "wl_shm") == 0) {
5018 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5019 wl_shm_add_listener(d->shm, &shm_listener, d);
5020 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5021 d->data_device_manager =
5022 wl_registry_bind(registry, id,
5023 &wl_data_device_manager_interface, 1);
5024 } else if (strcmp(interface, "text_cursor_position") == 0) {
5025 d->text_cursor_position =
5026 wl_registry_bind(registry, id,
5027 &text_cursor_position_interface, 1);
5028 } else if (strcmp(interface, "workspace_manager") == 0) {
5029 init_workspace_manager(d, id);
5030 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5032 wl_registry_bind(registry, id,
5033 &wl_subcompositor_interface, 1);
5036 if (d->global_handler)
5037 d->global_handler(d, id, interface, version, d->user_data);
5041 registry_handle_global_remove(void *data, struct wl_registry *registry,
5044 struct display *d = data;
5045 struct global *global;
5048 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5049 if (global->name != name)
5052 /* XXX: Should destroy bound globals, and call
5053 * the counterpart of display::global_handler
5055 wl_list_remove(&global->link);
5056 free(global->interface);
5062 display_bind(struct display *display, uint32_t name,
5063 const struct wl_interface *interface, uint32_t version)
5065 return wl_registry_bind(display->registry, name, interface, version);
5068 static const struct wl_registry_listener registry_listener = {
5069 registry_handle_global,
5070 registry_handle_global_remove
5073 #ifdef HAVE_CAIRO_EGL
5075 init_egl(struct display *d)
5077 EGLint major, minor;
5080 #ifdef USE_CAIRO_GLESV2
5081 # define GL_BIT EGL_OPENGL_ES2_BIT
5083 # define GL_BIT EGL_OPENGL_BIT
5086 static const EGLint argb_cfg_attribs[] = {
5087 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5093 EGL_RENDERABLE_TYPE, GL_BIT,
5097 #ifdef USE_CAIRO_GLESV2
5098 static const EGLint context_attribs[] = {
5099 EGL_CONTEXT_CLIENT_VERSION, 2,
5102 EGLint api = EGL_OPENGL_ES_API;
5104 EGLint *context_attribs = NULL;
5105 EGLint api = EGL_OPENGL_API;
5108 d->dpy = eglGetDisplay(d->display);
5109 if (!eglInitialize(d->dpy, &major, &minor)) {
5110 fprintf(stderr, "failed to initialize EGL\n");
5114 if (!eglBindAPI(api)) {
5115 fprintf(stderr, "failed to bind EGL client API\n");
5119 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5120 &d->argb_config, 1, &n) || n != 1) {
5121 fprintf(stderr, "failed to choose argb EGL config\n");
5125 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5126 EGL_NO_CONTEXT, context_attribs);
5127 if (d->argb_ctx == NULL) {
5128 fprintf(stderr, "failed to create EGL context\n");
5132 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5133 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5134 fprintf(stderr, "failed to get cairo EGL argb device\n");
5142 fini_egl(struct display *display)
5144 cairo_device_destroy(display->argb_device);
5146 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5149 eglTerminate(display->dpy);
5155 init_dummy_surface(struct display *display)
5160 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5162 display->dummy_surface =
5163 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5165 display->dummy_surface_data = data;
5169 handle_display_data(struct task *task, uint32_t events)
5171 struct display *display =
5172 container_of(task, struct display, display_task);
5173 struct epoll_event ep;
5176 display->display_fd_events = events;
5178 if (events & EPOLLERR || events & EPOLLHUP) {
5179 display_exit(display);
5183 if (events & EPOLLIN) {
5184 ret = wl_display_dispatch(display->display);
5186 display_exit(display);
5191 if (events & EPOLLOUT) {
5192 ret = wl_display_flush(display->display);
5194 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5195 ep.data.ptr = &display->display_task;
5196 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5197 display->display_fd, &ep);
5198 } else if (ret == -1 && errno != EAGAIN) {
5199 display_exit(display);
5206 log_handler(const char *format, va_list args)
5208 vfprintf(stderr, format, args);
5212 display_create(int *argc, char *argv[])
5216 wl_log_set_handler_client(log_handler);
5218 d = zalloc(sizeof *d);
5222 d->display = wl_display_connect(NULL);
5223 if (d->display == NULL) {
5224 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5229 d->xkb_context = xkb_context_new(0);
5230 if (d->xkb_context == NULL) {
5231 fprintf(stderr, "Failed to create XKB context\n");
5236 d->epoll_fd = os_epoll_create_cloexec();
5237 d->display_fd = wl_display_get_fd(d->display);
5238 d->display_task.run = handle_display_data;
5239 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5242 wl_list_init(&d->deferred_list);
5243 wl_list_init(&d->input_list);
5244 wl_list_init(&d->output_list);
5245 wl_list_init(&d->global_list);
5248 d->workspace_count = 1;
5250 d->registry = wl_display_get_registry(d->display);
5251 wl_registry_add_listener(d->registry, ®istry_listener, d);
5253 if (wl_display_dispatch(d->display) < 0) {
5254 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5258 #ifdef HAVE_CAIRO_EGL
5259 if (init_egl(d) < 0)
5260 fprintf(stderr, "EGL does not seem to work, "
5261 "falling back to software rendering and wl_shm.\n");
5266 d->theme = theme_create();
5268 wl_list_init(&d->window_list);
5270 init_dummy_surface(d);
5276 display_destroy_outputs(struct display *display)
5279 struct output *output;
5281 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5282 output_destroy(output);
5286 display_destroy_inputs(struct display *display)
5289 struct input *input;
5291 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5292 input_destroy(input);
5296 display_destroy(struct display *display)
5298 if (!wl_list_empty(&display->window_list))
5299 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5300 wl_list_length(&display->window_list));
5302 if (!wl_list_empty(&display->deferred_list))
5303 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5305 cairo_surface_destroy(display->dummy_surface);
5306 free(display->dummy_surface_data);
5308 display_destroy_outputs(display);
5309 display_destroy_inputs(display);
5311 xkb_context_unref(display->xkb_context);
5313 theme_destroy(display->theme);
5314 destroy_cursors(display);
5316 #ifdef HAVE_CAIRO_EGL
5317 if (display->argb_device)
5321 if (display->subcompositor)
5322 wl_subcompositor_destroy(display->subcompositor);
5325 wl_shell_destroy(display->shell);
5328 wl_shm_destroy(display->shm);
5330 if (display->data_device_manager)
5331 wl_data_device_manager_destroy(display->data_device_manager);
5333 wl_compositor_destroy(display->compositor);
5334 wl_registry_destroy(display->registry);
5336 close(display->epoll_fd);
5338 if (!(display->display_fd_events & EPOLLERR) &&
5339 !(display->display_fd_events & EPOLLHUP))
5340 wl_display_flush(display->display);
5342 wl_display_disconnect(display->display);
5347 display_set_user_data(struct display *display, void *data)
5349 display->user_data = data;
5353 display_get_user_data(struct display *display)
5355 return display->user_data;
5359 display_get_display(struct display *display)
5361 return display->display;
5365 display_has_subcompositor(struct display *display)
5367 if (display->subcompositor)
5370 wl_display_roundtrip(display->display);
5372 return display->subcompositor != NULL;
5376 display_get_cairo_device(struct display *display)
5378 return display->argb_device;
5382 display_get_output(struct display *display)
5384 return container_of(display->output_list.next, struct output, link);
5387 struct wl_compositor *
5388 display_get_compositor(struct display *display)
5390 return display->compositor;
5394 display_get_serial(struct display *display)
5396 return display->serial;
5400 display_get_egl_display(struct display *d)
5405 struct wl_data_source *
5406 display_create_data_source(struct display *display)
5408 return wl_data_device_manager_create_data_source(display->data_device_manager);
5412 display_get_argb_egl_config(struct display *d)
5414 return d->argb_config;
5418 display_get_shell(struct display *display)
5420 return display->shell;
5424 display_acquire_window_surface(struct display *display,
5425 struct window *window,
5428 struct surface *surface = window->main_surface;
5430 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5433 widget_get_cairo_surface(window->main_surface->widget);
5434 return surface->toysurface->acquire(surface->toysurface, ctx);
5438 display_release_window_surface(struct display *display,
5439 struct window *window)
5441 struct surface *surface = window->main_surface;
5443 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5446 surface->toysurface->release(surface->toysurface);
5450 display_defer(struct display *display, struct task *task)
5452 wl_list_insert(&display->deferred_list, &task->link);
5456 display_watch_fd(struct display *display,
5457 int fd, uint32_t events, struct task *task)
5459 struct epoll_event ep;
5463 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5467 display_unwatch_fd(struct display *display, int fd)
5469 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5473 display_run(struct display *display)
5476 struct epoll_event ep[16];
5479 display->running = 1;
5481 while (!wl_list_empty(&display->deferred_list)) {
5482 task = container_of(display->deferred_list.prev,
5484 wl_list_remove(&task->link);
5488 wl_display_dispatch_pending(display->display);
5490 if (!display->running)
5493 ret = wl_display_flush(display->display);
5494 if (ret < 0 && errno == EAGAIN) {
5496 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5497 ep[0].data.ptr = &display->display_task;
5499 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5500 display->display_fd, &ep[0]);
5501 } else if (ret < 0) {
5505 count = epoll_wait(display->epoll_fd,
5506 ep, ARRAY_LENGTH(ep), -1);
5507 for (i = 0; i < count; i++) {
5508 task = ep[i].data.ptr;
5509 task->run(task, ep[i].events);
5515 display_exit(struct display *display)
5517 display->running = 0;
5521 keysym_modifiers_add(struct wl_array *modifiers_map,
5524 size_t len = strlen(name) + 1;
5527 p = wl_array_add(modifiers_map, len);
5532 strncpy(p, name, len);
5535 static xkb_mod_index_t
5536 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5539 xkb_mod_index_t index = 0;
5540 char *p = modifiers_map->data;
5542 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5543 if (strcmp(p, name) == 0)
5550 return XKB_MOD_INVALID;
5554 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5557 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5559 if (index == XKB_MOD_INVALID)
5560 return XKB_MOD_INVALID;
5566 fail_on_null(void *p)
5569 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5579 return fail_on_null(malloc(s));
5585 return fail_on_null(zalloc(s));
5589 xstrdup(const char *s)
5591 return fail_on_null(strdup(s));