c8836a80a446661d74c9a93a047080362098ac18
[profile/ivi/wayland.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 #include "../config.h"
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
33 #include <cairo.h>
34 #include <glib.h>
35 #include <glib-object.h>
36 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #include <sys/mman.h>
38
39 #include <wayland-egl.h>
40
41 #include <GL/gl.h>
42 #include <EGL/egl.h>
43 #include <EGL/eglext.h>
44
45 #ifdef HAVE_CAIRO_EGL
46 #include <cairo-gl.h>
47 #endif
48
49 #include <X11/extensions/XKBcommon.h>
50
51 #include <linux/input.h>
52 #include "wayland-util.h"
53 #include "wayland-client.h"
54 #include "wayland-glib.h"
55 #include "cairo-util.h"
56
57 #include "window.h"
58
59 struct display {
60         struct wl_display *display;
61         struct wl_egl_display *native_dpy;
62         struct wl_compositor *compositor;
63         struct wl_shell *shell;
64         struct wl_shm *shm;
65         struct wl_output *output;
66         struct rectangle screen_allocation;
67         int authenticated;
68         EGLDisplay dpy;
69         EGLContext ctx;
70         cairo_device_t *device;
71         int fd;
72         GMainLoop *loop;
73         GSource *source;
74         struct wl_list window_list;
75         struct wl_list input_list;
76         char *device_name;
77         cairo_surface_t *active_frame, *inactive_frame, *shadow;
78         struct xkb_desc *xkb;
79         cairo_surface_t **pointer_surfaces;
80
81         display_global_handler_t global_handler;
82
83         PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
84         PFNEGLCREATEIMAGEKHRPROC create_image;
85         PFNEGLDESTROYIMAGEKHRPROC destroy_image;
86 };
87
88 struct window {
89         struct display *display;
90         struct window *parent;
91         struct wl_surface *surface;
92         char *title;
93         struct rectangle allocation, saved_allocation, server_allocation;
94         int x, y;
95         int resize_edges;
96         int redraw_scheduled;
97         int minimum_width, minimum_height;
98         int margin;
99         int fullscreen;
100         int decoration;
101         struct input *grab_device;
102         struct input *keyboard_device;
103         uint32_t name;
104         enum window_buffer_type buffer_type;
105
106         EGLImageKHR *image;
107         cairo_surface_t *cairo_surface, *pending_surface;
108
109         window_resize_handler_t resize_handler;
110         window_redraw_handler_t redraw_handler;
111         window_key_handler_t key_handler;
112         window_button_handler_t button_handler;
113         window_keyboard_focus_handler_t keyboard_focus_handler;
114         window_motion_handler_t motion_handler;
115
116         void *user_data;
117         struct wl_list link;
118 };
119
120 struct input {
121         struct display *display;
122         struct wl_input_device *input_device;
123         struct window *pointer_focus;
124         struct window *keyboard_focus;
125         struct selection_offer *offer;
126         uint32_t current_pointer_image;
127         uint32_t modifiers;
128         int32_t x, y, sx, sy;
129         struct wl_list link;
130 };
131
132 enum {
133         POINTER_DEFAULT = 100,
134         POINTER_UNSET
135 };
136
137 enum window_location {
138         WINDOW_INTERIOR = 0,
139         WINDOW_RESIZING_TOP = 1,
140         WINDOW_RESIZING_BOTTOM = 2,
141         WINDOW_RESIZING_LEFT = 4,
142         WINDOW_RESIZING_TOP_LEFT = 5,
143         WINDOW_RESIZING_BOTTOM_LEFT = 6,
144         WINDOW_RESIZING_RIGHT = 8,
145         WINDOW_RESIZING_TOP_RIGHT = 9,
146         WINDOW_RESIZING_BOTTOM_RIGHT = 10,
147         WINDOW_RESIZING_MASK = 15,
148         WINDOW_EXTERIOR = 16,
149         WINDOW_TITLEBAR = 17,
150         WINDOW_CLIENT_AREA = 18,
151 };
152
153 const char *option_xkb_layout = "us";
154 const char *option_xkb_variant = "";
155 const char *option_xkb_options = "";
156
157 static const GOptionEntry xkb_option_entries[] = {
158         { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
159           &option_xkb_layout, "XKB Layout" },
160         { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
161           &option_xkb_variant, "XKB Variant" },
162         { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
163           &option_xkb_options, "XKB Options" },
164         { NULL }
165 };
166
167 static void
168 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
169 {
170         cairo_move_to(cr, x0, y0 + radius);
171         cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
172         cairo_line_to(cr, x1 - radius, y0);
173         cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
174         cairo_line_to(cr, x1, y1 - radius);
175         cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
176         cairo_line_to(cr, x0 + radius, y1);
177         cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
178         cairo_close_path(cr);
179 }
180
181 static const cairo_user_data_key_t surface_data_key;
182 struct surface_data {
183         struct wl_buffer *buffer;
184 };
185
186 #define MULT(_d,c,a,t) \
187         do { t = c * a + 0x7f; _d = ((t >> 8) + t) >> 8; } while (0)
188
189 #ifdef HAVE_CAIRO_EGL
190
191 struct egl_image_surface_data {
192         struct surface_data data;
193         EGLImageKHR image;
194         GLuint texture;
195         struct display *display;
196         struct wl_egl_pixmap *pixmap;
197 };
198
199 static void
200 egl_image_surface_data_destroy(void *p)
201 {
202         struct egl_image_surface_data *data = p;
203         struct display *d = data->display;
204
205         cairo_device_acquire(d->device);
206         glDeleteTextures(1, &data->texture);
207         cairo_device_release(d->device);
208
209         d->destroy_image(d->dpy, data->image);
210         wl_buffer_destroy(data->data.buffer);
211         wl_egl_pixmap_destroy(data->pixmap);
212         free(p);
213 }
214
215 EGLImageKHR
216 display_get_image_for_egl_image_surface(struct display *display,
217                                         cairo_surface_t *surface)
218 {
219         struct egl_image_surface_data *data;
220
221         data = cairo_surface_get_user_data (surface, &surface_data_key);
222
223         return data->image;
224 }
225
226 static cairo_surface_t *
227 display_create_egl_image_surface(struct display *display,
228                                  struct rectangle *rectangle)
229 {
230         struct egl_image_surface_data *data;
231         EGLDisplay dpy = display->dpy;
232         cairo_surface_t *surface;
233         struct wl_visual *visual;
234
235         data = malloc(sizeof *data);
236         if (data == NULL)
237                 return NULL;
238
239         data->display = display;
240
241         visual = wl_display_get_premultiplied_argb_visual(display->display);
242         data->pixmap =wl_egl_pixmap_create(display->native_dpy,
243                                            rectangle->width,
244                                            rectangle->height,
245                                            visual, 0);
246         if (data->pixmap == NULL) {
247                 free(data);
248                 return NULL;
249         }
250
251         data->image = display->create_image(dpy, NULL,
252                                             EGL_NATIVE_PIXMAP_KHR,
253                                             (EGLClientBuffer) data->pixmap,
254                                             NULL);
255         if (data->image == EGL_NO_IMAGE_KHR) {
256                 wl_egl_pixmap_destroy(data->pixmap);
257                 free(data);
258                 return NULL;
259         }
260
261         data->data.buffer =
262                 wl_egl_pixmap_create_buffer(display->native_dpy, data->pixmap);
263
264         cairo_device_acquire(display->device);
265         glGenTextures(1, &data->texture);
266         glBindTexture(GL_TEXTURE_2D, data->texture);
267         display->image_target_texture_2d(GL_TEXTURE_2D, data->image);
268         cairo_device_release(display->device);
269
270         surface = cairo_gl_surface_create_for_texture(display->device,
271                                                       CAIRO_CONTENT_COLOR_ALPHA,
272                                                       data->texture,
273                                                       rectangle->width,
274                                                       rectangle->height);
275
276         cairo_surface_set_user_data (surface, &surface_data_key,
277                                      data, egl_image_surface_data_destroy);
278
279         return surface;
280 }
281
282 static cairo_surface_t *
283 display_create_egl_image_surface_from_file(struct display *display,
284                                            const char *filename,
285                                            struct rectangle *rect)
286 {
287         cairo_surface_t *surface;
288         GdkPixbuf *pixbuf;
289         GError *error = NULL;
290         int stride, i;
291         unsigned char *pixels, *p, *end;
292         struct egl_image_surface_data *data;
293
294         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
295                                                    rect->width, rect->height,
296                                                    FALSE, &error);
297         if (error != NULL)
298                 return NULL;
299
300         if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
301             gdk_pixbuf_get_n_channels(pixbuf) != 4) {
302                 g_object_unref(pixbuf);
303                 return NULL;
304         }
305
306
307         stride = gdk_pixbuf_get_rowstride(pixbuf);
308         pixels = gdk_pixbuf_get_pixels(pixbuf);
309
310         for (i = 0; i < rect->height; i++) {
311                 p = pixels + i * stride;
312                 end = p + rect->width * 4;
313                 while (p < end) {
314                         unsigned int t;
315
316                         MULT(p[0], p[0], p[3], t);
317                         MULT(p[1], p[1], p[3], t);
318                         MULT(p[2], p[2], p[3], t);
319                         p += 4;
320
321                 }
322         }
323
324         surface = display_create_egl_image_surface(display, rect);
325         if (surface == NULL) {
326                 g_object_unref(pixbuf);
327                 return NULL;
328         }
329
330         data = cairo_surface_get_user_data(surface, &surface_data_key);
331
332         cairo_device_acquire(display->device);
333         glBindTexture(GL_TEXTURE_2D, data->texture);
334         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rect->width, rect->height,
335                         GL_RGBA, GL_UNSIGNED_BYTE, pixels);
336         cairo_device_release(display->device);
337
338         g_object_unref(pixbuf);
339
340         return surface;
341 }
342
343 #endif
344
345 struct wl_buffer *
346 display_get_buffer_for_surface(struct display *display,
347                                cairo_surface_t *surface)
348 {
349         struct surface_data *data;
350
351         data = cairo_surface_get_user_data (surface, &surface_data_key);
352
353         return data->buffer;
354 }
355
356 struct shm_surface_data {
357         struct surface_data data;
358         void *map;
359         size_t length;
360 };
361
362 static void
363 shm_surface_data_destroy(void *p)
364 {
365         struct shm_surface_data *data = p;
366
367         wl_buffer_destroy(data->data.buffer);
368         munmap(data->map, data->length);
369 }
370
371 static cairo_surface_t *
372 display_create_shm_surface(struct display *display,
373                            struct rectangle *rectangle)
374 {
375         struct shm_surface_data *data;
376         cairo_surface_t *surface;
377         struct wl_visual *visual;
378         int stride, fd;
379         char filename[] = "/tmp/wayland-shm-XXXXXX";
380
381         data = malloc(sizeof *data);
382         if (data == NULL)
383                 return NULL;
384
385         stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
386                                                 rectangle->width);
387         data->length = stride * rectangle->height;
388         fd = mkstemp(filename);
389         if (fd < 0) {
390                 fprintf(stderr, "open %s failed: %m\n", filename);
391                 return NULL;
392         }
393         if (ftruncate(fd, data->length) < 0) {
394                 fprintf(stderr, "ftruncate failed: %m\n");
395                 close(fd);
396                 return NULL;
397         }
398
399         data->map = mmap(NULL, data->length,
400                          PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
401         unlink(filename);
402
403         if (data->map == MAP_FAILED) {
404                 fprintf(stderr, "mmap failed: %m\n");
405                 close(fd);
406                 return NULL;
407         }
408
409         surface = cairo_image_surface_create_for_data (data->map,
410                                                        CAIRO_FORMAT_ARGB32,
411                                                        rectangle->width,
412                                                        rectangle->height,
413                                                        stride);
414
415         cairo_surface_set_user_data (surface, &surface_data_key,
416                                      data, shm_surface_data_destroy);
417
418         visual = wl_display_get_premultiplied_argb_visual(display->display);
419         data->data.buffer = wl_shm_create_buffer(display->shm,
420                                                  fd,
421                                                  rectangle->width,
422                                                  rectangle->height,
423                                                  stride, visual);
424
425         close(fd);
426
427         return surface;
428 }
429
430 static cairo_surface_t *
431 display_create_shm_surface_from_file(struct display *display,
432                                      const char *filename,
433                                      struct rectangle *rect)
434 {
435         cairo_surface_t *surface;
436         GdkPixbuf *pixbuf;
437         GError *error = NULL;
438         int stride, i;
439         unsigned char *pixels, *p, *end, *dest_data;
440         int dest_stride;
441         uint32_t *d;
442
443         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
444                                                    rect->width, rect->height,
445                                                    FALSE, &error);
446         if (error != NULL)
447                 return NULL;
448
449         if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
450             gdk_pixbuf_get_n_channels(pixbuf) != 4) {
451                 g_object_unref(pixbuf);
452                 return NULL;
453         }
454
455         stride = gdk_pixbuf_get_rowstride(pixbuf);
456         pixels = gdk_pixbuf_get_pixels(pixbuf);
457
458         surface = display_create_shm_surface(display, rect);
459         if (surface == NULL) {
460                 g_object_unref(pixbuf);
461                 return NULL;
462         }
463
464         dest_data = cairo_image_surface_get_data (surface);
465         dest_stride = cairo_image_surface_get_stride (surface);
466
467         for (i = 0; i < rect->height; i++) {
468                 d = (uint32_t *) (dest_data + i * dest_stride);
469                 p = pixels + i * stride;
470                 end = p + rect->width * 4;
471                 while (p < end) {
472                         unsigned int t;
473                         unsigned char a, r, g, b;
474
475                         a = p[3];
476                         MULT(r, p[0], a, t);
477                         MULT(g, p[1], a, t);
478                         MULT(b, p[2], a, t);
479                         p += 4;
480                         *d++ = (a << 24) | (r << 16) | (g << 8) | b;
481                 }
482         }
483
484         g_object_unref(pixbuf);
485
486         return surface;
487 }
488
489 static int
490 check_size(struct rectangle *rect)
491 {
492         if (rect->width && rect->height)
493                 return 0;
494
495         fprintf(stderr, "tried to create surface of "
496                 "width: %d, height: %d\n", rect->width, rect->height);
497         return -1;
498 }
499
500 cairo_surface_t *
501 display_create_surface(struct display *display,
502                        struct rectangle *rectangle)
503 {
504         if (check_size(rectangle) < 0)
505                 return NULL;
506 #ifdef HAVE_CAIRO_EGL
507         if (display->dpy) {
508                 return display_create_egl_image_surface(display, rectangle);
509         }
510 #endif
511         return display_create_shm_surface(display, rectangle);
512 }
513
514 static cairo_surface_t *
515 display_create_surface_from_file(struct display *display,
516                                  const char *filename,
517                                  struct rectangle *rectangle)
518 {
519         if (check_size(rectangle) < 0)
520                 return NULL;
521 #ifdef HAVE_CAIRO_EGL
522         if (display->dpy) {
523                 return display_create_egl_image_surface_from_file(display,
524                                                                   filename,
525                                                                   rectangle);
526         }
527 #endif
528         return display_create_shm_surface_from_file(display, filename, rectangle);
529 }
530  static const struct {
531         const char *filename;
532         int hotspot_x, hotspot_y;
533 } pointer_images[] = {
534         { DATADIR "/wayland/bottom_left_corner.png",     6, 30 },
535         { DATADIR "/wayland/bottom_right_corner.png",   28, 28 },
536         { DATADIR "/wayland/bottom_side.png",           16, 20 },
537         { DATADIR "/wayland/grabbing.png",              20, 17 },
538         { DATADIR "/wayland/left_ptr.png",              10,  5 },
539         { DATADIR "/wayland/left_side.png",             10, 20 },
540         { DATADIR "/wayland/right_side.png",            30, 19 },
541         { DATADIR "/wayland/top_left_corner.png",        8,  8 },
542         { DATADIR "/wayland/top_right_corner.png",      26,  8 },
543         { DATADIR "/wayland/top_side.png",              18,  8 },
544         { DATADIR "/wayland/xterm.png",                 15, 15 },
545         { DATADIR "/wayland/hand1.png",                 18, 11 }
546 };
547
548 static void
549 create_pointer_surfaces(struct display *display)
550 {
551         int i, count;
552         const int width = 32, height = 32;
553         struct rectangle rect;
554
555         count = ARRAY_LENGTH(pointer_images);
556         display->pointer_surfaces =
557                 malloc(count * sizeof *display->pointer_surfaces);
558         rect.width = width;
559         rect.height = height;
560         for (i = 0; i < count; i++) {
561                 display->pointer_surfaces[i] =
562                         display_create_surface_from_file(display,
563                                                          pointer_images[i].filename,
564                                                          &rect);
565         }
566
567 }
568
569 cairo_surface_t *
570 display_get_pointer_surface(struct display *display, int pointer,
571                             int *width, int *height,
572                             int *hotspot_x, int *hotspot_y)
573 {
574         cairo_surface_t *surface;
575
576         surface = display->pointer_surfaces[pointer];
577 #if HAVE_CAIRO_EGL
578         *width = cairo_gl_surface_get_width(surface);
579         *height = cairo_gl_surface_get_height(surface);
580 #else
581         *width = cairo_image_surface_get_width(surface);
582         *height = cairo_image_surface_get_height(surface);
583 #endif
584         *hotspot_x = pointer_images[pointer].hotspot_x;
585         *hotspot_y = pointer_images[pointer].hotspot_y;
586
587         return cairo_surface_reference(surface);
588 }
589
590
591 static void
592 window_attach_surface(struct window *window);
593
594 static void
595 free_surface(void *data)
596 {
597         struct window *window = data;
598
599         cairo_surface_destroy(window->pending_surface);
600         window->pending_surface = NULL;
601         if (window->cairo_surface)
602                 window_attach_surface(window);
603 }
604
605 static void
606 window_attach_surface(struct window *window)
607 {
608         struct display *display = window->display;
609         struct wl_buffer *buffer;
610         int32_t x, y;
611
612         if (window->pending_surface != NULL)
613                 return;
614
615         window->pending_surface = window->cairo_surface;
616         window->cairo_surface = NULL;
617
618         buffer = display_get_buffer_for_surface(display,
619                                                 window->pending_surface);
620         if (window->resize_edges & WINDOW_RESIZING_LEFT)
621                 x = window->server_allocation.width -
622                         window->allocation.width;
623         else
624                 x = 0;
625
626         if (window->resize_edges & WINDOW_RESIZING_TOP)
627                 y = window->server_allocation.height -
628                         window->allocation.height;
629         else
630                 y = 0;
631
632         window->server_allocation = window->allocation;
633         window->resize_edges = 0;
634         wl_surface_attach(window->surface, buffer, x, y);
635         wl_display_sync_callback(display->display, free_surface, window);
636
637         if (window->fullscreen)
638                 wl_surface_map_fullscreen(window->surface);
639         else if (!window->parent)
640                 wl_surface_map_toplevel(window->surface);
641         else
642                 wl_surface_map_transient(window->surface,
643                                          window->parent->surface,
644                                          window->x, window->y, 0);
645
646         wl_surface_damage(window->surface, 0, 0,
647                           window->allocation.width,
648                           window->allocation.height);
649 }
650
651 void
652 window_flush(struct window *window)
653 {
654        if (window->cairo_surface)
655                window_attach_surface(window);
656 }
657
658 void
659 window_set_surface(struct window *window, cairo_surface_t *surface)
660 {
661         cairo_surface_reference(surface);
662
663         if (window->cairo_surface != NULL)
664                 cairo_surface_destroy(window->cairo_surface);
665
666         window->cairo_surface = surface;
667 }
668
669 void
670 window_create_surface(struct window *window)
671 {
672         cairo_surface_t *surface;
673
674         switch (window->buffer_type) {
675 #ifdef HAVE_CAIRO_EGL
676         case WINDOW_BUFFER_TYPE_EGL_IMAGE:
677                 surface = display_create_surface(window->display,
678                                                  &window->allocation);
679                 break;
680 #endif
681         case WINDOW_BUFFER_TYPE_SHM:
682                 surface = display_create_shm_surface(window->display,
683                                                      &window->allocation);
684                 break;
685         default:
686                 surface = NULL;
687                 break;
688         }
689
690         window_set_surface(window, surface);
691         cairo_surface_destroy(surface);
692 }
693
694 static void
695 window_draw_menu(struct window *window)
696 {
697         cairo_t *cr;
698         int width, height, r = 5;
699
700         window_create_surface(window);
701
702         cr = cairo_create(window->cairo_surface);
703         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
704         cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
705         cairo_paint(cr);
706
707         width = window->allocation.width;
708         height = window->allocation.height;
709         rounded_rect(cr, r, r, width - r, height - r, r);
710         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
711         cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 0.5);
712         cairo_fill(cr);
713         cairo_destroy(cr);
714 }
715
716 static void
717 window_draw_decorations(struct window *window)
718 {
719         cairo_t *cr;
720         cairo_text_extents_t extents;
721         cairo_surface_t *frame;
722         int width, height, shadow_dx = 3, shadow_dy = 3;
723
724         window_create_surface(window);
725
726         width = window->allocation.width;
727         height = window->allocation.height;
728
729         cr = cairo_create(window->cairo_surface);
730
731         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
732         cairo_set_source_rgba(cr, 0, 0, 0, 0);
733         cairo_paint(cr);
734
735         cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
736         tile_mask(cr, window->display->shadow,
737                   shadow_dx, shadow_dy, width, height,
738                   window->margin + 10 - shadow_dx,
739                   window->margin + 10 - shadow_dy);
740
741         if (window->keyboard_device)
742                 frame = window->display->active_frame;
743         else
744                 frame = window->display->inactive_frame;
745
746         tile_source(cr, frame, 0, 0, width, height,
747                     window->margin + 10, window->margin + 50);
748
749         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
750         cairo_set_font_size(cr, 14);
751         cairo_text_extents(cr, window->title, &extents);
752         cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
753         if (window->keyboard_device)
754                 cairo_set_source_rgb(cr, 0, 0, 0);
755         else
756                 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
757         cairo_show_text(cr, window->title);
758
759         cairo_destroy(cr);
760
761         cairo_device_flush (window->display->device);
762 }
763
764 void
765 window_destroy(struct window *window)
766 {
767         wl_surface_destroy(window->surface);
768         wl_list_remove(&window->link);
769         free(window);
770 }
771
772 void
773 display_flush_cairo_device(struct display *display)
774 {
775         cairo_device_flush (display->device);
776 }
777
778 void
779 window_draw(struct window *window)
780 {
781         if (window->parent)
782                 window_draw_menu(window);
783         else if (window->fullscreen || !window->decoration)
784                 window_create_surface(window);
785         else
786                 window_draw_decorations(window);
787 }
788
789 cairo_surface_t *
790 window_get_surface(struct window *window)
791 {
792         return cairo_surface_reference(window->cairo_surface);
793 }
794
795 static int
796 get_pointer_location(struct window *window, int32_t x, int32_t y)
797 {
798         int vlocation, hlocation, location;
799         const int grip_size = 8;
800
801         if (!window->decoration)
802                 return WINDOW_CLIENT_AREA;
803
804         if (x < window->margin)
805                 hlocation = WINDOW_EXTERIOR;
806         else if (window->margin <= x && x < window->margin + grip_size)
807                 hlocation = WINDOW_RESIZING_LEFT;
808         else if (x < window->allocation.width - window->margin - grip_size)
809                 hlocation = WINDOW_INTERIOR;
810         else if (x < window->allocation.width - window->margin)
811                 hlocation = WINDOW_RESIZING_RIGHT;
812         else
813                 hlocation = WINDOW_EXTERIOR;
814
815         if (y < window->margin)
816                 vlocation = WINDOW_EXTERIOR;
817         else if (window->margin <= y && y < window->margin + grip_size)
818                 vlocation = WINDOW_RESIZING_TOP;
819         else if (y < window->allocation.height - window->margin - grip_size)
820                 vlocation = WINDOW_INTERIOR;
821         else if (y < window->allocation.height - window->margin)
822                 vlocation = WINDOW_RESIZING_BOTTOM;
823         else
824                 vlocation = WINDOW_EXTERIOR;
825
826         location = vlocation | hlocation;
827         if (location & WINDOW_EXTERIOR)
828                 location = WINDOW_EXTERIOR;
829         if (location == WINDOW_INTERIOR && y < window->margin + 50)
830                 location = WINDOW_TITLEBAR;
831         else if (location == WINDOW_INTERIOR)
832                 location = WINDOW_CLIENT_AREA;
833
834         return location;
835 }
836
837 static void
838 set_pointer_image(struct input *input, uint32_t time, int pointer)
839 {
840         struct display *display = input->display;
841         struct wl_buffer *buffer;
842         cairo_surface_t *surface;
843         int location;
844
845         location = get_pointer_location(input->pointer_focus,
846                                         input->sx, input->sy);
847         switch (location) {
848         case WINDOW_RESIZING_TOP:
849                 pointer = POINTER_TOP;
850                 break;
851         case WINDOW_RESIZING_BOTTOM:
852                 pointer = POINTER_BOTTOM;
853                 break;
854         case WINDOW_RESIZING_LEFT:
855                 pointer = POINTER_LEFT;
856                 break;
857         case WINDOW_RESIZING_RIGHT:
858                 pointer = POINTER_RIGHT;
859                 break;
860         case WINDOW_RESIZING_TOP_LEFT:
861                 pointer = POINTER_TOP_LEFT;
862                 break;
863         case WINDOW_RESIZING_TOP_RIGHT:
864                 pointer = POINTER_TOP_RIGHT;
865                 break;
866         case WINDOW_RESIZING_BOTTOM_LEFT:
867                 pointer = POINTER_BOTTOM_LEFT;
868                 break;
869         case WINDOW_RESIZING_BOTTOM_RIGHT:
870                 pointer = POINTER_BOTTOM_RIGHT;
871                 break;
872         case WINDOW_EXTERIOR:
873         case WINDOW_TITLEBAR:
874                 if (input->current_pointer_image == POINTER_DEFAULT)
875                         return;
876
877                 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
878                 input->current_pointer_image = POINTER_DEFAULT;
879                 return;
880         default:
881                 break;
882         }
883
884         if (pointer == input->current_pointer_image)
885                 return;
886
887         input->current_pointer_image = pointer;
888         surface = display->pointer_surfaces[pointer];
889         buffer = display_get_buffer_for_surface(display, surface);
890         wl_input_device_attach(input->input_device, time, buffer,
891                                pointer_images[pointer].hotspot_x,
892                                pointer_images[pointer].hotspot_y);
893 }
894
895 static void
896 window_handle_motion(void *data, struct wl_input_device *input_device,
897                      uint32_t time,
898                      int32_t x, int32_t y, int32_t sx, int32_t sy)
899 {
900         struct input *input = data;
901         struct window *window = input->pointer_focus;
902         int pointer = POINTER_LEFT_PTR;
903
904         input->x = x;
905         input->y = y;
906         input->sx = sx;
907         input->sy = sy;
908
909         if (window->motion_handler)
910                 pointer = (*window->motion_handler)(window, input, time,
911                                                     x, y, sx, sy,
912                                                     window->user_data);
913
914         set_pointer_image(input, time, pointer);
915 }
916
917 static void
918 window_handle_button(void *data,
919                      struct wl_input_device *input_device,
920                      uint32_t time, uint32_t button, uint32_t state)
921 {
922         struct input *input = data;
923         struct window *window = input->pointer_focus;
924         int location;
925
926         location = get_pointer_location(window, input->sx, input->sy);
927
928         if (button == BTN_LEFT && state == 1) {
929                 switch (location) {
930                 case WINDOW_TITLEBAR:
931                         wl_shell_move(window->display->shell,
932                                       window->surface, input_device, time);
933                         break;
934                 case WINDOW_RESIZING_TOP:
935                 case WINDOW_RESIZING_BOTTOM:
936                 case WINDOW_RESIZING_LEFT:
937                 case WINDOW_RESIZING_RIGHT:
938                 case WINDOW_RESIZING_TOP_LEFT:
939                 case WINDOW_RESIZING_TOP_RIGHT:
940                 case WINDOW_RESIZING_BOTTOM_LEFT:
941                 case WINDOW_RESIZING_BOTTOM_RIGHT:
942                         wl_shell_resize(window->display->shell,
943                                         window->surface, input_device, time,
944                                         location);
945                         break;
946                 case WINDOW_CLIENT_AREA:
947                         if (window->button_handler)
948                                 (*window->button_handler)(window,
949                                                           input, time,
950                                                           button, state,
951                                                           window->user_data);
952                         break;
953                 }
954         } else {
955                 if (window->button_handler)
956                         (*window->button_handler)(window,
957                                                   input, time,
958                                                   button, state,
959                                                   window->user_data);
960         }
961 }
962
963 static void
964 window_handle_key(void *data, struct wl_input_device *input_device,
965                   uint32_t time, uint32_t key, uint32_t state)
966 {
967         struct input *input = data;
968         struct window *window = input->keyboard_focus;
969         struct display *d = window->display;
970         uint32_t code, sym, level;
971
972         code = key + d->xkb->min_key_code;
973         if (window->keyboard_device != input)
974                 return;
975
976         level = 0;
977         if (input->modifiers & XKB_COMMON_SHIFT_MASK &&
978             XkbKeyGroupWidth(d->xkb, code, 0) > 1)
979                 level = 1;
980
981         sym = XkbKeySymEntry(d->xkb, code, level, 0);
982
983         if (state)
984                 input->modifiers |= d->xkb->map->modmap[code];
985         else
986                 input->modifiers &= ~d->xkb->map->modmap[code];
987
988         if (window->key_handler)
989                 (*window->key_handler)(window, input, time, key, sym, state,
990                                        window->user_data);
991 }
992
993 static void
994 window_handle_pointer_focus(void *data,
995                             struct wl_input_device *input_device,
996                             uint32_t time, struct wl_surface *surface,
997                             int32_t x, int32_t y, int32_t sx, int32_t sy)
998 {
999         struct input *input = data;
1000         struct window *window;
1001         int pointer;
1002
1003         if (surface) {
1004                 input->pointer_focus = wl_surface_get_user_data(surface);
1005                 window = input->pointer_focus;
1006
1007                 input->x = x;
1008                 input->y = y;
1009                 input->sx = sx;
1010                 input->sy = sy;
1011
1012                 pointer = POINTER_LEFT_PTR;
1013                 if (window->motion_handler)
1014                         pointer = (*window->motion_handler)(window,
1015                                                             input, time,
1016                                                             x, y, sx, sy,
1017                                                             window->user_data);
1018
1019                 set_pointer_image(input, time, pointer);
1020         } else {
1021                 input->pointer_focus = NULL;
1022                 input->current_pointer_image = POINTER_UNSET;
1023         }
1024 }
1025
1026 static void
1027 window_handle_keyboard_focus(void *data,
1028                              struct wl_input_device *input_device,
1029                              uint32_t time,
1030                              struct wl_surface *surface,
1031                              struct wl_array *keys)
1032 {
1033         struct input *input = data;
1034         struct window *window = input->keyboard_focus;
1035         struct display *d = input->display;
1036         uint32_t *k, *end;
1037
1038         window = input->keyboard_focus;
1039         if (window) {
1040                 window->keyboard_device = NULL;
1041                 if (window->keyboard_focus_handler)
1042                         (*window->keyboard_focus_handler)(window, NULL,
1043                                                           window->user_data);
1044         }
1045
1046         if (surface)
1047                 input->keyboard_focus = wl_surface_get_user_data(surface);
1048         else
1049                 input->keyboard_focus = NULL;
1050
1051         end = keys->data + keys->size;
1052         input->modifiers = 0;
1053         for (k = keys->data; k < end; k++)
1054                 input->modifiers |= d->xkb->map->modmap[*k];
1055
1056         window = input->keyboard_focus;
1057         if (window) {
1058                 window->keyboard_device = input;
1059                 if (window->keyboard_focus_handler)
1060                         (*window->keyboard_focus_handler)(window,
1061                                                           window->keyboard_device,
1062                                                           window->user_data);
1063         }
1064 }
1065
1066 static const struct wl_input_device_listener input_device_listener = {
1067         window_handle_motion,
1068         window_handle_button,
1069         window_handle_key,
1070         window_handle_pointer_focus,
1071         window_handle_keyboard_focus,
1072 };
1073
1074 void
1075 input_get_position(struct input *input, int32_t *x, int32_t *y)
1076 {
1077         *x = input->sx;
1078         *y = input->sy;
1079 }
1080
1081 struct wl_input_device *
1082 input_get_input_device(struct input *input)
1083 {
1084         return input->input_device;
1085 }
1086
1087 uint32_t
1088 input_get_modifiers(struct input *input)
1089 {
1090         return input->modifiers;
1091 }
1092
1093 struct wl_drag *
1094 window_create_drag(struct window *window)
1095 {
1096         cairo_device_flush (window->display->device);
1097
1098         return wl_shell_create_drag(window->display->shell);
1099 }
1100
1101 void
1102 window_move(struct window *window, struct input *input, uint32_t time)
1103 {
1104         wl_shell_move(window->display->shell,
1105                       window->surface, input->input_device, time);
1106 }
1107
1108 void
1109 window_activate_drag(struct wl_drag *drag, struct window *window,
1110                      struct input *input, uint32_t time)
1111 {
1112         wl_drag_activate(drag, window->surface, input->input_device, time);
1113 }
1114
1115 static void
1116 handle_configure(void *data, struct wl_shell *shell,
1117                  uint32_t time, uint32_t edges,
1118                  struct wl_surface *surface, int32_t width, int32_t height)
1119 {
1120         struct window *window = wl_surface_get_user_data(surface);
1121         int32_t child_width, child_height;
1122
1123         /* FIXME: this is probably the wrong place to check for width
1124          * or height <= 0, but it prevents the compositor from crashing
1125          */
1126         if (width <= 0 || height <= 0)
1127                 return;
1128
1129         window->resize_edges = edges;
1130
1131         if (window->resize_handler) {
1132                 child_width = width - 20 - window->margin * 2;
1133                 child_height = height - 60 - window->margin * 2;
1134
1135                 (*window->resize_handler)(window,
1136                                           child_width, child_height,
1137                                           window->user_data);
1138         } else {
1139                 window->allocation.width = width;
1140                 window->allocation.height = height;
1141
1142                 if (window->redraw_handler)
1143                         window_schedule_redraw(window);
1144         }
1145 }
1146
1147 static const struct wl_shell_listener shell_listener = {
1148         handle_configure,
1149 };
1150
1151 void
1152 window_get_child_allocation(struct window *window,
1153                             struct rectangle *allocation)
1154 {
1155         if (window->fullscreen || !window->decoration) {
1156                 *allocation = window->allocation;
1157         } else {
1158                 allocation->x = window->margin + 10;
1159                 allocation->y = window->margin + 50;
1160                 allocation->width =
1161                         window->allocation.width - 20 - window->margin * 2;
1162                 allocation->height =
1163                         window->allocation.height - 60 - window->margin * 2;
1164         }
1165 }
1166
1167 void
1168 window_set_child_size(struct window *window, int32_t width, int32_t height)
1169 {
1170         if (!window->fullscreen) {
1171                 window->allocation.x = 20 + window->margin;
1172                 window->allocation.y = 60 + window->margin;
1173                 window->allocation.width = width + 20 + window->margin * 2;
1174                 window->allocation.height = height + 60 + window->margin * 2;
1175         } else {
1176                 window->allocation.x = 0;
1177                 window->allocation.y = 0;
1178                 window->allocation.width = width;
1179                 window->allocation.height = height;
1180         }
1181 }
1182
1183 static gboolean
1184 idle_redraw(void *data)
1185 {
1186         struct window *window = data;
1187
1188         window->redraw_handler(window, window->user_data);
1189
1190         window->redraw_scheduled = 0;
1191
1192         return FALSE;
1193 }
1194
1195 void
1196 window_schedule_redraw(struct window *window)
1197 {
1198         if (!window->redraw_scheduled) {
1199                 g_idle_add(idle_redraw, window);
1200                 window->redraw_scheduled = 1;
1201         }
1202 }
1203
1204 void
1205 window_set_fullscreen(struct window *window, int fullscreen)
1206 {
1207         int32_t width, height;
1208
1209         if (window->fullscreen == fullscreen)
1210                 return;
1211
1212         window->fullscreen = fullscreen;
1213         if (window->fullscreen) {
1214                 window->saved_allocation = window->allocation;
1215                 width = window->display->screen_allocation.width;
1216                 height = window->display->screen_allocation.height;
1217         } else {
1218                 width = window->saved_allocation.width - 20 - window->margin * 2;
1219                 height = window->saved_allocation.height - 60 - window->margin * 2;
1220         }
1221
1222         (*window->resize_handler)(window, width, height, window->user_data);
1223 }
1224
1225 void
1226 window_set_decoration(struct window *window, int decoration)
1227 {
1228         window->decoration = decoration;
1229 }
1230
1231 void
1232 window_set_user_data(struct window *window, void *data)
1233 {
1234         window->user_data = data;
1235 }
1236
1237 void *
1238 window_get_user_data(struct window *window)
1239 {
1240         return window->user_data;
1241 }
1242
1243 void
1244 window_set_resize_handler(struct window *window,
1245                           window_resize_handler_t handler)
1246 {
1247         window->resize_handler = handler;
1248 }
1249
1250 void
1251 window_set_redraw_handler(struct window *window,
1252                           window_redraw_handler_t handler)
1253 {
1254         window->redraw_handler = handler;
1255 }
1256
1257 void
1258 window_set_key_handler(struct window *window,
1259                        window_key_handler_t handler)
1260 {
1261         window->key_handler = handler;
1262 }
1263
1264 void
1265 window_set_button_handler(struct window *window,
1266                           window_button_handler_t handler)
1267 {
1268         window->button_handler = handler;
1269 }
1270
1271 void
1272 window_set_motion_handler(struct window *window,
1273                           window_motion_handler_t handler)
1274 {
1275         window->motion_handler = handler;
1276 }
1277
1278 void
1279 window_set_keyboard_focus_handler(struct window *window,
1280                                   window_keyboard_focus_handler_t handler)
1281 {
1282         window->keyboard_focus_handler = handler;
1283 }
1284
1285 void
1286 window_set_title(struct window *window, const char *title)
1287 {
1288         free(window->title);
1289         window->title = strdup(title);
1290 }
1291
1292 const char *
1293 window_get_title(struct window *window)
1294 {
1295         return window->title;
1296 }
1297
1298 void
1299 window_damage(struct window *window, int32_t x, int32_t y,
1300               int32_t width, int32_t height)
1301 {
1302         wl_surface_damage(window->surface, x, y, width, height);
1303 }
1304
1305 static struct window *
1306 window_create_internal(struct display *display, struct window *parent,
1307                         int32_t width, int32_t height)
1308 {
1309         struct window *window;
1310
1311         window = malloc(sizeof *window);
1312         if (window == NULL)
1313                 return NULL;
1314
1315         memset(window, 0, sizeof *window);
1316         window->display = display;
1317         window->parent = parent;
1318         window->surface = wl_compositor_create_surface(display->compositor);
1319         window->allocation.x = 0;
1320         window->allocation.y = 0;
1321         window->allocation.width = width;
1322         window->allocation.height = height;
1323         window->saved_allocation = window->allocation;
1324         window->margin = 16;
1325         window->decoration = 1;
1326
1327         if (display->dpy)
1328                 window->buffer_type = WINDOW_BUFFER_TYPE_EGL_IMAGE;
1329         else
1330                 window->buffer_type = WINDOW_BUFFER_TYPE_SHM;
1331
1332         wl_surface_set_user_data(window->surface, window);
1333         wl_list_insert(display->window_list.prev, &window->link);
1334
1335         return window;
1336 }
1337
1338 struct window *
1339 window_create(struct display *display, int32_t width, int32_t height)
1340 {
1341         struct window *window;
1342
1343         window = window_create_internal(display, NULL, width, height);
1344         if (!window)
1345                 return NULL;
1346
1347         return window;
1348 }
1349
1350 struct window *
1351 window_create_transient(struct display *display, struct window *parent,
1352                         int32_t x, int32_t y, int32_t width, int32_t height)
1353 {
1354         struct window *window;
1355
1356         window = window_create_internal(parent->display,
1357                                         parent, width, height);
1358         if (!window)
1359                 return NULL;
1360
1361         window->x = x;
1362         window->y = y;
1363
1364         return window;
1365 }
1366
1367 void
1368 window_set_buffer_type(struct window *window, enum window_buffer_type type)
1369 {
1370         window->buffer_type = type;
1371 }
1372
1373 static void
1374 display_handle_geometry(void *data,
1375                         struct wl_output *output,
1376                         int32_t x, int32_t y, int32_t width, int32_t height)
1377 {
1378         struct display *display = data;
1379
1380         display->screen_allocation.x = x;
1381         display->screen_allocation.y = y;
1382         display->screen_allocation.width = width;
1383         display->screen_allocation.height = height;
1384 }
1385
1386 static const struct wl_output_listener output_listener = {
1387         display_handle_geometry,
1388 };
1389
1390 static void
1391 display_add_input(struct display *d, uint32_t id)
1392 {
1393         struct input *input;
1394
1395         input = malloc(sizeof *input);
1396         if (input == NULL)
1397                 return;
1398
1399         memset(input, 0, sizeof *input);
1400         input->display = d;
1401         input->input_device = wl_input_device_create(d->display, id);
1402         input->pointer_focus = NULL;
1403         input->keyboard_focus = NULL;
1404         wl_list_insert(d->input_list.prev, &input->link);
1405
1406         wl_input_device_add_listener(input->input_device,
1407                                      &input_device_listener, input);
1408         wl_input_device_set_user_data(input->input_device, input);
1409 }
1410
1411 struct selection_offer {
1412         struct display *display;
1413         struct wl_selection_offer *offer;
1414         struct wl_array types;
1415         struct input *input;
1416 };
1417
1418 int
1419 input_offers_mime_type(struct input *input, const char *type)
1420 {
1421         struct selection_offer *offer = input->offer;
1422         char **p, **end;
1423
1424         if (offer == NULL)
1425                 return 0;
1426
1427         end = offer->types.data + offer->types.size;
1428         for (p = offer->types.data; p < end; p++)
1429                 if (strcmp(*p, type) == 0)
1430                         return 1;
1431
1432         return 0;
1433 }
1434
1435 void
1436 input_receive_mime_type(struct input *input, const char *type, int fd)
1437 {
1438         struct selection_offer *offer = input->offer;
1439
1440         /* FIXME: A number of things can go wrong here: the object may
1441          * not be the current selection offer any more (which could
1442          * still work, but the source may have gone away or just
1443          * destroyed its wl_selection) or the offer may not have the
1444          * requested type after all (programmer/client error,
1445          * typically) */
1446         wl_selection_offer_receive(offer->offer, type, fd);
1447 }
1448
1449 static void
1450 selection_offer_offer(void *data,
1451                       struct wl_selection_offer *selection_offer,
1452                       const char *type)
1453 {
1454         struct selection_offer *offer = data;
1455
1456         char **p;
1457
1458         p = wl_array_add(&offer->types, sizeof *p);
1459         if (p)
1460                 *p = strdup(type);
1461 };
1462
1463 static void
1464 selection_offer_keyboard_focus(void *data,
1465                                struct wl_selection_offer *selection_offer,
1466                                struct wl_input_device *input_device)
1467 {
1468         struct selection_offer *offer = data;
1469         struct input *input;
1470         char **p, **end;
1471
1472         if (input_device == NULL) {
1473                 printf("selection offer retracted %p\n", selection_offer);
1474                 input = offer->input;
1475                 input->offer = NULL;
1476                 wl_selection_offer_destroy(selection_offer);
1477                 wl_array_release(&offer->types);
1478                 free(offer);
1479                 return;
1480         }
1481
1482         input = wl_input_device_get_user_data(input_device);
1483         printf("new selection offer %p:", selection_offer);
1484
1485         offer->input = input;
1486         input->offer = offer;
1487         end = offer->types.data + offer->types.size;
1488         for (p = offer->types.data; p < end; p++)
1489                 printf(" %s", *p);
1490
1491         printf("\n");
1492 }
1493
1494 struct wl_selection_offer_listener selection_offer_listener = {
1495         selection_offer_offer,
1496         selection_offer_keyboard_focus
1497 };
1498
1499 static void
1500 add_selection_offer(struct display *d, uint32_t id)
1501 {
1502         struct selection_offer *offer;
1503
1504         offer = malloc(sizeof *offer);
1505         if (offer == NULL)
1506                 return;
1507
1508         offer->offer = wl_selection_offer_create(d->display, id);
1509         offer->display = d;
1510         wl_array_init(&offer->types);
1511         offer->input = NULL;
1512
1513         wl_selection_offer_add_listener(offer->offer,
1514                                         &selection_offer_listener, offer);
1515 }
1516
1517 static void
1518 display_handle_global(struct wl_display *display, uint32_t id,
1519                       const char *interface, uint32_t version, void *data)
1520 {
1521         struct display *d = data;
1522
1523         if (strcmp(interface, "compositor") == 0) {
1524                 d->compositor = wl_compositor_create(display, id);
1525         } else if (strcmp(interface, "output") == 0) {
1526                 d->output = wl_output_create(display, id);
1527                 wl_output_add_listener(d->output, &output_listener, d);
1528         } else if (strcmp(interface, "input_device") == 0) {
1529                 display_add_input(d, id);
1530         } else if (strcmp(interface, "shell") == 0) {
1531                 d->shell = wl_shell_create(display, id);
1532                 wl_shell_add_listener(d->shell, &shell_listener, d);
1533         } else if (strcmp(interface, "shm") == 0) {
1534                 d->shm = wl_shm_create(display, id);
1535         } else if (strcmp(interface, "selection_offer") == 0) {
1536                 add_selection_offer(d, id);
1537         } else if (d->global_handler) {
1538                 d->global_handler(d, interface, id, version);
1539         }
1540 }
1541
1542 static void
1543 display_render_frame(struct display *d)
1544 {
1545         int radius = 8;
1546         cairo_t *cr;
1547
1548         d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1549         cr = cairo_create(d->shadow);
1550         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1551         cairo_set_source_rgba(cr, 0, 0, 0, 1);
1552         rounded_rect(cr, 16, 16, 112, 112, radius);
1553         cairo_fill(cr);
1554         cairo_destroy(cr);
1555         blur_surface(d->shadow, 64);
1556
1557         d->active_frame =
1558                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1559         cr = cairo_create(d->active_frame);
1560         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1561         cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1562         rounded_rect(cr, 16, 16, 112, 112, radius);
1563         cairo_fill(cr);
1564         cairo_destroy(cr);
1565
1566         d->inactive_frame =
1567                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1568         cr = cairo_create(d->inactive_frame);
1569         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1570         cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1571         rounded_rect(cr, 16, 16, 112, 112, radius);
1572         cairo_fill(cr);
1573         cairo_destroy(cr);
1574 }
1575
1576 static void
1577 init_xkb(struct display *d)
1578 {
1579         struct xkb_rule_names names;
1580
1581         names.rules = "evdev";
1582         names.model = "pc105";
1583         names.layout = option_xkb_layout;
1584         names.variant = option_xkb_variant;
1585         names.options = option_xkb_options;
1586
1587         d->xkb = xkb_compile_keymap_from_rules(&names);
1588         if (!d->xkb) {
1589                 fprintf(stderr, "Failed to compile keymap\n");
1590                 exit(1);
1591         }
1592 }
1593
1594 static int
1595 init_egl(struct display *d)
1596 {
1597         EGLint major, minor;
1598
1599         d->dpy = eglGetDisplay(d->native_dpy);
1600         if (!eglInitialize(d->dpy, &major, &minor)) {
1601                 fprintf(stderr, "failed to initialize display\n");
1602                 return -1;
1603         }
1604
1605         if (!eglBindAPI(EGL_OPENGL_API)) {
1606                 fprintf(stderr, "failed to bind api EGL_OPENGL_API\n");
1607                 return -1;
1608         }
1609
1610         d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1611         if (d->ctx == NULL) {
1612                 fprintf(stderr, "failed to create context\n");
1613                 return -1;
1614         }
1615
1616         if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1617                 fprintf(stderr, "faile to make context current\n");
1618                 return -1;
1619         }
1620
1621 #ifdef HAVE_CAIRO_EGL
1622         d->device = cairo_egl_device_create(d->dpy, d->ctx);
1623         if (d->device == NULL) {
1624                 fprintf(stderr, "failed to get cairo egl device\n");
1625                 return -1;
1626         }
1627 #endif
1628
1629         return 0;
1630 }
1631
1632 struct display *
1633 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1634 {
1635         struct display *d;
1636         GOptionContext *context;
1637         GOptionGroup *xkb_option_group;
1638         GError *error;
1639
1640         g_type_init();
1641
1642         context = g_option_context_new(NULL);
1643         if (option_entries)
1644                 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1645
1646         xkb_option_group = g_option_group_new("xkb",
1647                                               "Keyboard options",
1648                                               "Show all XKB options",
1649                                               NULL, NULL);
1650         g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1651         g_option_context_add_group (context, xkb_option_group);
1652
1653         if (!g_option_context_parse(context, argc, argv, &error)) {
1654                 fprintf(stderr, "option parsing failed: %s\n", error->message);
1655                 exit(EXIT_FAILURE);
1656         }
1657
1658         g_option_context_free(context);
1659
1660         d = malloc(sizeof *d);
1661         if (d == NULL)
1662                 return NULL;
1663
1664         memset(d, 0, sizeof *d);
1665
1666         d->display = wl_display_connect(NULL);
1667         if (d->display == NULL) {
1668                 fprintf(stderr, "failed to create display: %m\n");
1669                 return NULL;
1670         }
1671
1672         wl_list_init(&d->input_list);
1673
1674         /* Set up listener so we'll catch all events. */
1675         wl_display_add_global_listener(d->display,
1676                                        display_handle_global, d);
1677
1678         d->native_dpy = wl_egl_display_create(d->display);
1679
1680         /* Process connection events. */
1681         wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1682
1683         if (init_egl(d) < 0)
1684                 return NULL;
1685
1686         d->image_target_texture_2d =
1687                 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
1688         d->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
1689         d->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
1690
1691         create_pointer_surfaces(d);
1692
1693         display_render_frame(d);
1694
1695         d->loop = g_main_loop_new(NULL, FALSE);
1696         d->source = wl_glib_source_new(d->display);
1697         g_source_attach(d->source, NULL);
1698
1699         wl_list_init(&d->window_list);
1700
1701         init_xkb(d);
1702
1703         return d;
1704 }
1705
1706 struct wl_display *
1707 display_get_display(struct display *display)
1708 {
1709         return display->display;
1710 }
1711
1712 struct wl_compositor *
1713 display_get_compositor(struct display *display)
1714 {
1715         return display->compositor;
1716 }
1717
1718 EGLDisplay
1719 display_get_egl_display(struct display *d)
1720 {
1721         return d->dpy;
1722 }
1723
1724 struct wl_shell *
1725 display_get_shell(struct display *display)
1726 {
1727         return display->shell;
1728 }
1729
1730 void
1731 display_run(struct display *d)
1732 {
1733         g_main_loop_run(d->loop);
1734 }
1735
1736 void
1737 display_set_global_handler(struct display *display,
1738                            display_global_handler_t handler)
1739 {
1740         display->global_handler = handler;
1741 }