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