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>
45 #include <wayland-egl.h>
47 #ifdef USE_CAIRO_GLESV2
48 #include <GLES2/gl2.h>
49 #include <GLES2/gl2ext.h>
54 #include <EGL/eglext.h>
57 #else /* HAVE_CAIRO_EGL */
58 typedef void *EGLDisplay;
59 typedef void *EGLConfig;
60 typedef void *EGLContext;
61 #define EGL_NO_DISPLAY ((EGLDisplay)0)
62 #endif /* no HAVE_CAIRO_EGL */
64 #include <xkbcommon/xkbcommon.h>
65 #include <wayland-cursor.h>
67 #include <linux/input.h>
68 #include <wayland-client.h>
69 #include "../shared/cairo-util.h"
70 #include "text-cursor-position-client-protocol.h"
71 #include "workspaces-client-protocol.h"
72 #include "../shared/os-compatibility.h"
86 struct wl_display *display;
87 struct wl_registry *registry;
88 struct wl_compositor *compositor;
89 struct wl_shell *shell;
91 struct wl_data_device_manager *data_device_manager;
92 struct text_cursor_position *text_cursor_position;
93 struct workspace_manager *workspace_manager;
95 EGLConfig argb_config;
97 cairo_device_t *argb_device;
101 uint32_t display_fd_events;
102 struct task display_task;
105 struct wl_list deferred_list;
109 struct wl_list global_list;
110 struct wl_list window_list;
111 struct wl_list input_list;
112 struct wl_list output_list;
116 struct wl_cursor_theme *cursor_theme;
117 struct wl_cursor **cursors;
119 display_output_handler_t output_configure_handler;
120 display_global_handler_t global_handler;
124 struct xkb_context *xkb_context;
127 uint32_t workspace_count;
140 struct window_output {
141 struct output *output;
146 struct display *display;
147 struct window *parent;
148 struct wl_list window_output_list;
149 struct wl_surface *surface;
150 struct wl_shell_surface *shell_surface;
151 struct wl_region *input_region;
152 struct wl_region *opaque_region;
154 struct rectangle allocation, saved_allocation, server_allocation;
155 struct rectangle min_allocation;
156 struct rectangle pending_allocation;
159 int redraw_scheduled;
161 struct task redraw_task;
167 enum window_buffer_type buffer_type;
169 cairo_surface_t *cairo_surface;
171 struct shm_pool *pool;
173 window_key_handler_t key_handler;
174 window_keyboard_focus_handler_t keyboard_focus_handler;
175 window_data_handler_t data_handler;
176 window_drop_handler_t drop_handler;
177 window_close_handler_t close_handler;
178 window_fullscreen_handler_t fullscreen_handler;
180 struct wl_callback *frame_cb;
183 struct widget *widget;
190 struct window *window;
191 struct tooltip *tooltip;
192 struct wl_list child_list;
194 struct rectangle allocation;
195 widget_resize_handler_t resize_handler;
196 widget_redraw_handler_t redraw_handler;
197 widget_enter_handler_t enter_handler;
198 widget_leave_handler_t leave_handler;
199 widget_motion_handler_t motion_handler;
200 widget_button_handler_t button_handler;
201 widget_axis_handler_t axis_handler;
208 struct display *display;
209 struct wl_seat *seat;
210 struct wl_pointer *pointer;
211 struct wl_keyboard *keyboard;
212 struct window *pointer_focus;
213 struct window *keyboard_focus;
215 uint32_t cursor_anim_start;
216 struct wl_callback *cursor_frame_cb;
217 struct wl_surface *pointer_surface;
219 uint32_t pointer_enter_serial;
220 uint32_t cursor_serial;
224 struct widget *focus_widget;
226 uint32_t grab_button;
228 struct wl_data_device *data_device;
229 struct data_offer *drag_offer;
230 struct data_offer *selection_offer;
233 struct xkb_keymap *keymap;
234 struct xkb_state *state;
235 xkb_mod_mask_t control_mask;
236 xkb_mod_mask_t alt_mask;
237 xkb_mod_mask_t shift_mask;
240 struct task repeat_task;
244 uint32_t repeat_time;
248 struct display *display;
249 struct wl_output *output;
250 struct rectangle allocation;
254 display_output_handler_t destroy_handler;
258 enum frame_button_action {
259 FRAME_BUTTON_NULL = 0,
260 FRAME_BUTTON_ICON = 1,
261 FRAME_BUTTON_CLOSE = 2,
262 FRAME_BUTTON_MINIMIZE = 3,
263 FRAME_BUTTON_MAXIMIZE = 4,
266 enum frame_button_pointer {
267 FRAME_BUTTON_DEFAULT = 0,
268 FRAME_BUTTON_OVER = 1,
269 FRAME_BUTTON_ACTIVE = 2,
272 enum frame_button_align {
273 FRAME_BUTTON_RIGHT = 0,
274 FRAME_BUTTON_LEFT = 1,
277 enum frame_button_decoration {
278 FRAME_BUTTON_NONE = 0,
279 FRAME_BUTTON_FANCY = 1,
282 struct frame_button {
283 struct widget *widget;
285 cairo_surface_t *icon;
286 enum frame_button_action type;
287 enum frame_button_pointer state;
288 struct wl_list link; /* buttons_list */
289 enum frame_button_align align;
290 enum frame_button_decoration decoration;
294 struct widget *widget;
295 struct widget *child;
296 struct wl_list buttons_list;
300 struct window *window;
301 struct widget *widget;
303 const char **entries;
312 struct widget *parent;
313 struct window *window;
314 struct widget *widget;
316 struct task tooltip_task;
322 struct wl_shm_pool *pool;
329 CURSOR_DEFAULT = 100,
333 enum window_location {
335 WINDOW_RESIZING_TOP = 1,
336 WINDOW_RESIZING_BOTTOM = 2,
337 WINDOW_RESIZING_LEFT = 4,
338 WINDOW_RESIZING_TOP_LEFT = 5,
339 WINDOW_RESIZING_BOTTOM_LEFT = 6,
340 WINDOW_RESIZING_RIGHT = 8,
341 WINDOW_RESIZING_TOP_RIGHT = 9,
342 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
343 WINDOW_RESIZING_MASK = 15,
344 WINDOW_EXTERIOR = 16,
345 WINDOW_TITLEBAR = 17,
346 WINDOW_CLIENT_AREA = 18,
349 static const cairo_user_data_key_t surface_data_key;
350 struct surface_data {
351 struct wl_buffer *buffer;
354 #define MULT(_d,c,a,t) \
355 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
357 #ifdef HAVE_CAIRO_EGL
359 struct egl_window_surface_data {
360 struct display *display;
361 struct wl_surface *surface;
362 struct wl_egl_window *window;
367 egl_window_surface_data_destroy(void *p)
369 struct egl_window_surface_data *data = p;
370 struct display *d = data->display;
372 eglDestroySurface(d->dpy, data->surf);
373 wl_egl_window_destroy(data->window);
374 data->surface = NULL;
379 static cairo_surface_t *
380 display_create_egl_window_surface(struct display *display,
381 struct wl_surface *surface,
383 struct rectangle *rectangle)
385 cairo_surface_t *cairo_surface;
386 struct egl_window_surface_data *data;
388 cairo_device_t *device;
390 data = malloc(sizeof *data);
394 data->display = display;
395 data->surface = surface;
397 config = display->argb_config;
398 device = display->argb_device;
400 data->window = wl_egl_window_create(surface,
404 data->surf = eglCreateWindowSurface(display->dpy, config,
407 cairo_surface = cairo_gl_surface_create_for_egl(device,
412 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
413 data, egl_window_surface_data_destroy);
415 return cairo_surface;
421 display_get_buffer_for_surface(struct display *display,
422 cairo_surface_t *surface)
424 struct surface_data *data;
426 data = cairo_surface_get_user_data (surface, &surface_data_key);
431 struct shm_surface_data {
432 struct surface_data data;
433 struct shm_pool *pool;
437 shm_pool_destroy(struct shm_pool *pool);
440 shm_surface_data_destroy(void *p)
442 struct shm_surface_data *data = p;
444 wl_buffer_destroy(data->data.buffer);
446 shm_pool_destroy(data->pool);
451 static struct wl_shm_pool *
452 make_shm_pool(struct display *display, int size, void **data)
454 struct wl_shm_pool *pool;
457 fd = os_create_anonymous_file(size);
459 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
464 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
465 if (*data == MAP_FAILED) {
466 fprintf(stderr, "mmap failed: %m\n");
471 pool = wl_shm_create_pool(display->shm, fd, size);
478 static struct shm_pool *
479 shm_pool_create(struct display *display, size_t size)
481 struct shm_pool *pool = malloc(sizeof *pool);
486 pool->pool = make_shm_pool(display, size, &pool->data);
499 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
501 if (pool->used + size > pool->size)
504 *offset = pool->used;
507 return (char *) pool->data + *offset;
510 /* destroy the pool. this does not unmap the memory though */
512 shm_pool_destroy(struct shm_pool *pool)
514 munmap(pool->data, pool->size);
515 wl_shm_pool_destroy(pool->pool);
519 /* Start allocating from the beginning of the pool again */
521 shm_pool_reset(struct shm_pool *pool)
527 data_length_for_shm_surface(struct rectangle *rect)
531 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
533 return stride * rect->height;
536 static cairo_surface_t *
537 display_create_shm_surface_from_pool(struct display *display,
538 struct rectangle *rectangle,
539 uint32_t flags, struct shm_pool *pool)
541 struct shm_surface_data *data;
543 cairo_surface_t *surface;
544 int stride, length, offset;
547 data = malloc(sizeof *data);
551 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
553 length = stride * rectangle->height;
555 map = shm_pool_allocate(pool, length, &offset);
562 surface = cairo_image_surface_create_for_data (map,
568 cairo_surface_set_user_data (surface, &surface_data_key,
569 data, shm_surface_data_destroy);
571 if (flags & SURFACE_OPAQUE)
572 format = WL_SHM_FORMAT_XRGB8888;
574 format = WL_SHM_FORMAT_ARGB8888;
576 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
584 static cairo_surface_t *
585 display_create_shm_surface(struct display *display,
586 struct rectangle *rectangle, uint32_t flags,
587 struct window *window)
589 struct shm_surface_data *data;
590 struct shm_pool *pool;
591 cairo_surface_t *surface;
593 if (window && window->pool) {
594 shm_pool_reset(window->pool);
595 surface = display_create_shm_surface_from_pool(display,
603 pool = shm_pool_create(display,
604 data_length_for_shm_surface(rectangle));
609 display_create_shm_surface_from_pool(display, rectangle,
613 shm_pool_destroy(pool);
617 /* make sure we destroy the pool when the surface is destroyed */
618 data = cairo_surface_get_user_data(surface, &surface_data_key);
625 check_size(struct rectangle *rect)
627 if (rect->width && rect->height)
630 fprintf(stderr, "tried to create surface of "
631 "width: %d, height: %d\n", rect->width, rect->height);
636 display_create_surface(struct display *display,
637 struct wl_surface *surface,
638 struct rectangle *rectangle,
641 if (check_size(rectangle) < 0)
643 #ifdef HAVE_CAIRO_EGL
644 if (display->dpy && !(flags & SURFACE_SHM))
645 return display_create_egl_window_surface(display,
650 return display_create_shm_surface(display, rectangle, flags, NULL);
654 * The following correspondences between file names and cursors was copied
655 * from: https://bugs.kde.org/attachment.cgi?id=67313
658 static const char *bottom_left_corners[] = {
659 "bottom_left_corner",
663 static const char *bottom_right_corners[] = {
664 "bottom_right_corner",
668 static const char *bottom_sides[] = {
673 static const char *grabbings[] = {
676 "208530c400c041818281048008011002"
679 static const char *left_ptrs[] = {
686 static const char *left_sides[] = {
691 static const char *right_sides[] = {
696 static const char *top_left_corners[] = {
701 static const char *top_right_corners[] = {
706 static const char *top_sides[] = {
711 static const char *xterms[] = {
717 static const char *hand1s[] = {
721 "e29285e634086352946a0e7090d73106"
724 static const char *watches[] = {
727 "0426c94ea35c87780ff01dc239897213"
730 struct cursor_alternatives {
735 static const struct cursor_alternatives cursors[] = {
736 {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
737 {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
738 {bottom_sides, ARRAY_LENGTH(bottom_sides)},
739 {grabbings, ARRAY_LENGTH(grabbings)},
740 {left_ptrs, ARRAY_LENGTH(left_ptrs)},
741 {left_sides, ARRAY_LENGTH(left_sides)},
742 {right_sides, ARRAY_LENGTH(right_sides)},
743 {top_left_corners, ARRAY_LENGTH(top_left_corners)},
744 {top_right_corners, ARRAY_LENGTH(top_right_corners)},
745 {top_sides, ARRAY_LENGTH(top_sides)},
746 {xterms, ARRAY_LENGTH(xterms)},
747 {hand1s, ARRAY_LENGTH(hand1s)},
748 {watches, ARRAY_LENGTH(watches)},
752 create_cursors(struct display *display)
757 struct wl_cursor *cursor;
758 struct config_key shell_keys[] = {
759 { "cursor-theme", CONFIG_KEY_STRING, &theme },
761 struct config_section cs[] = {
762 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
765 config_file = config_file_path("weston.ini");
766 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
769 display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
771 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
773 for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
775 for (j = 0; !cursor && j < cursors[i].count; ++j)
776 cursor = wl_cursor_theme_get_cursor(
777 display->cursor_theme, cursors[i].names[j]);
780 fprintf(stderr, "could not load cursor '%s'\n",
781 cursors[i].names[0]);
783 display->cursors[i] = cursor;
788 destroy_cursors(struct display *display)
790 wl_cursor_theme_destroy(display->cursor_theme);
791 free(display->cursors);
794 struct wl_cursor_image *
795 display_get_pointer_image(struct display *display, int pointer)
797 struct wl_cursor *cursor = display->cursors[pointer];
799 return cursor ? cursor->images[0] : NULL;
803 window_get_resize_dx_dy(struct window *window, int *x, int *y)
805 if (window->resize_edges & WINDOW_RESIZING_LEFT)
806 *x = window->server_allocation.width - window->allocation.width;
810 if (window->resize_edges & WINDOW_RESIZING_TOP)
811 *y = window->server_allocation.height -
812 window->allocation.height;
816 window->resize_edges = 0;
820 window_attach_surface(struct window *window)
822 struct display *display = window->display;
823 struct wl_buffer *buffer;
824 #ifdef HAVE_CAIRO_EGL
825 struct egl_window_surface_data *data;
829 if (window->type == TYPE_NONE) {
830 window->type = TYPE_TOPLEVEL;
832 wl_shell_surface_set_toplevel(window->shell_surface);
835 if (window->opaque_region) {
836 wl_surface_set_opaque_region(window->surface,
837 window->opaque_region);
838 wl_region_destroy(window->opaque_region);
839 window->opaque_region = NULL;
842 if (window->input_region) {
843 wl_surface_set_input_region(window->surface,
844 window->input_region);
845 wl_region_destroy(window->input_region);
846 window->input_region = NULL;
849 switch (window->buffer_type) {
850 #ifdef HAVE_CAIRO_EGL
851 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
852 data = cairo_surface_get_user_data(window->cairo_surface,
855 cairo_gl_surface_swapbuffers(window->cairo_surface);
856 wl_egl_window_get_attached_size(data->window,
857 &window->server_allocation.width,
858 &window->server_allocation.height);
861 case WINDOW_BUFFER_TYPE_SHM:
863 display_get_buffer_for_surface(display,
864 window->cairo_surface);
866 window_get_resize_dx_dy(window, &x, &y);
867 wl_surface_attach(window->surface, buffer, x, y);
868 wl_surface_damage(window->surface, 0, 0,
869 window->allocation.width,
870 window->allocation.height);
871 wl_surface_commit(window->surface);
872 window->server_allocation = window->allocation;
880 window_has_focus(struct window *window)
882 return window->focus_count > 0;
886 window_flush(struct window *window)
888 if (window->cairo_surface)
889 window_attach_surface(window);
893 window_set_surface(struct window *window, cairo_surface_t *surface)
895 cairo_surface_reference(surface);
897 if (window->cairo_surface != NULL)
898 cairo_surface_destroy(window->cairo_surface);
900 window->cairo_surface = surface;
903 #ifdef HAVE_CAIRO_EGL
905 window_resize_cairo_window_surface(struct window *window)
907 struct egl_window_surface_data *data;
910 data = cairo_surface_get_user_data(window->cairo_surface,
913 window_get_resize_dx_dy(window, &x, &y),
914 wl_egl_window_resize(data->window,
915 window->allocation.width,
916 window->allocation.height,
919 cairo_gl_surface_set_size(window->cairo_surface,
920 window->allocation.width,
921 window->allocation.height);
926 window_get_display(struct window *window)
928 return window->display;
932 window_create_surface(struct window *window)
934 cairo_surface_t *surface;
937 if (!window->transparent)
938 flags = SURFACE_OPAQUE;
940 switch (window->buffer_type) {
941 #ifdef HAVE_CAIRO_EGL
942 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
943 if (window->cairo_surface) {
944 window_resize_cairo_window_surface(window);
947 surface = display_create_surface(window->display,
949 &window->allocation, flags);
952 case WINDOW_BUFFER_TYPE_SHM:
953 surface = display_create_shm_surface(window->display,
962 window_set_surface(window, surface);
963 cairo_surface_destroy(surface);
966 static void frame_destroy(struct frame *frame);
969 window_destroy(struct window *window)
971 struct display *display = window->display;
973 struct window_output *window_output;
974 struct window_output *window_output_tmp;
976 if (window->redraw_scheduled)
977 wl_list_remove(&window->redraw_task.link);
979 wl_list_for_each(input, &display->input_list, link) {
980 if (input->pointer_focus == window)
981 input->pointer_focus = NULL;
982 if (input->keyboard_focus == window)
983 input->keyboard_focus = NULL;
984 if (input->focus_widget &&
985 input->focus_widget->window == window)
986 input->focus_widget = NULL;
989 wl_list_for_each_safe(window_output, window_output_tmp,
990 &window->window_output_list, link) {
991 free (window_output);
994 if (window->input_region)
995 wl_region_destroy(window->input_region);
996 if (window->opaque_region)
997 wl_region_destroy(window->opaque_region);
1000 frame_destroy(window->frame);
1002 if (window->shell_surface)
1003 wl_shell_surface_destroy(window->shell_surface);
1004 wl_surface_destroy(window->surface);
1005 wl_list_remove(&window->link);
1007 if (window->cairo_surface != NULL)
1008 cairo_surface_destroy(window->cairo_surface);
1010 if (window->frame_cb)
1011 wl_callback_destroy(window->frame_cb);
1012 free(window->title);
1016 static struct widget *
1017 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1019 struct widget *child, *target;
1021 wl_list_for_each(child, &widget->child_list, link) {
1022 target = widget_find_widget(child, x, y);
1027 if (widget->allocation.x <= x &&
1028 x < widget->allocation.x + widget->allocation.width &&
1029 widget->allocation.y <= y &&
1030 y < widget->allocation.y + widget->allocation.height) {
1037 static struct widget *
1038 widget_create(struct window *window, void *data)
1040 struct widget *widget;
1042 widget = malloc(sizeof *widget);
1043 memset(widget, 0, sizeof *widget);
1044 widget->window = window;
1045 widget->user_data = data;
1046 widget->allocation = window->allocation;
1047 wl_list_init(&widget->child_list);
1049 widget->tooltip = NULL;
1050 widget->tooltip_count = 0;
1056 window_add_widget(struct window *window, void *data)
1058 window->widget = widget_create(window, data);
1059 wl_list_init(&window->widget->link);
1061 return window->widget;
1065 widget_add_widget(struct widget *parent, void *data)
1067 struct widget *widget;
1069 widget = widget_create(parent->window, data);
1070 wl_list_insert(parent->child_list.prev, &widget->link);
1076 widget_destroy(struct widget *widget)
1078 struct display *display = widget->window->display;
1079 struct input *input;
1081 if (widget->tooltip) {
1082 free(widget->tooltip);
1083 widget->tooltip = NULL;
1086 wl_list_for_each(input, &display->input_list, link) {
1087 if (input->focus_widget == widget)
1088 input->focus_widget = NULL;
1091 wl_list_remove(&widget->link);
1096 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1098 *allocation = widget->allocation;
1102 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1104 widget->allocation.width = width;
1105 widget->allocation.height = height;
1109 widget_set_allocation(struct widget *widget,
1110 int32_t x, int32_t y, int32_t width, int32_t height)
1112 widget->allocation.x = x;
1113 widget->allocation.y = y;
1114 widget_set_size(widget, width, height);
1118 widget_set_transparent(struct widget *widget, int transparent)
1120 widget->opaque = !transparent;
1124 widget_get_user_data(struct widget *widget)
1126 return widget->user_data;
1130 widget_set_resize_handler(struct widget *widget,
1131 widget_resize_handler_t handler)
1133 widget->resize_handler = handler;
1137 widget_set_redraw_handler(struct widget *widget,
1138 widget_redraw_handler_t handler)
1140 widget->redraw_handler = handler;
1144 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1146 widget->enter_handler = handler;
1150 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1152 widget->leave_handler = handler;
1156 widget_set_motion_handler(struct widget *widget,
1157 widget_motion_handler_t handler)
1159 widget->motion_handler = handler;
1163 widget_set_button_handler(struct widget *widget,
1164 widget_button_handler_t handler)
1166 widget->button_handler = handler;
1170 widget_set_axis_handler(struct widget *widget,
1171 widget_axis_handler_t handler)
1173 widget->axis_handler = handler;
1177 widget_schedule_redraw(struct widget *widget)
1179 window_schedule_redraw(widget->window);
1183 window_get_surface(struct window *window)
1185 return cairo_surface_reference(window->cairo_surface);
1189 window_get_wl_surface(struct window *window)
1191 return window->surface;
1194 struct wl_shell_surface *
1195 window_get_wl_shell_surface(struct window *window)
1197 return window->shell_surface;
1201 tooltip_redraw_handler(struct widget *widget, void *data)
1204 const int32_t r = 3;
1205 struct tooltip *tooltip = data;
1206 int32_t width, height;
1207 struct window *window = widget->window;
1209 cr = cairo_create(window->cairo_surface);
1210 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1211 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1214 width = window->allocation.width;
1215 height = window->allocation.height;
1216 rounded_rect(cr, 0, 0, width, height, r);
1218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1219 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1222 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1223 cairo_move_to(cr, 10, 16);
1224 cairo_show_text(cr, tooltip->entry);
1228 static cairo_text_extents_t
1229 get_text_extents(struct tooltip *tooltip)
1231 struct window *window;
1233 cairo_text_extents_t extents;
1235 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1237 window = tooltip->widget->window->parent;
1238 cr = cairo_create(window->cairo_surface);
1239 cairo_text_extents(cr, tooltip->entry, &extents);
1246 window_create_tooltip(struct tooltip *tooltip)
1248 struct widget *parent = tooltip->parent;
1249 struct display *display = parent->window->display;
1250 struct window *window;
1251 const int offset_y = 27;
1252 const int margin = 3;
1253 cairo_text_extents_t extents;
1255 if (tooltip->widget)
1258 window = window_create_transient(display, parent->window, tooltip->x,
1259 tooltip->y + offset_y,
1260 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1264 tooltip->window = window;
1265 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1267 extents = get_text_extents(tooltip);
1268 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1269 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1275 widget_destroy_tooltip(struct widget *parent)
1277 struct tooltip *tooltip = parent->tooltip;
1279 parent->tooltip_count = 0;
1283 if (tooltip->widget) {
1284 widget_destroy(tooltip->widget);
1285 window_destroy(tooltip->window);
1286 tooltip->widget = NULL;
1287 tooltip->window = NULL;
1290 close(tooltip->tooltip_fd);
1291 free(tooltip->entry);
1293 parent->tooltip = NULL;
1297 tooltip_func(struct task *task, uint32_t events)
1299 struct tooltip *tooltip =
1300 container_of(task, struct tooltip, tooltip_task);
1303 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1305 window_create_tooltip(tooltip);
1308 #define TOOLTIP_TIMEOUT 500
1310 tooltip_timer_reset(struct tooltip *tooltip)
1312 struct itimerspec its;
1314 its.it_interval.tv_sec = 0;
1315 its.it_interval.tv_nsec = 0;
1316 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1317 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1318 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1319 fprintf(stderr, "could not set timerfd\n: %m");
1327 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1329 struct tooltip *tooltip = parent->tooltip;
1331 parent->tooltip_count++;
1335 tooltip_timer_reset(tooltip);
1339 /* the handler might be triggered too fast via input device motion, so
1340 * we need this check here to make sure tooltip is fully initialized */
1341 if (parent->tooltip_count > 1)
1344 tooltip = malloc(sizeof *tooltip);
1348 parent->tooltip = tooltip;
1349 tooltip->parent = parent;
1350 tooltip->widget = NULL;
1351 tooltip->window = NULL;
1354 tooltip->entry = strdup(entry);
1355 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1356 if (tooltip->tooltip_fd < 0) {
1357 fprintf(stderr, "could not create timerfd\n: %m");
1361 tooltip->tooltip_task.run = tooltip_func;
1362 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1363 EPOLLIN, &tooltip->tooltip_task);
1364 tooltip_timer_reset(tooltip);
1370 workspace_manager_state(void *data,
1371 struct workspace_manager *workspace_manager,
1375 struct display *display = data;
1377 display->workspace = current;
1378 display->workspace_count = count;
1381 static const struct workspace_manager_listener workspace_manager_listener = {
1382 workspace_manager_state
1386 frame_resize_handler(struct widget *widget,
1387 int32_t width, int32_t height, void *data)
1389 struct frame *frame = data;
1390 struct widget *child = frame->child;
1391 struct rectangle allocation;
1392 struct display *display = widget->window->display;
1393 struct frame_button * button;
1394 struct theme *t = display->theme;
1395 int x_l, x_r, y, w, h;
1396 int decoration_width, decoration_height;
1397 int opaque_margin, shadow_margin;
1399 switch (widget->window->type) {
1400 case TYPE_FULLSCREEN:
1401 decoration_width = 0;
1402 decoration_height = 0;
1406 allocation.width = width;
1407 allocation.height = height;
1410 wl_list_for_each(button, &frame->buttons_list, link)
1411 button->widget->opaque = 1;
1413 case TYPE_MAXIMIZED:
1414 decoration_width = t->width * 2;
1415 decoration_height = t->width + t->titlebar_height;
1417 allocation.x = t->width;
1418 allocation.y = t->titlebar_height;
1419 allocation.width = width - decoration_width;
1420 allocation.height = height - decoration_height;
1424 wl_list_for_each(button, &frame->buttons_list, link)
1425 button->widget->opaque = 0;
1428 decoration_width = (t->width + t->margin) * 2;
1429 decoration_height = t->width +
1430 t->titlebar_height + t->margin * 2;
1432 allocation.x = t->width + t->margin;
1433 allocation.y = t->titlebar_height + t->margin;
1434 allocation.width = width - decoration_width;
1435 allocation.height = height - decoration_height;
1437 opaque_margin = t->margin + t->frame_radius;
1439 wl_list_for_each(button, &frame->buttons_list, link)
1440 button->widget->opaque = 0;
1444 widget_set_allocation(child, allocation.x, allocation.y,
1445 allocation.width, allocation.height);
1447 if (child->resize_handler)
1448 child->resize_handler(child,
1453 width = child->allocation.width + decoration_width;
1454 height = child->allocation.height + decoration_height;
1456 shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1458 widget->window->input_region =
1459 wl_compositor_create_region(display->compositor);
1460 if (widget->window->type != TYPE_FULLSCREEN) {
1461 wl_region_add(widget->window->input_region,
1462 shadow_margin, shadow_margin,
1463 width - 2 * shadow_margin,
1464 height - 2 * shadow_margin);
1466 wl_region_add(widget->window->input_region,
1467 0, 0, width, height);
1470 widget_set_allocation(widget, 0, 0, width, height);
1472 if (child->opaque) {
1473 widget->window->opaque_region =
1474 wl_compositor_create_region(display->compositor);
1475 wl_region_add(widget->window->opaque_region,
1476 opaque_margin, opaque_margin,
1477 widget->allocation.width - 2 * opaque_margin,
1478 widget->allocation.height - 2 * opaque_margin);
1481 /* frame internal buttons */
1482 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1483 x_l = t->width + shadow_margin;
1484 y = t->width + shadow_margin;
1485 wl_list_for_each(button, &frame->buttons_list, link) {
1486 const int button_padding = 4;
1487 w = cairo_image_surface_get_width(button->icon);
1488 h = cairo_image_surface_get_height(button->icon);
1490 if (button->decoration == FRAME_BUTTON_FANCY)
1493 if (button->align == FRAME_BUTTON_LEFT) {
1494 widget_set_allocation(button->widget,
1495 x_l, y , w + 1, h + 1);
1497 x_l += button_padding;
1500 widget_set_allocation(button->widget,
1501 x_r, y , w + 1, h + 1);
1502 x_r -= button_padding;
1508 frame_button_enter_handler(struct widget *widget,
1509 struct input *input, float x, float y, void *data)
1511 struct frame_button *frame_button = data;
1513 widget_schedule_redraw(frame_button->widget);
1514 frame_button->state = FRAME_BUTTON_OVER;
1516 return CURSOR_LEFT_PTR;
1520 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1522 struct frame_button *frame_button = data;
1524 widget_schedule_redraw(frame_button->widget);
1525 frame_button->state = FRAME_BUTTON_DEFAULT;
1529 frame_button_button_handler(struct widget *widget,
1530 struct input *input, uint32_t time,
1532 enum wl_pointer_button_state state, void *data)
1534 struct frame_button *frame_button = data;
1535 struct window *window = widget->window;
1536 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1538 if (button != BTN_LEFT)
1542 case WL_POINTER_BUTTON_STATE_PRESSED:
1543 frame_button->state = FRAME_BUTTON_ACTIVE;
1544 widget_schedule_redraw(frame_button->widget);
1546 if (frame_button->type == FRAME_BUTTON_ICON)
1547 window_show_frame_menu(window, input, time);
1549 case WL_POINTER_BUTTON_STATE_RELEASED:
1550 frame_button->state = FRAME_BUTTON_DEFAULT;
1551 widget_schedule_redraw(frame_button->widget);
1558 switch (frame_button->type) {
1559 case FRAME_BUTTON_CLOSE:
1560 if (window->close_handler)
1561 window->close_handler(window->parent,
1564 display_exit(window->display);
1566 case FRAME_BUTTON_MINIMIZE:
1567 fprintf(stderr,"Minimize stub\n");
1569 case FRAME_BUTTON_MAXIMIZE:
1570 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1573 /* Unknown operation */
1579 frame_button_motion_handler(struct widget *widget,
1580 struct input *input, uint32_t time,
1581 float x, float y, void *data)
1583 struct frame_button *frame_button = data;
1584 enum frame_button_pointer previous_button_state = frame_button->state;
1586 /* only track state for a pressed button */
1587 if (input->grab != widget)
1588 return CURSOR_LEFT_PTR;
1590 if (x > widget->allocation.x &&
1591 x < (widget->allocation.x + widget->allocation.width) &&
1592 y > widget->allocation.y &&
1593 y < (widget->allocation.y + widget->allocation.height)) {
1594 frame_button->state = FRAME_BUTTON_ACTIVE;
1596 frame_button->state = FRAME_BUTTON_DEFAULT;
1599 if (frame_button->state != previous_button_state)
1600 widget_schedule_redraw(frame_button->widget);
1602 return CURSOR_LEFT_PTR;
1606 frame_button_redraw_handler(struct widget *widget, void *data)
1608 struct frame_button *frame_button = data;
1610 int width, height, x, y;
1611 struct window *window = widget->window;
1613 x = widget->allocation.x;
1614 y = widget->allocation.y;
1615 width = widget->allocation.width;
1616 height = widget->allocation.height;
1625 cr = cairo_create(window->cairo_surface);
1627 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1628 cairo_set_line_width(cr, 1);
1630 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1631 cairo_rectangle (cr, x, y, 25, 16);
1633 cairo_stroke_preserve(cr);
1635 switch (frame_button->state) {
1636 case FRAME_BUTTON_DEFAULT:
1637 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1639 case FRAME_BUTTON_OVER:
1640 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1642 case FRAME_BUTTON_ACTIVE:
1643 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1652 cairo_set_source_surface(cr, frame_button->icon, x, y);
1658 static struct widget *
1659 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1660 enum frame_button_align align, enum frame_button_decoration style)
1662 struct frame_button *frame_button;
1663 const char *icon = data;
1665 frame_button = malloc (sizeof *frame_button);
1666 memset(frame_button, 0, sizeof *frame_button);
1668 frame_button->icon = cairo_image_surface_create_from_png(icon);
1669 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1670 frame_button->frame = frame;
1671 frame_button->type = type;
1672 frame_button->align = align;
1673 frame_button->decoration = style;
1675 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1677 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1678 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1679 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1680 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1681 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
1682 return frame_button->widget;
1686 frame_button_destroy(struct frame_button *frame_button)
1688 widget_destroy(frame_button->widget);
1689 wl_list_remove(&frame_button->link);
1690 cairo_surface_destroy(frame_button->icon);
1697 frame_redraw_handler(struct widget *widget, void *data)
1700 struct window *window = widget->window;
1701 struct theme *t = window->display->theme;
1704 if (window->type == TYPE_FULLSCREEN)
1707 cr = cairo_create(window->cairo_surface);
1709 if (window->focus_count)
1710 flags |= THEME_FRAME_ACTIVE;
1711 if (window->type == TYPE_MAXIMIZED)
1712 flags |= THEME_FRAME_MAXIMIZED;
1713 theme_render_frame(t, cr, widget->allocation.width,
1714 widget->allocation.height, window->title, flags);
1720 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1722 struct theme *t = frame->widget->window->display->theme;
1723 struct window *window = frame->widget->window;
1726 location = theme_get_location(t, input->sx, input->sy,
1727 frame->widget->allocation.width,
1728 frame->widget->allocation.height,
1729 window->type == TYPE_MAXIMIZED ?
1730 THEME_FRAME_MAXIMIZED : 0);
1733 case THEME_LOCATION_RESIZING_TOP:
1735 case THEME_LOCATION_RESIZING_BOTTOM:
1736 return CURSOR_BOTTOM;
1737 case THEME_LOCATION_RESIZING_LEFT:
1739 case THEME_LOCATION_RESIZING_RIGHT:
1740 return CURSOR_RIGHT;
1741 case THEME_LOCATION_RESIZING_TOP_LEFT:
1742 return CURSOR_TOP_LEFT;
1743 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1744 return CURSOR_TOP_RIGHT;
1745 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1746 return CURSOR_BOTTOM_LEFT;
1747 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1748 return CURSOR_BOTTOM_RIGHT;
1749 case THEME_LOCATION_EXTERIOR:
1750 case THEME_LOCATION_TITLEBAR:
1752 return CURSOR_LEFT_PTR;
1757 frame_menu_func(struct window *window, int index, void *data)
1759 struct display *display;
1763 if (window->close_handler)
1764 window->close_handler(window->parent,
1767 display_exit(window->display);
1769 case 1: /* move to workspace above */
1770 display = window->display;
1771 if (display->workspace > 0)
1772 workspace_manager_move_surface(display->workspace_manager,
1774 display->workspace - 1);
1776 case 2: /* move to workspace below */
1777 display = window->display;
1778 if (display->workspace < display->workspace_count - 1)
1779 workspace_manager_move_surface(display->workspace_manager,
1781 display->workspace + 1);
1783 case 3: /* fullscreen */
1784 /* we don't have a way to get out of fullscreen for now */
1785 if (window->fullscreen_handler)
1786 window->fullscreen_handler(window, window->user_data);
1792 window_show_frame_menu(struct window *window,
1793 struct input *input, uint32_t time)
1798 static const char *entries[] = {
1800 "Move to workspace above", "Move to workspace below",
1804 if (window->fullscreen_handler)
1805 count = ARRAY_LENGTH(entries);
1807 count = ARRAY_LENGTH(entries) - 1;
1809 input_get_position(input, &x, &y);
1810 window_show_menu(window->display, input, time, window,
1811 x - 10, y - 10, frame_menu_func, entries, count);
1815 frame_enter_handler(struct widget *widget,
1816 struct input *input, float x, float y, void *data)
1818 return frame_get_pointer_image_for_location(data, input);
1822 frame_motion_handler(struct widget *widget,
1823 struct input *input, uint32_t time,
1824 float x, float y, void *data)
1826 return frame_get_pointer_image_for_location(data, input);
1830 frame_button_handler(struct widget *widget,
1831 struct input *input, uint32_t time,
1832 uint32_t button, enum wl_pointer_button_state state,
1836 struct frame *frame = data;
1837 struct window *window = widget->window;
1838 struct display *display = window->display;
1841 location = theme_get_location(display->theme, input->sx, input->sy,
1842 frame->widget->allocation.width,
1843 frame->widget->allocation.height,
1844 window->type == TYPE_MAXIMIZED ?
1845 THEME_FRAME_MAXIMIZED : 0);
1847 if (window->display->shell && button == BTN_LEFT &&
1848 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1850 case THEME_LOCATION_TITLEBAR:
1851 if (!window->shell_surface)
1853 input_ungrab(input);
1854 wl_shell_surface_move(window->shell_surface,
1855 input_get_seat(input),
1858 case THEME_LOCATION_RESIZING_TOP:
1859 case THEME_LOCATION_RESIZING_BOTTOM:
1860 case THEME_LOCATION_RESIZING_LEFT:
1861 case THEME_LOCATION_RESIZING_RIGHT:
1862 case THEME_LOCATION_RESIZING_TOP_LEFT:
1863 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1864 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1865 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1866 if (!window->shell_surface)
1868 input_ungrab(input);
1870 if (!display->dpy) {
1871 /* If we're using shm, allocate a big
1872 pool to create buffers out of while
1873 we resize. We should probably base
1874 this number on the size of the output. */
1876 shm_pool_create(display, 6 * 1024 * 1024);
1879 wl_shell_surface_resize(window->shell_surface,
1880 input_get_seat(input),
1881 display->serial, location);
1884 } else if (button == BTN_RIGHT &&
1885 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1886 window_show_frame_menu(window, input, time);
1891 frame_create(struct window *window, void *data)
1893 struct frame *frame;
1895 frame = malloc(sizeof *frame);
1896 memset(frame, 0, sizeof *frame);
1898 frame->widget = window_add_widget(window, frame);
1899 frame->child = widget_add_widget(frame->widget, data);
1901 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1902 widget_set_resize_handler(frame->widget, frame_resize_handler);
1903 widget_set_enter_handler(frame->widget, frame_enter_handler);
1904 widget_set_motion_handler(frame->widget, frame_motion_handler);
1905 widget_set_button_handler(frame->widget, frame_button_handler);
1907 /* Create empty list for frame buttons */
1908 wl_list_init(&frame->buttons_list);
1910 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1911 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1913 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1914 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1916 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1917 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1919 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1920 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1922 window->frame = frame;
1924 return frame->child;
1928 frame_set_child_size(struct widget *widget, int child_width, int child_height)
1930 struct display *display = widget->window->display;
1931 struct theme *t = display->theme;
1932 int decoration_width, decoration_height;
1934 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1936 if (widget->window->type != TYPE_FULLSCREEN) {
1937 decoration_width = (t->width + margin) * 2;
1938 decoration_height = t->width +
1939 t->titlebar_height + margin * 2;
1941 width = child_width + decoration_width;
1942 height = child_height + decoration_height;
1944 width = child_width;
1945 height = child_height;
1948 window_schedule_resize(widget->window, width, height);
1952 frame_destroy(struct frame *frame)
1954 struct frame_button *button, *tmp;
1956 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1957 frame_button_destroy(button);
1959 /* frame->child must be destroyed by the application */
1960 widget_destroy(frame->widget);
1965 input_set_focus_widget(struct input *input, struct widget *focus,
1968 struct widget *old, *widget;
1969 int pointer = CURSOR_LEFT_PTR;
1971 if (focus == input->focus_widget)
1974 old = input->focus_widget;
1978 widget = input->grab;
1979 if (widget->leave_handler)
1980 widget->leave_handler(old, input, widget->user_data);
1981 input->focus_widget = NULL;
1987 widget = input->grab;
1988 input->focus_widget = focus;
1989 if (widget->enter_handler)
1990 pointer = widget->enter_handler(focus, input, x, y,
1993 input_set_pointer_image(input, pointer);
1998 input_grab(struct input *input, struct widget *widget, uint32_t button)
2000 input->grab = widget;
2001 input->grab_button = button;
2005 input_ungrab(struct input *input)
2007 struct widget *widget;
2010 if (input->pointer_focus) {
2011 widget = widget_find_widget(input->pointer_focus->widget,
2012 input->sx, input->sy);
2013 input_set_focus_widget(input, widget, input->sx, input->sy);
2018 input_remove_pointer_focus(struct input *input)
2020 struct window *window = input->pointer_focus;
2025 input_set_focus_widget(input, NULL, 0, 0);
2027 input->pointer_focus = NULL;
2028 input->current_cursor = CURSOR_UNSET;
2032 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2033 uint32_t serial, struct wl_surface *surface,
2034 wl_fixed_t sx_w, wl_fixed_t sy_w)
2036 struct input *input = data;
2037 struct window *window;
2038 struct widget *widget;
2039 float sx = wl_fixed_to_double(sx_w);
2040 float sy = wl_fixed_to_double(sy_w);
2043 /* enter event for a window we've just destroyed */
2047 input->display->serial = serial;
2048 input->pointer_enter_serial = serial;
2049 input->pointer_focus = wl_surface_get_user_data(surface);
2050 window = input->pointer_focus;
2053 shm_pool_destroy(window->pool);
2054 window->pool = NULL;
2055 /* Schedule a redraw to free the pool */
2056 window_schedule_redraw(window);
2062 widget = widget_find_widget(window->widget, sx, sy);
2063 input_set_focus_widget(input, widget, sx, sy);
2067 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2068 uint32_t serial, struct wl_surface *surface)
2070 struct input *input = data;
2072 input->display->serial = serial;
2073 input_remove_pointer_focus(input);
2077 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2078 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2080 struct input *input = data;
2081 struct window *window = input->pointer_focus;
2082 struct widget *widget;
2083 int cursor = CURSOR_LEFT_PTR;
2084 float sx = wl_fixed_to_double(sx_w);
2085 float sy = wl_fixed_to_double(sy_w);
2093 if (!(input->grab && input->grab_button)) {
2094 widget = widget_find_widget(window->widget, sx, sy);
2095 input_set_focus_widget(input, widget, sx, sy);
2099 widget = input->grab;
2101 widget = input->focus_widget;
2102 if (widget && widget->motion_handler)
2103 cursor = widget->motion_handler(input->focus_widget,
2104 input, time, sx, sy,
2107 input_set_pointer_image(input, cursor);
2111 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2112 uint32_t time, uint32_t button, uint32_t state_w)
2114 struct input *input = data;
2115 struct widget *widget;
2116 enum wl_pointer_button_state state = state_w;
2118 input->display->serial = serial;
2119 if (input->focus_widget && input->grab == NULL &&
2120 state == WL_POINTER_BUTTON_STATE_PRESSED)
2121 input_grab(input, input->focus_widget, button);
2123 widget = input->grab;
2124 if (widget && widget->button_handler)
2125 (*widget->button_handler)(widget,
2128 input->grab->user_data);
2130 if (input->grab && input->grab_button == button &&
2131 state == WL_POINTER_BUTTON_STATE_RELEASED)
2132 input_ungrab(input);
2136 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2137 uint32_t time, uint32_t axis, wl_fixed_t value)
2139 struct input *input = data;
2140 struct widget *widget;
2142 widget = input->focus_widget;
2144 widget = input->grab;
2145 if (widget && widget->axis_handler)
2146 (*widget->axis_handler)(widget,
2152 static const struct wl_pointer_listener pointer_listener = {
2153 pointer_handle_enter,
2154 pointer_handle_leave,
2155 pointer_handle_motion,
2156 pointer_handle_button,
2157 pointer_handle_axis,
2161 input_remove_keyboard_focus(struct input *input)
2163 struct window *window = input->keyboard_focus;
2164 struct itimerspec its;
2166 its.it_interval.tv_sec = 0;
2167 its.it_interval.tv_nsec = 0;
2168 its.it_value.tv_sec = 0;
2169 its.it_value.tv_nsec = 0;
2170 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2175 window->focus_count--;
2176 if (window->keyboard_focus_handler)
2177 (*window->keyboard_focus_handler)(window, NULL,
2180 input->keyboard_focus = NULL;
2184 keyboard_repeat_func(struct task *task, uint32_t events)
2186 struct input *input =
2187 container_of(task, struct input, repeat_task);
2188 struct window *window = input->keyboard_focus;
2191 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2192 /* If we change the timer between the fd becoming
2193 * readable and getting here, there'll be nothing to
2194 * read and we get EAGAIN. */
2197 if (window && window->key_handler) {
2198 (*window->key_handler)(window, input, input->repeat_time,
2199 input->repeat_key, input->repeat_sym,
2200 WL_KEYBOARD_KEY_STATE_PRESSED,
2206 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2207 uint32_t format, int fd, uint32_t size)
2209 struct input *input = data;
2217 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2222 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2223 if (map_str == MAP_FAILED) {
2228 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2230 XKB_KEYMAP_FORMAT_TEXT_V1,
2232 munmap(map_str, size);
2235 if (!input->xkb.keymap) {
2236 fprintf(stderr, "failed to compile keymap\n");
2240 input->xkb.state = xkb_state_new(input->xkb.keymap);
2241 if (!input->xkb.state) {
2242 fprintf(stderr, "failed to create XKB state\n");
2243 xkb_map_unref(input->xkb.keymap);
2244 input->xkb.keymap = NULL;
2248 input->xkb.control_mask =
2249 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2250 input->xkb.alt_mask =
2251 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2252 input->xkb.shift_mask =
2253 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2257 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2258 uint32_t serial, struct wl_surface *surface,
2259 struct wl_array *keys)
2261 struct input *input = data;
2262 struct window *window;
2264 input->display->serial = serial;
2265 input->keyboard_focus = wl_surface_get_user_data(surface);
2267 window = input->keyboard_focus;
2268 window->focus_count++;
2269 if (window->keyboard_focus_handler)
2270 (*window->keyboard_focus_handler)(window,
2271 input, window->user_data);
2275 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2276 uint32_t serial, struct wl_surface *surface)
2278 struct input *input = data;
2280 input->display->serial = serial;
2281 input_remove_keyboard_focus(input);
2285 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2286 uint32_t serial, uint32_t time, uint32_t key,
2289 struct input *input = data;
2290 struct window *window = input->keyboard_focus;
2291 uint32_t code, num_syms;
2292 enum wl_keyboard_key_state state = state_w;
2293 const xkb_keysym_t *syms;
2295 struct itimerspec its;
2297 input->display->serial = serial;
2299 if (!window || !input->xkb.state)
2302 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2304 sym = XKB_KEY_NoSymbol;
2308 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2309 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2310 window_set_maximized(window,
2311 window->type != TYPE_MAXIMIZED);
2312 } else if (sym == XKB_KEY_F11 &&
2313 window->fullscreen_handler &&
2314 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2315 window->fullscreen_handler(window, window->user_data);
2316 } else if (sym == XKB_KEY_F4 &&
2317 input->modifiers == MOD_ALT_MASK &&
2318 state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2319 if (window->close_handler)
2320 window->close_handler(window->parent,
2323 display_exit(window->display);
2324 } else if (window->key_handler) {
2325 (*window->key_handler)(window, input, time, key,
2326 sym, state, window->user_data);
2329 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2330 key == input->repeat_key) {
2331 its.it_interval.tv_sec = 0;
2332 its.it_interval.tv_nsec = 0;
2333 its.it_value.tv_sec = 0;
2334 its.it_value.tv_nsec = 0;
2335 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2336 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2337 input->repeat_sym = sym;
2338 input->repeat_key = key;
2339 input->repeat_time = time;
2340 its.it_interval.tv_sec = 0;
2341 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2342 its.it_value.tv_sec = 0;
2343 its.it_value.tv_nsec = 400 * 1000 * 1000;
2344 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2349 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2350 uint32_t serial, uint32_t mods_depressed,
2351 uint32_t mods_latched, uint32_t mods_locked,
2354 struct input *input = data;
2355 xkb_mod_mask_t mask;
2357 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2358 mods_locked, 0, 0, group);
2359 mask = xkb_state_serialize_mods(input->xkb.state,
2360 XKB_STATE_DEPRESSED |
2362 input->modifiers = 0;
2363 if (mask & input->xkb.control_mask)
2364 input->modifiers |= MOD_CONTROL_MASK;
2365 if (mask & input->xkb.alt_mask)
2366 input->modifiers |= MOD_ALT_MASK;
2367 if (mask & input->xkb.shift_mask)
2368 input->modifiers |= MOD_SHIFT_MASK;
2371 static const struct wl_keyboard_listener keyboard_listener = {
2372 keyboard_handle_keymap,
2373 keyboard_handle_enter,
2374 keyboard_handle_leave,
2375 keyboard_handle_key,
2376 keyboard_handle_modifiers,
2380 seat_handle_capabilities(void *data, struct wl_seat *seat,
2381 enum wl_seat_capability caps)
2383 struct input *input = data;
2385 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2386 input->pointer = wl_seat_get_pointer(seat);
2387 wl_pointer_set_user_data(input->pointer, input);
2388 wl_pointer_add_listener(input->pointer, &pointer_listener,
2390 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2391 wl_pointer_destroy(input->pointer);
2392 input->pointer = NULL;
2395 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2396 input->keyboard = wl_seat_get_keyboard(seat);
2397 wl_keyboard_set_user_data(input->keyboard, input);
2398 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2400 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2401 wl_keyboard_destroy(input->keyboard);
2402 input->keyboard = NULL;
2406 static const struct wl_seat_listener seat_listener = {
2407 seat_handle_capabilities,
2411 input_get_position(struct input *input, int32_t *x, int32_t *y)
2418 input_get_display(struct input *input)
2420 return input->display;
2424 input_get_seat(struct input *input)
2430 input_get_modifiers(struct input *input)
2432 return input->modifiers;
2436 input_get_focus_widget(struct input *input)
2438 return input->focus_widget;
2442 struct wl_data_offer *offer;
2443 struct input *input;
2444 struct wl_array types;
2447 struct task io_task;
2455 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2457 struct data_offer *offer = data;
2460 p = wl_array_add(&offer->types, sizeof *p);
2464 static const struct wl_data_offer_listener data_offer_listener = {
2469 data_offer_destroy(struct data_offer *offer)
2474 if (offer->refcount == 0) {
2475 wl_data_offer_destroy(offer->offer);
2476 for (p = offer->types.data; *p; p++)
2478 wl_array_release(&offer->types);
2484 data_device_data_offer(void *data,
2485 struct wl_data_device *data_device,
2486 struct wl_data_offer *_offer)
2488 struct data_offer *offer;
2490 offer = malloc(sizeof *offer);
2492 wl_array_init(&offer->types);
2493 offer->refcount = 1;
2494 offer->input = data;
2495 offer->offer = _offer;
2496 wl_data_offer_add_listener(offer->offer,
2497 &data_offer_listener, offer);
2501 data_device_enter(void *data, struct wl_data_device *data_device,
2502 uint32_t serial, struct wl_surface *surface,
2503 wl_fixed_t x_w, wl_fixed_t y_w,
2504 struct wl_data_offer *offer)
2506 struct input *input = data;
2507 struct window *window;
2509 float x = wl_fixed_to_double(x_w);
2510 float y = wl_fixed_to_double(y_w);
2513 input->pointer_enter_serial = serial;
2514 window = wl_surface_get_user_data(surface);
2515 input->pointer_focus = window;
2518 input->drag_offer = wl_data_offer_get_user_data(offer);
2520 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2523 types_data = input->drag_offer->types.data;
2525 input->drag_offer = NULL;
2529 window = input->pointer_focus;
2530 if (window->data_handler)
2531 window->data_handler(window, input, x, y, types_data,
2536 data_device_leave(void *data, struct wl_data_device *data_device)
2538 struct input *input = data;
2540 if (input->drag_offer) {
2541 data_offer_destroy(input->drag_offer);
2542 input->drag_offer = NULL;
2547 data_device_motion(void *data, struct wl_data_device *data_device,
2548 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2550 struct input *input = data;
2551 struct window *window = input->pointer_focus;
2552 float x = wl_fixed_to_double(x_w);
2553 float y = wl_fixed_to_double(y_w);
2559 if (input->drag_offer)
2560 types_data = input->drag_offer->types.data;
2564 if (window->data_handler)
2565 window->data_handler(window, input, x, y, types_data,
2570 data_device_drop(void *data, struct wl_data_device *data_device)
2572 struct input *input = data;
2573 struct window *window = input->pointer_focus;
2575 if (window->drop_handler)
2576 window->drop_handler(window, input,
2577 input->sx, input->sy, window->user_data);
2581 data_device_selection(void *data,
2582 struct wl_data_device *wl_data_device,
2583 struct wl_data_offer *offer)
2585 struct input *input = data;
2588 if (input->selection_offer)
2589 data_offer_destroy(input->selection_offer);
2592 input->selection_offer = wl_data_offer_get_user_data(offer);
2593 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2596 input->selection_offer = NULL;
2600 static const struct wl_data_device_listener data_device_listener = {
2601 data_device_data_offer,
2606 data_device_selection
2610 input_set_pointer_image_index(struct input *input, int index)
2612 struct wl_buffer *buffer;
2613 struct wl_cursor *cursor;
2614 struct wl_cursor_image *image;
2616 if (!input->pointer)
2619 cursor = input->display->cursors[input->current_cursor];
2623 if (index >= (int) cursor->image_count) {
2624 fprintf(stderr, "cursor index out of range\n");
2628 image = cursor->images[index];
2629 buffer = wl_cursor_image_get_buffer(image);
2633 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
2634 input->pointer_surface,
2635 image->hotspot_x, image->hotspot_y);
2636 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2637 wl_surface_damage(input->pointer_surface, 0, 0,
2638 image->width, image->height);
2639 wl_surface_commit(input->pointer_surface);
2642 static const struct wl_callback_listener pointer_surface_listener;
2645 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2648 struct input *input = data;
2649 struct wl_cursor *cursor;
2653 assert(callback == input->cursor_frame_cb);
2654 wl_callback_destroy(callback);
2655 input->cursor_frame_cb = NULL;
2658 if (!input->pointer)
2661 if (input->current_cursor == CURSOR_BLANK) {
2662 wl_pointer_set_cursor(input->pointer,
2663 input->pointer_enter_serial,
2668 if (input->current_cursor == CURSOR_UNSET)
2670 cursor = input->display->cursors[input->current_cursor];
2674 /* FIXME We don't have the current time on the first call so we set
2675 * the animation start to the time of the first frame callback. */
2677 input->cursor_anim_start = 0;
2678 else if (input->cursor_anim_start == 0)
2679 input->cursor_anim_start = time;
2681 if (time == 0 || input->cursor_anim_start == 0)
2684 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2686 if (cursor->image_count > 1) {
2687 input->cursor_frame_cb =
2688 wl_surface_frame(input->pointer_surface);
2689 wl_callback_add_listener(input->cursor_frame_cb,
2690 &pointer_surface_listener, input);
2693 input_set_pointer_image_index(input, i);
2696 static const struct wl_callback_listener pointer_surface_listener = {
2697 pointer_surface_frame_callback
2701 input_set_pointer_image(struct input *input, int pointer)
2705 if (!input->pointer)
2708 if (input->pointer_enter_serial > input->cursor_serial)
2711 if (!force && pointer == input->current_cursor)
2714 input->current_cursor = pointer;
2715 input->cursor_serial = input->pointer_enter_serial;
2716 if (!input->cursor_frame_cb)
2717 pointer_surface_frame_callback(input, NULL, 0);
2719 /* The current frame callback may be stuck if, for instance,
2720 * the set cursor request was processed by the server after
2721 * this client lost the focus. In this case the cursor surface
2722 * might not be mapped and the frame callback wouldn't ever
2723 * complete. Send a set_cursor and attach to try to map the
2724 * cursor surface again so that the callback will finish */
2725 input_set_pointer_image_index(input, 0);
2729 struct wl_data_device *
2730 input_get_data_device(struct input *input)
2732 return input->data_device;
2736 input_set_selection(struct input *input,
2737 struct wl_data_source *source, uint32_t time)
2739 wl_data_device_set_selection(input->data_device, source, time);
2743 input_accept(struct input *input, const char *type)
2745 wl_data_offer_accept(input->drag_offer->offer,
2746 input->pointer_enter_serial, type);
2750 offer_io_func(struct task *task, uint32_t events)
2752 struct data_offer *offer =
2753 container_of(task, struct data_offer, io_task);
2757 len = read(offer->fd, buffer, sizeof buffer);
2758 offer->func(buffer, len,
2759 offer->x, offer->y, offer->user_data);
2763 data_offer_destroy(offer);
2768 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2769 data_func_t func, void *user_data)
2773 if (pipe2(p, O_CLOEXEC) == -1)
2776 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2779 offer->io_task.run = offer_io_func;
2783 offer->user_data = user_data;
2785 display_watch_fd(offer->input->display,
2786 offer->fd, EPOLLIN, &offer->io_task);
2790 input_receive_drag_data(struct input *input, const char *mime_type,
2791 data_func_t func, void *data)
2793 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2794 input->drag_offer->x = input->sx;
2795 input->drag_offer->y = input->sy;
2799 input_receive_selection_data(struct input *input, const char *mime_type,
2800 data_func_t func, void *data)
2804 if (input->selection_offer == NULL)
2807 for (p = input->selection_offer->types.data; *p; p++)
2808 if (strcmp(mime_type, *p) == 0)
2814 data_offer_receive_data(input->selection_offer,
2815 mime_type, func, data);
2820 input_receive_selection_data_to_fd(struct input *input,
2821 const char *mime_type, int fd)
2823 if (input->selection_offer)
2824 wl_data_offer_receive(input->selection_offer->offer,
2831 window_move(struct window *window, struct input *input, uint32_t serial)
2833 if (!window->shell_surface)
2836 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2840 idle_resize(struct window *window)
2842 struct widget *widget;
2844 window->resize_needed = 0;
2845 widget = window->widget;
2846 widget_set_allocation(widget,
2847 window->pending_allocation.x,
2848 window->pending_allocation.y,
2849 window->pending_allocation.width,
2850 window->pending_allocation.height);
2852 if (window->input_region) {
2853 wl_region_destroy(window->input_region);
2854 window->input_region = NULL;
2857 if (window->opaque_region) {
2858 wl_region_destroy(window->opaque_region);
2859 window->opaque_region = NULL;
2862 if (widget->resize_handler)
2863 widget->resize_handler(widget,
2864 widget->allocation.width,
2865 widget->allocation.height,
2868 if (window->allocation.width != widget->allocation.width ||
2869 window->allocation.height != widget->allocation.height) {
2870 window->allocation = widget->allocation;
2871 window_schedule_redraw(window);
2876 window_schedule_resize(struct window *window, int width, int height)
2878 window->pending_allocation.x = 0;
2879 window->pending_allocation.y = 0;
2880 window->pending_allocation.width = width;
2881 window->pending_allocation.height = height;
2883 if (window->min_allocation.width == 0)
2884 window->min_allocation = window->pending_allocation;
2885 if (window->pending_allocation.width < window->min_allocation.width)
2886 window->pending_allocation.width = window->min_allocation.width;
2887 if (window->pending_allocation.height < window->min_allocation.height)
2888 window->pending_allocation.height = window->min_allocation.height;
2890 window->resize_needed = 1;
2891 window_schedule_redraw(window);
2895 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2897 window_schedule_resize(widget->window, width, height);
2901 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2904 wl_shell_surface_pong(shell_surface, serial);
2908 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2909 uint32_t edges, int32_t width, int32_t height)
2911 struct window *window = data;
2913 window->resize_edges = edges;
2914 window_schedule_resize(window, width, height);
2918 menu_destroy(struct menu *menu)
2920 widget_destroy(menu->widget);
2921 window_destroy(menu->window);
2926 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2928 struct window *window = data;
2929 struct menu *menu = window->widget->user_data;
2931 /* FIXME: Need more context in this event, at least the input
2932 * device. Or just use wl_callback. And this really needs to
2933 * be a window vfunc that the menu can set. And we need the
2936 menu->func(window->parent, menu->current, window->parent->user_data);
2937 input_ungrab(menu->input);
2941 static const struct wl_shell_surface_listener shell_surface_listener = {
2948 window_get_allocation(struct window *window,
2949 struct rectangle *allocation)
2951 *allocation = window->allocation;
2955 widget_redraw(struct widget *widget)
2957 struct widget *child;
2959 if (widget->redraw_handler)
2960 widget->redraw_handler(widget, widget->user_data);
2961 wl_list_for_each(child, &widget->child_list, link)
2962 widget_redraw(child);
2966 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2968 struct window *window = data;
2970 assert(callback == window->frame_cb);
2971 wl_callback_destroy(callback);
2972 window->frame_cb = 0;
2973 window->redraw_scheduled = 0;
2974 if (window->redraw_needed)
2975 window_schedule_redraw(window);
2978 static const struct wl_callback_listener listener = {
2983 idle_redraw(struct task *task, uint32_t events)
2985 struct window *window = container_of(task, struct window, redraw_task);
2987 if (window->resize_needed)
2988 idle_resize(window);
2990 window_create_surface(window);
2991 widget_redraw(window->widget);
2992 window->redraw_needed = 0;
2993 wl_list_init(&window->redraw_task.link);
2995 window->frame_cb = wl_surface_frame(window->surface);
2996 wl_callback_add_listener(window->frame_cb, &listener, window);
2997 window_flush(window);
3001 window_schedule_redraw(struct window *window)
3003 window->redraw_needed = 1;
3004 if (!window->redraw_scheduled) {
3005 window->redraw_task.run = idle_redraw;
3006 display_defer(window->display, &window->redraw_task);
3007 window->redraw_scheduled = 1;
3012 window_is_fullscreen(struct window *window)
3014 return window->type == TYPE_FULLSCREEN;
3018 window_set_fullscreen(struct window *window, int fullscreen)
3020 if (!window->display->shell)
3023 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3027 window->type = TYPE_FULLSCREEN;
3028 window->saved_allocation = window->allocation;
3029 wl_shell_surface_set_fullscreen(window->shell_surface,
3030 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3033 window->type = TYPE_TOPLEVEL;
3034 wl_shell_surface_set_toplevel(window->shell_surface);
3035 window_schedule_resize(window,
3036 window->saved_allocation.width,
3037 window->saved_allocation.height);
3042 window_is_maximized(struct window *window)
3044 return window->type == TYPE_MAXIMIZED;
3048 window_set_maximized(struct window *window, int maximized)
3050 if (!window->display->shell)
3053 if ((window->type == TYPE_MAXIMIZED) == maximized)
3056 if (window->type == TYPE_TOPLEVEL) {
3057 window->saved_allocation = window->allocation;
3058 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3059 window->type = TYPE_MAXIMIZED;
3061 wl_shell_surface_set_toplevel(window->shell_surface);
3062 window->type = TYPE_TOPLEVEL;
3063 window_schedule_resize(window,
3064 window->saved_allocation.width,
3065 window->saved_allocation.height);
3070 window_set_user_data(struct window *window, void *data)
3072 window->user_data = data;
3076 window_get_user_data(struct window *window)
3078 return window->user_data;
3082 window_set_key_handler(struct window *window,
3083 window_key_handler_t handler)
3085 window->key_handler = handler;
3089 window_set_keyboard_focus_handler(struct window *window,
3090 window_keyboard_focus_handler_t handler)
3092 window->keyboard_focus_handler = handler;
3096 window_set_data_handler(struct window *window, window_data_handler_t handler)
3098 window->data_handler = handler;
3102 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3104 window->drop_handler = handler;
3108 window_set_close_handler(struct window *window,
3109 window_close_handler_t handler)
3111 window->close_handler = handler;
3115 window_set_fullscreen_handler(struct window *window,
3116 window_fullscreen_handler_t handler)
3118 window->fullscreen_handler = handler;
3122 window_set_title(struct window *window, const char *title)
3124 free(window->title);
3125 window->title = strdup(title);
3126 if (window->shell_surface)
3127 wl_shell_surface_set_title(window->shell_surface, title);
3131 window_get_title(struct window *window)
3133 return window->title;
3137 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3139 struct text_cursor_position *text_cursor_position =
3140 window->display->text_cursor_position;
3142 if (!text_cursor_position)
3145 text_cursor_position_notify(text_cursor_position,
3147 wl_fixed_from_int(x),
3148 wl_fixed_from_int(y));
3152 window_damage(struct window *window, int32_t x, int32_t y,
3153 int32_t width, int32_t height)
3155 wl_surface_damage(window->surface, x, y, width, height);
3159 surface_enter(void *data,
3160 struct wl_surface *wl_surface, struct wl_output *wl_output)
3162 struct window *window = data;
3163 struct output *output;
3164 struct output *output_found = NULL;
3165 struct window_output *window_output;
3167 wl_list_for_each(output, &window->display->output_list, link) {
3168 if (output->output == wl_output) {
3169 output_found = output;
3177 window_output = malloc (sizeof *window_output);
3178 window_output->output = output_found;
3180 wl_list_insert (&window->window_output_list, &window_output->link);
3184 surface_leave(void *data,
3185 struct wl_surface *wl_surface, struct wl_output *output)
3187 struct window *window = data;
3188 struct window_output *window_output;
3189 struct window_output *window_output_found = NULL;
3191 wl_list_for_each(window_output, &window->window_output_list, link) {
3192 if (window_output->output->output == output) {
3193 window_output_found = window_output;
3198 if (window_output_found) {
3199 wl_list_remove(&window_output_found->link);
3200 free(window_output_found);
3204 static const struct wl_surface_listener surface_listener = {
3209 static struct window *
3210 window_create_internal(struct display *display,
3211 struct window *parent, int type)
3213 struct window *window;
3215 window = malloc(sizeof *window);
3219 memset(window, 0, sizeof *window);
3220 window->display = display;
3221 window->parent = parent;
3222 window->surface = wl_compositor_create_surface(display->compositor);
3223 wl_surface_add_listener(window->surface, &surface_listener, window);
3224 if (type != TYPE_CUSTOM && display->shell) {
3225 window->shell_surface =
3226 wl_shell_get_shell_surface(display->shell,
3229 window->allocation.x = 0;
3230 window->allocation.y = 0;
3231 window->allocation.width = 0;
3232 window->allocation.height = 0;
3233 window->saved_allocation = window->allocation;
3234 window->transparent = 1;
3235 window->type = type;
3236 window->input_region = NULL;
3237 window->opaque_region = NULL;
3240 #ifdef HAVE_CAIRO_EGL
3241 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3243 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3246 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3248 wl_surface_set_user_data(window->surface, window);
3249 wl_list_insert(display->window_list.prev, &window->link);
3250 wl_list_init(&window->redraw_task.link);
3252 if (window->shell_surface) {
3253 wl_shell_surface_set_user_data(window->shell_surface, window);
3254 wl_shell_surface_add_listener(window->shell_surface,
3255 &shell_surface_listener, window);
3258 wl_list_init (&window->window_output_list);
3264 window_create(struct display *display)
3266 struct window *window;
3268 window = window_create_internal(display, NULL, TYPE_NONE);
3276 window_create_custom(struct display *display)
3278 struct window *window;
3280 window = window_create_internal(display, NULL, TYPE_CUSTOM);
3288 window_create_transient(struct display *display, struct window *parent,
3289 int32_t x, int32_t y, uint32_t flags)
3291 struct window *window;
3293 window = window_create_internal(parent->display,
3294 parent, TYPE_TRANSIENT);
3302 wl_shell_surface_set_transient(window->shell_surface,
3303 window->parent->surface,
3304 window->x, window->y, flags);
3310 menu_set_item(struct menu *menu, int sy)
3314 next = (sy - 8) / 20;
3315 if (menu->current != next) {
3316 menu->current = next;
3317 widget_schedule_redraw(menu->widget);
3322 menu_motion_handler(struct widget *widget,
3323 struct input *input, uint32_t time,
3324 float x, float y, void *data)
3326 struct menu *menu = data;
3328 if (widget == menu->widget)
3329 menu_set_item(data, y);
3331 return CURSOR_LEFT_PTR;
3335 menu_enter_handler(struct widget *widget,
3336 struct input *input, float x, float y, void *data)
3338 struct menu *menu = data;
3340 if (widget == menu->widget)
3341 menu_set_item(data, y);
3343 return CURSOR_LEFT_PTR;
3347 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3349 struct menu *menu = data;
3351 if (widget == menu->widget)
3352 menu_set_item(data, -200);
3356 menu_button_handler(struct widget *widget,
3357 struct input *input, uint32_t time,
3358 uint32_t button, enum wl_pointer_button_state state,
3362 struct menu *menu = data;
3364 if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3365 (menu->release_count > 0 || time - menu->time > 500)) {
3366 /* Either relase after press-drag-release or
3367 * click-motion-click. */
3368 menu->func(menu->window->parent,
3369 menu->current, menu->window->parent->user_data);
3370 input_ungrab(input);
3372 } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3373 menu->release_count++;
3378 menu_redraw_handler(struct widget *widget, void *data)
3381 const int32_t r = 3, margin = 3;
3382 struct menu *menu = data;
3383 int32_t width, height, i;
3384 struct window *window = widget->window;
3386 cr = cairo_create(window->cairo_surface);
3387 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3388 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3391 width = window->allocation.width;
3392 height = window->allocation.height;
3393 rounded_rect(cr, 0, 0, width, height, r);
3395 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3396 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3399 for (i = 0; i < menu->count; i++) {
3400 if (i == menu->current) {
3401 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3402 cairo_rectangle(cr, margin, i * 20 + margin,
3403 width - 2 * margin, 20);
3405 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3406 cairo_move_to(cr, 10, i * 20 + 16);
3407 cairo_show_text(cr, menu->entries[i]);
3409 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3410 cairo_move_to(cr, 10, i * 20 + 16);
3411 cairo_show_text(cr, menu->entries[i]);
3419 window_show_menu(struct display *display,
3420 struct input *input, uint32_t time, struct window *parent,
3421 int32_t x, int32_t y,
3422 menu_func_t func, const char **entries, int count)
3424 struct window *window;
3426 const int32_t margin = 3;
3428 menu = malloc(sizeof *menu);
3432 window = window_create_internal(parent->display, parent, TYPE_MENU);
3438 menu->window = window;
3439 menu->widget = window_add_widget(menu->window, menu);
3440 menu->entries = entries;
3441 menu->count = count;
3442 menu->release_count = 0;
3446 menu->input = input;
3447 window->type = TYPE_MENU;
3451 input_ungrab(input);
3452 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3453 display_get_serial(window->display),
3454 window->parent->surface,
3455 window->x, window->y, 0);
3457 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3458 widget_set_enter_handler(menu->widget, menu_enter_handler);
3459 widget_set_leave_handler(menu->widget, menu_leave_handler);
3460 widget_set_motion_handler(menu->widget, menu_motion_handler);
3461 widget_set_button_handler(menu->widget, menu_button_handler);
3463 input_grab(input, menu->widget, 0);
3464 window_schedule_resize(window, 200, count * 20 + margin * 2);
3468 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3470 window->buffer_type = type;
3475 display_handle_geometry(void *data,
3476 struct wl_output *wl_output,
3479 int physical_height,
3485 struct output *output = data;
3487 output->allocation.x = x;
3488 output->allocation.y = y;
3489 output->transform = transform;
3493 display_handle_mode(void *data,
3494 struct wl_output *wl_output,
3500 struct output *output = data;
3501 struct display *display = output->display;
3503 if (flags & WL_OUTPUT_MODE_CURRENT) {
3504 output->allocation.width = width;
3505 output->allocation.height = height;
3506 if (display->output_configure_handler)
3507 (*display->output_configure_handler)(
3508 output, display->user_data);
3512 static const struct wl_output_listener output_listener = {
3513 display_handle_geometry,
3518 display_add_output(struct display *d, uint32_t id)
3520 struct output *output;
3522 output = malloc(sizeof *output);
3526 memset(output, 0, sizeof *output);
3527 output->display = d;
3529 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
3530 wl_list_insert(d->output_list.prev, &output->link);
3532 wl_output_add_listener(output->output, &output_listener, output);
3536 output_destroy(struct output *output)
3538 if (output->destroy_handler)
3539 (*output->destroy_handler)(output, output->user_data);
3541 wl_output_destroy(output->output);
3542 wl_list_remove(&output->link);
3547 display_set_global_handler(struct display *display,
3548 display_global_handler_t handler)
3550 struct global *global;
3552 display->global_handler = handler;
3556 wl_list_for_each(global, &display->global_list, link)
3557 display->global_handler(display,
3558 global->name, global->interface,
3559 global->version, display->user_data);
3563 display_set_output_configure_handler(struct display *display,
3564 display_output_handler_t handler)
3566 struct output *output;
3568 display->output_configure_handler = handler;
3572 wl_list_for_each(output, &display->output_list, link) {
3573 if (output->allocation.width == 0 &&
3574 output->allocation.height == 0)
3577 (*display->output_configure_handler)(output,
3578 display->user_data);
3583 output_set_user_data(struct output *output, void *data)
3585 output->user_data = data;
3589 output_get_user_data(struct output *output)
3591 return output->user_data;
3595 output_set_destroy_handler(struct output *output,
3596 display_output_handler_t handler)
3598 output->destroy_handler = handler;
3599 /* FIXME: implement this, once we have way to remove outputs */
3603 output_get_allocation(struct output *output, struct rectangle *base)
3605 struct rectangle allocation = output->allocation;
3607 switch (output->transform) {
3608 case WL_OUTPUT_TRANSFORM_90:
3609 case WL_OUTPUT_TRANSFORM_270:
3610 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3611 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3612 /* Swap width and height */
3613 allocation.width = output->allocation.height;
3614 allocation.height = output->allocation.width;
3622 output_get_wl_output(struct output *output)
3624 return output->output;
3628 fini_xkb(struct input *input)
3630 xkb_state_unref(input->xkb.state);
3631 xkb_map_unref(input->xkb.keymap);
3635 display_add_input(struct display *d, uint32_t id)
3637 struct input *input;
3639 input = malloc(sizeof *input);
3643 memset(input, 0, sizeof *input);
3645 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
3646 input->pointer_focus = NULL;
3647 input->keyboard_focus = NULL;
3648 wl_list_insert(d->input_list.prev, &input->link);
3650 wl_seat_add_listener(input->seat, &seat_listener, input);
3651 wl_seat_set_user_data(input->seat, input);
3653 input->data_device =
3654 wl_data_device_manager_get_data_device(d->data_device_manager,
3656 wl_data_device_add_listener(input->data_device, &data_device_listener,
3659 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3661 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3662 TFD_CLOEXEC | TFD_NONBLOCK);
3663 input->repeat_task.run = keyboard_repeat_func;
3664 display_watch_fd(d, input->repeat_timer_fd,
3665 EPOLLIN, &input->repeat_task);
3669 input_destroy(struct input *input)
3671 input_remove_keyboard_focus(input);
3672 input_remove_pointer_focus(input);
3674 if (input->drag_offer)
3675 data_offer_destroy(input->drag_offer);
3677 if (input->selection_offer)
3678 data_offer_destroy(input->selection_offer);
3680 wl_data_device_destroy(input->data_device);
3683 wl_surface_destroy(input->pointer_surface);
3685 wl_list_remove(&input->link);
3686 wl_seat_destroy(input->seat);
3687 close(input->repeat_timer_fd);
3692 init_workspace_manager(struct display *d, uint32_t id)
3694 d->workspace_manager =
3695 wl_registry_bind(d->registry, id,
3696 &workspace_manager_interface, 1);
3697 if (d->workspace_manager != NULL)
3698 workspace_manager_add_listener(d->workspace_manager,
3699 &workspace_manager_listener,
3704 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3705 const char *interface, uint32_t version)
3707 struct display *d = data;
3708 struct global *global;
3710 global = malloc(sizeof *global);
3712 global->interface = strdup(interface);
3713 global->version = version;
3714 wl_list_insert(d->global_list.prev, &global->link);
3716 if (strcmp(interface, "wl_compositor") == 0) {
3717 d->compositor = wl_registry_bind(registry, id,
3718 &wl_compositor_interface, 1);
3719 } else if (strcmp(interface, "wl_output") == 0) {
3720 display_add_output(d, id);
3721 } else if (strcmp(interface, "wl_seat") == 0) {
3722 display_add_input(d, id);
3723 } else if (strcmp(interface, "wl_shell") == 0) {
3724 d->shell = wl_registry_bind(registry,
3725 id, &wl_shell_interface, 1);
3726 } else if (strcmp(interface, "wl_shm") == 0) {
3727 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
3728 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3729 d->data_device_manager =
3730 wl_registry_bind(registry, id,
3731 &wl_data_device_manager_interface, 1);
3732 } else if (strcmp(interface, "text_cursor_position") == 0) {
3733 d->text_cursor_position =
3734 wl_registry_bind(registry, id,
3735 &text_cursor_position_interface, 1);
3736 } else if (strcmp(interface, "workspace_manager") == 0) {
3737 init_workspace_manager(d, id);
3740 if (d->global_handler)
3741 d->global_handler(d, id, interface, version, d->user_data);
3745 display_bind(struct display *display, uint32_t name,
3746 const struct wl_interface *interface, uint32_t version)
3748 return wl_registry_bind(display->registry, name, interface, version);
3751 static const struct wl_registry_listener registry_listener = {
3752 registry_handle_global
3755 #ifdef HAVE_CAIRO_EGL
3757 init_egl(struct display *d)
3759 EGLint major, minor;
3762 #ifdef USE_CAIRO_GLESV2
3763 # define GL_BIT EGL_OPENGL_ES2_BIT
3765 # define GL_BIT EGL_OPENGL_BIT
3768 static const EGLint argb_cfg_attribs[] = {
3769 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3775 EGL_RENDERABLE_TYPE, GL_BIT,
3779 #ifdef USE_CAIRO_GLESV2
3780 static const EGLint context_attribs[] = {
3781 EGL_CONTEXT_CLIENT_VERSION, 2,
3784 EGLint api = EGL_OPENGL_ES_API;
3786 EGLint *context_attribs = NULL;
3787 EGLint api = EGL_OPENGL_API;
3790 d->dpy = eglGetDisplay(d->display);
3791 if (!eglInitialize(d->dpy, &major, &minor)) {
3792 fprintf(stderr, "failed to initialize display\n");
3796 if (!eglBindAPI(api)) {
3797 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3801 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3802 &d->argb_config, 1, &n) || n != 1) {
3803 fprintf(stderr, "failed to choose argb config\n");
3807 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3808 EGL_NO_CONTEXT, context_attribs);
3809 if (d->argb_ctx == NULL) {
3810 fprintf(stderr, "failed to create context\n");
3814 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3815 fprintf(stderr, "failed to make context current\n");
3819 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3820 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3821 fprintf(stderr, "failed to get cairo egl argb device\n");
3829 fini_egl(struct display *display)
3831 cairo_device_destroy(display->argb_device);
3833 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3836 eglTerminate(display->dpy);
3842 handle_display_data(struct task *task, uint32_t events)
3844 struct display *display =
3845 container_of(task, struct display, display_task);
3846 struct epoll_event ep;
3849 display->display_fd_events = events;
3851 if (events & EPOLLERR || events & EPOLLHUP) {
3852 display_exit(display);
3856 if (events & EPOLLIN) {
3857 ret = wl_display_dispatch(display->display);
3859 display_exit(display);
3864 if (events & EPOLLOUT) {
3865 ret = wl_display_flush(display->display);
3867 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3868 ep.data.ptr = &display->display_task;
3869 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3870 display->display_fd, &ep);
3871 } else if (ret == -1 && errno != EAGAIN) {
3872 display_exit(display);
3879 display_create(int argc, char *argv[])
3883 d = malloc(sizeof *d);
3887 memset(d, 0, sizeof *d);
3889 d->display = wl_display_connect(NULL);
3890 if (d->display == NULL) {
3891 fprintf(stderr, "failed to create display: %m\n");
3895 d->epoll_fd = os_epoll_create_cloexec();
3896 d->display_fd = wl_display_get_fd(d->display);
3897 d->display_task.run = handle_display_data;
3898 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3901 wl_list_init(&d->deferred_list);
3902 wl_list_init(&d->input_list);
3903 wl_list_init(&d->output_list);
3904 wl_list_init(&d->global_list);
3906 d->xkb_context = xkb_context_new(0);
3907 if (d->xkb_context == NULL) {
3908 fprintf(stderr, "Failed to create XKB context\n");
3913 d->workspace_count = 1;
3915 d->registry = wl_display_get_registry(d->display);
3916 wl_registry_add_listener(d->registry, ®istry_listener, d);
3917 wl_display_dispatch(d->display);
3918 #ifdef HAVE_CAIRO_EGL
3919 if (init_egl(d) < 0)
3925 d->theme = theme_create();
3927 wl_list_init(&d->window_list);
3933 display_destroy_outputs(struct display *display)
3936 struct output *output;
3938 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3939 output_destroy(output);
3943 display_destroy_inputs(struct display *display)
3946 struct input *input;
3948 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3949 input_destroy(input);
3953 display_destroy(struct display *display)
3955 if (!wl_list_empty(&display->window_list))
3956 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3957 wl_list_length(&display->window_list));
3959 if (!wl_list_empty(&display->deferred_list))
3960 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3962 display_destroy_outputs(display);
3963 display_destroy_inputs(display);
3965 xkb_context_unref(display->xkb_context);
3967 theme_destroy(display->theme);
3968 destroy_cursors(display);
3970 #ifdef HAVE_CAIRO_EGL
3975 wl_shell_destroy(display->shell);
3978 wl_shm_destroy(display->shm);
3980 if (display->data_device_manager)
3981 wl_data_device_manager_destroy(display->data_device_manager);
3983 wl_compositor_destroy(display->compositor);
3985 close(display->epoll_fd);
3987 if (!(display->display_fd_events & EPOLLERR) &&
3988 !(display->display_fd_events & EPOLLHUP))
3989 wl_display_flush(display->display);
3991 wl_display_disconnect(display->display);
3996 display_set_user_data(struct display *display, void *data)
3998 display->user_data = data;
4002 display_get_user_data(struct display *display)
4004 return display->user_data;
4008 display_get_display(struct display *display)
4010 return display->display;
4014 display_get_output(struct display *display)
4016 return container_of(display->output_list.next, struct output, link);
4019 struct wl_compositor *
4020 display_get_compositor(struct display *display)
4022 return display->compositor;
4026 display_get_serial(struct display *display)
4028 return display->serial;
4032 display_get_egl_display(struct display *d)
4037 struct wl_data_source *
4038 display_create_data_source(struct display *display)
4040 return wl_data_device_manager_create_data_source(display->data_device_manager);
4044 display_get_argb_egl_config(struct display *d)
4046 return d->argb_config;
4050 display_get_shell(struct display *display)
4052 return display->shell;
4056 display_acquire_window_surface(struct display *display,
4057 struct window *window,
4060 #ifdef HAVE_CAIRO_EGL
4061 struct egl_window_surface_data *data;
4062 cairo_device_t *device;
4064 if (!window->cairo_surface)
4066 device = cairo_surface_get_device(window->cairo_surface);
4071 if (device == display->argb_device)
4072 ctx = display->argb_ctx;
4077 data = cairo_surface_get_user_data(window->cairo_surface,
4080 cairo_device_flush(device);
4081 cairo_device_acquire(device);
4082 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4083 fprintf(stderr, "failed to make surface current\n");
4092 display_release_window_surface(struct display *display,
4093 struct window *window)
4095 #ifdef HAVE_CAIRO_EGL
4096 cairo_device_t *device;
4098 device = cairo_surface_get_device(window->cairo_surface);
4102 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
4103 fprintf(stderr, "failed to make context current\n");
4104 cairo_device_release(device);
4109 display_defer(struct display *display, struct task *task)
4111 wl_list_insert(&display->deferred_list, &task->link);
4115 display_watch_fd(struct display *display,
4116 int fd, uint32_t events, struct task *task)
4118 struct epoll_event ep;
4122 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4126 display_unwatch_fd(struct display *display, int fd)
4128 epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4132 display_run(struct display *display)
4135 struct epoll_event ep[16];
4138 display->running = 1;
4140 while (!wl_list_empty(&display->deferred_list)) {
4141 task = container_of(display->deferred_list.prev,
4143 wl_list_remove(&task->link);
4147 wl_display_dispatch_pending(display->display);
4149 if (!display->running)
4152 ret = wl_display_flush(display->display);
4153 if (ret < 0 && errno == EAGAIN) {
4155 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4156 ep[0].data.ptr = &display->display_task;
4158 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4159 display->display_fd, &ep[0]);
4160 } else if (ret < 0) {
4164 count = epoll_wait(display->epoll_fd,
4165 ep, ARRAY_LENGTH(ep), -1);
4166 for (i = 0; i < count; i++) {
4167 task = ep[i].data.ptr;
4168 task->run(task, ep[i].events);
4174 display_exit(struct display *display)
4176 display->running = 0;