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"
38 #include <sys/epoll.h>
39 #include <sys/timerfd.h>
43 #include <wayland-egl.h>
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
52 #include <EGL/eglext.h>
58 #include <xkbcommon/xkbcommon.h>
59 #include <wayland-cursor.h>
61 #include <linux/input.h>
62 #include <wayland-client.h>
63 #include "../shared/cairo-util.h"
70 struct wl_display *display;
71 struct wl_compositor *compositor;
72 struct wl_shell *shell;
74 struct wl_data_device_manager *data_device_manager;
76 EGLConfig argb_config;
78 cairo_device_t *argb_device;
83 struct task display_task;
86 struct wl_list deferred_list;
90 struct wl_list window_list;
91 struct wl_list input_list;
92 struct wl_list output_list;
97 struct xkb_rule_names names;
98 struct xkb_keymap *keymap;
99 struct xkb_state *state;
100 struct xkb_context *context;
101 xkb_mod_mask_t control_mask;
102 xkb_mod_mask_t alt_mask;
103 xkb_mod_mask_t shift_mask;
106 struct wl_cursor_theme *cursor_theme;
108 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
109 PFNEGLCREATEIMAGEKHRPROC create_image;
110 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
112 display_output_handler_t output_configure_handler;
127 struct window_output {
128 struct output *output;
133 struct display *display;
134 struct window *parent;
135 struct wl_list window_output_list;
136 struct wl_surface *surface;
137 struct wl_shell_surface *shell_surface;
138 struct wl_region *input_region;
139 struct wl_region *opaque_region;
141 struct rectangle allocation, saved_allocation, server_allocation;
142 struct rectangle pending_allocation;
145 int redraw_scheduled;
147 struct task redraw_task;
151 struct input *keyboard_device;
152 enum window_buffer_type buffer_type;
154 cairo_surface_t *cairo_surface;
156 struct shm_pool *pool;
158 window_key_handler_t key_handler;
159 window_keyboard_focus_handler_t keyboard_focus_handler;
160 window_data_handler_t data_handler;
161 window_drop_handler_t drop_handler;
162 window_close_handler_t close_handler;
165 struct widget *widget;
172 struct window *window;
173 struct tooltip *tooltip;
174 struct wl_list child_list;
176 struct rectangle allocation;
177 widget_resize_handler_t resize_handler;
178 widget_redraw_handler_t redraw_handler;
179 widget_enter_handler_t enter_handler;
180 widget_leave_handler_t leave_handler;
181 widget_motion_handler_t motion_handler;
182 widget_button_handler_t button_handler;
189 struct display *display;
190 struct wl_seat *seat;
191 struct wl_pointer *pointer;
192 struct wl_keyboard *keyboard;
193 struct window *pointer_focus;
194 struct window *keyboard_focus;
197 uint32_t pointer_enter_serial;
201 struct widget *focus_widget;
203 uint32_t grab_button;
205 struct wl_data_device *data_device;
206 struct data_offer *drag_offer;
207 struct data_offer *selection_offer;
211 struct display *display;
212 struct wl_output *output;
213 struct rectangle allocation;
216 display_output_handler_t destroy_handler;
220 enum frame_button_action {
221 FRAME_BUTTON_NULL = 0,
222 FRAME_BUTTON_ICON = 1,
223 FRAME_BUTTON_CLOSE = 2,
224 FRAME_BUTTON_MINIMIZE = 3,
225 FRAME_BUTTON_MAXIMIZE = 4,
228 enum frame_button_pointer {
229 FRAME_BUTTON_DEFAULT = 0,
230 FRAME_BUTTON_OVER = 1,
231 FRAME_BUTTON_ACTIVE = 2,
234 enum frame_button_align {
235 FRAME_BUTTON_RIGHT = 0,
236 FRAME_BUTTON_LEFT = 1,
239 enum frame_button_decoration {
240 FRAME_BUTTON_NONE = 0,
241 FRAME_BUTTON_FANCY = 1,
244 struct frame_button {
245 struct widget *widget;
247 cairo_surface_t *icon;
248 enum frame_button_action type;
249 enum frame_button_pointer state;
250 struct wl_list link; /* buttons_list */
251 enum frame_button_align align;
252 enum frame_button_decoration decoration;
256 struct widget *widget;
257 struct widget *child;
258 struct wl_list buttons_list;
262 struct window *window;
263 struct widget *widget;
265 const char **entries;
273 struct widget *parent;
274 struct window *window;
275 struct widget *widget;
277 struct task tooltip_task;
283 struct wl_shm_pool *pool;
290 WL_CURSOR_DEFAULT = 100,
294 enum window_location {
296 WINDOW_RESIZING_TOP = 1,
297 WINDOW_RESIZING_BOTTOM = 2,
298 WINDOW_RESIZING_LEFT = 4,
299 WINDOW_RESIZING_TOP_LEFT = 5,
300 WINDOW_RESIZING_BOTTOM_LEFT = 6,
301 WINDOW_RESIZING_RIGHT = 8,
302 WINDOW_RESIZING_TOP_RIGHT = 9,
303 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
304 WINDOW_RESIZING_MASK = 15,
305 WINDOW_EXTERIOR = 16,
306 WINDOW_TITLEBAR = 17,
307 WINDOW_CLIENT_AREA = 18,
310 const char *option_xkb_layout = "us";
311 const char *option_xkb_variant = "";
312 const char *option_xkb_options = "";
314 static const struct weston_option xkb_options[] = {
315 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
316 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
317 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
320 static const cairo_user_data_key_t surface_data_key;
321 struct surface_data {
322 struct wl_buffer *buffer;
325 #define MULT(_d,c,a,t) \
326 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
328 #ifdef HAVE_CAIRO_EGL
330 struct egl_window_surface_data {
331 struct display *display;
332 struct wl_surface *surface;
333 struct wl_egl_window *window;
338 egl_window_surface_data_destroy(void *p)
340 struct egl_window_surface_data *data = p;
341 struct display *d = data->display;
343 eglDestroySurface(d->dpy, data->surf);
344 wl_egl_window_destroy(data->window);
345 data->surface = NULL;
350 static cairo_surface_t *
351 display_create_egl_window_surface(struct display *display,
352 struct wl_surface *surface,
354 struct rectangle *rectangle)
356 cairo_surface_t *cairo_surface;
357 struct egl_window_surface_data *data;
359 cairo_device_t *device;
361 data = malloc(sizeof *data);
365 data->display = display;
366 data->surface = surface;
368 config = display->argb_config;
369 device = display->argb_device;
371 data->window = wl_egl_window_create(surface,
375 data->surf = eglCreateWindowSurface(display->dpy, config,
378 cairo_surface = cairo_gl_surface_create_for_egl(device,
383 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
384 data, egl_window_surface_data_destroy);
386 return cairo_surface;
392 display_get_buffer_for_surface(struct display *display,
393 cairo_surface_t *surface)
395 struct surface_data *data;
397 data = cairo_surface_get_user_data (surface, &surface_data_key);
402 struct shm_surface_data {
403 struct surface_data data;
404 struct shm_pool *pool;
408 shm_pool_destroy(struct shm_pool *pool);
411 shm_surface_data_destroy(void *p)
413 struct shm_surface_data *data = p;
415 wl_buffer_destroy(data->data.buffer);
417 shm_pool_destroy(data->pool);
420 static struct wl_shm_pool *
421 make_shm_pool(struct display *display, int size, void **data)
423 char filename[] = "/tmp/wayland-shm-XXXXXX";
424 struct wl_shm_pool *pool;
427 fd = mkstemp(filename);
429 fprintf(stderr, "open %s failed: %m\n", filename);
432 if (ftruncate(fd, size) < 0) {
433 fprintf(stderr, "ftruncate failed: %m\n");
438 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
441 if (*data == MAP_FAILED) {
442 fprintf(stderr, "mmap failed: %m\n");
447 pool = wl_shm_create_pool(display->shm, fd, size);
454 static struct shm_pool *
455 shm_pool_create(struct display *display, size_t size)
457 struct shm_pool *pool = malloc(sizeof *pool);
462 pool->pool = make_shm_pool(display, size, &pool->data);
475 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
477 if (pool->used + size > pool->size)
480 *offset = pool->used;
483 return (char *) pool->data + *offset;
486 /* destroy the pool. this does not unmap the memory though */
488 shm_pool_destroy(struct shm_pool *pool)
490 munmap(pool->data, pool->size);
491 wl_shm_pool_destroy(pool->pool);
495 /* Start allocating from the beginning of the pool again */
497 shm_pool_reset(struct shm_pool *pool)
503 data_length_for_shm_surface(struct rectangle *rect)
507 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
509 return stride * rect->height;
512 static cairo_surface_t *
513 display_create_shm_surface_from_pool(struct display *display,
514 struct rectangle *rectangle,
515 uint32_t flags, struct shm_pool *pool)
517 struct shm_surface_data *data;
519 cairo_surface_t *surface;
520 int stride, length, offset;
523 data = malloc(sizeof *data);
527 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
529 length = stride * rectangle->height;
531 map = shm_pool_allocate(pool, length, &offset);
538 surface = cairo_image_surface_create_for_data (map,
544 cairo_surface_set_user_data (surface, &surface_data_key,
545 data, shm_surface_data_destroy);
547 if (flags & SURFACE_OPAQUE)
548 format = WL_SHM_FORMAT_XRGB8888;
550 format = WL_SHM_FORMAT_ARGB8888;
552 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
560 static cairo_surface_t *
561 display_create_shm_surface(struct display *display,
562 struct rectangle *rectangle, uint32_t flags,
563 struct window *window)
565 struct shm_surface_data *data;
566 struct shm_pool *pool;
567 cairo_surface_t *surface;
569 if (window && window->pool) {
570 shm_pool_reset(window->pool);
571 surface = display_create_shm_surface_from_pool(display,
579 pool = shm_pool_create(display,
580 data_length_for_shm_surface(rectangle));
585 display_create_shm_surface_from_pool(display, rectangle,
589 shm_pool_destroy(pool);
593 /* make sure we destroy the pool when the surface is destroyed */
594 data = cairo_surface_get_user_data(surface, &surface_data_key);
601 check_size(struct rectangle *rect)
603 if (rect->width && rect->height)
606 fprintf(stderr, "tried to create surface of "
607 "width: %d, height: %d\n", rect->width, rect->height);
612 display_create_surface(struct display *display,
613 struct wl_surface *surface,
614 struct rectangle *rectangle,
617 if (check_size(rectangle) < 0)
619 #ifdef HAVE_CAIRO_EGL
621 return display_create_egl_window_surface(display,
626 return display_create_shm_surface(display, rectangle, flags, NULL);
629 struct wl_cursor_image *
630 display_get_pointer_image(struct display *display, int pointer)
632 struct wl_cursor *cursor =
633 wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
635 return cursor ? cursor->images[0] : NULL;
639 window_get_resize_dx_dy(struct window *window, int *x, int *y)
641 if (window->resize_edges & WINDOW_RESIZING_LEFT)
642 *x = window->server_allocation.width - window->allocation.width;
646 if (window->resize_edges & WINDOW_RESIZING_TOP)
647 *y = window->server_allocation.height -
648 window->allocation.height;
652 window->resize_edges = 0;
656 window_attach_surface(struct window *window)
658 struct display *display = window->display;
659 struct wl_buffer *buffer;
660 #ifdef HAVE_CAIRO_EGL
661 struct egl_window_surface_data *data;
665 if (window->type == TYPE_NONE) {
666 window->type = TYPE_TOPLEVEL;
668 wl_shell_surface_set_toplevel(window->shell_surface);
671 switch (window->buffer_type) {
672 #ifdef HAVE_CAIRO_EGL
673 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
674 data = cairo_surface_get_user_data(window->cairo_surface,
677 cairo_gl_surface_swapbuffers(window->cairo_surface);
678 wl_egl_window_get_attached_size(data->window,
679 &window->server_allocation.width,
680 &window->server_allocation.height);
683 case WINDOW_BUFFER_TYPE_SHM:
685 display_get_buffer_for_surface(display,
686 window->cairo_surface);
688 window_get_resize_dx_dy(window, &x, &y);
689 wl_surface_attach(window->surface, buffer, x, y);
690 wl_surface_damage(window->surface, 0, 0,
691 window->allocation.width,
692 window->allocation.height);
693 window->server_allocation = window->allocation;
694 cairo_surface_destroy(window->cairo_surface);
695 window->cairo_surface = NULL;
701 if (window->input_region) {
702 wl_surface_set_input_region(window->surface,
703 window->input_region);
704 wl_region_destroy(window->input_region);
705 window->input_region = NULL;
708 if (window->opaque_region) {
709 wl_surface_set_opaque_region(window->surface,
710 window->opaque_region);
711 wl_region_destroy(window->opaque_region);
712 window->opaque_region = NULL;
717 window_flush(struct window *window)
719 if (window->cairo_surface)
720 window_attach_surface(window);
724 window_set_surface(struct window *window, cairo_surface_t *surface)
726 cairo_surface_reference(surface);
728 if (window->cairo_surface != NULL)
729 cairo_surface_destroy(window->cairo_surface);
731 window->cairo_surface = surface;
734 #ifdef HAVE_CAIRO_EGL
736 window_resize_cairo_window_surface(struct window *window)
738 struct egl_window_surface_data *data;
741 data = cairo_surface_get_user_data(window->cairo_surface,
744 window_get_resize_dx_dy(window, &x, &y),
745 wl_egl_window_resize(data->window,
746 window->allocation.width,
747 window->allocation.height,
750 cairo_gl_surface_set_size(window->cairo_surface,
751 window->allocation.width,
752 window->allocation.height);
757 window_get_display(struct window *window)
759 return window->display;
763 window_create_surface(struct window *window)
765 cairo_surface_t *surface;
768 if (!window->transparent)
769 flags = SURFACE_OPAQUE;
771 switch (window->buffer_type) {
772 #ifdef HAVE_CAIRO_EGL
773 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
774 if (window->cairo_surface) {
775 window_resize_cairo_window_surface(window);
778 surface = display_create_surface(window->display,
780 &window->allocation, flags);
783 case WINDOW_BUFFER_TYPE_SHM:
784 surface = display_create_shm_surface(window->display,
793 window_set_surface(window, surface);
794 cairo_surface_destroy(surface);
797 static void frame_destroy(struct frame *frame);
800 window_destroy(struct window *window)
802 struct display *display = window->display;
804 struct window_output *window_output;
805 struct window_output *window_output_tmp;
807 if (window->redraw_scheduled)
808 wl_list_remove(&window->redraw_task.link);
810 wl_list_for_each(input, &display->input_list, link) {
811 if (input->pointer_focus == window)
812 input->pointer_focus = NULL;
813 if (input->keyboard_focus == window)
814 input->keyboard_focus = NULL;
815 if (input->focus_widget &&
816 input->focus_widget->window == window)
817 input->focus_widget = NULL;
820 wl_list_for_each_safe(window_output, window_output_tmp,
821 &window->window_output_list, link) {
822 free (window_output);
825 if (window->input_region)
826 wl_region_destroy(window->input_region);
827 if (window->opaque_region)
828 wl_region_destroy(window->opaque_region);
831 frame_destroy(window->frame);
833 if (window->shell_surface)
834 wl_shell_surface_destroy(window->shell_surface);
835 wl_surface_destroy(window->surface);
836 wl_list_remove(&window->link);
838 if (window->cairo_surface != NULL)
839 cairo_surface_destroy(window->cairo_surface);
845 static struct widget *
846 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
848 struct widget *child, *target;
850 wl_list_for_each(child, &widget->child_list, link) {
851 target = widget_find_widget(child, x, y);
856 if (widget->allocation.x <= x &&
857 x < widget->allocation.x + widget->allocation.width &&
858 widget->allocation.y <= y &&
859 y < widget->allocation.y + widget->allocation.height) {
866 static struct widget *
867 widget_create(struct window *window, void *data)
869 struct widget *widget;
871 widget = malloc(sizeof *widget);
872 memset(widget, 0, sizeof *widget);
873 widget->window = window;
874 widget->user_data = data;
875 widget->allocation = window->allocation;
876 wl_list_init(&widget->child_list);
878 widget->tooltip = NULL;
879 widget->tooltip_count = 0;
885 window_add_widget(struct window *window, void *data)
887 window->widget = widget_create(window, data);
888 wl_list_init(&window->widget->link);
890 return window->widget;
894 widget_add_widget(struct widget *parent, void *data)
896 struct widget *widget;
898 widget = widget_create(parent->window, data);
899 wl_list_insert(parent->child_list.prev, &widget->link);
905 widget_destroy(struct widget *widget)
907 struct display *display = widget->window->display;
910 if (widget->tooltip) {
911 free(widget->tooltip);
912 widget->tooltip = NULL;
915 wl_list_for_each(input, &display->input_list, link) {
916 if (input->focus_widget == widget)
917 input->focus_widget = NULL;
920 wl_list_remove(&widget->link);
925 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
927 *allocation = widget->allocation;
931 widget_set_size(struct widget *widget, int32_t width, int32_t height)
933 widget->allocation.width = width;
934 widget->allocation.height = height;
938 widget_set_allocation(struct widget *widget,
939 int32_t x, int32_t y, int32_t width, int32_t height)
941 widget->allocation.x = x;
942 widget->allocation.y = y;
943 widget_set_size(widget, width, height);
947 widget_set_transparent(struct widget *widget, int transparent)
949 widget->opaque = !transparent;
953 widget_get_user_data(struct widget *widget)
955 return widget->user_data;
959 widget_set_resize_handler(struct widget *widget,
960 widget_resize_handler_t handler)
962 widget->resize_handler = handler;
966 widget_set_redraw_handler(struct widget *widget,
967 widget_redraw_handler_t handler)
969 widget->redraw_handler = handler;
973 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
975 widget->enter_handler = handler;
979 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
981 widget->leave_handler = handler;
985 widget_set_motion_handler(struct widget *widget,
986 widget_motion_handler_t handler)
988 widget->motion_handler = handler;
992 widget_set_button_handler(struct widget *widget,
993 widget_button_handler_t handler)
995 widget->button_handler = handler;
999 widget_schedule_redraw(struct widget *widget)
1001 window_schedule_redraw(widget->window);
1005 window_get_surface(struct window *window)
1007 return cairo_surface_reference(window->cairo_surface);
1011 window_get_wl_surface(struct window *window)
1013 return window->surface;
1016 struct wl_shell_surface *
1017 window_get_wl_shell_surface(struct window *window)
1019 return window->shell_surface;
1023 tooltip_redraw_handler(struct widget *widget, void *data)
1026 const int32_t r = 3;
1027 struct tooltip *tooltip = data;
1028 int32_t width, height;
1029 struct window *window = widget->window;
1031 cr = cairo_create(window->cairo_surface);
1032 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1033 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1036 width = window->allocation.width;
1037 height = window->allocation.height;
1038 rounded_rect(cr, 0, 0, width, height, r);
1040 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1041 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1044 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1045 cairo_move_to(cr, 10, 16);
1046 cairo_show_text(cr, tooltip->entry);
1050 static cairo_text_extents_t
1051 get_text_extents(struct tooltip *tooltip)
1053 struct window *window;
1055 cairo_text_extents_t extents;
1057 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1059 window = tooltip->widget->window->parent;
1060 cr = cairo_create(window->cairo_surface);
1061 cairo_text_extents(cr, tooltip->entry, &extents);
1068 window_create_tooltip(struct tooltip *tooltip)
1070 struct widget *parent = tooltip->parent;
1071 struct display *display = parent->window->display;
1072 struct window *window;
1073 const int offset_y = 27;
1074 const int margin = 3;
1075 cairo_text_extents_t extents;
1077 if (tooltip->widget)
1080 window = window_create_transient(display, parent->window, tooltip->x,
1081 tooltip->y + offset_y,
1082 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1086 tooltip->window = window;
1087 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1089 extents = get_text_extents(tooltip);
1090 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1091 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1097 widget_destroy_tooltip(struct widget *parent)
1099 struct tooltip *tooltip = parent->tooltip;
1101 parent->tooltip_count = 0;
1105 if (tooltip->widget) {
1106 widget_destroy(tooltip->widget);
1107 window_destroy(tooltip->window);
1108 tooltip->widget = NULL;
1109 tooltip->window = NULL;
1112 close(tooltip->tooltip_fd);
1113 free(tooltip->entry);
1115 parent->tooltip = NULL;
1119 tooltip_func(struct task *task, uint32_t events)
1121 struct tooltip *tooltip =
1122 container_of(task, struct tooltip, tooltip_task);
1125 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1126 window_create_tooltip(tooltip);
1129 #define TOOLTIP_TIMEOUT 500
1131 tooltip_timer_reset(struct tooltip *tooltip)
1133 struct itimerspec its;
1135 its.it_interval.tv_sec = 0;
1136 its.it_interval.tv_nsec = 0;
1137 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1138 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1139 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1140 fprintf(stderr, "could not set timerfd\n: %m");
1148 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1150 struct tooltip *tooltip = parent->tooltip;
1152 parent->tooltip_count++;
1156 tooltip_timer_reset(tooltip);
1160 /* the handler might be triggered too fast via input device motion, so
1161 * we need this check here to make sure tooltip is fully initialized */
1162 if (parent->tooltip_count > 1)
1165 tooltip = malloc(sizeof *tooltip);
1169 parent->tooltip = tooltip;
1170 tooltip->parent = parent;
1171 tooltip->widget = NULL;
1172 tooltip->window = NULL;
1175 tooltip->entry = strdup(entry);
1176 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1177 if (tooltip->tooltip_fd < 0) {
1178 fprintf(stderr, "could not create timerfd\n: %m");
1182 tooltip->tooltip_task.run = tooltip_func;
1183 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1184 EPOLLIN, &tooltip->tooltip_task);
1185 tooltip_timer_reset(tooltip);
1191 frame_resize_handler(struct widget *widget,
1192 int32_t width, int32_t height, void *data)
1194 struct frame *frame = data;
1195 struct widget *child = frame->child;
1196 struct rectangle allocation;
1197 struct display *display = widget->window->display;
1198 struct frame_button * button;
1199 struct theme *t = display->theme;
1200 int x_l, x_r, y, w, h;
1201 int decoration_width, decoration_height;
1204 if (widget->window->type != TYPE_FULLSCREEN) {
1205 decoration_width = (t->width + t->margin) * 2;
1206 decoration_height = t->width +
1207 t->titlebar_height + t->margin * 2;
1209 allocation.x = t->width + t->margin;
1210 allocation.y = t->titlebar_height + t->margin;
1211 allocation.width = width - decoration_width;
1212 allocation.height = height - decoration_height;
1214 opaque_margin = t->margin + t->frame_radius;
1216 wl_list_for_each(button, &frame->buttons_list, link)
1217 button->widget->opaque = 0;
1219 decoration_width = 0;
1220 decoration_height = 0;
1224 allocation.width = width;
1225 allocation.height = height;
1228 wl_list_for_each(button, &frame->buttons_list, link)
1229 button->widget->opaque = 1;
1232 widget_set_allocation(child, allocation.x, allocation.y,
1233 allocation.width, allocation.height);
1235 if (child->resize_handler)
1236 child->resize_handler(child,
1241 width = child->allocation.width + decoration_width;
1242 height = child->allocation.height + decoration_height;
1244 widget->window->input_region =
1245 wl_compositor_create_region(display->compositor);
1246 wl_region_add(widget->window->input_region,
1247 t->margin, t->margin,
1248 width - 2 * t->margin,
1249 height - 2 * t->margin);
1251 widget_set_allocation(widget, 0, 0, width, height);
1253 if (child->opaque) {
1254 widget->window->opaque_region =
1255 wl_compositor_create_region(display->compositor);
1256 wl_region_add(widget->window->opaque_region,
1257 opaque_margin, opaque_margin,
1258 widget->allocation.width - 2 * opaque_margin,
1259 widget->allocation.height - 2 * opaque_margin);
1262 /* frame internal buttons */
1263 x_r = frame->widget->allocation.width - t->width - t->margin;
1264 x_l = t->width + t->margin;
1265 y = t->width + t->margin;
1266 wl_list_for_each(button, &frame->buttons_list, link) {
1267 const int button_padding = 4;
1268 w = cairo_image_surface_get_width(button->icon);
1269 h = cairo_image_surface_get_height(button->icon);
1271 if (button->decoration == FRAME_BUTTON_FANCY)
1274 if (button->align == FRAME_BUTTON_LEFT) {
1275 widget_set_allocation(button->widget,
1276 x_l, y , w + 1, h + 1);
1278 x_l += button_padding;
1281 widget_set_allocation(button->widget,
1282 x_r, y , w + 1, h + 1);
1283 x_r -= button_padding;
1289 frame_button_enter_handler(struct widget *widget,
1290 struct input *input, float x, float y, void *data)
1292 struct frame_button *frame_button = data;
1294 widget_schedule_redraw(frame_button->widget);
1295 frame_button->state = FRAME_BUTTON_OVER;
1297 return WL_CURSOR_LEFT_PTR;
1301 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1303 struct frame_button *frame_button = data;
1305 widget_schedule_redraw(frame_button->widget);
1306 frame_button->state = FRAME_BUTTON_DEFAULT;
1310 frame_button_button_handler(struct widget *widget,
1311 struct input *input, uint32_t time,
1312 uint32_t button, uint32_t state, void *data)
1314 struct frame_button *frame_button = data;
1315 struct window *window = widget->window;
1317 if (button != BTN_LEFT)
1322 frame_button->state = FRAME_BUTTON_ACTIVE;
1323 widget_schedule_redraw(frame_button->widget);
1325 if (frame_button->type == FRAME_BUTTON_ICON)
1326 window_show_frame_menu(window, input, time);
1329 frame_button->state = FRAME_BUTTON_DEFAULT;
1330 widget_schedule_redraw(frame_button->widget);
1334 switch (frame_button->type) {
1335 case FRAME_BUTTON_CLOSE:
1336 if (window->close_handler)
1337 window->close_handler(window->parent,
1340 display_exit(window->display);
1342 case FRAME_BUTTON_MINIMIZE:
1343 fprintf(stderr,"Minimize stub\n");
1345 case FRAME_BUTTON_MAXIMIZE:
1346 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1349 /* Unknown operation */
1355 frame_button_redraw_handler(struct widget *widget, void *data)
1357 struct frame_button *frame_button = data;
1359 int width, height, x, y;
1360 struct window *window = widget->window;
1362 x = widget->allocation.x;
1363 y = widget->allocation.y;
1364 width = widget->allocation.width;
1365 height = widget->allocation.height;
1374 cr = cairo_create(window->cairo_surface);
1376 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1377 cairo_set_line_width(cr, 1);
1379 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1380 cairo_rectangle (cr, x, y, 25, 16);
1382 cairo_stroke_preserve(cr);
1384 switch (frame_button->state) {
1385 case FRAME_BUTTON_DEFAULT:
1386 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1388 case FRAME_BUTTON_OVER:
1389 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1391 case FRAME_BUTTON_ACTIVE:
1392 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1401 cairo_set_source_surface(cr, frame_button->icon, x, y);
1407 static struct widget *
1408 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1409 enum frame_button_align align, enum frame_button_decoration style)
1411 struct frame_button *frame_button;
1412 const char *icon = data;
1414 frame_button = malloc (sizeof *frame_button);
1415 memset(frame_button, 0, sizeof *frame_button);
1417 frame_button->icon = cairo_image_surface_create_from_png(icon);
1418 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1419 frame_button->frame = frame;
1420 frame_button->type = type;
1421 frame_button->align = align;
1422 frame_button->decoration = style;
1424 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1426 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1427 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1428 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1429 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1430 return frame_button->widget;
1434 frame_button_destroy(struct frame_button *frame_button)
1436 widget_destroy(frame_button->widget);
1437 wl_list_remove(&frame_button->link);
1438 cairo_surface_destroy(frame_button->icon);
1445 frame_redraw_handler(struct widget *widget, void *data)
1448 struct window *window = widget->window;
1449 struct theme *t = window->display->theme;
1452 if (window->type == TYPE_FULLSCREEN)
1455 cr = cairo_create(window->cairo_surface);
1457 if (window->keyboard_device)
1458 flags |= THEME_FRAME_ACTIVE;
1459 theme_render_frame(t, cr, widget->allocation.width,
1460 widget->allocation.height, window->title, flags);
1466 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1468 struct theme *t = frame->widget->window->display->theme;
1471 location = theme_get_location(t, input->sx, input->sy,
1472 frame->widget->allocation.width,
1473 frame->widget->allocation.height);
1476 case THEME_LOCATION_RESIZING_TOP:
1477 return WL_CURSOR_TOP;
1478 case THEME_LOCATION_RESIZING_BOTTOM:
1479 return WL_CURSOR_BOTTOM;
1480 case THEME_LOCATION_RESIZING_LEFT:
1481 return WL_CURSOR_LEFT;
1482 case THEME_LOCATION_RESIZING_RIGHT:
1483 return WL_CURSOR_RIGHT;
1484 case THEME_LOCATION_RESIZING_TOP_LEFT:
1485 return WL_CURSOR_TOP_LEFT;
1486 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1487 return WL_CURSOR_TOP_RIGHT;
1488 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1489 return WL_CURSOR_BOTTOM_LEFT;
1490 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1491 return WL_CURSOR_BOTTOM_RIGHT;
1492 case THEME_LOCATION_EXTERIOR:
1493 case THEME_LOCATION_TITLEBAR:
1495 return WL_CURSOR_LEFT_PTR;
1500 frame_menu_func(struct window *window, int index, void *data)
1504 if (window->close_handler)
1505 window->close_handler(window->parent,
1508 display_exit(window->display);
1510 case 1: /* fullscreen */
1511 /* we don't have a way to get out of fullscreen for now */
1512 window_set_fullscreen(window, 1);
1514 case 2: /* rotate */
1521 window_show_frame_menu(struct window *window,
1522 struct input *input, uint32_t time)
1526 static const char *entries[] = {
1527 "Close", "Fullscreen", "Rotate", "Scale"
1530 input_get_position(input, &x, &y);
1531 window_show_menu(window->display, input, time, window,
1532 x - 10, y - 10, frame_menu_func, entries, 4);
1536 frame_enter_handler(struct widget *widget,
1537 struct input *input, float x, float y, void *data)
1539 return frame_get_pointer_image_for_location(data, input);
1543 frame_motion_handler(struct widget *widget,
1544 struct input *input, uint32_t time,
1545 float x, float y, void *data)
1547 return frame_get_pointer_image_for_location(data, input);
1551 frame_button_handler(struct widget *widget,
1552 struct input *input, uint32_t time,
1553 uint32_t button, uint32_t state, void *data)
1556 struct frame *frame = data;
1557 struct window *window = widget->window;
1558 struct display *display = window->display;
1561 location = theme_get_location(display->theme, input->sx, input->sy,
1562 frame->widget->allocation.width,
1563 frame->widget->allocation.height);
1565 if (window->display->shell && button == BTN_LEFT && state == 1) {
1567 case THEME_LOCATION_TITLEBAR:
1568 if (!window->shell_surface)
1570 input_set_pointer_image(input, time, WL_CURSOR_DRAGGING);
1571 input_ungrab(input);
1572 wl_shell_surface_move(window->shell_surface,
1573 input_get_seat(input),
1576 case THEME_LOCATION_RESIZING_TOP:
1577 case THEME_LOCATION_RESIZING_BOTTOM:
1578 case THEME_LOCATION_RESIZING_LEFT:
1579 case THEME_LOCATION_RESIZING_RIGHT:
1580 case THEME_LOCATION_RESIZING_TOP_LEFT:
1581 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1582 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1583 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1584 if (!window->shell_surface)
1586 input_ungrab(input);
1588 if (!display->dpy) {
1589 /* If we're using shm, allocate a big
1590 pool to create buffers out of while
1591 we resize. We should probably base
1592 this number on the size of the output. */
1594 shm_pool_create(display, 6 * 1024 * 1024);
1597 wl_shell_surface_resize(window->shell_surface,
1598 input_get_seat(input),
1599 display->serial, location);
1602 } else if (button == BTN_RIGHT && state == 1) {
1603 window_show_frame_menu(window, input, time);
1608 frame_create(struct window *window, void *data)
1610 struct frame *frame;
1612 frame = malloc(sizeof *frame);
1613 memset(frame, 0, sizeof *frame);
1615 frame->widget = window_add_widget(window, frame);
1616 frame->child = widget_add_widget(frame->widget, data);
1618 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1619 widget_set_resize_handler(frame->widget, frame_resize_handler);
1620 widget_set_enter_handler(frame->widget, frame_enter_handler);
1621 widget_set_motion_handler(frame->widget, frame_motion_handler);
1622 widget_set_button_handler(frame->widget, frame_button_handler);
1624 /* Create empty list for frame buttons */
1625 wl_list_init(&frame->buttons_list);
1627 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1628 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1630 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1631 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1633 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1634 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1636 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1637 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1639 window->frame = frame;
1641 return frame->child;
1645 frame_destroy(struct frame *frame)
1647 struct frame_button *button, *tmp;
1649 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1650 frame_button_destroy(button);
1652 /* frame->child must be destroyed by the application */
1653 widget_destroy(frame->widget);
1658 input_set_focus_widget(struct input *input, struct widget *focus,
1661 struct widget *old, *widget;
1662 int pointer = WL_CURSOR_LEFT_PTR;
1664 if (focus == input->focus_widget)
1667 old = input->focus_widget;
1671 widget = input->grab;
1672 if (widget->leave_handler)
1673 widget->leave_handler(old, input, widget->user_data);
1674 input->focus_widget = NULL;
1680 widget = input->grab;
1681 if (widget->enter_handler)
1682 pointer = widget->enter_handler(focus, input, x, y,
1684 input->focus_widget = focus;
1686 input_set_pointer_image(input, input->pointer_enter_serial,
1692 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1693 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1695 struct input *input = data;
1696 struct window *window = input->pointer_focus;
1697 struct widget *widget;
1698 int cursor = WL_CURSOR_LEFT_PTR;
1699 float sx = wl_fixed_to_double(sx_w);
1700 float sy = wl_fixed_to_double(sy_w);
1705 if (!(input->grab && input->grab_button)) {
1706 widget = widget_find_widget(window->widget, sx, sy);
1707 input_set_focus_widget(input, widget, sx, sy);
1711 widget = input->grab;
1713 widget = input->focus_widget;
1714 if (widget && widget->motion_handler)
1715 cursor = widget->motion_handler(input->focus_widget,
1716 input, time, sx, sy,
1719 input_set_pointer_image(input, time, cursor);
1723 input_grab(struct input *input, struct widget *widget, uint32_t button)
1725 input->grab = widget;
1726 input->grab_button = button;
1730 input_ungrab(struct input *input)
1732 struct widget *widget;
1735 if (input->pointer_focus) {
1736 widget = widget_find_widget(input->pointer_focus->widget,
1737 input->sx, input->sy);
1738 input_set_focus_widget(input, widget, input->sx, input->sy);
1743 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1744 uint32_t time, uint32_t button, uint32_t state)
1746 struct input *input = data;
1747 struct widget *widget;
1749 input->display->serial = serial;
1750 if (input->focus_widget && input->grab == NULL && state)
1751 input_grab(input, input->focus_widget, button);
1753 widget = input->grab;
1754 if (widget && widget->button_handler)
1755 (*widget->button_handler)(widget,
1758 input->grab->user_data);
1760 if (input->grab && input->grab_button == button && !state)
1761 input_ungrab(input);
1765 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1766 uint32_t time, uint32_t axis, int32_t value)
1771 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1772 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1774 struct input *input = data;
1775 struct window *window = input->keyboard_focus;
1776 struct display *d = input->display;
1777 uint32_t code, num_syms;
1778 const xkb_keysym_t *syms;
1780 xkb_mod_mask_t mask;
1782 input->display->serial = serial;
1784 if (!window || window->keyboard_device != input)
1787 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1788 xkb_state_update_key(d->xkb.state, code,
1789 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1791 mask = xkb_state_serialize_mods(d->xkb.state,
1792 XKB_STATE_DEPRESSED |
1794 input->modifiers = 0;
1795 if (mask & input->display->xkb.control_mask)
1796 input->modifiers |= MOD_CONTROL_MASK;
1797 if (mask & input->display->xkb.alt_mask)
1798 input->modifiers |= MOD_ALT_MASK;
1799 if (mask & input->display->xkb.shift_mask)
1800 input->modifiers |= MOD_SHIFT_MASK;
1802 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
1803 input->modifiers == MOD_ALT_MASK) {
1805 window_set_maximized(window,
1806 window->type != TYPE_MAXIMIZED);
1807 } else if (window->key_handler) {
1811 sym = XKB_KEY_NoSymbol;
1813 (*window->key_handler)(window, input, time, key,
1814 sym, state, window->user_data);
1819 input_remove_pointer_focus(struct input *input)
1821 struct window *window = input->pointer_focus;
1826 input_set_focus_widget(input, NULL, 0, 0);
1828 input->pointer_focus = NULL;
1829 input->current_cursor = WL_CURSOR_UNSET;
1833 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1834 uint32_t serial, struct wl_surface *surface,
1835 wl_fixed_t sx_w, wl_fixed_t sy_w)
1837 struct input *input = data;
1838 struct window *window;
1839 struct widget *widget;
1840 float sx = wl_fixed_to_double(sx_w);
1841 float sy = wl_fixed_to_double(sy_w);
1844 /* enter event for a window we've just destroyed */
1848 input->display->serial = serial;
1849 input->pointer_enter_serial = serial;
1850 input->pointer_focus = wl_surface_get_user_data(surface);
1851 window = input->pointer_focus;
1854 shm_pool_destroy(window->pool);
1855 window->pool = NULL;
1856 /* Schedule a redraw to free the pool */
1857 window_schedule_redraw(window);
1863 widget = widget_find_widget(window->widget, sx, sy);
1864 input_set_focus_widget(input, widget, sx, sy);
1868 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1869 uint32_t serial, struct wl_surface *surface)
1871 struct input *input = data;
1873 input->display->serial = serial;
1874 input_remove_pointer_focus(input);
1878 input_remove_keyboard_focus(struct input *input)
1880 struct window *window = input->keyboard_focus;
1885 window->keyboard_device = NULL;
1886 if (window->keyboard_focus_handler)
1887 (*window->keyboard_focus_handler)(window, NULL,
1890 input->keyboard_focus = NULL;
1894 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1895 uint32_t serial, struct wl_surface *surface,
1896 struct wl_array *keys)
1898 struct input *input = data;
1899 struct window *window;
1901 input->display->serial = serial;
1902 input->keyboard_focus = wl_surface_get_user_data(surface);
1904 window = input->keyboard_focus;
1905 window->keyboard_device = input;
1906 if (window->keyboard_focus_handler)
1907 (*window->keyboard_focus_handler)(window,
1908 window->keyboard_device,
1913 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1914 uint32_t serial, struct wl_surface *surface)
1916 struct input *input = data;
1918 input->display->serial = serial;
1919 input_remove_keyboard_focus(input);
1922 static const struct wl_pointer_listener pointer_listener = {
1923 pointer_handle_enter,
1924 pointer_handle_leave,
1925 pointer_handle_motion,
1926 pointer_handle_button,
1927 pointer_handle_axis,
1930 static const struct wl_keyboard_listener keyboard_listener = {
1931 keyboard_handle_enter,
1932 keyboard_handle_leave,
1933 keyboard_handle_key,
1937 seat_handle_capabilities(void *data, struct wl_seat *seat,
1938 enum wl_seat_capability caps)
1940 struct input *input = data;
1942 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1943 input->pointer = wl_seat_get_pointer(seat);
1944 wl_pointer_set_user_data(input->pointer, input);
1945 wl_pointer_add_listener(input->pointer, &pointer_listener,
1947 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1948 wl_pointer_destroy(input->pointer);
1949 input->pointer = NULL;
1952 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1953 input->keyboard = wl_seat_get_keyboard(seat);
1954 wl_keyboard_set_user_data(input->keyboard, input);
1955 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1957 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1958 wl_keyboard_destroy(input->keyboard);
1959 input->keyboard = NULL;
1963 static const struct wl_seat_listener seat_listener = {
1964 seat_handle_capabilities,
1968 input_get_position(struct input *input, int32_t *x, int32_t *y)
1975 input_get_seat(struct input *input)
1981 input_get_modifiers(struct input *input)
1983 return input->modifiers;
1987 input_get_focus_widget(struct input *input)
1989 return input->focus_widget;
1993 struct wl_data_offer *offer;
1994 struct input *input;
1995 struct wl_array types;
1998 struct task io_task;
2006 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2008 struct data_offer *offer = data;
2011 p = wl_array_add(&offer->types, sizeof *p);
2015 static const struct wl_data_offer_listener data_offer_listener = {
2020 data_offer_destroy(struct data_offer *offer)
2025 if (offer->refcount == 0) {
2026 wl_data_offer_destroy(offer->offer);
2027 for (p = offer->types.data; *p; p++)
2029 wl_array_release(&offer->types);
2035 data_device_data_offer(void *data,
2036 struct wl_data_device *data_device, uint32_t id)
2038 struct data_offer *offer;
2040 offer = malloc(sizeof *offer);
2042 wl_array_init(&offer->types);
2043 offer->refcount = 1;
2044 offer->input = data;
2046 /* FIXME: Generate typesafe wrappers for this */
2047 offer->offer = (struct wl_data_offer *)
2048 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2049 id, &wl_data_offer_interface);
2051 wl_data_offer_add_listener(offer->offer,
2052 &data_offer_listener, offer);
2056 data_device_enter(void *data, struct wl_data_device *data_device,
2057 uint32_t serial, struct wl_surface *surface,
2058 wl_fixed_t x_w, wl_fixed_t y_w,
2059 struct wl_data_offer *offer)
2061 struct input *input = data;
2062 struct window *window;
2063 float x = wl_fixed_to_double(x_w);
2064 float y = wl_fixed_to_double(y_w);
2067 input->pointer_enter_serial = serial;
2068 input->drag_offer = wl_data_offer_get_user_data(offer);
2069 window = wl_surface_get_user_data(surface);
2070 input->pointer_focus = window;
2072 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2075 window = input->pointer_focus;
2076 if (window->data_handler)
2077 window->data_handler(window, input, x, y,
2078 input->drag_offer->types.data,
2083 data_device_leave(void *data, struct wl_data_device *data_device)
2085 struct input *input = data;
2087 data_offer_destroy(input->drag_offer);
2088 input->drag_offer = NULL;
2092 data_device_motion(void *data, struct wl_data_device *data_device,
2093 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2095 struct input *input = data;
2096 struct window *window = input->pointer_focus;
2097 float x = wl_fixed_to_double(x_w);
2098 float y = wl_fixed_to_double(y_w);
2103 if (window->data_handler)
2104 window->data_handler(window, input, x, y,
2105 input->drag_offer->types.data,
2110 data_device_drop(void *data, struct wl_data_device *data_device)
2112 struct input *input = data;
2113 struct window *window = input->pointer_focus;
2115 if (window->drop_handler)
2116 window->drop_handler(window, input,
2117 input->sx, input->sy, window->user_data);
2121 data_device_selection(void *data,
2122 struct wl_data_device *wl_data_device,
2123 struct wl_data_offer *offer)
2125 struct input *input = data;
2128 if (input->selection_offer)
2129 data_offer_destroy(input->selection_offer);
2132 input->selection_offer = wl_data_offer_get_user_data(offer);
2133 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2138 static const struct wl_data_device_listener data_device_listener = {
2139 data_device_data_offer,
2144 data_device_selection
2148 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2150 struct display *display = input->display;
2151 struct wl_buffer *buffer;
2152 struct wl_cursor *cursor;
2153 struct wl_cursor_image *image;
2155 if (pointer == input->current_cursor)
2158 cursor = wl_cursor_theme_get_cursor(display->cursor_theme, pointer);
2162 image = cursor->images[0];
2163 buffer = wl_cursor_image_get_buffer(image);
2167 input->current_cursor = pointer;
2168 wl_pointer_attach(input->pointer, time, buffer,
2169 image->hotspot_x, image->hotspot_y);
2172 struct wl_data_device *
2173 input_get_data_device(struct input *input)
2175 return input->data_device;
2179 input_set_selection(struct input *input,
2180 struct wl_data_source *source, uint32_t time)
2182 wl_data_device_set_selection(input->data_device, source, time);
2186 input_accept(struct input *input, const char *type)
2188 wl_data_offer_accept(input->drag_offer->offer,
2189 input->pointer_enter_serial, type);
2193 offer_io_func(struct task *task, uint32_t events)
2195 struct data_offer *offer =
2196 container_of(task, struct data_offer, io_task);
2200 len = read(offer->fd, buffer, sizeof buffer);
2201 offer->func(buffer, len,
2202 offer->x, offer->y, offer->user_data);
2206 data_offer_destroy(offer);
2211 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2212 data_func_t func, void *user_data)
2216 if (pipe2(p, O_CLOEXEC) == -1)
2219 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2222 offer->io_task.run = offer_io_func;
2226 offer->user_data = user_data;
2228 display_watch_fd(offer->input->display,
2229 offer->fd, EPOLLIN, &offer->io_task);
2233 input_receive_drag_data(struct input *input, const char *mime_type,
2234 data_func_t func, void *data)
2236 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2237 input->drag_offer->x = input->sx;
2238 input->drag_offer->y = input->sy;
2242 input_receive_selection_data(struct input *input, const char *mime_type,
2243 data_func_t func, void *data)
2247 if (input->selection_offer == NULL)
2250 for (p = input->selection_offer->types.data; *p; p++)
2251 if (strcmp(mime_type, *p) == 0)
2257 data_offer_receive_data(input->selection_offer,
2258 mime_type, func, data);
2263 input_receive_selection_data_to_fd(struct input *input,
2264 const char *mime_type, int fd)
2266 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2272 window_move(struct window *window, struct input *input, uint32_t serial)
2274 if (!window->shell_surface)
2277 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2281 idle_resize(struct window *window)
2283 struct widget *widget;
2285 window->resize_needed = 0;
2286 widget = window->widget;
2287 widget_set_allocation(widget,
2288 window->pending_allocation.x,
2289 window->pending_allocation.y,
2290 window->pending_allocation.width,
2291 window->pending_allocation.height);
2293 if (window->input_region) {
2294 wl_region_destroy(window->input_region);
2295 window->input_region = NULL;
2298 if (window->opaque_region) {
2299 wl_region_destroy(window->opaque_region);
2300 window->opaque_region = NULL;
2303 if (widget->resize_handler)
2304 widget->resize_handler(widget,
2305 widget->allocation.width,
2306 widget->allocation.height,
2309 if (window->allocation.width != widget->allocation.width ||
2310 window->allocation.height != widget->allocation.height) {
2311 window->allocation = widget->allocation;
2312 window_schedule_redraw(window);
2317 window_schedule_resize(struct window *window, int width, int height)
2319 window->pending_allocation.x = 0;
2320 window->pending_allocation.y = 0;
2321 window->pending_allocation.width = width;
2322 window->pending_allocation.height = height;
2324 window->resize_needed = 1;
2325 window_schedule_redraw(window);
2329 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2331 window_schedule_resize(widget->window, width, height);
2335 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2338 wl_shell_surface_pong(shell_surface, serial);
2342 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2343 uint32_t edges, int32_t width, int32_t height)
2345 struct window *window = data;
2347 if (width <= 0 || height <= 0)
2350 window->resize_edges = edges;
2351 window_schedule_resize(window, width, height);
2355 menu_destroy(struct menu *menu)
2357 widget_destroy(menu->widget);
2358 window_destroy(menu->window);
2363 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2365 struct window *window = data;
2366 struct menu *menu = window->widget->user_data;
2368 /* FIXME: Need more context in this event, at least the input
2369 * device. Or just use wl_callback. And this really needs to
2370 * be a window vfunc that the menu can set. And we need the
2373 menu->func(window->parent, menu->current, window->parent->user_data);
2374 input_ungrab(menu->input);
2378 static const struct wl_shell_surface_listener shell_surface_listener = {
2385 window_get_allocation(struct window *window,
2386 struct rectangle *allocation)
2388 *allocation = window->allocation;
2392 widget_redraw(struct widget *widget)
2394 struct widget *child;
2396 if (widget->redraw_handler)
2397 widget->redraw_handler(widget, widget->user_data);
2398 wl_list_for_each(child, &widget->child_list, link)
2399 widget_redraw(child);
2403 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2405 struct window *window = data;
2407 wl_callback_destroy(callback);
2408 window->redraw_scheduled = 0;
2409 if (window->redraw_needed)
2410 window_schedule_redraw(window);
2413 static const struct wl_callback_listener listener = {
2418 idle_redraw(struct task *task, uint32_t events)
2420 struct window *window = container_of(task, struct window, redraw_task);
2421 struct wl_callback *callback;
2423 if (window->resize_needed)
2424 idle_resize(window);
2426 window_create_surface(window);
2427 widget_redraw(window->widget);
2428 window_flush(window);
2429 window->redraw_needed = 0;
2430 wl_list_init(&window->redraw_task.link);
2432 callback = wl_surface_frame(window->surface);
2433 wl_callback_add_listener(callback, &listener, window);
2437 window_schedule_redraw(struct window *window)
2439 window->redraw_needed = 1;
2440 if (!window->redraw_scheduled) {
2441 window->redraw_task.run = idle_redraw;
2442 display_defer(window->display, &window->redraw_task);
2443 window->redraw_scheduled = 1;
2448 window_set_custom(struct window *window)
2450 window->type = TYPE_CUSTOM;
2454 window_set_fullscreen(struct window *window, int fullscreen)
2456 if (!window->display->shell)
2459 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2463 window->type = TYPE_FULLSCREEN;
2464 window->saved_allocation = window->allocation;
2465 wl_shell_surface_set_fullscreen(window->shell_surface,
2466 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2469 window->type = TYPE_TOPLEVEL;
2470 wl_shell_surface_set_toplevel(window->shell_surface);
2471 window_schedule_resize(window,
2472 window->saved_allocation.width,
2473 window->saved_allocation.height);
2478 window_set_maximized(struct window *window, int maximized)
2480 if (!window->display->shell)
2483 if ((window->type == TYPE_MAXIMIZED) == maximized)
2486 if (window->type == TYPE_TOPLEVEL) {
2487 window->saved_allocation = window->allocation;
2488 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2489 window->type = TYPE_MAXIMIZED;
2491 wl_shell_surface_set_toplevel(window->shell_surface);
2492 window->type = TYPE_TOPLEVEL;
2493 window_schedule_resize(window,
2494 window->saved_allocation.width,
2495 window->saved_allocation.height);
2500 window_set_user_data(struct window *window, void *data)
2502 window->user_data = data;
2506 window_get_user_data(struct window *window)
2508 return window->user_data;
2512 window_set_key_handler(struct window *window,
2513 window_key_handler_t handler)
2515 window->key_handler = handler;
2519 window_set_keyboard_focus_handler(struct window *window,
2520 window_keyboard_focus_handler_t handler)
2522 window->keyboard_focus_handler = handler;
2526 window_set_data_handler(struct window *window, window_data_handler_t handler)
2528 window->data_handler = handler;
2532 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2534 window->drop_handler = handler;
2538 window_set_close_handler(struct window *window,
2539 window_close_handler_t handler)
2541 window->close_handler = handler;
2545 window_set_title(struct window *window, const char *title)
2547 free(window->title);
2548 window->title = strdup(title);
2549 if (window->shell_surface)
2550 wl_shell_surface_set_title(window->shell_surface, title);
2554 window_get_title(struct window *window)
2556 return window->title;
2560 window_damage(struct window *window, int32_t x, int32_t y,
2561 int32_t width, int32_t height)
2563 wl_surface_damage(window->surface, x, y, width, height);
2567 surface_enter(void *data,
2568 struct wl_surface *wl_surface, struct wl_output *wl_output)
2570 struct window *window = data;
2571 struct output *output;
2572 struct output *output_found = NULL;
2573 struct window_output *window_output;
2575 wl_list_for_each(output, &window->display->output_list, link) {
2576 if (output->output == wl_output) {
2577 output_found = output;
2585 window_output = malloc (sizeof *window_output);
2586 window_output->output = output_found;
2588 wl_list_insert (&window->window_output_list, &window_output->link);
2592 surface_leave(void *data,
2593 struct wl_surface *wl_surface, struct wl_output *output)
2595 struct window *window = data;
2596 struct window_output *window_output;
2597 struct window_output *window_output_found = NULL;
2599 wl_list_for_each(window_output, &window->window_output_list, link) {
2600 if (window_output->output->output == output) {
2601 window_output_found = window_output;
2606 if (window_output_found) {
2607 wl_list_remove(&window_output_found->link);
2608 free(window_output_found);
2612 static const struct wl_surface_listener surface_listener = {
2617 static struct window *
2618 window_create_internal(struct display *display, struct window *parent)
2620 struct window *window;
2622 window = malloc(sizeof *window);
2626 memset(window, 0, sizeof *window);
2627 window->display = display;
2628 window->parent = parent;
2629 window->surface = wl_compositor_create_surface(display->compositor);
2630 wl_surface_add_listener(window->surface, &surface_listener, window);
2631 if (display->shell) {
2632 window->shell_surface =
2633 wl_shell_get_shell_surface(display->shell,
2636 wl_shell_surface_set_title(window->shell_surface,
2639 window->allocation.x = 0;
2640 window->allocation.y = 0;
2641 window->allocation.width = 0;
2642 window->allocation.height = 0;
2643 window->saved_allocation = window->allocation;
2644 window->transparent = 1;
2645 window->type = TYPE_NONE;
2646 window->input_region = NULL;
2647 window->opaque_region = NULL;
2650 #ifdef HAVE_CAIRO_EGL
2651 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2653 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2656 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2658 wl_surface_set_user_data(window->surface, window);
2659 wl_list_insert(display->window_list.prev, &window->link);
2660 wl_list_init(&window->redraw_task.link);
2662 if (window->shell_surface) {
2663 wl_shell_surface_set_user_data(window->shell_surface, window);
2664 wl_shell_surface_add_listener(window->shell_surface,
2665 &shell_surface_listener, window);
2668 wl_list_init (&window->window_output_list);
2674 window_create(struct display *display)
2676 struct window *window;
2678 window = window_create_internal(display, NULL);
2686 window_create_transient(struct display *display, struct window *parent,
2687 int32_t x, int32_t y, uint32_t flags)
2689 struct window *window;
2691 window = window_create_internal(parent->display, parent);
2695 window->type = TYPE_TRANSIENT;
2700 wl_shell_surface_set_transient(window->shell_surface,
2701 window->parent->shell_surface,
2702 window->x, window->y, flags);
2708 menu_set_item(struct menu *menu, int sy)
2712 next = (sy - 8) / 20;
2713 if (menu->current != next) {
2714 menu->current = next;
2715 widget_schedule_redraw(menu->widget);
2720 menu_motion_handler(struct widget *widget,
2721 struct input *input, uint32_t time,
2722 float x, float y, void *data)
2724 struct menu *menu = data;
2726 if (widget == menu->widget)
2727 menu_set_item(data, y);
2729 return WL_CURSOR_LEFT_PTR;
2733 menu_enter_handler(struct widget *widget,
2734 struct input *input, float x, float y, void *data)
2736 struct menu *menu = data;
2738 if (widget == menu->widget)
2739 menu_set_item(data, y);
2741 return WL_CURSOR_LEFT_PTR;
2745 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2747 struct menu *menu = data;
2749 if (widget == menu->widget)
2750 menu_set_item(data, -200);
2754 menu_button_handler(struct widget *widget,
2755 struct input *input, uint32_t time,
2756 uint32_t button, uint32_t state, void *data)
2759 struct menu *menu = data;
2761 if (state == 0 && time - menu->time > 500) {
2762 /* Either relase after press-drag-release or
2763 * click-motion-click. */
2764 menu->func(menu->window->parent,
2765 menu->current, menu->window->parent->user_data);
2766 input_ungrab(input);
2772 menu_redraw_handler(struct widget *widget, void *data)
2775 const int32_t r = 3, margin = 3;
2776 struct menu *menu = data;
2777 int32_t width, height, i;
2778 struct window *window = widget->window;
2780 cr = cairo_create(window->cairo_surface);
2781 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2782 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2785 width = window->allocation.width;
2786 height = window->allocation.height;
2787 rounded_rect(cr, 0, 0, width, height, r);
2789 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2790 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2793 for (i = 0; i < menu->count; i++) {
2794 if (i == menu->current) {
2795 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2796 cairo_rectangle(cr, margin, i * 20 + margin,
2797 width - 2 * margin, 20);
2799 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2800 cairo_move_to(cr, 10, i * 20 + 16);
2801 cairo_show_text(cr, menu->entries[i]);
2803 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2804 cairo_move_to(cr, 10, i * 20 + 16);
2805 cairo_show_text(cr, menu->entries[i]);
2813 window_show_menu(struct display *display,
2814 struct input *input, uint32_t time, struct window *parent,
2815 int32_t x, int32_t y,
2816 menu_func_t func, const char **entries, int count)
2818 struct window *window;
2820 const int32_t margin = 3;
2822 menu = malloc(sizeof *menu);
2826 window = window_create_internal(parent->display, parent);
2830 menu->window = window;
2831 menu->widget = window_add_widget(menu->window, menu);
2832 menu->entries = entries;
2833 menu->count = count;
2837 menu->input = input;
2838 window->type = TYPE_MENU;
2842 input_ungrab(input);
2843 wl_shell_surface_set_popup(window->shell_surface, input->seat,
2844 display_get_serial(window->display),
2845 window->parent->shell_surface,
2846 window->x, window->y, 0);
2848 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
2849 widget_set_enter_handler(menu->widget, menu_enter_handler);
2850 widget_set_leave_handler(menu->widget, menu_leave_handler);
2851 widget_set_motion_handler(menu->widget, menu_motion_handler);
2852 widget_set_button_handler(menu->widget, menu_button_handler);
2854 input_grab(input, menu->widget, 0);
2855 window_schedule_resize(window, 200, count * 20 + margin * 2);
2859 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2861 window->buffer_type = type;
2866 display_handle_geometry(void *data,
2867 struct wl_output *wl_output,
2870 int physical_height,
2875 struct output *output = data;
2877 output->allocation.x = x;
2878 output->allocation.y = y;
2882 display_handle_mode(void *data,
2883 struct wl_output *wl_output,
2889 struct output *output = data;
2890 struct display *display = output->display;
2892 if (flags & WL_OUTPUT_MODE_CURRENT) {
2893 output->allocation.width = width;
2894 output->allocation.height = height;
2895 if (display->output_configure_handler)
2896 (*display->output_configure_handler)(
2897 output, display->user_data);
2901 static const struct wl_output_listener output_listener = {
2902 display_handle_geometry,
2907 display_add_output(struct display *d, uint32_t id)
2909 struct output *output;
2911 output = malloc(sizeof *output);
2915 memset(output, 0, sizeof *output);
2916 output->display = d;
2918 wl_display_bind(d->display, id, &wl_output_interface);
2919 wl_list_insert(d->output_list.prev, &output->link);
2921 wl_output_add_listener(output->output, &output_listener, output);
2925 output_destroy(struct output *output)
2927 if (output->destroy_handler)
2928 (*output->destroy_handler)(output, output->user_data);
2930 wl_output_destroy(output->output);
2931 wl_list_remove(&output->link);
2936 display_set_output_configure_handler(struct display *display,
2937 display_output_handler_t handler)
2939 struct output *output;
2941 display->output_configure_handler = handler;
2945 wl_list_for_each(output, &display->output_list, link)
2946 (*display->output_configure_handler)(output,
2947 display->user_data);
2951 output_set_user_data(struct output *output, void *data)
2953 output->user_data = data;
2957 output_get_user_data(struct output *output)
2959 return output->user_data;
2963 output_set_destroy_handler(struct output *output,
2964 display_output_handler_t handler)
2966 output->destroy_handler = handler;
2967 /* FIXME: implement this, once we have way to remove outputs */
2971 output_get_allocation(struct output *output, struct rectangle *allocation)
2973 *allocation = output->allocation;
2977 output_get_wl_output(struct output *output)
2979 return output->output;
2983 display_add_input(struct display *d, uint32_t id)
2985 struct input *input;
2987 input = malloc(sizeof *input);
2991 memset(input, 0, sizeof *input);
2993 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
2994 input->pointer_focus = NULL;
2995 input->keyboard_focus = NULL;
2996 wl_list_insert(d->input_list.prev, &input->link);
2998 wl_seat_add_listener(input->seat, &seat_listener, input);
2999 wl_seat_set_user_data(input->seat, input);
3001 input->data_device =
3002 wl_data_device_manager_get_data_device(d->data_device_manager,
3004 wl_data_device_add_listener(input->data_device, &data_device_listener,
3009 input_destroy(struct input *input)
3011 input_remove_keyboard_focus(input);
3012 input_remove_pointer_focus(input);
3014 if (input->drag_offer)
3015 data_offer_destroy(input->drag_offer);
3017 if (input->selection_offer)
3018 data_offer_destroy(input->selection_offer);
3020 wl_data_device_destroy(input->data_device);
3021 wl_list_remove(&input->link);
3022 wl_seat_destroy(input->seat);
3027 display_handle_global(struct wl_display *display, uint32_t id,
3028 const char *interface, uint32_t version, void *data)
3030 struct display *d = data;
3032 if (strcmp(interface, "wl_compositor") == 0) {
3034 wl_display_bind(display, id, &wl_compositor_interface);
3035 } else if (strcmp(interface, "wl_output") == 0) {
3036 display_add_output(d, id);
3037 } else if (strcmp(interface, "wl_seat") == 0) {
3038 display_add_input(d, id);
3039 } else if (strcmp(interface, "wl_shell") == 0) {
3040 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3041 } else if (strcmp(interface, "wl_shm") == 0) {
3042 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3043 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3044 d->data_device_manager =
3045 wl_display_bind(display, id,
3046 &wl_data_device_manager_interface);
3051 init_xkb(struct display *d)
3053 d->xkb.names.rules = "evdev";
3054 d->xkb.names.model = "pc105";
3055 d->xkb.names.layout = (char *) option_xkb_layout;
3056 d->xkb.names.variant = (char *) option_xkb_variant;
3057 d->xkb.names.options = (char *) option_xkb_options;
3059 d->xkb.context = xkb_context_new(0);
3060 if (!d->xkb.context) {
3061 fprintf(stderr, "Failed to create XKB context\n");
3065 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
3066 if (!d->xkb.keymap) {
3067 fprintf(stderr, "Failed to compile keymap\n");
3071 d->xkb.state = xkb_state_new(d->xkb.keymap);
3072 if (!d->xkb.state) {
3073 fprintf(stderr, "Failed to create XKB state\n");
3077 d->xkb.control_mask =
3078 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3080 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3082 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3087 fini_xkb(struct display *display)
3089 xkb_state_unref(display->xkb.state);
3090 xkb_map_unref(display->xkb.keymap);
3091 xkb_context_unref(display->xkb.context);
3094 #ifdef HAVE_CAIRO_EGL
3096 init_egl(struct display *d)
3098 EGLint major, minor;
3101 #ifdef USE_CAIRO_GLESV2
3102 # define GL_BIT EGL_OPENGL_ES2_BIT
3104 # define GL_BIT EGL_OPENGL_BIT
3107 static const EGLint argb_cfg_attribs[] = {
3108 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3114 EGL_RENDERABLE_TYPE, GL_BIT,
3118 #ifdef USE_CAIRO_GLESV2
3119 static const EGLint context_attribs[] = {
3120 EGL_CONTEXT_CLIENT_VERSION, 2,
3123 EGLint api = EGL_OPENGL_ES_API;
3125 EGLint *context_attribs = NULL;
3126 EGLint api = EGL_OPENGL_API;
3129 d->dpy = eglGetDisplay(d->display);
3130 if (!eglInitialize(d->dpy, &major, &minor)) {
3131 fprintf(stderr, "failed to initialize display\n");
3135 if (!eglBindAPI(api)) {
3136 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3140 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3141 &d->argb_config, 1, &n) || n != 1) {
3142 fprintf(stderr, "failed to choose argb config\n");
3146 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3147 EGL_NO_CONTEXT, context_attribs);
3148 if (d->argb_ctx == NULL) {
3149 fprintf(stderr, "failed to create context\n");
3153 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3154 fprintf(stderr, "failed to make context current\n");
3158 #ifdef HAVE_CAIRO_EGL
3159 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3160 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3161 fprintf(stderr, "failed to get cairo egl argb device\n");
3170 fini_egl(struct display *display)
3172 #ifdef HAVE_CAIRO_EGL
3173 cairo_device_destroy(display->argb_device);
3176 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3179 eglTerminate(display->dpy);
3185 event_mask_update(uint32_t mask, void *data)
3187 struct display *d = data;
3195 handle_display_data(struct task *task, uint32_t events)
3197 struct display *display =
3198 container_of(task, struct display, display_task);
3200 wl_display_iterate(display->display, display->mask);
3204 display_create(int argc, char *argv[])
3208 argc = parse_options(xkb_options,
3209 ARRAY_LENGTH(xkb_options), argc, argv);
3211 d = malloc(sizeof *d);
3215 memset(d, 0, sizeof *d);
3217 d->display = wl_display_connect(NULL);
3218 if (d->display == NULL) {
3219 fprintf(stderr, "failed to create display: %m\n");
3223 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3224 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3225 d->display_task.run = handle_display_data;
3226 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3228 wl_list_init(&d->deferred_list);
3229 wl_list_init(&d->input_list);
3230 wl_list_init(&d->output_list);
3232 /* Set up listener so we'll catch all events. */
3233 wl_display_add_global_listener(d->display,
3234 display_handle_global, d);
3236 /* Process connection events. */
3237 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3238 #ifdef HAVE_CAIRO_EGL
3239 if (init_egl(d) < 0)
3243 d->image_target_texture_2d =
3244 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3245 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3246 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3248 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
3250 d->theme = theme_create();
3252 wl_list_init(&d->window_list);
3260 display_destroy_outputs(struct display *display)
3263 struct output *output;
3265 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3266 output_destroy(output);
3270 display_destroy_inputs(struct display *display)
3273 struct input *input;
3275 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3276 input_destroy(input);
3280 display_destroy(struct display *display)
3282 if (!wl_list_empty(&display->window_list))
3283 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3285 if (!wl_list_empty(&display->deferred_list))
3286 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3288 display_destroy_outputs(display);
3289 display_destroy_inputs(display);
3293 theme_destroy(display->theme);
3294 wl_cursor_theme_destroy(display->cursor_theme);
3296 #ifdef HAVE_CAIRO_EGL
3301 wl_shell_destroy(display->shell);
3304 wl_shm_destroy(display->shm);
3306 if (display->data_device_manager)
3307 wl_data_device_manager_destroy(display->data_device_manager);
3309 wl_compositor_destroy(display->compositor);
3311 close(display->epoll_fd);
3313 wl_display_flush(display->display);
3314 wl_display_disconnect(display->display);
3319 display_set_user_data(struct display *display, void *data)
3321 display->user_data = data;
3325 display_get_user_data(struct display *display)
3327 return display->user_data;
3331 display_get_display(struct display *display)
3333 return display->display;
3337 display_get_output(struct display *display)
3339 return container_of(display->output_list.next, struct output, link);
3342 struct wl_compositor *
3343 display_get_compositor(struct display *display)
3345 return display->compositor;
3349 display_get_serial(struct display *display)
3351 return display->serial;
3355 display_get_egl_display(struct display *d)
3360 struct wl_data_source *
3361 display_create_data_source(struct display *display)
3363 return wl_data_device_manager_create_data_source(display->data_device_manager);
3367 display_get_argb_egl_config(struct display *d)
3369 return d->argb_config;
3373 display_get_shell(struct display *display)
3375 return display->shell;
3379 display_acquire_window_surface(struct display *display,
3380 struct window *window,
3383 #ifdef HAVE_CAIRO_EGL
3384 struct egl_window_surface_data *data;
3385 cairo_device_t *device;
3387 if (!window->cairo_surface)
3389 device = cairo_surface_get_device(window->cairo_surface);
3394 if (device == display->argb_device)
3395 ctx = display->argb_ctx;
3400 data = cairo_surface_get_user_data(window->cairo_surface,
3403 cairo_device_flush(device);
3404 cairo_device_acquire(device);
3405 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3406 fprintf(stderr, "failed to make surface current\n");
3415 display_release_window_surface(struct display *display,
3416 struct window *window)
3418 #ifdef HAVE_CAIRO_EGL
3419 cairo_device_t *device;
3421 device = cairo_surface_get_device(window->cairo_surface);
3425 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3426 fprintf(stderr, "failed to make context current\n");
3427 cairo_device_release(device);
3432 display_defer(struct display *display, struct task *task)
3434 wl_list_insert(&display->deferred_list, &task->link);
3438 display_watch_fd(struct display *display,
3439 int fd, uint32_t events, struct task *task)
3441 struct epoll_event ep;
3445 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3449 display_run(struct display *display)
3452 struct epoll_event ep[16];
3455 display->running = 1;
3457 wl_display_flush(display->display);
3459 while (!wl_list_empty(&display->deferred_list)) {
3460 task = container_of(display->deferred_list.next,
3462 wl_list_remove(&task->link);
3466 if (!display->running)
3469 wl_display_flush(display->display);
3471 count = epoll_wait(display->epoll_fd,
3472 ep, ARRAY_LENGTH(ep), -1);
3473 for (i = 0; i < count; i++) {
3474 task = ep[i].data.ptr;
3475 task->run(task, ep[i].events);
3481 display_exit(struct display *display)
3483 display->running = 0;