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