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