2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_subcompositor *subcompositor;
88 struct wl_shell *shell;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
94 EGLConfig argb_config;
96 cairo_device_t *argb_device;
100 uint32_t display_fd_events;
101 struct task display_task;
104 struct wl_list deferred_list;
108 struct wl_list global_list;
109 struct wl_list window_list;
110 struct wl_list input_list;
111 struct wl_list output_list;
115 struct wl_cursor_theme *cursor_theme;
116 struct wl_cursor **cursors;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
123 struct xkb_context *xkb_context;
126 uint32_t workspace_count;
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
146 struct window_output {
147 struct output *output;
153 * Prepare the surface for drawing. Makes sure there is a surface
154 * of the right size available for rendering, and returns it.
155 * dx,dy are the x,y of wl_surface.attach.
156 * width,height are the new buffer size.
157 * If flags has SURFACE_HINT_RESIZE set, the user is
158 * doing continuous resizing.
159 * Returns the Cairo surface to draw to.
161 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
162 int32_t width, int32_t height, uint32_t flags,
163 enum wl_output_transform buffer_transform, int32_t buffer_scale);
166 * Post the surface to the server, returning the server allocation
167 * rectangle. The Cairo surface from prepare() must be destroyed
168 * after calling this.
170 void (*swap)(struct toysurface *base,
171 enum wl_output_transform buffer_transform, int32_t buffer_scale,
172 struct rectangle *server_allocation);
175 * Make the toysurface current with the given EGL context.
176 * Returns 0 on success, and negative of failure.
178 int (*acquire)(struct toysurface *base, EGLContext ctx);
181 * Release the toysurface from the EGL context, returning control
184 void (*release)(struct toysurface *base);
187 * Destroy the toysurface, including the Cairo surface, any
188 * backing storage, and the Wayland protocol objects.
190 void (*destroy)(struct toysurface *base);
194 struct window *window;
196 struct wl_surface *surface;
197 struct wl_subsurface *subsurface;
199 int synchronized_default;
200 struct toysurface *toysurface;
201 struct widget *widget;
203 struct wl_callback *frame_cb;
206 struct rectangle allocation;
207 struct rectangle server_allocation;
209 struct wl_region *input_region;
210 struct wl_region *opaque_region;
212 enum window_buffer_type buffer_type;
213 enum wl_output_transform buffer_transform;
214 int32_t buffer_scale;
216 cairo_surface_t *cairo_surface;
222 struct display *display;
223 struct window *parent;
224 struct wl_list window_output_list;
226 struct rectangle saved_allocation;
227 struct rectangle min_allocation;
228 struct rectangle pending_allocation;
232 int redraw_task_scheduled;
233 struct task redraw_task;
240 int fullscreen_method;
241 int configure_requests;
243 enum preferred_format preferred_format;
245 window_key_handler_t key_handler;
246 window_keyboard_focus_handler_t keyboard_focus_handler;
247 window_data_handler_t data_handler;
248 window_drop_handler_t drop_handler;
249 window_close_handler_t close_handler;
250 window_fullscreen_handler_t fullscreen_handler;
251 window_output_handler_t output_handler;
253 struct surface *main_surface;
254 struct wl_shell_surface *shell_surface;
256 struct window_frame *frame;
258 /* struct surface::link, contains also main_surface */
259 struct wl_list subsurface_list;
266 struct window *window;
267 struct surface *surface;
268 struct tooltip *tooltip;
269 struct wl_list child_list;
271 struct rectangle allocation;
272 widget_resize_handler_t resize_handler;
273 widget_redraw_handler_t redraw_handler;
274 widget_enter_handler_t enter_handler;
275 widget_leave_handler_t leave_handler;
276 widget_motion_handler_t motion_handler;
277 widget_button_handler_t button_handler;
278 widget_touch_down_handler_t touch_down_handler;
279 widget_touch_up_handler_t touch_up_handler;
280 widget_touch_motion_handler_t touch_motion_handler;
281 widget_touch_frame_handler_t touch_frame_handler;
282 widget_touch_cancel_handler_t touch_cancel_handler;
283 widget_axis_handler_t axis_handler;
292 struct widget *widget;
297 struct display *display;
298 struct wl_seat *seat;
299 struct wl_pointer *pointer;
300 struct wl_keyboard *keyboard;
301 struct wl_touch *touch;
302 struct wl_list touch_point_list;
303 struct window *pointer_focus;
304 struct window *keyboard_focus;
305 struct window *touch_focus;
307 uint32_t cursor_anim_start;
308 struct wl_callback *cursor_frame_cb;
309 struct wl_surface *pointer_surface;
311 uint32_t pointer_enter_serial;
312 uint32_t cursor_serial;
316 struct widget *focus_widget;
318 uint32_t grab_button;
320 struct wl_data_device *data_device;
321 struct data_offer *drag_offer;
322 struct data_offer *selection_offer;
325 struct xkb_keymap *keymap;
326 struct xkb_state *state;
327 xkb_mod_mask_t control_mask;
328 xkb_mod_mask_t alt_mask;
329 xkb_mod_mask_t shift_mask;
332 struct task repeat_task;
336 uint32_t repeat_time;
340 struct display *display;
341 struct wl_output *output;
342 struct rectangle allocation;
347 display_output_handler_t destroy_handler;
351 struct window_frame {
352 struct widget *widget;
353 struct widget *child;
358 struct window *window;
359 struct widget *widget;
361 const char **entries;
370 struct widget *parent;
371 struct window *window;
372 struct widget *widget;
374 struct task tooltip_task;
380 struct wl_shm_pool *pool;
387 CURSOR_DEFAULT = 100,
391 enum window_location {
393 WINDOW_RESIZING_TOP = 1,
394 WINDOW_RESIZING_BOTTOM = 2,
395 WINDOW_RESIZING_LEFT = 4,
396 WINDOW_RESIZING_TOP_LEFT = 5,
397 WINDOW_RESIZING_BOTTOM_LEFT = 6,
398 WINDOW_RESIZING_RIGHT = 8,
399 WINDOW_RESIZING_TOP_RIGHT = 9,
400 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
401 WINDOW_RESIZING_MASK = 15,
402 WINDOW_EXTERIOR = 16,
403 WINDOW_TITLEBAR = 17,
404 WINDOW_CLIENT_AREA = 18,
407 static const cairo_user_data_key_t shm_surface_data_key;
414 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
415 __attribute__ ((format (printf, 4, 5)));
418 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
423 gettimeofday(&tv, NULL);
424 fprintf(stderr, "%8ld.%03ld ",
425 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
428 fprintf(stderr, "%s@%d ",
429 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
431 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
432 fprintf(stderr, "%s ", func);
435 vfprintf(stderr, fmt, ap);
439 #define DBG(fmt, ...) \
440 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
442 #define DBG_OBJ(obj, fmt, ...) \
443 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
447 #define DBG(...) do {} while (0)
448 #define DBG_OBJ(...) do {} while (0)
453 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
457 switch (buffer_transform) {
458 case WL_OUTPUT_TRANSFORM_90:
459 case WL_OUTPUT_TRANSFORM_270:
460 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
461 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
470 *width *= buffer_scale;
471 *height *= buffer_scale;
475 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
479 switch (buffer_transform) {
480 case WL_OUTPUT_TRANSFORM_90:
481 case WL_OUTPUT_TRANSFORM_270:
482 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
483 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
492 *width /= buffer_scale;
493 *height /= buffer_scale;
496 #ifdef HAVE_CAIRO_EGL
498 struct egl_window_surface {
499 struct toysurface base;
500 cairo_surface_t *cairo_surface;
501 struct display *display;
502 struct wl_surface *surface;
503 struct wl_egl_window *egl_window;
504 EGLSurface egl_surface;
507 static struct egl_window_surface *
508 to_egl_window_surface(struct toysurface *base)
510 return container_of(base, struct egl_window_surface, base);
513 static cairo_surface_t *
514 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
515 int32_t width, int32_t height, uint32_t flags,
516 enum wl_output_transform buffer_transform, int32_t buffer_scale)
518 struct egl_window_surface *surface = to_egl_window_surface(base);
520 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
522 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
523 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
525 return cairo_surface_reference(surface->cairo_surface);
529 egl_window_surface_swap(struct toysurface *base,
530 enum wl_output_transform buffer_transform, int32_t buffer_scale,
531 struct rectangle *server_allocation)
533 struct egl_window_surface *surface = to_egl_window_surface(base);
535 cairo_gl_surface_swapbuffers(surface->cairo_surface);
536 wl_egl_window_get_attached_size(surface->egl_window,
537 &server_allocation->width,
538 &server_allocation->height);
540 buffer_to_surface_size (buffer_transform, buffer_scale,
541 &server_allocation->width,
542 &server_allocation->height);
546 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
548 struct egl_window_surface *surface = to_egl_window_surface(base);
549 cairo_device_t *device;
551 device = cairo_surface_get_device(surface->cairo_surface);
556 if (device == surface->display->argb_device)
557 ctx = surface->display->argb_ctx;
562 cairo_device_flush(device);
563 cairo_device_acquire(device);
564 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
565 surface->egl_surface, ctx))
566 fprintf(stderr, "failed to make surface current\n");
572 egl_window_surface_release(struct toysurface *base)
574 struct egl_window_surface *surface = to_egl_window_surface(base);
575 cairo_device_t *device;
577 device = cairo_surface_get_device(surface->cairo_surface);
581 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
582 surface->display->argb_ctx))
583 fprintf(stderr, "failed to make context current\n");
585 cairo_device_release(device);
589 egl_window_surface_destroy(struct toysurface *base)
591 struct egl_window_surface *surface = to_egl_window_surface(base);
592 struct display *d = surface->display;
594 cairo_surface_destroy(surface->cairo_surface);
595 eglDestroySurface(d->dpy, surface->egl_surface);
596 wl_egl_window_destroy(surface->egl_window);
597 surface->surface = NULL;
602 static struct toysurface *
603 egl_window_surface_create(struct display *display,
604 struct wl_surface *wl_surface,
606 struct rectangle *rectangle)
608 struct egl_window_surface *surface;
610 if (display->dpy == EGL_NO_DISPLAY)
613 surface = calloc(1, sizeof *surface);
617 surface->base.prepare = egl_window_surface_prepare;
618 surface->base.swap = egl_window_surface_swap;
619 surface->base.acquire = egl_window_surface_acquire;
620 surface->base.release = egl_window_surface_release;
621 surface->base.destroy = egl_window_surface_destroy;
623 surface->display = display;
624 surface->surface = wl_surface;
626 surface->egl_window = wl_egl_window_create(surface->surface,
630 surface->egl_surface = eglCreateWindowSurface(display->dpy,
631 display->argb_config,
635 surface->cairo_surface =
636 cairo_gl_surface_create_for_egl(display->argb_device,
637 surface->egl_surface,
641 return &surface->base;
646 static struct toysurface *
647 egl_window_surface_create(struct display *display,
648 struct wl_surface *wl_surface,
650 struct rectangle *rectangle)
657 struct shm_surface_data {
658 struct wl_buffer *buffer;
659 struct shm_pool *pool;
663 display_get_buffer_for_surface(struct display *display,
664 cairo_surface_t *surface)
666 struct shm_surface_data *data;
668 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
674 shm_pool_destroy(struct shm_pool *pool);
677 shm_surface_data_destroy(void *p)
679 struct shm_surface_data *data = p;
681 wl_buffer_destroy(data->buffer);
683 shm_pool_destroy(data->pool);
688 static struct wl_shm_pool *
689 make_shm_pool(struct display *display, int size, void **data)
691 struct wl_shm_pool *pool;
694 fd = os_create_anonymous_file(size);
696 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
701 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
702 if (*data == MAP_FAILED) {
703 fprintf(stderr, "mmap failed: %m\n");
708 pool = wl_shm_create_pool(display->shm, fd, size);
715 static struct shm_pool *
716 shm_pool_create(struct display *display, size_t size)
718 struct shm_pool *pool = malloc(sizeof *pool);
723 pool->pool = make_shm_pool(display, size, &pool->data);
736 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
738 if (pool->used + size > pool->size)
741 *offset = pool->used;
744 return (char *) pool->data + *offset;
747 /* destroy the pool. this does not unmap the memory though */
749 shm_pool_destroy(struct shm_pool *pool)
751 munmap(pool->data, pool->size);
752 wl_shm_pool_destroy(pool->pool);
756 /* Start allocating from the beginning of the pool again */
758 shm_pool_reset(struct shm_pool *pool)
764 data_length_for_shm_surface(struct rectangle *rect)
768 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
770 return stride * rect->height;
773 static cairo_surface_t *
774 display_create_shm_surface_from_pool(struct display *display,
775 struct rectangle *rectangle,
776 uint32_t flags, struct shm_pool *pool)
778 struct shm_surface_data *data;
780 cairo_surface_t *surface;
781 cairo_format_t cairo_format;
782 int stride, length, offset;
785 data = malloc(sizeof *data);
789 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
790 cairo_format = CAIRO_FORMAT_RGB16_565;
792 cairo_format = CAIRO_FORMAT_ARGB32;
794 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
795 length = stride * rectangle->height;
797 map = shm_pool_allocate(pool, length, &offset);
804 surface = cairo_image_surface_create_for_data (map,
810 cairo_surface_set_user_data(surface, &shm_surface_data_key,
811 data, shm_surface_data_destroy);
813 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
814 format = WL_SHM_FORMAT_RGB565;
816 if (flags & SURFACE_OPAQUE)
817 format = WL_SHM_FORMAT_XRGB8888;
819 format = WL_SHM_FORMAT_ARGB8888;
822 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
830 static cairo_surface_t *
831 display_create_shm_surface(struct display *display,
832 struct rectangle *rectangle, uint32_t flags,
833 struct shm_pool *alternate_pool,
834 struct shm_surface_data **data_ret)
836 struct shm_surface_data *data;
837 struct shm_pool *pool;
838 cairo_surface_t *surface;
840 if (alternate_pool) {
841 shm_pool_reset(alternate_pool);
842 surface = display_create_shm_surface_from_pool(display,
847 data = cairo_surface_get_user_data(surface,
848 &shm_surface_data_key);
853 pool = shm_pool_create(display,
854 data_length_for_shm_surface(rectangle));
859 display_create_shm_surface_from_pool(display, rectangle,
863 shm_pool_destroy(pool);
867 /* make sure we destroy the pool when the surface is destroyed */
868 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
879 check_size(struct rectangle *rect)
881 if (rect->width && rect->height)
884 fprintf(stderr, "tried to create surface of "
885 "width: %d, height: %d\n", rect->width, rect->height);
890 display_create_surface(struct display *display,
891 struct wl_surface *surface,
892 struct rectangle *rectangle,
895 if (check_size(rectangle) < 0)
898 assert(flags & SURFACE_SHM);
899 return display_create_shm_surface(display, rectangle, flags,
903 struct shm_surface_leaf {
904 cairo_surface_t *cairo_surface;
905 /* 'data' is automatically destroyed, when 'cairo_surface' is */
906 struct shm_surface_data *data;
908 struct shm_pool *resize_pool;
913 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
915 if (leaf->cairo_surface)
916 cairo_surface_destroy(leaf->cairo_surface);
917 /* leaf->data already destroyed via cairo private */
919 if (leaf->resize_pool)
920 shm_pool_destroy(leaf->resize_pool);
922 memset(leaf, 0, sizeof *leaf);
928 struct toysurface base;
929 struct display *display;
930 struct wl_surface *surface;
934 struct shm_surface_leaf leaf[MAX_LEAVES];
935 struct shm_surface_leaf *current;
938 static struct shm_surface *
939 to_shm_surface(struct toysurface *base)
941 return container_of(base, struct shm_surface, base);
945 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
948 struct shm_surface_leaf *leaf;
949 char bufs[MAX_LEAVES + 1];
952 for (i = 0; i < MAX_LEAVES; i++) {
953 leaf = &surface->leaf[i];
957 else if (leaf->cairo_surface)
963 bufs[MAX_LEAVES] = '\0';
964 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
969 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
971 struct shm_surface *surface = data;
972 struct shm_surface_leaf *leaf;
976 shm_surface_buffer_state_debug(surface, "buffer_release before");
978 for (i = 0; i < MAX_LEAVES; i++) {
979 leaf = &surface->leaf[i];
980 if (leaf->data && leaf->data->buffer == buffer) {
985 assert(i < MAX_LEAVES && "unknown buffer released");
987 /* Leave one free leaf with storage, release others */
989 for (i = 0; i < MAX_LEAVES; i++) {
990 leaf = &surface->leaf[i];
992 if (!leaf->cairo_surface || leaf->busy)
998 shm_surface_leaf_release(leaf);
1001 shm_surface_buffer_state_debug(surface, "buffer_release after");
1004 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1005 shm_surface_buffer_release
1008 static cairo_surface_t *
1009 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1010 int32_t width, int32_t height, uint32_t flags,
1011 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1013 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1014 struct shm_surface *surface = to_shm_surface(base);
1015 struct rectangle rect = { 0};
1016 struct shm_surface_leaf *leaf = NULL;
1022 /* pick a free buffer, preferrably one that already has storage */
1023 for (i = 0; i < MAX_LEAVES; i++) {
1024 if (surface->leaf[i].busy)
1027 if (!leaf || surface->leaf[i].cairo_surface)
1028 leaf = &surface->leaf[i];
1030 DBG_OBJ(surface->surface, "pick leaf %d\n",
1031 (int)(leaf - &surface->leaf[0]));
1034 fprintf(stderr, "%s: all buffers are held by the server.\n",
1040 if (!resize_hint && leaf->resize_pool) {
1041 cairo_surface_destroy(leaf->cairo_surface);
1042 leaf->cairo_surface = NULL;
1043 shm_pool_destroy(leaf->resize_pool);
1044 leaf->resize_pool = NULL;
1047 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1049 if (leaf->cairo_surface &&
1050 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1051 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1054 if (leaf->cairo_surface)
1055 cairo_surface_destroy(leaf->cairo_surface);
1057 #ifdef USE_RESIZE_POOL
1058 if (resize_hint && !leaf->resize_pool) {
1059 /* Create a big pool to allocate from, while continuously
1060 * resizing. Mmapping a new pool in the server
1061 * is relatively expensive, so reusing a pool performs
1062 * better, but may temporarily reserve unneeded memory.
1064 /* We should probably base this number on the output size. */
1065 leaf->resize_pool = shm_pool_create(surface->display,
1071 rect.height = height;
1073 leaf->cairo_surface =
1074 display_create_shm_surface(surface->display, &rect,
1078 wl_buffer_add_listener(leaf->data->buffer,
1079 &shm_surface_buffer_listener, surface);
1082 surface->current = leaf;
1084 return cairo_surface_reference(leaf->cairo_surface);
1088 shm_surface_swap(struct toysurface *base,
1089 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1090 struct rectangle *server_allocation)
1092 struct shm_surface *surface = to_shm_surface(base);
1093 struct shm_surface_leaf *leaf = surface->current;
1095 server_allocation->width =
1096 cairo_image_surface_get_width(leaf->cairo_surface);
1097 server_allocation->height =
1098 cairo_image_surface_get_height(leaf->cairo_surface);
1100 buffer_to_surface_size (buffer_transform, buffer_scale,
1101 &server_allocation->width,
1102 &server_allocation->height);
1104 wl_surface_attach(surface->surface, leaf->data->buffer,
1105 surface->dx, surface->dy);
1106 wl_surface_damage(surface->surface, 0, 0,
1107 server_allocation->width, server_allocation->height);
1108 wl_surface_commit(surface->surface);
1110 DBG_OBJ(surface->surface, "leaf %d busy\n",
1111 (int)(leaf - &surface->leaf[0]));
1114 surface->current = NULL;
1118 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1124 shm_surface_release(struct toysurface *base)
1129 shm_surface_destroy(struct toysurface *base)
1131 struct shm_surface *surface = to_shm_surface(base);
1134 for (i = 0; i < MAX_LEAVES; i++)
1135 shm_surface_leaf_release(&surface->leaf[i]);
1140 static struct toysurface *
1141 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1142 uint32_t flags, struct rectangle *rectangle)
1144 struct shm_surface *surface;
1145 DBG_OBJ(wl_surface, "\n");
1147 surface = xmalloc(sizeof *surface);
1148 memset(surface, 0, sizeof *surface);
1153 surface->base.prepare = shm_surface_prepare;
1154 surface->base.swap = shm_surface_swap;
1155 surface->base.acquire = shm_surface_acquire;
1156 surface->base.release = shm_surface_release;
1157 surface->base.destroy = shm_surface_destroy;
1159 surface->display = display;
1160 surface->surface = wl_surface;
1161 surface->flags = flags;
1163 return &surface->base;
1167 * The following correspondences between file names and cursors was copied
1168 * from: https://bugs.kde.org/attachment.cgi?id=67313
1171 static const char *bottom_left_corners[] = {
1172 "bottom_left_corner",
1177 static const char *bottom_right_corners[] = {
1178 "bottom_right_corner",
1183 static const char *bottom_sides[] = {
1189 static const char *grabbings[] = {
1192 "208530c400c041818281048008011002"
1195 static const char *left_ptrs[] = {
1202 static const char *left_sides[] = {
1208 static const char *right_sides[] = {
1214 static const char *top_left_corners[] = {
1220 static const char *top_right_corners[] = {
1226 static const char *top_sides[] = {
1232 static const char *xterms[] = {
1238 static const char *hand1s[] = {
1242 "e29285e634086352946a0e7090d73106"
1245 static const char *watches[] = {
1248 "0426c94ea35c87780ff01dc239897213"
1251 struct cursor_alternatives {
1256 static const struct cursor_alternatives cursors[] = {
1257 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1258 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1259 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1260 {grabbings, ARRAY_LENGTH(grabbings)},
1261 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1262 {left_sides, ARRAY_LENGTH(left_sides)},
1263 {right_sides, ARRAY_LENGTH(right_sides)},
1264 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1265 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1266 {top_sides, ARRAY_LENGTH(top_sides)},
1267 {xterms, ARRAY_LENGTH(xterms)},
1268 {hand1s, ARRAY_LENGTH(hand1s)},
1269 {watches, ARRAY_LENGTH(watches)},
1273 create_cursors(struct display *display)
1275 struct weston_config *config;
1276 struct weston_config_section *s;
1280 struct wl_cursor *cursor;
1282 config = weston_config_parse("weston.ini");
1283 s = weston_config_get_section(config, "shell", NULL, NULL);
1284 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1285 weston_config_section_get_int(s, "cursor-size", &size, 32);
1286 weston_config_destroy(config);
1288 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1291 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1293 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1295 for (j = 0; !cursor && j < cursors[i].count; ++j)
1296 cursor = wl_cursor_theme_get_cursor(
1297 display->cursor_theme, cursors[i].names[j]);
1300 fprintf(stderr, "could not load cursor '%s'\n",
1301 cursors[i].names[0]);
1303 display->cursors[i] = cursor;
1308 destroy_cursors(struct display *display)
1310 wl_cursor_theme_destroy(display->cursor_theme);
1311 free(display->cursors);
1314 struct wl_cursor_image *
1315 display_get_pointer_image(struct display *display, int pointer)
1317 struct wl_cursor *cursor = display->cursors[pointer];
1319 return cursor ? cursor->images[0] : NULL;
1323 surface_flush(struct surface *surface)
1325 if (!surface->cairo_surface)
1328 if (surface->opaque_region) {
1329 wl_surface_set_opaque_region(surface->surface,
1330 surface->opaque_region);
1331 wl_region_destroy(surface->opaque_region);
1332 surface->opaque_region = NULL;
1335 if (surface->input_region) {
1336 wl_surface_set_input_region(surface->surface,
1337 surface->input_region);
1338 wl_region_destroy(surface->input_region);
1339 surface->input_region = NULL;
1342 surface->toysurface->swap(surface->toysurface,
1343 surface->buffer_transform, surface->buffer_scale,
1344 &surface->server_allocation);
1346 cairo_surface_destroy(surface->cairo_surface);
1347 surface->cairo_surface = NULL;
1351 window_has_focus(struct window *window)
1353 return window->focus_count > 0;
1357 window_flush(struct window *window)
1359 struct surface *surface;
1361 if (window->type == TYPE_NONE) {
1362 window->type = TYPE_TOPLEVEL;
1363 if (window->shell_surface)
1364 wl_shell_surface_set_toplevel(window->shell_surface);
1367 wl_list_for_each(surface, &window->subsurface_list, link) {
1368 if (surface == window->main_surface)
1371 surface_flush(surface);
1374 surface_flush(window->main_surface);
1378 window_get_display(struct window *window)
1380 return window->display;
1384 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1386 struct display *display = surface->window->display;
1387 struct rectangle allocation = surface->allocation;
1389 if (!surface->toysurface && display->dpy &&
1390 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1391 surface->toysurface =
1392 egl_window_surface_create(display,
1398 if (!surface->toysurface)
1399 surface->toysurface = shm_surface_create(display,
1401 flags, &allocation);
1403 surface->cairo_surface = surface->toysurface->prepare(
1404 surface->toysurface, dx, dy,
1405 allocation.width, allocation.height, flags,
1406 surface->buffer_transform, surface->buffer_scale);
1410 window_create_main_surface(struct window *window)
1412 struct surface *surface = window->main_surface;
1417 if (window->resizing)
1418 flags |= SURFACE_HINT_RESIZE;
1420 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1421 flags |= SURFACE_HINT_RGB565;
1423 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1424 dx = surface->server_allocation.width -
1425 surface->allocation.width;
1427 if (window->resize_edges & WINDOW_RESIZING_TOP)
1428 dy = surface->server_allocation.height -
1429 surface->allocation.height;
1431 window->resize_edges = 0;
1433 surface_create_surface(surface, dx, dy, flags);
1437 window_get_buffer_transform(struct window *window)
1439 return window->main_surface->buffer_transform;
1443 window_set_buffer_transform(struct window *window,
1444 enum wl_output_transform transform)
1446 window->main_surface->buffer_transform = transform;
1447 wl_surface_set_buffer_transform(window->main_surface->surface,
1452 window_set_buffer_scale(struct window *window,
1455 window->main_surface->buffer_scale = scale;
1456 wl_surface_set_buffer_scale(window->main_surface->surface,
1461 window_get_buffer_scale(struct window *window)
1463 return window->main_surface->buffer_scale;
1467 window_get_output_scale(struct window *window)
1469 struct window_output *window_output;
1470 struct window_output *window_output_tmp;
1473 wl_list_for_each_safe(window_output, window_output_tmp,
1474 &window->window_output_list, link) {
1475 if (window_output->output->scale > scale)
1476 scale = window_output->output->scale;
1482 static void window_frame_destroy(struct window_frame *frame);
1485 surface_destroy(struct surface *surface)
1487 if (surface->frame_cb)
1488 wl_callback_destroy(surface->frame_cb);
1490 if (surface->input_region)
1491 wl_region_destroy(surface->input_region);
1493 if (surface->opaque_region)
1494 wl_region_destroy(surface->opaque_region);
1496 if (surface->subsurface)
1497 wl_subsurface_destroy(surface->subsurface);
1499 wl_surface_destroy(surface->surface);
1501 if (surface->toysurface)
1502 surface->toysurface->destroy(surface->toysurface);
1504 wl_list_remove(&surface->link);
1509 window_destroy(struct window *window)
1511 struct display *display = window->display;
1512 struct input *input;
1513 struct window_output *window_output;
1514 struct window_output *window_output_tmp;
1516 wl_list_remove(&window->redraw_task.link);
1518 wl_list_for_each(input, &display->input_list, link) {
1519 if (input->touch_focus == window)
1520 input->touch_focus = NULL;
1521 if (input->pointer_focus == window)
1522 input->pointer_focus = NULL;
1523 if (input->keyboard_focus == window)
1524 input->keyboard_focus = NULL;
1525 if (input->focus_widget &&
1526 input->focus_widget->window == window)
1527 input->focus_widget = NULL;
1530 wl_list_for_each_safe(window_output, window_output_tmp,
1531 &window->window_output_list, link) {
1532 free (window_output);
1536 window_frame_destroy(window->frame);
1538 if (window->shell_surface)
1539 wl_shell_surface_destroy(window->shell_surface);
1541 surface_destroy(window->main_surface);
1543 wl_list_remove(&window->link);
1545 free(window->title);
1549 static struct widget *
1550 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1552 struct widget *child, *target;
1554 wl_list_for_each(child, &widget->child_list, link) {
1555 target = widget_find_widget(child, x, y);
1560 if (widget->allocation.x <= x &&
1561 x < widget->allocation.x + widget->allocation.width &&
1562 widget->allocation.y <= y &&
1563 y < widget->allocation.y + widget->allocation.height) {
1570 static struct widget *
1571 window_find_widget(struct window *window, int32_t x, int32_t y)
1573 struct surface *surface;
1574 struct widget *widget;
1576 wl_list_for_each(surface, &window->subsurface_list, link) {
1577 widget = widget_find_widget(surface->widget, x, y);
1585 static struct widget *
1586 widget_create(struct window *window, struct surface *surface, void *data)
1588 struct widget *widget;
1590 widget = xzalloc(sizeof *widget);
1591 widget->window = window;
1592 widget->surface = surface;
1593 widget->user_data = data;
1594 widget->allocation = surface->allocation;
1595 wl_list_init(&widget->child_list);
1597 widget->tooltip = NULL;
1598 widget->tooltip_count = 0;
1599 widget->default_cursor = CURSOR_LEFT_PTR;
1605 window_add_widget(struct window *window, void *data)
1607 struct widget *widget;
1609 widget = widget_create(window, window->main_surface, data);
1610 wl_list_init(&widget->link);
1611 window->main_surface->widget = widget;
1617 widget_add_widget(struct widget *parent, void *data)
1619 struct widget *widget;
1621 widget = widget_create(parent->window, parent->surface, data);
1622 wl_list_insert(parent->child_list.prev, &widget->link);
1628 widget_destroy(struct widget *widget)
1630 struct display *display = widget->window->display;
1631 struct surface *surface = widget->surface;
1632 struct input *input;
1634 /* Destroy the sub-surface along with the root widget */
1635 if (surface->widget == widget && surface->subsurface)
1636 surface_destroy(widget->surface);
1638 if (widget->tooltip) {
1639 free(widget->tooltip);
1640 widget->tooltip = NULL;
1643 wl_list_for_each(input, &display->input_list, link) {
1644 if (input->focus_widget == widget)
1645 input->focus_widget = NULL;
1648 wl_list_remove(&widget->link);
1653 widget_set_default_cursor(struct widget *widget, int cursor)
1655 widget->default_cursor = cursor;
1659 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1661 *allocation = widget->allocation;
1665 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1667 widget->allocation.width = width;
1668 widget->allocation.height = height;
1672 widget_set_allocation(struct widget *widget,
1673 int32_t x, int32_t y, int32_t width, int32_t height)
1675 widget->allocation.x = x;
1676 widget->allocation.y = y;
1677 widget_set_size(widget, width, height);
1681 widget_set_transparent(struct widget *widget, int transparent)
1683 widget->opaque = !transparent;
1687 widget_get_user_data(struct widget *widget)
1689 return widget->user_data;
1692 static cairo_surface_t *
1693 widget_get_cairo_surface(struct widget *widget)
1695 struct surface *surface = widget->surface;
1696 struct window *window = widget->window;
1698 if (!surface->cairo_surface) {
1699 if (surface == window->main_surface)
1700 window_create_main_surface(window);
1702 surface_create_surface(surface, 0, 0, 0);
1705 return surface->cairo_surface;
1709 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1711 struct surface *surface = widget->surface;
1714 enum wl_output_transform transform;
1715 int surface_width, surface_height;
1716 int translate_x, translate_y;
1719 surface_width = surface->allocation.width;
1720 surface_height = surface->allocation.height;
1722 transform = surface->buffer_transform;
1723 scale = surface->buffer_scale;
1725 switch (transform) {
1726 case WL_OUTPUT_TRANSFORM_FLIPPED:
1727 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1728 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1730 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1733 cairo_matrix_init_identity(&m);
1737 switch (transform) {
1738 case WL_OUTPUT_TRANSFORM_NORMAL:
1744 case WL_OUTPUT_TRANSFORM_FLIPPED:
1746 translate_x = surface_width;
1749 case WL_OUTPUT_TRANSFORM_90:
1751 translate_x = surface_height;
1754 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1756 translate_x = surface_height;
1757 translate_y = surface_width;
1759 case WL_OUTPUT_TRANSFORM_180:
1761 translate_x = surface_width;
1762 translate_y = surface_height;
1764 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1767 translate_y = surface_height;
1769 case WL_OUTPUT_TRANSFORM_270:
1770 angle = M_PI + M_PI_2;
1772 translate_y = surface_width;
1774 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1775 angle = M_PI + M_PI_2;
1781 cairo_scale(cr, scale, scale);
1782 cairo_translate(cr, translate_x, translate_y);
1783 cairo_rotate(cr, angle);
1784 cairo_transform(cr, &m);
1788 widget_cairo_create(struct widget *widget)
1790 struct surface *surface = widget->surface;
1791 cairo_surface_t *cairo_surface;
1794 cairo_surface = widget_get_cairo_surface(widget);
1795 cr = cairo_create(cairo_surface);
1797 widget_cairo_update_transform(widget, cr);
1799 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1805 widget_get_wl_surface(struct widget *widget)
1807 return widget->surface->surface;
1811 widget_get_last_time(struct widget *widget)
1813 return widget->surface->last_time;
1817 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1819 struct wl_compositor *comp = widget->window->display->compositor;
1820 struct surface *surface = widget->surface;
1822 if (!surface->input_region)
1823 surface->input_region = wl_compositor_create_region(comp);
1826 wl_region_add(surface->input_region,
1827 rect->x, rect->y, rect->width, rect->height);
1832 widget_set_resize_handler(struct widget *widget,
1833 widget_resize_handler_t handler)
1835 widget->resize_handler = handler;
1839 widget_set_redraw_handler(struct widget *widget,
1840 widget_redraw_handler_t handler)
1842 widget->redraw_handler = handler;
1846 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1848 widget->enter_handler = handler;
1852 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1854 widget->leave_handler = handler;
1858 widget_set_motion_handler(struct widget *widget,
1859 widget_motion_handler_t handler)
1861 widget->motion_handler = handler;
1865 widget_set_button_handler(struct widget *widget,
1866 widget_button_handler_t handler)
1868 widget->button_handler = handler;
1872 widget_set_touch_up_handler(struct widget *widget,
1873 widget_touch_up_handler_t handler)
1875 widget->touch_up_handler = handler;
1879 widget_set_touch_down_handler(struct widget *widget,
1880 widget_touch_down_handler_t handler)
1882 widget->touch_down_handler = handler;
1886 widget_set_touch_motion_handler(struct widget *widget,
1887 widget_touch_motion_handler_t handler)
1889 widget->touch_motion_handler = handler;
1893 widget_set_touch_frame_handler(struct widget *widget,
1894 widget_touch_frame_handler_t handler)
1896 widget->touch_frame_handler = handler;
1900 widget_set_touch_cancel_handler(struct widget *widget,
1901 widget_touch_cancel_handler_t handler)
1903 widget->touch_cancel_handler = handler;
1907 widget_set_axis_handler(struct widget *widget,
1908 widget_axis_handler_t handler)
1910 widget->axis_handler = handler;
1914 window_schedule_redraw_task(struct window *window);
1917 widget_schedule_redraw(struct widget *widget)
1919 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1920 widget->surface->redraw_needed = 1;
1921 window_schedule_redraw_task(widget->window);
1925 window_get_surface(struct window *window)
1927 cairo_surface_t *cairo_surface;
1929 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1931 return cairo_surface_reference(cairo_surface);
1935 window_get_wl_surface(struct window *window)
1937 return window->main_surface->surface;
1940 struct wl_shell_surface *
1941 window_get_wl_shell_surface(struct window *window)
1943 return window->shell_surface;
1947 tooltip_redraw_handler(struct widget *widget, void *data)
1950 const int32_t r = 3;
1951 struct tooltip *tooltip = data;
1952 int32_t width, height;
1954 cr = widget_cairo_create(widget);
1955 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1956 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1959 width = widget->allocation.width;
1960 height = widget->allocation.height;
1961 rounded_rect(cr, 0, 0, width, height, r);
1963 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1964 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1967 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1968 cairo_move_to(cr, 10, 16);
1969 cairo_show_text(cr, tooltip->entry);
1973 static cairo_text_extents_t
1974 get_text_extents(struct tooltip *tooltip)
1977 cairo_text_extents_t extents;
1979 /* Use the dummy_surface because tooltip's surface was not
1980 * created yet, and parent does not have a valid surface
1981 * outside repaint, either.
1983 cr = cairo_create(tooltip->window->display->dummy_surface);
1984 cairo_text_extents(cr, tooltip->entry, &extents);
1991 window_create_tooltip(struct tooltip *tooltip)
1993 struct widget *parent = tooltip->parent;
1994 struct display *display = parent->window->display;
1995 struct window *window;
1996 const int offset_y = 27;
1997 const int margin = 3;
1998 cairo_text_extents_t extents;
2000 if (tooltip->widget)
2003 window = window_create_transient(display, parent->window, tooltip->x,
2004 tooltip->y + offset_y,
2005 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
2009 tooltip->window = window;
2010 tooltip->widget = window_add_widget(tooltip->window, tooltip);
2012 extents = get_text_extents(tooltip);
2013 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2014 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2020 widget_destroy_tooltip(struct widget *parent)
2022 struct tooltip *tooltip = parent->tooltip;
2024 parent->tooltip_count = 0;
2028 if (tooltip->widget) {
2029 widget_destroy(tooltip->widget);
2030 window_destroy(tooltip->window);
2031 tooltip->widget = NULL;
2032 tooltip->window = NULL;
2035 close(tooltip->tooltip_fd);
2036 free(tooltip->entry);
2038 parent->tooltip = NULL;
2042 tooltip_func(struct task *task, uint32_t events)
2044 struct tooltip *tooltip =
2045 container_of(task, struct tooltip, tooltip_task);
2048 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2050 window_create_tooltip(tooltip);
2053 #define TOOLTIP_TIMEOUT 500
2055 tooltip_timer_reset(struct tooltip *tooltip)
2057 struct itimerspec its;
2059 its.it_interval.tv_sec = 0;
2060 its.it_interval.tv_nsec = 0;
2061 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2062 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2063 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2064 fprintf(stderr, "could not set timerfd\n: %m");
2072 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2074 struct tooltip *tooltip = parent->tooltip;
2076 parent->tooltip_count++;
2080 tooltip_timer_reset(tooltip);
2084 /* the handler might be triggered too fast via input device motion, so
2085 * we need this check here to make sure tooltip is fully initialized */
2086 if (parent->tooltip_count > 1)
2089 tooltip = malloc(sizeof *tooltip);
2093 parent->tooltip = tooltip;
2094 tooltip->parent = parent;
2095 tooltip->widget = NULL;
2096 tooltip->window = NULL;
2099 tooltip->entry = strdup(entry);
2100 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2101 if (tooltip->tooltip_fd < 0) {
2102 fprintf(stderr, "could not create timerfd\n: %m");
2106 tooltip->tooltip_task.run = tooltip_func;
2107 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2108 EPOLLIN, &tooltip->tooltip_task);
2109 tooltip_timer_reset(tooltip);
2115 workspace_manager_state(void *data,
2116 struct workspace_manager *workspace_manager,
2120 struct display *display = data;
2122 display->workspace = current;
2123 display->workspace_count = count;
2126 static const struct workspace_manager_listener workspace_manager_listener = {
2127 workspace_manager_state
2131 frame_resize_handler(struct widget *widget,
2132 int32_t width, int32_t height, void *data)
2134 struct window_frame *frame = data;
2135 struct widget *child = frame->child;
2136 struct rectangle interior;
2137 struct rectangle input;
2138 struct rectangle opaque;
2140 if (widget->window->type == TYPE_FULLSCREEN) {
2143 interior.width = width;
2144 interior.height = height;
2146 if (widget->window->type == TYPE_MAXIMIZED) {
2147 frame_set_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2149 frame_unset_flag(frame->frame, FRAME_FLAG_MAXIMIZED);
2152 frame_resize(frame->frame, width, height);
2153 frame_interior(frame->frame, &interior.x, &interior.y,
2154 &interior.width, &interior.height);
2157 widget_set_allocation(child, interior.x, interior.y,
2158 interior.width, interior.height);
2160 if (child->resize_handler) {
2161 child->resize_handler(child, interior.width, interior.height,
2164 if (widget->window->type == TYPE_FULLSCREEN) {
2165 width = child->allocation.width;
2166 height = child->allocation.height;
2168 frame_resize_inside(frame->frame,
2169 child->allocation.width,
2170 child->allocation.height);
2171 width = frame_width(frame->frame);
2172 height = frame_height(frame->frame);
2176 widget_set_allocation(widget, 0, 0, width, height);
2178 widget->surface->input_region =
2179 wl_compositor_create_region(widget->window->display->compositor);
2180 if (widget->window->type != TYPE_FULLSCREEN) {
2181 frame_input_rect(frame->frame, &input.x, &input.y,
2182 &input.width, &input.height);
2183 wl_region_add(widget->surface->input_region,
2184 input.x, input.y, input.width, input.height);
2186 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2189 widget_set_allocation(widget, 0, 0, width, height);
2191 if (child->opaque) {
2192 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2193 &opaque.width, &opaque.height);
2194 wl_region_add(widget->surface->opaque_region,
2195 opaque.x, opaque.y, opaque.width, opaque.height);
2199 widget_schedule_redraw(widget);
2203 frame_redraw_handler(struct widget *widget, void *data)
2206 struct window_frame *frame = data;
2207 struct window *window = widget->window;
2209 if (window->type == TYPE_FULLSCREEN)
2212 if (window->focus_count) {
2213 frame_set_flag(frame->frame, FRAME_FLAG_ACTIVE);
2215 frame_unset_flag(frame->frame, FRAME_FLAG_ACTIVE);
2218 cr = widget_cairo_create(widget);
2220 frame_repaint(frame->frame, cr);
2226 frame_get_pointer_image_for_location(struct window_frame *frame,
2227 enum theme_location location)
2229 struct window *window = frame->widget->window;
2231 if (window->type != TYPE_TOPLEVEL)
2232 return CURSOR_LEFT_PTR;
2235 case THEME_LOCATION_RESIZING_TOP:
2237 case THEME_LOCATION_RESIZING_BOTTOM:
2238 return CURSOR_BOTTOM;
2239 case THEME_LOCATION_RESIZING_LEFT:
2241 case THEME_LOCATION_RESIZING_RIGHT:
2242 return CURSOR_RIGHT;
2243 case THEME_LOCATION_RESIZING_TOP_LEFT:
2244 return CURSOR_TOP_LEFT;
2245 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2246 return CURSOR_TOP_RIGHT;
2247 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2248 return CURSOR_BOTTOM_LEFT;
2249 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2250 return CURSOR_BOTTOM_RIGHT;
2251 case THEME_LOCATION_EXTERIOR:
2252 case THEME_LOCATION_TITLEBAR:
2254 return CURSOR_LEFT_PTR;
2259 frame_menu_func(struct window *window, int index, void *data)
2261 struct display *display;
2265 if (window->close_handler)
2266 window->close_handler(window->parent,
2269 display_exit(window->display);
2271 case 1: /* move to workspace above */
2272 display = window->display;
2273 if (display->workspace > 0)
2274 workspace_manager_move_surface(
2275 display->workspace_manager,
2276 window->main_surface->surface,
2277 display->workspace - 1);
2279 case 2: /* move to workspace below */
2280 display = window->display;
2281 if (display->workspace < display->workspace_count - 1)
2282 workspace_manager_move_surface(
2283 display->workspace_manager,
2284 window->main_surface->surface,
2285 display->workspace + 1);
2287 case 3: /* fullscreen */
2288 /* we don't have a way to get out of fullscreen for now */
2289 if (window->fullscreen_handler)
2290 window->fullscreen_handler(window, window->user_data);
2296 window_show_frame_menu(struct window *window,
2297 struct input *input, uint32_t time)
2302 static const char *entries[] = {
2304 "Move to workspace above", "Move to workspace below",
2308 if (window->fullscreen_handler)
2309 count = ARRAY_LENGTH(entries);
2311 count = ARRAY_LENGTH(entries) - 1;
2313 input_get_position(input, &x, &y);
2314 window_show_menu(window->display, input, time, window,
2315 x - 10, y - 10, frame_menu_func, entries, count);
2319 frame_enter_handler(struct widget *widget,
2320 struct input *input, float x, float y, void *data)
2322 struct window_frame *frame = data;
2323 enum theme_location location;
2325 location = frame_pointer_enter(frame->frame, input, x, y);
2326 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2327 widget_schedule_redraw(frame->widget);
2329 return frame_get_pointer_image_for_location(data, location);
2333 frame_motion_handler(struct widget *widget,
2334 struct input *input, uint32_t time,
2335 float x, float y, void *data)
2337 struct window_frame *frame = data;
2338 enum theme_location location;
2340 location = frame_pointer_motion(frame->frame, input, x, y);
2341 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2342 widget_schedule_redraw(frame->widget);
2344 return frame_get_pointer_image_for_location(data, location);
2348 frame_leave_handler(struct widget *widget,
2349 struct input *input, void *data)
2351 struct window_frame *frame = data;
2353 frame_pointer_leave(frame->frame, input);
2354 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2355 widget_schedule_redraw(frame->widget);
2359 frame_handle_status(struct window_frame *frame, struct input *input,
2360 uint32_t time, enum theme_location location)
2362 struct window *window = frame->widget->window;
2365 status = frame_status(frame->frame);
2366 if (status & FRAME_STATUS_REPAINT)
2367 widget_schedule_redraw(frame->widget);
2369 if (status & FRAME_STATUS_MINIMIZE)
2370 fprintf(stderr,"Minimize stub\n");
2372 if (status & FRAME_STATUS_MENU) {
2373 window_show_frame_menu(window, input, time);
2374 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2377 if (status & FRAME_STATUS_MAXIMIZE) {
2378 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2379 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2382 if (status & FRAME_STATUS_CLOSE) {
2383 if (window->close_handler)
2384 window->close_handler(window->parent,
2387 display_exit(window->display);
2388 frame_status_clear(frame->frame, FRAME_STATUS_CLOSE);
2391 if ((status & FRAME_STATUS_MOVE) && window->shell_surface) {
2392 input_ungrab(input);
2393 wl_shell_surface_move(window->shell_surface,
2394 input_get_seat(input),
2395 window->display->serial);
2397 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2400 if ((status & FRAME_STATUS_RESIZE) && window->shell_surface) {
2401 input_ungrab(input);
2403 window->resizing = 1;
2404 wl_shell_surface_resize(window->shell_surface,
2405 input_get_seat(input),
2406 window->display->serial,
2409 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2414 frame_button_handler(struct widget *widget,
2415 struct input *input, uint32_t time,
2416 uint32_t button, enum wl_pointer_button_state state,
2420 struct window_frame *frame = data;
2421 enum theme_location location;
2423 location = frame_pointer_button(frame->frame, input, button, state);
2424 frame_handle_status(frame, input, time, location);
2428 frame_touch_down_handler(struct widget *widget, struct input *input,
2429 uint32_t serial, uint32_t time, int32_t id,
2430 float x, float y, void *data)
2432 struct window_frame *frame = data;
2434 frame_touch_down(frame->frame, input, id, x, y);
2435 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2439 frame_touch_up_handler(struct widget *widget,
2440 struct input *input, uint32_t serial, uint32_t time,
2441 int32_t id, void *data)
2443 struct window_frame *frame = data;
2445 frame_touch_up(frame->frame, input, id);
2446 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2450 window_frame_create(struct window *window, void *data)
2452 struct window_frame *frame;
2454 frame = xzalloc(sizeof *frame);
2455 frame->frame = frame_create(window->display->theme, 0, 0,
2456 FRAME_BUTTON_ALL, window->title);
2458 frame->widget = window_add_widget(window, frame);
2459 frame->child = widget_add_widget(frame->widget, data);
2461 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2462 widget_set_resize_handler(frame->widget, frame_resize_handler);
2463 widget_set_enter_handler(frame->widget, frame_enter_handler);
2464 widget_set_leave_handler(frame->widget, frame_leave_handler);
2465 widget_set_motion_handler(frame->widget, frame_motion_handler);
2466 widget_set_button_handler(frame->widget, frame_button_handler);
2467 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2468 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2470 window->frame = frame;
2472 return frame->child;
2476 window_frame_set_child_size(struct widget *widget, int child_width,
2479 struct display *display = widget->window->display;
2480 struct theme *t = display->theme;
2481 int decoration_width, decoration_height;
2483 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2485 if (widget->window->type != TYPE_FULLSCREEN) {
2486 decoration_width = (t->width + margin) * 2;
2487 decoration_height = t->width +
2488 t->titlebar_height + margin * 2;
2490 width = child_width + decoration_width;
2491 height = child_height + decoration_height;
2493 width = child_width;
2494 height = child_height;
2497 window_schedule_resize(widget->window, width, height);
2501 window_frame_destroy(struct window_frame *frame)
2503 frame_destroy(frame->frame);
2505 /* frame->child must be destroyed by the application */
2506 widget_destroy(frame->widget);
2511 input_set_focus_widget(struct input *input, struct widget *focus,
2514 struct widget *old, *widget;
2517 if (focus == input->focus_widget)
2520 old = input->focus_widget;
2524 widget = input->grab;
2525 if (widget->leave_handler)
2526 widget->leave_handler(old, input, widget->user_data);
2527 input->focus_widget = NULL;
2533 widget = input->grab;
2534 input->focus_widget = focus;
2535 if (widget->enter_handler)
2536 cursor = widget->enter_handler(focus, input, x, y,
2539 cursor = widget->default_cursor;
2541 input_set_pointer_image(input, cursor);
2546 input_grab(struct input *input, struct widget *widget, uint32_t button)
2548 input->grab = widget;
2549 input->grab_button = button;
2553 input_ungrab(struct input *input)
2555 struct widget *widget;
2558 if (input->pointer_focus) {
2559 widget = window_find_widget(input->pointer_focus,
2560 input->sx, input->sy);
2561 input_set_focus_widget(input, widget, input->sx, input->sy);
2566 input_remove_pointer_focus(struct input *input)
2568 struct window *window = input->pointer_focus;
2573 input_set_focus_widget(input, NULL, 0, 0);
2575 input->pointer_focus = NULL;
2576 input->current_cursor = CURSOR_UNSET;
2580 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2581 uint32_t serial, struct wl_surface *surface,
2582 wl_fixed_t sx_w, wl_fixed_t sy_w)
2584 struct input *input = data;
2585 struct window *window;
2586 struct widget *widget;
2587 float sx = wl_fixed_to_double(sx_w);
2588 float sy = wl_fixed_to_double(sy_w);
2591 /* enter event for a window we've just destroyed */
2595 input->display->serial = serial;
2596 input->pointer_enter_serial = serial;
2597 input->pointer_focus = wl_surface_get_user_data(surface);
2598 window = input->pointer_focus;
2600 if (window->resizing) {
2601 window->resizing = 0;
2602 /* Schedule a redraw to free the pool */
2603 window_schedule_redraw(window);
2609 widget = window_find_widget(window, sx, sy);
2610 input_set_focus_widget(input, widget, sx, sy);
2614 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2615 uint32_t serial, struct wl_surface *surface)
2617 struct input *input = data;
2619 input->display->serial = serial;
2620 input_remove_pointer_focus(input);
2624 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2625 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2627 struct input *input = data;
2628 struct window *window = input->pointer_focus;
2629 struct widget *widget;
2631 float sx = wl_fixed_to_double(sx_w);
2632 float sy = wl_fixed_to_double(sy_w);
2640 /* when making the window smaller - e.g. after a unmaximise we might
2641 * still have a pending motion event that the compositor has picked
2642 * based on the old surface dimensions
2644 if (sx > window->main_surface->allocation.width ||
2645 sy > window->main_surface->allocation.height)
2648 if (!(input->grab && input->grab_button)) {
2649 widget = window_find_widget(window, sx, sy);
2650 input_set_focus_widget(input, widget, sx, sy);
2654 widget = input->grab;
2656 widget = input->focus_widget;
2658 if (widget->motion_handler)
2659 cursor = widget->motion_handler(input->focus_widget,
2660 input, time, sx, sy,
2663 cursor = widget->default_cursor;
2665 cursor = CURSOR_LEFT_PTR;
2667 input_set_pointer_image(input, cursor);
2671 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2672 uint32_t time, uint32_t button, uint32_t state_w)
2674 struct input *input = data;
2675 struct widget *widget;
2676 enum wl_pointer_button_state state = state_w;
2678 input->display->serial = serial;
2679 if (input->focus_widget && input->grab == NULL &&
2680 state == WL_POINTER_BUTTON_STATE_PRESSED)
2681 input_grab(input, input->focus_widget, button);
2683 widget = input->grab;
2684 if (widget && widget->button_handler)
2685 (*widget->button_handler)(widget,
2688 input->grab->user_data);
2690 if (input->grab && input->grab_button == button &&
2691 state == WL_POINTER_BUTTON_STATE_RELEASED)
2692 input_ungrab(input);
2696 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2697 uint32_t time, uint32_t axis, wl_fixed_t value)
2699 struct input *input = data;
2700 struct widget *widget;
2702 widget = input->focus_widget;
2704 widget = input->grab;
2705 if (widget && widget->axis_handler)
2706 (*widget->axis_handler)(widget,
2712 static const struct wl_pointer_listener pointer_listener = {
2713 pointer_handle_enter,
2714 pointer_handle_leave,
2715 pointer_handle_motion,
2716 pointer_handle_button,
2717 pointer_handle_axis,
2721 input_remove_keyboard_focus(struct input *input)
2723 struct window *window = input->keyboard_focus;
2724 struct itimerspec its;
2726 its.it_interval.tv_sec = 0;
2727 its.it_interval.tv_nsec = 0;
2728 its.it_value.tv_sec = 0;
2729 its.it_value.tv_nsec = 0;
2730 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2735 window->focus_count--;
2736 if (window->keyboard_focus_handler)
2737 (*window->keyboard_focus_handler)(window, NULL,
2740 input->keyboard_focus = NULL;
2744 keyboard_repeat_func(struct task *task, uint32_t events)
2746 struct input *input =
2747 container_of(task, struct input, repeat_task);
2748 struct window *window = input->keyboard_focus;
2751 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2752 /* If we change the timer between the fd becoming
2753 * readable and getting here, there'll be nothing to
2754 * read and we get EAGAIN. */
2757 if (window && window->key_handler) {
2758 (*window->key_handler)(window, input, input->repeat_time,
2759 input->repeat_key, input->repeat_sym,
2760 WL_KEYBOARD_KEY_STATE_PRESSED,
2766 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2767 uint32_t format, int fd, uint32_t size)
2769 struct input *input = data;
2777 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2782 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2783 if (map_str == MAP_FAILED) {
2788 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2790 XKB_KEYMAP_FORMAT_TEXT_V1,
2792 munmap(map_str, size);
2795 if (!input->xkb.keymap) {
2796 fprintf(stderr, "failed to compile keymap\n");
2800 input->xkb.state = xkb_state_new(input->xkb.keymap);
2801 if (!input->xkb.state) {
2802 fprintf(stderr, "failed to create XKB state\n");
2803 xkb_map_unref(input->xkb.keymap);
2804 input->xkb.keymap = NULL;
2808 input->xkb.control_mask =
2809 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2810 input->xkb.alt_mask =
2811 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2812 input->xkb.shift_mask =
2813 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2817 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2818 uint32_t serial, struct wl_surface *surface,
2819 struct wl_array *keys)
2821 struct input *input = data;
2822 struct window *window;
2824 input->display->serial = serial;
2825 input->keyboard_focus = wl_surface_get_user_data(surface);
2827 window = input->keyboard_focus;
2828 window->focus_count++;
2829 if (window->keyboard_focus_handler)
2830 (*window->keyboard_focus_handler)(window,
2831 input, window->user_data);
2835 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2836 uint32_t serial, struct wl_surface *surface)
2838 struct input *input = data;
2840 input->display->serial = serial;
2841 input_remove_keyboard_focus(input);
2845 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2846 uint32_t serial, uint32_t time, uint32_t key,
2849 struct input *input = data;
2850 struct window *window = input->keyboard_focus;
2851 uint32_t code, num_syms;
2852 enum wl_keyboard_key_state state = state_w;
2853 const xkb_keysym_t *syms;
2855 struct itimerspec its;
2857 input->display->serial = serial;
2859 if (!window || !input->xkb.state)
2862 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2864 sym = XKB_KEY_NoSymbol;
2868 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2869 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2870 window_set_maximized(window,
2871 window->type != TYPE_MAXIMIZED);
2872 } else if (sym == XKB_KEY_F11 &&
2873 window->fullscreen_handler &&
2874 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2875 window->fullscreen_handler(window, window->user_data);
2876 } else if (sym == XKB_KEY_F4 &&
2877 input->modifiers == MOD_ALT_MASK &&
2878 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2879 if (window->close_handler)
2880 window->close_handler(window->parent,
2883 display_exit(window->display);
2884 } else if (window->key_handler) {
2885 (*window->key_handler)(window, input, time, key,
2886 sym, state, window->user_data);
2889 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2890 key == input->repeat_key) {
2891 its.it_interval.tv_sec = 0;
2892 its.it_interval.tv_nsec = 0;
2893 its.it_value.tv_sec = 0;
2894 its.it_value.tv_nsec = 0;
2895 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2896 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2897 input->repeat_sym = sym;
2898 input->repeat_key = key;
2899 input->repeat_time = time;
2900 its.it_interval.tv_sec = 0;
2901 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2902 its.it_value.tv_sec = 0;
2903 its.it_value.tv_nsec = 400 * 1000 * 1000;
2904 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2909 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2910 uint32_t serial, uint32_t mods_depressed,
2911 uint32_t mods_latched, uint32_t mods_locked,
2914 struct input *input = data;
2915 xkb_mod_mask_t mask;
2917 /* If we're not using a keymap, then we don't handle PC-style modifiers */
2918 if (!input->xkb.keymap)
2921 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2922 mods_locked, 0, 0, group);
2923 mask = xkb_state_serialize_mods(input->xkb.state,
2924 XKB_STATE_DEPRESSED |
2926 input->modifiers = 0;
2927 if (mask & input->xkb.control_mask)
2928 input->modifiers |= MOD_CONTROL_MASK;
2929 if (mask & input->xkb.alt_mask)
2930 input->modifiers |= MOD_ALT_MASK;
2931 if (mask & input->xkb.shift_mask)
2932 input->modifiers |= MOD_SHIFT_MASK;
2935 static const struct wl_keyboard_listener keyboard_listener = {
2936 keyboard_handle_keymap,
2937 keyboard_handle_enter,
2938 keyboard_handle_leave,
2939 keyboard_handle_key,
2940 keyboard_handle_modifiers,
2944 touch_handle_down(void *data, struct wl_touch *wl_touch,
2945 uint32_t serial, uint32_t time, struct wl_surface *surface,
2946 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
2948 struct input *input = data;
2949 struct widget *widget;
2950 float sx = wl_fixed_to_double(x_w);
2951 float sy = wl_fixed_to_double(y_w);
2953 input->display->serial = serial;
2954 input->touch_focus = wl_surface_get_user_data(surface);
2955 if (!input->touch_focus) {
2956 DBG("Failed to find to touch focus for surface %p\n", surface);
2960 widget = window_find_widget(input->touch_focus,
2961 wl_fixed_to_double(x_w),
2962 wl_fixed_to_double(y_w));
2964 struct touch_point *tp = xmalloc(sizeof *tp);
2967 tp->widget = widget;
2968 wl_list_insert(&input->touch_point_list, &tp->link);
2970 if (widget->touch_down_handler)
2971 (*widget->touch_down_handler)(widget, input,
2980 touch_handle_up(void *data, struct wl_touch *wl_touch,
2981 uint32_t serial, uint32_t time, int32_t id)
2983 struct input *input = data;
2984 struct touch_point *tp, *tmp;
2986 if (!input->touch_focus) {
2987 DBG("No touch focus found for touch up event!\n");
2991 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
2995 if (tp->widget->touch_up_handler)
2996 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
2998 tp->widget->user_data);
3000 wl_list_remove(&tp->link);
3008 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3009 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3011 struct input *input = data;
3012 struct touch_point *tp;
3013 float sx = wl_fixed_to_double(x_w);
3014 float sy = wl_fixed_to_double(y_w);
3016 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3018 if (!input->touch_focus) {
3019 DBG("No touch focus found for touch motion event!\n");
3023 wl_list_for_each(tp, &input->touch_point_list, link) {
3027 if (tp->widget->touch_motion_handler)
3028 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3030 tp->widget->user_data);
3036 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3038 struct input *input = data;
3039 struct touch_point *tp, *tmp;
3041 DBG("touch_handle_frame\n");
3043 if (!input->touch_focus) {
3044 DBG("No touch focus found for touch frame event!\n");
3048 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3049 if (tp->widget->touch_frame_handler)
3050 (*tp->widget->touch_frame_handler)(tp->widget, input,
3051 tp->widget->user_data);
3053 wl_list_remove(&tp->link);
3059 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3061 struct input *input = data;
3062 struct touch_point *tp, *tmp;
3064 DBG("touch_handle_cancel\n");
3066 if (!input->touch_focus) {
3067 DBG("No touch focus found for touch cancel event!\n");
3071 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3072 if (tp->widget->touch_cancel_handler)
3073 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3074 tp->widget->user_data);
3076 wl_list_remove(&tp->link);
3081 static const struct wl_touch_listener touch_listener = {
3084 touch_handle_motion,
3086 touch_handle_cancel,
3090 seat_handle_capabilities(void *data, struct wl_seat *seat,
3091 enum wl_seat_capability caps)
3093 struct input *input = data;
3095 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3096 input->pointer = wl_seat_get_pointer(seat);
3097 wl_pointer_set_user_data(input->pointer, input);
3098 wl_pointer_add_listener(input->pointer, &pointer_listener,
3100 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3101 wl_pointer_destroy(input->pointer);
3102 input->pointer = NULL;
3105 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3106 input->keyboard = wl_seat_get_keyboard(seat);
3107 wl_keyboard_set_user_data(input->keyboard, input);
3108 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3110 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3111 wl_keyboard_destroy(input->keyboard);
3112 input->keyboard = NULL;
3115 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3116 input->touch = wl_seat_get_touch(seat);
3117 wl_touch_set_user_data(input->touch, input);
3118 wl_touch_add_listener(input->touch, &touch_listener, input);
3119 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3120 wl_touch_destroy(input->touch);
3121 input->touch = NULL;
3126 seat_handle_name(void *data, struct wl_seat *seat,
3132 static const struct wl_seat_listener seat_listener = {
3133 seat_handle_capabilities,
3138 input_get_position(struct input *input, int32_t *x, int32_t *y)
3145 input_get_display(struct input *input)
3147 return input->display;
3151 input_get_seat(struct input *input)
3157 input_get_modifiers(struct input *input)
3159 return input->modifiers;
3163 input_get_focus_widget(struct input *input)
3165 return input->focus_widget;
3169 struct wl_data_offer *offer;
3170 struct input *input;
3171 struct wl_array types;
3174 struct task io_task;
3182 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3184 struct data_offer *offer = data;
3187 p = wl_array_add(&offer->types, sizeof *p);
3191 static const struct wl_data_offer_listener data_offer_listener = {
3196 data_offer_destroy(struct data_offer *offer)
3201 if (offer->refcount == 0) {
3202 wl_data_offer_destroy(offer->offer);
3203 for (p = offer->types.data; *p; p++)
3205 wl_array_release(&offer->types);
3211 data_device_data_offer(void *data,
3212 struct wl_data_device *data_device,
3213 struct wl_data_offer *_offer)
3215 struct data_offer *offer;
3217 offer = xmalloc(sizeof *offer);
3219 wl_array_init(&offer->types);
3220 offer->refcount = 1;
3221 offer->input = data;
3222 offer->offer = _offer;
3223 wl_data_offer_add_listener(offer->offer,
3224 &data_offer_listener, offer);
3228 data_device_enter(void *data, struct wl_data_device *data_device,
3229 uint32_t serial, struct wl_surface *surface,
3230 wl_fixed_t x_w, wl_fixed_t y_w,
3231 struct wl_data_offer *offer)
3233 struct input *input = data;
3234 struct window *window;
3236 float x = wl_fixed_to_double(x_w);
3237 float y = wl_fixed_to_double(y_w);
3240 input->pointer_enter_serial = serial;
3241 window = wl_surface_get_user_data(surface);
3242 input->pointer_focus = window;
3245 input->drag_offer = wl_data_offer_get_user_data(offer);
3247 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3250 types_data = input->drag_offer->types.data;
3252 input->drag_offer = NULL;
3256 window = input->pointer_focus;
3257 if (window->data_handler)
3258 window->data_handler(window, input, x, y, types_data,
3263 data_device_leave(void *data, struct wl_data_device *data_device)
3265 struct input *input = data;
3267 if (input->drag_offer) {
3268 data_offer_destroy(input->drag_offer);
3269 input->drag_offer = NULL;
3274 data_device_motion(void *data, struct wl_data_device *data_device,
3275 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3277 struct input *input = data;
3278 struct window *window = input->pointer_focus;
3279 float x = wl_fixed_to_double(x_w);
3280 float y = wl_fixed_to_double(y_w);
3286 if (input->drag_offer)
3287 types_data = input->drag_offer->types.data;
3291 if (window->data_handler)
3292 window->data_handler(window, input, x, y, types_data,
3297 data_device_drop(void *data, struct wl_data_device *data_device)
3299 struct input *input = data;
3300 struct window *window = input->pointer_focus;
3302 if (window->drop_handler)
3303 window->drop_handler(window, input,
3304 input->sx, input->sy, window->user_data);
3308 data_device_selection(void *data,
3309 struct wl_data_device *wl_data_device,
3310 struct wl_data_offer *offer)
3312 struct input *input = data;
3315 if (input->selection_offer)
3316 data_offer_destroy(input->selection_offer);
3319 input->selection_offer = wl_data_offer_get_user_data(offer);
3320 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3323 input->selection_offer = NULL;
3327 static const struct wl_data_device_listener data_device_listener = {
3328 data_device_data_offer,
3333 data_device_selection
3337 input_set_pointer_image_index(struct input *input, int index)
3339 struct wl_buffer *buffer;
3340 struct wl_cursor *cursor;
3341 struct wl_cursor_image *image;
3343 if (!input->pointer)
3346 cursor = input->display->cursors[input->current_cursor];
3350 if (index >= (int) cursor->image_count) {
3351 fprintf(stderr, "cursor index out of range\n");
3355 image = cursor->images[index];
3356 buffer = wl_cursor_image_get_buffer(image);
3360 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3361 input->pointer_surface,
3362 image->hotspot_x, image->hotspot_y);
3363 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3364 wl_surface_damage(input->pointer_surface, 0, 0,
3365 image->width, image->height);
3366 wl_surface_commit(input->pointer_surface);
3369 static const struct wl_callback_listener pointer_surface_listener;
3372 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3375 struct input *input = data;
3376 struct wl_cursor *cursor;
3380 assert(callback == input->cursor_frame_cb);
3381 wl_callback_destroy(callback);
3382 input->cursor_frame_cb = NULL;
3385 if (!input->pointer)
3388 if (input->current_cursor == CURSOR_BLANK) {
3389 wl_pointer_set_cursor(input->pointer,
3390 input->pointer_enter_serial,
3395 if (input->current_cursor == CURSOR_UNSET)
3397 cursor = input->display->cursors[input->current_cursor];
3401 /* FIXME We don't have the current time on the first call so we set
3402 * the animation start to the time of the first frame callback. */
3404 input->cursor_anim_start = 0;
3405 else if (input->cursor_anim_start == 0)
3406 input->cursor_anim_start = time;
3408 if (time == 0 || input->cursor_anim_start == 0)
3411 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3413 if (cursor->image_count > 1) {
3414 input->cursor_frame_cb =
3415 wl_surface_frame(input->pointer_surface);
3416 wl_callback_add_listener(input->cursor_frame_cb,
3417 &pointer_surface_listener, input);
3420 input_set_pointer_image_index(input, i);
3423 static const struct wl_callback_listener pointer_surface_listener = {
3424 pointer_surface_frame_callback
3428 input_set_pointer_image(struct input *input, int pointer)
3432 if (!input->pointer)
3435 if (input->pointer_enter_serial > input->cursor_serial)
3438 if (!force && pointer == input->current_cursor)
3441 input->current_cursor = pointer;
3442 input->cursor_serial = input->pointer_enter_serial;
3443 if (!input->cursor_frame_cb)
3444 pointer_surface_frame_callback(input, NULL, 0);
3446 /* The current frame callback may be stuck if, for instance,
3447 * the set cursor request was processed by the server after
3448 * this client lost the focus. In this case the cursor surface
3449 * might not be mapped and the frame callback wouldn't ever
3450 * complete. Send a set_cursor and attach to try to map the
3451 * cursor surface again so that the callback will finish */
3452 input_set_pointer_image_index(input, 0);
3456 struct wl_data_device *
3457 input_get_data_device(struct input *input)
3459 return input->data_device;
3463 input_set_selection(struct input *input,
3464 struct wl_data_source *source, uint32_t time)
3466 wl_data_device_set_selection(input->data_device, source, time);
3470 input_accept(struct input *input, const char *type)
3472 wl_data_offer_accept(input->drag_offer->offer,
3473 input->pointer_enter_serial, type);
3477 offer_io_func(struct task *task, uint32_t events)
3479 struct data_offer *offer =
3480 container_of(task, struct data_offer, io_task);
3484 len = read(offer->fd, buffer, sizeof buffer);
3485 offer->func(buffer, len,
3486 offer->x, offer->y, offer->user_data);
3490 data_offer_destroy(offer);
3495 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3496 data_func_t func, void *user_data)
3500 if (pipe2(p, O_CLOEXEC) == -1)
3503 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3506 offer->io_task.run = offer_io_func;
3510 offer->user_data = user_data;
3512 display_watch_fd(offer->input->display,
3513 offer->fd, EPOLLIN, &offer->io_task);
3517 input_receive_drag_data(struct input *input, const char *mime_type,
3518 data_func_t func, void *data)
3520 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3521 input->drag_offer->x = input->sx;
3522 input->drag_offer->y = input->sy;
3526 input_receive_drag_data_to_fd(struct input *input,
3527 const char *mime_type, int fd)
3529 if (input->drag_offer)
3530 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3536 input_receive_selection_data(struct input *input, const char *mime_type,
3537 data_func_t func, void *data)
3541 if (input->selection_offer == NULL)
3544 for (p = input->selection_offer->types.data; *p; p++)
3545 if (strcmp(mime_type, *p) == 0)
3551 data_offer_receive_data(input->selection_offer,
3552 mime_type, func, data);
3557 input_receive_selection_data_to_fd(struct input *input,
3558 const char *mime_type, int fd)
3560 if (input->selection_offer)
3561 wl_data_offer_receive(input->selection_offer->offer,
3568 window_move(struct window *window, struct input *input, uint32_t serial)
3570 if (!window->shell_surface)
3573 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3577 window_touch_move(struct window *window, struct input *input, uint32_t serial)
3579 if (!window->shell_surface)
3582 wl_shell_surface_move(window->shell_surface, input->seat,
3583 window->display->serial);
3587 surface_set_synchronized(struct surface *surface)
3589 if (!surface->subsurface)
3592 if (surface->synchronized)
3595 wl_subsurface_set_sync(surface->subsurface);
3596 surface->synchronized = 1;
3600 surface_set_synchronized_default(struct surface *surface)
3602 if (!surface->subsurface)
3605 if (surface->synchronized == surface->synchronized_default)
3608 if (surface->synchronized_default)
3609 wl_subsurface_set_sync(surface->subsurface);
3611 wl_subsurface_set_desync(surface->subsurface);
3613 surface->synchronized = surface->synchronized_default;
3617 surface_resize(struct surface *surface)
3619 struct widget *widget = surface->widget;
3620 struct wl_compositor *compositor = widget->window->display->compositor;
3622 if (surface->input_region) {
3623 wl_region_destroy(surface->input_region);
3624 surface->input_region = NULL;
3627 if (surface->opaque_region)
3628 wl_region_destroy(surface->opaque_region);
3630 surface->opaque_region = wl_compositor_create_region(compositor);
3632 if (widget->resize_handler)
3633 widget->resize_handler(widget,
3634 widget->allocation.width,
3635 widget->allocation.height,
3638 if (surface->subsurface &&
3639 (surface->allocation.x != widget->allocation.x ||
3640 surface->allocation.y != widget->allocation.y)) {
3641 wl_subsurface_set_position(surface->subsurface,
3642 widget->allocation.x,
3643 widget->allocation.y);
3645 if (surface->allocation.width != widget->allocation.width ||
3646 surface->allocation.height != widget->allocation.height) {
3647 window_schedule_redraw(widget->window);
3649 surface->allocation = widget->allocation;
3652 wl_region_add(surface->opaque_region, 0, 0,
3653 widget->allocation.width,
3654 widget->allocation.height);
3658 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3661 * This hack should be removed, when EGL respects
3662 * eglSwapInterval(0).
3664 * If this window has sub-surfaces, especially a free-running
3665 * EGL-widget, we need to post the parent surface once with
3666 * all the old state to guarantee, that the EGL-widget will
3667 * receive its frame callback soon. Otherwise, a forced call
3668 * to eglSwapBuffers may end up blocking, waiting for a frame
3669 * event that will never come, because we will commit the parent
3670 * surface with all new state only after eglSwapBuffers returns.
3672 * This assumes, that:
3673 * 1. When the EGL widget's resize hook is called, it pauses.
3674 * 2. When the EGL widget's redraw hook is called, it forces a
3675 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3676 * In a single threaded application condition 1 is a no-op.
3678 * XXX: This should actually be after the surface_resize() calls,
3679 * but cannot, because then it would commit the incomplete state
3680 * accumulated from the widget resize hooks.
3682 if (window->subsurface_list.next != &window->main_surface->link ||
3683 window->subsurface_list.prev != &window->main_surface->link)
3684 wl_surface_commit(window->main_surface->surface);
3688 idle_resize(struct window *window)
3690 struct surface *surface;
3692 window->resize_needed = 0;
3693 window->redraw_needed = 1;
3695 DBG("from %dx%d to %dx%d\n",
3696 window->main_surface->server_allocation.width,
3697 window->main_surface->server_allocation.height,
3698 window->pending_allocation.width,
3699 window->pending_allocation.height);
3701 hack_prevent_EGL_sub_surface_deadlock(window);
3703 widget_set_allocation(window->main_surface->widget,
3704 window->pending_allocation.x,
3705 window->pending_allocation.y,
3706 window->pending_allocation.width,
3707 window->pending_allocation.height);
3709 surface_resize(window->main_surface);
3711 /* The main surface is in the list, too. Main surface's
3712 * resize_handler is responsible for calling widget_set_allocation()
3713 * on all sub-surface root widgets, so they will be resized
3716 wl_list_for_each(surface, &window->subsurface_list, link) {
3717 if (surface == window->main_surface)
3720 surface_set_synchronized(surface);
3721 surface_resize(surface);
3726 window_schedule_resize(struct window *window, int width, int height)
3728 window->pending_allocation.x = 0;
3729 window->pending_allocation.y = 0;
3730 window->pending_allocation.width = width;
3731 window->pending_allocation.height = height;
3733 if (window->min_allocation.width == 0)
3734 window->min_allocation = window->pending_allocation;
3735 if (window->pending_allocation.width < window->min_allocation.width)
3736 window->pending_allocation.width = window->min_allocation.width;
3737 if (window->pending_allocation.height < window->min_allocation.height)
3738 window->pending_allocation.height = window->min_allocation.height;
3740 window->resize_needed = 1;
3741 window_schedule_redraw(window);
3745 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3747 window_schedule_resize(widget->window, width, height);
3751 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3754 wl_shell_surface_pong(shell_surface, serial);
3758 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3759 uint32_t edges, int32_t width, int32_t height)
3761 struct window *window = data;
3763 window->resize_edges = edges;
3764 window_schedule_resize(window, width, height);
3768 menu_destroy(struct menu *menu)
3770 widget_destroy(menu->widget);
3771 window_destroy(menu->window);
3776 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3778 struct window *window = data;
3779 struct menu *menu = window->main_surface->widget->user_data;
3781 /* FIXME: Need more context in this event, at least the input
3782 * device. Or just use wl_callback. And this really needs to
3783 * be a window vfunc that the menu can set. And we need the
3786 input_ungrab(menu->input);
3790 static const struct wl_shell_surface_listener shell_surface_listener = {
3797 window_get_allocation(struct window *window,
3798 struct rectangle *allocation)
3800 *allocation = window->main_surface->allocation;
3804 widget_redraw(struct widget *widget)
3806 struct widget *child;
3808 if (widget->redraw_handler)
3809 widget->redraw_handler(widget, widget->user_data);
3810 wl_list_for_each(child, &widget->child_list, link)
3811 widget_redraw(child);
3815 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3817 struct surface *surface = data;
3819 assert(callback == surface->frame_cb);
3820 DBG_OBJ(callback, "done\n");
3821 wl_callback_destroy(callback);
3822 surface->frame_cb = NULL;
3824 surface->last_time = time;
3826 if (surface->redraw_needed || surface->window->redraw_needed) {
3827 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3828 window_schedule_redraw_task(surface->window);
3832 static const struct wl_callback_listener listener = {
3837 surface_redraw(struct surface *surface)
3839 DBG_OBJ(surface->surface, "begin\n");
3841 if (!surface->window->redraw_needed && !surface->redraw_needed)
3844 /* Whole-window redraw forces a redraw even if the previous has
3845 * not yet hit the screen.
3847 if (surface->frame_cb) {
3848 if (!surface->window->redraw_needed)
3851 DBG_OBJ(surface->frame_cb, "cancelled\n");
3852 wl_callback_destroy(surface->frame_cb);
3855 surface->frame_cb = wl_surface_frame(surface->surface);
3856 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3857 DBG_OBJ(surface->frame_cb, "new\n");
3859 surface->redraw_needed = 0;
3860 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3861 widget_redraw(surface->widget);
3862 DBG_OBJ(surface->surface, "done\n");
3866 idle_redraw(struct task *task, uint32_t events)
3868 struct window *window = container_of(task, struct window, redraw_task);
3869 struct surface *surface;
3871 DBG(" --------- \n");
3873 wl_list_init(&window->redraw_task.link);
3874 window->redraw_task_scheduled = 0;
3876 if (window->resize_needed) {
3877 /* throttle resizing to the main surface display */
3878 if (window->main_surface->frame_cb) {
3879 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
3883 idle_resize(window);
3886 wl_list_for_each(surface, &window->subsurface_list, link)
3887 surface_redraw(surface);
3889 window->redraw_needed = 0;
3890 window_flush(window);
3892 wl_list_for_each(surface, &window->subsurface_list, link)
3893 surface_set_synchronized_default(surface);
3897 window_schedule_redraw_task(struct window *window)
3899 if (window->configure_requests)
3901 if (!window->redraw_task_scheduled) {
3902 window->redraw_task.run = idle_redraw;
3903 display_defer(window->display, &window->redraw_task);
3904 window->redraw_task_scheduled = 1;
3909 window_schedule_redraw(struct window *window)
3911 struct surface *surface;
3913 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3915 wl_list_for_each(surface, &window->subsurface_list, link)
3916 surface->redraw_needed = 1;
3918 window_schedule_redraw_task(window);
3922 window_is_fullscreen(struct window *window)
3924 return window->type == TYPE_FULLSCREEN;
3928 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3930 struct window *window = data;
3932 wl_callback_destroy(callback);
3933 window->configure_requests--;
3935 if (!window->configure_requests)
3936 window_schedule_redraw(window);
3939 static struct wl_callback_listener configure_request_listener = {
3940 configure_request_completed,
3944 window_defer_redraw_until_configure(struct window* window)
3946 struct wl_callback *callback;
3948 if (window->redraw_task_scheduled) {
3949 wl_list_remove(&window->redraw_task.link);
3950 window->redraw_task_scheduled = 0;
3953 callback = wl_display_sync(window->display->display);
3954 wl_callback_add_listener(callback, &configure_request_listener, window);
3955 window->configure_requests++;
3959 window_set_fullscreen(struct window *window, int fullscreen)
3961 if (!window->display->shell)
3964 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3968 window->saved_type = window->type;
3969 if (window->type == TYPE_TOPLEVEL) {
3970 window->saved_allocation = window->main_surface->allocation;
3972 window->type = TYPE_FULLSCREEN;
3973 wl_shell_surface_set_fullscreen(window->shell_surface,
3974 window->fullscreen_method,
3976 window_defer_redraw_until_configure (window);
3978 if (window->saved_type == TYPE_MAXIMIZED) {
3979 window_set_maximized(window, 1);
3981 window->type = TYPE_TOPLEVEL;
3982 wl_shell_surface_set_toplevel(window->shell_surface);
3983 window_schedule_resize(window,
3984 window->saved_allocation.width,
3985 window->saved_allocation.height);
3992 window_set_fullscreen_method(struct window *window,
3993 enum wl_shell_surface_fullscreen_method method)
3995 window->fullscreen_method = method;
3999 window_is_maximized(struct window *window)
4001 return window->type == TYPE_MAXIMIZED;
4005 window_set_maximized(struct window *window, int maximized)
4007 if (!window->display->shell)
4010 if ((window->type == TYPE_MAXIMIZED) == maximized)
4013 if (window->type == TYPE_TOPLEVEL) {
4014 window->saved_allocation = window->main_surface->allocation;
4015 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4016 window->type = TYPE_MAXIMIZED;
4017 window_defer_redraw_until_configure(window);
4018 } else if (window->type == TYPE_FULLSCREEN) {
4019 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4020 window->type = TYPE_MAXIMIZED;
4021 window_defer_redraw_until_configure(window);
4023 wl_shell_surface_set_toplevel(window->shell_surface);
4024 window->type = TYPE_TOPLEVEL;
4025 window_schedule_resize(window,
4026 window->saved_allocation.width,
4027 window->saved_allocation.height);
4032 window_set_user_data(struct window *window, void *data)
4034 window->user_data = data;
4038 window_get_user_data(struct window *window)
4040 return window->user_data;
4044 window_set_key_handler(struct window *window,
4045 window_key_handler_t handler)
4047 window->key_handler = handler;
4051 window_set_keyboard_focus_handler(struct window *window,
4052 window_keyboard_focus_handler_t handler)
4054 window->keyboard_focus_handler = handler;
4058 window_set_data_handler(struct window *window, window_data_handler_t handler)
4060 window->data_handler = handler;
4064 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4066 window->drop_handler = handler;
4070 window_set_close_handler(struct window *window,
4071 window_close_handler_t handler)
4073 window->close_handler = handler;
4077 window_set_fullscreen_handler(struct window *window,
4078 window_fullscreen_handler_t handler)
4080 window->fullscreen_handler = handler;
4084 window_set_output_handler(struct window *window,
4085 window_output_handler_t handler)
4087 window->output_handler = handler;
4091 window_set_title(struct window *window, const char *title)
4093 free(window->title);
4094 window->title = strdup(title);
4095 if (window->frame) {
4096 frame_set_title(window->frame->frame, title);
4097 widget_schedule_redraw(window->frame->widget);
4099 if (window->shell_surface)
4100 wl_shell_surface_set_title(window->shell_surface, title);
4104 window_get_title(struct window *window)
4106 return window->title;
4110 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4112 struct text_cursor_position *text_cursor_position =
4113 window->display->text_cursor_position;
4115 if (!text_cursor_position)
4118 text_cursor_position_notify(text_cursor_position,
4119 window->main_surface->surface,
4120 wl_fixed_from_int(x),
4121 wl_fixed_from_int(y));
4125 window_damage(struct window *window, int32_t x, int32_t y,
4126 int32_t width, int32_t height)
4128 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4132 surface_enter(void *data,
4133 struct wl_surface *wl_surface, struct wl_output *wl_output)
4135 struct window *window = data;
4136 struct output *output;
4137 struct output *output_found = NULL;
4138 struct window_output *window_output;
4140 wl_list_for_each(output, &window->display->output_list, link) {
4141 if (output->output == wl_output) {
4142 output_found = output;
4150 window_output = xmalloc(sizeof *window_output);
4151 window_output->output = output_found;
4153 wl_list_insert (&window->window_output_list, &window_output->link);
4155 if (window->output_handler)
4156 window->output_handler(window, output_found, 1,
4161 surface_leave(void *data,
4162 struct wl_surface *wl_surface, struct wl_output *output)
4164 struct window *window = data;
4165 struct window_output *window_output;
4166 struct window_output *window_output_found = NULL;
4168 wl_list_for_each(window_output, &window->window_output_list, link) {
4169 if (window_output->output->output == output) {
4170 window_output_found = window_output;
4175 if (window_output_found) {
4176 wl_list_remove(&window_output_found->link);
4178 if (window->output_handler)
4179 window->output_handler(window, window_output->output,
4180 0, window->user_data);
4182 free(window_output_found);
4186 static const struct wl_surface_listener surface_listener = {
4191 static struct surface *
4192 surface_create(struct window *window)
4194 struct display *display = window->display;
4195 struct surface *surface;
4197 surface = xmalloc(sizeof *surface);
4198 memset(surface, 0, sizeof *surface);
4202 surface->window = window;
4203 surface->surface = wl_compositor_create_surface(display->compositor);
4204 surface->buffer_scale = 1;
4205 wl_surface_add_listener(surface->surface, &surface_listener, window);
4207 wl_list_insert(&window->subsurface_list, &surface->link);
4212 static struct window *
4213 window_create_internal(struct display *display,
4214 struct window *parent, int type)
4216 struct window *window;
4217 struct surface *surface;
4219 window = xzalloc(sizeof *window);
4220 wl_list_init(&window->subsurface_list);
4221 window->display = display;
4222 window->parent = parent;
4224 surface = surface_create(window);
4225 window->main_surface = surface;
4227 if (type != TYPE_CUSTOM && display->shell) {
4228 window->shell_surface =
4229 wl_shell_get_shell_surface(display->shell,
4231 fail_on_null(window->shell_surface);
4234 window->type = type;
4235 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4236 window->configure_requests = 0;
4237 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4239 if (display->argb_device)
4240 #ifdef HAVE_CAIRO_EGL
4241 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4243 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4246 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4248 wl_surface_set_user_data(surface->surface, window);
4249 wl_list_insert(display->window_list.prev, &window->link);
4250 wl_list_init(&window->redraw_task.link);
4252 if (window->shell_surface) {
4253 wl_shell_surface_set_user_data(window->shell_surface, window);
4254 wl_shell_surface_add_listener(window->shell_surface,
4255 &shell_surface_listener, window);
4258 wl_list_init (&window->window_output_list);
4264 window_create(struct display *display)
4266 return window_create_internal(display, NULL, TYPE_NONE);
4270 window_create_custom(struct display *display)
4272 return window_create_internal(display, NULL, TYPE_CUSTOM);
4276 window_create_transient(struct display *display, struct window *parent,
4277 int32_t x, int32_t y, uint32_t flags)
4279 struct window *window;
4281 window = window_create_internal(parent->display,
4282 parent, TYPE_TRANSIENT);
4288 wl_shell_surface_set_transient(
4289 window->shell_surface,
4290 window->parent->main_surface->surface,
4291 window->x, window->y, flags);
4297 menu_set_item(struct menu *menu, int sy)
4301 next = (sy - 8) / 20;
4302 if (menu->current != next) {
4303 menu->current = next;
4304 widget_schedule_redraw(menu->widget);
4309 menu_motion_handler(struct widget *widget,
4310 struct input *input, uint32_t time,
4311 float x, float y, void *data)
4313 struct menu *menu = data;
4315 if (widget == menu->widget)
4316 menu_set_item(data, y);
4318 return CURSOR_LEFT_PTR;
4322 menu_enter_handler(struct widget *widget,
4323 struct input *input, float x, float y, void *data)
4325 struct menu *menu = data;
4327 if (widget == menu->widget)
4328 menu_set_item(data, y);
4330 return CURSOR_LEFT_PTR;
4334 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4336 struct menu *menu = data;
4338 if (widget == menu->widget)
4339 menu_set_item(data, -200);
4343 menu_button_handler(struct widget *widget,
4344 struct input *input, uint32_t time,
4345 uint32_t button, enum wl_pointer_button_state state,
4349 struct menu *menu = data;
4351 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4352 (menu->release_count > 0 || time - menu->time > 500)) {
4353 /* Either relase after press-drag-release or
4354 * click-motion-click. */
4355 menu->func(menu->window->parent,
4356 menu->current, menu->window->parent->user_data);
4357 input_ungrab(input);
4359 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4360 menu->release_count++;
4365 menu_redraw_handler(struct widget *widget, void *data)
4368 const int32_t r = 3, margin = 3;
4369 struct menu *menu = data;
4370 int32_t width, height, i;
4372 cr = widget_cairo_create(widget);
4373 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4374 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4377 width = widget->allocation.width;
4378 height = widget->allocation.height;
4379 rounded_rect(cr, 0, 0, width, height, r);
4381 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
4382 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4385 for (i = 0; i < menu->count; i++) {
4386 if (i == menu->current) {
4387 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4388 cairo_rectangle(cr, margin, i * 20 + margin,
4389 width - 2 * margin, 20);
4391 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4392 cairo_move_to(cr, 10, i * 20 + 16);
4393 cairo_show_text(cr, menu->entries[i]);
4395 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4396 cairo_move_to(cr, 10, i * 20 + 16);
4397 cairo_show_text(cr, menu->entries[i]);
4405 window_show_menu(struct display *display,
4406 struct input *input, uint32_t time, struct window *parent,
4407 int32_t x, int32_t y,
4408 menu_func_t func, const char **entries, int count)
4410 struct window *window;
4412 const int32_t margin = 3;
4414 menu = malloc(sizeof *menu);
4418 window = window_create_internal(parent->display, parent, TYPE_MENU);
4424 menu->window = window;
4425 menu->widget = window_add_widget(menu->window, menu);
4426 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4427 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4428 menu->entries = entries;
4429 menu->count = count;
4430 menu->release_count = 0;
4434 menu->input = input;
4435 window->type = TYPE_MENU;
4439 input_ungrab(input);
4440 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4441 display_get_serial(window->display),
4442 window->parent->main_surface->surface,
4443 window->x, window->y, 0);
4445 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4446 widget_set_enter_handler(menu->widget, menu_enter_handler);
4447 widget_set_leave_handler(menu->widget, menu_leave_handler);
4448 widget_set_motion_handler(menu->widget, menu_motion_handler);
4449 widget_set_button_handler(menu->widget, menu_button_handler);
4451 input_grab(input, menu->widget, 0);
4452 window_schedule_resize(window, 200, count * 20 + margin * 2);
4456 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4458 window->main_surface->buffer_type = type;
4462 window_set_preferred_format(struct window *window,
4463 enum preferred_format format)
4465 window->preferred_format = format;
4469 window_add_subsurface(struct window *window, void *data,
4470 enum subsurface_mode default_mode)
4472 struct widget *widget;
4473 struct surface *surface;
4474 struct wl_surface *parent;
4475 struct wl_subcompositor *subcompo = window->display->subcompositor;
4477 surface = surface_create(window);
4478 widget = widget_create(window, surface, data);
4479 wl_list_init(&widget->link);
4480 surface->widget = widget;
4482 parent = window->main_surface->surface;
4483 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4486 surface->synchronized = 1;
4488 switch (default_mode) {
4489 case SUBSURFACE_SYNCHRONIZED:
4490 surface->synchronized_default = 1;
4492 case SUBSURFACE_DESYNCHRONIZED:
4493 surface->synchronized_default = 0;
4496 assert(!"bad enum subsurface_mode");
4503 display_handle_geometry(void *data,
4504 struct wl_output *wl_output,
4507 int physical_height,
4513 struct output *output = data;
4515 output->allocation.x = x;
4516 output->allocation.y = y;
4517 output->transform = transform;
4521 display_handle_done(void *data,
4522 struct wl_output *wl_output)
4527 display_handle_scale(void *data,
4528 struct wl_output *wl_output,
4531 struct output *output = data;
4533 output->scale = scale;
4537 display_handle_mode(void *data,
4538 struct wl_output *wl_output,
4544 struct output *output = data;
4545 struct display *display = output->display;
4547 if (flags & WL_OUTPUT_MODE_CURRENT) {
4548 output->allocation.width = width;
4549 output->allocation.height = height;
4550 if (display->output_configure_handler)
4551 (*display->output_configure_handler)(
4552 output, display->user_data);
4556 static const struct wl_output_listener output_listener = {
4557 display_handle_geometry,
4558 display_handle_mode,
4559 display_handle_done,
4560 display_handle_scale
4564 display_add_output(struct display *d, uint32_t id)
4566 struct output *output;
4568 output = xzalloc(sizeof *output);
4569 output->display = d;
4572 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4573 wl_list_insert(d->output_list.prev, &output->link);
4575 wl_output_add_listener(output->output, &output_listener, output);
4579 output_destroy(struct output *output)
4581 if (output->destroy_handler)
4582 (*output->destroy_handler)(output, output->user_data);
4584 wl_output_destroy(output->output);
4585 wl_list_remove(&output->link);
4590 display_set_global_handler(struct display *display,
4591 display_global_handler_t handler)
4593 struct global *global;
4595 display->global_handler = handler;
4599 wl_list_for_each(global, &display->global_list, link)
4600 display->global_handler(display,
4601 global->name, global->interface,
4602 global->version, display->user_data);
4606 display_set_output_configure_handler(struct display *display,
4607 display_output_handler_t handler)
4609 struct output *output;
4611 display->output_configure_handler = handler;
4615 wl_list_for_each(output, &display->output_list, link) {
4616 if (output->allocation.width == 0 &&
4617 output->allocation.height == 0)
4620 (*display->output_configure_handler)(output,
4621 display->user_data);
4626 output_set_user_data(struct output *output, void *data)
4628 output->user_data = data;
4632 output_get_user_data(struct output *output)
4634 return output->user_data;
4638 output_set_destroy_handler(struct output *output,
4639 display_output_handler_t handler)
4641 output->destroy_handler = handler;
4642 /* FIXME: implement this, once we have way to remove outputs */
4646 output_get_allocation(struct output *output, struct rectangle *base)
4648 struct rectangle allocation = output->allocation;
4650 switch (output->transform) {
4651 case WL_OUTPUT_TRANSFORM_90:
4652 case WL_OUTPUT_TRANSFORM_270:
4653 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4654 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4655 /* Swap width and height */
4656 allocation.width = output->allocation.height;
4657 allocation.height = output->allocation.width;
4665 output_get_wl_output(struct output *output)
4667 return output->output;
4670 enum wl_output_transform
4671 output_get_transform(struct output *output)
4673 return output->transform;
4677 output_get_scale(struct output *output)
4679 return output->scale;
4683 fini_xkb(struct input *input)
4685 xkb_state_unref(input->xkb.state);
4686 xkb_map_unref(input->xkb.keymap);
4689 #define MAX(a,b) ((a) > (b) ? a : b)
4692 display_add_input(struct display *d, uint32_t id)
4694 struct input *input;
4696 input = xzalloc(sizeof *input);
4698 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
4699 MAX(d->seat_version, 3));
4700 input->touch_focus = NULL;
4701 input->pointer_focus = NULL;
4702 input->keyboard_focus = NULL;
4703 wl_list_init(&input->touch_point_list);
4704 wl_list_insert(d->input_list.prev, &input->link);
4706 wl_seat_add_listener(input->seat, &seat_listener, input);
4707 wl_seat_set_user_data(input->seat, input);
4709 input->data_device =
4710 wl_data_device_manager_get_data_device(d->data_device_manager,
4712 wl_data_device_add_listener(input->data_device, &data_device_listener,
4715 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4717 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4718 TFD_CLOEXEC | TFD_NONBLOCK);
4719 input->repeat_task.run = keyboard_repeat_func;
4720 display_watch_fd(d, input->repeat_timer_fd,
4721 EPOLLIN, &input->repeat_task);
4725 input_destroy(struct input *input)
4727 input_remove_keyboard_focus(input);
4728 input_remove_pointer_focus(input);
4730 if (input->drag_offer)
4731 data_offer_destroy(input->drag_offer);
4733 if (input->selection_offer)
4734 data_offer_destroy(input->selection_offer);
4736 wl_data_device_destroy(input->data_device);
4738 if (input->display->seat_version >= 3) {
4740 wl_pointer_release(input->pointer);
4741 if (input->keyboard)
4742 wl_keyboard_release(input->keyboard);
4747 wl_surface_destroy(input->pointer_surface);
4749 wl_list_remove(&input->link);
4750 wl_seat_destroy(input->seat);
4751 close(input->repeat_timer_fd);
4756 init_workspace_manager(struct display *d, uint32_t id)
4758 d->workspace_manager =
4759 wl_registry_bind(d->registry, id,
4760 &workspace_manager_interface, 1);
4761 if (d->workspace_manager != NULL)
4762 workspace_manager_add_listener(d->workspace_manager,
4763 &workspace_manager_listener,
4768 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4770 struct display *d = data;
4772 if (format == WL_SHM_FORMAT_RGB565)
4776 struct wl_shm_listener shm_listener = {
4781 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4782 const char *interface, uint32_t version)
4784 struct display *d = data;
4785 struct global *global;
4787 global = xmalloc(sizeof *global);
4789 global->interface = strdup(interface);
4790 global->version = version;
4791 wl_list_insert(d->global_list.prev, &global->link);
4793 if (strcmp(interface, "wl_compositor") == 0) {
4794 d->compositor = wl_registry_bind(registry, id,
4795 &wl_compositor_interface, 3);
4796 } else if (strcmp(interface, "wl_output") == 0) {
4797 display_add_output(d, id);
4798 } else if (strcmp(interface, "wl_seat") == 0) {
4799 d->seat_version = version;
4800 display_add_input(d, id);
4801 } else if (strcmp(interface, "wl_shell") == 0) {
4802 d->shell = wl_registry_bind(registry,
4803 id, &wl_shell_interface, 1);
4804 } else if (strcmp(interface, "wl_shm") == 0) {
4805 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4806 wl_shm_add_listener(d->shm, &shm_listener, d);
4807 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4808 d->data_device_manager =
4809 wl_registry_bind(registry, id,
4810 &wl_data_device_manager_interface, 1);
4811 } else if (strcmp(interface, "text_cursor_position") == 0) {
4812 d->text_cursor_position =
4813 wl_registry_bind(registry, id,
4814 &text_cursor_position_interface, 1);
4815 } else if (strcmp(interface, "workspace_manager") == 0) {
4816 init_workspace_manager(d, id);
4817 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4819 wl_registry_bind(registry, id,
4820 &wl_subcompositor_interface, 1);
4823 if (d->global_handler)
4824 d->global_handler(d, id, interface, version, d->user_data);
4828 registry_handle_global_remove(void *data, struct wl_registry *registry,
4831 struct display *d = data;
4832 struct global *global;
4835 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4836 if (global->name != name)
4839 /* XXX: Should destroy bound globals, and call
4840 * the counterpart of display::global_handler
4842 wl_list_remove(&global->link);
4843 free(global->interface);
4849 display_bind(struct display *display, uint32_t name,
4850 const struct wl_interface *interface, uint32_t version)
4852 return wl_registry_bind(display->registry, name, interface, version);
4855 static const struct wl_registry_listener registry_listener = {
4856 registry_handle_global,
4857 registry_handle_global_remove
4860 #ifdef HAVE_CAIRO_EGL
4862 init_egl(struct display *d)
4864 EGLint major, minor;
4867 #ifdef USE_CAIRO_GLESV2
4868 # define GL_BIT EGL_OPENGL_ES2_BIT
4870 # define GL_BIT EGL_OPENGL_BIT
4873 static const EGLint argb_cfg_attribs[] = {
4874 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4880 EGL_RENDERABLE_TYPE, GL_BIT,
4884 #ifdef USE_CAIRO_GLESV2
4885 static const EGLint context_attribs[] = {
4886 EGL_CONTEXT_CLIENT_VERSION, 2,
4889 EGLint api = EGL_OPENGL_ES_API;
4891 EGLint *context_attribs = NULL;
4892 EGLint api = EGL_OPENGL_API;
4895 d->dpy = eglGetDisplay(d->display);
4896 if (!eglInitialize(d->dpy, &major, &minor)) {
4897 fprintf(stderr, "failed to initialize EGL\n");
4901 if (!eglBindAPI(api)) {
4902 fprintf(stderr, "failed to bind EGL client API\n");
4906 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4907 &d->argb_config, 1, &n) || n != 1) {
4908 fprintf(stderr, "failed to choose argb EGL config\n");
4912 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
4913 EGL_NO_CONTEXT, context_attribs);
4914 if (d->argb_ctx == NULL) {
4915 fprintf(stderr, "failed to create EGL context\n");
4919 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4920 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4921 fprintf(stderr, "failed to get cairo EGL argb device\n");
4929 fini_egl(struct display *display)
4931 cairo_device_destroy(display->argb_device);
4933 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4936 eglTerminate(display->dpy);
4942 init_dummy_surface(struct display *display)
4947 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4949 display->dummy_surface =
4950 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4952 display->dummy_surface_data = data;
4956 handle_display_data(struct task *task, uint32_t events)
4958 struct display *display =
4959 container_of(task, struct display, display_task);
4960 struct epoll_event ep;
4963 display->display_fd_events = events;
4965 if (events & EPOLLERR || events & EPOLLHUP) {
4966 display_exit(display);
4970 if (events & EPOLLIN) {
4971 ret = wl_display_dispatch(display->display);
4973 display_exit(display);
4978 if (events & EPOLLOUT) {
4979 ret = wl_display_flush(display->display);
4981 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4982 ep.data.ptr = &display->display_task;
4983 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4984 display->display_fd, &ep);
4985 } else if (ret == -1 && errno != EAGAIN) {
4986 display_exit(display);
4993 log_handler(const char *format, va_list args)
4995 vfprintf(stderr, format, args);
4999 display_create(int *argc, char *argv[])
5003 wl_log_set_handler_client(log_handler);
5005 d = zalloc(sizeof *d);
5009 d->display = wl_display_connect(NULL);
5010 if (d->display == NULL) {
5011 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5016 d->xkb_context = xkb_context_new(0);
5017 if (d->xkb_context == NULL) {
5018 fprintf(stderr, "Failed to create XKB context\n");
5023 d->epoll_fd = os_epoll_create_cloexec();
5024 d->display_fd = wl_display_get_fd(d->display);
5025 d->display_task.run = handle_display_data;
5026 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5029 wl_list_init(&d->deferred_list);
5030 wl_list_init(&d->input_list);
5031 wl_list_init(&d->output_list);
5032 wl_list_init(&d->global_list);
5035 d->workspace_count = 1;
5037 d->registry = wl_display_get_registry(d->display);
5038 wl_registry_add_listener(d->registry, ®istry_listener, d);
5040 if (wl_display_dispatch(d->display) < 0) {
5041 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5045 #ifdef HAVE_CAIRO_EGL
5046 if (init_egl(d) < 0)
5047 fprintf(stderr, "EGL does not seem to work, "
5048 "falling back to software rendering and wl_shm.\n");
5053 d->theme = theme_create();
5055 wl_list_init(&d->window_list);
5057 init_dummy_surface(d);
5063 display_destroy_outputs(struct display *display)
5066 struct output *output;
5068 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5069 output_destroy(output);
5073 display_destroy_inputs(struct display *display)
5076 struct input *input;
5078 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5079 input_destroy(input);
5083 display_destroy(struct display *display)
5085 if (!wl_list_empty(&display->window_list))
5086 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5087 wl_list_length(&display->window_list));
5089 if (!wl_list_empty(&display->deferred_list))
5090 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5092 cairo_surface_destroy(display->dummy_surface);
5093 free(display->dummy_surface_data);
5095 display_destroy_outputs(display);
5096 display_destroy_inputs(display);
5098 xkb_context_unref(display->xkb_context);
5100 theme_destroy(display->theme);
5101 destroy_cursors(display);
5103 #ifdef HAVE_CAIRO_EGL
5104 if (display->argb_device)
5108 if (display->subcompositor)
5109 wl_subcompositor_destroy(display->subcompositor);
5112 wl_shell_destroy(display->shell);
5115 wl_shm_destroy(display->shm);
5117 if (display->data_device_manager)
5118 wl_data_device_manager_destroy(display->data_device_manager);
5120 wl_compositor_destroy(display->compositor);
5121 wl_registry_destroy(display->registry);
5123 close(display->epoll_fd);
5125 if (!(display->display_fd_events & EPOLLERR) &&
5126 !(display->display_fd_events & EPOLLHUP))
5127 wl_display_flush(display->display);
5129 wl_display_disconnect(display->display);
5134 display_set_user_data(struct display *display, void *data)
5136 display->user_data = data;
5140 display_get_user_data(struct display *display)
5142 return display->user_data;
5146 display_get_display(struct display *display)
5148 return display->display;
5152 display_has_subcompositor(struct display *display)
5154 if (display->subcompositor)
5157 wl_display_roundtrip(display->display);
5159 return display->subcompositor != NULL;
5163 display_get_cairo_device(struct display *display)
5165 return display->argb_device;
5169 display_get_output(struct display *display)
5171 return container_of(display->output_list.next, struct output, link);
5174 struct wl_compositor *
5175 display_get_compositor(struct display *display)
5177 return display->compositor;
5181 display_get_serial(struct display *display)
5183 return display->serial;
5187 display_get_egl_display(struct display *d)
5192 struct wl_data_source *
5193 display_create_data_source(struct display *display)
5195 return wl_data_device_manager_create_data_source(display->data_device_manager);
5199 display_get_argb_egl_config(struct display *d)
5201 return d->argb_config;
5205 display_get_shell(struct display *display)
5207 return display->shell;
5211 display_acquire_window_surface(struct display *display,
5212 struct window *window,
5215 struct surface *surface = window->main_surface;
5217 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5220 widget_get_cairo_surface(window->main_surface->widget);
5221 return surface->toysurface->acquire(surface->toysurface, ctx);
5225 display_release_window_surface(struct display *display,
5226 struct window *window)
5228 struct surface *surface = window->main_surface;
5230 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5233 surface->toysurface->release(surface->toysurface);
5237 display_defer(struct display *display, struct task *task)
5239 wl_list_insert(&display->deferred_list, &task->link);
5243 display_watch_fd(struct display *display,
5244 int fd, uint32_t events, struct task *task)
5246 struct epoll_event ep;
5250 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5254 display_unwatch_fd(struct display *display, int fd)
5256 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5260 display_run(struct display *display)
5263 struct epoll_event ep[16];
5266 display->running = 1;
5268 while (!wl_list_empty(&display->deferred_list)) {
5269 task = container_of(display->deferred_list.prev,
5271 wl_list_remove(&task->link);
5275 wl_display_dispatch_pending(display->display);
5277 if (!display->running)
5280 ret = wl_display_flush(display->display);
5281 if (ret < 0 && errno == EAGAIN) {
5283 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5284 ep[0].data.ptr = &display->display_task;
5286 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5287 display->display_fd, &ep[0]);
5288 } else if (ret < 0) {
5292 count = epoll_wait(display->epoll_fd,
5293 ep, ARRAY_LENGTH(ep), -1);
5294 for (i = 0; i < count; i++) {
5295 task = ep[i].data.ptr;
5296 task->run(task, ep[i].events);
5302 display_exit(struct display *display)
5304 display->running = 0;
5308 keysym_modifiers_add(struct wl_array *modifiers_map,
5311 size_t len = strlen(name) + 1;
5314 p = wl_array_add(modifiers_map, len);
5319 strncpy(p, name, len);
5322 static xkb_mod_index_t
5323 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5326 xkb_mod_index_t index = 0;
5327 char *p = modifiers_map->data;
5329 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5330 if (strcmp(p, name) == 0)
5337 return XKB_MOD_INVALID;
5341 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5344 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5346 if (index == XKB_MOD_INVALID)
5347 return XKB_MOD_INVALID;
5353 fail_on_null(void *p)
5356 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5366 return fail_on_null(malloc(s));
5372 return fail_on_null(zalloc(s));
5376 xstrdup(const char *s)
5378 return fail_on_null(strdup(s));