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