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