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