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;
107 struct wl_cursor **cursors;
109 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
110 PFNEGLCREATEIMAGEKHRPROC create_image;
111 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
113 display_output_handler_t output_configure_handler;
128 struct window_output {
129 struct output *output;
134 struct display *display;
135 struct window *parent;
136 struct wl_list window_output_list;
137 struct wl_surface *surface;
138 struct wl_shell_surface *shell_surface;
139 struct wl_region *input_region;
140 struct wl_region *opaque_region;
142 struct rectangle allocation, saved_allocation, server_allocation;
143 struct rectangle pending_allocation;
146 int redraw_scheduled;
148 struct task redraw_task;
152 struct input *keyboard_device;
153 enum window_buffer_type buffer_type;
155 cairo_surface_t *cairo_surface;
157 struct shm_pool *pool;
159 window_key_handler_t key_handler;
160 window_keyboard_focus_handler_t keyboard_focus_handler;
161 window_data_handler_t data_handler;
162 window_drop_handler_t drop_handler;
163 window_close_handler_t close_handler;
166 struct widget *widget;
173 struct window *window;
174 struct tooltip *tooltip;
175 struct wl_list child_list;
177 struct rectangle allocation;
178 widget_resize_handler_t resize_handler;
179 widget_redraw_handler_t redraw_handler;
180 widget_enter_handler_t enter_handler;
181 widget_leave_handler_t leave_handler;
182 widget_motion_handler_t motion_handler;
183 widget_button_handler_t button_handler;
190 struct display *display;
191 struct wl_seat *seat;
192 struct wl_pointer *pointer;
193 struct wl_keyboard *keyboard;
194 struct window *pointer_focus;
195 struct window *keyboard_focus;
198 uint32_t pointer_enter_serial;
202 struct widget *focus_widget;
204 uint32_t grab_button;
206 struct wl_data_device *data_device;
207 struct data_offer *drag_offer;
208 struct data_offer *selection_offer;
212 struct display *display;
213 struct wl_output *output;
214 struct rectangle allocation;
217 display_output_handler_t destroy_handler;
221 enum frame_button_action {
222 FRAME_BUTTON_NULL = 0,
223 FRAME_BUTTON_ICON = 1,
224 FRAME_BUTTON_CLOSE = 2,
225 FRAME_BUTTON_MINIMIZE = 3,
226 FRAME_BUTTON_MAXIMIZE = 4,
229 enum frame_button_pointer {
230 FRAME_BUTTON_DEFAULT = 0,
231 FRAME_BUTTON_OVER = 1,
232 FRAME_BUTTON_ACTIVE = 2,
235 enum frame_button_align {
236 FRAME_BUTTON_RIGHT = 0,
237 FRAME_BUTTON_LEFT = 1,
240 enum frame_button_decoration {
241 FRAME_BUTTON_NONE = 0,
242 FRAME_BUTTON_FANCY = 1,
245 struct frame_button {
246 struct widget *widget;
248 cairo_surface_t *icon;
249 enum frame_button_action type;
250 enum frame_button_pointer state;
251 struct wl_list link; /* buttons_list */
252 enum frame_button_align align;
253 enum frame_button_decoration decoration;
257 struct widget *widget;
258 struct widget *child;
259 struct wl_list buttons_list;
263 struct window *window;
264 struct widget *widget;
266 const char **entries;
274 struct widget *parent;
275 struct window *window;
276 struct widget *widget;
278 struct task tooltip_task;
284 struct wl_shm_pool *pool;
291 CURSOR_DEFAULT = 100,
295 enum window_location {
297 WINDOW_RESIZING_TOP = 1,
298 WINDOW_RESIZING_BOTTOM = 2,
299 WINDOW_RESIZING_LEFT = 4,
300 WINDOW_RESIZING_TOP_LEFT = 5,
301 WINDOW_RESIZING_BOTTOM_LEFT = 6,
302 WINDOW_RESIZING_RIGHT = 8,
303 WINDOW_RESIZING_TOP_RIGHT = 9,
304 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
305 WINDOW_RESIZING_MASK = 15,
306 WINDOW_EXTERIOR = 16,
307 WINDOW_TITLEBAR = 17,
308 WINDOW_CLIENT_AREA = 18,
311 const char *option_xkb_layout = "us";
312 const char *option_xkb_variant = "";
313 const char *option_xkb_options = "";
315 static const struct weston_option xkb_options[] = {
316 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
317 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
318 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
321 static const cairo_user_data_key_t surface_data_key;
322 struct surface_data {
323 struct wl_buffer *buffer;
326 #define MULT(_d,c,a,t) \
327 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
329 #ifdef HAVE_CAIRO_EGL
331 struct egl_window_surface_data {
332 struct display *display;
333 struct wl_surface *surface;
334 struct wl_egl_window *window;
339 egl_window_surface_data_destroy(void *p)
341 struct egl_window_surface_data *data = p;
342 struct display *d = data->display;
344 eglDestroySurface(d->dpy, data->surf);
345 wl_egl_window_destroy(data->window);
346 data->surface = NULL;
351 static cairo_surface_t *
352 display_create_egl_window_surface(struct display *display,
353 struct wl_surface *surface,
355 struct rectangle *rectangle)
357 cairo_surface_t *cairo_surface;
358 struct egl_window_surface_data *data;
360 cairo_device_t *device;
362 data = malloc(sizeof *data);
366 data->display = display;
367 data->surface = surface;
369 config = display->argb_config;
370 device = display->argb_device;
372 data->window = wl_egl_window_create(surface,
376 data->surf = eglCreateWindowSurface(display->dpy, config,
379 cairo_surface = cairo_gl_surface_create_for_egl(device,
384 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
385 data, egl_window_surface_data_destroy);
387 return cairo_surface;
393 display_get_buffer_for_surface(struct display *display,
394 cairo_surface_t *surface)
396 struct surface_data *data;
398 data = cairo_surface_get_user_data (surface, &surface_data_key);
403 struct shm_surface_data {
404 struct surface_data data;
405 struct shm_pool *pool;
409 shm_pool_destroy(struct shm_pool *pool);
412 shm_surface_data_destroy(void *p)
414 struct shm_surface_data *data = p;
416 wl_buffer_destroy(data->data.buffer);
418 shm_pool_destroy(data->pool);
421 static struct wl_shm_pool *
422 make_shm_pool(struct display *display, int size, void **data)
424 char filename[] = "/tmp/wayland-shm-XXXXXX";
425 struct wl_shm_pool *pool;
428 fd = mkstemp(filename);
430 fprintf(stderr, "open %s failed: %m\n", filename);
433 if (ftruncate(fd, size) < 0) {
434 fprintf(stderr, "ftruncate failed: %m\n");
439 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
442 if (*data == MAP_FAILED) {
443 fprintf(stderr, "mmap failed: %m\n");
448 pool = wl_shm_create_pool(display->shm, fd, size);
455 static struct shm_pool *
456 shm_pool_create(struct display *display, size_t size)
458 struct shm_pool *pool = malloc(sizeof *pool);
463 pool->pool = make_shm_pool(display, size, &pool->data);
476 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
478 if (pool->used + size > pool->size)
481 *offset = pool->used;
484 return (char *) pool->data + *offset;
487 /* destroy the pool. this does not unmap the memory though */
489 shm_pool_destroy(struct shm_pool *pool)
491 munmap(pool->data, pool->size);
492 wl_shm_pool_destroy(pool->pool);
496 /* Start allocating from the beginning of the pool again */
498 shm_pool_reset(struct shm_pool *pool)
504 data_length_for_shm_surface(struct rectangle *rect)
508 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
510 return stride * rect->height;
513 static cairo_surface_t *
514 display_create_shm_surface_from_pool(struct display *display,
515 struct rectangle *rectangle,
516 uint32_t flags, struct shm_pool *pool)
518 struct shm_surface_data *data;
520 cairo_surface_t *surface;
521 int stride, length, offset;
524 data = malloc(sizeof *data);
528 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530 length = stride * rectangle->height;
532 map = shm_pool_allocate(pool, length, &offset);
539 surface = cairo_image_surface_create_for_data (map,
545 cairo_surface_set_user_data (surface, &surface_data_key,
546 data, shm_surface_data_destroy);
548 if (flags & SURFACE_OPAQUE)
549 format = WL_SHM_FORMAT_XRGB8888;
551 format = WL_SHM_FORMAT_ARGB8888;
553 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
561 static cairo_surface_t *
562 display_create_shm_surface(struct display *display,
563 struct rectangle *rectangle, uint32_t flags,
564 struct window *window)
566 struct shm_surface_data *data;
567 struct shm_pool *pool;
568 cairo_surface_t *surface;
570 if (window && window->pool) {
571 shm_pool_reset(window->pool);
572 surface = display_create_shm_surface_from_pool(display,
580 pool = shm_pool_create(display,
581 data_length_for_shm_surface(rectangle));
586 display_create_shm_surface_from_pool(display, rectangle,
590 shm_pool_destroy(pool);
594 /* make sure we destroy the pool when the surface is destroyed */
595 data = cairo_surface_get_user_data(surface, &surface_data_key);
602 check_size(struct rectangle *rect)
604 if (rect->width && rect->height)
607 fprintf(stderr, "tried to create surface of "
608 "width: %d, height: %d\n", rect->width, rect->height);
613 display_create_surface(struct display *display,
614 struct wl_surface *surface,
615 struct rectangle *rectangle,
618 if (check_size(rectangle) < 0)
620 #ifdef HAVE_CAIRO_EGL
622 return display_create_egl_window_surface(display,
627 return display_create_shm_surface(display, rectangle, flags, NULL);
630 static const char *cursors[] = {
631 "bottom_left_corner",
632 "bottom_right_corner",
646 create_cursors(struct display *display)
650 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
652 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
654 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
655 display->cursors[i] =
656 wl_cursor_theme_get_cursor(display->cursor_theme,
661 destroy_cursors(struct display *display)
663 wl_cursor_theme_destroy(display->cursor_theme);
666 struct wl_cursor_image *
667 display_get_pointer_image(struct display *display, int pointer)
669 struct wl_cursor *cursor =
670 wl_cursor_theme_get_cursor(display->cursor_theme,
673 return cursor ? cursor->images[0] : NULL;
677 window_get_resize_dx_dy(struct window *window, int *x, int *y)
679 if (window->resize_edges & WINDOW_RESIZING_LEFT)
680 *x = window->server_allocation.width - window->allocation.width;
684 if (window->resize_edges & WINDOW_RESIZING_TOP)
685 *y = window->server_allocation.height -
686 window->allocation.height;
690 window->resize_edges = 0;
694 window_attach_surface(struct window *window)
696 struct display *display = window->display;
697 struct wl_buffer *buffer;
698 #ifdef HAVE_CAIRO_EGL
699 struct egl_window_surface_data *data;
703 if (window->type == TYPE_NONE) {
704 window->type = TYPE_TOPLEVEL;
706 wl_shell_surface_set_toplevel(window->shell_surface);
709 switch (window->buffer_type) {
710 #ifdef HAVE_CAIRO_EGL
711 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
712 data = cairo_surface_get_user_data(window->cairo_surface,
715 cairo_gl_surface_swapbuffers(window->cairo_surface);
716 wl_egl_window_get_attached_size(data->window,
717 &window->server_allocation.width,
718 &window->server_allocation.height);
721 case WINDOW_BUFFER_TYPE_SHM:
723 display_get_buffer_for_surface(display,
724 window->cairo_surface);
726 window_get_resize_dx_dy(window, &x, &y);
727 wl_surface_attach(window->surface, buffer, x, y);
728 wl_surface_damage(window->surface, 0, 0,
729 window->allocation.width,
730 window->allocation.height);
731 window->server_allocation = window->allocation;
732 cairo_surface_destroy(window->cairo_surface);
733 window->cairo_surface = NULL;
739 if (window->input_region) {
740 wl_surface_set_input_region(window->surface,
741 window->input_region);
742 wl_region_destroy(window->input_region);
743 window->input_region = NULL;
746 if (window->opaque_region) {
747 wl_surface_set_opaque_region(window->surface,
748 window->opaque_region);
749 wl_region_destroy(window->opaque_region);
750 window->opaque_region = NULL;
755 window_flush(struct window *window)
757 if (window->cairo_surface)
758 window_attach_surface(window);
762 window_set_surface(struct window *window, cairo_surface_t *surface)
764 cairo_surface_reference(surface);
766 if (window->cairo_surface != NULL)
767 cairo_surface_destroy(window->cairo_surface);
769 window->cairo_surface = surface;
772 #ifdef HAVE_CAIRO_EGL
774 window_resize_cairo_window_surface(struct window *window)
776 struct egl_window_surface_data *data;
779 data = cairo_surface_get_user_data(window->cairo_surface,
782 window_get_resize_dx_dy(window, &x, &y),
783 wl_egl_window_resize(data->window,
784 window->allocation.width,
785 window->allocation.height,
788 cairo_gl_surface_set_size(window->cairo_surface,
789 window->allocation.width,
790 window->allocation.height);
795 window_get_display(struct window *window)
797 return window->display;
801 window_create_surface(struct window *window)
803 cairo_surface_t *surface;
806 if (!window->transparent)
807 flags = SURFACE_OPAQUE;
809 switch (window->buffer_type) {
810 #ifdef HAVE_CAIRO_EGL
811 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
812 if (window->cairo_surface) {
813 window_resize_cairo_window_surface(window);
816 surface = display_create_surface(window->display,
818 &window->allocation, flags);
821 case WINDOW_BUFFER_TYPE_SHM:
822 surface = display_create_shm_surface(window->display,
831 window_set_surface(window, surface);
832 cairo_surface_destroy(surface);
835 static void frame_destroy(struct frame *frame);
838 window_destroy(struct window *window)
840 struct display *display = window->display;
842 struct window_output *window_output;
843 struct window_output *window_output_tmp;
845 if (window->redraw_scheduled)
846 wl_list_remove(&window->redraw_task.link);
848 wl_list_for_each(input, &display->input_list, link) {
849 if (input->pointer_focus == window)
850 input->pointer_focus = NULL;
851 if (input->keyboard_focus == window)
852 input->keyboard_focus = NULL;
853 if (input->focus_widget &&
854 input->focus_widget->window == window)
855 input->focus_widget = NULL;
858 wl_list_for_each_safe(window_output, window_output_tmp,
859 &window->window_output_list, link) {
860 free (window_output);
863 if (window->input_region)
864 wl_region_destroy(window->input_region);
865 if (window->opaque_region)
866 wl_region_destroy(window->opaque_region);
869 frame_destroy(window->frame);
871 if (window->shell_surface)
872 wl_shell_surface_destroy(window->shell_surface);
873 wl_surface_destroy(window->surface);
874 wl_list_remove(&window->link);
876 if (window->cairo_surface != NULL)
877 cairo_surface_destroy(window->cairo_surface);
883 static struct widget *
884 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
886 struct widget *child, *target;
888 wl_list_for_each(child, &widget->child_list, link) {
889 target = widget_find_widget(child, x, y);
894 if (widget->allocation.x <= x &&
895 x < widget->allocation.x + widget->allocation.width &&
896 widget->allocation.y <= y &&
897 y < widget->allocation.y + widget->allocation.height) {
904 static struct widget *
905 widget_create(struct window *window, void *data)
907 struct widget *widget;
909 widget = malloc(sizeof *widget);
910 memset(widget, 0, sizeof *widget);
911 widget->window = window;
912 widget->user_data = data;
913 widget->allocation = window->allocation;
914 wl_list_init(&widget->child_list);
916 widget->tooltip = NULL;
917 widget->tooltip_count = 0;
923 window_add_widget(struct window *window, void *data)
925 window->widget = widget_create(window, data);
926 wl_list_init(&window->widget->link);
928 return window->widget;
932 widget_add_widget(struct widget *parent, void *data)
934 struct widget *widget;
936 widget = widget_create(parent->window, data);
937 wl_list_insert(parent->child_list.prev, &widget->link);
943 widget_destroy(struct widget *widget)
945 struct display *display = widget->window->display;
948 if (widget->tooltip) {
949 free(widget->tooltip);
950 widget->tooltip = NULL;
953 wl_list_for_each(input, &display->input_list, link) {
954 if (input->focus_widget == widget)
955 input->focus_widget = NULL;
958 wl_list_remove(&widget->link);
963 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
965 *allocation = widget->allocation;
969 widget_set_size(struct widget *widget, int32_t width, int32_t height)
971 widget->allocation.width = width;
972 widget->allocation.height = height;
976 widget_set_allocation(struct widget *widget,
977 int32_t x, int32_t y, int32_t width, int32_t height)
979 widget->allocation.x = x;
980 widget->allocation.y = y;
981 widget_set_size(widget, width, height);
985 widget_set_transparent(struct widget *widget, int transparent)
987 widget->opaque = !transparent;
991 widget_get_user_data(struct widget *widget)
993 return widget->user_data;
997 widget_set_resize_handler(struct widget *widget,
998 widget_resize_handler_t handler)
1000 widget->resize_handler = handler;
1004 widget_set_redraw_handler(struct widget *widget,
1005 widget_redraw_handler_t handler)
1007 widget->redraw_handler = handler;
1011 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1013 widget->enter_handler = handler;
1017 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1019 widget->leave_handler = handler;
1023 widget_set_motion_handler(struct widget *widget,
1024 widget_motion_handler_t handler)
1026 widget->motion_handler = handler;
1030 widget_set_button_handler(struct widget *widget,
1031 widget_button_handler_t handler)
1033 widget->button_handler = handler;
1037 widget_schedule_redraw(struct widget *widget)
1039 window_schedule_redraw(widget->window);
1043 window_get_surface(struct window *window)
1045 return cairo_surface_reference(window->cairo_surface);
1049 window_get_wl_surface(struct window *window)
1051 return window->surface;
1054 struct wl_shell_surface *
1055 window_get_wl_shell_surface(struct window *window)
1057 return window->shell_surface;
1061 tooltip_redraw_handler(struct widget *widget, void *data)
1064 const int32_t r = 3;
1065 struct tooltip *tooltip = data;
1066 int32_t width, height;
1067 struct window *window = widget->window;
1069 cr = cairo_create(window->cairo_surface);
1070 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1071 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1074 width = window->allocation.width;
1075 height = window->allocation.height;
1076 rounded_rect(cr, 0, 0, width, height, r);
1078 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1079 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1082 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1083 cairo_move_to(cr, 10, 16);
1084 cairo_show_text(cr, tooltip->entry);
1088 static cairo_text_extents_t
1089 get_text_extents(struct tooltip *tooltip)
1091 struct window *window;
1093 cairo_text_extents_t extents;
1095 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1097 window = tooltip->widget->window->parent;
1098 cr = cairo_create(window->cairo_surface);
1099 cairo_text_extents(cr, tooltip->entry, &extents);
1106 window_create_tooltip(struct tooltip *tooltip)
1108 struct widget *parent = tooltip->parent;
1109 struct display *display = parent->window->display;
1110 struct window *window;
1111 const int offset_y = 27;
1112 const int margin = 3;
1113 cairo_text_extents_t extents;
1115 if (tooltip->widget)
1118 window = window_create_transient(display, parent->window, tooltip->x,
1119 tooltip->y + offset_y,
1120 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1124 tooltip->window = window;
1125 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1127 extents = get_text_extents(tooltip);
1128 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1129 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1135 widget_destroy_tooltip(struct widget *parent)
1137 struct tooltip *tooltip = parent->tooltip;
1139 parent->tooltip_count = 0;
1143 if (tooltip->widget) {
1144 widget_destroy(tooltip->widget);
1145 window_destroy(tooltip->window);
1146 tooltip->widget = NULL;
1147 tooltip->window = NULL;
1150 close(tooltip->tooltip_fd);
1151 free(tooltip->entry);
1153 parent->tooltip = NULL;
1157 tooltip_func(struct task *task, uint32_t events)
1159 struct tooltip *tooltip =
1160 container_of(task, struct tooltip, tooltip_task);
1163 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1164 window_create_tooltip(tooltip);
1167 #define TOOLTIP_TIMEOUT 500
1169 tooltip_timer_reset(struct tooltip *tooltip)
1171 struct itimerspec its;
1173 its.it_interval.tv_sec = 0;
1174 its.it_interval.tv_nsec = 0;
1175 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1176 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1177 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1178 fprintf(stderr, "could not set timerfd\n: %m");
1186 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1188 struct tooltip *tooltip = parent->tooltip;
1190 parent->tooltip_count++;
1194 tooltip_timer_reset(tooltip);
1198 /* the handler might be triggered too fast via input device motion, so
1199 * we need this check here to make sure tooltip is fully initialized */
1200 if (parent->tooltip_count > 1)
1203 tooltip = malloc(sizeof *tooltip);
1207 parent->tooltip = tooltip;
1208 tooltip->parent = parent;
1209 tooltip->widget = NULL;
1210 tooltip->window = NULL;
1213 tooltip->entry = strdup(entry);
1214 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1215 if (tooltip->tooltip_fd < 0) {
1216 fprintf(stderr, "could not create timerfd\n: %m");
1220 tooltip->tooltip_task.run = tooltip_func;
1221 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1222 EPOLLIN, &tooltip->tooltip_task);
1223 tooltip_timer_reset(tooltip);
1229 frame_resize_handler(struct widget *widget,
1230 int32_t width, int32_t height, void *data)
1232 struct frame *frame = data;
1233 struct widget *child = frame->child;
1234 struct rectangle allocation;
1235 struct display *display = widget->window->display;
1236 struct frame_button * button;
1237 struct theme *t = display->theme;
1238 int x_l, x_r, y, w, h;
1239 int decoration_width, decoration_height;
1242 if (widget->window->type != TYPE_FULLSCREEN) {
1243 decoration_width = (t->width + t->margin) * 2;
1244 decoration_height = t->width +
1245 t->titlebar_height + t->margin * 2;
1247 allocation.x = t->width + t->margin;
1248 allocation.y = t->titlebar_height + t->margin;
1249 allocation.width = width - decoration_width;
1250 allocation.height = height - decoration_height;
1252 opaque_margin = t->margin + t->frame_radius;
1254 wl_list_for_each(button, &frame->buttons_list, link)
1255 button->widget->opaque = 0;
1257 decoration_width = 0;
1258 decoration_height = 0;
1262 allocation.width = width;
1263 allocation.height = height;
1266 wl_list_for_each(button, &frame->buttons_list, link)
1267 button->widget->opaque = 1;
1270 widget_set_allocation(child, allocation.x, allocation.y,
1271 allocation.width, allocation.height);
1273 if (child->resize_handler)
1274 child->resize_handler(child,
1279 width = child->allocation.width + decoration_width;
1280 height = child->allocation.height + decoration_height;
1282 widget->window->input_region =
1283 wl_compositor_create_region(display->compositor);
1284 wl_region_add(widget->window->input_region,
1285 t->margin, t->margin,
1286 width - 2 * t->margin,
1287 height - 2 * t->margin);
1289 widget_set_allocation(widget, 0, 0, width, height);
1291 if (child->opaque) {
1292 widget->window->opaque_region =
1293 wl_compositor_create_region(display->compositor);
1294 wl_region_add(widget->window->opaque_region,
1295 opaque_margin, opaque_margin,
1296 widget->allocation.width - 2 * opaque_margin,
1297 widget->allocation.height - 2 * opaque_margin);
1300 /* frame internal buttons */
1301 x_r = frame->widget->allocation.width - t->width - t->margin;
1302 x_l = t->width + t->margin;
1303 y = t->width + t->margin;
1304 wl_list_for_each(button, &frame->buttons_list, link) {
1305 const int button_padding = 4;
1306 w = cairo_image_surface_get_width(button->icon);
1307 h = cairo_image_surface_get_height(button->icon);
1309 if (button->decoration == FRAME_BUTTON_FANCY)
1312 if (button->align == FRAME_BUTTON_LEFT) {
1313 widget_set_allocation(button->widget,
1314 x_l, y , w + 1, h + 1);
1316 x_l += button_padding;
1319 widget_set_allocation(button->widget,
1320 x_r, y , w + 1, h + 1);
1321 x_r -= button_padding;
1327 frame_button_enter_handler(struct widget *widget,
1328 struct input *input, float x, float y, void *data)
1330 struct frame_button *frame_button = data;
1332 widget_schedule_redraw(frame_button->widget);
1333 frame_button->state = FRAME_BUTTON_OVER;
1335 return CURSOR_LEFT_PTR;
1339 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1341 struct frame_button *frame_button = data;
1343 widget_schedule_redraw(frame_button->widget);
1344 frame_button->state = FRAME_BUTTON_DEFAULT;
1348 frame_button_button_handler(struct widget *widget,
1349 struct input *input, uint32_t time,
1350 uint32_t button, uint32_t state, void *data)
1352 struct frame_button *frame_button = data;
1353 struct window *window = widget->window;
1355 if (button != BTN_LEFT)
1360 frame_button->state = FRAME_BUTTON_ACTIVE;
1361 widget_schedule_redraw(frame_button->widget);
1363 if (frame_button->type == FRAME_BUTTON_ICON)
1364 window_show_frame_menu(window, input, time);
1367 frame_button->state = FRAME_BUTTON_DEFAULT;
1368 widget_schedule_redraw(frame_button->widget);
1372 switch (frame_button->type) {
1373 case FRAME_BUTTON_CLOSE:
1374 if (window->close_handler)
1375 window->close_handler(window->parent,
1378 display_exit(window->display);
1380 case FRAME_BUTTON_MINIMIZE:
1381 fprintf(stderr,"Minimize stub\n");
1383 case FRAME_BUTTON_MAXIMIZE:
1384 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1387 /* Unknown operation */
1393 frame_button_redraw_handler(struct widget *widget, void *data)
1395 struct frame_button *frame_button = data;
1397 int width, height, x, y;
1398 struct window *window = widget->window;
1400 x = widget->allocation.x;
1401 y = widget->allocation.y;
1402 width = widget->allocation.width;
1403 height = widget->allocation.height;
1412 cr = cairo_create(window->cairo_surface);
1414 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1415 cairo_set_line_width(cr, 1);
1417 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1418 cairo_rectangle (cr, x, y, 25, 16);
1420 cairo_stroke_preserve(cr);
1422 switch (frame_button->state) {
1423 case FRAME_BUTTON_DEFAULT:
1424 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1426 case FRAME_BUTTON_OVER:
1427 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1429 case FRAME_BUTTON_ACTIVE:
1430 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1439 cairo_set_source_surface(cr, frame_button->icon, x, y);
1445 static struct widget *
1446 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1447 enum frame_button_align align, enum frame_button_decoration style)
1449 struct frame_button *frame_button;
1450 const char *icon = data;
1452 frame_button = malloc (sizeof *frame_button);
1453 memset(frame_button, 0, sizeof *frame_button);
1455 frame_button->icon = cairo_image_surface_create_from_png(icon);
1456 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1457 frame_button->frame = frame;
1458 frame_button->type = type;
1459 frame_button->align = align;
1460 frame_button->decoration = style;
1462 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1464 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1465 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1466 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1467 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1468 return frame_button->widget;
1472 frame_button_destroy(struct frame_button *frame_button)
1474 widget_destroy(frame_button->widget);
1475 wl_list_remove(&frame_button->link);
1476 cairo_surface_destroy(frame_button->icon);
1483 frame_redraw_handler(struct widget *widget, void *data)
1486 struct window *window = widget->window;
1487 struct theme *t = window->display->theme;
1490 if (window->type == TYPE_FULLSCREEN)
1493 cr = cairo_create(window->cairo_surface);
1495 if (window->keyboard_device)
1496 flags |= THEME_FRAME_ACTIVE;
1497 theme_render_frame(t, cr, widget->allocation.width,
1498 widget->allocation.height, window->title, flags);
1504 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1506 struct theme *t = frame->widget->window->display->theme;
1509 location = theme_get_location(t, input->sx, input->sy,
1510 frame->widget->allocation.width,
1511 frame->widget->allocation.height);
1514 case THEME_LOCATION_RESIZING_TOP:
1516 case THEME_LOCATION_RESIZING_BOTTOM:
1517 return CURSOR_BOTTOM;
1518 case THEME_LOCATION_RESIZING_LEFT:
1520 case THEME_LOCATION_RESIZING_RIGHT:
1521 return CURSOR_RIGHT;
1522 case THEME_LOCATION_RESIZING_TOP_LEFT:
1523 return CURSOR_TOP_LEFT;
1524 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1525 return CURSOR_TOP_RIGHT;
1526 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1527 return CURSOR_BOTTOM_LEFT;
1528 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1529 return CURSOR_BOTTOM_RIGHT;
1530 case THEME_LOCATION_EXTERIOR:
1531 case THEME_LOCATION_TITLEBAR:
1533 return CURSOR_LEFT_PTR;
1538 frame_menu_func(struct window *window, int index, void *data)
1542 if (window->close_handler)
1543 window->close_handler(window->parent,
1546 display_exit(window->display);
1548 case 1: /* fullscreen */
1549 /* we don't have a way to get out of fullscreen for now */
1550 window_set_fullscreen(window, 1);
1552 case 2: /* rotate */
1559 window_show_frame_menu(struct window *window,
1560 struct input *input, uint32_t time)
1564 static const char *entries[] = {
1565 "Close", "Fullscreen", "Rotate", "Scale"
1568 input_get_position(input, &x, &y);
1569 window_show_menu(window->display, input, time, window,
1570 x - 10, y - 10, frame_menu_func, entries, 4);
1574 frame_enter_handler(struct widget *widget,
1575 struct input *input, float x, float y, void *data)
1577 return frame_get_pointer_image_for_location(data, input);
1581 frame_motion_handler(struct widget *widget,
1582 struct input *input, uint32_t time,
1583 float x, float y, void *data)
1585 return frame_get_pointer_image_for_location(data, input);
1589 frame_button_handler(struct widget *widget,
1590 struct input *input, uint32_t time,
1591 uint32_t button, uint32_t state, void *data)
1594 struct frame *frame = data;
1595 struct window *window = widget->window;
1596 struct display *display = window->display;
1599 location = theme_get_location(display->theme, input->sx, input->sy,
1600 frame->widget->allocation.width,
1601 frame->widget->allocation.height);
1603 if (window->display->shell && button == BTN_LEFT && state == 1) {
1605 case THEME_LOCATION_TITLEBAR:
1606 if (!window->shell_surface)
1608 input_set_pointer_image(input, time, CURSOR_DRAGGING);
1609 input_ungrab(input);
1610 wl_shell_surface_move(window->shell_surface,
1611 input_get_seat(input),
1614 case THEME_LOCATION_RESIZING_TOP:
1615 case THEME_LOCATION_RESIZING_BOTTOM:
1616 case THEME_LOCATION_RESIZING_LEFT:
1617 case THEME_LOCATION_RESIZING_RIGHT:
1618 case THEME_LOCATION_RESIZING_TOP_LEFT:
1619 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1620 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1621 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1622 if (!window->shell_surface)
1624 input_ungrab(input);
1626 if (!display->dpy) {
1627 /* If we're using shm, allocate a big
1628 pool to create buffers out of while
1629 we resize. We should probably base
1630 this number on the size of the output. */
1632 shm_pool_create(display, 6 * 1024 * 1024);
1635 wl_shell_surface_resize(window->shell_surface,
1636 input_get_seat(input),
1637 display->serial, location);
1640 } else if (button == BTN_RIGHT && state == 1) {
1641 window_show_frame_menu(window, input, time);
1646 frame_create(struct window *window, void *data)
1648 struct frame *frame;
1650 frame = malloc(sizeof *frame);
1651 memset(frame, 0, sizeof *frame);
1653 frame->widget = window_add_widget(window, frame);
1654 frame->child = widget_add_widget(frame->widget, data);
1656 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1657 widget_set_resize_handler(frame->widget, frame_resize_handler);
1658 widget_set_enter_handler(frame->widget, frame_enter_handler);
1659 widget_set_motion_handler(frame->widget, frame_motion_handler);
1660 widget_set_button_handler(frame->widget, frame_button_handler);
1662 /* Create empty list for frame buttons */
1663 wl_list_init(&frame->buttons_list);
1665 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1666 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1668 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1669 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1671 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1672 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1674 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1675 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677 window->frame = frame;
1679 return frame->child;
1683 frame_destroy(struct frame *frame)
1685 struct frame_button *button, *tmp;
1687 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1688 frame_button_destroy(button);
1690 /* frame->child must be destroyed by the application */
1691 widget_destroy(frame->widget);
1696 input_set_focus_widget(struct input *input, struct widget *focus,
1699 struct widget *old, *widget;
1700 int pointer = CURSOR_LEFT_PTR;
1702 if (focus == input->focus_widget)
1705 old = input->focus_widget;
1709 widget = input->grab;
1710 if (widget->leave_handler)
1711 widget->leave_handler(old, input, widget->user_data);
1712 input->focus_widget = NULL;
1718 widget = input->grab;
1719 if (widget->enter_handler)
1720 pointer = widget->enter_handler(focus, input, x, y,
1722 input->focus_widget = focus;
1724 input_set_pointer_image(input, input->pointer_enter_serial,
1730 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1731 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1733 struct input *input = data;
1734 struct window *window = input->pointer_focus;
1735 struct widget *widget;
1736 int cursor = CURSOR_LEFT_PTR;
1737 float sx = wl_fixed_to_double(sx_w);
1738 float sy = wl_fixed_to_double(sy_w);
1743 if (!(input->grab && input->grab_button)) {
1744 widget = widget_find_widget(window->widget, sx, sy);
1745 input_set_focus_widget(input, widget, sx, sy);
1749 widget = input->grab;
1751 widget = input->focus_widget;
1752 if (widget && widget->motion_handler)
1753 cursor = widget->motion_handler(input->focus_widget,
1754 input, time, sx, sy,
1757 input_set_pointer_image(input, time, cursor);
1761 input_grab(struct input *input, struct widget *widget, uint32_t button)
1763 input->grab = widget;
1764 input->grab_button = button;
1768 input_ungrab(struct input *input)
1770 struct widget *widget;
1773 if (input->pointer_focus) {
1774 widget = widget_find_widget(input->pointer_focus->widget,
1775 input->sx, input->sy);
1776 input_set_focus_widget(input, widget, input->sx, input->sy);
1781 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1782 uint32_t time, uint32_t button, uint32_t state)
1784 struct input *input = data;
1785 struct widget *widget;
1787 input->display->serial = serial;
1788 if (input->focus_widget && input->grab == NULL && state)
1789 input_grab(input, input->focus_widget, button);
1791 widget = input->grab;
1792 if (widget && widget->button_handler)
1793 (*widget->button_handler)(widget,
1796 input->grab->user_data);
1798 if (input->grab && input->grab_button == button && !state)
1799 input_ungrab(input);
1803 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1804 uint32_t time, uint32_t axis, int32_t value)
1809 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1810 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1812 struct input *input = data;
1813 struct window *window = input->keyboard_focus;
1814 struct display *d = input->display;
1815 uint32_t code, num_syms;
1816 const xkb_keysym_t *syms;
1818 xkb_mod_mask_t mask;
1820 input->display->serial = serial;
1822 if (!window || window->keyboard_device != input)
1825 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1826 xkb_state_update_key(d->xkb.state, code,
1827 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1829 mask = xkb_state_serialize_mods(d->xkb.state,
1830 XKB_STATE_DEPRESSED |
1832 input->modifiers = 0;
1833 if (mask & input->display->xkb.control_mask)
1834 input->modifiers |= MOD_CONTROL_MASK;
1835 if (mask & input->display->xkb.alt_mask)
1836 input->modifiers |= MOD_ALT_MASK;
1837 if (mask & input->display->xkb.shift_mask)
1838 input->modifiers |= MOD_SHIFT_MASK;
1840 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
1841 input->modifiers == MOD_ALT_MASK) {
1843 window_set_maximized(window,
1844 window->type != TYPE_MAXIMIZED);
1845 } else if (window->key_handler) {
1849 sym = XKB_KEY_NoSymbol;
1851 (*window->key_handler)(window, input, time, key,
1852 sym, state, window->user_data);
1857 input_remove_pointer_focus(struct input *input)
1859 struct window *window = input->pointer_focus;
1864 input_set_focus_widget(input, NULL, 0, 0);
1866 input->pointer_focus = NULL;
1867 input->current_cursor = CURSOR_UNSET;
1871 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1872 uint32_t serial, struct wl_surface *surface,
1873 wl_fixed_t sx_w, wl_fixed_t sy_w)
1875 struct input *input = data;
1876 struct window *window;
1877 struct widget *widget;
1878 float sx = wl_fixed_to_double(sx_w);
1879 float sy = wl_fixed_to_double(sy_w);
1882 /* enter event for a window we've just destroyed */
1886 input->display->serial = serial;
1887 input->pointer_enter_serial = serial;
1888 input->pointer_focus = wl_surface_get_user_data(surface);
1889 window = input->pointer_focus;
1892 shm_pool_destroy(window->pool);
1893 window->pool = NULL;
1894 /* Schedule a redraw to free the pool */
1895 window_schedule_redraw(window);
1901 widget = widget_find_widget(window->widget, sx, sy);
1902 input_set_focus_widget(input, widget, sx, sy);
1906 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1907 uint32_t serial, struct wl_surface *surface)
1909 struct input *input = data;
1911 input->display->serial = serial;
1912 input_remove_pointer_focus(input);
1916 input_remove_keyboard_focus(struct input *input)
1918 struct window *window = input->keyboard_focus;
1923 window->keyboard_device = NULL;
1924 if (window->keyboard_focus_handler)
1925 (*window->keyboard_focus_handler)(window, NULL,
1928 input->keyboard_focus = NULL;
1932 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1933 uint32_t serial, struct wl_surface *surface,
1934 struct wl_array *keys)
1936 struct input *input = data;
1937 struct window *window;
1939 input->display->serial = serial;
1940 input->keyboard_focus = wl_surface_get_user_data(surface);
1942 window = input->keyboard_focus;
1943 window->keyboard_device = input;
1944 if (window->keyboard_focus_handler)
1945 (*window->keyboard_focus_handler)(window,
1946 window->keyboard_device,
1951 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1952 uint32_t serial, struct wl_surface *surface)
1954 struct input *input = data;
1956 input->display->serial = serial;
1957 input_remove_keyboard_focus(input);
1960 static const struct wl_pointer_listener pointer_listener = {
1961 pointer_handle_enter,
1962 pointer_handle_leave,
1963 pointer_handle_motion,
1964 pointer_handle_button,
1965 pointer_handle_axis,
1968 static const struct wl_keyboard_listener keyboard_listener = {
1969 keyboard_handle_enter,
1970 keyboard_handle_leave,
1971 keyboard_handle_key,
1975 seat_handle_capabilities(void *data, struct wl_seat *seat,
1976 enum wl_seat_capability caps)
1978 struct input *input = data;
1980 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
1981 input->pointer = wl_seat_get_pointer(seat);
1982 wl_pointer_set_user_data(input->pointer, input);
1983 wl_pointer_add_listener(input->pointer, &pointer_listener,
1985 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
1986 wl_pointer_destroy(input->pointer);
1987 input->pointer = NULL;
1990 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
1991 input->keyboard = wl_seat_get_keyboard(seat);
1992 wl_keyboard_set_user_data(input->keyboard, input);
1993 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
1995 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
1996 wl_keyboard_destroy(input->keyboard);
1997 input->keyboard = NULL;
2001 static const struct wl_seat_listener seat_listener = {
2002 seat_handle_capabilities,
2006 input_get_position(struct input *input, int32_t *x, int32_t *y)
2013 input_get_seat(struct input *input)
2019 input_get_modifiers(struct input *input)
2021 return input->modifiers;
2025 input_get_focus_widget(struct input *input)
2027 return input->focus_widget;
2031 struct wl_data_offer *offer;
2032 struct input *input;
2033 struct wl_array types;
2036 struct task io_task;
2044 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2046 struct data_offer *offer = data;
2049 p = wl_array_add(&offer->types, sizeof *p);
2053 static const struct wl_data_offer_listener data_offer_listener = {
2058 data_offer_destroy(struct data_offer *offer)
2063 if (offer->refcount == 0) {
2064 wl_data_offer_destroy(offer->offer);
2065 for (p = offer->types.data; *p; p++)
2067 wl_array_release(&offer->types);
2073 data_device_data_offer(void *data,
2074 struct wl_data_device *data_device, uint32_t id)
2076 struct data_offer *offer;
2078 offer = malloc(sizeof *offer);
2080 wl_array_init(&offer->types);
2081 offer->refcount = 1;
2082 offer->input = data;
2084 /* FIXME: Generate typesafe wrappers for this */
2085 offer->offer = (struct wl_data_offer *)
2086 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2087 id, &wl_data_offer_interface);
2089 wl_data_offer_add_listener(offer->offer,
2090 &data_offer_listener, offer);
2094 data_device_enter(void *data, struct wl_data_device *data_device,
2095 uint32_t serial, struct wl_surface *surface,
2096 wl_fixed_t x_w, wl_fixed_t y_w,
2097 struct wl_data_offer *offer)
2099 struct input *input = data;
2100 struct window *window;
2101 float x = wl_fixed_to_double(x_w);
2102 float y = wl_fixed_to_double(y_w);
2105 input->pointer_enter_serial = serial;
2106 input->drag_offer = wl_data_offer_get_user_data(offer);
2107 window = wl_surface_get_user_data(surface);
2108 input->pointer_focus = window;
2110 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2113 window = input->pointer_focus;
2114 if (window->data_handler)
2115 window->data_handler(window, input, x, y,
2116 input->drag_offer->types.data,
2121 data_device_leave(void *data, struct wl_data_device *data_device)
2123 struct input *input = data;
2125 data_offer_destroy(input->drag_offer);
2126 input->drag_offer = NULL;
2130 data_device_motion(void *data, struct wl_data_device *data_device,
2131 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2133 struct input *input = data;
2134 struct window *window = input->pointer_focus;
2135 float x = wl_fixed_to_double(x_w);
2136 float y = wl_fixed_to_double(y_w);
2141 if (window->data_handler)
2142 window->data_handler(window, input, x, y,
2143 input->drag_offer->types.data,
2148 data_device_drop(void *data, struct wl_data_device *data_device)
2150 struct input *input = data;
2151 struct window *window = input->pointer_focus;
2153 if (window->drop_handler)
2154 window->drop_handler(window, input,
2155 input->sx, input->sy, window->user_data);
2159 data_device_selection(void *data,
2160 struct wl_data_device *wl_data_device,
2161 struct wl_data_offer *offer)
2163 struct input *input = data;
2166 if (input->selection_offer)
2167 data_offer_destroy(input->selection_offer);
2170 input->selection_offer = wl_data_offer_get_user_data(offer);
2171 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2176 static const struct wl_data_device_listener data_device_listener = {
2177 data_device_data_offer,
2182 data_device_selection
2186 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2188 struct wl_buffer *buffer;
2189 struct wl_cursor *cursor;
2190 struct wl_cursor_image *image;
2192 if (pointer == input->current_cursor)
2195 cursor = input->display->cursors[pointer];
2199 image = cursor->images[0];
2200 buffer = wl_cursor_image_get_buffer(image);
2204 input->current_cursor = pointer;
2205 wl_pointer_attach(input->pointer, time, buffer,
2206 image->hotspot_x, image->hotspot_y);
2209 struct wl_data_device *
2210 input_get_data_device(struct input *input)
2212 return input->data_device;
2216 input_set_selection(struct input *input,
2217 struct wl_data_source *source, uint32_t time)
2219 wl_data_device_set_selection(input->data_device, source, time);
2223 input_accept(struct input *input, const char *type)
2225 wl_data_offer_accept(input->drag_offer->offer,
2226 input->pointer_enter_serial, type);
2230 offer_io_func(struct task *task, uint32_t events)
2232 struct data_offer *offer =
2233 container_of(task, struct data_offer, io_task);
2237 len = read(offer->fd, buffer, sizeof buffer);
2238 offer->func(buffer, len,
2239 offer->x, offer->y, offer->user_data);
2243 data_offer_destroy(offer);
2248 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2249 data_func_t func, void *user_data)
2253 if (pipe2(p, O_CLOEXEC) == -1)
2256 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2259 offer->io_task.run = offer_io_func;
2263 offer->user_data = user_data;
2265 display_watch_fd(offer->input->display,
2266 offer->fd, EPOLLIN, &offer->io_task);
2270 input_receive_drag_data(struct input *input, const char *mime_type,
2271 data_func_t func, void *data)
2273 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2274 input->drag_offer->x = input->sx;
2275 input->drag_offer->y = input->sy;
2279 input_receive_selection_data(struct input *input, const char *mime_type,
2280 data_func_t func, void *data)
2284 if (input->selection_offer == NULL)
2287 for (p = input->selection_offer->types.data; *p; p++)
2288 if (strcmp(mime_type, *p) == 0)
2294 data_offer_receive_data(input->selection_offer,
2295 mime_type, func, data);
2300 input_receive_selection_data_to_fd(struct input *input,
2301 const char *mime_type, int fd)
2303 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2309 window_move(struct window *window, struct input *input, uint32_t serial)
2311 if (!window->shell_surface)
2314 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2318 idle_resize(struct window *window)
2320 struct widget *widget;
2322 window->resize_needed = 0;
2323 widget = window->widget;
2324 widget_set_allocation(widget,
2325 window->pending_allocation.x,
2326 window->pending_allocation.y,
2327 window->pending_allocation.width,
2328 window->pending_allocation.height);
2330 if (window->input_region) {
2331 wl_region_destroy(window->input_region);
2332 window->input_region = NULL;
2335 if (window->opaque_region) {
2336 wl_region_destroy(window->opaque_region);
2337 window->opaque_region = NULL;
2340 if (widget->resize_handler)
2341 widget->resize_handler(widget,
2342 widget->allocation.width,
2343 widget->allocation.height,
2346 if (window->allocation.width != widget->allocation.width ||
2347 window->allocation.height != widget->allocation.height) {
2348 window->allocation = widget->allocation;
2349 window_schedule_redraw(window);
2354 window_schedule_resize(struct window *window, int width, int height)
2356 window->pending_allocation.x = 0;
2357 window->pending_allocation.y = 0;
2358 window->pending_allocation.width = width;
2359 window->pending_allocation.height = height;
2361 window->resize_needed = 1;
2362 window_schedule_redraw(window);
2366 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2368 window_schedule_resize(widget->window, width, height);
2372 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2375 wl_shell_surface_pong(shell_surface, serial);
2379 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2380 uint32_t edges, int32_t width, int32_t height)
2382 struct window *window = data;
2384 if (width <= 0 || height <= 0)
2387 window->resize_edges = edges;
2388 window_schedule_resize(window, width, height);
2392 menu_destroy(struct menu *menu)
2394 widget_destroy(menu->widget);
2395 window_destroy(menu->window);
2400 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2402 struct window *window = data;
2403 struct menu *menu = window->widget->user_data;
2405 /* FIXME: Need more context in this event, at least the input
2406 * device. Or just use wl_callback. And this really needs to
2407 * be a window vfunc that the menu can set. And we need the
2410 menu->func(window->parent, menu->current, window->parent->user_data);
2411 input_ungrab(menu->input);
2415 static const struct wl_shell_surface_listener shell_surface_listener = {
2422 window_get_allocation(struct window *window,
2423 struct rectangle *allocation)
2425 *allocation = window->allocation;
2429 widget_redraw(struct widget *widget)
2431 struct widget *child;
2433 if (widget->redraw_handler)
2434 widget->redraw_handler(widget, widget->user_data);
2435 wl_list_for_each(child, &widget->child_list, link)
2436 widget_redraw(child);
2440 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2442 struct window *window = data;
2444 wl_callback_destroy(callback);
2445 window->redraw_scheduled = 0;
2446 if (window->redraw_needed)
2447 window_schedule_redraw(window);
2450 static const struct wl_callback_listener listener = {
2455 idle_redraw(struct task *task, uint32_t events)
2457 struct window *window = container_of(task, struct window, redraw_task);
2458 struct wl_callback *callback;
2460 if (window->resize_needed)
2461 idle_resize(window);
2463 window_create_surface(window);
2464 widget_redraw(window->widget);
2465 window_flush(window);
2466 window->redraw_needed = 0;
2467 wl_list_init(&window->redraw_task.link);
2469 callback = wl_surface_frame(window->surface);
2470 wl_callback_add_listener(callback, &listener, window);
2474 window_schedule_redraw(struct window *window)
2476 window->redraw_needed = 1;
2477 if (!window->redraw_scheduled) {
2478 window->redraw_task.run = idle_redraw;
2479 display_defer(window->display, &window->redraw_task);
2480 window->redraw_scheduled = 1;
2485 window_set_custom(struct window *window)
2487 window->type = TYPE_CUSTOM;
2491 window_set_fullscreen(struct window *window, int fullscreen)
2493 if (!window->display->shell)
2496 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2500 window->type = TYPE_FULLSCREEN;
2501 window->saved_allocation = window->allocation;
2502 wl_shell_surface_set_fullscreen(window->shell_surface,
2503 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2506 window->type = TYPE_TOPLEVEL;
2507 wl_shell_surface_set_toplevel(window->shell_surface);
2508 window_schedule_resize(window,
2509 window->saved_allocation.width,
2510 window->saved_allocation.height);
2515 window_set_maximized(struct window *window, int maximized)
2517 if (!window->display->shell)
2520 if ((window->type == TYPE_MAXIMIZED) == maximized)
2523 if (window->type == TYPE_TOPLEVEL) {
2524 window->saved_allocation = window->allocation;
2525 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2526 window->type = TYPE_MAXIMIZED;
2528 wl_shell_surface_set_toplevel(window->shell_surface);
2529 window->type = TYPE_TOPLEVEL;
2530 window_schedule_resize(window,
2531 window->saved_allocation.width,
2532 window->saved_allocation.height);
2537 window_set_user_data(struct window *window, void *data)
2539 window->user_data = data;
2543 window_get_user_data(struct window *window)
2545 return window->user_data;
2549 window_set_key_handler(struct window *window,
2550 window_key_handler_t handler)
2552 window->key_handler = handler;
2556 window_set_keyboard_focus_handler(struct window *window,
2557 window_keyboard_focus_handler_t handler)
2559 window->keyboard_focus_handler = handler;
2563 window_set_data_handler(struct window *window, window_data_handler_t handler)
2565 window->data_handler = handler;
2569 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2571 window->drop_handler = handler;
2575 window_set_close_handler(struct window *window,
2576 window_close_handler_t handler)
2578 window->close_handler = handler;
2582 window_set_title(struct window *window, const char *title)
2584 free(window->title);
2585 window->title = strdup(title);
2586 if (window->shell_surface)
2587 wl_shell_surface_set_title(window->shell_surface, title);
2591 window_get_title(struct window *window)
2593 return window->title;
2597 window_damage(struct window *window, int32_t x, int32_t y,
2598 int32_t width, int32_t height)
2600 wl_surface_damage(window->surface, x, y, width, height);
2604 surface_enter(void *data,
2605 struct wl_surface *wl_surface, struct wl_output *wl_output)
2607 struct window *window = data;
2608 struct output *output;
2609 struct output *output_found = NULL;
2610 struct window_output *window_output;
2612 wl_list_for_each(output, &window->display->output_list, link) {
2613 if (output->output == wl_output) {
2614 output_found = output;
2622 window_output = malloc (sizeof *window_output);
2623 window_output->output = output_found;
2625 wl_list_insert (&window->window_output_list, &window_output->link);
2629 surface_leave(void *data,
2630 struct wl_surface *wl_surface, struct wl_output *output)
2632 struct window *window = data;
2633 struct window_output *window_output;
2634 struct window_output *window_output_found = NULL;
2636 wl_list_for_each(window_output, &window->window_output_list, link) {
2637 if (window_output->output->output == output) {
2638 window_output_found = window_output;
2643 if (window_output_found) {
2644 wl_list_remove(&window_output_found->link);
2645 free(window_output_found);
2649 static const struct wl_surface_listener surface_listener = {
2654 static struct window *
2655 window_create_internal(struct display *display, struct window *parent)
2657 struct window *window;
2659 window = malloc(sizeof *window);
2663 memset(window, 0, sizeof *window);
2664 window->display = display;
2665 window->parent = parent;
2666 window->surface = wl_compositor_create_surface(display->compositor);
2667 wl_surface_add_listener(window->surface, &surface_listener, window);
2668 if (display->shell) {
2669 window->shell_surface =
2670 wl_shell_get_shell_surface(display->shell,
2673 wl_shell_surface_set_title(window->shell_surface,
2676 window->allocation.x = 0;
2677 window->allocation.y = 0;
2678 window->allocation.width = 0;
2679 window->allocation.height = 0;
2680 window->saved_allocation = window->allocation;
2681 window->transparent = 1;
2682 window->type = TYPE_NONE;
2683 window->input_region = NULL;
2684 window->opaque_region = NULL;
2687 #ifdef HAVE_CAIRO_EGL
2688 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2690 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2693 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2695 wl_surface_set_user_data(window->surface, window);
2696 wl_list_insert(display->window_list.prev, &window->link);
2697 wl_list_init(&window->redraw_task.link);
2699 if (window->shell_surface) {
2700 wl_shell_surface_set_user_data(window->shell_surface, window);
2701 wl_shell_surface_add_listener(window->shell_surface,
2702 &shell_surface_listener, window);
2705 wl_list_init (&window->window_output_list);
2711 window_create(struct display *display)
2713 struct window *window;
2715 window = window_create_internal(display, NULL);
2723 window_create_transient(struct display *display, struct window *parent,
2724 int32_t x, int32_t y, uint32_t flags)
2726 struct window *window;
2728 window = window_create_internal(parent->display, parent);
2732 window->type = TYPE_TRANSIENT;
2737 wl_shell_surface_set_transient(window->shell_surface,
2738 window->parent->shell_surface,
2739 window->x, window->y, flags);
2745 menu_set_item(struct menu *menu, int sy)
2749 next = (sy - 8) / 20;
2750 if (menu->current != next) {
2751 menu->current = next;
2752 widget_schedule_redraw(menu->widget);
2757 menu_motion_handler(struct widget *widget,
2758 struct input *input, uint32_t time,
2759 float x, float y, void *data)
2761 struct menu *menu = data;
2763 if (widget == menu->widget)
2764 menu_set_item(data, y);
2766 return CURSOR_LEFT_PTR;
2770 menu_enter_handler(struct widget *widget,
2771 struct input *input, float x, float y, void *data)
2773 struct menu *menu = data;
2775 if (widget == menu->widget)
2776 menu_set_item(data, y);
2778 return CURSOR_LEFT_PTR;
2782 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2784 struct menu *menu = data;
2786 if (widget == menu->widget)
2787 menu_set_item(data, -200);
2791 menu_button_handler(struct widget *widget,
2792 struct input *input, uint32_t time,
2793 uint32_t button, uint32_t state, void *data)
2796 struct menu *menu = data;
2798 if (state == 0 && time - menu->time > 500) {
2799 /* Either relase after press-drag-release or
2800 * click-motion-click. */
2801 menu->func(menu->window->parent,
2802 menu->current, menu->window->parent->user_data);
2803 input_ungrab(input);
2809 menu_redraw_handler(struct widget *widget, void *data)
2812 const int32_t r = 3, margin = 3;
2813 struct menu *menu = data;
2814 int32_t width, height, i;
2815 struct window *window = widget->window;
2817 cr = cairo_create(window->cairo_surface);
2818 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2819 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2822 width = window->allocation.width;
2823 height = window->allocation.height;
2824 rounded_rect(cr, 0, 0, width, height, r);
2826 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2827 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2830 for (i = 0; i < menu->count; i++) {
2831 if (i == menu->current) {
2832 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2833 cairo_rectangle(cr, margin, i * 20 + margin,
2834 width - 2 * margin, 20);
2836 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2837 cairo_move_to(cr, 10, i * 20 + 16);
2838 cairo_show_text(cr, menu->entries[i]);
2840 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2841 cairo_move_to(cr, 10, i * 20 + 16);
2842 cairo_show_text(cr, menu->entries[i]);
2850 window_show_menu(struct display *display,
2851 struct input *input, uint32_t time, struct window *parent,
2852 int32_t x, int32_t y,
2853 menu_func_t func, const char **entries, int count)
2855 struct window *window;
2857 const int32_t margin = 3;
2859 menu = malloc(sizeof *menu);
2863 window = window_create_internal(parent->display, parent);
2867 menu->window = window;
2868 menu->widget = window_add_widget(menu->window, menu);
2869 menu->entries = entries;
2870 menu->count = count;
2874 menu->input = input;
2875 window->type = TYPE_MENU;
2879 input_ungrab(input);
2880 wl_shell_surface_set_popup(window->shell_surface, input->seat,
2881 display_get_serial(window->display),
2882 window->parent->shell_surface,
2883 window->x, window->y, 0);
2885 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
2886 widget_set_enter_handler(menu->widget, menu_enter_handler);
2887 widget_set_leave_handler(menu->widget, menu_leave_handler);
2888 widget_set_motion_handler(menu->widget, menu_motion_handler);
2889 widget_set_button_handler(menu->widget, menu_button_handler);
2891 input_grab(input, menu->widget, 0);
2892 window_schedule_resize(window, 200, count * 20 + margin * 2);
2896 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2898 window->buffer_type = type;
2903 display_handle_geometry(void *data,
2904 struct wl_output *wl_output,
2907 int physical_height,
2912 struct output *output = data;
2914 output->allocation.x = x;
2915 output->allocation.y = y;
2919 display_handle_mode(void *data,
2920 struct wl_output *wl_output,
2926 struct output *output = data;
2927 struct display *display = output->display;
2929 if (flags & WL_OUTPUT_MODE_CURRENT) {
2930 output->allocation.width = width;
2931 output->allocation.height = height;
2932 if (display->output_configure_handler)
2933 (*display->output_configure_handler)(
2934 output, display->user_data);
2938 static const struct wl_output_listener output_listener = {
2939 display_handle_geometry,
2944 display_add_output(struct display *d, uint32_t id)
2946 struct output *output;
2948 output = malloc(sizeof *output);
2952 memset(output, 0, sizeof *output);
2953 output->display = d;
2955 wl_display_bind(d->display, id, &wl_output_interface);
2956 wl_list_insert(d->output_list.prev, &output->link);
2958 wl_output_add_listener(output->output, &output_listener, output);
2962 output_destroy(struct output *output)
2964 if (output->destroy_handler)
2965 (*output->destroy_handler)(output, output->user_data);
2967 wl_output_destroy(output->output);
2968 wl_list_remove(&output->link);
2973 display_set_output_configure_handler(struct display *display,
2974 display_output_handler_t handler)
2976 struct output *output;
2978 display->output_configure_handler = handler;
2982 wl_list_for_each(output, &display->output_list, link)
2983 (*display->output_configure_handler)(output,
2984 display->user_data);
2988 output_set_user_data(struct output *output, void *data)
2990 output->user_data = data;
2994 output_get_user_data(struct output *output)
2996 return output->user_data;
3000 output_set_destroy_handler(struct output *output,
3001 display_output_handler_t handler)
3003 output->destroy_handler = handler;
3004 /* FIXME: implement this, once we have way to remove outputs */
3008 output_get_allocation(struct output *output, struct rectangle *allocation)
3010 *allocation = output->allocation;
3014 output_get_wl_output(struct output *output)
3016 return output->output;
3020 display_add_input(struct display *d, uint32_t id)
3022 struct input *input;
3024 input = malloc(sizeof *input);
3028 memset(input, 0, sizeof *input);
3030 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3031 input->pointer_focus = NULL;
3032 input->keyboard_focus = NULL;
3033 wl_list_insert(d->input_list.prev, &input->link);
3035 wl_seat_add_listener(input->seat, &seat_listener, input);
3036 wl_seat_set_user_data(input->seat, input);
3038 input->data_device =
3039 wl_data_device_manager_get_data_device(d->data_device_manager,
3041 wl_data_device_add_listener(input->data_device, &data_device_listener,
3046 input_destroy(struct input *input)
3048 input_remove_keyboard_focus(input);
3049 input_remove_pointer_focus(input);
3051 if (input->drag_offer)
3052 data_offer_destroy(input->drag_offer);
3054 if (input->selection_offer)
3055 data_offer_destroy(input->selection_offer);
3057 wl_data_device_destroy(input->data_device);
3058 wl_list_remove(&input->link);
3059 wl_seat_destroy(input->seat);
3064 display_handle_global(struct wl_display *display, uint32_t id,
3065 const char *interface, uint32_t version, void *data)
3067 struct display *d = data;
3069 if (strcmp(interface, "wl_compositor") == 0) {
3071 wl_display_bind(display, id, &wl_compositor_interface);
3072 } else if (strcmp(interface, "wl_output") == 0) {
3073 display_add_output(d, id);
3074 } else if (strcmp(interface, "wl_seat") == 0) {
3075 display_add_input(d, id);
3076 } else if (strcmp(interface, "wl_shell") == 0) {
3077 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3078 } else if (strcmp(interface, "wl_shm") == 0) {
3079 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3080 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3081 d->data_device_manager =
3082 wl_display_bind(display, id,
3083 &wl_data_device_manager_interface);
3088 init_xkb(struct display *d)
3090 d->xkb.names.rules = "evdev";
3091 d->xkb.names.model = "pc105";
3092 d->xkb.names.layout = (char *) option_xkb_layout;
3093 d->xkb.names.variant = (char *) option_xkb_variant;
3094 d->xkb.names.options = (char *) option_xkb_options;
3096 d->xkb.context = xkb_context_new(0);
3097 if (!d->xkb.context) {
3098 fprintf(stderr, "Failed to create XKB context\n");
3102 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
3103 if (!d->xkb.keymap) {
3104 fprintf(stderr, "Failed to compile keymap\n");
3108 d->xkb.state = xkb_state_new(d->xkb.keymap);
3109 if (!d->xkb.state) {
3110 fprintf(stderr, "Failed to create XKB state\n");
3114 d->xkb.control_mask =
3115 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3117 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3119 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3124 fini_xkb(struct display *display)
3126 xkb_state_unref(display->xkb.state);
3127 xkb_map_unref(display->xkb.keymap);
3128 xkb_context_unref(display->xkb.context);
3131 #ifdef HAVE_CAIRO_EGL
3133 init_egl(struct display *d)
3135 EGLint major, minor;
3138 #ifdef USE_CAIRO_GLESV2
3139 # define GL_BIT EGL_OPENGL_ES2_BIT
3141 # define GL_BIT EGL_OPENGL_BIT
3144 static const EGLint argb_cfg_attribs[] = {
3145 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3151 EGL_RENDERABLE_TYPE, GL_BIT,
3155 #ifdef USE_CAIRO_GLESV2
3156 static const EGLint context_attribs[] = {
3157 EGL_CONTEXT_CLIENT_VERSION, 2,
3160 EGLint api = EGL_OPENGL_ES_API;
3162 EGLint *context_attribs = NULL;
3163 EGLint api = EGL_OPENGL_API;
3166 d->dpy = eglGetDisplay(d->display);
3167 if (!eglInitialize(d->dpy, &major, &minor)) {
3168 fprintf(stderr, "failed to initialize display\n");
3172 if (!eglBindAPI(api)) {
3173 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3177 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3178 &d->argb_config, 1, &n) || n != 1) {
3179 fprintf(stderr, "failed to choose argb config\n");
3183 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3184 EGL_NO_CONTEXT, context_attribs);
3185 if (d->argb_ctx == NULL) {
3186 fprintf(stderr, "failed to create context\n");
3190 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3191 fprintf(stderr, "failed to make context current\n");
3195 #ifdef HAVE_CAIRO_EGL
3196 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3197 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3198 fprintf(stderr, "failed to get cairo egl argb device\n");
3207 fini_egl(struct display *display)
3209 #ifdef HAVE_CAIRO_EGL
3210 cairo_device_destroy(display->argb_device);
3213 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3216 eglTerminate(display->dpy);
3222 event_mask_update(uint32_t mask, void *data)
3224 struct display *d = data;
3232 handle_display_data(struct task *task, uint32_t events)
3234 struct display *display =
3235 container_of(task, struct display, display_task);
3237 wl_display_iterate(display->display, display->mask);
3241 display_create(int argc, char *argv[])
3245 argc = parse_options(xkb_options,
3246 ARRAY_LENGTH(xkb_options), argc, argv);
3248 d = malloc(sizeof *d);
3252 memset(d, 0, sizeof *d);
3254 d->display = wl_display_connect(NULL);
3255 if (d->display == NULL) {
3256 fprintf(stderr, "failed to create display: %m\n");
3260 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3261 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3262 d->display_task.run = handle_display_data;
3263 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3265 wl_list_init(&d->deferred_list);
3266 wl_list_init(&d->input_list);
3267 wl_list_init(&d->output_list);
3269 /* Set up listener so we'll catch all events. */
3270 wl_display_add_global_listener(d->display,
3271 display_handle_global, d);
3273 /* Process connection events. */
3274 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3275 #ifdef HAVE_CAIRO_EGL
3276 if (init_egl(d) < 0)
3280 d->image_target_texture_2d =
3281 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3282 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3283 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3287 d->theme = theme_create();
3289 wl_list_init(&d->window_list);
3297 display_destroy_outputs(struct display *display)
3300 struct output *output;
3302 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3303 output_destroy(output);
3307 display_destroy_inputs(struct display *display)
3310 struct input *input;
3312 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3313 input_destroy(input);
3317 display_destroy(struct display *display)
3319 if (!wl_list_empty(&display->window_list))
3320 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3322 if (!wl_list_empty(&display->deferred_list))
3323 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3325 display_destroy_outputs(display);
3326 display_destroy_inputs(display);
3330 theme_destroy(display->theme);
3331 destroy_cursors(display);
3333 #ifdef HAVE_CAIRO_EGL
3338 wl_shell_destroy(display->shell);
3341 wl_shm_destroy(display->shm);
3343 if (display->data_device_manager)
3344 wl_data_device_manager_destroy(display->data_device_manager);
3346 wl_compositor_destroy(display->compositor);
3348 close(display->epoll_fd);
3350 wl_display_flush(display->display);
3351 wl_display_disconnect(display->display);
3356 display_set_user_data(struct display *display, void *data)
3358 display->user_data = data;
3362 display_get_user_data(struct display *display)
3364 return display->user_data;
3368 display_get_display(struct display *display)
3370 return display->display;
3374 display_get_output(struct display *display)
3376 return container_of(display->output_list.next, struct output, link);
3379 struct wl_compositor *
3380 display_get_compositor(struct display *display)
3382 return display->compositor;
3386 display_get_serial(struct display *display)
3388 return display->serial;
3392 display_get_egl_display(struct display *d)
3397 struct wl_data_source *
3398 display_create_data_source(struct display *display)
3400 return wl_data_device_manager_create_data_source(display->data_device_manager);
3404 display_get_argb_egl_config(struct display *d)
3406 return d->argb_config;
3410 display_get_shell(struct display *display)
3412 return display->shell;
3416 display_acquire_window_surface(struct display *display,
3417 struct window *window,
3420 #ifdef HAVE_CAIRO_EGL
3421 struct egl_window_surface_data *data;
3422 cairo_device_t *device;
3424 if (!window->cairo_surface)
3426 device = cairo_surface_get_device(window->cairo_surface);
3431 if (device == display->argb_device)
3432 ctx = display->argb_ctx;
3437 data = cairo_surface_get_user_data(window->cairo_surface,
3440 cairo_device_flush(device);
3441 cairo_device_acquire(device);
3442 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3443 fprintf(stderr, "failed to make surface current\n");
3452 display_release_window_surface(struct display *display,
3453 struct window *window)
3455 #ifdef HAVE_CAIRO_EGL
3456 cairo_device_t *device;
3458 device = cairo_surface_get_device(window->cairo_surface);
3462 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3463 fprintf(stderr, "failed to make context current\n");
3464 cairo_device_release(device);
3469 display_defer(struct display *display, struct task *task)
3471 wl_list_insert(&display->deferred_list, &task->link);
3475 display_watch_fd(struct display *display,
3476 int fd, uint32_t events, struct task *task)
3478 struct epoll_event ep;
3482 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3486 display_run(struct display *display)
3489 struct epoll_event ep[16];
3492 display->running = 1;
3494 wl_display_flush(display->display);
3496 while (!wl_list_empty(&display->deferred_list)) {
3497 task = container_of(display->deferred_list.next,
3499 wl_list_remove(&task->link);
3503 if (!display->running)
3506 wl_display_flush(display->display);
3508 count = epoll_wait(display->epoll_fd,
3509 ep, ARRAY_LENGTH(ep), -1);
3510 for (i = 0; i < count; i++) {
3511 task = ep[i].data.ptr;
3512 task->run(task, ep[i].events);
3518 display_exit(struct display *display)
3520 display->running = 0;