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