33b7b95f034cbfa259d5157e39d97001e51e3c73
[profile/ivi/weston.git] / clients / window.c
1 /*
2  * Copyright © 2008 Kristian Høgsberg
3  *
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.
13  *
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
20  * OF THIS SOFTWARE.
21  */
22
23 #define _GNU_SOURCE
24
25 #include "../config.h"
26
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <assert.h>
35 #include <time.h>
36 #include <cairo.h>
37 #include <glib.h>
38 #include <gdk-pixbuf/gdk-pixbuf.h>
39 #include <sys/mman.h>
40 #include <sys/epoll.h>
41
42 #include <wayland-egl.h>
43
44 #include <GL/gl.h>
45 #include <EGL/egl.h>
46 #include <EGL/eglext.h>
47
48 #ifdef HAVE_CAIRO_EGL
49 #include <cairo-gl.h>
50 #endif
51
52 #include <X11/extensions/XKBcommon.h>
53
54 #include <linux/input.h>
55 #include <wayland-client.h>
56 #include "cairo-util.h"
57
58 #include "window.h"
59
60 struct display {
61         struct wl_display *display;
62         struct wl_compositor *compositor;
63         struct wl_shell *shell;
64         struct wl_shm *shm;
65         struct wl_data_device_manager *data_device_manager;
66         EGLDisplay dpy;
67         EGLConfig rgb_config;
68         EGLConfig premultiplied_argb_config;
69         EGLContext rgb_ctx;
70         EGLContext argb_ctx;
71         cairo_device_t *rgb_device;
72         cairo_device_t *argb_device;
73
74         int display_fd;
75         uint32_t mask;
76         struct task display_task;
77
78         int epoll_fd;
79         struct wl_list deferred_list;
80
81         int running;
82
83         struct wl_list window_list;
84         struct wl_list input_list;
85         struct wl_list output_list;
86         cairo_surface_t *active_frame, *inactive_frame, *shadow;
87         struct xkb_desc *xkb;
88         cairo_surface_t **pointer_surfaces;
89
90         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
91         PFNEGLCREATEIMAGEKHRPROC create_image;
92         PFNEGLDESTROYIMAGEKHRPROC destroy_image;
93
94         display_output_handler_t output_configure_handler;
95
96         void *user_data;
97 };
98
99 enum {
100         TYPE_TOPLEVEL,
101         TYPE_FULLSCREEN,
102         TYPE_TRANSIENT,
103         TYPE_MENU,
104         TYPE_CUSTOM
105 };
106        
107 struct window {
108         struct display *display;
109         struct window *parent;
110         struct wl_surface *surface;
111         struct wl_shell_surface *shell_surface;
112         char *title;
113         struct rectangle allocation, saved_allocation, server_allocation;
114         int x, y;
115         int resize_edges;
116         int redraw_scheduled;
117         struct task redraw_task;
118         int minimum_width, minimum_height;
119         int margin;
120         int type;
121         int decoration;
122         int transparent;
123         struct input *keyboard_device;
124         uint32_t name;
125         enum window_buffer_type buffer_type;
126
127         cairo_surface_t *cairo_surface, *pending_surface;
128
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_widget_focus_handler_t widget_focus_handler;
138         window_data_handler_t data_handler;
139         window_drop_handler_t drop_handler;
140         window_close_handler_t close_handler;
141
142         struct wl_list widget_list;
143         struct widget *focus_widget;
144         uint32_t widget_grab_button;
145
146         struct window *menu;
147
148         void *user_data;
149         struct wl_list link;
150 };
151
152 struct widget {
153         struct wl_list link;
154         struct rectangle allocation;
155         void *user_data;
156 };
157
158 struct input {
159         struct display *display;
160         struct wl_input_device *input_device;
161         struct window *pointer_focus;
162         struct window *keyboard_focus;
163         uint32_t current_pointer_image;
164         uint32_t modifiers;
165         int32_t x, y, sx, sy;
166         struct wl_list link;
167
168         struct wl_data_device *data_device;
169         struct data_offer *drag_offer;
170         struct data_offer *selection_offer;
171 };
172
173 struct output {
174         struct display *display;
175         struct wl_output *output;
176         struct rectangle allocation;
177         struct wl_list link;
178
179         display_output_handler_t destroy_handler;
180         void *user_data;
181 };
182
183 struct menu {
184         struct window *window;
185         const char **entries;
186         uint32_t time;
187         int current;
188         int count;
189         menu_func_t func;
190 };
191
192 enum {
193         POINTER_DEFAULT = 100,
194         POINTER_UNSET
195 };
196
197 enum window_location {
198         WINDOW_INTERIOR = 0,
199         WINDOW_RESIZING_TOP = 1,
200         WINDOW_RESIZING_BOTTOM = 2,
201         WINDOW_RESIZING_LEFT = 4,
202         WINDOW_RESIZING_TOP_LEFT = 5,
203         WINDOW_RESIZING_BOTTOM_LEFT = 6,
204         WINDOW_RESIZING_RIGHT = 8,
205         WINDOW_RESIZING_TOP_RIGHT = 9,
206         WINDOW_RESIZING_BOTTOM_RIGHT = 10,
207         WINDOW_RESIZING_MASK = 15,
208         WINDOW_EXTERIOR = 16,
209         WINDOW_TITLEBAR = 17,
210         WINDOW_CLIENT_AREA = 18,
211 };
212
213 const char *option_xkb_layout = "us";
214 const char *option_xkb_variant = "";
215 const char *option_xkb_options = "";
216
217 static const GOptionEntry xkb_option_entries[] = {
218         { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
219           &option_xkb_layout, "XKB Layout" },
220         { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
221           &option_xkb_variant, "XKB Variant" },
222         { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
223           &option_xkb_options, "XKB Options" },
224         { NULL }
225 };
226
227 static const cairo_user_data_key_t surface_data_key;
228 struct surface_data {
229         struct wl_buffer *buffer;
230 };
231
232 #define MULT(_d,c,a,t) \
233         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
234
235 #ifdef HAVE_CAIRO_EGL
236
237 struct egl_window_surface_data {
238         struct display *display;
239         struct wl_surface *surface;
240         struct wl_egl_window *window;
241         EGLSurface surf;
242 };
243
244 static void
245 egl_window_surface_data_destroy(void *p)
246 {
247         struct egl_window_surface_data *data = p;
248         struct display *d = data->display;
249
250         eglDestroySurface(d->dpy, data->surf);
251         wl_egl_window_destroy(data->window);
252         data->surface = NULL;
253
254         free(p);
255 }
256
257 static cairo_surface_t *
258 display_create_egl_window_surface(struct display *display,
259                                   struct wl_surface *surface,
260                                   uint32_t flags,
261                                   struct rectangle *rectangle)
262 {
263         cairo_surface_t *cairo_surface;
264         struct egl_window_surface_data *data;
265         EGLConfig config;
266         const EGLint *attribs;
267         cairo_device_t *device;
268
269         static const EGLint premul_attribs[] = {
270                 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
271                 EGL_NONE
272         };
273         
274         data = malloc(sizeof *data);
275         if (data == NULL)
276                 return NULL;
277
278         data->display = display;
279         data->surface = surface;
280
281         if (flags & SURFACE_OPAQUE) {
282                 config = display->rgb_config;
283                 device = display->rgb_device;
284                 attribs = NULL;
285         } else {
286                 config = display->premultiplied_argb_config;
287                 device = display->argb_device;
288                 attribs = premul_attribs;
289         }
290
291         data->window = wl_egl_window_create(surface,
292                                             rectangle->width,
293                                             rectangle->height);
294
295         data->surf = eglCreateWindowSurface(display->dpy, config,
296                                             data->window, attribs);
297
298         cairo_surface = cairo_gl_surface_create_for_egl(device,
299                                                         data->surf,
300                                                         rectangle->width,
301                                                         rectangle->height);
302
303         cairo_surface_set_user_data(cairo_surface, &surface_data_key,
304                                     data, egl_window_surface_data_destroy);
305
306         return cairo_surface;
307 }
308
309 struct egl_image_surface_data {
310         struct surface_data data;
311         cairo_device_t *device;
312         EGLImageKHR image;
313         GLuint texture;
314         struct display *display;
315         struct wl_egl_pixmap *pixmap;
316 };
317
318 static void
319 egl_image_surface_data_destroy(void *p)
320 {
321         struct egl_image_surface_data *data = p;
322         struct display *d = data->display;
323
324         cairo_device_acquire(data->device);
325         glDeleteTextures(1, &data->texture);
326         cairo_device_release(data->device);
327
328         d->destroy_image(d->dpy, data->image);
329         wl_buffer_destroy(data->data.buffer);
330         wl_egl_pixmap_destroy(data->pixmap);
331         free(p);
332 }
333
334 EGLImageKHR
335 display_get_image_for_egl_image_surface(struct display *display,
336                                         cairo_surface_t *surface)
337 {
338         struct egl_image_surface_data *data;
339
340         data = cairo_surface_get_user_data (surface, &surface_data_key);
341
342         return data->image;
343 }
344
345 static cairo_surface_t *
346 display_create_egl_image_surface(struct display *display,
347                                  uint32_t flags,
348                                  struct rectangle *rectangle)
349 {
350         struct egl_image_surface_data *data;
351         EGLDisplay dpy = display->dpy;
352         cairo_surface_t *surface;
353         cairo_content_t content;
354
355         data = malloc(sizeof *data);
356         if (data == NULL)
357                 return NULL;
358
359         data->display = display;
360
361         data->pixmap = wl_egl_pixmap_create(rectangle->width,
362                                             rectangle->height, 0);
363         if (data->pixmap == NULL) {
364                 free(data);
365                 return NULL;
366         }
367
368         if (flags & SURFACE_OPAQUE) {
369                 data->device = display->rgb_device;
370                 content = CAIRO_CONTENT_COLOR;
371         } else {
372                 data->device = display->argb_device;
373                 content = CAIRO_CONTENT_COLOR_ALPHA;
374         }
375
376         data->image = display->create_image(dpy, NULL,
377                                             EGL_NATIVE_PIXMAP_KHR,
378                                             (EGLClientBuffer) data->pixmap,
379                                             NULL);
380         if (data->image == EGL_NO_IMAGE_KHR) {
381                 wl_egl_pixmap_destroy(data->pixmap);
382                 free(data);
383                 return NULL;
384         }
385
386         data->data.buffer =
387                 wl_egl_pixmap_create_buffer(data->pixmap);
388
389         cairo_device_acquire(data->device);
390         glGenTextures(1, &data->texture);
391         glBindTexture(GL_TEXTURE_2D, data->texture);
392         display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
393         cairo_device_release(data->device);
394
395         surface = cairo_gl_surface_create_for_texture(data->device,
396                                                       content,
397                                                       data->texture,
398                                                       rectangle->width,
399                                                       rectangle->height);
400
401         cairo_surface_set_user_data (surface, &surface_data_key,
402                                      data, egl_image_surface_data_destroy);
403
404         return surface;
405 }
406
407 static cairo_surface_t *
408 display_create_egl_image_surface_from_file(struct display *display,
409                                            const char *filename,
410                                            struct rectangle *rect)
411 {
412         cairo_surface_t *surface;
413         GdkPixbuf *pixbuf;
414         GError *error = NULL;
415         int stride, i;
416         unsigned char *pixels, *p, *end;
417         struct egl_image_surface_data *data;
418
419         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
420                                                    rect->width, rect->height,
421                                                    FALSE, &error);
422         if (error != NULL)
423                 return NULL;
424
425         if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
426             gdk_pixbuf_get_n_channels(pixbuf) != 4) {
427                 g_object_unref(pixbuf);
428                 return NULL;
429         }
430
431
432         stride = gdk_pixbuf_get_rowstride(pixbuf);
433         pixels = gdk_pixbuf_get_pixels(pixbuf);
434
435         for (i = 0; i < rect->height; i++) {
436                 p = pixels + i * stride;
437                 end = p + rect->width * 4;
438                 while (p < end) {
439                         unsigned int t;
440
441                         MULT(p[0], p[0], p[3], t);
442                         MULT(p[1], p[1], p[3], t);
443                         MULT(p[2], p[2], p[3], t);
444                         p += 4;
445
446                 }
447         }
448
449         surface = display_create_egl_image_surface(display, 0, rect);
450         if (surface == NULL) {
451                 g_object_unref(pixbuf);
452                 return NULL;
453         }
454
455         data = cairo_surface_get_user_data(surface, &surface_data_key);
456
457         cairo_device_acquire(display->argb_device);
458         glBindTexture(GL_TEXTURE_2D, data->texture);
459         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
460                         GL_RGBA, GL_UNSIGNED_BYTE, pixels);
461         cairo_device_release(display->argb_device);
462
463         g_object_unref(pixbuf);
464
465         return surface;
466 }
467
468 #endif
469
470 struct wl_buffer *
471 display_get_buffer_for_surface(struct display *display,
472                                cairo_surface_t *surface)
473 {
474         struct surface_data *data;
475
476         data = cairo_surface_get_user_data (surface, &surface_data_key);
477
478         return data->buffer;
479 }
480
481 struct shm_surface_data {
482         struct surface_data data;
483         void *map;
484         size_t length;
485 };
486
487 static void
488 shm_surface_data_destroy(void *p)
489 {
490         struct shm_surface_data *data = p;
491
492         wl_buffer_destroy(data->data.buffer);
493         munmap(data->map, data->length);
494 }
495
496 static cairo_surface_t *
497 display_create_shm_surface(struct display *display,
498                            struct rectangle *rectangle, uint32_t flags)
499 {
500         struct shm_surface_data *data;
501         uint32_t format;
502         cairo_surface_t *surface;
503         int stride, fd;
504         char filename[] = "/tmp/wayland-shm-XXXXXX";
505
506         data = malloc(sizeof *data);
507         if (data == NULL)
508                 return NULL;
509
510         stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
511                                                 rectangle->width);
512         data->length = stride * rectangle->height;
513         fd = mkstemp(filename);
514         if (fd < 0) {
515                 fprintf(stderr, "open %s failed: %m\n", filename);
516                 return NULL;
517         }
518         if (ftruncate(fd, data->length) < 0) {
519                 fprintf(stderr, "ftruncate failed: %m\n");
520                 close(fd);
521                 return NULL;
522         }
523
524         data->map = mmap(NULL, data->length,
525                          PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
526         unlink(filename);
527
528         if (data->map == MAP_FAILED) {
529                 fprintf(stderr, "mmap failed: %m\n");
530                 close(fd);
531                 return NULL;
532         }
533
534         surface = cairo_image_surface_create_for_data (data->map,
535                                                        CAIRO_FORMAT_ARGB32,
536                                                        rectangle->width,
537                                                        rectangle->height,
538                                                        stride);
539
540         cairo_surface_set_user_data (surface, &surface_data_key,
541                                      data, shm_surface_data_destroy);
542
543         if (flags & SURFACE_OPAQUE)
544                 format = WL_SHM_FORMAT_XRGB32;
545         else
546                 format = WL_SHM_FORMAT_PREMULTIPLIED_ARGB32;
547
548         data->data.buffer = wl_shm_create_buffer(display->shm,
549                                                  fd,
550                                                  rectangle->width,
551                                                  rectangle->height,
552                                                  stride, format);
553
554         close(fd);
555
556         return surface;
557 }
558
559 static cairo_surface_t *
560 display_create_shm_surface_from_file(struct display *display,
561                                      const char *filename,
562                                      struct rectangle *rect)
563 {
564         cairo_surface_t *surface;
565         GdkPixbuf *pixbuf;
566         GError *error = NULL;
567         int stride, i;
568         unsigned char *pixels, *p, *end, *dest_data;
569         int dest_stride;
570         uint32_t *d;
571
572         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
573                                                    rect->width, rect->height,
574                                                    FALSE, &error);
575         if (error != NULL)
576                 return NULL;
577
578         if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
579             gdk_pixbuf_get_n_channels(pixbuf) != 4) {
580                 g_object_unref(pixbuf);
581                 return NULL;
582         }
583
584         stride = gdk_pixbuf_get_rowstride(pixbuf);
585         pixels = gdk_pixbuf_get_pixels(pixbuf);
586
587         surface = display_create_shm_surface(display, rect, 0);
588         if (surface == NULL) {
589                 g_object_unref(pixbuf);
590                 return NULL;
591         }
592
593         dest_data = cairo_image_surface_get_data (surface);
594         dest_stride = cairo_image_surface_get_stride (surface);
595
596         for (i = 0; i < rect->height; i++) {
597                 d = (uint32_t *) (dest_data + i * dest_stride);
598                 p = pixels + i * stride;
599                 end = p + rect->width * 4;
600                 while (p < end) {
601                         unsigned int t;
602                         unsigned char a, r, g, b;
603
604                         a = p[3];
605                         MULT(r, p[0], a, t);
606                         MULT(g, p[1], a, t);
607                         MULT(b, p[2], a, t);
608                         p += 4;
609                         *d++ = (a << 24) | (r << 16) | (g << 8) | b;
610                 }
611         }
612
613         g_object_unref(pixbuf);
614
615         return surface;
616 }
617
618 static int
619 check_size(struct rectangle *rect)
620 {
621         if (rect->width && rect->height)
622                 return 0;
623
624         fprintf(stderr, "tried to create surface of "
625                 "width: %d, height: %d\n", rect->width, rect->height);
626         return -1;
627 }
628
629 cairo_surface_t *
630 display_create_surface(struct display *display,
631                        struct wl_surface *surface,
632                        struct rectangle *rectangle,
633                        uint32_t flags)
634 {
635         if (check_size(rectangle) < 0)
636                 return NULL;
637 #ifdef HAVE_CAIRO_EGL
638         if (display->dpy) {
639                 if (surface)
640                         return display_create_egl_window_surface(display,
641                                                                  surface,
642                                                                  flags,
643                                                                  rectangle);
644                 else
645                         return display_create_egl_image_surface(display,
646                                                                 flags,
647                                                                 rectangle);
648         }
649 #endif
650         return display_create_shm_surface(display, rectangle, flags);
651 }
652
653 static cairo_surface_t *
654 display_create_surface_from_file(struct display *display,
655                                  const char *filename,
656                                  struct rectangle *rectangle)
657 {
658         if (check_size(rectangle) < 0)
659                 return NULL;
660 #ifdef HAVE_CAIRO_EGL
661         if (display->dpy) {
662                 return display_create_egl_image_surface_from_file(display,
663                                                                   filename,
664                                                                   rectangle);
665         }
666 #endif
667         return display_create_shm_surface_from_file(display, filename, rectangle);
668 }
669  static const struct {
670         const char *filename;
671         int hotspot_x, hotspot_y;
672 } pointer_images[] = {
673         { DATADIR "/weston/bottom_left_corner.png",      6, 30 },
674         { DATADIR "/weston/bottom_right_corner.png",    28, 28 },
675         { DATADIR "/weston/bottom_side.png",            16, 20 },
676         { DATADIR "/weston/grabbing.png",               20, 17 },
677         { DATADIR "/weston/left_ptr.png",               10,  5 },
678         { DATADIR "/weston/left_side.png",              10, 20 },
679         { DATADIR "/weston/right_side.png",             30, 19 },
680         { DATADIR "/weston/top_left_corner.png",         8,  8 },
681         { DATADIR "/weston/top_right_corner.png",       26,  8 },
682         { DATADIR "/weston/top_side.png",               18,  8 },
683         { DATADIR "/weston/xterm.png",                  15, 15 },
684         { DATADIR "/weston/hand1.png",                  18, 11 }
685 };
686
687 static void
688 create_pointer_surfaces(struct display *display)
689 {
690         int i, count;
691         const int width = 32, height = 32;
692         struct rectangle rect;
693
694         count = ARRAY_LENGTH(pointer_images);
695         display->pointer_surfaces =
696                 malloc(count * sizeof *display->pointer_surfaces);
697         rect.width = width;
698         rect.height = height;
699         for (i = 0; i < count; i++) {
700                 display->pointer_surfaces[i] =
701                         display_create_surface_from_file(display,
702                                                          pointer_images[i].filename,
703                                                          &rect);
704                 if (!display->pointer_surfaces[i]) {
705                         fprintf(stderr, "Error loading pointer image: %s\n",
706                                 pointer_images[i].filename);
707                 }
708         }
709
710 }
711
712 static void
713 destroy_pointer_surfaces(struct display *display)
714 {
715         int i, count;
716
717         count = ARRAY_LENGTH(pointer_images);
718         for (i = 0; i < count; ++i) {
719                 if (display->pointer_surfaces[i])
720                         cairo_surface_destroy(display->pointer_surfaces[i]);
721         }
722         free(display->pointer_surfaces);
723 }
724
725 cairo_surface_t *
726 display_get_pointer_surface(struct display *display, int pointer,
727                             int *width, int *height,
728                             int *hotspot_x, int *hotspot_y)
729 {
730         cairo_surface_t *surface;
731
732         surface = display->pointer_surfaces[pointer];
733 #if HAVE_CAIRO_EGL
734         *width = cairo_gl_surface_get_width(surface);
735         *height = cairo_gl_surface_get_height(surface);
736 #else
737         *width = cairo_image_surface_get_width(surface);
738         *height = cairo_image_surface_get_height(surface);
739 #endif
740         *hotspot_x = pointer_images[pointer].hotspot_x;
741         *hotspot_y = pointer_images[pointer].hotspot_y;
742
743         return cairo_surface_reference(surface);
744 }
745
746 static void
747 window_attach_surface(struct window *window);
748
749 static void
750 free_surface(void *data, struct wl_callback *callback, uint32_t time)
751 {
752         struct window *window = data;
753
754         wl_callback_destroy(callback);
755         cairo_surface_destroy(window->pending_surface);
756         window->pending_surface = NULL;
757         if (window->cairo_surface)
758                 window_attach_surface(window);
759 }
760
761 static const struct wl_callback_listener free_surface_listener = {
762         free_surface
763 };
764
765 static void
766 window_get_resize_dx_dy(struct window *window, int *x, int *y)
767 {
768         if (window->resize_edges & WINDOW_RESIZING_LEFT)
769                 *x = window->server_allocation.width - window->allocation.width;
770         else
771                 *x = 0;
772
773         if (window->resize_edges & WINDOW_RESIZING_TOP)
774                 *y = window->server_allocation.height -
775                         window->allocation.height;
776         else
777                 *y = 0;
778
779         window->resize_edges = 0;
780 }
781
782 static void
783 window_set_type(struct window *window)
784 {
785         if (!window->shell_surface)
786                 return;
787
788         switch (window->type) {
789         case TYPE_FULLSCREEN:
790                 wl_shell_surface_set_fullscreen(window->shell_surface);
791                 break;
792         case TYPE_TOPLEVEL:
793                 wl_shell_surface_set_toplevel(window->shell_surface);
794                 break;
795         case TYPE_TRANSIENT:
796                 wl_shell_surface_set_transient(window->shell_surface,
797                                                window->parent->shell_surface,
798                                                window->x, window->y, 0);
799                 break;
800         case TYPE_MENU:
801                 break;
802         case TYPE_CUSTOM:
803                 break;
804         }
805 }
806
807 static void
808 window_attach_surface(struct window *window)
809 {
810         struct display *display = window->display;
811         struct wl_buffer *buffer;
812         struct wl_callback *cb;
813 #ifdef HAVE_CAIRO_EGL
814         struct egl_window_surface_data *data;
815 #endif
816         int32_t x, y;
817
818         if (display->shell)
819                 window_set_type(window);
820
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,
825                                                    &surface_data_key);
826
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);
831                 break;
832         case WINDOW_BUFFER_TYPE_EGL_IMAGE:
833 #endif
834         case WINDOW_BUFFER_TYPE_SHM:
835                 window_get_resize_dx_dy(window, &x, &y);
836
837                 if (window->pending_surface != NULL)
838                         return;
839
840                 window->pending_surface = window->cairo_surface;
841                 window->cairo_surface = NULL;
842
843                 buffer =
844                         display_get_buffer_for_surface(display,
845                                                        window->pending_surface);
846
847                 wl_surface_attach(window->surface, buffer, x, y);
848                 window->server_allocation = window->allocation;
849                 cb = wl_display_sync(display->display);
850                 wl_callback_add_listener(cb, &free_surface_listener, window);
851                 break;
852         default:
853                 return;
854         }
855
856         wl_surface_damage(window->surface, 0, 0,
857                           window->allocation.width,
858                           window->allocation.height);
859 }
860
861 void
862 window_flush(struct window *window)
863 {
864         if (window->cairo_surface) {
865                 switch (window->buffer_type) {
866                 case WINDOW_BUFFER_TYPE_EGL_IMAGE:
867                 case WINDOW_BUFFER_TYPE_SHM:
868                         display_surface_damage(window->display,
869                                                window->cairo_surface,
870                                                0, 0,
871                                                window->allocation.width,
872                                                window->allocation.height);
873                         break;
874                 default:
875                         break;
876                 }
877                 window_attach_surface(window);
878         }
879 }
880
881 void
882 window_set_surface(struct window *window, cairo_surface_t *surface)
883 {
884         cairo_surface_reference(surface);
885
886         if (window->cairo_surface != NULL)
887                 cairo_surface_destroy(window->cairo_surface);
888
889         window->cairo_surface = surface;
890 }
891
892 #ifdef HAVE_CAIRO_EGL
893 static void
894 window_resize_cairo_window_surface(struct window *window)
895 {
896         struct egl_window_surface_data *data;
897         int x, y;
898
899         data = cairo_surface_get_user_data(window->cairo_surface,
900                                            &surface_data_key);
901
902         window_get_resize_dx_dy(window, &x, &y),
903         wl_egl_window_resize(data->window,
904                              window->allocation.width,
905                              window->allocation.height,
906                              x,y);
907
908         cairo_gl_surface_set_size(window->cairo_surface,
909                                   window->allocation.width,
910                                   window->allocation.height);
911 }
912 #endif
913
914 struct display *
915 window_get_display(struct window *window)
916 {
917         return window->display;
918 }
919
920 void
921 window_create_surface(struct window *window)
922 {
923         cairo_surface_t *surface;
924         uint32_t flags = 0;
925         
926         if (!window->transparent)
927                 flags = SURFACE_OPAQUE;
928         
929         switch (window->buffer_type) {
930 #ifdef HAVE_CAIRO_EGL
931         case WINDOW_BUFFER_TYPE_EGL_WINDOW:
932                 if (window->cairo_surface) {
933                         window_resize_cairo_window_surface(window);
934                         return;
935                 }
936                 surface = display_create_surface(window->display,
937                                                  window->surface,
938                                                  &window->allocation, flags);
939                 break;
940         case WINDOW_BUFFER_TYPE_EGL_IMAGE:
941                 surface = display_create_surface(window->display,
942                                                  NULL,
943                                                  &window->allocation, flags);
944                 break;
945 #endif
946         case WINDOW_BUFFER_TYPE_SHM:
947                 surface = display_create_shm_surface(window->display,
948                                                      &window->allocation, flags);
949                 break;
950         default:
951                 surface = NULL;
952                 break;
953         }
954
955         window_set_surface(window, surface);
956         cairo_surface_destroy(surface);
957 }
958
959 static void
960 window_draw_decorations(struct window *window)
961 {
962         cairo_t *cr;
963         cairo_text_extents_t extents;
964         cairo_surface_t *frame;
965         int width, height, shadow_dx = 3, shadow_dy = 3;
966
967         window_create_surface(window);
968
969         width = window->allocation.width;
970         height = window->allocation.height;
971
972         cr = cairo_create(window->cairo_surface);
973
974         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
975         cairo_set_source_rgba(cr, 0, 0, 0, 0);
976         cairo_paint(cr);
977
978         cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
979         tile_mask(cr, window->display->shadow,
980                   shadow_dx, shadow_dy, width, height,
981                   window->margin + 10 - shadow_dx,
982                   window->margin + 10 - shadow_dy);
983
984         if (window->keyboard_device)
985                 frame = window->display->active_frame;
986         else
987                 frame = window->display->inactive_frame;
988
989         tile_source(cr, frame, 0, 0, width, height,
990                     window->margin + 10, window->margin + 50);
991
992         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
993         cairo_set_font_size(cr, 14);
994         cairo_text_extents(cr, window->title, &extents);
995         cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
996         if (window->keyboard_device)
997                 cairo_set_source_rgb(cr, 0, 0, 0);
998         else
999                 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
1000         cairo_show_text(cr, window->title);
1001
1002         cairo_destroy(cr);
1003 }
1004
1005 void
1006 window_destroy(struct window *window)
1007 {
1008         struct display *display = window->display;
1009         struct input *input;
1010
1011         if (window->redraw_scheduled)
1012                 wl_list_remove(&window->redraw_task.link);
1013
1014         wl_list_for_each(input, &display->input_list, link) {
1015                 if (input->pointer_focus == window)
1016                         input->pointer_focus = NULL;
1017                 if (input->keyboard_focus == window)
1018                         input->keyboard_focus = NULL;
1019         }
1020
1021         if (window->shell_surface)
1022                 wl_shell_surface_destroy(window->shell_surface);
1023         wl_surface_destroy(window->surface);
1024         wl_list_remove(&window->link);
1025
1026         if (window->cairo_surface != NULL)
1027                 cairo_surface_destroy(window->cairo_surface);
1028         if (window->pending_surface != NULL)
1029                 cairo_surface_destroy(window->pending_surface);
1030
1031         free(window->title);
1032         free(window);
1033 }
1034
1035 static struct widget *
1036 window_find_widget(struct window *window, int32_t x, int32_t y)
1037 {
1038         struct widget *widget;
1039
1040         wl_list_for_each(widget, &window->widget_list, link) {
1041                 if (widget->allocation.x <= x &&
1042                     x < widget->allocation.x + widget->allocation.width &&
1043                     widget->allocation.y <= y &&
1044                     y < widget->allocation.y + widget->allocation.height) {
1045                         return widget;
1046                 }
1047         }
1048
1049         return NULL;
1050 }
1051
1052 struct widget *
1053 window_add_widget(struct window *window, void *data)
1054 {
1055         struct widget *widget;
1056
1057         widget = malloc(sizeof *widget);
1058         memset(widget, 0, sizeof *widget);
1059         widget->user_data = data;
1060         wl_list_insert(window->widget_list.prev, &widget->link);
1061
1062         return widget;
1063 }
1064
1065 void
1066 window_for_each_widget(struct window *window, widget_func_t func, void *data)
1067 {
1068         struct widget *widget;
1069
1070         wl_list_for_each(widget, &window->widget_list, link)
1071                 func(widget, data);
1072 }
1073
1074 struct widget *
1075 window_get_focus_widget(struct window *window)
1076 {
1077         return window->focus_widget;
1078 }
1079
1080 void
1081 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1082 {
1083         *allocation = widget->allocation;
1084 }
1085
1086 void
1087 widget_set_allocation(struct widget *widget,
1088                       int32_t x, int32_t y, int32_t width, int32_t height)
1089 {
1090         widget->allocation.x = x;
1091         widget->allocation.y = y;
1092         widget->allocation.width = width;
1093         widget->allocation.height = height;
1094 }
1095
1096 void *
1097 widget_get_user_data(struct widget *widget)
1098 {
1099         return widget->user_data;
1100 }
1101
1102 void
1103 window_draw(struct window *window)
1104 {
1105         if (!window->decoration)
1106                 window_create_surface(window);
1107         else
1108                 window_draw_decorations(window);
1109 }
1110
1111 cairo_surface_t *
1112 window_get_surface(struct window *window)
1113 {
1114         return cairo_surface_reference(window->cairo_surface);
1115 }
1116
1117 struct wl_surface *
1118 window_get_wl_surface(struct window *window)
1119 {
1120         return window->surface;
1121 }
1122
1123 struct wl_shell_surface *
1124 window_get_wl_shell_surface(struct window *window)
1125 {
1126         return window->shell_surface;
1127 }
1128
1129 static int
1130 get_pointer_location(struct window *window, int32_t x, int32_t y)
1131 {
1132         int vlocation, hlocation, location;
1133         const int grip_size = 8;
1134
1135         if (!window->decoration)
1136                 return WINDOW_CLIENT_AREA;
1137
1138         if (x < window->margin)
1139                 hlocation = WINDOW_EXTERIOR;
1140         else if (window->margin <= x && x < window->margin + grip_size)
1141                 hlocation = WINDOW_RESIZING_LEFT;
1142         else if (x < window->allocation.width - window->margin - grip_size)
1143                 hlocation = WINDOW_INTERIOR;
1144         else if (x < window->allocation.width - window->margin)
1145                 hlocation = WINDOW_RESIZING_RIGHT;
1146         else
1147                 hlocation = WINDOW_EXTERIOR;
1148
1149         if (y < window->margin)
1150                 vlocation = WINDOW_EXTERIOR;
1151         else if (window->margin <= y && y < window->margin + grip_size)
1152                 vlocation = WINDOW_RESIZING_TOP;
1153         else if (y < window->allocation.height - window->margin - grip_size)
1154                 vlocation = WINDOW_INTERIOR;
1155         else if (y < window->allocation.height - window->margin)
1156                 vlocation = WINDOW_RESIZING_BOTTOM;
1157         else
1158                 vlocation = WINDOW_EXTERIOR;
1159
1160         location = vlocation | hlocation;
1161         if (location & WINDOW_EXTERIOR)
1162                 location = WINDOW_EXTERIOR;
1163         if (location == WINDOW_INTERIOR && y < window->margin + 50)
1164                 location = WINDOW_TITLEBAR;
1165         else if (location == WINDOW_INTERIOR)
1166                 location = WINDOW_CLIENT_AREA;
1167
1168         return location;
1169 }
1170
1171 static int
1172 input_get_pointer_image_for_location(struct input *input, int pointer)
1173 {
1174         int location;
1175
1176         location = get_pointer_location(input->pointer_focus,
1177                                         input->sx, input->sy);
1178         switch (location) {
1179         case WINDOW_RESIZING_TOP:
1180                 return POINTER_TOP;
1181         case WINDOW_RESIZING_BOTTOM:
1182                 return POINTER_BOTTOM;
1183         case WINDOW_RESIZING_LEFT:
1184                 return POINTER_LEFT;
1185         case WINDOW_RESIZING_RIGHT:
1186                 return POINTER_RIGHT;
1187         case WINDOW_RESIZING_TOP_LEFT:
1188                 return POINTER_TOP_LEFT;
1189         case WINDOW_RESIZING_TOP_RIGHT:
1190                 return POINTER_TOP_RIGHT;
1191         case WINDOW_RESIZING_BOTTOM_LEFT:
1192                 return POINTER_BOTTOM_LEFT;
1193         case WINDOW_RESIZING_BOTTOM_RIGHT:
1194                 return POINTER_BOTTOM_RIGHT;
1195         case WINDOW_EXTERIOR:
1196         case WINDOW_TITLEBAR:
1197                 return POINTER_LEFT_PTR;
1198         default:
1199                 return pointer;
1200         }
1201 }
1202
1203 void
1204 input_set_pointer_image(struct input *input, uint32_t time, int pointer)
1205 {
1206         struct display *display = input->display;
1207         struct wl_buffer *buffer;
1208         cairo_surface_t *surface;
1209
1210         if (pointer == input->current_pointer_image)
1211                 return;
1212
1213         input->current_pointer_image = pointer;
1214         surface = display->pointer_surfaces[pointer];
1215
1216         if (!surface)
1217                 return;
1218
1219         buffer = display_get_buffer_for_surface(display, surface);
1220         wl_input_device_attach(input->input_device, time, buffer,
1221                                pointer_images[pointer].hotspot_x,
1222                                pointer_images[pointer].hotspot_y);
1223 }
1224
1225 static void
1226 window_set_focus_widget(struct window *window, struct widget *focus)
1227 {
1228         void *data;
1229
1230         if (focus == window->focus_widget)
1231                 return;
1232
1233         window->focus_widget = focus;
1234         data = focus ? focus->user_data : NULL;
1235         if (window->widget_focus_handler)
1236                 window->widget_focus_handler(window, focus, data);
1237 }
1238
1239 static void
1240 input_handle_motion(void *data, struct wl_input_device *input_device,
1241                     uint32_t time,
1242                     int32_t x, int32_t y, int32_t sx, int32_t sy)
1243 {
1244         struct input *input = data;
1245         struct window *window = input->pointer_focus;
1246         struct widget *widget;
1247         int pointer = POINTER_LEFT_PTR;
1248
1249         input->x = x;
1250         input->y = y;
1251         input->sx = sx;
1252         input->sy = sy;
1253
1254         if (!window->focus_widget || !window->widget_grab_button) {
1255                 widget = window_find_widget(window, sx, sy);
1256                 window_set_focus_widget(window, widget);
1257         }
1258
1259         if (window->motion_handler)
1260                 pointer = (*window->motion_handler)(window, input, time,
1261                                                     x, y, sx, sy,
1262                                                     window->user_data);
1263
1264         pointer = input_get_pointer_image_for_location(input, pointer);
1265         input_set_pointer_image(input, time, pointer);
1266 }
1267
1268 static void
1269 window_menu_func(struct window *window, int index, void *data)
1270 {
1271         switch (index) {
1272         case 0: /* close */
1273                 if (window->close_handler)
1274                         window->close_handler(window->parent,
1275                                               window->user_data);
1276                 else
1277                         exit(0);
1278                 break;
1279         case 1: /* fullscreen */
1280                 /* we don't have a way to get out of fullscreen for now */
1281                 window_set_fullscreen(window, 1);
1282                 break;
1283         case 2: /* rotate */
1284         case 3: /* scale */
1285                 break;
1286         }
1287 }
1288
1289
1290 static void
1291 input_handle_button(void *data,
1292                     struct wl_input_device *input_device,
1293                     uint32_t time, uint32_t button, uint32_t state)
1294 {
1295         struct input *input = data;
1296         struct window *window = input->pointer_focus;
1297         struct widget *widget;
1298         int location;
1299         int32_t x, y;
1300         static const char *entries[] = {
1301                 "Close", "Fullscreen", "Rotate", "Scale"
1302         };
1303
1304         if (window->focus_widget && window->widget_grab_button == 0 && state)
1305                 window->widget_grab_button = button;
1306
1307         location = get_pointer_location(window, input->sx, input->sy);
1308
1309         if (window->display->shell && button == BTN_LEFT && state == 1) {
1310                 switch (location) {
1311                 case WINDOW_TITLEBAR:
1312                         if (!window->shell_surface)
1313                                 break;
1314                         input_set_pointer_image(input, time, POINTER_DRAGGING);
1315                         wl_shell_surface_move(window->shell_surface,
1316                                               input_device, time);
1317                         break;
1318                 case WINDOW_RESIZING_TOP:
1319                 case WINDOW_RESIZING_BOTTOM:
1320                 case WINDOW_RESIZING_LEFT:
1321                 case WINDOW_RESIZING_RIGHT:
1322                 case WINDOW_RESIZING_TOP_LEFT:
1323                 case WINDOW_RESIZING_TOP_RIGHT:
1324                 case WINDOW_RESIZING_BOTTOM_LEFT:
1325                 case WINDOW_RESIZING_BOTTOM_RIGHT:
1326                         if (!window->shell_surface)
1327                                 break;
1328                         wl_shell_surface_resize(window->shell_surface,
1329                                                 input_device, time,
1330                                                 location);
1331                         break;
1332                 case WINDOW_CLIENT_AREA:
1333                         if (window->button_handler)
1334                                 (*window->button_handler)(window,
1335                                                           input, time,
1336                                                           button, state,
1337                                                           window->user_data);
1338                         break;
1339                 }
1340         } else if (button == BTN_RIGHT && state == 1) {
1341                 switch (location) {
1342                 default:
1343                         input_get_position(input, &x, &y);
1344                         window->menu = window_create_menu(window->display,
1345                                                           input, time,
1346                                                           window,
1347                                                           x - 10, y - 10,
1348                                                           window_menu_func,
1349                                                           entries, 4);
1350                         window_schedule_redraw(window->menu);
1351                         break;
1352                 case WINDOW_CLIENT_AREA:
1353                         if (window->button_handler)
1354                                 (*window->button_handler)(window,
1355                                                           input, time,
1356                                                           button, state,
1357                                                           window->user_data);
1358                         break;
1359                 }
1360         } else {
1361                 if (window->button_handler)
1362                         (*window->button_handler)(window,
1363                                                   input, time,
1364                                                   button, state,
1365                                                   window->user_data);
1366         }
1367
1368         if (window->focus_widget &&
1369             window->widget_grab_button == button && !state) {
1370                 window->widget_grab_button = 0;
1371                 widget = window_find_widget(window, input->sx, input->sy);
1372                 window_set_focus_widget(window, widget);
1373         }
1374 }
1375
1376 static void
1377 input_handle_key(void *data, struct wl_input_device *input_device,
1378                  uint32_t time, uint32_t key, uint32_t state)
1379 {
1380         struct input *input = data;
1381         struct window *window = input->keyboard_focus;
1382         struct display *d = input->display;
1383         uint32_t code, sym, level;
1384
1385         code = key + d->xkb->min_key_code;
1386         if (!window || window->keyboard_device != input)
1387                 return;
1388
1389         level = 0;
1390         if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
1391             XkbKeyGroupWidth(d->xkb, code, 0) > 1)
1392                 level = 1;
1393
1394         sym = XkbKeySymEntry(d->xkb, code, level, 0);
1395
1396         if (state)
1397                 input->modifiers |= d->xkb->map->modmap[code];
1398         else
1399                 input->modifiers &= ~d->xkb->map->modmap[code];
1400
1401         if (window->key_handler)
1402                 (*window->key_handler)(window, input, time, key, sym, state,
1403                                        window->user_data);
1404 }
1405
1406 static void
1407 input_remove_pointer_focus(struct input *input, uint32_t time)
1408 {
1409         struct window *window = input->pointer_focus;
1410
1411         if (!window)
1412                 return;
1413
1414         window_set_focus_widget(window, NULL);
1415
1416         if (window->leave_handler)
1417                 window->leave_handler(window, input, time, window->user_data);
1418         input->pointer_focus = NULL;
1419         input->current_pointer_image = POINTER_UNSET;
1420 }
1421
1422 static void
1423 input_handle_pointer_focus(void *data,
1424                            struct wl_input_device *input_device,
1425                            uint32_t time, struct wl_surface *surface,
1426                            int32_t x, int32_t y, int32_t sx, int32_t sy)
1427 {
1428         struct input *input = data;
1429         struct window *window;
1430         struct widget *widget;
1431         int pointer;
1432
1433         window = input->pointer_focus;
1434         if (window && window->surface != surface)
1435                 input_remove_pointer_focus(input, time);
1436
1437         if (surface) {
1438                 input->pointer_focus = wl_surface_get_user_data(surface);
1439                 window = input->pointer_focus;
1440
1441                 input->x = x;
1442                 input->y = y;
1443                 input->sx = sx;
1444                 input->sy = sy;
1445
1446                 pointer = POINTER_LEFT_PTR;
1447                 if (window->enter_handler)
1448                         pointer = window->enter_handler(window, input,
1449                                                         time, sx, sy,
1450                                                         window->user_data);
1451
1452                 widget = window_find_widget(window, x, y);
1453                 window_set_focus_widget(window, widget);
1454
1455                 pointer = input_get_pointer_image_for_location(input, pointer);
1456                 input_set_pointer_image(input, time, pointer);
1457         }
1458 }
1459
1460 static void
1461 input_remove_keyboard_focus(struct input *input)
1462 {
1463         struct window *window = input->keyboard_focus;
1464
1465         if (!window)
1466                 return;
1467
1468         window->keyboard_device = NULL;
1469         if (window->keyboard_focus_handler)
1470                 (*window->keyboard_focus_handler)(window, NULL,
1471                                                   window->user_data);
1472
1473         input->keyboard_focus = NULL;
1474 }
1475
1476 static void
1477 input_handle_keyboard_focus(void *data,
1478                             struct wl_input_device *input_device,
1479                             uint32_t time,
1480                             struct wl_surface *surface,
1481                             struct wl_array *keys)
1482 {
1483         struct input *input = data;
1484         struct window *window;
1485         struct display *d = input->display;
1486         uint32_t *k, *end;
1487
1488         input_remove_keyboard_focus(input);
1489
1490         if (surface)
1491                 input->keyboard_focus = wl_surface_get_user_data(surface);
1492
1493         end = keys->data + keys->size;
1494         input->modifiers = 0;
1495         for (k = keys->data; k < end; k++)
1496                 input->modifiers |= d->xkb->map->modmap[*k];
1497
1498         window = input->keyboard_focus;
1499         if (window) {
1500                 window->keyboard_device = input;
1501                 if (window->keyboard_focus_handler)
1502                         (*window->keyboard_focus_handler)(window,
1503                                                           window->keyboard_device,
1504                                                           window->user_data);
1505         }
1506 }
1507
1508 static void
1509 input_handle_touch_down(void *data,
1510                         struct wl_input_device *wl_input_device,
1511                         uint32_t time, struct wl_surface *surface,
1512                         int32_t id, int32_t x, int32_t y)
1513 {
1514 }
1515
1516 static void
1517 input_handle_touch_up(void *data,
1518                       struct wl_input_device *wl_input_device,
1519                       uint32_t time, int32_t id)
1520 {
1521 }
1522
1523 static void
1524 input_handle_touch_motion(void *data,
1525                           struct wl_input_device *wl_input_device,
1526                           uint32_t time, int32_t id, int32_t x, int32_t y)
1527 {
1528 }
1529
1530 static void
1531 input_handle_touch_frame(void *data,
1532                          struct wl_input_device *wl_input_device)
1533 {
1534 }
1535
1536 static void
1537 input_handle_touch_cancel(void *data,
1538                           struct wl_input_device *wl_input_device)
1539 {
1540 }
1541
1542 static const struct wl_input_device_listener input_device_listener = {
1543         input_handle_motion,
1544         input_handle_button,
1545         input_handle_key,
1546         input_handle_pointer_focus,
1547         input_handle_keyboard_focus,
1548         input_handle_touch_down,
1549         input_handle_touch_up,
1550         input_handle_touch_motion,
1551         input_handle_touch_frame,
1552         input_handle_touch_cancel,
1553 };
1554
1555 void
1556 input_get_position(struct input *input, int32_t *x, int32_t *y)
1557 {
1558         *x = input->sx;
1559         *y = input->sy;
1560 }
1561
1562 struct wl_input_device *
1563 input_get_input_device(struct input *input)
1564 {
1565         return input->input_device;
1566 }
1567
1568 uint32_t
1569 input_get_modifiers(struct input *input)
1570 {
1571         return input->modifiers;
1572 }
1573
1574 struct data_offer {
1575         struct wl_data_offer *offer;
1576         struct input *input;
1577         struct wl_array types;
1578         int refcount;
1579
1580         struct task io_task;
1581         int fd;
1582         data_func_t func;
1583         int32_t x, y;
1584         void *user_data;
1585 };
1586
1587 static void
1588 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
1589 {
1590         struct data_offer *offer = data;
1591         char **p;
1592
1593         p = wl_array_add(&offer->types, sizeof *p);
1594         *p = strdup(type);
1595 }
1596
1597 static const struct wl_data_offer_listener data_offer_listener = {
1598         data_offer_offer,
1599 };
1600
1601 static void
1602 data_offer_destroy(struct data_offer *offer)
1603 {
1604         char **p;
1605
1606         offer->refcount--;
1607         if (offer->refcount == 0) {
1608                 wl_data_offer_destroy(offer->offer);
1609                 for (p = offer->types.data; *p; p++)
1610                         free(*p);
1611                 wl_array_release(&offer->types);
1612                 free(offer);
1613         }
1614 }
1615
1616 static void
1617 data_device_data_offer(void *data,
1618                        struct wl_data_device *data_device, uint32_t id)
1619 {
1620         struct data_offer *offer;
1621
1622         offer = malloc(sizeof *offer);
1623
1624         wl_array_init(&offer->types);
1625         offer->refcount = 1;
1626         offer->input = data;
1627
1628         /* FIXME: Generate typesafe wrappers for this */
1629         offer->offer = (struct wl_data_offer *)
1630                 wl_proxy_create_for_id((struct wl_proxy *) data_device,
1631                                        id, &wl_data_offer_interface);
1632
1633         wl_data_offer_add_listener(offer->offer,
1634                                    &data_offer_listener, offer);
1635 }
1636
1637 static void
1638 data_device_enter(void *data, struct wl_data_device *data_device,
1639                   uint32_t time, struct wl_surface *surface,
1640                   int32_t x, int32_t y, struct wl_data_offer *offer)
1641 {
1642         struct input *input = data;
1643         struct window *window;
1644         char **p;
1645
1646         input->drag_offer = wl_data_offer_get_user_data(offer);
1647         window = wl_surface_get_user_data(surface);
1648         input->pointer_focus = window;
1649
1650         p = wl_array_add(&input->drag_offer->types, sizeof *p);
1651         *p = NULL;
1652
1653         window = input->pointer_focus;
1654         if (window->data_handler)
1655                 window->data_handler(window, input, time, x, y,
1656                                      input->drag_offer->types.data,
1657                                      window->user_data);
1658 }
1659
1660 static void
1661 data_device_leave(void *data, struct wl_data_device *data_device)
1662 {
1663         struct input *input = data;
1664
1665         data_offer_destroy(input->drag_offer);
1666         input->drag_offer = NULL;
1667 }
1668
1669 static void
1670 data_device_motion(void *data, struct wl_data_device *data_device,
1671                    uint32_t time, int32_t x, int32_t y)
1672 {
1673         struct input *input = data;
1674         struct window *window = input->pointer_focus;
1675
1676         input->sx = x;
1677         input->sy = y;
1678
1679         if (window->data_handler)
1680                 window->data_handler(window, input, time, x, y,
1681                                      input->drag_offer->types.data,
1682                                      window->user_data);
1683 }
1684
1685 static void
1686 data_device_drop(void *data, struct wl_data_device *data_device)
1687 {
1688         struct input *input = data;
1689         struct window *window = input->pointer_focus;
1690
1691         if (window->drop_handler)
1692                 window->drop_handler(window, input,
1693                                      input->sx, input->sy, window->user_data);
1694 }
1695
1696 static void
1697 data_device_selection(void *data,
1698                       struct wl_data_device *wl_data_device,
1699                       struct wl_data_offer *offer)
1700 {
1701         struct input *input = data;
1702         char **p;
1703
1704         if (input->selection_offer)
1705                 data_offer_destroy(input->selection_offer);
1706
1707         input->selection_offer = wl_data_offer_get_user_data(offer);
1708         p = wl_array_add(&input->selection_offer->types, sizeof *p);
1709         *p = NULL;
1710 }
1711
1712 static const struct wl_data_device_listener data_device_listener = {
1713         data_device_data_offer,
1714         data_device_enter,
1715         data_device_leave,
1716         data_device_motion,
1717         data_device_drop,
1718         data_device_selection
1719 };
1720
1721 struct wl_data_device *
1722 input_get_data_device(struct input *input)
1723 {
1724         return input->data_device;
1725 }
1726
1727 void
1728 input_set_selection(struct input *input,
1729                     struct wl_data_source *source, uint32_t time)
1730 {
1731         wl_data_device_set_selection(input->data_device, source, time);
1732 }
1733
1734 void
1735 input_accept(struct input *input, uint32_t time, const char *type)
1736 {
1737         wl_data_offer_accept(input->drag_offer->offer, time, type);
1738 }
1739
1740 static void
1741 offer_io_func(struct task *task, uint32_t events)
1742 {
1743         struct data_offer *offer =
1744                 container_of(task, struct data_offer, io_task);
1745         unsigned int len;
1746         char buffer[4096];
1747
1748         len = read(offer->fd, buffer, sizeof buffer);
1749         offer->func(buffer, len,
1750                     offer->x, offer->y, offer->user_data);
1751
1752         if (len == 0) {
1753                 close(offer->fd);
1754                 data_offer_destroy(offer);
1755         }
1756 }
1757
1758 static void
1759 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
1760                         data_func_t func, void *user_data)
1761 {
1762         int p[2];
1763
1764         pipe2(p, O_CLOEXEC);
1765         wl_data_offer_receive(offer->offer, mime_type, p[1]);
1766         close(p[1]);
1767
1768         offer->io_task.run = offer_io_func;
1769         offer->fd = p[0];
1770         offer->func = func;
1771         offer->refcount++;
1772         offer->user_data = user_data;
1773
1774         display_watch_fd(offer->input->display,
1775                          offer->fd, EPOLLIN, &offer->io_task);
1776 }
1777
1778 void
1779 input_receive_drag_data(struct input *input, const char *mime_type,
1780                         data_func_t func, void *data)
1781 {
1782         data_offer_receive_data(input->drag_offer, mime_type, func, data);
1783         input->drag_offer->x = input->sx;
1784         input->drag_offer->y = input->sy;
1785 }
1786
1787 int
1788 input_receive_selection_data(struct input *input, const char *mime_type,
1789                              data_func_t func, void *data)
1790 {
1791         char **p;
1792
1793         if (input->selection_offer == NULL)
1794                 return -1;
1795
1796         for (p = input->selection_offer->types.data; *p; p++)
1797                 if (strcmp(mime_type, *p) == 0)
1798                         break;
1799
1800         if (*p == NULL)
1801                 return -1;
1802
1803         data_offer_receive_data(input->selection_offer,
1804                                 mime_type, func, data);
1805         return 0;
1806 }
1807
1808 int
1809 input_receive_selection_data_to_fd(struct input *input,
1810                                    const char *mime_type, int fd)
1811 {
1812         wl_data_offer_receive(input->selection_offer->offer, mime_type, fd);
1813
1814         return 0;
1815 }
1816
1817 void
1818 window_move(struct window *window, struct input *input, uint32_t time)
1819 {
1820         if (!window->shell_surface)
1821                 return;
1822
1823         wl_shell_surface_move(window->shell_surface,
1824                               input->input_device, time);
1825 }
1826
1827 static void
1828 handle_configure(void *data, struct wl_shell_surface *shell_surface,
1829                  uint32_t time, uint32_t edges,
1830                  int32_t width, int32_t height)
1831 {
1832         struct window *window = data;
1833         int32_t child_width, child_height;
1834
1835         /* FIXME: this is probably the wrong place to check for width
1836          * or height <= 0, but it prevents the compositor from crashing
1837          */
1838         if (width <= 0 || height <= 0)
1839                 return;
1840
1841         window->resize_edges = edges;
1842
1843         if (window->resize_handler) {
1844                 child_width = width - 20 - window->margin * 2;
1845                 child_height = height - 60 - window->margin * 2;
1846
1847                 (*window->resize_handler)(window,
1848                                           child_width, child_height,
1849                                           window->user_data);
1850         } else {
1851                 window->allocation.width = width;
1852                 window->allocation.height = height;
1853
1854                 if (window->redraw_handler)
1855                         window_schedule_redraw(window);
1856         }
1857 }
1858
1859 static void
1860 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
1861 {
1862         struct window *window = data;
1863         struct menu *menu = window_get_user_data(window);
1864         /* FIXME: Need more context in this event, at least the input
1865          * device.  Or just use wl_callback. */
1866
1867         menu->func(window->parent, menu->current, window->parent->user_data);
1868         window_destroy(window);
1869 }
1870
1871 static const struct wl_shell_surface_listener shell_surface_listener = {
1872         handle_configure,
1873         handle_popup_done
1874 };
1875
1876 void
1877 window_get_allocation(struct window *window,
1878                       struct rectangle *allocation)
1879 {
1880         *allocation = window->allocation;
1881 }
1882
1883 void
1884 window_get_child_allocation(struct window *window,
1885                             struct rectangle *allocation)
1886 {
1887         if (!window->decoration) {
1888                 *allocation = window->allocation;
1889         } else {
1890                 allocation->x = window->margin + 10;
1891                 allocation->y = window->margin + 50;
1892                 allocation->width =
1893                         window->allocation.width - 20 - window->margin * 2;
1894                 allocation->height =
1895                         window->allocation.height - 60 - window->margin * 2;
1896         }
1897 }
1898
1899 void
1900 window_set_child_size(struct window *window, int32_t width, int32_t height)
1901 {
1902         if (window->decoration) {
1903                 window->allocation.x = 20 + window->margin;
1904                 window->allocation.y = 60 + window->margin;
1905                 window->allocation.width = width + 20 + window->margin * 2;
1906                 window->allocation.height = height + 60 + window->margin * 2;
1907         } else {
1908                 window->allocation.x = 0;
1909                 window->allocation.y = 0;
1910                 window->allocation.width = width;
1911                 window->allocation.height = height;
1912         }
1913 }
1914
1915 static void
1916 idle_redraw(struct task *task, uint32_t events)
1917 {
1918         struct window *window =
1919                 container_of(task, struct window, redraw_task);
1920
1921         window->redraw_handler(window, window->user_data);
1922         window->redraw_scheduled = 0;
1923 }
1924
1925 void
1926 window_schedule_redraw(struct window *window)
1927 {
1928         if (!window->redraw_scheduled) {
1929                 window->redraw_task.run = idle_redraw;
1930                 display_defer(window->display, &window->redraw_task);
1931                 window->redraw_scheduled = 1;
1932         }
1933 }
1934
1935 void
1936 window_set_custom(struct window *window)
1937 {
1938         window->type = TYPE_CUSTOM;
1939 }
1940
1941 void
1942 window_set_fullscreen(struct window *window, int fullscreen)
1943 {
1944         int32_t width, height;
1945         struct output *output;
1946
1947         if ((window->type == TYPE_FULLSCREEN) == fullscreen)
1948                 return;
1949
1950         if (fullscreen) {
1951                 output = display_get_output(window->display);
1952                 window->type = TYPE_FULLSCREEN;
1953                 window->saved_allocation = window->allocation;
1954                 width = output->allocation.width;
1955                 height = output->allocation.height;
1956                 window->decoration = 0;
1957         } else {
1958                 window->type = TYPE_TOPLEVEL;
1959                 width = window->saved_allocation.width - 20 - window->margin * 2;
1960                 height = window->saved_allocation.height - 60 - window->margin * 2;
1961                 window->decoration = 1;
1962         }
1963
1964         (*window->resize_handler)(window, width, height, window->user_data);
1965 }
1966
1967 void
1968 window_set_decoration(struct window *window, int decoration)
1969 {
1970         window->decoration = decoration;
1971 }
1972
1973 void
1974 window_set_user_data(struct window *window, void *data)
1975 {
1976         window->user_data = data;
1977 }
1978
1979 void *
1980 window_get_user_data(struct window *window)
1981 {
1982         return window->user_data;
1983 }
1984
1985 void
1986 window_set_resize_handler(struct window *window,
1987                           window_resize_handler_t handler)
1988 {
1989         window->resize_handler = handler;
1990 }
1991
1992 void
1993 window_set_redraw_handler(struct window *window,
1994                           window_redraw_handler_t handler)
1995 {
1996         window->redraw_handler = handler;
1997 }
1998
1999 void
2000 window_set_key_handler(struct window *window,
2001                        window_key_handler_t handler)
2002 {
2003         window->key_handler = handler;
2004 }
2005
2006 void
2007 window_set_button_handler(struct window *window,
2008                           window_button_handler_t handler)
2009 {
2010         window->button_handler = handler;
2011 }
2012
2013 void
2014 window_set_motion_handler(struct window *window,
2015                           window_motion_handler_t handler)
2016 {
2017         window->motion_handler = handler;
2018 }
2019
2020 void
2021 window_set_enter_handler(struct window *window,
2022                           window_enter_handler_t handler)
2023 {
2024         window->enter_handler = handler;
2025 }
2026
2027 void
2028 window_set_leave_handler(struct window *window,
2029                           window_leave_handler_t handler)
2030 {
2031         window->leave_handler = handler;
2032 }
2033
2034 void
2035 window_set_keyboard_focus_handler(struct window *window,
2036                                   window_keyboard_focus_handler_t handler)
2037 {
2038         window->keyboard_focus_handler = handler;
2039 }
2040
2041 void
2042 window_set_widget_focus_handler(struct window *window,
2043                                 window_widget_focus_handler_t handler)
2044 {
2045         window->widget_focus_handler = handler;
2046 }
2047
2048 void
2049 window_set_data_handler(struct window *window, window_data_handler_t handler)
2050 {
2051         window->data_handler = handler;
2052 }
2053
2054 void
2055 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
2056 {
2057         window->drop_handler = handler;
2058 }
2059
2060 void
2061 window_set_close_handler(struct window *window,
2062                          window_close_handler_t handler)
2063 {
2064         window->close_handler = handler;
2065 }
2066
2067 void
2068 window_set_transparent(struct window *window, int transparent)
2069 {
2070         window->transparent = transparent;
2071 }
2072
2073 void
2074 window_set_title(struct window *window, const char *title)
2075 {
2076         free(window->title);
2077         window->title = strdup(title);
2078 }
2079
2080 const char *
2081 window_get_title(struct window *window)
2082 {
2083         return window->title;
2084 }
2085
2086 void
2087 display_surface_damage(struct display *display, cairo_surface_t *cairo_surface,
2088                        int32_t x, int32_t y, int32_t width, int32_t height)
2089 {
2090         struct wl_buffer *buffer;
2091
2092         buffer = display_get_buffer_for_surface(display, cairo_surface);
2093
2094         wl_buffer_damage(buffer, x, y, width, height);
2095 }
2096
2097 void
2098 window_damage(struct window *window, int32_t x, int32_t y,
2099               int32_t width, int32_t height)
2100 {
2101         wl_surface_damage(window->surface, x, y, width, height);
2102 }
2103
2104 static struct window *
2105 window_create_internal(struct display *display, struct window *parent,
2106                         int32_t width, int32_t height)
2107 {
2108         struct window *window;
2109
2110         window = malloc(sizeof *window);
2111         if (window == NULL)
2112                 return NULL;
2113
2114         memset(window, 0, sizeof *window);
2115         window->display = display;
2116         window->parent = parent;
2117         window->surface = wl_compositor_create_surface(display->compositor);
2118         if (display->shell) {
2119                 window->shell_surface =
2120                         wl_shell_get_shell_surface(display->shell,
2121                                                    window->surface);
2122         }
2123         window->allocation.x = 0;
2124         window->allocation.y = 0;
2125         window->allocation.width = width;
2126         window->allocation.height = height;
2127         window->saved_allocation = window->allocation;
2128         window->margin = 16;
2129         window->decoration = 1;
2130         window->transparent = 1;
2131         wl_list_init(&window->widget_list);
2132
2133         if (display->dpy)
2134 #ifdef HAVE_CAIRO_EGL
2135                 /* FIXME: make TYPE_EGL_IMAGE choosable for testing */
2136                 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
2137 #else
2138                 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2139 #endif
2140         else
2141                 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
2142
2143         wl_surface_set_user_data(window->surface, window);
2144         wl_list_insert(display->window_list.prev, &window->link);
2145
2146         if (window->shell_surface) {
2147                 wl_shell_surface_set_user_data(window->shell_surface, window);
2148                 wl_shell_surface_add_listener(window->shell_surface,
2149                                               &shell_surface_listener, window);
2150         }
2151
2152         return window;
2153 }
2154
2155 struct window *
2156 window_create(struct display *display, int32_t width, int32_t height)
2157 {
2158         struct window *window;
2159
2160         window = window_create_internal(display, NULL, width, height);
2161         if (!window)
2162                 return NULL;
2163
2164         return window;
2165 }
2166
2167 struct window *
2168 window_create_transient(struct display *display, struct window *parent,
2169                         int32_t x, int32_t y, int32_t width, int32_t height)
2170 {
2171         struct window *window;
2172
2173         window = window_create_internal(parent->display,
2174                                         parent, width, height);
2175         if (!window)
2176                 return NULL;
2177
2178         window->type = TYPE_TRANSIENT;
2179         window->x = x;
2180         window->y = y;
2181
2182         return window;
2183 }
2184
2185 static int
2186 menu_set_widget(struct window *window, struct menu *menu, int sy)
2187 {
2188         int next;
2189
2190         next = (sy - 8) / 20;
2191         if (menu->current != next) {
2192                 menu->current = next;
2193                 window_schedule_redraw(window);
2194         }
2195
2196         return POINTER_LEFT_PTR;
2197 }
2198
2199 static int
2200 menu_motion_handler(struct window *window,
2201                     struct input *input, uint32_t time,
2202                     int32_t x, int32_t y,
2203                     int32_t sx, int32_t sy, void *data)
2204 {
2205         return menu_set_widget(window, data, sy);
2206 }
2207
2208 static int
2209 menu_enter_handler(struct window *window,
2210                     struct input *input, uint32_t time,
2211                     int32_t x, int32_t y, void *data)
2212 {
2213         return menu_set_widget(window, data, y);
2214 }
2215
2216 static void
2217 menu_leave_handler(struct window *window,
2218                    struct input *input, uint32_t time, void *data)
2219 {
2220         menu_set_widget(window, data, -200);
2221 }
2222
2223 static void
2224 menu_button_handler(struct window *window,
2225                     struct input *input, uint32_t time,
2226                     int button, int state, void *data)
2227
2228 {
2229         struct menu *menu = data;
2230
2231         /* Either relase after press-drag-release or click-motion-click. */
2232         if (state == 0 && time - menu->time > 500) {
2233                 menu->func(window->parent, 
2234                            menu->current, window->parent->user_data);
2235                 window_destroy(window);
2236         }
2237 }
2238
2239 static void
2240 menu_redraw_handler(struct window *window, void *data)
2241 {
2242         cairo_t *cr;
2243         const int32_t r = 3, margin = 3;
2244         struct menu *menu = data;
2245         int32_t width, height, i;
2246
2247         width = 200;
2248         height = menu->count * 20 + margin * 2;
2249         window_set_child_size(window, width, height);
2250         window_create_surface(window);
2251
2252         cr = cairo_create(window->cairo_surface);
2253         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2254         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
2255         cairo_paint(cr);
2256
2257         width = window->allocation.width;
2258         height = window->allocation.height;
2259         rounded_rect(cr, 0, 0, width, height, r);
2260         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2261         cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
2262         cairo_fill(cr);
2263
2264         for (i = 0; i < menu->count; i++) {
2265                 if (i == menu->current) {
2266                         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2267                         cairo_rectangle(cr, margin, i * 20 + margin,
2268                                         width - 2 * margin, 20);
2269                         cairo_fill(cr);
2270                         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
2271                         cairo_move_to(cr, 10, i * 20 + 16);
2272                         cairo_show_text(cr, menu->entries[i]);
2273                 } else {
2274                         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2275                         cairo_move_to(cr, 10, i * 20 + 16);
2276                         cairo_show_text(cr, menu->entries[i]);
2277                 }
2278         }
2279
2280         cairo_destroy(cr);
2281         window_flush(window);
2282 }
2283
2284 struct window *
2285 window_create_menu(struct display *display,
2286                    struct input *input, uint32_t time, struct window *parent,
2287                    int32_t x, int32_t y,
2288                    menu_func_t func, const char **entries, int count)
2289 {
2290         struct window *window;
2291         struct menu *menu;
2292
2293         menu = malloc(sizeof *menu);
2294         if (!menu)
2295                 return NULL;
2296
2297         window = window_create_internal(parent->display, parent, 0, 0);
2298         if (!window)
2299                 return NULL;
2300
2301         menu->window = window;
2302         menu->entries = entries;
2303         menu->count = count;
2304         menu->time = time;
2305         menu->func = func;
2306         window->decoration = 0;
2307         window->type = TYPE_MENU;
2308         window->x = x;
2309         window->y = y;
2310
2311         wl_shell_surface_set_popup(window->shell_surface,
2312                                    input->input_device, time,
2313                                    window->parent->shell_surface,
2314                                    window->x, window->y, 0);
2315
2316         window_set_motion_handler(window, menu_motion_handler);
2317         window_set_enter_handler(window, menu_enter_handler);
2318         window_set_leave_handler(window, menu_leave_handler);
2319         window_set_button_handler(window, menu_button_handler);
2320         window_set_redraw_handler(window, menu_redraw_handler);
2321         window_set_user_data(window, menu);
2322
2323         return window;
2324 }
2325
2326 void
2327 window_set_buffer_type(struct window *window, enum window_buffer_type type)
2328 {
2329         window->buffer_type = type;
2330 }
2331
2332
2333 static void
2334 display_handle_geometry(void *data,
2335                         struct wl_output *wl_output,
2336                         int x, int y,
2337                         int physical_width,
2338                         int physical_height,
2339                         int subpixel,
2340                         const char *make,
2341                         const char *model)
2342 {
2343         struct output *output = data;
2344
2345         output->allocation.x = x;
2346         output->allocation.y = y;
2347 }
2348
2349 static void
2350 display_handle_mode(void *data,
2351                     struct wl_output *wl_output,
2352                     uint32_t flags,
2353                     int width,
2354                     int height,
2355                     int refresh)
2356 {
2357         struct output *output = data;
2358         struct display *display = output->display;
2359
2360         if (flags & WL_OUTPUT_MODE_CURRENT) {
2361                 output->allocation.width = width;
2362                 output->allocation.height = height;
2363                 if (display->output_configure_handler)
2364                         (*display->output_configure_handler)(
2365                                                 output, display->user_data);
2366         }
2367 }
2368
2369 static const struct wl_output_listener output_listener = {
2370         display_handle_geometry,
2371         display_handle_mode
2372 };
2373
2374 static void
2375 display_add_output(struct display *d, uint32_t id)
2376 {
2377         struct output *output;
2378
2379         output = malloc(sizeof *output);
2380         if (output == NULL)
2381                 return;
2382
2383         memset(output, 0, sizeof *output);
2384         output->display = d;
2385         output->output =
2386                 wl_display_bind(d->display, id, &wl_output_interface);
2387         wl_list_insert(d->output_list.prev, &output->link);
2388
2389         wl_output_add_listener(output->output, &output_listener, output);
2390 }
2391
2392 static void
2393 output_destroy(struct output *output)
2394 {
2395         if (output->destroy_handler)
2396                 (*output->destroy_handler)(output, output->user_data);
2397
2398         wl_output_destroy(output->output);
2399         wl_list_remove(&output->link);
2400         free(output);
2401 }
2402
2403 void
2404 display_set_output_configure_handler(struct display *display,
2405                                      display_output_handler_t handler)
2406 {
2407         struct output *output;
2408
2409         display->output_configure_handler = handler;
2410         if (!handler)
2411                 return;
2412
2413         wl_list_for_each(output, &display->output_list, link)
2414                 (*display->output_configure_handler)(output,
2415                                                      display->user_data);
2416 }
2417
2418 void
2419 output_set_user_data(struct output *output, void *data)
2420 {
2421         output->user_data = data;
2422 }
2423
2424 void *
2425 output_get_user_data(struct output *output)
2426 {
2427         return output->user_data;
2428 }
2429
2430 void
2431 output_set_destroy_handler(struct output *output,
2432                            display_output_handler_t handler)
2433 {
2434         output->destroy_handler = handler;
2435         /* FIXME: implement this, once we have way to remove outputs */
2436 }
2437
2438 void
2439 output_get_allocation(struct output *output, struct rectangle *allocation)
2440 {
2441         *allocation = output->allocation;
2442 }
2443
2444 struct wl_output *
2445 output_get_wl_output(struct output *output)
2446 {
2447         return output->output;
2448 }
2449
2450 static void
2451 display_add_input(struct display *d, uint32_t id)
2452 {
2453         struct input *input;
2454
2455         input = malloc(sizeof *input);
2456         if (input == NULL)
2457                 return;
2458
2459         memset(input, 0, sizeof *input);
2460         input->display = d;
2461         input->input_device =
2462                 wl_display_bind(d->display, id, &wl_input_device_interface);
2463         input->pointer_focus = NULL;
2464         input->keyboard_focus = NULL;
2465         wl_list_insert(d->input_list.prev, &input->link);
2466
2467         wl_input_device_add_listener(input->input_device,
2468                                      &input_device_listener, input);
2469         wl_input_device_set_user_data(input->input_device, input);
2470
2471         input->data_device =
2472                 wl_data_device_manager_get_data_device(d->data_device_manager,
2473                                                        input->input_device);
2474         wl_data_device_add_listener(input->data_device,
2475                                     &data_device_listener, input);
2476 }
2477
2478 static void
2479 input_destroy(struct input *input)
2480 {
2481         input_remove_keyboard_focus(input);
2482         input_remove_pointer_focus(input, 0);
2483
2484         if (input->drag_offer)
2485                 data_offer_destroy(input->drag_offer);
2486
2487         if (input->selection_offer)
2488                 data_offer_destroy(input->selection_offer);
2489
2490         wl_data_device_destroy(input->data_device);
2491         wl_list_remove(&input->link);
2492         wl_input_device_destroy(input->input_device);
2493         free(input);
2494 }
2495
2496 static void
2497 display_handle_global(struct wl_display *display, uint32_t id,
2498                       const char *interface, uint32_t version, void *data)
2499 {
2500         struct display *d = data;
2501
2502         if (strcmp(interface, "wl_compositor") == 0) {
2503                 d->compositor =
2504                         wl_display_bind(display, id, &wl_compositor_interface);
2505         } else if (strcmp(interface, "wl_output") == 0) {
2506                 display_add_output(d, id);
2507         } else if (strcmp(interface, "wl_input_device") == 0) {
2508                 display_add_input(d, id);
2509         } else if (strcmp(interface, "wl_shell") == 0) {
2510                 d->shell = wl_display_bind(display, id, &wl_shell_interface);
2511         } else if (strcmp(interface, "wl_shm") == 0) {
2512                 d->shm = wl_display_bind(display, id, &wl_shm_interface);
2513         } else if (strcmp(interface, "wl_data_device_manager") == 0) {
2514                 d->data_device_manager =
2515                         wl_display_bind(display, id,
2516                                         &wl_data_device_manager_interface);
2517         }
2518 }
2519
2520 static void
2521 display_render_frame(struct display *d)
2522 {
2523         int radius = 8;
2524         cairo_t *cr;
2525
2526         d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2527         cr = cairo_create(d->shadow);
2528         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2529         cairo_set_source_rgba(cr, 0, 0, 0, 1);
2530         rounded_rect(cr, 16, 16, 112, 112, radius);
2531         cairo_fill(cr);
2532         cairo_destroy(cr);
2533         blur_surface(d->shadow, 64);
2534
2535         d->active_frame =
2536                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2537         cr = cairo_create(d->active_frame);
2538         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2539         cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
2540         rounded_rect(cr, 16, 16, 112, 112, radius);
2541         cairo_fill(cr);
2542         cairo_destroy(cr);
2543
2544         d->inactive_frame =
2545                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
2546         cr = cairo_create(d->inactive_frame);
2547         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2548         cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
2549         rounded_rect(cr, 16, 16, 112, 112, radius);
2550         cairo_fill(cr);
2551         cairo_destroy(cr);
2552 }
2553
2554 static void
2555 init_xkb(struct display *d)
2556 {
2557         struct xkb_rule_names names;
2558
2559         names.rules = "evdev";
2560         names.model = "pc105";
2561         names.layout = option_xkb_layout;
2562         names.variant = option_xkb_variant;
2563         names.options = option_xkb_options;
2564
2565         d->xkb = xkb_compile_keymap_from_rules(&names);
2566         if (!d->xkb) {
2567                 fprintf(stderr, "Failed to compile keymap\n");
2568                 exit(1);
2569         }
2570 }
2571
2572 static void
2573 fini_xkb(struct display *display)
2574 {
2575         xkb_free_keymap(display->xkb);
2576 }
2577
2578 static int
2579 init_egl(struct display *d)
2580 {
2581         EGLint major, minor;
2582         EGLint n;
2583
2584         static const EGLint premul_argb_cfg_attribs[] = {
2585                 EGL_SURFACE_TYPE,
2586                         EGL_WINDOW_BIT | EGL_PIXMAP_BIT |
2587                         EGL_VG_ALPHA_FORMAT_PRE_BIT,
2588                 EGL_RED_SIZE, 1,
2589                 EGL_GREEN_SIZE, 1,
2590                 EGL_BLUE_SIZE, 1,
2591                 EGL_ALPHA_SIZE, 1,
2592                 EGL_DEPTH_SIZE, 1,
2593                 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2594                 EGL_NONE
2595         };
2596
2597         static const EGLint rgb_cfg_attribs[] = {
2598                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
2599                 EGL_RED_SIZE, 1,
2600                 EGL_GREEN_SIZE, 1,
2601                 EGL_BLUE_SIZE, 1,
2602                 EGL_ALPHA_SIZE, 0,
2603                 EGL_DEPTH_SIZE, 1,
2604                 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
2605                 EGL_NONE
2606         };
2607
2608         d->dpy = eglGetDisplay(d->display);
2609         if (!eglInitialize(d->dpy, &major, &minor)) {
2610                 fprintf(stderr, "failed to initialize display\n");
2611                 return -1;
2612         }
2613
2614         if (!eglBindAPI(EGL_OPENGL_API)) {
2615                 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
2616                 return -1;
2617         }
2618
2619         if (!eglChooseConfig(d->dpy, premul_argb_cfg_attribs,
2620                              &d->premultiplied_argb_config, 1, &n) || n != 1) {
2621                 fprintf(stderr, "failed to choose premul argb config\n");
2622                 return -1;
2623         }
2624
2625         if (!eglChooseConfig(d->dpy, rgb_cfg_attribs,
2626                              &d->rgb_config, 1, &n) || n != 1) {
2627                 fprintf(stderr, "failed to choose rgb config\n");
2628                 return -1;
2629         }
2630
2631         d->rgb_ctx = eglCreateContext(d->dpy, d->rgb_config, EGL_NO_CONTEXT, NULL);
2632         if (d->rgb_ctx == NULL) {
2633                 fprintf(stderr, "failed to create context\n");
2634                 return -1;
2635         }
2636         d->argb_ctx = eglCreateContext(d->dpy, d->premultiplied_argb_config,
2637                                        EGL_NO_CONTEXT, NULL);
2638         if (d->argb_ctx == NULL) {
2639                 fprintf(stderr, "failed to create context\n");
2640                 return -1;
2641         }
2642
2643         if (!eglMakeCurrent(d->dpy, NULL, NULL, d->rgb_ctx)) {
2644                 fprintf(stderr, "failed to make context current\n");
2645                 return -1;
2646         }
2647
2648 #ifdef HAVE_CAIRO_EGL
2649         d->rgb_device = cairo_egl_device_create(d->dpy, d->rgb_ctx);
2650         if (cairo_device_status(d->rgb_device) != CAIRO_STATUS_SUCCESS) {
2651                 fprintf(stderr, "failed to get cairo egl device\n");
2652                 return -1;
2653         }
2654         d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
2655         if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
2656                 fprintf(stderr, "failed to get cairo egl argb device\n");
2657                 return -1;
2658         }
2659 #endif
2660
2661         return 0;
2662 }
2663
2664 static void
2665 fini_egl(struct display *display)
2666 {
2667 #ifdef HAVE_CAIRO_EGL
2668         cairo_device_destroy(display->argb_device);
2669         cairo_device_destroy(display->rgb_device);
2670 #endif
2671
2672         eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
2673                        EGL_NO_CONTEXT);
2674
2675         eglTerminate(display->dpy);
2676         eglReleaseThread();
2677 }
2678
2679 static int
2680 event_mask_update(uint32_t mask, void *data)
2681 {
2682         struct display *d = data;
2683
2684         d->mask = mask;
2685
2686         return 0;
2687 }
2688
2689 static void
2690 handle_display_data(struct task *task, uint32_t events)
2691 {
2692         struct display *display =
2693                 container_of(task, struct display, display_task);
2694         
2695         wl_display_iterate(display->display, display->mask);
2696 }
2697
2698 struct display *
2699 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
2700 {
2701         struct display *d;
2702         GOptionContext *context;
2703         GOptionGroup *xkb_option_group;
2704         GError *error;
2705
2706         g_type_init();
2707
2708         context = g_option_context_new(NULL);
2709         if (option_entries)
2710                 g_option_context_add_main_entries(context, option_entries, "Wayland View");
2711
2712         xkb_option_group = g_option_group_new("xkb",
2713                                               "Keyboard options",
2714                                               "Show all XKB options",
2715                                               NULL, NULL);
2716         g_option_group_add_entries(xkb_option_group, xkb_option_entries);
2717         g_option_context_add_group (context, xkb_option_group);
2718
2719         if (!g_option_context_parse(context, argc, argv, &error)) {
2720                 fprintf(stderr, "option parsing failed: %s\n", error->message);
2721                 exit(EXIT_FAILURE);
2722         }
2723
2724         g_option_context_free(context);
2725
2726         d = malloc(sizeof *d);
2727         if (d == NULL)
2728                 return NULL;
2729
2730         memset(d, 0, sizeof *d);
2731
2732         d->display = wl_display_connect(NULL);
2733         if (d->display == NULL) {
2734                 fprintf(stderr, "failed to create display: %m\n");
2735                 return NULL;
2736         }
2737
2738         d->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2739         d->display_fd = wl_display_get_fd(d->display, event_mask_update, d);
2740         d->display_task.run = handle_display_data;
2741         display_watch_fd(d, d->display_fd, EPOLLIN, &d->display_task);
2742
2743         wl_list_init(&d->deferred_list);
2744         wl_list_init(&d->input_list);
2745         wl_list_init(&d->output_list);
2746
2747         /* Set up listener so we'll catch all events. */
2748         wl_display_add_global_listener(d->display,
2749                                        display_handle_global, d);
2750
2751         /* Process connection events. */
2752         wl_display_iterate(d->display, WL_DISPLAY_READABLE);
2753         if (init_egl(d) < 0)
2754                 return NULL;
2755
2756         d->image_target_texture_2d =
2757                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2758         d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
2759         d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
2760
2761         create_pointer_surfaces(d);
2762
2763         display_render_frame(d);
2764
2765         wl_list_init(&d->window_list);
2766
2767         init_xkb(d);
2768
2769         return d;
2770 }
2771
2772 static void
2773 display_destroy_outputs(struct display *display)
2774 {
2775         struct output *tmp;
2776         struct output *output;
2777
2778         wl_list_for_each_safe(output, tmp, &display->output_list, link)
2779                 output_destroy(output);
2780 }
2781
2782 static void
2783 display_destroy_inputs(struct display *display)
2784 {
2785         struct input *tmp;
2786         struct input *input;
2787
2788         wl_list_for_each_safe(input, tmp, &display->input_list, link)
2789                 input_destroy(input);
2790 }
2791
2792 void
2793 display_destroy(struct display *display)
2794 {
2795         if (!wl_list_empty(&display->window_list))
2796                 fprintf(stderr, "toytoolkit warning: windows exist.\n");
2797
2798         if (!wl_list_empty(&display->deferred_list))
2799                 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
2800
2801         display_destroy_outputs(display);
2802         display_destroy_inputs(display);
2803
2804         fini_xkb(display);
2805
2806         cairo_surface_destroy(display->active_frame);
2807         cairo_surface_destroy(display->inactive_frame);
2808         cairo_surface_destroy(display->shadow);
2809         destroy_pointer_surfaces(display);
2810
2811         fini_egl(display);
2812
2813         if (display->shell)
2814                 wl_shell_destroy(display->shell);
2815
2816         if (display->shm)
2817                 wl_shm_destroy(display->shm);
2818
2819         if (display->data_device_manager)
2820                 wl_data_device_manager_destroy(display->data_device_manager);
2821
2822         wl_compositor_destroy(display->compositor);
2823
2824         close(display->epoll_fd);
2825
2826         wl_display_flush(display->display);
2827         wl_display_destroy(display->display);
2828         free(display);
2829 }
2830
2831 void
2832 display_set_user_data(struct display *display, void *data)
2833 {
2834         display->user_data = data;
2835 }
2836
2837 void *
2838 display_get_user_data(struct display *display)
2839 {
2840         return display->user_data;
2841 }
2842
2843 struct wl_display *
2844 display_get_display(struct display *display)
2845 {
2846         return display->display;
2847 }
2848
2849 struct output *
2850 display_get_output(struct display *display)
2851 {
2852         return container_of(display->output_list.next, struct output, link);
2853 }
2854
2855 struct wl_compositor *
2856 display_get_compositor(struct display *display)
2857 {
2858         return display->compositor;
2859 }
2860
2861 EGLDisplay
2862 display_get_egl_display(struct display *d)
2863 {
2864         return d->dpy;
2865 }
2866
2867 struct wl_data_source *
2868 display_create_data_source(struct display *display)
2869 {
2870         return wl_data_device_manager_create_data_source(display->data_device_manager);
2871 }
2872
2873 EGLConfig
2874 display_get_rgb_egl_config(struct display *d)
2875 {
2876         return d->rgb_config;
2877 }
2878
2879 EGLConfig
2880 display_get_argb_egl_config(struct display *d)
2881 {
2882         return d->premultiplied_argb_config;
2883 }
2884
2885 struct wl_shell *
2886 display_get_shell(struct display *display)
2887 {
2888         return display->shell;
2889 }
2890
2891 int
2892 display_acquire_window_surface(struct display *display,
2893                                struct window *window,
2894                                EGLContext ctx)
2895 {
2896 #ifdef HAVE_CAIRO_EGL
2897         struct egl_window_surface_data *data;
2898         cairo_device_t *device;
2899
2900         if (!window->cairo_surface)
2901                 return -1;
2902         device = cairo_surface_get_device(window->cairo_surface);
2903         if (!device)
2904                 return -1;
2905
2906         if (!ctx) {
2907                 if (device == display->rgb_device)
2908                         ctx = display->rgb_ctx;
2909                 else if (device == display->argb_device)
2910                         ctx = display->argb_ctx;
2911                 else
2912                         assert(0);
2913         }
2914
2915         data = cairo_surface_get_user_data(window->cairo_surface,
2916                                            &surface_data_key);
2917
2918         cairo_device_flush(device);
2919         cairo_device_acquire(device);
2920         if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
2921                 fprintf(stderr, "failed to make surface current\n");
2922
2923         return 0;
2924 #else
2925         return -1;
2926 #endif
2927 }
2928
2929 void
2930 display_release_window_surface(struct display *display,
2931                                struct window *window)
2932 {
2933 #ifdef HAVE_CAIRO_EGL
2934         cairo_device_t *device;
2935         
2936         device = cairo_surface_get_device(window->cairo_surface);
2937         if (!device)
2938                 return;
2939
2940         if (!eglMakeCurrent(display->dpy, NULL, NULL, display->rgb_ctx))
2941                 fprintf(stderr, "failed to make context current\n");
2942         cairo_device_release(device);
2943 #endif
2944 }
2945
2946 void
2947 display_defer(struct display *display, struct task *task)
2948 {
2949         wl_list_insert(&display->deferred_list, &task->link);
2950 }
2951
2952 void
2953 display_watch_fd(struct display *display,
2954                  int fd, uint32_t events, struct task *task)
2955 {
2956         struct epoll_event ep;
2957
2958         ep.events = events;
2959         ep.data.ptr = task;
2960         epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
2961 }
2962
2963 void
2964 display_run(struct display *display)
2965 {
2966         struct task *task;
2967         struct epoll_event ep[16];
2968         int i, count;
2969
2970         display->running = 1;
2971         while (1) {
2972                 while (display->mask & WL_DISPLAY_WRITABLE)
2973                         wl_display_iterate(display->display,
2974                                            WL_DISPLAY_WRITABLE);
2975
2976                 if (!display->running)
2977                         break;
2978
2979                 count = epoll_wait(display->epoll_fd,
2980                                    ep, ARRAY_LENGTH(ep), -1);
2981                 for (i = 0; i < count; i++) {
2982                         task = ep[i].data.ptr;
2983                         task->run(task, ep[i].events);
2984                 }
2985
2986                 while (!wl_list_empty(&display->deferred_list)) {
2987                         task = container_of(display->deferred_list.next,
2988                                             struct task, link);
2989                         wl_list_remove(&task->link);
2990                         task->run(task, 0);
2991                 }
2992         }
2993 }
2994
2995 void
2996 display_exit(struct display *display)
2997 {
2998         display->running = 0;
2999 }