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"
64 #include "text-cursor-position-client-protocol.h"
65 #include "../shared/os-compatibility.h"
72 struct wl_display *display;
73 struct wl_compositor *compositor;
74 struct wl_shell *shell;
76 struct wl_data_device_manager *data_device_manager;
77 struct text_cursor_position *text_cursor_position;
79 EGLConfig argb_config;
81 cairo_device_t *argb_device;
86 struct task display_task;
89 struct wl_list deferred_list;
93 struct wl_list window_list;
94 struct wl_list input_list;
95 struct wl_list output_list;
99 struct wl_cursor_theme *cursor_theme;
100 struct wl_cursor **cursors;
102 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
103 PFNEGLCREATEIMAGEKHRPROC create_image;
104 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
106 display_output_handler_t output_configure_handler;
110 struct xkb_context *xkb_context;
123 struct window_output {
124 struct output *output;
129 struct display *display;
130 struct window *parent;
131 struct wl_list window_output_list;
132 struct wl_surface *surface;
133 struct wl_shell_surface *shell_surface;
134 struct wl_region *input_region;
135 struct wl_region *opaque_region;
137 struct rectangle allocation, saved_allocation, server_allocation;
138 struct rectangle pending_allocation;
141 int redraw_scheduled;
143 struct task redraw_task;
147 struct input *keyboard_device;
148 enum window_buffer_type buffer_type;
150 cairo_surface_t *cairo_surface;
152 struct shm_pool *pool;
154 window_key_handler_t key_handler;
155 window_keyboard_focus_handler_t keyboard_focus_handler;
156 window_data_handler_t data_handler;
157 window_drop_handler_t drop_handler;
158 window_close_handler_t close_handler;
161 struct widget *widget;
168 struct window *window;
169 struct tooltip *tooltip;
170 struct wl_list child_list;
172 struct rectangle allocation;
173 widget_resize_handler_t resize_handler;
174 widget_redraw_handler_t redraw_handler;
175 widget_enter_handler_t enter_handler;
176 widget_leave_handler_t leave_handler;
177 widget_motion_handler_t motion_handler;
178 widget_button_handler_t button_handler;
185 struct display *display;
186 struct wl_seat *seat;
187 struct wl_pointer *pointer;
188 struct wl_keyboard *keyboard;
189 struct window *pointer_focus;
190 struct window *keyboard_focus;
192 struct wl_surface *pointer_surface;
194 uint32_t pointer_enter_serial;
198 struct widget *focus_widget;
200 uint32_t grab_button;
202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
207 struct xkb_keymap *keymap;
208 struct xkb_state *state;
209 xkb_mod_mask_t control_mask;
210 xkb_mod_mask_t alt_mask;
211 xkb_mod_mask_t shift_mask;
216 struct display *display;
217 struct wl_output *output;
218 struct rectangle allocation;
221 display_output_handler_t destroy_handler;
225 enum frame_button_action {
226 FRAME_BUTTON_NULL = 0,
227 FRAME_BUTTON_ICON = 1,
228 FRAME_BUTTON_CLOSE = 2,
229 FRAME_BUTTON_MINIMIZE = 3,
230 FRAME_BUTTON_MAXIMIZE = 4,
233 enum frame_button_pointer {
234 FRAME_BUTTON_DEFAULT = 0,
235 FRAME_BUTTON_OVER = 1,
236 FRAME_BUTTON_ACTIVE = 2,
239 enum frame_button_align {
240 FRAME_BUTTON_RIGHT = 0,
241 FRAME_BUTTON_LEFT = 1,
244 enum frame_button_decoration {
245 FRAME_BUTTON_NONE = 0,
246 FRAME_BUTTON_FANCY = 1,
249 struct frame_button {
250 struct widget *widget;
252 cairo_surface_t *icon;
253 enum frame_button_action type;
254 enum frame_button_pointer state;
255 struct wl_list link; /* buttons_list */
256 enum frame_button_align align;
257 enum frame_button_decoration decoration;
261 struct widget *widget;
262 struct widget *child;
263 struct wl_list buttons_list;
267 struct window *window;
268 struct widget *widget;
270 const char **entries;
278 struct widget *parent;
279 struct window *window;
280 struct widget *widget;
282 struct task tooltip_task;
288 struct wl_shm_pool *pool;
295 CURSOR_DEFAULT = 100,
299 enum window_location {
301 WINDOW_RESIZING_TOP = 1,
302 WINDOW_RESIZING_BOTTOM = 2,
303 WINDOW_RESIZING_LEFT = 4,
304 WINDOW_RESIZING_TOP_LEFT = 5,
305 WINDOW_RESIZING_BOTTOM_LEFT = 6,
306 WINDOW_RESIZING_RIGHT = 8,
307 WINDOW_RESIZING_TOP_RIGHT = 9,
308 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
309 WINDOW_RESIZING_MASK = 15,
310 WINDOW_EXTERIOR = 16,
311 WINDOW_TITLEBAR = 17,
312 WINDOW_CLIENT_AREA = 18,
315 const char *option_xkb_layout = "us";
316 const char *option_xkb_variant = "";
317 const char *option_xkb_options = "";
319 static const struct weston_option xkb_options[] = {
320 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
321 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
322 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
325 static const cairo_user_data_key_t surface_data_key;
326 struct surface_data {
327 struct wl_buffer *buffer;
330 #define MULT(_d,c,a,t) \
331 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333 #ifdef HAVE_CAIRO_EGL
335 struct egl_window_surface_data {
336 struct display *display;
337 struct wl_surface *surface;
338 struct wl_egl_window *window;
343 egl_window_surface_data_destroy(void *p)
345 struct egl_window_surface_data *data = p;
346 struct display *d = data->display;
348 eglDestroySurface(d->dpy, data->surf);
349 wl_egl_window_destroy(data->window);
350 data->surface = NULL;
355 static cairo_surface_t *
356 display_create_egl_window_surface(struct display *display,
357 struct wl_surface *surface,
359 struct rectangle *rectangle)
361 cairo_surface_t *cairo_surface;
362 struct egl_window_surface_data *data;
364 cairo_device_t *device;
366 data = malloc(sizeof *data);
370 data->display = display;
371 data->surface = surface;
373 config = display->argb_config;
374 device = display->argb_device;
376 data->window = wl_egl_window_create(surface,
380 data->surf = eglCreateWindowSurface(display->dpy, config,
383 cairo_surface = cairo_gl_surface_create_for_egl(device,
388 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
389 data, egl_window_surface_data_destroy);
391 return cairo_surface;
397 display_get_buffer_for_surface(struct display *display,
398 cairo_surface_t *surface)
400 struct surface_data *data;
402 data = cairo_surface_get_user_data (surface, &surface_data_key);
407 struct shm_surface_data {
408 struct surface_data data;
409 struct shm_pool *pool;
413 shm_pool_destroy(struct shm_pool *pool);
416 shm_surface_data_destroy(void *p)
418 struct shm_surface_data *data = p;
420 wl_buffer_destroy(data->data.buffer);
422 shm_pool_destroy(data->pool);
425 static struct wl_shm_pool *
426 make_shm_pool(struct display *display, int size, void **data)
428 struct wl_shm_pool *pool;
431 fd = os_create_anonymous_file(size);
433 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
438 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
439 if (*data == MAP_FAILED) {
440 fprintf(stderr, "mmap failed: %m\n");
445 pool = wl_shm_create_pool(display->shm, fd, size);
452 static struct shm_pool *
453 shm_pool_create(struct display *display, size_t size)
455 struct shm_pool *pool = malloc(sizeof *pool);
460 pool->pool = make_shm_pool(display, size, &pool->data);
473 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
475 if (pool->used + size > pool->size)
478 *offset = pool->used;
481 return (char *) pool->data + *offset;
484 /* destroy the pool. this does not unmap the memory though */
486 shm_pool_destroy(struct shm_pool *pool)
488 munmap(pool->data, pool->size);
489 wl_shm_pool_destroy(pool->pool);
493 /* Start allocating from the beginning of the pool again */
495 shm_pool_reset(struct shm_pool *pool)
501 data_length_for_shm_surface(struct rectangle *rect)
505 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
507 return stride * rect->height;
510 static cairo_surface_t *
511 display_create_shm_surface_from_pool(struct display *display,
512 struct rectangle *rectangle,
513 uint32_t flags, struct shm_pool *pool)
515 struct shm_surface_data *data;
517 cairo_surface_t *surface;
518 int stride, length, offset;
521 data = malloc(sizeof *data);
525 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
527 length = stride * rectangle->height;
529 map = shm_pool_allocate(pool, length, &offset);
536 surface = cairo_image_surface_create_for_data (map,
542 cairo_surface_set_user_data (surface, &surface_data_key,
543 data, shm_surface_data_destroy);
545 if (flags & SURFACE_OPAQUE)
546 format = WL_SHM_FORMAT_XRGB8888;
548 format = WL_SHM_FORMAT_ARGB8888;
550 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
558 static cairo_surface_t *
559 display_create_shm_surface(struct display *display,
560 struct rectangle *rectangle, uint32_t flags,
561 struct window *window)
563 struct shm_surface_data *data;
564 struct shm_pool *pool;
565 cairo_surface_t *surface;
567 if (window && window->pool) {
568 shm_pool_reset(window->pool);
569 surface = display_create_shm_surface_from_pool(display,
577 pool = shm_pool_create(display,
578 data_length_for_shm_surface(rectangle));
583 display_create_shm_surface_from_pool(display, rectangle,
587 shm_pool_destroy(pool);
591 /* make sure we destroy the pool when the surface is destroyed */
592 data = cairo_surface_get_user_data(surface, &surface_data_key);
599 check_size(struct rectangle *rect)
601 if (rect->width && rect->height)
604 fprintf(stderr, "tried to create surface of "
605 "width: %d, height: %d\n", rect->width, rect->height);
610 display_create_surface(struct display *display,
611 struct wl_surface *surface,
612 struct rectangle *rectangle,
615 if (check_size(rectangle) < 0)
617 #ifdef HAVE_CAIRO_EGL
618 if (display->dpy && !(flags & SURFACE_SHM))
619 return display_create_egl_window_surface(display,
624 return display_create_shm_surface(display, rectangle, flags, NULL);
627 static const char *cursors[] = {
628 "bottom_left_corner",
629 "bottom_right_corner",
644 create_cursors(struct display *display)
648 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
650 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
652 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
653 display->cursors[i] =
654 wl_cursor_theme_get_cursor(display->cursor_theme,
659 destroy_cursors(struct display *display)
661 wl_cursor_theme_destroy(display->cursor_theme);
662 free(display->cursors);
665 struct wl_cursor_image *
666 display_get_pointer_image(struct display *display, int pointer)
668 struct wl_cursor *cursor =
669 wl_cursor_theme_get_cursor(display->cursor_theme,
672 return cursor ? cursor->images[0] : NULL;
676 window_get_resize_dx_dy(struct window *window, int *x, int *y)
678 if (window->resize_edges & WINDOW_RESIZING_LEFT)
679 *x = window->server_allocation.width - window->allocation.width;
683 if (window->resize_edges & WINDOW_RESIZING_TOP)
684 *y = window->server_allocation.height -
685 window->allocation.height;
689 window->resize_edges = 0;
693 window_attach_surface(struct window *window)
695 struct display *display = window->display;
696 struct wl_buffer *buffer;
697 #ifdef HAVE_CAIRO_EGL
698 struct egl_window_surface_data *data;
702 if (window->type == TYPE_NONE) {
703 window->type = TYPE_TOPLEVEL;
705 wl_shell_surface_set_toplevel(window->shell_surface);
708 switch (window->buffer_type) {
709 #ifdef HAVE_CAIRO_EGL
710 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
711 data = cairo_surface_get_user_data(window->cairo_surface,
714 cairo_gl_surface_swapbuffers(window->cairo_surface);
715 wl_egl_window_get_attached_size(data->window,
716 &window->server_allocation.width,
717 &window->server_allocation.height);
720 case WINDOW_BUFFER_TYPE_SHM:
722 display_get_buffer_for_surface(display,
723 window->cairo_surface);
725 window_get_resize_dx_dy(window, &x, &y);
726 wl_surface_attach(window->surface, buffer, x, y);
727 wl_surface_damage(window->surface, 0, 0,
728 window->allocation.width,
729 window->allocation.height);
730 window->server_allocation = window->allocation;
731 cairo_surface_destroy(window->cairo_surface);
732 window->cairo_surface = NULL;
738 if (window->input_region) {
739 wl_surface_set_input_region(window->surface,
740 window->input_region);
741 wl_region_destroy(window->input_region);
742 window->input_region = NULL;
745 if (window->opaque_region) {
746 wl_surface_set_opaque_region(window->surface,
747 window->opaque_region);
748 wl_region_destroy(window->opaque_region);
749 window->opaque_region = NULL;
754 window_flush(struct window *window)
756 if (window->cairo_surface)
757 window_attach_surface(window);
761 window_set_surface(struct window *window, cairo_surface_t *surface)
763 cairo_surface_reference(surface);
765 if (window->cairo_surface != NULL)
766 cairo_surface_destroy(window->cairo_surface);
768 window->cairo_surface = surface;
771 #ifdef HAVE_CAIRO_EGL
773 window_resize_cairo_window_surface(struct window *window)
775 struct egl_window_surface_data *data;
778 data = cairo_surface_get_user_data(window->cairo_surface,
781 window_get_resize_dx_dy(window, &x, &y),
782 wl_egl_window_resize(data->window,
783 window->allocation.width,
784 window->allocation.height,
787 cairo_gl_surface_set_size(window->cairo_surface,
788 window->allocation.width,
789 window->allocation.height);
794 window_get_display(struct window *window)
796 return window->display;
800 window_create_surface(struct window *window)
802 cairo_surface_t *surface;
805 if (!window->transparent)
806 flags = SURFACE_OPAQUE;
808 switch (window->buffer_type) {
809 #ifdef HAVE_CAIRO_EGL
810 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
811 if (window->cairo_surface) {
812 window_resize_cairo_window_surface(window);
815 surface = display_create_surface(window->display,
817 &window->allocation, flags);
820 case WINDOW_BUFFER_TYPE_SHM:
821 surface = display_create_shm_surface(window->display,
830 window_set_surface(window, surface);
831 cairo_surface_destroy(surface);
834 static void frame_destroy(struct frame *frame);
837 window_destroy(struct window *window)
839 struct display *display = window->display;
841 struct window_output *window_output;
842 struct window_output *window_output_tmp;
844 if (window->redraw_scheduled)
845 wl_list_remove(&window->redraw_task.link);
847 wl_list_for_each(input, &display->input_list, link) {
848 if (input->pointer_focus == window)
849 input->pointer_focus = NULL;
850 if (input->keyboard_focus == window)
851 input->keyboard_focus = NULL;
852 if (input->focus_widget &&
853 input->focus_widget->window == window)
854 input->focus_widget = NULL;
857 wl_list_for_each_safe(window_output, window_output_tmp,
858 &window->window_output_list, link) {
859 free (window_output);
862 if (window->input_region)
863 wl_region_destroy(window->input_region);
864 if (window->opaque_region)
865 wl_region_destroy(window->opaque_region);
868 frame_destroy(window->frame);
870 if (window->shell_surface)
871 wl_shell_surface_destroy(window->shell_surface);
872 wl_surface_destroy(window->surface);
873 wl_list_remove(&window->link);
875 if (window->cairo_surface != NULL)
876 cairo_surface_destroy(window->cairo_surface);
882 static struct widget *
883 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
885 struct widget *child, *target;
887 wl_list_for_each(child, &widget->child_list, link) {
888 target = widget_find_widget(child, x, y);
893 if (widget->allocation.x <= x &&
894 x < widget->allocation.x + widget->allocation.width &&
895 widget->allocation.y <= y &&
896 y < widget->allocation.y + widget->allocation.height) {
903 static struct widget *
904 widget_create(struct window *window, void *data)
906 struct widget *widget;
908 widget = malloc(sizeof *widget);
909 memset(widget, 0, sizeof *widget);
910 widget->window = window;
911 widget->user_data = data;
912 widget->allocation = window->allocation;
913 wl_list_init(&widget->child_list);
915 widget->tooltip = NULL;
916 widget->tooltip_count = 0;
922 window_add_widget(struct window *window, void *data)
924 window->widget = widget_create(window, data);
925 wl_list_init(&window->widget->link);
927 return window->widget;
931 widget_add_widget(struct widget *parent, void *data)
933 struct widget *widget;
935 widget = widget_create(parent->window, data);
936 wl_list_insert(parent->child_list.prev, &widget->link);
942 widget_destroy(struct widget *widget)
944 struct display *display = widget->window->display;
947 if (widget->tooltip) {
948 free(widget->tooltip);
949 widget->tooltip = NULL;
952 wl_list_for_each(input, &display->input_list, link) {
953 if (input->focus_widget == widget)
954 input->focus_widget = NULL;
957 wl_list_remove(&widget->link);
962 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
964 *allocation = widget->allocation;
968 widget_set_size(struct widget *widget, int32_t width, int32_t height)
970 widget->allocation.width = width;
971 widget->allocation.height = height;
975 widget_set_allocation(struct widget *widget,
976 int32_t x, int32_t y, int32_t width, int32_t height)
978 widget->allocation.x = x;
979 widget->allocation.y = y;
980 widget_set_size(widget, width, height);
984 widget_set_transparent(struct widget *widget, int transparent)
986 widget->opaque = !transparent;
990 widget_get_user_data(struct widget *widget)
992 return widget->user_data;
996 widget_set_resize_handler(struct widget *widget,
997 widget_resize_handler_t handler)
999 widget->resize_handler = handler;
1003 widget_set_redraw_handler(struct widget *widget,
1004 widget_redraw_handler_t handler)
1006 widget->redraw_handler = handler;
1010 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1012 widget->enter_handler = handler;
1016 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1018 widget->leave_handler = handler;
1022 widget_set_motion_handler(struct widget *widget,
1023 widget_motion_handler_t handler)
1025 widget->motion_handler = handler;
1029 widget_set_button_handler(struct widget *widget,
1030 widget_button_handler_t handler)
1032 widget->button_handler = handler;
1036 widget_schedule_redraw(struct widget *widget)
1038 window_schedule_redraw(widget->window);
1042 window_get_surface(struct window *window)
1044 return cairo_surface_reference(window->cairo_surface);
1048 window_get_wl_surface(struct window *window)
1050 return window->surface;
1053 struct wl_shell_surface *
1054 window_get_wl_shell_surface(struct window *window)
1056 return window->shell_surface;
1060 tooltip_redraw_handler(struct widget *widget, void *data)
1063 const int32_t r = 3;
1064 struct tooltip *tooltip = data;
1065 int32_t width, height;
1066 struct window *window = widget->window;
1068 cr = cairo_create(window->cairo_surface);
1069 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1070 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1073 width = window->allocation.width;
1074 height = window->allocation.height;
1075 rounded_rect(cr, 0, 0, width, height, r);
1077 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1078 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1081 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1082 cairo_move_to(cr, 10, 16);
1083 cairo_show_text(cr, tooltip->entry);
1087 static cairo_text_extents_t
1088 get_text_extents(struct tooltip *tooltip)
1090 struct window *window;
1092 cairo_text_extents_t extents;
1094 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1096 window = tooltip->widget->window->parent;
1097 cr = cairo_create(window->cairo_surface);
1098 cairo_text_extents(cr, tooltip->entry, &extents);
1105 window_create_tooltip(struct tooltip *tooltip)
1107 struct widget *parent = tooltip->parent;
1108 struct display *display = parent->window->display;
1109 struct window *window;
1110 const int offset_y = 27;
1111 const int margin = 3;
1112 cairo_text_extents_t extents;
1114 if (tooltip->widget)
1117 window = window_create_transient(display, parent->window, tooltip->x,
1118 tooltip->y + offset_y,
1119 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1123 tooltip->window = window;
1124 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1126 extents = get_text_extents(tooltip);
1127 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1128 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1134 widget_destroy_tooltip(struct widget *parent)
1136 struct tooltip *tooltip = parent->tooltip;
1138 parent->tooltip_count = 0;
1142 if (tooltip->widget) {
1143 widget_destroy(tooltip->widget);
1144 window_destroy(tooltip->window);
1145 tooltip->widget = NULL;
1146 tooltip->window = NULL;
1149 close(tooltip->tooltip_fd);
1150 free(tooltip->entry);
1152 parent->tooltip = NULL;
1156 tooltip_func(struct task *task, uint32_t events)
1158 struct tooltip *tooltip =
1159 container_of(task, struct tooltip, tooltip_task);
1162 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1163 window_create_tooltip(tooltip);
1166 #define TOOLTIP_TIMEOUT 500
1168 tooltip_timer_reset(struct tooltip *tooltip)
1170 struct itimerspec its;
1172 its.it_interval.tv_sec = 0;
1173 its.it_interval.tv_nsec = 0;
1174 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1175 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1176 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1177 fprintf(stderr, "could not set timerfd\n: %m");
1185 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1187 struct tooltip *tooltip = parent->tooltip;
1189 parent->tooltip_count++;
1193 tooltip_timer_reset(tooltip);
1197 /* the handler might be triggered too fast via input device motion, so
1198 * we need this check here to make sure tooltip is fully initialized */
1199 if (parent->tooltip_count > 1)
1202 tooltip = malloc(sizeof *tooltip);
1206 parent->tooltip = tooltip;
1207 tooltip->parent = parent;
1208 tooltip->widget = NULL;
1209 tooltip->window = NULL;
1212 tooltip->entry = strdup(entry);
1213 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1214 if (tooltip->tooltip_fd < 0) {
1215 fprintf(stderr, "could not create timerfd\n: %m");
1219 tooltip->tooltip_task.run = tooltip_func;
1220 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1221 EPOLLIN, &tooltip->tooltip_task);
1222 tooltip_timer_reset(tooltip);
1228 frame_resize_handler(struct widget *widget,
1229 int32_t width, int32_t height, void *data)
1231 struct frame *frame = data;
1232 struct widget *child = frame->child;
1233 struct rectangle allocation;
1234 struct display *display = widget->window->display;
1235 struct frame_button * button;
1236 struct theme *t = display->theme;
1237 int x_l, x_r, y, w, h;
1238 int decoration_width, decoration_height;
1241 if (widget->window->type != TYPE_FULLSCREEN) {
1242 decoration_width = (t->width + t->margin) * 2;
1243 decoration_height = t->width +
1244 t->titlebar_height + t->margin * 2;
1246 allocation.x = t->width + t->margin;
1247 allocation.y = t->titlebar_height + t->margin;
1248 allocation.width = width - decoration_width;
1249 allocation.height = height - decoration_height;
1251 opaque_margin = t->margin + t->frame_radius;
1253 wl_list_for_each(button, &frame->buttons_list, link)
1254 button->widget->opaque = 0;
1256 decoration_width = 0;
1257 decoration_height = 0;
1261 allocation.width = width;
1262 allocation.height = height;
1265 wl_list_for_each(button, &frame->buttons_list, link)
1266 button->widget->opaque = 1;
1269 widget_set_allocation(child, allocation.x, allocation.y,
1270 allocation.width, allocation.height);
1272 if (child->resize_handler)
1273 child->resize_handler(child,
1278 width = child->allocation.width + decoration_width;
1279 height = child->allocation.height + decoration_height;
1281 widget->window->input_region =
1282 wl_compositor_create_region(display->compositor);
1283 wl_region_add(widget->window->input_region,
1284 t->margin, t->margin,
1285 width - 2 * t->margin,
1286 height - 2 * t->margin);
1288 widget_set_allocation(widget, 0, 0, width, height);
1290 if (child->opaque) {
1291 widget->window->opaque_region =
1292 wl_compositor_create_region(display->compositor);
1293 wl_region_add(widget->window->opaque_region,
1294 opaque_margin, opaque_margin,
1295 widget->allocation.width - 2 * opaque_margin,
1296 widget->allocation.height - 2 * opaque_margin);
1299 /* frame internal buttons */
1300 x_r = frame->widget->allocation.width - t->width - t->margin;
1301 x_l = t->width + t->margin;
1302 y = t->width + t->margin;
1303 wl_list_for_each(button, &frame->buttons_list, link) {
1304 const int button_padding = 4;
1305 w = cairo_image_surface_get_width(button->icon);
1306 h = cairo_image_surface_get_height(button->icon);
1308 if (button->decoration == FRAME_BUTTON_FANCY)
1311 if (button->align == FRAME_BUTTON_LEFT) {
1312 widget_set_allocation(button->widget,
1313 x_l, y , w + 1, h + 1);
1315 x_l += button_padding;
1318 widget_set_allocation(button->widget,
1319 x_r, y , w + 1, h + 1);
1320 x_r -= button_padding;
1326 frame_button_enter_handler(struct widget *widget,
1327 struct input *input, float x, float y, void *data)
1329 struct frame_button *frame_button = data;
1331 widget_schedule_redraw(frame_button->widget);
1332 frame_button->state = FRAME_BUTTON_OVER;
1334 return CURSOR_LEFT_PTR;
1338 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1340 struct frame_button *frame_button = data;
1342 widget_schedule_redraw(frame_button->widget);
1343 frame_button->state = FRAME_BUTTON_DEFAULT;
1347 frame_button_button_handler(struct widget *widget,
1348 struct input *input, uint32_t time,
1350 enum wl_pointer_button_state state, void *data)
1352 struct frame_button *frame_button = data;
1353 struct window *window = widget->window;
1355 if (button != BTN_LEFT)
1359 case WL_POINTER_BUTTON_STATE_PRESSED:
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);
1366 case WL_POINTER_BUTTON_STATE_RELEASED:
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, enum wl_pointer_button_state state,
1595 struct frame *frame = data;
1596 struct window *window = widget->window;
1597 struct display *display = window->display;
1600 location = theme_get_location(display->theme, input->sx, input->sy,
1601 frame->widget->allocation.width,
1602 frame->widget->allocation.height);
1604 if (window->display->shell && button == BTN_LEFT &&
1605 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1607 case THEME_LOCATION_TITLEBAR:
1608 if (!window->shell_surface)
1610 input_set_pointer_image(input, CURSOR_DRAGGING);
1611 input_ungrab(input);
1612 wl_shell_surface_move(window->shell_surface,
1613 input_get_seat(input),
1616 case THEME_LOCATION_RESIZING_TOP:
1617 case THEME_LOCATION_RESIZING_BOTTOM:
1618 case THEME_LOCATION_RESIZING_LEFT:
1619 case THEME_LOCATION_RESIZING_RIGHT:
1620 case THEME_LOCATION_RESIZING_TOP_LEFT:
1621 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1622 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1623 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1624 if (!window->shell_surface)
1626 input_ungrab(input);
1628 if (!display->dpy) {
1629 /* If we're using shm, allocate a big
1630 pool to create buffers out of while
1631 we resize. We should probably base
1632 this number on the size of the output. */
1634 shm_pool_create(display, 6 * 1024 * 1024);
1637 wl_shell_surface_resize(window->shell_surface,
1638 input_get_seat(input),
1639 display->serial, location);
1642 } else if (button == BTN_RIGHT &&
1643 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1644 window_show_frame_menu(window, input, time);
1649 frame_create(struct window *window, void *data)
1651 struct frame *frame;
1653 frame = malloc(sizeof *frame);
1654 memset(frame, 0, sizeof *frame);
1656 frame->widget = window_add_widget(window, frame);
1657 frame->child = widget_add_widget(frame->widget, data);
1659 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1660 widget_set_resize_handler(frame->widget, frame_resize_handler);
1661 widget_set_enter_handler(frame->widget, frame_enter_handler);
1662 widget_set_motion_handler(frame->widget, frame_motion_handler);
1663 widget_set_button_handler(frame->widget, frame_button_handler);
1665 /* Create empty list for frame buttons */
1666 wl_list_init(&frame->buttons_list);
1668 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1669 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1671 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1672 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1674 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1675 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1677 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1678 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680 window->frame = frame;
1682 return frame->child;
1686 frame_destroy(struct frame *frame)
1688 struct frame_button *button, *tmp;
1690 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1691 frame_button_destroy(button);
1693 /* frame->child must be destroyed by the application */
1694 widget_destroy(frame->widget);
1699 input_set_focus_widget(struct input *input, struct widget *focus,
1702 struct widget *old, *widget;
1703 int pointer = CURSOR_LEFT_PTR;
1705 if (focus == input->focus_widget)
1708 old = input->focus_widget;
1712 widget = input->grab;
1713 if (widget->leave_handler)
1714 widget->leave_handler(old, input, widget->user_data);
1715 input->focus_widget = NULL;
1721 widget = input->grab;
1722 input->focus_widget = focus;
1723 if (widget->enter_handler)
1724 pointer = widget->enter_handler(focus, input, x, y,
1727 input_set_pointer_image(input, pointer);
1732 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1733 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1735 struct input *input = data;
1736 struct window *window = input->pointer_focus;
1737 struct widget *widget;
1738 int cursor = CURSOR_LEFT_PTR;
1739 float sx = wl_fixed_to_double(sx_w);
1740 float sy = wl_fixed_to_double(sy_w);
1745 if (!(input->grab && input->grab_button)) {
1746 widget = widget_find_widget(window->widget, sx, sy);
1747 input_set_focus_widget(input, widget, sx, sy);
1751 widget = input->grab;
1753 widget = input->focus_widget;
1754 if (widget && widget->motion_handler)
1755 cursor = widget->motion_handler(input->focus_widget,
1756 input, time, sx, sy,
1759 input_set_pointer_image(input, cursor);
1763 input_grab(struct input *input, struct widget *widget, uint32_t button)
1765 input->grab = widget;
1766 input->grab_button = button;
1770 input_ungrab(struct input *input)
1772 struct widget *widget;
1775 if (input->pointer_focus) {
1776 widget = widget_find_widget(input->pointer_focus->widget,
1777 input->sx, input->sy);
1778 input_set_focus_widget(input, widget, input->sx, input->sy);
1783 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1784 uint32_t time, uint32_t button, uint32_t state_w)
1786 struct input *input = data;
1787 struct widget *widget;
1788 enum wl_pointer_button_state state = state_w;
1790 input->display->serial = serial;
1791 if (input->focus_widget && input->grab == NULL &&
1792 state == WL_POINTER_BUTTON_STATE_PRESSED)
1793 input_grab(input, input->focus_widget, button);
1795 widget = input->grab;
1796 if (widget && widget->button_handler)
1797 (*widget->button_handler)(widget,
1800 input->grab->user_data);
1802 if (input->grab && input->grab_button == button &&
1803 state == WL_POINTER_BUTTON_STATE_RELEASED)
1804 input_ungrab(input);
1808 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1809 uint32_t time, uint32_t axis, wl_fixed_t value)
1814 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1815 uint32_t serial, uint32_t time, uint32_t key,
1818 struct input *input = data;
1819 struct window *window = input->keyboard_focus;
1820 uint32_t code, num_syms;
1821 enum wl_keyboard_key_state state = state_w;
1822 const xkb_keysym_t *syms;
1824 xkb_mod_mask_t mask;
1826 input->display->serial = serial;
1828 if (!window || window->keyboard_device != input || !input->xkb.state)
1831 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
1833 mask = xkb_state_serialize_mods(input->xkb.state,
1834 XKB_STATE_DEPRESSED |
1836 input->modifiers = 0;
1837 if (mask & input->xkb.control_mask)
1838 input->modifiers |= MOD_CONTROL_MASK;
1839 if (mask & input->xkb.alt_mask)
1840 input->modifiers |= MOD_ALT_MASK;
1841 if (mask & input->xkb.shift_mask)
1842 input->modifiers |= MOD_SHIFT_MASK;
1844 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
1845 input->modifiers == MOD_ALT_MASK) {
1846 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1847 window_set_maximized(window,
1848 window->type != TYPE_MAXIMIZED);
1849 } else if (window->key_handler) {
1853 sym = XKB_KEY_NoSymbol;
1855 (*window->key_handler)(window, input, time, key,
1856 sym, state, window->user_data);
1861 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1862 uint32_t serial, uint32_t mods_depressed,
1863 uint32_t mods_latched, uint32_t mods_locked,
1866 struct input *input = data;
1868 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1869 mods_locked, 0, 0, group);
1873 input_remove_pointer_focus(struct input *input)
1875 struct window *window = input->pointer_focus;
1880 input_set_focus_widget(input, NULL, 0, 0);
1882 input->pointer_focus = NULL;
1883 input->current_cursor = CURSOR_UNSET;
1887 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1888 uint32_t serial, struct wl_surface *surface,
1889 wl_fixed_t sx_w, wl_fixed_t sy_w)
1891 struct input *input = data;
1892 struct window *window;
1893 struct widget *widget;
1894 float sx = wl_fixed_to_double(sx_w);
1895 float sy = wl_fixed_to_double(sy_w);
1898 /* enter event for a window we've just destroyed */
1902 input->display->serial = serial;
1903 input->pointer_enter_serial = serial;
1904 input->pointer_focus = wl_surface_get_user_data(surface);
1905 window = input->pointer_focus;
1908 shm_pool_destroy(window->pool);
1909 window->pool = NULL;
1910 /* Schedule a redraw to free the pool */
1911 window_schedule_redraw(window);
1917 widget = widget_find_widget(window->widget, sx, sy);
1918 input_set_focus_widget(input, widget, sx, sy);
1922 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1923 uint32_t serial, struct wl_surface *surface)
1925 struct input *input = data;
1927 input->display->serial = serial;
1928 input_remove_pointer_focus(input);
1932 input_remove_keyboard_focus(struct input *input)
1934 struct window *window = input->keyboard_focus;
1939 window->keyboard_device = NULL;
1940 if (window->keyboard_focus_handler)
1941 (*window->keyboard_focus_handler)(window, NULL,
1944 input->keyboard_focus = NULL;
1948 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1949 uint32_t serial, struct wl_surface *surface,
1950 struct wl_array *keys)
1952 struct input *input = data;
1953 struct window *window;
1955 input->display->serial = serial;
1956 input->keyboard_focus = wl_surface_get_user_data(surface);
1958 window = input->keyboard_focus;
1959 window->keyboard_device = input;
1960 if (window->keyboard_focus_handler)
1961 (*window->keyboard_focus_handler)(window,
1962 window->keyboard_device,
1967 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1968 uint32_t serial, struct wl_surface *surface)
1970 struct input *input = data;
1972 input->display->serial = serial;
1973 input_remove_keyboard_focus(input);
1977 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1978 uint32_t format, int fd, uint32_t size)
1980 struct input *input = data;
1988 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1993 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1994 if (map_str == MAP_FAILED) {
1999 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2001 XKB_KEYMAP_FORMAT_TEXT_V1,
2003 munmap(map_str, size);
2006 if (!input->xkb.keymap) {
2007 fprintf(stderr, "failed to compile keymap\n");
2011 input->xkb.state = xkb_state_new(input->xkb.keymap);
2012 if (!input->xkb.state) {
2013 fprintf(stderr, "failed to create XKB state\n");
2014 xkb_map_unref(input->xkb.keymap);
2015 input->xkb.keymap = NULL;
2019 input->xkb.control_mask =
2020 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2021 input->xkb.alt_mask =
2022 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2023 input->xkb.shift_mask =
2024 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2027 static const struct wl_pointer_listener pointer_listener = {
2028 pointer_handle_enter,
2029 pointer_handle_leave,
2030 pointer_handle_motion,
2031 pointer_handle_button,
2032 pointer_handle_axis,
2035 static const struct wl_keyboard_listener keyboard_listener = {
2036 keyboard_handle_keymap,
2037 keyboard_handle_enter,
2038 keyboard_handle_leave,
2039 keyboard_handle_key,
2040 keyboard_handle_modifiers,
2044 seat_handle_capabilities(void *data, struct wl_seat *seat,
2045 enum wl_seat_capability caps)
2047 struct input *input = data;
2049 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2050 input->pointer = wl_seat_get_pointer(seat);
2051 wl_pointer_set_user_data(input->pointer, input);
2052 wl_pointer_add_listener(input->pointer, &pointer_listener,
2054 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2055 wl_pointer_destroy(input->pointer);
2056 input->pointer = NULL;
2059 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2060 input->keyboard = wl_seat_get_keyboard(seat);
2061 wl_keyboard_set_user_data(input->keyboard, input);
2062 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2064 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2065 wl_keyboard_destroy(input->keyboard);
2066 input->keyboard = NULL;
2070 static const struct wl_seat_listener seat_listener = {
2071 seat_handle_capabilities,
2075 input_get_position(struct input *input, int32_t *x, int32_t *y)
2082 input_get_display(struct input *input)
2084 return input->display;
2088 input_get_seat(struct input *input)
2094 input_get_modifiers(struct input *input)
2096 return input->modifiers;
2100 input_get_focus_widget(struct input *input)
2102 return input->focus_widget;
2106 struct wl_data_offer *offer;
2107 struct input *input;
2108 struct wl_array types;
2111 struct task io_task;
2119 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2121 struct data_offer *offer = data;
2124 p = wl_array_add(&offer->types, sizeof *p);
2128 static const struct wl_data_offer_listener data_offer_listener = {
2133 data_offer_destroy(struct data_offer *offer)
2138 if (offer->refcount == 0) {
2139 wl_data_offer_destroy(offer->offer);
2140 for (p = offer->types.data; *p; p++)
2142 wl_array_release(&offer->types);
2148 data_device_data_offer(void *data,
2149 struct wl_data_device *data_device, uint32_t id)
2151 struct data_offer *offer;
2153 offer = malloc(sizeof *offer);
2155 wl_array_init(&offer->types);
2156 offer->refcount = 1;
2157 offer->input = data;
2159 /* FIXME: Generate typesafe wrappers for this */
2160 offer->offer = (struct wl_data_offer *)
2161 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2162 id, &wl_data_offer_interface);
2164 wl_data_offer_add_listener(offer->offer,
2165 &data_offer_listener, offer);
2169 data_device_enter(void *data, struct wl_data_device *data_device,
2170 uint32_t serial, struct wl_surface *surface,
2171 wl_fixed_t x_w, wl_fixed_t y_w,
2172 struct wl_data_offer *offer)
2174 struct input *input = data;
2175 struct window *window;
2177 float x = wl_fixed_to_double(x_w);
2178 float y = wl_fixed_to_double(y_w);
2181 input->pointer_enter_serial = serial;
2182 window = wl_surface_get_user_data(surface);
2183 input->pointer_focus = window;
2186 input->drag_offer = wl_data_offer_get_user_data(offer);
2188 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2191 types_data = input->drag_offer->types.data;
2193 input->drag_offer = NULL;
2197 window = input->pointer_focus;
2198 if (window->data_handler)
2199 window->data_handler(window, input, x, y, types_data,
2204 data_device_leave(void *data, struct wl_data_device *data_device)
2206 struct input *input = data;
2208 if (input->drag_offer) {
2209 data_offer_destroy(input->drag_offer);
2210 input->drag_offer = NULL;
2215 data_device_motion(void *data, struct wl_data_device *data_device,
2216 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2218 struct input *input = data;
2219 struct window *window = input->pointer_focus;
2220 float x = wl_fixed_to_double(x_w);
2221 float y = wl_fixed_to_double(y_w);
2227 if (input->drag_offer)
2228 types_data = input->drag_offer->types.data;
2232 if (window->data_handler)
2233 window->data_handler(window, input, x, y, types_data,
2238 data_device_drop(void *data, struct wl_data_device *data_device)
2240 struct input *input = data;
2241 struct window *window = input->pointer_focus;
2243 if (window->drop_handler)
2244 window->drop_handler(window, input,
2245 input->sx, input->sy, window->user_data);
2249 data_device_selection(void *data,
2250 struct wl_data_device *wl_data_device,
2251 struct wl_data_offer *offer)
2253 struct input *input = data;
2256 if (input->selection_offer)
2257 data_offer_destroy(input->selection_offer);
2260 input->selection_offer = wl_data_offer_get_user_data(offer);
2261 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2264 input->selection_offer = NULL;
2268 static const struct wl_data_device_listener data_device_listener = {
2269 data_device_data_offer,
2274 data_device_selection
2278 input_set_pointer_image_index(struct input *input, int pointer, int index)
2280 struct wl_buffer *buffer;
2281 struct wl_cursor *cursor;
2282 struct wl_cursor_image *image;
2284 cursor = input->display->cursors[pointer];
2288 if (index >= (int) cursor->image_count) {
2289 fprintf(stderr, "cursor index out of range\n");
2293 image = cursor->images[index];
2294 buffer = wl_cursor_image_get_buffer(image);
2298 input->current_cursor = pointer;
2299 wl_pointer_set_cursor(input->pointer, input->display->serial,
2300 input->pointer_surface,
2301 image->hotspot_x, image->hotspot_y);
2302 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2303 wl_surface_damage(input->pointer_surface, 0, 0,
2304 image->width, image->height);
2308 input_set_pointer_image(struct input *input, int pointer)
2310 if (pointer == input->current_cursor)
2313 input_set_pointer_image_index(input, pointer, 0);
2316 struct wl_data_device *
2317 input_get_data_device(struct input *input)
2319 return input->data_device;
2323 input_set_selection(struct input *input,
2324 struct wl_data_source *source, uint32_t time)
2326 wl_data_device_set_selection(input->data_device, source, time);
2330 input_accept(struct input *input, const char *type)
2332 wl_data_offer_accept(input->drag_offer->offer,
2333 input->pointer_enter_serial, type);
2337 offer_io_func(struct task *task, uint32_t events)
2339 struct data_offer *offer =
2340 container_of(task, struct data_offer, io_task);
2344 len = read(offer->fd, buffer, sizeof buffer);
2345 offer->func(buffer, len,
2346 offer->x, offer->y, offer->user_data);
2350 data_offer_destroy(offer);
2355 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2356 data_func_t func, void *user_data)
2360 if (pipe2(p, O_CLOEXEC) == -1)
2363 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2366 offer->io_task.run = offer_io_func;
2370 offer->user_data = user_data;
2372 display_watch_fd(offer->input->display,
2373 offer->fd, EPOLLIN, &offer->io_task);
2377 input_receive_drag_data(struct input *input, const char *mime_type,
2378 data_func_t func, void *data)
2380 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2381 input->drag_offer->x = input->sx;
2382 input->drag_offer->y = input->sy;
2386 input_receive_selection_data(struct input *input, const char *mime_type,
2387 data_func_t func, void *data)
2391 if (input->selection_offer == NULL)
2394 for (p = input->selection_offer->types.data; *p; p++)
2395 if (strcmp(mime_type, *p) == 0)
2401 data_offer_receive_data(input->selection_offer,
2402 mime_type, func, data);
2407 input_receive_selection_data_to_fd(struct input *input,
2408 const char *mime_type, int fd)
2410 if (input->selection_offer)
2411 wl_data_offer_receive(input->selection_offer->offer,
2418 window_move(struct window *window, struct input *input, uint32_t serial)
2420 if (!window->shell_surface)
2423 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2427 idle_resize(struct window *window)
2429 struct widget *widget;
2431 window->resize_needed = 0;
2432 widget = window->widget;
2433 widget_set_allocation(widget,
2434 window->pending_allocation.x,
2435 window->pending_allocation.y,
2436 window->pending_allocation.width,
2437 window->pending_allocation.height);
2439 if (window->input_region) {
2440 wl_region_destroy(window->input_region);
2441 window->input_region = NULL;
2444 if (window->opaque_region) {
2445 wl_region_destroy(window->opaque_region);
2446 window->opaque_region = NULL;
2449 if (widget->resize_handler)
2450 widget->resize_handler(widget,
2451 widget->allocation.width,
2452 widget->allocation.height,
2455 if (window->allocation.width != widget->allocation.width ||
2456 window->allocation.height != widget->allocation.height) {
2457 window->allocation = widget->allocation;
2458 window_schedule_redraw(window);
2463 window_schedule_resize(struct window *window, int width, int height)
2465 window->pending_allocation.x = 0;
2466 window->pending_allocation.y = 0;
2467 window->pending_allocation.width = width;
2468 window->pending_allocation.height = height;
2470 window->resize_needed = 1;
2471 window_schedule_redraw(window);
2475 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2477 window_schedule_resize(widget->window, width, height);
2481 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2484 wl_shell_surface_pong(shell_surface, serial);
2488 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2489 uint32_t edges, int32_t width, int32_t height)
2491 struct window *window = data;
2493 if (width <= 0 || height <= 0)
2496 window->resize_edges = edges;
2497 window_schedule_resize(window, width, height);
2501 menu_destroy(struct menu *menu)
2503 widget_destroy(menu->widget);
2504 window_destroy(menu->window);
2509 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2511 struct window *window = data;
2512 struct menu *menu = window->widget->user_data;
2514 /* FIXME: Need more context in this event, at least the input
2515 * device. Or just use wl_callback. And this really needs to
2516 * be a window vfunc that the menu can set. And we need the
2519 menu->func(window->parent, menu->current, window->parent->user_data);
2520 input_ungrab(menu->input);
2524 static const struct wl_shell_surface_listener shell_surface_listener = {
2531 window_get_allocation(struct window *window,
2532 struct rectangle *allocation)
2534 *allocation = window->allocation;
2538 widget_redraw(struct widget *widget)
2540 struct widget *child;
2542 if (widget->redraw_handler)
2543 widget->redraw_handler(widget, widget->user_data);
2544 wl_list_for_each(child, &widget->child_list, link)
2545 widget_redraw(child);
2549 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2551 struct window *window = data;
2553 wl_callback_destroy(callback);
2554 window->redraw_scheduled = 0;
2555 if (window->redraw_needed)
2556 window_schedule_redraw(window);
2559 static const struct wl_callback_listener listener = {
2564 idle_redraw(struct task *task, uint32_t events)
2566 struct window *window = container_of(task, struct window, redraw_task);
2567 struct wl_callback *callback;
2569 if (window->resize_needed)
2570 idle_resize(window);
2572 window_create_surface(window);
2573 widget_redraw(window->widget);
2574 window_flush(window);
2575 window->redraw_needed = 0;
2576 wl_list_init(&window->redraw_task.link);
2578 callback = wl_surface_frame(window->surface);
2579 wl_callback_add_listener(callback, &listener, window);
2583 window_schedule_redraw(struct window *window)
2585 window->redraw_needed = 1;
2586 if (!window->redraw_scheduled) {
2587 window->redraw_task.run = idle_redraw;
2588 display_defer(window->display, &window->redraw_task);
2589 window->redraw_scheduled = 1;
2594 window_set_custom(struct window *window)
2596 window->type = TYPE_CUSTOM;
2600 window_set_fullscreen(struct window *window, int fullscreen)
2602 if (!window->display->shell)
2605 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2609 window->type = TYPE_FULLSCREEN;
2610 window->saved_allocation = window->allocation;
2611 wl_shell_surface_set_fullscreen(window->shell_surface,
2612 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2615 window->type = TYPE_TOPLEVEL;
2616 wl_shell_surface_set_toplevel(window->shell_surface);
2617 window_schedule_resize(window,
2618 window->saved_allocation.width,
2619 window->saved_allocation.height);
2624 window_set_maximized(struct window *window, int maximized)
2626 if (!window->display->shell)
2629 if ((window->type == TYPE_MAXIMIZED) == maximized)
2632 if (window->type == TYPE_TOPLEVEL) {
2633 window->saved_allocation = window->allocation;
2634 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2635 window->type = TYPE_MAXIMIZED;
2637 wl_shell_surface_set_toplevel(window->shell_surface);
2638 window->type = TYPE_TOPLEVEL;
2639 window_schedule_resize(window,
2640 window->saved_allocation.width,
2641 window->saved_allocation.height);
2646 window_set_user_data(struct window *window, void *data)
2648 window->user_data = data;
2652 window_get_user_data(struct window *window)
2654 return window->user_data;
2658 window_set_key_handler(struct window *window,
2659 window_key_handler_t handler)
2661 window->key_handler = handler;
2665 window_set_keyboard_focus_handler(struct window *window,
2666 window_keyboard_focus_handler_t handler)
2668 window->keyboard_focus_handler = handler;
2672 window_set_data_handler(struct window *window, window_data_handler_t handler)
2674 window->data_handler = handler;
2678 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2680 window->drop_handler = handler;
2684 window_set_close_handler(struct window *window,
2685 window_close_handler_t handler)
2687 window->close_handler = handler;
2691 window_set_title(struct window *window, const char *title)
2693 free(window->title);
2694 window->title = strdup(title);
2695 if (window->shell_surface)
2696 wl_shell_surface_set_title(window->shell_surface, title);
2700 window_get_title(struct window *window)
2702 return window->title;
2706 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2708 struct text_cursor_position *text_cursor_position =
2709 window->display->text_cursor_position;
2711 if (!text_cursor_position)
2714 text_cursor_position_notify(text_cursor_position,
2716 wl_fixed_from_int(x),
2717 wl_fixed_from_int(y));
2721 window_damage(struct window *window, int32_t x, int32_t y,
2722 int32_t width, int32_t height)
2724 wl_surface_damage(window->surface, x, y, width, height);
2728 surface_enter(void *data,
2729 struct wl_surface *wl_surface, struct wl_output *wl_output)
2731 struct window *window = data;
2732 struct output *output;
2733 struct output *output_found = NULL;
2734 struct window_output *window_output;
2736 wl_list_for_each(output, &window->display->output_list, link) {
2737 if (output->output == wl_output) {
2738 output_found = output;
2746 window_output = malloc (sizeof *window_output);
2747 window_output->output = output_found;
2749 wl_list_insert (&window->window_output_list, &window_output->link);
2753 surface_leave(void *data,
2754 struct wl_surface *wl_surface, struct wl_output *output)
2756 struct window *window = data;
2757 struct window_output *window_output;
2758 struct window_output *window_output_found = NULL;
2760 wl_list_for_each(window_output, &window->window_output_list, link) {
2761 if (window_output->output->output == output) {
2762 window_output_found = window_output;
2767 if (window_output_found) {
2768 wl_list_remove(&window_output_found->link);
2769 free(window_output_found);
2773 static const struct wl_surface_listener surface_listener = {
2778 static struct window *
2779 window_create_internal(struct display *display, struct window *parent)
2781 struct window *window;
2783 window = malloc(sizeof *window);
2787 memset(window, 0, sizeof *window);
2788 window->display = display;
2789 window->parent = parent;
2790 window->surface = wl_compositor_create_surface(display->compositor);
2791 wl_surface_add_listener(window->surface, &surface_listener, window);
2792 if (display->shell) {
2793 window->shell_surface =
2794 wl_shell_get_shell_surface(display->shell,
2797 wl_shell_surface_set_title(window->shell_surface,
2800 window->allocation.x = 0;
2801 window->allocation.y = 0;
2802 window->allocation.width = 0;
2803 window->allocation.height = 0;
2804 window->saved_allocation = window->allocation;
2805 window->transparent = 1;
2806 window->type = TYPE_NONE;
2807 window->input_region = NULL;
2808 window->opaque_region = NULL;
2811 #ifdef HAVE_CAIRO_EGL
2812 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2814 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2817 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2819 wl_surface_set_user_data(window->surface, window);
2820 wl_list_insert(display->window_list.prev, &window->link);
2821 wl_list_init(&window->redraw_task.link);
2823 if (window->shell_surface) {
2824 wl_shell_surface_set_user_data(window->shell_surface, window);
2825 wl_shell_surface_add_listener(window->shell_surface,
2826 &shell_surface_listener, window);
2829 wl_list_init (&window->window_output_list);
2835 window_create(struct display *display)
2837 struct window *window;
2839 window = window_create_internal(display, NULL);
2847 window_create_transient(struct display *display, struct window *parent,
2848 int32_t x, int32_t y, uint32_t flags)
2850 struct window *window;
2852 window = window_create_internal(parent->display, parent);
2856 window->type = TYPE_TRANSIENT;
2861 wl_shell_surface_set_transient(window->shell_surface,
2862 window->parent->shell_surface,
2863 window->x, window->y, flags);
2869 menu_set_item(struct menu *menu, int sy)
2873 next = (sy - 8) / 20;
2874 if (menu->current != next) {
2875 menu->current = next;
2876 widget_schedule_redraw(menu->widget);
2881 menu_motion_handler(struct widget *widget,
2882 struct input *input, uint32_t time,
2883 float x, float y, void *data)
2885 struct menu *menu = data;
2887 if (widget == menu->widget)
2888 menu_set_item(data, y);
2890 return CURSOR_LEFT_PTR;
2894 menu_enter_handler(struct widget *widget,
2895 struct input *input, float x, float y, void *data)
2897 struct menu *menu = data;
2899 if (widget == menu->widget)
2900 menu_set_item(data, y);
2902 return CURSOR_LEFT_PTR;
2906 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2908 struct menu *menu = data;
2910 if (widget == menu->widget)
2911 menu_set_item(data, -200);
2915 menu_button_handler(struct widget *widget,
2916 struct input *input, uint32_t time,
2917 uint32_t button, enum wl_pointer_button_state state,
2921 struct menu *menu = data;
2923 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2924 time - menu->time > 500) {
2925 /* Either relase after press-drag-release or
2926 * click-motion-click. */
2927 menu->func(menu->window->parent,
2928 menu->current, menu->window->parent->user_data);
2929 input_ungrab(input);
2935 menu_redraw_handler(struct widget *widget, void *data)
2938 const int32_t r = 3, margin = 3;
2939 struct menu *menu = data;
2940 int32_t width, height, i;
2941 struct window *window = widget->window;
2943 cr = cairo_create(window->cairo_surface);
2944 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2945 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2948 width = window->allocation.width;
2949 height = window->allocation.height;
2950 rounded_rect(cr, 0, 0, width, height, r);
2952 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2953 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2956 for (i = 0; i < menu->count; i++) {
2957 if (i == menu->current) {
2958 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2959 cairo_rectangle(cr, margin, i * 20 + margin,
2960 width - 2 * margin, 20);
2962 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2963 cairo_move_to(cr, 10, i * 20 + 16);
2964 cairo_show_text(cr, menu->entries[i]);
2966 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2967 cairo_move_to(cr, 10, i * 20 + 16);
2968 cairo_show_text(cr, menu->entries[i]);
2976 window_show_menu(struct display *display,
2977 struct input *input, uint32_t time, struct window *parent,
2978 int32_t x, int32_t y,
2979 menu_func_t func, const char **entries, int count)
2981 struct window *window;
2983 const int32_t margin = 3;
2985 menu = malloc(sizeof *menu);
2989 window = window_create_internal(parent->display, parent);
2993 menu->window = window;
2994 menu->widget = window_add_widget(menu->window, menu);
2995 menu->entries = entries;
2996 menu->count = count;
3000 menu->input = input;
3001 window->type = TYPE_MENU;
3005 input_ungrab(input);
3006 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3007 display_get_serial(window->display),
3008 window->parent->shell_surface,
3009 window->x, window->y, 0);
3011 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3012 widget_set_enter_handler(menu->widget, menu_enter_handler);
3013 widget_set_leave_handler(menu->widget, menu_leave_handler);
3014 widget_set_motion_handler(menu->widget, menu_motion_handler);
3015 widget_set_button_handler(menu->widget, menu_button_handler);
3017 input_grab(input, menu->widget, 0);
3018 window_schedule_resize(window, 200, count * 20 + margin * 2);
3022 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3024 window->buffer_type = type;
3029 display_handle_geometry(void *data,
3030 struct wl_output *wl_output,
3033 int physical_height,
3038 struct output *output = data;
3040 output->allocation.x = x;
3041 output->allocation.y = y;
3045 display_handle_mode(void *data,
3046 struct wl_output *wl_output,
3052 struct output *output = data;
3053 struct display *display = output->display;
3055 if (flags & WL_OUTPUT_MODE_CURRENT) {
3056 output->allocation.width = width;
3057 output->allocation.height = height;
3058 if (display->output_configure_handler)
3059 (*display->output_configure_handler)(
3060 output, display->user_data);
3064 static const struct wl_output_listener output_listener = {
3065 display_handle_geometry,
3070 display_add_output(struct display *d, uint32_t id)
3072 struct output *output;
3074 output = malloc(sizeof *output);
3078 memset(output, 0, sizeof *output);
3079 output->display = d;
3081 wl_display_bind(d->display, id, &wl_output_interface);
3082 wl_list_insert(d->output_list.prev, &output->link);
3084 wl_output_add_listener(output->output, &output_listener, output);
3088 output_destroy(struct output *output)
3090 if (output->destroy_handler)
3091 (*output->destroy_handler)(output, output->user_data);
3093 wl_output_destroy(output->output);
3094 wl_list_remove(&output->link);
3099 display_set_output_configure_handler(struct display *display,
3100 display_output_handler_t handler)
3102 struct output *output;
3104 display->output_configure_handler = handler;
3108 wl_list_for_each(output, &display->output_list, link)
3109 (*display->output_configure_handler)(output,
3110 display->user_data);
3114 output_set_user_data(struct output *output, void *data)
3116 output->user_data = data;
3120 output_get_user_data(struct output *output)
3122 return output->user_data;
3126 output_set_destroy_handler(struct output *output,
3127 display_output_handler_t handler)
3129 output->destroy_handler = handler;
3130 /* FIXME: implement this, once we have way to remove outputs */
3134 output_get_allocation(struct output *output, struct rectangle *allocation)
3136 *allocation = output->allocation;
3140 output_get_wl_output(struct output *output)
3142 return output->output;
3146 fini_xkb(struct input *input)
3148 xkb_state_unref(input->xkb.state);
3149 xkb_map_unref(input->xkb.keymap);
3153 display_add_input(struct display *d, uint32_t id)
3155 struct input *input;
3157 input = malloc(sizeof *input);
3161 memset(input, 0, sizeof *input);
3163 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3164 input->pointer_focus = NULL;
3165 input->keyboard_focus = NULL;
3166 wl_list_insert(d->input_list.prev, &input->link);
3168 wl_seat_add_listener(input->seat, &seat_listener, input);
3169 wl_seat_set_user_data(input->seat, input);
3171 input->data_device =
3172 wl_data_device_manager_get_data_device(d->data_device_manager,
3174 wl_data_device_add_listener(input->data_device, &data_device_listener,
3177 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3181 input_destroy(struct input *input)
3183 input_remove_keyboard_focus(input);
3184 input_remove_pointer_focus(input);
3186 if (input->xkb.state)
3187 xkb_state_unref(input->xkb.state);
3188 if (input->xkb.keymap)
3189 xkb_map_unref(input->xkb.keymap);
3191 if (input->drag_offer)
3192 data_offer_destroy(input->drag_offer);
3194 if (input->selection_offer)
3195 data_offer_destroy(input->selection_offer);
3197 wl_data_device_destroy(input->data_device);
3200 wl_surface_destroy(input->pointer_surface);
3202 wl_list_remove(&input->link);
3203 wl_seat_destroy(input->seat);
3208 display_handle_global(struct wl_display *display, uint32_t id,
3209 const char *interface, uint32_t version, void *data)
3211 struct display *d = data;
3213 if (strcmp(interface, "wl_compositor") == 0) {
3215 wl_display_bind(display, id, &wl_compositor_interface);
3216 } else if (strcmp(interface, "wl_output") == 0) {
3217 display_add_output(d, id);
3218 } else if (strcmp(interface, "wl_seat") == 0) {
3219 display_add_input(d, id);
3220 } else if (strcmp(interface, "wl_shell") == 0) {
3221 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3222 } else if (strcmp(interface, "wl_shm") == 0) {
3223 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3224 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3225 d->data_device_manager =
3226 wl_display_bind(display, id,
3227 &wl_data_device_manager_interface);
3228 } else if (strcmp(interface, "text_cursor_position") == 0) {
3229 d->text_cursor_position =
3230 wl_display_bind(display, id,
3231 &text_cursor_position_interface);
3235 #ifdef HAVE_CAIRO_EGL
3237 init_egl(struct display *d)
3239 EGLint major, minor;
3242 #ifdef USE_CAIRO_GLESV2
3243 # define GL_BIT EGL_OPENGL_ES2_BIT
3245 # define GL_BIT EGL_OPENGL_BIT
3248 static const EGLint argb_cfg_attribs[] = {
3249 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3255 EGL_RENDERABLE_TYPE, GL_BIT,
3259 #ifdef USE_CAIRO_GLESV2
3260 static const EGLint context_attribs[] = {
3261 EGL_CONTEXT_CLIENT_VERSION, 2,
3264 EGLint api = EGL_OPENGL_ES_API;
3266 EGLint *context_attribs = NULL;
3267 EGLint api = EGL_OPENGL_API;
3270 d->dpy = eglGetDisplay(d->display);
3271 if (!eglInitialize(d->dpy, &major, &minor)) {
3272 fprintf(stderr, "failed to initialize display\n");
3276 if (!eglBindAPI(api)) {
3277 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3281 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3282 &d->argb_config, 1, &n) || n != 1) {
3283 fprintf(stderr, "failed to choose argb config\n");
3287 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3288 EGL_NO_CONTEXT, context_attribs);
3289 if (d->argb_ctx == NULL) {
3290 fprintf(stderr, "failed to create context\n");
3294 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3295 fprintf(stderr, "failed to make context current\n");
3299 #ifdef HAVE_CAIRO_EGL
3300 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3301 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3302 fprintf(stderr, "failed to get cairo egl argb device\n");
3311 fini_egl(struct display *display)
3313 #ifdef HAVE_CAIRO_EGL
3314 cairo_device_destroy(display->argb_device);
3317 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3320 eglTerminate(display->dpy);
3326 event_mask_update(uint32_t mask, void *data)
3328 struct display *d = data;
3336 handle_display_data(struct task *task, uint32_t events)
3338 struct display *display =
3339 container_of(task, struct display, display_task);
3341 wl_display_iterate(display->display, display->mask);
3345 display_create(int argc, char *argv[])
3349 argc = parse_options(xkb_options,
3350 ARRAY_LENGTH(xkb_options), argc, argv);
3352 d = malloc(sizeof *d);
3356 memset(d, 0, sizeof *d);
3358 d->display = wl_display_connect(NULL);
3359 if (d->display == NULL) {
3360 fprintf(stderr, "failed to create display: %m\n");
3364 d->epoll_fd = os_epoll_create_cloexec();
3365 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3366 d->display_task.run = handle_display_data;
3367 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3369 wl_list_init(&d->deferred_list);
3370 wl_list_init(&d->input_list);
3371 wl_list_init(&d->output_list);
3373 d->xkb_context = xkb_context_new(0);
3374 if (d->xkb_context == NULL) {
3375 fprintf(stderr, "Failed to create XKB context\n");
3379 /* Set up listener so we'll catch all events. */
3380 wl_display_add_global_listener(d->display,
3381 display_handle_global, d);
3383 /* Process connection events. */
3384 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3385 #ifdef HAVE_CAIRO_EGL
3386 if (init_egl(d) < 0)
3390 d->image_target_texture_2d =
3391 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3392 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3393 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3397 d->theme = theme_create();
3399 wl_list_init(&d->window_list);
3405 display_destroy_outputs(struct display *display)
3408 struct output *output;
3410 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3411 output_destroy(output);
3415 display_destroy_inputs(struct display *display)
3418 struct input *input;
3420 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3421 input_destroy(input);
3425 display_destroy(struct display *display)
3427 if (!wl_list_empty(&display->window_list))
3428 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3430 if (!wl_list_empty(&display->deferred_list))
3431 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3433 display_destroy_outputs(display);
3434 display_destroy_inputs(display);
3436 xkb_context_unref(display->xkb_context);
3438 theme_destroy(display->theme);
3439 destroy_cursors(display);
3441 #ifdef HAVE_CAIRO_EGL
3446 wl_shell_destroy(display->shell);
3449 wl_shm_destroy(display->shm);
3451 if (display->data_device_manager)
3452 wl_data_device_manager_destroy(display->data_device_manager);
3454 wl_compositor_destroy(display->compositor);
3456 close(display->epoll_fd);
3458 wl_display_flush(display->display);
3459 wl_display_disconnect(display->display);
3464 display_set_user_data(struct display *display, void *data)
3466 display->user_data = data;
3470 display_get_user_data(struct display *display)
3472 return display->user_data;
3476 display_get_display(struct display *display)
3478 return display->display;
3482 display_get_output(struct display *display)
3484 return container_of(display->output_list.next, struct output, link);
3487 struct wl_compositor *
3488 display_get_compositor(struct display *display)
3490 return display->compositor;
3494 display_get_serial(struct display *display)
3496 return display->serial;
3500 display_get_egl_display(struct display *d)
3505 struct wl_data_source *
3506 display_create_data_source(struct display *display)
3508 return wl_data_device_manager_create_data_source(display->data_device_manager);
3512 display_get_argb_egl_config(struct display *d)
3514 return d->argb_config;
3518 display_get_shell(struct display *display)
3520 return display->shell;
3524 display_acquire_window_surface(struct display *display,
3525 struct window *window,
3528 #ifdef HAVE_CAIRO_EGL
3529 struct egl_window_surface_data *data;
3530 cairo_device_t *device;
3532 if (!window->cairo_surface)
3534 device = cairo_surface_get_device(window->cairo_surface);
3539 if (device == display->argb_device)
3540 ctx = display->argb_ctx;
3545 data = cairo_surface_get_user_data(window->cairo_surface,
3548 cairo_device_flush(device);
3549 cairo_device_acquire(device);
3550 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3551 fprintf(stderr, "failed to make surface current\n");
3560 display_release_window_surface(struct display *display,
3561 struct window *window)
3563 #ifdef HAVE_CAIRO_EGL
3564 cairo_device_t *device;
3566 device = cairo_surface_get_device(window->cairo_surface);
3570 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3571 fprintf(stderr, "failed to make context current\n");
3572 cairo_device_release(device);
3577 display_defer(struct display *display, struct task *task)
3579 wl_list_insert(&display->deferred_list, &task->link);
3583 display_watch_fd(struct display *display,
3584 int fd, uint32_t events, struct task *task)
3586 struct epoll_event ep;
3590 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3594 display_run(struct display *display)
3597 struct epoll_event ep[16];
3600 display->running = 1;
3602 wl_display_flush(display->display);
3604 while (!wl_list_empty(&display->deferred_list)) {
3605 task = container_of(display->deferred_list.next,
3607 wl_list_remove(&task->link);
3611 if (!display->running)
3614 wl_display_flush(display->display);
3616 count = epoll_wait(display->epoll_fd,
3617 ep, ARRAY_LENGTH(ep), -1);
3618 for (i = 0; i < count; i++) {
3619 task = ep[i].data.ptr;
3620 task->run(task, ep[i].events);
3626 display_exit(struct display *display)
3628 display->running = 0;