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