2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2012 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
26 #include "../config.h"
40 #include <sys/epoll.h>
41 #include <sys/timerfd.h>
46 #include <wayland-egl.h>
48 #ifdef USE_CAIRO_GLESV2
49 #include <GLES2/gl2.h>
50 #include <GLES2/gl2ext.h>
55 #include <EGL/eglext.h>
58 #else /* HAVE_CAIRO_EGL */
59 typedef void *EGLDisplay;
60 typedef void *EGLConfig;
61 typedef void *EGLContext;
62 #define EGL_NO_DISPLAY ((EGLDisplay)0)
63 #endif /* no HAVE_CAIRO_EGL */
65 #include <xkbcommon/xkbcommon.h>
66 #include <wayland-cursor.h>
68 #include <linux/input.h>
69 #include <wayland-client.h>
70 #include "../shared/cairo-util.h"
71 #include "text-cursor-position-client-protocol.h"
72 #include "workspaces-client-protocol.h"
73 #include "../shared/os-compatibility.h"
87 struct wl_display *display;
88 struct wl_registry *registry;
89 struct wl_compositor *compositor;
90 struct wl_shell *shell;
92 struct wl_data_device_manager *data_device_manager;
93 struct text_cursor_position *text_cursor_position;
94 struct workspace_manager *workspace_manager;
96 EGLConfig argb_config;
98 cairo_device_t *argb_device;
102 uint32_t display_fd_events;
103 struct task display_task;
106 struct wl_list deferred_list;
110 struct wl_list global_list;
111 struct wl_list window_list;
112 struct wl_list input_list;
113 struct wl_list output_list;
117 struct wl_cursor_theme *cursor_theme;
118 struct wl_cursor **cursors;
120 display_output_handler_t output_configure_handler;
121 display_global_handler_t global_handler;
125 struct xkb_context *xkb_context;
128 uint32_t workspace_count;
130 /* A hack to get text extents for tooltips */
131 cairo_surface_t *dummy_surface;
132 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 surface size.
156 * If resize_hint is non-zero, the user is doing continuous resizing.
157 * Returns the Cairo surface to draw to.
159 cairo_surface_t *(*prepare)(struct toysurface *base, int dx, int dy,
160 int width, int height, int resize_hint);
163 * Post the surface to the server, returning the server allocation
164 * rectangle. The Cairo surface from prepare() must be destroyed
165 * after calling this.
167 void (*swap)(struct toysurface *base,
168 struct rectangle *server_allocation);
171 * Make the toysurface current with the given EGL context.
172 * Returns 0 on success, and negative of failure.
174 int (*acquire)(struct toysurface *base, EGLContext ctx);
177 * Release the toysurface from the EGL context, returning control
180 void (*release)(struct toysurface *base);
183 * Destroy the toysurface, including the Cairo surface, any
184 * backing storage, and the Wayland protocol objects.
186 void (*destroy)(struct toysurface *base);
190 struct display *display;
191 struct window *parent;
192 struct wl_list window_output_list;
193 struct wl_surface *surface;
194 struct wl_shell_surface *shell_surface;
195 struct wl_region *input_region;
196 struct wl_region *opaque_region;
198 struct rectangle allocation, saved_allocation, server_allocation;
199 struct rectangle min_allocation;
200 struct rectangle pending_allocation;
203 int redraw_scheduled;
205 struct task redraw_task;
211 enum window_buffer_type buffer_type;
212 enum wl_output_transform buffer_transform;
213 struct toysurface *toysurface;
214 cairo_surface_t *cairo_surface;
218 window_key_handler_t key_handler;
219 window_keyboard_focus_handler_t keyboard_focus_handler;
220 window_data_handler_t data_handler;
221 window_drop_handler_t drop_handler;
222 window_close_handler_t close_handler;
223 window_fullscreen_handler_t fullscreen_handler;
224 window_output_handler_t output_handler;
226 struct wl_callback *frame_cb;
229 struct widget *widget;
236 struct window *window;
237 struct tooltip *tooltip;
238 struct wl_list child_list;
240 struct rectangle allocation;
241 widget_resize_handler_t resize_handler;
242 widget_redraw_handler_t redraw_handler;
243 widget_enter_handler_t enter_handler;
244 widget_leave_handler_t leave_handler;
245 widget_motion_handler_t motion_handler;
246 widget_button_handler_t button_handler;
247 widget_axis_handler_t axis_handler;
254 struct display *display;
255 struct wl_seat *seat;
256 struct wl_pointer *pointer;
257 struct wl_keyboard *keyboard;
258 struct window *pointer_focus;
259 struct window *keyboard_focus;
261 uint32_t cursor_anim_start;
262 struct wl_callback *cursor_frame_cb;
263 struct wl_surface *pointer_surface;
265 uint32_t pointer_enter_serial;
266 uint32_t cursor_serial;
270 struct widget *focus_widget;
272 uint32_t grab_button;
274 struct wl_data_device *data_device;
275 struct data_offer *drag_offer;
276 struct data_offer *selection_offer;
279 struct xkb_keymap *keymap;
280 struct xkb_state *state;
281 xkb_mod_mask_t control_mask;
282 xkb_mod_mask_t alt_mask;
283 xkb_mod_mask_t shift_mask;
286 struct task repeat_task;
290 uint32_t repeat_time;
294 struct display *display;
295 struct wl_output *output;
296 struct rectangle allocation;
300 display_output_handler_t destroy_handler;
304 enum frame_button_action {
305 FRAME_BUTTON_NULL = 0,
306 FRAME_BUTTON_ICON = 1,
307 FRAME_BUTTON_CLOSE = 2,
308 FRAME_BUTTON_MINIMIZE = 3,
309 FRAME_BUTTON_MAXIMIZE = 4,
312 enum frame_button_pointer {
313 FRAME_BUTTON_DEFAULT = 0,
314 FRAME_BUTTON_OVER = 1,
315 FRAME_BUTTON_ACTIVE = 2,
318 enum frame_button_align {
319 FRAME_BUTTON_RIGHT = 0,
320 FRAME_BUTTON_LEFT = 1,
323 enum frame_button_decoration {
324 FRAME_BUTTON_NONE = 0,
325 FRAME_BUTTON_FANCY = 1,
328 struct frame_button {
329 struct widget *widget;
331 cairo_surface_t *icon;
332 enum frame_button_action type;
333 enum frame_button_pointer state;
334 struct wl_list link; /* buttons_list */
335 enum frame_button_align align;
336 enum frame_button_decoration decoration;
340 struct widget *widget;
341 struct widget *child;
342 struct wl_list buttons_list;
346 struct window *window;
347 struct widget *widget;
349 const char **entries;
358 struct widget *parent;
359 struct window *window;
360 struct widget *widget;
362 struct task tooltip_task;
368 struct wl_shm_pool *pool;
375 CURSOR_DEFAULT = 100,
379 enum window_location {
381 WINDOW_RESIZING_TOP = 1,
382 WINDOW_RESIZING_BOTTOM = 2,
383 WINDOW_RESIZING_LEFT = 4,
384 WINDOW_RESIZING_TOP_LEFT = 5,
385 WINDOW_RESIZING_BOTTOM_LEFT = 6,
386 WINDOW_RESIZING_RIGHT = 8,
387 WINDOW_RESIZING_TOP_RIGHT = 9,
388 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
389 WINDOW_RESIZING_MASK = 15,
390 WINDOW_EXTERIOR = 16,
391 WINDOW_TITLEBAR = 17,
392 WINDOW_CLIENT_AREA = 18,
395 static const cairo_user_data_key_t shm_surface_data_key;
397 #ifdef HAVE_CAIRO_EGL
399 struct egl_window_surface {
400 struct toysurface base;
401 cairo_surface_t *cairo_surface;
402 struct display *display;
403 struct wl_surface *surface;
404 struct wl_egl_window *egl_window;
405 EGLSurface egl_surface;
408 static struct egl_window_surface *
409 to_egl_window_surface(struct toysurface *base)
411 return container_of(base, struct egl_window_surface, base);
414 static cairo_surface_t *
415 egl_window_surface_prepare(struct toysurface *base, int dx, int dy,
416 int width, int height, int resize_hint)
418 struct egl_window_surface *surface = to_egl_window_surface(base);
420 wl_egl_window_resize(surface->egl_window, width, height, dx, dy);
421 cairo_gl_surface_set_size(surface->cairo_surface, width, height);
423 return cairo_surface_reference(surface->cairo_surface);
427 egl_window_surface_swap(struct toysurface *base,
428 struct rectangle *server_allocation)
430 struct egl_window_surface *surface = to_egl_window_surface(base);
432 cairo_gl_surface_swapbuffers(surface->cairo_surface);
433 wl_egl_window_get_attached_size(surface->egl_window,
434 &server_allocation->width,
435 &server_allocation->height);
439 egl_window_surface_acquire(struct toysurface *base, EGLContext ctx)
441 struct egl_window_surface *surface = to_egl_window_surface(base);
442 cairo_device_t *device;
444 device = cairo_surface_get_device(surface->cairo_surface);
449 if (device == surface->display->argb_device)
450 ctx = surface->display->argb_ctx;
455 cairo_device_flush(device);
456 cairo_device_acquire(device);
457 if (!eglMakeCurrent(surface->display->dpy, surface->egl_surface,
458 surface->egl_surface, ctx))
459 fprintf(stderr, "failed to make surface current\n");
465 egl_window_surface_release(struct toysurface *base)
467 struct egl_window_surface *surface = to_egl_window_surface(base);
468 cairo_device_t *device;
470 device = cairo_surface_get_device(surface->cairo_surface);
474 if (!eglMakeCurrent(surface->display->dpy, NULL, NULL,
475 surface->display->argb_ctx))
476 fprintf(stderr, "failed to make context current\n");
478 cairo_device_release(device);
482 egl_window_surface_destroy(struct toysurface *base)
484 struct egl_window_surface *surface = to_egl_window_surface(base);
485 struct display *d = surface->display;
487 cairo_surface_destroy(surface->cairo_surface);
488 eglDestroySurface(d->dpy, surface->egl_surface);
489 wl_egl_window_destroy(surface->egl_window);
490 surface->surface = NULL;
495 static struct toysurface *
496 egl_window_surface_create(struct display *display,
497 struct wl_surface *wl_surface,
499 struct rectangle *rectangle)
501 struct egl_window_surface *surface;
503 if (display->dpy == EGL_NO_DISPLAY)
506 surface = calloc(1, sizeof *surface);
510 surface->base.prepare = egl_window_surface_prepare;
511 surface->base.swap = egl_window_surface_swap;
512 surface->base.acquire = egl_window_surface_acquire;
513 surface->base.release = egl_window_surface_release;
514 surface->base.destroy = egl_window_surface_destroy;
516 surface->display = display;
517 surface->surface = wl_surface;
519 surface->egl_window = wl_egl_window_create(surface->surface,
523 surface->egl_surface = eglCreateWindowSurface(display->dpy,
524 display->argb_config,
528 surface->cairo_surface =
529 cairo_gl_surface_create_for_egl(display->argb_device,
530 surface->egl_surface,
534 return &surface->base;
539 static struct toysurface *
540 egl_window_surface_create(struct display *display,
541 struct wl_surface *wl_surface,
543 struct rectangle *rectangle)
550 struct shm_surface_data {
551 struct wl_buffer *buffer;
552 struct shm_pool *pool;
556 display_get_buffer_for_surface(struct display *display,
557 cairo_surface_t *surface)
559 struct shm_surface_data *data;
561 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
567 shm_pool_destroy(struct shm_pool *pool);
570 shm_surface_data_destroy(void *p)
572 struct shm_surface_data *data = p;
574 wl_buffer_destroy(data->buffer);
576 shm_pool_destroy(data->pool);
581 static struct wl_shm_pool *
582 make_shm_pool(struct display *display, int size, void **data)
584 struct wl_shm_pool *pool;
587 fd = os_create_anonymous_file(size);
589 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
594 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
595 if (*data == MAP_FAILED) {
596 fprintf(stderr, "mmap failed: %m\n");
601 pool = wl_shm_create_pool(display->shm, fd, size);
608 static struct shm_pool *
609 shm_pool_create(struct display *display, size_t size)
611 struct shm_pool *pool = malloc(sizeof *pool);
616 pool->pool = make_shm_pool(display, size, &pool->data);
629 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
631 if (pool->used + size > pool->size)
634 *offset = pool->used;
637 return (char *) pool->data + *offset;
640 /* destroy the pool. this does not unmap the memory though */
642 shm_pool_destroy(struct shm_pool *pool)
644 munmap(pool->data, pool->size);
645 wl_shm_pool_destroy(pool->pool);
649 /* Start allocating from the beginning of the pool again */
651 shm_pool_reset(struct shm_pool *pool)
657 data_length_for_shm_surface(struct rectangle *rect)
661 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
663 return stride * rect->height;
666 static cairo_surface_t *
667 display_create_shm_surface_from_pool(struct display *display,
668 struct rectangle *rectangle,
669 uint32_t flags, struct shm_pool *pool)
671 struct shm_surface_data *data;
673 cairo_surface_t *surface;
674 int stride, length, offset;
677 data = malloc(sizeof *data);
681 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
683 length = stride * rectangle->height;
685 map = shm_pool_allocate(pool, length, &offset);
692 surface = cairo_image_surface_create_for_data (map,
698 cairo_surface_set_user_data(surface, &shm_surface_data_key,
699 data, shm_surface_data_destroy);
701 if (flags & SURFACE_OPAQUE)
702 format = WL_SHM_FORMAT_XRGB8888;
704 format = WL_SHM_FORMAT_ARGB8888;
706 data->buffer = wl_shm_pool_create_buffer(pool->pool, offset,
714 static cairo_surface_t *
715 display_create_shm_surface(struct display *display,
716 struct rectangle *rectangle, uint32_t flags,
717 struct shm_pool *alternate_pool,
718 struct shm_surface_data **data_ret)
720 struct shm_surface_data *data;
721 struct shm_pool *pool;
722 cairo_surface_t *surface;
724 if (alternate_pool) {
725 shm_pool_reset(alternate_pool);
726 surface = display_create_shm_surface_from_pool(display,
731 data = cairo_surface_get_user_data(surface,
732 &shm_surface_data_key);
737 pool = shm_pool_create(display,
738 data_length_for_shm_surface(rectangle));
743 display_create_shm_surface_from_pool(display, rectangle,
747 shm_pool_destroy(pool);
751 /* make sure we destroy the pool when the surface is destroyed */
752 data = cairo_surface_get_user_data(surface, &shm_surface_data_key);
763 check_size(struct rectangle *rect)
765 if (rect->width && rect->height)
768 fprintf(stderr, "tried to create surface of "
769 "width: %d, height: %d\n", rect->width, rect->height);
774 display_create_surface(struct display *display,
775 struct wl_surface *surface,
776 struct rectangle *rectangle,
779 if (check_size(rectangle) < 0)
782 assert(flags & SURFACE_SHM);
783 return display_create_shm_surface(display, rectangle, flags,
787 struct shm_surface_leaf {
788 cairo_surface_t *cairo_surface;
789 /* 'data' is automatically destroyed, when 'cairo_surface' is */
790 struct shm_surface_data *data;
792 struct shm_pool *resize_pool;
797 shm_surface_leaf_release(struct shm_surface_leaf *leaf)
799 if (leaf->cairo_surface)
800 cairo_surface_destroy(leaf->cairo_surface);
801 /* leaf->data already destroyed via cairo private */
803 if (leaf->resize_pool)
804 shm_pool_destroy(leaf->resize_pool);
808 struct toysurface base;
809 struct display *display;
810 struct wl_surface *surface;
814 struct shm_surface_leaf leaf[2];
815 struct shm_surface_leaf *current;
818 static struct shm_surface *
819 to_shm_surface(struct toysurface *base)
821 return container_of(base, struct shm_surface, base);
825 shm_surface_buffer_release(void *data, struct wl_buffer *buffer)
827 struct shm_surface_leaf *leaf = data;
832 static const struct wl_buffer_listener shm_surface_buffer_listener = {
833 shm_surface_buffer_release
836 static cairo_surface_t *
837 shm_surface_prepare(struct toysurface *base, int dx, int dy,
838 int width, int height, int resize_hint)
840 struct shm_surface *surface = to_shm_surface(base);
841 struct rectangle rect = { 0, 0, width, height };
842 struct shm_surface_leaf *leaf;
847 /* pick a free buffer from the two */
848 if (!surface->leaf[0].busy)
849 leaf = &surface->leaf[0];
850 else if (!surface->leaf[1].busy)
851 leaf = &surface->leaf[1];
853 fprintf(stderr, "%s: both buffers are held by the server.\n",
858 if (!resize_hint && leaf->resize_pool) {
859 cairo_surface_destroy(leaf->cairo_surface);
860 leaf->cairo_surface = NULL;
861 shm_pool_destroy(leaf->resize_pool);
862 leaf->resize_pool = NULL;
865 if (leaf->cairo_surface &&
866 cairo_image_surface_get_width(leaf->cairo_surface) == width &&
867 cairo_image_surface_get_height(leaf->cairo_surface) == height)
870 if (leaf->cairo_surface)
871 cairo_surface_destroy(leaf->cairo_surface);
873 if (resize_hint && !leaf->resize_pool) {
874 /* Create a big pool to allocate from, while continuously
875 * resizing. Mmapping a new pool in the server
876 * is relatively expensive, so reusing a pool performs
877 * better, but may temporarily reserve unneeded memory.
879 /* We should probably base this number on the output size. */
880 leaf->resize_pool = shm_pool_create(surface->display,
884 leaf->cairo_surface =
885 display_create_shm_surface(surface->display, &rect,
889 wl_buffer_add_listener(leaf->data->buffer,
890 &shm_surface_buffer_listener, leaf);
893 surface->current = leaf;
895 return cairo_surface_reference(leaf->cairo_surface);
899 shm_surface_swap(struct toysurface *base,
900 struct rectangle *server_allocation)
902 struct shm_surface *surface = to_shm_surface(base);
903 struct shm_surface_leaf *leaf = surface->current;
905 server_allocation->width =
906 cairo_image_surface_get_width(leaf->cairo_surface);
907 server_allocation->height =
908 cairo_image_surface_get_height(leaf->cairo_surface);
910 wl_surface_attach(surface->surface, leaf->data->buffer,
911 surface->dx, surface->dy);
912 wl_surface_damage(surface->surface, 0, 0,
913 server_allocation->width, server_allocation->height);
914 wl_surface_commit(surface->surface);
917 surface->current = NULL;
921 shm_surface_acquire(struct toysurface *base, EGLContext ctx)
927 shm_surface_release(struct toysurface *base)
932 shm_surface_destroy(struct toysurface *base)
934 struct shm_surface *surface = to_shm_surface(base);
936 shm_surface_leaf_release(&surface->leaf[0]);
937 shm_surface_leaf_release(&surface->leaf[1]);
942 static struct toysurface *
943 shm_surface_create(struct display *display, struct wl_surface *wl_surface,
944 uint32_t flags, struct rectangle *rectangle)
946 struct shm_surface *surface;
948 surface = calloc(1, sizeof *surface);
952 surface->base.prepare = shm_surface_prepare;
953 surface->base.swap = shm_surface_swap;
954 surface->base.acquire = shm_surface_acquire;
955 surface->base.release = shm_surface_release;
956 surface->base.destroy = shm_surface_destroy;
958 surface->display = display;
959 surface->surface = wl_surface;
960 surface->flags = flags;
962 return &surface->base;
966 * The following correspondences between file names and cursors was copied
967 * from: https://bugs.kde.org/attachment.cgi?id=67313
970 static const char *bottom_left_corners[] = {
971 "bottom_left_corner",
975 static const char *bottom_right_corners[] = {
976 "bottom_right_corner",
980 static const char *bottom_sides[] = {
985 static const char *grabbings[] = {
988 "208530c400c041818281048008011002"
991 static const char *left_ptrs[] = {
998 static const char *left_sides[] = {
1003 static const char *right_sides[] = {
1008 static const char *top_left_corners[] = {
1013 static const char *top_right_corners[] = {
1018 static const char *top_sides[] = {
1023 static const char *xterms[] = {
1029 static const char *hand1s[] = {
1033 "e29285e634086352946a0e7090d73106"
1036 static const char *watches[] = {
1039 "0426c94ea35c87780ff01dc239897213"
1042 struct cursor_alternatives {
1047 static const struct cursor_alternatives cursors[] = {
1048 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
1049 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
1050 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
1051 {grabbings, ARRAY_LENGTH(grabbings)},
1052 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
1053 {left_sides, ARRAY_LENGTH(left_sides)},
1054 {right_sides, ARRAY_LENGTH(right_sides)},
1055 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
1056 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
1057 {top_sides, ARRAY_LENGTH(top_sides)},
1058 {xterms, ARRAY_LENGTH(xterms)},
1059 {hand1s, ARRAY_LENGTH(hand1s)},
1060 {watches, ARRAY_LENGTH(watches)},
1064 create_cursors(struct display *display)
1069 struct wl_cursor *cursor;
1070 struct config_key shell_keys[] = {
1071 { "cursor-theme", CONFIG_KEY_STRING, &theme },
1073 struct config_section cs[] = {
1074 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
1077 config_file = config_file_path("weston.ini");
1078 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
1081 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
1083 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
1085 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
1087 for (j = 0; !cursor && j < cursors[i].count; ++j)
1088 cursor = wl_cursor_theme_get_cursor(
1089 display->cursor_theme, cursors[i].names[j]);
1092 fprintf(stderr, "could not load cursor '%s'\n",
1093 cursors[i].names[0]);
1095 display->cursors[i] = cursor;
1100 destroy_cursors(struct display *display)
1102 wl_cursor_theme_destroy(display->cursor_theme);
1103 free(display->cursors);
1106 struct wl_cursor_image *
1107 display_get_pointer_image(struct display *display, int pointer)
1109 struct wl_cursor *cursor = display->cursors[pointer];
1111 return cursor ? cursor->images[0] : NULL;
1115 window_get_resize_dx_dy(struct window *window, int *x, int *y)
1117 if (window->resize_edges & WINDOW_RESIZING_LEFT)
1118 *x = window->server_allocation.width - window->allocation.width;
1122 if (window->resize_edges & WINDOW_RESIZING_TOP)
1123 *y = window->server_allocation.height -
1124 window->allocation.height;
1128 window->resize_edges = 0;
1132 window_attach_surface(struct window *window)
1134 struct display *display = window->display;
1136 if (window->type == TYPE_NONE) {
1137 window->type = TYPE_TOPLEVEL;
1139 wl_shell_surface_set_toplevel(window->shell_surface);
1142 if (window->opaque_region) {
1143 wl_surface_set_opaque_region(window->surface,
1144 window->opaque_region);
1145 wl_region_destroy(window->opaque_region);
1146 window->opaque_region = NULL;
1149 if (window->input_region) {
1150 wl_surface_set_input_region(window->surface,
1151 window->input_region);
1152 wl_region_destroy(window->input_region);
1153 window->input_region = NULL;
1156 window->toysurface->swap(window->toysurface,
1157 &window->server_allocation);
1161 window_has_focus(struct window *window)
1163 return window->focus_count > 0;
1167 window_flush(struct window *window)
1169 if (!window->cairo_surface)
1172 window_attach_surface(window);
1173 cairo_surface_destroy(window->cairo_surface);
1174 window->cairo_surface = NULL;
1178 window_get_display(struct window *window)
1180 return window->display;
1184 window_create_surface(struct window *window)
1186 struct rectangle allocation = window->allocation;
1190 if (!window->transparent)
1191 flags = SURFACE_OPAQUE;
1193 switch (window->buffer_transform) {
1194 case WL_OUTPUT_TRANSFORM_90:
1195 case WL_OUTPUT_TRANSFORM_270:
1196 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1197 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1198 allocation.width = window->allocation.height;
1199 allocation.height = window->allocation.width;
1205 if (!window->toysurface &&
1206 window->buffer_type == WINDOW_BUFFER_TYPE_EGL_WINDOW &&
1207 window->display->dpy) {
1208 window->toysurface =
1209 egl_window_surface_create(window->display,
1210 window->surface, flags,
1214 if (!window->toysurface)
1215 window->toysurface = shm_surface_create(window->display,
1216 window->surface, flags,
1219 window_get_resize_dx_dy(window, &dx, &dy);
1220 window->cairo_surface =
1221 window->toysurface->prepare(window->toysurface, dx, dy,
1228 window_get_buffer_transform(struct window *window)
1230 return window->buffer_transform;
1234 window_set_buffer_transform(struct window *window,
1235 enum wl_output_transform transform)
1237 window->buffer_transform = transform;
1238 wl_surface_set_buffer_transform(window->surface, transform);
1241 static void frame_destroy(struct frame *frame);
1244 window_destroy(struct window *window)
1246 struct display *display = window->display;
1247 struct input *input;
1248 struct window_output *window_output;
1249 struct window_output *window_output_tmp;
1251 if (window->redraw_scheduled)
1252 wl_list_remove(&window->redraw_task.link);
1254 wl_list_for_each(input, &display->input_list, link) {
1255 if (input->pointer_focus == window)
1256 input->pointer_focus = NULL;
1257 if (input->keyboard_focus == window)
1258 input->keyboard_focus = NULL;
1259 if (input->focus_widget &&
1260 input->focus_widget->window == window)
1261 input->focus_widget = NULL;
1264 wl_list_for_each_safe(window_output, window_output_tmp,
1265 &window->window_output_list, link) {
1266 free (window_output);
1269 if (window->input_region)
1270 wl_region_destroy(window->input_region);
1271 if (window->opaque_region)
1272 wl_region_destroy(window->opaque_region);
1275 frame_destroy(window->frame);
1277 if (window->shell_surface)
1278 wl_shell_surface_destroy(window->shell_surface);
1279 wl_surface_destroy(window->surface);
1280 wl_list_remove(&window->link);
1282 if (window->toysurface)
1283 window->toysurface->destroy(window->toysurface);
1285 if (window->frame_cb)
1286 wl_callback_destroy(window->frame_cb);
1287 free(window->title);
1291 static struct widget *
1292 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1294 struct widget *child, *target;
1296 wl_list_for_each(child, &widget->child_list, link) {
1297 target = widget_find_widget(child, x, y);
1302 if (widget->allocation.x <= x &&
1303 x < widget->allocation.x + widget->allocation.width &&
1304 widget->allocation.y <= y &&
1305 y < widget->allocation.y + widget->allocation.height) {
1312 static struct widget *
1313 widget_create(struct window *window, void *data)
1315 struct widget *widget;
1317 widget = malloc(sizeof *widget);
1318 memset(widget, 0, sizeof *widget);
1319 widget->window = window;
1320 widget->user_data = data;
1321 widget->allocation = window->allocation;
1322 wl_list_init(&widget->child_list);
1324 widget->tooltip = NULL;
1325 widget->tooltip_count = 0;
1331 window_add_widget(struct window *window, void *data)
1333 window->widget = widget_create(window, data);
1334 wl_list_init(&window->widget->link);
1336 return window->widget;
1340 widget_add_widget(struct widget *parent, void *data)
1342 struct widget *widget;
1344 widget = widget_create(parent->window, data);
1345 wl_list_insert(parent->child_list.prev, &widget->link);
1351 widget_destroy(struct widget *widget)
1353 struct display *display = widget->window->display;
1354 struct input *input;
1356 if (widget->tooltip) {
1357 free(widget->tooltip);
1358 widget->tooltip = NULL;
1361 wl_list_for_each(input, &display->input_list, link) {
1362 if (input->focus_widget == widget)
1363 input->focus_widget = NULL;
1366 wl_list_remove(&widget->link);
1371 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1373 *allocation = widget->allocation;
1377 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1379 widget->allocation.width = width;
1380 widget->allocation.height = height;
1384 widget_set_allocation(struct widget *widget,
1385 int32_t x, int32_t y, int32_t width, int32_t height)
1387 widget->allocation.x = x;
1388 widget->allocation.y = y;
1389 widget_set_size(widget, width, height);
1393 widget_set_transparent(struct widget *widget, int transparent)
1395 widget->opaque = !transparent;
1399 widget_get_user_data(struct widget *widget)
1401 return widget->user_data;
1405 widget_set_resize_handler(struct widget *widget,
1406 widget_resize_handler_t handler)
1408 widget->resize_handler = handler;
1412 widget_set_redraw_handler(struct widget *widget,
1413 widget_redraw_handler_t handler)
1415 widget->redraw_handler = handler;
1419 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1421 widget->enter_handler = handler;
1425 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1427 widget->leave_handler = handler;
1431 widget_set_motion_handler(struct widget *widget,
1432 widget_motion_handler_t handler)
1434 widget->motion_handler = handler;
1438 widget_set_button_handler(struct widget *widget,
1439 widget_button_handler_t handler)
1441 widget->button_handler = handler;
1445 widget_set_axis_handler(struct widget *widget,
1446 widget_axis_handler_t handler)
1448 widget->axis_handler = handler;
1452 widget_schedule_redraw(struct widget *widget)
1454 window_schedule_redraw(widget->window);
1458 window_get_surface(struct window *window)
1460 return cairo_surface_reference(window->cairo_surface);
1464 window_get_wl_surface(struct window *window)
1466 return window->surface;
1469 struct wl_shell_surface *
1470 window_get_wl_shell_surface(struct window *window)
1472 return window->shell_surface;
1476 tooltip_redraw_handler(struct widget *widget, void *data)
1479 const int32_t r = 3;
1480 struct tooltip *tooltip = data;
1481 int32_t width, height;
1482 struct window *window = widget->window;
1484 cr = cairo_create(window->cairo_surface);
1485 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1486 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1489 width = window->allocation.width;
1490 height = window->allocation.height;
1491 rounded_rect(cr, 0, 0, width, height, r);
1493 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1494 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1497 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1498 cairo_move_to(cr, 10, 16);
1499 cairo_show_text(cr, tooltip->entry);
1503 static cairo_text_extents_t
1504 get_text_extents(struct tooltip *tooltip)
1507 cairo_text_extents_t extents;
1509 /* Use the dummy_surface because tooltip's surface was not
1510 * created yet, and parent does not have a valid surface
1511 * outside repaint, either.
1513 cr = cairo_create(tooltip->window->display->dummy_surface);
1514 cairo_text_extents(cr, tooltip->entry, &extents);
1521 window_create_tooltip(struct tooltip *tooltip)
1523 struct widget *parent = tooltip->parent;
1524 struct display *display = parent->window->display;
1525 struct window *window;
1526 const int offset_y = 27;
1527 const int margin = 3;
1528 cairo_text_extents_t extents;
1530 if (tooltip->widget)
1533 window = window_create_transient(display, parent->window, tooltip->x,
1534 tooltip->y + offset_y,
1535 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1539 tooltip->window = window;
1540 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1542 extents = get_text_extents(tooltip);
1543 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1544 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1550 widget_destroy_tooltip(struct widget *parent)
1552 struct tooltip *tooltip = parent->tooltip;
1554 parent->tooltip_count = 0;
1558 if (tooltip->widget) {
1559 widget_destroy(tooltip->widget);
1560 window_destroy(tooltip->window);
1561 tooltip->widget = NULL;
1562 tooltip->window = NULL;
1565 close(tooltip->tooltip_fd);
1566 free(tooltip->entry);
1568 parent->tooltip = NULL;
1572 tooltip_func(struct task *task, uint32_t events)
1574 struct tooltip *tooltip =
1575 container_of(task, struct tooltip, tooltip_task);
1578 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1580 window_create_tooltip(tooltip);
1583 #define TOOLTIP_TIMEOUT 500
1585 tooltip_timer_reset(struct tooltip *tooltip)
1587 struct itimerspec its;
1589 its.it_interval.tv_sec = 0;
1590 its.it_interval.tv_nsec = 0;
1591 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1592 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1593 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1594 fprintf(stderr, "could not set timerfd\n: %m");
1602 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1604 struct tooltip *tooltip = parent->tooltip;
1606 parent->tooltip_count++;
1610 tooltip_timer_reset(tooltip);
1614 /* the handler might be triggered too fast via input device motion, so
1615 * we need this check here to make sure tooltip is fully initialized */
1616 if (parent->tooltip_count > 1)
1619 tooltip = malloc(sizeof *tooltip);
1623 parent->tooltip = tooltip;
1624 tooltip->parent = parent;
1625 tooltip->widget = NULL;
1626 tooltip->window = NULL;
1629 tooltip->entry = strdup(entry);
1630 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1631 if (tooltip->tooltip_fd < 0) {
1632 fprintf(stderr, "could not create timerfd\n: %m");
1636 tooltip->tooltip_task.run = tooltip_func;
1637 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1638 EPOLLIN, &tooltip->tooltip_task);
1639 tooltip_timer_reset(tooltip);
1645 workspace_manager_state(void *data,
1646 struct workspace_manager *workspace_manager,
1650 struct display *display = data;
1652 display->workspace = current;
1653 display->workspace_count = count;
1656 static const struct workspace_manager_listener workspace_manager_listener = {
1657 workspace_manager_state
1661 frame_resize_handler(struct widget *widget,
1662 int32_t width, int32_t height, void *data)
1664 struct frame *frame = data;
1665 struct widget *child = frame->child;
1666 struct rectangle allocation;
1667 struct display *display = widget->window->display;
1668 struct frame_button * button;
1669 struct theme *t = display->theme;
1670 int x_l, x_r, y, w, h;
1671 int decoration_width, decoration_height;
1672 int opaque_margin, shadow_margin;
1674 switch (widget->window->type) {
1675 case TYPE_FULLSCREEN:
1676 decoration_width = 0;
1677 decoration_height = 0;
1681 allocation.width = width;
1682 allocation.height = height;
1685 wl_list_for_each(button, &frame->buttons_list, link)
1686 button->widget->opaque = 1;
1688 case TYPE_MAXIMIZED:
1689 decoration_width = t->width * 2;
1690 decoration_height = t->width + t->titlebar_height;
1692 allocation.x = t->width;
1693 allocation.y = t->titlebar_height;
1694 allocation.width = width - decoration_width;
1695 allocation.height = height - decoration_height;
1699 wl_list_for_each(button, &frame->buttons_list, link)
1700 button->widget->opaque = 0;
1703 decoration_width = (t->width + t->margin) * 2;
1704 decoration_height = t->width +
1705 t->titlebar_height + t->margin * 2;
1707 allocation.x = t->width + t->margin;
1708 allocation.y = t->titlebar_height + t->margin;
1709 allocation.width = width - decoration_width;
1710 allocation.height = height - decoration_height;
1712 opaque_margin = t->margin + t->frame_radius;
1714 wl_list_for_each(button, &frame->buttons_list, link)
1715 button->widget->opaque = 0;
1719 widget_set_allocation(child, allocation.x, allocation.y,
1720 allocation.width, allocation.height);
1722 if (child->resize_handler)
1723 child->resize_handler(child,
1728 width = child->allocation.width + decoration_width;
1729 height = child->allocation.height + decoration_height;
1731 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1733 widget->window->input_region =
1734 wl_compositor_create_region(display->compositor);
1735 if (widget->window->type != TYPE_FULLSCREEN) {
1736 wl_region_add(widget->window->input_region,
1737 shadow_margin, shadow_margin,
1738 width - 2 * shadow_margin,
1739 height - 2 * shadow_margin);
1741 wl_region_add(widget->window->input_region,
1742 0, 0, width, height);
1745 widget_set_allocation(widget, 0, 0, width, height);
1748 wl_region_add(widget->window->opaque_region,
1749 opaque_margin, opaque_margin,
1750 widget->allocation.width - 2 * opaque_margin,
1751 widget->allocation.height - 2 * opaque_margin);
1753 /* frame internal buttons */
1754 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1755 x_l = t->width + shadow_margin;
1756 y = t->width + shadow_margin;
1757 wl_list_for_each(button, &frame->buttons_list, link) {
1758 const int button_padding = 4;
1759 w = cairo_image_surface_get_width(button->icon);
1760 h = cairo_image_surface_get_height(button->icon);
1762 if (button->decoration == FRAME_BUTTON_FANCY)
1765 if (button->align == FRAME_BUTTON_LEFT) {
1766 widget_set_allocation(button->widget,
1767 x_l, y , w + 1, h + 1);
1769 x_l += button_padding;
1772 widget_set_allocation(button->widget,
1773 x_r, y , w + 1, h + 1);
1774 x_r -= button_padding;
1780 frame_button_enter_handler(struct widget *widget,
1781 struct input *input, float x, float y, void *data)
1783 struct frame_button *frame_button = data;
1785 widget_schedule_redraw(frame_button->widget);
1786 frame_button->state = FRAME_BUTTON_OVER;
1788 return CURSOR_LEFT_PTR;
1792 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1794 struct frame_button *frame_button = data;
1796 widget_schedule_redraw(frame_button->widget);
1797 frame_button->state = FRAME_BUTTON_DEFAULT;
1801 frame_button_button_handler(struct widget *widget,
1802 struct input *input, uint32_t time,
1804 enum wl_pointer_button_state state, void *data)
1806 struct frame_button *frame_button = data;
1807 struct window *window = widget->window;
1808 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1810 if (button != BTN_LEFT)
1814 case WL_POINTER_BUTTON_STATE_PRESSED:
1815 frame_button->state = FRAME_BUTTON_ACTIVE;
1816 widget_schedule_redraw(frame_button->widget);
1818 if (frame_button->type == FRAME_BUTTON_ICON)
1819 window_show_frame_menu(window, input, time);
1821 case WL_POINTER_BUTTON_STATE_RELEASED:
1822 frame_button->state = FRAME_BUTTON_DEFAULT;
1823 widget_schedule_redraw(frame_button->widget);
1830 switch (frame_button->type) {
1831 case FRAME_BUTTON_CLOSE:
1832 if (window->close_handler)
1833 window->close_handler(window->parent,
1836 display_exit(window->display);
1838 case FRAME_BUTTON_MINIMIZE:
1839 fprintf(stderr,"Minimize stub\n");
1841 case FRAME_BUTTON_MAXIMIZE:
1842 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1845 /* Unknown operation */
1851 frame_button_motion_handler(struct widget *widget,
1852 struct input *input, uint32_t time,
1853 float x, float y, void *data)
1855 struct frame_button *frame_button = data;
1856 enum frame_button_pointer previous_button_state = frame_button->state;
1858 /* only track state for a pressed button */
1859 if (input->grab != widget)
1860 return CURSOR_LEFT_PTR;
1862 if (x > widget->allocation.x &&
1863 x < (widget->allocation.x + widget->allocation.width) &&
1864 y > widget->allocation.y &&
1865 y < (widget->allocation.y + widget->allocation.height)) {
1866 frame_button->state = FRAME_BUTTON_ACTIVE;
1868 frame_button->state = FRAME_BUTTON_DEFAULT;
1871 if (frame_button->state != previous_button_state)
1872 widget_schedule_redraw(frame_button->widget);
1874 return CURSOR_LEFT_PTR;
1878 frame_button_redraw_handler(struct widget *widget, void *data)
1880 struct frame_button *frame_button = data;
1882 int width, height, x, y;
1883 struct window *window = widget->window;
1885 x = widget->allocation.x;
1886 y = widget->allocation.y;
1887 width = widget->allocation.width;
1888 height = widget->allocation.height;
1897 cr = cairo_create(window->cairo_surface);
1899 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1900 cairo_set_line_width(cr, 1);
1902 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1903 cairo_rectangle (cr, x, y, 25, 16);
1905 cairo_stroke_preserve(cr);
1907 switch (frame_button->state) {
1908 case FRAME_BUTTON_DEFAULT:
1909 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1911 case FRAME_BUTTON_OVER:
1912 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1914 case FRAME_BUTTON_ACTIVE:
1915 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1924 cairo_set_source_surface(cr, frame_button->icon, x, y);
1930 static struct widget *
1931 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1932 enum frame_button_align align, enum frame_button_decoration style)
1934 struct frame_button *frame_button;
1935 const char *icon = data;
1937 frame_button = malloc (sizeof *frame_button);
1938 memset(frame_button, 0, sizeof *frame_button);
1940 frame_button->icon = cairo_image_surface_create_from_png(icon);
1941 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1942 frame_button->frame = frame;
1943 frame_button->type = type;
1944 frame_button->align = align;
1945 frame_button->decoration = style;
1947 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1949 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1950 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1951 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1952 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1953 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
1954 return frame_button->widget;
1958 frame_button_destroy(struct frame_button *frame_button)
1960 widget_destroy(frame_button->widget);
1961 wl_list_remove(&frame_button->link);
1962 cairo_surface_destroy(frame_button->icon);
1969 frame_redraw_handler(struct widget *widget, void *data)
1972 struct window *window = widget->window;
1973 struct theme *t = window->display->theme;
1976 if (window->type == TYPE_FULLSCREEN)
1979 cr = cairo_create(window->cairo_surface);
1981 if (window->focus_count)
1982 flags |= THEME_FRAME_ACTIVE;
1983 if (window->type == TYPE_MAXIMIZED)
1984 flags |= THEME_FRAME_MAXIMIZED;
1985 theme_render_frame(t, cr, widget->allocation.width,
1986 widget->allocation.height, window->title, flags);
1992 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1994 struct theme *t = frame->widget->window->display->theme;
1995 struct window *window = frame->widget->window;
1998 location = theme_get_location(t, input->sx, input->sy,
1999 frame->widget->allocation.width,
2000 frame->widget->allocation.height,
2001 window->type == TYPE_MAXIMIZED ?
2002 THEME_FRAME_MAXIMIZED : 0);
2005 case THEME_LOCATION_RESIZING_TOP:
2007 case THEME_LOCATION_RESIZING_BOTTOM:
2008 return CURSOR_BOTTOM;
2009 case THEME_LOCATION_RESIZING_LEFT:
2011 case THEME_LOCATION_RESIZING_RIGHT:
2012 return CURSOR_RIGHT;
2013 case THEME_LOCATION_RESIZING_TOP_LEFT:
2014 return CURSOR_TOP_LEFT;
2015 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2016 return CURSOR_TOP_RIGHT;
2017 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2018 return CURSOR_BOTTOM_LEFT;
2019 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2020 return CURSOR_BOTTOM_RIGHT;
2021 case THEME_LOCATION_EXTERIOR:
2022 case THEME_LOCATION_TITLEBAR:
2024 return CURSOR_LEFT_PTR;
2029 frame_menu_func(struct window *window, int index, void *data)
2031 struct display *display;
2035 if (window->close_handler)
2036 window->close_handler(window->parent,
2039 display_exit(window->display);
2041 case 1: /* move to workspace above */
2042 display = window->display;
2043 if (display->workspace > 0)
2044 workspace_manager_move_surface(display->workspace_manager,
2046 display->workspace - 1);
2048 case 2: /* move to workspace below */
2049 display = window->display;
2050 if (display->workspace < display->workspace_count - 1)
2051 workspace_manager_move_surface(display->workspace_manager,
2053 display->workspace + 1);
2055 case 3: /* fullscreen */
2056 /* we don't have a way to get out of fullscreen for now */
2057 if (window->fullscreen_handler)
2058 window->fullscreen_handler(window, window->user_data);
2064 window_show_frame_menu(struct window *window,
2065 struct input *input, uint32_t time)
2070 static const char *entries[] = {
2072 "Move to workspace above", "Move to workspace below",
2076 if (window->fullscreen_handler)
2077 count = ARRAY_LENGTH(entries);
2079 count = ARRAY_LENGTH(entries) - 1;
2081 input_get_position(input, &x, &y);
2082 window_show_menu(window->display, input, time, window,
2083 x - 10, y - 10, frame_menu_func, entries, count);
2087 frame_enter_handler(struct widget *widget,
2088 struct input *input, float x, float y, void *data)
2090 return frame_get_pointer_image_for_location(data, input);
2094 frame_motion_handler(struct widget *widget,
2095 struct input *input, uint32_t time,
2096 float x, float y, void *data)
2098 return frame_get_pointer_image_for_location(data, input);
2102 frame_button_handler(struct widget *widget,
2103 struct input *input, uint32_t time,
2104 uint32_t button, enum wl_pointer_button_state state,
2108 struct frame *frame = data;
2109 struct window *window = widget->window;
2110 struct display *display = window->display;
2113 location = theme_get_location(display->theme, input->sx, input->sy,
2114 frame->widget->allocation.width,
2115 frame->widget->allocation.height,
2116 window->type == TYPE_MAXIMIZED ?
2117 THEME_FRAME_MAXIMIZED : 0);
2119 if (window->display->shell && button == BTN_LEFT &&
2120 state == WL_POINTER_BUTTON_STATE_PRESSED) {
2122 case THEME_LOCATION_TITLEBAR:
2123 if (!window->shell_surface)
2125 input_ungrab(input);
2126 wl_shell_surface_move(window->shell_surface,
2127 input_get_seat(input),
2130 case THEME_LOCATION_RESIZING_TOP:
2131 case THEME_LOCATION_RESIZING_BOTTOM:
2132 case THEME_LOCATION_RESIZING_LEFT:
2133 case THEME_LOCATION_RESIZING_RIGHT:
2134 case THEME_LOCATION_RESIZING_TOP_LEFT:
2135 case THEME_LOCATION_RESIZING_TOP_RIGHT:
2136 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
2137 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
2138 if (!window->shell_surface)
2140 input_ungrab(input);
2142 window->resizing = 1;
2143 wl_shell_surface_resize(window->shell_surface,
2144 input_get_seat(input),
2145 display->serial, location);
2148 } else if (button == BTN_RIGHT &&
2149 state == WL_POINTER_BUTTON_STATE_PRESSED) {
2150 window_show_frame_menu(window, input, time);
2155 frame_create(struct window *window, void *data)
2157 struct frame *frame;
2159 frame = malloc(sizeof *frame);
2160 memset(frame, 0, sizeof *frame);
2162 frame->widget = window_add_widget(window, frame);
2163 frame->child = widget_add_widget(frame->widget, data);
2165 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
2166 widget_set_resize_handler(frame->widget, frame_resize_handler);
2167 widget_set_enter_handler(frame->widget, frame_enter_handler);
2168 widget_set_motion_handler(frame->widget, frame_motion_handler);
2169 widget_set_button_handler(frame->widget, frame_button_handler);
2171 /* Create empty list for frame buttons */
2172 wl_list_init(&frame->buttons_list);
2174 frame_button_create(frame, DATADIR "/weston/icon_window.png",
2175 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
2177 frame_button_create(frame, DATADIR "/weston/sign_close.png",
2178 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2180 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
2181 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2183 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
2184 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
2186 window->frame = frame;
2188 return frame->child;
2192 frame_set_child_size(struct widget *widget, int child_width, int child_height)
2194 struct display *display = widget->window->display;
2195 struct theme *t = display->theme;
2196 int decoration_width, decoration_height;
2198 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
2200 if (widget->window->type != TYPE_FULLSCREEN) {
2201 decoration_width = (t->width + margin) * 2;
2202 decoration_height = t->width +
2203 t->titlebar_height + margin * 2;
2205 width = child_width + decoration_width;
2206 height = child_height + decoration_height;
2208 width = child_width;
2209 height = child_height;
2212 window_schedule_resize(widget->window, width, height);
2216 frame_destroy(struct frame *frame)
2218 struct frame_button *button, *tmp;
2220 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
2221 frame_button_destroy(button);
2223 /* frame->child must be destroyed by the application */
2224 widget_destroy(frame->widget);
2229 input_set_focus_widget(struct input *input, struct widget *focus,
2232 struct widget *old, *widget;
2233 int pointer = CURSOR_LEFT_PTR;
2235 if (focus == input->focus_widget)
2238 old = input->focus_widget;
2242 widget = input->grab;
2243 if (widget->leave_handler)
2244 widget->leave_handler(old, input, widget->user_data);
2245 input->focus_widget = NULL;
2251 widget = input->grab;
2252 input->focus_widget = focus;
2253 if (widget->enter_handler)
2254 pointer = widget->enter_handler(focus, input, x, y,
2257 input_set_pointer_image(input, pointer);
2262 input_grab(struct input *input, struct widget *widget, uint32_t button)
2264 input->grab = widget;
2265 input->grab_button = button;
2269 input_ungrab(struct input *input)
2271 struct widget *widget;
2274 if (input->pointer_focus) {
2275 widget = widget_find_widget(input->pointer_focus->widget,
2276 input->sx, input->sy);
2277 input_set_focus_widget(input, widget, input->sx, input->sy);
2282 input_remove_pointer_focus(struct input *input)
2284 struct window *window = input->pointer_focus;
2289 input_set_focus_widget(input, NULL, 0, 0);
2291 input->pointer_focus = NULL;
2292 input->current_cursor = CURSOR_UNSET;
2296 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2297 uint32_t serial, struct wl_surface *surface,
2298 wl_fixed_t sx_w, wl_fixed_t sy_w)
2300 struct input *input = data;
2301 struct window *window;
2302 struct widget *widget;
2303 float sx = wl_fixed_to_double(sx_w);
2304 float sy = wl_fixed_to_double(sy_w);
2307 /* enter event for a window we've just destroyed */
2311 input->display->serial = serial;
2312 input->pointer_enter_serial = serial;
2313 input->pointer_focus = wl_surface_get_user_data(surface);
2314 window = input->pointer_focus;
2316 if (window->resizing) {
2317 window->resizing = 0;
2318 /* Schedule a redraw to free the pool */
2319 window_schedule_redraw(window);
2325 widget = widget_find_widget(window->widget, sx, sy);
2326 input_set_focus_widget(input, widget, sx, sy);
2330 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2331 uint32_t serial, struct wl_surface *surface)
2333 struct input *input = data;
2335 input->display->serial = serial;
2336 input_remove_pointer_focus(input);
2340 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2341 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2343 struct input *input = data;
2344 struct window *window = input->pointer_focus;
2345 struct widget *widget;
2346 int cursor = CURSOR_LEFT_PTR;
2347 float sx = wl_fixed_to_double(sx_w);
2348 float sy = wl_fixed_to_double(sy_w);
2356 if (!(input->grab && input->grab_button)) {
2357 widget = widget_find_widget(window->widget, sx, sy);
2358 input_set_focus_widget(input, widget, sx, sy);
2362 widget = input->grab;
2364 widget = input->focus_widget;
2365 if (widget && widget->motion_handler)
2366 cursor = widget->motion_handler(input->focus_widget,
2367 input, time, sx, sy,
2370 input_set_pointer_image(input, cursor);
2374 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2375 uint32_t time, uint32_t button, uint32_t state_w)
2377 struct input *input = data;
2378 struct widget *widget;
2379 enum wl_pointer_button_state state = state_w;
2381 input->display->serial = serial;
2382 if (input->focus_widget && input->grab == NULL &&
2383 state == WL_POINTER_BUTTON_STATE_PRESSED)
2384 input_grab(input, input->focus_widget, button);
2386 widget = input->grab;
2387 if (widget && widget->button_handler)
2388 (*widget->button_handler)(widget,
2391 input->grab->user_data);
2393 if (input->grab && input->grab_button == button &&
2394 state == WL_POINTER_BUTTON_STATE_RELEASED)
2395 input_ungrab(input);
2399 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2400 uint32_t time, uint32_t axis, wl_fixed_t value)
2402 struct input *input = data;
2403 struct widget *widget;
2405 widget = input->focus_widget;
2407 widget = input->grab;
2408 if (widget && widget->axis_handler)
2409 (*widget->axis_handler)(widget,
2415 static const struct wl_pointer_listener pointer_listener = {
2416 pointer_handle_enter,
2417 pointer_handle_leave,
2418 pointer_handle_motion,
2419 pointer_handle_button,
2420 pointer_handle_axis,
2424 input_remove_keyboard_focus(struct input *input)
2426 struct window *window = input->keyboard_focus;
2427 struct itimerspec its;
2429 its.it_interval.tv_sec = 0;
2430 its.it_interval.tv_nsec = 0;
2431 its.it_value.tv_sec = 0;
2432 its.it_value.tv_nsec = 0;
2433 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2438 window->focus_count--;
2439 if (window->keyboard_focus_handler)
2440 (*window->keyboard_focus_handler)(window, NULL,
2443 input->keyboard_focus = NULL;
2447 keyboard_repeat_func(struct task *task, uint32_t events)
2449 struct input *input =
2450 container_of(task, struct input, repeat_task);
2451 struct window *window = input->keyboard_focus;
2454 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2455 /* If we change the timer between the fd becoming
2456 * readable and getting here, there'll be nothing to
2457 * read and we get EAGAIN. */
2460 if (window && window->key_handler) {
2461 (*window->key_handler)(window, input, input->repeat_time,
2462 input->repeat_key, input->repeat_sym,
2463 WL_KEYBOARD_KEY_STATE_PRESSED,
2469 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2470 uint32_t format, int fd, uint32_t size)
2472 struct input *input = data;
2480 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2485 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2486 if (map_str == MAP_FAILED) {
2491 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2493 XKB_KEYMAP_FORMAT_TEXT_V1,
2495 munmap(map_str, size);
2498 if (!input->xkb.keymap) {
2499 fprintf(stderr, "failed to compile keymap\n");
2503 input->xkb.state = xkb_state_new(input->xkb.keymap);
2504 if (!input->xkb.state) {
2505 fprintf(stderr, "failed to create XKB state\n");
2506 xkb_map_unref(input->xkb.keymap);
2507 input->xkb.keymap = NULL;
2511 input->xkb.control_mask =
2512 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2513 input->xkb.alt_mask =
2514 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2515 input->xkb.shift_mask =
2516 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2520 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2521 uint32_t serial, struct wl_surface *surface,
2522 struct wl_array *keys)
2524 struct input *input = data;
2525 struct window *window;
2527 input->display->serial = serial;
2528 input->keyboard_focus = wl_surface_get_user_data(surface);
2530 window = input->keyboard_focus;
2531 window->focus_count++;
2532 if (window->keyboard_focus_handler)
2533 (*window->keyboard_focus_handler)(window,
2534 input, window->user_data);
2538 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2539 uint32_t serial, struct wl_surface *surface)
2541 struct input *input = data;
2543 input->display->serial = serial;
2544 input_remove_keyboard_focus(input);
2548 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2549 uint32_t serial, uint32_t time, uint32_t key,
2552 struct input *input = data;
2553 struct window *window = input->keyboard_focus;
2554 uint32_t code, num_syms;
2555 enum wl_keyboard_key_state state = state_w;
2556 const xkb_keysym_t *syms;
2558 struct itimerspec its;
2560 input->display->serial = serial;
2562 if (!window || !input->xkb.state)
2565 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2567 sym = XKB_KEY_NoSymbol;
2571 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2572 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2573 window_set_maximized(window,
2574 window->type != TYPE_MAXIMIZED);
2575 } else if (sym == XKB_KEY_F11 &&
2576 window->fullscreen_handler &&
2577 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2578 window->fullscreen_handler(window, window->user_data);
2579 } else if (sym == XKB_KEY_F4 &&
2580 input->modifiers == MOD_ALT_MASK &&
2581 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2582 if (window->close_handler)
2583 window->close_handler(window->parent,
2586 display_exit(window->display);
2587 } else if (window->key_handler) {
2588 (*window->key_handler)(window, input, time, key,
2589 sym, state, window->user_data);
2592 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2593 key == input->repeat_key) {
2594 its.it_interval.tv_sec = 0;
2595 its.it_interval.tv_nsec = 0;
2596 its.it_value.tv_sec = 0;
2597 its.it_value.tv_nsec = 0;
2598 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2599 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2600 input->repeat_sym = sym;
2601 input->repeat_key = key;
2602 input->repeat_time = time;
2603 its.it_interval.tv_sec = 0;
2604 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2605 its.it_value.tv_sec = 0;
2606 its.it_value.tv_nsec = 400 * 1000 * 1000;
2607 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2612 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2613 uint32_t serial, uint32_t mods_depressed,
2614 uint32_t mods_latched, uint32_t mods_locked,
2617 struct input *input = data;
2618 xkb_mod_mask_t mask;
2620 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2621 mods_locked, 0, 0, group);
2622 mask = xkb_state_serialize_mods(input->xkb.state,
2623 XKB_STATE_DEPRESSED |
2625 input->modifiers = 0;
2626 if (mask & input->xkb.control_mask)
2627 input->modifiers |= MOD_CONTROL_MASK;
2628 if (mask & input->xkb.alt_mask)
2629 input->modifiers |= MOD_ALT_MASK;
2630 if (mask & input->xkb.shift_mask)
2631 input->modifiers |= MOD_SHIFT_MASK;
2634 static const struct wl_keyboard_listener keyboard_listener = {
2635 keyboard_handle_keymap,
2636 keyboard_handle_enter,
2637 keyboard_handle_leave,
2638 keyboard_handle_key,
2639 keyboard_handle_modifiers,
2643 seat_handle_capabilities(void *data, struct wl_seat *seat,
2644 enum wl_seat_capability caps)
2646 struct input *input = data;
2648 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2649 input->pointer = wl_seat_get_pointer(seat);
2650 wl_pointer_set_user_data(input->pointer, input);
2651 wl_pointer_add_listener(input->pointer, &pointer_listener,
2653 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2654 wl_pointer_destroy(input->pointer);
2655 input->pointer = NULL;
2658 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2659 input->keyboard = wl_seat_get_keyboard(seat);
2660 wl_keyboard_set_user_data(input->keyboard, input);
2661 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2663 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2664 wl_keyboard_destroy(input->keyboard);
2665 input->keyboard = NULL;
2669 static const struct wl_seat_listener seat_listener = {
2670 seat_handle_capabilities,
2674 input_get_position(struct input *input, int32_t *x, int32_t *y)
2681 input_get_display(struct input *input)
2683 return input->display;
2687 input_get_seat(struct input *input)
2693 input_get_modifiers(struct input *input)
2695 return input->modifiers;
2699 input_get_focus_widget(struct input *input)
2701 return input->focus_widget;
2705 struct wl_data_offer *offer;
2706 struct input *input;
2707 struct wl_array types;
2710 struct task io_task;
2718 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2720 struct data_offer *offer = data;
2723 p = wl_array_add(&offer->types, sizeof *p);
2727 static const struct wl_data_offer_listener data_offer_listener = {
2732 data_offer_destroy(struct data_offer *offer)
2737 if (offer->refcount == 0) {
2738 wl_data_offer_destroy(offer->offer);
2739 for (p = offer->types.data; *p; p++)
2741 wl_array_release(&offer->types);
2747 data_device_data_offer(void *data,
2748 struct wl_data_device *data_device,
2749 struct wl_data_offer *_offer)
2751 struct data_offer *offer;
2753 offer = malloc(sizeof *offer);
2755 wl_array_init(&offer->types);
2756 offer->refcount = 1;
2757 offer->input = data;
2758 offer->offer = _offer;
2759 wl_data_offer_add_listener(offer->offer,
2760 &data_offer_listener, offer);
2764 data_device_enter(void *data, struct wl_data_device *data_device,
2765 uint32_t serial, struct wl_surface *surface,
2766 wl_fixed_t x_w, wl_fixed_t y_w,
2767 struct wl_data_offer *offer)
2769 struct input *input = data;
2770 struct window *window;
2772 float x = wl_fixed_to_double(x_w);
2773 float y = wl_fixed_to_double(y_w);
2776 input->pointer_enter_serial = serial;
2777 window = wl_surface_get_user_data(surface);
2778 input->pointer_focus = window;
2781 input->drag_offer = wl_data_offer_get_user_data(offer);
2783 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2786 types_data = input->drag_offer->types.data;
2788 input->drag_offer = NULL;
2792 window = input->pointer_focus;
2793 if (window->data_handler)
2794 window->data_handler(window, input, x, y, types_data,
2799 data_device_leave(void *data, struct wl_data_device *data_device)
2801 struct input *input = data;
2803 if (input->drag_offer) {
2804 data_offer_destroy(input->drag_offer);
2805 input->drag_offer = NULL;
2810 data_device_motion(void *data, struct wl_data_device *data_device,
2811 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2813 struct input *input = data;
2814 struct window *window = input->pointer_focus;
2815 float x = wl_fixed_to_double(x_w);
2816 float y = wl_fixed_to_double(y_w);
2822 if (input->drag_offer)
2823 types_data = input->drag_offer->types.data;
2827 if (window->data_handler)
2828 window->data_handler(window, input, x, y, types_data,
2833 data_device_drop(void *data, struct wl_data_device *data_device)
2835 struct input *input = data;
2836 struct window *window = input->pointer_focus;
2838 if (window->drop_handler)
2839 window->drop_handler(window, input,
2840 input->sx, input->sy, window->user_data);
2844 data_device_selection(void *data,
2845 struct wl_data_device *wl_data_device,
2846 struct wl_data_offer *offer)
2848 struct input *input = data;
2851 if (input->selection_offer)
2852 data_offer_destroy(input->selection_offer);
2855 input->selection_offer = wl_data_offer_get_user_data(offer);
2856 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2859 input->selection_offer = NULL;
2863 static const struct wl_data_device_listener data_device_listener = {
2864 data_device_data_offer,
2869 data_device_selection
2873 input_set_pointer_image_index(struct input *input, int index)
2875 struct wl_buffer *buffer;
2876 struct wl_cursor *cursor;
2877 struct wl_cursor_image *image;
2879 if (!input->pointer)
2882 cursor = input->display->cursors[input->current_cursor];
2886 if (index >= (int) cursor->image_count) {
2887 fprintf(stderr, "cursor index out of range\n");
2891 image = cursor->images[index];
2892 buffer = wl_cursor_image_get_buffer(image);
2896 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
2897 input->pointer_surface,
2898 image->hotspot_x, image->hotspot_y);
2899 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2900 wl_surface_damage(input->pointer_surface, 0, 0,
2901 image->width, image->height);
2902 wl_surface_commit(input->pointer_surface);
2905 static const struct wl_callback_listener pointer_surface_listener;
2908 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2911 struct input *input = data;
2912 struct wl_cursor *cursor;
2916 assert(callback == input->cursor_frame_cb);
2917 wl_callback_destroy(callback);
2918 input->cursor_frame_cb = NULL;
2921 if (!input->pointer)
2924 if (input->current_cursor == CURSOR_BLANK) {
2925 wl_pointer_set_cursor(input->pointer,
2926 input->pointer_enter_serial,
2931 if (input->current_cursor == CURSOR_UNSET)
2933 cursor = input->display->cursors[input->current_cursor];
2937 /* FIXME We don't have the current time on the first call so we set
2938 * the animation start to the time of the first frame callback. */
2940 input->cursor_anim_start = 0;
2941 else if (input->cursor_anim_start == 0)
2942 input->cursor_anim_start = time;
2944 if (time == 0 || input->cursor_anim_start == 0)
2947 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2949 if (cursor->image_count > 1) {
2950 input->cursor_frame_cb =
2951 wl_surface_frame(input->pointer_surface);
2952 wl_callback_add_listener(input->cursor_frame_cb,
2953 &pointer_surface_listener, input);
2956 input_set_pointer_image_index(input, i);
2959 static const struct wl_callback_listener pointer_surface_listener = {
2960 pointer_surface_frame_callback
2964 input_set_pointer_image(struct input *input, int pointer)
2968 if (!input->pointer)
2971 if (input->pointer_enter_serial > input->cursor_serial)
2974 if (!force && pointer == input->current_cursor)
2977 input->current_cursor = pointer;
2978 input->cursor_serial = input->pointer_enter_serial;
2979 if (!input->cursor_frame_cb)
2980 pointer_surface_frame_callback(input, NULL, 0);
2982 /* The current frame callback may be stuck if, for instance,
2983 * the set cursor request was processed by the server after
2984 * this client lost the focus. In this case the cursor surface
2985 * might not be mapped and the frame callback wouldn't ever
2986 * complete. Send a set_cursor and attach to try to map the
2987 * cursor surface again so that the callback will finish */
2988 input_set_pointer_image_index(input, 0);
2992 struct wl_data_device *
2993 input_get_data_device(struct input *input)
2995 return input->data_device;
2999 input_set_selection(struct input *input,
3000 struct wl_data_source *source, uint32_t time)
3002 wl_data_device_set_selection(input->data_device, source, time);
3006 input_accept(struct input *input, const char *type)
3008 wl_data_offer_accept(input->drag_offer->offer,
3009 input->pointer_enter_serial, type);
3013 offer_io_func(struct task *task, uint32_t events)
3015 struct data_offer *offer =
3016 container_of(task, struct data_offer, io_task);
3020 len = read(offer->fd, buffer, sizeof buffer);
3021 offer->func(buffer, len,
3022 offer->x, offer->y, offer->user_data);
3026 data_offer_destroy(offer);
3031 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
3032 data_func_t func, void *user_data)
3036 if (pipe2(p, O_CLOEXEC) == -1)
3039 wl_data_offer_receive(offer->offer, mime_type, p[1]);
3042 offer->io_task.run = offer_io_func;
3046 offer->user_data = user_data;
3048 display_watch_fd(offer->input->display,
3049 offer->fd, EPOLLIN, &offer->io_task);
3053 input_receive_drag_data(struct input *input, const char *mime_type,
3054 data_func_t func, void *data)
3056 data_offer_receive_data(input->drag_offer, mime_type, func, data);
3057 input->drag_offer->x = input->sx;
3058 input->drag_offer->y = input->sy;
3062 input_receive_selection_data(struct input *input, const char *mime_type,
3063 data_func_t func, void *data)
3067 if (input->selection_offer == NULL)
3070 for (p = input->selection_offer->types.data; *p; p++)
3071 if (strcmp(mime_type, *p) == 0)
3077 data_offer_receive_data(input->selection_offer,
3078 mime_type, func, data);
3083 input_receive_selection_data_to_fd(struct input *input,
3084 const char *mime_type, int fd)
3086 if (input->selection_offer)
3087 wl_data_offer_receive(input->selection_offer->offer,
3094 window_move(struct window *window, struct input *input, uint32_t serial)
3096 if (!window->shell_surface)
3099 wl_shell_surface_move(window->shell_surface, input->seat, serial);
3103 idle_resize(struct window *window)
3105 struct widget *widget;
3106 struct wl_compositor *compositor = window->display->compositor;
3108 window->resize_needed = 0;
3109 widget = window->widget;
3110 widget_set_allocation(widget,
3111 window->pending_allocation.x,
3112 window->pending_allocation.y,
3113 window->pending_allocation.width,
3114 window->pending_allocation.height);
3116 if (window->input_region) {
3117 wl_region_destroy(window->input_region);
3118 window->input_region = NULL;
3121 if (window->opaque_region)
3122 wl_region_destroy(window->opaque_region);
3124 window->opaque_region = wl_compositor_create_region(compositor);
3126 if (widget->resize_handler)
3127 widget->resize_handler(widget,
3128 widget->allocation.width,
3129 widget->allocation.height,
3132 if (window->allocation.width != widget->allocation.width ||
3133 window->allocation.height != widget->allocation.height) {
3134 window->allocation = widget->allocation;
3135 window_schedule_redraw(window);
3139 wl_region_add(window->opaque_region, 0, 0,
3140 widget->allocation.width,
3141 widget->allocation.height);
3145 window_schedule_resize(struct window *window, int width, int height)
3147 window->pending_allocation.x = 0;
3148 window->pending_allocation.y = 0;
3149 window->pending_allocation.width = width;
3150 window->pending_allocation.height = height;
3152 if (window->min_allocation.width == 0)
3153 window->min_allocation = window->pending_allocation;
3154 if (window->pending_allocation.width < window->min_allocation.width)
3155 window->pending_allocation.width = window->min_allocation.width;
3156 if (window->pending_allocation.height < window->min_allocation.height)
3157 window->pending_allocation.height = window->min_allocation.height;
3159 window->resize_needed = 1;
3160 window_schedule_redraw(window);
3164 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
3166 window_schedule_resize(widget->window, width, height);
3170 handle_ping(void *data, struct wl_shell_surface *shell_surface,
3173 wl_shell_surface_pong(shell_surface, serial);
3177 handle_configure(void *data, struct wl_shell_surface *shell_surface,
3178 uint32_t edges, int32_t width, int32_t height)
3180 struct window *window = data;
3182 window->resize_edges = edges;
3183 window_schedule_resize(window, width, height);
3187 menu_destroy(struct menu *menu)
3189 widget_destroy(menu->widget);
3190 window_destroy(menu->window);
3195 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
3197 struct window *window = data;
3198 struct menu *menu = window->widget->user_data;
3200 /* FIXME: Need more context in this event, at least the input
3201 * device. Or just use wl_callback. And this really needs to
3202 * be a window vfunc that the menu can set. And we need the
3205 menu->func(window->parent, menu->current, window->parent->user_data);
3206 input_ungrab(menu->input);
3210 static const struct wl_shell_surface_listener shell_surface_listener = {
3217 window_get_allocation(struct window *window,
3218 struct rectangle *allocation)
3220 *allocation = window->allocation;
3224 widget_redraw(struct widget *widget)
3226 struct widget *child;
3228 if (widget->redraw_handler)
3229 widget->redraw_handler(widget, widget->user_data);
3230 wl_list_for_each(child, &widget->child_list, link)
3231 widget_redraw(child);
3235 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
3237 struct window *window = data;
3239 assert(callback == window->frame_cb);
3240 wl_callback_destroy(callback);
3241 window->frame_cb = 0;
3242 window->redraw_scheduled = 0;
3243 if (window->redraw_needed)
3244 window_schedule_redraw(window);
3247 static const struct wl_callback_listener listener = {
3252 idle_redraw(struct task *task, uint32_t events)
3254 struct window *window = container_of(task, struct window, redraw_task);
3256 if (window->resize_needed)
3257 idle_resize(window);
3259 window_create_surface(window);
3260 widget_redraw(window->widget);
3261 window->redraw_needed = 0;
3262 wl_list_init(&window->redraw_task.link);
3264 window->frame_cb = wl_surface_frame(window->surface);
3265 wl_callback_add_listener(window->frame_cb, &listener, window);
3266 window_flush(window);
3270 window_schedule_redraw(struct window *window)
3272 window->redraw_needed = 1;
3273 if (!window->redraw_scheduled) {
3274 window->redraw_task.run = idle_redraw;
3275 display_defer(window->display, &window->redraw_task);
3276 window->redraw_scheduled = 1;
3281 window_is_fullscreen(struct window *window)
3283 return window->type == TYPE_FULLSCREEN;
3287 window_set_fullscreen(struct window *window, int fullscreen)
3289 if (!window->display->shell)
3292 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3296 window->type = TYPE_FULLSCREEN;
3297 window->saved_allocation = window->allocation;
3298 wl_shell_surface_set_fullscreen(window->shell_surface,
3299 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3302 window->type = TYPE_TOPLEVEL;
3303 wl_shell_surface_set_toplevel(window->shell_surface);
3304 window_schedule_resize(window,
3305 window->saved_allocation.width,
3306 window->saved_allocation.height);
3311 window_is_maximized(struct window *window)
3313 return window->type == TYPE_MAXIMIZED;
3317 window_set_maximized(struct window *window, int maximized)
3319 if (!window->display->shell)
3322 if ((window->type == TYPE_MAXIMIZED) == maximized)
3325 if (window->type == TYPE_TOPLEVEL) {
3326 window->saved_allocation = window->allocation;
3327 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3328 window->type = TYPE_MAXIMIZED;
3330 wl_shell_surface_set_toplevel(window->shell_surface);
3331 window->type = TYPE_TOPLEVEL;
3332 window_schedule_resize(window,
3333 window->saved_allocation.width,
3334 window->saved_allocation.height);
3339 window_set_user_data(struct window *window, void *data)
3341 window->user_data = data;
3345 window_get_user_data(struct window *window)
3347 return window->user_data;
3351 window_set_key_handler(struct window *window,
3352 window_key_handler_t handler)
3354 window->key_handler = handler;
3358 window_set_keyboard_focus_handler(struct window *window,
3359 window_keyboard_focus_handler_t handler)
3361 window->keyboard_focus_handler = handler;
3365 window_set_data_handler(struct window *window, window_data_handler_t handler)
3367 window->data_handler = handler;
3371 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3373 window->drop_handler = handler;
3377 window_set_close_handler(struct window *window,
3378 window_close_handler_t handler)
3380 window->close_handler = handler;
3384 window_set_fullscreen_handler(struct window *window,
3385 window_fullscreen_handler_t handler)
3387 window->fullscreen_handler = handler;
3391 window_set_output_handler(struct window *window,
3392 window_output_handler_t handler)
3394 window->output_handler = handler;
3398 window_set_title(struct window *window, const char *title)
3400 free(window->title);
3401 window->title = strdup(title);
3402 if (window->shell_surface)
3403 wl_shell_surface_set_title(window->shell_surface, title);
3407 window_get_title(struct window *window)
3409 return window->title;
3413 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3415 struct text_cursor_position *text_cursor_position =
3416 window->display->text_cursor_position;
3418 if (!text_cursor_position)
3421 text_cursor_position_notify(text_cursor_position,
3423 wl_fixed_from_int(x),
3424 wl_fixed_from_int(y));
3428 window_damage(struct window *window, int32_t x, int32_t y,
3429 int32_t width, int32_t height)
3431 wl_surface_damage(window->surface, x, y, width, height);
3435 surface_enter(void *data,
3436 struct wl_surface *wl_surface, struct wl_output *wl_output)
3438 struct window *window = data;
3439 struct output *output;
3440 struct output *output_found = NULL;
3441 struct window_output *window_output;
3443 wl_list_for_each(output, &window->display->output_list, link) {
3444 if (output->output == wl_output) {
3445 output_found = output;
3453 window_output = malloc (sizeof *window_output);
3454 window_output->output = output_found;
3456 wl_list_insert (&window->window_output_list, &window_output->link);
3458 if (window->output_handler)
3459 window->output_handler(window, output_found, 1,
3464 surface_leave(void *data,
3465 struct wl_surface *wl_surface, struct wl_output *output)
3467 struct window *window = data;
3468 struct window_output *window_output;
3469 struct window_output *window_output_found = NULL;
3471 wl_list_for_each(window_output, &window->window_output_list, link) {
3472 if (window_output->output->output == output) {
3473 window_output_found = window_output;
3478 if (window_output_found) {
3479 wl_list_remove(&window_output_found->link);
3481 if (window->output_handler)
3482 window->output_handler(window, window_output->output,
3483 0, window->user_data);
3485 free(window_output_found);
3489 static const struct wl_surface_listener surface_listener = {
3494 static struct window *
3495 window_create_internal(struct display *display,
3496 struct window *parent, int type)
3498 struct window *window;
3500 window = malloc(sizeof *window);
3504 memset(window, 0, sizeof *window);
3505 window->display = display;
3506 window->parent = parent;
3507 window->surface = wl_compositor_create_surface(display->compositor);
3508 wl_surface_add_listener(window->surface, &surface_listener, window);
3509 if (type != TYPE_CUSTOM && display->shell) {
3510 window->shell_surface =
3511 wl_shell_get_shell_surface(display->shell,
3514 window->allocation.x = 0;
3515 window->allocation.y = 0;
3516 window->allocation.width = 0;
3517 window->allocation.height = 0;
3518 window->saved_allocation = window->allocation;
3519 window->transparent = 1;
3520 window->type = type;
3521 window->input_region = NULL;
3522 window->opaque_region = NULL;
3525 #ifdef HAVE_CAIRO_EGL
3526 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3528 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3531 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3533 wl_surface_set_user_data(window->surface, window);
3534 wl_list_insert(display->window_list.prev, &window->link);
3535 wl_list_init(&window->redraw_task.link);
3537 if (window->shell_surface) {
3538 wl_shell_surface_set_user_data(window->shell_surface, window);
3539 wl_shell_surface_add_listener(window->shell_surface,
3540 &shell_surface_listener, window);
3543 wl_list_init (&window->window_output_list);
3549 window_create(struct display *display)
3551 struct window *window;
3553 window = window_create_internal(display, NULL, TYPE_NONE);
3561 window_create_custom(struct display *display)
3563 struct window *window;
3565 window = window_create_internal(display, NULL, TYPE_CUSTOM);
3573 window_create_transient(struct display *display, struct window *parent,
3574 int32_t x, int32_t y, uint32_t flags)
3576 struct window *window;
3578 window = window_create_internal(parent->display,
3579 parent, TYPE_TRANSIENT);
3587 wl_shell_surface_set_transient(window->shell_surface,
3588 window->parent->surface,
3589 window->x, window->y, flags);
3595 menu_set_item(struct menu *menu, int sy)
3599 next = (sy - 8) / 20;
3600 if (menu->current != next) {
3601 menu->current = next;
3602 widget_schedule_redraw(menu->widget);
3607 menu_motion_handler(struct widget *widget,
3608 struct input *input, uint32_t time,
3609 float x, float y, void *data)
3611 struct menu *menu = data;
3613 if (widget == menu->widget)
3614 menu_set_item(data, y);
3616 return CURSOR_LEFT_PTR;
3620 menu_enter_handler(struct widget *widget,
3621 struct input *input, float x, float y, void *data)
3623 struct menu *menu = data;
3625 if (widget == menu->widget)
3626 menu_set_item(data, y);
3628 return CURSOR_LEFT_PTR;
3632 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3634 struct menu *menu = data;
3636 if (widget == menu->widget)
3637 menu_set_item(data, -200);
3641 menu_button_handler(struct widget *widget,
3642 struct input *input, uint32_t time,
3643 uint32_t button, enum wl_pointer_button_state state,
3647 struct menu *menu = data;
3649 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3650 (menu->release_count > 0 || time - menu->time > 500)) {
3651 /* Either relase after press-drag-release or
3652 * click-motion-click. */
3653 menu->func(menu->window->parent,
3654 menu->current, menu->window->parent->user_data);
3655 input_ungrab(input);
3657 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3658 menu->release_count++;
3663 menu_redraw_handler(struct widget *widget, void *data)
3666 const int32_t r = 3, margin = 3;
3667 struct menu *menu = data;
3668 int32_t width, height, i;
3669 struct window *window = widget->window;
3671 cr = cairo_create(window->cairo_surface);
3672 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3673 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3676 width = window->allocation.width;
3677 height = window->allocation.height;
3678 rounded_rect(cr, 0, 0, width, height, r);
3680 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3681 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3684 for (i = 0; i < menu->count; i++) {
3685 if (i == menu->current) {
3686 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3687 cairo_rectangle(cr, margin, i * 20 + margin,
3688 width - 2 * margin, 20);
3690 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3691 cairo_move_to(cr, 10, i * 20 + 16);
3692 cairo_show_text(cr, menu->entries[i]);
3694 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3695 cairo_move_to(cr, 10, i * 20 + 16);
3696 cairo_show_text(cr, menu->entries[i]);
3704 window_show_menu(struct display *display,
3705 struct input *input, uint32_t time, struct window *parent,
3706 int32_t x, int32_t y,
3707 menu_func_t func, const char **entries, int count)
3709 struct window *window;
3711 const int32_t margin = 3;
3713 menu = malloc(sizeof *menu);
3717 window = window_create_internal(parent->display, parent, TYPE_MENU);
3723 menu->window = window;
3724 menu->widget = window_add_widget(menu->window, menu);
3725 menu->entries = entries;
3726 menu->count = count;
3727 menu->release_count = 0;
3731 menu->input = input;
3732 window->type = TYPE_MENU;
3736 input_ungrab(input);
3737 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3738 display_get_serial(window->display),
3739 window->parent->surface,
3740 window->x, window->y, 0);
3742 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3743 widget_set_enter_handler(menu->widget, menu_enter_handler);
3744 widget_set_leave_handler(menu->widget, menu_leave_handler);
3745 widget_set_motion_handler(menu->widget, menu_motion_handler);
3746 widget_set_button_handler(menu->widget, menu_button_handler);
3748 input_grab(input, menu->widget, 0);
3749 window_schedule_resize(window, 200, count * 20 + margin * 2);
3753 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3755 window->buffer_type = type;
3760 display_handle_geometry(void *data,
3761 struct wl_output *wl_output,
3764 int physical_height,
3770 struct output *output = data;
3772 output->allocation.x = x;
3773 output->allocation.y = y;
3774 output->transform = transform;
3778 display_handle_mode(void *data,
3779 struct wl_output *wl_output,
3785 struct output *output = data;
3786 struct display *display = output->display;
3788 if (flags & WL_OUTPUT_MODE_CURRENT) {
3789 output->allocation.width = width;
3790 output->allocation.height = height;
3791 if (display->output_configure_handler)
3792 (*display->output_configure_handler)(
3793 output, display->user_data);
3797 static const struct wl_output_listener output_listener = {
3798 display_handle_geometry,
3803 display_add_output(struct display *d, uint32_t id)
3805 struct output *output;
3807 output = malloc(sizeof *output);
3811 memset(output, 0, sizeof *output);
3812 output->display = d;
3814 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
3815 wl_list_insert(d->output_list.prev, &output->link);
3817 wl_output_add_listener(output->output, &output_listener, output);
3821 output_destroy(struct output *output)
3823 if (output->destroy_handler)
3824 (*output->destroy_handler)(output, output->user_data);
3826 wl_output_destroy(output->output);
3827 wl_list_remove(&output->link);
3832 display_set_global_handler(struct display *display,
3833 display_global_handler_t handler)
3835 struct global *global;
3837 display->global_handler = handler;
3841 wl_list_for_each(global, &display->global_list, link)
3842 display->global_handler(display,
3843 global->name, global->interface,
3844 global->version, display->user_data);
3848 display_set_output_configure_handler(struct display *display,
3849 display_output_handler_t handler)
3851 struct output *output;
3853 display->output_configure_handler = handler;
3857 wl_list_for_each(output, &display->output_list, link) {
3858 if (output->allocation.width == 0 &&
3859 output->allocation.height == 0)
3862 (*display->output_configure_handler)(output,
3863 display->user_data);
3868 output_set_user_data(struct output *output, void *data)
3870 output->user_data = data;
3874 output_get_user_data(struct output *output)
3876 return output->user_data;
3880 output_set_destroy_handler(struct output *output,
3881 display_output_handler_t handler)
3883 output->destroy_handler = handler;
3884 /* FIXME: implement this, once we have way to remove outputs */
3888 output_get_allocation(struct output *output, struct rectangle *base)
3890 struct rectangle allocation = output->allocation;
3892 switch (output->transform) {
3893 case WL_OUTPUT_TRANSFORM_90:
3894 case WL_OUTPUT_TRANSFORM_270:
3895 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3896 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3897 /* Swap width and height */
3898 allocation.width = output->allocation.height;
3899 allocation.height = output->allocation.width;
3907 output_get_wl_output(struct output *output)
3909 return output->output;
3912 enum wl_output_transform
3913 output_get_transform(struct output *output)
3915 return output->transform;
3919 fini_xkb(struct input *input)
3921 xkb_state_unref(input->xkb.state);
3922 xkb_map_unref(input->xkb.keymap);
3926 display_add_input(struct display *d, uint32_t id)
3928 struct input *input;
3930 input = malloc(sizeof *input);
3934 memset(input, 0, sizeof *input);
3936 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
3937 input->pointer_focus = NULL;
3938 input->keyboard_focus = NULL;
3939 wl_list_insert(d->input_list.prev, &input->link);
3941 wl_seat_add_listener(input->seat, &seat_listener, input);
3942 wl_seat_set_user_data(input->seat, input);
3944 input->data_device =
3945 wl_data_device_manager_get_data_device(d->data_device_manager,
3947 wl_data_device_add_listener(input->data_device, &data_device_listener,
3950 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3952 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3953 TFD_CLOEXEC | TFD_NONBLOCK);
3954 input->repeat_task.run = keyboard_repeat_func;
3955 display_watch_fd(d, input->repeat_timer_fd,
3956 EPOLLIN, &input->repeat_task);
3960 input_destroy(struct input *input)
3962 input_remove_keyboard_focus(input);
3963 input_remove_pointer_focus(input);
3965 if (input->drag_offer)
3966 data_offer_destroy(input->drag_offer);
3968 if (input->selection_offer)
3969 data_offer_destroy(input->selection_offer);
3971 wl_data_device_destroy(input->data_device);
3974 wl_surface_destroy(input->pointer_surface);
3976 wl_list_remove(&input->link);
3977 wl_seat_destroy(input->seat);
3978 close(input->repeat_timer_fd);
3983 init_workspace_manager(struct display *d, uint32_t id)
3985 d->workspace_manager =
3986 wl_registry_bind(d->registry, id,
3987 &workspace_manager_interface, 1);
3988 if (d->workspace_manager != NULL)
3989 workspace_manager_add_listener(d->workspace_manager,
3990 &workspace_manager_listener,
3995 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3996 const char *interface, uint32_t version)
3998 struct display *d = data;
3999 struct global *global;
4001 global = malloc(sizeof *global);
4003 global->interface = strdup(interface);
4004 global->version = version;
4005 wl_list_insert(d->global_list.prev, &global->link);
4007 if (strcmp(interface, "wl_compositor") == 0) {
4008 d->compositor = wl_registry_bind(registry, id,
4009 &wl_compositor_interface, 1);
4010 } else if (strcmp(interface, "wl_output") == 0) {
4011 display_add_output(d, id);
4012 } else if (strcmp(interface, "wl_seat") == 0) {
4013 display_add_input(d, id);
4014 } else if (strcmp(interface, "wl_shell") == 0) {
4015 d->shell = wl_registry_bind(registry,
4016 id, &wl_shell_interface, 1);
4017 } else if (strcmp(interface, "wl_shm") == 0) {
4018 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
4019 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
4020 d->data_device_manager =
4021 wl_registry_bind(registry, id,
4022 &wl_data_device_manager_interface, 1);
4023 } else if (strcmp(interface, "text_cursor_position") == 0) {
4024 d->text_cursor_position =
4025 wl_registry_bind(registry, id,
4026 &text_cursor_position_interface, 1);
4027 } else if (strcmp(interface, "workspace_manager") == 0) {
4028 init_workspace_manager(d, id);
4031 if (d->global_handler)
4032 d->global_handler(d, id, interface, version, d->user_data);
4036 display_bind(struct display *display, uint32_t name,
4037 const struct wl_interface *interface, uint32_t version)
4039 return wl_registry_bind(display->registry, name, interface, version);
4042 static const struct wl_registry_listener registry_listener = {
4043 registry_handle_global
4046 #ifdef HAVE_CAIRO_EGL
4048 init_egl(struct display *d)
4050 EGLint major, minor;
4053 #ifdef USE_CAIRO_GLESV2
4054 # define GL_BIT EGL_OPENGL_ES2_BIT
4056 # define GL_BIT EGL_OPENGL_BIT
4059 static const EGLint argb_cfg_attribs[] = {
4060 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4066 EGL_RENDERABLE_TYPE, GL_BIT,
4070 #ifdef USE_CAIRO_GLESV2
4071 static const EGLint context_attribs[] = {
4072 EGL_CONTEXT_CLIENT_VERSION, 2,
4075 EGLint api = EGL_OPENGL_ES_API;
4077 EGLint *context_attribs = NULL;
4078 EGLint api = EGL_OPENGL_API;
4081 d->dpy = eglGetDisplay(d->display);
4082 if (!eglInitialize(d->dpy, &major, &minor)) {
4083 fprintf(stderr, "failed to initialize display\n");
4087 if (!eglBindAPI(api)) {
4088 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
4092 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
4093 &d->argb_config, 1, &n) || n != 1) {
4094 fprintf(stderr, "failed to choose argb config\n");
4098 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
4099 EGL_NO_CONTEXT, context_attribs);
4100 if (d->argb_ctx == NULL) {
4101 fprintf(stderr, "failed to create context\n");
4105 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
4106 fprintf(stderr, "failed to make context current\n");
4110 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
4111 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
4112 fprintf(stderr, "failed to get cairo egl argb device\n");
4120 fini_egl(struct display *display)
4122 cairo_device_destroy(display->argb_device);
4124 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
4127 eglTerminate(display->dpy);
4133 init_dummy_surface(struct display *display)
4138 len = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, 1);
4140 display->dummy_surface =
4141 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
4143 display->dummy_surface_data = data;
4147 handle_display_data(struct task *task, uint32_t events)
4149 struct display *display =
4150 container_of(task, struct display, display_task);
4151 struct epoll_event ep;
4154 display->display_fd_events = events;
4156 if (events & EPOLLERR || events & EPOLLHUP) {
4157 display_exit(display);
4161 if (events & EPOLLIN) {
4162 ret = wl_display_dispatch(display->display);
4164 display_exit(display);
4169 if (events & EPOLLOUT) {
4170 ret = wl_display_flush(display->display);
4172 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
4173 ep.data.ptr = &display->display_task;
4174 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4175 display->display_fd, &ep);
4176 } else if (ret == -1 && errno != EAGAIN) {
4177 display_exit(display);
4184 display_create(int argc, char *argv[])
4188 d = malloc(sizeof *d);
4192 memset(d, 0, sizeof *d);
4194 d->display = wl_display_connect(NULL);
4195 if (d->display == NULL) {
4196 fprintf(stderr, "failed to create display: %m\n");
4200 d->epoll_fd = os_epoll_create_cloexec();
4201 d->display_fd = wl_display_get_fd(d->display);
4202 d->display_task.run = handle_display_data;
4203 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
4206 wl_list_init(&d->deferred_list);
4207 wl_list_init(&d->input_list);
4208 wl_list_init(&d->output_list);
4209 wl_list_init(&d->global_list);
4211 d->xkb_context = xkb_context_new(0);
4212 if (d->xkb_context == NULL) {
4213 fprintf(stderr, "Failed to create XKB context\n");
4218 d->workspace_count = 1;
4220 d->registry = wl_display_get_registry(d->display);
4221 wl_registry_add_listener(d->registry, ®istry_listener, d);
4222 wl_display_dispatch(d->display);
4223 #ifdef HAVE_CAIRO_EGL
4224 if (init_egl(d) < 0)
4230 d->theme = theme_create();
4232 wl_list_init(&d->window_list);
4234 init_dummy_surface(d);
4240 display_destroy_outputs(struct display *display)
4243 struct output *output;
4245 wl_list_for_each_safe(output, tmp, &display->output_list, link)
4246 output_destroy(output);
4250 display_destroy_inputs(struct display *display)
4253 struct input *input;
4255 wl_list_for_each_safe(input, tmp, &display->input_list, link)
4256 input_destroy(input);
4260 display_destroy(struct display *display)
4262 if (!wl_list_empty(&display->window_list))
4263 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
4264 wl_list_length(&display->window_list));
4266 if (!wl_list_empty(&display->deferred_list))
4267 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
4269 cairo_surface_destroy(display->dummy_surface);
4270 free(display->dummy_surface_data);
4272 display_destroy_outputs(display);
4273 display_destroy_inputs(display);
4275 xkb_context_unref(display->xkb_context);
4277 theme_destroy(display->theme);
4278 destroy_cursors(display);
4280 #ifdef HAVE_CAIRO_EGL
4285 wl_shell_destroy(display->shell);
4288 wl_shm_destroy(display->shm);
4290 if (display->data_device_manager)
4291 wl_data_device_manager_destroy(display->data_device_manager);
4293 wl_compositor_destroy(display->compositor);
4295 close(display->epoll_fd);
4297 if (!(display->display_fd_events & EPOLLERR) &&
4298 !(display->display_fd_events & EPOLLHUP))
4299 wl_display_flush(display->display);
4301 wl_display_disconnect(display->display);
4306 display_set_user_data(struct display *display, void *data)
4308 display->user_data = data;
4312 display_get_user_data(struct display *display)
4314 return display->user_data;
4318 display_get_display(struct display *display)
4320 return display->display;
4324 display_get_output(struct display *display)
4326 return container_of(display->output_list.next, struct output, link);
4329 struct wl_compositor *
4330 display_get_compositor(struct display *display)
4332 return display->compositor;
4336 display_get_serial(struct display *display)
4338 return display->serial;
4342 display_get_egl_display(struct display *d)
4347 struct wl_data_source *
4348 display_create_data_source(struct display *display)
4350 return wl_data_device_manager_create_data_source(display->data_device_manager);
4354 display_get_argb_egl_config(struct display *d)
4356 return d->argb_config;
4360 display_get_shell(struct display *display)
4362 return display->shell;
4366 display_acquire_window_surface(struct display *display,
4367 struct window *window,
4370 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
4373 return window->toysurface->acquire(window->toysurface, ctx);
4377 display_release_window_surface(struct display *display,
4378 struct window *window)
4380 if (window->buffer_type != WINDOW_BUFFER_TYPE_EGL_WINDOW)
4383 window->toysurface->release(window->toysurface);
4387 display_defer(struct display *display, struct task *task)
4389 wl_list_insert(&display->deferred_list, &task->link);
4393 display_watch_fd(struct display *display,
4394 int fd, uint32_t events, struct task *task)
4396 struct epoll_event ep;
4400 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4404 display_unwatch_fd(struct display *display, int fd)
4406 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4410 display_run(struct display *display)
4413 struct epoll_event ep[16];
4416 display->running = 1;
4418 while (!wl_list_empty(&display->deferred_list)) {
4419 task = container_of(display->deferred_list.prev,
4421 wl_list_remove(&task->link);
4425 wl_display_dispatch_pending(display->display);
4427 if (!display->running)
4430 ret = wl_display_flush(display->display);
4431 if (ret < 0 && errno == EAGAIN) {
4433 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4434 ep[0].data.ptr = &display->display_task;
4436 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4437 display->display_fd, &ep[0]);
4438 } else if (ret < 0) {
4442 count = epoll_wait(display->epoll_fd,
4443 ep, ARRAY_LENGTH(ep), -1);
4444 for (i = 0; i < count; i++) {
4445 task = ep[i].data.ptr;
4446 task->run(task, ep[i].events);
4452 display_exit(struct display *display)
4454 display->running = 0;
4458 keysym_modifiers_add(struct wl_array *modifiers_map,
4461 size_t len = strlen(name) + 1;
4464 p = wl_array_add(modifiers_map, len);
4469 strncpy(p, name, len);
4472 static xkb_mod_index_t
4473 keysym_modifiers_get_index(struct wl_array *modifiers_map,
4476 xkb_mod_index_t index = 0;
4477 char *p = modifiers_map->data;
4479 while ((const char *)p < (const char *)(modifiers_map->data + modifiers_map->size)) {
4480 if (strcmp(p, name) == 0)
4487 return XKB_MOD_INVALID;
4491 keysym_modifiers_get_mask(struct wl_array *modifiers_map,
4494 xkb_mod_index_t index = keysym_modifiers_get_index(modifiers_map, name);
4496 if (index == XKB_MOD_INVALID)
4497 return XKB_MOD_INVALID;