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;
100 struct xkb_rule_names names;
101 struct xkb_keymap *keymap;
102 struct xkb_state *state;
103 struct xkb_context *context;
104 xkb_mod_mask_t control_mask;
105 xkb_mod_mask_t alt_mask;
106 xkb_mod_mask_t shift_mask;
109 struct wl_cursor_theme *cursor_theme;
110 struct wl_cursor **cursors;
112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
116 display_output_handler_t output_configure_handler;
131 struct window_output {
132 struct output *output;
137 struct display *display;
138 struct window *parent;
139 struct wl_list window_output_list;
140 struct wl_surface *surface;
141 struct wl_shell_surface *shell_surface;
142 struct wl_region *input_region;
143 struct wl_region *opaque_region;
145 struct rectangle allocation, saved_allocation, server_allocation;
146 struct rectangle pending_allocation;
149 int redraw_scheduled;
151 struct task redraw_task;
155 int send_cursor_position;
156 struct input *keyboard_device;
157 enum window_buffer_type buffer_type;
159 cairo_surface_t *cairo_surface;
161 struct shm_pool *pool;
163 window_key_handler_t key_handler;
164 window_keyboard_focus_handler_t keyboard_focus_handler;
165 window_data_handler_t data_handler;
166 window_drop_handler_t drop_handler;
167 window_close_handler_t close_handler;
170 struct widget *widget;
177 struct window *window;
178 struct tooltip *tooltip;
179 struct wl_list child_list;
181 struct rectangle allocation;
182 widget_resize_handler_t resize_handler;
183 widget_redraw_handler_t redraw_handler;
184 widget_enter_handler_t enter_handler;
185 widget_leave_handler_t leave_handler;
186 widget_motion_handler_t motion_handler;
187 widget_button_handler_t button_handler;
194 struct display *display;
195 struct wl_seat *seat;
196 struct wl_pointer *pointer;
197 struct wl_keyboard *keyboard;
198 struct window *pointer_focus;
199 struct window *keyboard_focus;
202 uint32_t pointer_enter_serial;
206 struct widget *focus_widget;
208 uint32_t grab_button;
210 struct wl_data_device *data_device;
211 struct data_offer *drag_offer;
212 struct data_offer *selection_offer;
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,
1355 uint32_t button, uint32_t state, void *data)
1357 struct frame_button *frame_button = data;
1358 struct window *window = widget->window;
1360 if (button != BTN_LEFT)
1365 frame_button->state = FRAME_BUTTON_ACTIVE;
1366 widget_schedule_redraw(frame_button->widget);
1368 if (frame_button->type == FRAME_BUTTON_ICON)
1369 window_show_frame_menu(window, input, time);
1372 frame_button->state = FRAME_BUTTON_DEFAULT;
1373 widget_schedule_redraw(frame_button->widget);
1377 switch (frame_button->type) {
1378 case FRAME_BUTTON_CLOSE:
1379 if (window->close_handler)
1380 window->close_handler(window->parent,
1383 display_exit(window->display);
1385 case FRAME_BUTTON_MINIMIZE:
1386 fprintf(stderr,"Minimize stub\n");
1388 case FRAME_BUTTON_MAXIMIZE:
1389 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1392 /* Unknown operation */
1398 frame_button_redraw_handler(struct widget *widget, void *data)
1400 struct frame_button *frame_button = data;
1402 int width, height, x, y;
1403 struct window *window = widget->window;
1405 x = widget->allocation.x;
1406 y = widget->allocation.y;
1407 width = widget->allocation.width;
1408 height = widget->allocation.height;
1417 cr = cairo_create(window->cairo_surface);
1419 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1420 cairo_set_line_width(cr, 1);
1422 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1423 cairo_rectangle (cr, x, y, 25, 16);
1425 cairo_stroke_preserve(cr);
1427 switch (frame_button->state) {
1428 case FRAME_BUTTON_DEFAULT:
1429 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1431 case FRAME_BUTTON_OVER:
1432 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1434 case FRAME_BUTTON_ACTIVE:
1435 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1444 cairo_set_source_surface(cr, frame_button->icon, x, y);
1450 static struct widget *
1451 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1452 enum frame_button_align align, enum frame_button_decoration style)
1454 struct frame_button *frame_button;
1455 const char *icon = data;
1457 frame_button = malloc (sizeof *frame_button);
1458 memset(frame_button, 0, sizeof *frame_button);
1460 frame_button->icon = cairo_image_surface_create_from_png(icon);
1461 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1462 frame_button->frame = frame;
1463 frame_button->type = type;
1464 frame_button->align = align;
1465 frame_button->decoration = style;
1467 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1469 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1470 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1471 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1472 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1473 return frame_button->widget;
1477 frame_button_destroy(struct frame_button *frame_button)
1479 widget_destroy(frame_button->widget);
1480 wl_list_remove(&frame_button->link);
1481 cairo_surface_destroy(frame_button->icon);
1488 frame_redraw_handler(struct widget *widget, void *data)
1491 struct window *window = widget->window;
1492 struct theme *t = window->display->theme;
1495 if (window->type == TYPE_FULLSCREEN)
1498 cr = cairo_create(window->cairo_surface);
1500 if (window->keyboard_device)
1501 flags |= THEME_FRAME_ACTIVE;
1502 theme_render_frame(t, cr, widget->allocation.width,
1503 widget->allocation.height, window->title, flags);
1509 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1511 struct theme *t = frame->widget->window->display->theme;
1514 location = theme_get_location(t, input->sx, input->sy,
1515 frame->widget->allocation.width,
1516 frame->widget->allocation.height);
1519 case THEME_LOCATION_RESIZING_TOP:
1521 case THEME_LOCATION_RESIZING_BOTTOM:
1522 return CURSOR_BOTTOM;
1523 case THEME_LOCATION_RESIZING_LEFT:
1525 case THEME_LOCATION_RESIZING_RIGHT:
1526 return CURSOR_RIGHT;
1527 case THEME_LOCATION_RESIZING_TOP_LEFT:
1528 return CURSOR_TOP_LEFT;
1529 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1530 return CURSOR_TOP_RIGHT;
1531 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1532 return CURSOR_BOTTOM_LEFT;
1533 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1534 return CURSOR_BOTTOM_RIGHT;
1535 case THEME_LOCATION_EXTERIOR:
1536 case THEME_LOCATION_TITLEBAR:
1538 return CURSOR_LEFT_PTR;
1543 frame_menu_func(struct window *window, int index, void *data)
1547 if (window->close_handler)
1548 window->close_handler(window->parent,
1551 display_exit(window->display);
1553 case 1: /* fullscreen */
1554 /* we don't have a way to get out of fullscreen for now */
1555 window_set_fullscreen(window, 1);
1557 case 2: /* rotate */
1564 window_show_frame_menu(struct window *window,
1565 struct input *input, uint32_t time)
1569 static const char *entries[] = {
1570 "Close", "Fullscreen", "Rotate", "Scale"
1573 input_get_position(input, &x, &y);
1574 window_show_menu(window->display, input, time, window,
1575 x - 10, y - 10, frame_menu_func, entries, 4);
1579 frame_enter_handler(struct widget *widget,
1580 struct input *input, float x, float y, void *data)
1582 return frame_get_pointer_image_for_location(data, input);
1586 frame_motion_handler(struct widget *widget,
1587 struct input *input, uint32_t time,
1588 float x, float y, void *data)
1590 return frame_get_pointer_image_for_location(data, input);
1594 frame_button_handler(struct widget *widget,
1595 struct input *input, uint32_t time,
1596 uint32_t button, uint32_t state, void *data)
1599 struct frame *frame = data;
1600 struct window *window = widget->window;
1601 struct display *display = window->display;
1604 location = theme_get_location(display->theme, input->sx, input->sy,
1605 frame->widget->allocation.width,
1606 frame->widget->allocation.height);
1608 if (window->display->shell && button == BTN_LEFT && state == 1) {
1610 case THEME_LOCATION_TITLEBAR:
1611 if (!window->shell_surface)
1613 input_set_pointer_image(input, time, CURSOR_DRAGGING);
1614 input_ungrab(input);
1615 wl_shell_surface_move(window->shell_surface,
1616 input_get_seat(input),
1619 case THEME_LOCATION_RESIZING_TOP:
1620 case THEME_LOCATION_RESIZING_BOTTOM:
1621 case THEME_LOCATION_RESIZING_LEFT:
1622 case THEME_LOCATION_RESIZING_RIGHT:
1623 case THEME_LOCATION_RESIZING_TOP_LEFT:
1624 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1625 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1626 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1627 if (!window->shell_surface)
1629 input_ungrab(input);
1631 if (!display->dpy) {
1632 /* If we're using shm, allocate a big
1633 pool to create buffers out of while
1634 we resize. We should probably base
1635 this number on the size of the output. */
1637 shm_pool_create(display, 6 * 1024 * 1024);
1640 wl_shell_surface_resize(window->shell_surface,
1641 input_get_seat(input),
1642 display->serial, location);
1645 } else if (button == BTN_RIGHT && state == 1) {
1646 window_show_frame_menu(window, input, time);
1651 frame_create(struct window *window, void *data)
1653 struct frame *frame;
1655 frame = malloc(sizeof *frame);
1656 memset(frame, 0, sizeof *frame);
1658 frame->widget = window_add_widget(window, frame);
1659 frame->child = widget_add_widget(frame->widget, data);
1661 widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1662 widget_set_resize_handler(frame->widget, frame_resize_handler);
1663 widget_set_enter_handler(frame->widget, frame_enter_handler);
1664 widget_set_motion_handler(frame->widget, frame_motion_handler);
1665 widget_set_button_handler(frame->widget, frame_button_handler);
1667 /* Create empty list for frame buttons */
1668 wl_list_init(&frame->buttons_list);
1670 frame_button_create(frame, DATADIR "/weston/icon_window.png",
1671 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1673 frame_button_create(frame, DATADIR "/weston/sign_close.png",
1674 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1676 frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1677 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1679 frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1680 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1682 window->frame = frame;
1684 return frame->child;
1688 frame_destroy(struct frame *frame)
1690 struct frame_button *button, *tmp;
1692 wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1693 frame_button_destroy(button);
1695 /* frame->child must be destroyed by the application */
1696 widget_destroy(frame->widget);
1701 input_set_focus_widget(struct input *input, struct widget *focus,
1704 struct widget *old, *widget;
1705 int pointer = CURSOR_LEFT_PTR;
1707 if (focus == input->focus_widget)
1710 old = input->focus_widget;
1714 widget = input->grab;
1715 if (widget->leave_handler)
1716 widget->leave_handler(old, input, widget->user_data);
1717 input->focus_widget = NULL;
1723 widget = input->grab;
1724 if (widget->enter_handler)
1725 pointer = widget->enter_handler(focus, input, x, y,
1727 input->focus_widget = focus;
1729 input_set_pointer_image(input, input->pointer_enter_serial,
1735 pointer_handle_motion(void *data, struct wl_pointer *pointer,
1736 uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
1738 struct input *input = data;
1739 struct window *window = input->pointer_focus;
1740 struct widget *widget;
1741 int cursor = CURSOR_LEFT_PTR;
1742 float sx = wl_fixed_to_double(sx_w);
1743 float sy = wl_fixed_to_double(sy_w);
1748 if (!(input->grab && input->grab_button)) {
1749 widget = widget_find_widget(window->widget, sx, sy);
1750 input_set_focus_widget(input, widget, sx, sy);
1754 widget = input->grab;
1756 widget = input->focus_widget;
1757 if (widget && widget->motion_handler)
1758 cursor = widget->motion_handler(input->focus_widget,
1759 input, time, sx, sy,
1762 input_set_pointer_image(input, time, cursor);
1766 input_grab(struct input *input, struct widget *widget, uint32_t button)
1768 input->grab = widget;
1769 input->grab_button = button;
1773 input_ungrab(struct input *input)
1775 struct widget *widget;
1778 if (input->pointer_focus) {
1779 widget = widget_find_widget(input->pointer_focus->widget,
1780 input->sx, input->sy);
1781 input_set_focus_widget(input, widget, input->sx, input->sy);
1786 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
1787 uint32_t time, uint32_t button, uint32_t state)
1789 struct input *input = data;
1790 struct widget *widget;
1792 input->display->serial = serial;
1793 if (input->focus_widget && input->grab == NULL && state)
1794 input_grab(input, input->focus_widget, button);
1796 widget = input->grab;
1797 if (widget && widget->button_handler)
1798 (*widget->button_handler)(widget,
1801 input->grab->user_data);
1803 if (input->grab && input->grab_button == button && !state)
1804 input_ungrab(input);
1808 pointer_handle_axis(void *data, struct wl_pointer *pointer,
1809 uint32_t time, uint32_t axis, int32_t value)
1814 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
1815 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1817 struct input *input = data;
1818 struct window *window = input->keyboard_focus;
1819 struct display *d = input->display;
1820 uint32_t code, num_syms;
1821 const xkb_keysym_t *syms;
1823 xkb_mod_mask_t mask;
1825 input->display->serial = serial;
1827 if (!window || window->keyboard_device != input)
1831 window->send_cursor_position = 1;
1833 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1835 mask = xkb_state_serialize_mods(d->xkb.state,
1836 XKB_STATE_DEPRESSED |
1838 input->modifiers = 0;
1839 if (mask & input->display->xkb.control_mask)
1840 input->modifiers |= MOD_CONTROL_MASK;
1841 if (mask & input->display->xkb.alt_mask)
1842 input->modifiers |= MOD_ALT_MASK;
1843 if (mask & input->display->xkb.shift_mask)
1844 input->modifiers |= MOD_SHIFT_MASK;
1846 if (num_syms == 1 && syms[0] == XKB_KEY_F5 &&
1847 input->modifiers == MOD_ALT_MASK) {
1849 window_set_maximized(window,
1850 window->type != TYPE_MAXIMIZED);
1851 } else if (window->key_handler) {
1855 sym = XKB_KEY_NoSymbol;
1857 (*window->key_handler)(window, input, time, key,
1858 sym, state, window->user_data);
1863 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
1864 uint32_t serial, uint32_t mods_depressed,
1865 uint32_t mods_latched, uint32_t mods_locked,
1868 struct input *input = data;
1870 xkb_state_update_mask(input->display->xkb.state,
1880 input_remove_pointer_focus(struct input *input)
1882 struct window *window = input->pointer_focus;
1887 input_set_focus_widget(input, NULL, 0, 0);
1889 input->pointer_focus = NULL;
1890 input->current_cursor = CURSOR_UNSET;
1894 pointer_handle_enter(void *data, struct wl_pointer *pointer,
1895 uint32_t serial, struct wl_surface *surface,
1896 wl_fixed_t sx_w, wl_fixed_t sy_w)
1898 struct input *input = data;
1899 struct window *window;
1900 struct widget *widget;
1901 float sx = wl_fixed_to_double(sx_w);
1902 float sy = wl_fixed_to_double(sy_w);
1905 /* enter event for a window we've just destroyed */
1909 input->display->serial = serial;
1910 input->pointer_enter_serial = serial;
1911 input->pointer_focus = wl_surface_get_user_data(surface);
1912 window = input->pointer_focus;
1915 shm_pool_destroy(window->pool);
1916 window->pool = NULL;
1917 /* Schedule a redraw to free the pool */
1918 window_schedule_redraw(window);
1924 widget = widget_find_widget(window->widget, sx, sy);
1925 input_set_focus_widget(input, widget, sx, sy);
1929 pointer_handle_leave(void *data, struct wl_pointer *pointer,
1930 uint32_t serial, struct wl_surface *surface)
1932 struct input *input = data;
1934 input->display->serial = serial;
1935 input_remove_pointer_focus(input);
1939 input_remove_keyboard_focus(struct input *input)
1941 struct window *window = input->keyboard_focus;
1946 window->keyboard_device = NULL;
1947 if (window->keyboard_focus_handler)
1948 (*window->keyboard_focus_handler)(window, NULL,
1951 input->keyboard_focus = NULL;
1955 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
1956 uint32_t serial, struct wl_surface *surface,
1957 struct wl_array *keys)
1959 struct input *input = data;
1960 struct window *window;
1962 input->display->serial = serial;
1963 input->keyboard_focus = wl_surface_get_user_data(surface);
1965 window = input->keyboard_focus;
1966 window->keyboard_device = input;
1967 if (window->keyboard_focus_handler)
1968 (*window->keyboard_focus_handler)(window,
1969 window->keyboard_device,
1974 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
1975 uint32_t serial, struct wl_surface *surface)
1977 struct input *input = data;
1979 input->display->serial = serial;
1980 input_remove_keyboard_focus(input);
1983 static const struct wl_pointer_listener pointer_listener = {
1984 pointer_handle_enter,
1985 pointer_handle_leave,
1986 pointer_handle_motion,
1987 pointer_handle_button,
1988 pointer_handle_axis,
1991 static const struct wl_keyboard_listener keyboard_listener = {
1992 keyboard_handle_enter,
1993 keyboard_handle_leave,
1994 keyboard_handle_key,
1995 keyboard_handle_modifiers,
1999 seat_handle_capabilities(void *data, struct wl_seat *seat,
2000 enum wl_seat_capability caps)
2002 struct input *input = data;
2004 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2005 input->pointer = wl_seat_get_pointer(seat);
2006 wl_pointer_set_user_data(input->pointer, input);
2007 wl_pointer_add_listener(input->pointer, &pointer_listener,
2009 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2010 wl_pointer_destroy(input->pointer);
2011 input->pointer = NULL;
2014 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2015 input->keyboard = wl_seat_get_keyboard(seat);
2016 wl_keyboard_set_user_data(input->keyboard, input);
2017 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2019 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2020 wl_keyboard_destroy(input->keyboard);
2021 input->keyboard = NULL;
2025 static const struct wl_seat_listener seat_listener = {
2026 seat_handle_capabilities,
2030 input_get_position(struct input *input, int32_t *x, int32_t *y)
2037 input_get_seat(struct input *input)
2043 input_get_modifiers(struct input *input)
2045 return input->modifiers;
2049 input_get_focus_widget(struct input *input)
2051 return input->focus_widget;
2055 struct wl_data_offer *offer;
2056 struct input *input;
2057 struct wl_array types;
2060 struct task io_task;
2068 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2070 struct data_offer *offer = data;
2073 p = wl_array_add(&offer->types, sizeof *p);
2077 static const struct wl_data_offer_listener data_offer_listener = {
2082 data_offer_destroy(struct data_offer *offer)
2087 if (offer->refcount == 0) {
2088 wl_data_offer_destroy(offer->offer);
2089 for (p = offer->types.data; *p; p++)
2091 wl_array_release(&offer->types);
2097 data_device_data_offer(void *data,
2098 struct wl_data_device *data_device, uint32_t id)
2100 struct data_offer *offer;
2102 offer = malloc(sizeof *offer);
2104 wl_array_init(&offer->types);
2105 offer->refcount = 1;
2106 offer->input = data;
2108 /* FIXME: Generate typesafe wrappers for this */
2109 offer->offer = (struct wl_data_offer *)
2110 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2111 id, &wl_data_offer_interface);
2113 wl_data_offer_add_listener(offer->offer,
2114 &data_offer_listener, offer);
2118 data_device_enter(void *data, struct wl_data_device *data_device,
2119 uint32_t serial, struct wl_surface *surface,
2120 wl_fixed_t x_w, wl_fixed_t y_w,
2121 struct wl_data_offer *offer)
2123 struct input *input = data;
2124 struct window *window;
2126 float x = wl_fixed_to_double(x_w);
2127 float y = wl_fixed_to_double(y_w);
2130 input->pointer_enter_serial = serial;
2131 window = wl_surface_get_user_data(surface);
2132 input->pointer_focus = window;
2135 input->drag_offer = wl_data_offer_get_user_data(offer);
2137 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2140 types_data = input->drag_offer->types.data;
2142 input->drag_offer = NULL;
2146 window = input->pointer_focus;
2147 if (window->data_handler)
2148 window->data_handler(window, input, x, y, types_data,
2153 data_device_leave(void *data, struct wl_data_device *data_device)
2155 struct input *input = data;
2157 if (input->drag_offer) {
2158 data_offer_destroy(input->drag_offer);
2159 input->drag_offer = NULL;
2164 data_device_motion(void *data, struct wl_data_device *data_device,
2165 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2167 struct input *input = data;
2168 struct window *window = input->pointer_focus;
2169 float x = wl_fixed_to_double(x_w);
2170 float y = wl_fixed_to_double(y_w);
2176 if (input->drag_offer)
2177 types_data = input->drag_offer->types.data;
2181 if (window->data_handler)
2182 window->data_handler(window, input, x, y, types_data,
2187 data_device_drop(void *data, struct wl_data_device *data_device)
2189 struct input *input = data;
2190 struct window *window = input->pointer_focus;
2192 if (window->drop_handler)
2193 window->drop_handler(window, input,
2194 input->sx, input->sy, window->user_data);
2198 data_device_selection(void *data,
2199 struct wl_data_device *wl_data_device,
2200 struct wl_data_offer *offer)
2202 struct input *input = data;
2205 if (input->selection_offer)
2206 data_offer_destroy(input->selection_offer);
2209 input->selection_offer = wl_data_offer_get_user_data(offer);
2210 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2215 static const struct wl_data_device_listener data_device_listener = {
2216 data_device_data_offer,
2221 data_device_selection
2225 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2227 struct wl_buffer *buffer;
2228 struct wl_cursor *cursor;
2229 struct wl_cursor_image *image;
2231 if (pointer == input->current_cursor)
2234 cursor = input->display->cursors[pointer];
2238 image = cursor->images[0];
2239 buffer = wl_cursor_image_get_buffer(image);
2243 input->current_cursor = pointer;
2244 wl_pointer_attach(input->pointer, time, buffer,
2245 image->hotspot_x, image->hotspot_y);
2248 struct wl_data_device *
2249 input_get_data_device(struct input *input)
2251 return input->data_device;
2255 input_set_selection(struct input *input,
2256 struct wl_data_source *source, uint32_t time)
2258 wl_data_device_set_selection(input->data_device, source, time);
2262 input_accept(struct input *input, const char *type)
2264 wl_data_offer_accept(input->drag_offer->offer,
2265 input->pointer_enter_serial, type);
2269 offer_io_func(struct task *task, uint32_t events)
2271 struct data_offer *offer =
2272 container_of(task, struct data_offer, io_task);
2276 len = read(offer->fd, buffer, sizeof buffer);
2277 offer->func(buffer, len,
2278 offer->x, offer->y, offer->user_data);
2282 data_offer_destroy(offer);
2287 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2288 data_func_t func, void *user_data)
2292 if (pipe2(p, O_CLOEXEC) == -1)
2295 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2298 offer->io_task.run = offer_io_func;
2302 offer->user_data = user_data;
2304 display_watch_fd(offer->input->display,
2305 offer->fd, EPOLLIN, &offer->io_task);
2309 input_receive_drag_data(struct input *input, const char *mime_type,
2310 data_func_t func, void *data)
2312 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2313 input->drag_offer->x = input->sx;
2314 input->drag_offer->y = input->sy;
2318 input_receive_selection_data(struct input *input, const char *mime_type,
2319 data_func_t func, void *data)
2323 if (input->selection_offer == NULL)
2326 for (p = input->selection_offer->types.data; *p; p++)
2327 if (strcmp(mime_type, *p) == 0)
2333 data_offer_receive_data(input->selection_offer,
2334 mime_type, func, data);
2339 input_receive_selection_data_to_fd(struct input *input,
2340 const char *mime_type, int fd)
2342 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2348 window_move(struct window *window, struct input *input, uint32_t serial)
2350 if (!window->shell_surface)
2353 wl_shell_surface_move(window->shell_surface, input->seat, serial);
2357 idle_resize(struct window *window)
2359 struct widget *widget;
2361 window->resize_needed = 0;
2362 widget = window->widget;
2363 widget_set_allocation(widget,
2364 window->pending_allocation.x,
2365 window->pending_allocation.y,
2366 window->pending_allocation.width,
2367 window->pending_allocation.height);
2369 if (window->input_region) {
2370 wl_region_destroy(window->input_region);
2371 window->input_region = NULL;
2374 if (window->opaque_region) {
2375 wl_region_destroy(window->opaque_region);
2376 window->opaque_region = NULL;
2379 if (widget->resize_handler)
2380 widget->resize_handler(widget,
2381 widget->allocation.width,
2382 widget->allocation.height,
2385 if (window->allocation.width != widget->allocation.width ||
2386 window->allocation.height != widget->allocation.height) {
2387 window->allocation = widget->allocation;
2388 window_schedule_redraw(window);
2393 window_schedule_resize(struct window *window, int width, int height)
2395 window->pending_allocation.x = 0;
2396 window->pending_allocation.y = 0;
2397 window->pending_allocation.width = width;
2398 window->pending_allocation.height = height;
2400 window->resize_needed = 1;
2401 window_schedule_redraw(window);
2405 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2407 window_schedule_resize(widget->window, width, height);
2411 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2414 wl_shell_surface_pong(shell_surface, serial);
2418 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2419 uint32_t edges, int32_t width, int32_t height)
2421 struct window *window = data;
2423 if (width <= 0 || height <= 0)
2426 window->resize_edges = edges;
2427 window_schedule_resize(window, width, height);
2431 menu_destroy(struct menu *menu)
2433 widget_destroy(menu->widget);
2434 window_destroy(menu->window);
2439 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2441 struct window *window = data;
2442 struct menu *menu = window->widget->user_data;
2444 /* FIXME: Need more context in this event, at least the input
2445 * device. Or just use wl_callback. And this really needs to
2446 * be a window vfunc that the menu can set. And we need the
2449 menu->func(window->parent, menu->current, window->parent->user_data);
2450 input_ungrab(menu->input);
2454 static const struct wl_shell_surface_listener shell_surface_listener = {
2461 window_get_allocation(struct window *window,
2462 struct rectangle *allocation)
2464 *allocation = window->allocation;
2468 widget_redraw(struct widget *widget)
2470 struct widget *child;
2472 if (widget->redraw_handler)
2473 widget->redraw_handler(widget, widget->user_data);
2474 wl_list_for_each(child, &widget->child_list, link)
2475 widget_redraw(child);
2479 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2481 struct window *window = data;
2483 wl_callback_destroy(callback);
2484 window->redraw_scheduled = 0;
2485 if (window->redraw_needed)
2486 window_schedule_redraw(window);
2489 static const struct wl_callback_listener listener = {
2494 idle_redraw(struct task *task, uint32_t events)
2496 struct window *window = container_of(task, struct window, redraw_task);
2497 struct wl_callback *callback;
2499 if (window->resize_needed)
2500 idle_resize(window);
2502 window_create_surface(window);
2503 widget_redraw(window->widget);
2504 window_flush(window);
2505 window->redraw_needed = 0;
2506 wl_list_init(&window->redraw_task.link);
2508 callback = wl_surface_frame(window->surface);
2509 wl_callback_add_listener(callback, &listener, window);
2513 window_schedule_redraw(struct window *window)
2515 window->redraw_needed = 1;
2516 if (!window->redraw_scheduled) {
2517 window->redraw_task.run = idle_redraw;
2518 display_defer(window->display, &window->redraw_task);
2519 window->redraw_scheduled = 1;
2524 window_set_custom(struct window *window)
2526 window->type = TYPE_CUSTOM;
2530 window_set_fullscreen(struct window *window, int fullscreen)
2532 if (!window->display->shell)
2535 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2539 window->type = TYPE_FULLSCREEN;
2540 window->saved_allocation = window->allocation;
2541 wl_shell_surface_set_fullscreen(window->shell_surface,
2542 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2545 window->type = TYPE_TOPLEVEL;
2546 wl_shell_surface_set_toplevel(window->shell_surface);
2547 window_schedule_resize(window,
2548 window->saved_allocation.width,
2549 window->saved_allocation.height);
2554 window_set_maximized(struct window *window, int maximized)
2556 if (!window->display->shell)
2559 if ((window->type == TYPE_MAXIMIZED) == maximized)
2562 if (window->type == TYPE_TOPLEVEL) {
2563 window->saved_allocation = window->allocation;
2564 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2565 window->type = TYPE_MAXIMIZED;
2567 wl_shell_surface_set_toplevel(window->shell_surface);
2568 window->type = TYPE_TOPLEVEL;
2569 window_schedule_resize(window,
2570 window->saved_allocation.width,
2571 window->saved_allocation.height);
2576 window_set_user_data(struct window *window, void *data)
2578 window->user_data = data;
2582 window_get_user_data(struct window *window)
2584 return window->user_data;
2588 window_set_key_handler(struct window *window,
2589 window_key_handler_t handler)
2591 window->key_handler = handler;
2595 window_set_keyboard_focus_handler(struct window *window,
2596 window_keyboard_focus_handler_t handler)
2598 window->keyboard_focus_handler = handler;
2602 window_set_data_handler(struct window *window, window_data_handler_t handler)
2604 window->data_handler = handler;
2608 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2610 window->drop_handler = handler;
2614 window_set_close_handler(struct window *window,
2615 window_close_handler_t handler)
2617 window->close_handler = handler;
2621 window_set_title(struct window *window, const char *title)
2623 free(window->title);
2624 window->title = strdup(title);
2625 if (window->shell_surface)
2626 wl_shell_surface_set_title(window->shell_surface, title);
2630 window_get_title(struct window *window)
2632 return window->title;
2636 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
2638 struct text_cursor_position *text_cursor_position =
2639 window->display->text_cursor_position;
2641 if (!window->send_cursor_position || !text_cursor_position)
2644 text_cursor_position_notify(text_cursor_position,
2645 window->surface, x, y);
2647 window->send_cursor_position = 0;
2651 window_damage(struct window *window, int32_t x, int32_t y,
2652 int32_t width, int32_t height)
2654 wl_surface_damage(window->surface, x, y, width, height);
2658 surface_enter(void *data,
2659 struct wl_surface *wl_surface, struct wl_output *wl_output)
2661 struct window *window = data;
2662 struct output *output;
2663 struct output *output_found = NULL;
2664 struct window_output *window_output;
2666 wl_list_for_each(output, &window->display->output_list, link) {
2667 if (output->output == wl_output) {
2668 output_found = output;
2676 window_output = malloc (sizeof *window_output);
2677 window_output->output = output_found;
2679 wl_list_insert (&window->window_output_list, &window_output->link);
2683 surface_leave(void *data,
2684 struct wl_surface *wl_surface, struct wl_output *output)
2686 struct window *window = data;
2687 struct window_output *window_output;
2688 struct window_output *window_output_found = NULL;
2690 wl_list_for_each(window_output, &window->window_output_list, link) {
2691 if (window_output->output->output == output) {
2692 window_output_found = window_output;
2697 if (window_output_found) {
2698 wl_list_remove(&window_output_found->link);
2699 free(window_output_found);
2703 static const struct wl_surface_listener surface_listener = {
2708 static struct window *
2709 window_create_internal(struct display *display, struct window *parent)
2711 struct window *window;
2713 window = malloc(sizeof *window);
2717 memset(window, 0, sizeof *window);
2718 window->display = display;
2719 window->parent = parent;
2720 window->surface = wl_compositor_create_surface(display->compositor);
2721 wl_surface_add_listener(window->surface, &surface_listener, window);
2722 if (display->shell) {
2723 window->shell_surface =
2724 wl_shell_get_shell_surface(display->shell,
2727 wl_shell_surface_set_title(window->shell_surface,
2730 window->allocation.x = 0;
2731 window->allocation.y = 0;
2732 window->allocation.width = 0;
2733 window->allocation.height = 0;
2734 window->saved_allocation = window->allocation;
2735 window->transparent = 1;
2736 window->send_cursor_position = 0;
2737 window->type = TYPE_NONE;
2738 window->input_region = NULL;
2739 window->opaque_region = NULL;
2742 #ifdef HAVE_CAIRO_EGL
2743 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2745 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2748 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2750 wl_surface_set_user_data(window->surface, window);
2751 wl_list_insert(display->window_list.prev, &window->link);
2752 wl_list_init(&window->redraw_task.link);
2754 if (window->shell_surface) {
2755 wl_shell_surface_set_user_data(window->shell_surface, window);
2756 wl_shell_surface_add_listener(window->shell_surface,
2757 &shell_surface_listener, window);
2760 wl_list_init (&window->window_output_list);
2766 window_create(struct display *display)
2768 struct window *window;
2770 window = window_create_internal(display, NULL);
2778 window_create_transient(struct display *display, struct window *parent,
2779 int32_t x, int32_t y, uint32_t flags)
2781 struct window *window;
2783 window = window_create_internal(parent->display, parent);
2787 window->type = TYPE_TRANSIENT;
2792 wl_shell_surface_set_transient(window->shell_surface,
2793 window->parent->shell_surface,
2794 window->x, window->y, flags);
2800 menu_set_item(struct menu *menu, int sy)
2804 next = (sy - 8) / 20;
2805 if (menu->current != next) {
2806 menu->current = next;
2807 widget_schedule_redraw(menu->widget);
2812 menu_motion_handler(struct widget *widget,
2813 struct input *input, uint32_t time,
2814 float x, float y, void *data)
2816 struct menu *menu = data;
2818 if (widget == menu->widget)
2819 menu_set_item(data, y);
2821 return CURSOR_LEFT_PTR;
2825 menu_enter_handler(struct widget *widget,
2826 struct input *input, float x, float y, void *data)
2828 struct menu *menu = data;
2830 if (widget == menu->widget)
2831 menu_set_item(data, y);
2833 return CURSOR_LEFT_PTR;
2837 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2839 struct menu *menu = data;
2841 if (widget == menu->widget)
2842 menu_set_item(data, -200);
2846 menu_button_handler(struct widget *widget,
2847 struct input *input, uint32_t time,
2848 uint32_t button, uint32_t state, void *data)
2851 struct menu *menu = data;
2853 if (state == 0 && time - menu->time > 500) {
2854 /* Either relase after press-drag-release or
2855 * click-motion-click. */
2856 menu->func(menu->window->parent,
2857 menu->current, menu->window->parent->user_data);
2858 input_ungrab(input);
2864 menu_redraw_handler(struct widget *widget, void *data)
2867 const int32_t r = 3, margin = 3;
2868 struct menu *menu = data;
2869 int32_t width, height, i;
2870 struct window *window = widget->window;
2872 cr = cairo_create(window->cairo_surface);
2873 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2874 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2877 width = window->allocation.width;
2878 height = window->allocation.height;
2879 rounded_rect(cr, 0, 0, width, height, r);
2881 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2882 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2885 for (i = 0; i < menu->count; i++) {
2886 if (i == menu->current) {
2887 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2888 cairo_rectangle(cr, margin, i * 20 + margin,
2889 width - 2 * margin, 20);
2891 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2892 cairo_move_to(cr, 10, i * 20 + 16);
2893 cairo_show_text(cr, menu->entries[i]);
2895 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2896 cairo_move_to(cr, 10, i * 20 + 16);
2897 cairo_show_text(cr, menu->entries[i]);
2905 window_show_menu(struct display *display,
2906 struct input *input, uint32_t time, struct window *parent,
2907 int32_t x, int32_t y,
2908 menu_func_t func, const char **entries, int count)
2910 struct window *window;
2912 const int32_t margin = 3;
2914 menu = malloc(sizeof *menu);
2918 window = window_create_internal(parent->display, parent);
2922 menu->window = window;
2923 menu->widget = window_add_widget(menu->window, menu);
2924 menu->entries = entries;
2925 menu->count = count;
2929 menu->input = input;
2930 window->type = TYPE_MENU;
2934 input_ungrab(input);
2935 wl_shell_surface_set_popup(window->shell_surface, input->seat,
2936 display_get_serial(window->display),
2937 window->parent->shell_surface,
2938 window->x, window->y, 0);
2940 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
2941 widget_set_enter_handler(menu->widget, menu_enter_handler);
2942 widget_set_leave_handler(menu->widget, menu_leave_handler);
2943 widget_set_motion_handler(menu->widget, menu_motion_handler);
2944 widget_set_button_handler(menu->widget, menu_button_handler);
2946 input_grab(input, menu->widget, 0);
2947 window_schedule_resize(window, 200, count * 20 + margin * 2);
2951 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2953 window->buffer_type = type;
2958 display_handle_geometry(void *data,
2959 struct wl_output *wl_output,
2962 int physical_height,
2967 struct output *output = data;
2969 output->allocation.x = x;
2970 output->allocation.y = y;
2974 display_handle_mode(void *data,
2975 struct wl_output *wl_output,
2981 struct output *output = data;
2982 struct display *display = output->display;
2984 if (flags & WL_OUTPUT_MODE_CURRENT) {
2985 output->allocation.width = width;
2986 output->allocation.height = height;
2987 if (display->output_configure_handler)
2988 (*display->output_configure_handler)(
2989 output, display->user_data);
2993 static const struct wl_output_listener output_listener = {
2994 display_handle_geometry,
2999 display_add_output(struct display *d, uint32_t id)
3001 struct output *output;
3003 output = malloc(sizeof *output);
3007 memset(output, 0, sizeof *output);
3008 output->display = d;
3010 wl_display_bind(d->display, id, &wl_output_interface);
3011 wl_list_insert(d->output_list.prev, &output->link);
3013 wl_output_add_listener(output->output, &output_listener, output);
3017 output_destroy(struct output *output)
3019 if (output->destroy_handler)
3020 (*output->destroy_handler)(output, output->user_data);
3022 wl_output_destroy(output->output);
3023 wl_list_remove(&output->link);
3028 display_set_output_configure_handler(struct display *display,
3029 display_output_handler_t handler)
3031 struct output *output;
3033 display->output_configure_handler = handler;
3037 wl_list_for_each(output, &display->output_list, link)
3038 (*display->output_configure_handler)(output,
3039 display->user_data);
3043 output_set_user_data(struct output *output, void *data)
3045 output->user_data = data;
3049 output_get_user_data(struct output *output)
3051 return output->user_data;
3055 output_set_destroy_handler(struct output *output,
3056 display_output_handler_t handler)
3058 output->destroy_handler = handler;
3059 /* FIXME: implement this, once we have way to remove outputs */
3063 output_get_allocation(struct output *output, struct rectangle *allocation)
3065 *allocation = output->allocation;
3069 output_get_wl_output(struct output *output)
3071 return output->output;
3075 display_add_input(struct display *d, uint32_t id)
3077 struct input *input;
3079 input = malloc(sizeof *input);
3083 memset(input, 0, sizeof *input);
3085 input->seat = wl_display_bind(d->display, id, &wl_seat_interface);
3086 input->pointer_focus = NULL;
3087 input->keyboard_focus = NULL;
3088 wl_list_insert(d->input_list.prev, &input->link);
3090 wl_seat_add_listener(input->seat, &seat_listener, input);
3091 wl_seat_set_user_data(input->seat, input);
3093 input->data_device =
3094 wl_data_device_manager_get_data_device(d->data_device_manager,
3096 wl_data_device_add_listener(input->data_device, &data_device_listener,
3101 input_destroy(struct input *input)
3103 input_remove_keyboard_focus(input);
3104 input_remove_pointer_focus(input);
3106 if (input->drag_offer)
3107 data_offer_destroy(input->drag_offer);
3109 if (input->selection_offer)
3110 data_offer_destroy(input->selection_offer);
3112 wl_data_device_destroy(input->data_device);
3113 wl_list_remove(&input->link);
3114 wl_seat_destroy(input->seat);
3119 display_handle_global(struct wl_display *display, uint32_t id,
3120 const char *interface, uint32_t version, void *data)
3122 struct display *d = data;
3124 if (strcmp(interface, "wl_compositor") == 0) {
3126 wl_display_bind(display, id, &wl_compositor_interface);
3127 } else if (strcmp(interface, "wl_output") == 0) {
3128 display_add_output(d, id);
3129 } else if (strcmp(interface, "wl_seat") == 0) {
3130 display_add_input(d, id);
3131 } else if (strcmp(interface, "wl_shell") == 0) {
3132 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3133 } else if (strcmp(interface, "wl_shm") == 0) {
3134 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3135 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3136 d->data_device_manager =
3137 wl_display_bind(display, id,
3138 &wl_data_device_manager_interface);
3139 } else if (strcmp(interface, "text_cursor_position") == 0) {
3140 d->text_cursor_position =
3141 wl_display_bind(display, id,
3142 &text_cursor_position_interface);
3147 init_xkb(struct display *d)
3149 d->xkb.names.rules = "evdev";
3150 d->xkb.names.model = "pc105";
3151 d->xkb.names.layout = (char *) option_xkb_layout;
3152 d->xkb.names.variant = (char *) option_xkb_variant;
3153 d->xkb.names.options = (char *) option_xkb_options;
3155 d->xkb.context = xkb_context_new(0);
3156 if (!d->xkb.context) {
3157 fprintf(stderr, "Failed to create XKB context\n");
3161 d->xkb.keymap = xkb_map_new_from_names(d->xkb.context, &d->xkb.names, 0);
3162 if (!d->xkb.keymap) {
3163 fprintf(stderr, "Failed to compile keymap\n");
3167 d->xkb.state = xkb_state_new(d->xkb.keymap);
3168 if (!d->xkb.state) {
3169 fprintf(stderr, "Failed to create XKB state\n");
3173 d->xkb.control_mask =
3174 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3176 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3178 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3183 fini_xkb(struct display *display)
3185 xkb_state_unref(display->xkb.state);
3186 xkb_map_unref(display->xkb.keymap);
3187 xkb_context_unref(display->xkb.context);
3190 #ifdef HAVE_CAIRO_EGL
3192 init_egl(struct display *d)
3194 EGLint major, minor;
3197 #ifdef USE_CAIRO_GLESV2
3198 # define GL_BIT EGL_OPENGL_ES2_BIT
3200 # define GL_BIT EGL_OPENGL_BIT
3203 static const EGLint argb_cfg_attribs[] = {
3204 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3210 EGL_RENDERABLE_TYPE, GL_BIT,
3214 #ifdef USE_CAIRO_GLESV2
3215 static const EGLint context_attribs[] = {
3216 EGL_CONTEXT_CLIENT_VERSION, 2,
3219 EGLint api = EGL_OPENGL_ES_API;
3221 EGLint *context_attribs = NULL;
3222 EGLint api = EGL_OPENGL_API;
3225 d->dpy = eglGetDisplay(d->display);
3226 if (!eglInitialize(d->dpy, &major, &minor)) {
3227 fprintf(stderr, "failed to initialize display\n");
3231 if (!eglBindAPI(api)) {
3232 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3236 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3237 &d->argb_config, 1, &n) || n != 1) {
3238 fprintf(stderr, "failed to choose argb config\n");
3242 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3243 EGL_NO_CONTEXT, context_attribs);
3244 if (d->argb_ctx == NULL) {
3245 fprintf(stderr, "failed to create context\n");
3249 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3250 fprintf(stderr, "failed to make context current\n");
3254 #ifdef HAVE_CAIRO_EGL
3255 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3256 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3257 fprintf(stderr, "failed to get cairo egl argb device\n");
3266 fini_egl(struct display *display)
3268 #ifdef HAVE_CAIRO_EGL
3269 cairo_device_destroy(display->argb_device);
3272 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3275 eglTerminate(display->dpy);
3281 event_mask_update(uint32_t mask, void *data)
3283 struct display *d = data;
3291 handle_display_data(struct task *task, uint32_t events)
3293 struct display *display =
3294 container_of(task, struct display, display_task);
3296 wl_display_iterate(display->display, display->mask);
3300 display_create(int argc, char *argv[])
3304 argc = parse_options(xkb_options,
3305 ARRAY_LENGTH(xkb_options), argc, argv);
3307 d = malloc(sizeof *d);
3311 memset(d, 0, sizeof *d);
3313 d->display = wl_display_connect(NULL);
3314 if (d->display == NULL) {
3315 fprintf(stderr, "failed to create display: %m\n");
3319 d->epoll_fd = os_epoll_create_cloexec();
3320 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3321 d->display_task.run = handle_display_data;
3322 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3324 wl_list_init(&d->deferred_list);
3325 wl_list_init(&d->input_list);
3326 wl_list_init(&d->output_list);
3328 /* Set up listener so we'll catch all events. */
3329 wl_display_add_global_listener(d->display,
3330 display_handle_global, d);
3332 /* Process connection events. */
3333 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3334 #ifdef HAVE_CAIRO_EGL
3335 if (init_egl(d) < 0)
3339 d->image_target_texture_2d =
3340 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3341 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3342 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3346 d->theme = theme_create();
3348 wl_list_init(&d->window_list);
3356 display_destroy_outputs(struct display *display)
3359 struct output *output;
3361 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3362 output_destroy(output);
3366 display_destroy_inputs(struct display *display)
3369 struct input *input;
3371 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3372 input_destroy(input);
3376 display_destroy(struct display *display)
3378 if (!wl_list_empty(&display->window_list))
3379 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3381 if (!wl_list_empty(&display->deferred_list))
3382 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3384 display_destroy_outputs(display);
3385 display_destroy_inputs(display);
3389 theme_destroy(display->theme);
3390 destroy_cursors(display);
3392 #ifdef HAVE_CAIRO_EGL
3397 wl_shell_destroy(display->shell);
3400 wl_shm_destroy(display->shm);
3402 if (display->data_device_manager)
3403 wl_data_device_manager_destroy(display->data_device_manager);
3405 wl_compositor_destroy(display->compositor);
3407 close(display->epoll_fd);
3409 wl_display_flush(display->display);
3410 wl_display_disconnect(display->display);
3415 display_set_user_data(struct display *display, void *data)
3417 display->user_data = data;
3421 display_get_user_data(struct display *display)
3423 return display->user_data;
3427 display_get_display(struct display *display)
3429 return display->display;
3433 display_get_output(struct display *display)
3435 return container_of(display->output_list.next, struct output, link);
3438 struct wl_compositor *
3439 display_get_compositor(struct display *display)
3441 return display->compositor;
3445 display_get_serial(struct display *display)
3447 return display->serial;
3451 display_get_egl_display(struct display *d)
3456 struct wl_data_source *
3457 display_create_data_source(struct display *display)
3459 return wl_data_device_manager_create_data_source(display->data_device_manager);
3463 display_get_argb_egl_config(struct display *d)
3465 return d->argb_config;
3469 display_get_shell(struct display *display)
3471 return display->shell;
3475 display_acquire_window_surface(struct display *display,
3476 struct window *window,
3479 #ifdef HAVE_CAIRO_EGL
3480 struct egl_window_surface_data *data;
3481 cairo_device_t *device;
3483 if (!window->cairo_surface)
3485 device = cairo_surface_get_device(window->cairo_surface);
3490 if (device == display->argb_device)
3491 ctx = display->argb_ctx;
3496 data = cairo_surface_get_user_data(window->cairo_surface,
3499 cairo_device_flush(device);
3500 cairo_device_acquire(device);
3501 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3502 fprintf(stderr, "failed to make surface current\n");
3511 display_release_window_surface(struct display *display,
3512 struct window *window)
3514 #ifdef HAVE_CAIRO_EGL
3515 cairo_device_t *device;
3517 device = cairo_surface_get_device(window->cairo_surface);
3521 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3522 fprintf(stderr, "failed to make context current\n");
3523 cairo_device_release(device);
3528 display_defer(struct display *display, struct task *task)
3530 wl_list_insert(&display->deferred_list, &task->link);
3534 display_watch_fd(struct display *display,
3535 int fd, uint32_t events, struct task *task)
3537 struct epoll_event ep;
3541 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3545 display_run(struct display *display)
3548 struct epoll_event ep[16];
3551 display->running = 1;
3553 wl_display_flush(display->display);
3555 while (!wl_list_empty(&display->deferred_list)) {
3556 task = container_of(display->deferred_list.next,
3558 wl_list_remove(&task->link);
3562 if (!display->running)
3565 wl_display_flush(display->display);
3567 count = epoll_wait(display->epoll_fd,
3568 ep, ARRAY_LENGTH(ep), -1);
3569 for (i = 0; i < count; i++) {
3570 task = ep[i].data.ptr;
3571 task->run(task, ep[i].events);
3577 display_exit(struct display *display)
3579 display->running = 0;