Split GRAB_MOTION and GRAB_MOVE handling
[profile/ivi/wayland.git] / compositor-x11.c
1 /*
2  * Copyright © 2008-2010 Kristian Høgsberg
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <sys/time.h>
27 #include <linux/input.h>
28
29 #include <xcb/xcb.h>
30 #include <xcb/dri2.h>
31 #include <xcb/xfixes.h>
32
33 #define GL_GLEXT_PROTOTYPES
34 #define EGL_EGLEXT_PROTOTYPES
35 #include <GLES2/gl2.h>
36 #include <GLES2/gl2ext.h>
37 #include <EGL/egl.h>
38 #include <EGL/eglext.h>
39
40 #include "compositor.h"
41
42 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
43
44 struct x11_compositor {
45         struct wlsc_compositor   base;
46
47         xcb_connection_t        *conn;
48         xcb_screen_t            *screen;
49         xcb_cursor_t             null_cursor;
50         int                      dri2_major;
51         int                      dri2_minor;
52         struct wl_event_source  *xcb_source;
53         struct {
54                 xcb_atom_t               wm_protocols;
55                 xcb_atom_t               wm_normal_hints;
56                 xcb_atom_t               wm_size_hints;
57                 xcb_atom_t               wm_delete_window;
58                 xcb_atom_t               net_wm_name;
59                 xcb_atom_t               utf8_string;
60         } atom;
61 };
62
63 struct x11_output {
64         struct wlsc_output      base;
65
66         xcb_xfixes_region_t     region;
67         xcb_window_t            window;
68         GLuint                  rbo;
69         EGLImageKHR             image;
70         xcb_rectangle_t         damage[16];
71         int                     damage_count;
72 };
73
74 struct x11_input {
75         struct wlsc_input_device base;
76 };
77
78
79 static void
80 x11_input_create(struct x11_compositor *c)
81 {
82         struct x11_input *input;
83
84         input = malloc(sizeof *input);
85         if (input == NULL)
86                 return;
87
88         memset(input, 0, sizeof *input);
89         wlsc_input_device_init(&input->base, &c->base);
90
91         c->base.input_device = &input->base;
92 }
93
94
95 static int
96 dri2_connect(struct x11_compositor *c)
97 {
98         xcb_xfixes_query_version_reply_t *xfixes_query;
99         xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
100         xcb_dri2_query_version_reply_t *dri2_query;
101         xcb_dri2_query_version_cookie_t dri2_query_cookie;
102         xcb_dri2_connect_reply_t *connect;
103         xcb_dri2_connect_cookie_t connect_cookie;
104         xcb_generic_error_t *error;
105         char path[256];
106         int fd;
107
108         xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
109         xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
110
111         xfixes_query_cookie =
112                 xcb_xfixes_query_version(c->conn,
113                                          XCB_XFIXES_MAJOR_VERSION,
114                                          XCB_XFIXES_MINOR_VERSION);
115    
116         dri2_query_cookie =
117                 xcb_dri2_query_version (c->conn,
118                                         XCB_DRI2_MAJOR_VERSION,
119                                         XCB_DRI2_MINOR_VERSION);
120
121         connect_cookie = xcb_dri2_connect_unchecked (c->conn,
122                                                      c->screen->root,
123                                                      XCB_DRI2_DRIVER_TYPE_DRI);
124    
125         xfixes_query =
126                 xcb_xfixes_query_version_reply (c->conn,
127                                                 xfixes_query_cookie, &error);
128         if (xfixes_query == NULL ||
129             error != NULL || xfixes_query->major_version < 2) {
130                 free(error);
131                 return -1;
132         }
133         free(xfixes_query);
134
135         dri2_query =
136                 xcb_dri2_query_version_reply (c->conn,
137                                               dri2_query_cookie, &error);
138         if (dri2_query == NULL || error != NULL) {
139                 fprintf(stderr, "DRI2: failed to query version");
140                 free(error);
141                 return EGL_FALSE;
142         }
143         c->dri2_major = dri2_query->major_version;
144         c->dri2_minor = dri2_query->minor_version;
145         free(dri2_query);
146
147         connect = xcb_dri2_connect_reply (c->conn,
148                                           connect_cookie, NULL);
149         if (connect == NULL ||
150             connect->driver_name_length + connect->device_name_length == 0) {
151                 fprintf(stderr, "DRI2: failed to authenticate");
152                 return -1;
153         }
154
155         snprintf(path, sizeof path, "%.*s",
156                  xcb_dri2_connect_device_name_length (connect),
157                  xcb_dri2_connect_device_name (connect));
158         free(connect);
159
160         fd = open(path, O_RDWR);
161         if (fd < 0) {
162                 fprintf(stderr,
163                         "DRI2: could not open %s (%s)", path, strerror(errno));
164                 return -1;
165         }
166
167         return wlsc_drm_init(&c->base, fd, path);
168 }
169
170 static int
171 dri2_authenticate(struct x11_compositor *c, uint32_t magic)
172 {
173         xcb_dri2_authenticate_reply_t *authenticate;
174         xcb_dri2_authenticate_cookie_t authenticate_cookie;
175
176         authenticate_cookie =
177                 xcb_dri2_authenticate_unchecked(c->conn,
178                                                 c->screen->root, magic);
179         authenticate =
180                 xcb_dri2_authenticate_reply(c->conn,
181                                             authenticate_cookie, NULL);
182         if (authenticate == NULL || !authenticate->authenticated) {
183                 fprintf(stderr, "DRI2: failed to authenticate");
184                 free(authenticate);
185                 return -1;
186         }
187
188         free(authenticate);
189
190         return 0;
191 }
192
193 static int
194 x11_compositor_init_egl(struct x11_compositor *c)
195 {
196         EGLint major, minor;
197         const char *extensions;
198         drm_magic_t magic;
199         static const EGLint context_attribs[] = {
200                 EGL_CONTEXT_CLIENT_VERSION, 2,
201                 EGL_NONE
202         };
203
204         if (dri2_connect(c) < 0)
205                 return -1;
206
207         if (drmGetMagic(c->base.drm.fd, &magic)) {
208                 fprintf(stderr, "DRI2: failed to get drm magic");
209                 return -1;
210         }
211
212         if (dri2_authenticate(c, magic) < 0)
213                 return -1;
214
215         c->base.display = eglGetDRMDisplayMESA(c->base.drm.fd);
216         if (c->base.display == NULL) {
217                 fprintf(stderr, "failed to create display\n");
218                 return -1;
219         }
220
221         if (!eglInitialize(c->base.display, &major, &minor)) {
222                 fprintf(stderr, "failed to initialize display\n");
223                 return -1;
224         }
225
226         extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
227         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
228                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
229                 return -1;
230         }
231
232         eglBindAPI(EGL_OPENGL_ES_API);
233         c->base.context = eglCreateContext(c->base.display, NULL,
234                                            EGL_NO_CONTEXT, context_attribs);
235         if (c->base.context == NULL) {
236                 fprintf(stderr, "failed to create context\n");
237                 return -1;
238         }
239
240         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
241                             EGL_NO_SURFACE, c->base.context)) {
242                 fprintf(stderr, "failed to make context current\n");
243                 return -1;
244         }
245
246         return 0;
247 }
248
249 static void
250 x11_compositor_present(struct wlsc_compositor *base)
251 {
252         struct x11_compositor *c = (struct x11_compositor *) base;
253         struct x11_output *output;
254         xcb_dri2_copy_region_cookie_t cookie;
255         struct timeval tv;
256         uint32_t msec;
257
258         glFlush();
259
260         wl_list_for_each(output, &c->base.output_list, base.link) {
261                 cookie = xcb_dri2_copy_region_unchecked(c->conn,
262                                                         output->window,
263                                                         output->region,
264                                                         XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
265                                                         XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
266                 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
267         }       
268
269         gettimeofday(&tv, NULL);
270         msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
271         wlsc_compositor_finish_frame(&c->base, msec);
272 }
273
274 static void
275 x11_output_set_wm_protocols(struct x11_output *output)
276 {
277         xcb_atom_t list[1];
278         struct x11_compositor *c =
279                 (struct x11_compositor *) output->base.compositor;
280
281         list[0] = c->atom.wm_delete_window;
282         xcb_change_property (c->conn, 
283                              XCB_PROP_MODE_REPLACE,
284                              output->window,
285                              c->atom.wm_protocols,
286                              XCB_ATOM_ATOM,
287                              32,
288                              ARRAY_SIZE(list),
289                              list);
290 }
291
292 struct wm_normal_hints {
293         uint32_t flags;
294         uint32_t pad[4];
295         int32_t min_width, min_height;
296         int32_t max_width, max_height;
297         int32_t width_inc, height_inc;
298         int32_t min_aspect_x, min_aspect_y;
299         int32_t max_aspect_x, max_aspect_y;
300         int32_t base_width, base_height;
301         int32_t win_gravity;
302 };
303
304 #define WM_NORMAL_HINTS_MIN_SIZE        16
305 #define WM_NORMAL_HINTS_MAX_SIZE        32
306
307 static int
308 x11_compositor_create_output(struct x11_compositor *c, int width, int height)
309 {
310         static const char name[] = "Wayland Compositor";
311         struct x11_output *output;
312         xcb_dri2_dri2_buffer_t *buffers;
313         xcb_dri2_get_buffers_reply_t *reply;
314         xcb_dri2_get_buffers_cookie_t cookie;
315         xcb_screen_iterator_t iter;
316         xcb_rectangle_t rectangle;
317         struct wm_normal_hints normal_hints;
318         unsigned int attachments[] =
319                 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
320         uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
321         uint32_t values[2] = { 
322                 XCB_EVENT_MASK_KEY_PRESS |
323                 XCB_EVENT_MASK_KEY_RELEASE |
324                 XCB_EVENT_MASK_BUTTON_PRESS |
325                 XCB_EVENT_MASK_BUTTON_RELEASE |
326                 XCB_EVENT_MASK_POINTER_MOTION |
327                 XCB_EVENT_MASK_EXPOSURE |
328                 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
329                 XCB_EVENT_MASK_ENTER_WINDOW |
330                 XCB_EVENT_MASK_LEAVE_WINDOW,
331                 0
332         };
333
334         EGLint attribs[] = {
335                 EGL_WIDTH,              0,
336                 EGL_HEIGHT,             0,
337                 EGL_IMAGE_STRIDE_MESA,  0,
338                 EGL_IMAGE_FORMAT_MESA,  EGL_IMAGE_FORMAT_ARGB8888_MESA,
339                 EGL_NONE
340         };
341
342         output = malloc(sizeof *output);
343         if (output == NULL)
344                 return -1;
345
346         memset(output, 0, sizeof *output);
347         wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
348
349         values[1] = c->null_cursor;
350         output->window = xcb_generate_id(c->conn);
351         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
352         xcb_create_window(c->conn,
353                           XCB_COPY_FROM_PARENT,
354                           output->window,
355                           iter.data->root,
356                           0, 0,
357                           width, height,
358                           0,
359                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
360                           iter.data->root_visual,
361                           mask, values);
362
363         /* Don't resize me. */
364         memset(&normal_hints, 0, sizeof normal_hints);
365         normal_hints.flags =
366                 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
367         normal_hints.min_width = width;
368         normal_hints.min_height = height;
369         normal_hints.max_width = width;
370         normal_hints.max_height = height;
371         xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
372                              c->atom.wm_normal_hints,
373                              c->atom.wm_size_hints, 32,
374                              sizeof normal_hints / 4,
375                              (uint8_t *) &normal_hints);
376
377         xcb_map_window(c->conn, output->window);
378
379         /* Set window name.  Don't bother with non-EWMH WMs. */
380         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
381                             c->atom.net_wm_name, c->atom.utf8_string, 8,
382                             strlen(name), name);
383
384         rectangle.x = 0;
385         rectangle.y = 0;
386         rectangle.width = width;
387         rectangle.height = height;
388         output->region = xcb_generate_id(c->conn);
389         xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
390
391         xcb_dri2_create_drawable (c->conn, output->window);
392
393         x11_output_set_wm_protocols(output);
394
395         cookie = xcb_dri2_get_buffers_unchecked (c->conn,
396                                                  output->window,
397                                                  1, 1, attachments);
398         reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
399         if (reply == NULL)
400                 return -1;
401         buffers = xcb_dri2_get_buffers_buffers (reply);
402         if (buffers == NULL)
403                 return -1;
404
405         if (reply->count != 1) {
406                 fprintf(stderr,
407                         "got wrong number of buffers (%d)\n", reply->count);
408                 return -1;
409         }
410
411         attribs[1] = reply->width;
412         attribs[3] = reply->height;
413         attribs[5] = buffers[0].pitch / 4;
414         output->image =
415                 eglCreateImageKHR(c->base.display, c->base.context,
416                                   EGL_DRM_IMAGE_MESA,
417                                   (EGLClientBuffer) buffers[0].name,
418                                   attribs);
419         free(reply);
420
421         glGenRenderbuffers(1, &output->rbo);
422         glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
423
424         glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
425                                                output->image);
426
427         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
428                                   GL_COLOR_ATTACHMENT0,
429                                   GL_RENDERBUFFER,
430                                   output->rbo);
431
432         wl_list_insert(c->base.output_list.prev, &output->base.link);
433
434         return 0;
435 }
436
437 static void
438 idle_repaint(void *data)
439 {
440         struct x11_output *output = data;
441         struct x11_compositor *c =
442                 (struct x11_compositor *) output->base.compositor;
443         xcb_xfixes_region_t region;
444         xcb_dri2_copy_region_cookie_t cookie;
445         
446         if (output->damage_count <= ARRAY_SIZE(output->damage)) {
447                 region = xcb_generate_id(c->conn);
448                 xcb_xfixes_create_region(c->conn, region,
449                                          output->damage_count, output->damage);
450         } else {
451                 region = output->region;
452         }
453
454         cookie = xcb_dri2_copy_region_unchecked(c->conn,
455                                                 output->window,
456                                                 region,
457                                                 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
458                                                 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
459
460         if (region != output->region)
461                 xcb_xfixes_destroy_region(c->conn, region);
462
463         free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
464         output->damage_count = 0;
465 }
466
467 static struct x11_output *
468 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
469 {
470         struct x11_output *output;
471
472         wl_list_for_each(output, &c->base.output_list, base.link) {
473                 if (output->window == window)
474                         return output;
475         }
476
477         return NULL;
478 }
479
480 static void
481 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
482 {
483         struct x11_compositor *c = data;
484         struct x11_output *output;
485         xcb_generic_event_t *event;
486         struct wl_event_loop *loop;
487         xcb_client_message_event_t *client_message;
488         xcb_motion_notify_event_t *motion_notify;
489         xcb_key_press_event_t *key_press;
490         xcb_button_press_event_t *button_press;
491         xcb_expose_event_t *expose;
492         xcb_rectangle_t *r;
493         xcb_atom_t atom;
494
495         loop = wl_display_get_event_loop(c->base.wl_display);
496         while (event = xcb_poll_for_event (c->conn), event != NULL) {
497                 switch (event->response_type & ~0x80) {
498
499                 case XCB_KEY_PRESS:
500                         key_press = (xcb_key_press_event_t *) event;
501                         notify_key(c->base.input_device,
502                                    key_press->time, key_press->detail - 8, 1);
503                         break;
504                 case XCB_KEY_RELEASE:
505                         key_press = (xcb_key_press_event_t *) event;
506                         notify_key(c->base.input_device,
507                                    key_press->time, key_press->detail - 8, 0);
508                         break;
509                 case XCB_BUTTON_PRESS:
510                         button_press = (xcb_button_press_event_t *) event;
511                         notify_button(c->base.input_device,
512                                       button_press->time,
513                                       button_press->detail + BTN_LEFT - 1, 1);
514                         break;
515                 case XCB_BUTTON_RELEASE:
516                         button_press = (xcb_button_press_event_t *) event;
517                         notify_button(c->base.input_device,
518                                       button_press->time,
519                                       button_press->detail + BTN_LEFT - 1, 0);
520                         break;
521
522                 case XCB_MOTION_NOTIFY:
523                         motion_notify = (xcb_motion_notify_event_t *) event;
524                         notify_motion(c->base.input_device,
525                                       motion_notify->time,
526                                       motion_notify->event_x,
527                                       motion_notify->event_y);
528                         break;
529
530                 case XCB_EXPOSE:
531                         expose = (xcb_expose_event_t *) event;
532                         output = x11_compositor_find_output(c, expose->window);
533                         if (output->damage_count == 0)
534                                 wl_event_loop_add_idle(loop,
535                                                        idle_repaint, output);
536
537                         r = &output->damage[output->damage_count++];
538                         if (output->damage_count > 16)
539                                 break;
540                         r->x = expose->x;
541                         r->y = expose->y;
542                         r->width = expose->width;
543                         r->height = expose->height;
544                         break;
545
546                 case XCB_ENTER_NOTIFY:
547                         c->base.focus = 1;
548                         wlsc_compositor_schedule_repaint(&c->base);
549                         break;
550
551                 case XCB_LEAVE_NOTIFY:
552                         c->base.focus = 0;
553                         wlsc_compositor_schedule_repaint(&c->base);
554                         break;
555
556                 case XCB_CLIENT_MESSAGE:
557                         client_message = (xcb_client_message_event_t *) event;
558                         atom = client_message->data.data32[0];
559                         if (atom == c->atom.wm_delete_window)
560                                 exit(1);
561                         break;
562                 default: 
563
564                         break;
565                 }
566
567                 free (event);
568         }
569 }
570
571 #define F(field) offsetof(struct x11_compositor, field)
572
573 static void
574 x11_compositor_get_resources(struct x11_compositor *c)
575 {
576         static const struct { const char *name; int offset; } atoms[] = {
577                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
578                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
579                 { "WM_SIZE_HINTS",      F(atom.wm_size_hints) },
580                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
581                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
582                 { "UTF8_STRING",        F(atom.utf8_string) },
583         };
584
585         xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
586         xcb_intern_atom_reply_t *reply;
587         xcb_pixmap_t pixmap;
588         xcb_gc_t gc;
589         int i;
590         uint8_t data[] = { 0, 0, 0, 0 };
591
592         for (i = 0; i < ARRAY_SIZE(atoms); i++)
593                 cookies[i] = xcb_intern_atom (c->conn, 0,
594                                               strlen(atoms[i].name),
595                                               atoms[i].name);
596
597         for (i = 0; i < ARRAY_SIZE(atoms); i++) {
598                 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
599                 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
600                 free(reply);
601         }
602
603         pixmap = xcb_generate_id(c->conn);
604         gc = xcb_generate_id(c->conn);
605         xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
606         xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
607         xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
608                       pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
609         c->null_cursor = xcb_generate_id(c->conn);
610         xcb_create_cursor (c->conn, c->null_cursor,
611                            pixmap, pixmap, 0, 0, 0,  0, 0, 0,  1, 1);
612         xcb_free_gc(c->conn, gc);
613         xcb_free_pixmap(c->conn, pixmap);
614 }
615
616 static int
617 x11_authenticate(struct wlsc_compositor *c, uint32_t id)
618 {
619         return dri2_authenticate((struct x11_compositor *) c, id);
620 }
621
622 struct wlsc_compositor *
623 x11_compositor_create(struct wl_display *display)
624 {
625         struct x11_compositor *c;
626         struct wl_event_loop *loop;
627         xcb_screen_iterator_t s;
628
629         c = malloc(sizeof *c);
630         if (c == NULL)
631                 return NULL;
632
633         memset(c, 0, sizeof *c);
634         c->conn = xcb_connect(0, 0);
635
636         if (xcb_connection_has_error(c->conn))
637                 return NULL;
638
639         s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
640         c->screen = s.data;
641
642         x11_compositor_get_resources(c);
643
644         c->base.wl_display = display;
645         x11_compositor_init_egl(c);
646
647         /* Can't init base class until we have a current egl context */
648         if (wlsc_compositor_init(&c->base, display) < 0)
649                 return NULL;
650
651         x11_compositor_create_output(c, 1024, 640);
652
653         x11_input_create(c);
654
655         loop = wl_display_get_event_loop(c->base.wl_display);
656
657         c->xcb_source =
658                 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
659                                      WL_EVENT_READABLE,
660                                      x11_compositor_handle_event, c);
661
662         c->base.authenticate = x11_authenticate;
663         c->base.present = x11_compositor_present;
664
665         return &c->base;
666 }