screenshooter: Grab pixel data directly before buffer swap
[profile/ivi/weston-ivi-shell.git] / src / compositor-x11.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2010-2011 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #include <linux/input.h>
37
38 #include <xcb/xcb.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xlib-xcb.h>
41
42 #include <GLES2/gl2.h>
43 #include <EGL/egl.h>
44
45 #include "compositor.h"
46 #include "../shared/config-parser.h"
47
48 struct x11_compositor {
49         struct weston_compositor         base;
50
51         EGLSurface               dummy_pbuffer;
52
53         Display                 *dpy;
54         xcb_connection_t        *conn;
55         xcb_screen_t            *screen;
56         xcb_cursor_t             null_cursor;
57         struct wl_array          keys;
58         struct wl_event_source  *xcb_source;
59         struct {
60                 xcb_atom_t               wm_protocols;
61                 xcb_atom_t               wm_normal_hints;
62                 xcb_atom_t               wm_size_hints;
63                 xcb_atom_t               wm_delete_window;
64                 xcb_atom_t               wm_class;
65                 xcb_atom_t               net_wm_name;
66                 xcb_atom_t               net_wm_icon;
67                 xcb_atom_t               net_wm_state;
68                 xcb_atom_t               net_wm_state_fullscreen;
69                 xcb_atom_t               string;
70                 xcb_atom_t               utf8_string;
71                 xcb_atom_t               cardinal;
72         } atom;
73 };
74
75 struct x11_output {
76         struct weston_output    base;
77
78         xcb_window_t            window;
79         EGLSurface              egl_surface;
80         struct weston_mode      mode;
81         struct wl_event_source *finish_frame_timer;
82 };
83
84 struct x11_input {
85         struct weston_input_device base;
86 };
87
88
89 static int
90 x11_input_create(struct x11_compositor *c)
91 {
92         struct x11_input *input;
93
94         input = malloc(sizeof *input);
95         if (input == NULL)
96                 return -1;
97
98         memset(input, 0, sizeof *input);
99         weston_input_device_init(&input->base, &c->base);
100
101         c->base.input_device = &input->base.input_device;
102
103         return 0;
104 }
105
106 static void
107 x11_input_destroy(struct x11_compositor *compositor)
108 {
109         struct x11_input *input = container_of(compositor->base.input_device,
110                                                struct x11_input,
111                                                base.input_device);
112
113         weston_input_device_release(&input->base);
114         free(input);
115 }
116
117 static int
118 x11_compositor_init_egl(struct x11_compositor *c)
119 {
120         EGLint major, minor;
121         EGLint n;
122         EGLint config_attribs[] = {
123                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
124                 EGL_RED_SIZE, 1,
125                 EGL_GREEN_SIZE, 1,
126                 EGL_BLUE_SIZE, 1,
127                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
128                 EGL_NONE
129         };
130         static const EGLint context_attribs[] = {
131                 EGL_CONTEXT_CLIENT_VERSION, 2,
132                 EGL_NONE
133         };
134
135         static const EGLint pbuffer_attribs[] = {
136                 EGL_WIDTH, 10,
137                 EGL_HEIGHT, 10,
138                 EGL_NONE
139         };
140
141         c->base.display = eglGetDisplay(c->dpy);
142         if (c->base.display == NULL) {
143                 fprintf(stderr, "failed to create display\n");
144                 return -1;
145         }
146
147         if (!eglInitialize(c->base.display, &major, &minor)) {
148                 fprintf(stderr, "failed to initialize display\n");
149                 return -1;
150         }
151
152         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
153                 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
154                 return -1;
155         }
156         if (!eglChooseConfig(c->base.display, config_attribs,
157                              &c->base.config, 1, &n) || n == 0) {
158                 fprintf(stderr, "failed to choose config: %d\n", n);
159                 return -1;
160         }
161
162         c->base.context = eglCreateContext(c->base.display, c->base.config,
163                                            EGL_NO_CONTEXT, context_attribs);
164         if (c->base.context == NULL) {
165                 fprintf(stderr, "failed to create context\n");
166                 return -1;
167         }
168
169         c->dummy_pbuffer = eglCreatePbufferSurface(c->base.display,
170                                                    c->base.config,
171                                                    pbuffer_attribs);
172         if (c->base.context == NULL) {
173                 fprintf(stderr, "failed to create dummy pbuffer\n");
174                 return -1;
175         }
176
177         if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
178                             EGL_NO_SURFACE, c->base.context)) {
179                 fprintf(stderr, "failed to make context current\n");
180                 return -1;
181         }
182
183         return 0;
184 }
185
186 static void
187 x11_compositor_fini_egl(struct x11_compositor *compositor)
188 {
189         eglMakeCurrent(compositor->base.display,
190                        EGL_NO_SURFACE, EGL_NO_SURFACE,
191                        EGL_NO_CONTEXT);
192
193         eglTerminate(compositor->base.display);
194         eglReleaseThread();
195 }
196
197 static void
198 x11_output_repaint(struct weston_output *output_base,
199                    pixman_region32_t *damage)
200 {
201         struct x11_output *output = (struct x11_output *)output_base;
202         struct x11_compositor *compositor =
203                 (struct x11_compositor *)output->base.compositor;
204         struct weston_surface *surface;
205
206         if (!eglMakeCurrent(compositor->base.display, output->egl_surface,
207                             output->egl_surface, compositor->base.context)) {
208                 fprintf(stderr, "failed to make current\n");
209                 return;
210         }
211
212         wl_list_for_each_reverse(surface, &compositor->base.surface_list, link)
213                 weston_surface_draw(surface, &output->base, damage);
214
215         weston_output_do_read_pixels(&output->base);
216
217         eglSwapBuffers(compositor->base.display, output->egl_surface);
218
219         wl_event_source_timer_update(output->finish_frame_timer, 10);
220 }
221
222 static int
223 finish_frame_handler(void *data)
224 {
225         struct x11_output *output = data;
226         uint32_t msec;
227         struct timeval tv;
228         
229         gettimeofday(&tv, NULL);
230         msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
231         weston_output_finish_frame(&output->base, msec);
232
233         return 1;
234 }
235
236 static void
237 x11_output_destroy(struct weston_output *output_base)
238 {
239         struct x11_output *output = (struct x11_output *)output_base;
240         struct x11_compositor *compositor =
241                 (struct x11_compositor *)output->base.compositor;
242
243         wl_list_remove(&output->base.link);
244         wl_event_source_remove(output->finish_frame_timer);
245
246         eglDestroySurface(compositor->base.display, output->egl_surface);
247
248         xcb_destroy_window(compositor->conn, output->window);
249
250         weston_output_destroy(&output->base);
251
252         free(output);
253 }
254
255 static void
256 x11_output_set_wm_protocols(struct x11_output *output)
257 {
258         xcb_atom_t list[1];
259         struct x11_compositor *c =
260                 (struct x11_compositor *) output->base.compositor;
261
262         list[0] = c->atom.wm_delete_window;
263         xcb_change_property (c->conn, 
264                              XCB_PROP_MODE_REPLACE,
265                              output->window,
266                              c->atom.wm_protocols,
267                              XCB_ATOM_ATOM,
268                              32,
269                              ARRAY_LENGTH(list),
270                              list);
271 }
272
273 static void
274 x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
275 {
276         xcb_client_message_event_t event;
277         struct x11_compositor *c =
278                 (struct x11_compositor *) output->base.compositor;
279         xcb_screen_iterator_t iter;
280
281 #define _NET_WM_STATE_REMOVE        0    /* remove/unset property */
282 #define _NET_WM_STATE_ADD           1    /* add/set property */
283 #define _NET_WM_STATE_TOGGLE        2    /* toggle property  */  
284
285         memset(&event, 0, sizeof event);
286         event.response_type = XCB_CLIENT_MESSAGE;
287         event.format = 32;
288         event.window = output->window;
289         event.type = c->atom.net_wm_state;
290
291         event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
292         event.data.data32[1] = state;
293         event.data.data32[2] = 0;
294         event.data.data32[3] = 0;
295         event.data.data32[4] = 0;
296
297         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
298         xcb_send_event(c->conn, 0, iter.data->root,
299                        XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
300                        XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
301                        (void *) &event);
302 }
303
304
305 struct wm_normal_hints {
306         uint32_t flags;
307         uint32_t pad[4];
308         int32_t min_width, min_height;
309         int32_t max_width, max_height;
310         int32_t width_inc, height_inc;
311         int32_t min_aspect_x, min_aspect_y;
312         int32_t max_aspect_x, max_aspect_y;
313         int32_t base_width, base_height;
314         int32_t win_gravity;
315 };
316
317 #define WM_NORMAL_HINTS_MIN_SIZE        16
318 #define WM_NORMAL_HINTS_MAX_SIZE        32
319
320 static void
321 x11_output_set_icon(struct x11_compositor *c,
322                     struct x11_output *output, const char *filename)
323 {
324         uint32_t *icon;
325         int32_t width, height;
326         pixman_image_t *image;
327
328         image = load_image(filename);
329         if (!image)
330                 return;
331         width = pixman_image_get_width(image);
332         height = pixman_image_get_height(image);
333         icon = malloc(width * height * 4 + 8);
334         if (!icon) {
335                 pixman_image_unref(image);
336                 return;
337         }
338
339         icon[0] = width;
340         icon[1] = height;
341         memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
342         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
343                             c->atom.net_wm_icon, c->atom.cardinal, 32,
344                             width * height + 2, icon);
345         free(icon);
346         pixman_image_unref(image);
347 }
348
349 static int
350 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
351                              int width, int height, int fullscreen)
352 {
353         static const char name[] = "Weston Compositor";
354         static const char class[] = "weston-1\0Weston Compositor";
355         struct x11_output *output;
356         xcb_screen_iterator_t iter;
357         struct wm_normal_hints normal_hints;
358         struct wl_event_loop *loop;
359         uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
360         uint32_t values[2] = { 
361                 XCB_EVENT_MASK_KEY_PRESS |
362                 XCB_EVENT_MASK_KEY_RELEASE |
363                 XCB_EVENT_MASK_BUTTON_PRESS |
364                 XCB_EVENT_MASK_BUTTON_RELEASE |
365                 XCB_EVENT_MASK_POINTER_MOTION |
366                 XCB_EVENT_MASK_EXPOSURE |
367                 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
368                 XCB_EVENT_MASK_ENTER_WINDOW |
369                 XCB_EVENT_MASK_LEAVE_WINDOW |
370                 XCB_EVENT_MASK_KEYMAP_STATE |
371                 XCB_EVENT_MASK_FOCUS_CHANGE,
372                 0
373         };
374
375         output = malloc(sizeof *output);
376         if (output == NULL)
377                 return -1;
378
379         memset(output, 0, sizeof *output);
380
381         output->mode.flags =
382                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
383         output->mode.width = width;
384         output->mode.height = height;
385         output->mode.refresh = 60;
386         wl_list_init(&output->base.mode_list);
387         wl_list_insert(&output->base.mode_list, &output->mode.link);
388
389         output->base.current = &output->mode;
390         weston_output_init(&output->base, &c->base, x, y, width, height,
391                          WL_OUTPUT_FLIPPED);
392
393         values[1] = c->null_cursor;
394         output->window = xcb_generate_id(c->conn);
395         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
396         xcb_create_window(c->conn,
397                           XCB_COPY_FROM_PARENT,
398                           output->window,
399                           iter.data->root,
400                           0, 0,
401                           width, height,
402                           0,
403                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
404                           iter.data->root_visual,
405                           mask, values);
406
407         /* Don't resize me. */
408         memset(&normal_hints, 0, sizeof normal_hints);
409         normal_hints.flags =
410                 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
411         normal_hints.min_width = width;
412         normal_hints.min_height = height;
413         normal_hints.max_width = width;
414         normal_hints.max_height = height;
415         xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
416                              c->atom.wm_normal_hints,
417                              c->atom.wm_size_hints, 32,
418                              sizeof normal_hints / 4,
419                              (uint8_t *) &normal_hints);
420
421         /* Set window name.  Don't bother with non-EWMH WMs. */
422         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
423                             c->atom.net_wm_name, c->atom.utf8_string, 8,
424                             strlen(name), name);
425         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
426                             c->atom.wm_class, c->atom.string, 8,
427                             sizeof class, class);
428
429         x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
430
431         xcb_map_window(c->conn, output->window);
432
433         x11_output_set_wm_protocols(output);
434
435         if (fullscreen)
436                 x11_output_change_state(output, 1,
437                                         c->atom.net_wm_state_fullscreen);
438
439         output->egl_surface = 
440                 eglCreateWindowSurface(c->base.display, c->base.config,
441                                        output->window, NULL);
442         if (!output->egl_surface) {
443                 fprintf(stderr, "failed to create window surface\n");
444                 return -1;
445         }
446         if (!eglMakeCurrent(c->base.display, output->egl_surface,
447                             output->egl_surface, c->base.context)) {
448                 fprintf(stderr, "failed to make surface current\n");
449                 return -1;
450         }
451
452         loop = wl_display_get_event_loop(c->base.wl_display);
453         output->finish_frame_timer =
454                 wl_event_loop_add_timer(loop, finish_frame_handler, output);
455
456         output->base.origin = output->base.current;
457         output->base.repaint = x11_output_repaint;
458         output->base.destroy = x11_output_destroy;
459         output->base.assign_planes = NULL;
460         output->base.set_backlight = NULL;
461         output->base.set_dpms = NULL;
462         output->base.switch_mode = NULL;
463
464         wl_list_insert(c->base.output_list.prev, &output->base.link);
465
466         return 0;
467 }
468
469 static struct x11_output *
470 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
471 {
472         struct x11_output *output;
473
474         wl_list_for_each(output, &c->base.output_list, base.link) {
475                 if (output->window == window)
476                         return output;
477         }
478
479         return NULL;
480 }
481
482 static void
483 x11_compositor_deliver_button_event(struct x11_compositor *c,
484                                     xcb_generic_event_t *event, int state)
485 {
486         xcb_button_press_event_t *button_event =
487                 (xcb_button_press_event_t *) event;
488         int button;
489
490         switch (button_event->detail) {
491         default:
492                 button = button_event->detail + BTN_LEFT - 1;
493                 break;
494         case 2:
495                 button = BTN_MIDDLE;
496                 break;
497         case 3:
498                 button = BTN_RIGHT;
499                 break;
500         case 4:
501                 if (state)
502                         notify_axis(c->base.input_device,
503                                       weston_compositor_get_time(),
504                                       WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, 1);
505                 return;
506         case 5:
507                 if (state)
508                         notify_axis(c->base.input_device,
509                                       weston_compositor_get_time(),
510                                       WL_INPUT_DEVICE_AXIS_VERTICAL_SCROLL, -1);
511                 return;
512         case 6:
513                 if (state)
514                         notify_axis(c->base.input_device,
515                                       weston_compositor_get_time(),
516                                       WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, 1);
517                 return;
518         case 7:
519                 if (state)
520                         notify_axis(c->base.input_device,
521                                       weston_compositor_get_time(),
522                                       WL_INPUT_DEVICE_AXIS_HORIZONTAL_SCROLL, -1);
523                 return;
524         }
525
526         notify_button(c->base.input_device,
527                       weston_compositor_get_time(), button, state);
528 }
529
530 static int
531 x11_compositor_next_event(struct x11_compositor *c,
532                           xcb_generic_event_t **event, uint32_t mask)
533 {
534         if (mask & WL_EVENT_READABLE) {
535                 *event = xcb_poll_for_event(c->conn);
536         } else {
537 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
538                 *event = xcb_poll_for_queued_event(c->conn);
539 #else
540                 *event = xcb_poll_for_event(c->conn);
541 #endif
542         }
543
544         return *event != NULL;
545 }
546
547 static int
548 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
549 {
550         struct x11_compositor *c = data;
551         struct x11_output *output;
552         xcb_generic_event_t *event, *prev;
553         xcb_client_message_event_t *client_message;
554         xcb_motion_notify_event_t *motion_notify;
555         xcb_enter_notify_event_t *enter_notify;
556         xcb_key_press_event_t *key_press, *key_release;
557         xcb_keymap_notify_event_t *keymap_notify;
558         xcb_focus_in_event_t *focus_in;
559         xcb_atom_t atom;
560         uint32_t *k;
561         uint32_t i, set;
562
563         prev = NULL;
564         while (x11_compositor_next_event(c, &event, mask)) {
565                 switch (prev ? prev->response_type & ~0x80 : 0x80) {
566                 case XCB_KEY_RELEASE:
567                         key_release = (xcb_key_press_event_t *) prev;
568                         key_press = (xcb_key_press_event_t *) event;
569                         if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
570                             key_release->time == key_press->time &&
571                             key_release->detail == key_press->detail) {
572                                 /* Don't deliver the held key release
573                                  * event or the new key press event. */
574                                 free(event);
575                                 free(prev);
576                                 prev = NULL;
577                                 continue;
578                         } else {
579                                 /* Deliver the held key release now
580                                  * and fall through and handle the new
581                                  * event below. */
582                                 notify_key(c->base.input_device,
583                                            weston_compositor_get_time(),
584                                            key_release->detail - 8, 0);
585                                 free(prev);
586                                 prev = NULL;
587                                 break;
588                         }
589
590                 case XCB_FOCUS_IN:
591                         /* assert event is keymap_notify */
592                         focus_in = (xcb_focus_in_event_t *) prev;
593                         keymap_notify = (xcb_keymap_notify_event_t *) event;
594                         c->keys.size = 0;
595                         for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
596                                 set = keymap_notify->keys[i >> 3] &
597                                         (1 << (i & 7));
598                                 if (set) {
599                                         k = wl_array_add(&c->keys, sizeof *k);
600                                         *k = i;
601                                 }
602                         }
603
604                         output = x11_compositor_find_output(c, focus_in->event);
605                         notify_keyboard_focus(c->base.input_device, &c->keys);
606
607                         free(prev);
608                         prev = NULL;
609                         break;
610
611                 default:
612                         /* No previous event held */
613                         break;
614                 }
615
616                 switch (event->response_type & ~0x80) {
617                 case XCB_KEY_PRESS:
618                         key_press = (xcb_key_press_event_t *) event;
619                         notify_key(c->base.input_device,
620                                    weston_compositor_get_time(),
621                                    key_press->detail - 8, 1);
622                         break;
623                 case XCB_KEY_RELEASE:
624                         prev = event;
625                         break;
626                 case XCB_BUTTON_PRESS:
627                         x11_compositor_deliver_button_event(c, event, 1);
628                         break;
629                 case XCB_BUTTON_RELEASE:
630                         x11_compositor_deliver_button_event(c, event, 0);
631                         break;
632                 case XCB_MOTION_NOTIFY:
633                         motion_notify = (xcb_motion_notify_event_t *) event;
634                         output = x11_compositor_find_output(c, motion_notify->event);
635                         notify_motion(c->base.input_device,
636                                       weston_compositor_get_time(),
637                                       output->base.x + motion_notify->event_x,
638                                       output->base.y + motion_notify->event_y);
639                         break;
640
641                 case XCB_EXPOSE:
642                         /* FIXME: schedule output repaint */
643                         /* output = x11_compositor_find_output(c, expose->window); */
644
645                         weston_compositor_schedule_repaint(&c->base);
646                         break;
647
648                 case XCB_ENTER_NOTIFY:
649                         enter_notify = (xcb_enter_notify_event_t *) event;
650                         if (enter_notify->state >= Button1Mask)
651                                 break;
652                         output = x11_compositor_find_output(c, enter_notify->event);
653                         notify_pointer_focus(c->base.input_device,
654                                              &output->base,
655                                              output->base.x + enter_notify->event_x,
656                                              output->base.y + enter_notify->event_y);
657                         break;
658
659                 case XCB_LEAVE_NOTIFY:
660                         enter_notify = (xcb_enter_notify_event_t *) event;
661                         if (enter_notify->state >= Button1Mask)
662                                 break;
663                         output = x11_compositor_find_output(c, enter_notify->event);
664                         notify_pointer_focus(c->base.input_device, NULL,
665                                              output->base.x + enter_notify->event_x,
666                                              output->base.y + enter_notify->event_y);
667                         break;
668
669                 case XCB_CLIENT_MESSAGE:
670                         client_message = (xcb_client_message_event_t *) event;
671                         atom = client_message->data.data32[0];
672                         if (atom == c->atom.wm_delete_window)
673                                 wl_display_terminate(c->base.wl_display);
674                         break;
675
676                 case XCB_FOCUS_IN:
677                         focus_in = (xcb_focus_in_event_t *) event;
678                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
679                                 break;
680
681                         prev = event;
682                         break;
683
684                 case XCB_FOCUS_OUT:
685                         focus_in = (xcb_focus_in_event_t *) event;
686                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
687                             focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
688                                 break;
689                         notify_keyboard_focus(c->base.input_device, NULL);
690                         break;
691
692                 default:
693                         break;
694                 }
695
696                 if (prev != event)
697                         free (event);
698         }
699
700         switch (prev ? prev->response_type & ~0x80 : 0x80) {
701         case XCB_KEY_RELEASE:
702                 key_release = (xcb_key_press_event_t *) prev;
703                 notify_key(c->base.input_device,
704                            weston_compositor_get_time(),
705                            key_release->detail - 8, 0);
706                 free(prev);
707                 prev = NULL;
708                 break;
709         default:
710                 break;
711         }
712
713         return event != NULL;
714 }
715
716 #define F(field) offsetof(struct x11_compositor, field)
717
718 static void
719 x11_compositor_get_resources(struct x11_compositor *c)
720 {
721         static const struct { const char *name; int offset; } atoms[] = {
722                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
723                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
724                 { "WM_SIZE_HINTS",      F(atom.wm_size_hints) },
725                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
726                 { "WM_CLASS",           F(atom.wm_class) },
727                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
728                 { "_NET_WM_ICON",       F(atom.net_wm_icon) },
729                 { "_NET_WM_STATE",      F(atom.net_wm_state) },
730                 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
731                 { "STRING",             F(atom.string) },
732                 { "UTF8_STRING",        F(atom.utf8_string) },
733                 { "CARDINAL",           F(atom.cardinal) },
734         };
735
736         xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
737         xcb_intern_atom_reply_t *reply;
738         xcb_pixmap_t pixmap;
739         xcb_gc_t gc;
740         unsigned int i;
741         uint8_t data[] = { 0, 0, 0, 0 };
742
743         for (i = 0; i < ARRAY_LENGTH(atoms); i++)
744                 cookies[i] = xcb_intern_atom (c->conn, 0,
745                                               strlen(atoms[i].name),
746                                               atoms[i].name);
747
748         for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
749                 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
750                 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
751                 free(reply);
752         }
753
754         pixmap = xcb_generate_id(c->conn);
755         gc = xcb_generate_id(c->conn);
756         xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
757         xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
758         xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
759                       pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
760         c->null_cursor = xcb_generate_id(c->conn);
761         xcb_create_cursor (c->conn, c->null_cursor,
762                            pixmap, pixmap, 0, 0, 0,  0, 0, 0,  1, 1);
763         xcb_free_gc(c->conn, gc);
764         xcb_free_pixmap(c->conn, pixmap);
765 }
766
767 static void
768 x11_destroy(struct weston_compositor *ec)
769 {
770         struct x11_compositor *compositor = (struct x11_compositor *)ec;
771
772         wl_event_source_remove(compositor->xcb_source);
773         x11_input_destroy(compositor);
774
775         weston_compositor_shutdown(ec); /* destroys outputs, too */
776
777         x11_compositor_fini_egl(compositor);
778
779         XCloseDisplay(compositor->dpy);
780         free(ec);
781 }
782
783 static struct weston_compositor *
784 x11_compositor_create(struct wl_display *display,
785                       int width, int height, int count, int fullscreen)
786 {
787         struct x11_compositor *c;
788         xcb_screen_iterator_t s;
789         int i, x;
790
791         c = malloc(sizeof *c);
792         if (c == NULL)
793                 return NULL;
794
795         memset(c, 0, sizeof *c);
796
797         c->dpy = XOpenDisplay(NULL);
798         if (c->dpy == NULL)
799                 return NULL;
800
801         c->conn = XGetXCBConnection(c->dpy);
802         XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
803
804         if (xcb_connection_has_error(c->conn))
805                 return NULL;
806
807         s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
808         c->screen = s.data;
809         wl_array_init(&c->keys);
810
811         x11_compositor_get_resources(c);
812
813         c->base.wl_display = display;
814         if (x11_compositor_init_egl(c) < 0)
815                 return NULL;
816
817         c->base.destroy = x11_destroy;
818
819         /* Can't init base class until we have a current egl context */
820         if (weston_compositor_init(&c->base, display) < 0)
821                 return NULL;
822
823         for (i = 0, x = 0; i < count; i++) {
824                 if (x11_compositor_create_output(c, x, 0, width, height,
825                                                  fullscreen) < 0)
826                         return NULL;
827                 x += width;
828         }
829
830         if (x11_input_create(c) < 0)
831                 return NULL;
832
833         c->xcb_source =
834                 wl_event_loop_add_fd(c->base.input_loop,
835                                      xcb_get_file_descriptor(c->conn),
836                                      WL_EVENT_READABLE,
837                                      x11_compositor_handle_event, c);
838         wl_event_source_check(c->xcb_source);
839
840         return &c->base;
841 }
842
843 WL_EXPORT struct weston_compositor *
844 backend_init(struct wl_display *display, int argc, char *argv[])
845 {
846         int width = 1024, height = 640, fullscreen = 0, count = 1;
847
848         const struct weston_option x11_options[] = {
849                 { WESTON_OPTION_INTEGER, "width", 0, &width },
850                 { WESTON_OPTION_INTEGER, "height", 0, &height },
851                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
852                 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
853         };
854
855         parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
856
857         return x11_compositor_create(display,
858                                      width, height, count, fullscreen);
859 }