2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012-2013 Collabora, Ltd.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_subcompositor *subcompositor;
88 struct wl_shell *shell;
90 struct wl_data_device_manager *data_device_manager;
91 struct text_cursor_position *text_cursor_position;
92 struct workspace_manager *workspace_manager;
94 EGLConfig argb_config;
96 cairo_device_t *argb_device;
100 uint32_t display_fd_events;
101 struct task display_task;
104 struct wl_list deferred_list;
108 struct wl_list global_list;
109 struct wl_list window_list;
110 struct wl_list input_list;
111 struct wl_list output_list;
115 struct wl_cursor_theme *cursor_theme;
116 struct wl_cursor **cursors;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
123 struct xkb_context *xkb_context;
126 uint32_t workspace_count;
128 /* A hack to get text extents for tooltips */
129 cairo_surface_t *dummy_surface;
130 void *dummy_surface_data;
145 struct window_output {
146 struct output *output;
152 * Prepare the surface for drawing. Makes sure there is a surface
153 * of the right size available for rendering, and returns it.
154 * dx,dy are the x,y of wl_surface.attach.
155 * width,height are the new buffer size.
156 * If flags has SURFACE_HINT_RESIZE set, the user is
157 * doing continuous resizing.
158 * Returns the Cairo surface to draw to.
160 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
161 int32_t width, int32_t height, uint32_t flags,
162 enum wl_output_transform buffer_transform, int32_t buffer_scale);
165 * Post the surface to the server, returning the server allocation
166 * rectangle. The Cairo surface from prepare() must be destroyed
167 * after calling this.
169 void (*swap)(struct toysurface *base,
170 enum wl_output_transform buffer_transform, int32_t buffer_scale,
171 struct rectangle *server_allocation);
174 * Make the toysurface current with the given EGL context.
175 * Returns 0 on success, and negative of failure.
177 int (*acquire)(struct toysurface *base, EGLContext ctx);
180 * Release the toysurface from the EGL context, returning control
183 void (*release)(struct toysurface *base);
186 * Destroy the toysurface, including the Cairo surface, any
187 * backing storage, and the Wayland protocol objects.
189 void (*destroy)(struct toysurface *base);
193 struct window *window;
195 struct wl_surface *surface;
196 struct wl_subsurface *subsurface;
198 int synchronized_default;
199 struct toysurface *toysurface;
200 struct widget *widget;
202 struct wl_callback *frame_cb;
205 struct rectangle allocation;
206 struct rectangle server_allocation;
208 struct wl_region *input_region;
209 struct wl_region *opaque_region;
211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
213 int32_t buffer_scale;
215 cairo_surface_t *cairo_surface;
221 struct display *display;
222 struct window *parent;
223 struct wl_list window_output_list;
225 struct rectangle saved_allocation;
226 struct rectangle min_allocation;
227 struct rectangle pending_allocation;
231 int redraw_task_scheduled;
232 struct task redraw_task;
239 int fullscreen_method;
240 int configure_requests;
242 enum preferred_format preferred_format;
244 window_key_handler_t key_handler;
245 window_keyboard_focus_handler_t keyboard_focus_handler;
246 window_data_handler_t data_handler;
247 window_drop_handler_t drop_handler;
248 window_close_handler_t close_handler;
249 window_fullscreen_handler_t fullscreen_handler;
250 window_output_handler_t output_handler;
252 struct surface *main_surface;
253 struct wl_shell_surface *shell_surface;
257 /* struct surface::link, contains also main_surface */
258 struct wl_list subsurface_list;
265 struct window *window;
266 struct surface *surface;
267 struct tooltip *tooltip;
268 struct wl_list child_list;
270 struct rectangle allocation;
271 widget_resize_handler_t resize_handler;
272 widget_redraw_handler_t redraw_handler;
273 widget_enter_handler_t enter_handler;
274 widget_leave_handler_t leave_handler;
275 widget_motion_handler_t motion_handler;
276 widget_button_handler_t button_handler;
277 widget_axis_handler_t axis_handler;
285 struct display *display;
286 struct wl_seat *seat;
287 struct wl_pointer *pointer;
288 struct wl_keyboard *keyboard;
289 struct window *pointer_focus;
290 struct window *keyboard_focus;
292 uint32_t cursor_anim_start;
293 struct wl_callback *cursor_frame_cb;
294 struct wl_surface *pointer_surface;
296 uint32_t pointer_enter_serial;
297 uint32_t cursor_serial;
301 struct widget *focus_widget;
303 uint32_t grab_button;
305 struct wl_data_device *data_device;
306 struct data_offer *drag_offer;
307 struct data_offer *selection_offer;
310 struct xkb_keymap *keymap;
311 struct xkb_state *state;
312 xkb_mod_mask_t control_mask;
313 xkb_mod_mask_t alt_mask;
314 xkb_mod_mask_t shift_mask;
317 struct task repeat_task;
321 uint32_t repeat_time;
325 struct display *display;
326 struct wl_output *output;
327 struct rectangle allocation;
332 display_output_handler_t destroy_handler;
336 enum frame_button_action {
337 FRAME_BUTTON_NULL = 0,
338 FRAME_BUTTON_ICON = 1,
339 FRAME_BUTTON_CLOSE = 2,
340 FRAME_BUTTON_MINIMIZE = 3,
341 FRAME_BUTTON_MAXIMIZE = 4,
344 enum frame_button_pointer {
345 FRAME_BUTTON_DEFAULT = 0,
346 FRAME_BUTTON_OVER = 1,
347 FRAME_BUTTON_ACTIVE = 2,
350 enum frame_button_align {
351 FRAME_BUTTON_RIGHT = 0,
352 FRAME_BUTTON_LEFT = 1,
355 enum frame_button_decoration {
356 FRAME_BUTTON_NONE = 0,
357 FRAME_BUTTON_FANCY = 1,
360 struct frame_button {
361 struct widget *widget;
363 cairo_surface_t *icon;
364 enum frame_button_action type;
365 enum frame_button_pointer state;
366 struct wl_list link; /* buttons_list */
367 enum frame_button_align align;
368 enum frame_button_decoration decoration;
372 struct widget *widget;
373 struct widget *child;
374 struct wl_list buttons_list;
378 struct window *window;
379 struct widget *widget;
381 const char **entries;
390 struct widget *parent;
391 struct window *window;
392 struct widget *widget;
394 struct task tooltip_task;
400 struct wl_shm_pool *pool;
407 CURSOR_DEFAULT = 100,
411 enum window_location {
413 WINDOW_RESIZING_TOP = 1,
414 WINDOW_RESIZING_BOTTOM = 2,
415 WINDOW_RESIZING_LEFT = 4,
416 WINDOW_RESIZING_TOP_LEFT = 5,
417 WINDOW_RESIZING_BOTTOM_LEFT = 6,
418 WINDOW_RESIZING_RIGHT = 8,
419 WINDOW_RESIZING_TOP_RIGHT = 9,
420 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
421 WINDOW_RESIZING_MASK = 15,
422 WINDOW_EXTERIOR = 16,
423 WINDOW_TITLEBAR = 17,
424 WINDOW_CLIENT_AREA = 18,
427 static const cairo_user_data_key_t shm_surface_data_key;
434 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
435 __attribute__ ((format (printf, 4, 5)));
438 debug_print(void *proxy, int line, const char *func, const char *fmt, ...)
443 gettimeofday(&tv, NULL);
444 fprintf(stderr, "%8ld.%03ld ",
445 (long)tv.tv_sec & 0xffff, (long)tv.tv_usec / 1000);
448 fprintf(stderr, "%s@%d ",
449 wl_proxy_get_class(proxy), wl_proxy_get_id(proxy));
451 /*fprintf(stderr, __FILE__ ":%d:%s ", line, func);*/
452 fprintf(stderr, "%s ", func);
455 vfprintf(stderr, fmt, ap);
459 #define DBG(fmt, ...) \
460 debug_print(NULL, __LINE__, __func__, fmt, ##__VA_ARGS__)
462 #define DBG_OBJ(obj, fmt, ...) \
463 debug_print(obj, __LINE__, __func__, fmt, ##__VA_ARGS__)
467 #define DBG(...) do {} while (0)
468 #define DBG_OBJ(...) do {} while (0)
473 surface_to_buffer_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
477 switch (buffer_transform) {
478 case WL_OUTPUT_TRANSFORM_90:
479 case WL_OUTPUT_TRANSFORM_270:
480 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
481 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
490 *width *= buffer_scale;
491 *height *= buffer_scale;
495 buffer_to_surface_size (enum wl_output_transform buffer_transform, int32_t buffer_scale, int32_t *width, int32_t *height)
499 switch (buffer_transform) {
500 case WL_OUTPUT_TRANSFORM_90:
501 case WL_OUTPUT_TRANSFORM_270:
502 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
512 *width /= buffer_scale;
513 *height /= buffer_scale;
516 #ifdef HAVE_CAIRO_EGL
518 struct egl_window_surface {
519 struct toysurface base;
520 cairo_surface_t *cairo_surface;
521 struct display *display;
522 struct wl_surface *surface;
523 struct wl_egl_window *egl_window;
524 EGLSurface egl_surface;
527 static struct egl_window_surface *
528 to_egl_window_surface(struct toysurface *base)
530 return container_of(base, struct egl_window_surface, base);
533 static cairo_surface_t *
534 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
535 int32_t width, int32_t height, uint32_t flags,
536 enum wl_output_transform buffer_transform, int32_t buffer_scale)
538 struct egl_window_surface *surface = to_egl_window_surface(base);
540 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
542 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
543 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
545 return cairo_surface_reference(surface->cairo_surface);
549 egl_window_surface_swap(struct toysurface *base,
550 enum wl_output_transform buffer_transform, int32_t buffer_scale,
551 struct rectangle *server_allocation)
553 struct egl_window_surface *surface = to_egl_window_surface(base);
555 cairo_gl_surface_swapbuffers(surface->cairo_surface);
556 wl_egl_window_get_attached_size(surface->egl_window,
557 &server_allocation->width,
558 &server_allocation->height);
560 buffer_to_surface_size (buffer_transform, buffer_scale,
561 &server_allocation->width,
562 &server_allocation->height);
566 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
568 struct egl_window_surface *surface = to_egl_window_surface(base);
569 cairo_device_t *device;
571 device = cairo_surface_get_device(surface->cairo_surface);
576 if (device == surface->display->argb_device)
577 ctx = surface->display->argb_ctx;
582 cairo_device_flush(device);
583 cairo_device_acquire(device);
584 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
585 surface->egl_surface, ctx))
586 fprintf(stderr, "failed to make surface current\n");
592 egl_window_surface_release(struct toysurface *base)
594 struct egl_window_surface *surface = to_egl_window_surface(base);
595 cairo_device_t *device;
597 device = cairo_surface_get_device(surface->cairo_surface);
601 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
602 surface->display->argb_ctx))
603 fprintf(stderr, "failed to make context current\n");
605 cairo_device_release(device);
609 egl_window_surface_destroy(struct toysurface *base)
611 struct egl_window_surface *surface = to_egl_window_surface(base);
612 struct display *d = surface->display;
614 cairo_surface_destroy(surface->cairo_surface);
615 eglDestroySurface(d->dpy, surface->egl_surface);
616 wl_egl_window_destroy(surface->egl_window);
617 surface->surface = NULL;
622 static struct toysurface *
623 egl_window_surface_create(struct display *display,
624 struct wl_surface *wl_surface,
626 struct rectangle *rectangle)
628 struct egl_window_surface *surface;
630 if (display->dpy == EGL_NO_DISPLAY)
633 surface = calloc(1, sizeof *surface);
637 surface->base.prepare = egl_window_surface_prepare;
638 surface->base.swap = egl_window_surface_swap;
639 surface->base.acquire = egl_window_surface_acquire;
640 surface->base.release = egl_window_surface_release;
641 surface->base.destroy = egl_window_surface_destroy;
643 surface->display = display;
644 surface->surface = wl_surface;
646 surface->egl_window = wl_egl_window_create(surface->surface,
650 surface->egl_surface = eglCreateWindowSurface(display->dpy,
651 display->argb_config,
655 surface->cairo_surface =
656 cairo_gl_surface_create_for_egl(display->argb_device,
657 surface->egl_surface,
661 return &surface->base;
666 static struct toysurface *
667 egl_window_surface_create(struct display *display,
668 struct wl_surface *wl_surface,
670 struct rectangle *rectangle)
677 struct shm_surface_data {
678 struct wl_buffer *buffer;
679 struct shm_pool *pool;
683 display_get_buffer_for_surface(struct display *display,
684 cairo_surface_t *surface)
686 struct shm_surface_data *data;
688 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
694 shm_pool_destroy(struct shm_pool *pool);
697 shm_surface_data_destroy(void *p)
699 struct shm_surface_data *data = p;
701 wl_buffer_destroy(data->buffer);
703 shm_pool_destroy(data->pool);
708 static struct wl_shm_pool *
709 make_shm_pool(struct display *display, int size, void **data)
711 struct wl_shm_pool *pool;
714 fd = os_create_anonymous_file(size);
716 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
721 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
722 if (*data == MAP_FAILED) {
723 fprintf(stderr, "mmap failed: %m\n");
728 pool = wl_shm_create_pool(display->shm, fd, size);
735 static struct shm_pool *
736 shm_pool_create(struct display *display, size_t size)
738 struct shm_pool *pool = malloc(sizeof *pool);
743 pool->pool = make_shm_pool(display, size, &pool->data);
756 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
758 if (pool->used + size > pool->size)
761 *offset = pool->used;
764 return (char *) pool->data + *offset;
767 /* destroy the pool. this does not unmap the memory though */
769 shm_pool_destroy(struct shm_pool *pool)
771 munmap(pool->data, pool->size);
772 wl_shm_pool_destroy(pool->pool);
776 /* Start allocating from the beginning of the pool again */
778 shm_pool_reset(struct shm_pool *pool)
784 data_length_for_shm_surface(struct rectangle *rect)
788 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
790 return stride * rect->height;
793 static cairo_surface_t *
794 display_create_shm_surface_from_pool(struct display *display,
795 struct rectangle *rectangle,
796 uint32_t flags, struct shm_pool *pool)
798 struct shm_surface_data *data;
800 cairo_surface_t *surface;
801 cairo_format_t cairo_format;
802 int stride, length, offset;
805 data = malloc(sizeof *data);
809 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
810 cairo_format = CAIRO_FORMAT_RGB16_565;
812 cairo_format = CAIRO_FORMAT_ARGB32;
814 stride = cairo_format_stride_for_width (cairo_format, rectangle->width);
815 length = stride * rectangle->height;
817 map = shm_pool_allocate(pool, length, &offset);
824 surface = cairo_image_surface_create_for_data (map,
830 cairo_surface_set_user_data(surface, &shm_surface_data_key,
831 data, shm_surface_data_destroy);
833 if (flags & SURFACE_HINT_RGB565 && display->has_rgb565)
834 format = WL_SHM_FORMAT_RGB565;
836 if (flags & SURFACE_OPAQUE)
837 format = WL_SHM_FORMAT_XRGB8888;
839 format = WL_SHM_FORMAT_ARGB8888;
842 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
850 static cairo_surface_t *
851 display_create_shm_surface(struct display *display,
852 struct rectangle *rectangle, uint32_t flags,
853 struct shm_pool *alternate_pool,
854 struct shm_surface_data **data_ret)
856 struct shm_surface_data *data;
857 struct shm_pool *pool;
858 cairo_surface_t *surface;
860 if (alternate_pool) {
861 shm_pool_reset(alternate_pool);
862 surface = display_create_shm_surface_from_pool(display,
867 data = cairo_surface_get_user_data(surface,
868 &shm_surface_data_key);
873 pool = shm_pool_create(display,
874 data_length_for_shm_surface(rectangle));
879 display_create_shm_surface_from_pool(display, rectangle,
883 shm_pool_destroy(pool);
887 /* make sure we destroy the pool when the surface is destroyed */
888 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
899 check_size(struct rectangle *rect)
901 if (rect->width && rect->height)
904 fprintf(stderr, "tried to create surface of "
905 "width: %d, height: %d\n", rect->width, rect->height);
910 display_create_surface(struct display *display,
911 struct wl_surface *surface,
912 struct rectangle *rectangle,
915 if (check_size(rectangle) < 0)
918 assert(flags & SURFACE_SHM);
919 return display_create_shm_surface(display, rectangle, flags,
923 struct shm_surface_leaf {
924 cairo_surface_t *cairo_surface;
925 /* 'data' is automatically destroyed, when 'cairo_surface' is */
926 struct shm_surface_data *data;
928 struct shm_pool *resize_pool;
933 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
935 if (leaf->cairo_surface)
936 cairo_surface_destroy(leaf->cairo_surface);
937 /* leaf->data already destroyed via cairo private */
939 if (leaf->resize_pool)
940 shm_pool_destroy(leaf->resize_pool);
942 memset(leaf, 0, sizeof *leaf);
948 struct toysurface base;
949 struct display *display;
950 struct wl_surface *surface;
954 struct shm_surface_leaf leaf[MAX_LEAVES];
955 struct shm_surface_leaf *current;
958 static struct shm_surface *
959 to_shm_surface(struct toysurface *base)
961 return container_of(base, struct shm_surface, base);
965 shm_surface_buffer_state_debug(struct shm_surface *surface, const char *msg)
968 struct shm_surface_leaf *leaf;
969 char bufs[MAX_LEAVES + 1];
972 for (i = 0; i < MAX_LEAVES; i++) {
973 leaf = &surface->leaf[i];
977 else if (leaf->cairo_surface)
983 bufs[MAX_LEAVES] = '\0';
984 DBG_OBJ(surface->surface, "%s, leaves [%s]\n", msg, bufs);
989 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
991 struct shm_surface *surface = data;
992 struct shm_surface_leaf *leaf;
996 shm_surface_buffer_state_debug(surface, "buffer_release before");
998 for (i = 0; i < MAX_LEAVES; i++) {
999 leaf = &surface->leaf[i];
1000 if (leaf->data && leaf->data->buffer == buffer) {
1005 assert(i < MAX_LEAVES && "unknown buffer released");
1007 /* Leave one free leaf with storage, release others */
1009 for (i = 0; i < MAX_LEAVES; i++) {
1010 leaf = &surface->leaf[i];
1012 if (!leaf->cairo_surface || leaf->busy)
1018 shm_surface_leaf_release(leaf);
1021 shm_surface_buffer_state_debug(surface, "buffer_release after");
1024 static const struct wl_buffer_listener shm_surface_buffer_listener = {
1025 shm_surface_buffer_release
1028 static cairo_surface_t *
1029 shm_surface_prepare(struct toysurface *base, int dx, int dy,
1030 int32_t width, int32_t height, uint32_t flags,
1031 enum wl_output_transform buffer_transform, int32_t buffer_scale)
1033 int resize_hint = !!(flags & SURFACE_HINT_RESIZE);
1034 struct shm_surface *surface = to_shm_surface(base);
1035 struct rectangle rect = { 0};
1036 struct shm_surface_leaf *leaf = NULL;
1042 /* pick a free buffer, preferrably one that already has storage */
1043 for (i = 0; i < MAX_LEAVES; i++) {
1044 if (surface->leaf[i].busy)
1047 if (!leaf || surface->leaf[i].cairo_surface)
1048 leaf = &surface->leaf[i];
1050 DBG_OBJ(surface->surface, "pick leaf %d\n",
1051 (int)(leaf - &surface->leaf[0]));
1054 fprintf(stderr, "%s: all buffers are held by the server.\n",
1060 if (!resize_hint && leaf->resize_pool) {
1061 cairo_surface_destroy(leaf->cairo_surface);
1062 leaf->cairo_surface = NULL;
1063 shm_pool_destroy(leaf->resize_pool);
1064 leaf->resize_pool = NULL;
1067 surface_to_buffer_size (buffer_transform, buffer_scale, &width, &height);
1069 if (leaf->cairo_surface &&
1070 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
1071 cairo_image_surface_get_height(leaf->cairo_surface) == height)
1074 if (leaf->cairo_surface)
1075 cairo_surface_destroy(leaf->cairo_surface);
1077 #ifdef USE_RESIZE_POOL
1078 if (resize_hint && !leaf->resize_pool) {
1079 /* Create a big pool to allocate from, while continuously
1080 * resizing. Mmapping a new pool in the server
1081 * is relatively expensive, so reusing a pool performs
1082 * better, but may temporarily reserve unneeded memory.
1084 /* We should probably base this number on the output size. */
1085 leaf->resize_pool = shm_pool_create(surface->display,
1091 rect.height = height;
1093 leaf->cairo_surface =
1094 display_create_shm_surface(surface->display, &rect,
1098 wl_buffer_add_listener(leaf->data->buffer,
1099 &shm_surface_buffer_listener, surface);
1102 surface->current = leaf;
1104 return cairo_surface_reference(leaf->cairo_surface);
1108 shm_surface_swap(struct toysurface *base,
1109 enum wl_output_transform buffer_transform, int32_t buffer_scale,
1110 struct rectangle *server_allocation)
1112 struct shm_surface *surface = to_shm_surface(base);
1113 struct shm_surface_leaf *leaf = surface->current;
1115 server_allocation->width =
1116 cairo_image_surface_get_width(leaf->cairo_surface);
1117 server_allocation->height =
1118 cairo_image_surface_get_height(leaf->cairo_surface);
1120 buffer_to_surface_size (buffer_transform, buffer_scale,
1121 &server_allocation->width,
1122 &server_allocation->height);
1124 wl_surface_attach(surface->surface, leaf->data->buffer,
1125 surface->dx, surface->dy);
1126 wl_surface_damage(surface->surface, 0, 0,
1127 server_allocation->width, server_allocation->height);
1128 wl_surface_commit(surface->surface);
1130 DBG_OBJ(surface->surface, "leaf %d busy\n",
1131 (int)(leaf - &surface->leaf[0]));
1134 surface->current = NULL;
1138 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
1144 shm_surface_release(struct toysurface *base)
1149 shm_surface_destroy(struct toysurface *base)
1151 struct shm_surface *surface = to_shm_surface(base);
1154 for (i = 0; i < MAX_LEAVES; i++)
1155 shm_surface_leaf_release(&surface->leaf[i]);
1160 static struct toysurface *
1161 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
1162 uint32_t flags, struct rectangle *rectangle)
1164 struct shm_surface *surface;
1165 DBG_OBJ(wl_surface, "\n");
1167 surface = xmalloc(sizeof *surface);
1168 memset(surface, 0, sizeof *surface);
1173 surface->base.prepare = shm_surface_prepare;
1174 surface->base.swap = shm_surface_swap;
1175 surface->base.acquire = shm_surface_acquire;
1176 surface->base.release = shm_surface_release;
1177 surface->base.destroy = shm_surface_destroy;
1179 surface->display = display;
1180 surface->surface = wl_surface;
1181 surface->flags = flags;
1183 return &surface->base;
1187 * The following correspondences between file names and cursors was copied
1188 * from: https://bugs.kde.org/attachment.cgi?id=67313
1191 static const char *bottom_left_corners[] = {
1192 "bottom_left_corner",
1197 static const char *bottom_right_corners[] = {
1198 "bottom_right_corner",
1203 static const char *bottom_sides[] = {
1209 static const char *grabbings[] = {
1212 "208530c400c041818281048008011002"
1215 static const char *left_ptrs[] = {
1222 static const char *left_sides[] = {
1228 static const char *right_sides[] = {
1234 static const char *top_left_corners[] = {
1240 static const char *top_right_corners[] = {
1246 static const char *top_sides[] = {
1252 static const char *xterms[] = {
1258 static const char *hand1s[] = {
1262 "e29285e634086352946a0e7090d73106"
1265 static const char *watches[] = {
1268 "0426c94ea35c87780ff01dc239897213"
1271 struct cursor_alternatives {
1276 static const struct cursor_alternatives cursors[] = {
1277 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1278 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1279 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1280 {grabbings, ARRAY_LENGTH(grabbings)},
1281 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1282 {left_sides, ARRAY_LENGTH(left_sides)},
1283 {right_sides, ARRAY_LENGTH(right_sides)},
1284 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1285 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1286 {top_sides, ARRAY_LENGTH(top_sides)},
1287 {xterms, ARRAY_LENGTH(xterms)},
1288 {hand1s, ARRAY_LENGTH(hand1s)},
1289 {watches, ARRAY_LENGTH(watches)},
1293 create_cursors(struct display *display)
1297 unsigned int size = 32;
1299 struct wl_cursor *cursor;
1300 struct config_key shell_keys[] = {
1301 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1302 { "cursor-size", CONFIG_KEY_UNSIGNED_INTEGER, &size },
1304 struct config_section cs[] = {
1305 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1308 config_fd = open_config_file("weston.ini");
1309 parse_config_file(config_fd, cs, ARRAY_LENGTH(cs), NULL);
1312 display->cursor_theme = wl_cursor_theme_load(theme, size, display->shm);
1314 xmalloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1316 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1318 for (j = 0; !cursor && j < cursors[i].count; ++j)
1319 cursor = wl_cursor_theme_get_cursor(
1320 display->cursor_theme, cursors[i].names[j]);
1323 fprintf(stderr, "could not load cursor '%s'\n",
1324 cursors[i].names[0]);
1326 display->cursors[i] = cursor;
1331 destroy_cursors(struct display *display)
1333 wl_cursor_theme_destroy(display->cursor_theme);
1334 free(display->cursors);
1337 struct wl_cursor_image *
1338 display_get_pointer_image(struct display *display, int pointer)
1340 struct wl_cursor *cursor = display->cursors[pointer];
1342 return cursor ? cursor->images[0] : NULL;
1346 surface_flush(struct surface *surface)
1348 if (!surface->cairo_surface)
1351 if (surface->opaque_region) {
1352 wl_surface_set_opaque_region(surface->surface,
1353 surface->opaque_region);
1354 wl_region_destroy(surface->opaque_region);
1355 surface->opaque_region = NULL;
1358 if (surface->input_region) {
1359 wl_surface_set_input_region(surface->surface,
1360 surface->input_region);
1361 wl_region_destroy(surface->input_region);
1362 surface->input_region = NULL;
1365 surface->toysurface->swap(surface->toysurface,
1366 surface->buffer_transform, surface->buffer_scale,
1367 &surface->server_allocation);
1369 cairo_surface_destroy(surface->cairo_surface);
1370 surface->cairo_surface = NULL;
1374 window_has_focus(struct window *window)
1376 return window->focus_count > 0;
1380 window_flush(struct window *window)
1382 struct surface *surface;
1384 if (window->type == TYPE_NONE) {
1385 window->type = TYPE_TOPLEVEL;
1386 if (window->shell_surface)
1387 wl_shell_surface_set_toplevel(window->shell_surface);
1390 wl_list_for_each(surface, &window->subsurface_list, link) {
1391 if (surface == window->main_surface)
1394 surface_flush(surface);
1397 surface_flush(window->main_surface);
1401 window_get_display(struct window *window)
1403 return window->display;
1407 surface_create_surface(struct surface *surface, int dx, int dy, uint32_t flags)
1409 struct display *display = surface->window->display;
1410 struct rectangle allocation = surface->allocation;
1412 if (!surface->toysurface && display->dpy &&
1413 surface->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW) {
1414 surface->toysurface =
1415 egl_window_surface_create(display,
1421 if (!surface->toysurface)
1422 surface->toysurface = shm_surface_create(display,
1424 flags, &allocation);
1426 surface->cairo_surface = surface->toysurface->prepare(
1427 surface->toysurface, dx, dy,
1428 allocation.width, allocation.height, flags,
1429 surface->buffer_transform, surface->buffer_scale);
1433 window_create_main_surface(struct window *window)
1435 struct surface *surface = window->main_surface;
1440 if (window->resizing)
1441 flags |= SURFACE_HINT_RESIZE;
1443 if (window->preferred_format == WINDOW_PREFERRED_FORMAT_RGB565)
1444 flags |= SURFACE_HINT_RGB565;
1446 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1447 dx = surface->server_allocation.width -
1448 surface->allocation.width;
1450 if (window->resize_edges & WINDOW_RESIZING_TOP)
1451 dy = surface->server_allocation.height -
1452 surface->allocation.height;
1454 window->resize_edges = 0;
1456 surface_create_surface(surface, dx, dy, flags);
1460 window_get_buffer_transform(struct window *window)
1462 return window->main_surface->buffer_transform;
1466 window_set_buffer_transform(struct window *window,
1467 enum wl_output_transform transform)
1469 window->main_surface->buffer_transform = transform;
1470 wl_surface_set_buffer_transform(window->main_surface->surface,
1475 window_set_buffer_scale(struct window *window,
1478 window->main_surface->buffer_scale = scale;
1479 wl_surface_set_buffer_scale(window->main_surface->surface,
1484 window_get_buffer_scale(struct window *window)
1486 return window->main_surface->buffer_scale;
1490 window_get_output_scale(struct window *window)
1492 struct window_output *window_output;
1493 struct window_output *window_output_tmp;
1496 wl_list_for_each_safe(window_output, window_output_tmp,
1497 &window->window_output_list, link) {
1498 if (window_output->output->scale > scale)
1499 scale = window_output->output->scale;
1505 static void frame_destroy(struct frame *frame);
1508 surface_destroy(struct surface *surface)
1510 if (surface->frame_cb)
1511 wl_callback_destroy(surface->frame_cb);
1513 if (surface->input_region)
1514 wl_region_destroy(surface->input_region);
1516 if (surface->opaque_region)
1517 wl_region_destroy(surface->opaque_region);
1519 if (surface->subsurface)
1520 wl_subsurface_destroy(surface->subsurface);
1522 wl_surface_destroy(surface->surface);
1524 if (surface->toysurface)
1525 surface->toysurface->destroy(surface->toysurface);
1527 wl_list_remove(&surface->link);
1532 window_destroy(struct window *window)
1534 struct display *display = window->display;
1535 struct input *input;
1536 struct window_output *window_output;
1537 struct window_output *window_output_tmp;
1539 wl_list_remove(&window->redraw_task.link);
1541 wl_list_for_each(input, &display->input_list, link) {
1542 if (input->pointer_focus == window)
1543 input->pointer_focus = NULL;
1544 if (input->keyboard_focus == window)
1545 input->keyboard_focus = NULL;
1546 if (input->focus_widget &&
1547 input->focus_widget->window == window)
1548 input->focus_widget = NULL;
1551 wl_list_for_each_safe(window_output, window_output_tmp,
1552 &window->window_output_list, link) {
1553 free (window_output);
1557 frame_destroy(window->frame);
1559 if (window->shell_surface)
1560 wl_shell_surface_destroy(window->shell_surface);
1562 surface_destroy(window->main_surface);
1564 wl_list_remove(&window->link);
1566 free(window->title);
1570 static struct widget *
1571 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1573 struct widget *child, *target;
1575 wl_list_for_each(child, &widget->child_list, link) {
1576 target = widget_find_widget(child, x, y);
1581 if (widget->allocation.x <= x &&
1582 x < widget->allocation.x + widget->allocation.width &&
1583 widget->allocation.y <= y &&
1584 y < widget->allocation.y + widget->allocation.height) {
1591 static struct widget *
1592 window_find_widget(struct window *window, int32_t x, int32_t y)
1594 struct surface *surface;
1595 struct widget *widget;
1597 wl_list_for_each(surface, &window->subsurface_list, link) {
1598 widget = widget_find_widget(surface->widget, x, y);
1606 static struct widget *
1607 widget_create(struct window *window, struct surface *surface, void *data)
1609 struct widget *widget;
1611 widget = xzalloc(sizeof *widget);
1612 widget->window = window;
1613 widget->surface = surface;
1614 widget->user_data = data;
1615 widget->allocation = surface->allocation;
1616 wl_list_init(&widget->child_list);
1618 widget->tooltip = NULL;
1619 widget->tooltip_count = 0;
1620 widget->default_cursor = CURSOR_LEFT_PTR;
1626 window_add_widget(struct window *window, void *data)
1628 struct widget *widget;
1630 widget = widget_create(window, window->main_surface, data);
1631 wl_list_init(&widget->link);
1632 window->main_surface->widget = widget;
1638 widget_add_widget(struct widget *parent, void *data)
1640 struct widget *widget;
1642 widget = widget_create(parent->window, parent->surface, data);
1643 wl_list_insert(parent->child_list.prev, &widget->link);
1649 widget_destroy(struct widget *widget)
1651 struct display *display = widget->window->display;
1652 struct surface *surface = widget->surface;
1653 struct input *input;
1655 /* Destroy the sub-surface along with the root widget */
1656 if (surface->widget == widget && surface->subsurface)
1657 surface_destroy(widget->surface);
1659 if (widget->tooltip) {
1660 free(widget->tooltip);
1661 widget->tooltip = NULL;
1664 wl_list_for_each(input, &display->input_list, link) {
1665 if (input->focus_widget == widget)
1666 input->focus_widget = NULL;
1669 wl_list_remove(&widget->link);
1674 widget_set_default_cursor(struct widget *widget, int cursor)
1676 widget->default_cursor = cursor;
1680 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1682 *allocation = widget->allocation;
1686 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1688 widget->allocation.width = width;
1689 widget->allocation.height = height;
1693 widget_set_allocation(struct widget *widget,
1694 int32_t x, int32_t y, int32_t width, int32_t height)
1696 widget->allocation.x = x;
1697 widget->allocation.y = y;
1698 widget_set_size(widget, width, height);
1702 widget_set_transparent(struct widget *widget, int transparent)
1704 widget->opaque = !transparent;
1708 widget_get_user_data(struct widget *widget)
1710 return widget->user_data;
1713 static cairo_surface_t *
1714 widget_get_cairo_surface(struct widget *widget)
1716 struct surface *surface = widget->surface;
1717 struct window *window = widget->window;
1719 if (!surface->cairo_surface) {
1720 if (surface == window->main_surface)
1721 window_create_main_surface(window);
1723 surface_create_surface(surface, 0, 0, 0);
1726 return surface->cairo_surface;
1730 widget_cairo_update_transform(struct widget *widget, cairo_t *cr)
1732 struct surface *surface = widget->surface;
1735 enum wl_output_transform transform;
1736 int surface_width, surface_height;
1737 int translate_x, translate_y;
1740 surface_width = surface->allocation.width;
1741 surface_height = surface->allocation.height;
1743 transform = surface->buffer_transform;
1744 scale = surface->buffer_scale;
1746 switch (transform) {
1747 case WL_OUTPUT_TRANSFORM_FLIPPED:
1748 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1749 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1750 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1751 cairo_matrix_init(&m, -1, 0, 0, 1, 0, 0);
1754 cairo_matrix_init_identity(&m);
1758 switch (transform) {
1759 case WL_OUTPUT_TRANSFORM_NORMAL:
1765 case WL_OUTPUT_TRANSFORM_FLIPPED:
1767 translate_x = surface_width;
1770 case WL_OUTPUT_TRANSFORM_90:
1772 translate_x = surface_height;
1775 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1777 translate_x = surface_height;
1778 translate_y = surface_width;
1780 case WL_OUTPUT_TRANSFORM_180:
1782 translate_x = surface_width;
1783 translate_y = surface_height;
1785 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1788 translate_y = surface_height;
1790 case WL_OUTPUT_TRANSFORM_270:
1791 angle = M_PI + M_PI_2;
1793 translate_y = surface_width;
1795 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1796 angle = M_PI + M_PI_2;
1802 cairo_scale(cr, scale, scale);
1803 cairo_translate(cr, translate_x, translate_y);
1804 cairo_rotate(cr, angle);
1805 cairo_transform(cr, &m);
1809 widget_cairo_create(struct widget *widget)
1811 struct surface *surface = widget->surface;
1812 cairo_surface_t *cairo_surface;
1815 cairo_surface = widget_get_cairo_surface(widget);
1816 cr = cairo_create(cairo_surface);
1818 widget_cairo_update_transform(widget, cr);
1820 cairo_translate(cr, -surface->allocation.x, -surface->allocation.y);
1826 widget_get_wl_surface(struct widget *widget)
1828 return widget->surface->surface;
1832 widget_get_last_time(struct widget *widget)
1834 return widget->surface->last_time;
1838 widget_input_region_add(struct widget *widget, const struct rectangle *rect)
1840 struct wl_compositor *comp = widget->window->display->compositor;
1841 struct surface *surface = widget->surface;
1843 if (!surface->input_region)
1844 surface->input_region = wl_compositor_create_region(comp);
1847 wl_region_add(surface->input_region,
1848 rect->x, rect->y, rect->width, rect->height);
1853 widget_set_resize_handler(struct widget *widget,
1854 widget_resize_handler_t handler)
1856 widget->resize_handler = handler;
1860 widget_set_redraw_handler(struct widget *widget,
1861 widget_redraw_handler_t handler)
1863 widget->redraw_handler = handler;
1867 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1869 widget->enter_handler = handler;
1873 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1875 widget->leave_handler = handler;
1879 widget_set_motion_handler(struct widget *widget,
1880 widget_motion_handler_t handler)
1882 widget->motion_handler = handler;
1886 widget_set_button_handler(struct widget *widget,
1887 widget_button_handler_t handler)
1889 widget->button_handler = handler;
1893 widget_set_axis_handler(struct widget *widget,
1894 widget_axis_handler_t handler)
1896 widget->axis_handler = handler;
1900 window_schedule_redraw_task(struct window *window);
1903 widget_schedule_redraw(struct widget *widget)
1905 DBG_OBJ(widget->surface->surface, "widget %p\n", widget);
1906 widget->surface->redraw_needed = 1;
1907 window_schedule_redraw_task(widget->window);
1911 window_get_surface(struct window *window)
1913 cairo_surface_t *cairo_surface;
1915 cairo_surface = widget_get_cairo_surface(window->main_surface->widget);
1917 return cairo_surface_reference(cairo_surface);
1921 window_get_wl_surface(struct window *window)
1923 return window->main_surface->surface;
1926 struct wl_shell_surface *
1927 window_get_wl_shell_surface(struct window *window)
1929 return window->shell_surface;
1933 tooltip_redraw_handler(struct widget *widget, void *data)
1936 const int32_t r = 3;
1937 struct tooltip *tooltip = data;
1938 int32_t width, height;
1940 cr = widget_cairo_create(widget);
1941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1942 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1945 width = widget->allocation.width;
1946 height = widget->allocation.height;
1947 rounded_rect(cr, 0, 0, width, height, r);
1949 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1950 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1953 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1954 cairo_move_to(cr, 10, 16);
1955 cairo_show_text(cr, tooltip->entry);
1959 static cairo_text_extents_t
1960 get_text_extents(struct tooltip *tooltip)
1963 cairo_text_extents_t extents;
1965 /* Use the dummy_surface because tooltip's surface was not
1966 * created yet, and parent does not have a valid surface
1967 * outside repaint, either.
1969 cr = cairo_create(tooltip->window->display->dummy_surface);
1970 cairo_text_extents(cr, tooltip->entry, &extents);
1977 window_create_tooltip(struct tooltip *tooltip)
1979 struct widget *parent = tooltip->parent;
1980 struct display *display = parent->window->display;
1981 struct window *window;
1982 const int offset_y = 27;
1983 const int margin = 3;
1984 cairo_text_extents_t extents;
1986 if (tooltip->widget)
1989 window = window_create_transient(display, parent->window, tooltip->x,
1990 tooltip->y + offset_y,
1991 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1995 tooltip->window = window;
1996 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1998 extents = get_text_extents(tooltip);
1999 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
2000 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
2006 widget_destroy_tooltip(struct widget *parent)
2008 struct tooltip *tooltip = parent->tooltip;
2010 parent->tooltip_count = 0;
2014 if (tooltip->widget) {
2015 widget_destroy(tooltip->widget);
2016 window_destroy(tooltip->window);
2017 tooltip->widget = NULL;
2018 tooltip->window = NULL;
2021 close(tooltip->tooltip_fd);
2022 free(tooltip->entry);
2024 parent->tooltip = NULL;
2028 tooltip_func(struct task *task, uint32_t events)
2030 struct tooltip *tooltip =
2031 container_of(task, struct tooltip, tooltip_task);
2034 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
2036 window_create_tooltip(tooltip);
2039 #define TOOLTIP_TIMEOUT 500
2041 tooltip_timer_reset(struct tooltip *tooltip)
2043 struct itimerspec its;
2045 its.it_interval.tv_sec = 0;
2046 its.it_interval.tv_nsec = 0;
2047 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
2048 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
2049 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
2050 fprintf(stderr, "could not set timerfd\n: %m");
2058 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
2060 struct tooltip *tooltip = parent->tooltip;
2062 parent->tooltip_count++;
2066 tooltip_timer_reset(tooltip);
2070 /* the handler might be triggered too fast via input device motion, so
2071 * we need this check here to make sure tooltip is fully initialized */
2072 if (parent->tooltip_count > 1)
2075 tooltip = malloc(sizeof *tooltip);
2079 parent->tooltip = tooltip;
2080 tooltip->parent = parent;
2081 tooltip->widget = NULL;
2082 tooltip->window = NULL;
2085 tooltip->entry = strdup(entry);
2086 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2087 if (tooltip->tooltip_fd < 0) {
2088 fprintf(stderr, "could not create timerfd\n: %m");
2092 tooltip->tooltip_task.run = tooltip_func;
2093 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
2094 EPOLLIN, &tooltip->tooltip_task);
2095 tooltip_timer_reset(tooltip);
2101 workspace_manager_state(void *data,
2102 struct workspace_manager *workspace_manager,
2106 struct display *display = data;
2108 display->workspace = current;
2109 display->workspace_count = count;
2112 static const struct workspace_manager_listener workspace_manager_listener = {
2113 workspace_manager_state
2117 frame_resize_handler(struct widget *widget,
2118 int32_t width, int32_t height, void *data)
2120 struct frame *frame = data;
2121 struct widget *child = frame->child;
2122 struct rectangle allocation;
2123 struct display *display = widget->window->display;
2124 struct surface *surface = widget->surface;
2125 struct frame_button * button;
2126 struct theme *t = display->theme;
2127 int x_l, x_r, y, w, h;
2128 int decoration_width, decoration_height;
2129 int opaque_margin, shadow_margin;
2131 switch (widget->window->type) {
2132 case TYPE_FULLSCREEN:
2133 decoration_width = 0;
2134 decoration_height = 0;
2138 allocation.width = width;
2139 allocation.height = height;
2142 wl_list_for_each(button, &frame->buttons_list, link)
2143 button->widget->opaque = 1;
2145 case TYPE_MAXIMIZED:
2146 decoration_width = t->width * 2;
2147 decoration_height = t->width + t->titlebar_height;
2149 allocation.x = t->width;
2150 allocation.y = t->titlebar_height;
2151 allocation.width = width - decoration_width;
2152 allocation.height = height - decoration_height;
2156 wl_list_for_each(button, &frame->buttons_list, link)
2157 button->widget->opaque = 0;
2160 decoration_width = (t->width + t->margin) * 2;
2161 decoration_height = t->width +
2162 t->titlebar_height + t->margin * 2;
2164 allocation.x = t->width + t->margin;
2165 allocation.y = t->titlebar_height + t->margin;
2166 allocation.width = width - decoration_width;
2167 allocation.height = height - decoration_height;
2169 opaque_margin = t->margin + t->frame_radius;
2171 wl_list_for_each(button, &frame->buttons_list, link)
2172 button->widget->opaque = 0;
2176 widget_set_allocation(child, allocation.x, allocation.y,
2177 allocation.width, allocation.height);
2179 if (child->resize_handler)
2180 child->resize_handler(child,
2185 width = child->allocation.width + decoration_width;
2186 height = child->allocation.height + decoration_height;
2188 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2190 surface->input_region =
2191 wl_compositor_create_region(display->compositor);
2192 if (widget->window->type != TYPE_FULLSCREEN) {
2193 wl_region_add(surface->input_region,
2194 shadow_margin, shadow_margin,
2195 width - 2 * shadow_margin,
2196 height - 2 * shadow_margin);
2198 wl_region_add(surface->input_region, 0, 0, width, height);
2201 widget_set_allocation(widget, 0, 0, width, height);
2204 wl_region_add(surface->opaque_region,
2205 opaque_margin, opaque_margin,
2206 widget->allocation.width - 2 * opaque_margin,
2207 widget->allocation.height - 2 * opaque_margin);
2209 /* frame internal buttons */
2210 x_r = frame->widget->allocation.width - t->width - shadow_margin;
2211 x_l = t->width + shadow_margin;
2212 y = t->width + shadow_margin;
2213 wl_list_for_each(button, &frame->buttons_list, link) {
2214 const int button_padding = 4;
2215 w = cairo_image_surface_get_width(button->icon);
2216 h = cairo_image_surface_get_height(button->icon);
2218 if (button->decoration == FRAME_BUTTON_FANCY)
2221 if (button->align == FRAME_BUTTON_LEFT) {
2222 widget_set_allocation(button->widget,
2223 x_l, y , w + 1, h + 1);
2225 x_l += button_padding;
2228 widget_set_allocation(button->widget,
2229 x_r, y , w + 1, h + 1);
2230 x_r -= button_padding;
2236 frame_button_enter_handler(struct widget *widget,
2237 struct input *input, float x, float y, void *data)
2239 struct frame_button *frame_button = data;
2241 widget_schedule_redraw(frame_button->widget);
2242 frame_button->state = FRAME_BUTTON_OVER;
2244 return CURSOR_LEFT_PTR;
2248 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
2250 struct frame_button *frame_button = data;
2252 widget_schedule_redraw(frame_button->widget);
2253 frame_button->state = FRAME_BUTTON_DEFAULT;
2257 frame_button_button_handler(struct widget *widget,
2258 struct input *input, uint32_t time,
2260 enum wl_pointer_button_state state, void *data)
2262 struct frame_button *frame_button = data;
2263 struct window *window = widget->window;
2264 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
2266 if (button != BTN_LEFT)
2270 case WL_POINTER_BUTTON_STATE_PRESSED:
2271 frame_button->state = FRAME_BUTTON_ACTIVE;
2272 widget_schedule_redraw(frame_button->widget);
2274 if (frame_button->type == FRAME_BUTTON_ICON)
2275 window_show_frame_menu(window, input, time);
2277 case WL_POINTER_BUTTON_STATE_RELEASED:
2278 frame_button->state = FRAME_BUTTON_DEFAULT;
2279 widget_schedule_redraw(frame_button->widget);
2286 switch (frame_button->type) {
2287 case FRAME_BUTTON_CLOSE:
2288 if (window->close_handler)
2289 window->close_handler(window->parent,
2292 display_exit(window->display);
2294 case FRAME_BUTTON_MINIMIZE:
2295 fprintf(stderr,"Minimize stub\n");
2297 case FRAME_BUTTON_MAXIMIZE:
2298 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
2301 /* Unknown operation */
2307 frame_button_motion_handler(struct widget *widget,
2308 struct input *input, uint32_t time,
2309 float x, float y, void *data)
2311 struct frame_button *frame_button = data;
2312 enum frame_button_pointer previous_button_state = frame_button->state;
2314 /* only track state for a pressed button */
2315 if (input->grab != widget)
2316 return CURSOR_LEFT_PTR;
2318 if (x > widget->allocation.x &&
2319 x < (widget->allocation.x + widget->allocation.width) &&
2320 y > widget->allocation.y &&
2321 y < (widget->allocation.y + widget->allocation.height)) {
2322 frame_button->state = FRAME_BUTTON_ACTIVE;
2324 frame_button->state = FRAME_BUTTON_DEFAULT;
2327 if (frame_button->state != previous_button_state)
2328 widget_schedule_redraw(frame_button->widget);
2330 return CURSOR_LEFT_PTR;
2334 frame_button_redraw_handler(struct widget *widget, void *data)
2336 struct frame_button *frame_button = data;
2338 int width, height, x, y;
2340 x = widget->allocation.x;
2341 y = widget->allocation.y;
2342 width = widget->allocation.width;
2343 height = widget->allocation.height;
2352 cr = widget_cairo_create(widget);
2354 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
2355 cairo_set_line_width(cr, 1);
2357 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2358 cairo_rectangle (cr, x, y, 25, 16);
2360 cairo_stroke_preserve(cr);
2362 switch (frame_button->state) {
2363 case FRAME_BUTTON_DEFAULT:
2364 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
2366 case FRAME_BUTTON_OVER:
2367 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2369 case FRAME_BUTTON_ACTIVE:
2370 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
2379 cairo_set_source_surface(cr, frame_button->icon, x, y);
2385 static struct widget *
2386 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
2387 enum frame_button_align align, enum frame_button_decoration style)
2389 struct frame_button *frame_button;
2390 const char *icon = data;
2392 frame_button = xzalloc (sizeof *frame_button);
2393 frame_button->icon = cairo_image_surface_create_from_png(icon);
2394 frame_button->widget = widget_add_widget(frame->widget, frame_button);
2395 frame_button->frame = frame;
2396 frame_button->type = type;
2397 frame_button->align = align;
2398 frame_button->decoration = style;
2400 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
2402 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
2403 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
2404 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
2405 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
2406 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
2407 return frame_button->widget;
2411 frame_button_destroy(struct frame_button *frame_button)
2413 widget_destroy(frame_button->widget);
2414 wl_list_remove(&frame_button->link);
2415 cairo_surface_destroy(frame_button->icon);
2422 frame_redraw_handler(struct widget *widget, void *data)
2425 struct window *window = widget->window;
2426 struct theme *t = window->display->theme;
2429 if (window->type == TYPE_FULLSCREEN)
2432 cr = widget_cairo_create(widget);
2434 if (window->focus_count)
2435 flags |= THEME_FRAME_ACTIVE;
2436 if (window->type == TYPE_MAXIMIZED)
2437 flags |= THEME_FRAME_MAXIMIZED;
2438 theme_render_frame(t, cr, widget->allocation.width,
2439 widget->allocation.height, window->title, flags);
2445 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
2447 struct theme *t = frame->widget->window->display->theme;
2448 struct window *window = frame->widget->window;
2451 if (window->type != TYPE_TOPLEVEL)
2452 return CURSOR_LEFT_PTR;
2454 location = theme_get_location(t, input->sx, input->sy,
2455 frame->widget->allocation.width,
2456 frame->widget->allocation.height,
2457 window->type == TYPE_MAXIMIZED ?
2458 THEME_FRAME_MAXIMIZED : 0);
2461 case THEME_LOCATION_RESIZING_TOP:
2463 case THEME_LOCATION_RESIZING_BOTTOM:
2464 return CURSOR_BOTTOM;
2465 case THEME_LOCATION_RESIZING_LEFT:
2467 case THEME_LOCATION_RESIZING_RIGHT:
2468 return CURSOR_RIGHT;
2469 case THEME_LOCATION_RESIZING_TOP_LEFT:
2470 return CURSOR_TOP_LEFT;
2471 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2472 return CURSOR_TOP_RIGHT;
2473 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2474 return CURSOR_BOTTOM_LEFT;
2475 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2476 return CURSOR_BOTTOM_RIGHT;
2477 case THEME_LOCATION_EXTERIOR:
2478 case THEME_LOCATION_TITLEBAR:
2480 return CURSOR_LEFT_PTR;
2485 frame_menu_func(struct window *window, int index, void *data)
2487 struct display *display;
2491 if (window->close_handler)
2492 window->close_handler(window->parent,
2495 display_exit(window->display);
2497 case 1: /* move to workspace above */
2498 display = window->display;
2499 if (display->workspace > 0)
2500 workspace_manager_move_surface(
2501 display->workspace_manager,
2502 window->main_surface->surface,
2503 display->workspace - 1);
2505 case 2: /* move to workspace below */
2506 display = window->display;
2507 if (display->workspace < display->workspace_count - 1)
2508 workspace_manager_move_surface(
2509 display->workspace_manager,
2510 window->main_surface->surface,
2511 display->workspace + 1);
2513 case 3: /* fullscreen */
2514 /* we don't have a way to get out of fullscreen for now */
2515 if (window->fullscreen_handler)
2516 window->fullscreen_handler(window, window->user_data);
2522 window_show_frame_menu(struct window *window,
2523 struct input *input, uint32_t time)
2528 static const char *entries[] = {
2530 "Move to workspace above", "Move to workspace below",
2534 if (window->fullscreen_handler)
2535 count = ARRAY_LENGTH(entries);
2537 count = ARRAY_LENGTH(entries) - 1;
2539 input_get_position(input, &x, &y);
2540 window_show_menu(window->display, input, time, window,
2541 x - 10, y - 10, frame_menu_func, entries, count);
2545 frame_enter_handler(struct widget *widget,
2546 struct input *input, float x, float y, void *data)
2548 return frame_get_pointer_image_for_location(data, input);
2552 frame_motion_handler(struct widget *widget,
2553 struct input *input, uint32_t time,
2554 float x, float y, void *data)
2556 return frame_get_pointer_image_for_location(data, input);
2560 frame_button_handler(struct widget *widget,
2561 struct input *input, uint32_t time,
2562 uint32_t button, enum wl_pointer_button_state state,
2566 struct frame *frame = data;
2567 struct window *window = widget->window;
2568 struct display *display = window->display;
2571 if (state != WL_POINTER_BUTTON_STATE_PRESSED)
2574 location = theme_get_location(display->theme, input->sx, input->sy,
2575 frame->widget->allocation.width,
2576 frame->widget->allocation.height,
2577 window->type == TYPE_MAXIMIZED ?
2578 THEME_FRAME_MAXIMIZED : 0);
2580 if (window->display->shell && button == BTN_LEFT &&
2581 window->type == TYPE_TOPLEVEL) {
2583 case THEME_LOCATION_TITLEBAR:
2584 if (!window->shell_surface)
2586 input_ungrab(input);
2587 wl_shell_surface_move(window->shell_surface,
2588 input_get_seat(input),
2591 case THEME_LOCATION_RESIZING_TOP:
2592 case THEME_LOCATION_RESIZING_BOTTOM:
2593 case THEME_LOCATION_RESIZING_LEFT:
2594 case THEME_LOCATION_RESIZING_RIGHT:
2595 case THEME_LOCATION_RESIZING_TOP_LEFT:
2596 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2597 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2598 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2599 if (!window->shell_surface)
2601 input_ungrab(input);
2603 window->resizing = 1;
2604 wl_shell_surface_resize(window->shell_surface,
2605 input_get_seat(input),
2606 display->serial, location);
2609 } else if (button == BTN_RIGHT &&
2610 (window->type == TYPE_TOPLEVEL ||
2611 window->type == TYPE_MAXIMIZED)) {
2612 window_show_frame_menu(window, input, time);
2617 frame_create(struct window *window, void *data)
2619 struct frame *frame;
2621 frame = xzalloc(sizeof *frame);
2622 frame->widget = window_add_widget(window, frame);
2623 frame->child = widget_add_widget(frame->widget, data);
2625 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2626 widget_set_resize_handler(frame->widget, frame_resize_handler);
2627 widget_set_enter_handler(frame->widget, frame_enter_handler);
2628 widget_set_motion_handler(frame->widget, frame_motion_handler);
2629 widget_set_button_handler(frame->widget, frame_button_handler);
2631 /* Create empty list for frame buttons */
2632 wl_list_init(&frame->buttons_list);
2634 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2635 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2637 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2638 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2640 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2641 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2643 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2644 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2646 window->frame = frame;
2648 return frame->child;
2652 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2654 struct display *display = widget->window->display;
2655 struct theme *t = display->theme;
2656 int decoration_width, decoration_height;
2658 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2660 if (widget->window->type != TYPE_FULLSCREEN) {
2661 decoration_width = (t->width + margin) * 2;
2662 decoration_height = t->width +
2663 t->titlebar_height + margin * 2;
2665 width = child_width + decoration_width;
2666 height = child_height + decoration_height;
2668 width = child_width;
2669 height = child_height;
2672 window_schedule_resize(widget->window, width, height);
2676 frame_destroy(struct frame *frame)
2678 struct frame_button *button, *tmp;
2680 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2681 frame_button_destroy(button);
2683 /* frame->child must be destroyed by the application */
2684 widget_destroy(frame->widget);
2689 input_set_focus_widget(struct input *input, struct widget *focus,
2692 struct widget *old, *widget;
2695 if (focus == input->focus_widget)
2698 old = input->focus_widget;
2702 widget = input->grab;
2703 if (widget->leave_handler)
2704 widget->leave_handler(old, input, widget->user_data);
2705 input->focus_widget = NULL;
2711 widget = input->grab;
2712 input->focus_widget = focus;
2713 if (widget->enter_handler)
2714 cursor = widget->enter_handler(focus, input, x, y,
2717 cursor = widget->default_cursor;
2719 input_set_pointer_image(input, cursor);
2724 input_grab(struct input *input, struct widget *widget, uint32_t button)
2726 input->grab = widget;
2727 input->grab_button = button;
2731 input_ungrab(struct input *input)
2733 struct widget *widget;
2736 if (input->pointer_focus) {
2737 widget = window_find_widget(input->pointer_focus,
2738 input->sx, input->sy);
2739 input_set_focus_widget(input, widget, input->sx, input->sy);
2744 input_remove_pointer_focus(struct input *input)
2746 struct window *window = input->pointer_focus;
2751 input_set_focus_widget(input, NULL, 0, 0);
2753 input->pointer_focus = NULL;
2754 input->current_cursor = CURSOR_UNSET;
2758 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2759 uint32_t serial, struct wl_surface *surface,
2760 wl_fixed_t sx_w, wl_fixed_t sy_w)
2762 struct input *input = data;
2763 struct window *window;
2764 struct widget *widget;
2765 float sx = wl_fixed_to_double(sx_w);
2766 float sy = wl_fixed_to_double(sy_w);
2769 /* enter event for a window we've just destroyed */
2773 input->display->serial = serial;
2774 input->pointer_enter_serial = serial;
2775 input->pointer_focus = wl_surface_get_user_data(surface);
2776 window = input->pointer_focus;
2778 if (window->resizing) {
2779 window->resizing = 0;
2780 /* Schedule a redraw to free the pool */
2781 window_schedule_redraw(window);
2787 widget = window_find_widget(window, sx, sy);
2788 input_set_focus_widget(input, widget, sx, sy);
2792 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2793 uint32_t serial, struct wl_surface *surface)
2795 struct input *input = data;
2797 input->display->serial = serial;
2798 input_remove_pointer_focus(input);
2802 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2803 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2805 struct input *input = data;
2806 struct window *window = input->pointer_focus;
2807 struct widget *widget;
2809 float sx = wl_fixed_to_double(sx_w);
2810 float sy = wl_fixed_to_double(sy_w);
2812 /* when making the window smaller - e.g. after a unmaximise we might
2813 * still have a pending motion event that the compositor has picked
2814 * based on the old surface dimensions
2816 if (sx > window->main_surface->allocation.width ||
2817 sy > window->main_surface->allocation.height)
2826 if (!(input->grab && input->grab_button)) {
2827 widget = window_find_widget(window, sx, sy);
2828 input_set_focus_widget(input, widget, sx, sy);
2832 widget = input->grab;
2834 widget = input->focus_widget;
2835 if (widget && widget->motion_handler)
2836 cursor = widget->motion_handler(input->focus_widget,
2837 input, time, sx, sy,
2840 cursor = input->focus_widget->default_cursor;
2842 input_set_pointer_image(input, cursor);
2846 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2847 uint32_t time, uint32_t button, uint32_t state_w)
2849 struct input *input = data;
2850 struct widget *widget;
2851 enum wl_pointer_button_state state = state_w;
2853 input->display->serial = serial;
2854 if (input->focus_widget && input->grab == NULL &&
2855 state == WL_POINTER_BUTTON_STATE_PRESSED)
2856 input_grab(input, input->focus_widget, button);
2858 widget = input->grab;
2859 if (widget && widget->button_handler)
2860 (*widget->button_handler)(widget,
2863 input->grab->user_data);
2865 if (input->grab && input->grab_button == button &&
2866 state == WL_POINTER_BUTTON_STATE_RELEASED)
2867 input_ungrab(input);
2871 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2872 uint32_t time, uint32_t axis, wl_fixed_t value)
2874 struct input *input = data;
2875 struct widget *widget;
2877 widget = input->focus_widget;
2879 widget = input->grab;
2880 if (widget && widget->axis_handler)
2881 (*widget->axis_handler)(widget,
2887 static const struct wl_pointer_listener pointer_listener = {
2888 pointer_handle_enter,
2889 pointer_handle_leave,
2890 pointer_handle_motion,
2891 pointer_handle_button,
2892 pointer_handle_axis,
2896 input_remove_keyboard_focus(struct input *input)
2898 struct window *window = input->keyboard_focus;
2899 struct itimerspec its;
2901 its.it_interval.tv_sec = 0;
2902 its.it_interval.tv_nsec = 0;
2903 its.it_value.tv_sec = 0;
2904 its.it_value.tv_nsec = 0;
2905 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2910 window->focus_count--;
2911 if (window->keyboard_focus_handler)
2912 (*window->keyboard_focus_handler)(window, NULL,
2915 input->keyboard_focus = NULL;
2919 keyboard_repeat_func(struct task *task, uint32_t events)
2921 struct input *input =
2922 container_of(task, struct input, repeat_task);
2923 struct window *window = input->keyboard_focus;
2926 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2927 /* If we change the timer between the fd becoming
2928 * readable and getting here, there'll be nothing to
2929 * read and we get EAGAIN. */
2932 if (window && window->key_handler) {
2933 (*window->key_handler)(window, input, input->repeat_time,
2934 input->repeat_key, input->repeat_sym,
2935 WL_KEYBOARD_KEY_STATE_PRESSED,
2941 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2942 uint32_t format, int fd, uint32_t size)
2944 struct input *input = data;
2952 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2957 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2958 if (map_str == MAP_FAILED) {
2963 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2965 XKB_KEYMAP_FORMAT_TEXT_V1,
2967 munmap(map_str, size);
2970 if (!input->xkb.keymap) {
2971 fprintf(stderr, "failed to compile keymap\n");
2975 input->xkb.state = xkb_state_new(input->xkb.keymap);
2976 if (!input->xkb.state) {
2977 fprintf(stderr, "failed to create XKB state\n");
2978 xkb_map_unref(input->xkb.keymap);
2979 input->xkb.keymap = NULL;
2983 input->xkb.control_mask =
2984 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2985 input->xkb.alt_mask =
2986 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2987 input->xkb.shift_mask =
2988 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2992 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2993 uint32_t serial, struct wl_surface *surface,
2994 struct wl_array *keys)
2996 struct input *input = data;
2997 struct window *window;
2999 input->display->serial = serial;
3000 input->keyboard_focus = wl_surface_get_user_data(surface);
3002 window = input->keyboard_focus;
3003 window->focus_count++;
3004 if (window->keyboard_focus_handler)
3005 (*window->keyboard_focus_handler)(window,
3006 input, window->user_data);
3010 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
3011 uint32_t serial, struct wl_surface *surface)
3013 struct input *input = data;
3015 input->display->serial = serial;
3016 input_remove_keyboard_focus(input);
3020 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
3021 uint32_t serial, uint32_t time, uint32_t key,
3024 struct input *input = data;
3025 struct window *window = input->keyboard_focus;
3026 uint32_t code, num_syms;
3027 enum wl_keyboard_key_state state = state_w;
3028 const xkb_keysym_t *syms;
3030 struct itimerspec its;
3032 input->display->serial = serial;
3034 if (!window || !input->xkb.state)
3037 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
3039 sym = XKB_KEY_NoSymbol;
3043 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
3044 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
3045 window_set_maximized(window,
3046 window->type != TYPE_MAXIMIZED);
3047 } else if (sym == XKB_KEY_F11 &&
3048 window->fullscreen_handler &&
3049 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3050 window->fullscreen_handler(window, window->user_data);
3051 } else if (sym == XKB_KEY_F4 &&
3052 input->modifiers == MOD_ALT_MASK &&
3053 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3054 if (window->close_handler)
3055 window->close_handler(window->parent,
3058 display_exit(window->display);
3059 } else if (window->key_handler) {
3060 (*window->key_handler)(window, input, time, key,
3061 sym, state, window->user_data);
3064 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
3065 key == input->repeat_key) {
3066 its.it_interval.tv_sec = 0;
3067 its.it_interval.tv_nsec = 0;
3068 its.it_value.tv_sec = 0;
3069 its.it_value.tv_nsec = 0;
3070 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3071 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
3072 input->repeat_sym = sym;
3073 input->repeat_key = key;
3074 input->repeat_time = time;
3075 its.it_interval.tv_sec = 0;
3076 its.it_interval.tv_nsec = 25 * 1000 * 1000;
3077 its.it_value.tv_sec = 0;
3078 its.it_value.tv_nsec = 400 * 1000 * 1000;
3079 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
3084 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
3085 uint32_t serial, uint32_t mods_depressed,
3086 uint32_t mods_latched, uint32_t mods_locked,
3089 struct input *input = data;
3090 xkb_mod_mask_t mask;
3092 /* If we're not using a keymap, then we don't handle PC-style modifiers */
3093 if (!input->xkb.keymap)
3096 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
3097 mods_locked, 0, 0, group);
3098 mask = xkb_state_serialize_mods(input->xkb.state,
3099 XKB_STATE_DEPRESSED |
3101 input->modifiers = 0;
3102 if (mask & input->xkb.control_mask)
3103 input->modifiers |= MOD_CONTROL_MASK;
3104 if (mask & input->xkb.alt_mask)
3105 input->modifiers |= MOD_ALT_MASK;
3106 if (mask & input->xkb.shift_mask)
3107 input->modifiers |= MOD_SHIFT_MASK;
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,
3119 seat_handle_capabilities(void *data, struct wl_seat *seat,
3120 enum wl_seat_capability caps)
3122 struct input *input = data;
3124 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
3125 input->pointer = wl_seat_get_pointer(seat);
3126 wl_pointer_set_user_data(input->pointer, input);
3127 wl_pointer_add_listener(input->pointer, &pointer_listener,
3129 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
3130 wl_pointer_destroy(input->pointer);
3131 input->pointer = NULL;
3134 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
3135 input->keyboard = wl_seat_get_keyboard(seat);
3136 wl_keyboard_set_user_data(input->keyboard, input);
3137 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
3139 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
3140 wl_keyboard_destroy(input->keyboard);
3141 input->keyboard = NULL;
3145 static const struct wl_seat_listener seat_listener = {
3146 seat_handle_capabilities,
3150 input_get_position(struct input *input, int32_t *x, int32_t *y)
3157 input_get_display(struct input *input)
3159 return input->display;
3163 input_get_seat(struct input *input)
3169 input_get_modifiers(struct input *input)
3171 return input->modifiers;
3175 input_get_focus_widget(struct input *input)
3177 return input->focus_widget;
3181 struct wl_data_offer *offer;
3182 struct input *input;
3183 struct wl_array types;
3186 struct task io_task;
3194 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
3196 struct data_offer *offer = data;
3199 p = wl_array_add(&offer->types, sizeof *p);
3203 static const struct wl_data_offer_listener data_offer_listener = {
3208 data_offer_destroy(struct data_offer *offer)
3213 if (offer->refcount == 0) {
3214 wl_data_offer_destroy(offer->offer);
3215 for (p = offer->types.data; *p; p++)
3217 wl_array_release(&offer->types);
3223 data_device_data_offer(void *data,
3224 struct wl_data_device *data_device,
3225 struct wl_data_offer *_offer)
3227 struct data_offer *offer;
3229 offer = xmalloc(sizeof *offer);
3231 wl_array_init(&offer->types);
3232 offer->refcount = 1;
3233 offer->input = data;
3234 offer->offer = _offer;
3235 wl_data_offer_add_listener(offer->offer,
3236 &data_offer_listener, offer);
3240 data_device_enter(void *data, struct wl_data_device *data_device,
3241 uint32_t serial, struct wl_surface *surface,
3242 wl_fixed_t x_w, wl_fixed_t y_w,
3243 struct wl_data_offer *offer)
3245 struct input *input = data;
3246 struct window *window;
3248 float x = wl_fixed_to_double(x_w);
3249 float y = wl_fixed_to_double(y_w);
3252 input->pointer_enter_serial = serial;
3253 window = wl_surface_get_user_data(surface);
3254 input->pointer_focus = window;
3257 input->drag_offer = wl_data_offer_get_user_data(offer);
3259 p = wl_array_add(&input->drag_offer->types, sizeof *p);
3262 types_data = input->drag_offer->types.data;
3264 input->drag_offer = NULL;
3268 window = input->pointer_focus;
3269 if (window->data_handler)
3270 window->data_handler(window, input, x, y, types_data,
3275 data_device_leave(void *data, struct wl_data_device *data_device)
3277 struct input *input = data;
3279 if (input->drag_offer) {
3280 data_offer_destroy(input->drag_offer);
3281 input->drag_offer = NULL;
3286 data_device_motion(void *data, struct wl_data_device *data_device,
3287 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
3289 struct input *input = data;
3290 struct window *window = input->pointer_focus;
3291 float x = wl_fixed_to_double(x_w);
3292 float y = wl_fixed_to_double(y_w);
3298 if (input->drag_offer)
3299 types_data = input->drag_offer->types.data;
3303 if (window->data_handler)
3304 window->data_handler(window, input, x, y, types_data,
3309 data_device_drop(void *data, struct wl_data_device *data_device)
3311 struct input *input = data;
3312 struct window *window = input->pointer_focus;
3314 if (window->drop_handler)
3315 window->drop_handler(window, input,
3316 input->sx, input->sy, window->user_data);
3320 data_device_selection(void *data,
3321 struct wl_data_device *wl_data_device,
3322 struct wl_data_offer *offer)
3324 struct input *input = data;
3327 if (input->selection_offer)
3328 data_offer_destroy(input->selection_offer);
3331 input->selection_offer = wl_data_offer_get_user_data(offer);
3332 p = wl_array_add(&input->selection_offer->types, sizeof *p);
3335 input->selection_offer = NULL;
3339 static const struct wl_data_device_listener data_device_listener = {
3340 data_device_data_offer,
3345 data_device_selection
3349 input_set_pointer_image_index(struct input *input, int index)
3351 struct wl_buffer *buffer;
3352 struct wl_cursor *cursor;
3353 struct wl_cursor_image *image;
3355 if (!input->pointer)
3358 cursor = input->display->cursors[input->current_cursor];
3362 if (index >= (int) cursor->image_count) {
3363 fprintf(stderr, "cursor index out of range\n");
3367 image = cursor->images[index];
3368 buffer = wl_cursor_image_get_buffer(image);
3372 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
3373 input->pointer_surface,
3374 image->hotspot_x, image->hotspot_y);
3375 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
3376 wl_surface_damage(input->pointer_surface, 0, 0,
3377 image->width, image->height);
3378 wl_surface_commit(input->pointer_surface);
3381 static const struct wl_callback_listener pointer_surface_listener;
3384 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
3387 struct input *input = data;
3388 struct wl_cursor *cursor;
3392 assert(callback == input->cursor_frame_cb);
3393 wl_callback_destroy(callback);
3394 input->cursor_frame_cb = NULL;
3397 if (!input->pointer)
3400 if (input->current_cursor == CURSOR_BLANK) {
3401 wl_pointer_set_cursor(input->pointer,
3402 input->pointer_enter_serial,
3407 if (input->current_cursor == CURSOR_UNSET)
3409 cursor = input->display->cursors[input->current_cursor];
3413 /* FIXME We don't have the current time on the first call so we set
3414 * the animation start to the time of the first frame callback. */
3416 input->cursor_anim_start = 0;
3417 else if (input->cursor_anim_start == 0)
3418 input->cursor_anim_start = time;
3420 if (time == 0 || input->cursor_anim_start == 0)
3423 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
3425 if (cursor->image_count > 1) {
3426 input->cursor_frame_cb =
3427 wl_surface_frame(input->pointer_surface);
3428 wl_callback_add_listener(input->cursor_frame_cb,
3429 &pointer_surface_listener, input);
3432 input_set_pointer_image_index(input, i);
3435 static const struct wl_callback_listener pointer_surface_listener = {
3436 pointer_surface_frame_callback
3440 input_set_pointer_image(struct input *input, int pointer)
3444 if (!input->pointer)
3447 if (input->pointer_enter_serial > input->cursor_serial)
3450 if (!force && pointer == input->current_cursor)
3453 input->current_cursor = pointer;
3454 input->cursor_serial = input->pointer_enter_serial;
3455 if (!input->cursor_frame_cb)
3456 pointer_surface_frame_callback(input, NULL, 0);
3458 /* The current frame callback may be stuck if, for instance,
3459 * the set cursor request was processed by the server after
3460 * this client lost the focus. In this case the cursor surface
3461 * might not be mapped and the frame callback wouldn't ever
3462 * complete. Send a set_cursor and attach to try to map the
3463 * cursor surface again so that the callback will finish */
3464 input_set_pointer_image_index(input, 0);
3468 struct wl_data_device *
3469 input_get_data_device(struct input *input)
3471 return input->data_device;
3475 input_set_selection(struct input *input,
3476 struct wl_data_source *source, uint32_t time)
3478 wl_data_device_set_selection(input->data_device, source, time);
3482 input_accept(struct input *input, const char *type)
3484 wl_data_offer_accept(input->drag_offer->offer,
3485 input->pointer_enter_serial, type);
3489 offer_io_func(struct task *task, uint32_t events)
3491 struct data_offer *offer =
3492 container_of(task, struct data_offer, io_task);
3496 len = read(offer->fd, buffer, sizeof buffer);
3497 offer->func(buffer, len,
3498 offer->x, offer->y, offer->user_data);
3502 data_offer_destroy(offer);
3507 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3508 data_func_t func, void *user_data)
3512 if (pipe2(p, O_CLOEXEC) == -1)
3515 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3518 offer->io_task.run = offer_io_func;
3522 offer->user_data = user_data;
3524 display_watch_fd(offer->input->display,
3525 offer->fd, EPOLLIN, &offer->io_task);
3529 input_receive_drag_data(struct input *input, const char *mime_type,
3530 data_func_t func, void *data)
3532 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3533 input->drag_offer->x = input->sx;
3534 input->drag_offer->y = input->sy;
3538 input_receive_selection_data(struct input *input, const char *mime_type,
3539 data_func_t func, void *data)
3543 if (input->selection_offer == NULL)
3546 for (p = input->selection_offer->types.data; *p; p++)
3547 if (strcmp(mime_type, *p) == 0)
3553 data_offer_receive_data(input->selection_offer,
3554 mime_type, func, data);
3559 input_receive_selection_data_to_fd(struct input *input,
3560 const char *mime_type, int fd)
3562 if (input->selection_offer)
3563 wl_data_offer_receive(input->selection_offer->offer,
3570 window_move(struct window *window, struct input *input, uint32_t serial)
3572 if (!window->shell_surface)
3575 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3579 surface_set_synchronized(struct surface *surface)
3581 if (!surface->subsurface)
3584 if (surface->synchronized)
3587 wl_subsurface_set_sync(surface->subsurface);
3588 surface->synchronized = 1;
3592 surface_set_synchronized_default(struct surface *surface)
3594 if (!surface->subsurface)
3597 if (surface->synchronized == surface->synchronized_default)
3600 if (surface->synchronized_default)
3601 wl_subsurface_set_sync(surface->subsurface);
3603 wl_subsurface_set_desync(surface->subsurface);
3605 surface->synchronized = surface->synchronized_default;
3609 surface_resize(struct surface *surface)
3611 struct widget *widget = surface->widget;
3612 struct wl_compositor *compositor = widget->window->display->compositor;
3614 if (surface->input_region) {
3615 wl_region_destroy(surface->input_region);
3616 surface->input_region = NULL;
3619 if (surface->opaque_region)
3620 wl_region_destroy(surface->opaque_region);
3622 surface->opaque_region = wl_compositor_create_region(compositor);
3624 if (widget->resize_handler)
3625 widget->resize_handler(widget,
3626 widget->allocation.width,
3627 widget->allocation.height,
3630 if (surface->subsurface &&
3631 (surface->allocation.x != widget->allocation.x ||
3632 surface->allocation.y != widget->allocation.y)) {
3633 wl_subsurface_set_position(surface->subsurface,
3634 widget->allocation.x,
3635 widget->allocation.y);
3637 if (surface->allocation.width != widget->allocation.width ||
3638 surface->allocation.height != widget->allocation.height) {
3639 window_schedule_redraw(widget->window);
3641 surface->allocation = widget->allocation;
3644 wl_region_add(surface->opaque_region, 0, 0,
3645 widget->allocation.width,
3646 widget->allocation.height);
3650 hack_prevent_EGL_sub_surface_deadlock(struct window *window)
3653 * This hack should be removed, when EGL respects
3654 * eglSwapInterval(0).
3656 * If this window has sub-surfaces, especially a free-running
3657 * EGL-widget, we need to post the parent surface once with
3658 * all the old state to guarantee, that the EGL-widget will
3659 * receive its frame callback soon. Otherwise, a forced call
3660 * to eglSwapBuffers may end up blocking, waiting for a frame
3661 * event that will never come, because we will commit the parent
3662 * surface with all new state only after eglSwapBuffers returns.
3664 * This assumes, that:
3665 * 1. When the EGL widget's resize hook is called, it pauses.
3666 * 2. When the EGL widget's redraw hook is called, it forces a
3667 * repaint and a call to eglSwapBuffers(), and maybe resumes.
3668 * In a single threaded application condition 1 is a no-op.
3670 * XXX: This should actually be after the surface_resize() calls,
3671 * but cannot, because then it would commit the incomplete state
3672 * accumulated from the widget resize hooks.
3674 if (window->subsurface_list.next != &window->main_surface->link ||
3675 window->subsurface_list.prev != &window->main_surface->link)
3676 wl_surface_commit(window->main_surface->surface);
3680 idle_resize(struct window *window)
3682 struct surface *surface;
3684 window->resize_needed = 0;
3685 window->redraw_needed = 1;
3687 DBG("from %dx%d to %dx%d\n",
3688 window->main_surface->server_allocation.width,
3689 window->main_surface->server_allocation.height,
3690 window->pending_allocation.width,
3691 window->pending_allocation.height);
3693 hack_prevent_EGL_sub_surface_deadlock(window);
3695 widget_set_allocation(window->main_surface->widget,
3696 window->pending_allocation.x,
3697 window->pending_allocation.y,
3698 window->pending_allocation.width,
3699 window->pending_allocation.height);
3701 surface_resize(window->main_surface);
3703 /* The main surface is in the list, too. Main surface's
3704 * resize_handler is responsible for calling widget_set_allocation()
3705 * on all sub-surface root widgets, so they will be resized
3708 wl_list_for_each(surface, &window->subsurface_list, link) {
3709 if (surface == window->main_surface)
3712 surface_set_synchronized(surface);
3713 surface_resize(surface);
3718 window_schedule_resize(struct window *window, int width, int height)
3720 window->pending_allocation.x = 0;
3721 window->pending_allocation.y = 0;
3722 window->pending_allocation.width = width;
3723 window->pending_allocation.height = height;
3725 if (window->min_allocation.width == 0)
3726 window->min_allocation = window->pending_allocation;
3727 if (window->pending_allocation.width < window->min_allocation.width)
3728 window->pending_allocation.width = window->min_allocation.width;
3729 if (window->pending_allocation.height < window->min_allocation.height)
3730 window->pending_allocation.height = window->min_allocation.height;
3732 window->resize_needed = 1;
3733 window_schedule_redraw(window);
3737 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3739 window_schedule_resize(widget->window, width, height);
3743 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3746 wl_shell_surface_pong(shell_surface, serial);
3750 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3751 uint32_t edges, int32_t width, int32_t height)
3753 struct window *window = data;
3755 window->resize_edges = edges;
3756 window_schedule_resize(window, width, height);
3760 menu_destroy(struct menu *menu)
3762 widget_destroy(menu->widget);
3763 window_destroy(menu->window);
3768 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3770 struct window *window = data;
3771 struct menu *menu = window->main_surface->widget->user_data;
3773 /* FIXME: Need more context in this event, at least the input
3774 * device. Or just use wl_callback. And this really needs to
3775 * be a window vfunc that the menu can set. And we need the
3778 input_ungrab(menu->input);
3782 static const struct wl_shell_surface_listener shell_surface_listener = {
3789 window_get_allocation(struct window *window,
3790 struct rectangle *allocation)
3792 *allocation = window->main_surface->allocation;
3796 widget_redraw(struct widget *widget)
3798 struct widget *child;
3800 if (widget->redraw_handler)
3801 widget->redraw_handler(widget, widget->user_data);
3802 wl_list_for_each(child, &widget->child_list, link)
3803 widget_redraw(child);
3807 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3809 struct surface *surface = data;
3811 assert(callback == surface->frame_cb);
3812 DBG_OBJ(callback, "done\n");
3813 wl_callback_destroy(callback);
3814 surface->frame_cb = NULL;
3816 surface->last_time = time;
3818 if (surface->redraw_needed || surface->window->redraw_needed) {
3819 DBG_OBJ(surface->surface, "window_schedule_redraw_task\n");
3820 window_schedule_redraw_task(surface->window);
3824 static const struct wl_callback_listener listener = {
3829 surface_redraw(struct surface *surface)
3831 DBG_OBJ(surface->surface, "begin\n");
3833 if (!surface->window->redraw_needed && !surface->redraw_needed)
3836 /* Whole-window redraw forces a redraw even if the previous has
3837 * not yet hit the screen.
3839 if (surface->frame_cb) {
3840 if (!surface->window->redraw_needed)
3843 DBG_OBJ(surface->frame_cb, "cancelled\n");
3844 wl_callback_destroy(surface->frame_cb);
3847 surface->frame_cb = wl_surface_frame(surface->surface);
3848 wl_callback_add_listener(surface->frame_cb, &listener, surface);
3849 DBG_OBJ(surface->frame_cb, "new\n");
3851 surface->redraw_needed = 0;
3852 DBG_OBJ(surface->surface, "-> widget_redraw\n");
3853 widget_redraw(surface->widget);
3854 DBG_OBJ(surface->surface, "done\n");
3858 idle_redraw(struct task *task, uint32_t events)
3860 struct window *window = container_of(task, struct window, redraw_task);
3861 struct surface *surface;
3863 DBG(" --------- \n");
3865 wl_list_init(&window->redraw_task.link);
3866 window->redraw_task_scheduled = 0;
3868 if (window->resize_needed) {
3869 /* throttle resizing to the main surface display */
3870 if (window->main_surface->frame_cb) {
3871 DBG_OBJ(window->main_surface->frame_cb, "pending\n");
3875 idle_resize(window);
3878 wl_list_for_each(surface, &window->subsurface_list, link)
3879 surface_redraw(surface);
3881 window->redraw_needed = 0;
3882 window_flush(window);
3884 wl_list_for_each(surface, &window->subsurface_list, link)
3885 surface_set_synchronized_default(surface);
3889 window_schedule_redraw_task(struct window *window)
3891 if (window->configure_requests)
3893 if (!window->redraw_task_scheduled) {
3894 window->redraw_task.run = idle_redraw;
3895 display_defer(window->display, &window->redraw_task);
3896 window->redraw_task_scheduled = 1;
3901 window_schedule_redraw(struct window *window)
3903 struct surface *surface;
3905 DBG_OBJ(window->main_surface->surface, "window %p\n", window);
3907 wl_list_for_each(surface, &window->subsurface_list, link)
3908 surface->redraw_needed = 1;
3910 window_schedule_redraw_task(window);
3914 window_is_fullscreen(struct window *window)
3916 return window->type == TYPE_FULLSCREEN;
3920 configure_request_completed(void *data, struct wl_callback *callback, uint32_t time)
3922 struct window *window = data;
3924 wl_callback_destroy(callback);
3925 window->configure_requests--;
3927 if (!window->configure_requests)
3928 window_schedule_redraw(window);
3931 static struct wl_callback_listener configure_request_listener = {
3932 configure_request_completed,
3936 window_defer_redraw_until_configure(struct window* window)
3938 struct wl_callback *callback;
3940 if (window->redraw_task_scheduled) {
3941 wl_list_remove(&window->redraw_task.link);
3942 window->redraw_task_scheduled = 0;
3945 callback = wl_display_sync(window->display->display);
3946 wl_callback_add_listener(callback, &configure_request_listener, window);
3947 window->configure_requests++;
3951 window_set_fullscreen(struct window *window, int fullscreen)
3953 if (!window->display->shell)
3956 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3960 window->saved_type = window->type;
3961 if (window->type == TYPE_TOPLEVEL) {
3962 window->saved_allocation = window->main_surface->allocation;
3964 window->type = TYPE_FULLSCREEN;
3965 wl_shell_surface_set_fullscreen(window->shell_surface,
3966 window->fullscreen_method,
3968 window_defer_redraw_until_configure (window);
3970 if (window->saved_type == TYPE_MAXIMIZED) {
3971 window_set_maximized(window, 1);
3973 window->type = TYPE_TOPLEVEL;
3974 wl_shell_surface_set_toplevel(window->shell_surface);
3975 window_schedule_resize(window,
3976 window->saved_allocation.width,
3977 window->saved_allocation.height);
3984 window_set_fullscreen_method(struct window *window,
3985 enum wl_shell_surface_fullscreen_method method)
3987 window->fullscreen_method = method;
3991 window_is_maximized(struct window *window)
3993 return window->type == TYPE_MAXIMIZED;
3997 window_set_maximized(struct window *window, int maximized)
3999 if (!window->display->shell)
4002 if ((window->type == TYPE_MAXIMIZED) == maximized)
4005 if (window->type == TYPE_TOPLEVEL) {
4006 window->saved_allocation = window->main_surface->allocation;
4007 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4008 window->type = TYPE_MAXIMIZED;
4009 window_defer_redraw_until_configure(window);
4010 } else if (window->type == TYPE_FULLSCREEN) {
4011 wl_shell_surface_set_maximized(window->shell_surface, NULL);
4012 window->type = TYPE_MAXIMIZED;
4013 window_defer_redraw_until_configure(window);
4015 wl_shell_surface_set_toplevel(window->shell_surface);
4016 window->type = TYPE_TOPLEVEL;
4017 window_schedule_resize(window,
4018 window->saved_allocation.width,
4019 window->saved_allocation.height);
4024 window_set_user_data(struct window *window, void *data)
4026 window->user_data = data;
4030 window_get_user_data(struct window *window)
4032 return window->user_data;
4036 window_set_key_handler(struct window *window,
4037 window_key_handler_t handler)
4039 window->key_handler = handler;
4043 window_set_keyboard_focus_handler(struct window *window,
4044 window_keyboard_focus_handler_t handler)
4046 window->keyboard_focus_handler = handler;
4050 window_set_data_handler(struct window *window, window_data_handler_t handler)
4052 window->data_handler = handler;
4056 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
4058 window->drop_handler = handler;
4062 window_set_close_handler(struct window *window,
4063 window_close_handler_t handler)
4065 window->close_handler = handler;
4069 window_set_fullscreen_handler(struct window *window,
4070 window_fullscreen_handler_t handler)
4072 window->fullscreen_handler = handler;
4076 window_set_output_handler(struct window *window,
4077 window_output_handler_t handler)
4079 window->output_handler = handler;
4083 window_set_title(struct window *window, const char *title)
4085 free(window->title);
4086 window->title = strdup(title);
4087 if (window->shell_surface)
4088 wl_shell_surface_set_title(window->shell_surface, title);
4092 window_get_title(struct window *window)
4094 return window->title;
4098 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
4100 struct text_cursor_position *text_cursor_position =
4101 window->display->text_cursor_position;
4103 if (!text_cursor_position)
4106 text_cursor_position_notify(text_cursor_position,
4107 window->main_surface->surface,
4108 wl_fixed_from_int(x),
4109 wl_fixed_from_int(y));
4113 window_damage(struct window *window, int32_t x, int32_t y,
4114 int32_t width, int32_t height)
4116 wl_surface_damage(window->main_surface->surface, x, y, width, height);
4120 surface_enter(void *data,
4121 struct wl_surface *wl_surface, struct wl_output *wl_output)
4123 struct window *window = data;
4124 struct output *output;
4125 struct output *output_found = NULL;
4126 struct window_output *window_output;
4128 wl_list_for_each(output, &window->display->output_list, link) {
4129 if (output->output == wl_output) {
4130 output_found = output;
4138 window_output = xmalloc(sizeof *window_output);
4139 window_output->output = output_found;
4141 wl_list_insert (&window->window_output_list, &window_output->link);
4143 if (window->output_handler)
4144 window->output_handler(window, output_found, 1,
4149 surface_leave(void *data,
4150 struct wl_surface *wl_surface, struct wl_output *output)
4152 struct window *window = data;
4153 struct window_output *window_output;
4154 struct window_output *window_output_found = NULL;
4156 wl_list_for_each(window_output, &window->window_output_list, link) {
4157 if (window_output->output->output == output) {
4158 window_output_found = window_output;
4163 if (window_output_found) {
4164 wl_list_remove(&window_output_found->link);
4166 if (window->output_handler)
4167 window->output_handler(window, window_output->output,
4168 0, window->user_data);
4170 free(window_output_found);
4174 static const struct wl_surface_listener surface_listener = {
4179 static struct surface *
4180 surface_create(struct window *window)
4182 struct display *display = window->display;
4183 struct surface *surface;
4185 surface = xmalloc(sizeof *surface);
4186 memset(surface, 0, sizeof *surface);
4190 surface->window = window;
4191 surface->surface = wl_compositor_create_surface(display->compositor);
4192 surface->buffer_scale = 1;
4193 wl_surface_add_listener(surface->surface, &surface_listener, window);
4195 wl_list_insert(&window->subsurface_list, &surface->link);
4200 static struct window *
4201 window_create_internal(struct display *display,
4202 struct window *parent, int type)
4204 struct window *window;
4205 struct surface *surface;
4207 window = xzalloc(sizeof *window);
4208 wl_list_init(&window->subsurface_list);
4209 window->display = display;
4210 window->parent = parent;
4212 surface = surface_create(window);
4213 window->main_surface = surface;
4215 if (type != TYPE_CUSTOM && display->shell) {
4216 window->shell_surface =
4217 wl_shell_get_shell_surface(display->shell,
4219 fail_on_null(window->shell_surface);
4222 window->type = type;
4223 window->fullscreen_method = WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT;
4224 window->configure_requests = 0;
4225 window->preferred_format = WINDOW_PREFERRED_FORMAT_NONE;
4227 if (display->argb_device)
4228 #ifdef HAVE_CAIRO_EGL
4229 surface->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
4231 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4234 surface->buffer_type = WINDOW_BUFFER_TYPE_SHM;
4236 wl_surface_set_user_data(surface->surface, window);
4237 wl_list_insert(display->window_list.prev, &window->link);
4238 wl_list_init(&window->redraw_task.link);
4240 if (window->shell_surface) {
4241 wl_shell_surface_set_user_data(window->shell_surface, window);
4242 wl_shell_surface_add_listener(window->shell_surface,
4243 &shell_surface_listener, window);
4246 wl_list_init (&window->window_output_list);
4252 window_create(struct display *display)
4254 return window_create_internal(display, NULL, TYPE_NONE);
4258 window_create_custom(struct display *display)
4260 return window_create_internal(display, NULL, TYPE_CUSTOM);
4264 window_create_transient(struct display *display, struct window *parent,
4265 int32_t x, int32_t y, uint32_t flags)
4267 struct window *window;
4269 window = window_create_internal(parent->display,
4270 parent, TYPE_TRANSIENT);
4276 wl_shell_surface_set_transient(
4277 window->shell_surface,
4278 window->parent->main_surface->surface,
4279 window->x, window->y, flags);
4285 menu_set_item(struct menu *menu, int sy)
4289 next = (sy - 8) / 20;
4290 if (menu->current != next) {
4291 menu->current = next;
4292 widget_schedule_redraw(menu->widget);
4297 menu_motion_handler(struct widget *widget,
4298 struct input *input, uint32_t time,
4299 float x, float y, void *data)
4301 struct menu *menu = data;
4303 if (widget == menu->widget)
4304 menu_set_item(data, y);
4306 return CURSOR_LEFT_PTR;
4310 menu_enter_handler(struct widget *widget,
4311 struct input *input, float x, float y, void *data)
4313 struct menu *menu = data;
4315 if (widget == menu->widget)
4316 menu_set_item(data, y);
4318 return CURSOR_LEFT_PTR;
4322 menu_leave_handler(struct widget *widget, struct input *input, void *data)
4324 struct menu *menu = data;
4326 if (widget == menu->widget)
4327 menu_set_item(data, -200);
4331 menu_button_handler(struct widget *widget,
4332 struct input *input, uint32_t time,
4333 uint32_t button, enum wl_pointer_button_state state,
4337 struct menu *menu = data;
4339 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
4340 (menu->release_count > 0 || time - menu->time > 500)) {
4341 /* Either relase after press-drag-release or
4342 * click-motion-click. */
4343 menu->func(menu->window->parent,
4344 menu->current, menu->window->parent->user_data);
4345 input_ungrab(input);
4347 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
4348 menu->release_count++;
4353 menu_redraw_handler(struct widget *widget, void *data)
4356 const int32_t r = 3, margin = 3;
4357 struct menu *menu = data;
4358 int32_t width, height, i;
4360 cr = widget_cairo_create(widget);
4361 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
4362 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
4365 width = widget->allocation.width;
4366 height = widget->allocation.height;
4367 rounded_rect(cr, 0, 0, width, height, r);
4369 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
4370 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
4373 for (i = 0; i < menu->count; i++) {
4374 if (i == menu->current) {
4375 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4376 cairo_rectangle(cr, margin, i * 20 + margin,
4377 width - 2 * margin, 20);
4379 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
4380 cairo_move_to(cr, 10, i * 20 + 16);
4381 cairo_show_text(cr, menu->entries[i]);
4383 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
4384 cairo_move_to(cr, 10, i * 20 + 16);
4385 cairo_show_text(cr, menu->entries[i]);
4393 window_show_menu(struct display *display,
4394 struct input *input, uint32_t time, struct window *parent,
4395 int32_t x, int32_t y,
4396 menu_func_t func, const char **entries, int count)
4398 struct window *window;
4400 const int32_t margin = 3;
4402 menu = malloc(sizeof *menu);
4406 window = window_create_internal(parent->display, parent, TYPE_MENU);
4412 menu->window = window;
4413 menu->widget = window_add_widget(menu->window, menu);
4414 window_set_buffer_scale (menu->window, window_get_buffer_scale (parent));
4415 window_set_buffer_transform (menu->window, window_get_buffer_transform (parent));
4416 menu->entries = entries;
4417 menu->count = count;
4418 menu->release_count = 0;
4422 menu->input = input;
4423 window->type = TYPE_MENU;
4427 input_ungrab(input);
4428 wl_shell_surface_set_popup(window->shell_surface, input->seat,
4429 display_get_serial(window->display),
4430 window->parent->main_surface->surface,
4431 window->x, window->y, 0);
4433 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
4434 widget_set_enter_handler(menu->widget, menu_enter_handler);
4435 widget_set_leave_handler(menu->widget, menu_leave_handler);
4436 widget_set_motion_handler(menu->widget, menu_motion_handler);
4437 widget_set_button_handler(menu->widget, menu_button_handler);
4439 input_grab(input, menu->widget, 0);
4440 window_schedule_resize(window, 200, count * 20 + margin * 2);
4444 window_set_buffer_type(struct window *window, enum window_buffer_type type)
4446 window->main_surface->buffer_type = type;
4450 window_set_preferred_format(struct window *window,
4451 enum preferred_format format)
4453 window->preferred_format = format;
4457 window_add_subsurface(struct window *window, void *data,
4458 enum subsurface_mode default_mode)
4460 struct widget *widget;
4461 struct surface *surface;
4462 struct wl_surface *parent;
4463 struct wl_subcompositor *subcompo = window->display->subcompositor;
4468 surface = surface_create(window);
4469 widget = widget_create(window, surface, data);
4470 wl_list_init(&widget->link);
4471 surface->widget = widget;
4473 parent = window->main_surface->surface;
4474 surface->subsurface = wl_subcompositor_get_subsurface(subcompo,
4477 surface->synchronized = 1;
4479 switch (default_mode) {
4480 case SUBSURFACE_SYNCHRONIZED:
4481 surface->synchronized_default = 1;
4483 case SUBSURFACE_DESYNCHRONIZED:
4484 surface->synchronized_default = 0;
4487 assert(!"bad enum subsurface_mode");
4494 display_handle_geometry(void *data,
4495 struct wl_output *wl_output,
4498 int physical_height,
4504 struct output *output = data;
4506 output->allocation.x = x;
4507 output->allocation.y = y;
4508 output->transform = transform;
4512 display_handle_done(void *data,
4513 struct wl_output *wl_output)
4518 display_handle_scale(void *data,
4519 struct wl_output *wl_output,
4522 struct output *output = data;
4524 output->scale = scale;
4528 display_handle_mode(void *data,
4529 struct wl_output *wl_output,
4535 struct output *output = data;
4536 struct display *display = output->display;
4538 if (flags & WL_OUTPUT_MODE_CURRENT) {
4539 output->allocation.width = width;
4540 output->allocation.height = height;
4541 if (display->output_configure_handler)
4542 (*display->output_configure_handler)(
4543 output, display->user_data);
4547 static const struct wl_output_listener output_listener = {
4548 display_handle_geometry,
4549 display_handle_mode,
4550 display_handle_done,
4551 display_handle_scale
4555 display_add_output(struct display *d, uint32_t id)
4557 struct output *output;
4559 output = zalloc(sizeof *output);
4563 output->display = d;
4566 wl_registry_bind(d->registry, id, &wl_output_interface, 2);
4567 wl_list_insert(d->output_list.prev, &output->link);
4569 wl_output_add_listener(output->output, &output_listener, output);
4573 output_destroy(struct output *output)
4575 if (output->destroy_handler)
4576 (*output->destroy_handler)(output, output->user_data);
4578 wl_output_destroy(output->output);
4579 wl_list_remove(&output->link);
4584 display_set_global_handler(struct display *display,
4585 display_global_handler_t handler)
4587 struct global *global;
4589 display->global_handler = handler;
4593 wl_list_for_each(global, &display->global_list, link)
4594 display->global_handler(display,
4595 global->name, global->interface,
4596 global->version, display->user_data);
4600 display_set_output_configure_handler(struct display *display,
4601 display_output_handler_t handler)
4603 struct output *output;
4605 display->output_configure_handler = handler;
4609 wl_list_for_each(output, &display->output_list, link) {
4610 if (output->allocation.width == 0 &&
4611 output->allocation.height == 0)
4614 (*display->output_configure_handler)(output,
4615 display->user_data);
4620 output_set_user_data(struct output *output, void *data)
4622 output->user_data = data;
4626 output_get_user_data(struct output *output)
4628 return output->user_data;
4632 output_set_destroy_handler(struct output *output,
4633 display_output_handler_t handler)
4635 output->destroy_handler = handler;
4636 /* FIXME: implement this, once we have way to remove outputs */
4640 output_get_allocation(struct output *output, struct rectangle *base)
4642 struct rectangle allocation = output->allocation;
4644 switch (output->transform) {
4645 case WL_OUTPUT_TRANSFORM_90:
4646 case WL_OUTPUT_TRANSFORM_270:
4647 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4648 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4649 /* Swap width and height */
4650 allocation.width = output->allocation.height;
4651 allocation.height = output->allocation.width;
4659 output_get_wl_output(struct output *output)
4661 return output->output;
4664 enum wl_output_transform
4665 output_get_transform(struct output *output)
4667 return output->transform;
4671 output_get_scale(struct output *output)
4673 return output->scale;
4677 fini_xkb(struct input *input)
4679 xkb_state_unref(input->xkb.state);
4680 xkb_map_unref(input->xkb.keymap);
4684 display_add_input(struct display *d, uint32_t id)
4686 struct input *input;
4688 input = zalloc(sizeof *input);
4693 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
4694 input->pointer_focus = NULL;
4695 input->keyboard_focus = NULL;
4696 wl_list_insert(d->input_list.prev, &input->link);
4698 wl_seat_add_listener(input->seat, &seat_listener, input);
4699 wl_seat_set_user_data(input->seat, input);
4701 input->data_device =
4702 wl_data_device_manager_get_data_device(d->data_device_manager,
4704 wl_data_device_add_listener(input->data_device, &data_device_listener,
4707 input->pointer_surface = wl_compositor_create_surface(d->compositor);
4709 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
4710 TFD_CLOEXEC | TFD_NONBLOCK);
4711 input->repeat_task.run = keyboard_repeat_func;
4712 display_watch_fd(d, input->repeat_timer_fd,
4713 EPOLLIN, &input->repeat_task);
4717 input_destroy(struct input *input)
4719 input_remove_keyboard_focus(input);
4720 input_remove_pointer_focus(input);
4722 if (input->drag_offer)
4723 data_offer_destroy(input->drag_offer);
4725 if (input->selection_offer)
4726 data_offer_destroy(input->selection_offer);
4728 wl_data_device_destroy(input->data_device);
4731 wl_surface_destroy(input->pointer_surface);
4733 wl_list_remove(&input->link);
4734 wl_seat_destroy(input->seat);
4735 close(input->repeat_timer_fd);
4740 init_workspace_manager(struct display *d, uint32_t id)
4742 d->workspace_manager =
4743 wl_registry_bind(d->registry, id,
4744 &workspace_manager_interface, 1);
4745 if (d->workspace_manager != NULL)
4746 workspace_manager_add_listener(d->workspace_manager,
4747 &workspace_manager_listener,
4752 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
4754 struct display *d = data;
4756 if (format == WL_SHM_FORMAT_RGB565)
4760 struct wl_shm_listener shm_listener = {
4765 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
4766 const char *interface, uint32_t version)
4768 struct display *d = data;
4769 struct global *global;
4771 global = xmalloc(sizeof *global);
4773 global->interface = strdup(interface);
4774 global->version = version;
4775 wl_list_insert(d->global_list.prev, &global->link);
4777 if (strcmp(interface, "wl_compositor") == 0) {
4778 d->compositor = wl_registry_bind(registry, id,
4779 &wl_compositor_interface, 3);
4780 } else if (strcmp(interface, "wl_output") == 0) {
4781 display_add_output(d, id);
4782 } else if (strcmp(interface, "wl_seat") == 0) {
4783 display_add_input(d, id);
4784 } else if (strcmp(interface, "wl_shell") == 0) {
4785 d->shell = wl_registry_bind(registry,
4786 id, &wl_shell_interface, 1);
4787 } else if (strcmp(interface, "wl_shm") == 0) {
4788 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4789 wl_shm_add_listener(d->shm, &shm_listener, d);
4790 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4791 d->data_device_manager =
4792 wl_registry_bind(registry, id,
4793 &wl_data_device_manager_interface, 1);
4794 } else if (strcmp(interface, "text_cursor_position") == 0) {
4795 d->text_cursor_position =
4796 wl_registry_bind(registry, id,
4797 &text_cursor_position_interface, 1);
4798 } else if (strcmp(interface, "workspace_manager") == 0) {
4799 init_workspace_manager(d, id);
4800 } else if (strcmp(interface, "wl_subcompositor") == 0) {
4802 wl_registry_bind(registry, id,
4803 &wl_subcompositor_interface, 1);
4806 if (d->global_handler)
4807 d->global_handler(d, id, interface, version, d->user_data);
4811 registry_handle_global_remove(void *data, struct wl_registry *registry,
4814 struct display *d = data;
4815 struct global *global;
4818 wl_list_for_each_safe(global, tmp, &d->global_list, link) {
4819 if (global->name != name)
4822 /* XXX: Should destroy bound globals, and call
4823 * the counterpart of display::global_handler
4825 wl_list_remove(&global->link);
4826 free(global->interface);
4832 display_bind(struct display *display, uint32_t name,
4833 const struct wl_interface *interface, uint32_t version)
4835 return wl_registry_bind(display->registry, name, interface, version);
4838 static const struct wl_registry_listener registry_listener = {
4839 registry_handle_global,
4840 registry_handle_global_remove
4843 #ifdef HAVE_CAIRO_EGL
4845 init_egl(struct display *d)
4847 EGLint major, minor;
4850 #ifdef USE_CAIRO_GLESV2
4851 # define GL_BIT EGL_OPENGL_ES2_BIT
4853 # define GL_BIT EGL_OPENGL_BIT
4856 static const EGLint argb_cfg_attribs[] = {
4857 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4863 EGL_RENDERABLE_TYPE, GL_BIT,
4867 #ifdef USE_CAIRO_GLESV2
4868 static const EGLint context_attribs[] = {
4869 EGL_CONTEXT_CLIENT_VERSION, 2,
4872 EGLint api = EGL_OPENGL_ES_API;
4874 EGLint *context_attribs = NULL;
4875 EGLint api = EGL_OPENGL_API;
4878 d->dpy = eglGetDisplay(d->display);
4879 if (!eglInitialize(d->dpy, &major, &minor)) {
4880 fprintf(stderr, "failed to initialize EGL\n");
4884 if (!eglBindAPI(api)) {
4885 fprintf(stderr, "failed to bind EGL client API\n");
4889 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4890 &d->argb_config, 1, &n) || n != 1) {
4891 fprintf(stderr, "failed to choose argb EGL config\n");
4895 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
4896 EGL_NO_CONTEXT, context_attribs);
4897 if (d->argb_ctx == NULL) {
4898 fprintf(stderr, "failed to create EGL context\n");
4902 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
4903 fprintf(stderr, "failed to make EGL context current\n");
4907 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4908 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4909 fprintf(stderr, "failed to get cairo EGL argb device\n");
4917 fini_egl(struct display *display)
4919 cairo_device_destroy(display->argb_device);
4921 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4924 eglTerminate(display->dpy);
4930 init_dummy_surface(struct display *display)
4935 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4937 display->dummy_surface =
4938 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4940 display->dummy_surface_data = data;
4944 handle_display_data(struct task *task, uint32_t events)
4946 struct display *display =
4947 container_of(task, struct display, display_task);
4948 struct epoll_event ep;
4951 display->display_fd_events = events;
4953 if (events & EPOLLERR || events & EPOLLHUP) {
4954 display_exit(display);
4958 if (events & EPOLLIN) {
4959 ret = wl_display_dispatch(display->display);
4961 display_exit(display);
4966 if (events & EPOLLOUT) {
4967 ret = wl_display_flush(display->display);
4969 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4970 ep.data.ptr = &display->display_task;
4971 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4972 display->display_fd, &ep);
4973 } else if (ret == -1 && errno != EAGAIN) {
4974 display_exit(display);
4981 log_handler(const char *format, va_list args)
4983 vfprintf(stderr, format, args);
4987 display_create(int *argc, char *argv[])
4991 wl_log_set_handler_client(log_handler);
4993 d = zalloc(sizeof *d);
4997 d->display = wl_display_connect(NULL);
4998 if (d->display == NULL) {
4999 fprintf(stderr, "failed to connect to Wayland display: %m\n");
5004 d->xkb_context = xkb_context_new(0);
5005 if (d->xkb_context == NULL) {
5006 fprintf(stderr, "Failed to create XKB context\n");
5011 d->epoll_fd = os_epoll_create_cloexec();
5012 d->display_fd = wl_display_get_fd(d->display);
5013 d->display_task.run = handle_display_data;
5014 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
5017 wl_list_init(&d->deferred_list);
5018 wl_list_init(&d->input_list);
5019 wl_list_init(&d->output_list);
5020 wl_list_init(&d->global_list);
5023 d->workspace_count = 1;
5025 d->registry = wl_display_get_registry(d->display);
5026 wl_registry_add_listener(d->registry, ®istry_listener, d);
5028 if (wl_display_dispatch(d->display) < 0) {
5029 fprintf(stderr, "Failed to process Wayland connection: %m\n");
5033 #ifdef HAVE_CAIRO_EGL
5034 if (init_egl(d) < 0)
5035 fprintf(stderr, "EGL does not seem to work, "
5036 "falling back to software rendering and wl_shm.\n");
5041 d->theme = theme_create();
5043 wl_list_init(&d->window_list);
5045 init_dummy_surface(d);
5051 display_destroy_outputs(struct display *display)
5054 struct output *output;
5056 wl_list_for_each_safe(output, tmp, &display->output_list, link)
5057 output_destroy(output);
5061 display_destroy_inputs(struct display *display)
5064 struct input *input;
5066 wl_list_for_each_safe(input, tmp, &display->input_list, link)
5067 input_destroy(input);
5071 display_destroy(struct display *display)
5073 if (!wl_list_empty(&display->window_list))
5074 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
5075 wl_list_length(&display->window_list));
5077 if (!wl_list_empty(&display->deferred_list))
5078 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
5080 cairo_surface_destroy(display->dummy_surface);
5081 free(display->dummy_surface_data);
5083 display_destroy_outputs(display);
5084 display_destroy_inputs(display);
5086 xkb_context_unref(display->xkb_context);
5088 theme_destroy(display->theme);
5089 destroy_cursors(display);
5091 #ifdef HAVE_CAIRO_EGL
5092 if (display->argb_device)
5096 if (display->subcompositor)
5097 wl_subcompositor_destroy(display->subcompositor);
5100 wl_shell_destroy(display->shell);
5103 wl_shm_destroy(display->shm);
5105 if (display->data_device_manager)
5106 wl_data_device_manager_destroy(display->data_device_manager);
5108 wl_compositor_destroy(display->compositor);
5109 wl_registry_destroy(display->registry);
5111 close(display->epoll_fd);
5113 if (!(display->display_fd_events & EPOLLERR) &&
5114 !(display->display_fd_events & EPOLLHUP))
5115 wl_display_flush(display->display);
5117 wl_display_disconnect(display->display);
5122 display_set_user_data(struct display *display, void *data)
5124 display->user_data = data;
5128 display_get_user_data(struct display *display)
5130 return display->user_data;
5134 display_get_display(struct display *display)
5136 return display->display;
5140 display_get_cairo_device(struct display *display)
5142 return display->argb_device;
5146 display_get_output(struct display *display)
5148 return container_of(display->output_list.next, struct output, link);
5151 struct wl_compositor *
5152 display_get_compositor(struct display *display)
5154 return display->compositor;
5158 display_get_serial(struct display *display)
5160 return display->serial;
5164 display_get_egl_display(struct display *d)
5169 struct wl_data_source *
5170 display_create_data_source(struct display *display)
5172 return wl_data_device_manager_create_data_source(display->data_device_manager);
5176 display_get_argb_egl_config(struct display *d)
5178 return d->argb_config;
5182 display_get_shell(struct display *display)
5184 return display->shell;
5188 display_acquire_window_surface(struct display *display,
5189 struct window *window,
5192 struct surface *surface = window->main_surface;
5194 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5197 widget_get_cairo_surface(window->main_surface->widget);
5198 return surface->toysurface->acquire(surface->toysurface, ctx);
5202 display_release_window_surface(struct display *display,
5203 struct window *window)
5205 struct surface *surface = window->main_surface;
5207 if (surface->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
5210 surface->toysurface->release(surface->toysurface);
5214 display_defer(struct display *display, struct task *task)
5216 wl_list_insert(&display->deferred_list, &task->link);
5220 display_watch_fd(struct display *display,
5221 int fd, uint32_t events, struct task *task)
5223 struct epoll_event ep;
5227 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
5231 display_unwatch_fd(struct display *display, int fd)
5233 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
5237 display_run(struct display *display)
5240 struct epoll_event ep[16];
5243 display->running = 1;
5245 while (!wl_list_empty(&display->deferred_list)) {
5246 task = container_of(display->deferred_list.prev,
5248 wl_list_remove(&task->link);
5252 wl_display_dispatch_pending(display->display);
5254 if (!display->running)
5257 ret = wl_display_flush(display->display);
5258 if (ret < 0 && errno == EAGAIN) {
5260 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
5261 ep[0].data.ptr = &display->display_task;
5263 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
5264 display->display_fd, &ep[0]);
5265 } else if (ret < 0) {
5269 count = epoll_wait(display->epoll_fd,
5270 ep, ARRAY_LENGTH(ep), -1);
5271 for (i = 0; i < count; i++) {
5272 task = ep[i].data.ptr;
5273 task->run(task, ep[i].events);
5279 display_exit(struct display *display)
5281 display->running = 0;
5285 keysym_modifiers_add(struct wl_array *modifiers_map,
5288 size_t len = strlen(name) + 1;
5291 p = wl_array_add(modifiers_map, len);
5296 strncpy(p, name, len);
5299 static xkb_mod_index_t
5300 keysym_modifiers_get_index(struct wl_array *modifiers_map,
5303 xkb_mod_index_t index = 0;
5304 char *p = modifiers_map->data;
5306 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
5307 if (strcmp(p, name) == 0)
5314 return XKB_MOD_INVALID;
5318 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
5321 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
5323 if (index == XKB_MOD_INVALID)
5324 return XKB_MOD_INVALID;
5330 fail_on_null(void *p)
5333 fprintf(stderr, "wayland-scanner: out of memory\n");
5343 return fail_on_null(malloc(s));
5349 return fail_on_null(zalloc(s));
5353 xstrdup(const char *s)
5355 return fail_on_null(strdup(s));