Update snapshot
[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 <errno.h>
34 #include <math.h>
35 #include <assert.h>
36 #include <time.h>
37 #include <cairo.h>
38 #include <sys/mman.h>
39 #include <sys/epoll.h>
40 #include <sys/timerfd.h>
41
42 #ifdef HAVE_CAIRO_EGL
43 #include <wayland-egl.h>
44
45 #ifdef USE_CAIRO_GLESV2
46 #include <GLES2/gl2.h>
47 #include <GLES2/gl2ext.h>
48 #else
49 #include <GL/gl.h>
50 #endif
51 #include <EGL/egl.h>
52 #include <EGL/eglext.h>
53
54 #include <cairo-gl.h>
55 #else /* HAVE_CAIRO_EGL */
56 typedef void *EGLDisplay;
57 typedef void *EGLConfig;
58 typedef void *EGLContext;
59 #define EGL_NO_DISPLAY ((EGLDisplay)0)
60 #endif /* no HAVE_CAIRO_EGL */
61
62 #include <xkbcommon/xkbcommon.h>
63 #include <wayland-cursor.h>
64
65 #include <linux/input.h>
66 #include <wayland-client.h>
67 #include "../shared/cairo-util.h"
68 #include "text-cursor-position-client-protocol.h"
69 #include "workspaces-client-protocol.h"
70 #include "../shared/os-compatibility.h"
71
72 #include "window.h"
73
74 struct shm_pool;
75
76 struct global {
77         uint32_t name;
78         char *interface;
79         uint32_t version;
80         struct wl_list link;
81 };
82
83 struct display {
84         struct wl_display *display;
85         struct wl_registry *registry;
86         struct wl_compositor *compositor;
87         struct wl_shell *shell;
88         struct wl_shm *shm;
89         struct wl_data_device_manager *data_device_manager;
90         struct text_cursor_position *text_cursor_position;
91         struct workspace_manager *workspace_manager;
92         EGLDisplay dpy;
93         EGLConfig argb_config;
94         EGLContext argb_ctx;
95         cairo_device_t *argb_device;
96         uint32_t serial;
97
98         int display_fd;
99         uint32_t display_fd_events;
100         struct task display_task;
101
102         int epoll_fd;
103         struct wl_list deferred_list;
104
105         int running;
106
107         struct wl_list global_list;
108         struct wl_list window_list;
109         struct wl_list input_list;
110         struct wl_list output_list;
111
112         struct theme *theme;
113
114         struct wl_cursor_theme *cursor_theme;
115         struct wl_cursor **cursors;
116
117         display_output_handler_t output_configure_handler;
118         display_global_handler_t global_handler;
119
120         void *user_data;
121
122         struct xkb_context *xkb_context;
123
124         uint32_t workspace;
125         uint32_t workspace_count;
126 };
127
128 enum {
129         TYPE_NONE,
130         TYPE_TOPLEVEL,
131         TYPE_FULLSCREEN,
132         TYPE_MAXIMIZED,
133         TYPE_TRANSIENT,
134         TYPE_MENU,
135         TYPE_CUSTOM
136 };
137
138 struct window_output {
139         struct output *output;
140         struct wl_list link;
141 };
142
143 struct window {
144         struct display *display;
145         struct window *parent;
146         struct wl_list window_output_list;
147         struct wl_surface *surface;
148         struct wl_shell_surface *shell_surface;
149         struct wl_region *input_region;
150         struct wl_region *opaque_region;
151         char *title;
152         struct rectangle allocation, saved_allocation, server_allocation;
153         struct rectangle min_allocation;
154         struct rectangle pending_allocation;
155         int x, y;
156         int resize_edges;
157         int redraw_scheduled;
158         int redraw_needed;
159         struct task redraw_task;
160         int resize_needed;
161         int type;
162         int transparent;
163         int focus_count;
164
165         enum window_buffer_type buffer_type;
166
167         cairo_surface_t *cairo_surface;
168
169         struct shm_pool *pool;
170
171         window_key_handler_t key_handler;
172         window_keyboard_focus_handler_t keyboard_focus_handler;
173         window_data_handler_t data_handler;
174         window_drop_handler_t drop_handler;
175         window_close_handler_t close_handler;
176         window_fullscreen_handler_t fullscreen_handler;
177
178         struct wl_callback *frame_cb;
179
180         struct frame *frame;
181         struct widget *widget;
182
183         void *user_data;
184         struct wl_list link;
185 };
186
187 struct widget {
188         struct window *window;
189         struct tooltip *tooltip;
190         struct wl_list child_list;
191         struct wl_list link;
192         struct rectangle allocation;
193         widget_resize_handler_t resize_handler;
194         widget_redraw_handler_t redraw_handler;
195         widget_enter_handler_t enter_handler;
196         widget_leave_handler_t leave_handler;
197         widget_motion_handler_t motion_handler;
198         widget_button_handler_t button_handler;
199         widget_axis_handler_t axis_handler;
200         void *user_data;
201         int opaque;
202         int tooltip_count;
203 };
204
205 struct input {
206         struct display *display;
207         struct wl_seat *seat;
208         struct wl_pointer *pointer;
209         struct wl_keyboard *keyboard;
210         struct window *pointer_focus;
211         struct window *keyboard_focus;
212         int current_cursor;
213         uint32_t cursor_anim_start;
214         struct wl_callback *cursor_frame_cb;
215         struct wl_surface *pointer_surface;
216         uint32_t modifiers;
217         uint32_t pointer_enter_serial;
218         uint32_t cursor_serial;
219         float sx, sy;
220         struct wl_list link;
221
222         struct widget *focus_widget;
223         struct widget *grab;
224         uint32_t grab_button;
225
226         struct wl_data_device *data_device;
227         struct data_offer *drag_offer;
228         struct data_offer *selection_offer;
229
230         struct {
231                 struct xkb_keymap *keymap;
232                 struct xkb_state *state;
233                 xkb_mod_mask_t control_mask;
234                 xkb_mod_mask_t alt_mask;
235                 xkb_mod_mask_t shift_mask;
236         } xkb;
237
238         struct task repeat_task;
239         int repeat_timer_fd;
240         uint32_t repeat_sym;
241         uint32_t repeat_key;
242         uint32_t repeat_time;
243 };
244
245 struct output {
246         struct display *display;
247         struct wl_output *output;
248         struct rectangle allocation;
249         struct wl_list link;
250         int transform;
251
252         display_output_handler_t destroy_handler;
253         void *user_data;
254 };
255
256 enum frame_button_action {
257         FRAME_BUTTON_NULL = 0,
258         FRAME_BUTTON_ICON = 1,
259         FRAME_BUTTON_CLOSE = 2,
260         FRAME_BUTTON_MINIMIZE = 3,
261         FRAME_BUTTON_MAXIMIZE = 4,
262 };
263
264 enum frame_button_pointer {
265         FRAME_BUTTON_DEFAULT = 0,
266         FRAME_BUTTON_OVER = 1,
267         FRAME_BUTTON_ACTIVE = 2,
268 };
269
270 enum frame_button_align {
271         FRAME_BUTTON_RIGHT = 0,
272         FRAME_BUTTON_LEFT = 1,
273 };
274
275 enum frame_button_decoration {
276         FRAME_BUTTON_NONE = 0,
277         FRAME_BUTTON_FANCY = 1,
278 };
279
280 struct frame_button {
281         struct widget *widget;
282         struct frame *frame;
283         cairo_surface_t *icon;
284         enum frame_button_action type;
285         enum frame_button_pointer state;
286         struct wl_list link;    /* buttons_list */
287         enum frame_button_align align;
288         enum frame_button_decoration decoration;
289 };
290
291 struct frame {
292         struct widget *widget;
293         struct widget *child;
294         struct wl_list buttons_list;
295 };
296
297 struct menu {
298         struct window *window;
299         struct widget *widget;
300         struct input *input;
301         const char **entries;
302         uint32_t time;
303         int current;
304         int count;
305         int release_count;
306         menu_func_t func;
307 };
308
309 struct tooltip {
310         struct widget *parent;
311         struct window *window;
312         struct widget *widget;
313         char *entry;
314         struct task tooltip_task;
315         int tooltip_fd;
316         float x, y;
317 };
318
319 struct shm_pool {
320         struct wl_shm_pool *pool;
321         size_t size;
322         size_t used;
323         void *data;
324 };
325
326 enum {
327         CURSOR_DEFAULT = 100,
328         CURSOR_UNSET
329 };
330
331 enum window_location {
332         WINDOW_INTERIOR = 0,
333         WINDOW_RESIZING_TOP = 1,
334         WINDOW_RESIZING_BOTTOM = 2,
335         WINDOW_RESIZING_LEFT = 4,
336         WINDOW_RESIZING_TOP_LEFT = 5,
337         WINDOW_RESIZING_BOTTOM_LEFT = 6,
338         WINDOW_RESIZING_RIGHT = 8,
339         WINDOW_RESIZING_TOP_RIGHT = 9,
340         WINDOW_RESIZING_BOTTOM_RIGHT = 10,
341         WINDOW_RESIZING_MASK = 15,
342         WINDOW_EXTERIOR = 16,
343         WINDOW_TITLEBAR = 17,
344         WINDOW_CLIENT_AREA = 18,
345 };
346
347 static const cairo_user_data_key_t surface_data_key;
348 struct surface_data {
349         struct wl_buffer *buffer;
350 };
351
352 #define MULT(_d,c,a,t) \
353         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
354
355 #ifdef HAVE_CAIRO_EGL
356
357 struct egl_window_surface_data {
358         struct display *display;
359         struct wl_surface *surface;
360         struct wl_egl_window *window;
361         EGLSurface surf;
362 };
363
364 static void
365 egl_window_surface_data_destroy(void *p)
366 {
367         struct egl_window_surface_data *data = p;
368         struct display *d = data->display;
369
370         eglDestroySurface(d->dpy, data->surf);
371         wl_egl_window_destroy(data->window);
372         data->surface = NULL;
373
374         free(p);
375 }
376
377 static cairo_surface_t *
378 display_create_egl_window_surface(struct display *display,
379                                   struct wl_surface *surface,
380                                   uint32_t flags,
381                                   struct rectangle *rectangle)
382 {
383         cairo_surface_t *cairo_surface;
384         struct egl_window_surface_data *data;
385         EGLConfig config;
386         cairo_device_t *device;
387
388         data = malloc(sizeof *data);
389         if (data == NULL)
390                 return NULL;
391
392         data->display = display;
393         data->surface = surface;
394
395         config = display->argb_config;
396         device = display->argb_device;
397
398         data->window = wl_egl_window_create(surface,
399                                             rectangle->width,
400                                             rectangle->height);
401
402         data->surf = eglCreateWindowSurface(display->dpy, config,
403                                             data->window, NULL);
404
405         cairo_surface = cairo_gl_surface_create_for_egl(device,
406                                                         data->surf,
407                                                         rectangle->width,
408                                                         rectangle->height);
409
410         cairo_surface_set_user_data(cairo_surface, &surface_data_key,
411                                     data, egl_window_surface_data_destroy);
412
413         return cairo_surface;
414 }
415
416 #endif
417
418 struct wl_buffer *
419 display_get_buffer_for_surface(struct display *display,
420                                cairo_surface_t *surface)
421 {
422         struct surface_data *data;
423
424         data = cairo_surface_get_user_data (surface, &surface_data_key);
425
426         return data->buffer;
427 }
428
429 struct shm_surface_data {
430         struct surface_data data;
431         struct shm_pool *pool;
432 };
433
434 static void
435 shm_pool_destroy(struct shm_pool *pool);
436
437 static void
438 shm_surface_data_destroy(void *p)
439 {
440         struct shm_surface_data *data = p;
441
442         wl_buffer_destroy(data->data.buffer);
443         if (data->pool)
444                 shm_pool_destroy(data->pool);
445
446         free(data);
447 }
448
449 static struct wl_shm_pool *
450 make_shm_pool(struct display *display, int size, void **data)
451 {
452         struct wl_shm_pool *pool;
453         int fd;
454
455         fd = os_create_anonymous_file(size);
456         if (fd < 0) {
457                 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
458                         size);
459                 return NULL;
460         }
461
462         *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
463         if (*data == MAP_FAILED) {
464                 fprintf(stderr, "mmap failed: %m\n");
465                 close(fd);
466                 return NULL;
467         }
468
469         pool = wl_shm_create_pool(display->shm, fd, size);
470
471         close(fd);
472
473         return pool;
474 }
475
476 static struct shm_pool *
477 shm_pool_create(struct display *display, size_t size)
478 {
479         struct shm_pool *pool = malloc(sizeof *pool);
480
481         if (!pool)
482                 return NULL;
483
484         pool->pool = make_shm_pool(display, size, &pool->data);
485         if (!pool->pool) {
486                 free(pool);
487                 return NULL;
488         }
489
490         pool->size = size;
491         pool->used = 0;
492
493         return pool;
494 }
495
496 static void *
497 shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
498 {
499         if (pool->used + size > pool->size)
500                 return NULL;
501
502         *offset = pool->used;
503         pool->used += size;
504
505         return (char *) pool->data + *offset;
506 }
507
508 /* destroy the pool. this does not unmap the memory though */
509 static void
510 shm_pool_destroy(struct shm_pool *pool)
511 {
512         munmap(pool->data, pool->size);
513         wl_shm_pool_destroy(pool->pool);
514         free(pool);
515 }
516
517 /* Start allocating from the beginning of the pool again */
518 static void
519 shm_pool_reset(struct shm_pool *pool)
520 {
521         pool->used = 0;
522 }
523
524 static int
525 data_length_for_shm_surface(struct rectangle *rect)
526 {
527         int stride;
528
529         stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
530                                                 rect->width);
531         return stride * rect->height;
532 }
533
534 static cairo_surface_t *
535 display_create_shm_surface_from_pool(struct display *display,
536                                      struct rectangle *rectangle,
537                                      uint32_t flags, struct shm_pool *pool)
538 {
539         struct shm_surface_data *data;
540         uint32_t format;
541         cairo_surface_t *surface;
542         int stride, length, offset;
543         void *map;
544
545         data = malloc(sizeof *data);
546         if (data == NULL)
547                 return NULL;
548
549         stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
550                                                 rectangle->width);
551         length = stride * rectangle->height;
552         data->pool = NULL;
553         map = shm_pool_allocate(pool, length, &offset);
554
555         if (!map) {
556                 free(data);
557                 return NULL;
558         }
559
560         surface = cairo_image_surface_create_for_data (map,
561                                                        CAIRO_FORMAT_ARGB32,
562                                                        rectangle->width,
563                                                        rectangle->height,
564                                                        stride);
565
566         cairo_surface_set_user_data (surface, &surface_data_key,
567                                      data, shm_surface_data_destroy);
568
569         if (flags & SURFACE_OPAQUE)
570                 format = WL_SHM_FORMAT_XRGB8888;
571         else
572                 format = WL_SHM_FORMAT_ARGB8888;
573
574         data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
575                                                       rectangle->width,
576                                                       rectangle->height,
577                                                       stride, format);
578
579         return surface;
580 }
581
582 static cairo_surface_t *
583 display_create_shm_surface(struct display *display,
584                            struct rectangle *rectangle, uint32_t flags,
585                            struct window *window)
586 {
587         struct shm_surface_data *data;
588         struct shm_pool *pool;
589         cairo_surface_t *surface;
590
591         if (window && window->pool) {
592                 shm_pool_reset(window->pool);
593                 surface = display_create_shm_surface_from_pool(display,
594                                                                rectangle,
595                                                                flags,
596                                                                window->pool);
597                 if (surface)
598                         return surface;
599         }
600
601         pool = shm_pool_create(display,
602                                data_length_for_shm_surface(rectangle));
603         if (!pool)
604                 return NULL;
605
606         surface =
607                 display_create_shm_surface_from_pool(display, rectangle,
608                                                      flags, pool);
609
610         if (!surface) {
611                 shm_pool_destroy(pool);
612                 return NULL;
613         }
614
615         /* make sure we destroy the pool when the surface is destroyed */
616         data = cairo_surface_get_user_data(surface, &surface_data_key);
617         data->pool = pool;
618
619         return surface;
620 }
621
622 static int
623 check_size(struct rectangle *rect)
624 {
625         if (rect->width && rect->height)
626                 return 0;
627
628         fprintf(stderr, "tried to create surface of "
629                 "width: %d, height: %d\n", rect->width, rect->height);
630         return -1;
631 }
632
633 cairo_surface_t *
634 display_create_surface(struct display *display,
635                        struct wl_surface *surface,
636                        struct rectangle *rectangle,
637                        uint32_t flags)
638 {
639         if (check_size(rectangle) < 0)
640                 return NULL;
641 #ifdef HAVE_CAIRO_EGL
642         if (display->dpy && !(flags & SURFACE_SHM))
643                 return display_create_egl_window_surface(display,
644                                                          surface,
645                                                          flags,
646                                                          rectangle);
647 #endif
648         return display_create_shm_surface(display, rectangle, flags, NULL);
649 }
650
651 /*
652  * The following correspondences between file names and cursors was copied
653  * from: https://bugs.kde.org/attachment.cgi?id=67313
654  */
655
656 static const char *bottom_left_corners[] = {
657         "bottom_left_corner",
658         "sw-resize"
659 };
660
661 static const char *bottom_right_corners[] = {
662         "bottom_right_corner",
663         "se-resize"
664 };
665
666 static const char *bottom_sides[] = {
667         "bottom_side",
668         "s-resize"
669 };
670
671 static const char *grabbings[] = {
672         "grabbing",
673         "closedhand",
674         "208530c400c041818281048008011002"
675 };
676
677 static const char *left_ptrs[] = {
678         "left_ptr",
679         "default",
680         "top_left_arrow",
681         "left-arrow"
682 };
683
684 static const char *left_sides[] = {
685         "left_side",
686         "w-resize"
687 };
688
689 static const char *right_sides[] = {
690         "right_side",
691         "e-resize"
692 };
693
694 static const char *top_left_corners[] = {
695         "top_left_corner",
696         "nw-resize"
697 };
698
699 static const char *top_right_corners[] = {
700         "top_right_corner",
701         "ne-resize"
702 };
703
704 static const char *top_sides[] = {
705         "top_side",
706         "n-resize"
707 };
708
709 static const char *xterms[] = {
710         "xterm",
711         "ibeam",
712         "text"
713 };
714
715 static const char *hand1s[] = {
716         "hand1",
717         "pointer",
718         "pointing_hand",
719         "e29285e634086352946a0e7090d73106"
720 };
721
722 static const char *watches[] = {
723         "watch",
724         "wait",
725         "0426c94ea35c87780ff01dc239897213"
726 };
727
728 struct cursor_alternatives {
729         const char **names;
730         size_t count;
731 };
732
733 static const struct cursor_alternatives cursors[] = {
734         {bottom_left_corners, ARRAY_LENGTH(bottom_left_corners)},
735         {bottom_right_corners, ARRAY_LENGTH(bottom_right_corners)},
736         {bottom_sides, ARRAY_LENGTH(bottom_sides)},
737         {grabbings, ARRAY_LENGTH(grabbings)},
738         {left_ptrs, ARRAY_LENGTH(left_ptrs)},
739         {left_sides, ARRAY_LENGTH(left_sides)},
740         {right_sides, ARRAY_LENGTH(right_sides)},
741         {top_left_corners, ARRAY_LENGTH(top_left_corners)},
742         {top_right_corners, ARRAY_LENGTH(top_right_corners)},
743         {top_sides, ARRAY_LENGTH(top_sides)},
744         {xterms, ARRAY_LENGTH(xterms)},
745         {hand1s, ARRAY_LENGTH(hand1s)},
746         {watches, ARRAY_LENGTH(watches)},
747 };
748
749 static void
750 create_cursors(struct display *display)
751 {
752         char *config_file;
753         char *theme = NULL;
754         unsigned int i, j;
755         struct wl_cursor *cursor;
756         struct config_key shell_keys[] = {
757                 { "cursor-theme", CONFIG_KEY_STRING, &theme },
758         };
759         struct config_section cs[] = {
760                 { "shell", shell_keys, ARRAY_LENGTH(shell_keys), NULL },
761         };
762
763         config_file = config_file_path("weston.ini");
764         parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
765         free(config_file);
766
767         display->cursor_theme = wl_cursor_theme_load(theme, 32, display->shm);
768         display->cursors =
769                 malloc(ARRAY_LENGTH(cursors) * sizeof display->cursors[0]);
770
771         for (i = 0; i < ARRAY_LENGTH(cursors); i++) {
772                 cursor = NULL;
773                 for (j = 0; !cursor && j < cursors[i].count; ++j)
774                         cursor = wl_cursor_theme_get_cursor(
775                             display->cursor_theme, cursors[i].names[j]);
776
777                 if (!cursor)
778                         fprintf(stderr, "could not load cursor '%s'\n",
779                                 cursors[i].names[0]);
780
781                 display->cursors[i] = cursor;
782         }
783 }
784
785 static void
786 destroy_cursors(struct display *display)
787 {
788         wl_cursor_theme_destroy(display->cursor_theme);
789         free(display->cursors);
790 }
791
792 struct wl_cursor_image *
793 display_get_pointer_image(struct display *display, int pointer)
794 {
795         struct wl_cursor *cursor = display->cursors[pointer];
796
797         return cursor ? cursor->images[0] : NULL;
798 }
799
800 static void
801 window_get_resize_dx_dy(struct window *window, int *x, int *y)
802 {
803         if (window->resize_edges & WINDOW_RESIZING_LEFT)
804                 *x = window->server_allocation.width - window->allocation.width;
805         else
806                 *x = 0;
807
808         if (window->resize_edges & WINDOW_RESIZING_TOP)
809                 *y = window->server_allocation.height -
810                         window->allocation.height;
811         else
812                 *y = 0;
813
814         window->resize_edges = 0;
815 }
816
817 static void
818 window_attach_surface(struct window *window)
819 {
820         struct display *display = window->display;
821         struct wl_buffer *buffer;
822 #ifdef HAVE_CAIRO_EGL
823         struct egl_window_surface_data *data;
824 #endif
825         int32_t x, y;
826
827         if (window->type == TYPE_NONE) {
828                 window->type = TYPE_TOPLEVEL;
829                 if (display->shell)
830                         wl_shell_surface_set_toplevel(window->shell_surface);
831         }
832
833         if (window->opaque_region) {
834                 wl_surface_set_opaque_region(window->surface,
835                                              window->opaque_region);
836                 wl_region_destroy(window->opaque_region);
837                 window->opaque_region = NULL;
838         }
839
840         if (window->input_region) {
841                 wl_surface_set_input_region(window->surface,
842                                             window->input_region);
843                 wl_region_destroy(window->input_region);
844                 window->input_region = NULL;
845         }
846
847         switch (window->buffer_type) {
848 #ifdef HAVE_CAIRO_EGL
849         case WINDOW_BUFFER_TYPE_EGL_WINDOW:
850                 data = cairo_surface_get_user_data(window->cairo_surface,
851                                                    &surface_data_key);
852
853                 cairo_gl_surface_swapbuffers(window->cairo_surface);
854                 wl_egl_window_get_attached_size(data->window,
855                                 &window->server_allocation.width,
856                                 &window->server_allocation.height);
857                 break;
858 #endif
859         case WINDOW_BUFFER_TYPE_SHM:
860                 buffer =
861                         display_get_buffer_for_surface(display,
862                                                        window->cairo_surface);
863
864                 window_get_resize_dx_dy(window, &x, &y);
865                 wl_surface_attach(window->surface, buffer, x, y);
866                 wl_surface_damage(window->surface, 0, 0,
867                                   window->allocation.width,
868                                   window->allocation.height);
869                 wl_surface_commit(window->surface);
870                 window->server_allocation = window->allocation;
871                 break;
872         default:
873                 return;
874         }
875 }
876
877 int
878 window_has_focus(struct window *window)
879 {
880         return window->focus_count > 0;
881 }
882
883 void
884 window_flush(struct window *window)
885 {
886         if (window->cairo_surface)
887                 window_attach_surface(window);
888 }
889
890 void
891 window_set_surface(struct window *window, cairo_surface_t *surface)
892 {
893         cairo_surface_reference(surface);
894
895         if (window->cairo_surface != NULL)
896                 cairo_surface_destroy(window->cairo_surface);
897
898         window->cairo_surface = surface;
899 }
900
901 #ifdef HAVE_CAIRO_EGL
902 static void
903 window_resize_cairo_window_surface(struct window *window)
904 {
905         struct egl_window_surface_data *data;
906         int x, y;
907
908         data = cairo_surface_get_user_data(window->cairo_surface,
909                                            &surface_data_key);
910
911         window_get_resize_dx_dy(window, &x, &y),
912         wl_egl_window_resize(data->window,
913                              window->allocation.width,
914                              window->allocation.height,
915                              x,y);
916
917         cairo_gl_surface_set_size(window->cairo_surface,
918                                   window->allocation.width,
919                                   window->allocation.height);
920 }
921 #endif
922
923 struct display *
924 window_get_display(struct window *window)
925 {
926         return window->display;
927 }
928
929 void
930 window_create_surface(struct window *window)
931 {
932         cairo_surface_t *surface;
933         uint32_t flags = 0;
934         
935         if (!window->transparent)
936                 flags = SURFACE_OPAQUE;
937         
938         switch (window->buffer_type) {
939 #ifdef HAVE_CAIRO_EGL
940         case WINDOW_BUFFER_TYPE_EGL_WINDOW:
941                 if (window->cairo_surface) {
942                         window_resize_cairo_window_surface(window);
943                         return;
944                 }
945                 surface = display_create_surface(window->display,
946                                                  window->surface,
947                                                  &window->allocation, flags);
948                 break;
949 #endif
950         case WINDOW_BUFFER_TYPE_SHM:
951                 surface = display_create_shm_surface(window->display,
952                                                      &window->allocation,
953                                                      flags, window);
954                 break;
955         default:
956                 surface = NULL;
957                 break;
958         }
959
960         window_set_surface(window, surface);
961         cairo_surface_destroy(surface);
962 }
963
964 static void frame_destroy(struct frame *frame);
965
966 void
967 window_destroy(struct window *window)
968 {
969         struct display *display = window->display;
970         struct input *input;
971         struct window_output *window_output;
972         struct window_output *window_output_tmp;
973
974         if (window->redraw_scheduled)
975                 wl_list_remove(&window->redraw_task.link);
976
977         wl_list_for_each(input, &display->input_list, link) {
978                 if (input->pointer_focus == window)
979                         input->pointer_focus = NULL;
980                 if (input->keyboard_focus == window)
981                         input->keyboard_focus = NULL;
982                 if (input->focus_widget &&
983                     input->focus_widget->window == window)
984                         input->focus_widget = NULL;
985         }
986
987         wl_list_for_each_safe(window_output, window_output_tmp,
988                               &window->window_output_list, link) {
989                 free (window_output);
990         }
991
992         if (window->input_region)
993                 wl_region_destroy(window->input_region);
994         if (window->opaque_region)
995                 wl_region_destroy(window->opaque_region);
996
997         if (window->frame)
998                 frame_destroy(window->frame);
999
1000         if (window->shell_surface)
1001                 wl_shell_surface_destroy(window->shell_surface);
1002         wl_surface_destroy(window->surface);
1003         wl_list_remove(&window->link);
1004
1005         if (window->cairo_surface != NULL)
1006                 cairo_surface_destroy(window->cairo_surface);
1007
1008         if (window->frame_cb)
1009                 wl_callback_destroy(window->frame_cb);
1010         free(window->title);
1011         free(window);
1012 }
1013
1014 static struct widget *
1015 widget_find_widget(struct widget *widget, int32_t x, int32_t y)
1016 {
1017         struct widget *child, *target;
1018
1019         wl_list_for_each(child, &widget->child_list, link) {
1020                 target = widget_find_widget(child, x, y);
1021                 if (target)
1022                         return target;
1023         }
1024
1025         if (widget->allocation.x <= x &&
1026             x < widget->allocation.x + widget->allocation.width &&
1027             widget->allocation.y <= y &&
1028             y < widget->allocation.y + widget->allocation.height) {
1029                 return widget;
1030         }
1031
1032         return NULL;
1033 }
1034
1035 static struct widget *
1036 widget_create(struct window *window, void *data)
1037 {
1038         struct widget *widget;
1039
1040         widget = malloc(sizeof *widget);
1041         memset(widget, 0, sizeof *widget);
1042         widget->window = window;
1043         widget->user_data = data;
1044         widget->allocation = window->allocation;
1045         wl_list_init(&widget->child_list);
1046         widget->opaque = 0;
1047         widget->tooltip = NULL;
1048         widget->tooltip_count = 0;
1049
1050         return widget;
1051 }
1052
1053 struct widget *
1054 window_add_widget(struct window *window, void *data)
1055 {
1056         window->widget = widget_create(window, data);
1057         wl_list_init(&window->widget->link);
1058
1059         return window->widget;
1060 }
1061
1062 struct widget *
1063 widget_add_widget(struct widget *parent, void *data)
1064 {
1065         struct widget *widget;
1066
1067         widget = widget_create(parent->window, data);
1068         wl_list_insert(parent->child_list.prev, &widget->link);
1069
1070         return widget;
1071 }
1072
1073 void
1074 widget_destroy(struct widget *widget)
1075 {
1076         struct display *display = widget->window->display;
1077         struct input *input;
1078
1079         if (widget->tooltip) {
1080                 free(widget->tooltip);
1081                 widget->tooltip = NULL;
1082         }
1083
1084         wl_list_for_each(input, &display->input_list, link) {
1085                 if (input->focus_widget == widget)
1086                         input->focus_widget = NULL;
1087         }
1088
1089         wl_list_remove(&widget->link);
1090         free(widget);
1091 }
1092
1093 void
1094 widget_get_allocation(struct widget *widget, struct rectangle *allocation)
1095 {
1096         *allocation = widget->allocation;
1097 }
1098
1099 void
1100 widget_set_size(struct widget *widget, int32_t width, int32_t height)
1101 {
1102         widget->allocation.width = width;
1103         widget->allocation.height = height;
1104 }
1105
1106 void
1107 widget_set_allocation(struct widget *widget,
1108                       int32_t x, int32_t y, int32_t width, int32_t height)
1109 {
1110         widget->allocation.x = x;
1111         widget->allocation.y = y;
1112         widget_set_size(widget, width, height);
1113 }
1114
1115 void
1116 widget_set_transparent(struct widget *widget, int transparent)
1117 {
1118         widget->opaque = !transparent;
1119 }
1120
1121 void *
1122 widget_get_user_data(struct widget *widget)
1123 {
1124         return widget->user_data;
1125 }
1126
1127 void
1128 widget_set_resize_handler(struct widget *widget,
1129                           widget_resize_handler_t handler)
1130 {
1131         widget->resize_handler = handler;
1132 }
1133
1134 void
1135 widget_set_redraw_handler(struct widget *widget,
1136                           widget_redraw_handler_t handler)
1137 {
1138         widget->redraw_handler = handler;
1139 }
1140
1141 void
1142 widget_set_enter_handler(struct widget *widget, widget_enter_handler_t handler)
1143 {
1144         widget->enter_handler = handler;
1145 }
1146
1147 void
1148 widget_set_leave_handler(struct widget *widget, widget_leave_handler_t handler)
1149 {
1150         widget->leave_handler = handler;
1151 }
1152
1153 void
1154 widget_set_motion_handler(struct widget *widget,
1155                           widget_motion_handler_t handler)
1156 {
1157         widget->motion_handler = handler;
1158 }
1159
1160 void
1161 widget_set_button_handler(struct widget *widget,
1162                           widget_button_handler_t handler)
1163 {
1164         widget->button_handler = handler;
1165 }
1166
1167 void
1168 widget_set_axis_handler(struct widget *widget,
1169                         widget_axis_handler_t handler)
1170 {
1171         widget->axis_handler = handler;
1172 }
1173
1174 void
1175 widget_schedule_redraw(struct widget *widget)
1176 {
1177         window_schedule_redraw(widget->window);
1178 }
1179
1180 cairo_surface_t *
1181 window_get_surface(struct window *window)
1182 {
1183         return cairo_surface_reference(window->cairo_surface);
1184 }
1185
1186 struct wl_surface *
1187 window_get_wl_surface(struct window *window)
1188 {
1189         return window->surface;
1190 }
1191
1192 struct wl_shell_surface *
1193 window_get_wl_shell_surface(struct window *window)
1194 {
1195         return window->shell_surface;
1196 }
1197
1198 static void
1199 tooltip_redraw_handler(struct widget *widget, void *data)
1200 {
1201         cairo_t *cr;
1202         const int32_t r = 3;
1203         struct tooltip *tooltip = data;
1204         int32_t width, height;
1205         struct window *window = widget->window;
1206
1207         cr = cairo_create(window->cairo_surface);
1208         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1209         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
1210         cairo_paint(cr);
1211
1212         width = window->allocation.width;
1213         height = window->allocation.height;
1214         rounded_rect(cr, 0, 0, width, height, r);
1215
1216         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1217         cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
1218         cairo_fill(cr);
1219
1220         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1221         cairo_move_to(cr, 10, 16);
1222         cairo_show_text(cr, tooltip->entry);
1223         cairo_destroy(cr);
1224 }
1225
1226 static cairo_text_extents_t
1227 get_text_extents(struct tooltip *tooltip)
1228 {
1229         struct window *window;
1230         cairo_t *cr;
1231         cairo_text_extents_t extents;
1232
1233         /* we borrow cairo_surface from the parent cause tooltip's wasn't
1234          * created yet */
1235         window = tooltip->widget->window->parent;
1236         cr = cairo_create(window->cairo_surface);
1237         cairo_text_extents(cr, tooltip->entry, &extents);
1238         cairo_destroy(cr);
1239
1240         return extents;
1241 }
1242
1243 static int
1244 window_create_tooltip(struct tooltip *tooltip)
1245 {
1246         struct widget *parent = tooltip->parent;
1247         struct display *display = parent->window->display;
1248         struct window *window;
1249         const int offset_y = 27;
1250         const int margin = 3;
1251         cairo_text_extents_t extents;
1252
1253         if (tooltip->widget)
1254                 return 0;
1255
1256         window = window_create_transient(display, parent->window, tooltip->x,
1257                                          tooltip->y + offset_y,
1258                                          WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
1259         if (!window)
1260                 return -1;
1261
1262         tooltip->window = window;
1263         tooltip->widget = window_add_widget(tooltip->window, tooltip);
1264
1265         extents = get_text_extents(tooltip);
1266         widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
1267         window_schedule_resize(window, extents.width + 20, 20 + margin * 2);
1268
1269         return 0;
1270 }
1271
1272 void
1273 widget_destroy_tooltip(struct widget *parent)
1274 {
1275         struct tooltip *tooltip = parent->tooltip;
1276
1277         parent->tooltip_count = 0;
1278         if (!tooltip)
1279                 return;
1280
1281         if (tooltip->widget) {
1282                 widget_destroy(tooltip->widget);
1283                 window_destroy(tooltip->window);
1284                 tooltip->widget = NULL;
1285                 tooltip->window = NULL;
1286         }
1287
1288         close(tooltip->tooltip_fd);
1289         free(tooltip->entry);
1290         free(tooltip);
1291         parent->tooltip = NULL;
1292 }
1293
1294 static void
1295 tooltip_func(struct task *task, uint32_t events)
1296 {
1297         struct tooltip *tooltip =
1298                 container_of(task, struct tooltip, tooltip_task);
1299         uint64_t exp;
1300
1301         if (read(tooltip->tooltip_fd, &exp, sizeof (uint64_t)) != sizeof (uint64_t))
1302                 abort();
1303         window_create_tooltip(tooltip);
1304 }
1305
1306 #define TOOLTIP_TIMEOUT 500
1307 static int
1308 tooltip_timer_reset(struct tooltip *tooltip)
1309 {
1310         struct itimerspec its;
1311
1312         its.it_interval.tv_sec = 0;
1313         its.it_interval.tv_nsec = 0;
1314         its.it_value.tv_sec = TOOLTIP_TIMEOUT / 1000;
1315         its.it_value.tv_nsec = (TOOLTIP_TIMEOUT % 1000) * 1000 * 1000;
1316         if (timerfd_settime(tooltip->tooltip_fd, 0, &its, NULL) < 0) {
1317                 fprintf(stderr, "could not set timerfd\n: %m");
1318                 return -1;
1319         }
1320
1321         return 0;
1322 }
1323
1324 int
1325 widget_set_tooltip(struct widget *parent, char *entry, float x, float y)
1326 {
1327         struct tooltip *tooltip = parent->tooltip;
1328
1329         parent->tooltip_count++;
1330         if (tooltip) {
1331                 tooltip->x = x;
1332                 tooltip->y = y;
1333                 tooltip_timer_reset(tooltip);
1334                 return 0;
1335         }
1336
1337         /* the handler might be triggered too fast via input device motion, so
1338          * we need this check here to make sure tooltip is fully initialized */
1339         if (parent->tooltip_count > 1)
1340                 return 0;
1341
1342         tooltip = malloc(sizeof *tooltip);
1343         if (!tooltip)
1344                 return -1;
1345
1346         parent->tooltip = tooltip;
1347         tooltip->parent = parent;
1348         tooltip->widget = NULL;
1349         tooltip->window = NULL;
1350         tooltip->x = x;
1351         tooltip->y = y;
1352         tooltip->entry = strdup(entry);
1353         tooltip->tooltip_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
1354         if (tooltip->tooltip_fd < 0) {
1355                 fprintf(stderr, "could not create timerfd\n: %m");
1356                 return -1;
1357         }
1358
1359         tooltip->tooltip_task.run = tooltip_func;
1360         display_watch_fd(parent->window->display, tooltip->tooltip_fd,
1361                          EPOLLIN, &tooltip->tooltip_task);
1362         tooltip_timer_reset(tooltip);
1363
1364         return 0;
1365 }
1366
1367 static void
1368 workspace_manager_state(void *data,
1369                         struct workspace_manager *workspace_manager,
1370                         uint32_t current,
1371                         uint32_t count)
1372 {
1373         struct display *display = data;
1374
1375         display->workspace = current;
1376         display->workspace_count = count;
1377 }
1378
1379 static const struct workspace_manager_listener workspace_manager_listener = {
1380         workspace_manager_state
1381 };
1382
1383 static void
1384 frame_resize_handler(struct widget *widget,
1385                      int32_t width, int32_t height, void *data)
1386 {
1387         struct frame *frame = data;
1388         struct widget *child = frame->child;
1389         struct rectangle allocation;
1390         struct display *display = widget->window->display;
1391         struct frame_button * button;
1392         struct theme *t = display->theme;
1393         int x_l, x_r, y, w, h;
1394         int decoration_width, decoration_height;
1395         int opaque_margin, shadow_margin;
1396
1397         switch (widget->window->type) {
1398         case TYPE_FULLSCREEN:
1399                 decoration_width = 0;
1400                 decoration_height = 0;
1401
1402                 allocation.x = 0;
1403                 allocation.y = 0;
1404                 allocation.width = width;
1405                 allocation.height = height;
1406                 opaque_margin = 0;
1407
1408                 wl_list_for_each(button, &frame->buttons_list, link)
1409                         button->widget->opaque = 1;
1410                 break;
1411         case TYPE_MAXIMIZED:
1412                 decoration_width = t->width * 2;
1413                 decoration_height = t->width + t->titlebar_height;
1414
1415                 allocation.x = t->width;
1416                 allocation.y = t->titlebar_height;
1417                 allocation.width = width - decoration_width;
1418                 allocation.height = height - decoration_height;
1419
1420                 opaque_margin = 0;
1421
1422                 wl_list_for_each(button, &frame->buttons_list, link)
1423                         button->widget->opaque = 0;
1424                 break;
1425         default:
1426                 decoration_width = (t->width + t->margin) * 2;
1427                 decoration_height = t->width +
1428                         t->titlebar_height + t->margin * 2;
1429
1430                 allocation.x = t->width + t->margin;
1431                 allocation.y = t->titlebar_height + t->margin;
1432                 allocation.width = width - decoration_width;
1433                 allocation.height = height - decoration_height;
1434
1435                 opaque_margin = t->margin + t->frame_radius;
1436
1437                 wl_list_for_each(button, &frame->buttons_list, link)
1438                         button->widget->opaque = 0;
1439                 break;
1440         }
1441
1442         widget_set_allocation(child, allocation.x, allocation.y,
1443                               allocation.width, allocation.height);
1444
1445         if (child->resize_handler)
1446                 child->resize_handler(child,
1447                                       allocation.width,
1448                                       allocation.height,
1449                                       child->user_data);
1450
1451         width = child->allocation.width + decoration_width;
1452         height = child->allocation.height + decoration_height;
1453
1454         shadow_margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1455
1456         widget->window->input_region =
1457                 wl_compositor_create_region(display->compositor);
1458         if (widget->window->type != TYPE_FULLSCREEN) {
1459                 wl_region_add(widget->window->input_region,
1460                               shadow_margin, shadow_margin,
1461                               width - 2 * shadow_margin,
1462                               height - 2 * shadow_margin);
1463         } else {
1464                 wl_region_add(widget->window->input_region,
1465                               0, 0, width, height);
1466         }
1467
1468         widget_set_allocation(widget, 0, 0, width, height);
1469
1470         if (child->opaque) {
1471                 widget->window->opaque_region =
1472                         wl_compositor_create_region(display->compositor);
1473                 wl_region_add(widget->window->opaque_region,
1474                               opaque_margin, opaque_margin,
1475                               widget->allocation.width - 2 * opaque_margin,
1476                               widget->allocation.height - 2 * opaque_margin);
1477         }
1478
1479         /* frame internal buttons */
1480         x_r = frame->widget->allocation.width - t->width - shadow_margin;
1481         x_l = t->width + shadow_margin;
1482         y = t->width + shadow_margin;
1483         wl_list_for_each(button, &frame->buttons_list, link) {
1484                 const int button_padding = 4;
1485                 w = cairo_image_surface_get_width(button->icon);
1486                 h = cairo_image_surface_get_height(button->icon);
1487
1488                 if (button->decoration == FRAME_BUTTON_FANCY)
1489                         w += 10;
1490
1491                 if (button->align == FRAME_BUTTON_LEFT) {
1492                         widget_set_allocation(button->widget,
1493                                               x_l, y , w + 1, h + 1);
1494                         x_l += w;
1495                         x_l += button_padding;
1496                 } else {
1497                         x_r -= w;
1498                         widget_set_allocation(button->widget,
1499                                               x_r, y , w + 1, h + 1);
1500                         x_r -= button_padding;
1501                 }
1502         }
1503 }
1504
1505 static int
1506 frame_button_enter_handler(struct widget *widget,
1507                            struct input *input, float x, float y, void *data)
1508 {
1509         struct frame_button *frame_button = data;
1510
1511         widget_schedule_redraw(frame_button->widget);
1512         frame_button->state = FRAME_BUTTON_OVER;
1513
1514         return CURSOR_LEFT_PTR;
1515 }
1516
1517 static void
1518 frame_button_leave_handler(struct widget *widget, struct input *input, void *data)
1519 {
1520         struct frame_button *frame_button = data;
1521
1522         widget_schedule_redraw(frame_button->widget);
1523         frame_button->state = FRAME_BUTTON_DEFAULT;
1524 }
1525
1526 static void
1527 frame_button_button_handler(struct widget *widget,
1528                             struct input *input, uint32_t time,
1529                             uint32_t button,
1530                             enum wl_pointer_button_state state, void *data)
1531 {
1532         struct frame_button *frame_button = data;
1533         struct window *window = widget->window;
1534         int was_pressed = (frame_button->state == FRAME_BUTTON_ACTIVE);
1535
1536         if (button != BTN_LEFT)
1537                 return;
1538
1539         switch (state) {
1540         case WL_POINTER_BUTTON_STATE_PRESSED:
1541                 frame_button->state = FRAME_BUTTON_ACTIVE;
1542                 widget_schedule_redraw(frame_button->widget);
1543
1544                 if (frame_button->type == FRAME_BUTTON_ICON)
1545                         window_show_frame_menu(window, input, time);
1546                 return;
1547         case WL_POINTER_BUTTON_STATE_RELEASED:
1548                 frame_button->state = FRAME_BUTTON_DEFAULT;
1549                 widget_schedule_redraw(frame_button->widget);
1550                 break;
1551         }
1552
1553         if (!was_pressed)
1554                 return;
1555
1556         switch (frame_button->type) {
1557         case FRAME_BUTTON_CLOSE:
1558                 if (window->close_handler)
1559                         window->close_handler(window->parent,
1560                                               window->user_data);
1561                 else
1562                         display_exit(window->display);
1563                 break;
1564         case FRAME_BUTTON_MINIMIZE:
1565                 fprintf(stderr,"Minimize stub\n");
1566                 break;
1567         case FRAME_BUTTON_MAXIMIZE:
1568                 window_set_maximized(window, window->type != TYPE_MAXIMIZED);
1569                 break;
1570         default:
1571                 /* Unknown operation */
1572                 break;
1573         }
1574 }
1575
1576 static int
1577 frame_button_motion_handler(struct widget *widget,
1578                             struct input *input, uint32_t time,
1579                             float x, float y, void *data)
1580 {
1581         struct frame_button *frame_button = data;
1582         enum frame_button_pointer previous_button_state = frame_button->state;
1583
1584         /* only track state for a pressed button */
1585         if (input->grab != widget)
1586                 return CURSOR_LEFT_PTR;
1587
1588         if (x > widget->allocation.x &&
1589             x < (widget->allocation.x + widget->allocation.width) &&
1590             y > widget->allocation.y &&
1591             y < (widget->allocation.y + widget->allocation.height)) {
1592                 frame_button->state = FRAME_BUTTON_ACTIVE;
1593         } else {
1594                 frame_button->state = FRAME_BUTTON_DEFAULT;
1595         }
1596
1597         if (frame_button->state != previous_button_state)
1598                 widget_schedule_redraw(frame_button->widget);
1599
1600         return CURSOR_LEFT_PTR;
1601 }
1602
1603 static void
1604 frame_button_redraw_handler(struct widget *widget, void *data)
1605 {
1606         struct frame_button *frame_button = data;
1607         cairo_t *cr;
1608         int width, height, x, y;
1609         struct window *window = widget->window;
1610
1611         x = widget->allocation.x;
1612         y = widget->allocation.y;
1613         width = widget->allocation.width;
1614         height = widget->allocation.height;
1615
1616         if (!width)
1617                 return;
1618         if (!height)
1619                 return;
1620         if (widget->opaque)
1621                 return;
1622
1623         cr = cairo_create(window->cairo_surface);
1624
1625         if (frame_button->decoration == FRAME_BUTTON_FANCY) {
1626                 cairo_set_line_width(cr, 1);
1627
1628                 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
1629                 cairo_rectangle (cr, x, y, 25, 16);
1630
1631                 cairo_stroke_preserve(cr);
1632
1633                 switch (frame_button->state) {
1634                 case FRAME_BUTTON_DEFAULT:
1635                         cairo_set_source_rgb(cr, 0.88, 0.88, 0.88);
1636                         break;
1637                 case FRAME_BUTTON_OVER:
1638                         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1639                         break;
1640                 case FRAME_BUTTON_ACTIVE:
1641                         cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);
1642                         break;
1643                 }
1644
1645                 cairo_fill (cr);
1646
1647                 x += 4;
1648         }
1649
1650         cairo_set_source_surface(cr, frame_button->icon, x, y);
1651         cairo_paint(cr);
1652
1653         cairo_destroy(cr);
1654 }
1655
1656 static struct widget *
1657 frame_button_create(struct frame *frame, void *data, enum frame_button_action type,
1658         enum frame_button_align align, enum frame_button_decoration style)
1659 {
1660         struct frame_button *frame_button;
1661         const char *icon = data;
1662
1663         frame_button = malloc (sizeof *frame_button);
1664         memset(frame_button, 0, sizeof *frame_button);
1665
1666         frame_button->icon = cairo_image_surface_create_from_png(icon);
1667         frame_button->widget = widget_add_widget(frame->widget, frame_button);
1668         frame_button->frame = frame;
1669         frame_button->type = type;
1670         frame_button->align = align;
1671         frame_button->decoration = style;
1672
1673         wl_list_insert(frame->buttons_list.prev, &frame_button->link);
1674
1675         widget_set_redraw_handler(frame_button->widget, frame_button_redraw_handler);
1676         widget_set_enter_handler(frame_button->widget, frame_button_enter_handler);
1677         widget_set_leave_handler(frame_button->widget, frame_button_leave_handler);
1678         widget_set_button_handler(frame_button->widget, frame_button_button_handler);
1679         widget_set_motion_handler(frame_button->widget, frame_button_motion_handler);
1680         return frame_button->widget;
1681 }
1682
1683 static void
1684 frame_button_destroy(struct frame_button *frame_button)
1685 {
1686         widget_destroy(frame_button->widget);
1687         wl_list_remove(&frame_button->link);
1688         cairo_surface_destroy(frame_button->icon);
1689         free(frame_button);
1690
1691         return;
1692 }
1693
1694 static void
1695 frame_redraw_handler(struct widget *widget, void *data)
1696 {
1697         cairo_t *cr;
1698         struct window *window = widget->window;
1699         struct theme *t = window->display->theme;
1700         uint32_t flags = 0;
1701
1702         if (window->type == TYPE_FULLSCREEN)
1703                 return;
1704
1705         cr = cairo_create(window->cairo_surface);
1706
1707         if (window->focus_count)
1708                 flags |= THEME_FRAME_ACTIVE;
1709         if (window->type == TYPE_MAXIMIZED)
1710                 flags |= THEME_FRAME_MAXIMIZED;
1711         theme_render_frame(t, cr, widget->allocation.width,
1712                            widget->allocation.height, window->title, flags);
1713
1714         cairo_destroy(cr);
1715 }
1716
1717 static int
1718 frame_get_pointer_image_for_location(struct frame *frame, struct input *input)
1719 {
1720         struct theme *t = frame->widget->window->display->theme;
1721         struct window *window = frame->widget->window;
1722         int location;
1723
1724         if (window->type != TYPE_TOPLEVEL)
1725                 return CURSOR_LEFT_PTR;
1726
1727         location = theme_get_location(t, input->sx, input->sy,
1728                                       frame->widget->allocation.width,
1729                                       frame->widget->allocation.height,
1730                                       window->type == TYPE_MAXIMIZED ?
1731                                       THEME_FRAME_MAXIMIZED : 0);
1732
1733         switch (location) {
1734         case THEME_LOCATION_RESIZING_TOP:
1735                 return CURSOR_TOP;
1736         case THEME_LOCATION_RESIZING_BOTTOM:
1737                 return CURSOR_BOTTOM;
1738         case THEME_LOCATION_RESIZING_LEFT:
1739                 return CURSOR_LEFT;
1740         case THEME_LOCATION_RESIZING_RIGHT:
1741                 return CURSOR_RIGHT;
1742         case THEME_LOCATION_RESIZING_TOP_LEFT:
1743                 return CURSOR_TOP_LEFT;
1744         case THEME_LOCATION_RESIZING_TOP_RIGHT:
1745                 return CURSOR_TOP_RIGHT;
1746         case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1747                 return CURSOR_BOTTOM_LEFT;
1748         case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1749                 return CURSOR_BOTTOM_RIGHT;
1750         case THEME_LOCATION_EXTERIOR:
1751         case THEME_LOCATION_TITLEBAR:
1752         default:
1753                 return CURSOR_LEFT_PTR;
1754         }
1755 }
1756
1757 static void
1758 frame_menu_func(struct window *window, int index, void *data)
1759 {
1760         struct display *display;
1761
1762         switch (index) {
1763         case 0: /* close */
1764                 if (window->close_handler)
1765                         window->close_handler(window->parent,
1766                                               window->user_data);
1767                 else
1768                         display_exit(window->display);
1769                 break;
1770         case 1: /* move to workspace above */
1771                 display = window->display;
1772                 if (display->workspace > 0)
1773                         workspace_manager_move_surface(display->workspace_manager,
1774                                                        window->surface,
1775                                                        display->workspace - 1);
1776                 break;
1777         case 2: /* move to workspace below */
1778                 display = window->display;
1779                 if (display->workspace < display->workspace_count - 1)
1780                         workspace_manager_move_surface(display->workspace_manager,
1781                                                        window->surface,
1782                                                        display->workspace + 1);
1783                 break;
1784         case 3: /* fullscreen */
1785                 /* we don't have a way to get out of fullscreen for now */
1786                 if (window->fullscreen_handler)
1787                         window->fullscreen_handler(window, window->user_data);
1788                 break;
1789         }
1790 }
1791
1792 void
1793 window_show_frame_menu(struct window *window,
1794                        struct input *input, uint32_t time)
1795 {
1796         int32_t x, y;
1797         int count;
1798
1799         static const char *entries[] = {
1800                 "Close",
1801                 "Move to workspace above", "Move to workspace below",
1802                 "Fullscreen"
1803         };
1804
1805         if (window->fullscreen_handler)
1806                 count = ARRAY_LENGTH(entries);
1807         else
1808                 count = ARRAY_LENGTH(entries) - 1;
1809
1810         input_get_position(input, &x, &y);
1811         window_show_menu(window->display, input, time, window,
1812                          x - 10, y - 10, frame_menu_func, entries, count);
1813 }
1814
1815 static int
1816 frame_enter_handler(struct widget *widget,
1817                     struct input *input, float x, float y, void *data)
1818 {
1819         return frame_get_pointer_image_for_location(data, input);
1820 }
1821
1822 static int
1823 frame_motion_handler(struct widget *widget,
1824                      struct input *input, uint32_t time,
1825                      float x, float y, void *data)
1826 {
1827         return frame_get_pointer_image_for_location(data, input);
1828 }
1829
1830 static void
1831 frame_button_handler(struct widget *widget,
1832                      struct input *input, uint32_t time,
1833                      uint32_t button, enum wl_pointer_button_state state,
1834                      void *data)
1835
1836 {
1837         struct frame *frame = data;
1838         struct window *window = widget->window;
1839         struct display *display = window->display;
1840         int location;
1841
1842         if (window->type != TYPE_TOPLEVEL)
1843                 return;
1844
1845         location = theme_get_location(display->theme, input->sx, input->sy,
1846                                       frame->widget->allocation.width,
1847                                       frame->widget->allocation.height,
1848                                       window->type == TYPE_MAXIMIZED ?
1849                                       THEME_FRAME_MAXIMIZED : 0);
1850
1851         if (window->display->shell && button == BTN_LEFT &&
1852             state == WL_POINTER_BUTTON_STATE_PRESSED) {
1853                 switch (location) {
1854                 case THEME_LOCATION_TITLEBAR:
1855                         if (!window->shell_surface)
1856                                 break;
1857                         input_ungrab(input);
1858                         wl_shell_surface_move(window->shell_surface,
1859                                               input_get_seat(input),
1860                                               display->serial);
1861                         break;
1862                 case THEME_LOCATION_RESIZING_TOP:
1863                 case THEME_LOCATION_RESIZING_BOTTOM:
1864                 case THEME_LOCATION_RESIZING_LEFT:
1865                 case THEME_LOCATION_RESIZING_RIGHT:
1866                 case THEME_LOCATION_RESIZING_TOP_LEFT:
1867                 case THEME_LOCATION_RESIZING_TOP_RIGHT:
1868                 case THEME_LOCATION_RESIZING_BOTTOM_LEFT:
1869                 case THEME_LOCATION_RESIZING_BOTTOM_RIGHT:
1870                         if (!window->shell_surface)
1871                                 break;
1872                         input_ungrab(input);
1873
1874                         if (!display->dpy) {
1875                                 /* If we're using shm, allocate a big
1876                                    pool to create buffers out of while
1877                                    we resize.  We should probably base
1878                                    this number on the size of the output. */
1879                                 window->pool =
1880                                         shm_pool_create(display, 6 * 1024 * 1024);
1881                         }
1882
1883                         wl_shell_surface_resize(window->shell_surface,
1884                                                 input_get_seat(input),
1885                                                 display->serial, location);
1886                         break;
1887                 }
1888         } else if (button == BTN_RIGHT &&
1889                    state == WL_POINTER_BUTTON_STATE_PRESSED) {
1890                 window_show_frame_menu(window, input, time);
1891         }
1892 }
1893
1894 struct widget *
1895 frame_create(struct window *window, void *data)
1896 {
1897         struct frame *frame;
1898
1899         frame = malloc(sizeof *frame);
1900         memset(frame, 0, sizeof *frame);
1901
1902         frame->widget = window_add_widget(window, frame);
1903         frame->child = widget_add_widget(frame->widget, data);
1904
1905         widget_set_redraw_handler(frame->widget, frame_redraw_handler);
1906         widget_set_resize_handler(frame->widget, frame_resize_handler);
1907         widget_set_enter_handler(frame->widget, frame_enter_handler);
1908         widget_set_motion_handler(frame->widget, frame_motion_handler);
1909         widget_set_button_handler(frame->widget, frame_button_handler);
1910
1911         /* Create empty list for frame buttons */
1912         wl_list_init(&frame->buttons_list);
1913
1914         frame_button_create(frame, DATADIR "/weston/icon_window.png",
1915                 FRAME_BUTTON_ICON, FRAME_BUTTON_LEFT, FRAME_BUTTON_NONE);
1916
1917         frame_button_create(frame, DATADIR "/weston/sign_close.png",
1918                 FRAME_BUTTON_CLOSE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1919
1920         frame_button_create(frame, DATADIR "/weston/sign_maximize.png",
1921                 FRAME_BUTTON_MAXIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1922
1923         frame_button_create(frame, DATADIR "/weston/sign_minimize.png",
1924                 FRAME_BUTTON_MINIMIZE, FRAME_BUTTON_RIGHT, FRAME_BUTTON_FANCY);
1925
1926         window->frame = frame;
1927
1928         return frame->child;
1929 }
1930
1931 void
1932 frame_set_child_size(struct widget *widget, int child_width, int child_height)
1933 {
1934         struct display *display = widget->window->display;
1935         struct theme *t = display->theme;
1936         int decoration_width, decoration_height;
1937         int width, height;
1938         int margin = widget->window->type == TYPE_MAXIMIZED ? 0 : t->margin;
1939
1940         if (widget->window->type != TYPE_FULLSCREEN) {
1941                 decoration_width = (t->width + margin) * 2;
1942                 decoration_height = t->width +
1943                         t->titlebar_height + margin * 2;
1944
1945                 width = child_width + decoration_width;
1946                 height = child_height + decoration_height;
1947         } else {
1948                 width = child_width;
1949                 height = child_height;
1950         }
1951
1952         window_schedule_resize(widget->window, width, height);
1953 }
1954
1955 static void
1956 frame_destroy(struct frame *frame)
1957 {
1958         struct frame_button *button, *tmp;
1959
1960         wl_list_for_each_safe(button, tmp, &frame->buttons_list, link)
1961                 frame_button_destroy(button);
1962
1963         /* frame->child must be destroyed by the application */
1964         widget_destroy(frame->widget);
1965         free(frame);
1966 }
1967
1968 static void
1969 input_set_focus_widget(struct input *input, struct widget *focus,
1970                        float x, float y)
1971 {
1972         struct widget *old, *widget;
1973         int pointer = CURSOR_LEFT_PTR;
1974
1975         if (focus == input->focus_widget)
1976                 return;
1977
1978         old = input->focus_widget;
1979         if (old) {
1980                 widget = old;
1981                 if (input->grab)
1982                         widget = input->grab;
1983                 if (widget->leave_handler)
1984                         widget->leave_handler(old, input, widget->user_data);
1985                 input->focus_widget = NULL;
1986         }
1987
1988         if (focus) {
1989                 widget = focus;
1990                 if (input->grab)
1991                         widget = input->grab;
1992                 input->focus_widget = focus;
1993                 if (widget->enter_handler)
1994                         pointer = widget->enter_handler(focus, input, x, y,
1995                                                         widget->user_data);
1996
1997                 input_set_pointer_image(input, pointer);
1998         }
1999 }
2000
2001 void
2002 input_grab(struct input *input, struct widget *widget, uint32_t button)
2003 {
2004         input->grab = widget;
2005         input->grab_button = button;
2006 }
2007
2008 void
2009 input_ungrab(struct input *input)
2010 {
2011         struct widget *widget;
2012
2013         input->grab = NULL;
2014         if (input->pointer_focus) {
2015                 widget = widget_find_widget(input->pointer_focus->widget,
2016                                             input->sx, input->sy);
2017                 input_set_focus_widget(input, widget, input->sx, input->sy);
2018         }
2019 }
2020
2021 static void
2022 input_remove_pointer_focus(struct input *input)
2023 {
2024         struct window *window = input->pointer_focus;
2025
2026         if (!window)
2027                 return;
2028
2029         input_set_focus_widget(input, NULL, 0, 0);
2030
2031         input->pointer_focus = NULL;
2032         input->current_cursor = CURSOR_UNSET;
2033 }
2034
2035 static void
2036 pointer_handle_enter(void *data, struct wl_pointer *pointer,
2037                      uint32_t serial, struct wl_surface *surface,
2038                      wl_fixed_t sx_w, wl_fixed_t sy_w)
2039 {
2040         struct input *input = data;
2041         struct window *window;
2042         struct widget *widget;
2043         float sx = wl_fixed_to_double(sx_w);
2044         float sy = wl_fixed_to_double(sy_w);
2045
2046         if (!surface) {
2047                 /* enter event for a window we've just destroyed */
2048                 return;
2049         }
2050
2051         input->display->serial = serial;
2052         input->pointer_enter_serial = serial;
2053         input->pointer_focus = wl_surface_get_user_data(surface);
2054         window = input->pointer_focus;
2055
2056         if (window->pool) {
2057                 shm_pool_destroy(window->pool);
2058                 window->pool = NULL;
2059                 /* Schedule a redraw to free the pool */
2060                 window_schedule_redraw(window);
2061         }
2062
2063         input->sx = sx;
2064         input->sy = sy;
2065
2066         widget = widget_find_widget(window->widget, sx, sy);
2067         input_set_focus_widget(input, widget, sx, sy);
2068 }
2069
2070 static void
2071 pointer_handle_leave(void *data, struct wl_pointer *pointer,
2072                      uint32_t serial, struct wl_surface *surface)
2073 {
2074         struct input *input = data;
2075
2076         input->display->serial = serial;
2077         input_remove_pointer_focus(input);
2078 }
2079
2080 static void
2081 pointer_handle_motion(void *data, struct wl_pointer *pointer,
2082                       uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
2083 {
2084         struct input *input = data;
2085         struct window *window = input->pointer_focus;
2086         struct widget *widget;
2087         int cursor = CURSOR_LEFT_PTR;
2088         float sx = wl_fixed_to_double(sx_w);
2089         float sy = wl_fixed_to_double(sy_w);
2090
2091         input->sx = sx;
2092         input->sy = sy;
2093
2094         if (!window)
2095                 return;
2096
2097         if (!(input->grab && input->grab_button)) {
2098                 widget = widget_find_widget(window->widget, sx, sy);
2099                 input_set_focus_widget(input, widget, sx, sy);
2100         }
2101
2102         if (input->grab)
2103                 widget = input->grab;
2104         else
2105                 widget = input->focus_widget;
2106         if (widget && widget->motion_handler)
2107                 cursor = widget->motion_handler(input->focus_widget,
2108                                                 input, time, sx, sy,
2109                                                 widget->user_data);
2110
2111         input_set_pointer_image(input, cursor);
2112 }
2113
2114 static void
2115 pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
2116                       uint32_t time, uint32_t button, uint32_t state_w)
2117 {
2118         struct input *input = data;
2119         struct widget *widget;
2120         enum wl_pointer_button_state state = state_w;
2121
2122         input->display->serial = serial;
2123         if (input->focus_widget && input->grab == NULL &&
2124             state == WL_POINTER_BUTTON_STATE_PRESSED)
2125                 input_grab(input, input->focus_widget, button);
2126
2127         widget = input->grab;
2128         if (widget && widget->button_handler)
2129                 (*widget->button_handler)(widget,
2130                                           input, time,
2131                                           button, state,
2132                                           input->grab->user_data);
2133
2134         if (input->grab && input->grab_button == button &&
2135             state == WL_POINTER_BUTTON_STATE_RELEASED)
2136                 input_ungrab(input);
2137 }
2138
2139 static void
2140 pointer_handle_axis(void *data, struct wl_pointer *pointer,
2141                     uint32_t time, uint32_t axis, wl_fixed_t value)
2142 {
2143         struct input *input = data;
2144         struct widget *widget;
2145
2146         widget = input->focus_widget;
2147         if (input->grab)
2148                 widget = input->grab;
2149         if (widget && widget->axis_handler)
2150                 (*widget->axis_handler)(widget,
2151                                         input, time,
2152                                         axis, value,
2153                                         widget->user_data);
2154 }
2155
2156 static const struct wl_pointer_listener pointer_listener = {
2157         pointer_handle_enter,
2158         pointer_handle_leave,
2159         pointer_handle_motion,
2160         pointer_handle_button,
2161         pointer_handle_axis,
2162 };
2163
2164 static void
2165 input_remove_keyboard_focus(struct input *input)
2166 {
2167         struct window *window = input->keyboard_focus;
2168         struct itimerspec its;
2169
2170         its.it_interval.tv_sec = 0;
2171         its.it_interval.tv_nsec = 0;
2172         its.it_value.tv_sec = 0;
2173         its.it_value.tv_nsec = 0;
2174         timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2175
2176         if (!window)
2177                 return;
2178
2179         window->focus_count--;
2180         if (window->keyboard_focus_handler)
2181                 (*window->keyboard_focus_handler)(window, NULL,
2182                                                   window->user_data);
2183
2184         input->keyboard_focus = NULL;
2185 }
2186
2187 static void
2188 keyboard_repeat_func(struct task *task, uint32_t events)
2189 {
2190         struct input *input =
2191                 container_of(task, struct input, repeat_task);
2192         struct window *window = input->keyboard_focus;
2193         uint64_t exp;
2194
2195         if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
2196                 /* If we change the timer between the fd becoming
2197                  * readable and getting here, there'll be nothing to
2198                  * read and we get EAGAIN. */
2199                 return;
2200
2201         if (window && window->key_handler) {
2202                 (*window->key_handler)(window, input, input->repeat_time,
2203                                        input->repeat_key, input->repeat_sym,
2204                                        WL_KEYBOARD_KEY_STATE_PRESSED,
2205                                        window->user_data);
2206         }
2207 }
2208
2209 static void
2210 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
2211                        uint32_t format, int fd, uint32_t size)
2212 {
2213         struct input *input = data;
2214         char *map_str;
2215
2216         if (!data) {
2217                 close(fd);
2218                 return;
2219         }
2220
2221         if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
2222                 close(fd);
2223                 return;
2224         }
2225
2226         map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2227         if (map_str == MAP_FAILED) {
2228                 close(fd);
2229                 return;
2230         }
2231
2232         input->xkb.keymap = xkb_map_new_from_string(input->display->xkb_context,
2233                                                     map_str,
2234                                                     XKB_KEYMAP_FORMAT_TEXT_V1,
2235                                                     0);
2236         munmap(map_str, size);
2237         close(fd);
2238
2239         if (!input->xkb.keymap) {
2240                 fprintf(stderr, "failed to compile keymap\n");
2241                 return;
2242         }
2243
2244         input->xkb.state = xkb_state_new(input->xkb.keymap);
2245         if (!input->xkb.state) {
2246                 fprintf(stderr, "failed to create XKB state\n");
2247                 xkb_map_unref(input->xkb.keymap);
2248                 input->xkb.keymap = NULL;
2249                 return;
2250         }
2251
2252         input->xkb.control_mask =
2253                 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control");
2254         input->xkb.alt_mask =
2255                 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1");
2256         input->xkb.shift_mask =
2257                 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift");
2258 }
2259
2260 static void
2261 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
2262                       uint32_t serial, struct wl_surface *surface,
2263                       struct wl_array *keys)
2264 {
2265         struct input *input = data;
2266         struct window *window;
2267
2268         input->display->serial = serial;
2269         input->keyboard_focus = wl_surface_get_user_data(surface);
2270
2271         window = input->keyboard_focus;
2272         window->focus_count++;
2273         if (window->keyboard_focus_handler)
2274                 (*window->keyboard_focus_handler)(window,
2275                                                   input, window->user_data);
2276 }
2277
2278 static void
2279 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
2280                       uint32_t serial, struct wl_surface *surface)
2281 {
2282         struct input *input = data;
2283
2284         input->display->serial = serial;
2285         input_remove_keyboard_focus(input);
2286 }
2287
2288 static void
2289 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
2290                     uint32_t serial, uint32_t time, uint32_t key,
2291                     uint32_t state_w)
2292 {
2293         struct input *input = data;
2294         struct window *window = input->keyboard_focus;
2295         uint32_t code, num_syms;
2296         enum wl_keyboard_key_state state = state_w;
2297         const xkb_keysym_t *syms;
2298         xkb_keysym_t sym;
2299         struct itimerspec its;
2300
2301         input->display->serial = serial;
2302         code = key + 8;
2303         if (!window || !input->xkb.state)
2304                 return;
2305
2306         num_syms = xkb_key_get_syms(input->xkb.state, code, &syms);
2307
2308         sym = XKB_KEY_NoSymbol;
2309         if (num_syms == 1)
2310                 sym = syms[0];
2311
2312         if (sym == XKB_KEY_F5 && input->modifiers == MOD_ALT_MASK) {
2313                 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2314                         window_set_maximized(window,
2315                                              window->type != TYPE_MAXIMIZED);
2316         } else if (sym == XKB_KEY_F11 &&
2317                    window->fullscreen_handler &&
2318                    state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2319                 window->fullscreen_handler(window, window->user_data);
2320         } else if (sym == XKB_KEY_F4 &&
2321                    input->modifiers == MOD_ALT_MASK &&
2322                    state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2323                 if (window->close_handler)
2324                         window->close_handler(window->parent,
2325                                               window->user_data);
2326                 else
2327                         display_exit(window->display);
2328         } else if (window->key_handler) {
2329                 (*window->key_handler)(window, input, time, key,
2330                                        sym, state, window->user_data);
2331         }
2332
2333         if (state == WL_KEYBOARD_KEY_STATE_RELEASED &&
2334             key == input->repeat_key) {
2335                 its.it_interval.tv_sec = 0;
2336                 its.it_interval.tv_nsec = 0;
2337                 its.it_value.tv_sec = 0;
2338                 its.it_value.tv_nsec = 0;
2339                 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2340         } else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
2341                 input->repeat_sym = sym;
2342                 input->repeat_key = key;
2343                 input->repeat_time = time;
2344                 its.it_interval.tv_sec = 0;
2345                 its.it_interval.tv_nsec = 25 * 1000 * 1000;
2346                 its.it_value.tv_sec = 0;
2347                 its.it_value.tv_nsec = 400 * 1000 * 1000;
2348                 timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
2349         }
2350 }
2351
2352 static void
2353 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
2354                           uint32_t serial, uint32_t mods_depressed,
2355                           uint32_t mods_latched, uint32_t mods_locked,
2356                           uint32_t group)
2357 {
2358         struct input *input = data;
2359         xkb_mod_mask_t mask;
2360
2361         xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
2362                               mods_locked, 0, 0, group);
2363         mask = xkb_state_serialize_mods(input->xkb.state,
2364                                         XKB_STATE_DEPRESSED |
2365                                         XKB_STATE_LATCHED);
2366         input->modifiers = 0;
2367         if (mask & input->xkb.control_mask)
2368                 input->modifiers |= MOD_CONTROL_MASK;
2369         if (mask & input->xkb.alt_mask)
2370                 input->modifiers |= MOD_ALT_MASK;
2371         if (mask & input->xkb.shift_mask)
2372                 input->modifiers |= MOD_SHIFT_MASK;
2373 }
2374
2375 static const struct wl_keyboard_listener keyboard_listener = {
2376         keyboard_handle_keymap,
2377         keyboard_handle_enter,
2378         keyboard_handle_leave,
2379         keyboard_handle_key,
2380         keyboard_handle_modifiers,
2381 };
2382
2383 static void
2384 seat_handle_capabilities(void *data, struct wl_seat *seat,
2385                          enum wl_seat_capability caps)
2386 {
2387         struct input *input = data;
2388
2389         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
2390                 input->pointer = wl_seat_get_pointer(seat);
2391                 wl_pointer_set_user_data(input->pointer, input);
2392                 wl_pointer_add_listener(input->pointer, &pointer_listener,
2393                                         input);
2394         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
2395                 wl_pointer_destroy(input->pointer);
2396                 input->pointer = NULL;
2397         }
2398
2399         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
2400                 input->keyboard = wl_seat_get_keyboard(seat);
2401                 wl_keyboard_set_user_data(input->keyboard, input);
2402                 wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
2403                                          input);
2404         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
2405                 wl_keyboard_destroy(input->keyboard);
2406                 input->keyboard = NULL;
2407         }
2408 }
2409
2410 static const struct wl_seat_listener seat_listener = {
2411         seat_handle_capabilities,
2412 };
2413
2414 void
2415 input_get_position(struct input *input, int32_t *x, int32_t *y)
2416 {
2417         *x = input->sx;
2418         *y = input->sy;
2419 }
2420
2421 struct display *
2422 input_get_display(struct input *input)
2423 {
2424         return input->display;
2425 }
2426
2427 struct wl_seat *
2428 input_get_seat(struct input *input)
2429 {
2430         return input->seat;
2431 }
2432
2433 uint32_t
2434 input_get_modifiers(struct input *input)
2435 {
2436         return input->modifiers;
2437 }
2438
2439 struct widget *
2440 input_get_focus_widget(struct input *input)
2441 {
2442         return input->focus_widget;
2443 }
2444
2445 struct data_offer {
2446         struct wl_data_offer *offer;
2447         struct input *input;
2448         struct wl_array types;
2449         int refcount;
2450
2451         struct task io_task;
2452         int fd;
2453         data_func_t func;
2454         int32_t x, y;
2455         void *user_data;
2456 };
2457
2458 static void
2459 data_offer_offer(void *data, struct wl_data_offer *wl_data_offer, const char *type)
2460 {
2461         struct data_offer *offer = data;
2462         char **p;
2463
2464         p = wl_array_add(&offer->types, sizeof *p);
2465         *p = strdup(type);
2466 }
2467
2468 static const struct wl_data_offer_listener data_offer_listener = {
2469         data_offer_offer,
2470 };
2471
2472 static void
2473 data_offer_destroy(struct data_offer *offer)
2474 {
2475         char **p;
2476
2477         offer->refcount--;
2478         if (offer->refcount == 0) {
2479                 wl_data_offer_destroy(offer->offer);
2480                 for (p = offer->types.data; *p; p++)
2481                         free(*p);
2482                 wl_array_release(&offer->types);
2483                 free(offer);
2484         }
2485 }
2486
2487 static void
2488 data_device_data_offer(void *data,
2489                        struct wl_data_device *data_device,
2490                        struct wl_data_offer *_offer)
2491 {
2492         struct data_offer *offer;
2493
2494         offer = malloc(sizeof *offer);
2495
2496         wl_array_init(&offer->types);
2497         offer->refcount = 1;
2498         offer->input = data;
2499         offer->offer = _offer;
2500         wl_data_offer_add_listener(offer->offer,
2501                                    &data_offer_listener, offer);
2502 }
2503
2504 static void
2505 data_device_enter(void *data, struct wl_data_device *data_device,
2506                   uint32_t serial, struct wl_surface *surface,
2507                   wl_fixed_t x_w, wl_fixed_t y_w,
2508                   struct wl_data_offer *offer)
2509 {
2510         struct input *input = data;
2511         struct window *window;
2512         void *types_data;
2513         float x = wl_fixed_to_double(x_w);
2514         float y = wl_fixed_to_double(y_w);
2515         char **p;
2516
2517         input->pointer_enter_serial = serial;
2518         window = wl_surface_get_user_data(surface);
2519         input->pointer_focus = window;
2520
2521         if (offer) {
2522                 input->drag_offer = wl_data_offer_get_user_data(offer);
2523
2524                 p = wl_array_add(&input->drag_offer->types, sizeof *p);
2525                 *p = NULL;
2526
2527                 types_data = input->drag_offer->types.data;
2528         } else {
2529                 input->drag_offer = NULL;
2530                 types_data = NULL;
2531         }
2532
2533         window = input->pointer_focus;
2534         if (window->data_handler)
2535                 window->data_handler(window, input, x, y, types_data,
2536                                      window->user_data);
2537 }
2538
2539 static void
2540 data_device_leave(void *data, struct wl_data_device *data_device)
2541 {
2542         struct input *input = data;
2543
2544         if (input->drag_offer) {
2545                 data_offer_destroy(input->drag_offer);
2546                 input->drag_offer = NULL;
2547         }
2548 }
2549
2550 static void
2551 data_device_motion(void *data, struct wl_data_device *data_device,
2552                    uint32_t time, wl_fixed_t x_w, wl_fixed_t y_w)
2553 {
2554         struct input *input = data;
2555         struct window *window = input->pointer_focus;
2556         float x = wl_fixed_to_double(x_w);
2557         float y = wl_fixed_to_double(y_w);
2558         void *types_data;
2559
2560         input->sx = x;
2561         input->sy = y;
2562
2563         if (input->drag_offer)
2564                 types_data = input->drag_offer->types.data;
2565         else
2566                 types_data = NULL;
2567
2568         if (window->data_handler)
2569                 window->data_handler(window, input, x, y, types_data,
2570                                      window->user_data);
2571 }
2572
2573 static void
2574 data_device_drop(void *data, struct wl_data_device *data_device)
2575 {
2576         struct input *input = data;
2577         struct window *window = input->pointer_focus;
2578
2579         if (window->drop_handler)
2580                 window->drop_handler(window, input,
2581                                      input->sx, input->sy, window->user_data);
2582 }
2583
2584 static void
2585 data_device_selection(void *data,
2586                       struct wl_data_device *wl_data_device,
2587                       struct wl_data_offer *offer)
2588 {
2589         struct input *input = data;
2590         char **p;
2591
2592         if (input->selection_offer)
2593                 data_offer_destroy(input->selection_offer);
2594
2595         if (offer) {
2596                 input->selection_offer = wl_data_offer_get_user_data(offer);
2597                 p = wl_array_add(&input->selection_offer->types, sizeof *p);
2598                 *p = NULL;
2599         } else {
2600                 input->selection_offer = NULL;
2601         }
2602 }
2603
2604 static const struct wl_data_device_listener data_device_listener = {
2605         data_device_data_offer,
2606         data_device_enter,
2607         data_device_leave,
2608         data_device_motion,
2609         data_device_drop,
2610         data_device_selection
2611 };
2612
2613 static void
2614 input_set_pointer_image_index(struct input *input, int index)
2615 {
2616         struct wl_buffer *buffer;
2617         struct wl_cursor *cursor;
2618         struct wl_cursor_image *image;
2619
2620         if (!input->pointer)
2621                 return;
2622
2623         cursor = input->display->cursors[input->current_cursor];
2624         if (!cursor)
2625                 return;
2626
2627         if (index >= (int) cursor->image_count) {
2628                 fprintf(stderr, "cursor index out of range\n");
2629                 return;
2630         }
2631
2632         image = cursor->images[index];
2633         buffer = wl_cursor_image_get_buffer(image);
2634         if (!buffer)
2635                 return;
2636
2637         wl_pointer_set_cursor(input->pointer, input->pointer_enter_serial,
2638                               input->pointer_surface,
2639                               image->hotspot_x, image->hotspot_y);
2640         wl_surface_attach(input->pointer_surface, buffer, 0, 0);
2641         wl_surface_damage(input->pointer_surface, 0, 0,
2642                           image->width, image->height);
2643         wl_surface_commit(input->pointer_surface);
2644 }
2645
2646 static const struct wl_callback_listener pointer_surface_listener;
2647
2648 static void
2649 pointer_surface_frame_callback(void *data, struct wl_callback *callback,
2650                                uint32_t time)
2651 {
2652         struct input *input = data;
2653         struct wl_cursor *cursor;
2654         int i;
2655
2656         if (callback) {
2657                 assert(callback == input->cursor_frame_cb);
2658                 wl_callback_destroy(callback);
2659                 input->cursor_frame_cb = NULL;
2660         }
2661
2662         if (!input->pointer)
2663                 return;
2664
2665         if (input->current_cursor == CURSOR_BLANK) {
2666                 wl_pointer_set_cursor(input->pointer,
2667                                       input->pointer_enter_serial,
2668                                       NULL, 0, 0);
2669                 return;
2670         }
2671
2672         if (input->current_cursor == CURSOR_UNSET)
2673                 return;
2674         cursor = input->display->cursors[input->current_cursor];
2675         if (!cursor)
2676                 return;
2677
2678         /* FIXME We don't have the current time on the first call so we set
2679          * the animation start to the time of the first frame callback. */
2680         if (time == 0)
2681                 input->cursor_anim_start = 0;
2682         else if (input->cursor_anim_start == 0)
2683                 input->cursor_anim_start = time;
2684
2685         if (time == 0 || input->cursor_anim_start == 0)
2686                 i = 0;
2687         else
2688                 i = wl_cursor_frame(cursor, time - input->cursor_anim_start);
2689
2690         if (cursor->image_count > 1) {
2691                 input->cursor_frame_cb =
2692                         wl_surface_frame(input->pointer_surface);
2693                 wl_callback_add_listener(input->cursor_frame_cb,
2694                                          &pointer_surface_listener, input);
2695         }
2696
2697         input_set_pointer_image_index(input, i);
2698 }
2699
2700 static const struct wl_callback_listener pointer_surface_listener = {
2701         pointer_surface_frame_callback
2702 };
2703
2704 void
2705 input_set_pointer_image(struct input *input, int pointer)
2706 {
2707         int force = 0;
2708
2709         if (!input->pointer)
2710                 return;
2711
2712         if (input->pointer_enter_serial > input->cursor_serial)
2713                 force = 1;
2714
2715         if (!force && pointer == input->current_cursor)
2716                 return;
2717
2718         input->current_cursor = pointer;
2719         input->cursor_serial = input->pointer_enter_serial;
2720         if (!input->cursor_frame_cb)
2721                 pointer_surface_frame_callback(input, NULL, 0);
2722         else if (force) {
2723                 /* The current frame callback may be stuck if, for instance,
2724                  * the set cursor request was processed by the server after
2725                  * this client lost the focus. In this case the cursor surface
2726                  * might not be mapped and the frame callback wouldn't ever
2727                  * complete. Send a set_cursor and attach to try to map the
2728                  * cursor surface again so that the callback will finish */
2729                 input_set_pointer_image_index(input, 0);
2730         }
2731 }
2732
2733 struct wl_data_device *
2734 input_get_data_device(struct input *input)
2735 {
2736         return input->data_device;
2737 }
2738
2739 void
2740 input_set_selection(struct input *input,
2741                     struct wl_data_source *source, uint32_t time)
2742 {
2743         wl_data_device_set_selection(input->data_device, source, time);
2744 }
2745
2746 void
2747 input_accept(struct input *input, const char *type)
2748 {
2749         wl_data_offer_accept(input->drag_offer->offer,
2750                              input->pointer_enter_serial, type);
2751 }
2752
2753 static void
2754 offer_io_func(struct task *task, uint32_t events)
2755 {
2756         struct data_offer *offer =
2757                 container_of(task, struct data_offer, io_task);
2758         unsigned int len;
2759         char buffer[4096];
2760
2761         len = read(offer->fd, buffer, sizeof buffer);
2762         offer->func(buffer, len,
2763                     offer->x, offer->y, offer->user_data);
2764
2765         if (len == 0) {
2766                 close(offer->fd);
2767                 data_offer_destroy(offer);
2768         }
2769 }
2770
2771 static void
2772 data_offer_receive_data(struct data_offer *offer, const char *mime_type,
2773                         data_func_t func, void *user_data)
2774 {
2775         int p[2];
2776
2777         if (pipe2(p, O_CLOEXEC) == -1)
2778                 return;
2779
2780         wl_data_offer_receive(offer->offer, mime_type, p[1]);
2781         close(p[1]);
2782
2783         offer->io_task.run = offer_io_func;
2784         offer->fd = p[0];
2785         offer->func = func;
2786         offer->refcount++;
2787         offer->user_data = user_data;
2788
2789         display_watch_fd(offer->input->display,
2790                          offer->fd, EPOLLIN, &offer->io_task);
2791 }
2792
2793 void
2794 input_receive_drag_data(struct input *input, const char *mime_type,
2795                         data_func_t func, void *data)
2796 {
2797         data_offer_receive_data(input->drag_offer, mime_type, func, data);
2798         input->drag_offer->x = input->sx;
2799         input->drag_offer->y = input->sy;
2800 }
2801
2802 int
2803 input_receive_selection_data(struct input *input, const char *mime_type,
2804                              data_func_t func, void *data)
2805 {
2806         char **p;
2807
2808         if (input->selection_offer == NULL)
2809                 return -1;
2810
2811         for (p = input->selection_offer->types.data; *p; p++)
2812                 if (strcmp(mime_type, *p) == 0)
2813                         break;
2814
2815         if (*p == NULL)
2816                 return -1;
2817
2818         data_offer_receive_data(input->selection_offer,
2819                                 mime_type, func, data);
2820         return 0;
2821 }
2822
2823 int
2824 input_receive_selection_data_to_fd(struct input *input,
2825                                    const char *mime_type, int fd)
2826 {
2827         if (input->selection_offer)
2828                 wl_data_offer_receive(input->selection_offer->offer,
2829                                       mime_type, fd);
2830
2831         return 0;
2832 }
2833
2834 void
2835 window_move(struct window *window, struct input *input, uint32_t serial)
2836 {
2837         if (!window->shell_surface)
2838                 return;
2839
2840         wl_shell_surface_move(window->shell_surface, input->seat, serial);
2841 }
2842
2843 static void
2844 idle_resize(struct window *window)
2845 {
2846         struct widget *widget;
2847
2848         window->resize_needed = 0;
2849         widget = window->widget;
2850         widget_set_allocation(widget,
2851                               window->pending_allocation.x,
2852                               window->pending_allocation.y,
2853                               window->pending_allocation.width,
2854                               window->pending_allocation.height);
2855
2856         if (window->input_region) {
2857                 wl_region_destroy(window->input_region);
2858                 window->input_region = NULL;
2859         }
2860
2861         if (window->opaque_region) {
2862                 wl_region_destroy(window->opaque_region);
2863                 window->opaque_region = NULL;
2864         }
2865
2866         if (widget->resize_handler)
2867                 widget->resize_handler(widget,
2868                                        widget->allocation.width,
2869                                        widget->allocation.height,
2870                                        widget->user_data);
2871
2872         if (window->allocation.width != widget->allocation.width ||
2873             window->allocation.height != widget->allocation.height) {
2874                 window->allocation = widget->allocation;
2875                 window_schedule_redraw(window);
2876         }
2877 }
2878
2879 void
2880 window_schedule_resize(struct window *window, int width, int height)
2881 {
2882         window->pending_allocation.x = 0;
2883         window->pending_allocation.y = 0;
2884         window->pending_allocation.width = width;
2885         window->pending_allocation.height = height;
2886
2887         if (window->min_allocation.width == 0)
2888                 window->min_allocation = window->pending_allocation;
2889         if (window->pending_allocation.width < window->min_allocation.width)
2890                 window->pending_allocation.width = window->min_allocation.width;
2891         if (window->pending_allocation.height < window->min_allocation.height)
2892                 window->pending_allocation.height = window->min_allocation.height;
2893
2894         window->resize_needed = 1;
2895         window_schedule_redraw(window);
2896 }
2897
2898 void
2899 widget_schedule_resize(struct widget *widget, int32_t width, int32_t height)
2900 {
2901         window_schedule_resize(widget->window, width, height);
2902 }
2903
2904 static void
2905 handle_ping(void *data, struct wl_shell_surface *shell_surface,
2906                                                         uint32_t serial)
2907 {
2908         wl_shell_surface_pong(shell_surface, serial);
2909 }
2910
2911 static void
2912 handle_configure(void *data, struct wl_shell_surface *shell_surface,
2913                  uint32_t edges, int32_t width, int32_t height)
2914 {
2915         struct window *window = data;
2916
2917         window->resize_edges = edges;
2918         window_schedule_resize(window, width, height);
2919 }
2920
2921 static void
2922 menu_destroy(struct menu *menu)
2923 {
2924         widget_destroy(menu->widget);
2925         window_destroy(menu->window);
2926         free(menu);
2927 }
2928
2929 static void
2930 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
2931 {
2932         struct window *window = data;
2933         struct menu *menu = window->widget->user_data;
2934
2935         /* FIXME: Need more context in this event, at least the input
2936          * device.  Or just use wl_callback.  And this really needs to
2937          * be a window vfunc that the menu can set.  And we need the
2938          * time. */
2939
2940         menu->func(window->parent, menu->current, window->parent->user_data);
2941         input_ungrab(menu->input);
2942         menu_destroy(menu);
2943 }
2944
2945 static const struct wl_shell_surface_listener shell_surface_listener = {
2946         handle_ping,
2947         handle_configure,
2948         handle_popup_done
2949 };
2950
2951 void
2952 window_get_allocation(struct window *window,
2953                       struct rectangle *allocation)
2954 {
2955         *allocation = window->allocation;
2956 }
2957
2958 static void
2959 widget_redraw(struct widget *widget)
2960 {
2961         struct widget *child;
2962
2963         if (widget->redraw_handler)
2964                 widget->redraw_handler(widget, widget->user_data);
2965         wl_list_for_each(child, &widget->child_list, link)
2966                 widget_redraw(child);
2967 }
2968
2969 static void
2970 frame_callback(void *data, struct wl_callback *callback, uint32_t time)
2971 {
2972         struct window *window = data;
2973
2974         assert(callback == window->frame_cb);
2975         wl_callback_destroy(callback);
2976         window->frame_cb = 0;
2977         window->redraw_scheduled = 0;
2978         if (window->redraw_needed)
2979                 window_schedule_redraw(window);
2980 }
2981
2982 static const struct wl_callback_listener listener = {
2983         frame_callback
2984 };
2985
2986 static void
2987 idle_redraw(struct task *task, uint32_t events)
2988 {
2989         struct window *window = container_of(task, struct window, redraw_task);
2990
2991         if (window->resize_needed)
2992                 idle_resize(window);
2993
2994         window_create_surface(window);
2995         widget_redraw(window->widget);
2996         window->redraw_needed = 0;
2997         wl_list_init(&window->redraw_task.link);
2998
2999         window->frame_cb = wl_surface_frame(window->surface);
3000         wl_callback_add_listener(window->frame_cb, &listener, window);
3001         window_flush(window);
3002 }
3003
3004 void
3005 window_schedule_redraw(struct window *window)
3006 {
3007         window->redraw_needed = 1;
3008         if (!window->redraw_scheduled) {
3009                 window->redraw_task.run = idle_redraw;
3010                 display_defer(window->display, &window->redraw_task);
3011                 window->redraw_scheduled = 1;
3012         }
3013 }
3014
3015 int
3016 window_is_fullscreen(struct window *window)
3017 {
3018         return window->type == TYPE_FULLSCREEN;
3019 }
3020
3021 void
3022 window_set_fullscreen(struct window *window, int fullscreen)
3023 {
3024         if (!window->display->shell)
3025                 return;
3026
3027         if ((window->type == TYPE_FULLSCREEN) == fullscreen)
3028                 return;
3029
3030         if (fullscreen) {
3031                 window->type = TYPE_FULLSCREEN;
3032                 window->saved_allocation = window->allocation;
3033                 wl_shell_surface_set_fullscreen(window->shell_surface,
3034                                                 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
3035                                                 0, NULL);
3036         } else {
3037                 window->type = TYPE_TOPLEVEL;
3038                 wl_shell_surface_set_toplevel(window->shell_surface);
3039                 window_schedule_resize(window,
3040                                        window->saved_allocation.width,
3041                                        window->saved_allocation.height);
3042         }
3043 }
3044
3045 int
3046 window_is_maximized(struct window *window)
3047 {
3048         return window->type == TYPE_MAXIMIZED;
3049 }
3050
3051 void
3052 window_set_maximized(struct window *window, int maximized)
3053 {
3054         if (!window->display->shell)
3055                 return;
3056
3057         if ((window->type == TYPE_MAXIMIZED) == maximized)
3058                 return;
3059
3060         if (window->type == TYPE_TOPLEVEL) {
3061                 window->saved_allocation = window->allocation;
3062                 wl_shell_surface_set_maximized(window->shell_surface, NULL);
3063                 window->type = TYPE_MAXIMIZED;
3064         } else {
3065                 wl_shell_surface_set_toplevel(window->shell_surface);
3066                 window->type = TYPE_TOPLEVEL;
3067                 window_schedule_resize(window,
3068                                        window->saved_allocation.width,
3069                                        window->saved_allocation.height);
3070         }
3071 }
3072
3073 void
3074 window_set_user_data(struct window *window, void *data)
3075 {
3076         window->user_data = data;
3077 }
3078
3079 void *
3080 window_get_user_data(struct window *window)
3081 {
3082         return window->user_data;
3083 }
3084
3085 void
3086 window_set_key_handler(struct window *window,
3087                        window_key_handler_t handler)
3088 {
3089         window->key_handler = handler;
3090 }
3091
3092 void
3093 window_set_keyboard_focus_handler(struct window *window,
3094                                   window_keyboard_focus_handler_t handler)
3095 {
3096         window->keyboard_focus_handler = handler;
3097 }
3098
3099 void
3100 window_set_data_handler(struct window *window, window_data_handler_t handler)
3101 {
3102         window->data_handler = handler;
3103 }
3104
3105 void
3106 window_set_drop_handler(struct window *window, window_drop_handler_t handler)
3107 {
3108         window->drop_handler = handler;
3109 }
3110
3111 void
3112 window_set_close_handler(struct window *window,
3113                          window_close_handler_t handler)
3114 {
3115         window->close_handler = handler;
3116 }
3117
3118 void
3119 window_set_fullscreen_handler(struct window *window,
3120                               window_fullscreen_handler_t handler)
3121 {
3122         window->fullscreen_handler = handler;
3123 }
3124
3125 void
3126 window_set_title(struct window *window, const char *title)
3127 {
3128         free(window->title);
3129         window->title = strdup(title);
3130         if (window->shell_surface)
3131                 wl_shell_surface_set_title(window->shell_surface, title);
3132 }
3133
3134 const char *
3135 window_get_title(struct window *window)
3136 {
3137         return window->title;
3138 }
3139
3140 void
3141 window_set_text_cursor_position(struct window *window, int32_t x, int32_t y)
3142 {
3143         struct text_cursor_position *text_cursor_position =
3144                                         window->display->text_cursor_position;
3145
3146         if (!text_cursor_position)
3147                 return;
3148
3149         text_cursor_position_notify(text_cursor_position,
3150                                                 window->surface,
3151                                                 wl_fixed_from_int(x),
3152                                                 wl_fixed_from_int(y));
3153 }
3154
3155 void
3156 window_damage(struct window *window, int32_t x, int32_t y,
3157               int32_t width, int32_t height)
3158 {
3159         wl_surface_damage(window->surface, x, y, width, height);
3160 }
3161
3162 static void
3163 surface_enter(void *data,
3164               struct wl_surface *wl_surface, struct wl_output *wl_output)
3165 {
3166         struct window *window = data;
3167         struct output *output;
3168         struct output *output_found = NULL;
3169         struct window_output *window_output;
3170
3171         wl_list_for_each(output, &window->display->output_list, link) {
3172                 if (output->output == wl_output) {
3173                         output_found = output;
3174                         break;
3175                 }
3176         }
3177
3178         if (!output_found)
3179                 return;
3180
3181         window_output = malloc (sizeof *window_output);
3182         window_output->output = output_found;
3183
3184         wl_list_insert (&window->window_output_list, &window_output->link);
3185 }
3186
3187 static void
3188 surface_leave(void *data,
3189               struct wl_surface *wl_surface, struct wl_output *output)
3190 {
3191         struct window *window = data;
3192         struct window_output *window_output;
3193         struct window_output *window_output_found = NULL;
3194
3195         wl_list_for_each(window_output, &window->window_output_list, link) {
3196                 if (window_output->output->output == output) {
3197                         window_output_found = window_output;
3198                         break;
3199                 }
3200         }
3201
3202         if (window_output_found) {
3203                 wl_list_remove(&window_output_found->link);
3204                 free(window_output_found);
3205         }
3206 }
3207
3208 static const struct wl_surface_listener surface_listener = {
3209         surface_enter,
3210         surface_leave
3211 };
3212
3213 static struct window *
3214 window_create_internal(struct display *display,
3215                        struct window *parent, int type)
3216 {
3217         struct window *window;
3218
3219         window = malloc(sizeof *window);
3220         if (window == NULL)
3221                 return NULL;
3222
3223         memset(window, 0, sizeof *window);
3224         window->display = display;
3225         window->parent = parent;
3226         window->surface = wl_compositor_create_surface(display->compositor);
3227         wl_surface_add_listener(window->surface, &surface_listener, window);
3228         if (type != TYPE_CUSTOM && display->shell) {
3229                 window->shell_surface =
3230                         wl_shell_get_shell_surface(display->shell,
3231                                                    window->surface);
3232         }
3233         window->allocation.x = 0;
3234         window->allocation.y = 0;
3235         window->allocation.width = 0;
3236         window->allocation.height = 0;
3237         window->saved_allocation = window->allocation;
3238         window->transparent = 1;
3239         window->type = type;
3240         window->input_region = NULL;
3241         window->opaque_region = NULL;
3242
3243         if (display->dpy)
3244 #ifdef HAVE_CAIRO_EGL
3245                 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_WINDOW;
3246 #else
3247                 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3248 #endif
3249         else
3250                 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
3251
3252         wl_surface_set_user_data(window->surface, window);
3253         wl_list_insert(display->window_list.prev, &window->link);
3254         wl_list_init(&window->redraw_task.link);
3255
3256         if (window->shell_surface) {
3257                 wl_shell_surface_set_user_data(window->shell_surface, window);
3258                 wl_shell_surface_add_listener(window->shell_surface,
3259                                               &shell_surface_listener, window);
3260         }
3261
3262         wl_list_init (&window->window_output_list);
3263
3264         return window;
3265 }
3266
3267 struct window *
3268 window_create(struct display *display)
3269 {
3270         struct window *window;
3271
3272         window = window_create_internal(display, NULL, TYPE_NONE);
3273         if (!window)
3274                 return NULL;
3275
3276         return window;
3277 }
3278
3279 struct window *
3280 window_create_custom(struct display *display)
3281 {
3282         struct window *window;
3283
3284         window = window_create_internal(display, NULL, TYPE_CUSTOM);
3285         if (!window)
3286                 return NULL;
3287
3288         return window;
3289 }
3290
3291 struct window *
3292 window_create_transient(struct display *display, struct window *parent,
3293                         int32_t x, int32_t y, uint32_t flags)
3294 {
3295         struct window *window;
3296
3297         window = window_create_internal(parent->display,
3298                                         parent, TYPE_TRANSIENT);
3299         if (!window)
3300                 return NULL;
3301
3302         window->x = x;
3303         window->y = y;
3304
3305         if (display->shell)
3306                 wl_shell_surface_set_transient(window->shell_surface,
3307                                                window->parent->surface,
3308                                                window->x, window->y, flags);
3309
3310         return window;
3311 }
3312
3313 static void
3314 menu_set_item(struct menu *menu, int sy)
3315 {
3316         int next;
3317
3318         next = (sy - 8) / 20;
3319         if (menu->current != next) {
3320                 menu->current = next;
3321                 widget_schedule_redraw(menu->widget);
3322         }
3323 }
3324
3325 static int
3326 menu_motion_handler(struct widget *widget,
3327                     struct input *input, uint32_t time,
3328                     float x, float y, void *data)
3329 {
3330         struct menu *menu = data;
3331
3332         if (widget == menu->widget)
3333                 menu_set_item(data, y);
3334
3335         return CURSOR_LEFT_PTR;
3336 }
3337
3338 static int
3339 menu_enter_handler(struct widget *widget,
3340                    struct input *input, float x, float y, void *data)
3341 {
3342         struct menu *menu = data;
3343
3344         if (widget == menu->widget)
3345                 menu_set_item(data, y);
3346
3347         return CURSOR_LEFT_PTR;
3348 }
3349
3350 static void
3351 menu_leave_handler(struct widget *widget, struct input *input, void *data)
3352 {
3353         struct menu *menu = data;
3354
3355         if (widget == menu->widget)
3356                 menu_set_item(data, -200);
3357 }
3358
3359 static void
3360 menu_button_handler(struct widget *widget,
3361                     struct input *input, uint32_t time,
3362                     uint32_t button, enum wl_pointer_button_state state,
3363                     void *data)
3364
3365 {
3366         struct menu *menu = data;
3367
3368         if (state == WL_POINTER_BUTTON_STATE_RELEASED &&
3369             (menu->release_count > 0 || time - menu->time > 500)) {
3370                 /* Either relase after press-drag-release or
3371                  * click-motion-click. */
3372                 menu->func(menu->window->parent, 
3373                            menu->current, menu->window->parent->user_data);
3374                 input_ungrab(input);
3375                 menu_destroy(menu);
3376         } else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3377                 menu->release_count++;
3378         }
3379 }
3380
3381 static void
3382 menu_redraw_handler(struct widget *widget, void *data)
3383 {
3384         cairo_t *cr;
3385         const int32_t r = 3, margin = 3;
3386         struct menu *menu = data;
3387         int32_t width, height, i;
3388         struct window *window = widget->window;
3389
3390         cr = cairo_create(window->cairo_surface);
3391         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
3392         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
3393         cairo_paint(cr);
3394
3395         width = window->allocation.width;
3396         height = window->allocation.height;
3397         rounded_rect(cr, 0, 0, width, height, r);
3398
3399         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
3400         cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
3401         cairo_fill(cr);
3402
3403         for (i = 0; i < menu->count; i++) {
3404                 if (i == menu->current) {
3405                         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3406                         cairo_rectangle(cr, margin, i * 20 + margin,
3407                                         width - 2 * margin, 20);
3408                         cairo_fill(cr);
3409                         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
3410                         cairo_move_to(cr, 10, i * 20 + 16);
3411                         cairo_show_text(cr, menu->entries[i]);
3412                 } else {
3413                         cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
3414                         cairo_move_to(cr, 10, i * 20 + 16);
3415                         cairo_show_text(cr, menu->entries[i]);
3416                 }
3417         }
3418
3419         cairo_destroy(cr);
3420 }
3421
3422 void
3423 window_show_menu(struct display *display,
3424                  struct input *input, uint32_t time, struct window *parent,
3425                  int32_t x, int32_t y,
3426                  menu_func_t func, const char **entries, int count)
3427 {
3428         struct window *window;
3429         struct menu *menu;
3430         const int32_t margin = 3;
3431
3432         menu = malloc(sizeof *menu);
3433         if (!menu)
3434                 return;
3435
3436         window = window_create_internal(parent->display, parent, TYPE_MENU);
3437         if (!window) {
3438                 free(menu);
3439                 return;
3440         }
3441
3442         menu->window = window;
3443         menu->widget = window_add_widget(menu->window, menu);
3444         menu->entries = entries;
3445         menu->count = count;
3446         menu->release_count = 0;
3447         menu->current = -1;
3448         menu->time = time;
3449         menu->func = func;
3450         menu->input = input;
3451         window->type = TYPE_MENU;
3452         window->x = x;
3453         window->y = y;
3454
3455         input_ungrab(input);
3456         wl_shell_surface_set_popup(window->shell_surface, input->seat,
3457                                    display_get_serial(window->display),
3458                                    window->parent->surface,
3459                                    window->x, window->y, 0);
3460
3461         widget_set_redraw_handler(menu->widget, menu_redraw_handler);
3462         widget_set_enter_handler(menu->widget, menu_enter_handler);
3463         widget_set_leave_handler(menu->widget, menu_leave_handler);
3464         widget_set_motion_handler(menu->widget, menu_motion_handler);
3465         widget_set_button_handler(menu->widget, menu_button_handler);
3466
3467         input_grab(input, menu->widget, 0);
3468         window_schedule_resize(window, 200, count * 20 + margin * 2);
3469 }
3470
3471 void
3472 window_set_buffer_type(struct window *window, enum window_buffer_type type)
3473 {
3474         window->buffer_type = type;
3475 }
3476
3477
3478 static void
3479 display_handle_geometry(void *data,
3480                         struct wl_output *wl_output,
3481                         int x, int y,
3482                         int physical_width,
3483                         int physical_height,
3484                         int subpixel,
3485                         const char *make,
3486                         const char *model,
3487                         int transform)
3488 {
3489         struct output *output = data;
3490
3491         output->allocation.x = x;
3492         output->allocation.y = y;
3493         output->transform = transform;
3494 }
3495
3496 static void
3497 display_handle_mode(void *data,
3498                     struct wl_output *wl_output,
3499                     uint32_t flags,
3500                     int width,
3501                     int height,
3502                     int refresh)
3503 {
3504         struct output *output = data;
3505         struct display *display = output->display;
3506
3507         if (flags & WL_OUTPUT_MODE_CURRENT) {
3508                 output->allocation.width = width;
3509                 output->allocation.height = height;
3510                 if (display->output_configure_handler)
3511                         (*display->output_configure_handler)(
3512                                                 output, display->user_data);
3513         }
3514 }
3515
3516 static const struct wl_output_listener output_listener = {
3517         display_handle_geometry,
3518         display_handle_mode
3519 };
3520
3521 static void
3522 display_add_output(struct display *d, uint32_t id)
3523 {
3524         struct output *output;
3525
3526         output = malloc(sizeof *output);
3527         if (output == NULL)
3528                 return;
3529
3530         memset(output, 0, sizeof *output);
3531         output->display = d;
3532         output->output =
3533                 wl_registry_bind(d->registry, id, &wl_output_interface, 1);
3534         wl_list_insert(d->output_list.prev, &output->link);
3535
3536         wl_output_add_listener(output->output, &output_listener, output);
3537 }
3538
3539 static void
3540 output_destroy(struct output *output)
3541 {
3542         if (output->destroy_handler)
3543                 (*output->destroy_handler)(output, output->user_data);
3544
3545         wl_output_destroy(output->output);
3546         wl_list_remove(&output->link);
3547         free(output);
3548 }
3549
3550 void
3551 display_set_global_handler(struct display *display,
3552                            display_global_handler_t handler)
3553 {
3554         struct global *global;
3555
3556         display->global_handler = handler;
3557         if (!handler)
3558                 return;
3559
3560         wl_list_for_each(global, &display->global_list, link)
3561                 display->global_handler(display,
3562                                         global->name, global->interface,
3563                                         global->version, display->user_data);
3564 }
3565
3566 void
3567 display_set_output_configure_handler(struct display *display,
3568                                      display_output_handler_t handler)
3569 {
3570         struct output *output;
3571
3572         display->output_configure_handler = handler;
3573         if (!handler)
3574                 return;
3575
3576         wl_list_for_each(output, &display->output_list, link) {
3577                 if (output->allocation.width == 0 &&
3578                     output->allocation.height == 0)
3579                         continue;
3580
3581                 (*display->output_configure_handler)(output,
3582                                                      display->user_data);
3583         }
3584 }
3585
3586 void
3587 output_set_user_data(struct output *output, void *data)
3588 {
3589         output->user_data = data;
3590 }
3591
3592 void *
3593 output_get_user_data(struct output *output)
3594 {
3595         return output->user_data;
3596 }
3597
3598 void
3599 output_set_destroy_handler(struct output *output,
3600                            display_output_handler_t handler)
3601 {
3602         output->destroy_handler = handler;
3603         /* FIXME: implement this, once we have way to remove outputs */
3604 }
3605
3606 void
3607 output_get_allocation(struct output *output, struct rectangle *base)
3608 {
3609         struct rectangle allocation = output->allocation;
3610
3611         switch (output->transform) {
3612         case WL_OUTPUT_TRANSFORM_90:
3613         case WL_OUTPUT_TRANSFORM_270:
3614         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3615         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3616                 /* Swap width and height */
3617                 allocation.width = output->allocation.height;
3618                 allocation.height = output->allocation.width;
3619                 break;
3620         }
3621
3622         *base = allocation;
3623 }
3624
3625 struct wl_output *
3626 output_get_wl_output(struct output *output)
3627 {
3628         return output->output;
3629 }
3630
3631 static void
3632 fini_xkb(struct input *input)
3633 {
3634         xkb_state_unref(input->xkb.state);
3635         xkb_map_unref(input->xkb.keymap);
3636 }
3637
3638 static void
3639 display_add_input(struct display *d, uint32_t id)
3640 {
3641         struct input *input;
3642
3643         input = malloc(sizeof *input);
3644         if (input == NULL)
3645                 return;
3646
3647         memset(input, 0, sizeof *input);
3648         input->display = d;
3649         input->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, 1);
3650         input->pointer_focus = NULL;
3651         input->keyboard_focus = NULL;
3652         wl_list_insert(d->input_list.prev, &input->link);
3653
3654         wl_seat_add_listener(input->seat, &seat_listener, input);
3655         wl_seat_set_user_data(input->seat, input);
3656
3657         input->data_device =
3658                 wl_data_device_manager_get_data_device(d->data_device_manager,
3659                                                        input->seat);
3660         wl_data_device_add_listener(input->data_device, &data_device_listener,
3661                                     input);
3662
3663         input->pointer_surface = wl_compositor_create_surface(d->compositor);
3664
3665         input->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC,
3666                                                 TFD_CLOEXEC | TFD_NONBLOCK);
3667         input->repeat_task.run = keyboard_repeat_func;
3668         display_watch_fd(d, input->repeat_timer_fd,
3669                          EPOLLIN, &input->repeat_task);
3670 }
3671
3672 static void
3673 input_destroy(struct input *input)
3674 {
3675         input_remove_keyboard_focus(input);
3676         input_remove_pointer_focus(input);
3677
3678         if (input->drag_offer)
3679                 data_offer_destroy(input->drag_offer);
3680
3681         if (input->selection_offer)
3682                 data_offer_destroy(input->selection_offer);
3683
3684         wl_data_device_destroy(input->data_device);
3685         fini_xkb(input);
3686
3687         wl_surface_destroy(input->pointer_surface);
3688
3689         wl_list_remove(&input->link);
3690         wl_seat_destroy(input->seat);
3691         close(input->repeat_timer_fd);
3692         free(input);
3693 }
3694
3695 static void
3696 init_workspace_manager(struct display *d, uint32_t id)
3697 {
3698         d->workspace_manager =
3699                 wl_registry_bind(d->registry, id,
3700                                  &workspace_manager_interface, 1);
3701         if (d->workspace_manager != NULL)
3702                 workspace_manager_add_listener(d->workspace_manager,
3703                                                &workspace_manager_listener,
3704                                                d);
3705 }
3706
3707 static void
3708 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
3709                        const char *interface, uint32_t version)
3710 {
3711         struct display *d = data;
3712         struct global *global;
3713
3714         global = malloc(sizeof *global);
3715         global->name = id;
3716         global->interface = strdup(interface);
3717         global->version = version;
3718         wl_list_insert(d->global_list.prev, &global->link);
3719
3720         if (strcmp(interface, "wl_compositor") == 0) {
3721                 d->compositor = wl_registry_bind(registry, id,
3722                                                  &wl_compositor_interface, 1);
3723         } else if (strcmp(interface, "wl_output") == 0) {
3724                 display_add_output(d, id);
3725         } else if (strcmp(interface, "wl_seat") == 0) {
3726                 display_add_input(d, id);
3727         } else if (strcmp(interface, "wl_shell") == 0) {
3728                 d->shell = wl_registry_bind(registry,
3729                                             id, &wl_shell_interface, 1);
3730         } else if (strcmp(interface, "wl_shm") == 0) {
3731                 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
3732         } else if (strcmp(interface, "wl_data_device_manager") == 0) {
3733                 d->data_device_manager =
3734                         wl_registry_bind(registry, id,
3735                                          &wl_data_device_manager_interface, 1);
3736         } else if (strcmp(interface, "text_cursor_position") == 0) {
3737                 d->text_cursor_position =
3738                         wl_registry_bind(registry, id,
3739                                          &text_cursor_position_interface, 1);
3740         } else if (strcmp(interface, "workspace_manager") == 0) {
3741                 init_workspace_manager(d, id);
3742         }
3743
3744         if (d->global_handler)
3745                 d->global_handler(d, id, interface, version, d->user_data);
3746 }
3747
3748 void *
3749 display_bind(struct display *display, uint32_t name,
3750              const struct wl_interface *interface, uint32_t version)
3751 {
3752         return wl_registry_bind(display->registry, name, interface, version);
3753 }
3754
3755 static const struct wl_registry_listener registry_listener = {
3756         registry_handle_global
3757 };
3758
3759 #ifdef HAVE_CAIRO_EGL
3760 static int
3761 init_egl(struct display *d)
3762 {
3763         EGLint major, minor;
3764         EGLint n;
3765
3766 #ifdef USE_CAIRO_GLESV2
3767 #  define GL_BIT EGL_OPENGL_ES2_BIT
3768 #else
3769 #  define GL_BIT EGL_OPENGL_BIT
3770 #endif
3771
3772         static const EGLint argb_cfg_attribs[] = {
3773                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
3774                 EGL_RED_SIZE, 1,
3775                 EGL_GREEN_SIZE, 1,
3776                 EGL_BLUE_SIZE, 1,
3777                 EGL_ALPHA_SIZE, 1,
3778                 EGL_DEPTH_SIZE, 1,
3779                 EGL_RENDERABLE_TYPE, GL_BIT,
3780                 EGL_NONE
3781         };
3782
3783 #ifdef USE_CAIRO_GLESV2
3784         static const EGLint context_attribs[] = {
3785                 EGL_CONTEXT_CLIENT_VERSION, 2,
3786                 EGL_NONE
3787         };
3788         EGLint api = EGL_OPENGL_ES_API;
3789 #else
3790         EGLint *context_attribs = NULL;
3791         EGLint api = EGL_OPENGL_API;
3792 #endif
3793
3794         d->dpy = eglGetDisplay(d->display);
3795         if (!eglInitialize(d->dpy, &major, &minor)) {
3796                 fprintf(stderr, "failed to initialize display\n");
3797                 return -1;
3798         }
3799
3800         if (!eglBindAPI(api)) {
3801                 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
3802                 return -1;
3803         }
3804
3805         if (!eglChooseConfig(d->dpy, argb_cfg_attribs,
3806                              &d->argb_config, 1, &n) || n != 1) {
3807                 fprintf(stderr, "failed to choose argb config\n");
3808                 return -1;
3809         }
3810
3811         d->argb_ctx = eglCreateContext(d->dpy, d->argb_config,
3812                                        EGL_NO_CONTEXT, context_attribs);
3813         if (d->argb_ctx == NULL) {
3814                 fprintf(stderr, "failed to create context\n");
3815                 return -1;
3816         }
3817
3818         if (!eglMakeCurrent(d->dpy, NULL, NULL, d->argb_ctx)) {
3819                 fprintf(stderr, "failed to make context current\n");
3820                 return -1;
3821         }
3822
3823         d->argb_device = cairo_egl_device_create(d->dpy, d->argb_ctx);
3824         if (cairo_device_status(d->argb_device) != CAIRO_STATUS_SUCCESS) {
3825                 fprintf(stderr, "failed to get cairo egl argb device\n");
3826                 return -1;
3827         }
3828
3829         return 0;
3830 }
3831
3832 static void
3833 fini_egl(struct display *display)
3834 {
3835         cairo_device_destroy(display->argb_device);
3836
3837         eglMakeCurrent(display->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
3838                        EGL_NO_CONTEXT);
3839
3840         eglTerminate(display->dpy);
3841         eglReleaseThread();
3842 }
3843 #endif
3844
3845 static void
3846 handle_display_data(struct task *task, uint32_t events)
3847 {
3848         struct display *display =
3849                 container_of(task, struct display, display_task);
3850         struct epoll_event ep;
3851         int ret;
3852
3853         display->display_fd_events = events;
3854
3855         if (events & EPOLLERR || events & EPOLLHUP) {
3856                 display_exit(display);
3857                 return;
3858         }
3859
3860         if (events & EPOLLIN) {
3861                 ret = wl_display_dispatch(display->display);
3862                 if (ret == -1) {
3863                         display_exit(display);
3864                         return;
3865                 }
3866         }
3867
3868         if (events & EPOLLOUT) {
3869                 ret = wl_display_flush(display->display);
3870                 if (ret == 0) {
3871                         ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
3872                         ep.data.ptr = &display->display_task;
3873                         epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
3874                                   display->display_fd, &ep);
3875                 } else if (ret == -1 && errno != EAGAIN) {
3876                         display_exit(display);
3877                         return;
3878                 }
3879         }
3880 }
3881
3882 struct display *
3883 display_create(int argc, char *argv[])
3884 {
3885         struct display *d;
3886
3887         d = malloc(sizeof *d);
3888         if (d == NULL)
3889                 return NULL;
3890
3891         memset(d, 0, sizeof *d);
3892
3893         d->display = wl_display_connect(NULL);
3894         if (d->display == NULL) {
3895                 fprintf(stderr, "failed to create display: %m\n");
3896                 return NULL;
3897         }
3898
3899         d->epoll_fd = os_epoll_create_cloexec();
3900         d->display_fd = wl_display_get_fd(d->display);
3901         d->display_task.run = handle_display_data;
3902         display_watch_fd(d, d->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
3903                          &d->display_task);
3904
3905         wl_list_init(&d->deferred_list);
3906         wl_list_init(&d->input_list);
3907         wl_list_init(&d->output_list);
3908         wl_list_init(&d->global_list);
3909
3910         d->xkb_context = xkb_context_new(0);
3911         if (d->xkb_context == NULL) {
3912                 fprintf(stderr, "Failed to create XKB context\n");
3913                 return NULL;
3914         }
3915
3916         d->workspace = 0;
3917         d->workspace_count = 1;
3918
3919         d->registry = wl_display_get_registry(d->display);
3920         wl_registry_add_listener(d->registry, &registry_listener, d);
3921         wl_display_dispatch(d->display);
3922 #ifdef HAVE_CAIRO_EGL
3923         if (init_egl(d) < 0)
3924                 return NULL;
3925 #endif
3926
3927         create_cursors(d);
3928
3929         d->theme = theme_create();
3930
3931         wl_list_init(&d->window_list);
3932
3933         return d;
3934 }
3935
3936 static void
3937 display_destroy_outputs(struct display *display)
3938 {
3939         struct output *tmp;
3940         struct output *output;
3941
3942         wl_list_for_each_safe(output, tmp, &display->output_list, link)
3943                 output_destroy(output);
3944 }
3945
3946 static void
3947 display_destroy_inputs(struct display *display)
3948 {
3949         struct input *tmp;
3950         struct input *input;
3951
3952         wl_list_for_each_safe(input, tmp, &display->input_list, link)
3953                 input_destroy(input);
3954 }
3955
3956 void
3957 display_destroy(struct display *display)
3958 {
3959         if (!wl_list_empty(&display->window_list))
3960                 fprintf(stderr, "toytoolkit warning: %d windows exist.\n",
3961                         wl_list_length(&display->window_list));
3962
3963         if (!wl_list_empty(&display->deferred_list))
3964                 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n");
3965
3966         display_destroy_outputs(display);
3967         display_destroy_inputs(display);
3968
3969         xkb_context_unref(display->xkb_context);
3970
3971         theme_destroy(display->theme);
3972         destroy_cursors(display);
3973
3974 #ifdef HAVE_CAIRO_EGL
3975         fini_egl(display);
3976 #endif
3977
3978         if (display->shell)
3979                 wl_shell_destroy(display->shell);
3980
3981         if (display->shm)
3982                 wl_shm_destroy(display->shm);
3983
3984         if (display->data_device_manager)
3985                 wl_data_device_manager_destroy(display->data_device_manager);
3986
3987         wl_compositor_destroy(display->compositor);
3988
3989         close(display->epoll_fd);
3990
3991         if (!(display->display_fd_events & EPOLLERR) &&
3992             !(display->display_fd_events & EPOLLHUP))
3993                 wl_display_flush(display->display);
3994
3995         wl_display_disconnect(display->display);
3996         free(display);
3997 }
3998
3999 void
4000 display_set_user_data(struct display *display, void *data)
4001 {
4002         display->user_data = data;
4003 }
4004
4005 void *
4006 display_get_user_data(struct display *display)
4007 {
4008         return display->user_data;
4009 }
4010
4011 struct wl_display *
4012 display_get_display(struct display *display)
4013 {
4014         return display->display;
4015 }
4016
4017 struct output *
4018 display_get_output(struct display *display)
4019 {
4020         return container_of(display->output_list.next, struct output, link);
4021 }
4022
4023 struct wl_compositor *
4024 display_get_compositor(struct display *display)
4025 {
4026         return display->compositor;
4027 }
4028
4029 uint32_t
4030 display_get_serial(struct display *display)
4031 {
4032         return display->serial;
4033 }
4034
4035 EGLDisplay
4036 display_get_egl_display(struct display *d)
4037 {
4038         return d->dpy;
4039 }
4040
4041 struct wl_data_source *
4042 display_create_data_source(struct display *display)
4043 {
4044         return wl_data_device_manager_create_data_source(display->data_device_manager);
4045 }
4046
4047 EGLConfig
4048 display_get_argb_egl_config(struct display *d)
4049 {
4050         return d->argb_config;
4051 }
4052
4053 struct wl_shell *
4054 display_get_shell(struct display *display)
4055 {
4056         return display->shell;
4057 }
4058
4059 int
4060 display_acquire_window_surface(struct display *display,
4061                                struct window *window,
4062                                EGLContext ctx)
4063 {
4064 #ifdef HAVE_CAIRO_EGL
4065         struct egl_window_surface_data *data;
4066         cairo_device_t *device;
4067
4068         if (!window->cairo_surface)
4069                 return -1;
4070         device = cairo_surface_get_device(window->cairo_surface);
4071         if (!device)
4072                 return -1;
4073
4074         if (!ctx) {
4075                 if (device == display->argb_device)
4076                         ctx = display->argb_ctx;
4077                 else
4078                         assert(0);
4079         }
4080
4081         data = cairo_surface_get_user_data(window->cairo_surface,
4082                                            &surface_data_key);
4083
4084         cairo_device_flush(device);
4085         cairo_device_acquire(device);
4086         if (!eglMakeCurrent(display->dpy, data->surf, data->surf, ctx))
4087                 fprintf(stderr, "failed to make surface current\n");
4088
4089         return 0;
4090 #else
4091         return -1;
4092 #endif
4093 }
4094
4095 void
4096 display_release_window_surface(struct display *display,
4097                                struct window *window)
4098 {
4099 #ifdef HAVE_CAIRO_EGL
4100         cairo_device_t *device;
4101         
4102         device = cairo_surface_get_device(window->cairo_surface);
4103         if (!device)
4104                 return;
4105
4106         if (!eglMakeCurrent(display->dpy, NULL, NULL, display->argb_ctx))
4107                 fprintf(stderr, "failed to make context current\n");
4108         cairo_device_release(device);
4109 #endif
4110 }
4111
4112 void
4113 display_defer(struct display *display, struct task *task)
4114 {
4115         wl_list_insert(&display->deferred_list, &task->link);
4116 }
4117
4118 void
4119 display_watch_fd(struct display *display,
4120                  int fd, uint32_t events, struct task *task)
4121 {
4122         struct epoll_event ep;
4123
4124         ep.events = events;
4125         ep.data.ptr = task;
4126         epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
4127 }
4128
4129 void
4130 display_unwatch_fd(struct display *display, int fd)
4131 {
4132         epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
4133 }
4134
4135 void
4136 display_run(struct display *display)
4137 {
4138         struct task *task;
4139         struct epoll_event ep[16];
4140         int i, count, ret;
4141
4142         display->running = 1;
4143         while (1) {
4144                 while (!wl_list_empty(&display->deferred_list)) {
4145                         task = container_of(display->deferred_list.prev,
4146                                             struct task, link);
4147                         wl_list_remove(&task->link);
4148                         task->run(task, 0);
4149                 }
4150
4151                 wl_display_dispatch_pending(display->display);
4152
4153                 if (!display->running)
4154                         break;
4155
4156                 ret = wl_display_flush(display->display);
4157                 if (ret < 0 && errno == EAGAIN) {
4158                         ep[0].events =
4159                                 EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
4160                         ep[0].data.ptr = &display->display_task;
4161
4162                         epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD,
4163                                   display->display_fd, &ep[0]);
4164                 } else if (ret < 0) {
4165                         break;
4166                 }
4167
4168                 count = epoll_wait(display->epoll_fd,
4169                                    ep, ARRAY_LENGTH(ep), -1);
4170                 for (i = 0; i < count; i++) {
4171                         task = ep[i].data.ptr;
4172                         task->run(task, ep[i].events);
4173                 }
4174         }
4175 }
4176
4177 void
4178 display_exit(struct display *display)
4179 {
4180         display->running = 0;
4181 }