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;
197 uint32_t cursor_serial;
201 struct widget *focus_widget;
203 uint32_t grab_button;
205 struct wl_data_device *data_device;
206 struct data_offer *drag_offer;
207 struct data_offer *selection_offer;
210 struct xkb_keymap *keymap;
211 struct xkb_state *state;
212 xkb_mod_mask_t control_mask;
213 xkb_mod_mask_t alt_mask;
214 xkb_mod_mask_t shift_mask;
217 struct task repeat_task;
221 uint32_t repeat_time;
225 struct display *display;
226 struct wl_output *output;
227 struct rectangle allocation;
230 display_output_handler_t destroy_handler;
234 enum frame_button_action {
235 FRAME_BUTTON_NULL = 0,
236 FRAME_BUTTON_ICON = 1,
237 FRAME_BUTTON_CLOSE = 2,
238 FRAME_BUTTON_MINIMIZE = 3,
239 FRAME_BUTTON_MAXIMIZE = 4,
242 enum frame_button_pointer {
243 FRAME_BUTTON_DEFAULT = 0,
244 FRAME_BUTTON_OVER = 1,
245 FRAME_BUTTON_ACTIVE = 2,
248 enum frame_button_align {
249 FRAME_BUTTON_RIGHT = 0,
250 FRAME_BUTTON_LEFT = 1,
253 enum frame_button_decoration {
254 FRAME_BUTTON_NONE = 0,
255 FRAME_BUTTON_FANCY = 1,
258 struct frame_button {
259 struct widget *widget;
261 cairo_surface_t *icon;
262 enum frame_button_action type;
263 enum frame_button_pointer state;
264 struct wl_list link; /* buttons_list */
265 enum frame_button_align align;
266 enum frame_button_decoration decoration;
270 struct widget *widget;
271 struct widget *child;
272 struct wl_list buttons_list;
276 struct window *window;
277 struct widget *widget;
279 const char **entries;
287 struct widget *parent;
288 struct window *window;
289 struct widget *widget;
291 struct task tooltip_task;
297 struct wl_shm_pool *pool;
304 CURSOR_DEFAULT = 100,
308 enum window_location {
310 WINDOW_RESIZING_TOP = 1,
311 WINDOW_RESIZING_BOTTOM = 2,
312 WINDOW_RESIZING_LEFT = 4,
313 WINDOW_RESIZING_TOP_LEFT = 5,
314 WINDOW_RESIZING_BOTTOM_LEFT = 6,
315 WINDOW_RESIZING_RIGHT = 8,
316 WINDOW_RESIZING_TOP_RIGHT = 9,
317 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
318 WINDOW_RESIZING_MASK = 15,
319 WINDOW_EXTERIOR = 16,
320 WINDOW_TITLEBAR = 17,
321 WINDOW_CLIENT_AREA = 18,
324 const char *option_xkb_layout = "us";
325 const char *option_xkb_variant = "";
326 const char *option_xkb_options = "";
328 static const struct weston_option xkb_options[] = {
329 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
330 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
331 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
334 static const cairo_user_data_key_t surface_data_key;
335 struct surface_data {
336 struct wl_buffer *buffer;
339 #define MULT(_d,c,a,t) \
340 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
342 #ifdef HAVE_CAIRO_EGL
344 struct egl_window_surface_data {
345 struct display *display;
346 struct wl_surface *surface;
347 struct wl_egl_window *window;
352 egl_window_surface_data_destroy(void *p)
354 struct egl_window_surface_data *data = p;
355 struct display *d = data->display;
357 eglDestroySurface(d->dpy, data->surf);
358 wl_egl_window_destroy(data->window);
359 data->surface = NULL;
364 static cairo_surface_t *
365 display_create_egl_window_surface(struct display *display,
366 struct wl_surface *surface,
368 struct rectangle *rectangle)
370 cairo_surface_t *cairo_surface;
371 struct egl_window_surface_data *data;
373 cairo_device_t *device;
375 data = malloc(sizeof *data);
379 data->display = display;
380 data->surface = surface;
382 config = display->argb_config;
383 device = display->argb_device;
385 data->window = wl_egl_window_create(surface,
389 data->surf = eglCreateWindowSurface(display->dpy, config,
392 cairo_surface = cairo_gl_surface_create_for_egl(device,
397 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
398 data, egl_window_surface_data_destroy);
400 return cairo_surface;
406 display_get_buffer_for_surface(struct display *display,
407 cairo_surface_t *surface)
409 struct surface_data *data;
411 data = cairo_surface_get_user_data (surface, &surface_data_key);
416 struct shm_surface_data {
417 struct surface_data data;
418 struct shm_pool *pool;
422 shm_pool_destroy(struct shm_pool *pool);
425 shm_surface_data_destroy(void *p)
427 struct shm_surface_data *data = p;
429 wl_buffer_destroy(data->data.buffer);
431 shm_pool_destroy(data->pool);
436 static struct wl_shm_pool *
437 make_shm_pool(struct display *display, int size, void **data)
439 struct wl_shm_pool *pool;
442 fd = os_create_anonymous_file(size);
444 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
449 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
450 if (*data == MAP_FAILED) {
451 fprintf(stderr, "mmap failed: %m\n");
456 pool = wl_shm_create_pool(display->shm, fd, size);
463 static struct shm_pool *
464 shm_pool_create(struct display *display, size_t size)
466 struct shm_pool *pool = malloc(sizeof *pool);
471 pool->pool = make_shm_pool(display, size, &pool->data);
484 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
486 if (pool->used + size > pool->size)
489 *offset = pool->used;
492 return (char *) pool->data + *offset;
495 /* destroy the pool. this does not unmap the memory though */
497 shm_pool_destroy(struct shm_pool *pool)
499 munmap(pool->data, pool->size);
500 wl_shm_pool_destroy(pool->pool);
504 /* Start allocating from the beginning of the pool again */
506 shm_pool_reset(struct shm_pool *pool)
512 data_length_for_shm_surface(struct rectangle *rect)
516 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
518 return stride * rect->height;
521 static cairo_surface_t *
522 display_create_shm_surface_from_pool(struct display *display,
523 struct rectangle *rectangle,
524 uint32_t flags, struct shm_pool *pool)
526 struct shm_surface_data *data;
528 cairo_surface_t *surface;
529 int stride, length, offset;
532 data = malloc(sizeof *data);
536 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
538 length = stride * rectangle->height;
540 map = shm_pool_allocate(pool, length, &offset);
547 surface = cairo_image_surface_create_for_data (map,
553 cairo_surface_set_user_data (surface, &surface_data_key,
554 data, shm_surface_data_destroy);
556 if (flags & SURFACE_OPAQUE)
557 format = WL_SHM_FORMAT_XRGB8888;
559 format = WL_SHM_FORMAT_ARGB8888;
561 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
569 static cairo_surface_t *
570 display_create_shm_surface(struct display *display,
571 struct rectangle *rectangle, uint32_t flags,
572 struct window *window)
574 struct shm_surface_data *data;
575 struct shm_pool *pool;
576 cairo_surface_t *surface;
578 if (window && window->pool) {
579 shm_pool_reset(window->pool);
580 surface = display_create_shm_surface_from_pool(display,
588 pool = shm_pool_create(display,
589 data_length_for_shm_surface(rectangle));
594 display_create_shm_surface_from_pool(display, rectangle,
598 shm_pool_destroy(pool);
602 /* make sure we destroy the pool when the surface is destroyed */
603 data = cairo_surface_get_user_data(surface, &surface_data_key);
610 check_size(struct rectangle *rect)
612 if (rect->width && rect->height)
615 fprintf(stderr, "tried to create surface of "
616 "width: %d, height: %d\n", rect->width, rect->height);
621 display_create_surface(struct display *display,
622 struct wl_surface *surface,
623 struct rectangle *rectangle,
626 if (check_size(rectangle) < 0)
628 #ifdef HAVE_CAIRO_EGL
629 if (display->dpy && !(flags & SURFACE_SHM))
630 return display_create_egl_window_surface(display,
635 return display_create_shm_surface(display, rectangle, flags, NULL);
638 static const char *cursors[] = {
639 "bottom_left_corner",
640 "bottom_right_corner",
655 create_cursors(struct display *display)
659 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
661 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
663 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
664 display->cursors[i] =
665 wl_cursor_theme_get_cursor(display->cursor_theme,
670 destroy_cursors(struct display *display)
672 wl_cursor_theme_destroy(display->cursor_theme);
673 free(display->cursors);
676 struct wl_cursor_image *
677 display_get_pointer_image(struct display *display, int pointer)
679 struct wl_cursor *cursor =
680 wl_cursor_theme_get_cursor(display->cursor_theme,
683 return cursor ? cursor->images[0] : NULL;
687 window_get_resize_dx_dy(struct window *window, int *x, int *y)
689 if (window->resize_edges & WINDOW_RESIZING_LEFT)
690 *x = window->server_allocation.width - window->allocation.width;
694 if (window->resize_edges & WINDOW_RESIZING_TOP)
695 *y = window->server_allocation.height -
696 window->allocation.height;
700 window->resize_edges = 0;
704 window_attach_surface(struct window *window)
706 struct display *display = window->display;
707 struct wl_buffer *buffer;
708 #ifdef HAVE_CAIRO_EGL
709 struct egl_window_surface_data *data;
713 if (window->type == TYPE_NONE) {
714 window->type = TYPE_TOPLEVEL;
716 wl_shell_surface_set_toplevel(window->shell_surface);
719 switch (window->buffer_type) {
720 #ifdef HAVE_CAIRO_EGL
721 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
722 data = cairo_surface_get_user_data(window->cairo_surface,
725 cairo_gl_surface_swapbuffers(window->cairo_surface);
726 wl_egl_window_get_attached_size(data->window,
727 &window->server_allocation.width,
728 &window->server_allocation.height);
731 case WINDOW_BUFFER_TYPE_SHM:
733 display_get_buffer_for_surface(display,
734 window->cairo_surface);
736 window_get_resize_dx_dy(window, &x, &y);
737 wl_surface_attach(window->surface, buffer, x, y);
738 wl_surface_damage(window->surface, 0, 0,
739 window->allocation.width,
740 window->allocation.height);
741 window->server_allocation = window->allocation;
742 cairo_surface_destroy(window->cairo_surface);
743 window->cairo_surface = NULL;
749 if (window->input_region) {
750 wl_surface_set_input_region(window->surface,
751 window->input_region);
752 wl_region_destroy(window->input_region);
753 window->input_region = NULL;
756 if (window->opaque_region) {
757 wl_surface_set_opaque_region(window->surface,
758 window->opaque_region);
759 wl_region_destroy(window->opaque_region);
760 window->opaque_region = NULL;
765 window_flush(struct window *window)
767 if (window->cairo_surface)
768 window_attach_surface(window);
772 window_set_surface(struct window *window, cairo_surface_t *surface)
774 cairo_surface_reference(surface);
776 if (window->cairo_surface != NULL)
777 cairo_surface_destroy(window->cairo_surface);
779 window->cairo_surface = surface;
782 #ifdef HAVE_CAIRO_EGL
784 window_resize_cairo_window_surface(struct window *window)
786 struct egl_window_surface_data *data;
789 data = cairo_surface_get_user_data(window->cairo_surface,
792 window_get_resize_dx_dy(window, &x, &y),
793 wl_egl_window_resize(data->window,
794 window->allocation.width,
795 window->allocation.height,
798 cairo_gl_surface_set_size(window->cairo_surface,
799 window->allocation.width,
800 window->allocation.height);
805 window_get_display(struct window *window)
807 return window->display;
811 window_create_surface(struct window *window)
813 cairo_surface_t *surface;
816 if (!window->transparent)
817 flags = SURFACE_OPAQUE;
819 switch (window->buffer_type) {
820 #ifdef HAVE_CAIRO_EGL
821 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
822 if (window->cairo_surface) {
823 window_resize_cairo_window_surface(window);
826 surface = display_create_surface(window->display,
828 &window->allocation, flags);
831 case WINDOW_BUFFER_TYPE_SHM:
832 surface = display_create_shm_surface(window->display,
841 window_set_surface(window, surface);
842 cairo_surface_destroy(surface);
845 static void frame_destroy(struct frame *frame);
848 window_destroy(struct window *window)
850 struct display *display = window->display;
852 struct window_output *window_output;
853 struct window_output *window_output_tmp;
855 if (window->redraw_scheduled)
856 wl_list_remove(&window->redraw_task.link);
858 wl_list_for_each(input, &display->input_list, link) {
859 if (input->pointer_focus == window)
860 input->pointer_focus = NULL;
861 if (input->keyboard_focus == window)
862 input->keyboard_focus = NULL;
863 if (input->focus_widget &&
864 input->focus_widget->window == window)
865 input->focus_widget = NULL;
868 wl_list_for_each_safe(window_output, window_output_tmp,
869 &window->window_output_list, link) {
870 free (window_output);
873 if (window->input_region)
874 wl_region_destroy(window->input_region);
875 if (window->opaque_region)
876 wl_region_destroy(window->opaque_region);
879 frame_destroy(window->frame);
881 if (window->shell_surface)
882 wl_shell_surface_destroy(window->shell_surface);
883 wl_surface_destroy(window->surface);
884 wl_list_remove(&window->link);
886 if (window->cairo_surface != NULL)
887 cairo_surface_destroy(window->cairo_surface);
893 static struct widget *
894 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
896 struct widget *child, *target;
898 wl_list_for_each(child, &widget->child_list, link) {
899 target = widget_find_widget(child, x, y);
904 if (widget->allocation.x <= x &&
905 x < widget->allocation.x + widget->allocation.width &&
906 widget->allocation.y <= y &&
907 y < widget->allocation.y + widget->allocation.height) {
914 static struct widget *
915 widget_create(struct window *window, void *data)
917 struct widget *widget;
919 widget = malloc(sizeof *widget);
920 memset(widget, 0, sizeof *widget);
921 widget->window = window;
922 widget->user_data = data;
923 widget->allocation = window->allocation;
924 wl_list_init(&widget->child_list);
926 widget->tooltip = NULL;
927 widget->tooltip_count = 0;
933 window_add_widget(struct window *window, void *data)
935 window->widget = widget_create(window, data);
936 wl_list_init(&window->widget->link);
938 return window->widget;
942 widget_add_widget(struct widget *parent, void *data)
944 struct widget *widget;
946 widget = widget_create(parent->window, data);
947 wl_list_insert(parent->child_list.prev, &widget->link);
953 widget_destroy(struct widget *widget)
955 struct display *display = widget->window->display;
958 if (widget->tooltip) {
959 free(widget->tooltip);
960 widget->tooltip = NULL;
963 wl_list_for_each(input, &display->input_list, link) {
964 if (input->focus_widget == widget)
965 input->focus_widget = NULL;
968 wl_list_remove(&widget->link);
973 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
975 *allocation = widget->allocation;
979 widget_set_size(struct widget *widget, int32_t width, int32_t height)
981 widget->allocation.width = width;
982 widget->allocation.height = height;
986 widget_set_allocation(struct widget *widget,
987 int32_t x, int32_t y, int32_t width, int32_t height)
989 widget->allocation.x = x;
990 widget->allocation.y = y;
991 widget_set_size(widget, width, height);
995 widget_set_transparent(struct widget *widget, int transparent)
997 widget->opaque = !transparent;
1001 widget_get_user_data(struct widget *widget)
1003 return widget->user_data;
1007 widget_set_resize_handler(struct widget *widget,
1008 widget_resize_handler_t handler)
1010 widget->resize_handler = handler;
1014 widget_set_redraw_handler(struct widget *widget,
1015 widget_redraw_handler_t handler)
1017 widget->redraw_handler = handler;
1021 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1023 widget->enter_handler = handler;
1027 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1029 widget->leave_handler = handler;
1033 widget_set_motion_handler(struct widget *widget,
1034 widget_motion_handler_t handler)
1036 widget->motion_handler = handler;
1040 widget_set_button_handler(struct widget *widget,
1041 widget_button_handler_t handler)
1043 widget->button_handler = handler;
1047 widget_schedule_redraw(struct widget *widget)
1049 window_schedule_redraw(widget->window);
1053 window_get_surface(struct window *window)
1055 return cairo_surface_reference(window->cairo_surface);
1059 window_get_wl_surface(struct window *window)
1061 return window->surface;
1064 struct wl_shell_surface *
1065 window_get_wl_shell_surface(struct window *window)
1067 return window->shell_surface;
1071 tooltip_redraw_handler(struct widget *widget, void *data)
1074 const int32_t r = 3;
1075 struct tooltip *tooltip = data;
1076 int32_t width, height;
1077 struct window *window = widget->window;
1079 cr = cairo_create(window->cairo_surface);
1080 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1081 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1084 width = window->allocation.width;
1085 height = window->allocation.height;
1086 rounded_rect(cr, 0, 0, width, height, r);
1088 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1089 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1092 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1093 cairo_move_to(cr, 10, 16);
1094 cairo_show_text(cr, tooltip->entry);
1098 static cairo_text_extents_t
1099 get_text_extents(struct tooltip *tooltip)
1101 struct window *window;
1103 cairo_text_extents_t extents;
1105 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1107 window = tooltip->widget->window->parent;
1108 cr = cairo_create(window->cairo_surface);
1109 cairo_text_extents(cr, tooltip->entry, &extents);
1116 window_create_tooltip(struct tooltip *tooltip)
1118 struct widget *parent = tooltip->parent;
1119 struct display *display = parent->window->display;
1120 struct window *window;
1121 const int offset_y = 27;
1122 const int margin = 3;
1123 cairo_text_extents_t extents;
1125 if (tooltip->widget)
1128 window = window_create_transient(display, parent->window, tooltip->x,
1129 tooltip->y + offset_y,
1130 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1134 tooltip->window = window;
1135 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1137 extents = get_text_extents(tooltip);
1138 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1139 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1145 widget_destroy_tooltip(struct widget *parent)
1147 struct tooltip *tooltip = parent->tooltip;
1149 parent->tooltip_count = 0;
1153 if (tooltip->widget) {
1154 widget_destroy(tooltip->widget);
1155 window_destroy(tooltip->window);
1156 tooltip->widget = NULL;
1157 tooltip->window = NULL;
1160 close(tooltip->tooltip_fd);
1161 free(tooltip->entry);
1163 parent->tooltip = NULL;
1167 tooltip_func(struct task *task, uint32_t events)
1169 struct tooltip *tooltip =
1170 container_of(task, struct tooltip, tooltip_task);
1173 if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1175 window_create_tooltip(tooltip);
1178 #define TOOLTIP_TIMEOUT 500
1180 tooltip_timer_reset(struct tooltip *tooltip)
1182 struct itimerspec its;
1184 its.it_interval.tv_sec = 0;
1185 its.it_interval.tv_nsec = 0;
1186 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1187 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1188 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1189 fprintf(stderr, "could not set timerfd\n: %m");
1197 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1199 struct tooltip *tooltip = parent->tooltip;
1201 parent->tooltip_count++;
1205 tooltip_timer_reset(tooltip);
1209 /* the handler might be triggered too fast via input device motion, so
1210 * we need this check here to make sure tooltip is fully initialized */
1211 if (parent->tooltip_count > 1)
1214 tooltip = malloc(sizeof *tooltip);
1218 parent->tooltip = tooltip;
1219 tooltip->parent = parent;
1220 tooltip->widget = NULL;
1221 tooltip->window = NULL;
1224 tooltip->entry = strdup(entry);
1225 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1226 if (tooltip->tooltip_fd < 0) {
1227 fprintf(stderr, "could not create timerfd\n: %m");
1231 tooltip->tooltip_task.run = tooltip_func;
1232 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1233 EPOLLIN, &tooltip->tooltip_task);
1234 tooltip_timer_reset(tooltip);
1240 frame_resize_handler(struct widget *widget,
1241 int32_t width, int32_t height, void *data)
1243 struct frame *frame = data;
1244 struct widget *child = frame->child;
1245 struct rectangle allocation;
1246 struct display *display = widget->window->display;
1247 struct frame_button * button;
1248 struct theme *t = display->theme;
1249 int x_l, x_r, y, w, h;
1250 int decoration_width, decoration_height;
1253 if (widget->window->type != TYPE_FULLSCREEN) {
1254 decoration_width = (t->width + t->margin) * 2;
1255 decoration_height = t->width +
1256 t->titlebar_height + t->margin * 2;
1258 allocation.x = t->width + t->margin;
1259 allocation.y = t->titlebar_height + t->margin;
1260 allocation.width = width - decoration_width;
1261 allocation.height = height - decoration_height;
1263 opaque_margin = t->margin + t->frame_radius;
1265 wl_list_for_each(button, &frame->buttons_list, link)
1266 button->widget->opaque = 0;
1268 decoration_width = 0;
1269 decoration_height = 0;
1273 allocation.width = width;
1274 allocation.height = height;
1277 wl_list_for_each(button, &frame->buttons_list, link)
1278 button->widget->opaque = 1;
1281 widget_set_allocation(child, allocation.x, allocation.y,
1282 allocation.width, allocation.height);
1284 if (child->resize_handler)
1285 child->resize_handler(child,
1290 width = child->allocation.width + decoration_width;
1291 height = child->allocation.height + decoration_height;
1293 widget->window->input_region =
1294 wl_compositor_create_region(display->compositor);
1295 wl_region_add(widget->window->input_region,
1296 t->margin, t->margin,
1297 width - 2 * t->margin,
1298 height - 2 * t->margin);
1300 widget_set_allocation(widget, 0, 0, width, height);
1302 if (child->opaque) {
1303 widget->window->opaque_region =
1304 wl_compositor_create_region(display->compositor);
1305 wl_region_add(widget->window->opaque_region,
1306 opaque_margin, opaque_margin,
1307 widget->allocation.width - 2 * opaque_margin,
1308 widget->allocation.height - 2 * opaque_margin);
1311 /* frame internal buttons */
1312 x_r = frame->widget->allocation.width - t->width - t->margin;
1313 x_l = t->width + t->margin;
1314 y = t->width + t->margin;
1315 wl_list_for_each(button, &frame->buttons_list, link) {
1316 const int button_padding = 4;
1317 w = cairo_image_surface_get_width(button->icon);
1318 h = cairo_image_surface_get_height(button->icon);
1320 if (button->decoration == FRAME_BUTTON_FANCY)
1323 if (button->align == FRAME_BUTTON_LEFT) {
1324 widget_set_allocation(button->widget,
1325 x_l, y , w + 1, h + 1);
1327 x_l += button_padding;
1330 widget_set_allocation(button->widget,
1331 x_r, y , w + 1, h + 1);
1332 x_r -= button_padding;
1338 frame_button_enter_handler(struct widget *widget,
1339 struct input *input, float x, float y, void *data)
1341 struct frame_button *frame_button = data;
1343 widget_schedule_redraw(frame_button->widget);
1344 frame_button->state = FRAME_BUTTON_OVER;
1346 return CURSOR_LEFT_PTR;
1350 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1352 struct frame_button *frame_button = data;
1354 widget_schedule_redraw(frame_button->widget);
1355 frame_button->state = FRAME_BUTTON_DEFAULT;
1359 frame_button_button_handler(struct widget *widget,
1360 struct input *input, uint32_t time,
1362 enum wl_pointer_button_state state, void *data)
1364 struct frame_button *frame_button = data;
1365 struct window *window = widget->window;
1367 if (button != BTN_LEFT)
1371 case WL_POINTER_BUTTON_STATE_PRESSED:
1372 frame_button->state = FRAME_BUTTON_ACTIVE;
1373 widget_schedule_redraw(frame_button->widget);
1375 if (frame_button->type == FRAME_BUTTON_ICON)
1376 window_show_frame_menu(window, input, time);
1378 case WL_POINTER_BUTTON_STATE_RELEASED:
1379 frame_button->state = FRAME_BUTTON_DEFAULT;
1380 widget_schedule_redraw(frame_button->widget);
1384 switch (frame_button->type) {
1385 case FRAME_BUTTON_CLOSE:
1386 if (window->close_handler)
1387 window->close_handler(window->parent,
1390 display_exit(window->display);
1392 case FRAME_BUTTON_MINIMIZE:
1393 fprintf(stderr,"Minimize stub\n");
1395 case FRAME_BUTTON_MAXIMIZE:
1396 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1399 /* Unknown operation */
1405 frame_button_redraw_handler(struct widget *widget, void *data)
1407 struct frame_button *frame_button = data;
1409 int width, height, x, y;
1410 struct window *window = widget->window;
1412 x = widget->allocation.x;
1413 y = widget->allocation.y;
1414 width = widget->allocation.width;
1415 height = widget->allocation.height;
1424 cr = cairo_create(window->cairo_surface);
1426 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1427 cairo_set_line_width(cr, 1);
1429 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1430 cairo_rectangle (cr, x, y, 25, 16);
1432 cairo_stroke_preserve(cr);
1434 switch (frame_button->state) {
1435 case FRAME_BUTTON_DEFAULT:
1436 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1438 case FRAME_BUTTON_OVER:
1439 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1441 case FRAME_BUTTON_ACTIVE:
1442 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1451 cairo_set_source_surface(cr, frame_button->icon, x, y);
1457 static struct widget *
1458 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1459 enum frame_button_align align, enum frame_button_decoration style)
1461 struct frame_button *frame_button;
1462 const char *icon = data;
1464 frame_button = malloc (sizeof *frame_button);
1465 memset(frame_button, 0, sizeof *frame_button);
1467 frame_button->icon = cairo_image_surface_create_from_png(icon);
1468 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1469 frame_button->frame = frame;
1470 frame_button->type = type;
1471 frame_button->align = align;
1472 frame_button->decoration = style;
1474 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1476 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1477 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1478 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1479 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1480 return frame_button->widget;
1484 frame_button_destroy(struct frame_button *frame_button)
1486 widget_destroy(frame_button->widget);
1487 wl_list_remove(&frame_button->link);
1488 cairo_surface_destroy(frame_button->icon);
1495 frame_redraw_handler(struct widget *widget, void *data)
1498 struct window *window = widget->window;
1499 struct theme *t = window->display->theme;
1502 if (window->type == TYPE_FULLSCREEN)
1505 cr = cairo_create(window->cairo_surface);
1507 if (window->keyboard_device)
1508 flags |= THEME_FRAME_ACTIVE;
1509 theme_render_frame(t, cr, widget->allocation.width,
1510 widget->allocation.height, window->title, flags);
1516 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1518 struct theme *t = frame->widget->window->display->theme;
1521 location = theme_get_location(t, input->sx, input->sy,
1522 frame->widget->allocation.width,
1523 frame->widget->allocation.height);
1526 case THEME_LOCATION_RESIZING_TOP:
1528 case THEME_LOCATION_RESIZING_BOTTOM:
1529 return CURSOR_BOTTOM;
1530 case THEME_LOCATION_RESIZING_LEFT:
1532 case THEME_LOCATION_RESIZING_RIGHT:
1533 return CURSOR_RIGHT;
1534 case THEME_LOCATION_RESIZING_TOP_LEFT:
1535 return CURSOR_TOP_LEFT;
1536 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1537 return CURSOR_TOP_RIGHT;
1538 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1539 return CURSOR_BOTTOM_LEFT;
1540 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1541 return CURSOR_BOTTOM_RIGHT;
1542 case THEME_LOCATION_EXTERIOR:
1543 case THEME_LOCATION_TITLEBAR:
1545 return CURSOR_LEFT_PTR;
1550 frame_menu_func(struct window *window, int index, void *data)
1554 if (window->close_handler)
1555 window->close_handler(window->parent,
1558 display_exit(window->display);
1560 case 1: /* fullscreen */
1561 /* we don't have a way to get out of fullscreen for now */
1562 window_set_fullscreen(window, 1);
1564 case 2: /* rotate */
1571 window_show_frame_menu(struct window *window,
1572 struct input *input, uint32_t time)
1576 static const char *entries[] = {
1577 "Close", "Fullscreen", "Rotate", "Scale"
1580 input_get_position(input, &x, &y);
1581 window_show_menu(window->display, input, time, window,
1582 x - 10, y - 10, frame_menu_func, entries, 4);
1586 frame_enter_handler(struct widget *widget,
1587 struct input *input, float x, float y, void *data)
1589 return frame_get_pointer_image_for_location(data, input);
1593 frame_motion_handler(struct widget *widget,
1594 struct input *input, uint32_t time,
1595 float x, float y, void *data)
1597 return frame_get_pointer_image_for_location(data, input);
1601 frame_button_handler(struct widget *widget,
1602 struct input *input, uint32_t time,
1603 uint32_t button, enum wl_pointer_button_state state,
1607 struct frame *frame = data;
1608 struct window *window = widget->window;
1609 struct display *display = window->display;
1612 location = theme_get_location(display->theme, input->sx, input->sy,
1613 frame->widget->allocation.width,
1614 frame->widget->allocation.height);
1616 if (window->display->shell && button == BTN_LEFT &&
1617 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1619 case THEME_LOCATION_TITLEBAR:
1620 if (!window->shell_surface)
1622 input_set_pointer_image(input, CURSOR_DRAGGING);
1623 input_ungrab(input);
1624 wl_shell_surface_move(window->shell_surface,
1625 input_get_seat(input),
1628 case THEME_LOCATION_RESIZING_TOP:
1629 case THEME_LOCATION_RESIZING_BOTTOM:
1630 case THEME_LOCATION_RESIZING_LEFT:
1631 case THEME_LOCATION_RESIZING_RIGHT:
1632 case THEME_LOCATION_RESIZING_TOP_LEFT:
1633 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1634 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1635 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1636 if (!window->shell_surface)
1638 input_ungrab(input);
1640 if (!display->dpy) {
1641 /* If we're using shm, allocate a big
1642 pool to create buffers out of while
1643 we resize. We should probably base
1644 this number on the size of the output. */
1646 shm_pool_create(display, 6 * 1024 * 1024);
1649 wl_shell_surface_resize(window->shell_surface,
1650 input_get_seat(input),
1651 display->serial, location);
1654 } else if (button == BTN_RIGHT &&
1655 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1656 window_show_frame_menu(window, input, time);
1661 frame_create(struct window *window, void *data)
1663 struct frame *frame;
1665 frame = malloc(sizeof *frame);
1666 memset(frame, 0, sizeof *frame);
1668 frame->widget = window_add_widget(window, frame);
1669 frame->child = widget_add_widget(frame->widget, data);
1671 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1672 widget_set_resize_handler(frame->widget, frame_resize_handler);
1673 widget_set_enter_handler(frame->widget, frame_enter_handler);
1674 widget_set_motion_handler(frame->widget, frame_motion_handler);
1675 widget_set_button_handler(frame->widget, frame_button_handler);
1677 /* Create empty list for frame buttons */
1678 wl_list_init(&frame->buttons_list);
1680 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1681 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1683 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1684 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1687 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1689 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1690 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1692 window->frame = frame;
1694 return frame->child;
1698 frame_set_child_size(struct widget *widget, int child_width, int child_height)
1700 struct display *display = widget->window->display;
1701 struct theme *t = display->theme;
1702 int decoration_width, decoration_height;
1705 if (widget->window->type != TYPE_FULLSCREEN) {
1706 decoration_width = (t->width + t->margin) * 2;
1707 decoration_height = t->width +
1708 t->titlebar_height + t->margin * 2;
1710 width = child_width + decoration_width;
1711 height = child_height + decoration_height;
1713 width = child_width;
1714 height = child_height;
1717 window_schedule_resize(widget->window, width, height);
1721 frame_destroy(struct frame *frame)
1723 struct frame_button *button, *tmp;
1725 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1726 frame_button_destroy(button);
1728 /* frame->child must be destroyed by the application */
1729 widget_destroy(frame->widget);
1734 input_set_focus_widget(struct input *input, struct widget *focus,
1737 struct widget *old, *widget;
1738 int pointer = CURSOR_LEFT_PTR;
1740 if (focus == input->focus_widget)
1743 old = input->focus_widget;
1747 widget = input->grab;
1748 if (widget->leave_handler)
1749 widget->leave_handler(old, input, widget->user_data);
1750 input->focus_widget = NULL;
1756 widget = input->grab;
1757 input->focus_widget = focus;
1758 if (widget->enter_handler)
1759 pointer = widget->enter_handler(focus, input, x, y,
1762 input_set_pointer_image(input, pointer);
1767 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1768 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1770 struct input *input = data;
1771 struct window *window = input->pointer_focus;
1772 struct widget *widget;
1773 int cursor = CURSOR_LEFT_PTR;
1774 float sx = wl_fixed_to_double(sx_w);
1775 float sy = wl_fixed_to_double(sy_w);
1780 if (!(input->grab && input->grab_button)) {
1781 widget = widget_find_widget(window->widget, sx, sy);
1782 input_set_focus_widget(input, widget, sx, sy);
1786 widget = input->grab;
1788 widget = input->focus_widget;
1789 if (widget && widget->motion_handler)
1790 cursor = widget->motion_handler(input->focus_widget,
1791 input, time, sx, sy,
1794 input_set_pointer_image(input, cursor);
1798 input_grab(struct input *input, struct widget *widget, uint32_t button)
1800 input->grab = widget;
1801 input->grab_button = button;
1805 input_ungrab(struct input *input)
1807 struct widget *widget;
1810 if (input->pointer_focus) {
1811 widget = widget_find_widget(input->pointer_focus->widget,
1812 input->sx, input->sy);
1813 input_set_focus_widget(input, widget, input->sx, input->sy);
1818 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1819 uint32_t time, uint32_t button, uint32_t state_w)
1821 struct input *input = data;
1822 struct widget *widget;
1823 enum wl_pointer_button_state state = state_w;
1825 input->display->serial = serial;
1826 if (input->focus_widget && input->grab == NULL &&
1827 state == WL_POINTER_BUTTON_STATE_PRESSED)
1828 input_grab(input, input->focus_widget, button);
1830 widget = input->grab;
1831 if (widget && widget->button_handler)
1832 (*widget->button_handler)(widget,
1835 input->grab->user_data);
1837 if (input->grab && input->grab_button == button &&
1838 state == WL_POINTER_BUTTON_STATE_RELEASED)
1839 input_ungrab(input);
1843 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1844 uint32_t time, uint32_t axis, wl_fixed_t value)
1849 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1850 uint32_t serial, uint32_t time, uint32_t key,
1853 struct input *input = data;
1854 struct window *window = input->keyboard_focus;
1855 uint32_t code, num_syms;
1856 enum wl_keyboard_key_state state = state_w;
1857 const xkb_keysym_t *syms;
1859 xkb_mod_mask_t mask;
1860 struct itimerspec its;
1862 input->display->serial = serial;
1864 if (!window || window->keyboard_device != input || !input->xkb.state)
1867 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
1869 mask = xkb_state_serialize_mods(input->xkb.state,
1870 XKB_STATE_DEPRESSED |
1872 input->modifiers = 0;
1873 if (mask & input->xkb.control_mask)
1874 input->modifiers |= MOD_CONTROL_MASK;
1875 if (mask & input->xkb.alt_mask)
1876 input->modifiers |= MOD_ALT_MASK;
1877 if (mask & input->xkb.shift_mask)
1878 input->modifiers |= MOD_SHIFT_MASK;
1880 sym = XKB_KEY_NoSymbol;
1884 if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
1885 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1886 window_set_maximized(window,
1887 window->type != TYPE_MAXIMIZED);
1888 } else if (window->key_handler) {
1889 (*window->key_handler)(window, input, time, key,
1890 sym, state, window->user_data);
1893 if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
1894 key == input->repeat_key) {
1895 its.it_interval.tv_sec = 0;
1896 its.it_interval.tv_nsec = 0;
1897 its.it_value.tv_sec = 0;
1898 its.it_value.tv_nsec = 0;
1899 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1900 } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
1901 input->repeat_sym = sym;
1902 input->repeat_key = key;
1903 input->repeat_time = time;
1904 its.it_interval.tv_sec = 0;
1905 its.it_interval.tv_nsec = 25 * 1000 * 1000;
1906 its.it_value.tv_sec = 0;
1907 its.it_value.tv_nsec = 400 * 1000 * 1000;
1908 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
1913 keyboard_repeat_func(struct task *task, uint32_t events)
1915 struct input *input =
1916 container_of(task, struct input, repeat_task);
1917 struct window *window = input->keyboard_focus;
1920 if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
1921 /* If we change the timer between the fd becoming
1922 * readable and getting here, there'll be nothing to
1923 * read and we get EAGAIN. */
1926 if (window && window->key_handler) {
1927 (*window->key_handler)(window, input, input->repeat_time,
1928 input->repeat_key, input->repeat_sym,
1929 WL_KEYBOARD_KEY_STATE_PRESSED,
1935 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1936 uint32_t serial, uint32_t mods_depressed,
1937 uint32_t mods_latched, uint32_t mods_locked,
1940 struct input *input = data;
1942 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1943 mods_locked, 0, 0, group);
1947 input_remove_pointer_focus(struct input *input)
1949 struct window *window = input->pointer_focus;
1954 input_set_focus_widget(input, NULL, 0, 0);
1956 input->pointer_focus = NULL;
1957 input->current_cursor = CURSOR_UNSET;
1961 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1962 uint32_t serial, struct wl_surface *surface,
1963 wl_fixed_t sx_w, wl_fixed_t sy_w)
1965 struct input *input = data;
1966 struct window *window;
1967 struct widget *widget;
1968 float sx = wl_fixed_to_double(sx_w);
1969 float sy = wl_fixed_to_double(sy_w);
1972 /* enter event for a window we've just destroyed */
1976 input->display->serial = serial;
1977 input->pointer_enter_serial = serial;
1978 input->pointer_focus = wl_surface_get_user_data(surface);
1979 window = input->pointer_focus;
1982 shm_pool_destroy(window->pool);
1983 window->pool = NULL;
1984 /* Schedule a redraw to free the pool */
1985 window_schedule_redraw(window);
1991 widget = widget_find_widget(window->widget, sx, sy);
1992 input_set_focus_widget(input, widget, sx, sy);
1996 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1997 uint32_t serial, struct wl_surface *surface)
1999 struct input *input = data;
2001 input->display->serial = serial;
2002 input_remove_pointer_focus(input);
2006 input_remove_keyboard_focus(struct input *input)
2008 struct window *window = input->keyboard_focus;
2009 struct itimerspec its;
2011 its.it_interval.tv_sec = 0;
2012 its.it_interval.tv_nsec = 0;
2013 its.it_value.tv_sec = 0;
2014 its.it_value.tv_nsec = 0;
2015 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2020 window->keyboard_device = NULL;
2021 if (window->keyboard_focus_handler)
2022 (*window->keyboard_focus_handler)(window, NULL,
2025 input->keyboard_focus = NULL;
2029 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2030 uint32_t serial, struct wl_surface *surface,
2031 struct wl_array *keys)
2033 struct input *input = data;
2034 struct window *window;
2036 input->display->serial = serial;
2037 input->keyboard_focus = wl_surface_get_user_data(surface);
2039 window = input->keyboard_focus;
2040 window->keyboard_device = input;
2041 if (window->keyboard_focus_handler)
2042 (*window->keyboard_focus_handler)(window,
2043 window->keyboard_device,
2048 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2049 uint32_t serial, struct wl_surface *surface)
2051 struct input *input = data;
2053 input->display->serial = serial;
2054 input_remove_keyboard_focus(input);
2058 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2059 uint32_t format, int fd, uint32_t size)
2061 struct input *input = data;
2069 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2074 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2075 if (map_str == MAP_FAILED) {
2080 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2082 XKB_KEYMAP_FORMAT_TEXT_V1,
2084 munmap(map_str, size);
2087 if (!input->xkb.keymap) {
2088 fprintf(stderr, "failed to compile keymap\n");
2092 input->xkb.state = xkb_state_new(input->xkb.keymap);
2093 if (!input->xkb.state) {
2094 fprintf(stderr, "failed to create XKB state\n");
2095 xkb_map_unref(input->xkb.keymap);
2096 input->xkb.keymap = NULL;
2100 input->xkb.control_mask =
2101 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2102 input->xkb.alt_mask =
2103 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2104 input->xkb.shift_mask =
2105 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2108 static const struct wl_pointer_listener pointer_listener = {
2109 pointer_handle_enter,
2110 pointer_handle_leave,
2111 pointer_handle_motion,
2112 pointer_handle_button,
2113 pointer_handle_axis,
2116 static const struct wl_keyboard_listener keyboard_listener = {
2117 keyboard_handle_keymap,
2118 keyboard_handle_enter,
2119 keyboard_handle_leave,
2120 keyboard_handle_key,
2121 keyboard_handle_modifiers,
2125 seat_handle_capabilities(void *data, struct wl_seat *seat,
2126 enum wl_seat_capability caps)
2128 struct input *input = data;
2130 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2131 input->pointer = wl_seat_get_pointer(seat);
2132 wl_pointer_set_user_data(input->pointer, input);
2133 wl_pointer_add_listener(input->pointer, &pointer_listener,
2135 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2136 wl_pointer_destroy(input->pointer);
2137 input->pointer = NULL;
2140 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2141 input->keyboard = wl_seat_get_keyboard(seat);
2142 wl_keyboard_set_user_data(input->keyboard, input);
2143 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2145 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2146 wl_keyboard_destroy(input->keyboard);
2147 input->keyboard = NULL;
2151 static const struct wl_seat_listener seat_listener = {
2152 seat_handle_capabilities,
2156 input_get_position(struct input *input, int32_t *x, int32_t *y)
2163 input_get_display(struct input *input)
2165 return input->display;
2169 input_get_seat(struct input *input)
2175 input_get_modifiers(struct input *input)
2177 return input->modifiers;
2181 input_get_focus_widget(struct input *input)
2183 return input->focus_widget;
2187 struct wl_data_offer *offer;
2188 struct input *input;
2189 struct wl_array types;
2192 struct task io_task;
2200 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2202 struct data_offer *offer = data;
2205 p = wl_array_add(&offer->types, sizeof *p);
2209 static const struct wl_data_offer_listener data_offer_listener = {
2214 data_offer_destroy(struct data_offer *offer)
2219 if (offer->refcount == 0) {
2220 wl_data_offer_destroy(offer->offer);
2221 for (p = offer->types.data; *p; p++)
2223 wl_array_release(&offer->types);
2229 data_device_data_offer(void *data,
2230 struct wl_data_device *data_device,
2231 struct wl_data_offer *_offer)
2233 struct data_offer *offer;
2235 offer = malloc(sizeof *offer);
2237 wl_array_init(&offer->types);
2238 offer->refcount = 1;
2239 offer->input = data;
2240 offer->offer = _offer;
2241 wl_data_offer_add_listener(offer->offer,
2242 &data_offer_listener, offer);
2246 data_device_enter(void *data, struct wl_data_device *data_device,
2247 uint32_t serial, struct wl_surface *surface,
2248 wl_fixed_t x_w, wl_fixed_t y_w,
2249 struct wl_data_offer *offer)
2251 struct input *input = data;
2252 struct window *window;
2254 float x = wl_fixed_to_double(x_w);
2255 float y = wl_fixed_to_double(y_w);
2258 input->pointer_enter_serial = serial;
2259 window = wl_surface_get_user_data(surface);
2260 input->pointer_focus = window;
2263 input->drag_offer = wl_data_offer_get_user_data(offer);
2265 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2268 types_data = input->drag_offer->types.data;
2270 input->drag_offer = NULL;
2274 window = input->pointer_focus;
2275 if (window->data_handler)
2276 window->data_handler(window, input, x, y, types_data,
2281 data_device_leave(void *data, struct wl_data_device *data_device)
2283 struct input *input = data;
2285 if (input->drag_offer) {
2286 data_offer_destroy(input->drag_offer);
2287 input->drag_offer = NULL;
2292 data_device_motion(void *data, struct wl_data_device *data_device,
2293 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2295 struct input *input = data;
2296 struct window *window = input->pointer_focus;
2297 float x = wl_fixed_to_double(x_w);
2298 float y = wl_fixed_to_double(y_w);
2304 if (input->drag_offer)
2305 types_data = input->drag_offer->types.data;
2309 if (window->data_handler)
2310 window->data_handler(window, input, x, y, types_data,
2315 data_device_drop(void *data, struct wl_data_device *data_device)
2317 struct input *input = data;
2318 struct window *window = input->pointer_focus;
2320 if (window->drop_handler)
2321 window->drop_handler(window, input,
2322 input->sx, input->sy, window->user_data);
2326 data_device_selection(void *data,
2327 struct wl_data_device *wl_data_device,
2328 struct wl_data_offer *offer)
2330 struct input *input = data;
2333 if (input->selection_offer)
2334 data_offer_destroy(input->selection_offer);
2337 input->selection_offer = wl_data_offer_get_user_data(offer);
2338 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2341 input->selection_offer = NULL;
2345 static const struct wl_data_device_listener data_device_listener = {
2346 data_device_data_offer,
2351 data_device_selection
2355 input_set_pointer_image_index(struct input *input, int index)
2357 struct wl_buffer *buffer;
2358 struct wl_cursor *cursor;
2359 struct wl_cursor_image *image;
2361 cursor = input->display->cursors[input->current_cursor];
2365 if (index >= (int) cursor->image_count) {
2366 fprintf(stderr, "cursor index out of range\n");
2370 image = cursor->images[index];
2371 buffer = wl_cursor_image_get_buffer(image);
2375 wl_pointer_set_cursor(input->pointer, input->display->serial,
2376 input->pointer_surface,
2377 image->hotspot_x, image->hotspot_y);
2378 wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2379 wl_surface_damage(input->pointer_surface, 0, 0,
2380 image->width, image->height);
2383 static const struct wl_callback_listener pointer_surface_listener;
2386 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2389 struct input *input = data;
2390 struct wl_cursor *cursor;
2394 assert(callback == input->cursor_frame_cb);
2395 wl_callback_destroy(callback);
2396 input->cursor_frame_cb = NULL;
2399 if (input->current_cursor == CURSOR_BLANK) {
2400 wl_pointer_set_cursor(input->pointer, input->display->serial,
2405 if (input->current_cursor == CURSOR_UNSET)
2407 cursor = input->display->cursors[input->current_cursor];
2411 /* FIXME We don't have the current time on the first call so we set
2412 * the animation start to the time of the first frame callback. */
2414 input->cursor_anim_start = 0;
2415 else if (input->cursor_anim_start == 0)
2416 input->cursor_anim_start = time;
2418 if (time == 0 || input->cursor_anim_start == 0)
2421 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2423 input_set_pointer_image_index(input, i);
2425 if (cursor->image_count == 1)
2428 input->cursor_frame_cb = wl_surface_frame(input->pointer_surface);
2429 wl_callback_add_listener(input->cursor_frame_cb,
2430 &pointer_surface_listener, input);
2433 static const struct wl_callback_listener pointer_surface_listener = {
2434 pointer_surface_frame_callback
2438 input_set_pointer_image(struct input *input, int pointer)
2440 if (pointer == input->current_cursor &&
2441 input->pointer_enter_serial == input->cursor_serial)
2444 input->current_cursor = pointer;
2445 input->cursor_serial = input->pointer_enter_serial;
2446 if (!input->cursor_frame_cb)
2447 pointer_surface_frame_callback(input, NULL, 0);
2450 struct wl_data_device *
2451 input_get_data_device(struct input *input)
2453 return input->data_device;
2457 input_set_selection(struct input *input,
2458 struct wl_data_source *source, uint32_t time)
2460 wl_data_device_set_selection(input->data_device, source, time);
2464 input_accept(struct input *input, const char *type)
2466 wl_data_offer_accept(input->drag_offer->offer,
2467 input->pointer_enter_serial, type);
2471 offer_io_func(struct task *task, uint32_t events)
2473 struct data_offer *offer =
2474 container_of(task, struct data_offer, io_task);
2478 len = read(offer->fd, buffer, sizeof buffer);
2479 offer->func(buffer, len,
2480 offer->x, offer->y, offer->user_data);
2484 data_offer_destroy(offer);
2489 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2490 data_func_t func, void *user_data)
2494 if (pipe2(p, O_CLOEXEC) == -1)
2497 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2500 offer->io_task.run = offer_io_func;
2504 offer->user_data = user_data;
2506 display_watch_fd(offer->input->display,
2507 offer->fd, EPOLLIN, &offer->io_task);
2511 input_receive_drag_data(struct input *input, const char *mime_type,
2512 data_func_t func, void *data)
2514 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2515 input->drag_offer->x = input->sx;
2516 input->drag_offer->y = input->sy;
2520 input_receive_selection_data(struct input *input, const char *mime_type,
2521 data_func_t func, void *data)
2525 if (input->selection_offer == NULL)
2528 for (p = input->selection_offer->types.data; *p; p++)
2529 if (strcmp(mime_type, *p) == 0)
2535 data_offer_receive_data(input->selection_offer,
2536 mime_type, func, data);
2541 input_receive_selection_data_to_fd(struct input *input,
2542 const char *mime_type, int fd)
2544 if (input->selection_offer)
2545 wl_data_offer_receive(input->selection_offer->offer,
2552 window_move(struct window *window, struct input *input, uint32_t serial)
2554 if (!window->shell_surface)
2557 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2561 idle_resize(struct window *window)
2563 struct widget *widget;
2565 window->resize_needed = 0;
2566 widget = window->widget;
2567 widget_set_allocation(widget,
2568 window->pending_allocation.x,
2569 window->pending_allocation.y,
2570 window->pending_allocation.width,
2571 window->pending_allocation.height);
2573 if (window->input_region) {
2574 wl_region_destroy(window->input_region);
2575 window->input_region = NULL;
2578 if (window->opaque_region) {
2579 wl_region_destroy(window->opaque_region);
2580 window->opaque_region = NULL;
2583 if (widget->resize_handler)
2584 widget->resize_handler(widget,
2585 widget->allocation.width,
2586 widget->allocation.height,
2589 if (window->allocation.width != widget->allocation.width ||
2590 window->allocation.height != widget->allocation.height) {
2591 window->allocation = widget->allocation;
2592 window_schedule_redraw(window);
2597 window_schedule_resize(struct window *window, int width, int height)
2599 window->pending_allocation.x = 0;
2600 window->pending_allocation.y = 0;
2601 window->pending_allocation.width = width;
2602 window->pending_allocation.height = height;
2604 window->resize_needed = 1;
2605 window_schedule_redraw(window);
2609 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2611 window_schedule_resize(widget->window, width, height);
2615 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2618 wl_shell_surface_pong(shell_surface, serial);
2622 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2623 uint32_t edges, int32_t width, int32_t height)
2625 struct window *window = data;
2627 if (width <= 0 || height <= 0)
2630 window->resize_edges = edges;
2631 window_schedule_resize(window, width, height);
2635 menu_destroy(struct menu *menu)
2637 widget_destroy(menu->widget);
2638 window_destroy(menu->window);
2643 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2645 struct window *window = data;
2646 struct menu *menu = window->widget->user_data;
2648 /* FIXME: Need more context in this event, at least the input
2649 * device. Or just use wl_callback. And this really needs to
2650 * be a window vfunc that the menu can set. And we need the
2653 menu->func(window->parent, menu->current, window->parent->user_data);
2654 input_ungrab(menu->input);
2658 static const struct wl_shell_surface_listener shell_surface_listener = {
2665 window_get_allocation(struct window *window,
2666 struct rectangle *allocation)
2668 *allocation = window->allocation;
2672 widget_redraw(struct widget *widget)
2674 struct widget *child;
2676 if (widget->redraw_handler)
2677 widget->redraw_handler(widget, widget->user_data);
2678 wl_list_for_each(child, &widget->child_list, link)
2679 widget_redraw(child);
2683 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2685 struct window *window = data;
2687 wl_callback_destroy(callback);
2688 window->redraw_scheduled = 0;
2689 if (window->redraw_needed)
2690 window_schedule_redraw(window);
2693 static const struct wl_callback_listener listener = {
2698 idle_redraw(struct task *task, uint32_t events)
2700 struct window *window = container_of(task, struct window, redraw_task);
2701 struct wl_callback *callback;
2703 if (window->resize_needed)
2704 idle_resize(window);
2706 window_create_surface(window);
2707 widget_redraw(window->widget);
2708 window_flush(window);
2709 window->redraw_needed = 0;
2710 wl_list_init(&window->redraw_task.link);
2712 callback = wl_surface_frame(window->surface);
2713 wl_callback_add_listener(callback, &listener, window);
2717 window_schedule_redraw(struct window *window)
2719 window->redraw_needed = 1;
2720 if (!window->redraw_scheduled) {
2721 window->redraw_task.run = idle_redraw;
2722 display_defer(window->display, &window->redraw_task);
2723 window->redraw_scheduled = 1;
2728 window_set_custom(struct window *window)
2730 window->type = TYPE_CUSTOM;
2734 window_set_fullscreen(struct window *window, int fullscreen)
2736 if (!window->display->shell)
2739 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2743 window->type = TYPE_FULLSCREEN;
2744 window->saved_allocation = window->allocation;
2745 wl_shell_surface_set_fullscreen(window->shell_surface,
2746 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2749 window->type = TYPE_TOPLEVEL;
2750 wl_shell_surface_set_toplevel(window->shell_surface);
2751 window_schedule_resize(window,
2752 window->saved_allocation.width,
2753 window->saved_allocation.height);
2758 window_set_maximized(struct window *window, int maximized)
2760 if (!window->display->shell)
2763 if ((window->type == TYPE_MAXIMIZED) == maximized)
2766 if (window->type == TYPE_TOPLEVEL) {
2767 window->saved_allocation = window->allocation;
2768 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2769 window->type = TYPE_MAXIMIZED;
2771 wl_shell_surface_set_toplevel(window->shell_surface);
2772 window->type = TYPE_TOPLEVEL;
2773 window_schedule_resize(window,
2774 window->saved_allocation.width,
2775 window->saved_allocation.height);
2780 window_set_user_data(struct window *window, void *data)
2782 window->user_data = data;
2786 window_get_user_data(struct window *window)
2788 return window->user_data;
2792 window_set_key_handler(struct window *window,
2793 window_key_handler_t handler)
2795 window->key_handler = handler;
2799 window_set_keyboard_focus_handler(struct window *window,
2800 window_keyboard_focus_handler_t handler)
2802 window->keyboard_focus_handler = handler;
2806 window_set_data_handler(struct window *window, window_data_handler_t handler)
2808 window->data_handler = handler;
2812 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2814 window->drop_handler = handler;
2818 window_set_close_handler(struct window *window,
2819 window_close_handler_t handler)
2821 window->close_handler = handler;
2825 window_set_title(struct window *window, const char *title)
2827 free(window->title);
2828 window->title = strdup(title);
2829 if (window->shell_surface)
2830 wl_shell_surface_set_title(window->shell_surface, title);
2834 window_get_title(struct window *window)
2836 return window->title;
2840 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2842 struct text_cursor_position *text_cursor_position =
2843 window->display->text_cursor_position;
2845 if (!text_cursor_position)
2848 text_cursor_position_notify(text_cursor_position,
2850 wl_fixed_from_int(x),
2851 wl_fixed_from_int(y));
2855 window_damage(struct window *window, int32_t x, int32_t y,
2856 int32_t width, int32_t height)
2858 wl_surface_damage(window->surface, x, y, width, height);
2862 surface_enter(void *data,
2863 struct wl_surface *wl_surface, struct wl_output *wl_output)
2865 struct window *window = data;
2866 struct output *output;
2867 struct output *output_found = NULL;
2868 struct window_output *window_output;
2870 wl_list_for_each(output, &window->display->output_list, link) {
2871 if (output->output == wl_output) {
2872 output_found = output;
2880 window_output = malloc (sizeof *window_output);
2881 window_output->output = output_found;
2883 wl_list_insert (&window->window_output_list, &window_output->link);
2887 surface_leave(void *data,
2888 struct wl_surface *wl_surface, struct wl_output *output)
2890 struct window *window = data;
2891 struct window_output *window_output;
2892 struct window_output *window_output_found = NULL;
2894 wl_list_for_each(window_output, &window->window_output_list, link) {
2895 if (window_output->output->output == output) {
2896 window_output_found = window_output;
2901 if (window_output_found) {
2902 wl_list_remove(&window_output_found->link);
2903 free(window_output_found);
2907 static const struct wl_surface_listener surface_listener = {
2912 static struct window *
2913 window_create_internal(struct display *display,
2914 struct window *parent, int type)
2916 struct window *window;
2918 window = malloc(sizeof *window);
2922 memset(window, 0, sizeof *window);
2923 window->display = display;
2924 window->parent = parent;
2925 window->surface = wl_compositor_create_surface(display->compositor);
2926 wl_surface_add_listener(window->surface, &surface_listener, window);
2927 if (type != TYPE_CUSTOM && display->shell) {
2928 window->shell_surface =
2929 wl_shell_get_shell_surface(display->shell,
2932 wl_shell_surface_set_title(window->shell_surface,
2935 window->allocation.x = 0;
2936 window->allocation.y = 0;
2937 window->allocation.width = 0;
2938 window->allocation.height = 0;
2939 window->saved_allocation = window->allocation;
2940 window->transparent = 1;
2941 window->type = type;
2942 window->input_region = NULL;
2943 window->opaque_region = NULL;
2946 #ifdef HAVE_CAIRO_EGL
2947 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2949 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2952 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2954 wl_surface_set_user_data(window->surface, window);
2955 wl_list_insert(display->window_list.prev, &window->link);
2956 wl_list_init(&window->redraw_task.link);
2958 if (window->shell_surface) {
2959 wl_shell_surface_set_user_data(window->shell_surface, window);
2960 wl_shell_surface_add_listener(window->shell_surface,
2961 &shell_surface_listener, window);
2964 wl_list_init (&window->window_output_list);
2970 window_create(struct display *display)
2972 struct window *window;
2974 window = window_create_internal(display, NULL, TYPE_NONE);
2982 window_create_custom(struct display *display)
2984 struct window *window;
2986 window = window_create_internal(display, NULL, TYPE_CUSTOM);
2994 window_create_transient(struct display *display, struct window *parent,
2995 int32_t x, int32_t y, uint32_t flags)
2997 struct window *window;
2999 window = window_create_internal(parent->display,
3000 parent, TYPE_TRANSIENT);
3008 wl_shell_surface_set_transient(window->shell_surface,
3009 window->parent->surface,
3010 window->x, window->y, flags);
3016 menu_set_item(struct menu *menu, int sy)
3020 next = (sy - 8) / 20;
3021 if (menu->current != next) {
3022 menu->current = next;
3023 widget_schedule_redraw(menu->widget);
3028 menu_motion_handler(struct widget *widget,
3029 struct input *input, uint32_t time,
3030 float x, float y, void *data)
3032 struct menu *menu = data;
3034 if (widget == menu->widget)
3035 menu_set_item(data, y);
3037 return CURSOR_LEFT_PTR;
3041 menu_enter_handler(struct widget *widget,
3042 struct input *input, float x, float y, void *data)
3044 struct menu *menu = data;
3046 if (widget == menu->widget)
3047 menu_set_item(data, y);
3049 return CURSOR_LEFT_PTR;
3053 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3055 struct menu *menu = data;
3057 if (widget == menu->widget)
3058 menu_set_item(data, -200);
3062 menu_button_handler(struct widget *widget,
3063 struct input *input, uint32_t time,
3064 uint32_t button, enum wl_pointer_button_state state,
3068 struct menu *menu = data;
3070 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
3071 time - menu->time > 500) {
3072 /* Either relase after press-drag-release or
3073 * click-motion-click. */
3074 menu->func(menu->window->parent,
3075 menu->current, menu->window->parent->user_data);
3076 input_ungrab(input);
3082 menu_redraw_handler(struct widget *widget, void *data)
3085 const int32_t r = 3, margin = 3;
3086 struct menu *menu = data;
3087 int32_t width, height, i;
3088 struct window *window = widget->window;
3090 cr = cairo_create(window->cairo_surface);
3091 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3092 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3095 width = window->allocation.width;
3096 height = window->allocation.height;
3097 rounded_rect(cr, 0, 0, width, height, r);
3099 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3100 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3103 for (i = 0; i < menu->count; i++) {
3104 if (i == menu->current) {
3105 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3106 cairo_rectangle(cr, margin, i * 20 + margin,
3107 width - 2 * margin, 20);
3109 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3110 cairo_move_to(cr, 10, i * 20 + 16);
3111 cairo_show_text(cr, menu->entries[i]);
3113 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3114 cairo_move_to(cr, 10, i * 20 + 16);
3115 cairo_show_text(cr, menu->entries[i]);
3123 window_show_menu(struct display *display,
3124 struct input *input, uint32_t time, struct window *parent,
3125 int32_t x, int32_t y,
3126 menu_func_t func, const char **entries, int count)
3128 struct window *window;
3130 const int32_t margin = 3;
3132 menu = malloc(sizeof *menu);
3136 window = window_create_internal(parent->display, parent, TYPE_MENU);
3142 menu->window = window;
3143 menu->widget = window_add_widget(menu->window, menu);
3144 menu->entries = entries;
3145 menu->count = count;
3149 menu->input = input;
3150 window->type = TYPE_MENU;
3154 input_ungrab(input);
3155 wl_shell_surface_set_popup(window->shell_surface, input->seat,
3156 display_get_serial(window->display),
3157 window->parent->surface,
3158 window->x, window->y, 0);
3160 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3161 widget_set_enter_handler(menu->widget, menu_enter_handler);
3162 widget_set_leave_handler(menu->widget, menu_leave_handler);
3163 widget_set_motion_handler(menu->widget, menu_motion_handler);
3164 widget_set_button_handler(menu->widget, menu_button_handler);
3166 input_grab(input, menu->widget, 0);
3167 window_schedule_resize(window, 200, count * 20 + margin * 2);
3171 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3173 window->buffer_type = type;
3178 display_handle_geometry(void *data,
3179 struct wl_output *wl_output,
3182 int physical_height,
3187 struct output *output = data;
3189 output->allocation.x = x;
3190 output->allocation.y = y;
3194 display_handle_mode(void *data,
3195 struct wl_output *wl_output,
3201 struct output *output = data;
3202 struct display *display = output->display;
3204 if (flags & WL_OUTPUT_MODE_CURRENT) {
3205 output->allocation.width = width;
3206 output->allocation.height = height;
3207 if (display->output_configure_handler)
3208 (*display->output_configure_handler)(
3209 output, display->user_data);
3213 static const struct wl_output_listener output_listener = {
3214 display_handle_geometry,
3219 display_add_output(struct display *d, uint32_t id)
3221 struct output *output;
3223 output = malloc(sizeof *output);
3227 memset(output, 0, sizeof *output);
3228 output->display = d;
3230 wl_display_bind(d->display, id, &wl_output_interface);
3231 wl_list_insert(d->output_list.prev, &output->link);
3233 wl_output_add_listener(output->output, &output_listener, output);
3237 output_destroy(struct output *output)
3239 if (output->destroy_handler)
3240 (*output->destroy_handler)(output, output->user_data);
3242 wl_output_destroy(output->output);
3243 wl_list_remove(&output->link);
3248 display_set_output_configure_handler(struct display *display,
3249 display_output_handler_t handler)
3251 struct output *output;
3253 display->output_configure_handler = handler;
3257 wl_list_for_each(output, &display->output_list, link)
3258 (*display->output_configure_handler)(output,
3259 display->user_data);
3263 output_set_user_data(struct output *output, void *data)
3265 output->user_data = data;
3269 output_get_user_data(struct output *output)
3271 return output->user_data;
3275 output_set_destroy_handler(struct output *output,
3276 display_output_handler_t handler)
3278 output->destroy_handler = handler;
3279 /* FIXME: implement this, once we have way to remove outputs */
3283 output_get_allocation(struct output *output, struct rectangle *allocation)
3285 *allocation = output->allocation;
3289 output_get_wl_output(struct output *output)
3291 return output->output;
3295 fini_xkb(struct input *input)
3297 xkb_state_unref(input->xkb.state);
3298 xkb_map_unref(input->xkb.keymap);
3302 display_add_input(struct display *d, uint32_t id)
3304 struct input *input;
3306 input = malloc(sizeof *input);
3310 memset(input, 0, sizeof *input);
3312 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3313 input->pointer_focus = NULL;
3314 input->keyboard_focus = NULL;
3315 wl_list_insert(d->input_list.prev, &input->link);
3317 wl_seat_add_listener(input->seat, &seat_listener, input);
3318 wl_seat_set_user_data(input->seat, input);
3320 input->data_device =
3321 wl_data_device_manager_get_data_device(d->data_device_manager,
3323 wl_data_device_add_listener(input->data_device, &data_device_listener,
3326 input->pointer_surface = wl_compositor_create_surface(d->compositor);
3328 input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3329 TFD_CLOEXEC | TFD_NONBLOCK);
3330 input->repeat_task.run = keyboard_repeat_func;
3331 display_watch_fd(d, input->repeat_timer_fd,
3332 EPOLLIN, &input->repeat_task);
3336 input_destroy(struct input *input)
3338 input_remove_keyboard_focus(input);
3339 input_remove_pointer_focus(input);
3341 if (input->xkb.state)
3342 xkb_state_unref(input->xkb.state);
3343 if (input->xkb.keymap)
3344 xkb_map_unref(input->xkb.keymap);
3346 if (input->drag_offer)
3347 data_offer_destroy(input->drag_offer);
3349 if (input->selection_offer)
3350 data_offer_destroy(input->selection_offer);
3352 wl_data_device_destroy(input->data_device);
3355 wl_surface_destroy(input->pointer_surface);
3357 wl_list_remove(&input->link);
3358 wl_seat_destroy(input->seat);
3359 close(input->repeat_timer_fd);
3364 display_handle_global(struct wl_display *display, uint32_t id,
3365 const char *interface, uint32_t version, void *data)
3367 struct display *d = data;
3369 if (strcmp(interface, "wl_compositor") == 0) {
3371 wl_display_bind(display, id, &wl_compositor_interface);
3372 } else if (strcmp(interface, "wl_output") == 0) {
3373 display_add_output(d, id);
3374 } else if (strcmp(interface, "wl_seat") == 0) {
3375 display_add_input(d, id);
3376 } else if (strcmp(interface, "wl_shell") == 0) {
3377 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3378 } else if (strcmp(interface, "wl_shm") == 0) {
3379 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3380 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3381 d->data_device_manager =
3382 wl_display_bind(display, id,
3383 &wl_data_device_manager_interface);
3384 } else if (strcmp(interface, "text_cursor_position") == 0) {
3385 d->text_cursor_position =
3386 wl_display_bind(display, id,
3387 &text_cursor_position_interface);
3391 #ifdef HAVE_CAIRO_EGL
3393 init_egl(struct display *d)
3395 EGLint major, minor;
3398 #ifdef USE_CAIRO_GLESV2
3399 # define GL_BIT EGL_OPENGL_ES2_BIT
3401 # define GL_BIT EGL_OPENGL_BIT
3404 static const EGLint argb_cfg_attribs[] = {
3405 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3411 EGL_RENDERABLE_TYPE, GL_BIT,
3415 #ifdef USE_CAIRO_GLESV2
3416 static const EGLint context_attribs[] = {
3417 EGL_CONTEXT_CLIENT_VERSION, 2,
3420 EGLint api = EGL_OPENGL_ES_API;
3422 EGLint *context_attribs = NULL;
3423 EGLint api = EGL_OPENGL_API;
3426 d->dpy = eglGetDisplay(d->display);
3427 if (!eglInitialize(d->dpy, &major, &minor)) {
3428 fprintf(stderr, "failed to initialize display\n");
3432 if (!eglBindAPI(api)) {
3433 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3437 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3438 &d->argb_config, 1, &n) || n != 1) {
3439 fprintf(stderr, "failed to choose argb config\n");
3443 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3444 EGL_NO_CONTEXT, context_attribs);
3445 if (d->argb_ctx == NULL) {
3446 fprintf(stderr, "failed to create context\n");
3450 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3451 fprintf(stderr, "failed to make context current\n");
3455 #ifdef HAVE_CAIRO_EGL
3456 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3457 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3458 fprintf(stderr, "failed to get cairo egl argb device\n");
3467 fini_egl(struct display *display)
3469 #ifdef HAVE_CAIRO_EGL
3470 cairo_device_destroy(display->argb_device);
3473 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3476 eglTerminate(display->dpy);
3482 event_mask_update(uint32_t mask, void *data)
3484 struct display *d = data;
3492 handle_display_data(struct task *task, uint32_t events)
3494 struct display *display =
3495 container_of(task, struct display, display_task);
3497 wl_display_iterate(display->display, display->mask);
3501 display_create(int argc, char *argv[])
3505 argc = parse_options(xkb_options,
3506 ARRAY_LENGTH(xkb_options), argc, argv);
3508 d = malloc(sizeof *d);
3512 memset(d, 0, sizeof *d);
3514 d->display = wl_display_connect(NULL);
3515 if (d->display == NULL) {
3516 fprintf(stderr, "failed to create display: %m\n");
3520 d->epoll_fd = os_epoll_create_cloexec();
3521 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3522 d->display_task.run = handle_display_data;
3523 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3525 wl_list_init(&d->deferred_list);
3526 wl_list_init(&d->input_list);
3527 wl_list_init(&d->output_list);
3529 d->xkb_context = xkb_context_new(0);
3530 if (d->xkb_context == NULL) {
3531 fprintf(stderr, "Failed to create XKB context\n");
3535 /* Set up listener so we'll catch all events. */
3536 wl_display_add_global_listener(d->display,
3537 display_handle_global, d);
3539 /* Process connection events. */
3540 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3541 #ifdef HAVE_CAIRO_EGL
3542 if (init_egl(d) < 0)
3546 d->image_target_texture_2d =
3547 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3548 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3549 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3553 d->theme = theme_create();
3555 wl_list_init(&d->window_list);
3561 display_destroy_outputs(struct display *display)
3564 struct output *output;
3566 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3567 output_destroy(output);
3571 display_destroy_inputs(struct display *display)
3574 struct input *input;
3576 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3577 input_destroy(input);
3581 display_destroy(struct display *display)
3583 if (!wl_list_empty(&display->window_list))
3584 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3586 if (!wl_list_empty(&display->deferred_list))
3587 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3589 display_destroy_outputs(display);
3590 display_destroy_inputs(display);
3592 xkb_context_unref(display->xkb_context);
3594 theme_destroy(display->theme);
3595 destroy_cursors(display);
3597 #ifdef HAVE_CAIRO_EGL
3602 wl_shell_destroy(display->shell);
3605 wl_shm_destroy(display->shm);
3607 if (display->data_device_manager)
3608 wl_data_device_manager_destroy(display->data_device_manager);
3610 wl_compositor_destroy(display->compositor);
3612 close(display->epoll_fd);
3614 wl_display_flush(display->display);
3615 wl_display_disconnect(display->display);
3620 display_set_user_data(struct display *display, void *data)
3622 display->user_data = data;
3626 display_get_user_data(struct display *display)
3628 return display->user_data;
3632 display_get_display(struct display *display)
3634 return display->display;
3638 display_get_output(struct display *display)
3640 return container_of(display->output_list.next, struct output, link);
3643 struct wl_compositor *
3644 display_get_compositor(struct display *display)
3646 return display->compositor;
3650 display_get_serial(struct display *display)
3652 return display->serial;
3656 display_get_egl_display(struct display *d)
3661 struct wl_data_source *
3662 display_create_data_source(struct display *display)
3664 return wl_data_device_manager_create_data_source(display->data_device_manager);
3668 display_get_argb_egl_config(struct display *d)
3670 return d->argb_config;
3674 display_get_shell(struct display *display)
3676 return display->shell;
3680 display_acquire_window_surface(struct display *display,
3681 struct window *window,
3684 #ifdef HAVE_CAIRO_EGL
3685 struct egl_window_surface_data *data;
3686 cairo_device_t *device;
3688 if (!window->cairo_surface)
3690 device = cairo_surface_get_device(window->cairo_surface);
3695 if (device == display->argb_device)
3696 ctx = display->argb_ctx;
3701 data = cairo_surface_get_user_data(window->cairo_surface,
3704 cairo_device_flush(device);
3705 cairo_device_acquire(device);
3706 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3707 fprintf(stderr, "failed to make surface current\n");
3716 display_release_window_surface(struct display *display,
3717 struct window *window)
3719 #ifdef HAVE_CAIRO_EGL
3720 cairo_device_t *device;
3722 device = cairo_surface_get_device(window->cairo_surface);
3726 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3727 fprintf(stderr, "failed to make context current\n");
3728 cairo_device_release(device);
3733 display_defer(struct display *display, struct task *task)
3735 wl_list_insert(&display->deferred_list, &task->link);
3739 display_watch_fd(struct display *display,
3740 int fd, uint32_t events, struct task *task)
3742 struct epoll_event ep;
3746 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3750 display_run(struct display *display)
3753 struct epoll_event ep[16];
3756 display->running = 1;
3758 wl_display_flush(display->display);
3760 while (!wl_list_empty(&display->deferred_list)) {
3761 task = container_of(display->deferred_list.next,
3763 wl_list_remove(&task->link);
3767 if (!display->running)
3770 wl_display_flush(display->display);
3772 count = epoll_wait(display->epoll_fd,
3773 ep, ARRAY_LENGTH(ep), -1);
3774 for (i = 0; i < count; i++) {
3775 task = ep[i].data.ptr;
3776 task->run(task, ep[i].events);
3782 display_exit(struct display *display)
3784 display->running = 0;