compositor: Don't set DPMS state on start up
[profile/ivi/weston.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 <assert.h>
29 #include <stddef.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 #ifdef HAVE_XCB_XKB
40 #include <xcb/xkb.h>
41 #endif
42
43 #include <X11/Xlib.h>
44 #include <X11/Xlib-xcb.h>
45
46 #include <xkbcommon/xkbcommon.h>
47
48 #include <GLES2/gl2.h>
49 #include <EGL/egl.h>
50
51 #include "compositor.h"
52 #include "../shared/config-parser.h"
53 #include "../shared/image-loader.h"
54
55 #define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
56
57 static char *output_name;
58 static char *output_mode;
59 static char *output_transform;
60 static int option_width;
61 static int option_height;
62 static int option_count;
63 static struct wl_list configured_output_list;
64
65 struct x11_configured_output {
66         char *name;
67         int width, height;
68         uint32_t transform;
69         struct wl_list link;
70 };
71
72 struct x11_compositor {
73         struct weston_compositor         base;
74
75         Display                 *dpy;
76         xcb_connection_t        *conn;
77         xcb_screen_t            *screen;
78         xcb_cursor_t             null_cursor;
79         struct wl_array          keys;
80         struct wl_event_source  *xcb_source;
81         struct xkb_keymap       *xkb_keymap;
82         unsigned int             has_xkb;
83         uint8_t                  xkb_event_base;
84
85         /* We could map multi-pointer X to multiple wayland seats, but
86          * for now we only support core X input. */
87         struct weston_seat       core_seat;
88
89         struct {
90                 xcb_atom_t               wm_protocols;
91                 xcb_atom_t               wm_normal_hints;
92                 xcb_atom_t               wm_size_hints;
93                 xcb_atom_t               wm_delete_window;
94                 xcb_atom_t               wm_class;
95                 xcb_atom_t               net_wm_name;
96                 xcb_atom_t               net_wm_icon;
97                 xcb_atom_t               net_wm_state;
98                 xcb_atom_t               net_wm_state_fullscreen;
99                 xcb_atom_t               string;
100                 xcb_atom_t               utf8_string;
101                 xcb_atom_t               cardinal;
102                 xcb_atom_t               xkb_names;
103         } atom;
104 };
105
106 struct x11_output {
107         struct weston_output    base;
108
109         xcb_window_t            window;
110         struct weston_mode      mode;
111         struct wl_event_source *finish_frame_timer;
112 };
113
114 static struct xkb_keymap *
115 x11_compositor_get_keymap(struct x11_compositor *c)
116 {
117         xcb_get_property_cookie_t cookie;
118         xcb_get_property_reply_t *reply;
119         struct xkb_rule_names names;
120         struct xkb_keymap *ret;
121         const char *value_all, *value_part;
122         int length_all, length_part;
123
124         memset(&names, 0, sizeof(names));
125
126         cookie = xcb_get_property(c->conn, 0, c->screen->root,
127                                   c->atom.xkb_names, c->atom.string, 0, 1024);
128         reply = xcb_get_property_reply(c->conn, cookie, NULL);
129         if (reply == NULL)
130                 return NULL;
131
132         value_all = xcb_get_property_value(reply);
133         length_all = xcb_get_property_value_length(reply);
134         value_part = value_all;
135
136 #define copy_prop_value(to) \
137         length_part = strlen(value_part); \
138         if (value_part + length_part < (value_all + length_all) && \
139             length_part > 0) \
140                 names.to = value_part; \
141         value_part += length_part + 1;
142
143         copy_prop_value(rules);
144         copy_prop_value(model);
145         copy_prop_value(layout);
146         copy_prop_value(variant);
147         copy_prop_value(options);
148 #undef copy_prop_value
149
150         ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
151
152         free(reply);
153         return ret;
154 }
155
156 static uint32_t
157 get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
158 {
159         struct weston_xkb_info *info = &c->core_seat.xkb_info;
160         uint32_t ret = 0;
161
162         if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
163                 ret |= (1 << info->shift_mod);
164         if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
165                 ret |= (1 << info->caps_mod);
166         if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
167                 ret |= (1 << info->ctrl_mod);
168         if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
169                 ret |= (1 << info->alt_mod);
170         if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
171                 ret |= (1 << info->mod2_mod);
172         if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
173                 ret |= (1 << info->mod3_mod);
174         if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
175                 ret |= (1 << info->super_mod);
176         if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
177                 ret |= (1 << info->mod5_mod);
178
179         return ret;
180 }
181
182 static void
183 x11_compositor_setup_xkb(struct x11_compositor *c)
184 {
185 #ifndef HAVE_XCB_XKB
186         weston_log("XCB-XKB not available during build\n");
187         c->has_xkb = 0;
188         c->xkb_event_base = 0;
189         return;
190 #else
191         const xcb_query_extension_reply_t *ext;
192         xcb_generic_error_t *error;
193         xcb_void_cookie_t select;
194         xcb_xkb_use_extension_cookie_t use_ext;
195         xcb_xkb_use_extension_reply_t *use_ext_reply;
196         xcb_xkb_per_client_flags_cookie_t pcf;
197         xcb_xkb_per_client_flags_reply_t *pcf_reply;
198         xcb_xkb_get_state_cookie_t state;
199         xcb_xkb_get_state_reply_t *state_reply;
200
201         c->has_xkb = 0;
202         c->xkb_event_base = 0;
203
204         ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
205         if (!ext) {
206                 weston_log("XKB extension not available on host X11 server\n");
207                 return;
208         }
209         c->xkb_event_base = ext->first_event;
210
211         select = xcb_xkb_select_events_checked(c->conn,
212                                                XCB_XKB_ID_USE_CORE_KBD,
213                                                XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
214                                                0,
215                                                XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
216                                                0,
217                                                0,
218                                                NULL);
219         error = xcb_request_check(c->conn, select);
220         if (error) {
221                 weston_log("error: failed to select for XKB state events\n");
222                 free(error);
223                 return;
224         }
225
226         use_ext = xcb_xkb_use_extension(c->conn,
227                                         XCB_XKB_MAJOR_VERSION,
228                                         XCB_XKB_MINOR_VERSION);
229         use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL);
230         if (!use_ext_reply) {
231                 weston_log("couldn't start using XKB extension\n");
232                 return;
233         }
234
235         if (!use_ext_reply->supported) {
236                 weston_log("XKB extension version on the server is too old "
237                            "(want %d.%d, has %d.%d)\n",
238                            XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
239                            use_ext_reply->serverMajor, use_ext_reply->serverMinor);
240                 free(use_ext_reply);
241                 return;
242         }
243         free(use_ext_reply);
244
245         pcf = xcb_xkb_per_client_flags(c->conn,
246                                        XCB_XKB_ID_USE_CORE_KBD,
247                                        XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
248                                        XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
249                                        0,
250                                        0,
251                                        0);
252         pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL);
253         if (!pcf_reply ||
254             !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
255                 weston_log("failed to set XKB per-client flags, not using "
256                            "detectable repeat\n");
257                 free(pcf_reply);
258                 return;
259         }
260         free(pcf_reply);
261
262         state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
263         state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL);
264         if (!state_reply) {
265                 weston_log("failed to get initial XKB state\n");
266                 return;
267         }
268
269         xkb_state_update_mask(c->core_seat.xkb_state.state,
270                               get_xkb_mod_mask(c, state_reply->baseMods),
271                               get_xkb_mod_mask(c, state_reply->latchedMods),
272                               get_xkb_mod_mask(c, state_reply->lockedMods),
273                               0,
274                               0,
275                               state_reply->group);
276
277         free(state_reply);
278
279         c->has_xkb = 1;
280 #endif
281 }
282
283 static int
284 x11_input_create(struct x11_compositor *c, int no_input)
285 {
286         struct xkb_keymap *keymap;
287
288         weston_seat_init(&c->core_seat, &c->base);
289
290         if (no_input)
291                 return 0;
292
293         weston_seat_init_pointer(&c->core_seat);
294
295         keymap = x11_compositor_get_keymap(c);
296         if (weston_seat_init_keyboard(&c->core_seat, keymap) < 0)
297                 return -1;
298         if (keymap)
299                 xkb_map_unref(keymap);
300
301         x11_compositor_setup_xkb(c);
302
303         return 0;
304 }
305
306 static void
307 x11_input_destroy(struct x11_compositor *compositor)
308 {
309         weston_seat_release(&compositor->core_seat);
310 }
311
312 static int
313 x11_compositor_init_egl(struct x11_compositor *c)
314 {
315         EGLint major, minor;
316         EGLint n;
317         EGLint config_attribs[] = {
318                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
319                 EGL_RED_SIZE, 1,
320                 EGL_GREEN_SIZE, 1,
321                 EGL_BLUE_SIZE, 1,
322                 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
323                 EGL_NONE
324         };
325
326         c->base.egl_display = eglGetDisplay(c->dpy);
327         if (c->base.egl_display == NULL) {
328                 weston_log("failed to create display\n");
329                 return -1;
330         }
331
332         if (!eglInitialize(c->base.egl_display, &major, &minor)) {
333                 weston_log("failed to initialize display\n");
334                 return -1;
335         }
336
337         if (!eglChooseConfig(c->base.egl_display, config_attribs,
338                              &c->base.egl_config, 1, &n) || n == 0) {
339                 weston_log("failed to choose config: %d\n", n);
340                 return -1;
341         }
342
343         return 0;
344 }
345
346 static void
347 x11_compositor_fini_egl(struct x11_compositor *compositor)
348 {
349         gles2_renderer_destroy(&compositor->base);
350
351         eglMakeCurrent(compositor->base.egl_display,
352                        EGL_NO_SURFACE, EGL_NO_SURFACE,
353                        EGL_NO_CONTEXT);
354
355         eglTerminate(compositor->base.egl_display);
356         eglReleaseThread();
357 }
358
359 static void
360 x11_output_repaint(struct weston_output *output_base,
361                    pixman_region32_t *damage)
362 {
363         struct x11_output *output = (struct x11_output *)output_base;
364         struct weston_compositor *ec = output->base.compositor;
365
366         ec->renderer->repaint_output(output_base, damage);
367
368         pixman_region32_subtract(&ec->primary_plane.damage,
369                                  &ec->primary_plane.damage, damage);
370
371         wl_event_source_timer_update(output->finish_frame_timer, 10);
372 }
373
374 static int
375 finish_frame_handler(void *data)
376 {
377         struct x11_output *output = data;
378         uint32_t msec;
379         struct timeval tv;
380         
381         gettimeofday(&tv, NULL);
382         msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
383         weston_output_finish_frame(&output->base, msec);
384
385         return 1;
386 }
387
388 static void
389 x11_output_destroy(struct weston_output *output_base)
390 {
391         struct x11_output *output = (struct x11_output *)output_base;
392         struct x11_compositor *compositor =
393                 (struct x11_compositor *)output->base.compositor;
394
395         wl_list_remove(&output->base.link);
396         wl_event_source_remove(output->finish_frame_timer);
397
398         eglDestroySurface(compositor->base.egl_display,
399                           output->base.egl_surface);
400
401         xcb_destroy_window(compositor->conn, output->window);
402
403         weston_output_destroy(&output->base);
404
405         free(output);
406 }
407
408 static void
409 x11_output_set_wm_protocols(struct x11_output *output)
410 {
411         xcb_atom_t list[1];
412         struct x11_compositor *c =
413                 (struct x11_compositor *) output->base.compositor;
414
415         list[0] = c->atom.wm_delete_window;
416         xcb_change_property (c->conn, 
417                              XCB_PROP_MODE_REPLACE,
418                              output->window,
419                              c->atom.wm_protocols,
420                              XCB_ATOM_ATOM,
421                              32,
422                              ARRAY_LENGTH(list),
423                              list);
424 }
425
426 static void
427 x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
428 {
429         xcb_client_message_event_t event;
430         struct x11_compositor *c =
431                 (struct x11_compositor *) output->base.compositor;
432         xcb_screen_iterator_t iter;
433
434 #define _NET_WM_STATE_REMOVE        0    /* remove/unset property */
435 #define _NET_WM_STATE_ADD           1    /* add/set property */
436 #define _NET_WM_STATE_TOGGLE        2    /* toggle property  */  
437
438         memset(&event, 0, sizeof event);
439         event.response_type = XCB_CLIENT_MESSAGE;
440         event.format = 32;
441         event.window = output->window;
442         event.type = c->atom.net_wm_state;
443
444         event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
445         event.data.data32[1] = state;
446         event.data.data32[2] = 0;
447         event.data.data32[3] = 0;
448         event.data.data32[4] = 0;
449
450         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
451         xcb_send_event(c->conn, 0, iter.data->root,
452                        XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
453                        XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
454                        (void *) &event);
455 }
456
457
458 struct wm_normal_hints {
459         uint32_t flags;
460         uint32_t pad[4];
461         int32_t min_width, min_height;
462         int32_t max_width, max_height;
463         int32_t width_inc, height_inc;
464         int32_t min_aspect_x, min_aspect_y;
465         int32_t max_aspect_x, max_aspect_y;
466         int32_t base_width, base_height;
467         int32_t win_gravity;
468 };
469
470 #define WM_NORMAL_HINTS_MIN_SIZE        16
471 #define WM_NORMAL_HINTS_MAX_SIZE        32
472
473 static void
474 x11_output_set_icon(struct x11_compositor *c,
475                     struct x11_output *output, const char *filename)
476 {
477         uint32_t *icon;
478         int32_t width, height;
479         pixman_image_t *image;
480
481         image = load_image(filename);
482         if (!image)
483                 return;
484         width = pixman_image_get_width(image);
485         height = pixman_image_get_height(image);
486         icon = malloc(width * height * 4 + 8);
487         if (!icon) {
488                 pixman_image_unref(image);
489                 return;
490         }
491
492         icon[0] = width;
493         icon[1] = height;
494         memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
495         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
496                             c->atom.net_wm_icon, c->atom.cardinal, 32,
497                             width * height + 2, icon);
498         free(icon);
499         pixman_image_unref(image);
500 }
501
502 static struct x11_output *
503 x11_compositor_create_output(struct x11_compositor *c, int x, int y,
504                              int width, int height, int fullscreen,
505                              int no_input, char *configured_name,
506                              uint32_t transform)
507 {
508         static const char name[] = "Weston Compositor";
509         static const char class[] = "weston-1\0Weston Compositor";
510         char title[32];
511         struct x11_output *output;
512         xcb_screen_iterator_t iter;
513         struct wm_normal_hints normal_hints;
514         struct wl_event_loop *loop;
515         uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
516         uint32_t values[2] = {
517                 XCB_EVENT_MASK_EXPOSURE |
518                 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
519                 0
520         };
521
522         if (configured_name)
523                 sprintf(title, "%s - %s", name, configured_name);
524         else
525                 strcpy(title, name);
526
527         if (!no_input)
528                 values[0] |=
529                         XCB_EVENT_MASK_KEY_PRESS |
530                         XCB_EVENT_MASK_KEY_RELEASE |
531                         XCB_EVENT_MASK_BUTTON_PRESS |
532                         XCB_EVENT_MASK_BUTTON_RELEASE |
533                         XCB_EVENT_MASK_POINTER_MOTION |
534                         XCB_EVENT_MASK_ENTER_WINDOW |
535                         XCB_EVENT_MASK_LEAVE_WINDOW |
536                         XCB_EVENT_MASK_KEYMAP_STATE |
537                         XCB_EVENT_MASK_FOCUS_CHANGE;
538
539         output = malloc(sizeof *output);
540         if (output == NULL)
541                 return NULL;
542
543         memset(output, 0, sizeof *output);
544
545         output->mode.flags =
546                 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
547         output->mode.width = width;
548         output->mode.height = height;
549         output->mode.refresh = 60000;
550         wl_list_init(&output->base.mode_list);
551         wl_list_insert(&output->base.mode_list, &output->mode.link);
552
553         output->base.current = &output->mode;
554         output->base.make = "xwayland";
555         output->base.model = "none";
556         weston_output_init(&output->base, &c->base,
557                            x, y, width, height, transform);
558
559         values[1] = c->null_cursor;
560         output->window = xcb_generate_id(c->conn);
561         iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
562         xcb_create_window(c->conn,
563                           XCB_COPY_FROM_PARENT,
564                           output->window,
565                           iter.data->root,
566                           0, 0,
567                           width, height,
568                           0,
569                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
570                           iter.data->root_visual,
571                           mask, values);
572
573         /* Don't resize me. */
574         memset(&normal_hints, 0, sizeof normal_hints);
575         normal_hints.flags =
576                 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
577         normal_hints.min_width = width;
578         normal_hints.min_height = height;
579         normal_hints.max_width = width;
580         normal_hints.max_height = height;
581         xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
582                              c->atom.wm_normal_hints,
583                              c->atom.wm_size_hints, 32,
584                              sizeof normal_hints / 4,
585                              (uint8_t *) &normal_hints);
586
587         /* Set window name.  Don't bother with non-EWMH WMs. */
588         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
589                             c->atom.net_wm_name, c->atom.utf8_string, 8,
590                             strlen(title), title);
591         xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
592                             c->atom.wm_class, c->atom.string, 8,
593                             sizeof class, class);
594
595         x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
596
597         xcb_map_window(c->conn, output->window);
598
599         x11_output_set_wm_protocols(output);
600
601         if (fullscreen)
602                 x11_output_change_state(output, 1,
603                                         c->atom.net_wm_state_fullscreen);
604
605         output->base.egl_surface = 
606                 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
607                                        output->window, NULL);
608         if (!output->base.egl_surface) {
609                 weston_log("failed to create window surface\n");
610                 return NULL;
611         }
612
613         loop = wl_display_get_event_loop(c->base.wl_display);
614         output->finish_frame_timer =
615                 wl_event_loop_add_timer(loop, finish_frame_handler, output);
616
617         output->base.origin = output->base.current;
618         output->base.repaint = x11_output_repaint;
619         output->base.destroy = x11_output_destroy;
620         output->base.assign_planes = NULL;
621         output->base.set_backlight = NULL;
622         output->base.set_dpms = NULL;
623         output->base.switch_mode = NULL;
624
625         wl_list_insert(c->base.output_list.prev, &output->base.link);
626
627         weston_log("x11 output %dx%d, window id %d\n",
628                    width, height, output->window);
629
630         return output;
631 }
632
633 static struct x11_output *
634 x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
635 {
636         struct x11_output *output;
637
638         wl_list_for_each(output, &c->base.output_list, base.link) {
639                 if (output->window == window)
640                         return output;
641         }
642
643         return NULL;
644 }
645
646 #ifdef HAVE_XCB_XKB
647 static void
648 update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
649 {
650         xkb_state_update_mask(c->core_seat.xkb_state.state,
651                               get_xkb_mod_mask(c, state->baseMods),
652                               get_xkb_mod_mask(c, state->latchedMods),
653                               get_xkb_mod_mask(c, state->lockedMods),
654                               0,
655                               0,
656                               state->group);
657
658         notify_modifiers(&c->core_seat,
659                          wl_display_next_serial(c->base.wl_display));
660 }
661 #endif
662
663 /**
664  * This is monumentally unpleasant.  If we don't have XCB-XKB bindings,
665  * the best we can do (given that XCB also lacks XI2 support), is to take
666  * the state from the core key events.  Unfortunately that only gives us
667  * the effective (i.e. union of depressed/latched/locked) state, and we
668  * need the granularity.
669  *
670  * So we still update the state with every key event we see, but also use
671  * the state field from X11 events as a mask so we don't get any stuck
672  * modifiers.
673  */
674 static void
675 update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
676 {
677         uint32_t mask = get_xkb_mod_mask(c, x11_mask);
678         struct wl_keyboard *keyboard = &c->core_seat.keyboard;
679
680         xkb_state_update_mask(c->core_seat.xkb_state.state,
681                               keyboard->modifiers.mods_depressed & mask,
682                               keyboard->modifiers.mods_latched & mask,
683                               keyboard->modifiers.mods_locked & mask,
684                               0,
685                               0,
686                               (x11_mask >> 13) & 3);
687         notify_modifiers(&c->core_seat,
688                          wl_display_next_serial(c->base.wl_display));
689 }
690
691 static void
692 x11_compositor_deliver_button_event(struct x11_compositor *c,
693                                     xcb_generic_event_t *event, int state)
694 {
695         xcb_button_press_event_t *button_event =
696                 (xcb_button_press_event_t *) event;
697         uint32_t button;
698         struct x11_output *output;
699
700         output = x11_compositor_find_output(c, button_event->event);
701
702         if (state)
703                 xcb_grab_pointer(c->conn, 0, output->window,
704                                  XCB_EVENT_MASK_BUTTON_PRESS |
705                                  XCB_EVENT_MASK_BUTTON_RELEASE |
706                                  XCB_EVENT_MASK_POINTER_MOTION |
707                                  XCB_EVENT_MASK_ENTER_WINDOW |
708                                  XCB_EVENT_MASK_LEAVE_WINDOW,
709                                  XCB_GRAB_MODE_ASYNC,
710                                  XCB_GRAB_MODE_ASYNC,
711                                  output->window, XCB_CURSOR_NONE,
712                                  button_event->time);
713         else
714                 xcb_ungrab_pointer(c->conn, button_event->time);
715
716         if (!c->has_xkb)
717                 update_xkb_state_from_core(c, button_event->state);
718
719         switch (button_event->detail) {
720         default:
721                 button = button_event->detail + BTN_LEFT - 1;
722                 break;
723         case 2:
724                 button = BTN_MIDDLE;
725                 break;
726         case 3:
727                 button = BTN_RIGHT;
728                 break;
729         case 4:
730                 /* Axis are measured in pixels, but the xcb events are discrete
731                  * steps. Therefore move the axis by some pixels every step. */
732                 if (state)
733                         notify_axis(&c->core_seat,
734                                     weston_compositor_get_time(),
735                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
736                                     -DEFAULT_AXIS_STEP_DISTANCE);
737                 return;
738         case 5:
739                 if (state)
740                         notify_axis(&c->core_seat,
741                                     weston_compositor_get_time(),
742                                     WL_POINTER_AXIS_VERTICAL_SCROLL,
743                                     DEFAULT_AXIS_STEP_DISTANCE);
744                 return;
745         case 6:
746                 if (state)
747                         notify_axis(&c->core_seat,
748                                     weston_compositor_get_time(),
749                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
750                                     -DEFAULT_AXIS_STEP_DISTANCE);
751                 return;
752         case 7:
753                 if (state)
754                         notify_axis(&c->core_seat,
755                                     weston_compositor_get_time(),
756                                     WL_POINTER_AXIS_HORIZONTAL_SCROLL,
757                                     DEFAULT_AXIS_STEP_DISTANCE);
758                 return;
759         }
760
761         notify_button(&c->core_seat,
762                       weston_compositor_get_time(), button,
763                       state ? WL_POINTER_BUTTON_STATE_PRESSED :
764                               WL_POINTER_BUTTON_STATE_RELEASED);
765 }
766
767 static void
768 x11_output_transform_coordinate(struct x11_output *x11_output,
769                                                 wl_fixed_t *x, wl_fixed_t *y)
770 {
771         struct weston_output *output = &x11_output->base;
772         wl_fixed_t tx, ty;
773         wl_fixed_t width = wl_fixed_from_int(output->width - 1);
774         wl_fixed_t height = wl_fixed_from_int(output->height - 1);
775
776         switch(output->transform) {
777         case WL_OUTPUT_TRANSFORM_NORMAL:
778         default:
779                 tx = *x;
780                 ty = *y;
781                 break;
782         case WL_OUTPUT_TRANSFORM_90:
783                 tx = *y;
784                 ty = height - *x;
785                 break;
786         case WL_OUTPUT_TRANSFORM_180:
787                 tx = width - *x;
788                 ty = height - *y;
789                 break;
790         case WL_OUTPUT_TRANSFORM_270:
791                 tx = width - *y;
792                 ty = *x;
793                 break;
794         case WL_OUTPUT_TRANSFORM_FLIPPED:
795                 tx = width - *x;
796                 ty = *y;
797                 break;
798         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
799                 tx = width - *y;
800                 ty = height - *x;
801                 break;
802         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
803                 tx = *x;
804                 ty = height - *y;
805                 break;
806         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
807                 tx = *y;
808                 ty = *x;
809                 break;
810         }
811
812         tx += wl_fixed_from_int(output->x);
813         ty += wl_fixed_from_int(output->y);
814
815         *x = tx;
816         *y = ty;
817 }
818
819 static void
820 x11_compositor_deliver_motion_event(struct x11_compositor *c,
821                                         xcb_generic_event_t *event)
822 {
823         struct x11_output *output;
824         wl_fixed_t x, y;
825         xcb_motion_notify_event_t *motion_notify =
826                         (xcb_motion_notify_event_t *) event;
827
828         if (!c->has_xkb)
829                 update_xkb_state_from_core(c, motion_notify->state);
830         output = x11_compositor_find_output(c, motion_notify->event);
831         x = wl_fixed_from_int(motion_notify->event_x);
832         y = wl_fixed_from_int(motion_notify->event_y);
833         x11_output_transform_coordinate(output, &x, &y);
834
835         notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
836 }
837
838 static void
839 x11_compositor_deliver_enter_event(struct x11_compositor *c,
840                                         xcb_generic_event_t *event)
841 {
842         struct x11_output *output;
843         wl_fixed_t x, y;
844
845         xcb_enter_notify_event_t *enter_notify =
846                         (xcb_enter_notify_event_t *) event;
847         if (enter_notify->state >= Button1Mask)
848                 return;
849         if (!c->has_xkb)
850                 update_xkb_state_from_core(c, enter_notify->state);
851         output = x11_compositor_find_output(c, enter_notify->event);
852         x = wl_fixed_from_int(enter_notify->event_x);
853         y = wl_fixed_from_int(enter_notify->event_y);
854         x11_output_transform_coordinate(output, &x, &y);
855
856         notify_pointer_focus(&c->core_seat, &output->base, x, y);
857 }
858
859 static int
860 x11_compositor_next_event(struct x11_compositor *c,
861                           xcb_generic_event_t **event, uint32_t mask)
862 {
863         if (mask & WL_EVENT_READABLE) {
864                 *event = xcb_poll_for_event(c->conn);
865         } else {
866 #ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
867                 *event = xcb_poll_for_queued_event(c->conn);
868 #else
869                 *event = xcb_poll_for_event(c->conn);
870 #endif
871         }
872
873         return *event != NULL;
874 }
875
876 static int
877 x11_compositor_handle_event(int fd, uint32_t mask, void *data)
878 {
879         struct x11_compositor *c = data;
880         struct x11_output *output;
881         xcb_generic_event_t *event, *prev;
882         xcb_client_message_event_t *client_message;
883         xcb_enter_notify_event_t *enter_notify;
884         xcb_key_press_event_t *key_press, *key_release;
885         xcb_keymap_notify_event_t *keymap_notify;
886         xcb_focus_in_event_t *focus_in;
887         xcb_expose_event_t *expose;
888         xcb_atom_t atom;
889         uint32_t *k;
890         uint32_t i, set;
891         uint8_t response_type;
892         int count;
893
894         prev = NULL;
895         count = 0;
896         while (x11_compositor_next_event(c, &event, mask)) {
897                 response_type = event->response_type & ~0x80;
898
899                 switch (prev ? prev->response_type & ~0x80 : 0x80) {
900                 case XCB_KEY_RELEASE:
901                         /* Suppress key repeat events; this is only used if we
902                          * don't have XCB XKB support. */
903                         key_release = (xcb_key_press_event_t *) prev;
904                         key_press = (xcb_key_press_event_t *) event;
905                         if (response_type == XCB_KEY_PRESS &&
906                             key_release->time == key_press->time &&
907                             key_release->detail == key_press->detail) {
908                                 /* Don't deliver the held key release
909                                  * event or the new key press event. */
910                                 free(event);
911                                 free(prev);
912                                 prev = NULL;
913                                 continue;
914                         } else {
915                                 /* Deliver the held key release now
916                                  * and fall through and handle the new
917                                  * event below. */
918                                 update_xkb_state_from_core(c, key_release->state);
919                                 notify_key(&c->core_seat,
920                                            weston_compositor_get_time(),
921                                            key_release->detail - 8,
922                                            WL_KEYBOARD_KEY_STATE_RELEASED,
923                                            STATE_UPDATE_AUTOMATIC);
924                                 free(prev);
925                                 prev = NULL;
926                                 break;
927                         }
928
929                 case XCB_FOCUS_IN:
930                         assert(response_type == XCB_KEYMAP_NOTIFY);
931                         keymap_notify = (xcb_keymap_notify_event_t *) event;
932                         c->keys.size = 0;
933                         for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
934                                 set = keymap_notify->keys[i >> 3] &
935                                         (1 << (i & 7));
936                                 if (set) {
937                                         k = wl_array_add(&c->keys, sizeof *k);
938                                         *k = i;
939                                 }
940                         }
941
942                         /* Unfortunately the state only comes with the enter
943                          * event, rather than with the focus event.  I'm not
944                          * sure of the exact semantics around it and whether
945                          * we can ensure that we get both? */
946                         notify_keyboard_focus_in(&c->core_seat, &c->keys,
947                                                  STATE_UPDATE_AUTOMATIC);
948
949                         free(prev);
950                         prev = NULL;
951                         break;
952
953                 default:
954                         /* No previous event held */
955                         break;
956                 }
957
958                 switch (response_type) {
959                 case XCB_KEY_PRESS:
960                         key_press = (xcb_key_press_event_t *) event;
961                         if (!c->has_xkb)
962                                 update_xkb_state_from_core(c, key_press->state);
963                         notify_key(&c->core_seat,
964                                    weston_compositor_get_time(),
965                                    key_press->detail - 8,
966                                    WL_KEYBOARD_KEY_STATE_PRESSED,
967                                    c->has_xkb ? STATE_UPDATE_NONE :
968                                                 STATE_UPDATE_AUTOMATIC);
969                         break;
970                 case XCB_KEY_RELEASE:
971                         /* If we don't have XKB, we need to use the lame
972                          * autorepeat detection above. */
973                         if (!c->has_xkb) {
974                                 prev = event;
975                                 break;
976                         }
977                         key_release = (xcb_key_press_event_t *) event;
978                         notify_key(&c->core_seat,
979                                    weston_compositor_get_time(),
980                                    key_release->detail - 8,
981                                    WL_KEYBOARD_KEY_STATE_RELEASED,
982                                    STATE_UPDATE_NONE);
983                         break;
984                 case XCB_BUTTON_PRESS:
985                         x11_compositor_deliver_button_event(c, event, 1);
986                         break;
987                 case XCB_BUTTON_RELEASE:
988                         x11_compositor_deliver_button_event(c, event, 0);
989                         break;
990                 case XCB_MOTION_NOTIFY:
991                         x11_compositor_deliver_motion_event(c, event);
992                         break;
993
994                 case XCB_EXPOSE:
995                         expose = (xcb_expose_event_t *) event;
996                         output = x11_compositor_find_output(c, expose->window);
997                         weston_output_schedule_repaint(&output->base);
998                         break;
999
1000                 case XCB_ENTER_NOTIFY:
1001                         x11_compositor_deliver_enter_event(c, event);
1002                         break;
1003
1004                 case XCB_LEAVE_NOTIFY:
1005                         enter_notify = (xcb_enter_notify_event_t *) event;
1006                         if (enter_notify->state >= Button1Mask)
1007                                 break;
1008                         if (!c->has_xkb)
1009                                 update_xkb_state_from_core(c, enter_notify->state);
1010                         notify_pointer_focus(&c->core_seat, NULL, 0, 0);
1011                         break;
1012
1013                 case XCB_CLIENT_MESSAGE:
1014                         client_message = (xcb_client_message_event_t *) event;
1015                         atom = client_message->data.data32[0];
1016                         if (atom == c->atom.wm_delete_window)
1017                                 wl_display_terminate(c->base.wl_display);
1018                         break;
1019
1020                 case XCB_FOCUS_IN:
1021                         focus_in = (xcb_focus_in_event_t *) event;
1022                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1023                                 break;
1024
1025                         prev = event;
1026                         break;
1027
1028                 case XCB_FOCUS_OUT:
1029                         focus_in = (xcb_focus_in_event_t *) event;
1030                         if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1031                             focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
1032                                 break;
1033                         notify_keyboard_focus_out(&c->core_seat);
1034                         break;
1035
1036                 default:
1037                         break;
1038                 }
1039
1040 #ifdef HAVE_XCB_XKB
1041                 if (c->has_xkb &&
1042                     response_type == c->xkb_event_base) {
1043                         xcb_xkb_state_notify_event_t *state =
1044                                 (xcb_xkb_state_notify_event_t *) event;
1045                         if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1046                                 update_xkb_state(c, state);
1047                 }
1048 #endif
1049
1050                 count++;
1051                 if (prev != event)
1052                         free (event);
1053         }
1054
1055         switch (prev ? prev->response_type & ~0x80 : 0x80) {
1056         case XCB_KEY_RELEASE:
1057                 key_release = (xcb_key_press_event_t *) prev;
1058                 update_xkb_state_from_core(c, key_release->state);
1059                 notify_key(&c->core_seat,
1060                            weston_compositor_get_time(),
1061                            key_release->detail - 8,
1062                            WL_KEYBOARD_KEY_STATE_RELEASED,
1063                            STATE_UPDATE_AUTOMATIC);
1064                 free(prev);
1065                 prev = NULL;
1066                 break;
1067         default:
1068                 break;
1069         }
1070
1071         return count;
1072 }
1073
1074 #define F(field) offsetof(struct x11_compositor, field)
1075
1076 static void
1077 x11_compositor_get_resources(struct x11_compositor *c)
1078 {
1079         static const struct { const char *name; int offset; } atoms[] = {
1080                 { "WM_PROTOCOLS",       F(atom.wm_protocols) },
1081                 { "WM_NORMAL_HINTS",    F(atom.wm_normal_hints) },
1082                 { "WM_SIZE_HINTS",      F(atom.wm_size_hints) },
1083                 { "WM_DELETE_WINDOW",   F(atom.wm_delete_window) },
1084                 { "WM_CLASS",           F(atom.wm_class) },
1085                 { "_NET_WM_NAME",       F(atom.net_wm_name) },
1086                 { "_NET_WM_ICON",       F(atom.net_wm_icon) },
1087                 { "_NET_WM_STATE",      F(atom.net_wm_state) },
1088                 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
1089                 { "STRING",             F(atom.string) },
1090                 { "UTF8_STRING",        F(atom.utf8_string) },
1091                 { "CARDINAL",           F(atom.cardinal) },
1092                 { "_XKB_RULES_NAMES",   F(atom.xkb_names) },
1093         };
1094
1095         xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
1096         xcb_intern_atom_reply_t *reply;
1097         xcb_pixmap_t pixmap;
1098         xcb_gc_t gc;
1099         unsigned int i;
1100         uint8_t data[] = { 0, 0, 0, 0 };
1101
1102         for (i = 0; i < ARRAY_LENGTH(atoms); i++)
1103                 cookies[i] = xcb_intern_atom (c->conn, 0,
1104                                               strlen(atoms[i].name),
1105                                               atoms[i].name);
1106
1107         for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
1108                 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1109                 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1110                 free(reply);
1111         }
1112
1113         pixmap = xcb_generate_id(c->conn);
1114         gc = xcb_generate_id(c->conn);
1115         xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1116         xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1117         xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1118                       pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1119         c->null_cursor = xcb_generate_id(c->conn);
1120         xcb_create_cursor (c->conn, c->null_cursor,
1121                            pixmap, pixmap, 0, 0, 0,  0, 0, 0,  1, 1);
1122         xcb_free_gc(c->conn, gc);
1123         xcb_free_pixmap(c->conn, pixmap);
1124 }
1125
1126 static void
1127 x11_restore(struct weston_compositor *ec)
1128 {
1129 }
1130
1131 static void
1132 x11_free_configured_output(struct x11_configured_output *output)
1133 {
1134         free(output->name);
1135         free(output);
1136 }
1137
1138 static void
1139 x11_destroy(struct weston_compositor *ec)
1140 {
1141         struct x11_compositor *compositor = (struct x11_compositor *)ec;
1142         struct x11_configured_output *o, *n;
1143
1144         wl_list_for_each_safe(o, n, &configured_output_list, link)
1145                 x11_free_configured_output(o);
1146
1147         wl_event_source_remove(compositor->xcb_source);
1148         x11_input_destroy(compositor);
1149
1150         weston_compositor_shutdown(ec); /* destroys outputs, too */
1151
1152         x11_compositor_fini_egl(compositor);
1153
1154         XCloseDisplay(compositor->dpy);
1155         free(ec);
1156 }
1157
1158 static struct weston_compositor *
1159 x11_compositor_create(struct wl_display *display,
1160                       int fullscreen,
1161                       int no_input,
1162                       int argc, char *argv[], const char *config_file)
1163 {
1164         struct x11_compositor *c;
1165         struct x11_configured_output *o;
1166         struct x11_output *output;
1167         xcb_screen_iterator_t s;
1168         int i, x = 0, output_count = 0;
1169         int width, height, count;
1170
1171         weston_log("initializing x11 backend\n");
1172
1173         c = malloc(sizeof *c);
1174         if (c == NULL)
1175                 return NULL;
1176
1177         memset(c, 0, sizeof *c);
1178
1179         if (weston_compositor_init(&c->base, display, argc, argv,
1180                                    config_file) < 0)
1181                 goto err_free;
1182
1183         c->dpy = XOpenDisplay(NULL);
1184         if (c->dpy == NULL)
1185                 goto err_free;
1186
1187         c->conn = XGetXCBConnection(c->dpy);
1188         XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1189
1190         if (xcb_connection_has_error(c->conn))
1191                 goto err_xdisplay;
1192
1193         s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1194         c->screen = s.data;
1195         wl_array_init(&c->keys);
1196
1197         x11_compositor_get_resources(c);
1198
1199         c->base.wl_display = display;
1200         if (x11_compositor_init_egl(c) < 0)
1201                 goto err_xdisplay;
1202
1203         c->base.destroy = x11_destroy;
1204         c->base.restore = x11_restore;
1205
1206         if (x11_input_create(c, no_input) < 0)
1207                 goto err_egl;
1208
1209         width = option_width ? option_width : 1024;
1210         height = option_height ? option_height : 640;
1211         count = option_count ? option_count : 1;
1212
1213         wl_list_for_each(o, &configured_output_list, link) {
1214                 output = x11_compositor_create_output(c, x, 0,
1215                                                       option_width ? width :
1216                                                       o->width,
1217                                                       option_height ? height :
1218                                                       o->height,
1219                                                       fullscreen, no_input,
1220                                                       o->name, o->transform);
1221                 if (output == NULL)
1222                         goto err_x11_input;
1223
1224                 x = pixman_region32_extents(&output->base.region)->x2;
1225
1226                 output_count++;
1227                 if (option_count && output_count >= option_count)
1228                         break;
1229         }
1230
1231         for (i = output_count; i < count; i++) {
1232                 output = x11_compositor_create_output(c, x, 0, width, height,
1233                                                       fullscreen, no_input, NULL,
1234                                                       WL_OUTPUT_TRANSFORM_NORMAL);
1235                 if (output == NULL)
1236                         goto err_x11_input;
1237                 x = pixman_region32_extents(&output->base.region)->x2;
1238         }
1239
1240         if (gles2_renderer_init(&c->base) < 0)
1241                 goto err_egl;
1242
1243         c->xcb_source =
1244                 wl_event_loop_add_fd(c->base.input_loop,
1245                                      xcb_get_file_descriptor(c->conn),
1246                                      WL_EVENT_READABLE,
1247                                      x11_compositor_handle_event, c);
1248         wl_event_source_check(c->xcb_source);
1249
1250         return &c->base;
1251
1252 err_x11_input:
1253         x11_input_destroy(c);
1254 err_egl:
1255         x11_compositor_fini_egl(c);
1256 err_xdisplay:
1257         XCloseDisplay(c->dpy);
1258 err_free:
1259         free(c);
1260         return NULL;
1261 }
1262
1263 static void
1264 x11_output_set_transform(struct x11_configured_output *output)
1265 {
1266         if (!output_transform) {
1267                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1268                 return;
1269         }
1270
1271         if (!strcmp(output_transform, "normal"))
1272                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1273         else if (!strcmp(output_transform, "90"))
1274                 output->transform = WL_OUTPUT_TRANSFORM_90;
1275         else if (!strcmp(output_transform, "180"))
1276                 output->transform = WL_OUTPUT_TRANSFORM_180;
1277         else if (!strcmp(output_transform, "270"))
1278                 output->transform = WL_OUTPUT_TRANSFORM_270;
1279         else if (!strcmp(output_transform, "flipped"))
1280                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1281         else if (!strcmp(output_transform, "flipped-90"))
1282                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1283         else if (!strcmp(output_transform, "flipped-180"))
1284                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1285         else if (!strcmp(output_transform, "flipped-270"))
1286                 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1287         else {
1288                 weston_log("Invalid transform \"%s\" for output %s\n",
1289                                                 output_transform, output_name);
1290                 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1291         }
1292 }
1293
1294 static void
1295 output_section_done(void *data)
1296 {
1297         struct x11_configured_output *output;
1298
1299         output = malloc(sizeof *output);
1300
1301         if (!output || !output_name || (output_name[0] != 'X') ||
1302                                 (!output_mode && !output_transform)) {
1303                 if (output_name)
1304                         free(output_name);
1305                 output_name = NULL;
1306                 free(output);
1307                 goto err_free;
1308         }
1309
1310         output->name = output_name;
1311
1312         if (output_mode) {
1313                 if (sscanf(output_mode, "%dx%d", &output->width,
1314                                                 &output->height) != 2) {
1315                         weston_log("Invalid mode \"%s\" for output %s\n",
1316                                                         output_mode, output_name);
1317                         x11_free_configured_output(output);
1318                         goto err_free;
1319                 }
1320         } else {
1321                 output->width = 1024;
1322                 output->height = 640;
1323         }
1324
1325         x11_output_set_transform(output);
1326
1327         wl_list_insert(configured_output_list.prev, &output->link);
1328
1329 err_free:
1330         if (output_mode)
1331                 free(output_mode);
1332         if (output_transform)
1333                 free(output_transform);
1334         output_mode = NULL;
1335         output_transform = NULL;
1336 }
1337
1338 WL_EXPORT struct weston_compositor *
1339 backend_init(struct wl_display *display, int argc, char *argv[],
1340              const char *config_file)
1341 {
1342         int fullscreen = 0;
1343         int no_input = 0;
1344
1345         const struct weston_option x11_options[] = {
1346                 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1347                 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
1348                 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
1349                 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
1350                 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
1351         };
1352
1353         parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
1354
1355         wl_list_init(&configured_output_list);
1356
1357         const struct config_key x11_config_keys[] = {
1358                 { "name", CONFIG_KEY_STRING, &output_name },
1359                 { "mode", CONFIG_KEY_STRING, &output_mode },
1360                 { "transform", CONFIG_KEY_STRING, &output_transform },
1361         };
1362
1363         const struct config_section config_section[] = {
1364                 { "output", x11_config_keys,
1365                 ARRAY_LENGTH(x11_config_keys), output_section_done },
1366         };
1367
1368         parse_config_file(config_file, config_section,
1369                                 ARRAY_LENGTH(config_section), NULL);
1370
1371         return x11_compositor_create(display,
1372                                      fullscreen,
1373                                      no_input,
1374                                      argc, argv, config_file);
1375 }