2 * Copyright © 2008 Kristian Høgsberg
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 #include "../config.h"
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
84 struct wl_display *display;
85 struct wl_registry *registry;
86 struct wl_compositor *compositor;
87 struct wl_shell *shell;
89 struct wl_data_device_manager *data_device_manager;
90 struct text_cursor_position *text_cursor_position;
91 struct workspace_manager *workspace_manager;
93 EGLConfig argb_config;
95 cairo_device_t *argb_device;
99 uint32_t display_fd_events;
100 struct task display_task;
103 struct wl_list deferred_list;
107 struct wl_list global_list;
108 struct wl_list window_list;
109 struct wl_list input_list;
110 struct wl_list output_list;
114 struct wl_cursor_theme *cursor_theme;
115 struct wl_cursor **cursors;
117 display_output_handler_t output_configure_handler;
118 display_global_handler_t global_handler;
122 struct xkb_context *xkb_context;
125 uint32_t workspace_count;
138 struct window_output {
139 struct output *output;
144 struct display *display;
145 struct window *parent;
146 struct wl_list window_output_list;
147 struct wl_surface *surface;
148 struct wl_shell_surface *shell_surface;
149 struct wl_region *input_region;
150 struct wl_region *opaque_region;
152 struct rectangle allocation, saved_allocation, server_allocation;
153 struct rectangle min_allocation;
154 struct rectangle pending_allocation;
157 int redraw_scheduled;
159 struct task redraw_task;
165 enum window_buffer_type buffer_type;
167 cairo_surface_t *cairo_surface;
169 struct shm_pool *pool;
171 window_key_handler_t key_handler;
172 window_keyboard_focus_handler_t keyboard_focus_handler;
173 window_data_handler_t data_handler;
174 window_drop_handler_t drop_handler;
175 window_close_handler_t close_handler;
176 window_fullscreen_handler_t fullscreen_handler;
178 struct wl_callback *frame_cb;
181 struct widget *widget;
188 struct window *window;
189 struct tooltip *tooltip;
190 struct wl_list child_list;
192 struct rectangle allocation;
193 widget_resize_handler_t resize_handler;
194 widget_redraw_handler_t redraw_handler;
195 widget_enter_handler_t enter_handler;
196 widget_leave_handler_t leave_handler;
197 widget_motion_handler_t motion_handler;
198 widget_button_handler_t button_handler;
199 widget_axis_handler_t axis_handler;
206 struct display *display;
207 struct wl_seat *seat;
208 struct wl_pointer *pointer;
209 struct wl_keyboard *keyboard;
210 struct window *pointer_focus;
211 struct window *keyboard_focus;
213 uint32_t cursor_anim_start;
214 struct wl_callback *cursor_frame_cb;
215 struct wl_surface *pointer_surface;
217 uint32_t pointer_enter_serial;
218 uint32_t cursor_serial;
222 struct widget *focus_widget;
224 uint32_t grab_button;
226 struct wl_data_device *data_device;
227 struct data_offer *drag_offer;
228 struct data_offer *selection_offer;
231 struct xkb_keymap *keymap;
232 struct xkb_state *state;
233 xkb_mod_mask_t control_mask;
234 xkb_mod_mask_t alt_mask;
235 xkb_mod_mask_t shift_mask;
238 struct task repeat_task;
242 uint32_t repeat_time;
246 struct display *display;
247 struct wl_output *output;
248 struct rectangle allocation;
252 display_output_handler_t destroy_handler;
256 enum frame_button_action {
257 FRAME_BUTTON_NULL = 0,
258 FRAME_BUTTON_ICON = 1,
259 FRAME_BUTTON_CLOSE = 2,
260 FRAME_BUTTON_MINIMIZE = 3,
261 FRAME_BUTTON_MAXIMIZE = 4,
264 enum frame_button_pointer {
265 FRAME_BUTTON_DEFAULT = 0,
266 FRAME_BUTTON_OVER = 1,
267 FRAME_BUTTON_ACTIVE = 2,
270 enum frame_button_align {
271 FRAME_BUTTON_RIGHT = 0,
272 FRAME_BUTTON_LEFT = 1,
275 enum frame_button_decoration {
276 FRAME_BUTTON_NONE = 0,
277 FRAME_BUTTON_FANCY = 1,
280 struct frame_button {
281 struct widget *widget;
283 cairo_surface_t *icon;
284 enum frame_button_action type;
285 enum frame_button_pointer state;
286 struct wl_list link; /* buttons_list */
287 enum frame_button_align align;
288 enum frame_button_decoration decoration;
292 struct widget *widget;
293 struct widget *child;
294 struct wl_list buttons_list;
298 struct window *window;
299 struct widget *widget;
301 const char **entries;
310 struct widget *parent;
311 struct window *window;
312 struct widget *widget;
314 struct task tooltip_task;
320 struct wl_shm_pool *pool;
327 CURSOR_DEFAULT = 100,
331 enum window_location {
333 WINDOW_RESIZING_TOP = 1,
334 WINDOW_RESIZING_BOTTOM = 2,
335 WINDOW_RESIZING_LEFT = 4,
336 WINDOW_RESIZING_TOP_LEFT = 5,
337 WINDOW_RESIZING_BOTTOM_LEFT = 6,
338 WINDOW_RESIZING_RIGHT = 8,
339 WINDOW_RESIZING_TOP_RIGHT = 9,
340 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
341 WINDOW_RESIZING_MASK = 15,
342 WINDOW_EXTERIOR = 16,
343 WINDOW_TITLEBAR = 17,
344 WINDOW_CLIENT_AREA = 18,
347 static const cairo_user_data_key_t surface_data_key;
348 struct surface_data {
349 struct wl_buffer *buffer;
352 #define MULT(_d,c,a,t) \
353 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
355 #ifdef HAVE_CAIRO_EGL
357 struct egl_window_surface_data {
358 struct display *display;
359 struct wl_surface *surface;
360 struct wl_egl_window *window;
365 egl_window_surface_data_destroy(void *p)
367 struct egl_window_surface_data *data = p;
368 struct display *d = data->display;
370 eglDestroySurface(d->dpy, data->surf);
371 wl_egl_window_destroy(data->window);
372 data->surface = NULL;
377 static cairo_surface_t *
378 display_create_egl_window_surface(struct display *display,
379 struct wl_surface *surface,
381 struct rectangle *rectangle)
383 cairo_surface_t *cairo_surface;
384 struct egl_window_surface_data *data;
386 cairo_device_t *device;
388 data = malloc(sizeof *data);
392 data->display = display;
393 data->surface = surface;
395 config = display->argb_config;
396 device = display->argb_device;
398 data->window = wl_egl_window_create(surface,
402 data->surf = eglCreateWindowSurface(display->dpy, config,
405 cairo_surface = cairo_gl_surface_create_for_egl(device,
410 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
411 data, egl_window_surface_data_destroy);
413 return cairo_surface;
419 display_get_buffer_for_surface(struct display *display,
420 cairo_surface_t *surface)
422 struct surface_data *data;
424 data = cairo_surface_get_user_data (surface, &surface_data_key);
429 struct shm_surface_data {
430 struct surface_data data;
431 struct shm_pool *pool;
435 shm_pool_destroy(struct shm_pool *pool);
438 shm_surface_data_destroy(void *p)
440 struct shm_surface_data *data = p;
442 wl_buffer_destroy(data->data.buffer);
444 shm_pool_destroy(data->pool);
449 static struct wl_shm_pool *
450 make_shm_pool(struct display *display, int size, void **data)
452 struct wl_shm_pool *pool;
455 fd = os_create_anonymous_file(size);
457 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
462 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
463 if (*data == MAP_FAILED) {
464 fprintf(stderr, "mmap failed: %m\n");
469 pool = wl_shm_create_pool(display->shm, fd, size);
476 static struct shm_pool *
477 shm_pool_create(struct display *display, size_t size)
479 struct shm_pool *pool = malloc(sizeof *pool);
484 pool->pool = make_shm_pool(display, size, &pool->data);
497 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
499 if (pool->used + size > pool->size)
502 *offset = pool->used;
505 return (char *) pool->data + *offset;
508 /* destroy the pool. this does not unmap the memory though */
510 shm_pool_destroy(struct shm_pool *pool)
512 munmap(pool->data, pool->size);
513 wl_shm_pool_destroy(pool->pool);
517 /* Start allocating from the beginning of the pool again */
519 shm_pool_reset(struct shm_pool *pool)
525 data_length_for_shm_surface(struct rectangle *rect)
529 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
531 return stride * rect->height;
534 static cairo_surface_t *
535 display_create_shm_surface_from_pool(struct display *display,
536 struct rectangle *rectangle,
537 uint32_t flags, struct shm_pool *pool)
539 struct shm_surface_data *data;
541 cairo_surface_t *surface;
542 int stride, length, offset;
545 data = malloc(sizeof *data);
549 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
551 length = stride * rectangle->height;
553 map = shm_pool_allocate(pool, length, &offset);
560 surface = cairo_image_surface_create_for_data (map,
566 cairo_surface_set_user_data (surface, &surface_data_key,
567 data, shm_surface_data_destroy);
569 if (flags & SURFACE_OPAQUE)
570 format = WL_SHM_FORMAT_XRGB8888;
572 format = WL_SHM_FORMAT_ARGB8888;
574 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
582 static cairo_surface_t *
583 display_create_shm_surface(struct display *display,
584 struct rectangle *rectangle, uint32_t flags,
585 struct window *window)
587 struct shm_surface_data *data;
588 struct shm_pool *pool;
589 cairo_surface_t *surface;
591 if (window && window->pool) {
592 shm_pool_reset(window->pool);
593 surface = display_create_shm_surface_from_pool(display,
601 pool = shm_pool_create(display,
602 data_length_for_shm_surface(rectangle));
607 display_create_shm_surface_from_pool(display, rectangle,
611 shm_pool_destroy(pool);
615 /* make sure we destroy the pool when the surface is destroyed */
616 data = cairo_surface_get_user_data(surface, &surface_data_key);
623 check_size(struct rectangle *rect)
625 if (rect->width && rect->height)
628 fprintf(stderr, "tried to create surface of "
629 "width: %d, height: %d\n", rect->width, rect->height);
634 display_create_surface(struct display *display,
635 struct wl_surface *surface,
636 struct rectangle *rectangle,
639 if (check_size(rectangle) < 0)
641 #ifdef HAVE_CAIRO_EGL
642 if (display->dpy && !(flags & SURFACE_SHM))
643 return display_create_egl_window_surface(display,
648 return display_create_shm_surface(display, rectangle, flags, NULL);
652 * The following correspondences between file names and cursors was copied
653 * from: https://bugs.kde.org/attachment.cgi?id=67313
656 static const char *bottom_left_corners[] = {
657 "bottom_left_corner",
661 static const char *bottom_right_corners[] = {
662 "bottom_right_corner",
666 static const char *bottom_sides[] = {
671 static const char *grabbings[] = {
674 "208530c400c041818281048008011002"
677 static const char *left_ptrs[] = {
684 static const char *left_sides[] = {
689 static const char *right_sides[] = {
694 static const char *top_left_corners[] = {
699 static const char *top_right_corners[] = {
704 static const char *top_sides[] = {
709 static const char *xterms[] = {
715 static const char *hand1s[] = {
719 "e29285e634086352946a0e7090d73106"
722 static const char *watches[] = {
725 "0426c94ea35c87780ff01dc239897213"
728 struct cursor_alternatives {
733 static const struct cursor_alternatives cursors[] = {
734 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
735 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
736 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
737 {grabbings, ARRAY_LENGTH(grabbings)},
738 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
739 {left_sides, ARRAY_LENGTH(left_sides)},
740 {right_sides, ARRAY_LENGTH(right_sides)},
741 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
742 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
743 {top_sides, ARRAY_LENGTH(top_sides)},
744 {xterms, ARRAY_LENGTH(xterms)},
745 {hand1s, ARRAY_LENGTH(hand1s)},
746 {watches, ARRAY_LENGTH(watches)},
750 create_cursors(struct display *display)
755 struct wl_cursor *cursor;
756 struct config_key shell_keys[] = {
757 { "cursor-theme", CONFIG_KEY_STRING, &theme },
759 struct config_section cs[] = {
760 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
763 config_file = config_file_path("weston.ini");
764 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
767 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
769 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
771 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
773 for (j = 0; !cursor && j < cursors[i].count; ++j)
774 cursor = wl_cursor_theme_get_cursor(
775 display->cursor_theme, cursors[i].names[j]);
778 fprintf(stderr, "could not load cursor '%s'\n",
779 cursors[i].names[0]);
781 display->cursors[i] = cursor;
786 destroy_cursors(struct display *display)
788 wl_cursor_theme_destroy(display->cursor_theme);
789 free(display->cursors);
792 struct wl_cursor_image *
793 display_get_pointer_image(struct display *display, int pointer)
795 struct wl_cursor *cursor = display->cursors[pointer];
797 return cursor ? cursor->images[0] : NULL;
801 window_get_resize_dx_dy(struct window *window, int *x, int *y)
803 if (window->resize_edges & WINDOW_RESIZING_LEFT)
804 *x = window->server_allocation.width - window->allocation.width;
808 if (window->resize_edges & WINDOW_RESIZING_TOP)
809 *y = window->server_allocation.height -
810 window->allocation.height;
814 window->resize_edges = 0;
818 window_attach_surface(struct window *window)
820 struct display *display = window->display;
821 struct wl_buffer *buffer;
822 #ifdef HAVE_CAIRO_EGL
823 struct egl_window_surface_data *data;
827 if (window->type == TYPE_NONE) {
828 window->type = TYPE_TOPLEVEL;
830 wl_shell_surface_set_toplevel(window->shell_surface);
833 if (window->opaque_region) {
834 wl_surface_set_opaque_region(window->surface,
835 window->opaque_region);
836 wl_region_destroy(window->opaque_region);
837 window->opaque_region = NULL;
840 if (window->input_region) {
841 wl_surface_set_input_region(window->surface,
842 window->input_region);
843 wl_region_destroy(window->input_region);
844 window->input_region = NULL;
847 switch (window->buffer_type) {
848 #ifdef HAVE_CAIRO_EGL
849 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
850 data = cairo_surface_get_user_data(window->cairo_surface,
853 cairo_gl_surface_swapbuffers(window->cairo_surface);
854 wl_egl_window_get_attached_size(data->window,
855 &window->server_allocation.width,
856 &window->server_allocation.height);
859 case WINDOW_BUFFER_TYPE_SHM:
861 display_get_buffer_for_surface(display,
862 window->cairo_surface);
864 window_get_resize_dx_dy(window, &x, &y);
865 wl_surface_attach(window->surface, buffer, x, y);
866 wl_surface_damage(window->surface, 0, 0,
867 window->allocation.width,
868 window->allocation.height);
869 wl_surface_commit(window->surface);
870 window->server_allocation = window->allocation;
878 window_has_focus(struct window *window)
880 return window->focus_count > 0;
884 window_flush(struct window *window)
886 if (window->cairo_surface)
887 window_attach_surface(window);
891 window_set_surface(struct window *window, cairo_surface_t *surface)
893 cairo_surface_reference(surface);
895 if (window->cairo_surface != NULL)
896 cairo_surface_destroy(window->cairo_surface);
898 window->cairo_surface = surface;
901 #ifdef HAVE_CAIRO_EGL
903 window_resize_cairo_window_surface(struct window *window)
905 struct egl_window_surface_data *data;
908 data = cairo_surface_get_user_data(window->cairo_surface,
911 window_get_resize_dx_dy(window, &x, &y),
912 wl_egl_window_resize(data->window,
913 window->allocation.width,
914 window->allocation.height,
917 cairo_gl_surface_set_size(window->cairo_surface,
918 window->allocation.width,
919 window->allocation.height);
924 window_get_display(struct window *window)
926 return window->display;
930 window_create_surface(struct window *window)
932 cairo_surface_t *surface;
935 if (!window->transparent)
936 flags = SURFACE_OPAQUE;
938 switch (window->buffer_type) {
939 #ifdef HAVE_CAIRO_EGL
940 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
941 if (window->cairo_surface) {
942 window_resize_cairo_window_surface(window);
945 surface = display_create_surface(window->display,
947 &window->allocation, flags);
950 case WINDOW_BUFFER_TYPE_SHM:
951 surface = display_create_shm_surface(window->display,
960 window_set_surface(window, surface);
961 cairo_surface_destroy(surface);
964 static void frame_destroy(struct frame *frame);
967 window_destroy(struct window *window)
969 struct display *display = window->display;
971 struct window_output *window_output;
972 struct window_output *window_output_tmp;
974 if (window->redraw_scheduled)
975 wl_list_remove(&window->redraw_task.link);
977 wl_list_for_each(input, &display->input_list, link) {
978 if (input->pointer_focus == window)
979 input->pointer_focus = NULL;
980 if (input->keyboard_focus == window)
981 input->keyboard_focus = NULL;
982 if (input->focus_widget &&
983 input->focus_widget->window == window)
984 input->focus_widget = NULL;
987 wl_list_for_each_safe(window_output, window_output_tmp,
988 &window->window_output_list, link) {
989 free (window_output);
992 if (window->input_region)
993 wl_region_destroy(window->input_region);
994 if (window->opaque_region)
995 wl_region_destroy(window->opaque_region);
998 frame_destroy(window->frame);
1000 if (window->shell_surface)
1001 wl_shell_surface_destroy(window->shell_surface);
1002 wl_surface_destroy(window->surface);
1003 wl_list_remove(&window->link);
1005 if (window->cairo_surface != NULL)
1006 cairo_surface_destroy(window->cairo_surface);
1008 if (window->frame_cb)
1009 wl_callback_destroy(window->frame_cb);
1010 free(window->title);
1014 static struct widget *
1015 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1017 struct widget *child, *target;
1019 wl_list_for_each(child, &widget->child_list, link) {
1020 target = widget_find_widget(child, x, y);
1025 if (widget->allocation.x <= x &&
1026 x < widget->allocation.x + widget->allocation.width &&
1027 widget->allocation.y <= y &&
1028 y < widget->allocation.y + widget->allocation.height) {
1035 static struct widget *
1036 widget_create(struct window *window, void *data)
1038 struct widget *widget;
1040 widget = malloc(sizeof *widget);
1041 memset(widget, 0, sizeof *widget);
1042 widget->window = window;
1043 widget->user_data = data;
1044 widget->allocation = window->allocation;
1045 wl_list_init(&widget->child_list);
1047 widget->tooltip = NULL;
1048 widget->tooltip_count = 0;
1054 window_add_widget(struct window *window, void *data)
1056 window->widget = widget_create(window, data);
1057 wl_list_init(&window->widget->link);
1059 return window->widget;
1063 widget_add_widget(struct widget *parent, void *data)
1065 struct widget *widget;
1067 widget = widget_create(parent->window, data);
1068 wl_list_insert(parent->child_list.prev, &widget->link);
1074 widget_destroy(struct widget *widget)
1076 struct display *display = widget->window->display;
1077 struct input *input;
1079 if (widget->tooltip) {
1080 free(widget->tooltip);
1081 widget->tooltip = NULL;
1084 wl_list_for_each(input, &display->input_list, link) {
1085 if (input->focus_widget == widget)
1086 input->focus_widget = NULL;
1089 wl_list_remove(&widget->link);
1094 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1096 *allocation = widget->allocation;
1100 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1102 widget->allocation.width = width;
1103 widget->allocation.height = height;
1107 widget_set_allocation(struct widget *widget,
1108 int32_t x, int32_t y, int32_t width, int32_t height)
1110 widget->allocation.x = x;
1111 widget->allocation.y = y;
1112 widget_set_size(widget, width, height);
1116 widget_set_transparent(struct widget *widget, int transparent)
1118 widget->opaque = !transparent;
1122 widget_get_user_data(struct widget *widget)
1124 return widget->user_data;
1128 widget_set_resize_handler(struct widget *widget,
1129 widget_resize_handler_t handler)
1131 widget->resize_handler = handler;
1135 widget_set_redraw_handler(struct widget *widget,
1136 widget_redraw_handler_t handler)
1138 widget->redraw_handler = handler;
1142 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1144 widget->enter_handler = handler;
1148 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1150 widget->leave_handler = handler;
1154 widget_set_motion_handler(struct widget *widget,
1155 widget_motion_handler_t handler)
1157 widget->motion_handler = handler;
1161 widget_set_button_handler(struct widget *widget,
1162 widget_button_handler_t handler)
1164 widget->button_handler = handler;
1168 widget_set_axis_handler(struct widget *widget,
1169 widget_axis_handler_t handler)
1171 widget->axis_handler = handler;
1175 widget_schedule_redraw(struct widget *widget)
1177 window_schedule_redraw(widget->window);
1181 window_get_surface(struct window *window)
1183 return cairo_surface_reference(window->cairo_surface);
1187 window_get_wl_surface(struct window *window)
1189 return window->surface;
1192 struct wl_shell_surface *
1193 window_get_wl_shell_surface(struct window *window)
1195 return window->shell_surface;
1199 tooltip_redraw_handler(struct widget *widget, void *data)
1202 const int32_t r = 3;
1203 struct tooltip *tooltip = data;
1204 int32_t width, height;
1205 struct window *window = widget->window;
1207 cr = cairo_create(window->cairo_surface);
1208 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1209 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1212 width = window->allocation.width;
1213 height = window->allocation.height;
1214 rounded_rect(cr, 0, 0, width, height, r);
1216 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1217 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1220 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1221 cairo_move_to(cr, 10, 16);
1222 cairo_show_text(cr, tooltip->entry);
1226 static cairo_text_extents_t
1227 get_text_extents(struct tooltip *tooltip)
1229 struct window *window;
1231 cairo_text_extents_t extents;
1233 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1235 window = tooltip->widget->window->parent;
1236 cr = cairo_create(window->cairo_surface);
1237 cairo_text_extents(cr, tooltip->entry, &extents);
1244 window_create_tooltip(struct tooltip *tooltip)
1246 struct widget *parent = tooltip->parent;
1247 struct display *display = parent->window->display;
1248 struct window *window;
1249 const int offset_y = 27;
1250 const int margin = 3;
1251 cairo_text_extents_t extents;
1253 if (tooltip->widget)
1256 window = window_create_transient(display, parent->window, tooltip->x,
1257 tooltip->y + offset_y,
1258 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1262 tooltip->window = window;
1263 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1265 extents = get_text_extents(tooltip);
1266 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1267 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1273 widget_destroy_tooltip(struct widget *parent)
1275 struct tooltip *tooltip = parent->tooltip;
1277 parent->tooltip_count = 0;
1281 if (tooltip->widget) {
1282 widget_destroy(tooltip->widget);
1283 window_destroy(tooltip->window);
1284 tooltip->widget = NULL;
1285 tooltip->window = NULL;
1288 close(tooltip->tooltip_fd);
1289 free(tooltip->entry);
1291 parent->tooltip = NULL;
1295 tooltip_func(struct task *task, uint32_t events)
1297 struct tooltip *tooltip =
1298 container_of(task, struct tooltip, tooltip_task);
1301 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1303 window_create_tooltip(tooltip);
1306 #define TOOLTIP_TIMEOUT 500
1308 tooltip_timer_reset(struct tooltip *tooltip)
1310 struct itimerspec its;
1312 its.it_interval.tv_sec = 0;
1313 its.it_interval.tv_nsec = 0;
1314 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1315 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1316 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1317 fprintf(stderr, "could not set timerfd\n: %m");
1325 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1327 struct tooltip *tooltip = parent->tooltip;
1329 parent->tooltip_count++;
1333 tooltip_timer_reset(tooltip);
1337 /* the handler might be triggered too fast via input device motion, so
1338 * we need this check here to make sure tooltip is fully initialized */
1339 if (parent->tooltip_count > 1)
1342 tooltip = malloc(sizeof *tooltip);
1346 parent->tooltip = tooltip;
1347 tooltip->parent = parent;
1348 tooltip->widget = NULL;
1349 tooltip->window = NULL;
1352 tooltip->entry = strdup(entry);
1353 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1354 if (tooltip->tooltip_fd < 0) {
1355 fprintf(stderr, "could not create timerfd\n: %m");
1359 tooltip->tooltip_task.run = tooltip_func;
1360 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1361 EPOLLIN, &tooltip->tooltip_task);
1362 tooltip_timer_reset(tooltip);
1368 workspace_manager_state(void *data,
1369 struct workspace_manager *workspace_manager,
1373 struct display *display = data;
1375 display->workspace = current;
1376 display->workspace_count = count;
1379 static const struct workspace_manager_listener workspace_manager_listener = {
1380 workspace_manager_state
1384 frame_resize_handler(struct widget *widget,
1385 int32_t width, int32_t height, void *data)
1387 struct frame *frame = data;
1388 struct widget *child = frame->child;
1389 struct rectangle allocation;
1390 struct display *display = widget->window->display;
1391 struct frame_button * button;
1392 struct theme *t = display->theme;
1393 int x_l, x_r, y, w, h;
1394 int decoration_width, decoration_height;
1395 int opaque_margin, shadow_margin;
1397 switch (widget->window->type) {
1398 case TYPE_FULLSCREEN:
1399 decoration_width = 0;
1400 decoration_height = 0;
1404 allocation.width = width;
1405 allocation.height = height;
1408 wl_list_for_each(button, &frame->buttons_list, link)
1409 button->widget->opaque = 1;
1411 case TYPE_MAXIMIZED:
1412 decoration_width = t->width * 2;
1413 decoration_height = t->width + t->titlebar_height;
1415 allocation.x = t->width;
1416 allocation.y = t->titlebar_height;
1417 allocation.width = width - decoration_width;
1418 allocation.height = height - decoration_height;
1422 wl_list_for_each(button, &frame->buttons_list, link)
1423 button->widget->opaque = 0;
1426 decoration_width = (t->width + t->margin) * 2;
1427 decoration_height = t->width +
1428 t->titlebar_height + t->margin * 2;
1430 allocation.x = t->width + t->margin;
1431 allocation.y = t->titlebar_height + t->margin;
1432 allocation.width = width - decoration_width;
1433 allocation.height = height - decoration_height;
1435 opaque_margin = t->margin + t->frame_radius;
1437 wl_list_for_each(button, &frame->buttons_list, link)
1438 button->widget->opaque = 0;
1442 widget_set_allocation(child, allocation.x, allocation.y,
1443 allocation.width, allocation.height);
1445 if (child->resize_handler)
1446 child->resize_handler(child,
1451 width = child->allocation.width + decoration_width;
1452 height = child->allocation.height + decoration_height;
1454 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1456 widget->window->input_region =
1457 wl_compositor_create_region(display->compositor);
1458 if (widget->window->type != TYPE_FULLSCREEN) {
1459 wl_region_add(widget->window->input_region,
1460 shadow_margin, shadow_margin,
1461 width - 2 * shadow_margin,
1462 height - 2 * shadow_margin);
1464 wl_region_add(widget->window->input_region,
1465 0, 0, width, height);
1468 widget_set_allocation(widget, 0, 0, width, height);
1470 if (child->opaque) {
1471 widget->window->opaque_region =
1472 wl_compositor_create_region(display->compositor);
1473 wl_region_add(widget->window->opaque_region,
1474 opaque_margin, opaque_margin,
1475 widget->allocation.width - 2 * opaque_margin,
1476 widget->allocation.height - 2 * opaque_margin);
1479 /* frame internal buttons */
1480 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1481 x_l = t->width + shadow_margin;
1482 y = t->width + shadow_margin;
1483 wl_list_for_each(button, &frame->buttons_list, link) {
1484 const int button_padding = 4;
1485 w = cairo_image_surface_get_width(button->icon);
1486 h = cairo_image_surface_get_height(button->icon);
1488 if (button->decoration == FRAME_BUTTON_FANCY)
1491 if (button->align == FRAME_BUTTON_LEFT) {
1492 widget_set_allocation(button->widget,
1493 x_l, y , w + 1, h + 1);
1495 x_l += button_padding;
1498 widget_set_allocation(button->widget,
1499 x_r, y , w + 1, h + 1);
1500 x_r -= button_padding;
1506 frame_button_enter_handler(struct widget *widget,
1507 struct input *input, float x, float y, void *data)
1509 struct frame_button *frame_button = data;
1511 widget_schedule_redraw(frame_button->widget);
1512 frame_button->state = FRAME_BUTTON_OVER;
1514 return CURSOR_LEFT_PTR;
1518 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1520 struct frame_button *frame_button = data;
1522 widget_schedule_redraw(frame_button->widget);
1523 frame_button->state = FRAME_BUTTON_DEFAULT;
1527 frame_button_button_handler(struct widget *widget,
1528 struct input *input, uint32_t time,
1530 enum wl_pointer_button_state state, void *data)
1532 struct frame_button *frame_button = data;
1533 struct window *window = widget->window;
1534 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1536 if (button != BTN_LEFT)
1540 case WL_POINTER_BUTTON_STATE_PRESSED:
1541 frame_button->state = FRAME_BUTTON_ACTIVE;
1542 widget_schedule_redraw(frame_button->widget);
1544 if (frame_button->type == FRAME_BUTTON_ICON)
1545 window_show_frame_menu(window, input, time);
1547 case WL_POINTER_BUTTON_STATE_RELEASED:
1548 frame_button->state = FRAME_BUTTON_DEFAULT;
1549 widget_schedule_redraw(frame_button->widget);
1556 switch (frame_button->type) {
1557 case FRAME_BUTTON_CLOSE:
1558 if (window->close_handler)
1559 window->close_handler(window->parent,
1562 display_exit(window->display);
1564 case FRAME_BUTTON_MINIMIZE:
1565 fprintf(stderr,"Minimize stub\n");
1567 case FRAME_BUTTON_MAXIMIZE:
1568 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1571 /* Unknown operation */
1577 frame_button_motion_handler(struct widget *widget,
1578 struct input *input, uint32_t time,
1579 float x, float y, void *data)
1581 struct frame_button *frame_button = data;
1582 enum frame_button_pointer previous_button_state = frame_button->state;
1584 /* only track state for a pressed button */
1585 if (input->grab != widget)
1586 return CURSOR_LEFT_PTR;
1588 if (x > widget->allocation.x &&
1589 x < (widget->allocation.x + widget->allocation.width) &&
1590 y > widget->allocation.y &&
1591 y < (widget->allocation.y + widget->allocation.height)) {
1592 frame_button->state = FRAME_BUTTON_ACTIVE;
1594 frame_button->state = FRAME_BUTTON_DEFAULT;
1597 if (frame_button->state != previous_button_state)
1598 widget_schedule_redraw(frame_button->widget);
1600 return CURSOR_LEFT_PTR;
1604 frame_button_redraw_handler(struct widget *widget, void *data)
1606 struct frame_button *frame_button = data;
1608 int width, height, x, y;
1609 struct window *window = widget->window;
1611 x = widget->allocation.x;
1612 y = widget->allocation.y;
1613 width = widget->allocation.width;
1614 height = widget->allocation.height;
1623 cr = cairo_create(window->cairo_surface);
1625 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1626 cairo_set_line_width(cr, 1);
1628 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1629 cairo_rectangle (cr, x, y, 25, 16);
1631 cairo_stroke_preserve(cr);
1633 switch (frame_button->state) {
1634 case FRAME_BUTTON_DEFAULT:
1635 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1637 case FRAME_BUTTON_OVER:
1638 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1640 case FRAME_BUTTON_ACTIVE:
1641 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1650 cairo_set_source_surface(cr, frame_button->icon, x, y);
1656 static struct widget *
1657 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1658 enum frame_button_align align, enum frame_button_decoration style)
1660 struct frame_button *frame_button;
1661 const char *icon = data;
1663 frame_button = malloc (sizeof *frame_button);
1664 memset(frame_button, 0, sizeof *frame_button);
1666 frame_button->icon = cairo_image_surface_create_from_png(icon);
1667 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1668 frame_button->frame = frame;
1669 frame_button->type = type;
1670 frame_button->align = align;
1671 frame_button->decoration = style;
1673 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1675 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1676 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1677 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1678 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1679 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
1680 return frame_button->widget;
1684 frame_button_destroy(struct frame_button *frame_button)
1686 widget_destroy(frame_button->widget);
1687 wl_list_remove(&frame_button->link);
1688 cairo_surface_destroy(frame_button->icon);
1695 frame_redraw_handler(struct widget *widget, void *data)
1698 struct window *window = widget->window;
1699 struct theme *t = window->display->theme;
1702 if (window->type == TYPE_FULLSCREEN)
1705 cr = cairo_create(window->cairo_surface);
1707 if (window->focus_count)
1708 flags |= THEME_FRAME_ACTIVE;
1709 if (window->type == TYPE_MAXIMIZED)
1710 flags |= THEME_FRAME_MAXIMIZED;
1711 theme_render_frame(t, cr, widget->allocation.width,
1712 widget->allocation.height, window->title, flags);
1718 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1720 struct theme *t = frame->widget->window->display->theme;
1721 struct window *window = frame->widget->window;
1724 if (window->type != TYPE_TOPLEVEL)
1725 return CURSOR_LEFT_PTR;
1727 location = theme_get_location(t, input->sx, input->sy,
1728 frame->widget->allocation.width,
1729 frame->widget->allocation.height,
1730 window->type == TYPE_MAXIMIZED ?
1731 THEME_FRAME_MAXIMIZED : 0);
1734 case THEME_LOCATION_RESIZING_TOP:
1736 case THEME_LOCATION_RESIZING_BOTTOM:
1737 return CURSOR_BOTTOM;
1738 case THEME_LOCATION_RESIZING_LEFT:
1740 case THEME_LOCATION_RESIZING_RIGHT:
1741 return CURSOR_RIGHT;
1742 case THEME_LOCATION_RESIZING_TOP_LEFT:
1743 return CURSOR_TOP_LEFT;
1744 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1745 return CURSOR_TOP_RIGHT;
1746 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1747 return CURSOR_BOTTOM_LEFT;
1748 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1749 return CURSOR_BOTTOM_RIGHT;
1750 case THEME_LOCATION_EXTERIOR:
1751 case THEME_LOCATION_TITLEBAR:
1753 return CURSOR_LEFT_PTR;
1758 frame_menu_func(struct window *window, int index, void *data)
1760 struct display *display;
1764 if (window->close_handler)
1765 window->close_handler(window->parent,
1768 display_exit(window->display);
1770 case 1: /* move to workspace above */
1771 display = window->display;
1772 if (display->workspace > 0)
1773 workspace_manager_move_surface(display->workspace_manager,
1775 display->workspace - 1);
1777 case 2: /* move to workspace below */
1778 display = window->display;
1779 if (display->workspace < display->workspace_count - 1)
1780 workspace_manager_move_surface(display->workspace_manager,
1782 display->workspace + 1);
1784 case 3: /* fullscreen */
1785 /* we don't have a way to get out of fullscreen for now */
1786 if (window->fullscreen_handler)
1787 window->fullscreen_handler(window, window->user_data);
1793 window_show_frame_menu(struct window *window,
1794 struct input *input, uint32_t time)
1799 static const char *entries[] = {
1801 "Move to workspace above", "Move to workspace below",
1805 if (window->fullscreen_handler)
1806 count = ARRAY_LENGTH(entries);
1808 count = ARRAY_LENGTH(entries) - 1;
1810 input_get_position(input, &x, &y);
1811 window_show_menu(window->display, input, time, window,
1812 x - 10, y - 10, frame_menu_func, entries, count);
1816 frame_enter_handler(struct widget *widget,
1817 struct input *input, float x, float y, void *data)
1819 return frame_get_pointer_image_for_location(data, input);
1823 frame_motion_handler(struct widget *widget,
1824 struct input *input, uint32_t time,
1825 float x, float y, void *data)
1827 return frame_get_pointer_image_for_location(data, input);
1831 frame_button_handler(struct widget *widget,
1832 struct input *input, uint32_t time,
1833 uint32_t button, enum wl_pointer_button_state state,
1837 struct frame *frame = data;
1838 struct window *window = widget->window;
1839 struct display *display = window->display;
1842 if (window->type != TYPE_TOPLEVEL)
1845 location = theme_get_location(display->theme, input->sx, input->sy,
1846 frame->widget->allocation.width,
1847 frame->widget->allocation.height,
1848 window->type == TYPE_MAXIMIZED ?
1849 THEME_FRAME_MAXIMIZED : 0);
1851 if (window->display->shell && button == BTN_LEFT &&
1852 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1854 case THEME_LOCATION_TITLEBAR:
1855 if (!window->shell_surface)
1857 input_ungrab(input);
1858 wl_shell_surface_move(window->shell_surface,
1859 input_get_seat(input),
1862 case THEME_LOCATION_RESIZING_TOP:
1863 case THEME_LOCATION_RESIZING_BOTTOM:
1864 case THEME_LOCATION_RESIZING_LEFT:
1865 case THEME_LOCATION_RESIZING_RIGHT:
1866 case THEME_LOCATION_RESIZING_TOP_LEFT:
1867 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1868 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1869 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1870 if (!window->shell_surface)
1872 input_ungrab(input);
1874 if (!display->dpy) {
1875 /* If we're using shm, allocate a big
1876 pool to create buffers out of while
1877 we resize. We should probably base
1878 this number on the size of the output. */
1880 shm_pool_create(display, 6 * 1024 * 1024);
1883 wl_shell_surface_resize(window->shell_surface,
1884 input_get_seat(input),
1885 display->serial, location);
1888 } else if (button == BTN_RIGHT &&
1889 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1890 window_show_frame_menu(window, input, time);
1895 frame_create(struct window *window, void *data)
1897 struct frame *frame;
1899 frame = malloc(sizeof *frame);
1900 memset(frame, 0, sizeof *frame);
1902 frame->widget = window_add_widget(window, frame);
1903 frame->child = widget_add_widget(frame->widget, data);
1905 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1906 widget_set_resize_handler(frame->widget, frame_resize_handler);
1907 widget_set_enter_handler(frame->widget, frame_enter_handler);
1908 widget_set_motion_handler(frame->widget, frame_motion_handler);
1909 widget_set_button_handler(frame->widget, frame_button_handler);
1911 /* Create empty list for frame buttons */
1912 wl_list_init(&frame->buttons_list);
1914 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1915 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1917 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1918 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1920 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1921 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1923 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1924 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1926 window->frame = frame;
1928 return frame->child;
1932 frame_set_child_size(struct widget *widget, int child_width, int child_height)
1934 struct display *display = widget->window->display;
1935 struct theme *t = display->theme;
1936 int decoration_width, decoration_height;
1938 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1940 if (widget->window->type != TYPE_FULLSCREEN) {
1941 decoration_width = (t->width + margin) * 2;
1942 decoration_height = t->width +
1943 t->titlebar_height + margin * 2;
1945 width = child_width + decoration_width;
1946 height = child_height + decoration_height;
1948 width = child_width;
1949 height = child_height;
1952 window_schedule_resize(widget->window, width, height);
1956 frame_destroy(struct frame *frame)
1958 struct frame_button *button, *tmp;
1960 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1961 frame_button_destroy(button);
1963 /* frame->child must be destroyed by the application */
1964 widget_destroy(frame->widget);
1969 input_set_focus_widget(struct input *input, struct widget *focus,
1972 struct widget *old, *widget;
1973 int pointer = CURSOR_LEFT_PTR;
1975 if (focus == input->focus_widget)
1978 old = input->focus_widget;
1982 widget = input->grab;
1983 if (widget->leave_handler)
1984 widget->leave_handler(old, input, widget->user_data);
1985 input->focus_widget = NULL;
1991 widget = input->grab;
1992 input->focus_widget = focus;
1993 if (widget->enter_handler)
1994 pointer = widget->enter_handler(focus, input, x, y,
1997 input_set_pointer_image(input, pointer);
2002 input_grab(struct input *input, struct widget *widget, uint32_t button)
2004 input->grab = widget;
2005 input->grab_button = button;
2009 input_ungrab(struct input *input)
2011 struct widget *widget;
2014 if (input->pointer_focus) {
2015 widget = widget_find_widget(input->pointer_focus->widget,
2016 input->sx, input->sy);
2017 input_set_focus_widget(input, widget, input->sx, input->sy);
2022 input_remove_pointer_focus(struct input *input)
2024 struct window *window = input->pointer_focus;
2029 input_set_focus_widget(input, NULL, 0, 0);
2031 input->pointer_focus = NULL;
2032 input->current_cursor = CURSOR_UNSET;
2036 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2037 uint32_t serial, struct wl_surface *surface,
2038 wl_fixed_t sx_w, wl_fixed_t sy_w)
2040 struct input *input = data;
2041 struct window *window;
2042 struct widget *widget;
2043 float sx = wl_fixed_to_double(sx_w);
2044 float sy = wl_fixed_to_double(sy_w);
2047 /* enter event for a window we've just destroyed */
2051 input->display->serial = serial;
2052 input->pointer_enter_serial = serial;
2053 input->pointer_focus = wl_surface_get_user_data(surface);
2054 window = input->pointer_focus;
2057 shm_pool_destroy(window->pool);
2058 window->pool = NULL;
2059 /* Schedule a redraw to free the pool */
2060 window_schedule_redraw(window);
2066 widget = widget_find_widget(window->widget, sx, sy);
2067 input_set_focus_widget(input, widget, sx, sy);
2071 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2072 uint32_t serial, struct wl_surface *surface)
2074 struct input *input = data;
2076 input->display->serial = serial;
2077 input_remove_pointer_focus(input);
2081 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2082 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2084 struct input *input = data;
2085 struct window *window = input->pointer_focus;
2086 struct widget *widget;
2087 int cursor = CURSOR_LEFT_PTR;
2088 float sx = wl_fixed_to_double(sx_w);
2089 float sy = wl_fixed_to_double(sy_w);
2097 if (!(input->grab && input->grab_button)) {
2098 widget = widget_find_widget(window->widget, sx, sy);
2099 input_set_focus_widget(input, widget, sx, sy);
2103 widget = input->grab;
2105 widget = input->focus_widget;
2106 if (widget && widget->motion_handler)
2107 cursor = widget->motion_handler(input->focus_widget,
2108 input, time, sx, sy,
2111 input_set_pointer_image(input, cursor);
2115 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2116 uint32_t time, uint32_t button, uint32_t state_w)
2118 struct input *input = data;
2119 struct widget *widget;
2120 enum wl_pointer_button_state state = state_w;
2122 input->display->serial = serial;
2123 if (input->focus_widget && input->grab == NULL &&
2124 state == WL_POINTER_BUTTON_STATE_PRESSED)
2125 input_grab(input, input->focus_widget, button);
2127 widget = input->grab;
2128 if (widget && widget->button_handler)
2129 (*widget->button_handler)(widget,
2132 input->grab->user_data);
2134 if (input->grab && input->grab_button == button &&
2135 state == WL_POINTER_BUTTON_STATE_RELEASED)
2136 input_ungrab(input);
2140 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2141 uint32_t time, uint32_t axis, wl_fixed_t value)
2143 struct input *input = data;
2144 struct widget *widget;
2146 widget = input->focus_widget;
2148 widget = input->grab;
2149 if (widget && widget->axis_handler)
2150 (*widget->axis_handler)(widget,
2156 static const struct wl_pointer_listener pointer_listener = {
2157 pointer_handle_enter,
2158 pointer_handle_leave,
2159 pointer_handle_motion,
2160 pointer_handle_button,
2161 pointer_handle_axis,
2165 input_remove_keyboard_focus(struct input *input)
2167 struct window *window = input->keyboard_focus;
2168 struct itimerspec its;
2170 its.it_interval.tv_sec = 0;
2171 its.it_interval.tv_nsec = 0;
2172 its.it_value.tv_sec = 0;
2173 its.it_value.tv_nsec = 0;
2174 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2179 window->focus_count--;
2180 if (window->keyboard_focus_handler)
2181 (*window->keyboard_focus_handler)(window, NULL,
2184 input->keyboard_focus = NULL;
2188 keyboard_repeat_func(struct task *task, uint32_t events)
2190 struct input *input =
2191 container_of(task, struct input, repeat_task);
2192 struct window *window = input->keyboard_focus;
2195 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2196 /* If we change the timer between the fd becoming
2197 * readable and getting here, there'll be nothing to
2198 * read and we get EAGAIN. */
2201 if (window && window->key_handler) {
2202 (*window->key_handler)(window, input, input->repeat_time,
2203 input->repeat_key, input->repeat_sym,
2204 WL_KEYBOARD_KEY_STATE_PRESSED,
2210 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2211 uint32_t format, int fd, uint32_t size)
2213 struct input *input = data;
2221 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2226 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2227 if (map_str == MAP_FAILED) {
2232 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2234 XKB_KEYMAP_FORMAT_TEXT_V1,
2236 munmap(map_str, size);
2239 if (!input->xkb.keymap) {
2240 fprintf(stderr, "failed to compile keymap\n");
2244 input->xkb.state = xkb_state_new(input->xkb.keymap);
2245 if (!input->xkb.state) {
2246 fprintf(stderr, "failed to create XKB state\n");
2247 xkb_map_unref(input->xkb.keymap);
2248 input->xkb.keymap = NULL;
2252 input->xkb.control_mask =
2253 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2254 input->xkb.alt_mask =
2255 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2256 input->xkb.shift_mask =
2257 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2261 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2262 uint32_t serial, struct wl_surface *surface,
2263 struct wl_array *keys)
2265 struct input *input = data;
2266 struct window *window;
2268 input->display->serial = serial;
2269 input->keyboard_focus = wl_surface_get_user_data(surface);
2271 window = input->keyboard_focus;
2272 window->focus_count++;
2273 if (window->keyboard_focus_handler)
2274 (*window->keyboard_focus_handler)(window,
2275 input, window->user_data);
2279 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2280 uint32_t serial, struct wl_surface *surface)
2282 struct input *input = data;
2284 input->display->serial = serial;
2285 input_remove_keyboard_focus(input);
2289 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2290 uint32_t serial, uint32_t time, uint32_t key,
2293 struct input *input = data;
2294 struct window *window = input->keyboard_focus;
2295 uint32_t code, num_syms;
2296 enum wl_keyboard_key_state state = state_w;
2297 const xkb_keysym_t *syms;
2299 struct itimerspec its;
2301 input->display->serial = serial;
2303 if (!window || !input->xkb.state)
2306 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2308 sym = XKB_KEY_NoSymbol;
2312 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2313 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2314 window_set_maximized(window,
2315 window->type != TYPE_MAXIMIZED);
2316 } else if (sym == XKB_KEY_F11 &&
2317 window->fullscreen_handler &&
2318 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2319 window->fullscreen_handler(window, window->user_data);
2320 } else if (sym == XKB_KEY_F4 &&
2321 input->modifiers == MOD_ALT_MASK &&
2322 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2323 if (window->close_handler)
2324 window->close_handler(window->parent,
2327 display_exit(window->display);
2328 } else if (window->key_handler) {
2329 (*window->key_handler)(window, input, time, key,
2330 sym, state, window->user_data);
2333 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2334 key == input->repeat_key) {
2335 its.it_interval.tv_sec = 0;
2336 its.it_interval.tv_nsec = 0;
2337 its.it_value.tv_sec = 0;
2338 its.it_value.tv_nsec = 0;
2339 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2340 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2341 input->repeat_sym = sym;
2342 input->repeat_key = key;
2343 input->repeat_time = time;
2344 its.it_interval.tv_sec = 0;
2345 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2346 its.it_value.tv_sec = 0;
2347 its.it_value.tv_nsec = 400 * 1000 * 1000;
2348 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2353 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2354 uint32_t serial, uint32_t mods_depressed,
2355 uint32_t mods_latched, uint32_t mods_locked,
2358 struct input *input = data;
2359 xkb_mod_mask_t mask;
2361 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2362 mods_locked, 0, 0, group);
2363 mask = xkb_state_serialize_mods(input->xkb.state,
2364 XKB_STATE_DEPRESSED |
2366 input->modifiers = 0;
2367 if (mask & input->xkb.control_mask)
2368 input->modifiers |= MOD_CONTROL_MASK;
2369 if (mask & input->xkb.alt_mask)
2370 input->modifiers |= MOD_ALT_MASK;
2371 if (mask & input->xkb.shift_mask)
2372 input->modifiers |= MOD_SHIFT_MASK;
2375 static const struct wl_keyboard_listener keyboard_listener = {
2376 keyboard_handle_keymap,
2377 keyboard_handle_enter,
2378 keyboard_handle_leave,
2379 keyboard_handle_key,
2380 keyboard_handle_modifiers,
2384 seat_handle_capabilities(void *data, struct wl_seat *seat,
2385 enum wl_seat_capability caps)
2387 struct input *input = data;
2389 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2390 input->pointer = wl_seat_get_pointer(seat);
2391 wl_pointer_set_user_data(input->pointer, input);
2392 wl_pointer_add_listener(input->pointer, &pointer_listener,
2394 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2395 wl_pointer_destroy(input->pointer);
2396 input->pointer = NULL;
2399 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2400 input->keyboard = wl_seat_get_keyboard(seat);
2401 wl_keyboard_set_user_data(input->keyboard, input);
2402 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2404 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2405 wl_keyboard_destroy(input->keyboard);
2406 input->keyboard = NULL;
2410 static const struct wl_seat_listener seat_listener = {
2411 seat_handle_capabilities,
2415 input_get_position(struct input *input, int32_t *x, int32_t *y)
2422 input_get_display(struct input *input)
2424 return input->display;
2428 input_get_seat(struct input *input)
2434 input_get_modifiers(struct input *input)
2436 return input->modifiers;
2440 input_get_focus_widget(struct input *input)
2442 return input->focus_widget;
2446 struct wl_data_offer *offer;
2447 struct input *input;
2448 struct wl_array types;
2451 struct task io_task;
2459 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2461 struct data_offer *offer = data;
2464 p = wl_array_add(&offer->types, sizeof *p);
2468 static const struct wl_data_offer_listener data_offer_listener = {
2473 data_offer_destroy(struct data_offer *offer)
2478 if (offer->refcount == 0) {
2479 wl_data_offer_destroy(offer->offer);
2480 for (p = offer->types.data; *p; p++)
2482 wl_array_release(&offer->types);
2488 data_device_data_offer(void *data,
2489 struct wl_data_device *data_device,
2490 struct wl_data_offer *_offer)
2492 struct data_offer *offer;
2494 offer = malloc(sizeof *offer);
2496 wl_array_init(&offer->types);
2497 offer->refcount = 1;
2498 offer->input = data;
2499 offer->offer = _offer;
2500 wl_data_offer_add_listener(offer->offer,
2501 &data_offer_listener, offer);
2505 data_device_enter(void *data, struct wl_data_device *data_device,
2506 uint32_t serial, struct wl_surface *surface,
2507 wl_fixed_t x_w, wl_fixed_t y_w,
2508 struct wl_data_offer *offer)
2510 struct input *input = data;
2511 struct window *window;
2513 float x = wl_fixed_to_double(x_w);
2514 float y = wl_fixed_to_double(y_w);
2517 input->pointer_enter_serial = serial;
2518 window = wl_surface_get_user_data(surface);
2519 input->pointer_focus = window;
2522 input->drag_offer = wl_data_offer_get_user_data(offer);
2524 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2527 types_data = input->drag_offer->types.data;
2529 input->drag_offer = NULL;
2533 window = input->pointer_focus;
2534 if (window->data_handler)
2535 window->data_handler(window, input, x, y, types_data,
2540 data_device_leave(void *data, struct wl_data_device *data_device)
2542 struct input *input = data;
2544 if (input->drag_offer) {
2545 data_offer_destroy(input->drag_offer);
2546 input->drag_offer = NULL;
2551 data_device_motion(void *data, struct wl_data_device *data_device,
2552 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2554 struct input *input = data;
2555 struct window *window = input->pointer_focus;
2556 float x = wl_fixed_to_double(x_w);
2557 float y = wl_fixed_to_double(y_w);
2563 if (input->drag_offer)
2564 types_data = input->drag_offer->types.data;
2568 if (window->data_handler)
2569 window->data_handler(window, input, x, y, types_data,
2574 data_device_drop(void *data, struct wl_data_device *data_device)
2576 struct input *input = data;
2577 struct window *window = input->pointer_focus;
2579 if (window->drop_handler)
2580 window->drop_handler(window, input,
2581 input->sx, input->sy, window->user_data);
2585 data_device_selection(void *data,
2586 struct wl_data_device *wl_data_device,
2587 struct wl_data_offer *offer)
2589 struct input *input = data;
2592 if (input->selection_offer)
2593 data_offer_destroy(input->selection_offer);
2596 input->selection_offer = wl_data_offer_get_user_data(offer);
2597 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2600 input->selection_offer = NULL;
2604 static const struct wl_data_device_listener data_device_listener = {
2605 data_device_data_offer,
2610 data_device_selection
2614 input_set_pointer_image_index(struct input *input, int index)
2616 struct wl_buffer *buffer;
2617 struct wl_cursor *cursor;
2618 struct wl_cursor_image *image;
2620 if (!input->pointer)
2623 cursor = input->display->cursors[input->current_cursor];
2627 if (index >= (int) cursor->image_count) {
2628 fprintf(stderr, "cursor index out of range\n");
2632 image = cursor->images[index];
2633 buffer = wl_cursor_image_get_buffer(image);
2637 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
2638 input->pointer_surface,
2639 image->hotspot_x, image->hotspot_y);
2640 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2641 wl_surface_damage(input->pointer_surface, 0, 0,
2642 image->width, image->height);
2643 wl_surface_commit(input->pointer_surface);
2646 static const struct wl_callback_listener pointer_surface_listener;
2649 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2652 struct input *input = data;
2653 struct wl_cursor *cursor;
2657 assert(callback == input->cursor_frame_cb);
2658 wl_callback_destroy(callback);
2659 input->cursor_frame_cb = NULL;
2662 if (!input->pointer)
2665 if (input->current_cursor == CURSOR_BLANK) {
2666 wl_pointer_set_cursor(input->pointer,
2667 input->pointer_enter_serial,
2672 if (input->current_cursor == CURSOR_UNSET)
2674 cursor = input->display->cursors[input->current_cursor];
2678 /* FIXME We don't have the current time on the first call so we set
2679 * the animation start to the time of the first frame callback. */
2681 input->cursor_anim_start = 0;
2682 else if (input->cursor_anim_start == 0)
2683 input->cursor_anim_start = time;
2685 if (time == 0 || input->cursor_anim_start == 0)
2688 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2690 if (cursor->image_count > 1) {
2691 input->cursor_frame_cb =
2692 wl_surface_frame(input->pointer_surface);
2693 wl_callback_add_listener(input->cursor_frame_cb,
2694 &pointer_surface_listener, input);
2697 input_set_pointer_image_index(input, i);
2700 static const struct wl_callback_listener pointer_surface_listener = {
2701 pointer_surface_frame_callback
2705 input_set_pointer_image(struct input *input, int pointer)
2709 if (!input->pointer)
2712 if (input->pointer_enter_serial > input->cursor_serial)
2715 if (!force && pointer == input->current_cursor)
2718 input->current_cursor = pointer;
2719 input->cursor_serial = input->pointer_enter_serial;
2720 if (!input->cursor_frame_cb)
2721 pointer_surface_frame_callback(input, NULL, 0);
2723 /* The current frame callback may be stuck if, for instance,
2724 * the set cursor request was processed by the server after
2725 * this client lost the focus. In this case the cursor surface
2726 * might not be mapped and the frame callback wouldn't ever
2727 * complete. Send a set_cursor and attach to try to map the
2728 * cursor surface again so that the callback will finish */
2729 input_set_pointer_image_index(input, 0);
2733 struct wl_data_device *
2734 input_get_data_device(struct input *input)
2736 return input->data_device;
2740 input_set_selection(struct input *input,
2741 struct wl_data_source *source, uint32_t time)
2743 wl_data_device_set_selection(input->data_device, source, time);
2747 input_accept(struct input *input, const char *type)
2749 wl_data_offer_accept(input->drag_offer->offer,
2750 input->pointer_enter_serial, type);
2754 offer_io_func(struct task *task, uint32_t events)
2756 struct data_offer *offer =
2757 container_of(task, struct data_offer, io_task);
2761 len = read(offer->fd, buffer, sizeof buffer);
2762 offer->func(buffer, len,
2763 offer->x, offer->y, offer->user_data);
2767 data_offer_destroy(offer);
2772 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2773 data_func_t func, void *user_data)
2777 if (pipe2(p, O_CLOEXEC) == -1)
2780 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2783 offer->io_task.run = offer_io_func;
2787 offer->user_data = user_data;
2789 display_watch_fd(offer->input->display,
2790 offer->fd, EPOLLIN, &offer->io_task);
2794 input_receive_drag_data(struct input *input, const char *mime_type,
2795 data_func_t func, void *data)
2797 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2798 input->drag_offer->x = input->sx;
2799 input->drag_offer->y = input->sy;
2803 input_receive_selection_data(struct input *input, const char *mime_type,
2804 data_func_t func, void *data)
2808 if (input->selection_offer == NULL)
2811 for (p = input->selection_offer->types.data; *p; p++)
2812 if (strcmp(mime_type, *p) == 0)
2818 data_offer_receive_data(input->selection_offer,
2819 mime_type, func, data);
2824 input_receive_selection_data_to_fd(struct input *input,
2825 const char *mime_type, int fd)
2827 if (input->selection_offer)
2828 wl_data_offer_receive(input->selection_offer->offer,
2835 window_move(struct window *window, struct input *input, uint32_t serial)
2837 if (!window->shell_surface)
2840 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2844 idle_resize(struct window *window)
2846 struct widget *widget;
2848 window->resize_needed = 0;
2849 widget = window->widget;
2850 widget_set_allocation(widget,
2851 window->pending_allocation.x,
2852 window->pending_allocation.y,
2853 window->pending_allocation.width,
2854 window->pending_allocation.height);
2856 if (window->input_region) {
2857 wl_region_destroy(window->input_region);
2858 window->input_region = NULL;
2861 if (window->opaque_region) {
2862 wl_region_destroy(window->opaque_region);
2863 window->opaque_region = NULL;
2866 if (widget->resize_handler)
2867 widget->resize_handler(widget,
2868 widget->allocation.width,
2869 widget->allocation.height,
2872 if (window->allocation.width != widget->allocation.width ||
2873 window->allocation.height != widget->allocation.height) {
2874 window->allocation = widget->allocation;
2875 window_schedule_redraw(window);
2880 window_schedule_resize(struct window *window, int width, int height)
2882 window->pending_allocation.x = 0;
2883 window->pending_allocation.y = 0;
2884 window->pending_allocation.width = width;
2885 window->pending_allocation.height = height;
2887 if (window->min_allocation.width == 0)
2888 window->min_allocation = window->pending_allocation;
2889 if (window->pending_allocation.width < window->min_allocation.width)
2890 window->pending_allocation.width = window->min_allocation.width;
2891 if (window->pending_allocation.height < window->min_allocation.height)
2892 window->pending_allocation.height = window->min_allocation.height;
2894 window->resize_needed = 1;
2895 window_schedule_redraw(window);
2899 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2901 window_schedule_resize(widget->window, width, height);
2905 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2908 wl_shell_surface_pong(shell_surface, serial);
2912 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2913 uint32_t edges, int32_t width, int32_t height)
2915 struct window *window = data;
2917 window->resize_edges = edges;
2918 window_schedule_resize(window, width, height);
2922 menu_destroy(struct menu *menu)
2924 widget_destroy(menu->widget);
2925 window_destroy(menu->window);
2930 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2932 struct window *window = data;
2933 struct menu *menu = window->widget->user_data;
2935 /* FIXME: Need more context in this event, at least the input
2936 * device. Or just use wl_callback. And this really needs to
2937 * be a window vfunc that the menu can set. And we need the
2940 menu->func(window->parent, menu->current, window->parent->user_data);
2941 input_ungrab(menu->input);
2945 static const struct wl_shell_surface_listener shell_surface_listener = {
2952 window_get_allocation(struct window *window,
2953 struct rectangle *allocation)
2955 *allocation = window->allocation;
2959 widget_redraw(struct widget *widget)
2961 struct widget *child;
2963 if (widget->redraw_handler)
2964 widget->redraw_handler(widget, widget->user_data);
2965 wl_list_for_each(child, &widget->child_list, link)
2966 widget_redraw(child);
2970 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2972 struct window *window = data;
2974 assert(callback == window->frame_cb);
2975 wl_callback_destroy(callback);
2976 window->frame_cb = 0;
2977 window->redraw_scheduled = 0;
2978 if (window->redraw_needed)
2979 window_schedule_redraw(window);
2982 static const struct wl_callback_listener listener = {
2987 idle_redraw(struct task *task, uint32_t events)
2989 struct window *window = container_of(task, struct window, redraw_task);
2991 if (window->resize_needed)
2992 idle_resize(window);
2994 window_create_surface(window);
2995 widget_redraw(window->widget);
2996 window->redraw_needed = 0;
2997 wl_list_init(&window->redraw_task.link);
2999 window->frame_cb = wl_surface_frame(window->surface);
3000 wl_callback_add_listener(window->frame_cb, &listener, window);
3001 window_flush(window);
3005 window_schedule_redraw(struct window *window)
3007 window->redraw_needed = 1;
3008 if (!window->redraw_scheduled) {
3009 window->redraw_task.run = idle_redraw;
3010 display_defer(window->display, &window->redraw_task);
3011 window->redraw_scheduled = 1;
3016 window_is_fullscreen(struct window *window)
3018 return window->type == TYPE_FULLSCREEN;
3022 window_set_fullscreen(struct window *window, int fullscreen)
3024 if (!window->display->shell)
3027 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3031 window->type = TYPE_FULLSCREEN;
3032 window->saved_allocation = window->allocation;
3033 wl_shell_surface_set_fullscreen(window->shell_surface,
3034 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3037 window->type = TYPE_TOPLEVEL;
3038 wl_shell_surface_set_toplevel(window->shell_surface);
3039 window_schedule_resize(window,
3040 window->saved_allocation.width,
3041 window->saved_allocation.height);
3046 window_is_maximized(struct window *window)
3048 return window->type == TYPE_MAXIMIZED;
3052 window_set_maximized(struct window *window, int maximized)
3054 if (!window->display->shell)
3057 if ((window->type == TYPE_MAXIMIZED) == maximized)
3060 if (window->type == TYPE_TOPLEVEL) {
3061 window->saved_allocation = window->allocation;
3062 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3063 window->type = TYPE_MAXIMIZED;
3065 wl_shell_surface_set_toplevel(window->shell_surface);
3066 window->type = TYPE_TOPLEVEL;
3067 window_schedule_resize(window,
3068 window->saved_allocation.width,
3069 window->saved_allocation.height);
3074 window_set_user_data(struct window *window, void *data)
3076 window->user_data = data;
3080 window_get_user_data(struct window *window)
3082 return window->user_data;
3086 window_set_key_handler(struct window *window,
3087 window_key_handler_t handler)
3089 window->key_handler = handler;
3093 window_set_keyboard_focus_handler(struct window *window,
3094 window_keyboard_focus_handler_t handler)
3096 window->keyboard_focus_handler = handler;
3100 window_set_data_handler(struct window *window, window_data_handler_t handler)
3102 window->data_handler = handler;
3106 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3108 window->drop_handler = handler;
3112 window_set_close_handler(struct window *window,
3113 window_close_handler_t handler)
3115 window->close_handler = handler;
3119 window_set_fullscreen_handler(struct window *window,
3120 window_fullscreen_handler_t handler)
3122 window->fullscreen_handler = handler;
3126 window_set_title(struct window *window, const char *title)
3128 free(window->title);
3129 window->title = strdup(title);
3130 if (window->shell_surface)
3131 wl_shell_surface_set_title(window->shell_surface, title);
3135 window_get_title(struct window *window)
3137 return window->title;
3141 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3143 struct text_cursor_position *text_cursor_position =
3144 window->display->text_cursor_position;
3146 if (!text_cursor_position)
3149 text_cursor_position_notify(text_cursor_position,
3151 wl_fixed_from_int(x),
3152 wl_fixed_from_int(y));
3156 window_damage(struct window *window, int32_t x, int32_t y,
3157 int32_t width, int32_t height)
3159 wl_surface_damage(window->surface, x, y, width, height);
3163 surface_enter(void *data,
3164 struct wl_surface *wl_surface, struct wl_output *wl_output)
3166 struct window *window = data;
3167 struct output *output;
3168 struct output *output_found = NULL;
3169 struct window_output *window_output;
3171 wl_list_for_each(output, &window->display->output_list, link) {
3172 if (output->output == wl_output) {
3173 output_found = output;
3181 window_output = malloc (sizeof *window_output);
3182 window_output->output = output_found;
3184 wl_list_insert (&window->window_output_list, &window_output->link);
3188 surface_leave(void *data,
3189 struct wl_surface *wl_surface, struct wl_output *output)
3191 struct window *window = data;
3192 struct window_output *window_output;
3193 struct window_output *window_output_found = NULL;
3195 wl_list_for_each(window_output, &window->window_output_list, link) {
3196 if (window_output->output->output == output) {
3197 window_output_found = window_output;
3202 if (window_output_found) {
3203 wl_list_remove(&window_output_found->link);
3204 free(window_output_found);
3208 static const struct wl_surface_listener surface_listener = {
3213 static struct window *
3214 window_create_internal(struct display *display,
3215 struct window *parent, int type)
3217 struct window *window;
3219 window = malloc(sizeof *window);
3223 memset(window, 0, sizeof *window);
3224 window->display = display;
3225 window->parent = parent;
3226 window->surface = wl_compositor_create_surface(display->compositor);
3227 wl_surface_add_listener(window->surface, &surface_listener, window);
3228 if (type != TYPE_CUSTOM && display->shell) {
3229 window->shell_surface =
3230 wl_shell_get_shell_surface(display->shell,
3233 window->allocation.x = 0;
3234 window->allocation.y = 0;
3235 window->allocation.width = 0;
3236 window->allocation.height = 0;
3237 window->saved_allocation = window->allocation;
3238 window->transparent = 1;
3239 window->type = type;
3240 window->input_region = NULL;
3241 window->opaque_region = NULL;
3244 #ifdef HAVE_CAIRO_EGL
3245 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3247 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3250 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3252 wl_surface_set_user_data(window->surface, window);
3253 wl_list_insert(display->window_list.prev, &window->link);
3254 wl_list_init(&window->redraw_task.link);
3256 if (window->shell_surface) {
3257 wl_shell_surface_set_user_data(window->shell_surface, window);
3258 wl_shell_surface_add_listener(window->shell_surface,
3259 &shell_surface_listener, window);
3262 wl_list_init (&window->window_output_list);
3268 window_create(struct display *display)
3270 struct window *window;
3272 window = window_create_internal(display, NULL, TYPE_NONE);
3280 window_create_custom(struct display *display)
3282 struct window *window;
3284 window = window_create_internal(display, NULL, TYPE_CUSTOM);
3292 window_create_transient(struct display *display, struct window *parent,
3293 int32_t x, int32_t y, uint32_t flags)
3295 struct window *window;
3297 window = window_create_internal(parent->display,
3298 parent, TYPE_TRANSIENT);
3306 wl_shell_surface_set_transient(window->shell_surface,
3307 window->parent->surface,
3308 window->x, window->y, flags);
3314 menu_set_item(struct menu *menu, int sy)
3318 next = (sy - 8) / 20;
3319 if (menu->current != next) {
3320 menu->current = next;
3321 widget_schedule_redraw(menu->widget);
3326 menu_motion_handler(struct widget *widget,
3327 struct input *input, uint32_t time,
3328 float x, float y, void *data)
3330 struct menu *menu = data;
3332 if (widget == menu->widget)
3333 menu_set_item(data, y);
3335 return CURSOR_LEFT_PTR;
3339 menu_enter_handler(struct widget *widget,
3340 struct input *input, float x, float y, void *data)
3342 struct menu *menu = data;
3344 if (widget == menu->widget)
3345 menu_set_item(data, y);
3347 return CURSOR_LEFT_PTR;
3351 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3353 struct menu *menu = data;
3355 if (widget == menu->widget)
3356 menu_set_item(data, -200);
3360 menu_button_handler(struct widget *widget,
3361 struct input *input, uint32_t time,
3362 uint32_t button, enum wl_pointer_button_state state,
3366 struct menu *menu = data;
3368 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3369 (menu->release_count > 0 || time - menu->time > 500)) {
3370 /* Either relase after press-drag-release or
3371 * click-motion-click. */
3372 menu->func(menu->window->parent,
3373 menu->current, menu->window->parent->user_data);
3374 input_ungrab(input);
3376 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3377 menu->release_count++;
3382 menu_redraw_handler(struct widget *widget, void *data)
3385 const int32_t r = 3, margin = 3;
3386 struct menu *menu = data;
3387 int32_t width, height, i;
3388 struct window *window = widget->window;
3390 cr = cairo_create(window->cairo_surface);
3391 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3392 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3395 width = window->allocation.width;
3396 height = window->allocation.height;
3397 rounded_rect(cr, 0, 0, width, height, r);
3399 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3400 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3403 for (i = 0; i < menu->count; i++) {
3404 if (i == menu->current) {
3405 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3406 cairo_rectangle(cr, margin, i * 20 + margin,
3407 width - 2 * margin, 20);
3409 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3410 cairo_move_to(cr, 10, i * 20 + 16);
3411 cairo_show_text(cr, menu->entries[i]);
3413 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3414 cairo_move_to(cr, 10, i * 20 + 16);
3415 cairo_show_text(cr, menu->entries[i]);
3423 window_show_menu(struct display *display,
3424 struct input *input, uint32_t time, struct window *parent,
3425 int32_t x, int32_t y,
3426 menu_func_t func, const char **entries, int count)
3428 struct window *window;
3430 const int32_t margin = 3;
3432 menu = malloc(sizeof *menu);
3436 window = window_create_internal(parent->display, parent, TYPE_MENU);
3442 menu->window = window;
3443 menu->widget = window_add_widget(menu->window, menu);
3444 menu->entries = entries;
3445 menu->count = count;
3446 menu->release_count = 0;
3450 menu->input = input;
3451 window->type = TYPE_MENU;
3455 input_ungrab(input);
3456 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3457 display_get_serial(window->display),
3458 window->parent->surface,
3459 window->x, window->y, 0);
3461 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3462 widget_set_enter_handler(menu->widget, menu_enter_handler);
3463 widget_set_leave_handler(menu->widget, menu_leave_handler);
3464 widget_set_motion_handler(menu->widget, menu_motion_handler);
3465 widget_set_button_handler(menu->widget, menu_button_handler);
3467 input_grab(input, menu->widget, 0);
3468 window_schedule_resize(window, 200, count * 20 + margin * 2);
3472 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3474 window->buffer_type = type;
3479 display_handle_geometry(void *data,
3480 struct wl_output *wl_output,
3483 int physical_height,
3489 struct output *output = data;
3491 output->allocation.x = x;
3492 output->allocation.y = y;
3493 output->transform = transform;
3497 display_handle_mode(void *data,
3498 struct wl_output *wl_output,
3504 struct output *output = data;
3505 struct display *display = output->display;
3507 if (flags & WL_OUTPUT_MODE_CURRENT) {
3508 output->allocation.width = width;
3509 output->allocation.height = height;
3510 if (display->output_configure_handler)
3511 (*display->output_configure_handler)(
3512 output, display->user_data);
3516 static const struct wl_output_listener output_listener = {
3517 display_handle_geometry,
3522 display_add_output(struct display *d, uint32_t id)
3524 struct output *output;
3526 output = malloc(sizeof *output);
3530 memset(output, 0, sizeof *output);
3531 output->display = d;
3533 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
3534 wl_list_insert(d->output_list.prev, &output->link);
3536 wl_output_add_listener(output->output, &output_listener, output);
3540 output_destroy(struct output *output)
3542 if (output->destroy_handler)
3543 (*output->destroy_handler)(output, output->user_data);
3545 wl_output_destroy(output->output);
3546 wl_list_remove(&output->link);
3551 display_set_global_handler(struct display *display,
3552 display_global_handler_t handler)
3554 struct global *global;
3556 display->global_handler = handler;
3560 wl_list_for_each(global, &display->global_list, link)
3561 display->global_handler(display,
3562 global->name, global->interface,
3563 global->version, display->user_data);
3567 display_set_output_configure_handler(struct display *display,
3568 display_output_handler_t handler)
3570 struct output *output;
3572 display->output_configure_handler = handler;
3576 wl_list_for_each(output, &display->output_list, link) {
3577 if (output->allocation.width == 0 &&
3578 output->allocation.height == 0)
3581 (*display->output_configure_handler)(output,
3582 display->user_data);
3587 output_set_user_data(struct output *output, void *data)
3589 output->user_data = data;
3593 output_get_user_data(struct output *output)
3595 return output->user_data;
3599 output_set_destroy_handler(struct output *output,
3600 display_output_handler_t handler)
3602 output->destroy_handler = handler;
3603 /* FIXME: implement this, once we have way to remove outputs */
3607 output_get_allocation(struct output *output, struct rectangle *base)
3609 struct rectangle allocation = output->allocation;
3611 switch (output->transform) {
3612 case WL_OUTPUT_TRANSFORM_90:
3613 case WL_OUTPUT_TRANSFORM_270:
3614 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3615 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3616 /* Swap width and height */
3617 allocation.width = output->allocation.height;
3618 allocation.height = output->allocation.width;
3626 output_get_wl_output(struct output *output)
3628 return output->output;
3632 fini_xkb(struct input *input)
3634 xkb_state_unref(input->xkb.state);
3635 xkb_map_unref(input->xkb.keymap);
3639 display_add_input(struct display *d, uint32_t id)
3641 struct input *input;
3643 input = malloc(sizeof *input);
3647 memset(input, 0, sizeof *input);
3649 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
3650 input->pointer_focus = NULL;
3651 input->keyboard_focus = NULL;
3652 wl_list_insert(d->input_list.prev, &input->link);
3654 wl_seat_add_listener(input->seat, &seat_listener, input);
3655 wl_seat_set_user_data(input->seat, input);
3657 input->data_device =
3658 wl_data_device_manager_get_data_device(d->data_device_manager,
3660 wl_data_device_add_listener(input->data_device, &data_device_listener,
3663 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3665 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3666 TFD_CLOEXEC | TFD_NONBLOCK);
3667 input->repeat_task.run = keyboard_repeat_func;
3668 display_watch_fd(d, input->repeat_timer_fd,
3669 EPOLLIN, &input->repeat_task);
3673 input_destroy(struct input *input)
3675 input_remove_keyboard_focus(input);
3676 input_remove_pointer_focus(input);
3678 if (input->drag_offer)
3679 data_offer_destroy(input->drag_offer);
3681 if (input->selection_offer)
3682 data_offer_destroy(input->selection_offer);
3684 wl_data_device_destroy(input->data_device);
3687 wl_surface_destroy(input->pointer_surface);
3689 wl_list_remove(&input->link);
3690 wl_seat_destroy(input->seat);
3691 close(input->repeat_timer_fd);
3696 init_workspace_manager(struct display *d, uint32_t id)
3698 d->workspace_manager =
3699 wl_registry_bind(d->registry, id,
3700 &workspace_manager_interface, 1);
3701 if (d->workspace_manager != NULL)
3702 workspace_manager_add_listener(d->workspace_manager,
3703 &workspace_manager_listener,
3708 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3709 const char *interface, uint32_t version)
3711 struct display *d = data;
3712 struct global *global;
3714 global = malloc(sizeof *global);
3716 global->interface = strdup(interface);
3717 global->version = version;
3718 wl_list_insert(d->global_list.prev, &global->link);
3720 if (strcmp(interface, "wl_compositor") == 0) {
3721 d->compositor = wl_registry_bind(registry, id,
3722 &wl_compositor_interface, 1);
3723 } else if (strcmp(interface, "wl_output") == 0) {
3724 display_add_output(d, id);
3725 } else if (strcmp(interface, "wl_seat") == 0) {
3726 display_add_input(d, id);
3727 } else if (strcmp(interface, "wl_shell") == 0) {
3728 d->shell = wl_registry_bind(registry,
3729 id, &wl_shell_interface, 1);
3730 } else if (strcmp(interface, "wl_shm") == 0) {
3731 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
3732 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3733 d->data_device_manager =
3734 wl_registry_bind(registry, id,
3735 &wl_data_device_manager_interface, 1);
3736 } else if (strcmp(interface, "text_cursor_position") == 0) {
3737 d->text_cursor_position =
3738 wl_registry_bind(registry, id,
3739 &text_cursor_position_interface, 1);
3740 } else if (strcmp(interface, "workspace_manager") == 0) {
3741 init_workspace_manager(d, id);
3744 if (d->global_handler)
3745 d->global_handler(d, id, interface, version, d->user_data);
3749 display_bind(struct display *display, uint32_t name,
3750 const struct wl_interface *interface, uint32_t version)
3752 return wl_registry_bind(display->registry, name, interface, version);
3755 static const struct wl_registry_listener registry_listener = {
3756 registry_handle_global
3759 #ifdef HAVE_CAIRO_EGL
3761 init_egl(struct display *d)
3763 EGLint major, minor;
3766 #ifdef USE_CAIRO_GLESV2
3767 # define GL_BIT EGL_OPENGL_ES2_BIT
3769 # define GL_BIT EGL_OPENGL_BIT
3772 static const EGLint argb_cfg_attribs[] = {
3773 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3779 EGL_RENDERABLE_TYPE, GL_BIT,
3783 #ifdef USE_CAIRO_GLESV2
3784 static const EGLint context_attribs[] = {
3785 EGL_CONTEXT_CLIENT_VERSION, 2,
3788 EGLint api = EGL_OPENGL_ES_API;
3790 EGLint *context_attribs = NULL;
3791 EGLint api = EGL_OPENGL_API;
3794 d->dpy = eglGetDisplay(d->display);
3795 if (!eglInitialize(d->dpy, &major, &minor)) {
3796 fprintf(stderr, "failed to initialize display\n");
3800 if (!eglBindAPI(api)) {
3801 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3805 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3806 &d->argb_config, 1, &n) || n != 1) {
3807 fprintf(stderr, "failed to choose argb config\n");
3811 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3812 EGL_NO_CONTEXT, context_attribs);
3813 if (d->argb_ctx == NULL) {
3814 fprintf(stderr, "failed to create context\n");
3818 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3819 fprintf(stderr, "failed to make context current\n");
3823 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3824 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3825 fprintf(stderr, "failed to get cairo egl argb device\n");
3833 fini_egl(struct display *display)
3835 cairo_device_destroy(display->argb_device);
3837 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3840 eglTerminate(display->dpy);
3846 handle_display_data(struct task *task, uint32_t events)
3848 struct display *display =
3849 container_of(task, struct display, display_task);
3850 struct epoll_event ep;
3853 display->display_fd_events = events;
3855 if (events & EPOLLERR || events & EPOLLHUP) {
3856 display_exit(display);
3860 if (events & EPOLLIN) {
3861 ret = wl_display_dispatch(display->display);
3863 display_exit(display);
3868 if (events & EPOLLOUT) {
3869 ret = wl_display_flush(display->display);
3871 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3872 ep.data.ptr = &display->display_task;
3873 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3874 display->display_fd, &ep);
3875 } else if (ret == -1 && errno != EAGAIN) {
3876 display_exit(display);
3883 display_create(int argc, char *argv[])
3887 d = malloc(sizeof *d);
3891 memset(d, 0, sizeof *d);
3893 d->display = wl_display_connect(NULL);
3894 if (d->display == NULL) {
3895 fprintf(stderr, "failed to create display: %m\n");
3899 d->epoll_fd = os_epoll_create_cloexec();
3900 d->display_fd = wl_display_get_fd(d->display);
3901 d->display_task.run = handle_display_data;
3902 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3905 wl_list_init(&d->deferred_list);
3906 wl_list_init(&d->input_list);
3907 wl_list_init(&d->output_list);
3908 wl_list_init(&d->global_list);
3910 d->xkb_context = xkb_context_new(0);
3911 if (d->xkb_context == NULL) {
3912 fprintf(stderr, "Failed to create XKB context\n");
3917 d->workspace_count = 1;
3919 d->registry = wl_display_get_registry(d->display);
3920 wl_registry_add_listener(d->registry, ®istry_listener, d);
3921 wl_display_dispatch(d->display);
3922 #ifdef HAVE_CAIRO_EGL
3923 if (init_egl(d) < 0)
3929 d->theme = theme_create();
3931 wl_list_init(&d->window_list);
3937 display_destroy_outputs(struct display *display)
3940 struct output *output;
3942 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3943 output_destroy(output);
3947 display_destroy_inputs(struct display *display)
3950 struct input *input;
3952 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3953 input_destroy(input);
3957 display_destroy(struct display *display)
3959 if (!wl_list_empty(&display->window_list))
3960 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3961 wl_list_length(&display->window_list));
3963 if (!wl_list_empty(&display->deferred_list))
3964 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3966 display_destroy_outputs(display);
3967 display_destroy_inputs(display);
3969 xkb_context_unref(display->xkb_context);
3971 theme_destroy(display->theme);
3972 destroy_cursors(display);
3974 #ifdef HAVE_CAIRO_EGL
3979 wl_shell_destroy(display->shell);
3982 wl_shm_destroy(display->shm);
3984 if (display->data_device_manager)
3985 wl_data_device_manager_destroy(display->data_device_manager);
3987 wl_compositor_destroy(display->compositor);
3989 close(display->epoll_fd);
3991 if (!(display->display_fd_events & EPOLLERR) &&
3992 !(display->display_fd_events & EPOLLHUP))
3993 wl_display_flush(display->display);
3995 wl_display_disconnect(display->display);
4000 display_set_user_data(struct display *display, void *data)
4002 display->user_data = data;
4006 display_get_user_data(struct display *display)
4008 return display->user_data;
4012 display_get_display(struct display *display)
4014 return display->display;
4018 display_get_output(struct display *display)
4020 return container_of(display->output_list.next, struct output, link);
4023 struct wl_compositor *
4024 display_get_compositor(struct display *display)
4026 return display->compositor;
4030 display_get_serial(struct display *display)
4032 return display->serial;
4036 display_get_egl_display(struct display *d)
4041 struct wl_data_source *
4042 display_create_data_source(struct display *display)
4044 return wl_data_device_manager_create_data_source(display->data_device_manager);
4048 display_get_argb_egl_config(struct display *d)
4050 return d->argb_config;
4054 display_get_shell(struct display *display)
4056 return display->shell;
4060 display_acquire_window_surface(struct display *display,
4061 struct window *window,
4064 #ifdef HAVE_CAIRO_EGL
4065 struct egl_window_surface_data *data;
4066 cairo_device_t *device;
4068 if (!window->cairo_surface)
4070 device = cairo_surface_get_device(window->cairo_surface);
4075 if (device == display->argb_device)
4076 ctx = display->argb_ctx;
4081 data = cairo_surface_get_user_data(window->cairo_surface,
4084 cairo_device_flush(device);
4085 cairo_device_acquire(device);
4086 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4087 fprintf(stderr, "failed to make surface current\n");
4096 display_release_window_surface(struct display *display,
4097 struct window *window)
4099 #ifdef HAVE_CAIRO_EGL
4100 cairo_device_t *device;
4102 device = cairo_surface_get_device(window->cairo_surface);
4106 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
4107 fprintf(stderr, "failed to make context current\n");
4108 cairo_device_release(device);
4113 display_defer(struct display *display, struct task *task)
4115 wl_list_insert(&display->deferred_list, &task->link);
4119 display_watch_fd(struct display *display,
4120 int fd, uint32_t events, struct task *task)
4122 struct epoll_event ep;
4126 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4130 display_unwatch_fd(struct display *display, int fd)
4132 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4136 display_run(struct display *display)
4139 struct epoll_event ep[16];
4142 display->running = 1;
4144 while (!wl_list_empty(&display->deferred_list)) {
4145 task = container_of(display->deferred_list.prev,
4147 wl_list_remove(&task->link);
4151 wl_display_dispatch_pending(display->display);
4153 if (!display->running)
4156 ret = wl_display_flush(display->display);
4157 if (ret < 0 && errno == EAGAIN) {
4159 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4160 ep[0].data.ptr = &display->display_task;
4162 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4163 display->display_fd, &ep[0]);
4164 } else if (ret < 0) {
4168 count = epoll_wait(display->epoll_fd,
4169 ep, ARRAY_LENGTH(ep), -1);
4170 for (i = 0; i < count; i++) {
4171 task = ep[i].data.ptr;
4172 task->run(task, ep[i].events);
4178 display_exit(struct display *display)
4180 display->running = 0;