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>
44 #include <wayland-egl.h>
46 #ifdef USE_CAIRO_GLESV2
47 #include <GLES2/gl2.h>
48 #include <GLES2/gl2ext.h>
53 #include <EGL/eglext.h>
59 #include <xkbcommon/xkbcommon.h>
60 #include <wayland-cursor.h>
62 #include <linux/input.h>
63 #include <wayland-client.h>
64 #include "../shared/cairo-util.h"
65 #include "text-cursor-position-client-protocol.h"
66 #include "workspaces-client-protocol.h"
67 #include "../shared/os-compatibility.h"
81 struct wl_display *display;
82 struct wl_registry *registry;
83 struct wl_compositor *compositor;
84 struct wl_shell *shell;
86 struct wl_data_device_manager *data_device_manager;
87 struct text_cursor_position *text_cursor_position;
88 struct workspace_manager *workspace_manager;
90 EGLConfig argb_config;
92 cairo_device_t *argb_device;
96 uint32_t display_fd_events;
97 struct task display_task;
100 struct wl_list deferred_list;
104 struct wl_list global_list;
105 struct wl_list window_list;
106 struct wl_list input_list;
107 struct wl_list output_list;
111 struct wl_cursor_theme *cursor_theme;
112 struct wl_cursor **cursors;
114 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
115 PFNEGLCREATEIMAGEKHRPROC create_image;
116 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
118 display_output_handler_t output_configure_handler;
119 display_global_handler_t global_handler;
123 struct xkb_context *xkb_context;
126 uint32_t workspace_count;
139 struct window_output {
140 struct output *output;
145 struct display *display;
146 struct window *parent;
147 struct wl_list window_output_list;
148 struct wl_surface *surface;
149 struct wl_shell_surface *shell_surface;
150 struct wl_region *input_region;
151 struct wl_region *opaque_region;
153 struct rectangle allocation, saved_allocation, server_allocation;
154 struct rectangle min_allocation;
155 struct rectangle pending_allocation;
158 int redraw_scheduled;
160 struct task redraw_task;
166 enum window_buffer_type buffer_type;
168 cairo_surface_t *cairo_surface;
170 struct shm_pool *pool;
172 window_key_handler_t key_handler;
173 window_keyboard_focus_handler_t keyboard_focus_handler;
174 window_data_handler_t data_handler;
175 window_drop_handler_t drop_handler;
176 window_close_handler_t close_handler;
177 window_fullscreen_handler_t fullscreen_handler;
179 struct wl_callback *frame_cb;
182 struct widget *widget;
189 struct window *window;
190 struct tooltip *tooltip;
191 struct wl_list child_list;
193 struct rectangle allocation;
194 widget_resize_handler_t resize_handler;
195 widget_redraw_handler_t redraw_handler;
196 widget_enter_handler_t enter_handler;
197 widget_leave_handler_t leave_handler;
198 widget_motion_handler_t motion_handler;
199 widget_button_handler_t button_handler;
200 widget_axis_handler_t axis_handler;
207 struct display *display;
208 struct wl_seat *seat;
209 struct wl_pointer *pointer;
210 struct wl_keyboard *keyboard;
211 struct window *pointer_focus;
212 struct window *keyboard_focus;
214 uint32_t cursor_anim_start;
215 struct wl_callback *cursor_frame_cb;
216 struct wl_surface *pointer_surface;
218 uint32_t pointer_enter_serial;
219 uint32_t cursor_serial;
223 struct widget *focus_widget;
225 uint32_t grab_button;
227 struct wl_data_device *data_device;
228 struct data_offer *drag_offer;
229 struct data_offer *selection_offer;
232 struct xkb_keymap *keymap;
233 struct xkb_state *state;
234 xkb_mod_mask_t control_mask;
235 xkb_mod_mask_t alt_mask;
236 xkb_mod_mask_t shift_mask;
239 struct task repeat_task;
243 uint32_t repeat_time;
247 struct display *display;
248 struct wl_output *output;
249 struct rectangle allocation;
253 display_output_handler_t destroy_handler;
257 enum frame_button_action {
258 FRAME_BUTTON_NULL = 0,
259 FRAME_BUTTON_ICON = 1,
260 FRAME_BUTTON_CLOSE = 2,
261 FRAME_BUTTON_MINIMIZE = 3,
262 FRAME_BUTTON_MAXIMIZE = 4,
265 enum frame_button_pointer {
266 FRAME_BUTTON_DEFAULT = 0,
267 FRAME_BUTTON_OVER = 1,
268 FRAME_BUTTON_ACTIVE = 2,
271 enum frame_button_align {
272 FRAME_BUTTON_RIGHT = 0,
273 FRAME_BUTTON_LEFT = 1,
276 enum frame_button_decoration {
277 FRAME_BUTTON_NONE = 0,
278 FRAME_BUTTON_FANCY = 1,
281 struct frame_button {
282 struct widget *widget;
284 cairo_surface_t *icon;
285 enum frame_button_action type;
286 enum frame_button_pointer state;
287 struct wl_list link; /* buttons_list */
288 enum frame_button_align align;
289 enum frame_button_decoration decoration;
293 struct widget *widget;
294 struct widget *child;
295 struct wl_list buttons_list;
299 struct window *window;
300 struct widget *widget;
302 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 switch (window->buffer_type) {
834 #ifdef HAVE_CAIRO_EGL
835 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
836 data = cairo_surface_get_user_data(window->cairo_surface,
839 cairo_gl_surface_swapbuffers(window->cairo_surface);
840 wl_egl_window_get_attached_size(data->window,
841 &window->server_allocation.width,
842 &window->server_allocation.height);
845 case WINDOW_BUFFER_TYPE_SHM:
847 display_get_buffer_for_surface(display,
848 window->cairo_surface);
850 window_get_resize_dx_dy(window, &x, &y);
851 wl_surface_attach(window->surface, buffer, x, y);
852 wl_surface_damage(window->surface, 0, 0,
853 window->allocation.width,
854 window->allocation.height);
855 wl_surface_commit(window->surface);
856 window->server_allocation = window->allocation;
857 cairo_surface_destroy(window->cairo_surface);
858 window->cairo_surface = NULL;
864 if (window->input_region) {
865 wl_surface_set_input_region(window->surface,
866 window->input_region);
867 wl_region_destroy(window->input_region);
868 window->input_region = NULL;
871 if (window->opaque_region) {
872 wl_surface_set_opaque_region(window->surface,
873 window->opaque_region);
874 wl_region_destroy(window->opaque_region);
875 window->opaque_region = NULL;
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 if (widget->window->type != TYPE_FULLSCREEN) {
1459 widget->window->input_region =
1460 wl_compositor_create_region(display->compositor);
1461 wl_region_add(widget->window->input_region,
1462 shadow_margin, shadow_margin,
1463 width - 2 * shadow_margin,
1464 height - 2 * shadow_margin);
1467 widget_set_allocation(widget, 0, 0, width, height);
1469 if (child->opaque) {
1470 widget->window->opaque_region =
1471 wl_compositor_create_region(display->compositor);
1472 wl_region_add(widget->window->opaque_region,
1473 opaque_margin, opaque_margin,
1474 widget->allocation.width - 2 * opaque_margin,
1475 widget->allocation.height - 2 * opaque_margin);
1478 /* frame internal buttons */
1479 x_r = frame->widget->allocation.width - t->width - shadow_margin;
1480 x_l = t->width + shadow_margin;
1481 y = t->width + shadow_margin;
1482 wl_list_for_each(button, &frame->buttons_list, link) {
1483 const int button_padding = 4;
1484 w = cairo_image_surface_get_width(button->icon);
1485 h = cairo_image_surface_get_height(button->icon);
1487 if (button->decoration == FRAME_BUTTON_FANCY)
1490 if (button->align == FRAME_BUTTON_LEFT) {
1491 widget_set_allocation(button->widget,
1492 x_l, y , w + 1, h + 1);
1494 x_l += button_padding;
1497 widget_set_allocation(button->widget,
1498 x_r, y , w + 1, h + 1);
1499 x_r -= button_padding;
1505 frame_button_enter_handler(struct widget *widget,
1506 struct input *input, float x, float y, void *data)
1508 struct frame_button *frame_button = data;
1510 widget_schedule_redraw(frame_button->widget);
1511 frame_button->state = FRAME_BUTTON_OVER;
1513 return CURSOR_LEFT_PTR;
1517 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1519 struct frame_button *frame_button = data;
1521 widget_schedule_redraw(frame_button->widget);
1522 frame_button->state = FRAME_BUTTON_DEFAULT;
1526 frame_button_button_handler(struct widget *widget,
1527 struct input *input, uint32_t time,
1529 enum wl_pointer_button_state state, void *data)
1531 struct frame_button *frame_button = data;
1532 struct window *window = widget->window;
1533 int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1535 if (button != BTN_LEFT)
1539 case WL_POINTER_BUTTON_STATE_PRESSED:
1540 frame_button->state = FRAME_BUTTON_ACTIVE;
1541 widget_schedule_redraw(frame_button->widget);
1543 if (frame_button->type == FRAME_BUTTON_ICON)
1544 window_show_frame_menu(window, input, time);
1546 case WL_POINTER_BUTTON_STATE_RELEASED:
1547 frame_button->state = FRAME_BUTTON_DEFAULT;
1548 widget_schedule_redraw(frame_button->widget);
1555 switch (frame_button->type) {
1556 case FRAME_BUTTON_CLOSE:
1557 if (window->close_handler)
1558 window->close_handler(window->parent,
1561 display_exit(window->display);
1563 case FRAME_BUTTON_MINIMIZE:
1564 fprintf(stderr,"Minimize stub\n");
1566 case FRAME_BUTTON_MAXIMIZE:
1567 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1570 /* Unknown operation */
1576 frame_button_motion_handler(struct widget *widget,
1577 struct input *input, uint32_t time,
1578 float x, float y, void *data)
1580 struct frame_button *frame_button = data;
1581 enum frame_button_pointer previous_button_state = frame_button->state;
1583 /* only track state for a pressed button */
1584 if (input->grab != widget)
1585 return CURSOR_LEFT_PTR;
1587 if (x > widget->allocation.x &&
1588 x < (widget->allocation.x + widget->allocation.width) &&
1589 y > widget->allocation.y &&
1590 y < (widget->allocation.y + widget->allocation.height)) {
1591 frame_button->state = FRAME_BUTTON_ACTIVE;
1593 frame_button->state = FRAME_BUTTON_DEFAULT;
1596 if (frame_button->state != previous_button_state)
1597 widget_schedule_redraw(frame_button->widget);
1599 return CURSOR_LEFT_PTR;
1603 frame_button_redraw_handler(struct widget *widget, void *data)
1605 struct frame_button *frame_button = data;
1607 int width, height, x, y;
1608 struct window *window = widget->window;
1610 x = widget->allocation.x;
1611 y = widget->allocation.y;
1612 width = widget->allocation.width;
1613 height = widget->allocation.height;
1622 cr = cairo_create(window->cairo_surface);
1624 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1625 cairo_set_line_width(cr, 1);
1627 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1628 cairo_rectangle (cr, x, y, 25, 16);
1630 cairo_stroke_preserve(cr);
1632 switch (frame_button->state) {
1633 case FRAME_BUTTON_DEFAULT:
1634 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1636 case FRAME_BUTTON_OVER:
1637 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1639 case FRAME_BUTTON_ACTIVE:
1640 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1649 cairo_set_source_surface(cr, frame_button->icon, x, y);
1655 static struct widget *
1656 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1657 enum frame_button_align align, enum frame_button_decoration style)
1659 struct frame_button *frame_button;
1660 const char *icon = data;
1662 frame_button = malloc (sizeof *frame_button);
1663 memset(frame_button, 0, sizeof *frame_button);
1665 frame_button->icon = cairo_image_surface_create_from_png(icon);
1666 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1667 frame_button->frame = frame;
1668 frame_button->type = type;
1669 frame_button->align = align;
1670 frame_button->decoration = style;
1672 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1674 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1675 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1676 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1677 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1678 widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
1679 return frame_button->widget;
1683 frame_button_destroy(struct frame_button *frame_button)
1685 widget_destroy(frame_button->widget);
1686 wl_list_remove(&frame_button->link);
1687 cairo_surface_destroy(frame_button->icon);
1694 frame_redraw_handler(struct widget *widget, void *data)
1697 struct window *window = widget->window;
1698 struct theme *t = window->display->theme;
1701 if (window->type == TYPE_FULLSCREEN)
1704 cr = cairo_create(window->cairo_surface);
1706 if (window->focus_count)
1707 flags |= THEME_FRAME_ACTIVE;
1708 if (window->type == TYPE_MAXIMIZED)
1709 flags |= THEME_FRAME_MAXIMIZED;
1710 theme_render_frame(t, cr, widget->allocation.width,
1711 widget->allocation.height, window->title, flags);
1717 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1719 struct theme *t = frame->widget->window->display->theme;
1720 struct window *window = frame->widget->window;
1723 location = theme_get_location(t, input->sx, input->sy,
1724 frame->widget->allocation.width,
1725 frame->widget->allocation.height,
1726 window->type == TYPE_MAXIMIZED ?
1727 THEME_FRAME_MAXIMIZED : 0);
1730 case THEME_LOCATION_RESIZING_TOP:
1732 case THEME_LOCATION_RESIZING_BOTTOM:
1733 return CURSOR_BOTTOM;
1734 case THEME_LOCATION_RESIZING_LEFT:
1736 case THEME_LOCATION_RESIZING_RIGHT:
1737 return CURSOR_RIGHT;
1738 case THEME_LOCATION_RESIZING_TOP_LEFT:
1739 return CURSOR_TOP_LEFT;
1740 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1741 return CURSOR_TOP_RIGHT;
1742 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1743 return CURSOR_BOTTOM_LEFT;
1744 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1745 return CURSOR_BOTTOM_RIGHT;
1746 case THEME_LOCATION_EXTERIOR:
1747 case THEME_LOCATION_TITLEBAR:
1749 return CURSOR_LEFT_PTR;
1754 frame_menu_func(struct window *window, int index, void *data)
1756 struct display *display;
1760 if (window->close_handler)
1761 window->close_handler(window->parent,
1764 display_exit(window->display);
1766 case 1: /* fullscreen */
1767 /* we don't have a way to get out of fullscreen for now */
1768 if (window->fullscreen_handler)
1769 window->fullscreen_handler(window, window->user_data);
1771 case 2: /* move to workspace above */
1772 display = window->display;
1773 if (display->workspace > 0)
1774 workspace_manager_move_surface(display->workspace_manager,
1776 display->workspace - 1);
1778 case 3: /* move to workspace below */
1779 display = window->display;
1780 if (display->workspace < display->workspace_count - 1)
1781 workspace_manager_move_surface(display->workspace_manager,
1783 display->workspace + 1);
1789 window_show_frame_menu(struct window *window,
1790 struct input *input, uint32_t time)
1794 static const char *entries[] = {
1795 "Close", "Fullscreen",
1796 "Move to workspace above", "Move to workspace below"
1799 input_get_position(input, &x, &y);
1800 window_show_menu(window->display, input, time, window,
1801 x - 10, y - 10, frame_menu_func, entries,
1802 ARRAY_LENGTH(entries));
1806 frame_enter_handler(struct widget *widget,
1807 struct input *input, float x, float y, void *data)
1809 return frame_get_pointer_image_for_location(data, input);
1813 frame_motion_handler(struct widget *widget,
1814 struct input *input, uint32_t time,
1815 float x, float y, void *data)
1817 return frame_get_pointer_image_for_location(data, input);
1821 frame_button_handler(struct widget *widget,
1822 struct input *input, uint32_t time,
1823 uint32_t button, enum wl_pointer_button_state state,
1827 struct frame *frame = data;
1828 struct window *window = widget->window;
1829 struct display *display = window->display;
1832 location = theme_get_location(display->theme, input->sx, input->sy,
1833 frame->widget->allocation.width,
1834 frame->widget->allocation.height,
1835 window->type == TYPE_MAXIMIZED ?
1836 THEME_FRAME_MAXIMIZED : 0);
1838 if (window->display->shell && button == BTN_LEFT &&
1839 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1841 case THEME_LOCATION_TITLEBAR:
1842 if (!window->shell_surface)
1844 input_set_pointer_image(input, CURSOR_DRAGGING);
1845 input_ungrab(input);
1846 wl_shell_surface_move(window->shell_surface,
1847 input_get_seat(input),
1850 case THEME_LOCATION_RESIZING_TOP:
1851 case THEME_LOCATION_RESIZING_BOTTOM:
1852 case THEME_LOCATION_RESIZING_LEFT:
1853 case THEME_LOCATION_RESIZING_RIGHT:
1854 case THEME_LOCATION_RESIZING_TOP_LEFT:
1855 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1856 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1857 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1858 if (!window->shell_surface)
1860 input_ungrab(input);
1862 if (!display->dpy) {
1863 /* If we're using shm, allocate a big
1864 pool to create buffers out of while
1865 we resize. We should probably base
1866 this number on the size of the output. */
1868 shm_pool_create(display, 6 * 1024 * 1024);
1871 wl_shell_surface_resize(window->shell_surface,
1872 input_get_seat(input),
1873 display->serial, location);
1876 } else if (button == BTN_RIGHT &&
1877 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1878 window_show_frame_menu(window, input, time);
1883 frame_create(struct window *window, void *data)
1885 struct frame *frame;
1887 frame = malloc(sizeof *frame);
1888 memset(frame, 0, sizeof *frame);
1890 frame->widget = window_add_widget(window, frame);
1891 frame->child = widget_add_widget(frame->widget, data);
1893 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1894 widget_set_resize_handler(frame->widget, frame_resize_handler);
1895 widget_set_enter_handler(frame->widget, frame_enter_handler);
1896 widget_set_motion_handler(frame->widget, frame_motion_handler);
1897 widget_set_button_handler(frame->widget, frame_button_handler);
1899 /* Create empty list for frame buttons */
1900 wl_list_init(&frame->buttons_list);
1902 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1903 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1905 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1906 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1908 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1909 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1911 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1912 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1914 window->frame = frame;
1916 return frame->child;
1920 frame_set_child_size(struct widget *widget, int child_width, int child_height)
1922 struct display *display = widget->window->display;
1923 struct theme *t = display->theme;
1924 int decoration_width, decoration_height;
1926 int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1928 if (widget->window->type != TYPE_FULLSCREEN) {
1929 decoration_width = (t->width + margin) * 2;
1930 decoration_height = t->width +
1931 t->titlebar_height + margin * 2;
1933 width = child_width + decoration_width;
1934 height = child_height + decoration_height;
1936 width = child_width;
1937 height = child_height;
1940 window_schedule_resize(widget->window, width, height);
1944 frame_destroy(struct frame *frame)
1946 struct frame_button *button, *tmp;
1948 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1949 frame_button_destroy(button);
1951 /* frame->child must be destroyed by the application */
1952 widget_destroy(frame->widget);
1957 input_set_focus_widget(struct input *input, struct widget *focus,
1960 struct widget *old, *widget;
1961 int pointer = CURSOR_LEFT_PTR;
1963 if (focus == input->focus_widget)
1966 old = input->focus_widget;
1970 widget = input->grab;
1971 if (widget->leave_handler)
1972 widget->leave_handler(old, input, widget->user_data);
1973 input->focus_widget = NULL;
1979 widget = input->grab;
1980 input->focus_widget = focus;
1981 if (widget->enter_handler)
1982 pointer = widget->enter_handler(focus, input, x, y,
1985 input_set_pointer_image(input, pointer);
1990 input_grab(struct input *input, struct widget *widget, uint32_t button)
1992 input->grab = widget;
1993 input->grab_button = button;
1997 input_ungrab(struct input *input)
1999 struct widget *widget;
2002 if (input->pointer_focus) {
2003 widget = widget_find_widget(input->pointer_focus->widget,
2004 input->sx, input->sy);
2005 input_set_focus_widget(input, widget, input->sx, input->sy);
2010 input_remove_pointer_focus(struct input *input)
2012 struct window *window = input->pointer_focus;
2017 input_set_focus_widget(input, NULL, 0, 0);
2019 input->pointer_focus = NULL;
2020 input->current_cursor = CURSOR_UNSET;
2024 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2025 uint32_t serial, struct wl_surface *surface,
2026 wl_fixed_t sx_w, wl_fixed_t sy_w)
2028 struct input *input = data;
2029 struct window *window;
2030 struct widget *widget;
2031 float sx = wl_fixed_to_double(sx_w);
2032 float sy = wl_fixed_to_double(sy_w);
2035 /* enter event for a window we've just destroyed */
2039 input->display->serial = serial;
2040 input->pointer_enter_serial = serial;
2041 input->pointer_focus = wl_surface_get_user_data(surface);
2042 window = input->pointer_focus;
2045 shm_pool_destroy(window->pool);
2046 window->pool = NULL;
2047 /* Schedule a redraw to free the pool */
2048 window_schedule_redraw(window);
2054 widget = widget_find_widget(window->widget, sx, sy);
2055 input_set_focus_widget(input, widget, sx, sy);
2059 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2060 uint32_t serial, struct wl_surface *surface)
2062 struct input *input = data;
2064 input->display->serial = serial;
2065 input_remove_pointer_focus(input);
2069 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2070 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2072 struct input *input = data;
2073 struct window *window = input->pointer_focus;
2074 struct widget *widget;
2075 int cursor = CURSOR_LEFT_PTR;
2076 float sx = wl_fixed_to_double(sx_w);
2077 float sy = wl_fixed_to_double(sy_w);
2085 if (!(input->grab && input->grab_button)) {
2086 widget = widget_find_widget(window->widget, sx, sy);
2087 input_set_focus_widget(input, widget, sx, sy);
2091 widget = input->grab;
2093 widget = input->focus_widget;
2094 if (widget && widget->motion_handler)
2095 cursor = widget->motion_handler(widget,
2096 input, time, sx, sy,
2099 input_set_pointer_image(input, cursor);
2103 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2104 uint32_t time, uint32_t button, uint32_t state_w)
2106 struct input *input = data;
2107 struct widget *widget;
2108 enum wl_pointer_button_state state = state_w;
2110 input->display->serial = serial;
2111 if (input->focus_widget && input->grab == NULL &&
2112 state == WL_POINTER_BUTTON_STATE_PRESSED)
2113 input_grab(input, input->focus_widget, button);
2115 widget = input->grab;
2116 if (widget && widget->button_handler)
2117 (*widget->button_handler)(widget,
2120 input->grab->user_data);
2122 if (input->grab && input->grab_button == button &&
2123 state == WL_POINTER_BUTTON_STATE_RELEASED)
2124 input_ungrab(input);
2128 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2129 uint32_t time, uint32_t axis, wl_fixed_t value)
2131 struct input *input = data;
2132 struct widget *widget;
2134 widget = input->focus_widget;
2136 widget = input->grab;
2137 if (widget && widget->axis_handler)
2138 (*widget->axis_handler)(widget,
2144 static const struct wl_pointer_listener pointer_listener = {
2145 pointer_handle_enter,
2146 pointer_handle_leave,
2147 pointer_handle_motion,
2148 pointer_handle_button,
2149 pointer_handle_axis,
2153 input_remove_keyboard_focus(struct input *input)
2155 struct window *window = input->keyboard_focus;
2156 struct itimerspec its;
2158 its.it_interval.tv_sec = 0;
2159 its.it_interval.tv_nsec = 0;
2160 its.it_value.tv_sec = 0;
2161 its.it_value.tv_nsec = 0;
2162 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2167 window->focus_count--;
2168 if (window->keyboard_focus_handler)
2169 (*window->keyboard_focus_handler)(window, NULL,
2172 input->keyboard_focus = NULL;
2176 keyboard_repeat_func(struct task *task, uint32_t events)
2178 struct input *input =
2179 container_of(task, struct input, repeat_task);
2180 struct window *window = input->keyboard_focus;
2183 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2184 /* If we change the timer between the fd becoming
2185 * readable and getting here, there'll be nothing to
2186 * read and we get EAGAIN. */
2189 if (window && window->key_handler) {
2190 (*window->key_handler)(window, input, input->repeat_time,
2191 input->repeat_key, input->repeat_sym,
2192 WL_KEYBOARD_KEY_STATE_PRESSED,
2198 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2199 uint32_t format, int fd, uint32_t size)
2201 struct input *input = data;
2209 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2214 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2215 if (map_str == MAP_FAILED) {
2220 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2222 XKB_KEYMAP_FORMAT_TEXT_V1,
2224 munmap(map_str, size);
2227 if (!input->xkb.keymap) {
2228 fprintf(stderr, "failed to compile keymap\n");
2232 input->xkb.state = xkb_state_new(input->xkb.keymap);
2233 if (!input->xkb.state) {
2234 fprintf(stderr, "failed to create XKB state\n");
2235 xkb_map_unref(input->xkb.keymap);
2236 input->xkb.keymap = NULL;
2240 input->xkb.control_mask =
2241 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2242 input->xkb.alt_mask =
2243 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2244 input->xkb.shift_mask =
2245 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2249 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2250 uint32_t serial, struct wl_surface *surface,
2251 struct wl_array *keys)
2253 struct input *input = data;
2254 struct window *window;
2256 input->display->serial = serial;
2257 input->keyboard_focus = wl_surface_get_user_data(surface);
2259 window = input->keyboard_focus;
2260 window->focus_count++;
2261 if (window->keyboard_focus_handler)
2262 (*window->keyboard_focus_handler)(window,
2263 input, window->user_data);
2267 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2268 uint32_t serial, struct wl_surface *surface)
2270 struct input *input = data;
2272 input->display->serial = serial;
2273 input_remove_keyboard_focus(input);
2277 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2278 uint32_t serial, uint32_t time, uint32_t key,
2281 struct input *input = data;
2282 struct window *window = input->keyboard_focus;
2283 uint32_t code, num_syms;
2284 enum wl_keyboard_key_state state = state_w;
2285 const xkb_keysym_t *syms;
2287 xkb_mod_mask_t mask;
2288 struct itimerspec its;
2290 input->display->serial = serial;
2292 if (!window || !input->xkb.state)
2295 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2297 mask = xkb_state_serialize_mods(input->xkb.state,
2298 XKB_STATE_DEPRESSED |
2300 input->modifiers = 0;
2301 if (mask & input->xkb.control_mask)
2302 input->modifiers |= MOD_CONTROL_MASK;
2303 if (mask & input->xkb.alt_mask)
2304 input->modifiers |= MOD_ALT_MASK;
2305 if (mask & input->xkb.shift_mask)
2306 input->modifiers |= MOD_SHIFT_MASK;
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;
2360 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2361 mods_locked, 0, 0, group);
2364 static const struct wl_keyboard_listener keyboard_listener = {
2365 keyboard_handle_keymap,
2366 keyboard_handle_enter,
2367 keyboard_handle_leave,
2368 keyboard_handle_key,
2369 keyboard_handle_modifiers,
2373 seat_handle_capabilities(void *data, struct wl_seat *seat,
2374 enum wl_seat_capability caps)
2376 struct input *input = data;
2378 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2379 input->pointer = wl_seat_get_pointer(seat);
2380 wl_pointer_set_user_data(input->pointer, input);
2381 wl_pointer_add_listener(input->pointer, &pointer_listener,
2383 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2384 wl_pointer_destroy(input->pointer);
2385 input->pointer = NULL;
2388 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2389 input->keyboard = wl_seat_get_keyboard(seat);
2390 wl_keyboard_set_user_data(input->keyboard, input);
2391 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2393 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2394 wl_keyboard_destroy(input->keyboard);
2395 input->keyboard = NULL;
2399 static const struct wl_seat_listener seat_listener = {
2400 seat_handle_capabilities,
2404 input_get_position(struct input *input, int32_t *x, int32_t *y)
2411 input_get_display(struct input *input)
2413 return input->display;
2417 input_get_seat(struct input *input)
2423 input_get_modifiers(struct input *input)
2425 return input->modifiers;
2429 input_get_focus_widget(struct input *input)
2431 return input->focus_widget;
2435 struct wl_data_offer *offer;
2436 struct input *input;
2437 struct wl_array types;
2440 struct task io_task;
2448 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2450 struct data_offer *offer = data;
2453 p = wl_array_add(&offer->types, sizeof *p);
2457 static const struct wl_data_offer_listener data_offer_listener = {
2462 data_offer_destroy(struct data_offer *offer)
2467 if (offer->refcount == 0) {
2468 wl_data_offer_destroy(offer->offer);
2469 for (p = offer->types.data; *p; p++)
2471 wl_array_release(&offer->types);
2477 data_device_data_offer(void *data,
2478 struct wl_data_device *data_device,
2479 struct wl_data_offer *_offer)
2481 struct data_offer *offer;
2483 offer = malloc(sizeof *offer);
2485 wl_array_init(&offer->types);
2486 offer->refcount = 1;
2487 offer->input = data;
2488 offer->offer = _offer;
2489 wl_data_offer_add_listener(offer->offer,
2490 &data_offer_listener, offer);
2494 data_device_enter(void *data, struct wl_data_device *data_device,
2495 uint32_t serial, struct wl_surface *surface,
2496 wl_fixed_t x_w, wl_fixed_t y_w,
2497 struct wl_data_offer *offer)
2499 struct input *input = data;
2500 struct window *window;
2502 float x = wl_fixed_to_double(x_w);
2503 float y = wl_fixed_to_double(y_w);
2506 input->pointer_enter_serial = serial;
2507 window = wl_surface_get_user_data(surface);
2508 input->pointer_focus = window;
2511 input->drag_offer = wl_data_offer_get_user_data(offer);
2513 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2516 types_data = input->drag_offer->types.data;
2518 input->drag_offer = NULL;
2522 window = input->pointer_focus;
2523 if (window->data_handler)
2524 window->data_handler(window, input, x, y, types_data,
2529 data_device_leave(void *data, struct wl_data_device *data_device)
2531 struct input *input = data;
2533 if (input->drag_offer) {
2534 data_offer_destroy(input->drag_offer);
2535 input->drag_offer = NULL;
2540 data_device_motion(void *data, struct wl_data_device *data_device,
2541 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2543 struct input *input = data;
2544 struct window *window = input->pointer_focus;
2545 float x = wl_fixed_to_double(x_w);
2546 float y = wl_fixed_to_double(y_w);
2552 if (input->drag_offer)
2553 types_data = input->drag_offer->types.data;
2557 if (window->data_handler)
2558 window->data_handler(window, input, x, y, types_data,
2563 data_device_drop(void *data, struct wl_data_device *data_device)
2565 struct input *input = data;
2566 struct window *window = input->pointer_focus;
2568 if (window->drop_handler)
2569 window->drop_handler(window, input,
2570 input->sx, input->sy, window->user_data);
2574 data_device_selection(void *data,
2575 struct wl_data_device *wl_data_device,
2576 struct wl_data_offer *offer)
2578 struct input *input = data;
2581 if (input->selection_offer)
2582 data_offer_destroy(input->selection_offer);
2585 input->selection_offer = wl_data_offer_get_user_data(offer);
2586 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2589 input->selection_offer = NULL;
2593 static const struct wl_data_device_listener data_device_listener = {
2594 data_device_data_offer,
2599 data_device_selection
2603 input_set_pointer_image_index(struct input *input, int index)
2605 struct wl_buffer *buffer;
2606 struct wl_cursor *cursor;
2607 struct wl_cursor_image *image;
2609 cursor = input->display->cursors[input->current_cursor];
2613 if (index >= (int) cursor->image_count) {
2614 fprintf(stderr, "cursor index out of range\n");
2618 image = cursor->images[index];
2619 buffer = wl_cursor_image_get_buffer(image);
2623 wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
2624 input->pointer_surface,
2625 image->hotspot_x, image->hotspot_y);
2626 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2627 wl_surface_damage(input->pointer_surface, 0, 0,
2628 image->width, image->height);
2629 wl_surface_commit(input->pointer_surface);
2632 static const struct wl_callback_listener pointer_surface_listener;
2635 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2638 struct input *input = data;
2639 struct wl_cursor *cursor;
2643 assert(callback == input->cursor_frame_cb);
2644 wl_callback_destroy(callback);
2645 input->cursor_frame_cb = NULL;
2648 if (input->current_cursor == CURSOR_BLANK) {
2649 wl_pointer_set_cursor(input->pointer,
2650 input->pointer_enter_serial,
2655 if (input->current_cursor == CURSOR_UNSET)
2657 cursor = input->display->cursors[input->current_cursor];
2661 /* FIXME We don't have the current time on the first call so we set
2662 * the animation start to the time of the first frame callback. */
2664 input->cursor_anim_start = 0;
2665 else if (input->cursor_anim_start == 0)
2666 input->cursor_anim_start = time;
2668 if (time == 0 || input->cursor_anim_start == 0)
2671 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2673 input_set_pointer_image_index(input, i);
2675 if (cursor->image_count == 1)
2678 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2679 wl_callback_add_listener(input->cursor_frame_cb,
2680 &pointer_surface_listener, input);
2683 static const struct wl_callback_listener pointer_surface_listener = {
2684 pointer_surface_frame_callback
2688 input_set_pointer_image(struct input *input, int pointer)
2692 if (input->pointer_enter_serial > input->cursor_serial)
2695 if (!force && pointer == input->current_cursor)
2698 input->current_cursor = pointer;
2699 input->cursor_serial = input->pointer_enter_serial;
2700 if (!input->cursor_frame_cb)
2701 pointer_surface_frame_callback(input, NULL, 0);
2703 /* The current frame callback may be stuck if, for instance,
2704 * the set cursor request was processed by the server after
2705 * this client lost the focus. In this case the cursor surface
2706 * might not be mapped and the frame callback wouldn't ever
2707 * complete. Send a set_cursor and attach to try to map the
2708 * cursor surface again so that the callback will finish */
2709 input_set_pointer_image_index(input, 0);
2713 struct wl_data_device *
2714 input_get_data_device(struct input *input)
2716 return input->data_device;
2720 input_set_selection(struct input *input,
2721 struct wl_data_source *source, uint32_t time)
2723 wl_data_device_set_selection(input->data_device, source, time);
2727 input_accept(struct input *input, const char *type)
2729 wl_data_offer_accept(input->drag_offer->offer,
2730 input->pointer_enter_serial, type);
2734 offer_io_func(struct task *task, uint32_t events)
2736 struct data_offer *offer =
2737 container_of(task, struct data_offer, io_task);
2741 len = read(offer->fd, buffer, sizeof buffer);
2742 offer->func(buffer, len,
2743 offer->x, offer->y, offer->user_data);
2747 data_offer_destroy(offer);
2752 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2753 data_func_t func, void *user_data)
2757 if (pipe2(p, O_CLOEXEC) == -1)
2760 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2763 offer->io_task.run = offer_io_func;
2767 offer->user_data = user_data;
2769 display_watch_fd(offer->input->display,
2770 offer->fd, EPOLLIN, &offer->io_task);
2774 input_receive_drag_data(struct input *input, const char *mime_type,
2775 data_func_t func, void *data)
2777 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2778 input->drag_offer->x = input->sx;
2779 input->drag_offer->y = input->sy;
2783 input_receive_selection_data(struct input *input, const char *mime_type,
2784 data_func_t func, void *data)
2788 if (input->selection_offer == NULL)
2791 for (p = input->selection_offer->types.data; *p; p++)
2792 if (strcmp(mime_type, *p) == 0)
2798 data_offer_receive_data(input->selection_offer,
2799 mime_type, func, data);
2804 input_receive_selection_data_to_fd(struct input *input,
2805 const char *mime_type, int fd)
2807 if (input->selection_offer)
2808 wl_data_offer_receive(input->selection_offer->offer,
2815 window_move(struct window *window, struct input *input, uint32_t serial)
2817 if (!window->shell_surface)
2820 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2824 idle_resize(struct window *window)
2826 struct widget *widget;
2828 window->resize_needed = 0;
2829 widget = window->widget;
2830 widget_set_allocation(widget,
2831 window->pending_allocation.x,
2832 window->pending_allocation.y,
2833 window->pending_allocation.width,
2834 window->pending_allocation.height);
2836 if (window->input_region) {
2837 wl_region_destroy(window->input_region);
2838 window->input_region = NULL;
2841 if (window->opaque_region) {
2842 wl_region_destroy(window->opaque_region);
2843 window->opaque_region = NULL;
2846 if (widget->resize_handler)
2847 widget->resize_handler(widget,
2848 widget->allocation.width,
2849 widget->allocation.height,
2852 if (window->allocation.width != widget->allocation.width ||
2853 window->allocation.height != widget->allocation.height) {
2854 window->allocation = widget->allocation;
2855 window_schedule_redraw(window);
2860 window_schedule_resize(struct window *window, int width, int height)
2862 window->pending_allocation.x = 0;
2863 window->pending_allocation.y = 0;
2864 window->pending_allocation.width = width;
2865 window->pending_allocation.height = height;
2867 if (window->min_allocation.width == 0)
2868 window->min_allocation = window->pending_allocation;
2869 if (window->pending_allocation.width < window->min_allocation.width)
2870 window->pending_allocation.width = window->min_allocation.width;
2871 if (window->pending_allocation.height < window->min_allocation.height)
2872 window->pending_allocation.height = window->min_allocation.height;
2874 window->resize_needed = 1;
2875 window_schedule_redraw(window);
2879 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2881 window_schedule_resize(widget->window, width, height);
2885 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2888 wl_shell_surface_pong(shell_surface, serial);
2892 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2893 uint32_t edges, int32_t width, int32_t height)
2895 struct window *window = data;
2897 window->resize_edges = edges;
2898 window_schedule_resize(window, width, height);
2902 menu_destroy(struct menu *menu)
2904 widget_destroy(menu->widget);
2905 window_destroy(menu->window);
2910 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2912 struct window *window = data;
2913 struct menu *menu = window->widget->user_data;
2915 /* FIXME: Need more context in this event, at least the input
2916 * device. Or just use wl_callback. And this really needs to
2917 * be a window vfunc that the menu can set. And we need the
2920 menu->func(window->parent, menu->current, window->parent->user_data);
2921 input_ungrab(menu->input);
2925 static const struct wl_shell_surface_listener shell_surface_listener = {
2932 window_get_allocation(struct window *window,
2933 struct rectangle *allocation)
2935 *allocation = window->allocation;
2939 widget_redraw(struct widget *widget)
2941 struct widget *child;
2943 if (widget->redraw_handler)
2944 widget->redraw_handler(widget, widget->user_data);
2945 wl_list_for_each(child, &widget->child_list, link)
2946 widget_redraw(child);
2950 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2952 struct window *window = data;
2954 assert(callback == window->frame_cb);
2955 wl_callback_destroy(callback);
2956 window->frame_cb = 0;
2957 window->redraw_scheduled = 0;
2958 if (window->redraw_needed)
2959 window_schedule_redraw(window);
2962 static const struct wl_callback_listener listener = {
2967 idle_redraw(struct task *task, uint32_t events)
2969 struct window *window = container_of(task, struct window, redraw_task);
2971 if (window->resize_needed)
2972 idle_resize(window);
2974 window_create_surface(window);
2975 widget_redraw(window->widget);
2976 window_flush(window);
2977 window->redraw_needed = 0;
2978 wl_list_init(&window->redraw_task.link);
2980 window->frame_cb = wl_surface_frame(window->surface);
2981 wl_callback_add_listener(window->frame_cb, &listener, window);
2985 window_schedule_redraw(struct window *window)
2987 window->redraw_needed = 1;
2988 if (!window->redraw_scheduled) {
2989 window->redraw_task.run = idle_redraw;
2990 display_defer(window->display, &window->redraw_task);
2991 window->redraw_scheduled = 1;
2996 window_is_fullscreen(struct window *window)
2998 return window->type == TYPE_FULLSCREEN;
3002 window_set_fullscreen(struct window *window, int fullscreen)
3004 if (!window->display->shell)
3007 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3011 window->type = TYPE_FULLSCREEN;
3012 window->saved_allocation = window->allocation;
3013 wl_shell_surface_set_fullscreen(window->shell_surface,
3014 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3017 window->type = TYPE_TOPLEVEL;
3018 wl_shell_surface_set_toplevel(window->shell_surface);
3019 window_schedule_resize(window,
3020 window->saved_allocation.width,
3021 window->saved_allocation.height);
3026 window_is_maximized(struct window *window)
3028 return window->type == TYPE_MAXIMIZED;
3032 window_set_maximized(struct window *window, int maximized)
3034 if (!window->display->shell)
3037 if ((window->type == TYPE_MAXIMIZED) == maximized)
3040 if (window->type == TYPE_TOPLEVEL) {
3041 window->saved_allocation = window->allocation;
3042 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3043 window->type = TYPE_MAXIMIZED;
3045 wl_shell_surface_set_toplevel(window->shell_surface);
3046 window->type = TYPE_TOPLEVEL;
3047 window_schedule_resize(window,
3048 window->saved_allocation.width,
3049 window->saved_allocation.height);
3054 window_set_user_data(struct window *window, void *data)
3056 window->user_data = data;
3060 window_get_user_data(struct window *window)
3062 return window->user_data;
3066 window_set_key_handler(struct window *window,
3067 window_key_handler_t handler)
3069 window->key_handler = handler;
3073 window_set_keyboard_focus_handler(struct window *window,
3074 window_keyboard_focus_handler_t handler)
3076 window->keyboard_focus_handler = handler;
3080 window_set_data_handler(struct window *window, window_data_handler_t handler)
3082 window->data_handler = handler;
3086 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3088 window->drop_handler = handler;
3092 window_set_close_handler(struct window *window,
3093 window_close_handler_t handler)
3095 window->close_handler = handler;
3099 window_set_fullscreen_handler(struct window *window,
3100 window_fullscreen_handler_t handler)
3102 window->fullscreen_handler = handler;
3106 window_set_title(struct window *window, const char *title)
3108 free(window->title);
3109 window->title = strdup(title);
3110 if (window->shell_surface)
3111 wl_shell_surface_set_title(window->shell_surface, title);
3115 window_get_title(struct window *window)
3117 return window->title;
3121 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3123 struct text_cursor_position *text_cursor_position =
3124 window->display->text_cursor_position;
3126 if (!text_cursor_position)
3129 text_cursor_position_notify(text_cursor_position,
3131 wl_fixed_from_int(x),
3132 wl_fixed_from_int(y));
3136 window_damage(struct window *window, int32_t x, int32_t y,
3137 int32_t width, int32_t height)
3139 wl_surface_damage(window->surface, x, y, width, height);
3143 surface_enter(void *data,
3144 struct wl_surface *wl_surface, struct wl_output *wl_output)
3146 struct window *window = data;
3147 struct output *output;
3148 struct output *output_found = NULL;
3149 struct window_output *window_output;
3151 wl_list_for_each(output, &window->display->output_list, link) {
3152 if (output->output == wl_output) {
3153 output_found = output;
3161 window_output = malloc (sizeof *window_output);
3162 window_output->output = output_found;
3164 wl_list_insert (&window->window_output_list, &window_output->link);
3168 surface_leave(void *data,
3169 struct wl_surface *wl_surface, struct wl_output *output)
3171 struct window *window = data;
3172 struct window_output *window_output;
3173 struct window_output *window_output_found = NULL;
3175 wl_list_for_each(window_output, &window->window_output_list, link) {
3176 if (window_output->output->output == output) {
3177 window_output_found = window_output;
3182 if (window_output_found) {
3183 wl_list_remove(&window_output_found->link);
3184 free(window_output_found);
3188 static const struct wl_surface_listener surface_listener = {
3193 static struct window *
3194 window_create_internal(struct display *display,
3195 struct window *parent, int type)
3197 struct window *window;
3199 window = malloc(sizeof *window);
3203 memset(window, 0, sizeof *window);
3204 window->display = display;
3205 window->parent = parent;
3206 window->surface = wl_compositor_create_surface(display->compositor);
3207 wl_surface_add_listener(window->surface, &surface_listener, window);
3208 if (type != TYPE_CUSTOM && display->shell) {
3209 window->shell_surface =
3210 wl_shell_get_shell_surface(display->shell,
3213 window->allocation.x = 0;
3214 window->allocation.y = 0;
3215 window->allocation.width = 0;
3216 window->allocation.height = 0;
3217 window->saved_allocation = window->allocation;
3218 window->transparent = 1;
3219 window->type = type;
3220 window->input_region = NULL;
3221 window->opaque_region = NULL;
3224 #ifdef HAVE_CAIRO_EGL
3225 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3227 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3230 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3232 wl_surface_set_user_data(window->surface, window);
3233 wl_list_insert(display->window_list.prev, &window->link);
3234 wl_list_init(&window->redraw_task.link);
3236 if (window->shell_surface) {
3237 wl_shell_surface_set_user_data(window->shell_surface, window);
3238 wl_shell_surface_add_listener(window->shell_surface,
3239 &shell_surface_listener, window);
3242 wl_list_init (&window->window_output_list);
3248 window_create(struct display *display)
3250 struct window *window;
3252 window = window_create_internal(display, NULL, TYPE_NONE);
3260 window_create_custom(struct display *display)
3262 struct window *window;
3264 window = window_create_internal(display, NULL, TYPE_CUSTOM);
3272 window_create_transient(struct display *display, struct window *parent,
3273 int32_t x, int32_t y, uint32_t flags)
3275 struct window *window;
3277 window = window_create_internal(parent->display,
3278 parent, TYPE_TRANSIENT);
3286 wl_shell_surface_set_transient(window->shell_surface,
3287 window->parent->surface,
3288 window->x, window->y, flags);
3294 menu_set_item(struct menu *menu, int sy)
3298 next = (sy - 8) / 20;
3299 if (menu->current != next) {
3300 menu->current = next;
3301 widget_schedule_redraw(menu->widget);
3306 menu_motion_handler(struct widget *widget,
3307 struct input *input, uint32_t time,
3308 float x, float y, void *data)
3310 struct menu *menu = data;
3312 if (widget == menu->widget)
3313 menu_set_item(data, y);
3315 return CURSOR_LEFT_PTR;
3319 menu_enter_handler(struct widget *widget,
3320 struct input *input, float x, float y, void *data)
3322 struct menu *menu = data;
3324 if (widget == menu->widget)
3325 menu_set_item(data, y);
3327 return CURSOR_LEFT_PTR;
3331 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3333 struct menu *menu = data;
3335 if (widget == menu->widget)
3336 menu_set_item(data, -200);
3340 menu_button_handler(struct widget *widget,
3341 struct input *input, uint32_t time,
3342 uint32_t button, enum wl_pointer_button_state state,
3346 struct menu *menu = data;
3348 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3349 time - menu->time > 500) {
3350 /* Either relase after press-drag-release or
3351 * click-motion-click. */
3352 menu->func(menu->window->parent,
3353 menu->current, menu->window->parent->user_data);
3354 input_ungrab(input);
3360 menu_redraw_handler(struct widget *widget, void *data)
3363 const int32_t r = 3, margin = 3;
3364 struct menu *menu = data;
3365 int32_t width, height, i;
3366 struct window *window = widget->window;
3368 cr = cairo_create(window->cairo_surface);
3369 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3370 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3373 width = window->allocation.width;
3374 height = window->allocation.height;
3375 rounded_rect(cr, 0, 0, width, height, r);
3377 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3378 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3381 for (i = 0; i < menu->count; i++) {
3382 if (i == menu->current) {
3383 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3384 cairo_rectangle(cr, margin, i * 20 + margin,
3385 width - 2 * margin, 20);
3387 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3388 cairo_move_to(cr, 10, i * 20 + 16);
3389 cairo_show_text(cr, menu->entries[i]);
3391 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3392 cairo_move_to(cr, 10, i * 20 + 16);
3393 cairo_show_text(cr, menu->entries[i]);
3401 window_show_menu(struct display *display,
3402 struct input *input, uint32_t time, struct window *parent,
3403 int32_t x, int32_t y,
3404 menu_func_t func, const char **entries, int count)
3406 struct window *window;
3408 const int32_t margin = 3;
3410 menu = malloc(sizeof *menu);
3414 window = window_create_internal(parent->display, parent, TYPE_MENU);
3420 menu->window = window;
3421 menu->widget = window_add_widget(menu->window, menu);
3422 menu->entries = entries;
3423 menu->count = count;
3427 menu->input = input;
3428 window->type = TYPE_MENU;
3432 input_ungrab(input);
3433 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3434 display_get_serial(window->display),
3435 window->parent->surface,
3436 window->x, window->y, 0);
3438 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3439 widget_set_enter_handler(menu->widget, menu_enter_handler);
3440 widget_set_leave_handler(menu->widget, menu_leave_handler);
3441 widget_set_motion_handler(menu->widget, menu_motion_handler);
3442 widget_set_button_handler(menu->widget, menu_button_handler);
3444 input_grab(input, menu->widget, 0);
3445 window_schedule_resize(window, 200, count * 20 + margin * 2);
3449 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3451 window->buffer_type = type;
3456 display_handle_geometry(void *data,
3457 struct wl_output *wl_output,
3460 int physical_height,
3466 struct output *output = data;
3468 output->allocation.x = x;
3469 output->allocation.y = y;
3470 output->transform = transform;
3474 display_handle_mode(void *data,
3475 struct wl_output *wl_output,
3481 struct output *output = data;
3482 struct display *display = output->display;
3484 if (flags & WL_OUTPUT_MODE_CURRENT) {
3485 output->allocation.width = width;
3486 output->allocation.height = height;
3487 if (display->output_configure_handler)
3488 (*display->output_configure_handler)(
3489 output, display->user_data);
3493 static const struct wl_output_listener output_listener = {
3494 display_handle_geometry,
3499 display_add_output(struct display *d, uint32_t id)
3501 struct output *output;
3503 output = malloc(sizeof *output);
3507 memset(output, 0, sizeof *output);
3508 output->display = d;
3510 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
3511 wl_list_insert(d->output_list.prev, &output->link);
3513 wl_output_add_listener(output->output, &output_listener, output);
3517 output_destroy(struct output *output)
3519 if (output->destroy_handler)
3520 (*output->destroy_handler)(output, output->user_data);
3522 wl_output_destroy(output->output);
3523 wl_list_remove(&output->link);
3528 display_set_global_handler(struct display *display,
3529 display_global_handler_t handler)
3531 struct global *global;
3533 display->global_handler = handler;
3537 wl_list_for_each(global, &display->global_list, link)
3538 display->global_handler(display,
3539 global->name, global->interface,
3540 global->version, display->user_data);
3544 display_set_output_configure_handler(struct display *display,
3545 display_output_handler_t handler)
3547 struct output *output;
3549 display->output_configure_handler = handler;
3553 wl_list_for_each(output, &display->output_list, link)
3554 (*display->output_configure_handler)(output,
3555 display->user_data);
3559 output_set_user_data(struct output *output, void *data)
3561 output->user_data = data;
3565 output_get_user_data(struct output *output)
3567 return output->user_data;
3571 output_set_destroy_handler(struct output *output,
3572 display_output_handler_t handler)
3574 output->destroy_handler = handler;
3575 /* FIXME: implement this, once we have way to remove outputs */
3579 output_get_allocation(struct output *output, struct rectangle *base)
3581 struct rectangle allocation = output->allocation;
3583 switch (output->transform) {
3584 case WL_OUTPUT_TRANSFORM_90:
3585 case WL_OUTPUT_TRANSFORM_270:
3586 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3587 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3588 /* Swap width and height */
3589 allocation.width = output->allocation.height;
3590 allocation.height = output->allocation.width;
3598 output_get_wl_output(struct output *output)
3600 return output->output;
3604 fini_xkb(struct input *input)
3606 xkb_state_unref(input->xkb.state);
3607 xkb_map_unref(input->xkb.keymap);
3611 display_add_input(struct display *d, uint32_t id)
3613 struct input *input;
3615 input = malloc(sizeof *input);
3619 memset(input, 0, sizeof *input);
3621 input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
3622 input->pointer_focus = NULL;
3623 input->keyboard_focus = NULL;
3624 wl_list_insert(d->input_list.prev, &input->link);
3626 wl_seat_add_listener(input->seat, &seat_listener, input);
3627 wl_seat_set_user_data(input->seat, input);
3629 input->data_device =
3630 wl_data_device_manager_get_data_device(d->data_device_manager,
3632 wl_data_device_add_listener(input->data_device, &data_device_listener,
3635 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3637 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3638 TFD_CLOEXEC | TFD_NONBLOCK);
3639 input->repeat_task.run = keyboard_repeat_func;
3640 display_watch_fd(d, input->repeat_timer_fd,
3641 EPOLLIN, &input->repeat_task);
3645 input_destroy(struct input *input)
3647 input_remove_keyboard_focus(input);
3648 input_remove_pointer_focus(input);
3650 if (input->drag_offer)
3651 data_offer_destroy(input->drag_offer);
3653 if (input->selection_offer)
3654 data_offer_destroy(input->selection_offer);
3656 wl_data_device_destroy(input->data_device);
3659 wl_surface_destroy(input->pointer_surface);
3661 wl_list_remove(&input->link);
3662 wl_seat_destroy(input->seat);
3663 close(input->repeat_timer_fd);
3668 init_workspace_manager(struct display *d, uint32_t id)
3670 d->workspace_manager =
3671 wl_registry_bind(d->registry, id,
3672 &workspace_manager_interface, 1);
3673 if (d->workspace_manager != NULL)
3674 workspace_manager_add_listener(d->workspace_manager,
3675 &workspace_manager_listener,
3680 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3681 const char *interface, uint32_t version)
3683 struct display *d = data;
3684 struct global *global;
3686 global = malloc(sizeof *global);
3688 global->interface = strdup(interface);
3689 global->version = version;
3690 wl_list_insert(d->global_list.prev, &global->link);
3692 if (strcmp(interface, "wl_compositor") == 0) {
3693 d->compositor = wl_registry_bind(registry, id,
3694 &wl_compositor_interface, 1);
3695 } else if (strcmp(interface, "wl_output") == 0) {
3696 display_add_output(d, id);
3697 } else if (strcmp(interface, "wl_seat") == 0) {
3698 display_add_input(d, id);
3699 } else if (strcmp(interface, "wl_shell") == 0) {
3700 d->shell = wl_registry_bind(registry,
3701 id, &wl_shell_interface, 1);
3702 } else if (strcmp(interface, "wl_shm") == 0) {
3703 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
3704 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3705 d->data_device_manager =
3706 wl_registry_bind(registry, id,
3707 &wl_data_device_manager_interface, 1);
3708 } else if (strcmp(interface, "text_cursor_position") == 0) {
3709 d->text_cursor_position =
3710 wl_registry_bind(registry, id,
3711 &text_cursor_position_interface, 1);
3712 } else if (strcmp(interface, "workspace_manager") == 0) {
3713 init_workspace_manager(d, id);
3716 if (d->global_handler)
3717 d->global_handler(d, id, interface, version, d->user_data);
3721 display_bind(struct display *display, uint32_t name,
3722 const struct wl_interface *interface, uint32_t version)
3724 return wl_registry_bind(display->registry, name, interface, version);
3727 static const struct wl_registry_listener registry_listener = {
3728 registry_handle_global
3731 #ifdef HAVE_CAIRO_EGL
3733 init_egl(struct display *d)
3735 EGLint major, minor;
3738 #ifdef USE_CAIRO_GLESV2
3739 # define GL_BIT EGL_OPENGL_ES2_BIT
3741 # define GL_BIT EGL_OPENGL_BIT
3744 static const EGLint argb_cfg_attribs[] = {
3745 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3751 EGL_RENDERABLE_TYPE, GL_BIT,
3755 #ifdef USE_CAIRO_GLESV2
3756 static const EGLint context_attribs[] = {
3757 EGL_CONTEXT_CLIENT_VERSION, 2,
3760 EGLint api = EGL_OPENGL_ES_API;
3762 EGLint *context_attribs = NULL;
3763 EGLint api = EGL_OPENGL_API;
3766 d->dpy = eglGetDisplay(d->display);
3767 if (!eglInitialize(d->dpy, &major, &minor)) {
3768 fprintf(stderr, "failed to initialize display\n");
3772 if (!eglBindAPI(api)) {
3773 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3777 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3778 &d->argb_config, 1, &n) || n != 1) {
3779 fprintf(stderr, "failed to choose argb config\n");
3783 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3784 EGL_NO_CONTEXT, context_attribs);
3785 if (d->argb_ctx == NULL) {
3786 fprintf(stderr, "failed to create context\n");
3790 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3791 fprintf(stderr, "failed to make context current\n");
3795 #ifdef HAVE_CAIRO_EGL
3796 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3797 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3798 fprintf(stderr, "failed to get cairo egl argb device\n");
3807 fini_egl(struct display *display)
3809 #ifdef HAVE_CAIRO_EGL
3810 cairo_device_destroy(display->argb_device);
3813 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3816 eglTerminate(display->dpy);
3822 handle_display_data(struct task *task, uint32_t events)
3824 struct display *display =
3825 container_of(task, struct display, display_task);
3826 struct epoll_event ep;
3829 display->display_fd_events = events;
3831 if (events & EPOLLERR || events & EPOLLHUP) {
3832 display_exit(display);
3836 if (events & EPOLLIN)
3837 wl_display_dispatch(display->display);
3839 if (events & EPOLLOUT) {
3840 ret = wl_display_flush(display->display);
3842 ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3843 ep.data.ptr = &display->display_task;
3844 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3845 display->display_fd, &ep);
3846 } else if (ret == -1 && errno != EAGAIN) {
3847 display_exit(display);
3854 display_create(int argc, char *argv[])
3858 d = malloc(sizeof *d);
3862 memset(d, 0, sizeof *d);
3864 d->display = wl_display_connect(NULL);
3865 if (d->display == NULL) {
3866 fprintf(stderr, "failed to create display: %m\n");
3870 d->epoll_fd = os_epoll_create_cloexec();
3871 d->display_fd = wl_display_get_fd(d->display);
3872 d->display_task.run = handle_display_data;
3873 display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3876 wl_list_init(&d->deferred_list);
3877 wl_list_init(&d->input_list);
3878 wl_list_init(&d->output_list);
3879 wl_list_init(&d->global_list);
3881 d->xkb_context = xkb_context_new(0);
3882 if (d->xkb_context == NULL) {
3883 fprintf(stderr, "Failed to create XKB context\n");
3888 d->workspace_count = 1;
3890 d->registry = wl_display_get_registry(d->display);
3891 wl_registry_add_listener(d->registry, ®istry_listener, d);
3892 wl_display_dispatch(d->display);
3893 #ifdef HAVE_CAIRO_EGL
3894 if (init_egl(d) < 0)
3898 d->image_target_texture_2d =
3899 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3900 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3901 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3905 d->theme = theme_create();
3907 wl_list_init(&d->window_list);
3913 display_destroy_outputs(struct display *display)
3916 struct output *output;
3918 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3919 output_destroy(output);
3923 display_destroy_inputs(struct display *display)
3926 struct input *input;
3928 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3929 input_destroy(input);
3933 display_destroy(struct display *display)
3935 if (!wl_list_empty(&display->window_list))
3936 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3937 wl_list_length(&display->window_list));
3939 if (!wl_list_empty(&display->deferred_list))
3940 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3942 display_destroy_outputs(display);
3943 display_destroy_inputs(display);
3945 xkb_context_unref(display->xkb_context);
3947 theme_destroy(display->theme);
3948 destroy_cursors(display);
3950 #ifdef HAVE_CAIRO_EGL
3955 wl_shell_destroy(display->shell);
3958 wl_shm_destroy(display->shm);
3960 if (display->data_device_manager)
3961 wl_data_device_manager_destroy(display->data_device_manager);
3963 wl_compositor_destroy(display->compositor);
3965 close(display->epoll_fd);
3967 if (!(display->display_fd_events & EPOLLERR) &&
3968 !(display->display_fd_events & EPOLLHUP))
3969 wl_display_flush(display->display);
3971 wl_display_disconnect(display->display);
3976 display_set_user_data(struct display *display, void *data)
3978 display->user_data = data;
3982 display_get_user_data(struct display *display)
3984 return display->user_data;
3988 display_get_display(struct display *display)
3990 return display->display;
3994 display_get_output(struct display *display)
3996 return container_of(display->output_list.next, struct output, link);
3999 struct wl_compositor *
4000 display_get_compositor(struct display *display)
4002 return display->compositor;
4006 display_get_serial(struct display *display)
4008 return display->serial;
4012 display_get_egl_display(struct display *d)
4017 struct wl_data_source *
4018 display_create_data_source(struct display *display)
4020 return wl_data_device_manager_create_data_source(display->data_device_manager);
4024 display_get_argb_egl_config(struct display *d)
4026 return d->argb_config;
4030 display_get_shell(struct display *display)
4032 return display->shell;
4036 display_acquire_window_surface(struct display *display,
4037 struct window *window,
4040 #ifdef HAVE_CAIRO_EGL
4041 struct egl_window_surface_data *data;
4042 cairo_device_t *device;
4044 if (!window->cairo_surface)
4046 device = cairo_surface_get_device(window->cairo_surface);
4051 if (device == display->argb_device)
4052 ctx = display->argb_ctx;
4057 data = cairo_surface_get_user_data(window->cairo_surface,
4060 cairo_device_flush(device);
4061 cairo_device_acquire(device);
4062 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4063 fprintf(stderr, "failed to make surface current\n");
4072 display_release_window_surface(struct display *display,
4073 struct window *window)
4075 #ifdef HAVE_CAIRO_EGL
4076 cairo_device_t *device;
4078 device = cairo_surface_get_device(window->cairo_surface);
4082 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
4083 fprintf(stderr, "failed to make context current\n");
4084 cairo_device_release(device);
4089 display_defer(struct display *display, struct task *task)
4091 wl_list_insert(&display->deferred_list, &task->link);
4095 display_watch_fd(struct display *display,
4096 int fd, uint32_t events, struct task *task)
4098 struct epoll_event ep;
4102 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4106 display_run(struct display *display)
4109 struct epoll_event ep[16];
4112 display->running = 1;
4114 while (!wl_list_empty(&display->deferred_list)) {
4115 task = container_of(display->deferred_list.prev,
4117 wl_list_remove(&task->link);
4121 if (!display->running)
4124 ret = wl_display_flush(display->display);
4125 if (ret < 0 && errno == EAGAIN) {
4127 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4128 ep[0].data.ptr = &display->display_task;
4130 epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4131 display->display_fd, &ep[0]);
4132 } else if (ret < 0) {
4136 count = epoll_wait(display->epoll_fd,
4137 ep, ARRAY_LENGTH(ep), -1);
4138 for (i = 0; i < count; i++) {
4139 task = ep[i].data.ptr;
4140 task->run(task, ep[i].events);
4146 display_exit(struct display *display)
4148 display->running = 0;