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 <gdk-pixbuf/gdk-pixbuf.h>
40 #include <sys/epoll.h>
42 #include <wayland-egl.h>
46 #include <EGL/eglext.h>
52 #include <X11/extensions/XKBcommon.h>
54 #include <linux/input.h>
55 #include <wayland-client.h>
56 #include "cairo-util.h"
61 struct wl_display *display;
62 struct wl_compositor *compositor;
63 struct wl_shell *shell;
65 struct wl_output *output;
66 struct wl_data_device_manager *data_device_manager;
69 EGLConfig premultiplied_argb_config;
72 cairo_device_t *rgb_device;
73 cairo_device_t *argb_device;
77 struct task display_task;
80 struct wl_list deferred_list;
82 struct wl_list window_list;
83 struct wl_list input_list;
84 struct wl_list output_list;
86 cairo_surface_t *active_frame, *inactive_frame, *shadow;
88 cairo_surface_t **pointer_surfaces;
90 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91 PFNEGLCREATEIMAGEKHRPROC create_image;
92 PFNEGLDESTROYIMAGEKHRPROC destroy_image;
94 display_output_handler_t output_configure_handler;
107 struct display *display;
108 struct window *parent;
109 struct wl_surface *surface;
110 struct wl_shell_surface *shell_surface;
112 struct rectangle allocation, saved_allocation, server_allocation;
115 int redraw_scheduled;
116 struct task redraw_task;
117 int minimum_width, minimum_height;
122 struct input *keyboard_device;
124 enum window_buffer_type buffer_type;
127 cairo_surface_t *cairo_surface, *pending_surface;
129 window_resize_handler_t resize_handler;
130 window_redraw_handler_t redraw_handler;
131 window_key_handler_t key_handler;
132 window_button_handler_t button_handler;
133 window_keyboard_focus_handler_t keyboard_focus_handler;
134 window_motion_handler_t motion_handler;
135 window_enter_handler_t enter_handler;
136 window_leave_handler_t leave_handler;
137 window_item_focus_handler_t item_focus_handler;
138 window_data_handler_t data_handler;
139 window_drop_handler_t drop_handler;
141 struct wl_list item_list;
142 struct item *focus_item;
143 uint32_t item_grab_button;
151 struct rectangle allocation;
156 struct display *display;
157 struct wl_input_device *input_device;
158 struct window *pointer_focus;
159 struct window *keyboard_focus;
160 uint32_t current_pointer_image;
162 int32_t x, y, sx, sy;
165 struct wl_data_device *data_device;
166 struct data_offer *drag_offer;
167 struct data_offer *selection_offer;
171 struct display *display;
172 struct wl_output *output;
173 struct rectangle allocation;
176 display_output_handler_t destroy_handler;
181 POINTER_DEFAULT = 100,
185 enum window_location {
187 WINDOW_RESIZING_TOP = 1,
188 WINDOW_RESIZING_BOTTOM = 2,
189 WINDOW_RESIZING_LEFT = 4,
190 WINDOW_RESIZING_TOP_LEFT = 5,
191 WINDOW_RESIZING_BOTTOM_LEFT = 6,
192 WINDOW_RESIZING_RIGHT = 8,
193 WINDOW_RESIZING_TOP_RIGHT = 9,
194 WINDOW_RESIZING_BOTTOM_RIGHT = 10,
195 WINDOW_RESIZING_MASK = 15,
196 WINDOW_EXTERIOR = 16,
197 WINDOW_TITLEBAR = 17,
198 WINDOW_CLIENT_AREA = 18,
201 const char *option_xkb_layout = "us";
202 const char *option_xkb_variant = "";
203 const char *option_xkb_options = "";
205 static const GOptionEntry xkb_option_entries[] = {
206 { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
207 &option_xkb_layout, "XKB Layout" },
208 { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
209 &option_xkb_variant, "XKB Variant" },
210 { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
211 &option_xkb_options, "XKB Options" },
215 static const cairo_user_data_key_t surface_data_key;
216 struct surface_data {
217 struct wl_buffer *buffer;
220 #define MULT(_d,c,a,t) \
221 do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
223 #ifdef HAVE_CAIRO_EGL
225 struct egl_window_surface_data {
226 struct display *display;
227 struct wl_surface *surface;
228 struct wl_egl_window *window;
233 egl_window_surface_data_destroy(void *p)
235 struct egl_window_surface_data *data = p;
236 struct display *d = data->display;
238 eglDestroySurface(d->dpy, data->surf);
239 wl_egl_window_destroy(data->window);
240 data->surface = NULL;
245 static cairo_surface_t *
246 display_create_egl_window_surface(struct display *display,
247 struct wl_surface *surface,
249 struct rectangle *rectangle)
251 cairo_surface_t *cairo_surface;
252 struct egl_window_surface_data *data;
254 const EGLint *attribs;
255 cairo_device_t *device;
257 static const EGLint premul_attribs[] = {
258 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
262 data = malloc(sizeof *data);
266 data->display = display;
267 data->surface = surface;
269 if (flags & SURFACE_OPAQUE) {
270 config = display->rgb_config;
271 device = display->rgb_device;
274 config = display->premultiplied_argb_config;
275 device = display->argb_device;
276 attribs = premul_attribs;
279 data->window = wl_egl_window_create(surface,
283 data->surf = eglCreateWindowSurface(display->dpy, config,
284 data->window, attribs);
286 cairo_surface = cairo_gl_surface_create_for_egl(device,
291 cairo_surface_set_user_data(cairo_surface, &surface_data_key,
292 data, egl_window_surface_data_destroy);
294 return cairo_surface;
297 struct egl_image_surface_data {
298 struct surface_data data;
299 cairo_device_t *device;
302 struct display *display;
303 struct wl_egl_pixmap *pixmap;
307 egl_image_surface_data_destroy(void *p)
309 struct egl_image_surface_data *data = p;
310 struct display *d = data->display;
312 cairo_device_acquire(data->device);
313 glDeleteTextures(1, &data->texture);
314 cairo_device_release(data->device);
316 d->destroy_image(d->dpy, data->image);
317 wl_buffer_destroy(data->data.buffer);
318 wl_egl_pixmap_destroy(data->pixmap);
323 display_get_image_for_egl_image_surface(struct display *display,
324 cairo_surface_t *surface)
326 struct egl_image_surface_data *data;
328 data = cairo_surface_get_user_data (surface, &surface_data_key);
333 static cairo_surface_t *
334 display_create_egl_image_surface(struct display *display,
336 struct rectangle *rectangle)
338 struct egl_image_surface_data *data;
339 EGLDisplay dpy = display->dpy;
340 cairo_surface_t *surface;
341 cairo_content_t content;
343 data = malloc(sizeof *data);
347 data->display = display;
349 data->pixmap = wl_egl_pixmap_create(rectangle->width,
350 rectangle->height, 0);
351 if (data->pixmap == NULL) {
356 if (flags & SURFACE_OPAQUE) {
357 data->device = display->rgb_device;
358 content = CAIRO_CONTENT_COLOR;
360 data->device = display->argb_device;
361 content = CAIRO_CONTENT_COLOR_ALPHA;
364 data->image = display->create_image(dpy, NULL,
365 EGL_NATIVE_PIXMAP_KHR,
366 (EGLClientBuffer) data->pixmap,
368 if (data->image == EGL_NO_IMAGE_KHR) {
369 wl_egl_pixmap_destroy(data->pixmap);
375 wl_egl_pixmap_create_buffer(data->pixmap);
377 cairo_device_acquire(data->device);
378 glGenTextures(1, &data->texture);
379 glBindTexture(GL_TEXTURE_2D, data->texture);
380 display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
381 cairo_device_release(data->device);
383 surface = cairo_gl_surface_create_for_texture(data->device,
389 cairo_surface_set_user_data (surface, &surface_data_key,
390 data, egl_image_surface_data_destroy);
395 static cairo_surface_t *
396 display_create_egl_image_surface_from_file(struct display *display,
397 const char *filename,
398 struct rectangle *rect)
400 cairo_surface_t *surface;
402 GError *error = NULL;
404 unsigned char *pixels, *p, *end;
405 struct egl_image_surface_data *data;
407 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
408 rect->width, rect->height,
413 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
414 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
415 g_object_unref(pixbuf);
420 stride = gdk_pixbuf_get_rowstride(pixbuf);
421 pixels = gdk_pixbuf_get_pixels(pixbuf);
423 for (i = 0; i < rect->height; i++) {
424 p = pixels + i * stride;
425 end = p + rect->width * 4;
429 MULT(p[0], p[0], p[3], t);
430 MULT(p[1], p[1], p[3], t);
431 MULT(p[2], p[2], p[3], t);
437 surface = display_create_egl_image_surface(display, 0, rect);
438 if (surface == NULL) {
439 g_object_unref(pixbuf);
443 data = cairo_surface_get_user_data(surface, &surface_data_key);
445 cairo_device_acquire(display->argb_device);
446 glBindTexture(GL_TEXTURE_2D, data->texture);
447 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
448 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
449 cairo_device_release(display->argb_device);
451 g_object_unref(pixbuf);
459 display_get_buffer_for_surface(struct display *display,
460 cairo_surface_t *surface)
462 struct surface_data *data;
464 data = cairo_surface_get_user_data (surface, &surface_data_key);
469 struct shm_surface_data {
470 struct surface_data data;
476 shm_surface_data_destroy(void *p)
478 struct shm_surface_data *data = p;
480 wl_buffer_destroy(data->data.buffer);
481 munmap(data->map, data->length);
484 static cairo_surface_t *
485 display_create_shm_surface(struct display *display,
486 struct rectangle *rectangle, uint32_t flags)
488 struct shm_surface_data *data;
490 cairo_surface_t *surface;
492 char filename[] = "/tmp/wayland-shm-XXXXXX";
494 data = malloc(sizeof *data);
498 stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
500 data->length = stride * rectangle->height;
501 fd = mkstemp(filename);
503 fprintf(stderr, "open %s failed: %m\n", filename);
506 if (ftruncate(fd, data->length) < 0) {
507 fprintf(stderr, "ftruncate failed: %m\n");
512 data->map = mmap(NULL, data->length,
513 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
516 if (data->map == MAP_FAILED) {
517 fprintf(stderr, "mmap failed: %m\n");
522 surface = cairo_image_surface_create_for_data (data->map,
528 cairo_surface_set_user_data (surface, &surface_data_key,
529 data, shm_surface_data_destroy);
531 if (flags & SURFACE_OPAQUE)
532 format = WL_SHM_FORMAT_XRGB32;
534 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
536 data->data.buffer = wl_shm_create_buffer(display->shm,
547 static cairo_surface_t *
548 display_create_shm_surface_from_file(struct display *display,
549 const char *filename,
550 struct rectangle *rect)
552 cairo_surface_t *surface;
554 GError *error = NULL;
556 unsigned char *pixels, *p, *end, *dest_data;
560 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
561 rect->width, rect->height,
566 if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
567 gdk_pixbuf_get_n_channels(pixbuf) != 4) {
568 g_object_unref(pixbuf);
572 stride = gdk_pixbuf_get_rowstride(pixbuf);
573 pixels = gdk_pixbuf_get_pixels(pixbuf);
575 surface = display_create_shm_surface(display, rect, 0);
576 if (surface == NULL) {
577 g_object_unref(pixbuf);
581 dest_data = cairo_image_surface_get_data (surface);
582 dest_stride = cairo_image_surface_get_stride (surface);
584 for (i = 0; i < rect->height; i++) {
585 d = (uint32_t *) (dest_data + i * dest_stride);
586 p = pixels + i * stride;
587 end = p + rect->width * 4;
590 unsigned char a, r, g, b;
597 *d++ = (a << 24) | (r << 16) | (g << 8) | b;
601 g_object_unref(pixbuf);
607 check_size(struct rectangle *rect)
609 if (rect->width && rect->height)
612 fprintf(stderr, "tried to create surface of "
613 "width: %d, height: %d\n", rect->width, rect->height);
618 display_create_surface(struct display *display,
619 struct wl_surface *surface,
620 struct rectangle *rectangle,
623 if (check_size(rectangle) < 0)
625 #ifdef HAVE_CAIRO_EGL
628 return display_create_egl_window_surface(display,
633 return display_create_egl_image_surface(display,
638 return display_create_shm_surface(display, rectangle, flags);
641 static cairo_surface_t *
642 display_create_surface_from_file(struct display *display,
643 const char *filename,
644 struct rectangle *rectangle)
646 if (check_size(rectangle) < 0)
648 #ifdef HAVE_CAIRO_EGL
650 return display_create_egl_image_surface_from_file(display,
655 return display_create_shm_surface_from_file(display, filename, rectangle);
657 static const struct {
658 const char *filename;
659 int hotspot_x, hotspot_y;
660 } pointer_images[] = {
661 { DATADIR "/wayland/bottom_left_corner.png", 6, 30 },
662 { DATADIR "/wayland/bottom_right_corner.png", 28, 28 },
663 { DATADIR "/wayland/bottom_side.png", 16, 20 },
664 { DATADIR "/wayland/grabbing.png", 20, 17 },
665 { DATADIR "/wayland/left_ptr.png", 10, 5 },
666 { DATADIR "/wayland/left_side.png", 10, 20 },
667 { DATADIR "/wayland/right_side.png", 30, 19 },
668 { DATADIR "/wayland/top_left_corner.png", 8, 8 },
669 { DATADIR "/wayland/top_right_corner.png", 26, 8 },
670 { DATADIR "/wayland/top_side.png", 18, 8 },
671 { DATADIR "/wayland/xterm.png", 15, 15 },
672 { DATADIR "/wayland/hand1.png", 18, 11 }
676 create_pointer_surfaces(struct display *display)
679 const int width = 32, height = 32;
680 struct rectangle rect;
682 count = ARRAY_LENGTH(pointer_images);
683 display->pointer_surfaces =
684 malloc(count * sizeof *display->pointer_surfaces);
686 rect.height = height;
687 for (i = 0; i < count; i++) {
688 display->pointer_surfaces[i] =
689 display_create_surface_from_file(display,
690 pointer_images[i].filename,
692 if (!display->pointer_surfaces[i]) {
693 fprintf(stderr, "Error loading pointer image: %s\n",
694 pointer_images[i].filename);
701 display_get_pointer_surface(struct display *display, int pointer,
702 int *width, int *height,
703 int *hotspot_x, int *hotspot_y)
705 cairo_surface_t *surface;
707 surface = display->pointer_surfaces[pointer];
709 *width = cairo_gl_surface_get_width(surface);
710 *height = cairo_gl_surface_get_height(surface);
712 *width = cairo_image_surface_get_width(surface);
713 *height = cairo_image_surface_get_height(surface);
715 *hotspot_x = pointer_images[pointer].hotspot_x;
716 *hotspot_y = pointer_images[pointer].hotspot_y;
718 return cairo_surface_reference(surface);
722 window_attach_surface(struct window *window);
725 free_surface(void *data, struct wl_callback *callback, uint32_t time)
727 struct window *window = data;
729 wl_callback_destroy(callback);
730 cairo_surface_destroy(window->pending_surface);
731 window->pending_surface = NULL;
732 if (window->cairo_surface)
733 window_attach_surface(window);
736 static const struct wl_callback_listener free_surface_listener = {
741 window_get_resize_dx_dy(struct window *window, int *x, int *y)
743 if (window->resize_edges & WINDOW_RESIZING_LEFT)
744 *x = window->server_allocation.width - window->allocation.width;
748 if (window->resize_edges & WINDOW_RESIZING_TOP)
749 *y = window->server_allocation.height -
750 window->allocation.height;
754 window->resize_edges = 0;
758 window_set_type(struct window *window)
760 if (!window->shell_surface)
763 switch (window->type) {
764 case TYPE_FULLSCREEN:
765 wl_shell_surface_set_fullscreen(window->shell_surface);
768 wl_shell_surface_set_toplevel(window->shell_surface);
771 wl_shell_surface_set_transient(window->shell_surface,
772 window->parent->shell_surface,
773 window->x, window->y, 0);
781 window_attach_surface(struct window *window)
783 struct display *display = window->display;
784 struct wl_buffer *buffer;
785 struct wl_callback *cb;
786 #ifdef HAVE_CAIRO_EGL
787 struct egl_window_surface_data *data;
792 window_set_type(window);
794 switch (window->buffer_type) {
795 #ifdef HAVE_CAIRO_EGL
796 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
797 data = cairo_surface_get_user_data(window->cairo_surface,
800 cairo_gl_surface_swapbuffers(window->cairo_surface);
801 wl_egl_window_get_attached_size(data->window,
802 &window->server_allocation.width,
803 &window->server_allocation.height);
805 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
807 case WINDOW_BUFFER_TYPE_SHM:
808 window_get_resize_dx_dy(window, &x, &y);
810 if (window->pending_surface != NULL)
813 window->pending_surface = window->cairo_surface;
814 window->cairo_surface = NULL;
817 display_get_buffer_for_surface(display,
818 window->pending_surface);
820 wl_surface_attach(window->surface, buffer, x, y);
821 window->server_allocation = window->allocation;
822 cb = wl_display_sync(display->display);
823 wl_callback_add_listener(cb, &free_surface_listener, window);
829 wl_surface_damage(window->surface, 0, 0,
830 window->allocation.width,
831 window->allocation.height);
835 window_flush(struct window *window)
837 if (window->cairo_surface) {
838 switch (window->buffer_type) {
839 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
840 case WINDOW_BUFFER_TYPE_SHM:
841 display_surface_damage(window->display,
842 window->cairo_surface,
844 window->allocation.width,
845 window->allocation.height);
850 window_attach_surface(window);
855 window_set_surface(struct window *window, cairo_surface_t *surface)
857 cairo_surface_reference(surface);
859 if (window->cairo_surface != NULL)
860 cairo_surface_destroy(window->cairo_surface);
862 window->cairo_surface = surface;
865 #ifdef HAVE_CAIRO_EGL
867 window_resize_cairo_window_surface(struct window *window)
869 struct egl_window_surface_data *data;
872 data = cairo_surface_get_user_data(window->cairo_surface,
875 window_get_resize_dx_dy(window, &x, &y),
876 wl_egl_window_resize(data->window,
877 window->allocation.width,
878 window->allocation.height,
881 cairo_gl_surface_set_size(window->cairo_surface,
882 window->allocation.width,
883 window->allocation.height);
888 window_get_display(struct window *window)
890 return window->display;
894 window_create_surface(struct window *window)
896 cairo_surface_t *surface;
899 if (!window->transparent)
900 flags = SURFACE_OPAQUE;
902 switch (window->buffer_type) {
903 #ifdef HAVE_CAIRO_EGL
904 case WINDOW_BUFFER_TYPE_EGL_WINDOW:
905 if (window->cairo_surface) {
906 window_resize_cairo_window_surface(window);
909 surface = display_create_surface(window->display,
911 &window->allocation, flags);
913 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
914 surface = display_create_surface(window->display,
916 &window->allocation, flags);
919 case WINDOW_BUFFER_TYPE_SHM:
920 surface = display_create_shm_surface(window->display,
921 &window->allocation, flags);
928 window_set_surface(window, surface);
929 cairo_surface_destroy(surface);
933 window_draw_menu(struct window *window)
936 int width, height, r = 5;
938 window_create_surface(window);
940 cr = cairo_create(window->cairo_surface);
941 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
942 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
945 width = window->allocation.width;
946 height = window->allocation.height;
947 rounded_rect(cr, r, r, width - r, height - r, r);
948 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
949 cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
955 window_draw_decorations(struct window *window)
958 cairo_text_extents_t extents;
959 cairo_surface_t *frame;
960 int width, height, shadow_dx = 3, shadow_dy = 3;
962 window_create_surface(window);
964 width = window->allocation.width;
965 height = window->allocation.height;
967 cr = cairo_create(window->cairo_surface);
969 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
970 cairo_set_source_rgba(cr, 0, 0, 0, 0);
973 cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
974 tile_mask(cr, window->display->shadow,
975 shadow_dx, shadow_dy, width, height,
976 window->margin + 10 - shadow_dx,
977 window->margin + 10 - shadow_dy);
979 if (window->keyboard_device)
980 frame = window->display->active_frame;
982 frame = window->display->inactive_frame;
984 tile_source(cr, frame, 0, 0, width, height,
985 window->margin + 10, window->margin + 50);
987 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
988 cairo_set_font_size(cr, 14);
989 cairo_text_extents(cr, window->title, &extents);
990 cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
991 if (window->keyboard_device)
992 cairo_set_source_rgb(cr, 0, 0, 0);
994 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
995 cairo_show_text(cr, window->title);
1001 window_destroy(struct window *window)
1003 struct display *display = window->display;
1004 struct input *input;
1006 if (window->redraw_scheduled)
1007 wl_list_remove(&window->redraw_task.link);
1009 wl_list_for_each(input, &display->input_list, link) {
1010 if (input->pointer_focus == window)
1011 input->pointer_focus = NULL;
1012 if (input->keyboard_focus == window)
1013 input->keyboard_focus = NULL;
1016 if (window->shell_surface)
1017 wl_shell_surface_destroy(window->shell_surface);
1018 wl_surface_destroy(window->surface);
1019 wl_list_remove(&window->link);
1023 static struct item *
1024 window_find_item(struct window *window, int32_t x, int32_t y)
1028 wl_list_for_each(item, &window->item_list, link) {
1029 if (item->allocation.x <= x &&
1030 x < item->allocation.x + item->allocation.width &&
1031 item->allocation.y <= y &&
1032 y < item->allocation.y + item->allocation.height) {
1041 window_add_item(struct window *window, void *data)
1045 item = malloc(sizeof *item);
1046 memset(item, 0, sizeof *item);
1047 item->user_data = data;
1048 wl_list_insert(window->item_list.prev, &item->link);
1054 window_for_each_item(struct window *window, item_func_t func, void *data)
1058 wl_list_for_each(item, &window->item_list, link)
1063 window_get_focus_item(struct window *window)
1065 return window->focus_item;
1069 item_get_allocation(struct item *item, struct rectangle *allocation)
1071 *allocation = item->allocation;
1075 item_set_allocation(struct item *item,
1076 int32_t x, int32_t y, int32_t width, int32_t height)
1078 item->allocation.x = x;
1079 item->allocation.y = y;
1080 item->allocation.width = width;
1081 item->allocation.height = height;
1085 item_get_user_data(struct item *item)
1087 return item->user_data;
1091 window_draw(struct window *window)
1094 window_draw_menu(window);
1095 else if (!window->decoration)
1096 window_create_surface(window);
1098 window_draw_decorations(window);
1102 window_get_surface(struct window *window)
1104 return cairo_surface_reference(window->cairo_surface);
1108 window_get_wl_surface(struct window *window)
1110 return window->surface;
1113 struct wl_shell_surface *
1114 window_get_wl_shell_surface(struct window *window)
1116 return window->shell_surface;
1120 get_pointer_location(struct window *window, int32_t x, int32_t y)
1122 int vlocation, hlocation, location;
1123 const int grip_size = 8;
1125 if (!window->decoration)
1126 return WINDOW_CLIENT_AREA;
1128 if (x < window->margin)
1129 hlocation = WINDOW_EXTERIOR;
1130 else if (window->margin <= x && x < window->margin + grip_size)
1131 hlocation = WINDOW_RESIZING_LEFT;
1132 else if (x < window->allocation.width - window->margin - grip_size)
1133 hlocation = WINDOW_INTERIOR;
1134 else if (x < window->allocation.width - window->margin)
1135 hlocation = WINDOW_RESIZING_RIGHT;
1137 hlocation = WINDOW_EXTERIOR;
1139 if (y < window->margin)
1140 vlocation = WINDOW_EXTERIOR;
1141 else if (window->margin <= y && y < window->margin + grip_size)
1142 vlocation = WINDOW_RESIZING_TOP;
1143 else if (y < window->allocation.height - window->margin - grip_size)
1144 vlocation = WINDOW_INTERIOR;
1145 else if (y < window->allocation.height - window->margin)
1146 vlocation = WINDOW_RESIZING_BOTTOM;
1148 vlocation = WINDOW_EXTERIOR;
1150 location = vlocation | hlocation;
1151 if (location & WINDOW_EXTERIOR)
1152 location = WINDOW_EXTERIOR;
1153 if (location == WINDOW_INTERIOR && y < window->margin + 50)
1154 location = WINDOW_TITLEBAR;
1155 else if (location == WINDOW_INTERIOR)
1156 location = WINDOW_CLIENT_AREA;
1162 input_get_pointer_image_for_location(struct input *input, int pointer)
1166 location = get_pointer_location(input->pointer_focus,
1167 input->sx, input->sy);
1169 case WINDOW_RESIZING_TOP:
1171 case WINDOW_RESIZING_BOTTOM:
1172 return POINTER_BOTTOM;
1173 case WINDOW_RESIZING_LEFT:
1174 return POINTER_LEFT;
1175 case WINDOW_RESIZING_RIGHT:
1176 return POINTER_RIGHT;
1177 case WINDOW_RESIZING_TOP_LEFT:
1178 return POINTER_TOP_LEFT;
1179 case WINDOW_RESIZING_TOP_RIGHT:
1180 return POINTER_TOP_RIGHT;
1181 case WINDOW_RESIZING_BOTTOM_LEFT:
1182 return POINTER_BOTTOM_LEFT;
1183 case WINDOW_RESIZING_BOTTOM_RIGHT:
1184 return POINTER_BOTTOM_RIGHT;
1185 case WINDOW_EXTERIOR:
1186 case WINDOW_TITLEBAR:
1187 return POINTER_LEFT_PTR;
1194 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1196 struct display *display = input->display;
1197 struct wl_buffer *buffer;
1198 cairo_surface_t *surface;
1200 if (pointer == input->current_pointer_image)
1203 input->current_pointer_image = pointer;
1204 surface = display->pointer_surfaces[pointer];
1209 buffer = display_get_buffer_for_surface(display, surface);
1210 wl_input_device_attach(input->input_device, time, buffer,
1211 pointer_images[pointer].hotspot_x,
1212 pointer_images[pointer].hotspot_y);
1216 window_set_focus_item(struct window *window, struct item *focus)
1220 if (focus == window->focus_item)
1223 window->focus_item = focus;
1224 data = focus ? focus->user_data : NULL;
1225 if (window->item_focus_handler)
1226 window->item_focus_handler(window, focus, data);
1230 window_handle_motion(void *data, struct wl_input_device *input_device,
1232 int32_t x, int32_t y, int32_t sx, int32_t sy)
1234 struct input *input = data;
1235 struct window *window = input->pointer_focus;
1237 int pointer = POINTER_LEFT_PTR;
1244 if (!window->focus_item || !window->item_grab_button) {
1245 item = window_find_item(window, sx, sy);
1246 window_set_focus_item(window, item);
1249 if (window->motion_handler)
1250 pointer = (*window->motion_handler)(window, input, time,
1254 pointer = input_get_pointer_image_for_location(input, pointer);
1255 input_set_pointer_image(input, time, pointer);
1259 window_handle_button(void *data,
1260 struct wl_input_device *input_device,
1261 uint32_t time, uint32_t button, uint32_t state)
1263 struct input *input = data;
1264 struct window *window = input->pointer_focus;
1268 if (window->focus_item && window->item_grab_button == 0 && state)
1269 window->item_grab_button = button;
1271 location = get_pointer_location(window, input->sx, input->sy);
1273 if (window->display->shell &&
1274 button == BTN_LEFT && state == 1) {
1276 case WINDOW_TITLEBAR:
1277 if (!window->shell_surface)
1279 input_set_pointer_image(input, time, POINTER_DRAGGING);
1280 wl_shell_surface_move(window->shell_surface,
1281 input_device, time);
1283 case WINDOW_RESIZING_TOP:
1284 case WINDOW_RESIZING_BOTTOM:
1285 case WINDOW_RESIZING_LEFT:
1286 case WINDOW_RESIZING_RIGHT:
1287 case WINDOW_RESIZING_TOP_LEFT:
1288 case WINDOW_RESIZING_TOP_RIGHT:
1289 case WINDOW_RESIZING_BOTTOM_LEFT:
1290 case WINDOW_RESIZING_BOTTOM_RIGHT:
1291 if (!window->shell_surface)
1293 wl_shell_surface_resize(window->shell_surface,
1297 case WINDOW_CLIENT_AREA:
1298 if (window->button_handler)
1299 (*window->button_handler)(window,
1306 if (window->button_handler)
1307 (*window->button_handler)(window,
1313 if (window->focus_item &&
1314 window->item_grab_button == button && !state) {
1315 window->item_grab_button = 0;
1316 item = window_find_item(window, input->sx, input->sy);
1317 window_set_focus_item(window, item);
1322 window_handle_key(void *data, struct wl_input_device *input_device,
1323 uint32_t time, uint32_t key, uint32_t state)
1325 struct input *input = data;
1326 struct window *window = input->keyboard_focus;
1327 struct display *d = input->display;
1328 uint32_t code, sym, level;
1330 code = key + d->xkb->min_key_code;
1331 if (!window || window->keyboard_device != input)
1335 if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
1336 XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1339 sym = XkbKeySymEntry(d->xkb, code, level, 0);
1342 input->modifiers |= d->xkb->map->modmap[code];
1344 input->modifiers &= ~d->xkb->map->modmap[code];
1346 if (window->key_handler)
1347 (*window->key_handler)(window, input, time, key, sym, state,
1352 window_handle_pointer_focus(void *data,
1353 struct wl_input_device *input_device,
1354 uint32_t time, struct wl_surface *surface,
1355 int32_t x, int32_t y, int32_t sx, int32_t sy)
1357 struct input *input = data;
1358 struct window *window;
1362 window = input->pointer_focus;
1363 if (window && window->surface != surface) {
1364 window_set_focus_item(window, NULL);
1366 if (window->leave_handler)
1367 window->leave_handler(window, input,
1368 time, window->user_data);
1369 input->pointer_focus = NULL;
1370 input->current_pointer_image = POINTER_UNSET;
1374 input->pointer_focus = wl_surface_get_user_data(surface);
1375 window = input->pointer_focus;
1382 pointer = POINTER_LEFT_PTR;
1383 if (window->enter_handler)
1384 pointer = window->enter_handler(window, input,
1388 item = window_find_item(window, x, y);
1389 window_set_focus_item(window, item);
1391 pointer = input_get_pointer_image_for_location(input, pointer);
1392 input_set_pointer_image(input, time, pointer);
1397 window_handle_keyboard_focus(void *data,
1398 struct wl_input_device *input_device,
1400 struct wl_surface *surface,
1401 struct wl_array *keys)
1403 struct input *input = data;
1404 struct window *window = input->keyboard_focus;
1405 struct display *d = input->display;
1409 window->keyboard_device = NULL;
1410 if (window->keyboard_focus_handler)
1411 (*window->keyboard_focus_handler)(window, NULL,
1416 input->keyboard_focus = wl_surface_get_user_data(surface);
1418 input->keyboard_focus = NULL;
1420 end = keys->data + keys->size;
1421 input->modifiers = 0;
1422 for (k = keys->data; k < end; k++)
1423 input->modifiers |= d->xkb->map->modmap[*k];
1425 window = input->keyboard_focus;
1427 window->keyboard_device = input;
1428 if (window->keyboard_focus_handler)
1429 (*window->keyboard_focus_handler)(window,
1430 window->keyboard_device,
1435 static const struct wl_input_device_listener input_device_listener = {
1436 window_handle_motion,
1437 window_handle_button,
1439 window_handle_pointer_focus,
1440 window_handle_keyboard_focus,
1444 input_get_position(struct input *input, int32_t *x, int32_t *y)
1450 struct wl_input_device *
1451 input_get_input_device(struct input *input)
1453 return input->input_device;
1457 input_get_modifiers(struct input *input)
1459 return input->modifiers;
1463 struct wl_data_offer *offer;
1464 struct input *input;
1465 struct wl_array types;
1468 struct task io_task;
1476 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1478 struct data_offer *offer = data;
1481 p = wl_array_add(&offer->types, sizeof *p);
1485 static const struct wl_data_offer_listener data_offer_listener = {
1490 data_offer_destroy(struct data_offer *offer)
1495 if (offer->refcount == 0) {
1496 wl_data_offer_destroy(offer->offer);
1497 for (p = offer->types.data; *p; p++)
1499 wl_array_release(&offer->types);
1505 data_device_data_offer(void *data,
1506 struct wl_data_device *data_device, uint32_t id)
1508 struct data_offer *offer;
1510 offer = malloc(sizeof *offer);
1512 wl_array_init(&offer->types);
1513 offer->refcount = 1;
1514 offer->input = data;
1516 /* FIXME: Generate typesafe wrappers for this */
1517 offer->offer = (struct wl_data_offer *)
1518 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1519 id, &wl_data_offer_interface);
1521 wl_data_offer_add_listener(offer->offer,
1522 &data_offer_listener, offer);
1526 data_device_enter(void *data, struct wl_data_device *data_device,
1527 uint32_t time, struct wl_surface *surface,
1528 int32_t x, int32_t y, struct wl_data_offer *offer)
1530 struct input *input = data;
1531 struct window *window;
1534 input->drag_offer = wl_data_offer_get_user_data(offer);
1535 window = wl_surface_get_user_data(surface);
1536 input->pointer_focus = window;
1538 p = wl_array_add(&input->drag_offer->types, sizeof *p);
1541 window = input->pointer_focus;
1542 if (window->data_handler)
1543 window->data_handler(window, input, time, x, y,
1544 input->drag_offer->types.data,
1549 data_device_leave(void *data, struct wl_data_device *data_device)
1551 struct input *input = data;
1553 data_offer_destroy(input->drag_offer);
1554 input->drag_offer = NULL;
1558 data_device_motion(void *data, struct wl_data_device *data_device,
1559 uint32_t time, int32_t x, int32_t y)
1561 struct input *input = data;
1562 struct window *window = input->pointer_focus;
1567 if (window->data_handler)
1568 window->data_handler(window, input, time, x, y,
1569 input->drag_offer->types.data,
1574 data_device_drop(void *data, struct wl_data_device *data_device)
1576 struct input *input = data;
1577 struct window *window = input->pointer_focus;
1579 if (window->drop_handler)
1580 window->drop_handler(window, input,
1581 input->sx, input->sy, window->user_data);
1585 data_device_selection(void *data,
1586 struct wl_data_device *wl_data_device,
1587 struct wl_data_offer *offer)
1589 struct input *input = data;
1592 if (input->selection_offer)
1593 data_offer_destroy(input->selection_offer);
1595 input->selection_offer = wl_data_offer_get_user_data(offer);
1596 p = wl_array_add(&input->selection_offer->types, sizeof *p);
1600 static const struct wl_data_device_listener data_device_listener = {
1601 data_device_data_offer,
1606 data_device_selection
1609 struct wl_data_device *
1610 input_get_data_device(struct input *input)
1612 return input->data_device;
1616 input_set_selection(struct input *input,
1617 struct wl_data_source *source, uint32_t time)
1619 wl_data_device_set_selection(input->data_device, source, time);
1623 input_accept(struct input *input, uint32_t time, const char *type)
1625 wl_data_offer_accept(input->drag_offer->offer, time, type);
1629 offer_io_func(struct task *task, uint32_t events)
1631 struct data_offer *offer =
1632 container_of(task, struct data_offer, io_task);
1636 len = read(offer->fd, buffer, sizeof buffer);
1637 offer->func(buffer, len,
1638 offer->x, offer->y, offer->user_data);
1642 data_offer_destroy(offer);
1647 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1648 data_func_t func, void *user_data)
1652 pipe2(p, O_CLOEXEC);
1653 wl_data_offer_receive(offer->offer, mime_type, p[1]);
1656 offer->io_task.run = offer_io_func;
1660 offer->user_data = user_data;
1662 display_watch_fd(offer->input->display,
1663 offer->fd, EPOLLIN, &offer->io_task);
1667 input_receive_drag_data(struct input *input, const char *mime_type,
1668 data_func_t func, void *data)
1670 data_offer_receive_data(input->drag_offer, mime_type, func, data);
1671 input->drag_offer->x = input->sx;
1672 input->drag_offer->y = input->sy;
1676 input_receive_selection_data(struct input *input, const char *mime_type,
1677 data_func_t func, void *data)
1681 if (input->selection_offer == NULL)
1684 for (p = input->selection_offer->types.data; *p; p++)
1685 if (strcmp(mime_type, *p) == 0)
1691 data_offer_receive_data(input->selection_offer,
1692 mime_type, func, data);
1697 window_move(struct window *window, struct input *input, uint32_t time)
1699 if (!window->shell_surface)
1702 wl_shell_surface_move(window->shell_surface,
1703 input->input_device, time);
1707 handle_configure(void *data, struct wl_shell_surface *shell_surface,
1708 uint32_t time, uint32_t edges,
1709 int32_t width, int32_t height)
1711 struct window *window = data;
1712 int32_t child_width, child_height;
1714 /* FIXME: this is probably the wrong place to check for width
1715 * or height <= 0, but it prevents the compositor from crashing
1717 if (width <= 0 || height <= 0)
1720 window->resize_edges = edges;
1722 if (window->resize_handler) {
1723 child_width = width - 20 - window->margin * 2;
1724 child_height = height - 60 - window->margin * 2;
1726 (*window->resize_handler)(window,
1727 child_width, child_height,
1730 window->allocation.width = width;
1731 window->allocation.height = height;
1733 if (window->redraw_handler)
1734 window_schedule_redraw(window);
1738 static const struct wl_shell_surface_listener shell_surface_listener = {
1743 window_get_allocation(struct window *window,
1744 struct rectangle *allocation)
1746 *allocation = window->allocation;
1750 window_get_child_allocation(struct window *window,
1751 struct rectangle *allocation)
1753 if (!window->decoration) {
1754 *allocation = window->allocation;
1756 allocation->x = window->margin + 10;
1757 allocation->y = window->margin + 50;
1759 window->allocation.width - 20 - window->margin * 2;
1760 allocation->height =
1761 window->allocation.height - 60 - window->margin * 2;
1766 window_set_child_size(struct window *window, int32_t width, int32_t height)
1768 if (window->decoration) {
1769 window->allocation.x = 20 + window->margin;
1770 window->allocation.y = 60 + window->margin;
1771 window->allocation.width = width + 20 + window->margin * 2;
1772 window->allocation.height = height + 60 + window->margin * 2;
1774 window->allocation.x = 0;
1775 window->allocation.y = 0;
1776 window->allocation.width = width;
1777 window->allocation.height = height;
1782 idle_redraw(struct task *task, uint32_t events)
1784 struct window *window =
1785 container_of(task, struct window, redraw_task);
1787 window->redraw_handler(window, window->user_data);
1788 window->redraw_scheduled = 0;
1792 window_schedule_redraw(struct window *window)
1794 if (!window->redraw_scheduled) {
1795 window->redraw_task.run = idle_redraw;
1796 display_defer(window->display, &window->redraw_task);
1797 window->redraw_scheduled = 1;
1802 window_set_custom(struct window *window)
1804 window->type = TYPE_CUSTOM;
1808 window_set_fullscreen(struct window *window, int fullscreen)
1810 int32_t width, height;
1811 struct output *output;
1813 if ((window->type == TYPE_FULLSCREEN) == fullscreen)
1817 output = display_get_output(window->display);
1818 window->type = TYPE_FULLSCREEN;
1819 window->saved_allocation = window->allocation;
1820 width = output->allocation.width;
1821 height = output->allocation.height;
1822 window->decoration = 0;
1824 window->type = TYPE_TOPLEVEL;
1825 width = window->saved_allocation.width - 20 - window->margin * 2;
1826 height = window->saved_allocation.height - 60 - window->margin * 2;
1827 window->decoration = 1;
1830 (*window->resize_handler)(window, width, height, window->user_data);
1834 window_set_decoration(struct window *window, int decoration)
1836 window->decoration = decoration;
1840 window_set_user_data(struct window *window, void *data)
1842 window->user_data = data;
1846 window_get_user_data(struct window *window)
1848 return window->user_data;
1852 window_set_resize_handler(struct window *window,
1853 window_resize_handler_t handler)
1855 window->resize_handler = handler;
1859 window_set_redraw_handler(struct window *window,
1860 window_redraw_handler_t handler)
1862 window->redraw_handler = handler;
1866 window_set_key_handler(struct window *window,
1867 window_key_handler_t handler)
1869 window->key_handler = handler;
1873 window_set_button_handler(struct window *window,
1874 window_button_handler_t handler)
1876 window->button_handler = handler;
1880 window_set_motion_handler(struct window *window,
1881 window_motion_handler_t handler)
1883 window->motion_handler = handler;
1887 window_set_enter_handler(struct window *window,
1888 window_enter_handler_t handler)
1890 window->enter_handler = handler;
1894 window_set_leave_handler(struct window *window,
1895 window_leave_handler_t handler)
1897 window->leave_handler = handler;
1901 window_set_keyboard_focus_handler(struct window *window,
1902 window_keyboard_focus_handler_t handler)
1904 window->keyboard_focus_handler = handler;
1908 window_set_item_focus_handler(struct window *window,
1909 window_item_focus_handler_t handler)
1911 window->item_focus_handler = handler;
1915 window_set_data_handler(struct window *window, window_data_handler_t handler)
1917 window->data_handler = handler;
1921 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
1923 window->drop_handler = handler;
1927 window_set_transparent(struct window *window, int transparent)
1929 window->transparent = transparent;
1933 window_set_title(struct window *window, const char *title)
1935 free(window->title);
1936 window->title = strdup(title);
1940 window_get_title(struct window *window)
1942 return window->title;
1946 display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
1947 int32_t x, int32_t y, int32_t width, int32_t height)
1949 struct wl_buffer *buffer;
1951 buffer = display_get_buffer_for_surface(display, cairo_surface);
1953 wl_buffer_damage(buffer, x, y, width, height);
1957 window_damage(struct window *window, int32_t x, int32_t y,
1958 int32_t width, int32_t height)
1960 wl_surface_damage(window->surface, x, y, width, height);
1963 static struct window *
1964 window_create_internal(struct display *display, struct window *parent,
1965 int32_t width, int32_t height)
1967 struct window *window;
1969 window = malloc(sizeof *window);
1973 memset(window, 0, sizeof *window);
1974 window->display = display;
1975 window->parent = parent;
1976 window->surface = wl_compositor_create_surface(display->compositor);
1977 if (display->shell) {
1978 window->shell_surface =
1979 wl_shell_get_shell_surface(display->shell,
1982 window->allocation.x = 0;
1983 window->allocation.y = 0;
1984 window->allocation.width = width;
1985 window->allocation.height = height;
1986 window->saved_allocation = window->allocation;
1987 window->margin = 16;
1988 window->decoration = 1;
1989 window->transparent = 1;
1990 wl_list_init(&window->item_list);
1993 #ifdef HAVE_CAIRO_EGL
1994 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
1995 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
1997 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2000 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2002 wl_surface_set_user_data(window->surface, window);
2003 wl_list_insert(display->window_list.prev, &window->link);
2005 if (window->shell_surface) {
2006 wl_shell_surface_set_user_data(window->shell_surface, window);
2007 wl_shell_surface_add_listener(window->shell_surface,
2008 &shell_surface_listener, window);
2015 window_create(struct display *display, int32_t width, int32_t height)
2017 struct window *window;
2019 window = window_create_internal(display, NULL, width, height);
2027 window_create_transient(struct display *display, struct window *parent,
2028 int32_t x, int32_t y, int32_t width, int32_t height)
2030 struct window *window;
2032 window = window_create_internal(parent->display,
2033 parent, width, height);
2037 window->type = TYPE_TRANSIENT;
2045 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2047 window->buffer_type = type;
2052 display_handle_geometry(void *data,
2053 struct wl_output *wl_output,
2056 int physical_height,
2061 struct output *output = data;
2063 output->allocation.x = x;
2064 output->allocation.y = y;
2068 display_handle_mode(void *data,
2069 struct wl_output *wl_output,
2075 struct output *output = data;
2076 struct display *display = output->display;
2078 if (flags & WL_OUTPUT_MODE_CURRENT) {
2079 output->allocation.width = width;
2080 output->allocation.height = height;
2081 if (display->output_configure_handler)
2082 (*display->output_configure_handler)(
2083 output, display->user_data);
2087 static const struct wl_output_listener output_listener = {
2088 display_handle_geometry,
2093 display_add_output(struct display *d, uint32_t id)
2095 struct output *output;
2097 output = malloc(sizeof *output);
2101 memset(output, 0, sizeof *output);
2102 output->display = d;
2104 wl_display_bind(d->display, id, &wl_output_interface);
2105 wl_list_insert(d->output_list.prev, &output->link);
2107 wl_output_add_listener(output->output, &output_listener, output);
2111 display_set_output_configure_handler(struct display *display,
2112 display_output_handler_t handler)
2114 struct output *output;
2116 display->output_configure_handler = handler;
2120 wl_list_for_each(output, &display->output_list, link)
2121 (*display->output_configure_handler)(output,
2122 display->user_data);
2126 output_set_user_data(struct output *output, void *data)
2128 output->user_data = data;
2132 output_get_user_data(struct output *output)
2134 return output->user_data;
2138 output_set_destroy_handler(struct output *output,
2139 display_output_handler_t handler)
2141 output->destroy_handler = handler;
2142 /* FIXME: implement this, once we have way to remove outputs */
2146 output_get_allocation(struct output *output, struct rectangle *allocation)
2148 *allocation = output->allocation;
2152 output_get_wl_output(struct output *output)
2154 return output->output;
2158 display_add_input(struct display *d, uint32_t id)
2160 struct input *input;
2162 input = malloc(sizeof *input);
2166 memset(input, 0, sizeof *input);
2168 input->input_device =
2169 wl_display_bind(d->display, id, &wl_input_device_interface);
2170 input->pointer_focus = NULL;
2171 input->keyboard_focus = NULL;
2172 wl_list_insert(d->input_list.prev, &input->link);
2174 wl_input_device_add_listener(input->input_device,
2175 &input_device_listener, input);
2176 wl_input_device_set_user_data(input->input_device, input);
2178 input->data_device =
2179 wl_data_device_manager_get_data_device(d->data_device_manager,
2180 input->input_device);
2181 wl_data_device_add_listener(input->data_device,
2182 &data_device_listener, input);
2186 display_handle_global(struct wl_display *display, uint32_t id,
2187 const char *interface, uint32_t version, void *data)
2189 struct display *d = data;
2191 if (strcmp(interface, "wl_compositor") == 0) {
2193 wl_display_bind(display, id, &wl_compositor_interface);
2194 } else if (strcmp(interface, "wl_output") == 0) {
2195 display_add_output(d, id);
2196 } else if (strcmp(interface, "wl_input_device") == 0) {
2197 display_add_input(d, id);
2198 } else if (strcmp(interface, "wl_shell") == 0) {
2199 d->shell = wl_display_bind(display, id, &wl_shell_interface);
2200 } else if (strcmp(interface, "wl_shm") == 0) {
2201 d->shm = wl_display_bind(display, id, &wl_shm_interface);
2202 } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2203 d->data_device_manager =
2204 wl_display_bind(display, id,
2205 &wl_data_device_manager_interface);
2210 display_render_frame(struct display *d)
2215 d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2216 cr = cairo_create(d->shadow);
2217 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2218 cairo_set_source_rgba(cr, 0, 0, 0, 1);
2219 rounded_rect(cr, 16, 16, 112, 112, radius);
2222 blur_surface(d->shadow, 64);
2225 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2226 cr = cairo_create(d->active_frame);
2227 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2228 cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2229 rounded_rect(cr, 16, 16, 112, 112, radius);
2234 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2235 cr = cairo_create(d->inactive_frame);
2236 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2237 cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2238 rounded_rect(cr, 16, 16, 112, 112, radius);
2244 init_xkb(struct display *d)
2246 struct xkb_rule_names names;
2248 names.rules = "evdev";
2249 names.model = "pc105";
2250 names.layout = option_xkb_layout;
2251 names.variant = option_xkb_variant;
2252 names.options = option_xkb_options;
2254 d->xkb = xkb_compile_keymap_from_rules(&names);
2256 fprintf(stderr, "Failed to compile keymap\n");
2262 init_egl(struct display *d)
2264 EGLint major, minor;
2267 static const EGLint premul_argb_cfg_attribs[] = {
2269 EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2270 EGL_VG_ALPHA_FORMAT_PRE_BIT,
2276 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2280 static const EGLint rgb_cfg_attribs[] = {
2281 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2287 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2291 d->dpy = eglGetDisplay(d->display);
2292 if (!eglInitialize(d->dpy, &major, &minor)) {
2293 fprintf(stderr, "failed to initialize display\n");
2297 if (!eglBindAPI(EGL_OPENGL_API)) {
2298 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2302 if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
2303 &d->premultiplied_argb_config, 1, &n) || n != 1) {
2304 fprintf(stderr, "failed to choose premul argb config\n");
2308 if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2309 &d->rgb_config, 1, &n) || n != 1) {
2310 fprintf(stderr, "failed to choose rgb config\n");
2314 d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2315 if (d->rgb_ctx == NULL) {
2316 fprintf(stderr, "failed to create context\n");
2319 d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2320 EGL_NO_CONTEXT, NULL);
2321 if (d->argb_ctx == NULL) {
2322 fprintf(stderr, "failed to create context\n");
2326 if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
2327 fprintf(stderr, "failed to make context current\n");
2331 #ifdef HAVE_CAIRO_EGL
2332 d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2333 if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
2334 fprintf(stderr, "failed to get cairo egl device\n");
2337 d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2338 if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2339 fprintf(stderr, "failed to get cairo egl argb device\n");
2348 event_mask_update(uint32_t mask, void *data)
2350 struct display *d = data;
2358 handle_display_data(struct task *task, uint32_t events)
2360 struct display *display =
2361 container_of(task, struct display, display_task);
2363 wl_display_iterate(display->display, display->mask);
2367 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
2370 GOptionContext *context;
2371 GOptionGroup *xkb_option_group;
2376 context = g_option_context_new(NULL);
2378 g_option_context_add_main_entries(context, option_entries, "Wayland View");
2380 xkb_option_group = g_option_group_new("xkb",
2382 "Show all XKB options",
2384 g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2385 g_option_context_add_group (context, xkb_option_group);
2387 if (!g_option_context_parse(context, argc, argv, &error)) {
2388 fprintf(stderr, "option parsing failed: %s\n", error->message);
2392 g_option_context_free(context);
2394 d = malloc(sizeof *d);
2398 memset(d, 0, sizeof *d);
2400 d->display = wl_display_connect(NULL);
2401 if (d->display == NULL) {
2402 fprintf(stderr, "failed to create display: %m\n");
2406 d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2407 d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2408 d->display_task.run = handle_display_data;
2409 display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2411 wl_list_init(&d->deferred_list);
2412 wl_list_init(&d->input_list);
2413 wl_list_init(&d->output_list);
2415 /* Set up listener so we'll catch all events. */
2416 wl_display_add_global_listener(d->display,
2417 display_handle_global, d);
2419 /* Process connection events. */
2420 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
2421 if (init_egl(d) < 0)
2424 d->image_target_texture_2d =
2425 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2426 d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2427 d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2429 create_pointer_surfaces(d);
2431 display_render_frame(d);
2433 wl_list_init(&d->window_list);
2441 display_set_user_data(struct display *display, void *data)
2443 display->user_data = data;
2447 display_get_user_data(struct display *display)
2449 return display->user_data;
2453 display_get_display(struct display *display)
2455 return display->display;
2459 display_get_output(struct display *display)
2461 return container_of(display->output_list.next, struct output, link);
2464 struct wl_compositor *
2465 display_get_compositor(struct display *display)
2467 return display->compositor;
2471 display_get_egl_display(struct display *d)
2476 struct wl_data_source *
2477 display_create_data_source(struct display *display)
2479 return wl_data_device_manager_create_data_source(display->data_device_manager);
2483 display_get_rgb_egl_config(struct display *d)
2485 return d->rgb_config;
2489 display_get_argb_egl_config(struct display *d)
2491 return d->premultiplied_argb_config;
2495 display_get_shell(struct display *display)
2497 return display->shell;
2501 display_acquire_window_surface(struct display *display,
2502 struct window *window,
2505 #ifdef HAVE_CAIRO_EGL
2506 struct egl_window_surface_data *data;
2507 cairo_device_t *device;
2509 if (!window->cairo_surface)
2511 device = cairo_surface_get_device(window->cairo_surface);
2516 if (device == display->rgb_device)
2517 ctx = display->rgb_ctx;
2518 else if (device == display->argb_device)
2519 ctx = display->argb_ctx;
2524 data = cairo_surface_get_user_data(window->cairo_surface,
2527 cairo_device_flush(device);
2528 cairo_device_acquire(device);
2529 if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2530 fprintf(stderr, "failed to make surface current\n");
2539 display_release_window_surface(struct display *display,
2540 struct window *window)
2542 #ifdef HAVE_CAIRO_EGL
2543 cairo_device_t *device;
2545 device = cairo_surface_get_device(window->cairo_surface);
2549 if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
2550 fprintf(stderr, "failed to make context current\n");
2551 cairo_device_release(device);
2556 display_defer(struct display *display, struct task *task)
2558 wl_list_insert(&display->deferred_list, &task->link);
2562 display_watch_fd(struct display *display,
2563 int fd, uint32_t events, struct task *task)
2565 struct epoll_event ep;
2569 epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2573 display_run(struct display *display)
2576 struct epoll_event ep[16];
2580 while (display->mask & WL_DISPLAY_WRITABLE)
2581 wl_display_iterate(display->display,
2582 WL_DISPLAY_WRITABLE);
2584 count = epoll_wait(display->epoll_fd,
2585 ep, ARRAY_LENGTH(ep), -1);
2586 for (i = 0; i < count; i++) {
2587 task = ep[i].data.ptr;
2588 task->run(task, ep[i].events);
2591 while (!wl_list_empty(&display->deferred_list)) {
2592 task = container_of(display->deferred_list.next,
2594 wl_list_remove(&task->link);