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>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
58 #include <xkbcommon/xkbcommon.h>
59 #include <X11/keysym.h>
61 #include <X11/Xcursor/Xcursor.h>
63 #include <linux/input.h>
64 #include <wayland-client.h>
65 #include "cairo-util.h"
73 struct wl_display *display;
74 struct wl_compositor *compositor;
75 struct wl_shell *shell;
77 struct wl_data_device_manager *data_device_manager;
79 EGLConfig argb_config;
81 cairo_device_t *argb_device;
86 struct task display_task;
89 struct wl_list deferred_list;
93 struct wl_list window_list;
94 struct wl_list input_list;
95 struct wl_list output_list;
96 cairo_surface_t *active_frame, *inactive_frame, *shadow;
100 struct xkb_rule_names names;
101 struct xkb_keymap *keymap;
102 struct xkb_state *state;
103 struct xkb_context *context;
104 xkb_mod_mask_t control_mask;
105 xkb_mod_mask_t alt_mask;
106 xkb_mod_mask_t shift_mask;
109 struct cursor *cursors;
110 struct shm_pool *cursor_shm_pool;
112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
116 display_output_handler_t output_configure_handler;
132 struct display *display;
133 struct window *parent;
134 struct wl_surface *surface;
135 struct wl_shell_surface *shell_surface;
136 struct wl_region *input_region;
137 struct wl_region *opaque_region;
139 struct rectangle allocation, saved_allocation, server_allocation;
140 struct rectangle pending_allocation;
143 int redraw_scheduled;
145 struct task redraw_task;
149 struct input *keyboard_device;
150 enum window_buffer_type buffer_type;
152 cairo_surface_t *cairo_surface;
154 struct shm_pool *pool;
156 window_key_handler_t key_handler;
157 window_keyboard_focus_handler_t keyboard_focus_handler;
158 window_data_handler_t data_handler;
159 window_drop_handler_t drop_handler;
160 window_close_handler_t close_handler;
163 struct widget *widget;
170 struct window *window;
171 struct wl_list child_list;
173 struct rectangle allocation;
174 widget_resize_handler_t resize_handler;
175 widget_redraw_handler_t redraw_handler;
176 widget_enter_handler_t enter_handler;
177 widget_leave_handler_t leave_handler;
178 widget_motion_handler_t motion_handler;
179 widget_button_handler_t button_handler;
185 struct display *display;
186 struct wl_input_device *input_device;
187 struct window *pointer_focus;
188 struct window *keyboard_focus;
191 uint32_t pointer_enter_serial;
195 struct widget *focus_widget;
197 uint32_t grab_button;
199 struct wl_data_device *data_device;
200 struct data_offer *drag_offer;
201 struct data_offer *selection_offer;
205 struct display *display;
206 struct wl_output *output;
207 struct rectangle allocation;
210 display_output_handler_t destroy_handler;
215 struct widget *widget;
216 struct widget *child;
223 struct window *window;
224 struct widget *widget;
226 const char **entries;
233 struct cursor_image {
234 cairo_surface_t *surface;
236 int hotspot_x, hotspot_y;
242 struct cursor_image *images;
246 struct wl_shm_pool *pool;
253 POINTER_DEFAULT = 100,
257 enum window_location {
259 WINDOW_RESIZING_TOP = 1,
260 WINDOW_RESIZING_BOTTOM = 2,
261 WINDOW_RESIZING_LEFT = 4,
262 WINDOW_RESIZING_TOP_LEFT = 5,
263 WINDOW_RESIZING_BOTTOM_LEFT = 6,
264 WINDOW_RESIZING_RIGHT = 8,
265 WINDOW_RESIZING_TOP_RIGHT = 9,
266 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
267 WINDOW_RESIZING_MASK = 15,
268 WINDOW_EXTERIOR = 16,
269 WINDOW_TITLEBAR = 17,
270 WINDOW_CLIENT_AREA = 18,
273 const char *option_xkb_layout = "us";
274 const char *option_xkb_variant = "";
275 const char *option_xkb_options = "";
277 static const struct weston_option xkb_options[] = {
278 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
279 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
280 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
283 static const cairo_user_data_key_t surface_data_key;
284 struct surface_data {
285 struct wl_buffer *buffer;
288 #define MULT(_d,c,a,t) \
289 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
291 #ifdef HAVE_CAIRO_EGL
293 struct egl_window_surface_data {
294 struct display *display;
295 struct wl_surface *surface;
296 struct wl_egl_window *window;
301 egl_window_surface_data_destroy(void *p)
303 struct egl_window_surface_data *data = p;
304 struct display *d = data->display;
306 eglDestroySurface(d->dpy, data->surf);
307 wl_egl_window_destroy(data->window);
308 data->surface = NULL;
313 static cairo_surface_t *
314 display_create_egl_window_surface(struct display *display,
315 struct wl_surface *surface,
317 struct rectangle *rectangle)
319 cairo_surface_t *cairo_surface;
320 struct egl_window_surface_data *data;
322 cairo_device_t *device;
324 data = malloc(sizeof *data);
328 data->display = display;
329 data->surface = surface;
331 config = display->argb_config;
332 device = display->argb_device;
334 data->window = wl_egl_window_create(surface,
338 data->surf = eglCreateWindowSurface(display->dpy, config,
341 cairo_surface = cairo_gl_surface_create_for_egl(device,
346 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
347 data, egl_window_surface_data_destroy);
349 return cairo_surface;
355 display_get_buffer_for_surface(struct display *display,
356 cairo_surface_t *surface)
358 struct surface_data *data;
360 data = cairo_surface_get_user_data (surface, &surface_data_key);
365 struct shm_surface_data {
366 struct surface_data data;
367 struct shm_pool *pool;
371 shm_pool_destroy(struct shm_pool *pool);
374 shm_surface_data_destroy(void *p)
376 struct shm_surface_data *data = p;
378 wl_buffer_destroy(data->data.buffer);
380 shm_pool_destroy(data->pool);
384 shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
386 void *dest = cairo_image_surface_get_data(surface);
388 memcpy(dest, data, count);
391 static struct wl_shm_pool *
392 make_shm_pool(struct display *display, int size, void **data)
394 char filename[] = "/tmp/wayland-shm-XXXXXX";
395 struct wl_shm_pool *pool;
398 fd = mkstemp(filename);
400 fprintf(stderr, "open %s failed: %m\n", filename);
403 if (ftruncate(fd, size) < 0) {
404 fprintf(stderr, "ftruncate failed: %m\n");
409 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
412 if (*data == MAP_FAILED) {
413 fprintf(stderr, "mmap failed: %m\n");
418 pool = wl_shm_create_pool(display->shm, fd, size);
425 static struct shm_pool *
426 shm_pool_create(struct display *display, size_t size)
428 struct shm_pool *pool = malloc(sizeof *pool);
433 pool->pool = make_shm_pool(display, size, &pool->data);
446 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
448 if (pool->used + size > pool->size)
451 *offset = pool->used;
454 return (char *) pool->data + *offset;
457 /* destroy the pool. this does not unmap the memory though */
459 shm_pool_destroy(struct shm_pool *pool)
461 munmap(pool->data, pool->size);
462 wl_shm_pool_destroy(pool->pool);
466 /* Start allocating from the beginning of the pool again */
468 shm_pool_reset(struct shm_pool *pool)
474 data_length_for_shm_surface(struct rectangle *rect)
478 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
480 return stride * rect->height;
483 static cairo_surface_t *
484 display_create_shm_surface_from_pool(struct display *display,
485 struct rectangle *rectangle,
486 uint32_t flags, struct shm_pool *pool)
488 struct shm_surface_data *data;
490 cairo_surface_t *surface;
491 int stride, length, offset;
494 data = malloc(sizeof *data);
498 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
500 length = stride * rectangle->height;
502 map = shm_pool_allocate(pool, length, &offset);
509 surface = cairo_image_surface_create_for_data (map,
515 cairo_surface_set_user_data (surface, &surface_data_key,
516 data, shm_surface_data_destroy);
518 if (flags & SURFACE_OPAQUE)
519 format = WL_SHM_FORMAT_XRGB8888;
521 format = WL_SHM_FORMAT_ARGB8888;
523 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
531 static cairo_surface_t *
532 display_create_shm_surface(struct display *display,
533 struct rectangle *rectangle, uint32_t flags,
534 struct window *window)
536 struct shm_surface_data *data;
537 struct shm_pool *pool;
538 cairo_surface_t *surface;
540 if (window && window->pool) {
541 shm_pool_reset(window->pool);
542 surface = display_create_shm_surface_from_pool(display,
550 pool = shm_pool_create(display,
551 data_length_for_shm_surface(rectangle));
556 display_create_shm_surface_from_pool(display, rectangle,
560 shm_pool_destroy(pool);
564 /* make sure we destroy the pool when the surface is destroyed */
565 data = cairo_surface_get_user_data(surface, &surface_data_key);
572 check_size(struct rectangle *rect)
574 if (rect->width && rect->height)
577 fprintf(stderr, "tried to create surface of "
578 "width: %d, height: %d\n", rect->width, rect->height);
583 display_create_surface(struct display *display,
584 struct wl_surface *surface,
585 struct rectangle *rectangle,
588 if (check_size(rectangle) < 0)
590 #ifdef HAVE_CAIRO_EGL
592 return display_create_egl_window_surface(display,
597 return display_create_shm_surface(display, rectangle, flags, NULL);
600 static const char *cursors[] = {
601 "bottom_left_corner",
602 "bottom_right_corner",
616 create_cursor_from_images(struct display *display, struct cursor *cursor,
617 XcursorImages *images)
620 struct rectangle rect;
623 cursor->images = malloc(images->nimage * sizeof *cursor->images);
624 cursor->n_images = images->nimage;
626 for (i = 0; i < images->nimage; i++) {
627 image = images->images[i];
629 rect.width = image->width;
630 rect.height = image->height;
632 cursor->images[i].surface =
633 display_create_shm_surface_from_pool(display, &rect, 0,
634 display->cursor_shm_pool);
636 shm_surface_write(cursor->images[i].surface,
637 (unsigned char *) image->pixels,
638 image->width * image->height * sizeof image->pixels[0]);
640 cursor->images[i].width = image->width;
641 cursor->images[i].height = image->height;
642 cursor->images[i].hotspot_x = image->xhot;
643 cursor->images[i].hotspot_y = image->yhot;
644 cursor->images[i].delay = image->delay;
650 data_length_for_cursor_images(XcursorImages *images)
654 struct rectangle rect;
656 for (i = 0; i < images->nimage; i++) {
657 rect.width = images->images[i]->width;
658 rect.height = images->images[i]->height;
659 length += data_length_for_shm_surface(&rect);
666 create_cursors(struct display *display)
669 size_t pool_size = 0;
670 struct cursor *cursor;
671 XcursorImages **images;
673 count = ARRAY_LENGTH(cursors);
674 display->cursors = malloc(count * sizeof *display->cursors);
675 images = malloc(count * sizeof images[0]);
677 for (i = 0; i < count; i++) {
678 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
680 fprintf(stderr, "Error loading cursor: %s\n",
684 pool_size += data_length_for_cursor_images(images[i]);
687 display->cursor_shm_pool = shm_pool_create(display, pool_size);
689 for (i = 0; i < count; i++) {
690 cursor = &display->cursors[i];
693 cursor->n_images = 0;
694 cursor->images = NULL;
698 create_cursor_from_images(display, cursor, images[i]);
700 XcursorImagesDestroy(images[i]);
707 destroy_cursor_images(struct cursor *cursor)
711 for (i = 0; i < cursor->n_images; i++)
712 if (cursor->images[i].surface)
713 cairo_surface_destroy(cursor->images[i].surface);
715 free(cursor->images);
719 destroy_cursors(struct display *display)
723 count = ARRAY_LENGTH(cursors);
724 for (i = 0; i < count; ++i) {
725 destroy_cursor_images(&display->cursors[i]);
728 free(display->cursors);
729 shm_pool_destroy(display->cursor_shm_pool);
733 display_get_pointer_surface(struct display *display, int pointer,
734 int *width, int *height,
735 int *hotspot_x, int *hotspot_y)
737 struct cursor *cursor = &display->cursors[pointer];
738 cairo_surface_t *surface = cursor->images[0].surface;
740 /* FIXME returning information for the first image. Something better
741 * is needed for animated cursors */
743 *width = cairo_image_surface_get_width(surface);
744 *height = cairo_image_surface_get_height(surface);
746 *hotspot_x = cursor->images[0].hotspot_x;
747 *hotspot_y = cursor->images[0].hotspot_y;
749 return cairo_surface_reference(surface);
753 window_get_resize_dx_dy(struct window *window, int *x, int *y)
755 if (window->resize_edges & WINDOW_RESIZING_LEFT)
756 *x = window->server_allocation.width - window->allocation.width;
760 if (window->resize_edges & WINDOW_RESIZING_TOP)
761 *y = window->server_allocation.height -
762 window->allocation.height;
766 window->resize_edges = 0;
770 window_attach_surface(struct window *window)
772 struct display *display = window->display;
773 struct wl_buffer *buffer;
774 #ifdef HAVE_CAIRO_EGL
775 struct egl_window_surface_data *data;
779 if (window->type == TYPE_NONE) {
780 window->type = TYPE_TOPLEVEL;
782 wl_shell_surface_set_toplevel(window->shell_surface);
785 switch (window->buffer_type) {
786 #ifdef HAVE_CAIRO_EGL
787 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
788 data = cairo_surface_get_user_data(window->cairo_surface,
791 cairo_gl_surface_swapbuffers(window->cairo_surface);
792 wl_egl_window_get_attached_size(data->window,
793 &window->server_allocation.width,
794 &window->server_allocation.height);
797 case WINDOW_BUFFER_TYPE_SHM:
799 display_get_buffer_for_surface(display,
800 window->cairo_surface);
802 window_get_resize_dx_dy(window, &x, &y);
803 wl_surface_attach(window->surface, buffer, x, y);
804 wl_surface_damage(window->surface, 0, 0,
805 window->allocation.width,
806 window->allocation.height);
807 window->server_allocation = window->allocation;
808 cairo_surface_destroy(window->cairo_surface);
809 window->cairo_surface = NULL;
815 if (window->input_region) {
816 wl_surface_set_input_region(window->surface,
817 window->input_region);
818 wl_region_destroy(window->input_region);
819 window->input_region = NULL;
822 if (window->opaque_region) {
823 wl_surface_set_opaque_region(window->surface,
824 window->opaque_region);
825 wl_region_destroy(window->opaque_region);
826 window->opaque_region = NULL;
831 window_flush(struct window *window)
833 if (window->cairo_surface)
834 window_attach_surface(window);
838 window_set_surface(struct window *window, cairo_surface_t *surface)
840 cairo_surface_reference(surface);
842 if (window->cairo_surface != NULL)
843 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = surface;
848 #ifdef HAVE_CAIRO_EGL
850 window_resize_cairo_window_surface(struct window *window)
852 struct egl_window_surface_data *data;
855 data = cairo_surface_get_user_data(window->cairo_surface,
858 window_get_resize_dx_dy(window, &x, &y),
859 wl_egl_window_resize(data->window,
860 window->allocation.width,
861 window->allocation.height,
864 cairo_gl_surface_set_size(window->cairo_surface,
865 window->allocation.width,
866 window->allocation.height);
871 window_get_display(struct window *window)
873 return window->display;
877 window_create_surface(struct window *window)
879 cairo_surface_t *surface;
882 if (!window->transparent)
883 flags = SURFACE_OPAQUE;
885 switch (window->buffer_type) {
886 #ifdef HAVE_CAIRO_EGL
887 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
888 if (window->cairo_surface) {
889 window_resize_cairo_window_surface(window);
892 surface = display_create_surface(window->display,
894 &window->allocation, flags);
897 case WINDOW_BUFFER_TYPE_SHM:
898 surface = display_create_shm_surface(window->display,
907 window_set_surface(window, surface);
908 cairo_surface_destroy(surface);
911 static void frame_destroy(struct frame *frame);
914 window_destroy(struct window *window)
916 struct display *display = window->display;
919 if (window->redraw_scheduled)
920 wl_list_remove(&window->redraw_task.link);
922 wl_list_for_each(input, &display->input_list, link) {
923 if (input->pointer_focus == window)
924 input->pointer_focus = NULL;
925 if (input->keyboard_focus == window)
926 input->keyboard_focus = NULL;
927 if (input->focus_widget &&
928 input->focus_widget->window == window)
929 input->focus_widget = NULL;
932 if (window->input_region)
933 wl_region_destroy(window->input_region);
934 if (window->opaque_region)
935 wl_region_destroy(window->opaque_region);
938 frame_destroy(window->frame);
940 if (window->shell_surface)
941 wl_shell_surface_destroy(window->shell_surface);
942 wl_surface_destroy(window->surface);
943 wl_list_remove(&window->link);
945 if (window->cairo_surface != NULL)
946 cairo_surface_destroy(window->cairo_surface);
952 static struct widget *
953 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
955 struct widget *child, *target;
957 wl_list_for_each(child, &widget->child_list, link) {
958 target = widget_find_widget(child, x, y);
963 if (widget->allocation.x <= x &&
964 x < widget->allocation.x + widget->allocation.width &&
965 widget->allocation.y <= y &&
966 y < widget->allocation.y + widget->allocation.height) {
973 static struct widget *
974 widget_create(struct window *window, void *data)
976 struct widget *widget;
978 widget = malloc(sizeof *widget);
979 memset(widget, 0, sizeof *widget);
980 widget->window = window;
981 widget->user_data = data;
982 widget->allocation = window->allocation;
983 wl_list_init(&widget->child_list);
990 window_add_widget(struct window *window, void *data)
992 window->widget = widget_create(window, data);
993 wl_list_init(&window->widget->link);
995 return window->widget;
999 widget_add_widget(struct widget *parent, void *data)
1001 struct widget *widget;
1003 widget = widget_create(parent->window, data);
1004 wl_list_insert(parent->child_list.prev, &widget->link);
1010 widget_destroy(struct widget *widget)
1012 struct display *display = widget->window->display;
1013 struct input *input;
1015 wl_list_for_each(input, &display->input_list, link) {
1016 if (input->focus_widget == widget)
1017 input->focus_widget = NULL;
1020 wl_list_remove(&widget->link);
1025 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1027 *allocation = widget->allocation;
1031 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1033 widget->allocation.width = width;
1034 widget->allocation.height = height;
1038 widget_set_allocation(struct widget *widget,
1039 int32_t x, int32_t y, int32_t width, int32_t height)
1041 widget->allocation.x = x;
1042 widget->allocation.y = y;
1043 widget_set_size(widget, width, height);
1047 widget_set_transparent(struct widget *widget, int transparent)
1049 widget->opaque = !transparent;
1053 widget_get_user_data(struct widget *widget)
1055 return widget->user_data;
1059 widget_set_resize_handler(struct widget *widget,
1060 widget_resize_handler_t handler)
1062 widget->resize_handler = handler;
1066 widget_set_redraw_handler(struct widget *widget,
1067 widget_redraw_handler_t handler)
1069 widget->redraw_handler = handler;
1073 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1075 widget->enter_handler = handler;
1079 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1081 widget->leave_handler = handler;
1085 widget_set_motion_handler(struct widget *widget,
1086 widget_motion_handler_t handler)
1088 widget->motion_handler = handler;
1092 widget_set_button_handler(struct widget *widget,
1093 widget_button_handler_t handler)
1095 widget->button_handler = handler;
1099 widget_schedule_redraw(struct widget *widget)
1101 window_schedule_redraw(widget->window);
1105 window_get_surface(struct window *window)
1107 return cairo_surface_reference(window->cairo_surface);
1111 window_get_wl_surface(struct window *window)
1113 return window->surface;
1116 struct wl_shell_surface *
1117 window_get_wl_shell_surface(struct window *window)
1119 return window->shell_surface;
1123 frame_resize_handler(struct widget *widget,
1124 int32_t width, int32_t height, void *data)
1126 struct frame *frame = data;
1127 struct widget *child = frame->child;
1128 struct rectangle allocation;
1129 struct display *display = widget->window->display;
1130 int decoration_width, decoration_height;
1133 if (widget->window->type != TYPE_FULLSCREEN) {
1134 decoration_width = (frame->width + frame->margin) * 2;
1135 decoration_height = frame->width +
1136 frame->titlebar_height + frame->margin * 2;
1138 allocation.x = frame->width + frame->margin;
1139 allocation.y = frame->titlebar_height + frame->margin;
1140 allocation.width = width - decoration_width;
1141 allocation.height = height - decoration_height;
1143 widget->window->input_region =
1144 wl_compositor_create_region(display->compositor);
1145 wl_region_add(widget->window->input_region,
1146 frame->margin, frame->margin,
1147 width - 2 * frame->margin,
1148 height - 2 * frame->margin);
1150 opaque_margin = frame->margin + display->frame_radius;
1152 decoration_width = 0;
1153 decoration_height = 0;
1157 allocation.width = width;
1158 allocation.height = height;
1162 widget_set_allocation(child, allocation.x, allocation.y,
1163 allocation.width, allocation.height);
1165 if (child->resize_handler)
1166 child->resize_handler(child,
1171 widget_set_allocation(widget, 0, 0,
1172 child->allocation.width + decoration_width,
1173 child->allocation.height + decoration_height);
1175 if (child->opaque) {
1176 widget->window->opaque_region =
1177 wl_compositor_create_region(display->compositor);
1178 wl_region_add(widget->window->opaque_region,
1179 opaque_margin, opaque_margin,
1180 widget->allocation.width - 2 * opaque_margin,
1181 widget->allocation.height - 2 * opaque_margin);
1186 frame_redraw_handler(struct widget *widget, void *data)
1188 struct frame *frame = data;
1190 cairo_text_extents_t extents;
1191 cairo_surface_t *source;
1192 int x, y, width, height;
1193 struct window *window = widget->window;
1195 if (window->type == TYPE_FULLSCREEN)
1198 width = widget->allocation.width;
1199 height = widget->allocation.height;
1201 cr = cairo_create(window->cairo_surface);
1203 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1204 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1207 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
1208 tile_mask(cr, window->display->shadow,
1209 2, 2, width + 8, height + 8,
1212 if (window->keyboard_device)
1213 source = window->display->active_frame;
1215 source = window->display->inactive_frame;
1217 tile_source(cr, source,
1218 frame->margin, frame->margin,
1219 width - frame->margin * 2, height - frame->margin * 2,
1220 frame->width, frame->titlebar_height);
1222 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1223 cairo_select_font_face(cr, "sans",
1224 CAIRO_FONT_SLANT_NORMAL,
1225 CAIRO_FONT_WEIGHT_BOLD);
1226 cairo_set_font_size(cr, 14);
1227 cairo_text_extents(cr, window->title, &extents);
1228 x = (width - extents.width) / 2;
1229 y = frame->margin + 8 - extents.y_bearing;
1230 if (window->keyboard_device) {
1231 cairo_move_to(cr, x + 1, y + 1);
1232 cairo_set_source_rgb(cr, 1, 1, 1);
1233 cairo_show_text(cr, window->title);
1234 cairo_move_to(cr, x, y);
1235 cairo_set_source_rgb(cr, 0, 0, 0);
1236 cairo_show_text(cr, window->title);
1238 cairo_move_to(cr, x, y);
1239 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1240 cairo_show_text(cr, window->title);
1247 frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1249 struct widget *widget = frame->widget;
1250 int vlocation, hlocation, location;
1251 const int grip_size = 8;
1253 if (x < frame->margin)
1254 hlocation = WINDOW_EXTERIOR;
1255 else if (frame->margin <= x && x < frame->margin + grip_size)
1256 hlocation = WINDOW_RESIZING_LEFT;
1257 else if (x < widget->allocation.width - frame->margin - grip_size)
1258 hlocation = WINDOW_INTERIOR;
1259 else if (x < widget->allocation.width - frame->margin)
1260 hlocation = WINDOW_RESIZING_RIGHT;
1262 hlocation = WINDOW_EXTERIOR;
1264 if (y < frame->margin)
1265 vlocation = WINDOW_EXTERIOR;
1266 else if (frame->margin <= y && y < frame->margin + grip_size)
1267 vlocation = WINDOW_RESIZING_TOP;
1268 else if (y < widget->allocation.height - frame->margin - grip_size)
1269 vlocation = WINDOW_INTERIOR;
1270 else if (y < widget->allocation.height - frame->margin)
1271 vlocation = WINDOW_RESIZING_BOTTOM;
1273 vlocation = WINDOW_EXTERIOR;
1275 location = vlocation | hlocation;
1276 if (location & WINDOW_EXTERIOR)
1277 location = WINDOW_EXTERIOR;
1278 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
1279 location = WINDOW_TITLEBAR;
1280 else if (location == WINDOW_INTERIOR)
1281 location = WINDOW_CLIENT_AREA;
1287 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1291 location = frame_get_pointer_location(frame, input->sx, input->sy);
1293 case WINDOW_RESIZING_TOP:
1295 case WINDOW_RESIZING_BOTTOM:
1296 return POINTER_BOTTOM;
1297 case WINDOW_RESIZING_LEFT:
1298 return POINTER_LEFT;
1299 case WINDOW_RESIZING_RIGHT:
1300 return POINTER_RIGHT;
1301 case WINDOW_RESIZING_TOP_LEFT:
1302 return POINTER_TOP_LEFT;
1303 case WINDOW_RESIZING_TOP_RIGHT:
1304 return POINTER_TOP_RIGHT;
1305 case WINDOW_RESIZING_BOTTOM_LEFT:
1306 return POINTER_BOTTOM_LEFT;
1307 case WINDOW_RESIZING_BOTTOM_RIGHT:
1308 return POINTER_BOTTOM_RIGHT;
1309 case WINDOW_EXTERIOR:
1310 case WINDOW_TITLEBAR:
1312 return POINTER_LEFT_PTR;
1317 frame_menu_func(struct window *window, int index, void *data)
1321 if (window->close_handler)
1322 window->close_handler(window->parent,
1325 display_exit(window->display);
1327 case 1: /* fullscreen */
1328 /* we don't have a way to get out of fullscreen for now */
1329 window_set_fullscreen(window, 1);
1331 case 2: /* rotate */
1338 window_show_frame_menu(struct window *window,
1339 struct input *input, uint32_t time)
1343 static const char *entries[] = {
1344 "Close", "Fullscreen", "Rotate", "Scale"
1347 input_get_position(input, &x, &y);
1348 window_show_menu(window->display, input, time, window,
1349 x - 10, y - 10, frame_menu_func, entries, 4);
1353 frame_enter_handler(struct widget *widget,
1354 struct input *input, float x, float y, void *data)
1356 return frame_get_pointer_image_for_location(data, input);
1360 frame_motion_handler(struct widget *widget,
1361 struct input *input, uint32_t time,
1362 float x, float y, void *data)
1364 return frame_get_pointer_image_for_location(data, input);
1368 frame_button_handler(struct widget *widget,
1369 struct input *input, uint32_t time,
1370 uint32_t button, uint32_t state, void *data)
1373 struct frame *frame = data;
1374 struct window *window = widget->window;
1375 struct display *display = window->display;
1378 location = frame_get_pointer_location(frame, input->sx, input->sy);
1380 if (window->display->shell && button == BTN_LEFT && state == 1) {
1382 case WINDOW_TITLEBAR:
1383 if (!window->shell_surface)
1385 input_set_pointer_image(input, time, POINTER_DRAGGING);
1386 input_ungrab(input);
1387 wl_shell_surface_move(window->shell_surface,
1388 input_get_input_device(input),
1391 case WINDOW_RESIZING_TOP:
1392 case WINDOW_RESIZING_BOTTOM:
1393 case WINDOW_RESIZING_LEFT:
1394 case WINDOW_RESIZING_RIGHT:
1395 case WINDOW_RESIZING_TOP_LEFT:
1396 case WINDOW_RESIZING_TOP_RIGHT:
1397 case WINDOW_RESIZING_BOTTOM_LEFT:
1398 case WINDOW_RESIZING_BOTTOM_RIGHT:
1399 if (!window->shell_surface)
1401 input_ungrab(input);
1403 if (!display->dpy) {
1404 /* If we're using shm, allocate a big
1405 pool to create buffers out of while
1406 we resize. We should probably base
1407 this number on the size of the output. */
1409 shm_pool_create(display, 6 * 1024 * 1024);
1412 wl_shell_surface_resize(window->shell_surface,
1413 input_get_input_device(input),
1414 display->serial, location);
1417 } else if (button == BTN_RIGHT && state == 1) {
1418 window_show_frame_menu(window, input, time);
1423 frame_create(struct window *window, void *data)
1425 struct frame *frame;
1427 frame = malloc(sizeof *frame);
1428 memset(frame, 0, sizeof *frame);
1430 frame->widget = window_add_widget(window, frame);
1431 frame->child = widget_add_widget(frame->widget, data);
1434 frame->titlebar_height = 30
1436 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1437 widget_set_resize_handler(frame->widget, frame_resize_handler);
1438 widget_set_enter_handler(frame->widget, frame_enter_handler);
1439 widget_set_motion_handler(frame->widget, frame_motion_handler);
1440 widget_set_button_handler(frame->widget, frame_button_handler);
1442 window->frame = frame;
1444 return frame->child;
1448 frame_destroy(struct frame *frame)
1450 /* frame->child must be destroyed by the application */
1451 widget_destroy(frame->widget);
1456 input_set_focus_widget(struct input *input, struct widget *focus,
1459 struct widget *old, *widget;
1460 int pointer = POINTER_LEFT_PTR;
1462 if (focus == input->focus_widget)
1465 old = input->focus_widget;
1469 widget = input->grab;
1470 if (widget->leave_handler)
1471 widget->leave_handler(old, input, widget->user_data);
1472 input->focus_widget = NULL;
1478 widget = input->grab;
1479 if (widget->enter_handler)
1480 pointer = widget->enter_handler(focus, input, x, y,
1482 input->focus_widget = focus;
1484 input_set_pointer_image(input, input->pointer_enter_serial,
1490 input_handle_motion(void *data, struct wl_input_device *input_device,
1491 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1493 struct input *input = data;
1494 struct window *window = input->pointer_focus;
1495 struct widget *widget;
1496 int pointer = POINTER_LEFT_PTR;
1497 float sx = wl_fixed_to_double(sx_w);
1498 float sy = wl_fixed_to_double(sy_w);
1503 if (!(input->grab && input->grab_button)) {
1504 widget = widget_find_widget(window->widget, sx, sy);
1505 input_set_focus_widget(input, widget, sx, sy);
1509 widget = input->grab;
1511 widget = input->focus_widget;
1512 if (widget && widget->motion_handler)
1513 pointer = widget->motion_handler(input->focus_widget,
1514 input, time, sx, sy,
1517 input_set_pointer_image(input, time, pointer);
1521 input_grab(struct input *input, struct widget *widget, uint32_t button)
1523 input->grab = widget;
1524 input->grab_button = button;
1528 input_ungrab(struct input *input)
1530 struct widget *widget;
1533 if (input->pointer_focus) {
1534 widget = widget_find_widget(input->pointer_focus->widget,
1535 input->sx, input->sy);
1536 input_set_focus_widget(input, widget, input->sx, input->sy);
1541 input_handle_button(void *data,
1542 struct wl_input_device *input_device, uint32_t serial,
1543 uint32_t time, uint32_t button, uint32_t state)
1545 struct input *input = data;
1546 struct widget *widget;
1548 input->display->serial = serial;
1549 if (input->focus_widget && input->grab == NULL && state)
1550 input_grab(input, input->focus_widget, button);
1552 widget = input->grab;
1553 if (widget && widget->button_handler)
1554 (*widget->button_handler)(widget,
1557 input->grab->user_data);
1559 if (input->grab && input->grab_button == button && !state)
1560 input_ungrab(input);
1564 input_handle_axis(void *data,
1565 struct wl_input_device *input_device,
1566 uint32_t time, uint32_t axis, int32_t value)
1571 input_handle_key(void *data, struct wl_input_device *input_device,
1572 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1574 struct input *input = data;
1575 struct window *window = input->keyboard_focus;
1576 struct display *d = input->display;
1577 uint32_t code, num_syms;
1578 const xkb_keysym_t *syms;
1580 xkb_mod_mask_t mask;
1582 input->display->serial = serial;
1584 if (!window || window->keyboard_device != input)
1587 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1588 xkb_state_update_key(d->xkb.state, code,
1589 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1591 mask = xkb_state_serialise_mods(d->xkb.state,
1592 XKB_STATE_DEPRESSED |
1594 input->modifiers = 0;
1595 if (mask & input->display->xkb.control_mask)
1596 input->modifiers |= MOD_CONTROL_MASK;
1597 if (mask & input->display->xkb.alt_mask)
1598 input->modifiers |= MOD_ALT_MASK;
1599 if (mask & input->display->xkb.shift_mask)
1600 input->modifiers |= MOD_SHIFT_MASK;
1602 if (num_syms == 1 && syms[0] == XK_F5 &&
1603 input->modifiers == MOD_ALT_MASK) {
1605 window_set_maximized(window,
1606 window->type != TYPE_MAXIMIZED);
1607 } else if (window->key_handler) {
1613 (*window->key_handler)(window, input, time, key,
1614 sym, state, window->user_data);
1619 input_remove_pointer_focus(struct input *input)
1621 struct window *window = input->pointer_focus;
1626 input_set_focus_widget(input, NULL, 0, 0);
1628 input->pointer_focus = NULL;
1629 input->current_cursor = POINTER_UNSET;
1633 input_handle_pointer_enter(void *data,
1634 struct wl_input_device *input_device,
1635 uint32_t serial, struct wl_surface *surface,
1636 wl_fixed_t sx_w, wl_fixed_t sy_w)
1638 struct input *input = data;
1639 struct window *window;
1640 struct widget *widget;
1641 float sx = wl_fixed_to_double(sx_w);
1642 float sy = wl_fixed_to_double(sy_w);
1644 input->display->serial = serial;
1645 input->pointer_enter_serial = serial;
1646 input->pointer_focus = wl_surface_get_user_data(surface);
1647 window = input->pointer_focus;
1650 shm_pool_destroy(window->pool);
1651 window->pool = NULL;
1652 /* Schedule a redraw to free the pool */
1653 window_schedule_redraw(window);
1659 widget = widget_find_widget(window->widget, sx, sy);
1660 input_set_focus_widget(input, widget, sx, sy);
1664 input_handle_pointer_leave(void *data,
1665 struct wl_input_device *input_device,
1666 uint32_t serial, struct wl_surface *surface)
1668 struct input *input = data;
1670 input->display->serial = serial;
1671 input_remove_pointer_focus(input);
1675 input_remove_keyboard_focus(struct input *input)
1677 struct window *window = input->keyboard_focus;
1682 window->keyboard_device = NULL;
1683 if (window->keyboard_focus_handler)
1684 (*window->keyboard_focus_handler)(window, NULL,
1687 input->keyboard_focus = NULL;
1691 input_handle_keyboard_enter(void *data,
1692 struct wl_input_device *input_device,
1694 struct wl_surface *surface,
1695 struct wl_array *keys)
1697 struct input *input = data;
1698 struct window *window;
1700 input->display->serial = serial;
1701 input->keyboard_focus = wl_surface_get_user_data(surface);
1703 window = input->keyboard_focus;
1704 window->keyboard_device = input;
1705 if (window->keyboard_focus_handler)
1706 (*window->keyboard_focus_handler)(window,
1707 window->keyboard_device,
1712 input_handle_keyboard_leave(void *data,
1713 struct wl_input_device *input_device,
1715 struct wl_surface *surface)
1717 struct input *input = data;
1719 input->display->serial = serial;
1720 input_remove_keyboard_focus(input);
1724 input_handle_touch_down(void *data,
1725 struct wl_input_device *wl_input_device,
1726 uint32_t serial, uint32_t time,
1727 struct wl_surface *surface,
1728 int32_t id, wl_fixed_t x, wl_fixed_t y)
1733 input_handle_touch_up(void *data,
1734 struct wl_input_device *wl_input_device,
1735 uint32_t serial, uint32_t time, int32_t id)
1740 input_handle_touch_motion(void *data,
1741 struct wl_input_device *wl_input_device,
1742 uint32_t time, int32_t id,
1743 wl_fixed_t x, wl_fixed_t y)
1748 input_handle_touch_frame(void *data,
1749 struct wl_input_device *wl_input_device)
1754 input_handle_touch_cancel(void *data,
1755 struct wl_input_device *wl_input_device)
1759 static const struct wl_input_device_listener input_device_listener = {
1760 input_handle_motion,
1761 input_handle_button,
1764 input_handle_pointer_enter,
1765 input_handle_pointer_leave,
1766 input_handle_keyboard_enter,
1767 input_handle_keyboard_leave,
1768 input_handle_touch_down,
1769 input_handle_touch_up,
1770 input_handle_touch_motion,
1771 input_handle_touch_frame,
1772 input_handle_touch_cancel,
1776 input_get_position(struct input *input, int32_t *x, int32_t *y)
1782 struct wl_input_device *
1783 input_get_input_device(struct input *input)
1785 return input->input_device;
1789 input_get_modifiers(struct input *input)
1791 return input->modifiers;
1795 input_get_focus_widget(struct input *input)
1797 return input->focus_widget;
1801 struct wl_data_offer *offer;
1802 struct input *input;
1803 struct wl_array types;
1806 struct task io_task;
1814 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1816 struct data_offer *offer = data;
1819 p = wl_array_add(&offer->types, sizeof *p);
1823 static const struct wl_data_offer_listener data_offer_listener = {
1828 data_offer_destroy(struct data_offer *offer)
1833 if (offer->refcount == 0) {
1834 wl_data_offer_destroy(offer->offer);
1835 for (p = offer->types.data; *p; p++)
1837 wl_array_release(&offer->types);
1843 data_device_data_offer(void *data,
1844 struct wl_data_device *data_device, uint32_t id)
1846 struct data_offer *offer;
1848 offer = malloc(sizeof *offer);
1850 wl_array_init(&offer->types);
1851 offer->refcount = 1;
1852 offer->input = data;
1854 /* FIXME: Generate typesafe wrappers for this */
1855 offer->offer = (struct wl_data_offer *)
1856 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1857 id, &wl_data_offer_interface);
1859 wl_data_offer_add_listener(offer->offer,
1860 &data_offer_listener, offer);
1864 data_device_enter(void *data, struct wl_data_device *data_device,
1865 uint32_t serial, struct wl_surface *surface,
1866 wl_fixed_t x_w, wl_fixed_t y_w,
1867 struct wl_data_offer *offer)
1869 struct input *input = data;
1870 struct window *window;
1871 float x = wl_fixed_to_double(x_w);
1872 float y = wl_fixed_to_double(y_w);
1875 input->pointer_enter_serial = serial;
1876 input->drag_offer = wl_data_offer_get_user_data(offer);
1877 window = wl_surface_get_user_data(surface);
1878 input->pointer_focus = window;
1880 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1883 window = input->pointer_focus;
1884 if (window->data_handler)
1885 window->data_handler(window, input, x, y,
1886 input->drag_offer->types.data,
1891 data_device_leave(void *data, struct wl_data_device *data_device)
1893 struct input *input = data;
1895 data_offer_destroy(input->drag_offer);
1896 input->drag_offer = NULL;
1900 data_device_motion(void *data, struct wl_data_device *data_device,
1901 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
1903 struct input *input = data;
1904 struct window *window = input->pointer_focus;
1905 float x = wl_fixed_to_double(x_w);
1906 float y = wl_fixed_to_double(y_w);
1911 if (window->data_handler)
1912 window->data_handler(window, input, x, y,
1913 input->drag_offer->types.data,
1918 data_device_drop(void *data, struct wl_data_device *data_device)
1920 struct input *input = data;
1921 struct window *window = input->pointer_focus;
1923 if (window->drop_handler)
1924 window->drop_handler(window, input,
1925 input->sx, input->sy, window->user_data);
1929 data_device_selection(void *data,
1930 struct wl_data_device *wl_data_device,
1931 struct wl_data_offer *offer)
1933 struct input *input = data;
1936 if (input->selection_offer)
1937 data_offer_destroy(input->selection_offer);
1940 input->selection_offer = wl_data_offer_get_user_data(offer);
1941 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1946 static const struct wl_data_device_listener data_device_listener = {
1947 data_device_data_offer,
1952 data_device_selection
1956 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1958 struct display *display = input->display;
1959 struct wl_buffer *buffer;
1960 struct cursor_image *image;
1962 if (pointer == input->current_cursor)
1965 if (display->cursors[pointer].n_images == 0)
1968 image = &display->cursors[pointer].images[0];
1970 if (!image->surface)
1973 input->current_cursor = pointer;
1974 buffer = display_get_buffer_for_surface(display, image->surface);
1975 wl_input_device_attach(input->input_device, time, buffer,
1976 image->hotspot_x, image->hotspot_y);
1979 struct wl_data_device *
1980 input_get_data_device(struct input *input)
1982 return input->data_device;
1986 input_set_selection(struct input *input,
1987 struct wl_data_source *source, uint32_t time)
1989 wl_data_device_set_selection(input->data_device, source, time);
1993 input_accept(struct input *input, const char *type)
1995 wl_data_offer_accept(input->drag_offer->offer,
1996 input->pointer_enter_serial, type);
2000 offer_io_func(struct task *task, uint32_t events)
2002 struct data_offer *offer =
2003 container_of(task, struct data_offer, io_task);
2007 len = read(offer->fd, buffer, sizeof buffer);
2008 offer->func(buffer, len,
2009 offer->x, offer->y, offer->user_data);
2013 data_offer_destroy(offer);
2018 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2019 data_func_t func, void *user_data)
2023 if (pipe2(p, O_CLOEXEC) == -1)
2026 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2029 offer->io_task.run = offer_io_func;
2033 offer->user_data = user_data;
2035 display_watch_fd(offer->input->display,
2036 offer->fd, EPOLLIN, &offer->io_task);
2040 input_receive_drag_data(struct input *input, const char *mime_type,
2041 data_func_t func, void *data)
2043 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2044 input->drag_offer->x = input->sx;
2045 input->drag_offer->y = input->sy;
2049 input_receive_selection_data(struct input *input, const char *mime_type,
2050 data_func_t func, void *data)
2054 if (input->selection_offer == NULL)
2057 for (p = input->selection_offer->types.data; *p; p++)
2058 if (strcmp(mime_type, *p) == 0)
2064 data_offer_receive_data(input->selection_offer,
2065 mime_type, func, data);
2070 input_receive_selection_data_to_fd(struct input *input,
2071 const char *mime_type, int fd)
2073 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2079 window_move(struct window *window, struct input *input, uint32_t serial)
2081 if (!window->shell_surface)
2084 wl_shell_surface_move(window->shell_surface,
2085 input->input_device, serial);
2089 idle_resize(struct window *window)
2091 struct widget *widget;
2093 window->resize_needed = 0;
2094 widget = window->widget;
2095 widget_set_allocation(widget,
2096 window->pending_allocation.x,
2097 window->pending_allocation.y,
2098 window->pending_allocation.width,
2099 window->pending_allocation.height);
2101 if (window->input_region) {
2102 wl_region_destroy(window->input_region);
2103 window->input_region = NULL;
2106 if (window->opaque_region) {
2107 wl_region_destroy(window->opaque_region);
2108 window->opaque_region = NULL;
2111 if (widget->resize_handler)
2112 widget->resize_handler(widget,
2113 widget->allocation.width,
2114 widget->allocation.height,
2117 if (window->allocation.width != widget->allocation.width ||
2118 window->allocation.height != widget->allocation.height) {
2119 window->allocation = widget->allocation;
2120 window_schedule_redraw(window);
2125 window_schedule_resize(struct window *window, int width, int height)
2127 window->pending_allocation.x = 0;
2128 window->pending_allocation.y = 0;
2129 window->pending_allocation.width = width;
2130 window->pending_allocation.height = height;
2132 window->resize_needed = 1;
2133 window_schedule_redraw(window);
2137 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2139 window_schedule_resize(widget->window, width, height);
2143 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2146 wl_shell_surface_pong(shell_surface, serial);
2150 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2151 uint32_t edges, int32_t width, int32_t height)
2153 struct window *window = data;
2155 if (width <= 0 || height <= 0)
2158 window->resize_edges = edges;
2159 window_schedule_resize(window, width, height);
2163 menu_destroy(struct menu *menu)
2165 widget_destroy(menu->widget);
2166 window_destroy(menu->window);
2171 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2173 struct window *window = data;
2174 struct menu *menu = window->widget->user_data;
2176 /* FIXME: Need more context in this event, at least the input
2177 * device. Or just use wl_callback. And this really needs to
2178 * be a window vfunc that the menu can set. And we need the
2181 menu->func(window->parent, menu->current, window->parent->user_data);
2182 input_ungrab(menu->input);
2186 static const struct wl_shell_surface_listener shell_surface_listener = {
2193 window_get_allocation(struct window *window,
2194 struct rectangle *allocation)
2196 *allocation = window->allocation;
2200 widget_redraw(struct widget *widget)
2202 struct widget *child;
2204 if (widget->redraw_handler)
2205 widget->redraw_handler(widget, widget->user_data);
2206 wl_list_for_each(child, &widget->child_list, link)
2207 widget_redraw(child);
2211 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2213 struct window *window = data;
2215 wl_callback_destroy(callback);
2216 window->redraw_scheduled = 0;
2217 if (window->redraw_needed)
2218 window_schedule_redraw(window);
2221 static const struct wl_callback_listener listener = {
2226 idle_redraw(struct task *task, uint32_t events)
2228 struct window *window = container_of(task, struct window, redraw_task);
2229 struct wl_callback *callback;
2231 if (window->resize_needed)
2232 idle_resize(window);
2234 window_create_surface(window);
2235 widget_redraw(window->widget);
2236 window_flush(window);
2237 window->redraw_needed = 0;
2238 wl_list_init(&window->redraw_task.link);
2240 callback = wl_surface_frame(window->surface);
2241 wl_callback_add_listener(callback, &listener, window);
2245 window_schedule_redraw(struct window *window)
2247 window->redraw_needed = 1;
2248 if (!window->redraw_scheduled) {
2249 window->redraw_task.run = idle_redraw;
2250 display_defer(window->display, &window->redraw_task);
2251 window->redraw_scheduled = 1;
2256 window_set_custom(struct window *window)
2258 window->type = TYPE_CUSTOM;
2262 window_set_fullscreen(struct window *window, int fullscreen)
2264 if (!window->display->shell)
2267 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2271 window->type = TYPE_FULLSCREEN;
2272 window->saved_allocation = window->allocation;
2273 wl_shell_surface_set_fullscreen(window->shell_surface,
2274 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2277 window->type = TYPE_TOPLEVEL;
2278 wl_shell_surface_set_toplevel(window->shell_surface);
2279 window_schedule_resize(window,
2280 window->saved_allocation.width,
2281 window->saved_allocation.height);
2286 window_set_maximized(struct window *window, int maximized)
2288 if (!window->display->shell)
2291 if ((window->type == TYPE_MAXIMIZED) == maximized)
2294 if (window->type == TYPE_TOPLEVEL) {
2295 window->saved_allocation = window->allocation;
2296 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2297 window->type = TYPE_MAXIMIZED;
2299 wl_shell_surface_set_toplevel(window->shell_surface);
2300 window->type = TYPE_TOPLEVEL;
2301 window_schedule_resize(window,
2302 window->saved_allocation.width,
2303 window->saved_allocation.height);
2308 window_set_user_data(struct window *window, void *data)
2310 window->user_data = data;
2314 window_get_user_data(struct window *window)
2316 return window->user_data;
2320 window_set_key_handler(struct window *window,
2321 window_key_handler_t handler)
2323 window->key_handler = handler;
2327 window_set_keyboard_focus_handler(struct window *window,
2328 window_keyboard_focus_handler_t handler)
2330 window->keyboard_focus_handler = handler;
2334 window_set_data_handler(struct window *window, window_data_handler_t handler)
2336 window->data_handler = handler;
2340 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2342 window->drop_handler = handler;
2346 window_set_close_handler(struct window *window,
2347 window_close_handler_t handler)
2349 window->close_handler = handler;
2353 window_set_title(struct window *window, const char *title)
2355 free(window->title);
2356 window->title = strdup(title);
2357 if (window->shell_surface)
2358 wl_shell_surface_set_title(window->shell_surface, title);
2362 window_get_title(struct window *window)
2364 return window->title;
2368 window_damage(struct window *window, int32_t x, int32_t y,
2369 int32_t width, int32_t height)
2371 wl_surface_damage(window->surface, x, y, width, height);
2375 surface_enter(void *data,
2376 struct wl_surface *wl_surface, struct wl_output *output)
2381 surface_leave(void *data,
2382 struct wl_surface *wl_surface, struct wl_output *output)
2386 static const struct wl_surface_listener surface_listener = {
2391 static struct window *
2392 window_create_internal(struct display *display, struct window *parent)
2394 struct window *window;
2396 window = malloc(sizeof *window);
2400 memset(window, 0, sizeof *window);
2401 window->display = display;
2402 window->parent = parent;
2403 window->surface = wl_compositor_create_surface(display->compositor);
2404 wl_surface_add_listener(window->surface, &surface_listener, window);
2405 if (display->shell) {
2406 window->shell_surface =
2407 wl_shell_get_shell_surface(display->shell,
2410 wl_shell_surface_set_title(window->shell_surface,
2413 window->allocation.x = 0;
2414 window->allocation.y = 0;
2415 window->allocation.width = 0;
2416 window->allocation.height = 0;
2417 window->saved_allocation = window->allocation;
2418 window->transparent = 1;
2419 window->type = TYPE_NONE;
2420 window->input_region = NULL;
2421 window->opaque_region = NULL;
2424 #ifdef HAVE_CAIRO_EGL
2425 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2427 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2430 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2432 wl_surface_set_user_data(window->surface, window);
2433 wl_list_insert(display->window_list.prev, &window->link);
2434 wl_list_init(&window->redraw_task.link);
2436 if (window->shell_surface) {
2437 wl_shell_surface_set_user_data(window->shell_surface, window);
2438 wl_shell_surface_add_listener(window->shell_surface,
2439 &shell_surface_listener, window);
2446 window_create(struct display *display)
2448 struct window *window;
2450 window = window_create_internal(display, NULL);
2458 window_create_transient(struct display *display, struct window *parent,
2459 int32_t x, int32_t y)
2461 struct window *window;
2463 window = window_create_internal(parent->display, parent);
2467 window->type = TYPE_TRANSIENT;
2472 wl_shell_surface_set_transient(window->shell_surface,
2473 window->parent->shell_surface,
2474 window->x, window->y, 0);
2480 menu_set_item(struct menu *menu, int sy)
2484 next = (sy - 8) / 20;
2485 if (menu->current != next) {
2486 menu->current = next;
2487 widget_schedule_redraw(menu->widget);
2492 menu_motion_handler(struct widget *widget,
2493 struct input *input, uint32_t time,
2494 float x, float y, void *data)
2496 struct menu *menu = data;
2498 if (widget == menu->widget)
2499 menu_set_item(data, y);
2501 return POINTER_LEFT_PTR;
2505 menu_enter_handler(struct widget *widget,
2506 struct input *input, float x, float y, void *data)
2508 struct menu *menu = data;
2510 if (widget == menu->widget)
2511 menu_set_item(data, y);
2513 return POINTER_LEFT_PTR;
2517 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2519 struct menu *menu = data;
2521 if (widget == menu->widget)
2522 menu_set_item(data, -200);
2526 menu_button_handler(struct widget *widget,
2527 struct input *input, uint32_t time,
2528 uint32_t button, uint32_t state, void *data)
2531 struct menu *menu = data;
2533 if (state == 0 && time - menu->time > 500) {
2534 /* Either relase after press-drag-release or
2535 * click-motion-click. */
2536 menu->func(menu->window->parent,
2537 menu->current, menu->window->parent->user_data);
2538 input_ungrab(input);
2544 menu_redraw_handler(struct widget *widget, void *data)
2547 const int32_t r = 3, margin = 3;
2548 struct menu *menu = data;
2549 int32_t width, height, i;
2550 struct window *window = widget->window;
2552 cr = cairo_create(window->cairo_surface);
2553 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2554 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2557 width = window->allocation.width;
2558 height = window->allocation.height;
2559 rounded_rect(cr, 0, 0, width, height, r);
2561 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2562 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2565 for (i = 0; i < menu->count; i++) {
2566 if (i == menu->current) {
2567 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2568 cairo_rectangle(cr, margin, i * 20 + margin,
2569 width - 2 * margin, 20);
2571 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2572 cairo_move_to(cr, 10, i * 20 + 16);
2573 cairo_show_text(cr, menu->entries[i]);
2575 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2576 cairo_move_to(cr, 10, i * 20 + 16);
2577 cairo_show_text(cr, menu->entries[i]);
2585 window_show_menu(struct display *display,
2586 struct input *input, uint32_t time, struct window *parent,
2587 int32_t x, int32_t y,
2588 menu_func_t func, const char **entries, int count)
2590 struct window *window;
2592 const int32_t margin = 3;
2594 menu = malloc(sizeof *menu);
2598 window = window_create_internal(parent->display, parent);
2602 menu->window = window;
2603 menu->widget = window_add_widget(menu->window, menu);
2604 menu->entries = entries;
2605 menu->count = count;
2609 menu->input = input;
2610 window->type = TYPE_MENU;
2614 input_ungrab(input);
2615 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2616 display_get_serial(window->display),
2617 window->parent->shell_surface,
2618 window->x, window->y, 0);
2620 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
2621 widget_set_enter_handler(menu->widget, menu_enter_handler);
2622 widget_set_leave_handler(menu->widget, menu_leave_handler);
2623 widget_set_motion_handler(menu->widget, menu_motion_handler);
2624 widget_set_button_handler(menu->widget, menu_button_handler);
2626 input_grab(input, menu->widget, 0);
2627 window_schedule_resize(window, 200, count * 20 + margin * 2);
2631 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2633 window->buffer_type = type;
2638 display_handle_geometry(void *data,
2639 struct wl_output *wl_output,
2642 int physical_height,
2647 struct output *output = data;
2649 output->allocation.x = x;
2650 output->allocation.y = y;
2654 display_handle_mode(void *data,
2655 struct wl_output *wl_output,
2661 struct output *output = data;
2662 struct display *display = output->display;
2664 if (flags & WL_OUTPUT_MODE_CURRENT) {
2665 output->allocation.width = width;
2666 output->allocation.height = height;
2667 if (display->output_configure_handler)
2668 (*display->output_configure_handler)(
2669 output, display->user_data);
2673 static const struct wl_output_listener output_listener = {
2674 display_handle_geometry,
2679 display_add_output(struct display *d, uint32_t id)
2681 struct output *output;
2683 output = malloc(sizeof *output);
2687 memset(output, 0, sizeof *output);
2688 output->display = d;
2690 wl_display_bind(d->display, id, &wl_output_interface);
2691 wl_list_insert(d->output_list.prev, &output->link);
2693 wl_output_add_listener(output->output, &output_listener, output);
2697 output_destroy(struct output *output)
2699 if (output->destroy_handler)
2700 (*output->destroy_handler)(output, output->user_data);
2702 wl_output_destroy(output->output);
2703 wl_list_remove(&output->link);
2708 display_set_output_configure_handler(struct display *display,
2709 display_output_handler_t handler)
2711 struct output *output;
2713 display->output_configure_handler = handler;
2717 wl_list_for_each(output, &display->output_list, link)
2718 (*display->output_configure_handler)(output,
2719 display->user_data);
2723 output_set_user_data(struct output *output, void *data)
2725 output->user_data = data;
2729 output_get_user_data(struct output *output)
2731 return output->user_data;
2735 output_set_destroy_handler(struct output *output,
2736 display_output_handler_t handler)
2738 output->destroy_handler = handler;
2739 /* FIXME: implement this, once we have way to remove outputs */
2743 output_get_allocation(struct output *output, struct rectangle *allocation)
2745 *allocation = output->allocation;
2749 output_get_wl_output(struct output *output)
2751 return output->output;
2755 display_add_input(struct display *d, uint32_t id)
2757 struct input *input;
2759 input = malloc(sizeof *input);
2763 memset(input, 0, sizeof *input);
2765 input->input_device =
2766 wl_display_bind(d->display, id, &wl_input_device_interface);
2767 input->pointer_focus = NULL;
2768 input->keyboard_focus = NULL;
2769 wl_list_insert(d->input_list.prev, &input->link);
2771 wl_input_device_add_listener(input->input_device,
2772 &input_device_listener, input);
2773 wl_input_device_set_user_data(input->input_device, input);
2775 input->data_device =
2776 wl_data_device_manager_get_data_device(d->data_device_manager,
2777 input->input_device);
2778 wl_data_device_add_listener(input->data_device,
2779 &data_device_listener, input);
2783 input_destroy(struct input *input)
2785 input_remove_keyboard_focus(input);
2786 input_remove_pointer_focus(input);
2788 if (input->drag_offer)
2789 data_offer_destroy(input->drag_offer);
2791 if (input->selection_offer)
2792 data_offer_destroy(input->selection_offer);
2794 wl_data_device_destroy(input->data_device);
2795 wl_list_remove(&input->link);
2796 wl_input_device_destroy(input->input_device);
2801 display_handle_global(struct wl_display *display, uint32_t id,
2802 const char *interface, uint32_t version, void *data)
2804 struct display *d = data;
2806 if (strcmp(interface, "wl_compositor") == 0) {
2808 wl_display_bind(display, id, &wl_compositor_interface);
2809 } else if (strcmp(interface, "wl_output") == 0) {
2810 display_add_output(d, id);
2811 } else if (strcmp(interface, "wl_input_device") == 0) {
2812 display_add_input(d, id);
2813 } else if (strcmp(interface, "wl_shell") == 0) {
2814 d->shell = wl_display_bind(display, id, &wl_shell_interface);
2815 } else if (strcmp(interface, "wl_shm") == 0) {
2816 d->shm = wl_display_bind(display, id, &wl_shm_interface);
2817 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2818 d->data_device_manager =
2819 wl_display_bind(display, id,
2820 &wl_data_device_manager_interface);
2825 display_render_frame(struct display *d)
2828 cairo_pattern_t *pattern;
2830 d->frame_radius = 3;
2831 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2832 cr = cairo_create(d->shadow);
2833 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2834 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2835 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
2838 blur_surface(d->shadow, 64);
2841 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2842 cr = cairo_create(d->active_frame);
2843 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2845 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
2846 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
2847 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
2848 cairo_set_source(cr, pattern);
2849 cairo_pattern_destroy(pattern);
2851 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
2856 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2857 cr = cairo_create(d->inactive_frame);
2858 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2859 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
2860 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
2866 init_xkb(struct display *d)
2868 d->xkb.names.rules = "evdev";
2869 d->xkb.names.model = "pc105";
2870 d->xkb.names.layout = (char *) option_xkb_layout;
2871 d->xkb.names.variant = (char *) option_xkb_variant;
2872 d->xkb.names.options = (char *) option_xkb_options;
2874 d->xkb.context = xkb_context_new();
2875 if (!d->xkb.context) {
2876 fprintf(stderr, "Failed to create XKB context\n");
2881 xkb_map_new_from_names(d->xkb.context, &d->xkb.names);
2882 if (!d->xkb.keymap) {
2883 fprintf(stderr, "Failed to compile keymap\n");
2887 d->xkb.state = xkb_state_new(d->xkb.keymap);
2888 if (!d->xkb.state) {
2889 fprintf(stderr, "Failed to create XKB state\n");
2893 d->xkb.control_mask =
2894 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
2896 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
2898 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
2903 fini_xkb(struct display *display)
2905 xkb_state_unref(display->xkb.state);
2906 xkb_map_unref(display->xkb.keymap);
2907 xkb_context_unref(display->xkb.context);
2910 #ifdef HAVE_CAIRO_EGL
2912 init_egl(struct display *d)
2914 EGLint major, minor;
2917 #ifdef USE_CAIRO_GLESV2
2918 # define GL_BIT EGL_OPENGL_ES2_BIT
2920 # define GL_BIT EGL_OPENGL_BIT
2923 static const EGLint argb_cfg_attribs[] = {
2924 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2930 EGL_RENDERABLE_TYPE, GL_BIT,
2934 #ifdef USE_CAIRO_GLESV2
2935 static const EGLint context_attribs[] = {
2936 EGL_CONTEXT_CLIENT_VERSION, 2,
2939 EGLint api = EGL_OPENGL_ES_API;
2941 EGLint *context_attribs = NULL;
2942 EGLint api = EGL_OPENGL_API;
2945 d->dpy = eglGetDisplay(d->display);
2946 if (!eglInitialize(d->dpy, &major, &minor)) {
2947 fprintf(stderr, "failed to initialize display\n");
2951 if (!eglBindAPI(api)) {
2952 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2956 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
2957 &d->argb_config, 1, &n) || n != 1) {
2958 fprintf(stderr, "failed to choose argb config\n");
2962 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
2963 EGL_NO_CONTEXT, context_attribs);
2964 if (d->argb_ctx == NULL) {
2965 fprintf(stderr, "failed to create context\n");
2969 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
2970 fprintf(stderr, "failed to make context current\n");
2974 #ifdef HAVE_CAIRO_EGL
2975 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2976 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2977 fprintf(stderr, "failed to get cairo egl argb device\n");
2986 fini_egl(struct display *display)
2988 #ifdef HAVE_CAIRO_EGL
2989 cairo_device_destroy(display->argb_device);
2992 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2995 eglTerminate(display->dpy);
3001 event_mask_update(uint32_t mask, void *data)
3003 struct display *d = data;
3011 handle_display_data(struct task *task, uint32_t events)
3013 struct display *display =
3014 container_of(task, struct display, display_task);
3016 wl_display_iterate(display->display, display->mask);
3020 display_create(int argc, char *argv[])
3024 argc = parse_options(xkb_options,
3025 ARRAY_LENGTH(xkb_options), argc, argv);
3027 d = malloc(sizeof *d);
3031 memset(d, 0, sizeof *d);
3033 d->display = wl_display_connect(NULL);
3034 if (d->display == NULL) {
3035 fprintf(stderr, "failed to create display: %m\n");
3039 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3040 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3041 d->display_task.run = handle_display_data;
3042 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3044 wl_list_init(&d->deferred_list);
3045 wl_list_init(&d->input_list);
3046 wl_list_init(&d->output_list);
3048 /* Set up listener so we'll catch all events. */
3049 wl_display_add_global_listener(d->display,
3050 display_handle_global, d);
3052 /* Process connection events. */
3053 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3054 #ifdef HAVE_CAIRO_EGL
3055 if (init_egl(d) < 0)
3059 d->image_target_texture_2d =
3060 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3061 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3062 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3066 display_render_frame(d);
3068 wl_list_init(&d->window_list);
3076 display_destroy_outputs(struct display *display)
3079 struct output *output;
3081 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3082 output_destroy(output);
3086 display_destroy_inputs(struct display *display)
3089 struct input *input;
3091 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3092 input_destroy(input);
3096 display_destroy(struct display *display)
3098 if (!wl_list_empty(&display->window_list))
3099 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3101 if (!wl_list_empty(&display->deferred_list))
3102 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3104 display_destroy_outputs(display);
3105 display_destroy_inputs(display);
3109 cairo_surface_destroy(display->active_frame);
3110 cairo_surface_destroy(display->inactive_frame);
3111 cairo_surface_destroy(display->shadow);
3112 destroy_cursors(display);
3114 #ifdef HAVE_CAIRO_EGL
3119 wl_shell_destroy(display->shell);
3122 wl_shm_destroy(display->shm);
3124 if (display->data_device_manager)
3125 wl_data_device_manager_destroy(display->data_device_manager);
3127 wl_compositor_destroy(display->compositor);
3129 close(display->epoll_fd);
3131 wl_display_flush(display->display);
3132 wl_display_disconnect(display->display);
3137 display_set_user_data(struct display *display, void *data)
3139 display->user_data = data;
3143 display_get_user_data(struct display *display)
3145 return display->user_data;
3149 display_get_display(struct display *display)
3151 return display->display;
3155 display_get_output(struct display *display)
3157 return container_of(display->output_list.next, struct output, link);
3160 struct wl_compositor *
3161 display_get_compositor(struct display *display)
3163 return display->compositor;
3167 display_get_serial(struct display *display)
3169 return display->serial;
3173 display_get_egl_display(struct display *d)
3178 struct wl_data_source *
3179 display_create_data_source(struct display *display)
3181 return wl_data_device_manager_create_data_source(display->data_device_manager);
3185 display_get_argb_egl_config(struct display *d)
3187 return d->argb_config;
3191 display_get_shell(struct display *display)
3193 return display->shell;
3197 display_acquire_window_surface(struct display *display,
3198 struct window *window,
3201 #ifdef HAVE_CAIRO_EGL
3202 struct egl_window_surface_data *data;
3203 cairo_device_t *device;
3205 if (!window->cairo_surface)
3207 device = cairo_surface_get_device(window->cairo_surface);
3212 if (device == display->argb_device)
3213 ctx = display->argb_ctx;
3218 data = cairo_surface_get_user_data(window->cairo_surface,
3221 cairo_device_flush(device);
3222 cairo_device_acquire(device);
3223 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3224 fprintf(stderr, "failed to make surface current\n");
3233 display_release_window_surface(struct display *display,
3234 struct window *window)
3236 #ifdef HAVE_CAIRO_EGL
3237 cairo_device_t *device;
3239 device = cairo_surface_get_device(window->cairo_surface);
3243 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3244 fprintf(stderr, "failed to make context current\n");
3245 cairo_device_release(device);
3250 display_defer(struct display *display, struct task *task)
3252 wl_list_insert(&display->deferred_list, &task->link);
3256 display_watch_fd(struct display *display,
3257 int fd, uint32_t events, struct task *task)
3259 struct epoll_event ep;
3263 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3267 display_run(struct display *display)
3270 struct epoll_event ep[16];
3273 display->running = 1;
3275 wl_display_flush(display->display);
3277 while (!wl_list_empty(&display->deferred_list)) {
3278 task = container_of(display->deferred_list.next,
3280 wl_list_remove(&task->link);
3284 if (!display->running)
3287 wl_display_flush(display->display);
3289 count = epoll_wait(display->epoll_fd,
3290 ep, ARRAY_LENGTH(ep), -1);
3291 for (i = 0; i < count; i++) {
3292 task = ep[i].data.ptr;
3293 task->run(task, ep[i].events);
3299 display_exit(struct display *display)
3301 display->running = 0;