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