Add timestamp to pointer image attach request
[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 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <time.h>
31 #include <cairo.h>
32 #include <glib.h>
33 #include <glib-object.h>
34 #include <gdk-pixbuf/gdk-pixbuf.h>
35 #include <xf86drm.h>
36
37 #define EGL_EGLEXT_PROTOTYPES 1
38 #define GL_GLEXT_PROTOTYPES 1
39 #include <GL/gl.h>
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42 #include <cairo-gl.h>
43
44 #include <X11/extensions/XKBcommon.h>
45
46 #include <linux/input.h>
47 #include "wayland-util.h"
48 #include "wayland-client.h"
49 #include "wayland-glib.h"
50 #include "cairo-util.h"
51
52 #include "window.h"
53
54 struct display {
55         struct wl_display *display;
56         struct wl_compositor *compositor;
57         struct wl_shell *shell;
58         struct wl_drm *drm;
59         struct wl_output *output;
60         struct rectangle screen_allocation;
61         int authenticated;
62         EGLDisplay dpy;
63         EGLContext ctx;
64         cairo_device_t *device;
65         int fd;
66         GMainLoop *loop;
67         GSource *source;
68         struct wl_list window_list;
69         struct wl_list input_list;
70         char *device_name;
71         cairo_surface_t *active_frame, *inactive_frame, *shadow;
72         struct xkb_desc *xkb;
73         cairo_surface_t **pointer_surfaces;
74
75         display_drag_offer_handler_t drag_offer_handler;
76 };
77
78 struct window {
79         struct display *display;
80         struct wl_surface *surface;
81         const char *title;
82         struct rectangle allocation, saved_allocation, pending_allocation;
83         int resize_edges;
84         int redraw_scheduled;
85         int minimum_width, minimum_height;
86         int margin;
87         int fullscreen;
88         int decoration;
89         struct input *grab_device;
90         struct input *keyboard_device;
91         uint32_t name;
92
93         EGLImageKHR *image;
94         cairo_surface_t *cairo_surface, *pending_surface;
95
96         window_resize_handler_t resize_handler;
97         window_redraw_handler_t redraw_handler;
98         window_key_handler_t key_handler;
99         window_button_handler_t button_handler;
100         window_keyboard_focus_handler_t keyboard_focus_handler;
101         window_motion_handler_t motion_handler;
102
103         void *user_data;
104         struct wl_list link;
105 };
106
107 struct input {
108         struct display *display;
109         struct wl_input_device *input_device;
110         struct window *pointer_focus;
111         struct window *keyboard_focus;
112         uint32_t current_pointer_image;
113         uint32_t modifiers;
114         int32_t x, y, sx, sy;
115         struct wl_list link;
116 };
117
118 enum {
119         POINTER_DEFAULT = 100,
120         POINTER_UNSET
121 };
122
123 const char *option_xkb_layout = "us";
124 const char *option_xkb_variant = "";
125 const char *option_xkb_options = "";
126
127 static const GOptionEntry xkb_option_entries[] = {
128         { "xkb-layout", 0, 0, G_OPTION_ARG_STRING,
129           &option_xkb_layout, "XKB Layout" },
130         { "xkb-variant", 0, 0, G_OPTION_ARG_STRING,
131           &option_xkb_variant, "XKB Variant" },
132         { "xkb-options", 0, 0, G_OPTION_ARG_STRING,
133           &option_xkb_options, "XKB Options" },
134         { NULL }
135 };
136
137 static void
138 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
139 {
140         cairo_move_to(cr, x0, y0 + radius);
141         cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
142         cairo_line_to(cr, x1 - radius, y0);
143         cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
144         cairo_line_to(cr, x1, y1 - radius);
145         cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
146         cairo_line_to(cr, x0 + radius, y1);
147         cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
148         cairo_close_path(cr);
149 }
150
151 static int
152 texture_from_png(const char *filename, int width, int height)
153 {
154         GdkPixbuf *pixbuf;
155         GError *error = NULL;
156         int stride, i;
157         unsigned char *pixels, *p, *end;
158
159         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename,
160                                                    width, height,
161                                                    FALSE, &error);
162         if (error != NULL)
163                 return -1;
164
165         if (!gdk_pixbuf_get_has_alpha(pixbuf) ||
166             gdk_pixbuf_get_n_channels(pixbuf) != 4) {
167                 gdk_pixbuf_unref(pixbuf);
168                 return -1;
169         }
170
171
172         stride = gdk_pixbuf_get_rowstride(pixbuf);
173         pixels = gdk_pixbuf_get_pixels(pixbuf);
174
175         for (i = 0; i < height; i++) {
176                 p = pixels + i * stride;
177                 end = p + width * 4;
178                 while (p < end) {
179                         unsigned int t;
180
181 #define MULT(d,c,a,t) \
182         do { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } while (0)
183
184                         MULT(p[0], p[0], p[3], t);
185                         MULT(p[1], p[1], p[3], t);
186                         MULT(p[2], p[2], p[3], t);
187                         p += 4;
188
189                 }
190         }
191
192         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
193                      width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
194
195         gdk_pixbuf_unref(pixbuf);
196
197         return 0;
198 }
199
200 static const struct {
201         const char *filename;
202         int hotspot_x, hotspot_y;
203 } pointer_images[] = {
204         { DATADIR "/wayland/bottom_left_corner.png",     6, 30 },
205         { DATADIR "/wayland/bottom_right_corner.png",   28, 28 },
206         { DATADIR "/wayland/bottom_side.png",           16, 20 },
207         { DATADIR "/wayland/grabbing.png",              20, 17 },
208         { DATADIR "/wayland/left_ptr.png",              10,  5 },
209         { DATADIR "/wayland/left_side.png",             10, 20 },
210         { DATADIR "/wayland/right_side.png",            30, 19 },
211         { DATADIR "/wayland/top_left_corner.png",        8,  8 },
212         { DATADIR "/wayland/top_right_corner.png",      26,  8 },
213         { DATADIR "/wayland/top_side.png",              18,  8 },
214         { DATADIR "/wayland/xterm.png",                 15, 15 },
215         { DATADIR "/wayland/hand1.png",                 18, 11 }
216 };
217
218 static void
219 create_pointer_surfaces(struct display *display)
220 {
221         int i, count;
222         const int width = 32, height = 32;
223         struct rectangle rect;
224
225         count = ARRAY_LENGTH(pointer_images);
226         display->pointer_surfaces =
227                 malloc(count * sizeof *display->pointer_surfaces);
228         rect.width = width;
229         rect.height = height;
230         for (i = 0; i < count; i++) {
231                 display->pointer_surfaces[i] =
232                         display_create_surface(display, &rect);
233                 texture_from_png(pointer_images[i].filename, width, height);
234         }
235
236 }
237
238 static const cairo_user_data_key_t surface_data_key;
239 struct surface_data {
240         EGLImageKHR image;
241         GLuint texture;
242         EGLDisplay dpy;
243         struct wl_buffer *buffer;
244 };
245
246 static void
247 surface_data_destroy(void *p)
248 {
249         struct surface_data *data = p;
250
251         glDeleteTextures(1, &data->texture);
252         eglDestroyImageKHR(data->dpy, data->image);
253         if (data->buffer)
254                 wl_buffer_destroy(data->buffer);
255 }
256
257 cairo_surface_t *
258 display_create_surface(struct display *display,
259                        struct rectangle *rectangle)
260 {
261         struct surface_data *data;
262         EGLDisplay dpy = display->dpy;
263         cairo_surface_t *surface;
264
265         EGLint image_attribs[] = {
266                 EGL_WIDTH,              0,
267                 EGL_HEIGHT,             0,
268                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
269                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
270                 EGL_NONE
271         };
272
273         data = malloc(sizeof *data);
274         image_attribs[1] = rectangle->width;
275         image_attribs[3] = rectangle->height;
276         data->image = eglCreateDRMImageMESA(dpy, image_attribs);
277         glGenTextures(1, &data->texture);
278         data->dpy = dpy;
279         glBindTexture(GL_TEXTURE_2D, data->texture);
280         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, data->image);
281         data->buffer = NULL;
282
283         surface = cairo_gl_surface_create_for_texture(display->device,
284                                                       CAIRO_CONTENT_COLOR_ALPHA,
285                                                       data->texture,
286                                                       rectangle->width,
287                                                       rectangle->height);
288
289         cairo_surface_set_user_data (surface, &surface_data_key,
290                                      data, surface_data_destroy);
291
292         return surface;
293 }
294
295 struct wl_buffer *
296 display_get_buffer_for_surface(struct display *display,
297                                cairo_surface_t *surface)
298 {
299         struct surface_data *data;
300         struct wl_visual *visual;
301         struct wl_buffer *buffer;
302         EGLint name, stride;
303         int width, height;
304
305         data = cairo_surface_get_user_data (surface, &surface_data_key);
306         if (data->buffer)
307                 return data->buffer;
308
309         width = cairo_gl_surface_get_width (surface);
310         height = cairo_gl_surface_get_height (surface);
311
312         eglExportDRMImageMESA(display->dpy, data->image, &name, NULL, &stride);
313
314         visual = wl_display_get_premultiplied_argb_visual(display->display);
315         buffer = wl_drm_create_buffer(display->drm,
316                                       name, width, height, stride, visual);
317         data->buffer = buffer;
318
319         return buffer;
320 }
321
322 cairo_surface_t *
323 display_get_pointer_surface(struct display *display, int pointer,
324                             int *width, int *height,
325                             int *hotspot_x, int *hotspot_y)
326 {
327         cairo_surface_t *surface;
328
329         surface = display->pointer_surfaces[pointer];
330         *width = cairo_gl_surface_get_width(surface);
331         *height = cairo_gl_surface_get_height(surface);
332         *hotspot_x = pointer_images[pointer].hotspot_x;
333         *hotspot_y = pointer_images[pointer].hotspot_y;
334
335         return cairo_surface_reference(surface);
336 }
337
338
339 static void
340 window_attach_surface(struct window *window);
341
342 static void
343 free_surface(void *data)
344 {
345         struct window *window = data;
346
347         cairo_surface_destroy(window->pending_surface);
348         window->pending_surface = NULL;
349         if (window->cairo_surface)
350                 window_attach_surface(window);
351 }
352
353 static void
354 window_attach_surface(struct window *window)
355 {
356         struct display *display = window->display;
357         struct wl_buffer *buffer;
358
359         if (window->pending_surface != NULL)
360                 return;
361
362         window->pending_surface = window->cairo_surface;
363         window->cairo_surface = NULL;
364
365         buffer = display_get_buffer_for_surface(display,
366                                                 window->pending_surface);
367         wl_surface_attach(window->surface, buffer);
368
369         wl_surface_map(window->surface,
370                        window->allocation.x,
371                        window->allocation.y,
372                        window->allocation.width,
373                        window->allocation.height);
374
375         wl_display_sync_callback(display->display, free_surface, window);
376 }
377
378 void
379 window_flush(struct window *window)
380 {
381        if (window->cairo_surface)
382                window_attach_surface(window);
383 }
384
385 static void
386 window_draw_decorations(struct window *window)
387 {
388         cairo_t *cr;
389         cairo_text_extents_t extents;
390         cairo_pattern_t *outline, *bright, *dim;
391         cairo_surface_t *frame;
392         int width, height, shadow_dx = 3, shadow_dy = 3;
393
394         window->cairo_surface =
395                 display_create_surface(window->display, &window->allocation);
396         width = window->allocation.width;
397         height = window->allocation.height;
398
399         outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
400         bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
401         dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
402
403         cr = cairo_create(window->cairo_surface);
404
405         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
406         cairo_set_source_rgba(cr, 0, 0, 0, 0);
407         cairo_paint(cr);
408
409         cairo_set_source_rgba(cr, 0, 0, 0, 0.6);
410         tile_mask(cr, window->display->shadow,
411                   shadow_dx, shadow_dy, width, height,
412                   window->margin + 10 - shadow_dx,
413                   window->margin + 10 - shadow_dy);
414
415         if (window->keyboard_device)
416                 frame = window->display->active_frame;
417         else
418                 frame = window->display->inactive_frame;
419
420         tile_source(cr, frame, 0, 0, width, height,
421                     window->margin + 10, window->margin + 50);
422
423         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
424         cairo_set_font_size(cr, 14);
425         cairo_text_extents(cr, window->title, &extents);
426         cairo_move_to(cr, (width - extents.width) / 2, 32 - extents.y_bearing);
427         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
428         cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
429         cairo_set_line_width (cr, 4);
430         if (window->keyboard_device)
431                 cairo_set_source_rgb(cr, 0, 0, 0);
432         else
433                 cairo_set_source_rgb(cr, 0.8, 0.8, 0.8);
434         cairo_show_text(cr, window->title);
435
436         cairo_destroy(cr);
437
438         cairo_device_flush (window->display->device);
439 }
440
441 void
442 display_flush_cairo_device(struct display *display)
443 {
444         cairo_device_flush (display->device);
445 }
446
447 static void
448 window_draw_fullscreen(struct window *window)
449 {
450         window->cairo_surface =
451                 display_create_surface(window->display, &window->allocation);
452 }
453
454 void
455 window_draw(struct window *window)
456 {
457         if (window->cairo_surface != NULL)
458                 cairo_surface_destroy(window->cairo_surface);
459
460         if (window->fullscreen || !window->decoration)
461                 window_draw_fullscreen(window);
462         else
463                 window_draw_decorations(window);
464 }
465
466 cairo_surface_t *
467 window_get_surface(struct window *window)
468 {
469         return window->cairo_surface;
470 }
471
472 enum window_location {
473         WINDOW_INTERIOR = 0,
474         WINDOW_RESIZING_TOP = 1,
475         WINDOW_RESIZING_BOTTOM = 2,
476         WINDOW_RESIZING_LEFT = 4,
477         WINDOW_RESIZING_TOP_LEFT = 5,
478         WINDOW_RESIZING_BOTTOM_LEFT = 6,
479         WINDOW_RESIZING_RIGHT = 8,
480         WINDOW_RESIZING_TOP_RIGHT = 9,
481         WINDOW_RESIZING_BOTTOM_RIGHT = 10,
482         WINDOW_RESIZING_MASK = 15,
483         WINDOW_EXTERIOR = 16,
484         WINDOW_TITLEBAR = 17,
485         WINDOW_CLIENT_AREA = 18,
486 };
487
488 static int
489 get_pointer_location(struct window *window, int32_t x, int32_t y)
490 {
491         int vlocation, hlocation, location;
492         const int grip_size = 8;
493
494         if (x < window->margin)
495                 hlocation = WINDOW_EXTERIOR;
496         else if (window->margin <= x && x < window->margin + grip_size)
497                 hlocation = WINDOW_RESIZING_LEFT;
498         else if (x < window->allocation.width - window->margin - grip_size)
499                 hlocation = WINDOW_INTERIOR;
500         else if (x < window->allocation.width - window->margin)
501                 hlocation = WINDOW_RESIZING_RIGHT;
502         else
503                 hlocation = WINDOW_EXTERIOR;
504
505         if (y < window->margin)
506                 vlocation = WINDOW_EXTERIOR;
507         else if (window->margin <= y && y < window->margin + grip_size)
508                 vlocation = WINDOW_RESIZING_TOP;
509         else if (y < window->allocation.height - window->margin - grip_size)
510                 vlocation = WINDOW_INTERIOR;
511         else if (y < window->allocation.height - window->margin)
512                 vlocation = WINDOW_RESIZING_BOTTOM;
513         else
514                 vlocation = WINDOW_EXTERIOR;
515
516         location = vlocation | hlocation;
517         if (location & WINDOW_EXTERIOR)
518                 location = WINDOW_EXTERIOR;
519         if (location == WINDOW_INTERIOR && y < window->margin + 50)
520                 location = WINDOW_TITLEBAR;
521         else if (location == WINDOW_INTERIOR)
522                 location = WINDOW_CLIENT_AREA;
523
524         return location;
525 }
526
527 static void
528 set_pointer_image(struct input *input, uint32_t time, int pointer)
529 {
530         struct display *display = input->display;
531         struct wl_buffer *buffer;
532         cairo_surface_t *surface;
533         int location;
534
535         location = get_pointer_location(input->pointer_focus,
536                                         input->sx, input->sy);
537         switch (location) {
538         case WINDOW_RESIZING_TOP:
539                 pointer = POINTER_TOP;
540                 break;
541         case WINDOW_RESIZING_BOTTOM:
542                 pointer = POINTER_BOTTOM;
543                 break;
544         case WINDOW_RESIZING_LEFT:
545                 pointer = POINTER_LEFT;
546                 break;
547         case WINDOW_RESIZING_RIGHT:
548                 pointer = POINTER_RIGHT;
549                 break;
550         case WINDOW_RESIZING_TOP_LEFT:
551                 pointer = POINTER_TOP_LEFT;
552                 break;
553         case WINDOW_RESIZING_TOP_RIGHT:
554                 pointer = POINTER_TOP_RIGHT;
555                 break;
556         case WINDOW_RESIZING_BOTTOM_LEFT:
557                 pointer = POINTER_BOTTOM_LEFT;
558                 break;
559         case WINDOW_RESIZING_BOTTOM_RIGHT:
560                 pointer = POINTER_BOTTOM_RIGHT;
561                 break;
562         case WINDOW_EXTERIOR:
563         case WINDOW_TITLEBAR:
564                 if (input->current_pointer_image == POINTER_DEFAULT)
565                         return;
566
567                 wl_input_device_attach(input->input_device, time, NULL, 0, 0);
568                 input->current_pointer_image = POINTER_DEFAULT;
569                 return;
570         default:
571                 break;
572         }
573
574         if (pointer == input->current_pointer_image)
575                 return;
576
577         input->current_pointer_image = pointer;
578         surface = display->pointer_surfaces[pointer];
579         buffer = display_get_buffer_for_surface(display, surface);
580         wl_input_device_attach(input->input_device, time, buffer,
581                                pointer_images[pointer].hotspot_x,
582                                pointer_images[pointer].hotspot_y);
583 }
584
585 static void
586 window_handle_motion(void *data, struct wl_input_device *input_device,
587                      uint32_t time,
588                      int32_t x, int32_t y, int32_t sx, int32_t sy)
589 {
590         struct input *input = data;
591         struct window *window = input->pointer_focus;
592         int location, pointer = POINTER_LEFT_PTR;
593
594         input->x = x;
595         input->y = y;
596         input->sx = sx;
597         input->sy = sy;
598
599         location = get_pointer_location(window, input->sx, input->sy);
600
601         if (window->motion_handler)
602                 pointer = (*window->motion_handler)(window, input, time,
603                                                     x, y, sx, sy,
604                                                     window->user_data);
605
606         set_pointer_image(input, time, pointer);
607 }
608
609 static void
610 window_handle_button(void *data,
611                      struct wl_input_device *input_device,
612                      uint32_t time, uint32_t button, uint32_t state)
613 {
614         struct input *input = data;
615         struct window *window = input->pointer_focus;
616         int location;
617
618         location = get_pointer_location(window, input->sx, input->sy);
619
620         if (button == BTN_LEFT && state == 1) {
621                 switch (location) {
622                 case WINDOW_TITLEBAR:
623                         wl_shell_move(window->display->shell,
624                                       window->surface, input_device, time);
625                         break;
626                 case WINDOW_RESIZING_TOP:
627                 case WINDOW_RESIZING_BOTTOM:
628                 case WINDOW_RESIZING_LEFT:
629                 case WINDOW_RESIZING_RIGHT:
630                 case WINDOW_RESIZING_TOP_LEFT:
631                 case WINDOW_RESIZING_TOP_RIGHT:
632                 case WINDOW_RESIZING_BOTTOM_LEFT:
633                 case WINDOW_RESIZING_BOTTOM_RIGHT:
634                         wl_shell_resize(window->display->shell,
635                                         window->surface, input_device, time,
636                                         location);
637                         break;
638                 case WINDOW_CLIENT_AREA:
639                         if (window->button_handler)
640                                 (*window->button_handler)(window,
641                                                           input, time,
642                                                           button, state,
643                                                           window->user_data);
644                         break;
645                 }
646         } else {
647                 if (window->button_handler)
648                         (*window->button_handler)(window,
649                                                   input, time,
650                                                   button, state,
651                                                   window->user_data);
652         }
653 }
654
655 static void
656 window_handle_key(void *data, struct wl_input_device *input_device,
657                   uint32_t time, uint32_t key, uint32_t state)
658 {
659         struct input *input = data;
660         struct window *window = input->keyboard_focus;
661         struct display *d = window->display;
662         uint32_t code, sym, level;
663
664         code = key + d->xkb->min_key_code;
665         if (window->keyboard_device != input)
666                 return;
667
668         level = 0;
669         if (input->modifiers & WINDOW_MODIFIER_SHIFT &&
670             XkbKeyGroupWidth(d->xkb, code, 0) > 1)
671                 level = 1;
672
673         sym = XkbKeySymEntry(d->xkb, code, level, 0);
674
675         if (state)
676                 input->modifiers |= d->xkb->map->modmap[code];
677         else
678                 input->modifiers &= ~d->xkb->map->modmap[code];
679
680         if (window->key_handler)
681                 (*window->key_handler)(window, key, sym, state,
682                                        input->modifiers, window->user_data);
683 }
684
685 static void
686 window_handle_pointer_focus(void *data,
687                             struct wl_input_device *input_device,
688                             uint32_t time, struct wl_surface *surface,
689                             int32_t x, int32_t y, int32_t sx, int32_t sy)
690 {
691         struct input *input = data;
692         struct window *window;
693         int pointer;
694
695         if (surface) {
696                 input->pointer_focus = wl_surface_get_user_data(surface);
697                 window = input->pointer_focus;
698
699                 pointer = POINTER_LEFT_PTR;
700                 if (window->motion_handler)
701                         pointer = (*window->motion_handler)(window,
702                                                             input, time,
703                                                             x, y, sx, sy,
704                                                             window->user_data);
705
706                 set_pointer_image(input, time, pointer);
707         } else {
708                 input->pointer_focus = NULL;
709                 input->current_pointer_image = POINTER_UNSET;
710         }
711 }
712
713 static void
714 window_handle_keyboard_focus(void *data,
715                              struct wl_input_device *input_device,
716                              uint32_t time,
717                              struct wl_surface *surface,
718                              struct wl_array *keys)
719 {
720         struct input *input = data;
721         struct window *window = input->keyboard_focus;
722         struct display *d = input->display;
723         uint32_t *k, *end;
724
725         window = input->keyboard_focus;
726         if (window) {
727                 window->keyboard_device = NULL;
728                 if (window->keyboard_focus_handler)
729                         (*window->keyboard_focus_handler)(window, NULL,
730                                                           window->user_data);
731         }
732
733         if (surface)
734                 input->keyboard_focus = wl_surface_get_user_data(surface);
735         else
736                 input->keyboard_focus = NULL;
737
738         end = keys->data + keys->size;
739         for (k = keys->data; k < end; k++)
740                 input->modifiers |= d->xkb->map->modmap[*k];
741
742         window = input->keyboard_focus;
743         if (window) {
744                 window->keyboard_device = input;
745                 if (window->keyboard_focus_handler)
746                         (*window->keyboard_focus_handler)(window,
747                                                           window->keyboard_device,
748                                                           window->user_data);
749         }
750 }
751
752 static const struct wl_input_device_listener input_device_listener = {
753         window_handle_motion,
754         window_handle_button,
755         window_handle_key,
756         window_handle_pointer_focus,
757         window_handle_keyboard_focus,
758 };
759
760 void
761 input_get_position(struct input *input, int32_t *x, int32_t *y)
762 {
763         *x = input->sx;
764         *y = input->sy;
765 }
766
767 struct wl_input_device *
768 input_get_input_device(struct input *input)
769 {
770         return input->input_device;
771 }
772
773 struct wl_drag *
774 window_start_drag(struct window *window, struct input *input, uint32_t time,
775                   const struct wl_drag_listener *listener, void *data)
776 {
777         struct wl_drag *drag;
778
779         cairo_device_flush (window->display->device);
780
781         drag = wl_shell_create_drag(window->display->shell);
782         wl_drag_offer(drag, "text/plain");
783         wl_drag_offer(drag, "text/html");
784         wl_drag_activate(drag, window->surface, input->input_device, time);
785         wl_drag_add_listener(drag, listener, data);
786
787         return drag;
788 }
789
790 static void
791 handle_configure(void *data, struct wl_shell *shell,
792                  uint32_t time, uint32_t edges,
793                  struct wl_surface *surface,
794                  int32_t x, int32_t y, int32_t width, int32_t height)
795 {
796         struct window *window = wl_surface_get_user_data(surface);
797
798         window->resize_edges = edges;
799         window->pending_allocation.x = x;
800         window->pending_allocation.y = y;
801         window->pending_allocation.width = width;
802         window->pending_allocation.height = height;
803
804         if (!(edges & 15))
805                 return;
806
807         if (window->resize_handler)
808                 (*window->resize_handler)(window,
809                                           window->user_data);
810         else if (window->redraw_handler)
811                 window_schedule_redraw(window);
812 }
813
814 static const struct wl_shell_listener shell_listener = {
815         handle_configure,
816 };
817
818 void
819 window_get_child_rectangle(struct window *window,
820                            struct rectangle *rectangle)
821 {
822         if (window->fullscreen && !window->decoration) {
823                 *rectangle = window->allocation;
824         } else {
825                 rectangle->x = window->margin + 10;
826                 rectangle->y = window->margin + 50;
827                 rectangle->width = window->allocation.width - 20 - window->margin * 2;
828                 rectangle->height = window->allocation.height - 60 - window->margin * 2;
829         }
830 }
831
832 void
833 window_set_child_size(struct window *window,
834                       struct rectangle *rectangle)
835 {
836         int32_t width, height;
837
838         if (!window->fullscreen) {
839                 width = rectangle->width + 20 + window->margin * 2;
840                 height = rectangle->height + 60 + window->margin * 2;
841
842                 if (window->resize_edges & WINDOW_RESIZING_LEFT)
843                         window->allocation.x +=
844                                 window->allocation.width - width;
845                 if (window->resize_edges & WINDOW_RESIZING_TOP)
846                         window->allocation.y +=
847                                 window->allocation.height - height;
848
849                 window->allocation.width = width;
850                 window->allocation.height = height;
851         }
852 }
853
854 void
855 window_copy_image(struct window *window,
856                   struct rectangle *rectangle, EGLImageKHR image)
857 {
858         /* set image as read buffer, copy pixels or something... */
859 }
860
861 void
862 window_copy_surface(struct window *window,
863                     struct rectangle *rectangle,
864                     cairo_surface_t *surface)
865 {
866         cairo_t *cr;
867
868         cr = cairo_create (window->cairo_surface);
869
870         cairo_set_source_surface (cr,
871                                   surface,
872                                   rectangle->x, rectangle->y);
873
874         cairo_paint (cr);
875         cairo_destroy (cr);
876 }
877
878 static gboolean
879 idle_redraw(void *data)
880 {
881         struct window *window = data;
882
883         if (window->resize_edges)
884                 window->allocation = window->pending_allocation;
885
886         window->redraw_handler(window, window->user_data);
887
888         window->redraw_scheduled = 0;
889         window->resize_edges = 0;
890
891         return FALSE;
892 }
893
894 void
895 window_schedule_redraw(struct window *window)
896 {
897         if (!window->redraw_scheduled) {
898                 g_idle_add(idle_redraw, window);
899                 window->redraw_scheduled = 1;
900         }
901 }
902
903 void
904 window_set_fullscreen(struct window *window, int fullscreen)
905 {
906         window->fullscreen = fullscreen;
907         if (window->fullscreen) {
908                 window->saved_allocation = window->allocation;
909                 window->allocation = window->display->screen_allocation;
910         } else {
911                 window->allocation = window->saved_allocation;
912         }
913 }
914
915 void
916 window_set_decoration(struct window *window, int decoration)
917 {
918         window->decoration = decoration;
919 }
920
921 void
922 window_set_user_data(struct window *window, void *data)
923 {
924         window->user_data = data;
925 }
926
927 void *
928 window_get_user_data(struct window *window)
929 {
930         return window->user_data;
931 }
932
933 void
934 window_set_resize_handler(struct window *window,
935                           window_resize_handler_t handler)
936 {
937         window->resize_handler = handler;
938 }
939
940 void
941 window_set_redraw_handler(struct window *window,
942                           window_redraw_handler_t handler)
943 {
944         window->redraw_handler = handler;
945 }
946
947 void
948 window_set_key_handler(struct window *window,
949                        window_key_handler_t handler)
950 {
951         window->key_handler = handler;
952 }
953
954 void
955 window_set_button_handler(struct window *window,
956                           window_button_handler_t handler)
957 {
958         window->button_handler = handler;
959 }
960
961 void
962 window_set_motion_handler(struct window *window,
963                           window_motion_handler_t handler)
964 {
965         window->motion_handler = handler;
966 }
967
968 void
969 window_set_keyboard_focus_handler(struct window *window,
970                                   window_keyboard_focus_handler_t handler)
971 {
972         window->keyboard_focus_handler = handler;
973 }
974
975 void
976 window_move(struct window *window, int32_t x, int32_t y)
977 {
978         window->allocation.x = x;
979         window->allocation.y = y;
980
981         wl_surface_map(window->surface,
982                        window->allocation.x - window->margin,
983                        window->allocation.y - window->margin,
984                        window->allocation.width,
985                        window->allocation.height);
986 }
987
988 struct window *
989 window_create(struct display *display, const char *title,
990               int32_t x, int32_t y, int32_t width, int32_t height)
991 {
992         struct window *window;
993
994         window = malloc(sizeof *window);
995         if (window == NULL)
996                 return NULL;
997
998         memset(window, 0, sizeof *window);
999         window->display = display;
1000         window->title = strdup(title);
1001         window->surface = wl_compositor_create_surface(display->compositor);
1002         window->allocation.x = x;
1003         window->allocation.y = y;
1004         window->allocation.width = width;
1005         window->allocation.height = height;
1006         window->saved_allocation = window->allocation;
1007         window->margin = 16;
1008         window->decoration = 1;
1009
1010         wl_surface_set_user_data(window->surface, window);
1011         wl_list_insert(display->window_list.prev, &window->link);
1012
1013         return window;
1014 }
1015
1016 static void
1017 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1018 {
1019         struct display *d = data;
1020
1021         d->device_name = strdup(device);
1022 }
1023
1024 static void drm_handle_authenticated(void *data, struct wl_drm *drm)
1025 {
1026         struct display *d = data;
1027
1028         d->authenticated = 1;
1029 }
1030
1031 static const struct wl_drm_listener drm_listener = {
1032         drm_handle_device,
1033         drm_handle_authenticated
1034 };
1035
1036 static void
1037 display_handle_geometry(void *data,
1038                         struct wl_output *output,
1039                         int32_t width, int32_t height)
1040 {
1041         struct display *display = data;
1042
1043         display->screen_allocation.x = 0;
1044         display->screen_allocation.y = 0;
1045         display->screen_allocation.width = width;
1046         display->screen_allocation.height = height;
1047 }
1048
1049 static const struct wl_output_listener output_listener = {
1050         display_handle_geometry,
1051 };
1052
1053 static void
1054 display_add_input(struct display *d, uint32_t id)
1055 {
1056         struct input *input;
1057
1058         input = malloc(sizeof *input);
1059         if (input == NULL)
1060                 return;
1061
1062         memset(input, 0, sizeof *input);
1063         input->display = d;
1064         input->input_device = wl_input_device_create(d->display, id);
1065         input->pointer_focus = NULL;
1066         input->keyboard_focus = NULL;
1067         wl_list_insert(d->input_list.prev, &input->link);
1068
1069         wl_input_device_add_listener(input->input_device,
1070                                      &input_device_listener, input);
1071         wl_input_device_set_user_data(input->input_device, input);
1072 }
1073
1074 static void
1075 display_handle_global(struct wl_display *display, uint32_t id,
1076                       const char *interface, uint32_t version, void *data)
1077 {
1078         struct display *d = data;
1079         struct wl_drag_offer *offer;
1080
1081         if (strcmp(interface, "compositor") == 0) {
1082                 d->compositor = wl_compositor_create(display, id);
1083         } else if (strcmp(interface, "output") == 0) {
1084                 d->output = wl_output_create(display, id);
1085                 wl_output_add_listener(d->output, &output_listener, d);
1086         } else if (strcmp(interface, "input_device") == 0) {
1087                 display_add_input(d, id);
1088         } else if (strcmp(interface, "shell") == 0) {
1089                 d->shell = wl_shell_create(display, id);
1090                 wl_shell_add_listener(d->shell, &shell_listener, d);
1091         } else if (strcmp(interface, "drm") == 0) {
1092                 d->drm = wl_drm_create(display, id);
1093                 wl_drm_add_listener(d->drm, &drm_listener, d);
1094         } else if (strcmp(interface, "drag_offer") == 0) {
1095                 offer = wl_drag_offer_create(display, id);
1096                 d->drag_offer_handler(offer, d);
1097         }
1098 }
1099
1100 static const char socket_name[] = "\0wayland";
1101
1102 static void
1103 display_render_frame(struct display *d)
1104 {
1105         int radius = 8;
1106         cairo_t *cr;
1107
1108         d->shadow = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1109         cr = cairo_create(d->shadow);
1110         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1111         cairo_set_source_rgba(cr, 0, 0, 0, 1);
1112         rounded_rect(cr, 16, 16, 112, 112, radius);
1113         cairo_fill(cr);
1114         cairo_destroy(cr);
1115         blur_surface(d->shadow, 64);
1116
1117         d->active_frame =
1118                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1119         cr = cairo_create(d->active_frame);
1120         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1121         cairo_set_source_rgba(cr, 0.8, 0.8, 0.4, 1);
1122         rounded_rect(cr, 16, 16, 112, 112, radius);
1123         cairo_fill(cr);
1124         cairo_destroy(cr);
1125
1126         d->inactive_frame =
1127                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 128, 128);
1128         cr = cairo_create(d->inactive_frame);
1129         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1130         cairo_set_source_rgba(cr, 0.6, 0.6, 0.6, 1);
1131         rounded_rect(cr, 16, 16, 112, 112, radius);
1132         cairo_fill(cr);
1133         cairo_destroy(cr);
1134 }
1135
1136 static void
1137 init_xkb(struct display *d)
1138 {
1139         struct xkb_rule_names names;
1140
1141         names.rules = "evdev";
1142         names.model = "pc105";
1143         names.layout = option_xkb_layout;
1144         names.variant = option_xkb_variant;
1145         names.options = option_xkb_options;
1146
1147         d->xkb = xkb_compile_keymap_from_rules(&names);
1148         if (!d->xkb) {
1149                 fprintf(stderr, "Failed to compile keymap\n");
1150                 exit(1);
1151         }
1152 }
1153
1154 struct display *
1155 display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
1156 {
1157         struct display *d;
1158         EGLint major, minor;
1159         int fd;
1160         GOptionContext *context;
1161         GOptionGroup *xkb_option_group;
1162         GError *error;
1163         drm_magic_t magic;
1164
1165         g_type_init();
1166
1167         context = g_option_context_new(NULL);
1168         if (option_entries)
1169                 g_option_context_add_main_entries(context, option_entries, "Wayland View");
1170
1171         xkb_option_group = g_option_group_new("xkb",
1172                                               "Keyboard options",
1173                                               "Show all XKB options",
1174                                               NULL, NULL);
1175         g_option_group_add_entries(xkb_option_group, xkb_option_entries);
1176         g_option_context_add_group (context, xkb_option_group);
1177
1178         if (!g_option_context_parse(context, argc, argv, &error)) {
1179                 fprintf(stderr, "option parsing failed: %s\n", error->message);
1180                 exit(EXIT_FAILURE);
1181         }
1182
1183
1184         d = malloc(sizeof *d);
1185         if (d == NULL)
1186                 return NULL;
1187
1188         d->display = wl_display_create(socket_name, sizeof socket_name);
1189         if (d->display == NULL) {
1190                 fprintf(stderr, "failed to create display: %m\n");
1191                 return NULL;
1192         }
1193
1194         wl_list_init(&d->input_list);
1195
1196         /* Set up listener so we'll catch all events. */
1197         wl_display_add_global_listener(d->display,
1198                                        display_handle_global, d);
1199
1200         /* Process connection events. */
1201         wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1202
1203         fd = open(d->device_name, O_RDWR);
1204         if (fd < 0) {
1205                 fprintf(stderr, "drm open failed: %m\n");
1206                 return NULL;
1207         }
1208
1209         if (drmGetMagic(fd, &magic)) {
1210                 fprintf(stderr, "DRI2: failed to get drm magic");
1211                 return NULL;
1212         }
1213
1214         /* Wait for authenticated event */
1215         wl_drm_authenticate(d->drm, magic);
1216         wl_display_iterate(d->display, WL_DISPLAY_WRITABLE);
1217         while (!d->authenticated)
1218                 wl_display_iterate(d->display, WL_DISPLAY_READABLE);
1219
1220         d->dpy = eglGetDRMDisplayMESA(fd);
1221         if (!eglInitialize(d->dpy, &major, &minor)) {
1222                 fprintf(stderr, "failed to initialize display\n");
1223                 return NULL;
1224         }
1225
1226         eglBindAPI(EGL_OPENGL_API);
1227
1228         d->ctx = eglCreateContext(d->dpy, NULL, EGL_NO_CONTEXT, NULL);
1229         if (d->ctx == NULL) {
1230                 fprintf(stderr, "failed to create context\n");
1231                 return NULL;
1232         }
1233
1234         if (!eglMakeCurrent(d->dpy, NULL, NULL, d->ctx)) {
1235                 fprintf(stderr, "faile to make context current\n");
1236                 return NULL;
1237         }
1238
1239         d->device = cairo_egl_device_create(d->dpy, d->ctx);
1240         if (d->device == NULL) {
1241                 fprintf(stderr, "failed to get cairo drm device\n");
1242                 return NULL;
1243         }
1244
1245         create_pointer_surfaces(d);
1246
1247         display_render_frame(d);
1248
1249         d->loop = g_main_loop_new(NULL, FALSE);
1250         d->source = wl_glib_source_new(d->display);
1251         g_source_attach(d->source, NULL);
1252
1253         wl_list_init(&d->window_list);
1254
1255         init_xkb(d);
1256
1257         return d;
1258 }
1259
1260 struct wl_display *
1261 display_get_display(struct display *display)
1262 {
1263         return display->display;
1264 }
1265
1266 struct wl_compositor *
1267 display_get_compositor(struct display *display)
1268 {
1269         return display->compositor;
1270 }
1271
1272 EGLDisplay
1273 display_get_egl_display(struct display *d)
1274 {
1275         return d->dpy;
1276 }
1277
1278 void
1279 display_run(struct display *d)
1280 {
1281         g_main_loop_run(d->loop);
1282 }
1283
1284 void
1285 display_set_drag_offer_handler(struct display *display,
1286                                display_drag_offer_handler_t handler)
1287 {
1288         display->drag_offer_handler = handler;
1289 }