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