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"
39 #include <sys/epoll.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 <X11/keysym.h>
61 #include <X11/Xcursor/Xcursor.h>
63 #include <linux/input.h>
64 #include <wayland-client.h>
65 #include "cairo-util.h"
73 struct wl_display *display;
74 struct wl_compositor *compositor;
75 struct wl_shell *shell;
77 struct wl_data_device_manager *data_device_manager;
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;
96 cairo_surface_t *active_frame, *inactive_frame, *shadow;
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 cursor *cursors;
110 struct shm_pool *cursor_shm_pool;
112 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
113 PFNEGLCREATEIMAGEKHRPROC create_image;
114 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
116 display_output_handler_t output_configure_handler;
132 struct display *display;
133 struct window *parent;
134 struct wl_surface *surface;
135 struct wl_shell_surface *shell_surface;
136 struct wl_region *input_region;
137 struct wl_region *opaque_region;
139 struct rectangle allocation, saved_allocation, server_allocation;
140 struct rectangle pending_allocation;
143 int redraw_scheduled;
145 struct task redraw_task;
149 struct input *keyboard_device;
150 enum window_buffer_type buffer_type;
152 cairo_surface_t *cairo_surface;
154 struct shm_pool *pool;
156 window_key_handler_t key_handler;
157 window_keyboard_focus_handler_t keyboard_focus_handler;
158 window_data_handler_t data_handler;
159 window_drop_handler_t drop_handler;
160 window_close_handler_t close_handler;
163 struct widget *widget;
170 struct window *window;
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;
185 struct display *display;
186 struct wl_input_device *input_device;
187 struct window *pointer_focus;
188 struct window *keyboard_focus;
191 uint32_t pointer_enter_serial;
195 struct widget *focus_widget;
197 uint32_t grab_button;
199 struct wl_data_device *data_device;
200 struct data_offer *drag_offer;
201 struct data_offer *selection_offer;
205 struct display *display;
206 struct wl_output *output;
207 struct rectangle allocation;
210 display_output_handler_t destroy_handler;
214 enum frame_button_action {
215 FRAME_BUTTON_NULL = 0,
216 FRAME_BUTTON_ICON = 1,
217 FRAME_BUTTON_CLOSE = 2,
218 FRAME_BUTTON_MINIMIZE = 3,
219 FRAME_BUTTON_MAXIMIZE = 4,
222 enum frame_button_pointer {
223 FRAME_BUTTON_DEFAULT = 0,
224 FRAME_BUTTON_OVER = 1,
225 FRAME_BUTTON_ACTIVE = 2,
228 enum frame_button_align {
229 FRAME_BUTTON_RIGHT = 0,
230 FRAME_BUTTON_LEFT = 1,
233 enum frame_button_decoration {
234 FRAME_BUTTON_NONE = 0,
235 FRAME_BUTTON_FANCY = 1,
238 struct frame_button {
239 struct widget *widget;
241 cairo_surface_t *icon;
242 enum frame_button_action type;
243 enum frame_button_pointer state;
244 struct wl_list link; /* buttons_list */
245 enum frame_button_align align;
246 enum frame_button_decoration decoration;
250 struct widget *widget;
251 struct widget *child;
255 struct wl_list buttons_list;
259 struct window *window;
260 struct widget *widget;
262 const char **entries;
269 struct cursor_image {
270 cairo_surface_t *surface;
272 int hotspot_x, hotspot_y;
278 struct cursor_image *images;
282 struct wl_shm_pool *pool;
289 POINTER_DEFAULT = 100,
293 enum window_location {
295 WINDOW_RESIZING_TOP = 1,
296 WINDOW_RESIZING_BOTTOM = 2,
297 WINDOW_RESIZING_LEFT = 4,
298 WINDOW_RESIZING_TOP_LEFT = 5,
299 WINDOW_RESIZING_BOTTOM_LEFT = 6,
300 WINDOW_RESIZING_RIGHT = 8,
301 WINDOW_RESIZING_TOP_RIGHT = 9,
302 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
303 WINDOW_RESIZING_MASK = 15,
304 WINDOW_EXTERIOR = 16,
305 WINDOW_TITLEBAR = 17,
306 WINDOW_CLIENT_AREA = 18,
309 const char *option_xkb_layout = "us";
310 const char *option_xkb_variant = "";
311 const char *option_xkb_options = "";
313 static const struct weston_option xkb_options[] = {
314 { WESTON_OPTION_STRING, "xkb-layout", 0, &option_xkb_layout },
315 { WESTON_OPTION_STRING, "xkb-variant", 0, &option_xkb_variant },
316 { WESTON_OPTION_STRING, "xkb-options", 0, &option_xkb_options },
319 static const cairo_user_data_key_t surface_data_key;
320 struct surface_data {
321 struct wl_buffer *buffer;
324 #define MULT(_d,c,a,t) \
325 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
327 #ifdef HAVE_CAIRO_EGL
329 struct egl_window_surface_data {
330 struct display *display;
331 struct wl_surface *surface;
332 struct wl_egl_window *window;
337 egl_window_surface_data_destroy(void *p)
339 struct egl_window_surface_data *data = p;
340 struct display *d = data->display;
342 eglDestroySurface(d->dpy, data->surf);
343 wl_egl_window_destroy(data->window);
344 data->surface = NULL;
349 static cairo_surface_t *
350 display_create_egl_window_surface(struct display *display,
351 struct wl_surface *surface,
353 struct rectangle *rectangle)
355 cairo_surface_t *cairo_surface;
356 struct egl_window_surface_data *data;
358 cairo_device_t *device;
360 data = malloc(sizeof *data);
364 data->display = display;
365 data->surface = surface;
367 config = display->argb_config;
368 device = display->argb_device;
370 data->window = wl_egl_window_create(surface,
374 data->surf = eglCreateWindowSurface(display->dpy, config,
377 cairo_surface = cairo_gl_surface_create_for_egl(device,
382 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
383 data, egl_window_surface_data_destroy);
385 return cairo_surface;
391 display_get_buffer_for_surface(struct display *display,
392 cairo_surface_t *surface)
394 struct surface_data *data;
396 data = cairo_surface_get_user_data (surface, &surface_data_key);
401 struct shm_surface_data {
402 struct surface_data data;
403 struct shm_pool *pool;
407 shm_pool_destroy(struct shm_pool *pool);
410 shm_surface_data_destroy(void *p)
412 struct shm_surface_data *data = p;
414 wl_buffer_destroy(data->data.buffer);
416 shm_pool_destroy(data->pool);
420 shm_surface_write(cairo_surface_t *surface, unsigned char *data, int count)
422 void *dest = cairo_image_surface_get_data(surface);
424 memcpy(dest, data, count);
427 static struct wl_shm_pool *
428 make_shm_pool(struct display *display, int size, void **data)
430 char filename[] = "/tmp/wayland-shm-XXXXXX";
431 struct wl_shm_pool *pool;
434 fd = mkstemp(filename);
436 fprintf(stderr, "open %s failed: %m\n", filename);
439 if (ftruncate(fd, size) < 0) {
440 fprintf(stderr, "ftruncate failed: %m\n");
445 *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
448 if (*data == MAP_FAILED) {
449 fprintf(stderr, "mmap failed: %m\n");
454 pool = wl_shm_create_pool(display->shm, fd, size);
461 static struct shm_pool *
462 shm_pool_create(struct display *display, size_t size)
464 struct shm_pool *pool = malloc(sizeof *pool);
469 pool->pool = make_shm_pool(display, size, &pool->data);
482 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
484 if (pool->used + size > pool->size)
487 *offset = pool->used;
490 return (char *) pool->data + *offset;
493 /* destroy the pool. this does not unmap the memory though */
495 shm_pool_destroy(struct shm_pool *pool)
497 munmap(pool->data, pool->size);
498 wl_shm_pool_destroy(pool->pool);
502 /* Start allocating from the beginning of the pool again */
504 shm_pool_reset(struct shm_pool *pool)
510 data_length_for_shm_surface(struct rectangle *rect)
514 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
516 return stride * rect->height;
519 static cairo_surface_t *
520 display_create_shm_surface_from_pool(struct display *display,
521 struct rectangle *rectangle,
522 uint32_t flags, struct shm_pool *pool)
524 struct shm_surface_data *data;
526 cairo_surface_t *surface;
527 int stride, length, offset;
530 data = malloc(sizeof *data);
534 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
536 length = stride * rectangle->height;
538 map = shm_pool_allocate(pool, length, &offset);
545 surface = cairo_image_surface_create_for_data (map,
551 cairo_surface_set_user_data (surface, &surface_data_key,
552 data, shm_surface_data_destroy);
554 if (flags & SURFACE_OPAQUE)
555 format = WL_SHM_FORMAT_XRGB8888;
557 format = WL_SHM_FORMAT_ARGB8888;
559 data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
567 static cairo_surface_t *
568 display_create_shm_surface(struct display *display,
569 struct rectangle *rectangle, uint32_t flags,
570 struct window *window)
572 struct shm_surface_data *data;
573 struct shm_pool *pool;
574 cairo_surface_t *surface;
576 if (window && window->pool) {
577 shm_pool_reset(window->pool);
578 surface = display_create_shm_surface_from_pool(display,
586 pool = shm_pool_create(display,
587 data_length_for_shm_surface(rectangle));
592 display_create_shm_surface_from_pool(display, rectangle,
596 shm_pool_destroy(pool);
600 /* make sure we destroy the pool when the surface is destroyed */
601 data = cairo_surface_get_user_data(surface, &surface_data_key);
608 check_size(struct rectangle *rect)
610 if (rect->width && rect->height)
613 fprintf(stderr, "tried to create surface of "
614 "width: %d, height: %d\n", rect->width, rect->height);
619 display_create_surface(struct display *display,
620 struct wl_surface *surface,
621 struct rectangle *rectangle,
624 if (check_size(rectangle) < 0)
626 #ifdef HAVE_CAIRO_EGL
628 return display_create_egl_window_surface(display,
633 return display_create_shm_surface(display, rectangle, flags, NULL);
636 static const char *cursors[] = {
637 "bottom_left_corner",
638 "bottom_right_corner",
652 create_cursor_from_images(struct display *display, struct cursor *cursor,
653 XcursorImages *images)
656 struct rectangle rect;
659 cursor->images = malloc(images->nimage * sizeof *cursor->images);
660 cursor->n_images = images->nimage;
662 for (i = 0; i < images->nimage; i++) {
663 image = images->images[i];
665 rect.width = image->width;
666 rect.height = image->height;
668 cursor->images[i].surface =
669 display_create_shm_surface_from_pool(display, &rect, 0,
670 display->cursor_shm_pool);
672 shm_surface_write(cursor->images[i].surface,
673 (unsigned char *) image->pixels,
674 image->width * image->height * sizeof image->pixels[0]);
676 cursor->images[i].width = image->width;
677 cursor->images[i].height = image->height;
678 cursor->images[i].hotspot_x = image->xhot;
679 cursor->images[i].hotspot_y = image->yhot;
680 cursor->images[i].delay = image->delay;
686 data_length_for_cursor_images(XcursorImages *images)
690 struct rectangle rect;
692 for (i = 0; i < images->nimage; i++) {
693 rect.width = images->images[i]->width;
694 rect.height = images->images[i]->height;
695 length += data_length_for_shm_surface(&rect);
702 create_cursors(struct display *display)
705 size_t pool_size = 0;
706 struct cursor *cursor;
707 XcursorImages **images;
709 count = ARRAY_LENGTH(cursors);
710 display->cursors = malloc(count * sizeof *display->cursors);
711 images = malloc(count * sizeof images[0]);
713 for (i = 0; i < count; i++) {
714 images[i] = XcursorLibraryLoadImages(cursors[i], NULL, 32);
716 fprintf(stderr, "Error loading cursor: %s\n",
720 pool_size += data_length_for_cursor_images(images[i]);
723 display->cursor_shm_pool = shm_pool_create(display, pool_size);
725 for (i = 0; i < count; i++) {
726 cursor = &display->cursors[i];
729 cursor->n_images = 0;
730 cursor->images = NULL;
734 create_cursor_from_images(display, cursor, images[i]);
736 XcursorImagesDestroy(images[i]);
743 destroy_cursor_images(struct cursor *cursor)
747 for (i = 0; i < cursor->n_images; i++)
748 if (cursor->images[i].surface)
749 cairo_surface_destroy(cursor->images[i].surface);
751 free(cursor->images);
755 destroy_cursors(struct display *display)
759 count = ARRAY_LENGTH(cursors);
760 for (i = 0; i < count; ++i) {
761 destroy_cursor_images(&display->cursors[i]);
764 free(display->cursors);
765 shm_pool_destroy(display->cursor_shm_pool);
769 display_get_pointer_surface(struct display *display, int pointer,
770 int *width, int *height,
771 int *hotspot_x, int *hotspot_y)
773 struct cursor *cursor = &display->cursors[pointer];
774 cairo_surface_t *surface = cursor->images[0].surface;
776 /* FIXME returning information for the first image. Something better
777 * is needed for animated cursors */
779 *width = cairo_image_surface_get_width(surface);
780 *height = cairo_image_surface_get_height(surface);
782 *hotspot_x = cursor->images[0].hotspot_x;
783 *hotspot_y = cursor->images[0].hotspot_y;
785 return cairo_surface_reference(surface);
789 window_get_resize_dx_dy(struct window *window, int *x, int *y)
791 if (window->resize_edges & WINDOW_RESIZING_LEFT)
792 *x = window->server_allocation.width - window->allocation.width;
796 if (window->resize_edges & WINDOW_RESIZING_TOP)
797 *y = window->server_allocation.height -
798 window->allocation.height;
802 window->resize_edges = 0;
806 window_attach_surface(struct window *window)
808 struct display *display = window->display;
809 struct wl_buffer *buffer;
810 #ifdef HAVE_CAIRO_EGL
811 struct egl_window_surface_data *data;
815 if (window->type == TYPE_NONE) {
816 window->type = TYPE_TOPLEVEL;
818 wl_shell_surface_set_toplevel(window->shell_surface);
821 switch (window->buffer_type) {
822 #ifdef HAVE_CAIRO_EGL
823 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
824 data = cairo_surface_get_user_data(window->cairo_surface,
827 cairo_gl_surface_swapbuffers(window->cairo_surface);
828 wl_egl_window_get_attached_size(data->window,
829 &window->server_allocation.width,
830 &window->server_allocation.height);
833 case WINDOW_BUFFER_TYPE_SHM:
835 display_get_buffer_for_surface(display,
836 window->cairo_surface);
838 window_get_resize_dx_dy(window, &x, &y);
839 wl_surface_attach(window->surface, buffer, x, y);
840 wl_surface_damage(window->surface, 0, 0,
841 window->allocation.width,
842 window->allocation.height);
843 window->server_allocation = window->allocation;
844 cairo_surface_destroy(window->cairo_surface);
845 window->cairo_surface = NULL;
851 if (window->input_region) {
852 wl_surface_set_input_region(window->surface,
853 window->input_region);
854 wl_region_destroy(window->input_region);
855 window->input_region = NULL;
858 if (window->opaque_region) {
859 wl_surface_set_opaque_region(window->surface,
860 window->opaque_region);
861 wl_region_destroy(window->opaque_region);
862 window->opaque_region = NULL;
867 window_flush(struct window *window)
869 if (window->cairo_surface)
870 window_attach_surface(window);
874 window_set_surface(struct window *window, cairo_surface_t *surface)
876 cairo_surface_reference(surface);
878 if (window->cairo_surface != NULL)
879 cairo_surface_destroy(window->cairo_surface);
881 window->cairo_surface = surface;
884 #ifdef HAVE_CAIRO_EGL
886 window_resize_cairo_window_surface(struct window *window)
888 struct egl_window_surface_data *data;
891 data = cairo_surface_get_user_data(window->cairo_surface,
894 window_get_resize_dx_dy(window, &x, &y),
895 wl_egl_window_resize(data->window,
896 window->allocation.width,
897 window->allocation.height,
900 cairo_gl_surface_set_size(window->cairo_surface,
901 window->allocation.width,
902 window->allocation.height);
907 window_get_display(struct window *window)
909 return window->display;
913 window_create_surface(struct window *window)
915 cairo_surface_t *surface;
918 if (!window->transparent)
919 flags = SURFACE_OPAQUE;
921 switch (window->buffer_type) {
922 #ifdef HAVE_CAIRO_EGL
923 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
924 if (window->cairo_surface) {
925 window_resize_cairo_window_surface(window);
928 surface = display_create_surface(window->display,
930 &window->allocation, flags);
933 case WINDOW_BUFFER_TYPE_SHM:
934 surface = display_create_shm_surface(window->display,
943 window_set_surface(window, surface);
944 cairo_surface_destroy(surface);
947 static void frame_destroy(struct frame *frame);
950 window_destroy(struct window *window)
952 struct display *display = window->display;
955 if (window->redraw_scheduled)
956 wl_list_remove(&window->redraw_task.link);
958 wl_list_for_each(input, &display->input_list, link) {
959 if (input->pointer_focus == window)
960 input->pointer_focus = NULL;
961 if (input->keyboard_focus == window)
962 input->keyboard_focus = NULL;
963 if (input->focus_widget &&
964 input->focus_widget->window == window)
965 input->focus_widget = NULL;
968 if (window->input_region)
969 wl_region_destroy(window->input_region);
970 if (window->opaque_region)
971 wl_region_destroy(window->opaque_region);
974 frame_destroy(window->frame);
976 if (window->shell_surface)
977 wl_shell_surface_destroy(window->shell_surface);
978 wl_surface_destroy(window->surface);
979 wl_list_remove(&window->link);
981 if (window->cairo_surface != NULL)
982 cairo_surface_destroy(window->cairo_surface);
988 static struct widget *
989 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
991 struct widget *child, *target;
993 wl_list_for_each(child, &widget->child_list, link) {
994 target = widget_find_widget(child, x, y);
999 if (widget->allocation.x <= x &&
1000 x < widget->allocation.x + widget->allocation.width &&
1001 widget->allocation.y <= y &&
1002 y < widget->allocation.y + widget->allocation.height) {
1009 static struct widget *
1010 widget_create(struct window *window, void *data)
1012 struct widget *widget;
1014 widget = malloc(sizeof *widget);
1015 memset(widget, 0, sizeof *widget);
1016 widget->window = window;
1017 widget->user_data = data;
1018 widget->allocation = window->allocation;
1019 wl_list_init(&widget->child_list);
1026 window_add_widget(struct window *window, void *data)
1028 window->widget = widget_create(window, data);
1029 wl_list_init(&window->widget->link);
1031 return window->widget;
1035 widget_add_widget(struct widget *parent, void *data)
1037 struct widget *widget;
1039 widget = widget_create(parent->window, data);
1040 wl_list_insert(parent->child_list.prev, &widget->link);
1046 widget_destroy(struct widget *widget)
1048 struct display *display = widget->window->display;
1049 struct input *input;
1051 wl_list_for_each(input, &display->input_list, link) {
1052 if (input->focus_widget == widget)
1053 input->focus_widget = NULL;
1056 wl_list_remove(&widget->link);
1061 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1063 *allocation = widget->allocation;
1067 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1069 widget->allocation.width = width;
1070 widget->allocation.height = height;
1074 widget_set_allocation(struct widget *widget,
1075 int32_t x, int32_t y, int32_t width, int32_t height)
1077 widget->allocation.x = x;
1078 widget->allocation.y = y;
1079 widget_set_size(widget, width, height);
1083 widget_set_transparent(struct widget *widget, int transparent)
1085 widget->opaque = !transparent;
1089 widget_get_user_data(struct widget *widget)
1091 return widget->user_data;
1095 widget_set_resize_handler(struct widget *widget,
1096 widget_resize_handler_t handler)
1098 widget->resize_handler = handler;
1102 widget_set_redraw_handler(struct widget *widget,
1103 widget_redraw_handler_t handler)
1105 widget->redraw_handler = handler;
1109 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1111 widget->enter_handler = handler;
1115 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1117 widget->leave_handler = handler;
1121 widget_set_motion_handler(struct widget *widget,
1122 widget_motion_handler_t handler)
1124 widget->motion_handler = handler;
1128 widget_set_button_handler(struct widget *widget,
1129 widget_button_handler_t handler)
1131 widget->button_handler = handler;
1135 widget_schedule_redraw(struct widget *widget)
1137 window_schedule_redraw(widget->window);
1141 window_get_surface(struct window *window)
1143 return cairo_surface_reference(window->cairo_surface);
1147 window_get_wl_surface(struct window *window)
1149 return window->surface;
1152 struct wl_shell_surface *
1153 window_get_wl_shell_surface(struct window *window)
1155 return window->shell_surface;
1159 frame_resize_handler(struct widget *widget,
1160 int32_t width, int32_t height, void *data)
1162 struct frame *frame = data;
1163 struct widget *child = frame->child;
1164 struct rectangle allocation;
1165 struct display *display = widget->window->display;
1166 struct frame_button * button;
1167 int x_l, x_r, y, w, h;
1168 int decoration_width, decoration_height;
1171 if (widget->window->type != TYPE_FULLSCREEN) {
1172 decoration_width = (frame->width + frame->margin) * 2;
1173 decoration_height = frame->width +
1174 frame->titlebar_height + frame->margin * 2;
1176 allocation.x = frame->width + frame->margin;
1177 allocation.y = frame->titlebar_height + frame->margin;
1178 allocation.width = width - decoration_width;
1179 allocation.height = height - decoration_height;
1181 widget->window->input_region =
1182 wl_compositor_create_region(display->compositor);
1183 wl_region_add(widget->window->input_region,
1184 frame->margin, frame->margin,
1185 width - 2 * frame->margin,
1186 height - 2 * frame->margin);
1188 opaque_margin = frame->margin + display->frame_radius;
1190 wl_list_for_each(button, &frame->buttons_list, link)
1191 button->widget->opaque = 0;
1193 decoration_width = 0;
1194 decoration_height = 0;
1198 allocation.width = width;
1199 allocation.height = height;
1202 wl_list_for_each(button, &frame->buttons_list, link)
1203 button->widget->opaque = 1;
1206 widget_set_allocation(child, allocation.x, allocation.y,
1207 allocation.width, allocation.height);
1209 if (child->resize_handler)
1210 child->resize_handler(child,
1215 widget_set_allocation(widget, 0, 0,
1216 child->allocation.width + decoration_width,
1217 child->allocation.height + decoration_height);
1219 if (child->opaque) {
1220 widget->window->opaque_region =
1221 wl_compositor_create_region(display->compositor);
1222 wl_region_add(widget->window->opaque_region,
1223 opaque_margin, opaque_margin,
1224 widget->allocation.width - 2 * opaque_margin,
1225 widget->allocation.height - 2 * opaque_margin);
1228 /* frame internal buttons */
1229 x_r = frame->widget->allocation.width - frame->width - frame->margin;
1230 x_l = frame->width + frame->margin;
1231 y = frame->width + frame->margin;
1232 wl_list_for_each(button, &frame->buttons_list, link) {
1233 const int button_padding = 4;
1234 w = cairo_image_surface_get_width(button->icon);
1235 h = cairo_image_surface_get_height(button->icon);
1237 if (button->decoration == FRAME_BUTTON_FANCY)
1240 if (button->align == FRAME_BUTTON_LEFT) {
1241 widget_set_allocation(button->widget,
1242 x_l, y , w + 1, h + 1);
1244 x_l += button_padding;
1247 widget_set_allocation(button->widget,
1248 x_r, y , w + 1, h + 1);
1249 x_r -= button_padding;
1255 frame_button_enter_handler(struct widget *widget,
1256 struct input *input, float x, float y, void *data)
1258 struct frame_button *frame_button = data;
1260 widget_schedule_redraw(frame_button->widget);
1261 frame_button->state = FRAME_BUTTON_OVER;
1263 return POINTER_LEFT_PTR;
1267 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1269 struct frame_button *frame_button = data;
1271 widget_schedule_redraw(frame_button->widget);
1272 frame_button->state = FRAME_BUTTON_DEFAULT;
1276 frame_button_button_handler(struct widget *widget,
1277 struct input *input, uint32_t time,
1278 uint32_t button, uint32_t state, void *data)
1280 struct frame_button *frame_button = data;
1281 struct window *window = widget->window;
1283 if (button != BTN_LEFT)
1288 frame_button->state = FRAME_BUTTON_ACTIVE;
1289 widget_schedule_redraw(frame_button->widget);
1291 if (frame_button->type == FRAME_BUTTON_ICON)
1292 window_show_frame_menu(window, input, time);
1295 frame_button->state = FRAME_BUTTON_DEFAULT;
1296 widget_schedule_redraw(frame_button->widget);
1300 switch (frame_button->type) {
1301 case FRAME_BUTTON_CLOSE:
1302 if (window->close_handler)
1303 window->close_handler(window->parent,
1306 display_exit(window->display);
1308 case FRAME_BUTTON_MINIMIZE:
1309 fprintf(stderr,"Minimize stub\n");
1311 case FRAME_BUTTON_MAXIMIZE:
1312 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1315 /* Unknown operation */
1321 frame_button_redraw_handler(struct widget *widget, void *data)
1323 struct frame_button *frame_button = data;
1325 int width, height, x, y;
1326 struct window *window = widget->window;
1328 x = widget->allocation.x;
1329 y = widget->allocation.y;
1330 width = widget->allocation.width;
1331 height = widget->allocation.height;
1340 cr = cairo_create(window->cairo_surface);
1342 if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1343 cairo_set_line_width(cr, 1);
1345 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1346 cairo_rectangle (cr, x, y, 25, 16);
1348 cairo_stroke_preserve(cr);
1350 switch (frame_button->state) {
1351 case FRAME_BUTTON_DEFAULT:
1352 cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1354 case FRAME_BUTTON_OVER:
1355 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1357 case FRAME_BUTTON_ACTIVE:
1358 cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1367 cairo_set_source_surface(cr, frame_button->icon, x, y);
1373 static struct widget *
1374 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1375 enum frame_button_align align, enum frame_button_decoration style)
1377 struct frame_button *frame_button;
1378 const char *icon = data;
1380 frame_button = malloc (sizeof *frame_button);
1381 memset(frame_button, 0, sizeof *frame_button);
1383 frame_button->icon = cairo_image_surface_create_from_png(icon);
1384 frame_button->widget = widget_add_widget(frame->widget, frame_button);
1385 frame_button->frame = frame;
1386 frame_button->type = type;
1387 frame_button->align = align;
1388 frame_button->decoration = style;
1390 wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1392 widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1393 widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1394 widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1395 widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1396 return frame_button->widget;
1400 frame_button_destroy(struct frame_button *frame_button)
1402 widget_destroy(frame_button->widget);
1403 wl_list_remove(&frame_button->link);
1404 cairo_surface_destroy(frame_button->icon);
1411 frame_redraw_handler(struct widget *widget, void *data)
1413 struct frame *frame = data;
1415 cairo_text_extents_t extents;
1416 cairo_surface_t *source;
1417 int x, y, width, height;
1418 struct window *window = widget->window;
1420 if (window->type == TYPE_FULLSCREEN)
1423 width = widget->allocation.width;
1424 height = widget->allocation.height;
1426 cr = cairo_create(window->cairo_surface);
1428 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1429 cairo_set_source_rgba(cr, 0, 0, 0, 0);
1432 cairo_set_source_rgba(cr, 0, 0, 0, 0.45);
1433 tile_mask(cr, window->display->shadow,
1434 2, 2, width + 8, height + 8,
1437 if (window->keyboard_device)
1438 source = window->display->active_frame;
1440 source = window->display->inactive_frame;
1442 tile_source(cr, source,
1443 frame->margin, frame->margin,
1444 width - frame->margin * 2, height - frame->margin * 2,
1445 frame->width, frame->titlebar_height);
1447 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1448 cairo_select_font_face(cr, "sans",
1449 CAIRO_FONT_SLANT_NORMAL,
1450 CAIRO_FONT_WEIGHT_BOLD);
1451 cairo_set_font_size(cr, 14);
1452 cairo_text_extents(cr, window->title, &extents);
1453 x = (width - extents.width) / 2;
1454 y = frame->margin + 8 - extents.y_bearing;
1455 if (window->keyboard_device) {
1456 cairo_move_to(cr, x + 1, y + 1);
1457 cairo_set_source_rgb(cr, 1, 1, 1);
1458 cairo_show_text(cr, window->title);
1459 cairo_move_to(cr, x, y);
1460 cairo_set_source_rgb(cr, 0, 0, 0);
1461 cairo_show_text(cr, window->title);
1463 cairo_move_to(cr, x, y);
1464 cairo_set_source_rgb(cr, 0.4, 0.4, 0.4);
1465 cairo_show_text(cr, window->title);
1472 frame_get_pointer_location(struct frame *frame, int32_t x, int32_t y)
1474 struct widget *widget = frame->widget;
1475 int vlocation, hlocation, location;
1476 const int grip_size = 8;
1478 if (x < frame->margin)
1479 hlocation = WINDOW_EXTERIOR;
1480 else if (frame->margin <= x && x < frame->margin + grip_size)
1481 hlocation = WINDOW_RESIZING_LEFT;
1482 else if (x < widget->allocation.width - frame->margin - grip_size)
1483 hlocation = WINDOW_INTERIOR;
1484 else if (x < widget->allocation.width - frame->margin)
1485 hlocation = WINDOW_RESIZING_RIGHT;
1487 hlocation = WINDOW_EXTERIOR;
1489 if (y < frame->margin)
1490 vlocation = WINDOW_EXTERIOR;
1491 else if (frame->margin <= y && y < frame->margin + grip_size)
1492 vlocation = WINDOW_RESIZING_TOP;
1493 else if (y < widget->allocation.height - frame->margin - grip_size)
1494 vlocation = WINDOW_INTERIOR;
1495 else if (y < widget->allocation.height - frame->margin)
1496 vlocation = WINDOW_RESIZING_BOTTOM;
1498 vlocation = WINDOW_EXTERIOR;
1500 location = vlocation | hlocation;
1501 if (location & WINDOW_EXTERIOR)
1502 location = WINDOW_EXTERIOR;
1503 if (location == WINDOW_INTERIOR && y < frame->margin + 50)
1504 location = WINDOW_TITLEBAR;
1505 else if (location == WINDOW_INTERIOR)
1506 location = WINDOW_CLIENT_AREA;
1512 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1516 location = frame_get_pointer_location(frame, input->sx, input->sy);
1518 case WINDOW_RESIZING_TOP:
1520 case WINDOW_RESIZING_BOTTOM:
1521 return POINTER_BOTTOM;
1522 case WINDOW_RESIZING_LEFT:
1523 return POINTER_LEFT;
1524 case WINDOW_RESIZING_RIGHT:
1525 return POINTER_RIGHT;
1526 case WINDOW_RESIZING_TOP_LEFT:
1527 return POINTER_TOP_LEFT;
1528 case WINDOW_RESIZING_TOP_RIGHT:
1529 return POINTER_TOP_RIGHT;
1530 case WINDOW_RESIZING_BOTTOM_LEFT:
1531 return POINTER_BOTTOM_LEFT;
1532 case WINDOW_RESIZING_BOTTOM_RIGHT:
1533 return POINTER_BOTTOM_RIGHT;
1534 case WINDOW_EXTERIOR:
1535 case WINDOW_TITLEBAR:
1537 return POINTER_LEFT_PTR;
1542 frame_menu_func(struct window *window, int index, void *data)
1546 if (window->close_handler)
1547 window->close_handler(window->parent,
1550 display_exit(window->display);
1552 case 1: /* fullscreen */
1553 /* we don't have a way to get out of fullscreen for now */
1554 window_set_fullscreen(window, 1);
1556 case 2: /* rotate */
1563 window_show_frame_menu(struct window *window,
1564 struct input *input, uint32_t time)
1568 static const char *entries[] = {
1569 "Close", "Fullscreen", "Rotate", "Scale"
1572 input_get_position(input, &x, &y);
1573 window_show_menu(window->display, input, time, window,
1574 x - 10, y - 10, frame_menu_func, entries, 4);
1578 frame_enter_handler(struct widget *widget,
1579 struct input *input, float x, float y, void *data)
1581 return frame_get_pointer_image_for_location(data, input);
1585 frame_motion_handler(struct widget *widget,
1586 struct input *input, uint32_t time,
1587 float x, float y, void *data)
1589 return frame_get_pointer_image_for_location(data, input);
1593 frame_button_handler(struct widget *widget,
1594 struct input *input, uint32_t time,
1595 uint32_t button, uint32_t state, void *data)
1598 struct frame *frame = data;
1599 struct window *window = widget->window;
1600 struct display *display = window->display;
1603 location = frame_get_pointer_location(frame, input->sx, input->sy);
1605 if (window->display->shell && button == BTN_LEFT && state == 1) {
1607 case WINDOW_TITLEBAR:
1608 if (!window->shell_surface)
1610 input_set_pointer_image(input, time, POINTER_DRAGGING);
1611 input_ungrab(input);
1612 wl_shell_surface_move(window->shell_surface,
1613 input_get_input_device(input),
1616 case WINDOW_RESIZING_TOP:
1617 case WINDOW_RESIZING_BOTTOM:
1618 case WINDOW_RESIZING_LEFT:
1619 case WINDOW_RESIZING_RIGHT:
1620 case WINDOW_RESIZING_TOP_LEFT:
1621 case WINDOW_RESIZING_TOP_RIGHT:
1622 case WINDOW_RESIZING_BOTTOM_LEFT:
1623 case WINDOW_RESIZING_BOTTOM_RIGHT:
1624 if (!window->shell_surface)
1626 input_ungrab(input);
1628 if (!display->dpy) {
1629 /* If we're using shm, allocate a big
1630 pool to create buffers out of while
1631 we resize. We should probably base
1632 this number on the size of the output. */
1634 shm_pool_create(display, 6 * 1024 * 1024);
1637 wl_shell_surface_resize(window->shell_surface,
1638 input_get_input_device(input),
1639 display->serial, location);
1642 } else if (button == BTN_RIGHT && state == 1) {
1643 window_show_frame_menu(window, input, time);
1648 frame_create(struct window *window, void *data)
1650 struct frame *frame;
1652 frame = malloc(sizeof *frame);
1653 memset(frame, 0, sizeof *frame);
1655 frame->widget = window_add_widget(window, frame);
1656 frame->child = widget_add_widget(frame->widget, data);
1659 frame->titlebar_height = 27;
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 = POINTER_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 input_handle_motion(void *data, struct wl_input_device *input_device,
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 pointer = POINTER_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 pointer = widget->motion_handler(input->focus_widget,
1759 input, time, sx, sy,
1762 input_set_pointer_image(input, time, pointer);
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 input_handle_button(void *data,
1787 struct wl_input_device *input_device, uint32_t serial,
1788 uint32_t time, uint32_t button, uint32_t state)
1790 struct input *input = data;
1791 struct widget *widget;
1793 input->display->serial = serial;
1794 if (input->focus_widget && input->grab == NULL && state)
1795 input_grab(input, input->focus_widget, button);
1797 widget = input->grab;
1798 if (widget && widget->button_handler)
1799 (*widget->button_handler)(widget,
1802 input->grab->user_data);
1804 if (input->grab && input->grab_button == button && !state)
1805 input_ungrab(input);
1809 input_handle_axis(void *data,
1810 struct wl_input_device *input_device,
1811 uint32_t time, uint32_t axis, int32_t value)
1816 input_handle_key(void *data, struct wl_input_device *input_device,
1817 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1819 struct input *input = data;
1820 struct window *window = input->keyboard_focus;
1821 struct display *d = input->display;
1822 uint32_t code, num_syms;
1823 const xkb_keysym_t *syms;
1825 xkb_mod_mask_t mask;
1827 input->display->serial = serial;
1829 if (!window || window->keyboard_device != input)
1832 num_syms = xkb_key_get_syms(d->xkb.state, code, &syms);
1833 xkb_state_update_key(d->xkb.state, code,
1834 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1836 mask = xkb_state_serialise_mods(d->xkb.state,
1837 XKB_STATE_DEPRESSED |
1839 input->modifiers = 0;
1840 if (mask & input->display->xkb.control_mask)
1841 input->modifiers |= MOD_CONTROL_MASK;
1842 if (mask & input->display->xkb.alt_mask)
1843 input->modifiers |= MOD_ALT_MASK;
1844 if (mask & input->display->xkb.shift_mask)
1845 input->modifiers |= MOD_SHIFT_MASK;
1847 if (num_syms == 1 && syms[0] == XK_F5 &&
1848 input->modifiers == MOD_ALT_MASK) {
1850 window_set_maximized(window,
1851 window->type != TYPE_MAXIMIZED);
1852 } else if (window->key_handler) {
1858 (*window->key_handler)(window, input, time, key,
1859 sym, state, window->user_data);
1864 input_remove_pointer_focus(struct input *input)
1866 struct window *window = input->pointer_focus;
1871 input_set_focus_widget(input, NULL, 0, 0);
1873 input->pointer_focus = NULL;
1874 input->current_cursor = POINTER_UNSET;
1878 input_handle_pointer_enter(void *data,
1879 struct wl_input_device *input_device,
1880 uint32_t serial, struct wl_surface *surface,
1881 wl_fixed_t sx_w, wl_fixed_t sy_w)
1883 struct input *input = data;
1884 struct window *window;
1885 struct widget *widget;
1886 float sx = wl_fixed_to_double(sx_w);
1887 float sy = wl_fixed_to_double(sy_w);
1890 /* enter event for a window we've just destroyed */
1894 input->display->serial = serial;
1895 input->pointer_enter_serial = serial;
1896 input->pointer_focus = wl_surface_get_user_data(surface);
1897 window = input->pointer_focus;
1900 shm_pool_destroy(window->pool);
1901 window->pool = NULL;
1902 /* Schedule a redraw to free the pool */
1903 window_schedule_redraw(window);
1909 widget = widget_find_widget(window->widget, sx, sy);
1910 input_set_focus_widget(input, widget, sx, sy);
1914 input_handle_pointer_leave(void *data,
1915 struct wl_input_device *input_device,
1916 uint32_t serial, struct wl_surface *surface)
1918 struct input *input = data;
1920 input->display->serial = serial;
1921 input_remove_pointer_focus(input);
1925 input_remove_keyboard_focus(struct input *input)
1927 struct window *window = input->keyboard_focus;
1932 window->keyboard_device = NULL;
1933 if (window->keyboard_focus_handler)
1934 (*window->keyboard_focus_handler)(window, NULL,
1937 input->keyboard_focus = NULL;
1941 input_handle_keyboard_enter(void *data,
1942 struct wl_input_device *input_device,
1944 struct wl_surface *surface,
1945 struct wl_array *keys)
1947 struct input *input = data;
1948 struct window *window;
1950 input->display->serial = serial;
1951 input->keyboard_focus = wl_surface_get_user_data(surface);
1953 window = input->keyboard_focus;
1954 window->keyboard_device = input;
1955 if (window->keyboard_focus_handler)
1956 (*window->keyboard_focus_handler)(window,
1957 window->keyboard_device,
1962 input_handle_keyboard_leave(void *data,
1963 struct wl_input_device *input_device,
1965 struct wl_surface *surface)
1967 struct input *input = data;
1969 input->display->serial = serial;
1970 input_remove_keyboard_focus(input);
1974 input_handle_touch_down(void *data,
1975 struct wl_input_device *wl_input_device,
1976 uint32_t serial, uint32_t time,
1977 struct wl_surface *surface,
1978 int32_t id, wl_fixed_t x, wl_fixed_t y)
1983 input_handle_touch_up(void *data,
1984 struct wl_input_device *wl_input_device,
1985 uint32_t serial, uint32_t time, int32_t id)
1990 input_handle_touch_motion(void *data,
1991 struct wl_input_device *wl_input_device,
1992 uint32_t time, int32_t id,
1993 wl_fixed_t x, wl_fixed_t y)
1998 input_handle_touch_frame(void *data,
1999 struct wl_input_device *wl_input_device)
2004 input_handle_touch_cancel(void *data,
2005 struct wl_input_device *wl_input_device)
2009 static const struct wl_input_device_listener input_device_listener = {
2010 input_handle_motion,
2011 input_handle_button,
2014 input_handle_pointer_enter,
2015 input_handle_pointer_leave,
2016 input_handle_keyboard_enter,
2017 input_handle_keyboard_leave,
2018 input_handle_touch_down,
2019 input_handle_touch_up,
2020 input_handle_touch_motion,
2021 input_handle_touch_frame,
2022 input_handle_touch_cancel,
2026 input_get_position(struct input *input, int32_t *x, int32_t *y)
2032 struct wl_input_device *
2033 input_get_input_device(struct input *input)
2035 return input->input_device;
2039 input_get_modifiers(struct input *input)
2041 return input->modifiers;
2045 input_get_focus_widget(struct input *input)
2047 return input->focus_widget;
2051 struct wl_data_offer *offer;
2052 struct input *input;
2053 struct wl_array types;
2056 struct task io_task;
2064 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2066 struct data_offer *offer = data;
2069 p = wl_array_add(&offer->types, sizeof *p);
2073 static const struct wl_data_offer_listener data_offer_listener = {
2078 data_offer_destroy(struct data_offer *offer)
2083 if (offer->refcount == 0) {
2084 wl_data_offer_destroy(offer->offer);
2085 for (p = offer->types.data; *p; p++)
2087 wl_array_release(&offer->types);
2093 data_device_data_offer(void *data,
2094 struct wl_data_device *data_device, uint32_t id)
2096 struct data_offer *offer;
2098 offer = malloc(sizeof *offer);
2100 wl_array_init(&offer->types);
2101 offer->refcount = 1;
2102 offer->input = data;
2104 /* FIXME: Generate typesafe wrappers for this */
2105 offer->offer = (struct wl_data_offer *)
2106 wl_proxy_create_for_id((struct wl_proxy *) data_device,
2107 id, &wl_data_offer_interface);
2109 wl_data_offer_add_listener(offer->offer,
2110 &data_offer_listener, offer);
2114 data_device_enter(void *data, struct wl_data_device *data_device,
2115 uint32_t serial, struct wl_surface *surface,
2116 wl_fixed_t x_w, wl_fixed_t y_w,
2117 struct wl_data_offer *offer)
2119 struct input *input = data;
2120 struct window *window;
2121 float x = wl_fixed_to_double(x_w);
2122 float y = wl_fixed_to_double(y_w);
2125 input->pointer_enter_serial = serial;
2126 input->drag_offer = wl_data_offer_get_user_data(offer);
2127 window = wl_surface_get_user_data(surface);
2128 input->pointer_focus = window;
2130 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2133 window = input->pointer_focus;
2134 if (window->data_handler)
2135 window->data_handler(window, input, x, y,
2136 input->drag_offer->types.data,
2141 data_device_leave(void *data, struct wl_data_device *data_device)
2143 struct input *input = data;
2145 data_offer_destroy(input->drag_offer);
2146 input->drag_offer = NULL;
2150 data_device_motion(void *data, struct wl_data_device *data_device,
2151 uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2153 struct input *input = data;
2154 struct window *window = input->pointer_focus;
2155 float x = wl_fixed_to_double(x_w);
2156 float y = wl_fixed_to_double(y_w);
2161 if (window->data_handler)
2162 window->data_handler(window, input, x, y,
2163 input->drag_offer->types.data,
2168 data_device_drop(void *data, struct wl_data_device *data_device)
2170 struct input *input = data;
2171 struct window *window = input->pointer_focus;
2173 if (window->drop_handler)
2174 window->drop_handler(window, input,
2175 input->sx, input->sy, window->user_data);
2179 data_device_selection(void *data,
2180 struct wl_data_device *wl_data_device,
2181 struct wl_data_offer *offer)
2183 struct input *input = data;
2186 if (input->selection_offer)
2187 data_offer_destroy(input->selection_offer);
2190 input->selection_offer = wl_data_offer_get_user_data(offer);
2191 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2196 static const struct wl_data_device_listener data_device_listener = {
2197 data_device_data_offer,
2202 data_device_selection
2206 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
2208 struct display *display = input->display;
2209 struct wl_buffer *buffer;
2210 struct cursor_image *image;
2212 if (pointer == input->current_cursor)
2215 if (display->cursors[pointer].n_images == 0)
2218 image = &display->cursors[pointer].images[0];
2220 if (!image->surface)
2223 input->current_cursor = pointer;
2224 buffer = display_get_buffer_for_surface(display, image->surface);
2225 wl_input_device_attach(input->input_device, time, buffer,
2226 image->hotspot_x, image->hotspot_y);
2229 struct wl_data_device *
2230 input_get_data_device(struct input *input)
2232 return input->data_device;
2236 input_set_selection(struct input *input,
2237 struct wl_data_source *source, uint32_t time)
2239 wl_data_device_set_selection(input->data_device, source, time);
2243 input_accept(struct input *input, const char *type)
2245 wl_data_offer_accept(input->drag_offer->offer,
2246 input->pointer_enter_serial, type);
2250 offer_io_func(struct task *task, uint32_t events)
2252 struct data_offer *offer =
2253 container_of(task, struct data_offer, io_task);
2257 len = read(offer->fd, buffer, sizeof buffer);
2258 offer->func(buffer, len,
2259 offer->x, offer->y, offer->user_data);
2263 data_offer_destroy(offer);
2268 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2269 data_func_t func, void *user_data)
2273 if (pipe2(p, O_CLOEXEC) == -1)
2276 wl_data_offer_receive(offer->offer, mime_type, p[1]);
2279 offer->io_task.run = offer_io_func;
2283 offer->user_data = user_data;
2285 display_watch_fd(offer->input->display,
2286 offer->fd, EPOLLIN, &offer->io_task);
2290 input_receive_drag_data(struct input *input, const char *mime_type,
2291 data_func_t func, void *data)
2293 data_offer_receive_data(input->drag_offer, mime_type, func, data);
2294 input->drag_offer->x = input->sx;
2295 input->drag_offer->y = input->sy;
2299 input_receive_selection_data(struct input *input, const char *mime_type,
2300 data_func_t func, void *data)
2304 if (input->selection_offer == NULL)
2307 for (p = input->selection_offer->types.data; *p; p++)
2308 if (strcmp(mime_type, *p) == 0)
2314 data_offer_receive_data(input->selection_offer,
2315 mime_type, func, data);
2320 input_receive_selection_data_to_fd(struct input *input,
2321 const char *mime_type, int fd)
2323 wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
2329 window_move(struct window *window, struct input *input, uint32_t serial)
2331 if (!window->shell_surface)
2334 wl_shell_surface_move(window->shell_surface,
2335 input->input_device, serial);
2339 idle_resize(struct window *window)
2341 struct widget *widget;
2343 window->resize_needed = 0;
2344 widget = window->widget;
2345 widget_set_allocation(widget,
2346 window->pending_allocation.x,
2347 window->pending_allocation.y,
2348 window->pending_allocation.width,
2349 window->pending_allocation.height);
2351 if (window->input_region) {
2352 wl_region_destroy(window->input_region);
2353 window->input_region = NULL;
2356 if (window->opaque_region) {
2357 wl_region_destroy(window->opaque_region);
2358 window->opaque_region = NULL;
2361 if (widget->resize_handler)
2362 widget->resize_handler(widget,
2363 widget->allocation.width,
2364 widget->allocation.height,
2367 if (window->allocation.width != widget->allocation.width ||
2368 window->allocation.height != widget->allocation.height) {
2369 window->allocation = widget->allocation;
2370 window_schedule_redraw(window);
2375 window_schedule_resize(struct window *window, int width, int height)
2377 window->pending_allocation.x = 0;
2378 window->pending_allocation.y = 0;
2379 window->pending_allocation.width = width;
2380 window->pending_allocation.height = height;
2382 window->resize_needed = 1;
2383 window_schedule_redraw(window);
2387 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2389 window_schedule_resize(widget->window, width, height);
2393 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2396 wl_shell_surface_pong(shell_surface, serial);
2400 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2401 uint32_t edges, int32_t width, int32_t height)
2403 struct window *window = data;
2405 if (width <= 0 || height <= 0)
2408 window->resize_edges = edges;
2409 window_schedule_resize(window, width, height);
2413 menu_destroy(struct menu *menu)
2415 widget_destroy(menu->widget);
2416 window_destroy(menu->window);
2421 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2423 struct window *window = data;
2424 struct menu *menu = window->widget->user_data;
2426 /* FIXME: Need more context in this event, at least the input
2427 * device. Or just use wl_callback. And this really needs to
2428 * be a window vfunc that the menu can set. And we need the
2431 menu->func(window->parent, menu->current, window->parent->user_data);
2432 input_ungrab(menu->input);
2436 static const struct wl_shell_surface_listener shell_surface_listener = {
2443 window_get_allocation(struct window *window,
2444 struct rectangle *allocation)
2446 *allocation = window->allocation;
2450 widget_redraw(struct widget *widget)
2452 struct widget *child;
2454 if (widget->redraw_handler)
2455 widget->redraw_handler(widget, widget->user_data);
2456 wl_list_for_each(child, &widget->child_list, link)
2457 widget_redraw(child);
2461 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2463 struct window *window = data;
2465 wl_callback_destroy(callback);
2466 window->redraw_scheduled = 0;
2467 if (window->redraw_needed)
2468 window_schedule_redraw(window);
2471 static const struct wl_callback_listener listener = {
2476 idle_redraw(struct task *task, uint32_t events)
2478 struct window *window = container_of(task, struct window, redraw_task);
2479 struct wl_callback *callback;
2481 if (window->resize_needed)
2482 idle_resize(window);
2484 window_create_surface(window);
2485 widget_redraw(window->widget);
2486 window_flush(window);
2487 window->redraw_needed = 0;
2488 wl_list_init(&window->redraw_task.link);
2490 callback = wl_surface_frame(window->surface);
2491 wl_callback_add_listener(callback, &listener, window);
2495 window_schedule_redraw(struct window *window)
2497 window->redraw_needed = 1;
2498 if (!window->redraw_scheduled) {
2499 window->redraw_task.run = idle_redraw;
2500 display_defer(window->display, &window->redraw_task);
2501 window->redraw_scheduled = 1;
2506 window_set_custom(struct window *window)
2508 window->type = TYPE_CUSTOM;
2512 window_set_fullscreen(struct window *window, int fullscreen)
2514 if (!window->display->shell)
2517 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
2521 window->type = TYPE_FULLSCREEN;
2522 window->saved_allocation = window->allocation;
2523 wl_shell_surface_set_fullscreen(window->shell_surface,
2524 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
2527 window->type = TYPE_TOPLEVEL;
2528 wl_shell_surface_set_toplevel(window->shell_surface);
2529 window_schedule_resize(window,
2530 window->saved_allocation.width,
2531 window->saved_allocation.height);
2536 window_set_maximized(struct window *window, int maximized)
2538 if (!window->display->shell)
2541 if ((window->type == TYPE_MAXIMIZED) == maximized)
2544 if (window->type == TYPE_TOPLEVEL) {
2545 window->saved_allocation = window->allocation;
2546 wl_shell_surface_set_maximized(window->shell_surface, NULL);
2547 window->type = TYPE_MAXIMIZED;
2549 wl_shell_surface_set_toplevel(window->shell_surface);
2550 window->type = TYPE_TOPLEVEL;
2551 window_schedule_resize(window,
2552 window->saved_allocation.width,
2553 window->saved_allocation.height);
2558 window_set_user_data(struct window *window, void *data)
2560 window->user_data = data;
2564 window_get_user_data(struct window *window)
2566 return window->user_data;
2570 window_set_key_handler(struct window *window,
2571 window_key_handler_t handler)
2573 window->key_handler = handler;
2577 window_set_keyboard_focus_handler(struct window *window,
2578 window_keyboard_focus_handler_t handler)
2580 window->keyboard_focus_handler = handler;
2584 window_set_data_handler(struct window *window, window_data_handler_t handler)
2586 window->data_handler = handler;
2590 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2592 window->drop_handler = handler;
2596 window_set_close_handler(struct window *window,
2597 window_close_handler_t handler)
2599 window->close_handler = handler;
2603 window_set_title(struct window *window, const char *title)
2605 free(window->title);
2606 window->title = strdup(title);
2607 if (window->shell_surface)
2608 wl_shell_surface_set_title(window->shell_surface, title);
2612 window_get_title(struct window *window)
2614 return window->title;
2618 window_damage(struct window *window, int32_t x, int32_t y,
2619 int32_t width, int32_t height)
2621 wl_surface_damage(window->surface, x, y, width, height);
2625 surface_enter(void *data,
2626 struct wl_surface *wl_surface, struct wl_output *output)
2631 surface_leave(void *data,
2632 struct wl_surface *wl_surface, struct wl_output *output)
2636 static const struct wl_surface_listener surface_listener = {
2641 static struct window *
2642 window_create_internal(struct display *display, struct window *parent)
2644 struct window *window;
2646 window = malloc(sizeof *window);
2650 memset(window, 0, sizeof *window);
2651 window->display = display;
2652 window->parent = parent;
2653 window->surface = wl_compositor_create_surface(display->compositor);
2654 wl_surface_add_listener(window->surface, &surface_listener, window);
2655 if (display->shell) {
2656 window->shell_surface =
2657 wl_shell_get_shell_surface(display->shell,
2660 wl_shell_surface_set_title(window->shell_surface,
2663 window->allocation.x = 0;
2664 window->allocation.y = 0;
2665 window->allocation.width = 0;
2666 window->allocation.height = 0;
2667 window->saved_allocation = window->allocation;
2668 window->transparent = 1;
2669 window->type = TYPE_NONE;
2670 window->input_region = NULL;
2671 window->opaque_region = NULL;
2674 #ifdef HAVE_CAIRO_EGL
2675 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2677 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2680 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2682 wl_surface_set_user_data(window->surface, window);
2683 wl_list_insert(display->window_list.prev, &window->link);
2684 wl_list_init(&window->redraw_task.link);
2686 if (window->shell_surface) {
2687 wl_shell_surface_set_user_data(window->shell_surface, window);
2688 wl_shell_surface_add_listener(window->shell_surface,
2689 &shell_surface_listener, window);
2696 window_create(struct display *display)
2698 struct window *window;
2700 window = window_create_internal(display, NULL);
2708 window_create_transient(struct display *display, struct window *parent,
2709 int32_t x, int32_t y)
2711 struct window *window;
2713 window = window_create_internal(parent->display, parent);
2717 window->type = TYPE_TRANSIENT;
2722 wl_shell_surface_set_transient(window->shell_surface,
2723 window->parent->shell_surface,
2724 window->x, window->y, 0);
2730 menu_set_item(struct menu *menu, int sy)
2734 next = (sy - 8) / 20;
2735 if (menu->current != next) {
2736 menu->current = next;
2737 widget_schedule_redraw(menu->widget);
2742 menu_motion_handler(struct widget *widget,
2743 struct input *input, uint32_t time,
2744 float x, float y, void *data)
2746 struct menu *menu = data;
2748 if (widget == menu->widget)
2749 menu_set_item(data, y);
2751 return POINTER_LEFT_PTR;
2755 menu_enter_handler(struct widget *widget,
2756 struct input *input, float x, float y, void *data)
2758 struct menu *menu = data;
2760 if (widget == menu->widget)
2761 menu_set_item(data, y);
2763 return POINTER_LEFT_PTR;
2767 menu_leave_handler(struct widget *widget, struct input *input, void *data)
2769 struct menu *menu = data;
2771 if (widget == menu->widget)
2772 menu_set_item(data, -200);
2776 menu_button_handler(struct widget *widget,
2777 struct input *input, uint32_t time,
2778 uint32_t button, uint32_t state, void *data)
2781 struct menu *menu = data;
2783 if (state == 0 && time - menu->time > 500) {
2784 /* Either relase after press-drag-release or
2785 * click-motion-click. */
2786 menu->func(menu->window->parent,
2787 menu->current, menu->window->parent->user_data);
2788 input_ungrab(input);
2794 menu_redraw_handler(struct widget *widget, void *data)
2797 const int32_t r = 3, margin = 3;
2798 struct menu *menu = data;
2799 int32_t width, height, i;
2800 struct window *window = widget->window;
2802 cr = cairo_create(window->cairo_surface);
2803 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2804 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2807 width = window->allocation.width;
2808 height = window->allocation.height;
2809 rounded_rect(cr, 0, 0, width, height, r);
2811 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2812 cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2815 for (i = 0; i < menu->count; i++) {
2816 if (i == menu->current) {
2817 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2818 cairo_rectangle(cr, margin, i * 20 + margin,
2819 width - 2 * margin, 20);
2821 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2822 cairo_move_to(cr, 10, i * 20 + 16);
2823 cairo_show_text(cr, menu->entries[i]);
2825 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2826 cairo_move_to(cr, 10, i * 20 + 16);
2827 cairo_show_text(cr, menu->entries[i]);
2835 window_show_menu(struct display *display,
2836 struct input *input, uint32_t time, struct window *parent,
2837 int32_t x, int32_t y,
2838 menu_func_t func, const char **entries, int count)
2840 struct window *window;
2842 const int32_t margin = 3;
2844 menu = malloc(sizeof *menu);
2848 window = window_create_internal(parent->display, parent);
2852 menu->window = window;
2853 menu->widget = window_add_widget(menu->window, menu);
2854 menu->entries = entries;
2855 menu->count = count;
2859 menu->input = input;
2860 window->type = TYPE_MENU;
2864 input_ungrab(input);
2865 wl_shell_surface_set_popup(window->shell_surface, input->input_device,
2866 display_get_serial(window->display),
2867 window->parent->shell_surface,
2868 window->x, window->y, 0);
2870 widget_set_redraw_handler(menu->widget, menu_redraw_handler);
2871 widget_set_enter_handler(menu->widget, menu_enter_handler);
2872 widget_set_leave_handler(menu->widget, menu_leave_handler);
2873 widget_set_motion_handler(menu->widget, menu_motion_handler);
2874 widget_set_button_handler(menu->widget, menu_button_handler);
2876 input_grab(input, menu->widget, 0);
2877 window_schedule_resize(window, 200, count * 20 + margin * 2);
2881 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2883 window->buffer_type = type;
2888 display_handle_geometry(void *data,
2889 struct wl_output *wl_output,
2892 int physical_height,
2897 struct output *output = data;
2899 output->allocation.x = x;
2900 output->allocation.y = y;
2904 display_handle_mode(void *data,
2905 struct wl_output *wl_output,
2911 struct output *output = data;
2912 struct display *display = output->display;
2914 if (flags & WL_OUTPUT_MODE_CURRENT) {
2915 output->allocation.width = width;
2916 output->allocation.height = height;
2917 if (display->output_configure_handler)
2918 (*display->output_configure_handler)(
2919 output, display->user_data);
2923 static const struct wl_output_listener output_listener = {
2924 display_handle_geometry,
2929 display_add_output(struct display *d, uint32_t id)
2931 struct output *output;
2933 output = malloc(sizeof *output);
2937 memset(output, 0, sizeof *output);
2938 output->display = d;
2940 wl_display_bind(d->display, id, &wl_output_interface);
2941 wl_list_insert(d->output_list.prev, &output->link);
2943 wl_output_add_listener(output->output, &output_listener, output);
2947 output_destroy(struct output *output)
2949 if (output->destroy_handler)
2950 (*output->destroy_handler)(output, output->user_data);
2952 wl_output_destroy(output->output);
2953 wl_list_remove(&output->link);
2958 display_set_output_configure_handler(struct display *display,
2959 display_output_handler_t handler)
2961 struct output *output;
2963 display->output_configure_handler = handler;
2967 wl_list_for_each(output, &display->output_list, link)
2968 (*display->output_configure_handler)(output,
2969 display->user_data);
2973 output_set_user_data(struct output *output, void *data)
2975 output->user_data = data;
2979 output_get_user_data(struct output *output)
2981 return output->user_data;
2985 output_set_destroy_handler(struct output *output,
2986 display_output_handler_t handler)
2988 output->destroy_handler = handler;
2989 /* FIXME: implement this, once we have way to remove outputs */
2993 output_get_allocation(struct output *output, struct rectangle *allocation)
2995 *allocation = output->allocation;
2999 output_get_wl_output(struct output *output)
3001 return output->output;
3005 display_add_input(struct display *d, uint32_t id)
3007 struct input *input;
3009 input = malloc(sizeof *input);
3013 memset(input, 0, sizeof *input);
3015 input->input_device =
3016 wl_display_bind(d->display, id, &wl_input_device_interface);
3017 input->pointer_focus = NULL;
3018 input->keyboard_focus = NULL;
3019 wl_list_insert(d->input_list.prev, &input->link);
3021 wl_input_device_add_listener(input->input_device,
3022 &input_device_listener, input);
3023 wl_input_device_set_user_data(input->input_device, input);
3025 input->data_device =
3026 wl_data_device_manager_get_data_device(d->data_device_manager,
3027 input->input_device);
3028 wl_data_device_add_listener(input->data_device,
3029 &data_device_listener, input);
3033 input_destroy(struct input *input)
3035 input_remove_keyboard_focus(input);
3036 input_remove_pointer_focus(input);
3038 if (input->drag_offer)
3039 data_offer_destroy(input->drag_offer);
3041 if (input->selection_offer)
3042 data_offer_destroy(input->selection_offer);
3044 wl_data_device_destroy(input->data_device);
3045 wl_list_remove(&input->link);
3046 wl_input_device_destroy(input->input_device);
3051 display_handle_global(struct wl_display *display, uint32_t id,
3052 const char *interface, uint32_t version, void *data)
3054 struct display *d = data;
3056 if (strcmp(interface, "wl_compositor") == 0) {
3058 wl_display_bind(display, id, &wl_compositor_interface);
3059 } else if (strcmp(interface, "wl_output") == 0) {
3060 display_add_output(d, id);
3061 } else if (strcmp(interface, "wl_input_device") == 0) {
3062 display_add_input(d, id);
3063 } else if (strcmp(interface, "wl_shell") == 0) {
3064 d->shell = wl_display_bind(display, id, &wl_shell_interface);
3065 } else if (strcmp(interface, "wl_shm") == 0) {
3066 d->shm = wl_display_bind(display, id, &wl_shm_interface);
3067 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3068 d->data_device_manager =
3069 wl_display_bind(display, id,
3070 &wl_data_device_manager_interface);
3075 display_render_frame(struct display *d)
3078 cairo_pattern_t *pattern;
3080 d->frame_radius = 3;
3081 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3082 cr = cairo_create(d->shadow);
3083 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3084 cairo_set_source_rgba(cr, 0, 0, 0, 1);
3085 rounded_rect(cr, 32, 32, 96, 96, d->frame_radius);
3088 blur_surface(d->shadow, 64);
3091 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3092 cr = cairo_create(d->active_frame);
3093 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3095 pattern = cairo_pattern_create_linear(16, 16, 16, 112);
3096 cairo_pattern_add_color_stop_rgb(pattern, 0.0, 1.0, 1.0, 1.0);
3097 cairo_pattern_add_color_stop_rgb(pattern, 0.2, 0.8, 0.8, 0.8);
3098 cairo_set_source(cr, pattern);
3099 cairo_pattern_destroy(pattern);
3101 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
3106 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
3107 cr = cairo_create(d->inactive_frame);
3108 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3109 cairo_set_source_rgba(cr, 0.75, 0.75, 0.75, 1);
3110 rounded_rect(cr, 0, 0, 128, 128, d->frame_radius);
3116 init_xkb(struct display *d)
3118 d->xkb.names.rules = "evdev";
3119 d->xkb.names.model = "pc105";
3120 d->xkb.names.layout = (char *) option_xkb_layout;
3121 d->xkb.names.variant = (char *) option_xkb_variant;
3122 d->xkb.names.options = (char *) option_xkb_options;
3124 d->xkb.context = xkb_context_new();
3125 if (!d->xkb.context) {
3126 fprintf(stderr, "Failed to create XKB context\n");
3131 xkb_map_new_from_names(d->xkb.context, &d->xkb.names);
3132 if (!d->xkb.keymap) {
3133 fprintf(stderr, "Failed to compile keymap\n");
3137 d->xkb.state = xkb_state_new(d->xkb.keymap);
3138 if (!d->xkb.state) {
3139 fprintf(stderr, "Failed to create XKB state\n");
3143 d->xkb.control_mask =
3144 1 << xkb_map_mod_get_index(d->xkb.keymap, "Control");
3146 1 << xkb_map_mod_get_index(d->xkb.keymap, "Mod1");
3148 1 << xkb_map_mod_get_index(d->xkb.keymap, "Shift");
3153 fini_xkb(struct display *display)
3155 xkb_state_unref(display->xkb.state);
3156 xkb_map_unref(display->xkb.keymap);
3157 xkb_context_unref(display->xkb.context);
3160 #ifdef HAVE_CAIRO_EGL
3162 init_egl(struct display *d)
3164 EGLint major, minor;
3167 #ifdef USE_CAIRO_GLESV2
3168 # define GL_BIT EGL_OPENGL_ES2_BIT
3170 # define GL_BIT EGL_OPENGL_BIT
3173 static const EGLint argb_cfg_attribs[] = {
3174 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
3180 EGL_RENDERABLE_TYPE, GL_BIT,
3184 #ifdef USE_CAIRO_GLESV2
3185 static const EGLint context_attribs[] = {
3186 EGL_CONTEXT_CLIENT_VERSION, 2,
3189 EGLint api = EGL_OPENGL_ES_API;
3191 EGLint *context_attribs = NULL;
3192 EGLint api = EGL_OPENGL_API;
3195 d->dpy = eglGetDisplay(d->display);
3196 if (!eglInitialize(d->dpy, &major, &minor)) {
3197 fprintf(stderr, "failed to initialize display\n");
3201 if (!eglBindAPI(api)) {
3202 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3206 if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3207 &d->argb_config, 1, &n) || n != 1) {
3208 fprintf(stderr, "failed to choose argb config\n");
3212 d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3213 EGL_NO_CONTEXT, context_attribs);
3214 if (d->argb_ctx == NULL) {
3215 fprintf(stderr, "failed to create context\n");
3219 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3220 fprintf(stderr, "failed to make context current\n");
3224 #ifdef HAVE_CAIRO_EGL
3225 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3226 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3227 fprintf(stderr, "failed to get cairo egl argb device\n");
3236 fini_egl(struct display *display)
3238 #ifdef HAVE_CAIRO_EGL
3239 cairo_device_destroy(display->argb_device);
3242 eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3245 eglTerminate(display->dpy);
3251 event_mask_update(uint32_t mask, void *data)
3253 struct display *d = data;
3261 handle_display_data(struct task *task, uint32_t events)
3263 struct display *display =
3264 container_of(task, struct display, display_task);
3266 wl_display_iterate(display->display, display->mask);
3270 display_create(int argc, char *argv[])
3274 argc = parse_options(xkb_options,
3275 ARRAY_LENGTH(xkb_options), argc, argv);
3277 d = malloc(sizeof *d);
3281 memset(d, 0, sizeof *d);
3283 d->display = wl_display_connect(NULL);
3284 if (d->display == NULL) {
3285 fprintf(stderr, "failed to create display: %m\n");
3289 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
3290 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
3291 d->display_task.run = handle_display_data;
3292 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
3294 wl_list_init(&d->deferred_list);
3295 wl_list_init(&d->input_list);
3296 wl_list_init(&d->output_list);
3298 /* Set up listener so we'll catch all events. */
3299 wl_display_add_global_listener(d->display,
3300 display_handle_global, d);
3302 /* Process connection events. */
3303 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
3304 #ifdef HAVE_CAIRO_EGL
3305 if (init_egl(d) < 0)
3309 d->image_target_texture_2d =
3310 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3311 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3312 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3316 display_render_frame(d);
3318 wl_list_init(&d->window_list);
3326 display_destroy_outputs(struct display *display)
3329 struct output *output;
3331 wl_list_for_each_safe(output, tmp, &display->output_list, link)
3332 output_destroy(output);
3336 display_destroy_inputs(struct display *display)
3339 struct input *input;
3341 wl_list_for_each_safe(input, tmp, &display->input_list, link)
3342 input_destroy(input);
3346 display_destroy(struct display *display)
3348 if (!wl_list_empty(&display->window_list))
3349 fprintf(stderr, "toytoolkit warning: windows exist.\n");
3351 if (!wl_list_empty(&display->deferred_list))
3352 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3354 display_destroy_outputs(display);
3355 display_destroy_inputs(display);
3359 cairo_surface_destroy(display->active_frame);
3360 cairo_surface_destroy(display->inactive_frame);
3361 cairo_surface_destroy(display->shadow);
3362 destroy_cursors(display);
3364 #ifdef HAVE_CAIRO_EGL
3369 wl_shell_destroy(display->shell);
3372 wl_shm_destroy(display->shm);
3374 if (display->data_device_manager)
3375 wl_data_device_manager_destroy(display->data_device_manager);
3377 wl_compositor_destroy(display->compositor);
3379 close(display->epoll_fd);
3381 wl_display_flush(display->display);
3382 wl_display_disconnect(display->display);
3387 display_set_user_data(struct display *display, void *data)
3389 display->user_data = data;
3393 display_get_user_data(struct display *display)
3395 return display->user_data;
3399 display_get_display(struct display *display)
3401 return display->display;
3405 display_get_output(struct display *display)
3407 return container_of(display->output_list.next, struct output, link);
3410 struct wl_compositor *
3411 display_get_compositor(struct display *display)
3413 return display->compositor;
3417 display_get_serial(struct display *display)
3419 return display->serial;
3423 display_get_egl_display(struct display *d)
3428 struct wl_data_source *
3429 display_create_data_source(struct display *display)
3431 return wl_data_device_manager_create_data_source(display->data_device_manager);
3435 display_get_argb_egl_config(struct display *d)
3437 return d->argb_config;
3441 display_get_shell(struct display *display)
3443 return display->shell;
3447 display_acquire_window_surface(struct display *display,
3448 struct window *window,
3451 #ifdef HAVE_CAIRO_EGL
3452 struct egl_window_surface_data *data;
3453 cairo_device_t *device;
3455 if (!window->cairo_surface)
3457 device = cairo_surface_get_device(window->cairo_surface);
3462 if (device == display->argb_device)
3463 ctx = display->argb_ctx;
3468 data = cairo_surface_get_user_data(window->cairo_surface,
3471 cairo_device_flush(device);
3472 cairo_device_acquire(device);
3473 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
3474 fprintf(stderr, "failed to make surface current\n");
3483 display_release_window_surface(struct display *display,
3484 struct window *window)
3486 #ifdef HAVE_CAIRO_EGL
3487 cairo_device_t *device;
3489 device = cairo_surface_get_device(window->cairo_surface);
3493 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
3494 fprintf(stderr, "failed to make context current\n");
3495 cairo_device_release(device);
3500 display_defer(struct display *display, struct task *task)
3502 wl_list_insert(&display->deferred_list, &task->link);
3506 display_watch_fd(struct display *display,
3507 int fd, uint32_t events, struct task *task)
3509 struct epoll_event ep;
3513 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
3517 display_run(struct display *display)
3520 struct epoll_event ep[16];
3523 display->running = 1;
3525 wl_display_flush(display->display);
3527 while (!wl_list_empty(&display->deferred_list)) {
3528 task = container_of(display->deferred_list.next,
3530 wl_list_remove(&task->link);
3534 if (!display->running)
3537 wl_display_flush(display->display);
3539 count = epoll_wait(display->epoll_fd,
3540 ep, ARRAY_LENGTH(ep), -1);
3541 for (i = 0; i < count; i++) {
3542 task = ep[i].data.ptr;
3543 task->run(task, ep[i].events);
3549 display_exit(struct display *display)
3551 display->running = 0;