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