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
40 #include <sys/epoll.h>
41 #include <sys/timerfd.h>
44 #include <wayland-egl.h>
46 #ifdef USE_CAIRO_GLESV2
47 #include <GLES2/gl2.h>
48 #include <GLES2/gl2ext.h>
53 #include <EGL/eglext.h>
56 #else /* HAVE_CAIRO_EGL */
57 typedef void *EGLDisplay;
58 typedef void *EGLConfig;
59 typedef void *EGLContext;
60 #define EGL_NO_DISPLAY ((EGLDisplay)0)
61 #endif /* no HAVE_CAIRO_EGL */
63 #include <xkbcommon/xkbcommon.h>
64 #include <wayland-cursor.h>
66 #include <linux/input.h>
67 #include <wayland-client.h>
68 #include "../shared/cairo-util.h"
69 #include "xdg-shell-client-protocol.h"
70 #include "text-cursor-position-client-protocol.h"
71 #include "workspaces-client-protocol.h"
72 #include "../shared/os-compatibility.h"
76 #include <sys/types.h>
77 #include "ivi-application-client-protocol.h"
78 #define IVI_SURFACE_ID 9000
90 struct wl_display *display;
91 struct wl_registry *registry;
92 struct wl_compositor *compositor;
93 struct wl_subcompositor *subcompositor;
95 struct wl_data_device_manager *data_device_manager;
96 struct text_cursor_position *text_cursor_position;
97 struct workspace_manager *workspace_manager;
98 struct xdg_shell *xdg_shell;
99 struct ivi_application *ivi_application; /* ivi style shell */
101 EGLConfig argb_config;
103 cairo_device_t *argb_device;
107 uint32_t display_fd_events;
108 struct task display_task;
111 struct wl_list deferred_list;
115 struct wl_list global_list;
116 struct wl_list window_list;
117 struct wl_list input_list;
118 struct wl_list output_list;
122 struct wl_cursor_theme *cursor_theme;
123 struct wl_cursor **cursors;
125 display_output_handler_t output_configure_handler;
126 display_global_handler_t global_handler;
127 display_global_handler_t global_handler_remove;
131 struct xkb_context *xkb_context;
134 uint32_t workspace_count;
136 /* A hack to get text extents for tooltips */
137 cairo_surface_t *dummy_surface;
138 void *dummy_surface_data;
142 struct wl_shell *shell;
144 bool create_ivi_surface;
147 struct window_output {
148 struct output *output;
154 * Prepare the surface for drawing. Makes sure there is a surface
155 * of the right size available for rendering, and returns it.
156 * dx,dy are the x,y of wl_surface.attach.
157 * width,height are the new buffer size.
158 * If flags has SURFACE_HINT_RESIZE set, the user is
159 * doing continuous resizing.
160 * Returns the Cairo surface to draw to.
162 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
163 int32_t width, int32_t height, uint32_t flags,
164 enum wl_output_transform buffer_transform, int32_t buffer_scale);
167 * Post the surface to the server, returning the server allocation
168 * rectangle. The Cairo surface from prepare() must be destroyed
169 * after calling this.
171 void (*swap)(struct toysurface *base,
172 enum wl_output_transform buffer_transform, int32_t buffer_scale,
173 struct rectangle *server_allocation);
176 * Make the toysurface current with the given EGL context.
177 * Returns 0 on success, and negative of failure.
179 int (*acquire)(struct toysurface *base, EGLContext ctx);
182 * Release the toysurface from the EGL context, returning control
185 void (*release)(struct toysurface *base);
188 * Destroy the toysurface, including the Cairo surface, any
189 * backing storage, and the Wayland protocol objects.
191 void (*destroy)(struct toysurface *base);
195 struct window *window;
197 struct wl_surface *surface;
198 struct wl_subsurface *subsurface;
200 int synchronized_default;
201 struct toysurface *toysurface;
202 struct widget *widget;
204 struct wl_callback *frame_cb;
207 struct rectangle allocation;
208 struct rectangle server_allocation;
210 struct wl_region *input_region;
211 struct wl_region *opaque_region;
213 enum window_buffer_type buffer_type;
214 enum wl_output_transform buffer_transform;
215 int32_t buffer_scale;
217 cairo_surface_t *cairo_surface;
223 struct display *display;
224 struct wl_list window_output_list;
226 struct rectangle saved_allocation;
227 struct rectangle min_allocation;
228 struct rectangle pending_allocation;
229 struct rectangle last_geometry;
232 int redraw_task_scheduled;
233 struct task redraw_task;
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;
252 window_state_changed_handler_t state_changed_handler;
254 struct surface *main_surface;
255 struct xdg_surface *xdg_surface;
256 struct xdg_popup *xdg_popup;
257 struct wl_shell_surface *shell_surface;
259 struct window *parent;
260 struct wl_surface *last_parent_surface;
262 struct ivi_surface *ivi_surface;
264 struct window_frame *frame;
266 /* struct surface::link, contains also main_surface */
267 struct wl_list subsurface_list;
274 struct window *window;
275 struct surface *surface;
276 struct tooltip *tooltip;
277 struct wl_list child_list;
279 struct rectangle allocation;
280 widget_resize_handler_t resize_handler;
281 widget_redraw_handler_t redraw_handler;
282 widget_enter_handler_t enter_handler;
283 widget_leave_handler_t leave_handler;
284 widget_motion_handler_t motion_handler;
285 widget_button_handler_t button_handler;
286 widget_touch_down_handler_t touch_down_handler;
287 widget_touch_up_handler_t touch_up_handler;
288 widget_touch_motion_handler_t touch_motion_handler;
289 widget_touch_frame_handler_t touch_frame_handler;
290 widget_touch_cancel_handler_t touch_cancel_handler;
291 widget_axis_handler_t axis_handler;
296 /* If this is set to false then no cairo surface will be
297 * created before redrawing the surface. This is useful if the
298 * redraw handler is going to do completely custom rendering
299 * such as using EGL directly */
306 struct widget *widget;
311 struct display *display;
312 struct wl_seat *seat;
313 struct wl_pointer *pointer;
314 struct wl_keyboard *keyboard;
315 struct wl_touch *touch;
316 struct wl_list touch_point_list;
317 struct window *pointer_focus;
318 struct window *keyboard_focus;
319 struct window *touch_focus;
321 uint32_t cursor_anim_start;
322 struct wl_callback *cursor_frame_cb;
323 struct wl_surface *pointer_surface;
325 uint32_t pointer_enter_serial;
326 uint32_t cursor_serial;
330 struct widget *focus_widget;
332 uint32_t grab_button;
334 struct wl_data_device *data_device;
335 struct data_offer *drag_offer;
336 struct data_offer *selection_offer;
338 int32_t touch_grab_id;
339 float drag_x, drag_y;
340 struct window *drag_focus;
341 uint32_t drag_enter_serial;
344 struct xkb_keymap *keymap;
345 struct xkb_state *state;
346 xkb_mod_mask_t control_mask;
347 xkb_mod_mask_t alt_mask;
348 xkb_mod_mask_t shift_mask;
351 int32_t repeat_rate_sec;
352 int32_t repeat_rate_nsec;
353 int32_t repeat_delay_sec;
354 int32_t repeat_delay_nsec;
356 struct task repeat_task;
360 uint32_t repeat_time;
364 struct display *display;
365 struct wl_output *output;
366 uint32_t server_output_id;
367 struct rectangle allocation;
374 display_output_handler_t destroy_handler;
378 struct window_frame {
379 struct widget *widget;
380 struct widget *child;
384 uint32_t did_double, double_click;
389 struct window *window;
390 struct widget *widget;
393 const char **entries;
402 struct widget *parent;
403 struct widget *widget;
405 struct task tooltip_task;
411 struct wl_shm_pool *pool;
418 CURSOR_DEFAULT = 100,
422 static const cairo_user_data_key_t shm_surface_data_key;
429 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
430 __attribute__ ((format (printf, 4, 5)));
433 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
438 gettimeofday(&tv, NULL);
439 fprintf(stderr, "%8ld.%03ld ",
440 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
443 fprintf(stderr, "%s@%d ",
444 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
446 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
447 fprintf(stderr, "%s ", func);
450 vfprintf(stderr, fmt, ap);
454 #define DBG(fmt, ...) \
455 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
457 #define DBG_OBJ(obj, fmt, ...) \
458 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
462 #define DBG(...) do {} while (0)
463 #define DBG_OBJ(...) do {} while (0)
468 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
472 switch (buffer_transform) {
473 case WL_OUTPUT_TRANSFORM_90:
474 case WL_OUTPUT_TRANSFORM_270:
475 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
476 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
485 *width *= buffer_scale;
486 *height *= buffer_scale;
490 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
494 switch (buffer_transform) {
495 case WL_OUTPUT_TRANSFORM_90:
496 case WL_OUTPUT_TRANSFORM_270:
497 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
498 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
507 *width /= buffer_scale;
508 *height /= buffer_scale;
511 #ifdef HAVE_CAIRO_EGL
513 struct egl_window_surface {
514 struct toysurface base;
515 cairo_surface_t *cairo_surface;
516 struct display *display;
517 struct wl_surface *surface;
518 struct wl_egl_window *egl_window;
519 EGLSurface egl_surface;
522 static struct egl_window_surface *
523 to_egl_window_surface(struct toysurface *base)
525 return container_of(base, struct egl_window_surface, base);
528 static cairo_surface_t *
529 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
530 int32_t width, int32_t height, uint32_t flags,
531 enum wl_output_transform buffer_transform, int32_t buffer_scale)
533 struct egl_window_surface *surface = to_egl_window_surface(base);
535 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
537 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
538 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
540 return cairo_surface_reference(surface->cairo_surface);
544 egl_window_surface_swap(struct toysurface *base,
545 enum wl_output_transform buffer_transform, int32_t buffer_scale,
546 struct rectangle *server_allocation)
548 struct egl_window_surface *surface = to_egl_window_surface(base);
550 cairo_gl_surface_swapbuffers(surface->cairo_surface);
551 wl_egl_window_get_attached_size(surface->egl_window,
552 &server_allocation->width,
553 &server_allocation->height);
555 buffer_to_surface_size (buffer_transform, buffer_scale,
556 &server_allocation->width,
557 &server_allocation->height);
561 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
563 struct egl_window_surface *surface = to_egl_window_surface(base);
564 cairo_device_t *device;
566 device = cairo_surface_get_device(surface->cairo_surface);
571 if (device == surface->display->argb_device)
572 ctx = surface->display->argb_ctx;
577 cairo_device_flush(device);
578 cairo_device_acquire(device);
579 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
580 surface->egl_surface, ctx))
581 fprintf(stderr, "failed to make surface current\n");
587 egl_window_surface_release(struct toysurface *base)
589 struct egl_window_surface *surface = to_egl_window_surface(base);
590 cairo_device_t *device;
592 device = cairo_surface_get_device(surface->cairo_surface);
596 if (!eglMakeCurrent(surface->display->dpy,
597 EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
598 fprintf(stderr, "failed to make context current\n");
600 cairo_device_release(device);
604 egl_window_surface_destroy(struct toysurface *base)
606 struct egl_window_surface *surface = to_egl_window_surface(base);
607 struct display *d = surface->display;
609 cairo_surface_destroy(surface->cairo_surface);
610 eglDestroySurface(d->dpy, surface->egl_surface);
611 wl_egl_window_destroy(surface->egl_window);
612 surface->surface = NULL;
617 static struct toysurface *
618 egl_window_surface_create(struct display *display,
619 struct wl_surface *wl_surface,
621 struct rectangle *rectangle)
623 struct egl_window_surface *surface;
625 if (display->dpy == EGL_NO_DISPLAY)
628 surface = calloc(1, sizeof *surface);
632 surface->base.prepare = egl_window_surface_prepare;
633 surface->base.swap = egl_window_surface_swap;
634 surface->base.acquire = egl_window_surface_acquire;
635 surface->base.release = egl_window_surface_release;
636 surface->base.destroy = egl_window_surface_destroy;
638 surface->display = display;
639 surface->surface = wl_surface;
641 surface->egl_window = wl_egl_window_create(surface->surface,
645 surface->egl_surface = eglCreateWindowSurface(display->dpy,
646 display->argb_config,
650 surface->cairo_surface =
651 cairo_gl_surface_create_for_egl(display->argb_device,
652 surface->egl_surface,
656 return &surface->base;
661 static struct toysurface *
662 egl_window_surface_create(struct display *display,
663 struct wl_surface *wl_surface,
665 struct rectangle *rectangle)
672 struct shm_surface_data {
673 struct wl_buffer *buffer;
674 struct shm_pool *pool;
678 display_get_buffer_for_surface(struct display *display,
679 cairo_surface_t *surface)
681 struct shm_surface_data *data;
683 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
689 shm_pool_destroy(struct shm_pool *pool);
692 shm_surface_data_destroy(void *p)
694 struct shm_surface_data *data = p;
696 wl_buffer_destroy(data->buffer);
698 shm_pool_destroy(data->pool);
703 static struct wl_shm_pool *
704 make_shm_pool(struct display *display, int size, void **data)
706 struct wl_shm_pool *pool;
709 fd = os_create_anonymous_file(size);
711 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
716 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
717 if (*data == MAP_FAILED) {
718 fprintf(stderr, "mmap failed: %m\n");
723 pool = wl_shm_create_pool(display->shm, fd, size);
730 static struct shm_pool *
731 shm_pool_create(struct display *display, size_t size)
733 struct shm_pool *pool = malloc(sizeof *pool);
738 pool->pool = make_shm_pool(display, size, &pool->data);
751 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
753 if (pool->used + size > pool->size)
756 *offset = pool->used;
759 return (char *) pool->data + *offset;
762 /* destroy the pool. this does not unmap the memory though */
764 shm_pool_destroy(struct shm_pool *pool)
766 munmap(pool->data, pool->size);
767 wl_shm_pool_destroy(pool->pool);
771 /* Start allocating from the beginning of the pool again */
773 shm_pool_reset(struct shm_pool *pool)
779 data_length_for_shm_surface(struct rectangle *rect)
783 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
785 return stride * rect->height;
788 static cairo_surface_t *
789 display_create_shm_surface_from_pool(struct display *display,
790 struct rectangle *rectangle,
791 uint32_t flags, struct shm_pool *pool)
793 struct shm_surface_data *data;
795 cairo_surface_t *surface;
796 cairo_format_t cairo_format;
797 int stride, length, offset;
800 data = malloc(sizeof *data);
804 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
805 cairo_format = CAIRO_FORMAT_RGB16_565;
807 cairo_format = CAIRO_FORMAT_ARGB32;
809 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
810 length = stride * rectangle->height;
812 map = shm_pool_allocate(pool, length, &offset);
819 surface = cairo_image_surface_create_for_data (map,
825 cairo_surface_set_user_data(surface, &shm_surface_data_key,
826 data, shm_surface_data_destroy);
828 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
829 format = WL_SHM_FORMAT_RGB565;
831 if (flags & SURFACE_OPAQUE)
832 format = WL_SHM_FORMAT_XRGB8888;
834 format = WL_SHM_FORMAT_ARGB8888;
837 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
845 static cairo_surface_t *
846 display_create_shm_surface(struct display *display,
847 struct rectangle *rectangle, uint32_t flags,
848 struct shm_pool *alternate_pool,
849 struct shm_surface_data **data_ret)
851 struct shm_surface_data *data;
852 struct shm_pool *pool;
853 cairo_surface_t *surface;
855 if (alternate_pool) {
856 shm_pool_reset(alternate_pool);
857 surface = display_create_shm_surface_from_pool(display,
862 data = cairo_surface_get_user_data(surface,
863 &shm_surface_data_key);
868 pool = shm_pool_create(display,
869 data_length_for_shm_surface(rectangle));
874 display_create_shm_surface_from_pool(display, rectangle,
878 shm_pool_destroy(pool);
882 /* make sure we destroy the pool when the surface is destroyed */
883 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
894 check_size(struct rectangle *rect)
896 if (rect->width && rect->height)
899 fprintf(stderr, "tried to create surface of "
900 "width: %d, height: %d\n", rect->width, rect->height);
905 display_create_surface(struct display *display,
906 struct wl_surface *surface,
907 struct rectangle *rectangle,
910 if (check_size(rectangle) < 0)
913 assert(flags & SURFACE_SHM);
914 return display_create_shm_surface(display, rectangle, flags,
918 struct shm_surface_leaf {
919 cairo_surface_t *cairo_surface;
920 /* 'data' is automatically destroyed, when 'cairo_surface' is */
921 struct shm_surface_data *data;
923 struct shm_pool *resize_pool;
928 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
930 if (leaf->cairo_surface)
931 cairo_surface_destroy(leaf->cairo_surface);
932 /* leaf->data already destroyed via cairo private */
934 if (leaf->resize_pool)
935 shm_pool_destroy(leaf->resize_pool);
937 memset(leaf, 0, sizeof *leaf);
943 struct toysurface base;
944 struct display *display;
945 struct wl_surface *surface;
949 struct shm_surface_leaf leaf[MAX_LEAVES];
950 struct shm_surface_leaf *current;
953 static struct shm_surface *
954 to_shm_surface(struct toysurface *base)
956 return container_of(base, struct shm_surface, base);
960 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
963 struct shm_surface_leaf *leaf;
964 char bufs[MAX_LEAVES + 1];
967 for (i = 0; i < MAX_LEAVES; i++) {
968 leaf = &surface->leaf[i];
972 else if (leaf->cairo_surface)
978 bufs[MAX_LEAVES] = '\0';
979 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
984 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
986 struct shm_surface *surface = data;
987 struct shm_surface_leaf *leaf;
991 shm_surface_buffer_state_debug(surface, "buffer_release before");
993 for (i = 0; i < MAX_LEAVES; i++) {
994 leaf = &surface->leaf[i];
995 if (leaf->data && leaf->data->buffer == buffer) {
1000 assert(i < MAX_LEAVES && "unknown buffer released");
1002 /* Leave one free leaf with storage, release others */
1004 for (i = 0; i < MAX_LEAVES; i++) {
1005 leaf = &surface->leaf[i];
1007 if (!leaf->cairo_surface || leaf->busy)
1013 shm_surface_leaf_release(leaf);
1016 shm_surface_buffer_state_debug(surface, "buffer_release after");
1019 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1020 shm_surface_buffer_release
1023 static cairo_surface_t *
1024 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1025 int32_t width, int32_t height, uint32_t flags,
1026 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1028 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1029 struct shm_surface *surface = to_shm_surface(base);
1030 struct rectangle rect = { 0};
1031 struct shm_surface_leaf *leaf = NULL;
1037 /* pick a free buffer, preferrably one that already has storage */
1038 for (i = 0; i < MAX_LEAVES; i++) {
1039 if (surface->leaf[i].busy)
1042 if (!leaf || surface->leaf[i].cairo_surface)
1043 leaf = &surface->leaf[i];
1045 DBG_OBJ(surface->surface, "pick leaf %d\n",
1046 (int)(leaf - &surface->leaf[0]));
1049 fprintf(stderr, "%s: all buffers are held by the server.\n",
1055 if (!resize_hint && leaf->resize_pool) {
1056 cairo_surface_destroy(leaf->cairo_surface);
1057 leaf->cairo_surface = NULL;
1058 shm_pool_destroy(leaf->resize_pool);
1059 leaf->resize_pool = NULL;
1062 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1064 if (leaf->cairo_surface &&
1065 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1066 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1069 if (leaf->cairo_surface)
1070 cairo_surface_destroy(leaf->cairo_surface);
1072 #ifdef USE_RESIZE_POOL
1073 if (resize_hint && !leaf->resize_pool) {
1074 /* Create a big pool to allocate from, while continuously
1075 * resizing. Mmapping a new pool in the server
1076 * is relatively expensive, so reusing a pool performs
1077 * better, but may temporarily reserve unneeded memory.
1079 /* We should probably base this number on the output size. */
1080 leaf->resize_pool = shm_pool_create(surface->display,
1086 rect.height = height;
1088 leaf->cairo_surface =
1089 display_create_shm_surface(surface->display, &rect,
1093 if (!leaf->cairo_surface)
1096 wl_buffer_add_listener(leaf->data->buffer,
1097 &shm_surface_buffer_listener, surface);
1100 surface->current = leaf;
1102 return cairo_surface_reference(leaf->cairo_surface);
1106 shm_surface_swap(struct toysurface *base,
1107 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1108 struct rectangle *server_allocation)
1110 struct shm_surface *surface = to_shm_surface(base);
1111 struct shm_surface_leaf *leaf = surface->current;
1113 server_allocation->width =
1114 cairo_image_surface_get_width(leaf->cairo_surface);
1115 server_allocation->height =
1116 cairo_image_surface_get_height(leaf->cairo_surface);
1118 buffer_to_surface_size (buffer_transform, buffer_scale,
1119 &server_allocation->width,
1120 &server_allocation->height);
1122 wl_surface_attach(surface->surface, leaf->data->buffer,
1123 surface->dx, surface->dy);
1124 wl_surface_damage(surface->surface, 0, 0,
1125 server_allocation->width, server_allocation->height);
1126 wl_surface_commit(surface->surface);
1128 DBG_OBJ(surface->surface, "leaf %d busy\n",
1129 (int)(leaf - &surface->leaf[0]));
1132 surface->current = NULL;
1136 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1142 shm_surface_release(struct toysurface *base)
1147 shm_surface_destroy(struct toysurface *base)
1149 struct shm_surface *surface = to_shm_surface(base);
1152 for (i = 0; i < MAX_LEAVES; i++)
1153 shm_surface_leaf_release(&surface->leaf[i]);
1158 static struct toysurface *
1159 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1160 uint32_t flags, struct rectangle *rectangle)
1162 struct shm_surface *surface;
1163 DBG_OBJ(wl_surface, "\n");
1165 surface = xmalloc(sizeof *surface);
1166 memset(surface, 0, sizeof *surface);
1171 surface->base.prepare = shm_surface_prepare;
1172 surface->base.swap = shm_surface_swap;
1173 surface->base.acquire = shm_surface_acquire;
1174 surface->base.release = shm_surface_release;
1175 surface->base.destroy = shm_surface_destroy;
1177 surface->display = display;
1178 surface->surface = wl_surface;
1179 surface->flags = flags;
1181 return &surface->base;
1185 * The following correspondences between file names and cursors was copied
1186 * from: https://bugs.kde.org/attachment.cgi?id=67313
1189 static const char *bottom_left_corners[] = {
1190 "bottom_left_corner",
1195 static const char *bottom_right_corners[] = {
1196 "bottom_right_corner",
1201 static const char *bottom_sides[] = {
1207 static const char *grabbings[] = {
1210 "208530c400c041818281048008011002"
1213 static const char *left_ptrs[] = {
1220 static const char *left_sides[] = {
1226 static const char *right_sides[] = {
1232 static const char *top_left_corners[] = {
1238 static const char *top_right_corners[] = {
1244 static const char *top_sides[] = {
1250 static const char *xterms[] = {
1256 static const char *hand1s[] = {
1260 "e29285e634086352946a0e7090d73106"
1263 static const char *watches[] = {
1266 "0426c94ea35c87780ff01dc239897213"
1269 struct cursor_alternatives {
1274 static const struct cursor_alternatives cursors[] = {
1275 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1276 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1277 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1278 {grabbings, ARRAY_LENGTH(grabbings)},
1279 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1280 {left_sides, ARRAY_LENGTH(left_sides)},
1281 {right_sides, ARRAY_LENGTH(right_sides)},
1282 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1283 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1284 {top_sides, ARRAY_LENGTH(top_sides)},
1285 {xterms, ARRAY_LENGTH(xterms)},
1286 {hand1s, ARRAY_LENGTH(hand1s)},
1287 {watches, ARRAY_LENGTH(watches)},
1291 create_cursors(struct display *display)
1293 struct weston_config *config;
1294 struct weston_config_section *s;
1298 struct wl_cursor *cursor;
1300 config = weston_config_parse("weston.ini");
1301 s = weston_config_get_section(config, "shell", NULL, NULL);
1302 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1303 weston_config_section_get_int(s, "cursor-size", &size, 32);
1304 weston_config_destroy(config);
1306 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1307 if (!display->cursor_theme) {
1308 fprintf(stderr, "could not load theme '%s'\n", theme);
1313 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1315 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1317 for (j = 0; !cursor && j < cursors[i].count; ++j)
1318 cursor = wl_cursor_theme_get_cursor(
1319 display->cursor_theme, cursors[i].names[j]);
1322 fprintf(stderr, "could not load cursor '%s'\n",
1323 cursors[i].names[0]);
1325 display->cursors[i] = cursor;
1330 destroy_cursors(struct display *display)
1332 wl_cursor_theme_destroy(display->cursor_theme);
1333 free(display->cursors);
1336 struct wl_cursor_image *
1337 display_get_pointer_image(struct display *display, int pointer)
1339 struct wl_cursor *cursor = display->cursors[pointer];
1341 return cursor ? cursor->images[0] : NULL;
1345 surface_flush(struct surface *surface)
1347 if (!surface->cairo_surface)
1350 if (surface->opaque_region) {
1351 wl_surface_set_opaque_region(surface->surface,
1352 surface->opaque_region);
1353 wl_region_destroy(surface->opaque_region);
1354 surface->opaque_region = NULL;
1357 if (surface->input_region) {
1358 wl_surface_set_input_region(surface->surface,
1359 surface->input_region);
1360 wl_region_destroy(surface->input_region);
1361 surface->input_region = NULL;
1364 surface->toysurface->swap(surface->toysurface,
1365 surface->buffer_transform, surface->buffer_scale,
1366 &surface->server_allocation);
1368 cairo_surface_destroy(surface->cairo_surface);
1369 surface->cairo_surface = NULL;
1373 window_has_focus(struct window *window)
1375 return window->focused;
1379 window_close(struct window *window)
1381 if (window->close_handler)
1382 window->close_handler(window->user_data);
1384 display_exit(window->display);
1388 window_get_display(struct window *window)
1390 return window->display;
1393 static void handle_wl_shell_ping(void *data,
1394 struct wl_shell_surface *wl_shell_surface,
1397 struct surface *surface = (struct surface *)data;
1399 fprintf(stderr, "wl_shell: got ping event\n");
1401 wl_shell_surface_pong(wl_shell_surface, serial);
1402 wl_display_flush(surface->window->display->display);
1405 static void handle_wl_shell_configure(void *data,
1406 struct wl_shell_surface *wl_shell_surface,
1411 struct surface *surface = (struct surface *)data;
1413 fprintf(stderr, "wl_shell: got configure event: edges=%u size=%dx%d\n",
1414 edges, width,height);
1416 window_schedule_resize(surface->window, width, height);
1419 static void handle_wl_shell_popup_done(void *data,
1420 struct wl_shell_surface *wl_shell_surface)
1423 (void)wl_shell_surface;
1425 fprintf(stderr, "wl_shell: got popup done event\n");
1428 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
1429 handle_wl_shell_ping,
1430 handle_wl_shell_configure,
1431 handle_wl_shell_popup_done,
1435 handle_ivi_surface_configure(void *data, struct ivi_surface *ivi_surface,
1436 int32_t width, int32_t height)
1438 struct window *window = data;
1440 window_schedule_resize(window, width, height);
1443 static const struct ivi_surface_listener ivi_surface_listener = {
1444 handle_ivi_surface_configure,
1448 surface_create_surface(struct surface *surface, uint32_t flags)
1450 struct display *display = surface->window->display;
1451 struct rectangle allocation = surface->allocation;
1453 if (display->ivi_application) {
1454 if (!surface->toysurface && strcmp(surface->window->title,"Virtual keyboard")!=0) {
1455 if (display->create_ivi_surface) {
1456 uint32_t id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
1457 surface->window->ivi_surface =
1458 ivi_application_surface_create(display->ivi_application,
1459 id_ivisurf, surface->surface);
1460 if (surface->window->ivi_surface == NULL) {
1461 fprintf(stderr, "Failed to create ivi_client_surface\n");
1464 fprintf(stderr, "created IVI surface %u\n", id_ivisurf);
1467 surface->window->shell_surface =
1468 wl_shell_get_shell_surface(display->shell, surface->surface);
1469 if (surface->window->shell_surface == NULL)
1470 fprintf(stderr, "could not obtain shell_surface\n");
1472 wl_shell_surface_add_listener(surface->window->shell_surface,
1473 &wl_shell_surface_listener,
1475 if (surface->window->title) {
1476 wl_shell_surface_set_title(surface->window->shell_surface,
1477 surface->window->title);
1483 if (!surface->toysurface && display->dpy &&
1484 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1485 surface->toysurface =
1486 egl_window_surface_create(display,
1492 if (!surface->toysurface)
1493 surface->toysurface = shm_surface_create(display,
1495 flags, &allocation);
1497 surface->cairo_surface = surface->toysurface->prepare(
1498 surface->toysurface, 0, 0,
1499 allocation.width, allocation.height, flags,
1500 surface->buffer_transform, surface->buffer_scale);
1504 window_create_main_surface(struct window *window)
1506 struct surface *surface = window->main_surface;
1509 if (window->resizing)
1510 flags |= SURFACE_HINT_RESIZE;
1512 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1513 flags |= SURFACE_HINT_RGB565;
1515 surface_create_surface(surface, flags);
1519 window_get_buffer_transform(struct window *window)
1521 return window->main_surface->buffer_transform;
1525 window_set_buffer_transform(struct window *window,
1526 enum wl_output_transform transform)
1528 window->main_surface->buffer_transform = transform;
1529 wl_surface_set_buffer_transform(window->main_surface->surface,
1534 window_set_buffer_scale(struct window *window,
1537 window->main_surface->buffer_scale = scale;
1538 wl_surface_set_buffer_scale(window->main_surface->surface,
1543 window_get_buffer_scale(struct window *window)
1545 return window->main_surface->buffer_scale;
1549 window_get_output_scale(struct window *window)
1551 struct window_output *window_output;
1552 struct window_output *window_output_tmp;
1555 wl_list_for_each_safe(window_output, window_output_tmp,
1556 &window->window_output_list, link) {
1557 if (window_output->output->scale > scale)
1558 scale = window_output->output->scale;
1564 static void window_frame_destroy(struct window_frame *frame);
1567 surface_destroy(struct surface *surface)
1569 if (surface->frame_cb)
1570 wl_callback_destroy(surface->frame_cb);
1572 if (surface->input_region)
1573 wl_region_destroy(surface->input_region);
1575 if (surface->opaque_region)
1576 wl_region_destroy(surface->opaque_region);
1578 if (surface->subsurface)
1579 wl_subsurface_destroy(surface->subsurface);
1581 wl_surface_destroy(surface->surface);
1583 if (surface->toysurface)
1584 surface->toysurface->destroy(surface->toysurface);
1586 if (surface->window->display->ivi_application) {
1587 if(surface->window->ivi_surface && strcmp(surface->window->title,"Virtual keyboard"))
1588 ivi_surface_destroy(surface->window->ivi_surface);
1589 ivi_application_destroy(surface->window->display->ivi_application);
1592 wl_list_remove(&surface->link);
1597 window_destroy(struct window *window)
1599 struct display *display = window->display;
1600 struct input *input;
1601 struct window_output *window_output;
1602 struct window_output *window_output_tmp;
1604 wl_list_remove(&window->redraw_task.link);
1606 wl_list_for_each(input, &display->input_list, link) {
1607 if (input->touch_focus == window)
1608 input->touch_focus = NULL;
1609 if (input->pointer_focus == window)
1610 input->pointer_focus = NULL;
1611 if (input->keyboard_focus == window)
1612 input->keyboard_focus = NULL;
1613 if (input->focus_widget &&
1614 input->focus_widget->window == window)
1615 input->focus_widget = NULL;
1618 wl_list_for_each_safe(window_output, window_output_tmp,
1619 &window->window_output_list, link) {
1620 free (window_output);
1624 window_frame_destroy(window->frame);
1626 if (window->xdg_surface)
1627 xdg_surface_destroy(window->xdg_surface);
1628 if (window->xdg_popup)
1629 xdg_popup_destroy(window->xdg_popup);
1631 if (window->ivi_surface)
1632 ivi_surface_destroy(window->ivi_surface);
1634 surface_destroy(window->main_surface);
1636 wl_list_remove(&window->link);
1638 free(window->title);
1642 static struct widget *
1643 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1645 struct widget *child, *target;
1647 wl_list_for_each(child, &widget->child_list, link) {
1648 target = widget_find_widget(child, x, y);
1653 if (widget->allocation.x <= x &&
1654 x < widget->allocation.x + widget->allocation.width &&
1655 widget->allocation.y <= y &&
1656 y < widget->allocation.y + widget->allocation.height) {
1663 static struct widget *
1664 window_find_widget(struct window *window, int32_t x, int32_t y)
1666 struct surface *surface;
1667 struct widget *widget;
1669 wl_list_for_each(surface, &window->subsurface_list, link) {
1670 widget = widget_find_widget(surface->widget, x, y);
1678 static struct widget *
1679 widget_create(struct window *window, struct surface *surface, void *data)
1681 struct widget *widget;
1683 widget = xzalloc(sizeof *widget);
1684 widget->window = window;
1685 widget->surface = surface;
1686 widget->user_data = data;
1687 widget->allocation = surface->allocation;
1688 wl_list_init(&widget->child_list);
1690 widget->tooltip = NULL;
1691 widget->tooltip_count = 0;
1692 widget->default_cursor = CURSOR_LEFT_PTR;
1693 widget->use_cairo = 1;
1699 window_add_widget(struct window *window, void *data)
1701 struct widget *widget;
1703 widget = widget_create(window, window->main_surface, data);
1704 wl_list_init(&widget->link);
1705 window->main_surface->widget = widget;
1711 widget_add_widget(struct widget *parent, void *data)
1713 struct widget *widget;
1715 widget = widget_create(parent->window, parent->surface, data);
1716 wl_list_insert(parent->child_list.prev, &widget->link);
1722 widget_destroy(struct widget *widget)
1724 struct display *display = widget->window->display;
1725 struct surface *surface = widget->surface;
1726 struct input *input;
1728 /* Destroy the sub-surface along with the root widget */
1729 if (surface->widget == widget && surface->subsurface)
1730 surface_destroy(widget->surface);
1732 if (widget->tooltip)
1733 widget_destroy_tooltip(widget);
1735 wl_list_for_each(input, &display->input_list, link) {
1736 if (input->focus_widget == widget)
1737 input->focus_widget = NULL;
1740 wl_list_remove(&widget->link);
1745 widget_set_default_cursor(struct widget *widget, int cursor)
1747 widget->default_cursor = cursor;
1751 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1753 *allocation = widget->allocation;
1757 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1759 widget->allocation.width = width;
1760 widget->allocation.height = height;
1764 widget_set_allocation(struct widget *widget,
1765 int32_t x, int32_t y, int32_t width, int32_t height)
1767 widget->allocation.x = x;
1768 widget->allocation.y = y;
1769 widget_set_size(widget, width, height);
1773 widget_set_transparent(struct widget *widget, int transparent)
1775 widget->opaque = !transparent;
1779 widget_get_user_data(struct widget *widget)
1781 return widget->user_data;
1784 static cairo_surface_t *
1785 widget_get_cairo_surface(struct widget *widget)
1787 struct surface *surface = widget->surface;
1788 struct window *window = widget->window;
1790 assert(widget->use_cairo);
1792 if (!surface->cairo_surface) {
1793 if (surface == window->main_surface)
1794 window_create_main_surface(window);
1796 surface_create_surface(surface, 0);
1799 return surface->cairo_surface;
1803 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1805 struct surface *surface = widget->surface;
1808 enum wl_output_transform transform;
1809 int surface_width, surface_height;
1810 int translate_x, translate_y;
1813 surface_width = surface->allocation.width;
1814 surface_height = surface->allocation.height;
1816 transform = surface->buffer_transform;
1817 scale = surface->buffer_scale;
1819 switch (transform) {
1820 case WL_OUTPUT_TRANSFORM_FLIPPED:
1821 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1822 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1823 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1824 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1827 cairo_matrix_init_identity(&m);
1831 switch (transform) {
1832 case WL_OUTPUT_TRANSFORM_NORMAL:
1838 case WL_OUTPUT_TRANSFORM_FLIPPED:
1840 translate_x = surface_width;
1843 case WL_OUTPUT_TRANSFORM_90:
1845 translate_x = surface_height;
1848 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1850 translate_x = surface_height;
1851 translate_y = surface_width;
1853 case WL_OUTPUT_TRANSFORM_180:
1855 translate_x = surface_width;
1856 translate_y = surface_height;
1858 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1861 translate_y = surface_height;
1863 case WL_OUTPUT_TRANSFORM_270:
1864 angle = M_PI + M_PI_2;
1866 translate_y = surface_width;
1868 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1869 angle = M_PI + M_PI_2;
1875 cairo_scale(cr, scale, scale);
1876 cairo_translate(cr, translate_x, translate_y);
1877 cairo_rotate(cr, angle);
1878 cairo_transform(cr, &m);
1882 widget_cairo_create(struct widget *widget)
1884 struct surface *surface = widget->surface;
1885 cairo_surface_t *cairo_surface;
1888 cairo_surface = widget_get_cairo_surface(widget);
1889 cr = cairo_create(cairo_surface);
1891 widget_cairo_update_transform(widget, cr);
1893 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1899 widget_get_wl_surface(struct widget *widget)
1901 return widget->surface->surface;
1904 struct wl_subsurface *
1905 widget_get_wl_subsurface(struct widget *widget)
1907 return widget->surface->subsurface;
1911 widget_get_last_time(struct widget *widget)
1913 return widget->surface->last_time;
1917 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1919 struct wl_compositor *comp = widget->window->display->compositor;
1920 struct surface *surface = widget->surface;
1922 if (!surface->input_region)
1923 surface->input_region = wl_compositor_create_region(comp);
1926 wl_region_add(surface->input_region,
1927 rect->x, rect->y, rect->width, rect->height);
1932 widget_set_resize_handler(struct widget *widget,
1933 widget_resize_handler_t handler)
1935 widget->resize_handler = handler;
1939 widget_set_redraw_handler(struct widget *widget,
1940 widget_redraw_handler_t handler)
1942 widget->redraw_handler = handler;
1946 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1948 widget->enter_handler = handler;
1952 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1954 widget->leave_handler = handler;
1958 widget_set_motion_handler(struct widget *widget,
1959 widget_motion_handler_t handler)
1961 widget->motion_handler = handler;
1965 widget_set_button_handler(struct widget *widget,
1966 widget_button_handler_t handler)
1968 widget->button_handler = handler;
1972 widget_set_touch_up_handler(struct widget *widget,
1973 widget_touch_up_handler_t handler)
1975 widget->touch_up_handler = handler;
1979 widget_set_touch_down_handler(struct widget *widget,
1980 widget_touch_down_handler_t handler)
1982 widget->touch_down_handler = handler;
1986 widget_set_touch_motion_handler(struct widget *widget,
1987 widget_touch_motion_handler_t handler)
1989 widget->touch_motion_handler = handler;
1993 widget_set_touch_frame_handler(struct widget *widget,
1994 widget_touch_frame_handler_t handler)
1996 widget->touch_frame_handler = handler;
2000 widget_set_touch_cancel_handler(struct widget *widget,
2001 widget_touch_cancel_handler_t handler)
2003 widget->touch_cancel_handler = handler;
2007 widget_set_axis_handler(struct widget *widget,
2008 widget_axis_handler_t handler)
2010 widget->axis_handler = handler;
2014 window_schedule_redraw_task(struct window *window);
2017 widget_schedule_redraw(struct widget *widget)
2019 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
2020 widget->surface->redraw_needed = 1;
2021 window_schedule_redraw_task(widget->window);
2025 widget_set_use_cairo(struct widget *widget,
2028 widget->use_cairo = use_cairo;
2032 window_get_surface(struct window *window)
2034 cairo_surface_t *cairo_surface;
2036 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
2038 return cairo_surface_reference(cairo_surface);
2042 window_get_wl_surface(struct window *window)
2044 return window->main_surface->surface;
2048 tooltip_redraw_handler(struct widget *widget, void *data)
2051 const int32_t r = 3;
2052 struct tooltip *tooltip = data;
2053 int32_t width, height;
2055 cr = widget_cairo_create(widget);
2056 cairo_translate(cr, widget->allocation.x, widget->allocation.y);
2057 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2058 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2061 width = widget->allocation.width;
2062 height = widget->allocation.height;
2063 rounded_rect(cr, 0, 0, width, height, r);
2065 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2066 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2069 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2070 cairo_move_to(cr, 10, 16);
2071 cairo_show_text(cr, tooltip->entry);
2075 static cairo_text_extents_t
2076 get_text_extents(struct display *display, struct tooltip *tooltip)
2079 cairo_text_extents_t extents;
2081 /* Use the dummy_surface because tooltip's surface was not
2082 * created yet, and parent does not have a valid surface
2083 * outside repaint, either.
2085 cr = cairo_create(display->dummy_surface);
2086 cairo_text_extents(cr, tooltip->entry, &extents);
2093 window_create_tooltip(struct tooltip *tooltip)
2095 struct widget *parent = tooltip->parent;
2096 struct display *display = parent->window->display;
2097 const int offset_y = 27;
2098 const int margin = 3;
2099 cairo_text_extents_t extents;
2101 if (tooltip->widget)
2104 tooltip->widget = window_add_subsurface(parent->window, tooltip, SUBSURFACE_DESYNCHRONIZED);
2106 extents = get_text_extents(display, tooltip);
2107 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2108 widget_set_allocation(tooltip->widget,
2109 tooltip->x, tooltip->y + offset_y,
2110 extents.width + 20, 20 + margin * 2);
2116 widget_destroy_tooltip(struct widget *parent)
2118 struct tooltip *tooltip = parent->tooltip;
2120 parent->tooltip_count = 0;
2124 if (tooltip->widget) {
2125 widget_destroy(tooltip->widget);
2126 tooltip->widget = NULL;
2129 close(tooltip->tooltip_fd);
2130 free(tooltip->entry);
2132 parent->tooltip = NULL;
2136 tooltip_func(struct task *task, uint32_t events)
2138 struct tooltip *tooltip =
2139 container_of(task, struct tooltip, tooltip_task);
2142 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2144 window_create_tooltip(tooltip);
2147 #define TOOLTIP_TIMEOUT 500
2149 tooltip_timer_reset(struct tooltip *tooltip)
2151 struct itimerspec its;
2153 its.it_interval.tv_sec = 0;
2154 its.it_interval.tv_nsec = 0;
2155 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2156 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2157 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2158 fprintf(stderr, "could not set timerfd\n: %m");
2166 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2168 struct tooltip *tooltip = parent->tooltip;
2170 parent->tooltip_count++;
2174 tooltip_timer_reset(tooltip);
2178 /* the handler might be triggered too fast via input device motion, so
2179 * we need this check here to make sure tooltip is fully initialized */
2180 if (parent->tooltip_count > 1)
2183 tooltip = malloc(sizeof *tooltip);
2187 parent->tooltip = tooltip;
2188 tooltip->parent = parent;
2189 tooltip->widget = NULL;
2192 tooltip->entry = strdup(entry);
2193 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2194 if (tooltip->tooltip_fd < 0) {
2195 fprintf(stderr, "could not create timerfd\n: %m");
2199 tooltip->tooltip_task.run = tooltip_func;
2200 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2201 EPOLLIN, &tooltip->tooltip_task);
2202 tooltip_timer_reset(tooltip);
2208 workspace_manager_state(void *data,
2209 struct workspace_manager *workspace_manager,
2213 struct display *display = data;
2215 display->workspace = current;
2216 display->workspace_count = count;
2219 static const struct workspace_manager_listener workspace_manager_listener = {
2220 workspace_manager_state
2224 frame_resize_handler(struct widget *widget,
2225 int32_t width, int32_t height, void *data)
2227 struct window_frame *frame = data;
2228 struct widget *child = frame->child;
2229 struct rectangle interior;
2230 struct rectangle input;
2231 struct rectangle opaque;
2233 if (widget->window->fullscreen) {
2236 interior.width = width;
2237 interior.height = height;
2239 frame_resize(frame->frame, width, height);
2240 frame_interior(frame->frame, &interior.x, &interior.y,
2241 &interior.width, &interior.height);
2244 widget_set_allocation(child, interior.x, interior.y,
2245 interior.width, interior.height);
2247 if (child->resize_handler) {
2248 child->resize_handler(child, interior.width, interior.height,
2251 if (widget->window->fullscreen) {
2252 width = child->allocation.width;
2253 height = child->allocation.height;
2255 frame_resize_inside(frame->frame,
2256 child->allocation.width,
2257 child->allocation.height);
2258 width = frame_width(frame->frame);
2259 height = frame_height(frame->frame);
2263 widget_set_allocation(widget, 0, 0, width, height);
2265 widget->surface->input_region =
2266 wl_compositor_create_region(widget->window->display->compositor);
2267 if (!widget->window->fullscreen) {
2268 frame_input_rect(frame->frame, &input.x, &input.y,
2269 &input.width, &input.height);
2270 wl_region_add(widget->surface->input_region,
2271 input.x, input.y, input.width, input.height);
2273 wl_region_add(widget->surface->input_region, 0, 0, width, height);
2276 widget_set_allocation(widget, 0, 0, width, height);
2278 if (child->opaque) {
2279 if (!widget->window->fullscreen) {
2280 frame_opaque_rect(frame->frame, &opaque.x, &opaque.y,
2281 &opaque.width, &opaque.height);
2283 wl_region_add(widget->surface->opaque_region,
2285 opaque.width, opaque.height);
2287 wl_region_add(widget->surface->opaque_region,
2288 0, 0, width, height);
2293 widget_schedule_redraw(widget);
2297 frame_redraw_handler(struct widget *widget, void *data)
2300 struct window_frame *frame = data;
2301 struct window *window = widget->window;
2303 if (window->fullscreen)
2306 cr = widget_cairo_create(widget);
2308 frame_repaint(frame->frame, cr);
2314 frame_get_pointer_image_for_location(struct window_frame *frame,
2315 enum theme_location location)
2317 struct window *window = frame->widget->window;
2320 return CURSOR_LEFT_PTR;
2323 case THEME_LOCATION_RESIZING_TOP:
2325 case THEME_LOCATION_RESIZING_BOTTOM:
2326 return CURSOR_BOTTOM;
2327 case THEME_LOCATION_RESIZING_LEFT:
2329 case THEME_LOCATION_RESIZING_RIGHT:
2330 return CURSOR_RIGHT;
2331 case THEME_LOCATION_RESIZING_TOP_LEFT:
2332 return CURSOR_TOP_LEFT;
2333 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2334 return CURSOR_TOP_RIGHT;
2335 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2336 return CURSOR_BOTTOM_LEFT;
2337 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2338 return CURSOR_BOTTOM_RIGHT;
2339 case THEME_LOCATION_EXTERIOR:
2340 case THEME_LOCATION_TITLEBAR:
2342 return CURSOR_LEFT_PTR;
2347 frame_menu_func(void *data, struct input *input, int index)
2349 struct window *window = data;
2350 struct display *display;
2354 window_close(window);
2356 case 1: /* move to workspace above */
2357 display = window->display;
2358 if (display->workspace > 0)
2359 workspace_manager_move_surface(
2360 display->workspace_manager,
2361 window->main_surface->surface,
2362 display->workspace - 1);
2364 case 2: /* move to workspace below */
2365 display = window->display;
2366 if (display->workspace < display->workspace_count - 1)
2367 workspace_manager_move_surface(
2368 display->workspace_manager,
2369 window->main_surface->surface,
2370 display->workspace + 1);
2372 case 3: /* fullscreen */
2373 /* we don't have a way to get out of fullscreen for now */
2374 if (window->fullscreen_handler)
2375 window->fullscreen_handler(window, window->user_data);
2381 window_show_frame_menu(struct window *window,
2382 struct input *input, uint32_t time)
2387 static const char *entries[] = {
2389 "Move to workspace above", "Move to workspace below",
2393 if (window->fullscreen_handler)
2394 count = ARRAY_LENGTH(entries);
2396 count = ARRAY_LENGTH(entries) - 1;
2398 input_get_position(input, &x, &y);
2399 window_show_menu(window->display, input, time, window,
2400 x - 10, y - 10, frame_menu_func, entries, count);
2404 frame_enter_handler(struct widget *widget,
2405 struct input *input, float x, float y, void *data)
2407 struct window_frame *frame = data;
2408 enum theme_location location;
2410 location = frame_pointer_enter(frame->frame, input, x, y);
2411 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2412 widget_schedule_redraw(frame->widget);
2414 return frame_get_pointer_image_for_location(data, location);
2418 frame_motion_handler(struct widget *widget,
2419 struct input *input, uint32_t time,
2420 float x, float y, void *data)
2422 struct window_frame *frame = data;
2423 enum theme_location location;
2425 location = frame_pointer_motion(frame->frame, input, x, y);
2426 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2427 widget_schedule_redraw(frame->widget);
2429 return frame_get_pointer_image_for_location(data, location);
2433 frame_leave_handler(struct widget *widget,
2434 struct input *input, void *data)
2436 struct window_frame *frame = data;
2438 frame_pointer_leave(frame->frame, input);
2439 if (frame_status(frame->frame) & FRAME_STATUS_REPAINT)
2440 widget_schedule_redraw(frame->widget);
2444 frame_handle_status(struct window_frame *frame, struct input *input,
2445 uint32_t time, enum theme_location location)
2447 struct window *window = frame->widget->window;
2450 status = frame_status(frame->frame);
2451 if (status & FRAME_STATUS_REPAINT)
2452 widget_schedule_redraw(frame->widget);
2454 if (status & FRAME_STATUS_MINIMIZE) {
2455 window_set_minimized(window);
2456 frame_status_clear(frame->frame, FRAME_STATUS_MINIMIZE);
2459 if (status & FRAME_STATUS_MENU) {
2460 window_show_frame_menu(window, input, time);
2461 frame_status_clear(frame->frame, FRAME_STATUS_MENU);
2464 if (status & FRAME_STATUS_MAXIMIZE) {
2465 window_set_maximized(window, !window->maximized);
2466 frame_status_clear(frame->frame, FRAME_STATUS_MAXIMIZE);
2469 if (status & FRAME_STATUS_CLOSE) {
2470 window_close(window);
2474 if ((status & FRAME_STATUS_MOVE) && window->xdg_surface) {
2475 input_ungrab(input);
2476 xdg_surface_move(window->xdg_surface,
2477 input_get_seat(input),
2478 window->display->serial);
2480 frame_status_clear(frame->frame, FRAME_STATUS_MOVE);
2483 if ((status & FRAME_STATUS_RESIZE) && window->xdg_surface) {
2484 input_ungrab(input);
2486 xdg_surface_resize(window->xdg_surface,
2487 input_get_seat(input),
2488 window->display->serial,
2491 frame_status_clear(frame->frame, FRAME_STATUS_RESIZE);
2495 #define DOUBLE_CLICK_PERIOD 250
2497 frame_button_handler(struct widget *widget,
2498 struct input *input, uint32_t time,
2499 uint32_t button, enum wl_pointer_button_state state,
2503 struct window_frame *frame = data;
2504 enum theme_location location;
2506 frame->double_click = 0;
2507 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2508 if (time - frame->last_time <= DOUBLE_CLICK_PERIOD) {
2509 frame->double_click = 1;
2510 frame->did_double = 1;
2512 frame->did_double = 0;
2514 frame->last_time = time;
2515 } else if (frame->did_double == 1) {
2516 frame->double_click = 1;
2517 frame->did_double = 0;
2520 if (frame->double_click)
2521 location = frame_double_click(frame->frame, input,
2524 location = frame_pointer_button(frame->frame, input,
2527 frame_handle_status(frame, input, time, location);
2531 frame_touch_down_handler(struct widget *widget, struct input *input,
2532 uint32_t serial, uint32_t time, int32_t id,
2533 float x, float y, void *data)
2535 struct window_frame *frame = data;
2537 frame_touch_down(frame->frame, input, id, x, y);
2538 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2542 frame_touch_up_handler(struct widget *widget,
2543 struct input *input, uint32_t serial, uint32_t time,
2544 int32_t id, void *data)
2546 struct window_frame *frame = data;
2548 frame_touch_up(frame->frame, input, id);
2549 frame_handle_status(frame, input, time, THEME_LOCATION_CLIENT_AREA);
2553 window_frame_create(struct window *window, void *data)
2555 struct window_frame *frame;
2558 if (window->custom) {
2559 buttons = FRAME_BUTTON_NONE;
2561 buttons = FRAME_BUTTON_ALL;
2564 frame = xzalloc(sizeof *frame);
2565 frame->frame = frame_create(window->display->theme, 0, 0,
2566 buttons, window->title);
2568 frame->widget = window_add_widget(window, frame);
2569 frame->child = widget_add_widget(frame->widget, data);
2571 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2572 widget_set_resize_handler(frame->widget, frame_resize_handler);
2573 widget_set_enter_handler(frame->widget, frame_enter_handler);
2574 widget_set_leave_handler(frame->widget, frame_leave_handler);
2575 widget_set_motion_handler(frame->widget, frame_motion_handler);
2576 widget_set_button_handler(frame->widget, frame_button_handler);
2577 widget_set_touch_down_handler(frame->widget, frame_touch_down_handler);
2578 widget_set_touch_up_handler(frame->widget, frame_touch_up_handler);
2580 window->frame = frame;
2582 return frame->child;
2586 window_frame_set_child_size(struct widget *widget, int child_width,
2589 struct display *display = widget->window->display;
2590 struct theme *t = display->theme;
2591 int decoration_width, decoration_height;
2593 int margin = widget->window->maximized ? 0 : t->margin;
2595 if (!widget->window->fullscreen) {
2596 decoration_width = (t->width + margin) * 2;
2597 decoration_height = t->width +
2598 t->titlebar_height + margin * 2;
2600 width = child_width + decoration_width;
2601 height = child_height + decoration_height;
2603 width = child_width;
2604 height = child_height;
2607 window_schedule_resize(widget->window, width, height);
2611 window_frame_destroy(struct window_frame *frame)
2613 frame_destroy(frame->frame);
2615 /* frame->child must be destroyed by the application */
2616 widget_destroy(frame->widget);
2621 input_set_focus_widget(struct input *input, struct widget *focus,
2624 struct widget *old, *widget;
2627 if (focus == input->focus_widget)
2630 old = input->focus_widget;
2634 widget = input->grab;
2635 if (widget->leave_handler)
2636 widget->leave_handler(old, input, widget->user_data);
2637 input->focus_widget = NULL;
2643 widget = input->grab;
2644 input->focus_widget = focus;
2645 if (widget->enter_handler)
2646 cursor = widget->enter_handler(focus, input, x, y,
2649 cursor = widget->default_cursor;
2651 input_set_pointer_image(input, cursor);
2656 touch_grab(struct input *input, int32_t touch_id)
2658 input->touch_grab = 1;
2659 input->touch_grab_id = touch_id;
2663 touch_ungrab(struct input *input)
2665 struct touch_point *tp, *tmp;
2667 input->touch_grab = 0;
2669 wl_list_for_each_safe(tp, tmp,
2670 &input->touch_point_list, link) {
2671 if (tp->id != input->touch_grab_id)
2673 wl_list_remove(&tp->link);
2681 input_grab(struct input *input, struct widget *widget, uint32_t button)
2683 input->grab = widget;
2684 input->grab_button = button;
2686 input_set_focus_widget(input, widget, input->sx, input->sy);
2690 input_ungrab(struct input *input)
2692 struct widget *widget;
2695 if (input->pointer_focus) {
2696 widget = window_find_widget(input->pointer_focus,
2697 input->sx, input->sy);
2698 input_set_focus_widget(input, widget, input->sx, input->sy);
2703 input_remove_pointer_focus(struct input *input)
2705 struct window *window = input->pointer_focus;
2710 input_set_focus_widget(input, NULL, 0, 0);
2712 input->pointer_focus = NULL;
2713 input->current_cursor = CURSOR_UNSET;
2717 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2718 uint32_t serial, struct wl_surface *surface,
2719 wl_fixed_t sx_w, wl_fixed_t sy_w)
2721 struct input *input = data;
2722 struct window *window;
2723 struct widget *widget;
2724 float sx = wl_fixed_to_double(sx_w);
2725 float sy = wl_fixed_to_double(sy_w);
2728 /* enter event for a window we've just destroyed */
2732 window = wl_surface_get_user_data(surface);
2733 if (surface != window->main_surface->surface) {
2734 DBG("Ignoring input event from subsurface %p\n", surface);
2738 input->display->serial = serial;
2739 input->pointer_enter_serial = serial;
2740 input->pointer_focus = window;
2745 widget = window_find_widget(window, sx, sy);
2746 input_set_focus_widget(input, widget, sx, sy);
2750 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2751 uint32_t serial, struct wl_surface *surface)
2753 struct input *input = data;
2755 input->display->serial = serial;
2756 input_remove_pointer_focus(input);
2760 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2761 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2763 struct input *input = data;
2764 struct window *window = input->pointer_focus;
2765 struct widget *widget;
2767 float sx = wl_fixed_to_double(sx_w);
2768 float sy = wl_fixed_to_double(sy_w);
2776 /* when making the window smaller - e.g. after a unmaximise we might
2777 * still have a pending motion event that the compositor has picked
2778 * based on the old surface dimensions
2780 if (sx > window->main_surface->allocation.width ||
2781 sy > window->main_surface->allocation.height)
2784 if (!(input->grab && input->grab_button)) {
2785 widget = window_find_widget(window, sx, sy);
2786 input_set_focus_widget(input, widget, sx, sy);
2790 widget = input->grab;
2792 widget = input->focus_widget;
2794 if (widget->motion_handler)
2795 cursor = widget->motion_handler(input->focus_widget,
2796 input, time, sx, sy,
2799 cursor = widget->default_cursor;
2801 cursor = CURSOR_LEFT_PTR;
2803 input_set_pointer_image(input, cursor);
2807 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2808 uint32_t time, uint32_t button, uint32_t state_w)
2810 struct input *input = data;
2811 struct widget *widget;
2812 enum wl_pointer_button_state state = state_w;
2814 input->display->serial = serial;
2815 if (input->focus_widget && input->grab == NULL &&
2816 state == WL_POINTER_BUTTON_STATE_PRESSED)
2817 input_grab(input, input->focus_widget, button);
2819 widget = input->grab;
2820 if (widget && widget->button_handler)
2821 (*widget->button_handler)(widget,
2824 input->grab->user_data);
2826 if (input->grab && input->grab_button == button &&
2827 state == WL_POINTER_BUTTON_STATE_RELEASED)
2828 input_ungrab(input);
2832 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2833 uint32_t time, uint32_t axis, wl_fixed_t value)
2835 struct input *input = data;
2836 struct widget *widget;
2838 widget = input->focus_widget;
2840 widget = input->grab;
2841 if (widget && widget->axis_handler)
2842 (*widget->axis_handler)(widget,
2848 static const struct wl_pointer_listener pointer_listener = {
2849 pointer_handle_enter,
2850 pointer_handle_leave,
2851 pointer_handle_motion,
2852 pointer_handle_button,
2853 pointer_handle_axis,
2857 input_remove_keyboard_focus(struct input *input)
2859 struct window *window = input->keyboard_focus;
2860 struct itimerspec its;
2862 its.it_interval.tv_sec = 0;
2863 its.it_interval.tv_nsec = 0;
2864 its.it_value.tv_sec = 0;
2865 its.it_value.tv_nsec = 0;
2866 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2871 if (window->keyboard_focus_handler)
2872 (*window->keyboard_focus_handler)(window, NULL,
2875 input->keyboard_focus = NULL;
2879 keyboard_repeat_func(struct task *task, uint32_t events)
2881 struct input *input =
2882 container_of(task, struct input, repeat_task);
2883 struct window *window = input->keyboard_focus;
2886 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2887 /* If we change the timer between the fd becoming
2888 * readable and getting here, there'll be nothing to
2889 * read and we get EAGAIN. */
2892 if (window && window->key_handler) {
2893 (*window->key_handler)(window, input, input->repeat_time,
2894 input->repeat_key, input->repeat_sym,
2895 WL_KEYBOARD_KEY_STATE_PRESSED,
2901 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2902 uint32_t format, int fd, uint32_t size)
2904 struct input *input = data;
2905 struct xkb_keymap *keymap;
2906 struct xkb_state *state;
2914 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2919 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2920 if (map_str == MAP_FAILED) {
2925 keymap = xkb_keymap_new_from_string(input->display->xkb_context,
2927 XKB_KEYMAP_FORMAT_TEXT_V1,
2929 munmap(map_str, size);
2933 fprintf(stderr, "failed to compile keymap\n");
2937 state = xkb_state_new(keymap);
2939 fprintf(stderr, "failed to create XKB state\n");
2940 xkb_keymap_unref(keymap);
2944 xkb_keymap_unref(input->xkb.keymap);
2945 xkb_state_unref(input->xkb.state);
2946 input->xkb.keymap = keymap;
2947 input->xkb.state = state;
2949 input->xkb.control_mask =
2950 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
2951 input->xkb.alt_mask =
2952 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
2953 input->xkb.shift_mask =
2954 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
2958 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2959 uint32_t serial, struct wl_surface *surface,
2960 struct wl_array *keys)
2962 struct input *input = data;
2963 struct window *window;
2965 input->display->serial = serial;
2966 input->keyboard_focus = wl_surface_get_user_data(surface);
2968 window = input->keyboard_focus;
2969 if (window->keyboard_focus_handler)
2970 (*window->keyboard_focus_handler)(window,
2971 input, window->user_data);
2975 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2976 uint32_t serial, struct wl_surface *surface)
2978 struct input *input = data;
2980 input->display->serial = serial;
2981 input_remove_keyboard_focus(input);
2985 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2986 uint32_t serial, uint32_t time, uint32_t key,
2989 struct input *input = data;
2990 struct window *window = input->keyboard_focus;
2991 uint32_t code, num_syms;
2992 enum wl_keyboard_key_state state = state_w;
2993 const xkb_keysym_t *syms;
2995 struct itimerspec its;
2997 input->display->serial = serial;
2999 if (!window || !input->xkb.state)
3002 /* We only use input grabs for pointer events for now, so just
3003 * ignore key presses if a grab is active. We expand the key
3004 * event delivery mechanism to route events to widgets to
3005 * properly handle key grabs. In the meantime, this prevents
3006 * key event devlivery while a grab is active. */
3007 if (input->grab && input->grab_button == 0)
3010 num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);
3012 sym = XKB_KEY_NoSymbol;
3017 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
3018 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3019 window_set_maximized(window, !window->maximized);
3020 } else if (sym == XKB_KEY_F11 &&
3021 window->fullscreen_handler &&
3022 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3023 window->fullscreen_handler(window, window->user_data);
3024 } else if (sym == XKB_KEY_F4 &&
3025 input->modifiers == MOD_ALT_MASK &&
3026 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3027 window_close(window);
3028 } else if (window->key_handler) {
3029 (*window->key_handler)(window, input, time, key,
3030 sym, state, window->user_data);
3033 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3034 key == input->repeat_key) {
3035 its.it_interval.tv_sec = 0;
3036 its.it_interval.tv_nsec = 0;
3037 its.it_value.tv_sec = 0;
3038 its.it_value.tv_nsec = 0;
3039 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3040 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
3041 xkb_keymap_key_repeats(input->xkb.keymap, code)) {
3042 input->repeat_sym = sym;
3043 input->repeat_key = key;
3044 input->repeat_time = time;
3045 its.it_interval.tv_sec = input->repeat_rate_sec;
3046 its.it_interval.tv_nsec = input->repeat_rate_nsec;
3047 its.it_value.tv_sec = input->repeat_delay_sec;
3048 its.it_value.tv_nsec = input->repeat_delay_nsec;
3049 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3054 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3055 uint32_t serial, uint32_t mods_depressed,
3056 uint32_t mods_latched, uint32_t mods_locked,
3059 struct input *input = data;
3060 xkb_mod_mask_t mask;
3062 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3063 if (!input->xkb.keymap)
3066 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3067 mods_locked, 0, 0, group);
3068 mask = xkb_state_serialize_mods(input->xkb.state,
3069 XKB_STATE_MODS_DEPRESSED |
3070 XKB_STATE_MODS_LATCHED);
3071 input->modifiers = 0;
3072 if (mask & input->xkb.control_mask)
3073 input->modifiers |= MOD_CONTROL_MASK;
3074 if (mask & input->xkb.alt_mask)
3075 input->modifiers |= MOD_ALT_MASK;
3076 if (mask & input->xkb.shift_mask)
3077 input->modifiers |= MOD_SHIFT_MASK;
3081 set_repeat_info(struct input *input, int32_t rate, int32_t delay)
3083 input->repeat_rate_sec = input->repeat_rate_nsec = 0;
3084 input->repeat_delay_sec = input->repeat_delay_nsec = 0;
3086 /* a rate of zero disables any repeating, regardless of the delay's
3092 input->repeat_rate_sec = 1;
3094 input->repeat_rate_nsec = 1000000000 / rate;
3096 input->repeat_delay_sec = delay / 1000;
3097 delay -= (input->repeat_delay_sec * 1000);
3098 input->repeat_delay_nsec = delay * 1000 * 1000;
3102 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
3103 int32_t rate, int32_t delay)
3105 struct input *input = data;
3107 set_repeat_info(input, rate, delay);
3110 static const struct wl_keyboard_listener keyboard_listener = {
3111 keyboard_handle_keymap,
3112 keyboard_handle_enter,
3113 keyboard_handle_leave,
3114 keyboard_handle_key,
3115 keyboard_handle_modifiers,
3116 keyboard_handle_repeat_info
3121 touch_handle_down(void *data, struct wl_touch *wl_touch,
3122 uint32_t serial, uint32_t time, struct wl_surface *surface,
3123 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3125 struct input *input = data;
3126 struct widget *widget;
3127 float sx = wl_fixed_to_double(x_w);
3128 float sy = wl_fixed_to_double(y_w);
3130 input->display->serial = serial;
3131 input->touch_focus = wl_surface_get_user_data(surface);
3132 if (!input->touch_focus) {
3133 DBG("Failed to find to touch focus for surface %p\n", surface);
3137 if (surface != input->touch_focus->main_surface->surface) {
3138 DBG("Ignoring input event from subsurface %p\n", surface);
3139 input->touch_focus = NULL;
3144 widget = input->grab;
3146 widget = window_find_widget(input->touch_focus,
3147 wl_fixed_to_double(x_w),
3148 wl_fixed_to_double(y_w));
3150 struct touch_point *tp = xmalloc(sizeof *tp);
3153 tp->widget = widget;
3156 wl_list_insert(&input->touch_point_list, &tp->link);
3158 if (widget->touch_down_handler)
3159 (*widget->touch_down_handler)(widget, input,
3168 touch_handle_up(void *data, struct wl_touch *wl_touch,
3169 uint32_t serial, uint32_t time, int32_t id)
3171 struct input *input = data;
3172 struct touch_point *tp, *tmp;
3174 if (!input->touch_focus) {
3175 DBG("No touch focus found for touch up event!\n");
3179 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3183 if (tp->widget->touch_up_handler)
3184 (*tp->widget->touch_up_handler)(tp->widget, input, serial,
3186 tp->widget->user_data);
3188 wl_list_remove(&tp->link);
3196 touch_handle_motion(void *data, struct wl_touch *wl_touch,
3197 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
3199 struct input *input = data;
3200 struct touch_point *tp;
3201 float sx = wl_fixed_to_double(x_w);
3202 float sy = wl_fixed_to_double(y_w);
3204 DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&input->touch_point_list));
3206 if (!input->touch_focus) {
3207 DBG("No touch focus found for touch motion event!\n");
3211 wl_list_for_each(tp, &input->touch_point_list, link) {
3217 if (tp->widget->touch_motion_handler)
3218 (*tp->widget->touch_motion_handler)(tp->widget, input, time,
3220 tp->widget->user_data);
3226 touch_handle_frame(void *data, struct wl_touch *wl_touch)
3228 struct input *input = data;
3229 struct touch_point *tp, *tmp;
3231 DBG("touch_handle_frame\n");
3233 if (!input->touch_focus) {
3234 DBG("No touch focus found for touch frame event!\n");
3238 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3239 if (tp->widget->touch_frame_handler)
3240 (*tp->widget->touch_frame_handler)(tp->widget, input,
3241 tp->widget->user_data);
3246 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
3248 struct input *input = data;
3249 struct touch_point *tp, *tmp;
3251 DBG("touch_handle_cancel\n");
3253 if (!input->touch_focus) {
3254 DBG("No touch focus found for touch cancel event!\n");
3258 wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
3259 if (tp->widget->touch_cancel_handler)
3260 (*tp->widget->touch_cancel_handler)(tp->widget, input,
3261 tp->widget->user_data);
3263 wl_list_remove(&tp->link);
3268 static const struct wl_touch_listener touch_listener = {
3271 touch_handle_motion,
3273 touch_handle_cancel,
3277 seat_handle_capabilities(void *data, struct wl_seat *seat,
3278 enum wl_seat_capability caps)
3280 struct input *input = data;
3282 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3283 input->pointer = wl_seat_get_pointer(seat);
3284 wl_pointer_set_user_data(input->pointer, input);
3285 wl_pointer_add_listener(input->pointer, &pointer_listener,
3287 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3288 wl_pointer_destroy(input->pointer);
3289 input->pointer = NULL;
3292 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3293 input->keyboard = wl_seat_get_keyboard(seat);
3294 wl_keyboard_set_user_data(input->keyboard, input);
3295 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3297 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3298 wl_keyboard_destroy(input->keyboard);
3299 input->keyboard = NULL;
3302 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
3303 input->touch = wl_seat_get_touch(seat);
3304 wl_touch_set_user_data(input->touch, input);
3305 wl_touch_add_listener(input->touch, &touch_listener, input);
3306 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
3307 wl_touch_destroy(input->touch);
3308 input->touch = NULL;
3313 seat_handle_name(void *data, struct wl_seat *seat,
3319 static const struct wl_seat_listener seat_listener = {
3320 seat_handle_capabilities,
3325 input_get_position(struct input *input, int32_t *x, int32_t *y)
3332 input_get_touch(struct input *input, int32_t id, float *x, float *y)
3334 struct touch_point *tp;
3336 wl_list_for_each(tp, &input->touch_point_list, link) {
3349 input_get_display(struct input *input)
3351 return input->display;
3355 input_get_seat(struct input *input)
3361 input_get_modifiers(struct input *input)
3363 return input->modifiers;
3367 input_get_focus_widget(struct input *input)
3369 return input->focus_widget;
3373 struct wl_data_offer *offer;
3374 struct input *input;
3375 struct wl_array types;
3378 struct task io_task;
3386 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3388 struct data_offer *offer = data;
3391 p = wl_array_add(&offer->types, sizeof *p);
3395 static const struct wl_data_offer_listener data_offer_listener = {
3400 data_offer_destroy(struct data_offer *offer)
3405 if (offer->refcount == 0) {
3406 wl_data_offer_destroy(offer->offer);
3407 for (p = offer->types.data; *p; p++)
3409 wl_array_release(&offer->types);
3415 data_device_data_offer(void *data,
3416 struct wl_data_device *data_device,
3417 struct wl_data_offer *_offer)
3419 struct data_offer *offer;
3421 offer = xmalloc(sizeof *offer);
3423 wl_array_init(&offer->types);
3424 offer->refcount = 1;
3425 offer->input = data;
3426 offer->offer = _offer;
3427 wl_data_offer_add_listener(offer->offer,
3428 &data_offer_listener, offer);
3432 data_device_enter(void *data, struct wl_data_device *data_device,
3433 uint32_t serial, struct wl_surface *surface,
3434 wl_fixed_t x_w, wl_fixed_t y_w,
3435 struct wl_data_offer *offer)
3437 struct input *input = data;
3438 struct window *window;
3440 float x = wl_fixed_to_double(x_w);
3441 float y = wl_fixed_to_double(y_w);
3444 window = wl_surface_get_user_data(surface);
3445 input->drag_enter_serial = serial;
3446 input->drag_focus = window,
3450 if (!input->touch_grab)
3451 input->pointer_enter_serial = serial;
3454 input->drag_offer = wl_data_offer_get_user_data(offer);
3456 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3459 types_data = input->drag_offer->types.data;
3461 input->drag_offer = NULL;
3465 if (window->data_handler)
3466 window->data_handler(window, input, x, y, types_data,
3471 data_device_leave(void *data, struct wl_data_device *data_device)
3473 struct input *input = data;
3475 if (input->drag_offer) {
3476 data_offer_destroy(input->drag_offer);
3477 input->drag_offer = NULL;
3482 data_device_motion(void *data, struct wl_data_device *data_device,
3483 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3485 struct input *input = data;
3486 struct window *window = input->drag_focus;
3487 float x = wl_fixed_to_double(x_w);
3488 float y = wl_fixed_to_double(y_w);
3494 if (input->drag_offer)
3495 types_data = input->drag_offer->types.data;
3499 if (window->data_handler)
3500 window->data_handler(window, input, x, y, types_data,
3505 data_device_drop(void *data, struct wl_data_device *data_device)
3507 struct input *input = data;
3508 struct window *window = input->drag_focus;
3514 if (window->drop_handler)
3515 window->drop_handler(window, input,
3516 x, y, window->user_data);
3518 if (input->touch_grab)
3519 touch_ungrab(input);
3523 data_device_selection(void *data,
3524 struct wl_data_device *wl_data_device,
3525 struct wl_data_offer *offer)
3527 struct input *input = data;
3530 if (input->selection_offer)
3531 data_offer_destroy(input->selection_offer);
3534 input->selection_offer = wl_data_offer_get_user_data(offer);
3535 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3538 input->selection_offer = NULL;
3542 static const struct wl_data_device_listener data_device_listener = {
3543 data_device_data_offer,
3548 data_device_selection
3552 input_set_pointer_image_index(struct input *input, int index)
3554 struct wl_buffer *buffer;
3555 struct wl_cursor *cursor;
3556 struct wl_cursor_image *image;
3558 if (!input->pointer)
3561 cursor = input->display->cursors[input->current_cursor];
3565 if (index >= (int) cursor->image_count) {
3566 fprintf(stderr, "cursor index out of range\n");
3570 image = cursor->images[index];
3571 buffer = wl_cursor_image_get_buffer(image);
3575 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3576 wl_surface_damage(input->pointer_surface, 0, 0,
3577 image->width, image->height);
3578 wl_surface_commit(input->pointer_surface);
3579 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3580 input->pointer_surface,
3581 image->hotspot_x, image->hotspot_y);
3584 static const struct wl_callback_listener pointer_surface_listener;
3587 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3590 struct input *input = data;
3591 struct wl_cursor *cursor;
3595 assert(callback == input->cursor_frame_cb);
3596 wl_callback_destroy(callback);
3597 input->cursor_frame_cb = NULL;
3600 if (!input->pointer)
3603 if (input->current_cursor == CURSOR_BLANK) {
3604 wl_pointer_set_cursor(input->pointer,
3605 input->pointer_enter_serial,
3610 if (input->current_cursor == CURSOR_UNSET)
3612 cursor = input->display->cursors[input->current_cursor];
3616 /* FIXME We don't have the current time on the first call so we set
3617 * the animation start to the time of the first frame callback. */
3619 input->cursor_anim_start = 0;
3620 else if (input->cursor_anim_start == 0)
3621 input->cursor_anim_start = time;
3623 if (time == 0 || input->cursor_anim_start == 0)
3626 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3628 if (cursor->image_count > 1) {
3629 input->cursor_frame_cb =
3630 wl_surface_frame(input->pointer_surface);
3631 wl_callback_add_listener(input->cursor_frame_cb,
3632 &pointer_surface_listener, input);
3635 input_set_pointer_image_index(input, i);
3638 static const struct wl_callback_listener pointer_surface_listener = {
3639 pointer_surface_frame_callback
3643 input_set_pointer_image(struct input *input, int pointer)
3647 if (!input->pointer)
3650 if (input->pointer_enter_serial > input->cursor_serial)
3653 if (!force && pointer == input->current_cursor)
3656 input->current_cursor = pointer;
3657 input->cursor_serial = input->pointer_enter_serial;
3658 if (!input->cursor_frame_cb)
3659 pointer_surface_frame_callback(input, NULL, 0);
3661 /* The current frame callback may be stuck if, for instance,
3662 * the set cursor request was processed by the server after
3663 * this client lost the focus. In this case the cursor surface
3664 * might not be mapped and the frame callback wouldn't ever
3665 * complete. Send a set_cursor and attach to try to map the
3666 * cursor surface again so that the callback will finish */
3667 input_set_pointer_image_index(input, 0);
3671 struct wl_data_device *
3672 input_get_data_device(struct input *input)
3674 return input->data_device;
3678 input_set_selection(struct input *input,
3679 struct wl_data_source *source, uint32_t time)
3681 if (input->data_device)
3682 wl_data_device_set_selection(input->data_device, source, time);
3686 input_accept(struct input *input, const char *type)
3688 wl_data_offer_accept(input->drag_offer->offer,
3689 input->drag_enter_serial, type);
3693 offer_io_func(struct task *task, uint32_t events)
3695 struct data_offer *offer =
3696 container_of(task, struct data_offer, io_task);
3700 len = read(offer->fd, buffer, sizeof buffer);
3701 offer->func(buffer, len,
3702 offer->x, offer->y, offer->user_data);
3706 data_offer_destroy(offer);
3711 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3712 data_func_t func, void *user_data)
3716 if (pipe2(p, O_CLOEXEC) == -1)
3719 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3722 offer->io_task.run = offer_io_func;
3726 offer->user_data = user_data;
3728 display_watch_fd(offer->input->display,
3729 offer->fd, EPOLLIN, &offer->io_task);
3733 input_receive_drag_data(struct input *input, const char *mime_type,
3734 data_func_t func, void *data)
3736 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3737 input->drag_offer->x = input->drag_x;
3738 input->drag_offer->y = input->drag_y;
3742 input_receive_drag_data_to_fd(struct input *input,
3743 const char *mime_type, int fd)
3745 if (input->drag_offer)
3746 wl_data_offer_receive(input->drag_offer->offer, mime_type, fd);
3752 input_receive_selection_data(struct input *input, const char *mime_type,
3753 data_func_t func, void *data)
3757 if (input->selection_offer == NULL)
3760 for (p = input->selection_offer->types.data; *p; p++)
3761 if (strcmp(mime_type, *p) == 0)
3767 data_offer_receive_data(input->selection_offer,
3768 mime_type, func, data);
3773 input_receive_selection_data_to_fd(struct input *input,
3774 const char *mime_type, int fd)
3776 if (input->selection_offer)
3777 wl_data_offer_receive(input->selection_offer->offer,
3784 window_move(struct window *window, struct input *input, uint32_t serial)
3786 if (!window->xdg_surface)
3789 xdg_surface_move(window->xdg_surface, input->seat, serial);
3793 surface_set_synchronized(struct surface *surface)
3795 if (!surface->subsurface)
3798 if (surface->synchronized)
3801 wl_subsurface_set_sync(surface->subsurface);
3802 surface->synchronized = 1;
3806 surface_set_synchronized_default(struct surface *surface)
3808 if (!surface->subsurface)
3811 if (surface->synchronized == surface->synchronized_default)
3814 if (surface->synchronized_default)
3815 wl_subsurface_set_sync(surface->subsurface);
3817 wl_subsurface_set_desync(surface->subsurface);
3819 surface->synchronized = surface->synchronized_default;
3823 surface_resize(struct surface *surface)
3825 struct widget *widget = surface->widget;
3826 struct wl_compositor *compositor = widget->window->display->compositor;
3828 if (surface->input_region) {
3829 wl_region_destroy(surface->input_region);
3830 surface->input_region = NULL;
3833 if (surface->opaque_region)
3834 wl_region_destroy(surface->opaque_region);
3836 surface->opaque_region = wl_compositor_create_region(compositor);
3838 if (widget->resize_handler)
3839 widget->resize_handler(widget,
3840 widget->allocation.width,
3841 widget->allocation.height,
3844 if (surface->subsurface &&
3845 (surface->allocation.x != widget->allocation.x ||
3846 surface->allocation.y != widget->allocation.y)) {
3847 wl_subsurface_set_position(surface->subsurface,
3848 widget->allocation.x,
3849 widget->allocation.y);
3851 if (surface->allocation.width != widget->allocation.width ||
3852 surface->allocation.height != widget->allocation.height) {
3853 window_schedule_redraw(widget->window);
3855 surface->allocation = widget->allocation;
3858 wl_region_add(surface->opaque_region, 0, 0,
3859 widget->allocation.width,
3860 widget->allocation.height);
3864 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3867 * This hack should be removed, when EGL respects
3868 * eglSwapInterval(0).
3870 * If this window has sub-surfaces, especially a free-running
3871 * EGL-widget, we need to post the parent surface once with
3872 * all the old state to guarantee, that the EGL-widget will
3873 * receive its frame callback soon. Otherwise, a forced call
3874 * to eglSwapBuffers may end up blocking, waiting for a frame
3875 * event that will never come, because we will commit the parent
3876 * surface with all new state only after eglSwapBuffers returns.
3878 * This assumes, that:
3879 * 1. When the EGL widget's resize hook is called, it pauses.
3880 * 2. When the EGL widget's redraw hook is called, it forces a
3881 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3882 * In a single threaded application condition 1 is a no-op.
3884 * XXX: This should actually be after the surface_resize() calls,
3885 * but cannot, because then it would commit the incomplete state
3886 * accumulated from the widget resize hooks.
3888 if (window->subsurface_list.next != &window->main_surface->link ||
3889 window->subsurface_list.prev != &window->main_surface->link)
3890 wl_surface_commit(window->main_surface->surface);
3894 window_do_resize(struct window *window)
3896 struct surface *surface;
3898 widget_set_allocation(window->main_surface->widget,
3899 window->pending_allocation.x,
3900 window->pending_allocation.y,
3901 window->pending_allocation.width,
3902 window->pending_allocation.height);
3904 surface_resize(window->main_surface);
3906 /* The main surface is in the list, too. Main surface's
3907 * resize_handler is responsible for calling widget_set_allocation()
3908 * on all sub-surface root widgets, so they will be resized
3911 wl_list_for_each(surface, &window->subsurface_list, link) {
3912 if (surface == window->main_surface)
3915 surface_set_synchronized(surface);
3916 surface_resize(surface);
3919 if (!window->fullscreen && !window->maximized)
3920 window->saved_allocation = window->pending_allocation;
3924 idle_resize(struct window *window)
3926 window->resize_needed = 0;
3927 window->redraw_needed = 1;
3929 DBG("from %dx%d to %dx%d\n",
3930 window->main_surface->server_allocation.width,
3931 window->main_surface->server_allocation.height,
3932 window->pending_allocation.width,
3933 window->pending_allocation.height);
3935 hack_prevent_EGL_sub_surface_deadlock(window);
3937 window_do_resize(window);
3941 undo_resize(struct window *window)
3943 window->pending_allocation.width =
3944 window->main_surface->server_allocation.width;
3945 window->pending_allocation.height =
3946 window->main_surface->server_allocation.height;
3948 DBG("back to %dx%d\n",
3949 window->main_surface->server_allocation.width,
3950 window->main_surface->server_allocation.height);
3952 window_do_resize(window);
3954 if (window->pending_allocation.width == 0 &&
3955 window->pending_allocation.height == 0) {
3956 fprintf(stderr, "Error: Could not draw a surface, "
3957 "most likely due to insufficient disk space in "
3958 "%s (XDG_RUNTIME_DIR).\n", getenv("XDG_RUNTIME_DIR"));
3964 window_schedule_resize(struct window *window, int width, int height)
3966 /* We should probably get these numbers from the theme. */
3967 const int min_width = 200, min_height = 200;
3969 window->pending_allocation.x = 0;
3970 window->pending_allocation.y = 0;
3971 window->pending_allocation.width = width;
3972 window->pending_allocation.height = height;
3974 if (window->min_allocation.width == 0) {
3975 if (width < min_width && window->frame)
3976 window->min_allocation.width = min_width;
3978 window->min_allocation.width = width;
3979 if (height < min_height && window->frame)
3980 window->min_allocation.height = min_height;
3982 window->min_allocation.height = height;
3985 if (window->pending_allocation.width < window->min_allocation.width)
3986 window->pending_allocation.width = window->min_allocation.width;
3987 if (window->pending_allocation.height < window->min_allocation.height)
3988 window->pending_allocation.height = window->min_allocation.height;
3990 window->resize_needed = 1;
3991 window_schedule_redraw(window);
3995 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3997 window_schedule_resize(widget->window, width, height);
4001 window_get_shadow_margin(struct window *window)
4003 if (window->frame && !window->fullscreen)
4004 return frame_get_shadow_margin(window->frame->frame);
4010 handle_xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
4011 int32_t width, int32_t height,
4012 struct wl_array *states, uint32_t serial)
4014 struct window *window = data;
4017 window->maximized = 0;
4018 window->fullscreen = 0;
4019 window->resizing = 0;
4020 window->focused = 0;
4022 wl_array_for_each(p, states) {
4023 uint32_t state = *p;
4025 case XDG_SURFACE_STATE_MAXIMIZED:
4026 window->maximized = 1;
4028 case XDG_SURFACE_STATE_FULLSCREEN:
4029 window->fullscreen = 1;
4031 case XDG_SURFACE_STATE_RESIZING:
4032 window->resizing = 1;
4034 case XDG_SURFACE_STATE_ACTIVATED:
4035 window->focused = 1;
4043 if (window->frame) {
4044 if (window->maximized) {
4045 frame_set_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4047 frame_unset_flag(window->frame->frame, FRAME_FLAG_MAXIMIZED);
4050 if (window->focused) {
4051 frame_set_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4053 frame_unset_flag(window->frame->frame, FRAME_FLAG_ACTIVE);
4057 if (width > 0 && height > 0) {
4058 /* The width / height params are for window geometry,
4059 * but window_schedule_resize takes allocation. Add
4060 * on the shadow margin to get the difference. */
4061 int margin = window_get_shadow_margin(window);
4063 window_schedule_resize(window,
4065 height + margin * 2);
4067 window_schedule_resize(window,
4068 window->saved_allocation.width,
4069 window->saved_allocation.height);
4072 xdg_surface_ack_configure(window->xdg_surface, serial);
4074 if (window->state_changed_handler)
4075 window->state_changed_handler(window, window->user_data);
4079 handle_xdg_surface_delete(void *data, struct xdg_surface *xdg_surface)
4081 struct window *window = data;
4082 window_close(window);
4085 static const struct xdg_surface_listener xdg_surface_listener = {
4086 handle_xdg_surface_configure,
4087 handle_xdg_surface_delete,
4091 window_sync_parent(struct window *window)
4093 struct wl_surface *parent_surface;
4095 if (!window->xdg_surface)
4099 parent_surface = window->parent->main_surface->surface;
4101 parent_surface = NULL;
4103 if (parent_surface == window->last_parent_surface)
4106 xdg_surface_set_parent(window->xdg_surface, parent_surface);
4107 window->last_parent_surface = parent_surface;
4111 window_get_geometry(struct window *window, struct rectangle *geometry)
4113 if (window->frame && !window->fullscreen)
4114 frame_input_rect(window->frame->frame,
4120 window_get_allocation(window, geometry);
4124 window_sync_geometry(struct window *window)
4126 struct rectangle geometry;
4128 if (!window->xdg_surface)
4131 window_get_geometry(window, &geometry);
4132 if (geometry.x == window->last_geometry.x &&
4133 geometry.y == window->last_geometry.y &&
4134 geometry.width == window->last_geometry.width &&
4135 geometry.height == window->last_geometry.height)
4138 xdg_surface_set_window_geometry(window->xdg_surface,
4143 window->last_geometry = geometry;
4147 window_flush(struct window *window)
4149 struct surface *surface;
4151 if (!window->custom) {
4152 if (window->xdg_surface) {
4153 window_sync_parent(window);
4154 window_sync_geometry(window);
4158 wl_list_for_each(surface, &window->subsurface_list, link) {
4159 if (surface == window->main_surface)
4162 surface_flush(surface);
4165 surface_flush(window->main_surface);
4169 menu_destroy(struct menu *menu)
4171 widget_destroy(menu->widget);
4172 window_destroy(menu->window);
4173 frame_destroy(menu->frame);
4178 window_get_allocation(struct window *window,
4179 struct rectangle *allocation)
4181 *allocation = window->main_surface->allocation;
4185 widget_redraw(struct widget *widget)
4187 struct widget *child;
4189 if (widget->redraw_handler)
4190 widget->redraw_handler(widget, widget->user_data);
4191 wl_list_for_each(child, &widget->child_list, link)
4192 widget_redraw(child);
4196 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
4198 struct surface *surface = data;
4200 assert(callback == surface->frame_cb);
4201 DBG_OBJ(callback, "done\n");
4202 wl_callback_destroy(callback);
4203 surface->frame_cb = NULL;
4205 surface->last_time = time;
4207 if (surface->redraw_needed || surface->window->redraw_needed) {
4208 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
4209 window_schedule_redraw_task(surface->window);
4213 static const struct wl_callback_listener listener = {
4218 surface_redraw(struct surface *surface)
4220 DBG_OBJ(surface->surface, "begin\n");
4222 if (!surface->window->redraw_needed && !surface->redraw_needed)
4225 /* Whole-window redraw forces a redraw even if the previous has
4226 * not yet hit the screen.
4228 if (surface->frame_cb) {
4229 if (!surface->window->redraw_needed)
4232 DBG_OBJ(surface->frame_cb, "cancelled\n");
4233 wl_callback_destroy(surface->frame_cb);
4236 if (surface->widget->use_cairo &&
4237 !widget_get_cairo_surface(surface->widget)) {
4238 DBG_OBJ(surface->surface, "cancelled due buffer failure\n");
4242 surface->frame_cb = wl_surface_frame(surface->surface);
4243 wl_callback_add_listener(surface->frame_cb, &listener, surface);
4244 DBG_OBJ(surface->frame_cb, "new\n");
4246 surface->redraw_needed = 0;
4247 DBG_OBJ(surface->surface, "-> widget_redraw\n");
4248 widget_redraw(surface->widget);
4249 DBG_OBJ(surface->surface, "done\n");
4254 idle_redraw(struct task *task, uint32_t events)
4256 struct window *window = container_of(task, struct window, redraw_task);
4257 struct surface *surface;
4261 DBG(" --------- \n");
4263 wl_list_init(&window->redraw_task.link);
4264 window->redraw_task_scheduled = 0;
4266 if (window->resize_needed) {
4267 /* throttle resizing to the main surface display */
4268 if (window->main_surface->frame_cb) {
4269 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
4273 idle_resize(window);
4277 if (surface_redraw(window->main_surface) < 0) {
4279 * Only main_surface failure will cause us to undo the resize.
4280 * If sub-surfaces fail, they will just be broken with old
4285 wl_list_for_each(surface, &window->subsurface_list, link) {
4286 if (surface == window->main_surface)
4289 surface_redraw(surface);
4293 window->redraw_needed = 0;
4294 window_flush(window);
4296 wl_list_for_each(surface, &window->subsurface_list, link)
4297 surface_set_synchronized_default(surface);
4299 if (resized && failed) {
4300 /* Restore widget tree to correspond to what is on screen. */
4301 undo_resize(window);
4306 window_schedule_redraw_task(struct window *window)
4308 if (!window->redraw_task_scheduled) {
4309 window->redraw_task.run = idle_redraw;
4310 display_defer(window->display, &window->redraw_task);
4311 window->redraw_task_scheduled = 1;
4316 window_schedule_redraw(struct window *window)
4318 struct surface *surface;
4320 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
4322 wl_list_for_each(surface, &window->subsurface_list, link)
4323 surface->redraw_needed = 1;
4325 window_schedule_redraw_task(window);
4329 window_is_fullscreen(struct window *window)
4331 return window->fullscreen;
4335 window_set_fullscreen(struct window *window, int fullscreen)
4337 if (!window->xdg_surface)
4340 if (window->fullscreen == fullscreen)
4344 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
4346 xdg_surface_unset_fullscreen(window->xdg_surface);
4350 window_is_maximized(struct window *window)
4352 return window->maximized;
4356 window_set_maximized(struct window *window, int maximized)
4358 if (!window->xdg_surface)
4361 if (window->maximized == maximized)
4365 xdg_surface_set_maximized(window->xdg_surface);
4367 xdg_surface_unset_maximized(window->xdg_surface);
4371 window_is_resizing(struct window *window)
4373 return window->resizing;
4377 window_set_minimized(struct window *window)
4379 if (!window->xdg_surface)
4382 xdg_surface_set_minimized(window->xdg_surface);
4386 window_set_user_data(struct window *window, void *data)
4388 window->user_data = data;
4392 window_get_user_data(struct window *window)
4394 return window->user_data;
4398 window_set_key_handler(struct window *window,
4399 window_key_handler_t handler)
4401 window->key_handler = handler;
4405 window_set_keyboard_focus_handler(struct window *window,
4406 window_keyboard_focus_handler_t handler)
4408 window->keyboard_focus_handler = handler;
4412 window_set_data_handler(struct window *window, window_data_handler_t handler)
4414 window->data_handler = handler;
4418 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4420 window->drop_handler = handler;
4424 window_set_close_handler(struct window *window,
4425 window_close_handler_t handler)
4427 window->close_handler = handler;
4431 window_set_fullscreen_handler(struct window *window,
4432 window_fullscreen_handler_t handler)
4434 window->fullscreen_handler = handler;
4438 window_set_output_handler(struct window *window,
4439 window_output_handler_t handler)
4441 window->output_handler = handler;
4445 window_set_state_changed_handler(struct window *window,
4446 window_state_changed_handler_t handler)
4448 window->state_changed_handler = handler;
4452 window_set_title(struct window *window, const char *title)
4454 free(window->title);
4455 window->title = strdup(title);
4456 if (window->frame) {
4457 frame_set_title(window->frame->frame, title);
4458 widget_schedule_redraw(window->frame->widget);
4460 if (window->xdg_surface)
4461 xdg_surface_set_title(window->xdg_surface, title);
4462 if (window->shell_surface)
4463 wl_shell_surface_set_title(window->shell_surface, title);
4467 window_get_title(struct window *window)
4469 return window->title;
4473 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4475 struct text_cursor_position *text_cursor_position =
4476 window->display->text_cursor_position;
4478 if (!text_cursor_position)
4481 text_cursor_position_notify(text_cursor_position,
4482 window->main_surface->surface,
4483 wl_fixed_from_int(x),
4484 wl_fixed_from_int(y));
4488 window_damage(struct window *window, int32_t x, int32_t y,
4489 int32_t width, int32_t height)
4491 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4495 surface_enter(void *data,
4496 struct wl_surface *wl_surface, struct wl_output *wl_output)
4498 struct window *window = data;
4499 struct output *output;
4500 struct output *output_found = NULL;
4501 struct window_output *window_output;
4503 wl_list_for_each(output, &window->display->output_list, link) {
4504 if (output->output == wl_output) {
4505 output_found = output;
4513 window_output = xmalloc(sizeof *window_output);
4514 window_output->output = output_found;
4516 wl_list_insert (&window->window_output_list, &window_output->link);
4518 if (window->output_handler)
4519 window->output_handler(window, output_found, 1,
4524 surface_leave(void *data,
4525 struct wl_surface *wl_surface, struct wl_output *output)
4527 struct window *window = data;
4528 struct window_output *window_output;
4529 struct window_output *window_output_found = NULL;
4531 wl_list_for_each(window_output, &window->window_output_list, link) {
4532 if (window_output->output->output == output) {
4533 window_output_found = window_output;
4538 if (window_output_found) {
4539 wl_list_remove(&window_output_found->link);
4541 if (window->output_handler)
4542 window->output_handler(window, window_output->output,
4543 0, window->user_data);
4545 free(window_output_found);
4549 static const struct wl_surface_listener surface_listener = {
4554 static struct surface *
4555 surface_create(struct window *window)
4557 struct display *display = window->display;
4558 struct surface *surface;
4560 surface = xmalloc(sizeof *surface);
4561 memset(surface, 0, sizeof *surface);
4565 surface->window = window;
4566 surface->surface = wl_compositor_create_surface(display->compositor);
4567 surface->buffer_scale = 1;
4568 wl_surface_add_listener(surface->surface, &surface_listener, window);
4570 wl_list_insert(&window->subsurface_list, &surface->link);
4575 static enum window_buffer_type
4576 get_preferred_buffer_type(struct display *display)
4578 #ifdef HAVE_CAIRO_EGL
4579 if (display->argb_device && !getenv("TOYTOOLKIT_NO_EGL"))
4580 return WINDOW_BUFFER_TYPE_EGL_WINDOW;
4583 return WINDOW_BUFFER_TYPE_SHM;
4586 static struct window *
4587 window_create_internal(struct display *display, int custom)
4589 struct window *window;
4590 struct surface *surface;
4592 window = xzalloc(sizeof *window);
4593 wl_list_init(&window->subsurface_list);
4594 window->display = display;
4596 surface = surface_create(window);
4597 window->main_surface = surface;
4599 assert(custom || display->xdg_shell || display->ivi_application);
4601 window->custom = custom;
4602 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4604 surface->buffer_type = get_preferred_buffer_type(display);
4606 wl_surface_set_user_data(surface->surface, window);
4607 wl_list_insert(display->window_list.prev, &window->link);
4608 wl_list_init(&window->redraw_task.link);
4610 wl_list_init (&window->window_output_list);
4616 window_create(struct display *display)
4618 struct window *window;
4619 uint32_t id_ivisurf;
4621 window = window_create_internal(display, 0);
4623 if (window->display->xdg_shell) {
4624 window->xdg_surface =
4625 xdg_shell_get_xdg_surface(window->display->xdg_shell,
4626 window->main_surface->surface);
4627 fail_on_null(window->xdg_surface);
4629 xdg_surface_set_user_data(window->xdg_surface, window);
4630 xdg_surface_add_listener(window->xdg_surface,
4631 &xdg_surface_listener, window);
4632 } else if (display->ivi_application) {
4633 /* auto generation of ivi_id based on process id + basement of id */
4634 id_ivisurf = IVI_SURFACE_ID + (uint32_t)getpid();
4635 window->ivi_surface =
4636 ivi_application_surface_create(display->ivi_application,
4637 id_ivisurf, window->main_surface->surface);
4638 fail_on_null(window->ivi_surface);
4640 ivi_surface_add_listener(window->ivi_surface,
4641 &ivi_surface_listener, window);
4648 window_create_custom(struct display *display)
4650 return window_create_internal(display, 1);
4654 window_set_parent(struct window *window,
4655 struct window *parent_window)
4657 window->parent = parent_window;
4658 window_sync_parent(window);
4662 window_get_parent(struct window *window)
4664 return window->parent;
4668 menu_set_item(struct menu *menu, int sy)
4670 int32_t x, y, width, height;
4673 frame_interior(menu->frame, &x, &y, &width, &height);
4674 next = (sy - y) / 20;
4675 if (menu->current != next) {
4676 menu->current = next;
4677 widget_schedule_redraw(menu->widget);
4682 menu_motion_handler(struct widget *widget,
4683 struct input *input, uint32_t time,
4684 float x, float y, void *data)
4686 struct menu *menu = data;
4688 if (widget == menu->widget)
4689 menu_set_item(data, y);
4691 return CURSOR_LEFT_PTR;
4695 menu_enter_handler(struct widget *widget,
4696 struct input *input, float x, float y, void *data)
4698 struct menu *menu = data;
4700 if (widget == menu->widget)
4701 menu_set_item(data, y);
4703 return CURSOR_LEFT_PTR;
4707 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4709 struct menu *menu = data;
4711 if (widget == menu->widget)
4712 menu_set_item(data, -200);
4716 menu_button_handler(struct widget *widget,
4717 struct input *input, uint32_t time,
4718 uint32_t button, enum wl_pointer_button_state state,
4722 struct menu *menu = data;
4724 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4725 (menu->release_count > 0 || time - menu->time > 500)) {
4726 /* Either relase after press-drag-release or
4727 * click-motion-click. */
4728 menu->func(menu->user_data, input, menu->current);
4729 input_ungrab(input);
4731 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4732 menu->release_count++;
4737 menu_touch_up_handler(struct widget *widget,
4738 struct input *input,
4744 struct menu *menu = data;
4746 input_ungrab(input);
4751 menu_redraw_handler(struct widget *widget, void *data)
4754 struct menu *menu = data;
4755 int32_t x, y, width, height, i;
4757 cr = widget_cairo_create(widget);
4759 frame_repaint(menu->frame, cr);
4760 frame_interior(menu->frame, &x, &y, &width, &height);
4762 theme_set_background_source(menu->window->display->theme,
4763 cr, THEME_FRAME_ACTIVE);
4764 cairo_rectangle(cr, x, y, width, height);
4767 cairo_select_font_face(cr, "sans",
4768 CAIRO_FONT_SLANT_NORMAL,
4769 CAIRO_FONT_WEIGHT_NORMAL);
4770 cairo_set_font_size(cr, 12);
4772 for (i = 0; i < menu->count; i++) {
4773 if (i == menu->current) {
4774 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4775 cairo_rectangle(cr, x, y + i * 20, width, 20);
4777 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4778 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4779 cairo_show_text(cr, menu->entries[i]);
4781 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4782 cairo_move_to(cr, x + 10, y + i * 20 + 16);
4783 cairo_show_text(cr, menu->entries[i]);
4791 handle_popup_popup_done(void *data, struct xdg_popup *xdg_popup, uint32_t serial)
4793 struct window *window = data;
4794 struct menu *menu = window->main_surface->widget->user_data;
4796 input_ungrab(menu->input);
4800 static const struct xdg_popup_listener xdg_popup_listener = {
4801 handle_popup_popup_done,
4804 static struct menu *
4805 create_menu(struct display *display,
4806 struct input *input, uint32_t time,
4807 menu_func_t func, const char **entries, int count,
4810 struct window *window;
4813 menu = malloc(sizeof *menu);
4817 window = window_create_internal(display, 0);
4823 menu->window = window;
4824 menu->user_data = user_data;
4825 menu->widget = window_add_widget(menu->window, menu);
4826 menu->frame = frame_create(window->display->theme, 0, 0,
4827 FRAME_BUTTON_NONE, NULL);
4828 fail_on_null(menu->frame);
4829 menu->entries = entries;
4830 menu->count = count;
4831 menu->release_count = 0;
4835 menu->input = input;
4837 input_ungrab(input);
4839 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4840 widget_set_enter_handler(menu->widget, menu_enter_handler);
4841 widget_set_leave_handler(menu->widget, menu_leave_handler);
4842 widget_set_motion_handler(menu->widget, menu_motion_handler);
4843 widget_set_button_handler(menu->widget, menu_button_handler);
4844 widget_set_touch_up_handler(menu->widget, menu_touch_up_handler);
4846 input_grab(input, menu->widget, 0);
4847 frame_resize_inside(menu->frame, 200, count * 20);
4848 frame_set_flag(menu->frame, FRAME_FLAG_ACTIVE);
4849 window_schedule_resize(window, frame_width(menu->frame),
4850 frame_height(menu->frame));
4856 window_create_menu(struct display *display,
4857 struct input *input, uint32_t time,
4858 menu_func_t func, const char **entries, int count,
4862 menu = create_menu(display, input, time, func, entries, count, user_data);
4867 return menu->window;
4871 window_show_menu(struct display *display,
4872 struct input *input, uint32_t time, struct window *parent,
4873 int32_t x, int32_t y,
4874 menu_func_t func, const char **entries, int count)
4877 struct window *window;
4880 menu = create_menu(display, input, time, func, entries, count, parent);
4885 window = menu->window;
4887 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4888 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4893 frame_interior(menu->frame, &ix, &iy, NULL, NULL);
4895 if (!display->xdg_shell)
4898 window->xdg_popup = xdg_shell_get_xdg_popup(display->xdg_shell,
4899 window->main_surface->surface,
4900 parent->main_surface->surface,
4902 display_get_serial(window->display),
4906 fail_on_null(window->xdg_popup);
4908 xdg_popup_set_user_data(window->xdg_popup, window);
4909 xdg_popup_add_listener(window->xdg_popup,
4910 &xdg_popup_listener, window);
4914 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4916 window->main_surface->buffer_type = type;
4919 enum window_buffer_type
4920 window_get_buffer_type(struct window *window)
4922 return window->main_surface->buffer_type;
4926 window_set_preferred_format(struct window *window,
4927 enum preferred_format format)
4929 window->preferred_format = format;
4933 window_add_subsurface(struct window *window, void *data,
4934 enum subsurface_mode default_mode)
4936 struct widget *widget;
4937 struct surface *surface;
4938 struct wl_surface *parent;
4939 struct wl_subcompositor *subcompo = window->display->subcompositor;
4941 surface = surface_create(window);
4942 surface->buffer_type = window_get_buffer_type(window);
4943 widget = widget_create(window, surface, data);
4944 wl_list_init(&widget->link);
4945 surface->widget = widget;
4947 parent = window->main_surface->surface;
4948 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4951 surface->synchronized = 1;
4953 switch (default_mode) {
4954 case SUBSURFACE_SYNCHRONIZED:
4955 surface->synchronized_default = 1;
4957 case SUBSURFACE_DESYNCHRONIZED:
4958 surface->synchronized_default = 0;
4961 assert(!"bad enum subsurface_mode");
4964 window->resize_needed = 1;
4965 window_schedule_redraw(window);
4971 display_handle_geometry(void *data,
4972 struct wl_output *wl_output,
4975 int physical_height,
4981 struct output *output = data;
4983 output->allocation.x = x;
4984 output->allocation.y = y;
4985 output->transform = transform;
4989 output->make = strdup(make);
4992 free(output->model);
4993 output->model = strdup(model);
4997 display_handle_done(void *data,
4998 struct wl_output *wl_output)
5003 display_handle_scale(void *data,
5004 struct wl_output *wl_output,
5007 struct output *output = data;
5009 output->scale = scale;
5013 display_handle_mode(void *data,
5014 struct wl_output *wl_output,
5020 struct output *output = data;
5021 struct display *display = output->display;
5023 if (flags & WL_OUTPUT_MODE_CURRENT) {
5024 output->allocation.width = width;
5025 output->allocation.height = height;
5026 if (display->output_configure_handler)
5027 (*display->output_configure_handler)(
5028 output, display->user_data);
5032 static const struct wl_output_listener output_listener = {
5033 display_handle_geometry,
5034 display_handle_mode,
5035 display_handle_done,
5036 display_handle_scale
5040 display_add_output(struct display *d, uint32_t id)
5042 struct output *output;
5044 output = xzalloc(sizeof *output);
5045 output->display = d;
5048 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
5049 output->server_output_id = id;
5050 wl_list_insert(d->output_list.prev, &output->link);
5052 wl_output_add_listener(output->output, &output_listener, output);
5056 output_destroy(struct output *output)
5058 if (output->destroy_handler)
5059 (*output->destroy_handler)(output, output->user_data);
5061 wl_output_destroy(output->output);
5062 wl_list_remove(&output->link);
5067 display_destroy_output(struct display *d, uint32_t id)
5069 struct output *output;
5071 wl_list_for_each(output, &d->output_list, link) {
5072 if (output->server_output_id == id) {
5073 output_destroy(output);
5080 display_set_global_handler(struct display *display,
5081 display_global_handler_t handler)
5083 struct global *global;
5085 display->global_handler = handler;
5089 wl_list_for_each(global, &display->global_list, link)
5090 display->global_handler(display,
5091 global->name, global->interface,
5092 global->version, display->user_data);
5096 display_set_global_handler_remove(struct display *display,
5097 display_global_handler_t remove_handler)
5099 display->global_handler_remove = remove_handler;
5100 if (!remove_handler)
5105 display_set_output_configure_handler(struct display *display,
5106 display_output_handler_t handler)
5108 struct output *output;
5110 display->output_configure_handler = handler;
5114 wl_list_for_each(output, &display->output_list, link) {
5115 if (output->allocation.width == 0 &&
5116 output->allocation.height == 0)
5119 (*display->output_configure_handler)(output,
5120 display->user_data);
5125 output_set_user_data(struct output *output, void *data)
5127 output->user_data = data;
5131 output_get_user_data(struct output *output)
5133 return output->user_data;
5137 output_set_destroy_handler(struct output *output,
5138 display_output_handler_t handler)
5140 output->destroy_handler = handler;
5141 /* FIXME: implement this, once we have way to remove outputs */
5145 output_get_allocation(struct output *output, struct rectangle *base)
5147 struct rectangle allocation = output->allocation;
5149 switch (output->transform) {
5150 case WL_OUTPUT_TRANSFORM_90:
5151 case WL_OUTPUT_TRANSFORM_270:
5152 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
5153 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
5154 /* Swap width and height */
5155 allocation.width = output->allocation.height;
5156 allocation.height = output->allocation.width;
5164 output_get_wl_output(struct output *output)
5166 return output->output;
5169 enum wl_output_transform
5170 output_get_transform(struct output *output)
5172 return output->transform;
5176 output_get_scale(struct output *output)
5178 return output->scale;
5182 output_get_make(struct output *output)
5184 return output->make;
5188 output_get_model(struct output *output)
5190 return output->model;
5194 fini_xkb(struct input *input)
5196 xkb_state_unref(input->xkb.state);
5197 xkb_keymap_unref(input->xkb.keymap);
5200 #define MIN(a,b) ((a) < (b) ? a : b)
5203 display_add_input(struct display *d, uint32_t id)
5205 struct input *input;
5207 input = xzalloc(sizeof *input);
5209 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface,
5210 MIN(d->seat_version, 4));
5211 input->touch_focus = NULL;
5212 input->pointer_focus = NULL;
5213 input->keyboard_focus = NULL;
5214 wl_list_init(&input->touch_point_list);
5215 wl_list_insert(d->input_list.prev, &input->link);
5217 wl_seat_add_listener(input->seat, &seat_listener, input);
5218 wl_seat_set_user_data(input->seat, input);
5220 if (d->data_device_manager) {
5221 input->data_device =
5222 wl_data_device_manager_get_data_device(d->data_device_manager,
5224 wl_data_device_add_listener(input->data_device,
5225 &data_device_listener,
5229 input->pointer_surface = wl_compositor_create_surface(d->compositor);
5231 set_repeat_info(input, 40, 400);
5233 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
5234 TFD_CLOEXEC | TFD_NONBLOCK);
5235 input->repeat_task.run = keyboard_repeat_func;
5236 display_watch_fd(d, input->repeat_timer_fd,
5237 EPOLLIN, &input->repeat_task);
5241 input_destroy(struct input *input)
5243 input_remove_keyboard_focus(input);
5244 input_remove_pointer_focus(input);
5246 if (input->drag_offer)
5247 data_offer_destroy(input->drag_offer);
5249 if (input->selection_offer)
5250 data_offer_destroy(input->selection_offer);
5252 if (input->data_device)
5253 wl_data_device_destroy(input->data_device);
5255 if (input->display->seat_version >= 3) {
5257 wl_pointer_release(input->pointer);
5258 if (input->keyboard)
5259 wl_keyboard_release(input->keyboard);
5264 wl_surface_destroy(input->pointer_surface);
5266 wl_list_remove(&input->link);
5267 wl_seat_destroy(input->seat);
5268 close(input->repeat_timer_fd);
5273 init_workspace_manager(struct display *d, uint32_t id)
5275 d->workspace_manager =
5276 wl_registry_bind(d->registry, id,
5277 &workspace_manager_interface, 1);
5278 if (d->workspace_manager != NULL)
5279 workspace_manager_add_listener(d->workspace_manager,
5280 &workspace_manager_listener,
5285 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
5287 struct display *d = data;
5289 if (format == WL_SHM_FORMAT_RGB565)
5293 struct wl_shm_listener shm_listener = {
5298 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
5300 xdg_shell_pong(shell, serial);
5303 static const struct xdg_shell_listener xdg_shell_listener = {
5307 #define XDG_VERSION 4 /* The version of xdg-shell that we implement */
5308 #ifdef static_assert
5309 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
5310 "Interface version doesn't match implementation version");
5314 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
5315 const char *interface, uint32_t version)
5317 struct display *d = data;
5318 struct global *global;
5320 global = xmalloc(sizeof *global);
5322 global->interface = strdup(interface);
5323 global->version = version;
5324 wl_list_insert(d->global_list.prev, &global->link);
5326 if (strcmp(interface, "wl_compositor") == 0) {
5327 d->compositor = wl_registry_bind(registry, id,
5328 &wl_compositor_interface, 3);
5329 } else if (strcmp(interface, "wl_output") == 0) {
5330 display_add_output(d, id);
5331 } else if (strcmp(interface, "wl_seat") == 0) {
5332 d->seat_version = version;
5333 display_add_input(d, id);
5334 } else if (strcmp(interface, "wl_shm") == 0) {
5335 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
5336 wl_shm_add_listener(d->shm, &shm_listener, d);
5337 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
5338 d->data_device_manager =
5339 wl_registry_bind(registry, id,
5340 &wl_data_device_manager_interface, 1);
5341 } else if (strcmp(interface, "xdg_shell") == 0) {
5342 d->xdg_shell = wl_registry_bind(registry, id,
5343 &xdg_shell_interface, 1);
5344 xdg_shell_use_unstable_version(d->xdg_shell, XDG_VERSION);
5345 xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
5346 } else if (strcmp(interface, "text_cursor_position") == 0) {
5347 d->text_cursor_position =
5348 wl_registry_bind(registry, id,
5349 &text_cursor_position_interface, 1);
5350 } else if (strcmp(interface, "workspace_manager") == 0) {
5351 init_workspace_manager(d, id);
5352 } else if (strcmp(interface, "wl_subcompositor") == 0) {
5354 wl_registry_bind(registry, id,
5355 &wl_subcompositor_interface, 1);
5357 else if (strcmp(interface, "ivi_application") == 0) {
5358 d->ivi_application =
5359 wl_registry_bind(registry, id,
5360 &ivi_application_interface, 1);
5361 if (!d->create_ivi_surface) {
5362 fprintf(stderr, "ivi_application_interface is supported but IVI surface(s) "
5363 "will not be created\n"
5364 "due to command line option --no-ivi-surface\n");
5367 else if (strcmp(interface, "wl_shell") == 0) {
5369 wl_registry_bind(registry, id,
5370 &wl_shell_interface, 1);
5373 if (d->global_handler)
5374 d->global_handler(d, id, interface, version, d->user_data);
5378 registry_handle_global_remove(void *data, struct wl_registry *registry,
5381 struct display *d = data;
5382 struct global *global;
5385 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
5386 if (global->name != name)
5389 if (strcmp(global->interface, "wl_output") == 0)
5390 display_destroy_output(d, name);
5392 /* XXX: Should destroy remaining bound globals */
5394 if (d->global_handler_remove)
5395 d->global_handler_remove(d, name, global->interface,
5396 global->version, d->user_data);
5398 wl_list_remove(&global->link);
5399 free(global->interface);
5405 display_bind(struct display *display, uint32_t name,
5406 const struct wl_interface *interface, uint32_t version)
5408 return wl_registry_bind(display->registry, name, interface, version);
5411 static const struct wl_registry_listener registry_listener = {
5412 registry_handle_global,
5413 registry_handle_global_remove
5416 #ifdef HAVE_CAIRO_EGL
5418 init_egl(struct display *d)
5420 EGLint major, minor;
5423 #ifdef USE_CAIRO_GLESV2
5424 # define GL_BIT EGL_OPENGL_ES2_BIT
5426 # define GL_BIT EGL_OPENGL_BIT
5429 static const EGLint argb_cfg_attribs[] = {
5430 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
5436 EGL_RENDERABLE_TYPE, GL_BIT,
5440 #ifdef USE_CAIRO_GLESV2
5441 static const EGLint context_attribs[] = {
5442 EGL_CONTEXT_CLIENT_VERSION, 2,
5445 EGLint api = EGL_OPENGL_ES_API;
5447 EGLint *context_attribs = NULL;
5448 EGLint api = EGL_OPENGL_API;
5451 d->dpy = eglGetDisplay(d->display);
5452 if (!eglInitialize(d->dpy, &major, &minor)) {
5453 fprintf(stderr, "failed to initialize EGL\n");
5457 if (!eglBindAPI(api)) {
5458 fprintf(stderr, "failed to bind EGL client API\n");
5462 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
5463 &d->argb_config, 1, &n) || n != 1) {
5464 fprintf(stderr, "failed to choose argb EGL config\n");
5468 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
5469 EGL_NO_CONTEXT, context_attribs);
5470 if (d->argb_ctx == NULL) {
5471 fprintf(stderr, "failed to create EGL context\n");
5475 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
5476 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
5477 fprintf(stderr, "failed to get cairo EGL argb device\n");
5485 fini_egl(struct display *display)
5487 cairo_device_destroy(display->argb_device);
5489 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
5492 eglTerminate(display->dpy);
5498 init_dummy_surface(struct display *display)
5503 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
5505 display->dummy_surface =
5506 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
5508 display->dummy_surface_data = data;
5512 handle_display_data(struct task *task, uint32_t events)
5514 struct display *display =
5515 container_of(task, struct display, display_task);
5516 struct epoll_event ep;
5519 display->display_fd_events = events;
5521 if (events & EPOLLERR || events & EPOLLHUP) {
5522 display_exit(display);
5526 if (events & EPOLLIN) {
5527 ret = wl_display_dispatch(display->display);
5529 display_exit(display);
5534 if (events & EPOLLOUT) {
5535 ret = wl_display_flush(display->display);
5537 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
5538 ep.data.ptr = &display->display_task;
5539 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5540 display->display_fd, &ep);
5541 } else if (ret == -1 && errno != EAGAIN) {
5542 display_exit(display);
5549 log_handler(const char *format, va_list args)
5551 vfprintf(stderr, format, args);
5555 display_create(int *argc, char *argv[])
5558 bool create_ivi_surface;
5561 wl_log_set_handler_client(log_handler);
5563 create_ivi_surface = true;
5565 for (i = 0; i < *argc; i++) {
5566 if (!strcmp(argv[i], "--no-ivi-surface")) {
5567 create_ivi_surface = false;
5572 d = zalloc(sizeof *d);
5576 d->display = wl_display_connect(NULL);
5577 if (d->display == NULL) {
5578 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5583 d->xkb_context = xkb_context_new(0);
5584 if (d->xkb_context == NULL) {
5585 fprintf(stderr, "Failed to create XKB context\n");
5590 d->epoll_fd = os_epoll_create_cloexec();
5591 d->display_fd = wl_display_get_fd(d->display);
5592 d->display_task.run = handle_display_data;
5593 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5596 wl_list_init(&d->deferred_list);
5597 wl_list_init(&d->input_list);
5598 wl_list_init(&d->output_list);
5599 wl_list_init(&d->global_list);
5602 d->workspace_count = 1;
5604 d->create_ivi_surface = create_ivi_surface;
5606 d->registry = wl_display_get_registry(d->display);
5607 wl_registry_add_listener(d->registry, ®istry_listener, d);
5609 if (wl_display_dispatch(d->display) < 0) {
5610 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5614 #ifdef HAVE_CAIRO_EGL
5615 if (init_egl(d) < 0)
5616 fprintf(stderr, "EGL does not seem to work, "
5617 "falling back to software rendering and wl_shm.\n");
5622 d->theme = theme_create();
5624 wl_list_init(&d->window_list);
5626 init_dummy_surface(d);
5632 display_destroy_outputs(struct display *display)
5635 struct output *output;
5637 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5638 output_destroy(output);
5642 display_destroy_inputs(struct display *display)
5645 struct input *input;
5647 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5648 input_destroy(input);
5652 display_destroy(struct display *display)
5654 if (!wl_list_empty(&display->window_list))
5655 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5656 wl_list_length(&display->window_list));
5658 if (!wl_list_empty(&display->deferred_list))
5659 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5661 cairo_surface_destroy(display->dummy_surface);
5662 free(display->dummy_surface_data);
5664 display_destroy_outputs(display);
5665 display_destroy_inputs(display);
5667 xkb_context_unref(display->xkb_context);
5669 theme_destroy(display->theme);
5670 destroy_cursors(display);
5672 #ifdef HAVE_CAIRO_EGL
5673 if (display->argb_device)
5677 if (display->subcompositor)
5678 wl_subcompositor_destroy(display->subcompositor);
5680 if (display->xdg_shell)
5681 xdg_shell_destroy(display->xdg_shell);
5683 if (display->ivi_application)
5684 ivi_application_destroy(display->ivi_application);
5687 wl_shm_destroy(display->shm);
5689 if (display->data_device_manager)
5690 wl_data_device_manager_destroy(display->data_device_manager);
5692 wl_compositor_destroy(display->compositor);
5693 wl_registry_destroy(display->registry);
5695 close(display->epoll_fd);
5697 if (!(display->display_fd_events & EPOLLERR) &&
5698 !(display->display_fd_events & EPOLLHUP))
5699 wl_display_flush(display->display);
5701 wl_display_disconnect(display->display);
5706 display_set_user_data(struct display *display, void *data)
5708 display->user_data = data;
5712 display_get_user_data(struct display *display)
5714 return display->user_data;
5718 display_get_display(struct display *display)
5720 return display->display;
5724 display_has_subcompositor(struct display *display)
5726 if (display->subcompositor)
5729 wl_display_roundtrip(display->display);
5731 return display->subcompositor != NULL;
5735 display_get_cairo_device(struct display *display)
5737 return display->argb_device;
5741 display_get_output(struct display *display)
5743 return container_of(display->output_list.next, struct output, link);
5746 struct wl_compositor *
5747 display_get_compositor(struct display *display)
5749 return display->compositor;
5753 display_get_serial(struct display *display)
5755 return display->serial;
5759 display_get_egl_display(struct display *d)
5764 struct wl_data_source *
5765 display_create_data_source(struct display *display)
5767 if (display->data_device_manager)
5768 return wl_data_device_manager_create_data_source(display->data_device_manager);
5774 display_get_argb_egl_config(struct display *d)
5776 return d->argb_config;
5780 display_acquire_window_surface(struct display *display,
5781 struct window *window,
5784 struct surface *surface = window->main_surface;
5786 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5789 widget_get_cairo_surface(window->main_surface->widget);
5790 return surface->toysurface->acquire(surface->toysurface, ctx);
5794 display_release_window_surface(struct display *display,
5795 struct window *window)
5797 struct surface *surface = window->main_surface;
5799 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5802 surface->toysurface->release(surface->toysurface);
5806 display_defer(struct display *display, struct task *task)
5808 wl_list_insert(&display->deferred_list, &task->link);
5812 display_watch_fd(struct display *display,
5813 int fd, uint32_t events, struct task *task)
5815 struct epoll_event ep;
5819 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5823 display_unwatch_fd(struct display *display, int fd)
5825 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5829 display_run(struct display *display)
5832 struct epoll_event ep[16];
5835 display->running = 1;
5837 while (!wl_list_empty(&display->deferred_list)) {
5838 task = container_of(display->deferred_list.prev,
5840 wl_list_remove(&task->link);
5844 wl_display_dispatch_pending(display->display);
5846 if (!display->running)
5849 ret = wl_display_flush(display->display);
5850 if (ret < 0 && errno == EAGAIN) {
5852 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5853 ep[0].data.ptr = &display->display_task;
5855 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5856 display->display_fd, &ep[0]);
5857 } else if (ret < 0) {
5861 count = epoll_wait(display->epoll_fd,
5862 ep, ARRAY_LENGTH(ep), -1);
5863 for (i = 0; i < count; i++) {
5864 task = ep[i].data.ptr;
5865 task->run(task, ep[i].events);
5871 display_exit(struct display *display)
5873 display->running = 0;
5877 keysym_modifiers_add(struct wl_array *modifiers_map,
5880 size_t len = strlen(name) + 1;
5883 p = wl_array_add(modifiers_map, len);
5888 strncpy(p, name, len);
5891 static xkb_mod_index_t
5892 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5895 xkb_mod_index_t index = 0;
5896 char *p = modifiers_map->data;
5898 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5899 if (strcmp(p, name) == 0)
5906 return XKB_MOD_INVALID;
5910 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5913 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5915 if (index == XKB_MOD_INVALID)
5916 return XKB_MOD_INVALID;
5922 fail_on_null(void *p)
5925 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
5935 return fail_on_null(malloc(s));
5941 return fail_on_null(zalloc(s));
5945 xstrdup(const char *s)
5947 return fail_on_null(strdup(s));
5951 xrealloc(char *p, size_t s)
5953 return fail_on_null(realloc(p, s));