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