compositor-x11: rename use-shm to use-pixman
[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  * Copyright © 2013 Vasily Khoruzhick <anarsoul@gmail.com>
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the copyright holders not be used in
11  * advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission.  The copyright holders make
13  * no representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/time.h>
37 #include <sys/shm.h>
38 #include <linux/input.h>
39
40 #include <xcb/xcb.h>
41 #include <xcb/shm.h>
42 #ifdef HAVE_XCB_XKB
43 #include <xcb/xkb.h>
44 #endif
45
46 #include <X11/Xlib.h>
47 #include <X11/Xlib-xcb.h>
48
49 #include <xkbcommon/xkbcommon.h>
50
51 #include "compositor.h"
52 #include "gl-renderer.h"
53 #include "pixman-renderer.h"
54 #include "../shared/config-parser.h"
55 #include "../shared/cairo-util.h"
56
57 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
58
59 static char *output_name;
60 static char *output_mode;
61 static char *output_transform;
62 static int option_width;
63 static int option_height;
64 static int option_count;
65 static struct wl_list configured_output_list;
66
67 struct x11_configured_output {
68         char *name;
69         int width, height;
70         uint32_t transform;
71         struct wl_list link;
72 };
73
74 struct x11_compositor {
75         struct weston_compositor         base;
76
77         Display                 *dpy;
78         xcb_connection_t        *conn;
79         xcb_screen_t            *screen;
80         xcb_cursor_t             null_cursor;
81         struct wl_array          keys;
82         struct wl_event_source  *xcb_source;
83         struct xkb_keymap       *xkb_keymap;
84         unsigned int             has_xkb;
85         uint8_t                  xkb_event_base;
86         int                      use_pixman;
87
88         /* We could map multi-pointer X to multiple wayland seats, but
89          * for now we only support core X input. */
90         struct weston_seat       core_seat;
91
92         struct {
93                 xcb_atom_t               wm_protocols;
94                 xcb_atom_t               wm_normal_hints;
95                 xcb_atom_t               wm_size_hints;
96                 xcb_atom_t               wm_delete_window;
97                 xcb_atom_t               wm_class;
98                 xcb_atom_t               net_wm_name;
99                 xcb_atom_t               net_wm_icon;
100                 xcb_atom_t               net_wm_state;
101                 xcb_atom_t               net_wm_state_fullscreen;
102                 xcb_atom_t               string;
103                 xcb_atom_t               utf8_string;
104                 xcb_atom_t               cardinal;
105                 xcb_atom_t               xkb_names;
106         } atom;
107 };
108
109 struct x11_output {
110         struct weston_output    base;
111
112         xcb_window_t            window;
113         struct weston_mode      mode;
114         struct wl_event_source *finish_frame_timer;
115
116         xcb_gc_t                gc;
117         xcb_shm_seg_t           segment;
118         pixman_image_t         *hw_surface;
119         pixman_image_t         *shadow_surface;
120         int                     shm_id;
121         void                   *buf;
122         void                   *shadow_buf;
123         uint8_t                 depth;
124 };
125
126 static struct xkb_keymap *
127 x11_compositor_get_keymap(struct x11_compositor *c)
128 {
129         xcb_get_property_cookie_t cookie;
130         xcb_get_property_reply_t *reply;
131         struct xkb_rule_names names;
132         struct xkb_keymap *ret;
133         const char *value_all, *value_part;
134         int length_all, length_part;
135
136         memset(&names, 0, sizeof(names));
137
138         cookie = xcb_get_property(c->conn, 0, c->screen->root,
139                                   c->atom.xkb_names, c->atom.string, 0, 1024);
140         reply = xcb_get_property_reply(c->conn, cookie, NULL);
141         if (reply == NULL)
142                 return NULL;
143
144         value_all = xcb_get_property_value(reply);
145         length_all = xcb_get_property_value_length(reply);
146         value_part = value_all;
147
148 #define copy_prop_value(to) \
149         length_part = strlen(value_part); \
150         if (value_part + length_part < (value_all + length_all) && \
151             length_part > 0) \
152                 names.to = value_part; \
153         value_part += length_part + 1;
154
155         copy_prop_value(rules);
156         copy_prop_value(model);
157         copy_prop_value(layout);
158         copy_prop_value(variant);
159         copy_prop_value(options);
160 #undef copy_prop_value
161
162         ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
163
164         free(reply);
165         return ret;
166 }
167
168 static uint32_t
169 get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
170 {
171         struct weston_xkb_info *info = &c->core_seat.xkb_info;
172         uint32_t ret = 0;
173
174         if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
175                 ret |= (1 << info->shift_mod);
176         if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
177                 ret |= (1 << info->caps_mod);
178         if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
179                 ret |= (1 << info->ctrl_mod);
180         if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
181                 ret |= (1 << info->alt_mod);
182         if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
183                 ret |= (1 << info->mod2_mod);
184         if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
185                 ret |= (1 << info->mod3_mod);
186         if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
187                 ret |= (1 << info->super_mod);
188         if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
189                 ret |= (1 << info->mod5_mod);
190
191         return ret;
192 }
193
194 static void
195 x11_compositor_setup_xkb(struct x11_compositor *c)
196 {
197 #ifndef HAVE_XCB_XKB
198         weston_log("XCB-XKB not available during build\n");
199         c->has_xkb = 0;
200         c->xkb_event_base = 0;
201         return;
202 #else
203         const xcb_query_extension_reply_t *ext;
204         xcb_generic_error_t *error;
205         xcb_void_cookie_t select;
206         xcb_xkb_use_extension_cookie_t use_ext;
207         xcb_xkb_use_extension_reply_t *use_ext_reply;
208         xcb_xkb_per_client_flags_cookie_t pcf;
209         xcb_xkb_per_client_flags_reply_t *pcf_reply;
210         xcb_xkb_get_state_cookie_t state;
211         xcb_xkb_get_state_reply_t *state_reply;
212
213         c->has_xkb = 0;
214         c->xkb_event_base = 0;
215
216         ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
217         if (!ext) {
218                 weston_log("XKB extension not available on host X11 server\n");
219                 return;
220         }
221         c->xkb_event_base = ext->first_event;
222
223         select = xcb_xkb_select_events_checked(c->conn,
224                                                XCB_XKB_ID_USE_CORE_KBD,
225                                                XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
226                                                0,
227                                                XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
228                                                0,
229                                                0,
230                                                NULL);
231         error = xcb_request_check(c->conn, select);
232         if (error) {
233                 weston_log("error: failed to select for XKB state events\n");
234                 free(error);
235                 return;
236         }
237
238         use_ext = xcb_xkb_use_extension(c->conn,
239                                         XCB_XKB_MAJOR_VERSION,
240                                         XCB_XKB_MINOR_VERSION);
241         use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL);
242         if (!use_ext_reply) {
243                 weston_log("couldn't start using XKB extension\n");
244                 return;
245         }
246
247         if (!use_ext_reply->supported) {
248                 weston_log("XKB extension version on the server is too old "
249                            "(want %d.%d, has %d.%d)\n",
250                            XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
251                            use_ext_reply->serverMajor, use_ext_reply->serverMinor);
252                 free(use_ext_reply);
253                 return;
254         }
255         free(use_ext_reply);
256
257         pcf = xcb_xkb_per_client_flags(c->conn,
258                                        XCB_XKB_ID_USE_CORE_KBD,
259                                        XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
260                                        XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
261                                        0,
262                                        0,
263                                        0);
264         pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL);
265         if (!pcf_reply ||
266             !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
267                 weston_log("failed to set XKB per-client flags, not using "
268                            "detectable repeat\n");
269                 free(pcf_reply);
270                 return;
271         }
272         free(pcf_reply);
273
274         state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
275         state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL);
276         if (!state_reply) {
277                 weston_log("failed to get initial XKB state\n");
278                 return;
279         }
280
281         xkb_state_update_mask(c->core_seat.xkb_state.state,
282                               get_xkb_mod_mask(c, state_reply->baseMods),
283                               get_xkb_mod_mask(c, state_reply->latchedMods),
284                               get_xkb_mod_mask(c, state_reply->lockedMods),
285                               0,
286                               0,
287                               state_reply->group);
288
289         free(state_reply);
290
291         c->has_xkb = 1;
292 #endif
293 }
294
295 static int
296 x11_input_create(struct x11_compositor *c, int no_input)
297 {
298         struct xkb_keymap *keymap;
299
300         weston_seat_init(&c->core_seat, &c->base);
301
302         if (no_input)
303                 return 0;
304
305         weston_seat_init_pointer(&c->core_seat);
306
307         keymap = x11_compositor_get_keymap(c);
308         weston_seat_init_keyboard(&c->core_seat, keymap);
309         if (keymap)
310                 xkb_map_unref(keymap);
311
312         x11_compositor_setup_xkb(c);
313
314         return 0;
315 }
316
317 static void
318 x11_input_destroy(struct x11_compositor *compositor)
319 {
320         weston_seat_release(&compositor->core_seat);
321 }
322
323 static void
324 x11_output_repaint_gl(struct weston_output *output_base,
325                       pixman_region32_t *damage)
326 {
327         struct x11_output *output = (struct x11_output *)output_base;
328         struct weston_compositor *ec = output->base.compositor;
329
330         ec->renderer->repaint_output(output_base, damage);
331
332         pixman_region32_subtract(&ec->primary_plane.damage,
333                                  &ec->primary_plane.damage, damage);
334
335         wl_event_source_timer_update(output->finish_frame_timer, 10);
336 }
337
338 static void
339 x11_output_repaint_shm(struct weston_output *output_base,
340                        pixman_region32_t *damage)
341 {
342         struct x11_output *output = (struct x11_output *)output_base;
343         struct weston_compositor *ec = output->base.compositor;
344         struct x11_compositor *c = (struct x11_compositor *)ec;
345         pixman_box32_t *rects;
346         int nrects, i, src_x, src_y, x1, y1, x2, y2, width, height;
347         xcb_void_cookie_t cookie;
348         xcb_generic_error_t *err;
349
350         pixman_renderer_output_set_buffer(output_base, output->shadow_surface);
351         ec->renderer->repaint_output(output_base, damage);
352
353         width = pixman_image_get_width(output->shadow_surface);
354         height = pixman_image_get_height(output->shadow_surface);
355         rects = pixman_region32_rectangles(damage, &nrects);
356         for (i = 0; i < nrects; i++) {
357                 switch (output_base->transform) {
358                 default:
359                 case WL_OUTPUT_TRANSFORM_NORMAL:
360                         x1 = rects[i].x1;
361                         x2 = rects[i].x2;
362                         y1 = rects[i].y1;
363                         y2 = rects[i].y2;
364                         break;
365                 case WL_OUTPUT_TRANSFORM_180:
366                         x1 = width - rects[i].x2;
367                         x2 = width - rects[i].x1;
368                         y1 = height - rects[i].y2;
369                         y2 = height - rects[i].y1;
370                         break;
371                 case WL_OUTPUT_TRANSFORM_90:
372                         x1 = height - rects[i].y2;
373                         x2 = height - rects[i].y1;
374                         y1 = rects[i].x1;
375                         y2 = rects[i].x2;
376                         break;
377                 case WL_OUTPUT_TRANSFORM_270:
378                         x1 = rects[i].y1;
379                         x2 = rects[i].y2;
380                         y1 = width - rects[i].x2;
381                         y2 = width - rects[i].x1;
382                         break;
383                 }
384                 src_x = x1;
385                 src_y = y1;
386
387                 pixman_image_composite32(PIXMAN_OP_SRC,
388                         output->shadow_surface, /* src */
389                         NULL /* mask */,
390                         output->hw_surface, /* dest */
391                         src_x, src_y, /* src_x, src_y */
392                         0, 0, /* mask_x, mask_y */
393                         x1, y1, /* dest_x, dest_y */
394                         x2 - x1, /* width */
395                         y2 - y1 /* height */);
396         }
397
398         pixman_region32_subtract(&ec->primary_plane.damage,
399                                  &ec->primary_plane.damage, damage);
400         cookie = xcb_shm_put_image_checked(c->conn, output->window, output->gc,
401                                         pixman_image_get_width(output->hw_surface),
402                                         pixman_image_get_height(output->hw_surface),
403                                         0, 0,
404                                         pixman_image_get_width(output->hw_surface),
405                                         pixman_image_get_height(output->hw_surface),
406                                         0, 0, output->depth, XCB_IMAGE_FORMAT_Z_PIXMAP,
407                                         0, output->segment, 0);
408         err = xcb_request_check(c->conn, cookie);
409         if (err != NULL) {
410                 weston_log("Failed to put shm image, err: %d\n", err->error_code);
411                 free(err);
412         }
413
414         wl_event_source_timer_update(output->finish_frame_timer, 10);
415 }
416
417 static int
418 finish_frame_handler(void *data)
419 {
420         struct x11_output *output = data;
421         uint32_t msec;
422         struct timeval tv;
423         
424         gettimeofday(&tv, NULL);
425         msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
426         weston_output_finish_frame(&output->base, msec);
427
428         return 1;
429 }
430
431 static void
432 x11_output_deinit_shm(struct x11_compositor *c, struct x11_output *output)
433 {
434         xcb_void_cookie_t cookie;
435         xcb_generic_error_t *err;
436         xcb_free_gc(c->conn, output->gc);
437
438         pixman_image_unref(output->hw_surface);
439         output->hw_surface = NULL;
440         cookie = xcb_shm_detach_checked(c->conn, output->segment);
441         err = xcb_request_check(c->conn, cookie);
442         if (err) {
443                 weston_log("xcb_shm_detach failed, error %d\n", err->error_code);
444                 free(err);
445         }
446         shmdt(output->buf);
447
448         pixman_image_unref(output->shadow_surface);
449         output->shadow_surface = NULL;
450         free(output->shadow_buf);
451 }
452
453 static void
454 x11_output_destroy(struct weston_output *output_base)
455 {
456         struct x11_output *output = (struct x11_output *)output_base;
457         struct x11_compositor *compositor =
458                 (struct x11_compositor *)output->base.compositor;
459
460         wl_list_remove(&output->base.link);
461         wl_event_source_remove(output->finish_frame_timer);
462
463         if (compositor->use_pixman) {
464                 pixman_renderer_output_destroy(output_base);
465                 x11_output_deinit_shm(compositor, output);
466         } else
467                 gl_renderer_output_destroy(output_base);
468
469         xcb_destroy_window(compositor->conn, output->window);
470
471         weston_output_destroy(&output->base);
472
473         free(output);
474 }
475
476 static void
477 x11_output_set_wm_protocols(struct x11_compositor *c,
478                             struct x11_output *output)
479 {
480         xcb_atom_t list[1];
481
482         list[0] = c->atom.wm_delete_window;
483         xcb_change_property (c->conn, 
484                              XCB_PROP_MODE_REPLACE,
485                              output->window,
486                              c->atom.wm_protocols,
487                              XCB_ATOM_ATOM,
488                              32,
489                              ARRAY_LENGTH(list),
490                              list);
491 }
492
493 struct wm_normal_hints {
494         uint32_t flags;
495         uint32_t pad[4];
496         int32_t min_width, min_height;
497         int32_t max_width, max_height;
498         int32_t width_inc, height_inc;
499         int32_t min_aspect_x, min_aspect_y;
500         int32_t max_aspect_x, max_aspect_y;
501         int32_t base_width, base_height;
502         int32_t win_gravity;
503 };
504
505 #define WM_NORMAL_HINTS_MIN_SIZE        16
506 #define WM_NORMAL_HINTS_MAX_SIZE        32
507
508 static void
509 x11_output_set_icon(struct x11_compositor *c,
510                     struct x11_output *output, const char *filename)
511 {
512         uint32_t *icon;
513         int32_t width, height;
514         pixman_image_t *image;
515
516         image = load_image(filename);
517         if (!image)
518                 return;
519         width = pixman_image_get_width(image);
520         height = pixman_image_get_height(image);
521         icon = malloc(width * height * 4 + 8);
522         if (!icon) {
523                 pixman_image_unref(image);
524                 return;
525         }
526
527         icon[0] = width;
528         icon[1] = height;
529         memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
530         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
531                             c->atom.net_wm_icon, c->atom.cardinal, 32,
532                             width * height + 2, icon);
533         free(icon);
534         pixman_image_unref(image);
535 }
536
537 static void
538 x11_output_wait_for_map(struct x11_compositor *c, struct x11_output *output)
539 {
540         xcb_map_notify_event_t *map_notify;
541         xcb_configure_notify_event_t *configure_notify;
542         xcb_generic_event_t *event;
543         int mapped = 0;
544         uint8_t response_type;
545
546         /* This isn't the nicest way to do this.  Ideally, we could
547          * just go back to the main loop and once we get the map
548          * notify, we add the output to the compositor.  While we do
549          * support output hotplug, we can't start up with no outputs.
550          * We could add the output and then resize once we get the map
551          * notify, but we don't want to start up and immediately
552          * resize the output. */
553
554         xcb_flush(c->conn);
555
556         while (!mapped) {
557                 event = xcb_wait_for_event(c->conn);
558                 response_type = event->response_type & ~0x80;
559
560                 switch (response_type) {
561                 case XCB_MAP_NOTIFY:
562                         map_notify = (xcb_map_notify_event_t *) event;
563                         if (map_notify->window == output->window)
564                                 mapped = 1;
565                         break;
566
567                 case XCB_CONFIGURE_NOTIFY:
568                         configure_notify =
569                                 (xcb_configure_notify_event_t *) event;
570
571                         output->mode.width = configure_notify->width;
572                         output->mode.height = configure_notify->height;
573                         break;
574                 }
575         }
576 }
577
578 static xcb_visualtype_t *
579 find_visual_by_id(xcb_screen_t *screen,
580                    xcb_visualid_t id)
581 {
582         xcb_depth_iterator_t i;
583         xcb_visualtype_iterator_t j;
584         for (i = xcb_screen_allowed_depths_iterator(screen);
585              i.rem;
586              xcb_depth_next(&i)) {
587                 for (j = xcb_depth_visuals_iterator(i.data);
588                      j.rem;
589                      xcb_visualtype_next(&j)) {
590                         if (j.data->visual_id == id)
591                                 return j.data;
592                 }
593         }
594         return 0;
595 }
596
597 static uint8_t
598 get_depth_of_visual(xcb_screen_t *screen,
599                    xcb_visualid_t id)
600 {
601         xcb_depth_iterator_t i;
602         xcb_visualtype_iterator_t j;
603         for (i = xcb_screen_allowed_depths_iterator(screen);
604              i.rem;
605              xcb_depth_next(&i)) {
606                 for (j = xcb_depth_visuals_iterator(i.data);
607                      j.rem;
608                      xcb_visualtype_next(&j)) {
609                         if (j.data->visual_id == id)
610                                 return i.data->depth;
611                 }
612         }
613         return 0;
614 }
615
616 static int
617 x11_output_init_shm(struct x11_compositor *c, struct x11_output *output,
618         int width, int height)
619 {
620         xcb_screen_iterator_t iter;
621         xcb_visualtype_t *visual_type;
622         xcb_format_iterator_t fmt;
623         xcb_void_cookie_t cookie;
624         xcb_generic_error_t *err;
625         int shadow_width, shadow_height;
626         pixman_transform_t transform;
627         const xcb_query_extension_reply_t *ext;
628         int bitsperpixel = 0;
629         pixman_format_code_t pixman_format;
630
631         /* Check if SHM is available */
632         ext = xcb_get_extension_data(c->conn, &xcb_shm_id);
633         if (ext == NULL || !ext->present) {
634                 /* SHM is missing */
635                 weston_log("SHM extension is not available\n");
636                 errno = ENOENT;
637                 return -1;
638         }
639
640         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
641         visual_type = find_visual_by_id(iter.data, iter.data->root_visual);
642         if (!visual_type) {
643                 weston_log("Failed to lookup visual for root window\n");
644                 errno = ENOENT;
645                 return -1;
646         }
647         weston_log("Found visual, bits per value: %d, red_mask: %.8x, green_mask: %.8x, blue_mask: %.8x\n",
648                 visual_type->bits_per_rgb_value,
649                 visual_type->red_mask,
650                 visual_type->green_mask,
651                 visual_type->blue_mask);
652         output->depth = get_depth_of_visual(iter.data, iter.data->root_visual);
653         weston_log("Visual depth is %d\n", output->depth);
654
655         for (fmt = xcb_setup_pixmap_formats_iterator(xcb_get_setup(c->conn));
656              fmt.rem;
657              xcb_format_next(&fmt)) {
658                 if (fmt.data->depth == output->depth) {
659                         bitsperpixel = fmt.data->bits_per_pixel;
660                         break;
661                 }
662         }
663         weston_log("Found format for depth %d, bpp: %d\n",
664                 output->depth, bitsperpixel);
665
666         if  (bitsperpixel == 32 &&
667              visual_type->red_mask == 0xff0000 &&
668              visual_type->green_mask == 0x00ff00 &&
669              visual_type->blue_mask == 0x0000ff) {
670                 weston_log("Will use x8r8g8b8 format for SHM surfaces\n");
671                 pixman_format = PIXMAN_x8r8g8b8;
672         } else {
673                 weston_log("Can't find appropriate format for SHM pixmap\n");
674                 errno = ENOTSUP;
675                 return -1;
676         }
677
678
679         /* Create SHM segment and attach it */
680         output->shm_id = shmget(IPC_PRIVATE, width * height * (bitsperpixel / 8), IPC_CREAT | S_IRWXU);
681         if (output->shm_id == -1) {
682                 weston_log("x11shm: failed to allocate SHM segment\n");
683                 return -1;
684         }
685         output->buf = shmat(output->shm_id, NULL, 0 /* read/write */);
686         if (-1 == (long)output->buf) {
687                 weston_log("x11shm: failed to attach SHM segment\n");
688                 return -1;
689         }
690         output->segment = xcb_generate_id(c->conn);
691         cookie = xcb_shm_attach_checked(c->conn, output->segment, output->shm_id, 1);
692         err = xcb_request_check(c->conn, cookie);
693         if (err) {
694                 weston_log("x11shm: xcb_shm_attach error %d\n", err->error_code);
695                 free(err);
696                 return -1;
697         }
698
699         shmctl(output->shm_id, IPC_RMID, NULL);
700
701         /* Now create pixman image */
702         output->hw_surface = pixman_image_create_bits(pixman_format, width, height, output->buf,
703                 width * (bitsperpixel / 8));
704         pixman_transform_init_identity(&transform);
705         switch (output->base.transform) {
706         default:
707         case WL_OUTPUT_TRANSFORM_NORMAL:
708                 shadow_width = width;
709                 shadow_height = height;
710                 pixman_transform_rotate(&transform,
711                         NULL, 0, 0);
712                 pixman_transform_translate(&transform, NULL,
713                         0, 0);
714                 break;
715         case WL_OUTPUT_TRANSFORM_180:
716                 shadow_width = width;
717                 shadow_height = height;
718                 pixman_transform_rotate(&transform,
719                         NULL, -pixman_fixed_1, 0);
720                 pixman_transform_translate(NULL, &transform,
721                         pixman_int_to_fixed(shadow_width),
722                         pixman_int_to_fixed(shadow_height));
723                 break;
724         case WL_OUTPUT_TRANSFORM_270:
725                 shadow_width = height;
726                 shadow_height = width;
727                 pixman_transform_rotate(&transform,
728                         NULL, 0, pixman_fixed_1);
729                 pixman_transform_translate(&transform,
730                         NULL,
731                         pixman_int_to_fixed(shadow_width),
732                         0);
733                 break;
734         case WL_OUTPUT_TRANSFORM_90:
735                 shadow_width = height;
736                 shadow_height = width;
737                 pixman_transform_rotate(&transform,
738                         NULL, 0, -pixman_fixed_1);
739                 pixman_transform_translate(&transform,
740                         NULL,
741                         0,
742                         pixman_int_to_fixed(shadow_height));
743                 break;
744         }
745         output->shadow_buf = malloc(width * height *  (bitsperpixel / 8));
746         output->shadow_surface = pixman_image_create_bits(pixman_format, shadow_width, shadow_height,
747                 output->shadow_buf, shadow_width * (bitsperpixel / 8));
748         /* No need in transform for normal output */
749         if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
750                 pixman_image_set_transform(output->shadow_surface, &transform);
751
752         output->gc = xcb_generate_id(c->conn);
753         xcb_create_gc(c->conn, output->gc, output->window, 0, NULL);
754
755         return 0;
756 }
757
758 static struct x11_output *
759 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
760                              int width, int height, int fullscreen,
761                              int no_input, char *configured_name,
762                              uint32_t transform)
763 {
764         static const char name[] = "Weston Compositor";
765         static const char class[] = "weston-1\0Weston Compositor";
766         char title[32];
767         struct x11_output *output;
768         xcb_screen_iterator_t iter;
769         struct wm_normal_hints normal_hints;
770         struct wl_event_loop *loop;
771         uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
772         xcb_atom_t atom_list[1];
773         uint32_t values[2] = {
774                 XCB_EVENT_MASK_EXPOSURE |
775                 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
776                 0
777         };
778
779         if (configured_name)
780                 sprintf(title, "%s - %s", name, configured_name);
781         else
782                 strcpy(title, name);
783
784         if (!no_input)
785                 values[0] |=
786                         XCB_EVENT_MASK_KEY_PRESS |
787                         XCB_EVENT_MASK_KEY_RELEASE |
788                         XCB_EVENT_MASK_BUTTON_PRESS |
789                         XCB_EVENT_MASK_BUTTON_RELEASE |
790                         XCB_EVENT_MASK_POINTER_MOTION |
791                         XCB_EVENT_MASK_ENTER_WINDOW |
792                         XCB_EVENT_MASK_LEAVE_WINDOW |
793                         XCB_EVENT_MASK_KEYMAP_STATE |
794                         XCB_EVENT_MASK_FOCUS_CHANGE;
795
796         output = malloc(sizeof *output);
797         if (output == NULL)
798                 return NULL;
799
800         memset(output, 0, sizeof *output);
801
802         output->mode.flags =
803                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
804         output->mode.width = width;
805         output->mode.height = height;
806         output->mode.refresh = 60000;
807         wl_list_init(&output->base.mode_list);
808         wl_list_insert(&output->base.mode_list, &output->mode.link);
809
810         values[1] = c->null_cursor;
811         output->window = xcb_generate_id(c->conn);
812         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
813         xcb_create_window(c->conn,
814                           XCB_COPY_FROM_PARENT,
815                           output->window,
816                           iter.data->root,
817                           0, 0,
818                           width, height,
819                           0,
820                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
821                           iter.data->root_visual,
822                           mask, values);
823
824         if (fullscreen) {
825                 atom_list[0] = c->atom.net_wm_state_fullscreen;
826                 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE,
827                                     output->window,
828                                     c->atom.net_wm_state,
829                                     XCB_ATOM_ATOM, 32,
830                                     ARRAY_LENGTH(atom_list), atom_list);
831         } else {
832                 /* Don't resize me. */
833                 memset(&normal_hints, 0, sizeof normal_hints);
834                 normal_hints.flags =
835                         WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
836                 normal_hints.min_width = width;
837                 normal_hints.min_height = height;
838                 normal_hints.max_width = width;
839                 normal_hints.max_height = height;
840                 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
841                                     c->atom.wm_normal_hints,
842                                     c->atom.wm_size_hints, 32,
843                                     sizeof normal_hints / 4,
844                                     (uint8_t *) &normal_hints);
845         }
846
847         /* Set window name.  Don't bother with non-EWMH WMs. */
848         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
849                             c->atom.net_wm_name, c->atom.utf8_string, 8,
850                             strlen(title), title);
851         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
852                             c->atom.wm_class, c->atom.string, 8,
853                             sizeof class, class);
854
855         x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
856
857         x11_output_set_wm_protocols(c, output);
858
859         xcb_map_window(c->conn, output->window);
860
861         x11_output_wait_for_map(c, output);
862
863         output->base.origin = output->base.current;
864         if (c->use_pixman)
865                 output->base.repaint = x11_output_repaint_shm;
866         else
867                 output->base.repaint = x11_output_repaint_gl;
868         output->base.destroy = x11_output_destroy;
869         output->base.assign_planes = NULL;
870         output->base.set_backlight = NULL;
871         output->base.set_dpms = NULL;
872         output->base.switch_mode = NULL;
873         output->base.current = &output->mode;
874         output->base.make = "xwayland";
875         output->base.model = "none";
876         weston_output_init(&output->base, &c->base,
877                            x, y, width, height, transform);
878
879         if (c->use_pixman) {
880                 if (x11_output_init_shm(c, output, width, height) < 0)
881                         return NULL;
882                 if (pixman_renderer_output_create(&output->base) < 0) {
883                         x11_output_deinit_shm(c, output);
884                         return NULL;
885                 }
886         } else {
887                 if (gl_renderer_output_create(&output->base, output->window) < 0)
888                         return NULL;
889         }
890
891         loop = wl_display_get_event_loop(c->base.wl_display);
892         output->finish_frame_timer =
893                 wl_event_loop_add_timer(loop, finish_frame_handler, output);
894
895         wl_list_insert(c->base.output_list.prev, &output->base.link);
896
897         weston_log("x11 output %dx%d, window id %d\n",
898                    width, height, output->window);
899
900         return output;
901 }
902
903 static struct x11_output *
904 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
905 {
906         struct x11_output *output;
907
908         wl_list_for_each(output, &c->base.output_list, base.link) {
909                 if (output->window == window)
910                         return output;
911         }
912
913         return NULL;
914 }
915
916 #ifdef HAVE_XCB_XKB
917 static void
918 update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
919 {
920         xkb_state_update_mask(c->core_seat.xkb_state.state,
921                               get_xkb_mod_mask(c, state->baseMods),
922                               get_xkb_mod_mask(c, state->latchedMods),
923                               get_xkb_mod_mask(c, state->lockedMods),
924                               0,
925                               0,
926                               state->group);
927
928         notify_modifiers(&c->core_seat,
929                          wl_display_next_serial(c->base.wl_display));
930 }
931 #endif
932
933 /**
934  * This is monumentally unpleasant.  If we don't have XCB-XKB bindings,
935  * the best we can do (given that XCB also lacks XI2 support), is to take
936  * the state from the core key events.  Unfortunately that only gives us
937  * the effective (i.e. union of depressed/latched/locked) state, and we
938  * need the granularity.
939  *
940  * So we still update the state with every key event we see, but also use
941  * the state field from X11 events as a mask so we don't get any stuck
942  * modifiers.
943  */
944 static void
945 update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
946 {
947         uint32_t mask = get_xkb_mod_mask(c, x11_mask);
948         struct wl_keyboard *keyboard = &c->core_seat.keyboard;
949
950         xkb_state_update_mask(c->core_seat.xkb_state.state,
951                               keyboard->modifiers.mods_depressed & mask,
952                               keyboard->modifiers.mods_latched & mask,
953                               keyboard->modifiers.mods_locked & mask,
954                               0,
955                               0,
956                               (x11_mask >> 13) & 3);
957         notify_modifiers(&c->core_seat,
958                          wl_display_next_serial(c->base.wl_display));
959 }
960
961 static void
962 x11_compositor_deliver_button_event(struct x11_compositor *c,
963                                     xcb_generic_event_t *event, int state)
964 {
965         xcb_button_press_event_t *button_event =
966                 (xcb_button_press_event_t *) event;
967         uint32_t button;
968         struct x11_output *output;
969
970         output = x11_compositor_find_output(c, button_event->event);
971
972         if (state)
973                 xcb_grab_pointer(c->conn, 0, output->window,
974                                  XCB_EVENT_MASK_BUTTON_PRESS |
975                                  XCB_EVENT_MASK_BUTTON_RELEASE |
976                                  XCB_EVENT_MASK_POINTER_MOTION |
977                                  XCB_EVENT_MASK_ENTER_WINDOW |
978                                  XCB_EVENT_MASK_LEAVE_WINDOW,
979                                  XCB_GRAB_MODE_ASYNC,
980                                  XCB_GRAB_MODE_ASYNC,
981                                  output->window, XCB_CURSOR_NONE,
982                                  button_event->time);
983         else
984                 xcb_ungrab_pointer(c->conn, button_event->time);
985
986         if (!c->has_xkb)
987                 update_xkb_state_from_core(c, button_event->state);
988
989         switch (button_event->detail) {
990         default:
991                 button = button_event->detail + BTN_LEFT - 1;
992                 break;
993         case 2:
994                 button = BTN_MIDDLE;
995                 break;
996         case 3:
997                 button = BTN_RIGHT;
998                 break;
999         case 4:
1000                 /* Axis are measured in pixels, but the xcb events are discrete
1001                  * steps. Therefore move the axis by some pixels every step. */
1002                 if (state)
1003                         notify_axis(&c->core_seat,
1004                                     weston_compositor_get_time(),
1005                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
1006                                     -DEFAULT_AXIS_STEP_DISTANCE);
1007                 return;
1008         case 5:
1009                 if (state)
1010                         notify_axis(&c->core_seat,
1011                                     weston_compositor_get_time(),
1012                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
1013                                     DEFAULT_AXIS_STEP_DISTANCE);
1014                 return;
1015         case 6:
1016                 if (state)
1017                         notify_axis(&c->core_seat,
1018                                     weston_compositor_get_time(),
1019                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1020                                     -DEFAULT_AXIS_STEP_DISTANCE);
1021                 return;
1022         case 7:
1023                 if (state)
1024                         notify_axis(&c->core_seat,
1025                                     weston_compositor_get_time(),
1026                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
1027                                     DEFAULT_AXIS_STEP_DISTANCE);
1028                 return;
1029         }
1030
1031         notify_button(&c->core_seat,
1032                       weston_compositor_get_time(), button,
1033                       state ? WL_POINTER_BUTTON_STATE_PRESSED :
1034                               WL_POINTER_BUTTON_STATE_RELEASED);
1035 }
1036
1037 static void
1038 x11_output_transform_coordinate(struct x11_output *x11_output,
1039                                                 wl_fixed_t *x, wl_fixed_t *y)
1040 {
1041         struct weston_output *output = &x11_output->base;
1042         wl_fixed_t tx, ty;
1043         wl_fixed_t width = wl_fixed_from_int(output->width - 1);
1044         wl_fixed_t height = wl_fixed_from_int(output->height - 1);
1045
1046         switch(output->transform) {
1047         case WL_OUTPUT_TRANSFORM_NORMAL:
1048         default:
1049                 tx = *x;
1050                 ty = *y;
1051                 break;
1052         case WL_OUTPUT_TRANSFORM_90:
1053                 tx = *y;
1054                 ty = height - *x;
1055                 break;
1056         case WL_OUTPUT_TRANSFORM_180:
1057                 tx = width - *x;
1058                 ty = height - *y;
1059                 break;
1060         case WL_OUTPUT_TRANSFORM_270:
1061                 tx = width - *y;
1062                 ty = *x;
1063                 break;
1064         case WL_OUTPUT_TRANSFORM_FLIPPED:
1065                 tx = width - *x;
1066                 ty = *y;
1067                 break;
1068         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1069                 tx = width - *y;
1070                 ty = height - *x;
1071                 break;
1072         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
1073                 tx = *x;
1074                 ty = height - *y;
1075                 break;
1076         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1077                 tx = *y;
1078                 ty = *x;
1079                 break;
1080         }
1081
1082         tx += wl_fixed_from_int(output->x);
1083         ty += wl_fixed_from_int(output->y);
1084
1085         *x = tx;
1086         *y = ty;
1087 }
1088
1089 static void
1090 x11_compositor_deliver_motion_event(struct x11_compositor *c,
1091                                         xcb_generic_event_t *event)
1092 {
1093         struct x11_output *output;
1094         wl_fixed_t x, y;
1095         xcb_motion_notify_event_t *motion_notify =
1096                         (xcb_motion_notify_event_t *) event;
1097
1098         if (!c->has_xkb)
1099                 update_xkb_state_from_core(c, motion_notify->state);
1100         output = x11_compositor_find_output(c, motion_notify->event);
1101         x = wl_fixed_from_int(motion_notify->event_x);
1102         y = wl_fixed_from_int(motion_notify->event_y);
1103         x11_output_transform_coordinate(output, &x, &y);
1104
1105         notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
1106 }
1107
1108 static void
1109 x11_compositor_deliver_enter_event(struct x11_compositor *c,
1110                                         xcb_generic_event_t *event)
1111 {
1112         struct x11_output *output;
1113         wl_fixed_t x, y;
1114
1115         xcb_enter_notify_event_t *enter_notify =
1116                         (xcb_enter_notify_event_t *) event;
1117         if (enter_notify->state >= Button1Mask)
1118                 return;
1119         if (!c->has_xkb)
1120                 update_xkb_state_from_core(c, enter_notify->state);
1121         output = x11_compositor_find_output(c, enter_notify->event);
1122         x = wl_fixed_from_int(enter_notify->event_x);
1123         y = wl_fixed_from_int(enter_notify->event_y);
1124         x11_output_transform_coordinate(output, &x, &y);
1125
1126         notify_pointer_focus(&c->core_seat, &output->base, x, y);
1127 }
1128
1129 static int
1130 x11_compositor_next_event(struct x11_compositor *c,
1131                           xcb_generic_event_t **event, uint32_t mask)
1132 {
1133         if (mask & WL_EVENT_READABLE) {
1134                 *event = xcb_poll_for_event(c->conn);
1135         } else {
1136 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
1137                 *event = xcb_poll_for_queued_event(c->conn);
1138 #else
1139                 *event = xcb_poll_for_event(c->conn);
1140 #endif
1141         }
1142
1143         return *event != NULL;
1144 }
1145
1146 static int
1147 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
1148 {
1149         struct x11_compositor *c = data;
1150         struct x11_output *output;
1151         xcb_generic_event_t *event, *prev;
1152         xcb_client_message_event_t *client_message;
1153         xcb_enter_notify_event_t *enter_notify;
1154         xcb_key_press_event_t *key_press, *key_release;
1155         xcb_keymap_notify_event_t *keymap_notify;
1156         xcb_focus_in_event_t *focus_in;
1157         xcb_expose_event_t *expose;
1158         xcb_atom_t atom;
1159         uint32_t *k;
1160         uint32_t i, set;
1161         uint8_t response_type;
1162         int count;
1163
1164         prev = NULL;
1165         count = 0;
1166         while (x11_compositor_next_event(c, &event, mask)) {
1167                 response_type = event->response_type & ~0x80;
1168
1169                 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1170                 case XCB_KEY_RELEASE:
1171                         /* Suppress key repeat events; this is only used if we
1172                          * don't have XCB XKB support. */
1173                         key_release = (xcb_key_press_event_t *) prev;
1174                         key_press = (xcb_key_press_event_t *) event;
1175                         if (response_type == XCB_KEY_PRESS &&
1176                             key_release->time == key_press->time &&
1177                             key_release->detail == key_press->detail) {
1178                                 /* Don't deliver the held key release
1179                                  * event or the new key press event. */
1180                                 free(event);
1181                                 free(prev);
1182                                 prev = NULL;
1183                                 continue;
1184                         } else {
1185                                 /* Deliver the held key release now
1186                                  * and fall through and handle the new
1187                                  * event below. */
1188                                 update_xkb_state_from_core(c, key_release->state);
1189                                 notify_key(&c->core_seat,
1190                                            weston_compositor_get_time(),
1191                                            key_release->detail - 8,
1192                                            WL_KEYBOARD_KEY_STATE_RELEASED,
1193                                            STATE_UPDATE_AUTOMATIC);
1194                                 free(prev);
1195                                 prev = NULL;
1196                                 break;
1197                         }
1198
1199                 case XCB_FOCUS_IN:
1200                         assert(response_type == XCB_KEYMAP_NOTIFY);
1201                         keymap_notify = (xcb_keymap_notify_event_t *) event;
1202                         c->keys.size = 0;
1203                         for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
1204                                 set = keymap_notify->keys[i >> 3] &
1205                                         (1 << (i & 7));
1206                                 if (set) {
1207                                         k = wl_array_add(&c->keys, sizeof *k);
1208                                         *k = i;
1209                                 }
1210                         }
1211
1212                         /* Unfortunately the state only comes with the enter
1213                          * event, rather than with the focus event.  I'm not
1214                          * sure of the exact semantics around it and whether
1215                          * we can ensure that we get both? */
1216                         notify_keyboard_focus_in(&c->core_seat, &c->keys,
1217                                                  STATE_UPDATE_AUTOMATIC);
1218
1219                         free(prev);
1220                         prev = NULL;
1221                         break;
1222
1223                 default:
1224                         /* No previous event held */
1225                         break;
1226                 }
1227
1228                 switch (response_type) {
1229                 case XCB_KEY_PRESS:
1230                         key_press = (xcb_key_press_event_t *) event;
1231                         if (!c->has_xkb)
1232                                 update_xkb_state_from_core(c, key_press->state);
1233                         notify_key(&c->core_seat,
1234                                    weston_compositor_get_time(),
1235                                    key_press->detail - 8,
1236                                    WL_KEYBOARD_KEY_STATE_PRESSED,
1237                                    c->has_xkb ? STATE_UPDATE_NONE :
1238                                                 STATE_UPDATE_AUTOMATIC);
1239                         break;
1240                 case XCB_KEY_RELEASE:
1241                         /* If we don't have XKB, we need to use the lame
1242                          * autorepeat detection above. */
1243                         if (!c->has_xkb) {
1244                                 prev = event;
1245                                 break;
1246                         }
1247                         key_release = (xcb_key_press_event_t *) event;
1248                         notify_key(&c->core_seat,
1249                                    weston_compositor_get_time(),
1250                                    key_release->detail - 8,
1251                                    WL_KEYBOARD_KEY_STATE_RELEASED,
1252                                    STATE_UPDATE_NONE);
1253                         break;
1254                 case XCB_BUTTON_PRESS:
1255                         x11_compositor_deliver_button_event(c, event, 1);
1256                         break;
1257                 case XCB_BUTTON_RELEASE:
1258                         x11_compositor_deliver_button_event(c, event, 0);
1259                         break;
1260                 case XCB_MOTION_NOTIFY:
1261                         x11_compositor_deliver_motion_event(c, event);
1262                         break;
1263
1264                 case XCB_EXPOSE:
1265                         expose = (xcb_expose_event_t *) event;
1266                         output = x11_compositor_find_output(c, expose->window);
1267                         weston_output_schedule_repaint(&output->base);
1268                         break;
1269
1270                 case XCB_ENTER_NOTIFY:
1271                         x11_compositor_deliver_enter_event(c, event);
1272                         break;
1273
1274                 case XCB_LEAVE_NOTIFY:
1275                         enter_notify = (xcb_enter_notify_event_t *) event;
1276                         if (enter_notify->state >= Button1Mask)
1277                                 break;
1278                         if (!c->has_xkb)
1279                                 update_xkb_state_from_core(c, enter_notify->state);
1280                         notify_pointer_focus(&c->core_seat, NULL, 0, 0);
1281                         break;
1282
1283                 case XCB_CLIENT_MESSAGE:
1284                         client_message = (xcb_client_message_event_t *) event;
1285                         atom = client_message->data.data32[0];
1286                         if (atom == c->atom.wm_delete_window)
1287                                 wl_display_terminate(c->base.wl_display);
1288                         break;
1289
1290                 case XCB_FOCUS_IN:
1291                         focus_in = (xcb_focus_in_event_t *) event;
1292                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1293                                 break;
1294
1295                         prev = event;
1296                         break;
1297
1298                 case XCB_FOCUS_OUT:
1299                         focus_in = (xcb_focus_in_event_t *) event;
1300                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1301                             focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1302                                 break;
1303                         notify_keyboard_focus_out(&c->core_seat);
1304                         break;
1305
1306                 default:
1307                         break;
1308                 }
1309
1310 #ifdef HAVE_XCB_XKB
1311                 if (c->has_xkb &&
1312                     response_type == c->xkb_event_base) {
1313                         xcb_xkb_state_notify_event_t *state =
1314                                 (xcb_xkb_state_notify_event_t *) event;
1315                         if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1316                                 update_xkb_state(c, state);
1317                 }
1318 #endif
1319
1320                 count++;
1321                 if (prev != event)
1322                         free (event);
1323         }
1324
1325         switch (prev ? prev->response_type & ~0x80 : 0x80) {
1326         case XCB_KEY_RELEASE:
1327                 key_release = (xcb_key_press_event_t *) prev;
1328                 update_xkb_state_from_core(c, key_release->state);
1329                 notify_key(&c->core_seat,
1330                            weston_compositor_get_time(),
1331                            key_release->detail - 8,
1332                            WL_KEYBOARD_KEY_STATE_RELEASED,
1333                            STATE_UPDATE_AUTOMATIC);
1334                 free(prev);
1335                 prev = NULL;
1336                 break;
1337         default:
1338                 break;
1339         }
1340
1341         return count;
1342 }
1343
1344 #define F(field) offsetof(struct x11_compositor, field)
1345
1346 static void
1347 x11_compositor_get_resources(struct x11_compositor *c)
1348 {
1349         static const struct { const char *name; int offset; } atoms[] = {
1350                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
1351                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
1352                 { "WM_SIZE_HINTS",      F(atom.wm_size_hints) },
1353                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
1354                 { "WM_CLASS",           F(atom.wm_class) },
1355                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
1356                 { "_NET_WM_ICON",       F(atom.net_wm_icon) },
1357                 { "_NET_WM_STATE",      F(atom.net_wm_state) },
1358                 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1359                 { "STRING",             F(atom.string) },
1360                 { "UTF8_STRING",        F(atom.utf8_string) },
1361                 { "CARDINAL",           F(atom.cardinal) },
1362                 { "_XKB_RULES_NAMES",   F(atom.xkb_names) },
1363         };
1364
1365         xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1366         xcb_intern_atom_reply_t *reply;
1367         xcb_pixmap_t pixmap;
1368         xcb_gc_t gc;
1369         unsigned int i;
1370         uint8_t data[] = { 0, 0, 0, 0 };
1371
1372         for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1373                 cookies[i] = xcb_intern_atom (c->conn, 0,
1374                                               strlen(atoms[i].name),
1375                                               atoms[i].name);
1376
1377         for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1378                 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1379                 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1380                 free(reply);
1381         }
1382
1383         pixmap = xcb_generate_id(c->conn);
1384         gc = xcb_generate_id(c->conn);
1385         xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1386         xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1387         xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1388                       pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1389         c->null_cursor = xcb_generate_id(c->conn);
1390         xcb_create_cursor (c->conn, c->null_cursor,
1391                            pixmap, pixmap, 0, 0, 0,  0, 0, 0,  1, 1);
1392         xcb_free_gc(c->conn, gc);
1393         xcb_free_pixmap(c->conn, pixmap);
1394 }
1395
1396 static void
1397 x11_restore(struct weston_compositor *ec)
1398 {
1399 }
1400
1401 static void
1402 x11_free_configured_output(struct x11_configured_output *output)
1403 {
1404         free(output->name);
1405         free(output);
1406 }
1407
1408 static void
1409 x11_destroy(struct weston_compositor *ec)
1410 {
1411         struct x11_compositor *compositor = (struct x11_compositor *)ec;
1412         struct x11_configured_output *o, *n;
1413
1414         wl_list_for_each_safe(o, n, &configured_output_list, link)
1415                 x11_free_configured_output(o);
1416
1417         wl_event_source_remove(compositor->xcb_source);
1418         x11_input_destroy(compositor);
1419
1420         weston_compositor_shutdown(ec); /* destroys outputs, too */
1421
1422         if (compositor->use_pixman)
1423                 pixman_renderer_destroy(ec);
1424         else
1425                 gl_renderer_destroy(ec);
1426
1427         XCloseDisplay(compositor->dpy);
1428         free(ec);
1429 }
1430
1431 static struct weston_compositor *
1432 x11_compositor_create(struct wl_display *display,
1433                       int fullscreen,
1434                       int no_input,
1435                       int use_pixman,
1436                       int argc, char *argv[], const char *config_file)
1437 {
1438         struct x11_compositor *c;
1439         struct x11_configured_output *o;
1440         struct x11_output *output;
1441         xcb_screen_iterator_t s;
1442         int i, x = 0, output_count = 0;
1443         int width, height, count;
1444
1445         weston_log("initializing x11 backend\n");
1446
1447         c = malloc(sizeof *c);
1448         if (c == NULL)
1449                 return NULL;
1450
1451         memset(c, 0, sizeof *c);
1452
1453         if (weston_compositor_init(&c->base, display, argc, argv,
1454                                    config_file) < 0)
1455                 goto err_free;
1456
1457         c->dpy = XOpenDisplay(NULL);
1458         if (c->dpy == NULL)
1459                 goto err_free;
1460
1461         c->conn = XGetXCBConnection(c->dpy);
1462         XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1463
1464         if (xcb_connection_has_error(c->conn))
1465                 goto err_xdisplay;
1466
1467         s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1468         c->screen = s.data;
1469         wl_array_init(&c->keys);
1470
1471         x11_compositor_get_resources(c);
1472
1473         c->base.wl_display = display;
1474         c->use_pixman = use_pixman;
1475         if (c->use_pixman) {
1476                 if (pixman_renderer_init(&c->base) < 0)
1477                         goto err_xdisplay;
1478         }
1479         else {
1480                 if (gl_renderer_create(&c->base, c->dpy, gl_renderer_opaque_attribs,
1481                                 NULL) < 0)
1482                         goto err_xdisplay;
1483         }
1484         weston_log("Using %s renderer\n", use_pixman ? "pixman" : "gl");
1485
1486         c->base.destroy = x11_destroy;
1487         c->base.restore = x11_restore;
1488
1489         if (x11_input_create(c, no_input) < 0)
1490                 goto err_renderer;
1491
1492         width = option_width ? option_width : 1024;
1493         height = option_height ? option_height : 640;
1494         count = option_count ? option_count : 1;
1495
1496         wl_list_for_each(o, &configured_output_list, link) {
1497                 output = x11_compositor_create_output(c, x, 0,
1498                                                       option_width ? width :
1499                                                       o->width,
1500                                                       option_height ? height :
1501                                                       o->height,
1502                                                       fullscreen, no_input,
1503                                                       o->name, o->transform);
1504                 if (output == NULL)
1505                         goto err_x11_input;
1506
1507                 x = pixman_region32_extents(&output->base.region)->x2;
1508
1509                 output_count++;
1510                 if (option_count && output_count >= option_count)
1511                         break;
1512         }
1513
1514         for (i = output_count; i < count; i++) {
1515                 output = x11_compositor_create_output(c, x, 0, width, height,
1516                                                       fullscreen, no_input, NULL,
1517                                                       WL_OUTPUT_TRANSFORM_NORMAL);
1518                 if (output == NULL)
1519                         goto err_x11_input;
1520                 x = pixman_region32_extents(&output->base.region)->x2;
1521         }
1522
1523         c->xcb_source =
1524                 wl_event_loop_add_fd(c->base.input_loop,
1525                                      xcb_get_file_descriptor(c->conn),
1526                                      WL_EVENT_READABLE,
1527                                      x11_compositor_handle_event, c);
1528         wl_event_source_check(c->xcb_source);
1529
1530         return &c->base;
1531
1532 err_x11_input:
1533         x11_input_destroy(c);
1534 err_renderer:
1535         if (c->use_pixman)
1536                 pixman_renderer_destroy(&c->base);
1537         else
1538                 gl_renderer_destroy(&c->base);
1539 err_xdisplay:
1540         XCloseDisplay(c->dpy);
1541 err_free:
1542         free(c);
1543         return NULL;
1544 }
1545
1546 static void
1547 x11_output_set_transform(struct x11_configured_output *output)
1548 {
1549         if (!output_transform) {
1550                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1551                 return;
1552         }
1553
1554         if (!strcmp(output_transform, "normal"))
1555                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1556         else if (!strcmp(output_transform, "90"))
1557                 output->transform = WL_OUTPUT_TRANSFORM_90;
1558         else if (!strcmp(output_transform, "180"))
1559                 output->transform = WL_OUTPUT_TRANSFORM_180;
1560         else if (!strcmp(output_transform, "270"))
1561                 output->transform = WL_OUTPUT_TRANSFORM_270;
1562         else if (!strcmp(output_transform, "flipped"))
1563                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1564         else if (!strcmp(output_transform, "flipped-90"))
1565                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1566         else if (!strcmp(output_transform, "flipped-180"))
1567                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1568         else if (!strcmp(output_transform, "flipped-270"))
1569                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1570         else {
1571                 weston_log("Invalid transform \"%s\" for output %s\n",
1572                                                 output_transform, output_name);
1573                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1574         }
1575 }
1576
1577 static void
1578 output_section_done(void *data)
1579 {
1580         struct x11_configured_output *output;
1581
1582         output = malloc(sizeof *output);
1583
1584         if (!output || !output_name || (output_name[0] != 'X') ||
1585                                 (!output_mode && !output_transform)) {
1586                 if (output_name)
1587                         free(output_name);
1588                 output_name = NULL;
1589                 free(output);
1590                 goto err_free;
1591         }
1592
1593         output->name = output_name;
1594
1595         if (output_mode) {
1596                 if (sscanf(output_mode, "%dx%d", &output->width,
1597                                                 &output->height) != 2) {
1598                         weston_log("Invalid mode \"%s\" for output %s\n",
1599                                                         output_mode, output_name);
1600                         x11_free_configured_output(output);
1601                         goto err_free;
1602                 }
1603         } else {
1604                 output->width = 1024;
1605                 output->height = 640;
1606         }
1607
1608         x11_output_set_transform(output);
1609
1610         wl_list_insert(configured_output_list.prev, &output->link);
1611
1612 err_free:
1613         if (output_mode)
1614                 free(output_mode);
1615         if (output_transform)
1616                 free(output_transform);
1617         output_mode = NULL;
1618         output_transform = NULL;
1619 }
1620
1621 WL_EXPORT struct weston_compositor *
1622 backend_init(struct wl_display *display, int argc, char *argv[],
1623              const char *config_file)
1624 {
1625         int fullscreen = 0;
1626         int no_input = 0;
1627         int use_pixman = 0;
1628
1629         const struct weston_option x11_options[] = {
1630                 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1631                 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1632                 { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &fullscreen },
1633                 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1634                 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1635                 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1636         };
1637
1638         parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
1639
1640         wl_list_init(&configured_output_list);
1641
1642         const struct config_key x11_config_keys[] = {
1643                 { "name", CONFIG_KEY_STRING, &output_name },
1644                 { "mode", CONFIG_KEY_STRING, &output_mode },
1645                 { "transform", CONFIG_KEY_STRING, &output_transform },
1646         };
1647
1648         const struct config_section config_section[] = {
1649                 { "output", x11_config_keys,
1650                 ARRAY_LENGTH(x11_config_keys), output_section_done },
1651         };
1652
1653         parse_config_file(config_file, config_section,
1654                                 ARRAY_LENGTH(config_section), NULL);
1655
1656         return x11_compositor_create(display,
1657                                      fullscreen,
1658                                      no_input,
1659                                      use_pixman,
1660                                      argc, argv, config_file);
1661 }