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 int send_cursor_position;
148 struct input *keyboard_device;
149 enum window_buffer_type buffer_type;
151 cairo_surface_t *cairo_surface;
153 struct shm_pool *pool;
155 window_key_handler_t key_handler;
156 window_keyboard_focus_handler_t keyboard_focus_handler;
157 window_data_handler_t data_handler;
158 window_drop_handler_t drop_handler;
159 window_close_handler_t close_handler;
162 struct widget *widget;
169 struct window *window;
170 struct tooltip *tooltip;
171 struct wl_list child_list;
173 struct rectangle allocation;
174 widget_resize_handler_t resize_handler;
175 widget_redraw_handler_t redraw_handler;
176 widget_enter_handler_t enter_handler;
177 widget_leave_handler_t leave_handler;
178 widget_motion_handler_t motion_handler;
179 widget_button_handler_t button_handler;
186 struct display *display;
187 struct wl_seat *seat;
188 struct wl_pointer *pointer;
189 struct wl_keyboard *keyboard;
190 struct window *pointer_focus;
191 struct window *keyboard_focus;
194 uint32_t pointer_enter_serial;
198 struct widget *focus_widget;
200 uint32_t grab_button;
202 struct wl_data_device *data_device;
203 struct data_offer *drag_offer;
204 struct data_offer *selection_offer;
207 struct xkb_keymap *keymap;
208 struct xkb_state *state;
209 xkb_mod_mask_t control_mask;
210 xkb_mod_mask_t alt_mask;
211 xkb_mod_mask_t shift_mask;
216 struct display *display;
217 struct wl_output *output;
218 struct rectangle allocation;
221 display_output_handler_t destroy_handler;
225 enum frame_button_action {
226 FRAME_BUTTON_NULL = 0,
227 FRAME_BUTTON_ICON = 1,
228 FRAME_BUTTON_CLOSE = 2,
229 FRAME_BUTTON_MINIMIZE = 3,
230 FRAME_BUTTON_MAXIMIZE = 4,
233 enum frame_button_pointer {
234 FRAME_BUTTON_DEFAULT = 0,
235 FRAME_BUTTON_OVER = 1,
236 FRAME_BUTTON_ACTIVE = 2,
239 enum frame_button_align {
240 FRAME_BUTTON_RIGHT = 0,
241 FRAME_BUTTON_LEFT = 1,
244 enum frame_button_decoration {
245 FRAME_BUTTON_NONE = 0,
246 FRAME_BUTTON_FANCY = 1,
249 struct frame_button {
250 struct widget *widget;
252 cairo_surface_t *icon;
253 enum frame_button_action type;
254 enum frame_button_pointer state;
255 struct wl_list link; /* buttons_list */
256 enum frame_button_align align;
257 enum frame_button_decoration decoration;
261 struct widget *widget;
262 struct widget *child;
263 struct wl_list buttons_list;
267 struct window *window;
268 struct widget *widget;
270 const char **entries;
278 struct widget *parent;
279 struct window *window;
280 struct widget *widget;
282 struct task tooltip_task;
288 struct wl_shm_pool *pool;
295 CURSOR_DEFAULT = 100,
299 enum window_location {
301 WINDOW_RESIZING_TOP = 1,
302 WINDOW_RESIZING_BOTTOM = 2,
303 WINDOW_RESIZING_LEFT = 4,
304 WINDOW_RESIZING_TOP_LEFT = 5,
305 WINDOW_RESIZING_BOTTOM_LEFT = 6,
306 WINDOW_RESIZING_RIGHT = 8,
307 WINDOW_RESIZING_TOP_RIGHT = 9,
308 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
309 WINDOW_RESIZING_MASK = 15,
310 WINDOW_EXTERIOR = 16,
311 WINDOW_TITLEBAR = 17,
312 WINDOW_CLIENT_AREA = 18,
315 const char *option_xkb_layout = "us";
316 const char *option_xkb_variant = "";
317 const char *option_xkb_options = "";
319 static const struct weston_option xkb_options[] = {
320 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
321 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
322 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
325 static const cairo_user_data_key_t surface_data_key;
326 struct surface_data {
327 struct wl_buffer *buffer;
330 #define MULT(_d,c,a,t) \
331 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
333 #ifdef HAVE_CAIRO_EGL
335 struct egl_window_surface_data {
336 struct display *display;
337 struct wl_surface *surface;
338 struct wl_egl_window *window;
343 egl_window_surface_data_destroy(void *p)
345 struct egl_window_surface_data *data = p;
346 struct display *d = data->display;
348 eglDestroySurface(d->dpy, data->surf);
349 wl_egl_window_destroy(data->window);
350 data->surface = NULL;
355 static cairo_surface_t *
356 display_create_egl_window_surface(struct display *display,
357 struct wl_surface *surface,
359 struct rectangle *rectangle)
361 cairo_surface_t *cairo_surface;
362 struct egl_window_surface_data *data;
364 cairo_device_t *device;
366 data = malloc(sizeof *data);
370 data->display = display;
371 data->surface = surface;
373 config = display->argb_config;
374 device = display->argb_device;
376 data->window = wl_egl_window_create(surface,
380 data->surf = eglCreateWindowSurface(display->dpy, config,
383 cairo_surface = cairo_gl_surface_create_for_egl(device,
388 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
389 data, egl_window_surface_data_destroy);
391 return cairo_surface;
397 display_get_buffer_for_surface(struct display *display,
398 cairo_surface_t *surface)
400 struct surface_data *data;
402 data = cairo_surface_get_user_data (surface, &surface_data_key);
407 struct shm_surface_data {
408 struct surface_data data;
409 struct shm_pool *pool;
413 shm_pool_destroy(struct shm_pool *pool);
416 shm_surface_data_destroy(void *p)
418 struct shm_surface_data *data = p;
420 wl_buffer_destroy(data->data.buffer);
422 shm_pool_destroy(data->pool);
425 static struct wl_shm_pool *
426 make_shm_pool(struct display *display, int size, void **data)
428 char filename[] = "/tmp/wayland-shm-XXXXXX";
429 struct wl_shm_pool *pool;
432 fd = mkstemp(filename);
434 fprintf(stderr, "open %s failed: %m\n", filename);
437 if (ftruncate(fd, size) < 0) {
438 fprintf(stderr, "ftruncate failed: %m\n");
443 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
446 if (*data == MAP_FAILED) {
447 fprintf(stderr, "mmap failed: %m\n");
452 pool = wl_shm_create_pool(display->shm, fd, size);
459 static struct shm_pool *
460 shm_pool_create(struct display *display, size_t size)
462 struct shm_pool *pool = malloc(sizeof *pool);
467 pool->pool = make_shm_pool(display, size, &pool->data);
480 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
482 if (pool->used + size > pool->size)
485 *offset = pool->used;
488 return (char *) pool->data + *offset;
491 /* destroy the pool. this does not unmap the memory though */
493 shm_pool_destroy(struct shm_pool *pool)
495 munmap(pool->data, pool->size);
496 wl_shm_pool_destroy(pool->pool);
500 /* Start allocating from the beginning of the pool again */
502 shm_pool_reset(struct shm_pool *pool)
508 data_length_for_shm_surface(struct rectangle *rect)
512 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
514 return stride * rect->height;
517 static cairo_surface_t *
518 display_create_shm_surface_from_pool(struct display *display,
519 struct rectangle *rectangle,
520 uint32_t flags, struct shm_pool *pool)
522 struct shm_surface_data *data;
524 cairo_surface_t *surface;
525 int stride, length, offset;
528 data = malloc(sizeof *data);
532 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
534 length = stride * rectangle->height;
536 map = shm_pool_allocate(pool, length, &offset);
543 surface = cairo_image_surface_create_for_data (map,
549 cairo_surface_set_user_data (surface, &surface_data_key,
550 data, shm_surface_data_destroy);
552 if (flags & SURFACE_OPAQUE)
553 format = WL_SHM_FORMAT_XRGB8888;
555 format = WL_SHM_FORMAT_ARGB8888;
557 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
565 static cairo_surface_t *
566 display_create_shm_surface(struct display *display,
567 struct rectangle *rectangle, uint32_t flags,
568 struct window *window)
570 struct shm_surface_data *data;
571 struct shm_pool *pool;
572 cairo_surface_t *surface;
574 if (window && window->pool) {
575 shm_pool_reset(window->pool);
576 surface = display_create_shm_surface_from_pool(display,
584 pool = shm_pool_create(display,
585 data_length_for_shm_surface(rectangle));
590 display_create_shm_surface_from_pool(display, rectangle,
594 shm_pool_destroy(pool);
598 /* make sure we destroy the pool when the surface is destroyed */
599 data = cairo_surface_get_user_data(surface, &surface_data_key);
606 check_size(struct rectangle *rect)
608 if (rect->width && rect->height)
611 fprintf(stderr, "tried to create surface of "
612 "width: %d, height: %d\n", rect->width, rect->height);
617 display_create_surface(struct display *display,
618 struct wl_surface *surface,
619 struct rectangle *rectangle,
622 if (check_size(rectangle) < 0)
624 #ifdef HAVE_CAIRO_EGL
625 if (display->dpy && !(flags & SURFACE_SHM))
626 return display_create_egl_window_surface(display,
631 return display_create_shm_surface(display, rectangle, flags, NULL);
634 static const char *cursors[] = {
635 "bottom_left_corner",
636 "bottom_right_corner",
650 create_cursors(struct display *display)
654 display->cursor_theme = wl_cursor_theme_load(NULL, 32, display->shm);
656 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
658 for (i = 0; i < ARRAY_LENGTH(cursors); i++)
659 display->cursors[i] =
660 wl_cursor_theme_get_cursor(display->cursor_theme,
665 destroy_cursors(struct display *display)
667 wl_cursor_theme_destroy(display->cursor_theme);
668 free(display->cursors);
671 struct wl_cursor_image *
672 display_get_pointer_image(struct display *display, int pointer)
674 struct wl_cursor *cursor =
675 wl_cursor_theme_get_cursor(display->cursor_theme,
678 return cursor ? cursor->images[0] : NULL;
682 window_get_resize_dx_dy(struct window *window, int *x, int *y)
684 if (window->resize_edges & WINDOW_RESIZING_LEFT)
685 *x = window->server_allocation.width - window->allocation.width;
689 if (window->resize_edges & WINDOW_RESIZING_TOP)
690 *y = window->server_allocation.height -
691 window->allocation.height;
695 window->resize_edges = 0;
699 window_attach_surface(struct window *window)
701 struct display *display = window->display;
702 struct wl_buffer *buffer;
703 #ifdef HAVE_CAIRO_EGL
704 struct egl_window_surface_data *data;
708 if (window->type == TYPE_NONE) {
709 window->type = TYPE_TOPLEVEL;
711 wl_shell_surface_set_toplevel(window->shell_surface);
714 switch (window->buffer_type) {
715 #ifdef HAVE_CAIRO_EGL
716 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
717 data = cairo_surface_get_user_data(window->cairo_surface,
720 cairo_gl_surface_swapbuffers(window->cairo_surface);
721 wl_egl_window_get_attached_size(data->window,
722 &window->server_allocation.width,
723 &window->server_allocation.height);
726 case WINDOW_BUFFER_TYPE_SHM:
728 display_get_buffer_for_surface(display,
729 window->cairo_surface);
731 window_get_resize_dx_dy(window, &x, &y);
732 wl_surface_attach(window->surface, buffer, x, y);
733 wl_surface_damage(window->surface, 0, 0,
734 window->allocation.width,
735 window->allocation.height);
736 window->server_allocation = window->allocation;
737 cairo_surface_destroy(window->cairo_surface);
738 window->cairo_surface = NULL;
744 if (window->input_region) {
745 wl_surface_set_input_region(window->surface,
746 window->input_region);
747 wl_region_destroy(window->input_region);
748 window->input_region = NULL;
751 if (window->opaque_region) {
752 wl_surface_set_opaque_region(window->surface,
753 window->opaque_region);
754 wl_region_destroy(window->opaque_region);
755 window->opaque_region = NULL;
760 window_flush(struct window *window)
762 if (window->cairo_surface)
763 window_attach_surface(window);
767 window_set_surface(struct window *window, cairo_surface_t *surface)
769 cairo_surface_reference(surface);
771 if (window->cairo_surface != NULL)
772 cairo_surface_destroy(window->cairo_surface);
774 window->cairo_surface = surface;
777 #ifdef HAVE_CAIRO_EGL
779 window_resize_cairo_window_surface(struct window *window)
781 struct egl_window_surface_data *data;
784 data = cairo_surface_get_user_data(window->cairo_surface,
787 window_get_resize_dx_dy(window, &x, &y),
788 wl_egl_window_resize(data->window,
789 window->allocation.width,
790 window->allocation.height,
793 cairo_gl_surface_set_size(window->cairo_surface,
794 window->allocation.width,
795 window->allocation.height);
800 window_get_display(struct window *window)
802 return window->display;
806 window_create_surface(struct window *window)
808 cairo_surface_t *surface;
811 if (!window->transparent)
812 flags = SURFACE_OPAQUE;
814 switch (window->buffer_type) {
815 #ifdef HAVE_CAIRO_EGL
816 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
817 if (window->cairo_surface) {
818 window_resize_cairo_window_surface(window);
821 surface = display_create_surface(window->display,
823 &window->allocation, flags);
826 case WINDOW_BUFFER_TYPE_SHM:
827 surface = display_create_shm_surface(window->display,
836 window_set_surface(window, surface);
837 cairo_surface_destroy(surface);
840 static void frame_destroy(struct frame *frame);
843 window_destroy(struct window *window)
845 struct display *display = window->display;
847 struct window_output *window_output;
848 struct window_output *window_output_tmp;
850 if (window->redraw_scheduled)
851 wl_list_remove(&window->redraw_task.link);
853 wl_list_for_each(input, &display->input_list, link) {
854 if (input->pointer_focus == window)
855 input->pointer_focus = NULL;
856 if (input->keyboard_focus == window)
857 input->keyboard_focus = NULL;
858 if (input->focus_widget &&
859 input->focus_widget->window == window)
860 input->focus_widget = NULL;
863 wl_list_for_each_safe(window_output, window_output_tmp,
864 &window->window_output_list, link) {
865 free (window_output);
868 if (window->input_region)
869 wl_region_destroy(window->input_region);
870 if (window->opaque_region)
871 wl_region_destroy(window->opaque_region);
874 frame_destroy(window->frame);
876 if (window->shell_surface)
877 wl_shell_surface_destroy(window->shell_surface);
878 wl_surface_destroy(window->surface);
879 wl_list_remove(&window->link);
881 if (window->cairo_surface != NULL)
882 cairo_surface_destroy(window->cairo_surface);
888 static struct widget *
889 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
891 struct widget *child, *target;
893 wl_list_for_each(child, &widget->child_list, link) {
894 target = widget_find_widget(child, x, y);
899 if (widget->allocation.x <= x &&
900 x < widget->allocation.x + widget->allocation.width &&
901 widget->allocation.y <= y &&
902 y < widget->allocation.y + widget->allocation.height) {
909 static struct widget *
910 widget_create(struct window *window, void *data)
912 struct widget *widget;
914 widget = malloc(sizeof *widget);
915 memset(widget, 0, sizeof *widget);
916 widget->window = window;
917 widget->user_data = data;
918 widget->allocation = window->allocation;
919 wl_list_init(&widget->child_list);
921 widget->tooltip = NULL;
922 widget->tooltip_count = 0;
928 window_add_widget(struct window *window, void *data)
930 window->widget = widget_create(window, data);
931 wl_list_init(&window->widget->link);
933 return window->widget;
937 widget_add_widget(struct widget *parent, void *data)
939 struct widget *widget;
941 widget = widget_create(parent->window, data);
942 wl_list_insert(parent->child_list.prev, &widget->link);
948 widget_destroy(struct widget *widget)
950 struct display *display = widget->window->display;
953 if (widget->tooltip) {
954 free(widget->tooltip);
955 widget->tooltip = NULL;
958 wl_list_for_each(input, &display->input_list, link) {
959 if (input->focus_widget == widget)
960 input->focus_widget = NULL;
963 wl_list_remove(&widget->link);
968 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
970 *allocation = widget->allocation;
974 widget_set_size(struct widget *widget, int32_t width, int32_t height)
976 widget->allocation.width = width;
977 widget->allocation.height = height;
981 widget_set_allocation(struct widget *widget,
982 int32_t x, int32_t y, int32_t width, int32_t height)
984 widget->allocation.x = x;
985 widget->allocation.y = y;
986 widget_set_size(widget, width, height);
990 widget_set_transparent(struct widget *widget, int transparent)
992 widget->opaque = !transparent;
996 widget_get_user_data(struct widget *widget)
998 return widget->user_data;
1002 widget_set_resize_handler(struct widget *widget,
1003 widget_resize_handler_t handler)
1005 widget->resize_handler = handler;
1009 widget_set_redraw_handler(struct widget *widget,
1010 widget_redraw_handler_t handler)
1012 widget->redraw_handler = handler;
1016 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1018 widget->enter_handler = handler;
1022 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1024 widget->leave_handler = handler;
1028 widget_set_motion_handler(struct widget *widget,
1029 widget_motion_handler_t handler)
1031 widget->motion_handler = handler;
1035 widget_set_button_handler(struct widget *widget,
1036 widget_button_handler_t handler)
1038 widget->button_handler = handler;
1042 widget_schedule_redraw(struct widget *widget)
1044 window_schedule_redraw(widget->window);
1048 window_get_surface(struct window *window)
1050 return cairo_surface_reference(window->cairo_surface);
1054 window_get_wl_surface(struct window *window)
1056 return window->surface;
1059 struct wl_shell_surface *
1060 window_get_wl_shell_surface(struct window *window)
1062 return window->shell_surface;
1066 tooltip_redraw_handler(struct widget *widget, void *data)
1069 const int32_t r = 3;
1070 struct tooltip *tooltip = data;
1071 int32_t width, height;
1072 struct window *window = widget->window;
1074 cr = cairo_create(window->cairo_surface);
1075 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1076 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1079 width = window->allocation.width;
1080 height = window->allocation.height;
1081 rounded_rect(cr, 0, 0, width, height, r);
1083 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1084 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1087 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1088 cairo_move_to(cr, 10, 16);
1089 cairo_show_text(cr, tooltip->entry);
1093 static cairo_text_extents_t
1094 get_text_extents(struct tooltip *tooltip)
1096 struct window *window;
1098 cairo_text_extents_t extents;
1100 /* we borrow cairo_surface from the parent cause tooltip's wasn't
1102 window = tooltip->widget->window->parent;
1103 cr = cairo_create(window->cairo_surface);
1104 cairo_text_extents(cr, tooltip->entry, &extents);
1111 window_create_tooltip(struct tooltip *tooltip)
1113 struct widget *parent = tooltip->parent;
1114 struct display *display = parent->window->display;
1115 struct window *window;
1116 const int offset_y = 27;
1117 const int margin = 3;
1118 cairo_text_extents_t extents;
1120 if (tooltip->widget)
1123 window = window_create_transient(display, parent->window, tooltip->x,
1124 tooltip->y + offset_y,
1125 WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1129 tooltip->window = window;
1130 tooltip->widget = window_add_widget(tooltip->window, tooltip);
1132 extents = get_text_extents(tooltip);
1133 widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1134 window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1140 widget_destroy_tooltip(struct widget *parent)
1142 struct tooltip *tooltip = parent->tooltip;
1144 parent->tooltip_count = 0;
1148 if (tooltip->widget) {
1149 widget_destroy(tooltip->widget);
1150 window_destroy(tooltip->window);
1151 tooltip->widget = NULL;
1152 tooltip->window = NULL;
1155 close(tooltip->tooltip_fd);
1156 free(tooltip->entry);
1158 parent->tooltip = NULL;
1162 tooltip_func(struct task *task, uint32_t events)
1164 struct tooltip *tooltip =
1165 container_of(task, struct tooltip, tooltip_task);
1168 read(tooltip->tooltip_fd, &exp, sizeof (uint64_t));
1169 window_create_tooltip(tooltip);
1172 #define TOOLTIP_TIMEOUT 500
1174 tooltip_timer_reset(struct tooltip *tooltip)
1176 struct itimerspec its;
1178 its.it_interval.tv_sec = 0;
1179 its.it_interval.tv_nsec = 0;
1180 its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1181 its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1182 if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1183 fprintf(stderr, "could not set timerfd\n: %m");
1191 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1193 struct tooltip *tooltip = parent->tooltip;
1195 parent->tooltip_count++;
1199 tooltip_timer_reset(tooltip);
1203 /* the handler might be triggered too fast via input device motion, so
1204 * we need this check here to make sure tooltip is fully initialized */
1205 if (parent->tooltip_count > 1)
1208 tooltip = malloc(sizeof *tooltip);
1212 parent->tooltip = tooltip;
1213 tooltip->parent = parent;
1214 tooltip->widget = NULL;
1215 tooltip->window = NULL;
1218 tooltip->entry = strdup(entry);
1219 tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1220 if (tooltip->tooltip_fd < 0) {
1221 fprintf(stderr, "could not create timerfd\n: %m");
1225 tooltip->tooltip_task.run = tooltip_func;
1226 display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1227 EPOLLIN, &tooltip->tooltip_task);
1228 tooltip_timer_reset(tooltip);
1234 frame_resize_handler(struct widget *widget,
1235 int32_t width, int32_t height, void *data)
1237 struct frame *frame = data;
1238 struct widget *child = frame->child;
1239 struct rectangle allocation;
1240 struct display *display = widget->window->display;
1241 struct frame_button * button;
1242 struct theme *t = display->theme;
1243 int x_l, x_r, y, w, h;
1244 int decoration_width, decoration_height;
1247 if (widget->window->type != TYPE_FULLSCREEN) {
1248 decoration_width = (t->width + t->margin) * 2;
1249 decoration_height = t->width +
1250 t->titlebar_height + t->margin * 2;
1252 allocation.x = t->width + t->margin;
1253 allocation.y = t->titlebar_height + t->margin;
1254 allocation.width = width - decoration_width;
1255 allocation.height = height - decoration_height;
1257 opaque_margin = t->margin + t->frame_radius;
1259 wl_list_for_each(button, &frame->buttons_list, link)
1260 button->widget->opaque = 0;
1262 decoration_width = 0;
1263 decoration_height = 0;
1267 allocation.width = width;
1268 allocation.height = height;
1271 wl_list_for_each(button, &frame->buttons_list, link)
1272 button->widget->opaque = 1;
1275 widget_set_allocation(child, allocation.x, allocation.y,
1276 allocation.width, allocation.height);
1278 if (child->resize_handler)
1279 child->resize_handler(child,
1284 width = child->allocation.width + decoration_width;
1285 height = child->allocation.height + decoration_height;
1287 widget->window->input_region =
1288 wl_compositor_create_region(display->compositor);
1289 wl_region_add(widget->window->input_region,
1290 t->margin, t->margin,
1291 width - 2 * t->margin,
1292 height - 2 * t->margin);
1294 widget_set_allocation(widget, 0, 0, width, height);
1296 if (child->opaque) {
1297 widget->window->opaque_region =
1298 wl_compositor_create_region(display->compositor);
1299 wl_region_add(widget->window->opaque_region,
1300 opaque_margin, opaque_margin,
1301 widget->allocation.width - 2 * opaque_margin,
1302 widget->allocation.height - 2 * opaque_margin);
1305 /* frame internal buttons */
1306 x_r = frame->widget->allocation.width - t->width - t->margin;
1307 x_l = t->width + t->margin;
1308 y = t->width + t->margin;
1309 wl_list_for_each(button, &frame->buttons_list, link) {
1310 const int button_padding = 4;
1311 w = cairo_image_surface_get_width(button->icon);
1312 h = cairo_image_surface_get_height(button->icon);
1314 if (button->decoration == FRAME_BUTTON_FANCY)
1317 if (button->align == FRAME_BUTTON_LEFT) {
1318 widget_set_allocation(button->widget,
1319 x_l, y , w + 1, h + 1);
1321 x_l += button_padding;
1324 widget_set_allocation(button->widget,
1325 x_r, y , w + 1, h + 1);
1326 x_r -= button_padding;
1332 frame_button_enter_handler(struct widget *widget,
1333 struct input *input, float x, float y, void *data)
1335 struct frame_button *frame_button = data;
1337 widget_schedule_redraw(frame_button->widget);
1338 frame_button->state = FRAME_BUTTON_OVER;
1340 return CURSOR_LEFT_PTR;
1344 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1346 struct frame_button *frame_button = data;
1348 widget_schedule_redraw(frame_button->widget);
1349 frame_button->state = FRAME_BUTTON_DEFAULT;
1353 frame_button_button_handler(struct widget *widget,
1354 struct input *input, uint32_t time,
1356 enum wl_pointer_button_state state, void *data)
1358 struct frame_button *frame_button = data;
1359 struct window *window = widget->window;
1361 if (button != BTN_LEFT)
1365 case WL_POINTER_BUTTON_STATE_PRESSED:
1366 frame_button->state = FRAME_BUTTON_ACTIVE;
1367 widget_schedule_redraw(frame_button->widget);
1369 if (frame_button->type == FRAME_BUTTON_ICON)
1370 window_show_frame_menu(window, input, time);
1372 case WL_POINTER_BUTTON_STATE_RELEASED:
1373 frame_button->state = FRAME_BUTTON_DEFAULT;
1374 widget_schedule_redraw(frame_button->widget);
1378 switch (frame_button->type) {
1379 case FRAME_BUTTON_CLOSE:
1380 if (window->close_handler)
1381 window->close_handler(window->parent,
1384 display_exit(window->display);
1386 case FRAME_BUTTON_MINIMIZE:
1387 fprintf(stderr,"Minimize stub\n");
1389 case FRAME_BUTTON_MAXIMIZE:
1390 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1393 /* Unknown operation */
1399 frame_button_redraw_handler(struct widget *widget, void *data)
1401 struct frame_button *frame_button = data;
1403 int width, height, x, y;
1404 struct window *window = widget->window;
1406 x = widget->allocation.x;
1407 y = widget->allocation.y;
1408 width = widget->allocation.width;
1409 height = widget->allocation.height;
1418 cr = cairo_create(window->cairo_surface);
1420 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1421 cairo_set_line_width(cr, 1);
1423 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1424 cairo_rectangle (cr, x, y, 25, 16);
1426 cairo_stroke_preserve(cr);
1428 switch (frame_button->state) {
1429 case FRAME_BUTTON_DEFAULT:
1430 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1432 case FRAME_BUTTON_OVER:
1433 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1435 case FRAME_BUTTON_ACTIVE:
1436 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1445 cairo_set_source_surface(cr, frame_button->icon, x, y);
1451 static struct widget *
1452 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1453 enum frame_button_align align, enum frame_button_decoration style)
1455 struct frame_button *frame_button;
1456 const char *icon = data;
1458 frame_button = malloc (sizeof *frame_button);
1459 memset(frame_button, 0, sizeof *frame_button);
1461 frame_button->icon = cairo_image_surface_create_from_png(icon);
1462 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1463 frame_button->frame = frame;
1464 frame_button->type = type;
1465 frame_button->align = align;
1466 frame_button->decoration = style;
1468 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1470 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1471 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1472 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1473 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1474 return frame_button->widget;
1478 frame_button_destroy(struct frame_button *frame_button)
1480 widget_destroy(frame_button->widget);
1481 wl_list_remove(&frame_button->link);
1482 cairo_surface_destroy(frame_button->icon);
1489 frame_redraw_handler(struct widget *widget, void *data)
1492 struct window *window = widget->window;
1493 struct theme *t = window->display->theme;
1496 if (window->type == TYPE_FULLSCREEN)
1499 cr = cairo_create(window->cairo_surface);
1501 if (window->keyboard_device)
1502 flags |= THEME_FRAME_ACTIVE;
1503 theme_render_frame(t, cr, widget->allocation.width,
1504 widget->allocation.height, window->title, flags);
1510 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1512 struct theme *t = frame->widget->window->display->theme;
1515 location = theme_get_location(t, input->sx, input->sy,
1516 frame->widget->allocation.width,
1517 frame->widget->allocation.height);
1520 case THEME_LOCATION_RESIZING_TOP:
1522 case THEME_LOCATION_RESIZING_BOTTOM:
1523 return CURSOR_BOTTOM;
1524 case THEME_LOCATION_RESIZING_LEFT:
1526 case THEME_LOCATION_RESIZING_RIGHT:
1527 return CURSOR_RIGHT;
1528 case THEME_LOCATION_RESIZING_TOP_LEFT:
1529 return CURSOR_TOP_LEFT;
1530 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1531 return CURSOR_TOP_RIGHT;
1532 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1533 return CURSOR_BOTTOM_LEFT;
1534 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1535 return CURSOR_BOTTOM_RIGHT;
1536 case THEME_LOCATION_EXTERIOR:
1537 case THEME_LOCATION_TITLEBAR:
1539 return CURSOR_LEFT_PTR;
1544 frame_menu_func(struct window *window, int index, void *data)
1548 if (window->close_handler)
1549 window->close_handler(window->parent,
1552 display_exit(window->display);
1554 case 1: /* fullscreen */
1555 /* we don't have a way to get out of fullscreen for now */
1556 window_set_fullscreen(window, 1);
1558 case 2: /* rotate */
1565 window_show_frame_menu(struct window *window,
1566 struct input *input, uint32_t time)
1570 static const char *entries[] = {
1571 "Close", "Fullscreen", "Rotate", "Scale"
1574 input_get_position(input, &x, &y);
1575 window_show_menu(window->display, input, time, window,
1576 x - 10, y - 10, frame_menu_func, entries, 4);
1580 frame_enter_handler(struct widget *widget,
1581 struct input *input, float x, float y, void *data)
1583 return frame_get_pointer_image_for_location(data, input);
1587 frame_motion_handler(struct widget *widget,
1588 struct input *input, uint32_t time,
1589 float x, float y, void *data)
1591 return frame_get_pointer_image_for_location(data, input);
1595 frame_button_handler(struct widget *widget,
1596 struct input *input, uint32_t time,
1597 uint32_t button, enum wl_pointer_button_state state,
1601 struct frame *frame = data;
1602 struct window *window = widget->window;
1603 struct display *display = window->display;
1606 location = theme_get_location(display->theme, input->sx, input->sy,
1607 frame->widget->allocation.width,
1608 frame->widget->allocation.height);
1610 if (window->display->shell && button == BTN_LEFT &&
1611 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1613 case THEME_LOCATION_TITLEBAR:
1614 if (!window->shell_surface)
1616 input_set_pointer_image(input, time, CURSOR_DRAGGING);
1617 input_ungrab(input);
1618 wl_shell_surface_move(window->shell_surface,
1619 input_get_seat(input),
1622 case THEME_LOCATION_RESIZING_TOP:
1623 case THEME_LOCATION_RESIZING_BOTTOM:
1624 case THEME_LOCATION_RESIZING_LEFT:
1625 case THEME_LOCATION_RESIZING_RIGHT:
1626 case THEME_LOCATION_RESIZING_TOP_LEFT:
1627 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1628 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1629 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1630 if (!window->shell_surface)
1632 input_ungrab(input);
1634 if (!display->dpy) {
1635 /* If we're using shm, allocate a big
1636 pool to create buffers out of while
1637 we resize. We should probably base
1638 this number on the size of the output. */
1640 shm_pool_create(display, 6 * 1024 * 1024);
1643 wl_shell_surface_resize(window->shell_surface,
1644 input_get_seat(input),
1645 display->serial, location);
1648 } else if (button == BTN_RIGHT &&
1649 state == WL_POINTER_BUTTON_STATE_PRESSED) {
1650 window_show_frame_menu(window, input, time);
1655 frame_create(struct window *window, void *data)
1657 struct frame *frame;
1659 frame = malloc(sizeof *frame);
1660 memset(frame, 0, sizeof *frame);
1662 frame->widget = window_add_widget(window, frame);
1663 frame->child = widget_add_widget(frame->widget, data);
1665 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1666 widget_set_resize_handler(frame->widget, frame_resize_handler);
1667 widget_set_enter_handler(frame->widget, frame_enter_handler);
1668 widget_set_motion_handler(frame->widget, frame_motion_handler);
1669 widget_set_button_handler(frame->widget, frame_button_handler);
1671 /* Create empty list for frame buttons */
1672 wl_list_init(&frame->buttons_list);
1674 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1675 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1677 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1678 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1680 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1681 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1683 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1684 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1686 window->frame = frame;
1688 return frame->child;
1692 frame_destroy(struct frame *frame)
1694 struct frame_button *button, *tmp;
1696 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1697 frame_button_destroy(button);
1699 /* frame->child must be destroyed by the application */
1700 widget_destroy(frame->widget);
1705 input_set_focus_widget(struct input *input, struct widget *focus,
1708 struct widget *old, *widget;
1709 int pointer = CURSOR_LEFT_PTR;
1711 if (focus == input->focus_widget)
1714 old = input->focus_widget;
1718 widget = input->grab;
1719 if (widget->leave_handler)
1720 widget->leave_handler(old, input, widget->user_data);
1721 input->focus_widget = NULL;
1727 widget = input->grab;
1728 if (widget->enter_handler)
1729 pointer = widget->enter_handler(focus, input, x, y,
1731 input->focus_widget = focus;
1733 input_set_pointer_image(input, input->pointer_enter_serial,
1739 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1740 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1742 struct input *input = data;
1743 struct window *window = input->pointer_focus;
1744 struct widget *widget;
1745 int cursor = CURSOR_LEFT_PTR;
1746 float sx = wl_fixed_to_double(sx_w);
1747 float sy = wl_fixed_to_double(sy_w);
1752 if (!(input->grab && input->grab_button)) {
1753 widget = widget_find_widget(window->widget, sx, sy);
1754 input_set_focus_widget(input, widget, sx, sy);
1758 widget = input->grab;
1760 widget = input->focus_widget;
1761 if (widget && widget->motion_handler)
1762 cursor = widget->motion_handler(input->focus_widget,
1763 input, time, sx, sy,
1766 input_set_pointer_image(input, time, cursor);
1770 input_grab(struct input *input, struct widget *widget, uint32_t button)
1772 input->grab = widget;
1773 input->grab_button = button;
1777 input_ungrab(struct input *input)
1779 struct widget *widget;
1782 if (input->pointer_focus) {
1783 widget = widget_find_widget(input->pointer_focus->widget,
1784 input->sx, input->sy);
1785 input_set_focus_widget(input, widget, input->sx, input->sy);
1790 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1791 uint32_t time, uint32_t button, uint32_t state_w)
1793 struct input *input = data;
1794 struct widget *widget;
1795 enum wl_pointer_button_state state = state_w;
1797 input->display->serial = serial;
1798 if (input->focus_widget && input->grab == NULL &&
1799 state == WL_POINTER_BUTTON_STATE_PRESSED)
1800 input_grab(input, input->focus_widget, button);
1802 widget = input->grab;
1803 if (widget && widget->button_handler)
1804 (*widget->button_handler)(widget,
1807 input->grab->user_data);
1809 if (input->grab && input->grab_button == button &&
1810 state == WL_POINTER_BUTTON_STATE_RELEASED)
1811 input_ungrab(input);
1815 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1816 uint32_t time, uint32_t axis, wl_fixed_t value)
1821 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1822 uint32_t serial, uint32_t time, uint32_t key,
1825 struct input *input = data;
1826 struct window *window = input->keyboard_focus;
1827 uint32_t code, num_syms;
1828 enum wl_keyboard_key_state state = state_w;
1829 const xkb_keysym_t *syms;
1831 xkb_mod_mask_t mask;
1833 input->display->serial = serial;
1835 if (!window || window->keyboard_device != input || !input->xkb.state)
1839 window->send_cursor_position = 1;
1841 num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
1843 mask = xkb_state_serialize_mods(input->xkb.state,
1844 XKB_STATE_DEPRESSED |
1846 input->modifiers = 0;
1847 if (mask & input->xkb.control_mask)
1848 input->modifiers |= MOD_CONTROL_MASK;
1849 if (mask & input->xkb.alt_mask)
1850 input->modifiers |= MOD_ALT_MASK;
1851 if (mask & input->xkb.shift_mask)
1852 input->modifiers |= MOD_SHIFT_MASK;
1854 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
1855 input->modifiers == MOD_ALT_MASK) {
1856 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1857 window_set_maximized(window,
1858 window->type != TYPE_MAXIMIZED);
1859 } else if (window->key_handler) {
1863 sym = XKB_KEY_NoSymbol;
1865 (*window->key_handler)(window, input, time, key,
1866 sym, state, window->user_data);
1871 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1872 uint32_t serial, uint32_t mods_depressed,
1873 uint32_t mods_latched, uint32_t mods_locked,
1876 struct input *input = data;
1878 xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
1879 mods_locked, 0, 0, group);
1883 input_remove_pointer_focus(struct input *input)
1885 struct window *window = input->pointer_focus;
1890 input_set_focus_widget(input, NULL, 0, 0);
1892 input->pointer_focus = NULL;
1893 input->current_cursor = CURSOR_UNSET;
1897 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1898 uint32_t serial, struct wl_surface *surface,
1899 wl_fixed_t sx_w, wl_fixed_t sy_w)
1901 struct input *input = data;
1902 struct window *window;
1903 struct widget *widget;
1904 float sx = wl_fixed_to_double(sx_w);
1905 float sy = wl_fixed_to_double(sy_w);
1908 /* enter event for a window we've just destroyed */
1912 input->display->serial = serial;
1913 input->pointer_enter_serial = serial;
1914 input->pointer_focus = wl_surface_get_user_data(surface);
1915 window = input->pointer_focus;
1918 shm_pool_destroy(window->pool);
1919 window->pool = NULL;
1920 /* Schedule a redraw to free the pool */
1921 window_schedule_redraw(window);
1927 widget = widget_find_widget(window->widget, sx, sy);
1928 input_set_focus_widget(input, widget, sx, sy);
1932 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1933 uint32_t serial, struct wl_surface *surface)
1935 struct input *input = data;
1937 input->display->serial = serial;
1938 input_remove_pointer_focus(input);
1942 input_remove_keyboard_focus(struct input *input)
1944 struct window *window = input->keyboard_focus;
1949 window->keyboard_device = NULL;
1950 if (window->keyboard_focus_handler)
1951 (*window->keyboard_focus_handler)(window, NULL,
1954 input->keyboard_focus = NULL;
1958 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1959 uint32_t serial, struct wl_surface *surface,
1960 struct wl_array *keys)
1962 struct input *input = data;
1963 struct window *window;
1965 input->display->serial = serial;
1966 input->keyboard_focus = wl_surface_get_user_data(surface);
1968 window = input->keyboard_focus;
1969 window->keyboard_device = input;
1970 if (window->keyboard_focus_handler)
1971 (*window->keyboard_focus_handler)(window,
1972 window->keyboard_device,
1977 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1978 uint32_t serial, struct wl_surface *surface)
1980 struct input *input = data;
1982 input->display->serial = serial;
1983 input_remove_keyboard_focus(input);
1987 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
1988 uint32_t format, int fd, uint32_t size)
1990 struct input *input = data;
1998 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2003 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2004 if (map_str == MAP_FAILED) {
2009 input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2011 XKB_KEYMAP_FORMAT_TEXT_V1,
2013 munmap(map_str, size);
2016 if (!input->xkb.keymap) {
2017 fprintf(stderr, "failed to compile keymap\n");
2021 input->xkb.state = xkb_state_new(input->xkb.keymap);
2022 if (!input->xkb.state) {
2023 fprintf(stderr, "failed to create XKB state\n");
2024 xkb_map_unref(input->xkb.keymap);
2025 input->xkb.keymap = NULL;
2029 input->xkb.control_mask =
2030 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2031 input->xkb.alt_mask =
2032 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2033 input->xkb.shift_mask =
2034 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2037 static const struct wl_pointer_listener pointer_listener = {
2038 pointer_handle_enter,
2039 pointer_handle_leave,
2040 pointer_handle_motion,
2041 pointer_handle_button,
2042 pointer_handle_axis,
2045 static const struct wl_keyboard_listener keyboard_listener = {
2046 keyboard_handle_keymap,
2047 keyboard_handle_enter,
2048 keyboard_handle_leave,
2049 keyboard_handle_key,
2050 keyboard_handle_modifiers,
2054 seat_handle_capabilities(void *data, struct wl_seat *seat,
2055 enum wl_seat_capability caps)
2057 struct input *input = data;
2059 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2060 input->pointer = wl_seat_get_pointer(seat);
2061 wl_pointer_set_user_data(input->pointer, input);
2062 wl_pointer_add_listener(input->pointer, &pointer_listener,
2064 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2065 wl_pointer_destroy(input->pointer);
2066 input->pointer = NULL;
2069 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2070 input->keyboard = wl_seat_get_keyboard(seat);
2071 wl_keyboard_set_user_data(input->keyboard, input);
2072 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2074 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2075 wl_keyboard_destroy(input->keyboard);
2076 input->keyboard = NULL;
2080 static const struct wl_seat_listener seat_listener = {
2081 seat_handle_capabilities,
2085 input_get_position(struct input *input, int32_t *x, int32_t *y)
2092 input_get_seat(struct input *input)
2098 input_get_modifiers(struct input *input)
2100 return input->modifiers;
2104 input_get_focus_widget(struct input *input)
2106 return input->focus_widget;
2110 struct wl_data_offer *offer;
2111 struct input *input;
2112 struct wl_array types;
2115 struct task io_task;
2123 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2125 struct data_offer *offer = data;
2128 p = wl_array_add(&offer->types, sizeof *p);
2132 static const struct wl_data_offer_listener data_offer_listener = {
2137 data_offer_destroy(struct data_offer *offer)
2142 if (offer->refcount == 0) {
2143 wl_data_offer_destroy(offer->offer);
2144 for (p = offer->types.data; *p; p++)
2146 wl_array_release(&offer->types);
2152 data_device_data_offer(void *data,
2153 struct wl_data_device *data_device, uint32_t id)
2155 struct data_offer *offer;
2157 offer = malloc(sizeof *offer);
2159 wl_array_init(&offer->types);
2160 offer->refcount = 1;
2161 offer->input = data;
2163 /* FIXME: Generate typesafe wrappers for this */
2164 offer->offer = (struct wl_data_offer *)
2165 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2166 id, &wl_data_offer_interface);
2168 wl_data_offer_add_listener(offer->offer,
2169 &data_offer_listener, offer);
2173 data_device_enter(void *data, struct wl_data_device *data_device,
2174 uint32_t serial, struct wl_surface *surface,
2175 wl_fixed_t x_w, wl_fixed_t y_w,
2176 struct wl_data_offer *offer)
2178 struct input *input = data;
2179 struct window *window;
2181 float x = wl_fixed_to_double(x_w);
2182 float y = wl_fixed_to_double(y_w);
2185 input->pointer_enter_serial = serial;
2186 window = wl_surface_get_user_data(surface);
2187 input->pointer_focus = window;
2190 input->drag_offer = wl_data_offer_get_user_data(offer);
2192 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2195 types_data = input->drag_offer->types.data;
2197 input->drag_offer = NULL;
2201 window = input->pointer_focus;
2202 if (window->data_handler)
2203 window->data_handler(window, input, x, y, types_data,
2208 data_device_leave(void *data, struct wl_data_device *data_device)
2210 struct input *input = data;
2212 if (input->drag_offer) {
2213 data_offer_destroy(input->drag_offer);
2214 input->drag_offer = NULL;
2219 data_device_motion(void *data, struct wl_data_device *data_device,
2220 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2222 struct input *input = data;
2223 struct window *window = input->pointer_focus;
2224 float x = wl_fixed_to_double(x_w);
2225 float y = wl_fixed_to_double(y_w);
2231 if (input->drag_offer)
2232 types_data = input->drag_offer->types.data;
2236 if (window->data_handler)
2237 window->data_handler(window, input, x, y, types_data,
2242 data_device_drop(void *data, struct wl_data_device *data_device)
2244 struct input *input = data;
2245 struct window *window = input->pointer_focus;
2247 if (window->drop_handler)
2248 window->drop_handler(window, input,
2249 input->sx, input->sy, window->user_data);
2253 data_device_selection(void *data,
2254 struct wl_data_device *wl_data_device,
2255 struct wl_data_offer *offer)
2257 struct input *input = data;
2260 if (input->selection_offer)
2261 data_offer_destroy(input->selection_offer);
2264 input->selection_offer = wl_data_offer_get_user_data(offer);
2265 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2268 input->selection_offer = NULL;
2272 static const struct wl_data_device_listener data_device_listener = {
2273 data_device_data_offer,
2278 data_device_selection
2282 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2284 struct wl_buffer *buffer;
2285 struct wl_cursor *cursor;
2286 struct wl_cursor_image *image;
2288 if (pointer == input->current_cursor)
2291 cursor = input->display->cursors[pointer];
2295 image = cursor->images[0];
2296 buffer = wl_cursor_image_get_buffer(image);
2300 input->current_cursor = pointer;
2301 wl_pointer_attach(input->pointer, time, buffer,
2302 image->hotspot_x, image->hotspot_y);
2305 struct wl_data_device *
2306 input_get_data_device(struct input *input)
2308 return input->data_device;
2312 input_set_selection(struct input *input,
2313 struct wl_data_source *source, uint32_t time)
2315 wl_data_device_set_selection(input->data_device, source, time);
2319 input_accept(struct input *input, const char *type)
2321 wl_data_offer_accept(input->drag_offer->offer,
2322 input->pointer_enter_serial, type);
2326 offer_io_func(struct task *task, uint32_t events)
2328 struct data_offer *offer =
2329 container_of(task, struct data_offer, io_task);
2333 len = read(offer->fd, buffer, sizeof buffer);
2334 offer->func(buffer, len,
2335 offer->x, offer->y, offer->user_data);
2339 data_offer_destroy(offer);
2344 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2345 data_func_t func, void *user_data)
2349 if (pipe2(p, O_CLOEXEC) == -1)
2352 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2355 offer->io_task.run = offer_io_func;
2359 offer->user_data = user_data;
2361 display_watch_fd(offer->input->display,
2362 offer->fd, EPOLLIN, &offer->io_task);
2366 input_receive_drag_data(struct input *input, const char *mime_type,
2367 data_func_t func, void *data)
2369 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2370 input->drag_offer->x = input->sx;
2371 input->drag_offer->y = input->sy;
2375 input_receive_selection_data(struct input *input, const char *mime_type,
2376 data_func_t func, void *data)
2380 if (input->selection_offer == NULL)
2383 for (p = input->selection_offer->types.data; *p; p++)
2384 if (strcmp(mime_type, *p) == 0)
2390 data_offer_receive_data(input->selection_offer,
2391 mime_type, func, data);
2396 input_receive_selection_data_to_fd(struct input *input,
2397 const char *mime_type, int fd)
2399 if (input->selection_offer)
2400 wl_data_offer_receive(input->selection_offer->offer,
2407 window_move(struct window *window, struct input *input, uint32_t serial)
2409 if (!window->shell_surface)
2412 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2416 idle_resize(struct window *window)
2418 struct widget *widget;
2420 window->resize_needed = 0;
2421 widget = window->widget;
2422 widget_set_allocation(widget,
2423 window->pending_allocation.x,
2424 window->pending_allocation.y,
2425 window->pending_allocation.width,
2426 window->pending_allocation.height);
2428 if (window->input_region) {
2429 wl_region_destroy(window->input_region);
2430 window->input_region = NULL;
2433 if (window->opaque_region) {
2434 wl_region_destroy(window->opaque_region);
2435 window->opaque_region = NULL;
2438 if (widget->resize_handler)
2439 widget->resize_handler(widget,
2440 widget->allocation.width,
2441 widget->allocation.height,
2444 if (window->allocation.width != widget->allocation.width ||
2445 window->allocation.height != widget->allocation.height) {
2446 window->allocation = widget->allocation;
2447 window_schedule_redraw(window);
2452 window_schedule_resize(struct window *window, int width, int height)
2454 window->pending_allocation.x = 0;
2455 window->pending_allocation.y = 0;
2456 window->pending_allocation.width = width;
2457 window->pending_allocation.height = height;
2459 window->resize_needed = 1;
2460 window_schedule_redraw(window);
2464 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2466 window_schedule_resize(widget->window, width, height);
2470 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2473 wl_shell_surface_pong(shell_surface, serial);
2477 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2478 uint32_t edges, int32_t width, int32_t height)
2480 struct window *window = data;
2482 if (width <= 0 || height <= 0)
2485 window->resize_edges = edges;
2486 window_schedule_resize(window, width, height);
2490 menu_destroy(struct menu *menu)
2492 widget_destroy(menu->widget);
2493 window_destroy(menu->window);
2498 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2500 struct window *window = data;
2501 struct menu *menu = window->widget->user_data;
2503 /* FIXME: Need more context in this event, at least the input
2504 * device. Or just use wl_callback. And this really needs to
2505 * be a window vfunc that the menu can set. And we need the
2508 menu->func(window->parent, menu->current, window->parent->user_data);
2509 input_ungrab(menu->input);
2513 static const struct wl_shell_surface_listener shell_surface_listener = {
2520 window_get_allocation(struct window *window,
2521 struct rectangle *allocation)
2523 *allocation = window->allocation;
2527 widget_redraw(struct widget *widget)
2529 struct widget *child;
2531 if (widget->redraw_handler)
2532 widget->redraw_handler(widget, widget->user_data);
2533 wl_list_for_each(child, &widget->child_list, link)
2534 widget_redraw(child);
2538 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2540 struct window *window = data;
2542 wl_callback_destroy(callback);
2543 window->redraw_scheduled = 0;
2544 if (window->redraw_needed)
2545 window_schedule_redraw(window);
2548 static const struct wl_callback_listener listener = {
2553 idle_redraw(struct task *task, uint32_t events)
2555 struct window *window = container_of(task, struct window, redraw_task);
2556 struct wl_callback *callback;
2558 if (window->resize_needed)
2559 idle_resize(window);
2561 window_create_surface(window);
2562 widget_redraw(window->widget);
2563 window_flush(window);
2564 window->redraw_needed = 0;
2565 wl_list_init(&window->redraw_task.link);
2567 callback = wl_surface_frame(window->surface);
2568 wl_callback_add_listener(callback, &listener, window);
2572 window_schedule_redraw(struct window *window)
2574 window->redraw_needed = 1;
2575 if (!window->redraw_scheduled) {
2576 window->redraw_task.run = idle_redraw;
2577 display_defer(window->display, &window->redraw_task);
2578 window->redraw_scheduled = 1;
2583 window_set_custom(struct window *window)
2585 window->type = TYPE_CUSTOM;
2589 window_set_fullscreen(struct window *window, int fullscreen)
2591 if (!window->display->shell)
2594 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2598 window->type = TYPE_FULLSCREEN;
2599 window->saved_allocation = window->allocation;
2600 wl_shell_surface_set_fullscreen(window->shell_surface,
2601 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2604 window->type = TYPE_TOPLEVEL;
2605 wl_shell_surface_set_toplevel(window->shell_surface);
2606 window_schedule_resize(window,
2607 window->saved_allocation.width,
2608 window->saved_allocation.height);
2613 window_set_maximized(struct window *window, int maximized)
2615 if (!window->display->shell)
2618 if ((window->type == TYPE_MAXIMIZED) == maximized)
2621 if (window->type == TYPE_TOPLEVEL) {
2622 window->saved_allocation = window->allocation;
2623 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2624 window->type = TYPE_MAXIMIZED;
2626 wl_shell_surface_set_toplevel(window->shell_surface);
2627 window->type = TYPE_TOPLEVEL;
2628 window_schedule_resize(window,
2629 window->saved_allocation.width,
2630 window->saved_allocation.height);
2635 window_set_user_data(struct window *window, void *data)
2637 window->user_data = data;
2641 window_get_user_data(struct window *window)
2643 return window->user_data;
2647 window_set_key_handler(struct window *window,
2648 window_key_handler_t handler)
2650 window->key_handler = handler;
2654 window_set_keyboard_focus_handler(struct window *window,
2655 window_keyboard_focus_handler_t handler)
2657 window->keyboard_focus_handler = handler;
2661 window_set_data_handler(struct window *window, window_data_handler_t handler)
2663 window->data_handler = handler;
2667 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2669 window->drop_handler = handler;
2673 window_set_close_handler(struct window *window,
2674 window_close_handler_t handler)
2676 window->close_handler = handler;
2680 window_set_title(struct window *window, const char *title)
2682 free(window->title);
2683 window->title = strdup(title);
2684 if (window->shell_surface)
2685 wl_shell_surface_set_title(window->shell_surface, title);
2689 window_get_title(struct window *window)
2691 return window->title;
2695 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2697 struct text_cursor_position *text_cursor_position =
2698 window->display->text_cursor_position;
2700 if (!window->send_cursor_position || !text_cursor_position)
2703 text_cursor_position_notify(text_cursor_position,
2704 window->surface, x, y);
2706 window->send_cursor_position = 0;
2710 window_damage(struct window *window, int32_t x, int32_t y,
2711 int32_t width, int32_t height)
2713 wl_surface_damage(window->surface, x, y, width, height);
2717 surface_enter(void *data,
2718 struct wl_surface *wl_surface, struct wl_output *wl_output)
2720 struct window *window = data;
2721 struct output *output;
2722 struct output *output_found = NULL;
2723 struct window_output *window_output;
2725 wl_list_for_each(output, &window->display->output_list, link) {
2726 if (output->output == wl_output) {
2727 output_found = output;
2735 window_output = malloc (sizeof *window_output);
2736 window_output->output = output_found;
2738 wl_list_insert (&window->window_output_list, &window_output->link);
2742 surface_leave(void *data,
2743 struct wl_surface *wl_surface, struct wl_output *output)
2745 struct window *window = data;
2746 struct window_output *window_output;
2747 struct window_output *window_output_found = NULL;
2749 wl_list_for_each(window_output, &window->window_output_list, link) {
2750 if (window_output->output->output == output) {
2751 window_output_found = window_output;
2756 if (window_output_found) {
2757 wl_list_remove(&window_output_found->link);
2758 free(window_output_found);
2762 static const struct wl_surface_listener surface_listener = {
2767 static struct window *
2768 window_create_internal(struct display *display, struct window *parent)
2770 struct window *window;
2772 window = malloc(sizeof *window);
2776 memset(window, 0, sizeof *window);
2777 window->display = display;
2778 window->parent = parent;
2779 window->surface = wl_compositor_create_surface(display->compositor);
2780 wl_surface_add_listener(window->surface, &surface_listener, window);
2781 if (display->shell) {
2782 window->shell_surface =
2783 wl_shell_get_shell_surface(display->shell,
2786 wl_shell_surface_set_title(window->shell_surface,
2789 window->allocation.x = 0;
2790 window->allocation.y = 0;
2791 window->allocation.width = 0;
2792 window->allocation.height = 0;
2793 window->saved_allocation = window->allocation;
2794 window->transparent = 1;
2795 window->send_cursor_position = 0;
2796 window->type = TYPE_NONE;
2797 window->input_region = NULL;
2798 window->opaque_region = NULL;
2801 #ifdef HAVE_CAIRO_EGL
2802 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2804 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2807 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2809 wl_surface_set_user_data(window->surface, window);
2810 wl_list_insert(display->window_list.prev, &window->link);
2811 wl_list_init(&window->redraw_task.link);
2813 if (window->shell_surface) {
2814 wl_shell_surface_set_user_data(window->shell_surface, window);
2815 wl_shell_surface_add_listener(window->shell_surface,
2816 &shell_surface_listener, window);
2819 wl_list_init (&window->window_output_list);
2825 window_create(struct display *display)
2827 struct window *window;
2829 window = window_create_internal(display, NULL);
2837 window_create_transient(struct display *display, struct window *parent,
2838 int32_t x, int32_t y, uint32_t flags)
2840 struct window *window;
2842 window = window_create_internal(parent->display, parent);
2846 window->type = TYPE_TRANSIENT;
2851 wl_shell_surface_set_transient(window->shell_surface,
2852 window->parent->shell_surface,
2853 window->x, window->y, flags);
2859 menu_set_item(struct menu *menu, int sy)
2863 next = (sy - 8) / 20;
2864 if (menu->current != next) {
2865 menu->current = next;
2866 widget_schedule_redraw(menu->widget);
2871 menu_motion_handler(struct widget *widget,
2872 struct input *input, uint32_t time,
2873 float x, float y, void *data)
2875 struct menu *menu = data;
2877 if (widget == menu->widget)
2878 menu_set_item(data, y);
2880 return CURSOR_LEFT_PTR;
2884 menu_enter_handler(struct widget *widget,
2885 struct input *input, float x, float y, void *data)
2887 struct menu *menu = data;
2889 if (widget == menu->widget)
2890 menu_set_item(data, y);
2892 return CURSOR_LEFT_PTR;
2896 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2898 struct menu *menu = data;
2900 if (widget == menu->widget)
2901 menu_set_item(data, -200);
2905 menu_button_handler(struct widget *widget,
2906 struct input *input, uint32_t time,
2907 uint32_t button, enum wl_pointer_button_state state,
2911 struct menu *menu = data;
2913 if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
2914 time - menu->time > 500) {
2915 /* Either relase after press-drag-release or
2916 * click-motion-click. */
2917 menu->func(menu->window->parent,
2918 menu->current, menu->window->parent->user_data);
2919 input_ungrab(input);
2925 menu_redraw_handler(struct widget *widget, void *data)
2928 const int32_t r = 3, margin = 3;
2929 struct menu *menu = data;
2930 int32_t width, height, i;
2931 struct window *window = widget->window;
2933 cr = cairo_create(window->cairo_surface);
2934 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2935 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2938 width = window->allocation.width;
2939 height = window->allocation.height;
2940 rounded_rect(cr, 0, 0, width, height, r);
2942 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2943 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2946 for (i = 0; i < menu->count; i++) {
2947 if (i == menu->current) {
2948 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2949 cairo_rectangle(cr, margin, i * 20 + margin,
2950 width - 2 * margin, 20);
2952 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2953 cairo_move_to(cr, 10, i * 20 + 16);
2954 cairo_show_text(cr, menu->entries[i]);
2956 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2957 cairo_move_to(cr, 10, i * 20 + 16);
2958 cairo_show_text(cr, menu->entries[i]);
2966 window_show_menu(struct display *display,
2967 struct input *input, uint32_t time, struct window *parent,
2968 int32_t x, int32_t y,
2969 menu_func_t func, const char **entries, int count)
2971 struct window *window;
2973 const int32_t margin = 3;
2975 menu = malloc(sizeof *menu);
2979 window = window_create_internal(parent->display, parent);
2983 menu->window = window;
2984 menu->widget = window_add_widget(menu->window, menu);
2985 menu->entries = entries;
2986 menu->count = count;
2990 menu->input = input;
2991 window->type = TYPE_MENU;
2995 input_ungrab(input);
2996 wl_shell_surface_set_popup(window->shell_surface, input->seat,
2997 display_get_serial(window->display),
2998 window->parent->shell_surface,
2999 window->x, window->y, 0);
3001 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3002 widget_set_enter_handler(menu->widget, menu_enter_handler);
3003 widget_set_leave_handler(menu->widget, menu_leave_handler);
3004 widget_set_motion_handler(menu->widget, menu_motion_handler);
3005 widget_set_button_handler(menu->widget, menu_button_handler);
3007 input_grab(input, menu->widget, 0);
3008 window_schedule_resize(window, 200, count * 20 + margin * 2);
3012 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3014 window->buffer_type = type;
3019 display_handle_geometry(void *data,
3020 struct wl_output *wl_output,
3023 int physical_height,
3028 struct output *output = data;
3030 output->allocation.x = x;
3031 output->allocation.y = y;
3035 display_handle_mode(void *data,
3036 struct wl_output *wl_output,
3042 struct output *output = data;
3043 struct display *display = output->display;
3045 if (flags & WL_OUTPUT_MODE_CURRENT) {
3046 output->allocation.width = width;
3047 output->allocation.height = height;
3048 if (display->output_configure_handler)
3049 (*display->output_configure_handler)(
3050 output, display->user_data);
3054 static const struct wl_output_listener output_listener = {
3055 display_handle_geometry,
3060 display_add_output(struct display *d, uint32_t id)
3062 struct output *output;
3064 output = malloc(sizeof *output);
3068 memset(output, 0, sizeof *output);
3069 output->display = d;
3071 wl_display_bind(d->display, id, &wl_output_interface);
3072 wl_list_insert(d->output_list.prev, &output->link);
3074 wl_output_add_listener(output->output, &output_listener, output);
3078 output_destroy(struct output *output)
3080 if (output->destroy_handler)
3081 (*output->destroy_handler)(output, output->user_data);
3083 wl_output_destroy(output->output);
3084 wl_list_remove(&output->link);
3089 display_set_output_configure_handler(struct display *display,
3090 display_output_handler_t handler)
3092 struct output *output;
3094 display->output_configure_handler = handler;
3098 wl_list_for_each(output, &display->output_list, link)
3099 (*display->output_configure_handler)(output,
3100 display->user_data);
3104 output_set_user_data(struct output *output, void *data)
3106 output->user_data = data;
3110 output_get_user_data(struct output *output)
3112 return output->user_data;
3116 output_set_destroy_handler(struct output *output,
3117 display_output_handler_t handler)
3119 output->destroy_handler = handler;
3120 /* FIXME: implement this, once we have way to remove outputs */
3124 output_get_allocation(struct output *output, struct rectangle *allocation)
3126 *allocation = output->allocation;
3130 output_get_wl_output(struct output *output)
3132 return output->output;
3136 fini_xkb(struct input *input)
3138 xkb_state_unref(input->xkb.state);
3139 xkb_map_unref(input->xkb.keymap);
3143 display_add_input(struct display *d, uint32_t id)
3145 struct input *input;
3147 input = malloc(sizeof *input);
3151 memset(input, 0, sizeof *input);
3153 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3154 input->pointer_focus = NULL;
3155 input->keyboard_focus = NULL;
3156 wl_list_insert(d->input_list.prev, &input->link);
3158 wl_seat_add_listener(input->seat, &seat_listener, input);
3159 wl_seat_set_user_data(input->seat, input);
3161 input->data_device =
3162 wl_data_device_manager_get_data_device(d->data_device_manager,
3164 wl_data_device_add_listener(input->data_device, &data_device_listener,
3169 input_destroy(struct input *input)
3171 input_remove_keyboard_focus(input);
3172 input_remove_pointer_focus(input);
3174 if (input->xkb.state)
3175 xkb_state_unref(input->xkb.state);
3176 if (input->xkb.keymap)
3177 xkb_map_unref(input->xkb.keymap);
3179 if (input->drag_offer)
3180 data_offer_destroy(input->drag_offer);
3182 if (input->selection_offer)
3183 data_offer_destroy(input->selection_offer);
3185 wl_data_device_destroy(input->data_device);
3188 wl_list_remove(&input->link);
3189 wl_seat_destroy(input->seat);
3194 display_handle_global(struct wl_display *display, uint32_t id,
3195 const char *interface, uint32_t version, void *data)
3197 struct display *d = data;
3199 if (strcmp(interface, "wl_compositor") == 0) {
3201 wl_display_bind(display, id, &wl_compositor_interface);
3202 } else if (strcmp(interface, "wl_output") == 0) {
3203 display_add_output(d, id);
3204 } else if (strcmp(interface, "wl_seat") == 0) {
3205 display_add_input(d, id);
3206 } else if (strcmp(interface, "wl_shell") == 0) {
3207 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3208 } else if (strcmp(interface, "wl_shm") == 0) {
3209 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3210 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3211 d->data_device_manager =
3212 wl_display_bind(display, id,
3213 &wl_data_device_manager_interface);
3214 } else if (strcmp(interface, "text_cursor_position") == 0) {
3215 d->text_cursor_position =
3216 wl_display_bind(display, id,
3217 &text_cursor_position_interface);
3221 #ifdef HAVE_CAIRO_EGL
3223 init_egl(struct display *d)
3225 EGLint major, minor;
3228 #ifdef USE_CAIRO_GLESV2
3229 # define GL_BIT EGL_OPENGL_ES2_BIT
3231 # define GL_BIT EGL_OPENGL_BIT
3234 static const EGLint argb_cfg_attribs[] = {
3235 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3241 EGL_RENDERABLE_TYPE, GL_BIT,
3245 #ifdef USE_CAIRO_GLESV2
3246 static const EGLint context_attribs[] = {
3247 EGL_CONTEXT_CLIENT_VERSION, 2,
3250 EGLint api = EGL_OPENGL_ES_API;
3252 EGLint *context_attribs = NULL;
3253 EGLint api = EGL_OPENGL_API;
3256 d->dpy = eglGetDisplay(d->display);
3257 if (!eglInitialize(d->dpy, &major, &minor)) {
3258 fprintf(stderr, "failed to initialize display\n");
3262 if (!eglBindAPI(api)) {
3263 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3267 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3268 &d->argb_config, 1, &n) || n != 1) {
3269 fprintf(stderr, "failed to choose argb config\n");
3273 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3274 EGL_NO_CONTEXT, context_attribs);
3275 if (d->argb_ctx == NULL) {
3276 fprintf(stderr, "failed to create context\n");
3280 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3281 fprintf(stderr, "failed to make context current\n");
3285 #ifdef HAVE_CAIRO_EGL
3286 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3287 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3288 fprintf(stderr, "failed to get cairo egl argb device\n");
3297 fini_egl(struct display *display)
3299 #ifdef HAVE_CAIRO_EGL
3300 cairo_device_destroy(display->argb_device);
3303 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3306 eglTerminate(display->dpy);
3312 event_mask_update(uint32_t mask, void *data)
3314 struct display *d = data;
3322 handle_display_data(struct task *task, uint32_t events)
3324 struct display *display =
3325 container_of(task, struct display, display_task);
3327 wl_display_iterate(display->display, display->mask);
3331 display_create(int argc, char *argv[])
3335 argc = parse_options(xkb_options,
3336 ARRAY_LENGTH(xkb_options), argc, argv);
3338 d = malloc(sizeof *d);
3342 memset(d, 0, sizeof *d);
3344 d->display = wl_display_connect(NULL);
3345 if (d->display == NULL) {
3346 fprintf(stderr, "failed to create display: %m\n");
3350 d->epoll_fd = os_epoll_create_cloexec();
3351 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3352 d->display_task.run = handle_display_data;
3353 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3355 wl_list_init(&d->deferred_list);
3356 wl_list_init(&d->input_list);
3357 wl_list_init(&d->output_list);
3359 d->xkb_context = xkb_context_new(0);
3360 if (d->xkb_context == NULL) {
3361 fprintf(stderr, "Failed to create XKB context\n");
3365 /* Set up listener so we'll catch all events. */
3366 wl_display_add_global_listener(d->display,
3367 display_handle_global, d);
3369 /* Process connection events. */
3370 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3371 #ifdef HAVE_CAIRO_EGL
3372 if (init_egl(d) < 0)
3376 d->image_target_texture_2d =
3377 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3378 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3379 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3383 d->theme = theme_create();
3385 wl_list_init(&d->window_list);
3391 display_destroy_outputs(struct display *display)
3394 struct output *output;
3396 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3397 output_destroy(output);
3401 display_destroy_inputs(struct display *display)
3404 struct input *input;
3406 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3407 input_destroy(input);
3411 display_destroy(struct display *display)
3413 if (!wl_list_empty(&display->window_list))
3414 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3416 if (!wl_list_empty(&display->deferred_list))
3417 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3419 display_destroy_outputs(display);
3420 display_destroy_inputs(display);
3422 xkb_context_unref(display->xkb_context);
3424 theme_destroy(display->theme);
3425 destroy_cursors(display);
3427 #ifdef HAVE_CAIRO_EGL
3432 wl_shell_destroy(display->shell);
3435 wl_shm_destroy(display->shm);
3437 if (display->data_device_manager)
3438 wl_data_device_manager_destroy(display->data_device_manager);
3440 wl_compositor_destroy(display->compositor);
3442 close(display->epoll_fd);
3444 wl_display_flush(display->display);
3445 wl_display_disconnect(display->display);
3450 display_set_user_data(struct display *display, void *data)
3452 display->user_data = data;
3456 display_get_user_data(struct display *display)
3458 return display->user_data;
3462 display_get_display(struct display *display)
3464 return display->display;
3468 display_get_output(struct display *display)
3470 return container_of(display->output_list.next, struct output, link);
3473 struct wl_compositor *
3474 display_get_compositor(struct display *display)
3476 return display->compositor;
3480 display_get_serial(struct display *display)
3482 return display->serial;
3486 display_get_egl_display(struct display *d)
3491 struct wl_data_source *
3492 display_create_data_source(struct display *display)
3494 return wl_data_device_manager_create_data_source(display->data_device_manager);
3498 display_get_argb_egl_config(struct display *d)
3500 return d->argb_config;
3504 display_get_shell(struct display *display)
3506 return display->shell;
3510 display_acquire_window_surface(struct display *display,
3511 struct window *window,
3514 #ifdef HAVE_CAIRO_EGL
3515 struct egl_window_surface_data *data;
3516 cairo_device_t *device;
3518 if (!window->cairo_surface)
3520 device = cairo_surface_get_device(window->cairo_surface);
3525 if (device == display->argb_device)
3526 ctx = display->argb_ctx;
3531 data = cairo_surface_get_user_data(window->cairo_surface,
3534 cairo_device_flush(device);
3535 cairo_device_acquire(device);
3536 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3537 fprintf(stderr, "failed to make surface current\n");
3546 display_release_window_surface(struct display *display,
3547 struct window *window)
3549 #ifdef HAVE_CAIRO_EGL
3550 cairo_device_t *device;
3552 device = cairo_surface_get_device(window->cairo_surface);
3556 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3557 fprintf(stderr, "failed to make context current\n");
3558 cairo_device_release(device);
3563 display_defer(struct display *display, struct task *task)
3565 wl_list_insert(&display->deferred_list, &task->link);
3569 display_watch_fd(struct display *display,
3570 int fd, uint32_t events, struct task *task)
3572 struct epoll_event ep;
3576 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3580 display_run(struct display *display)
3583 struct epoll_event ep[16];
3586 display->running = 1;
3588 wl_display_flush(display->display);
3590 while (!wl_list_empty(&display->deferred_list)) {
3591 task = container_of(display->deferred_list.next,
3593 wl_list_remove(&task->link);
3597 if (!display->running)
3600 wl_display_flush(display->display);
3602 count = epoll_wait(display->epoll_fd,
3603 ep, ARRAY_LENGTH(ep), -1);
3604 for (i = 0; i < count; i++) {
3605 task = ep[i].data.ptr;
3606 task->run(task, ep[i].events);
3612 display_exit(struct display *display)
3614 display->running = 0;