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