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