Merge remote-tracking branch 'bnf/fullscreen-pageflip'
[profile/ivi/weston.git] / compositor / 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 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/time.h>
31 #include <linux/input.h>
32
33 #include <xcb/xcb.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xlib-xcb.h>
36
37 #include <GLES2/gl2.h>
38 #include <EGL/egl.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         Display                 *dpy;
48         xcb_connection_t        *conn;
49         xcb_screen_t            *screen;
50         xcb_cursor_t             null_cursor;
51         struct wl_array          keys;
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               wm_class;
59                 xcb_atom_t               net_wm_name;
60                 xcb_atom_t               net_wm_icon;
61                 xcb_atom_t               string;
62                 xcb_atom_t               utf8_string;
63                 xcb_atom_t               cardinal;
64         } atom;
65 };
66
67 struct x11_output {
68         struct wlsc_output      base;
69
70         xcb_window_t            window;
71         EGLSurface              egl_surface;
72 };
73
74 struct x11_input {
75         struct wlsc_input_device base;
76 };
77
78
79 static int
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 -1;
87
88         memset(input, 0, sizeof *input);
89         wlsc_input_device_init(&input->base, &c->base);
90
91         c->base.input_device = &input->base.input_device;
92
93         return 0;
94 }
95
96 static int
97 x11_compositor_init_egl(struct x11_compositor *c)
98 {
99         EGLint major, minor;
100         EGLint n;
101         const char *extensions;
102         EGLint config_attribs[] = {
103                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
104                 EGL_RED_SIZE, 1,
105                 EGL_GREEN_SIZE, 1,
106                 EGL_BLUE_SIZE, 1,
107                 EGL_DEPTH_SIZE, 1,
108                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
109                 EGL_NONE
110         };
111         static const EGLint context_attribs[] = {
112                 EGL_CONTEXT_CLIENT_VERSION, 2,
113                 EGL_NONE
114         };
115
116         c->base.display = eglGetDisplay(c->dpy);
117         if (c->base.display == NULL) {
118                 fprintf(stderr, "failed to create display\n");
119                 return -1;
120         }
121
122         if (!eglInitialize(c->base.display, &major, &minor)) {
123                 fprintf(stderr, "failed to initialize display\n");
124                 return -1;
125         }
126
127         extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
128         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
129                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
130                 return -1;
131         }
132
133         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
134                 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
135                 return -1;
136         }
137         if (!eglChooseConfig(c->base.display, config_attribs,
138                              &c->base.config, 1, &n) || n == 0) {
139                 fprintf(stderr, "failed to choose config: %d\n", n);
140                 return -1;
141         }
142
143         c->base.context = eglCreateContext(c->base.display, c->base.config,
144                                            EGL_NO_CONTEXT, context_attribs);
145         if (c->base.context == NULL) {
146                 fprintf(stderr, "failed to create context\n");
147                 return -1;
148         }
149
150         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
151                             EGL_NO_SURFACE, c->base.context)) {
152                 fprintf(stderr, "failed to make context current\n");
153                 return -1;
154         }
155
156         return 0;
157 }
158
159 static int
160 x11_output_prepare_render(struct wlsc_output *output_base)
161 {
162         struct x11_output *output = (struct x11_output *) output_base;
163         struct wlsc_compositor *ec = output->base.compositor;
164
165         if (!eglMakeCurrent(ec->display, output->egl_surface,
166                             output->egl_surface, ec->context)) {
167                 fprintf(stderr, "failed to make current\n");
168                 return -1;
169         }
170
171         return 0;
172 }
173
174 static int
175 x11_output_present(struct wlsc_output *output_base)
176 {
177         struct x11_output *output = (struct x11_output *) output_base;
178         struct wlsc_compositor *ec = output->base.compositor;
179         struct timeval tv;
180         uint32_t msec;
181
182         if (x11_output_prepare_render(&output->base))
183                 return -1;
184
185         eglSwapBuffers(ec->display, output->egl_surface);
186
187         gettimeofday(&tv, NULL);
188         msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
189         wlsc_output_finish_frame(&output->base, msec);
190
191         return 0;
192 }
193
194 static int
195 x11_output_image_is_scanoutable(struct wlsc_output *output_base,
196                                 EGLImageKHR image)
197 {
198         return 0;
199 }
200
201 static int
202 x11_output_set_cursor(struct wlsc_output *output_base,
203                       struct wl_input_device *input)
204 {
205         return -1;
206 }
207
208
209 static void
210 x11_output_set_wm_protocols(struct x11_output *output)
211 {
212         xcb_atom_t list[1];
213         struct x11_compositor *c =
214                 (struct x11_compositor *) output->base.compositor;
215
216         list[0] = c->atom.wm_delete_window;
217         xcb_change_property (c->conn, 
218                              XCB_PROP_MODE_REPLACE,
219                              output->window,
220                              c->atom.wm_protocols,
221                              XCB_ATOM_ATOM,
222                              32,
223                              ARRAY_SIZE(list),
224                              list);
225 }
226
227 struct wm_normal_hints {
228         uint32_t flags;
229         uint32_t pad[4];
230         int32_t min_width, min_height;
231         int32_t max_width, max_height;
232         int32_t width_inc, height_inc;
233         int32_t min_aspect_x, min_aspect_y;
234         int32_t max_aspect_x, max_aspect_y;
235         int32_t base_width, base_height;
236         int32_t win_gravity;
237 };
238
239 #define WM_NORMAL_HINTS_MIN_SIZE        16
240 #define WM_NORMAL_HINTS_MAX_SIZE        32
241
242 static void
243 x11_output_set_icon(struct x11_compositor *c, struct x11_output *output,
244                     const char *filename, int width, int height)
245 {
246         uint32_t *icon, *pixels;
247
248         pixels = wlsc_load_image(filename, width, height);
249         if (!pixels)
250                 return;
251         icon = malloc(width * height * 4 + 8);
252         if (!icon) {
253                 free(pixels);
254                 return;
255         }
256
257         icon[0] = width;
258         icon[1] = height;
259         memcpy(icon + 2, pixels, width * height * 4);
260         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
261                             c->atom.net_wm_icon, c->atom.cardinal, 32,
262                             width * height + 2, icon);
263         free(icon);
264         free(pixels);
265 }
266
267 static int
268 x11_compositor_create_output(struct x11_compositor *c, int width, int height)
269 {
270         static const char name[] = "Wayland Compositor";
271         static const char class[] = "wayland-1\0Wayland Compositor";
272         struct x11_output *output;
273         xcb_screen_iterator_t iter;
274         struct wm_normal_hints normal_hints;
275         uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
276         uint32_t values[2] = { 
277                 XCB_EVENT_MASK_KEY_PRESS |
278                 XCB_EVENT_MASK_KEY_RELEASE |
279                 XCB_EVENT_MASK_BUTTON_PRESS |
280                 XCB_EVENT_MASK_BUTTON_RELEASE |
281                 XCB_EVENT_MASK_POINTER_MOTION |
282                 XCB_EVENT_MASK_EXPOSURE |
283                 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
284                 XCB_EVENT_MASK_ENTER_WINDOW |
285                 XCB_EVENT_MASK_LEAVE_WINDOW |
286                 XCB_EVENT_MASK_KEYMAP_STATE |
287                 XCB_EVENT_MASK_FOCUS_CHANGE,
288                 0
289         };
290
291         output = malloc(sizeof *output);
292         if (output == NULL)
293                 return -1;
294
295         memset(output, 0, sizeof *output);
296         wlsc_output_init(&output->base, &c->base, 0, 0, width, height,
297                          WL_OUTPUT_FLIPPED);
298
299         values[1] = c->null_cursor;
300         output->window = xcb_generate_id(c->conn);
301         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
302         xcb_create_window(c->conn,
303                           XCB_COPY_FROM_PARENT,
304                           output->window,
305                           iter.data->root,
306                           0, 0,
307                           width, height,
308                           0,
309                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
310                           iter.data->root_visual,
311                           mask, values);
312
313         /* Don't resize me. */
314         memset(&normal_hints, 0, sizeof normal_hints);
315         normal_hints.flags =
316                 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
317         normal_hints.min_width = width;
318         normal_hints.min_height = height;
319         normal_hints.max_width = width;
320         normal_hints.max_height = height;
321         xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
322                              c->atom.wm_normal_hints,
323                              c->atom.wm_size_hints, 32,
324                              sizeof normal_hints / 4,
325                              (uint8_t *) &normal_hints);
326
327         /* Set window name.  Don't bother with non-EWMH WMs. */
328         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
329                             c->atom.net_wm_name, c->atom.utf8_string, 8,
330                             strlen(name), name);
331         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
332                             c->atom.wm_class, c->atom.string, 8,
333                             sizeof class, class);
334
335         x11_output_set_icon(c, output,
336                             DATADIR "/wayland/wayland.png", 128, 128);
337
338         xcb_map_window(c->conn, output->window);
339
340         x11_output_set_wm_protocols(output);
341
342         output->egl_surface = 
343                 eglCreateWindowSurface(c->base.display, c->base.config,
344                                        output->window, NULL);
345         if (!output->egl_surface) {
346                 fprintf(stderr, "failed to create window surface\n");
347                 return -1;
348         }
349         if (!eglMakeCurrent(c->base.display, output->egl_surface,
350                             output->egl_surface, c->base.context)) {
351                 fprintf(stderr, "failed to make surface current\n");
352                 return -1;
353         }
354
355         output->base.prepare_render = x11_output_prepare_render;
356         output->base.present = x11_output_present;
357         output->base.image_is_scanoutable = x11_output_image_is_scanoutable;
358         output->base.set_hardware_cursor = x11_output_set_cursor;
359
360         wl_list_insert(c->base.output_list.prev, &output->base.link);
361
362         return 0;
363 }
364
365 static struct x11_output *
366 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
367 {
368         struct x11_output *output;
369
370         wl_list_for_each(output, &c->base.output_list, base.link) {
371                 if (output->window == window)
372                         return output;
373         }
374
375         return NULL;
376 }
377
378 static void
379 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
380 {
381         struct x11_compositor *c = data;
382         struct x11_output *output;
383         xcb_generic_event_t *event;
384         xcb_client_message_event_t *client_message;
385         xcb_motion_notify_event_t *motion_notify;
386         xcb_enter_notify_event_t *enter_notify;
387         xcb_key_press_event_t *key_press, *prev_release;
388         xcb_button_press_event_t *button_press;
389         xcb_keymap_notify_event_t *keymap_notify;
390         xcb_focus_in_event_t *focus_in;
391         xcb_atom_t atom;
392         uint32_t *k;
393         int i, set;
394
395         prev_release = NULL;
396         while (event = xcb_poll_for_event (c->conn), event != NULL) {
397                 key_press = (xcb_key_press_event_t *) event;
398                 if (prev_release &&
399                     (event->response_type & ~0x80) == XCB_KEY_PRESS &&
400                     prev_release->time == key_press->time) {
401                         free(prev_release);
402                         free(event);
403                         prev_release = NULL;
404                         continue;
405                 } else if (prev_release) {
406                         notify_key(c->base.input_device,
407                                    prev_release->time,
408                                    prev_release->detail - 8, 0);
409                         free(prev_release);
410                         prev_release = NULL;
411                 }
412
413
414                 switch (event->response_type & ~0x80) {
415
416                 case XCB_KEY_PRESS:
417                         key_press = (xcb_key_press_event_t *) event;
418                         notify_key(c->base.input_device,
419                                    key_press->time, key_press->detail - 8, 1);
420                         break;
421                 case XCB_KEY_RELEASE:
422                         prev_release = (xcb_key_press_event_t *) event;
423                         break;
424                 case XCB_BUTTON_PRESS:
425                         button_press = (xcb_button_press_event_t *) event;
426                         notify_button(c->base.input_device,
427                                       button_press->time,
428                                       button_press->detail + BTN_LEFT - 1, 1);
429                         break;
430                 case XCB_BUTTON_RELEASE:
431                         button_press = (xcb_button_press_event_t *) event;
432                         notify_button(c->base.input_device,
433                                       button_press->time,
434                                       button_press->detail + BTN_LEFT - 1, 0);
435                         break;
436
437                 case XCB_MOTION_NOTIFY:
438                         motion_notify = (xcb_motion_notify_event_t *) event;
439                         notify_motion(c->base.input_device,
440                                       motion_notify->time,
441                                       motion_notify->event_x,
442                                       motion_notify->event_y);
443                         break;
444
445                 case XCB_EXPOSE:
446                         /* FIXME: schedule output repaint */
447                         /* output = x11_compositor_find_output(c, expose->window); */
448
449                         wlsc_compositor_schedule_repaint(&c->base);
450                         break;
451
452                 case XCB_ENTER_NOTIFY:
453                         enter_notify = (xcb_enter_notify_event_t *) event;
454                         if (enter_notify->state >= Button1Mask)
455                                 break;
456                         output = x11_compositor_find_output(c, enter_notify->event);
457                         notify_pointer_focus(c->base.input_device,
458                                              enter_notify->time,
459                                              &output->base,
460                                              enter_notify->event_x,
461                                              enter_notify->event_y);
462                         break;
463
464                 case XCB_LEAVE_NOTIFY:
465                         enter_notify = (xcb_enter_notify_event_t *) event;
466                         if (enter_notify->state >= Button1Mask)
467                                 break;
468                         notify_pointer_focus(c->base.input_device,
469                                              enter_notify->time,
470                                              NULL,
471                                              enter_notify->event_x,
472                                              enter_notify->event_y);
473                         break;
474
475                 case XCB_CLIENT_MESSAGE:
476                         client_message = (xcb_client_message_event_t *) event;
477                         atom = client_message->data.data32[0];
478                         if (atom == c->atom.wm_delete_window)
479                                 wl_display_terminate(c->base.wl_display);
480                         break;
481
482                 case XCB_FOCUS_IN:
483                         focus_in = (xcb_focus_in_event_t *) event;
484                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
485                                 break;
486
487                         output = x11_compositor_find_output(c, focus_in->event);
488                         notify_keyboard_focus(c->base.input_device,
489                                               get_time(),
490                                               &output->base, &c->keys);
491                         break;
492
493                 case XCB_FOCUS_OUT:
494                         focus_in = (xcb_focus_in_event_t *) event;
495                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
496                                 break;
497                         notify_keyboard_focus(c->base.input_device,
498                                               get_time(), NULL, NULL);
499                         break;
500
501                 case XCB_KEYMAP_NOTIFY:
502                         keymap_notify = (xcb_keymap_notify_event_t *) event;
503                         c->keys.size = 0;
504                         for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
505                                 set = keymap_notify->keys[i >> 3] &
506                                         (1 << (i & 7));
507                                 if (set) {
508                                         k = wl_array_add(&c->keys, sizeof *k);
509                                         *k = i;
510                                 }
511                         }
512                         break;
513                 default:
514                         break;
515                 }
516
517                 if ((xcb_generic_event_t *) prev_release != event)
518                         free (event);
519         }
520
521         if (prev_release) {
522                 key_press = (xcb_key_press_event_t *) prev_release;
523                 notify_key(c->base.input_device,
524                            prev_release->time, prev_release->detail - 8, 0);
525                 free(prev_release);
526                 prev_release = NULL;
527         }
528 }
529
530 #define F(field) offsetof(struct x11_compositor, field)
531
532 static void
533 x11_compositor_get_resources(struct x11_compositor *c)
534 {
535         static const struct { const char *name; int offset; } atoms[] = {
536                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
537                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
538                 { "WM_SIZE_HINTS",      F(atom.wm_size_hints) },
539                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
540                 { "WM_CLASS",           F(atom.wm_class) },
541                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
542                 { "_NET_WM_ICON",       F(atom.net_wm_icon) },
543                 { "STRING",             F(atom.string) },
544                 { "UTF8_STRING",        F(atom.utf8_string) },
545                 { "CARDINAL",           F(atom.cardinal) },
546         };
547
548         xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
549         xcb_intern_atom_reply_t *reply;
550         xcb_pixmap_t pixmap;
551         xcb_gc_t gc;
552         int i;
553         uint8_t data[] = { 0, 0, 0, 0 };
554
555         for (i = 0; i < ARRAY_SIZE(atoms); i++)
556                 cookies[i] = xcb_intern_atom (c->conn, 0,
557                                               strlen(atoms[i].name),
558                                               atoms[i].name);
559
560         for (i = 0; i < ARRAY_SIZE(atoms); i++) {
561                 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
562                 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
563                 free(reply);
564         }
565
566         pixmap = xcb_generate_id(c->conn);
567         gc = xcb_generate_id(c->conn);
568         xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
569         xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
570         xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
571                       pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
572         c->null_cursor = xcb_generate_id(c->conn);
573         xcb_create_cursor (c->conn, c->null_cursor,
574                            pixmap, pixmap, 0, 0, 0,  0, 0, 0,  1, 1);
575         xcb_free_gc(c->conn, gc);
576         xcb_free_pixmap(c->conn, pixmap);
577 }
578
579 static void
580 x11_destroy(struct wlsc_compositor *ec)
581 {
582         free(ec);
583 }
584
585 struct wlsc_compositor *
586 x11_compositor_create(struct wl_display *display, int width, int height)
587 {
588         struct x11_compositor *c;
589         struct wl_event_loop *loop;
590         xcb_screen_iterator_t s;
591
592         c = malloc(sizeof *c);
593         if (c == NULL)
594                 return NULL;
595
596         memset(c, 0, sizeof *c);
597
598         c->dpy = XOpenDisplay(NULL);
599
600         if (c->dpy == NULL)
601                 return NULL;
602
603         c->conn = XGetXCBConnection(c->dpy);
604
605         if (xcb_connection_has_error(c->conn))
606                 return NULL;
607
608         s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
609         c->screen = s.data;
610         wl_array_init(&c->keys);
611
612         x11_compositor_get_resources(c);
613
614         c->base.wl_display = display;
615         if (x11_compositor_init_egl(c) < 0)
616                 return NULL;
617
618         c->base.destroy = x11_destroy;
619
620         /* Can't init base class until we have a current egl context */
621         if (wlsc_compositor_init(&c->base, display) < 0)
622                 return NULL;
623
624         if (x11_compositor_create_output(c, width, height) < 0)
625                 return NULL;
626
627         if (x11_input_create(c) < 0)
628                 return NULL;
629
630         loop = wl_display_get_event_loop(c->base.wl_display);
631
632         c->xcb_source =
633                 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
634                                      WL_EVENT_READABLE,
635                                      x11_compositor_handle_event, c);
636
637         return &c->base;
638 }