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 uint32_t cursor_anim_start;
193 struct wl_callback *cursor_frame_cb;
194 struct wl_surface *pointer_surface;
196 uint32_t pointer_enter_serial;
200 struct widget *focus_widget;
202 uint32_t grab_button;
204 struct wl_data_device *data_device;
205 struct data_offer *drag_offer;
206 struct data_offer *selection_offer;
209 struct xkb_keymap *keymap;
210 struct xkb_state *state;
211 xkb_mod_mask_t control_mask;
212 xkb_mod_mask_t alt_mask;
213 xkb_mod_mask_t shift_mask;
216 struct task repeat_task;
220 uint32_t repeat_time;
224 struct display *display;
225 struct wl_output *output;
226 struct rectangle allocation;
229 display_output_handler_t destroy_handler;
233 enum frame_button_action {
234 FRAME_BUTTON_NULL = 0,
235 FRAME_BUTTON_ICON = 1,
236 FRAME_BUTTON_CLOSE = 2,
237 FRAME_BUTTON_MINIMIZE = 3,
238 FRAME_BUTTON_MAXIMIZE = 4,
241 enum frame_button_pointer {
242 FRAME_BUTTON_DEFAULT = 0,
243 FRAME_BUTTON_OVER = 1,
244 FRAME_BUTTON_ACTIVE = 2,
247 enum frame_button_align {
248 FRAME_BUTTON_RIGHT = 0,
249 FRAME_BUTTON_LEFT = 1,
252 enum frame_button_decoration {
253 FRAME_BUTTON_NONE = 0,
254 FRAME_BUTTON_FANCY = 1,
257 struct frame_button {
258 struct widget *widget;
260 cairo_surface_t *icon;
261 enum frame_button_action type;
262 enum frame_button_pointer state;
263 struct wl_list link; /* buttons_list */
264 enum frame_button_align align;
265 enum frame_button_decoration decoration;
269 struct widget *widget;
270 struct widget *child;
271 struct wl_list buttons_list;
275 struct window *window;
276 struct widget *widget;
278 const char **entries;
286 struct widget *parent;
287 struct window *window;
288 struct widget *widget;
290 struct task tooltip_task;
296 struct wl_shm_pool *pool;
303 CURSOR_DEFAULT = 100,
307 enum window_location {
309 WINDOW_RESIZING_TOP = 1,
310 WINDOW_RESIZING_BOTTOM = 2,
311 WINDOW_RESIZING_LEFT = 4,
312 WINDOW_RESIZING_TOP_LEFT = 5,
313 WINDOW_RESIZING_BOTTOM_LEFT = 6,
314 WINDOW_RESIZING_RIGHT = 8,
315 WINDOW_RESIZING_TOP_RIGHT = 9,
316 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
317 WINDOW_RESIZING_MASK = 15,
318 WINDOW_EXTERIOR = 16,
319 WINDOW_TITLEBAR = 17,
320 WINDOW_CLIENT_AREA = 18,
323 const char *option_xkb_layout = "us";
324 const char *option_xkb_variant = "";
325 const char *option_xkb_options = "";
327 static const struct weston_option xkb_options[] = {
328 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
329 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
330 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
333 static const cairo_user_data_key_t surface_data_key;
334 struct surface_data {
335 struct wl_buffer *buffer;
338 #define MULT(_d,c,a,t) \
339 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
341 #ifdef HAVE_CAIRO_EGL
343 struct egl_window_surface_data {
344 struct display *display;
345 struct wl_surface *surface;
346 struct wl_egl_window *window;
351 egl_window_surface_data_destroy(void *p)
353 struct egl_window_surface_data *data = p;
354 struct display *d = data->display;
356 eglDestroySurface(d->dpy, data->surf);
357 wl_egl_window_destroy(data->window);
358 data->surface = NULL;
363 static cairo_surface_t *
364 display_create_egl_window_surface(struct display *display,
365 struct wl_surface *surface,
367 struct rectangle *rectangle)
369 cairo_surface_t *cairo_surface;
370 struct egl_window_surface_data *data;
372 cairo_device_t *device;
374 data = malloc(sizeof *data);
378 data->display = display;
379 data->surface = surface;
381 config = display->argb_config;
382 device = display->argb_device;
384 data->window = wl_egl_window_create(surface,
388 data->surf = eglCreateWindowSurface(display->dpy, config,
391 cairo_surface = cairo_gl_surface_create_for_egl(device,
396 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
397 data, egl_window_surface_data_destroy);
399 return cairo_surface;
405 display_get_buffer_for_surface(struct display *display,
406 cairo_surface_t *surface)
408 struct surface_data *data;
410 data = cairo_surface_get_user_data (surface, &surface_data_key);
415 struct shm_surface_data {
416 struct surface_data data;
417 struct shm_pool *pool;
421 shm_pool_destroy(struct shm_pool *pool);
424 shm_surface_data_destroy(void *p)
426 struct shm_surface_data *data = p;
428 wl_buffer_destroy(data->data.buffer);
430 shm_pool_destroy(data->pool);
435 static struct wl_shm_pool *
436 make_shm_pool(struct display *display, int size, void **data)
438 struct wl_shm_pool *pool;
441 fd = os_create_anonymous_file(size);
443 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
448 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
449 if (*data == MAP_FAILED) {
450 fprintf(stderr, "mmap failed: %m\n");
455 pool = wl_shm_create_pool(display->shm, fd, size);
462 static struct shm_pool *
463 shm_pool_create(struct display *display, size_t size)
465 struct shm_pool *pool = malloc(sizeof *pool);
470 pool->pool = make_shm_pool(display, size, &pool->data);
483 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
485 if (pool->used + size > pool->size)
488 *offset = pool->used;
491 return (char *) pool->data + *offset;
494 /* destroy the pool. this does not unmap the memory though */
496 shm_pool_destroy(struct shm_pool *pool)
498 munmap(pool->data, pool->size);
499 wl_shm_pool_destroy(pool->pool);
503 /* Start allocating from the beginning of the pool again */
505 shm_pool_reset(struct shm_pool *pool)
511 data_length_for_shm_surface(struct rectangle *rect)
515 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
517 return stride * rect->height;
520 static cairo_surface_t *
521 display_create_shm_surface_from_pool(struct display *display,
522 struct rectangle *rectangle,
523 uint32_t flags, struct shm_pool *pool)
525 struct shm_surface_data *data;
527 cairo_surface_t *surface;
528 int stride, length, offset;
531 data = malloc(sizeof *data);
535 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
537 length = stride * rectangle->height;
539 map = shm_pool_allocate(pool, length, &offset);
546 surface = cairo_image_surface_create_for_data (map,
552 cairo_surface_set_user_data (surface, &surface_data_key,
553 data, shm_surface_data_destroy);
555 if (flags & SURFACE_OPAQUE)
556 format = WL_SHM_FORMAT_XRGB8888;
558 format = WL_SHM_FORMAT_ARGB8888;
560 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
568 static cairo_surface_t *
569 display_create_shm_surface(struct display *display,
570 struct rectangle *rectangle, uint32_t flags,
571 struct window *window)
573 struct shm_surface_data *data;
574 struct shm_pool *pool;
575 cairo_surface_t *surface;
577 if (window && window->pool) {
578 shm_pool_reset(window->pool);
579 surface = display_create_shm_surface_from_pool(display,
587 pool = shm_pool_create(display,
588 data_length_for_shm_surface(rectangle));
593 display_create_shm_surface_from_pool(display, rectangle,
597 shm_pool_destroy(pool);
601 /* make sure we destroy the pool when the surface is destroyed */
602 data = cairo_surface_get_user_data(surface, &surface_data_key);
609 check_size(struct rectangle *rect)
611 if (rect->width && rect->height)
614 fprintf(stderr, "tried to create surface of "
615 "width: %d, height: %d\n", rect->width, rect->height);
620 display_create_surface(struct display *display,
621 struct wl_surface *surface,
622 struct rectangle *rectangle,
625 if (check_size(rectangle) < 0)
627 #ifdef HAVE_CAIRO_EGL
628 if (display->dpy && !(flags & SURFACE_SHM))
629 return display_create_egl_window_surface(display,
634 return display_create_shm_surface(display, rectangle, flags, NULL);
637 static const char *cursors[] = {
638 "bottom_left_corner",
639 "bottom_right_corner",
654 create_cursors(struct display *display)
658 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
660 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
662 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
663 display->cursors[i] =
664 wl_cursor_theme_get_cursor(display->cursor_theme,
669 destroy_cursors(struct display *display)
671 wl_cursor_theme_destroy(display->cursor_theme);
672 free(display->cursors);
675 struct wl_cursor_image *
676 display_get_pointer_image(struct display *display, int pointer)
678 struct wl_cursor *cursor =
679 wl_cursor_theme_get_cursor(display->cursor_theme,
682 return cursor ? cursor->images[0] : NULL;
686 window_get_resize_dx_dy(struct window *window, int *x, int *y)
688 if (window->resize_edges & WINDOW_RESIZING_LEFT)
689 *x = window->server_allocation.width - window->allocation.width;
693 if (window->resize_edges & WINDOW_RESIZING_TOP)
694 *y = window->server_allocation.height -
695 window->allocation.height;
699 window->resize_edges = 0;
703 window_attach_surface(struct window *window)
705 struct display *display = window->display;
706 struct wl_buffer *buffer;
707 #ifdef HAVE_CAIRO_EGL
708 struct egl_window_surface_data *data;
712 if (window->type == TYPE_NONE) {
713 window->type = TYPE_TOPLEVEL;
715 wl_shell_surface_set_toplevel(window->shell_surface);
718 switch (window->buffer_type) {
719 #ifdef HAVE_CAIRO_EGL
720 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
721 data = cairo_surface_get_user_data(window->cairo_surface,
724 cairo_gl_surface_swapbuffers(window->cairo_surface);
725 wl_egl_window_get_attached_size(data->window,
726 &window->server_allocation.width,
727 &window->server_allocation.height);
730 case WINDOW_BUFFER_TYPE_SHM:
732 display_get_buffer_for_surface(display,
733 window->cairo_surface);
735 window_get_resize_dx_dy(window, &x, &y);
736 wl_surface_attach(window->surface, buffer, x, y);
737 wl_surface_damage(window->surface, 0, 0,
738 window->allocation.width,
739 window->allocation.height);
740 window->server_allocation = window->allocation;
741 cairo_surface_destroy(window->cairo_surface);
742 window->cairo_surface = NULL;
748 if (window->input_region) {
749 wl_surface_set_input_region(window->surface,
750 window->input_region);
751 wl_region_destroy(window->input_region);
752 window->input_region = NULL;
755 if (window->opaque_region) {
756 wl_surface_set_opaque_region(window->surface,
757 window->opaque_region);
758 wl_region_destroy(window->opaque_region);
759 window->opaque_region = NULL;
764 window_flush(struct window *window)
766 if (window->cairo_surface)
767 window_attach_surface(window);
771 window_set_surface(struct window *window, cairo_surface_t *surface)
773 cairo_surface_reference(surface);
775 if (window->cairo_surface != NULL)
776 cairo_surface_destroy(window->cairo_surface);
778 window->cairo_surface = surface;
781 #ifdef HAVE_CAIRO_EGL
783 window_resize_cairo_window_surface(struct window *window)
785 struct egl_window_surface_data *data;
788 data = cairo_surface_get_user_data(window->cairo_surface,
791 window_get_resize_dx_dy(window, &x, &y),
792 wl_egl_window_resize(data->window,
793 window->allocation.width,
794 window->allocation.height,
797 cairo_gl_surface_set_size(window->cairo_surface,
798 window->allocation.width,
799 window->allocation.height);
804 window_get_display(struct window *window)
806 return window->display;
810 window_create_surface(struct window *window)
812 cairo_surface_t *surface;
815 if (!window->transparent)
816 flags = SURFACE_OPAQUE;
818 switch (window->buffer_type) {
819 #ifdef HAVE_CAIRO_EGL
820 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
821 if (window->cairo_surface) {
822 window_resize_cairo_window_surface(window);
825 surface = display_create_surface(window->display,
827 &window->allocation, flags);
830 case WINDOW_BUFFER_TYPE_SHM:
831 surface = display_create_shm_surface(window->display,
840 window_set_surface(window, surface);
841 cairo_surface_destroy(surface);
844 static void frame_destroy(struct frame *frame);
847 window_destroy(struct window *window)
849 struct display *display = window->display;
851 struct window_output *window_output;
852 struct window_output *window_output_tmp;
854 if (window->redraw_scheduled)
855 wl_list_remove(&window->redraw_task.link);
857 wl_list_for_each(input, &display->input_list, link) {
858 if (input->pointer_focus == window)
859 input->pointer_focus = NULL;
860 if (input->keyboard_focus == window)
861 input->keyboard_focus = NULL;
862 if (input->focus_widget &&
863 input->focus_widget->window == window)
864 input->focus_widget = NULL;
867 wl_list_for_each_safe(window_output, window_output_tmp,
868 &window->window_output_list, link) {
869 free (window_output);
872 if (window->input_region)
873 wl_region_destroy(window->input_region);
874 if (window->opaque_region)
875 wl_region_destroy(window->opaque_region);
878 frame_destroy(window->frame);
880 if (window->shell_surface)
881 wl_shell_surface_destroy(window->shell_surface);
882 wl_surface_destroy(window->surface);
883 wl_list_remove(&window->link);
885 if (window->cairo_surface != NULL)
886 cairo_surface_destroy(window->cairo_surface);
892 static struct widget *
893 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
895 struct widget *child, *target;
897 wl_list_for_each(child, &widget->child_list, link) {
898 target = widget_find_widget(child, x, y);
903 if (widget->allocation.x <= x &&
904 x < widget->allocation.x + widget->allocation.width &&
905 widget->allocation.y <= y &&
906 y < widget->allocation.y + widget->allocation.height) {
913 static struct widget *
914 widget_create(struct window *window, void *data)
916 struct widget *widget;
918 widget = malloc(sizeof *widget);
919 memset(widget, 0, sizeof *widget);
920 widget->window = window;
921 widget->user_data = data;
922 widget->allocation = window->allocation;
923 wl_list_init(&widget->child_list);
925 widget->tooltip = NULL;
926 widget->tooltip_count = 0;
932 window_add_widget(struct window *window, void *data)
934 window->widget = widget_create(window, data);
935 wl_list_init(&window->widget->link);
937 return window->widget;
941 widget_add_widget(struct widget *parent, void *data)
943 struct widget *widget;
945 widget = widget_create(parent->window, data);
946 wl_list_insert(parent->child_list.prev, &widget->link);
952 widget_destroy(struct widget *widget)
954 struct display *display = widget->window->display;
957 if (widget->tooltip) {
958 free(widget->tooltip);
959 widget->tooltip = NULL;
962 wl_list_for_each(input, &display->input_list, link) {
963 if (input->focus_widget == widget)
964 input->focus_widget = NULL;
967 wl_list_remove(&widget->link);
972 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
974 *allocation = widget->allocation;
978 widget_set_size(struct widget *widget, int32_t width, int32_t height)
980 widget->allocation.width = width;
981 widget->allocation.height = height;
985 widget_set_allocation(struct widget *widget,
986 int32_t x, int32_t y, int32_t width, int32_t height)
988 widget->allocation.x = x;
989 widget->allocation.y = y;
990 widget_set_size(widget, width, height);
994 widget_set_transparent(struct widget *widget, int transparent)
996 widget->opaque = !transparent;
1000 widget_get_user_data(struct widget *widget)
1002 return widget->user_data;
1006 widget_set_resize_handler(struct widget *widget,
1007 widget_resize_handler_t handler)
1009 widget->resize_handler = handler;
1013 widget_set_redraw_handler(struct widget *widget,
1014 widget_redraw_handler_t handler)
1016 widget->redraw_handler = handler;
1020 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1022 widget->enter_handler = handler;
1026 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1028 widget->leave_handler = handler;
1032 widget_set_motion_handler(struct widget *widget,
1033 widget_motion_handler_t handler)
1035 widget->motion_handler = handler;
1039 widget_set_button_handler(struct widget *widget,
1040 widget_button_handler_t handler)
1042 widget->button_handler = handler;
1046 widget_schedule_redraw(struct widget *widget)
1048 window_schedule_redraw(widget->window);
1052 window_get_surface(struct window *window)
1054 return cairo_surface_reference(window->cairo_surface);
1058 window_get_wl_surface(struct window *window)
1060 return window->surface;
1063 struct wl_shell_surface *
1064 window_get_wl_shell_surface(struct window *window)
1066 return window->shell_surface;
1070 tooltip_redraw_handler(struct widget *widget, void *data)
1073 const int32_t r = 3;
1074 struct tooltip *tooltip = data;
1075 int32_t width, height;
1076 struct window *window = widget->window;
1078 cr = cairo_create(window->cairo_surface);
1079 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1080 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1083 width = window->allocation.width;
1084 height = window->allocation.height;
1085 rounded_rect(cr, 0, 0, width, height, r);
1087 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1088 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1091 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1092 cairo_move_to(cr, 10, 16);
1093 cairo_show_text(cr, tooltip->entry);
1097 static cairo_text_extents_t
1098 get_text_extents(struct tooltip *tooltip)
1100 struct window *window;
1102 cairo_text_extents_t extents;
1104 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1106 window = tooltip->widget->window->parent;
1107 cr = cairo_create(window->cairo_surface);
1108 cairo_text_extents(cr, tooltip->entry, &extents);
1115 window_create_tooltip(struct tooltip *tooltip)
1117 struct widget *parent = tooltip->parent;
1118 struct display *display = parent->window->display;
1119 struct window *window;
1120 const int offset_y = 27;
1121 const int margin = 3;
1122 cairo_text_extents_t extents;
1124 if (tooltip->widget)
1127 window = window_create_transient(display, parent->window, tooltip->x,
1128 tooltip->y + offset_y,
1129 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1133 tooltip->window = window;
1134 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1136 extents = get_text_extents(tooltip);
1137 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1138 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1144 widget_destroy_tooltip(struct widget *parent)
1146 struct tooltip *tooltip = parent->tooltip;
1148 parent->tooltip_count = 0;
1152 if (tooltip->widget) {
1153 widget_destroy(tooltip->widget);
1154 window_destroy(tooltip->window);
1155 tooltip->widget = NULL;
1156 tooltip->window = NULL;
1159 close(tooltip->tooltip_fd);
1160 free(tooltip->entry);
1162 parent->tooltip = NULL;
1166 tooltip_func(struct task *task, uint32_t events)
1168 struct tooltip *tooltip =
1169 container_of(task, struct tooltip, tooltip_task);
1172 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1173 window_create_tooltip(tooltip);
1176 #define TOOLTIP_TIMEOUT 500
1178 tooltip_timer_reset(struct tooltip *tooltip)
1180 struct itimerspec its;
1182 its.it_interval.tv_sec = 0;
1183 its.it_interval.tv_nsec = 0;
1184 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1185 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1186 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1187 fprintf(stderr, "could not set timerfd\n: %m");
1195 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1197 struct tooltip *tooltip = parent->tooltip;
1199 parent->tooltip_count++;
1203 tooltip_timer_reset(tooltip);
1207 /* the handler might be triggered too fast via input device motion, so
1208 * we need this check here to make sure tooltip is fully initialized */
1209 if (parent->tooltip_count > 1)
1212 tooltip = malloc(sizeof *tooltip);
1216 parent->tooltip = tooltip;
1217 tooltip->parent = parent;
1218 tooltip->widget = NULL;
1219 tooltip->window = NULL;
1222 tooltip->entry = strdup(entry);
1223 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1224 if (tooltip->tooltip_fd < 0) {
1225 fprintf(stderr, "could not create timerfd\n: %m");
1229 tooltip->tooltip_task.run = tooltip_func;
1230 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1231 EPOLLIN, &tooltip->tooltip_task);
1232 tooltip_timer_reset(tooltip);
1238 frame_resize_handler(struct widget *widget,
1239 int32_t width, int32_t height, void *data)
1241 struct frame *frame = data;
1242 struct widget *child = frame->child;
1243 struct rectangle allocation;
1244 struct display *display = widget->window->display;
1245 struct frame_button * button;
1246 struct theme *t = display->theme;
1247 int x_l, x_r, y, w, h;
1248 int decoration_width, decoration_height;
1251 if (widget->window->type != TYPE_FULLSCREEN) {
1252 decoration_width = (t->width + t->margin) * 2;
1253 decoration_height = t->width +
1254 t->titlebar_height + t->margin * 2;
1256 allocation.x = t->width + t->margin;
1257 allocation.y = t->titlebar_height + t->margin;
1258 allocation.width = width - decoration_width;
1259 allocation.height = height - decoration_height;
1261 opaque_margin = t->margin + t->frame_radius;
1263 wl_list_for_each(button, &frame->buttons_list, link)
1264 button->widget->opaque = 0;
1266 decoration_width = 0;
1267 decoration_height = 0;
1271 allocation.width = width;
1272 allocation.height = height;
1275 wl_list_for_each(button, &frame->buttons_list, link)
1276 button->widget->opaque = 1;
1279 widget_set_allocation(child, allocation.x, allocation.y,
1280 allocation.width, allocation.height);
1282 if (child->resize_handler)
1283 child->resize_handler(child,
1288 width = child->allocation.width + decoration_width;
1289 height = child->allocation.height + decoration_height;
1291 widget->window->input_region =
1292 wl_compositor_create_region(display->compositor);
1293 wl_region_add(widget->window->input_region,
1294 t->margin, t->margin,
1295 width - 2 * t->margin,
1296 height - 2 * t->margin);
1298 widget_set_allocation(widget, 0, 0, width, height);
1300 if (child->opaque) {
1301 widget->window->opaque_region =
1302 wl_compositor_create_region(display->compositor);
1303 wl_region_add(widget->window->opaque_region,
1304 opaque_margin, opaque_margin,
1305 widget->allocation.width - 2 * opaque_margin,
1306 widget->allocation.height - 2 * opaque_margin);
1309 /* frame internal buttons */
1310 x_r = frame->widget->allocation.width - t->width - t->margin;
1311 x_l = t->width + t->margin;
1312 y = t->width + t->margin;
1313 wl_list_for_each(button, &frame->buttons_list, link) {
1314 const int button_padding = 4;
1315 w = cairo_image_surface_get_width(button->icon);
1316 h = cairo_image_surface_get_height(button->icon);
1318 if (button->decoration == FRAME_BUTTON_FANCY)
1321 if (button->align == FRAME_BUTTON_LEFT) {
1322 widget_set_allocation(button->widget,
1323 x_l, y , w + 1, h + 1);
1325 x_l += button_padding;
1328 widget_set_allocation(button->widget,
1329 x_r, y , w + 1, h + 1);
1330 x_r -= button_padding;
1336 frame_button_enter_handler(struct widget *widget,
1337 struct input *input, float x, float y, void *data)
1339 struct frame_button *frame_button = data;
1341 widget_schedule_redraw(frame_button->widget);
1342 frame_button->state = FRAME_BUTTON_OVER;
1344 return CURSOR_LEFT_PTR;
1348 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1350 struct frame_button *frame_button = data;
1352 widget_schedule_redraw(frame_button->widget);
1353 frame_button->state = FRAME_BUTTON_DEFAULT;
1357 frame_button_button_handler(struct widget *widget,
1358 struct input *input, uint32_t time,
1360 enum wl_pointer_button_state state, void *data)
1362 struct frame_button *frame_button = data;
1363 struct window *window = widget->window;
1365 if (button != BTN_LEFT)
1369 case WL_POINTER_BUTTON_STATE_PRESSED:
1370 frame_button->state = FRAME_BUTTON_ACTIVE;
1371 widget_schedule_redraw(frame_button->widget);
1373 if (frame_button->type == FRAME_BUTTON_ICON)
1374 window_show_frame_menu(window, input, time);
1376 case WL_POINTER_BUTTON_STATE_RELEASED:
1377 frame_button->state = FRAME_BUTTON_DEFAULT;
1378 widget_schedule_redraw(frame_button->widget);
1382 switch (frame_button->type) {
1383 case FRAME_BUTTON_CLOSE:
1384 if (window->close_handler)
1385 window->close_handler(window->parent,
1388 display_exit(window->display);
1390 case FRAME_BUTTON_MINIMIZE:
1391 fprintf(stderr,"Minimize stub\n");
1393 case FRAME_BUTTON_MAXIMIZE:
1394 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1397 /* Unknown operation */
1403 frame_button_redraw_handler(struct widget *widget, void *data)
1405 struct frame_button *frame_button = data;
1407 int width, height, x, y;
1408 struct window *window = widget->window;
1410 x = widget->allocation.x;
1411 y = widget->allocation.y;
1412 width = widget->allocation.width;
1413 height = widget->allocation.height;
1422 cr = cairo_create(window->cairo_surface);
1424 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1425 cairo_set_line_width(cr, 1);
1427 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1428 cairo_rectangle (cr, x, y, 25, 16);
1430 cairo_stroke_preserve(cr);
1432 switch (frame_button->state) {
1433 case FRAME_BUTTON_DEFAULT:
1434 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1436 case FRAME_BUTTON_OVER:
1437 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1439 case FRAME_BUTTON_ACTIVE:
1440 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1449 cairo_set_source_surface(cr, frame_button->icon, x, y);
1455 static struct widget *
1456 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1457 enum frame_button_align align, enum frame_button_decoration style)
1459 struct frame_button *frame_button;
1460 const char *icon = data;
1462 frame_button = malloc (sizeof *frame_button);
1463 memset(frame_button, 0, sizeof *frame_button);
1465 frame_button->icon = cairo_image_surface_create_from_png(icon);
1466 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1467 frame_button->frame = frame;
1468 frame_button->type = type;
1469 frame_button->align = align;
1470 frame_button->decoration = style;
1472 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1474 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1475 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1476 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1477 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1478 return frame_button->widget;
1482 frame_button_destroy(struct frame_button *frame_button)
1484 widget_destroy(frame_button->widget);
1485 wl_list_remove(&frame_button->link);
1486 cairo_surface_destroy(frame_button->icon);
1493 frame_redraw_handler(struct widget *widget, void *data)
1496 struct window *window = widget->window;
1497 struct theme *t = window->display->theme;
1500 if (window->type == TYPE_FULLSCREEN)
1503 cr = cairo_create(window->cairo_surface);
1505 if (window->keyboard_device)
1506 flags |= THEME_FRAME_ACTIVE;
1507 theme_render_frame(t, cr, widget->allocation.width,
1508 widget->allocation.height, window->title, flags);
1514 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1516 struct theme *t = frame->widget->window->display->theme;
1519 location = theme_get_location(t, input->sx, input->sy,
1520 frame->widget->allocation.width,
1521 frame->widget->allocation.height);
1524 case THEME_LOCATION_RESIZING_TOP:
1526 case THEME_LOCATION_RESIZING_BOTTOM:
1527 return CURSOR_BOTTOM;
1528 case THEME_LOCATION_RESIZING_LEFT:
1530 case THEME_LOCATION_RESIZING_RIGHT:
1531 return CURSOR_RIGHT;
1532 case THEME_LOCATION_RESIZING_TOP_LEFT:
1533 return CURSOR_TOP_LEFT;
1534 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1535 return CURSOR_TOP_RIGHT;
1536 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1537 return CURSOR_BOTTOM_LEFT;
1538 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1539 return CURSOR_BOTTOM_RIGHT;
1540 case THEME_LOCATION_EXTERIOR:
1541 case THEME_LOCATION_TITLEBAR:
1543 return CURSOR_LEFT_PTR;
1548 frame_menu_func(struct window *window, int index, void *data)
1552 if (window->close_handler)
1553 window->close_handler(window->parent,
1556 display_exit(window->display);
1558 case 1: /* fullscreen */
1559 /* we don't have a way to get out of fullscreen for now */
1560 window_set_fullscreen(window, 1);
1562 case 2: /* rotate */
1569 window_show_frame_menu(struct window *window,
1570 struct input *input, uint32_t time)
1574 static const char *entries[] = {
1575 "Close", "Fullscreen", "Rotate", "Scale"
1578 input_get_position(input, &x, &y);
1579 window_show_menu(window->display, input, time, window,
1580 x - 10, y - 10, frame_menu_func, entries, 4);
1584 frame_enter_handler(struct widget *widget,
1585 struct input *input, float x, float y, void *data)
1587 return frame_get_pointer_image_for_location(data, input);
1591 frame_motion_handler(struct widget *widget,
1592 struct input *input, uint32_t time,
1593 float x, float y, void *data)
1595 return frame_get_pointer_image_for_location(data, input);
1599 frame_button_handler(struct widget *widget,
1600 struct input *input, uint32_t time,
1601 uint32_t button, enum wl_pointer_button_state state,
1605 struct frame *frame = data;
1606 struct window *window = widget->window;
1607 struct display *display = window->display;
1610 location = theme_get_location(display->theme, input->sx, input->sy,
1611 frame->widget->allocation.width,
1612 frame->widget->allocation.height);
1614 if (window->display->shell && button == BTN_LEFT &&
1615 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1617 case THEME_LOCATION_TITLEBAR:
1618 if (!window->shell_surface)
1620 input_set_pointer_image(input, CURSOR_DRAGGING);
1621 input_ungrab(input);
1622 wl_shell_surface_move(window->shell_surface,
1623 input_get_seat(input),
1626 case THEME_LOCATION_RESIZING_TOP:
1627 case THEME_LOCATION_RESIZING_BOTTOM:
1628 case THEME_LOCATION_RESIZING_LEFT:
1629 case THEME_LOCATION_RESIZING_RIGHT:
1630 case THEME_LOCATION_RESIZING_TOP_LEFT:
1631 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1632 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1633 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1634 if (!window->shell_surface)
1636 input_ungrab(input);
1638 if (!display->dpy) {
1639 /* If we're using shm, allocate a big
1640 pool to create buffers out of while
1641 we resize. We should probably base
1642 this number on the size of the output. */
1644 shm_pool_create(display, 6 * 1024 * 1024);
1647 wl_shell_surface_resize(window->shell_surface,
1648 input_get_seat(input),
1649 display->serial, location);
1652 } else if (button == BTN_RIGHT &&
1653 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1654 window_show_frame_menu(window, input, time);
1659 frame_create(struct window *window, void *data)
1661 struct frame *frame;
1663 frame = malloc(sizeof *frame);
1664 memset(frame, 0, sizeof *frame);
1666 frame->widget = window_add_widget(window, frame);
1667 frame->child = widget_add_widget(frame->widget, data);
1669 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1670 widget_set_resize_handler(frame->widget, frame_resize_handler);
1671 widget_set_enter_handler(frame->widget, frame_enter_handler);
1672 widget_set_motion_handler(frame->widget, frame_motion_handler);
1673 widget_set_button_handler(frame->widget, frame_button_handler);
1675 /* Create empty list for frame buttons */
1676 wl_list_init(&frame->buttons_list);
1678 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1679 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1681 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1682 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1684 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1685 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1687 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1688 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1690 window->frame = frame;
1692 return frame->child;
1696 frame_destroy(struct frame *frame)
1698 struct frame_button *button, *tmp;
1700 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1701 frame_button_destroy(button);
1703 /* frame->child must be destroyed by the application */
1704 widget_destroy(frame->widget);
1709 input_set_focus_widget(struct input *input, struct widget *focus,
1712 struct widget *old, *widget;
1713 int pointer = CURSOR_LEFT_PTR;
1715 if (focus == input->focus_widget)
1718 old = input->focus_widget;
1722 widget = input->grab;
1723 if (widget->leave_handler)
1724 widget->leave_handler(old, input, widget->user_data);
1725 input->focus_widget = NULL;
1731 widget = input->grab;
1732 input->focus_widget = focus;
1733 if (widget->enter_handler)
1734 pointer = widget->enter_handler(focus, input, x, y,
1737 input_set_pointer_image(input, pointer);
1742 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1743 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1745 struct input *input = data;
1746 struct window *window = input->pointer_focus;
1747 struct widget *widget;
1748 int cursor = CURSOR_LEFT_PTR;
1749 float sx = wl_fixed_to_double(sx_w);
1750 float sy = wl_fixed_to_double(sy_w);
1755 if (!(input->grab && input->grab_button)) {
1756 widget = widget_find_widget(window->widget, sx, sy);
1757 input_set_focus_widget(input, widget, sx, sy);
1761 widget = input->grab;
1763 widget = input->focus_widget;
1764 if (widget && widget->motion_handler)
1765 cursor = widget->motion_handler(input->focus_widget,
1766 input, time, sx, sy,
1769 input_set_pointer_image(input, cursor);
1773 input_grab(struct input *input, struct widget *widget, uint32_t button)
1775 input->grab = widget;
1776 input->grab_button = button;
1780 input_ungrab(struct input *input)
1782 struct widget *widget;
1785 if (input->pointer_focus) {
1786 widget = widget_find_widget(input->pointer_focus->widget,
1787 input->sx, input->sy);
1788 input_set_focus_widget(input, widget, input->sx, input->sy);
1793 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1794 uint32_t time, uint32_t button, uint32_t state_w)
1796 struct input *input = data;
1797 struct widget *widget;
1798 enum wl_pointer_button_state state = state_w;
1800 input->display->serial = serial;
1801 if (input->focus_widget && input->grab == NULL &&
1802 state == WL_POINTER_BUTTON_STATE_PRESSED)
1803 input_grab(input, input->focus_widget, button);
1805 widget = input->grab;
1806 if (widget && widget->button_handler)
1807 (*widget->button_handler)(widget,
1810 input->grab->user_data);
1812 if (input->grab && input->grab_button == button &&
1813 state == WL_POINTER_BUTTON_STATE_RELEASED)
1814 input_ungrab(input);
1818 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1819 uint32_t time, uint32_t axis, wl_fixed_t value)
1824 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1825 uint32_t serial, uint32_t time, uint32_t key,
1828 struct input *input = data;
1829 struct window *window = input->keyboard_focus;
1830 uint32_t code, num_syms;
1831 enum wl_keyboard_key_state state = state_w;
1832 const xkb_keysym_t *syms;
1834 xkb_mod_mask_t mask;
1835 struct itimerspec its;
1837 input->display->serial = serial;
1839 if (!window || window->keyboard_device != input || !input->xkb.state)
1842 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
1844 mask = xkb_state_serialize_mods(input->xkb.state,
1845 XKB_STATE_DEPRESSED |
1847 input->modifiers = 0;
1848 if (mask & input->xkb.control_mask)
1849 input->modifiers |= MOD_CONTROL_MASK;
1850 if (mask & input->xkb.alt_mask)
1851 input->modifiers |= MOD_ALT_MASK;
1852 if (mask & input->xkb.shift_mask)
1853 input->modifiers |= MOD_SHIFT_MASK;
1855 sym = XKB_KEY_NoSymbol;
1859 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
1860 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1861 window_set_maximized(window,
1862 window->type != TYPE_MAXIMIZED);
1863 } else if (window->key_handler) {
1864 (*window->key_handler)(window, input, time, key,
1865 sym, state, window->user_data);
1868 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1869 key == input->repeat_key) {
1870 its.it_interval.tv_sec = 0;
1871 its.it_interval.tv_nsec = 0;
1872 its.it_value.tv_sec = 0;
1873 its.it_value.tv_nsec = 0;
1874 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1875 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1876 input->repeat_sym = sym;
1877 input->repeat_key = key;
1878 input->repeat_time = time;
1879 its.it_interval.tv_sec = 0;
1880 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1881 its.it_value.tv_sec = 0;
1882 its.it_value.tv_nsec = 400 * 1000 * 1000;
1883 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1888 keyboard_repeat_func(struct task *task, uint32_t events)
1890 struct input *input =
1891 container_of(task, struct input, repeat_task);
1892 struct window *window = input->keyboard_focus;
1895 read(input->repeat_timer_fd, &exp, sizeof (uint64_t));
1897 if (window && window->key_handler) {
1898 (*window->key_handler)(window, input, input->repeat_time,
1899 input->repeat_key, input->repeat_sym,
1900 WL_KEYBOARD_KEY_STATE_PRESSED,
1906 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1907 uint32_t serial, uint32_t mods_depressed,
1908 uint32_t mods_latched, uint32_t mods_locked,
1911 struct input *input = data;
1913 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1914 mods_locked, 0, 0, group);
1918 input_remove_pointer_focus(struct input *input)
1920 struct window *window = input->pointer_focus;
1925 input_set_focus_widget(input, NULL, 0, 0);
1927 input->pointer_focus = NULL;
1928 input->current_cursor = CURSOR_UNSET;
1932 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1933 uint32_t serial, struct wl_surface *surface,
1934 wl_fixed_t sx_w, wl_fixed_t sy_w)
1936 struct input *input = data;
1937 struct window *window;
1938 struct widget *widget;
1939 float sx = wl_fixed_to_double(sx_w);
1940 float sy = wl_fixed_to_double(sy_w);
1943 /* enter event for a window we've just destroyed */
1947 input->display->serial = serial;
1948 input->pointer_enter_serial = serial;
1949 input->pointer_focus = wl_surface_get_user_data(surface);
1950 window = input->pointer_focus;
1953 shm_pool_destroy(window->pool);
1954 window->pool = NULL;
1955 /* Schedule a redraw to free the pool */
1956 window_schedule_redraw(window);
1962 widget = widget_find_widget(window->widget, sx, sy);
1963 input_set_focus_widget(input, widget, sx, sy);
1967 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1968 uint32_t serial, struct wl_surface *surface)
1970 struct input *input = data;
1972 input->display->serial = serial;
1973 input_remove_pointer_focus(input);
1977 input_remove_keyboard_focus(struct input *input)
1979 struct window *window = input->keyboard_focus;
1984 window->keyboard_device = NULL;
1985 if (window->keyboard_focus_handler)
1986 (*window->keyboard_focus_handler)(window, NULL,
1989 input->keyboard_focus = NULL;
1993 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1994 uint32_t serial, struct wl_surface *surface,
1995 struct wl_array *keys)
1997 struct input *input = data;
1998 struct window *window;
2000 input->display->serial = serial;
2001 input->keyboard_focus = wl_surface_get_user_data(surface);
2003 window = input->keyboard_focus;
2004 window->keyboard_device = input;
2005 if (window->keyboard_focus_handler)
2006 (*window->keyboard_focus_handler)(window,
2007 window->keyboard_device,
2012 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2013 uint32_t serial, struct wl_surface *surface)
2015 struct input *input = data;
2017 input->display->serial = serial;
2018 input_remove_keyboard_focus(input);
2022 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2023 uint32_t format, int fd, uint32_t size)
2025 struct input *input = data;
2033 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2038 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2039 if (map_str == MAP_FAILED) {
2044 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2046 XKB_KEYMAP_FORMAT_TEXT_V1,
2048 munmap(map_str, size);
2051 if (!input->xkb.keymap) {
2052 fprintf(stderr, "failed to compile keymap\n");
2056 input->xkb.state = xkb_state_new(input->xkb.keymap);
2057 if (!input->xkb.state) {
2058 fprintf(stderr, "failed to create XKB state\n");
2059 xkb_map_unref(input->xkb.keymap);
2060 input->xkb.keymap = NULL;
2064 input->xkb.control_mask =
2065 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2066 input->xkb.alt_mask =
2067 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2068 input->xkb.shift_mask =
2069 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2072 static const struct wl_pointer_listener pointer_listener = {
2073 pointer_handle_enter,
2074 pointer_handle_leave,
2075 pointer_handle_motion,
2076 pointer_handle_button,
2077 pointer_handle_axis,
2080 static const struct wl_keyboard_listener keyboard_listener = {
2081 keyboard_handle_keymap,
2082 keyboard_handle_enter,
2083 keyboard_handle_leave,
2084 keyboard_handle_key,
2085 keyboard_handle_modifiers,
2089 seat_handle_capabilities(void *data, struct wl_seat *seat,
2090 enum wl_seat_capability caps)
2092 struct input *input = data;
2094 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2095 input->pointer = wl_seat_get_pointer(seat);
2096 wl_pointer_set_user_data(input->pointer, input);
2097 wl_pointer_add_listener(input->pointer, &pointer_listener,
2099 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2100 wl_pointer_destroy(input->pointer);
2101 input->pointer = NULL;
2104 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2105 input->keyboard = wl_seat_get_keyboard(seat);
2106 wl_keyboard_set_user_data(input->keyboard, input);
2107 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2109 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2110 wl_keyboard_destroy(input->keyboard);
2111 input->keyboard = NULL;
2115 static const struct wl_seat_listener seat_listener = {
2116 seat_handle_capabilities,
2120 input_get_position(struct input *input, int32_t *x, int32_t *y)
2127 input_get_display(struct input *input)
2129 return input->display;
2133 input_get_seat(struct input *input)
2139 input_get_modifiers(struct input *input)
2141 return input->modifiers;
2145 input_get_focus_widget(struct input *input)
2147 return input->focus_widget;
2151 struct wl_data_offer *offer;
2152 struct input *input;
2153 struct wl_array types;
2156 struct task io_task;
2164 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2166 struct data_offer *offer = data;
2169 p = wl_array_add(&offer->types, sizeof *p);
2173 static const struct wl_data_offer_listener data_offer_listener = {
2178 data_offer_destroy(struct data_offer *offer)
2183 if (offer->refcount == 0) {
2184 wl_data_offer_destroy(offer->offer);
2185 for (p = offer->types.data; *p; p++)
2187 wl_array_release(&offer->types);
2193 data_device_data_offer(void *data,
2194 struct wl_data_device *data_device, uint32_t id)
2196 struct data_offer *offer;
2198 offer = malloc(sizeof *offer);
2200 wl_array_init(&offer->types);
2201 offer->refcount = 1;
2202 offer->input = data;
2204 /* FIXME: Generate typesafe wrappers for this */
2205 offer->offer = (struct wl_data_offer *)
2206 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2207 id, &wl_data_offer_interface);
2209 wl_data_offer_add_listener(offer->offer,
2210 &data_offer_listener, offer);
2214 data_device_enter(void *data, struct wl_data_device *data_device,
2215 uint32_t serial, struct wl_surface *surface,
2216 wl_fixed_t x_w, wl_fixed_t y_w,
2217 struct wl_data_offer *offer)
2219 struct input *input = data;
2220 struct window *window;
2222 float x = wl_fixed_to_double(x_w);
2223 float y = wl_fixed_to_double(y_w);
2226 input->pointer_enter_serial = serial;
2227 window = wl_surface_get_user_data(surface);
2228 input->pointer_focus = window;
2231 input->drag_offer = wl_data_offer_get_user_data(offer);
2233 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2236 types_data = input->drag_offer->types.data;
2238 input->drag_offer = NULL;
2242 window = input->pointer_focus;
2243 if (window->data_handler)
2244 window->data_handler(window, input, x, y, types_data,
2249 data_device_leave(void *data, struct wl_data_device *data_device)
2251 struct input *input = data;
2253 if (input->drag_offer) {
2254 data_offer_destroy(input->drag_offer);
2255 input->drag_offer = NULL;
2260 data_device_motion(void *data, struct wl_data_device *data_device,
2261 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2263 struct input *input = data;
2264 struct window *window = input->pointer_focus;
2265 float x = wl_fixed_to_double(x_w);
2266 float y = wl_fixed_to_double(y_w);
2272 if (input->drag_offer)
2273 types_data = input->drag_offer->types.data;
2277 if (window->data_handler)
2278 window->data_handler(window, input, x, y, types_data,
2283 data_device_drop(void *data, struct wl_data_device *data_device)
2285 struct input *input = data;
2286 struct window *window = input->pointer_focus;
2288 if (window->drop_handler)
2289 window->drop_handler(window, input,
2290 input->sx, input->sy, window->user_data);
2294 data_device_selection(void *data,
2295 struct wl_data_device *wl_data_device,
2296 struct wl_data_offer *offer)
2298 struct input *input = data;
2301 if (input->selection_offer)
2302 data_offer_destroy(input->selection_offer);
2305 input->selection_offer = wl_data_offer_get_user_data(offer);
2306 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2309 input->selection_offer = NULL;
2313 static const struct wl_data_device_listener data_device_listener = {
2314 data_device_data_offer,
2319 data_device_selection
2323 input_set_pointer_image_index(struct input *input, int index)
2325 struct wl_buffer *buffer;
2326 struct wl_cursor *cursor;
2327 struct wl_cursor_image *image;
2329 cursor = input->display->cursors[input->current_cursor];
2333 if (index >= (int) cursor->image_count) {
2334 fprintf(stderr, "cursor index out of range\n");
2338 image = cursor->images[index];
2339 buffer = wl_cursor_image_get_buffer(image);
2343 wl_pointer_set_cursor(input->pointer, input->display->serial,
2344 input->pointer_surface,
2345 image->hotspot_x, image->hotspot_y);
2346 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2347 wl_surface_damage(input->pointer_surface, 0, 0,
2348 image->width, image->height);
2351 static const struct wl_callback_listener pointer_surface_listener;
2354 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2357 struct input *input = data;
2358 struct wl_cursor *cursor;
2362 assert(callback == input->cursor_frame_cb);
2363 wl_callback_destroy(callback);
2364 input->cursor_frame_cb = NULL;
2367 if (input->current_cursor == CURSOR_UNSET)
2369 cursor = input->display->cursors[input->current_cursor];
2373 /* FIXME We don't have the current time on the first call so we set
2374 * the animation start to the time of the first frame callback. */
2376 input->cursor_anim_start = 0;
2377 else if (input->cursor_anim_start == 0)
2378 input->cursor_anim_start = time;
2380 if (time == 0 || input->cursor_anim_start == 0)
2383 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2385 input_set_pointer_image_index(input, i);
2387 if (cursor->image_count == 1)
2390 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2391 wl_callback_add_listener(input->cursor_frame_cb,
2392 &pointer_surface_listener, input);
2395 static const struct wl_callback_listener pointer_surface_listener = {
2396 pointer_surface_frame_callback
2400 input_set_pointer_image(struct input *input, int pointer)
2402 if (pointer == input->current_cursor)
2405 input->current_cursor = pointer;
2406 if (!input->cursor_frame_cb)
2407 pointer_surface_frame_callback(input, NULL, 0);
2410 struct wl_data_device *
2411 input_get_data_device(struct input *input)
2413 return input->data_device;
2417 input_set_selection(struct input *input,
2418 struct wl_data_source *source, uint32_t time)
2420 wl_data_device_set_selection(input->data_device, source, time);
2424 input_accept(struct input *input, const char *type)
2426 wl_data_offer_accept(input->drag_offer->offer,
2427 input->pointer_enter_serial, type);
2431 offer_io_func(struct task *task, uint32_t events)
2433 struct data_offer *offer =
2434 container_of(task, struct data_offer, io_task);
2438 len = read(offer->fd, buffer, sizeof buffer);
2439 offer->func(buffer, len,
2440 offer->x, offer->y, offer->user_data);
2444 data_offer_destroy(offer);
2449 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2450 data_func_t func, void *user_data)
2454 if (pipe2(p, O_CLOEXEC) == -1)
2457 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2460 offer->io_task.run = offer_io_func;
2464 offer->user_data = user_data;
2466 display_watch_fd(offer->input->display,
2467 offer->fd, EPOLLIN, &offer->io_task);
2471 input_receive_drag_data(struct input *input, const char *mime_type,
2472 data_func_t func, void *data)
2474 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2475 input->drag_offer->x = input->sx;
2476 input->drag_offer->y = input->sy;
2480 input_receive_selection_data(struct input *input, const char *mime_type,
2481 data_func_t func, void *data)
2485 if (input->selection_offer == NULL)
2488 for (p = input->selection_offer->types.data; *p; p++)
2489 if (strcmp(mime_type, *p) == 0)
2495 data_offer_receive_data(input->selection_offer,
2496 mime_type, func, data);
2501 input_receive_selection_data_to_fd(struct input *input,
2502 const char *mime_type, int fd)
2504 if (input->selection_offer)
2505 wl_data_offer_receive(input->selection_offer->offer,
2512 window_move(struct window *window, struct input *input, uint32_t serial)
2514 if (!window->shell_surface)
2517 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2521 idle_resize(struct window *window)
2523 struct widget *widget;
2525 window->resize_needed = 0;
2526 widget = window->widget;
2527 widget_set_allocation(widget,
2528 window->pending_allocation.x,
2529 window->pending_allocation.y,
2530 window->pending_allocation.width,
2531 window->pending_allocation.height);
2533 if (window->input_region) {
2534 wl_region_destroy(window->input_region);
2535 window->input_region = NULL;
2538 if (window->opaque_region) {
2539 wl_region_destroy(window->opaque_region);
2540 window->opaque_region = NULL;
2543 if (widget->resize_handler)
2544 widget->resize_handler(widget,
2545 widget->allocation.width,
2546 widget->allocation.height,
2549 if (window->allocation.width != widget->allocation.width ||
2550 window->allocation.height != widget->allocation.height) {
2551 window->allocation = widget->allocation;
2552 window_schedule_redraw(window);
2557 window_schedule_resize(struct window *window, int width, int height)
2559 window->pending_allocation.x = 0;
2560 window->pending_allocation.y = 0;
2561 window->pending_allocation.width = width;
2562 window->pending_allocation.height = height;
2564 window->resize_needed = 1;
2565 window_schedule_redraw(window);
2569 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2571 window_schedule_resize(widget->window, width, height);
2575 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2578 wl_shell_surface_pong(shell_surface, serial);
2582 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2583 uint32_t edges, int32_t width, int32_t height)
2585 struct window *window = data;
2587 if (width <= 0 || height <= 0)
2590 window->resize_edges = edges;
2591 window_schedule_resize(window, width, height);
2595 menu_destroy(struct menu *menu)
2597 widget_destroy(menu->widget);
2598 window_destroy(menu->window);
2603 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2605 struct window *window = data;
2606 struct menu *menu = window->widget->user_data;
2608 /* FIXME: Need more context in this event, at least the input
2609 * device. Or just use wl_callback. And this really needs to
2610 * be a window vfunc that the menu can set. And we need the
2613 menu->func(window->parent, menu->current, window->parent->user_data);
2614 input_ungrab(menu->input);
2618 static const struct wl_shell_surface_listener shell_surface_listener = {
2625 window_get_allocation(struct window *window,
2626 struct rectangle *allocation)
2628 *allocation = window->allocation;
2632 widget_redraw(struct widget *widget)
2634 struct widget *child;
2636 if (widget->redraw_handler)
2637 widget->redraw_handler(widget, widget->user_data);
2638 wl_list_for_each(child, &widget->child_list, link)
2639 widget_redraw(child);
2643 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2645 struct window *window = data;
2647 wl_callback_destroy(callback);
2648 window->redraw_scheduled = 0;
2649 if (window->redraw_needed)
2650 window_schedule_redraw(window);
2653 static const struct wl_callback_listener listener = {
2658 idle_redraw(struct task *task, uint32_t events)
2660 struct window *window = container_of(task, struct window, redraw_task);
2661 struct wl_callback *callback;
2663 if (window->resize_needed)
2664 idle_resize(window);
2666 window_create_surface(window);
2667 widget_redraw(window->widget);
2668 window_flush(window);
2669 window->redraw_needed = 0;
2670 wl_list_init(&window->redraw_task.link);
2672 callback = wl_surface_frame(window->surface);
2673 wl_callback_add_listener(callback, &listener, window);
2677 window_schedule_redraw(struct window *window)
2679 window->redraw_needed = 1;
2680 if (!window->redraw_scheduled) {
2681 window->redraw_task.run = idle_redraw;
2682 display_defer(window->display, &window->redraw_task);
2683 window->redraw_scheduled = 1;
2688 window_set_custom(struct window *window)
2690 window->type = TYPE_CUSTOM;
2694 window_set_fullscreen(struct window *window, int fullscreen)
2696 if (!window->display->shell)
2699 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2703 window->type = TYPE_FULLSCREEN;
2704 window->saved_allocation = window->allocation;
2705 wl_shell_surface_set_fullscreen(window->shell_surface,
2706 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2709 window->type = TYPE_TOPLEVEL;
2710 wl_shell_surface_set_toplevel(window->shell_surface);
2711 window_schedule_resize(window,
2712 window->saved_allocation.width,
2713 window->saved_allocation.height);
2718 window_set_maximized(struct window *window, int maximized)
2720 if (!window->display->shell)
2723 if ((window->type == TYPE_MAXIMIZED) == maximized)
2726 if (window->type == TYPE_TOPLEVEL) {
2727 window->saved_allocation = window->allocation;
2728 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2729 window->type = TYPE_MAXIMIZED;
2731 wl_shell_surface_set_toplevel(window->shell_surface);
2732 window->type = TYPE_TOPLEVEL;
2733 window_schedule_resize(window,
2734 window->saved_allocation.width,
2735 window->saved_allocation.height);
2740 window_set_user_data(struct window *window, void *data)
2742 window->user_data = data;
2746 window_get_user_data(struct window *window)
2748 return window->user_data;
2752 window_set_key_handler(struct window *window,
2753 window_key_handler_t handler)
2755 window->key_handler = handler;
2759 window_set_keyboard_focus_handler(struct window *window,
2760 window_keyboard_focus_handler_t handler)
2762 window->keyboard_focus_handler = handler;
2766 window_set_data_handler(struct window *window, window_data_handler_t handler)
2768 window->data_handler = handler;
2772 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2774 window->drop_handler = handler;
2778 window_set_close_handler(struct window *window,
2779 window_close_handler_t handler)
2781 window->close_handler = handler;
2785 window_set_title(struct window *window, const char *title)
2787 free(window->title);
2788 window->title = strdup(title);
2789 if (window->shell_surface)
2790 wl_shell_surface_set_title(window->shell_surface, title);
2794 window_get_title(struct window *window)
2796 return window->title;
2800 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2802 struct text_cursor_position *text_cursor_position =
2803 window->display->text_cursor_position;
2805 if (!text_cursor_position)
2808 text_cursor_position_notify(text_cursor_position,
2810 wl_fixed_from_int(x),
2811 wl_fixed_from_int(y));
2815 window_damage(struct window *window, int32_t x, int32_t y,
2816 int32_t width, int32_t height)
2818 wl_surface_damage(window->surface, x, y, width, height);
2822 surface_enter(void *data,
2823 struct wl_surface *wl_surface, struct wl_output *wl_output)
2825 struct window *window = data;
2826 struct output *output;
2827 struct output *output_found = NULL;
2828 struct window_output *window_output;
2830 wl_list_for_each(output, &window->display->output_list, link) {
2831 if (output->output == wl_output) {
2832 output_found = output;
2840 window_output = malloc (sizeof *window_output);
2841 window_output->output = output_found;
2843 wl_list_insert (&window->window_output_list, &window_output->link);
2847 surface_leave(void *data,
2848 struct wl_surface *wl_surface, struct wl_output *output)
2850 struct window *window = data;
2851 struct window_output *window_output;
2852 struct window_output *window_output_found = NULL;
2854 wl_list_for_each(window_output, &window->window_output_list, link) {
2855 if (window_output->output->output == output) {
2856 window_output_found = window_output;
2861 if (window_output_found) {
2862 wl_list_remove(&window_output_found->link);
2863 free(window_output_found);
2867 static const struct wl_surface_listener surface_listener = {
2872 static struct window *
2873 window_create_internal(struct display *display, struct window *parent)
2875 struct window *window;
2877 window = malloc(sizeof *window);
2881 memset(window, 0, sizeof *window);
2882 window->display = display;
2883 window->parent = parent;
2884 window->surface = wl_compositor_create_surface(display->compositor);
2885 wl_surface_add_listener(window->surface, &surface_listener, window);
2886 if (display->shell) {
2887 window->shell_surface =
2888 wl_shell_get_shell_surface(display->shell,
2891 wl_shell_surface_set_title(window->shell_surface,
2894 window->allocation.x = 0;
2895 window->allocation.y = 0;
2896 window->allocation.width = 0;
2897 window->allocation.height = 0;
2898 window->saved_allocation = window->allocation;
2899 window->transparent = 1;
2900 window->type = TYPE_NONE;
2901 window->input_region = NULL;
2902 window->opaque_region = NULL;
2905 #ifdef HAVE_CAIRO_EGL
2906 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2908 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2911 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2913 wl_surface_set_user_data(window->surface, window);
2914 wl_list_insert(display->window_list.prev, &window->link);
2915 wl_list_init(&window->redraw_task.link);
2917 if (window->shell_surface) {
2918 wl_shell_surface_set_user_data(window->shell_surface, window);
2919 wl_shell_surface_add_listener(window->shell_surface,
2920 &shell_surface_listener, window);
2923 wl_list_init (&window->window_output_list);
2929 window_create(struct display *display)
2931 struct window *window;
2933 window = window_create_internal(display, NULL);
2941 window_create_transient(struct display *display, struct window *parent,
2942 int32_t x, int32_t y, uint32_t flags)
2944 struct window *window;
2946 window = window_create_internal(parent->display, parent);
2950 window->type = TYPE_TRANSIENT;
2955 wl_shell_surface_set_transient(window->shell_surface,
2956 window->parent->shell_surface,
2957 window->x, window->y, flags);
2963 menu_set_item(struct menu *menu, int sy)
2967 next = (sy - 8) / 20;
2968 if (menu->current != next) {
2969 menu->current = next;
2970 widget_schedule_redraw(menu->widget);
2975 menu_motion_handler(struct widget *widget,
2976 struct input *input, uint32_t time,
2977 float x, float y, void *data)
2979 struct menu *menu = data;
2981 if (widget == menu->widget)
2982 menu_set_item(data, y);
2984 return CURSOR_LEFT_PTR;
2988 menu_enter_handler(struct widget *widget,
2989 struct input *input, float x, float y, void *data)
2991 struct menu *menu = data;
2993 if (widget == menu->widget)
2994 menu_set_item(data, y);
2996 return CURSOR_LEFT_PTR;
3000 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3002 struct menu *menu = data;
3004 if (widget == menu->widget)
3005 menu_set_item(data, -200);
3009 menu_button_handler(struct widget *widget,
3010 struct input *input, uint32_t time,
3011 uint32_t button, enum wl_pointer_button_state state,
3015 struct menu *menu = data;
3017 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3018 time - menu->time > 500) {
3019 /* Either relase after press-drag-release or
3020 * click-motion-click. */
3021 menu->func(menu->window->parent,
3022 menu->current, menu->window->parent->user_data);
3023 input_ungrab(input);
3029 menu_redraw_handler(struct widget *widget, void *data)
3032 const int32_t r = 3, margin = 3;
3033 struct menu *menu = data;
3034 int32_t width, height, i;
3035 struct window *window = widget->window;
3037 cr = cairo_create(window->cairo_surface);
3038 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3039 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3042 width = window->allocation.width;
3043 height = window->allocation.height;
3044 rounded_rect(cr, 0, 0, width, height, r);
3046 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3047 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3050 for (i = 0; i < menu->count; i++) {
3051 if (i == menu->current) {
3052 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3053 cairo_rectangle(cr, margin, i * 20 + margin,
3054 width - 2 * margin, 20);
3056 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3057 cairo_move_to(cr, 10, i * 20 + 16);
3058 cairo_show_text(cr, menu->entries[i]);
3060 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3061 cairo_move_to(cr, 10, i * 20 + 16);
3062 cairo_show_text(cr, menu->entries[i]);
3070 window_show_menu(struct display *display,
3071 struct input *input, uint32_t time, struct window *parent,
3072 int32_t x, int32_t y,
3073 menu_func_t func, const char **entries, int count)
3075 struct window *window;
3077 const int32_t margin = 3;
3079 menu = malloc(sizeof *menu);
3083 window = window_create_internal(parent->display, parent);
3087 menu->window = window;
3088 menu->widget = window_add_widget(menu->window, menu);
3089 menu->entries = entries;
3090 menu->count = count;
3094 menu->input = input;
3095 window->type = TYPE_MENU;
3099 input_ungrab(input);
3100 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3101 display_get_serial(window->display),
3102 window->parent->shell_surface,
3103 window->x, window->y, 0);
3105 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3106 widget_set_enter_handler(menu->widget, menu_enter_handler);
3107 widget_set_leave_handler(menu->widget, menu_leave_handler);
3108 widget_set_motion_handler(menu->widget, menu_motion_handler);
3109 widget_set_button_handler(menu->widget, menu_button_handler);
3111 input_grab(input, menu->widget, 0);
3112 window_schedule_resize(window, 200, count * 20 + margin * 2);
3116 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3118 window->buffer_type = type;
3123 display_handle_geometry(void *data,
3124 struct wl_output *wl_output,
3127 int physical_height,
3132 struct output *output = data;
3134 output->allocation.x = x;
3135 output->allocation.y = y;
3139 display_handle_mode(void *data,
3140 struct wl_output *wl_output,
3146 struct output *output = data;
3147 struct display *display = output->display;
3149 if (flags & WL_OUTPUT_MODE_CURRENT) {
3150 output->allocation.width = width;
3151 output->allocation.height = height;
3152 if (display->output_configure_handler)
3153 (*display->output_configure_handler)(
3154 output, display->user_data);
3158 static const struct wl_output_listener output_listener = {
3159 display_handle_geometry,
3164 display_add_output(struct display *d, uint32_t id)
3166 struct output *output;
3168 output = malloc(sizeof *output);
3172 memset(output, 0, sizeof *output);
3173 output->display = d;
3175 wl_display_bind(d->display, id, &wl_output_interface);
3176 wl_list_insert(d->output_list.prev, &output->link);
3178 wl_output_add_listener(output->output, &output_listener, output);
3182 output_destroy(struct output *output)
3184 if (output->destroy_handler)
3185 (*output->destroy_handler)(output, output->user_data);
3187 wl_output_destroy(output->output);
3188 wl_list_remove(&output->link);
3193 display_set_output_configure_handler(struct display *display,
3194 display_output_handler_t handler)
3196 struct output *output;
3198 display->output_configure_handler = handler;
3202 wl_list_for_each(output, &display->output_list, link)
3203 (*display->output_configure_handler)(output,
3204 display->user_data);
3208 output_set_user_data(struct output *output, void *data)
3210 output->user_data = data;
3214 output_get_user_data(struct output *output)
3216 return output->user_data;
3220 output_set_destroy_handler(struct output *output,
3221 display_output_handler_t handler)
3223 output->destroy_handler = handler;
3224 /* FIXME: implement this, once we have way to remove outputs */
3228 output_get_allocation(struct output *output, struct rectangle *allocation)
3230 *allocation = output->allocation;
3234 output_get_wl_output(struct output *output)
3236 return output->output;
3240 fini_xkb(struct input *input)
3242 xkb_state_unref(input->xkb.state);
3243 xkb_map_unref(input->xkb.keymap);
3247 display_add_input(struct display *d, uint32_t id)
3249 struct input *input;
3251 input = malloc(sizeof *input);
3255 memset(input, 0, sizeof *input);
3257 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3258 input->pointer_focus = NULL;
3259 input->keyboard_focus = NULL;
3260 wl_list_insert(d->input_list.prev, &input->link);
3262 wl_seat_add_listener(input->seat, &seat_listener, input);
3263 wl_seat_set_user_data(input->seat, input);
3265 input->data_device =
3266 wl_data_device_manager_get_data_device(d->data_device_manager,
3268 wl_data_device_add_listener(input->data_device, &data_device_listener,
3271 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3273 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
3274 input->repeat_task.run = keyboard_repeat_func;
3275 display_watch_fd(d, input->repeat_timer_fd,
3276 EPOLLIN, &input->repeat_task);
3280 input_destroy(struct input *input)
3282 input_remove_keyboard_focus(input);
3283 input_remove_pointer_focus(input);
3285 if (input->xkb.state)
3286 xkb_state_unref(input->xkb.state);
3287 if (input->xkb.keymap)
3288 xkb_map_unref(input->xkb.keymap);
3290 if (input->drag_offer)
3291 data_offer_destroy(input->drag_offer);
3293 if (input->selection_offer)
3294 data_offer_destroy(input->selection_offer);
3296 wl_data_device_destroy(input->data_device);
3299 wl_surface_destroy(input->pointer_surface);
3301 wl_list_remove(&input->link);
3302 wl_seat_destroy(input->seat);
3303 close(input->repeat_timer_fd);
3308 display_handle_global(struct wl_display *display, uint32_t id,
3309 const char *interface, uint32_t version, void *data)
3311 struct display *d = data;
3313 if (strcmp(interface, "wl_compositor") == 0) {
3315 wl_display_bind(display, id, &wl_compositor_interface);
3316 } else if (strcmp(interface, "wl_output") == 0) {
3317 display_add_output(d, id);
3318 } else if (strcmp(interface, "wl_seat") == 0) {
3319 display_add_input(d, id);
3320 } else if (strcmp(interface, "wl_shell") == 0) {
3321 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3322 } else if (strcmp(interface, "wl_shm") == 0) {
3323 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3324 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3325 d->data_device_manager =
3326 wl_display_bind(display, id,
3327 &wl_data_device_manager_interface);
3328 } else if (strcmp(interface, "text_cursor_position") == 0) {
3329 d->text_cursor_position =
3330 wl_display_bind(display, id,
3331 &text_cursor_position_interface);
3335 #ifdef HAVE_CAIRO_EGL
3337 init_egl(struct display *d)
3339 EGLint major, minor;
3342 #ifdef USE_CAIRO_GLESV2
3343 # define GL_BIT EGL_OPENGL_ES2_BIT
3345 # define GL_BIT EGL_OPENGL_BIT
3348 static const EGLint argb_cfg_attribs[] = {
3349 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3355 EGL_RENDERABLE_TYPE, GL_BIT,
3359 #ifdef USE_CAIRO_GLESV2
3360 static const EGLint context_attribs[] = {
3361 EGL_CONTEXT_CLIENT_VERSION, 2,
3364 EGLint api = EGL_OPENGL_ES_API;
3366 EGLint *context_attribs = NULL;
3367 EGLint api = EGL_OPENGL_API;
3370 d->dpy = eglGetDisplay(d->display);
3371 if (!eglInitialize(d->dpy, &major, &minor)) {
3372 fprintf(stderr, "failed to initialize display\n");
3376 if (!eglBindAPI(api)) {
3377 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3381 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3382 &d->argb_config, 1, &n) || n != 1) {
3383 fprintf(stderr, "failed to choose argb config\n");
3387 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3388 EGL_NO_CONTEXT, context_attribs);
3389 if (d->argb_ctx == NULL) {
3390 fprintf(stderr, "failed to create context\n");
3394 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3395 fprintf(stderr, "failed to make context current\n");
3399 #ifdef HAVE_CAIRO_EGL
3400 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3401 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3402 fprintf(stderr, "failed to get cairo egl argb device\n");
3411 fini_egl(struct display *display)
3413 #ifdef HAVE_CAIRO_EGL
3414 cairo_device_destroy(display->argb_device);
3417 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3420 eglTerminate(display->dpy);
3426 event_mask_update(uint32_t mask, void *data)
3428 struct display *d = data;
3436 handle_display_data(struct task *task, uint32_t events)
3438 struct display *display =
3439 container_of(task, struct display, display_task);
3441 wl_display_iterate(display->display, display->mask);
3445 display_create(int argc, char *argv[])
3449 argc = parse_options(xkb_options,
3450 ARRAY_LENGTH(xkb_options), argc, argv);
3452 d = malloc(sizeof *d);
3456 memset(d, 0, sizeof *d);
3458 d->display = wl_display_connect(NULL);
3459 if (d->display == NULL) {
3460 fprintf(stderr, "failed to create display: %m\n");
3464 d->epoll_fd = os_epoll_create_cloexec();
3465 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3466 d->display_task.run = handle_display_data;
3467 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3469 wl_list_init(&d->deferred_list);
3470 wl_list_init(&d->input_list);
3471 wl_list_init(&d->output_list);
3473 d->xkb_context = xkb_context_new(0);
3474 if (d->xkb_context == NULL) {
3475 fprintf(stderr, "Failed to create XKB context\n");
3479 /* Set up listener so we'll catch all events. */
3480 wl_display_add_global_listener(d->display,
3481 display_handle_global, d);
3483 /* Process connection events. */
3484 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3485 #ifdef HAVE_CAIRO_EGL
3486 if (init_egl(d) < 0)
3490 d->image_target_texture_2d =
3491 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3492 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3493 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3497 d->theme = theme_create();
3499 wl_list_init(&d->window_list);
3505 display_destroy_outputs(struct display *display)
3508 struct output *output;
3510 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3511 output_destroy(output);
3515 display_destroy_inputs(struct display *display)
3518 struct input *input;
3520 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3521 input_destroy(input);
3525 display_destroy(struct display *display)
3527 if (!wl_list_empty(&display->window_list))
3528 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3530 if (!wl_list_empty(&display->deferred_list))
3531 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3533 display_destroy_outputs(display);
3534 display_destroy_inputs(display);
3536 xkb_context_unref(display->xkb_context);
3538 theme_destroy(display->theme);
3539 destroy_cursors(display);
3541 #ifdef HAVE_CAIRO_EGL
3546 wl_shell_destroy(display->shell);
3549 wl_shm_destroy(display->shm);
3551 if (display->data_device_manager)
3552 wl_data_device_manager_destroy(display->data_device_manager);
3554 wl_compositor_destroy(display->compositor);
3556 close(display->epoll_fd);
3558 wl_display_flush(display->display);
3559 wl_display_disconnect(display->display);
3564 display_set_user_data(struct display *display, void *data)
3566 display->user_data = data;
3570 display_get_user_data(struct display *display)
3572 return display->user_data;
3576 display_get_display(struct display *display)
3578 return display->display;
3582 display_get_output(struct display *display)
3584 return container_of(display->output_list.next, struct output, link);
3587 struct wl_compositor *
3588 display_get_compositor(struct display *display)
3590 return display->compositor;
3594 display_get_serial(struct display *display)
3596 return display->serial;
3600 display_get_egl_display(struct display *d)
3605 struct wl_data_source *
3606 display_create_data_source(struct display *display)
3608 return wl_data_device_manager_create_data_source(display->data_device_manager);
3612 display_get_argb_egl_config(struct display *d)
3614 return d->argb_config;
3618 display_get_shell(struct display *display)
3620 return display->shell;
3624 display_acquire_window_surface(struct display *display,
3625 struct window *window,
3628 #ifdef HAVE_CAIRO_EGL
3629 struct egl_window_surface_data *data;
3630 cairo_device_t *device;
3632 if (!window->cairo_surface)
3634 device = cairo_surface_get_device(window->cairo_surface);
3639 if (device == display->argb_device)
3640 ctx = display->argb_ctx;
3645 data = cairo_surface_get_user_data(window->cairo_surface,
3648 cairo_device_flush(device);
3649 cairo_device_acquire(device);
3650 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3651 fprintf(stderr, "failed to make surface current\n");
3660 display_release_window_surface(struct display *display,
3661 struct window *window)
3663 #ifdef HAVE_CAIRO_EGL
3664 cairo_device_t *device;
3666 device = cairo_surface_get_device(window->cairo_surface);
3670 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3671 fprintf(stderr, "failed to make context current\n");
3672 cairo_device_release(device);
3677 display_defer(struct display *display, struct task *task)
3679 wl_list_insert(&display->deferred_list, &task->link);
3683 display_watch_fd(struct display *display,
3684 int fd, uint32_t events, struct task *task)
3686 struct epoll_event ep;
3690 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3694 display_run(struct display *display)
3697 struct epoll_event ep[16];
3700 display->running = 1;
3702 wl_display_flush(display->display);
3704 while (!wl_list_empty(&display->deferred_list)) {
3705 task = container_of(display->deferred_list.next,
3707 wl_list_remove(&task->link);
3711 if (!display->running)
3714 wl_display_flush(display->display);
3716 count = epoll_wait(display->epoll_fd,
3717 ep, ARRAY_LENGTH(ep), -1);
3718 for (i = 0; i < count; i++) {
3719 task = ep[i].data.ptr;
3720 task->run(task, ep[i].events);
3726 display_exit(struct display *display)
3728 display->running = 0;