blob: 2974d47813cd636e3018c44b692c0291d3be08ae [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2010-2011 Intel Corporation
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * 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.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * 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.
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040022 */
23
Chia-I Wu1b6c0ed2010-10-29 15:20:18 +080024#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040028#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040029#include <stdlib.h>
30#include <string.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040035#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040036
37#include <xcb/xcb.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010038#ifdef HAVE_XCB_XKB
39#include <xcb/xkb.h>
40#endif
41
Benjamin Franzke3b288af2011-02-20 19:58:42 +010042#include <X11/Xlib.h>
43#include <X11/Xlib-xcb.h>
44
Daniel Stone576cd152012-06-01 12:14:02 +010045#include <xkbcommon/xkbcommon.h>
46
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040047#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050#include "compositor.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040051#include "../shared/config-parser.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040052
Scott Moreau248aaec2012-08-03 14:19:52 -060053static char *output_name;
54static char *output_mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -060055static char *output_transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060056static int option_width;
57static int option_height;
58static int option_count;
59static struct wl_list configured_output_list;
60
61struct x11_configured_output {
62 char *name;
63 int width, height;
Scott Moreau1bad5db2012-08-18 01:04:05 -060064 uint32_t transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060065 struct wl_list link;
66};
67
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040068struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040070
Benjamin Franzke3b288af2011-02-20 19:58:42 +010071 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040072 xcb_connection_t *conn;
73 xcb_screen_t *screen;
74 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050075 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040076 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010077 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010078 unsigned int has_xkb;
79 uint8_t xkb_event_base;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040080
81 /* We could map multi-pointer X to multiple wayland seats, but
82 * for now we only support core X input. */
83 struct weston_seat core_seat;
84
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085 struct {
86 xcb_atom_t wm_protocols;
87 xcb_atom_t wm_normal_hints;
88 xcb_atom_t wm_size_hints;
89 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050090 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050092 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040093 xcb_atom_t net_wm_state;
94 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050095 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050097 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +010098 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099 } atom;
100};
101
102struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500103 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400104
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400105 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400107 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108};
109
Daniel Stone576cd152012-06-01 12:14:02 +0100110static struct xkb_keymap *
111x11_compositor_get_keymap(struct x11_compositor *c)
112{
113 xcb_get_property_cookie_t cookie;
114 xcb_get_property_reply_t *reply;
115 xcb_generic_error_t *error;
116 struct xkb_rule_names names;
117 struct xkb_keymap *ret;
118 const char *value_all, *value_part;
119 int length_all, length_part;
120
121 memset(&names, 0, sizeof(names));
122
123 cookie = xcb_get_property(c->conn, 0, c->screen->root,
124 c->atom.xkb_names, c->atom.string, 0, 1024);
125 reply = xcb_get_property_reply(c->conn, cookie, &error);
126 if (reply == NULL)
127 return NULL;
128
129 value_all = xcb_get_property_value(reply);
130 length_all = xcb_get_property_value_length(reply);
131 value_part = value_all;
132
133#define copy_prop_value(to) \
134 length_part = strlen(value_part); \
135 if (value_part + length_part < (value_all + length_all) && \
136 length_part > 0) \
137 names.to = value_part; \
138 value_part += length_part + 1;
139
140 copy_prop_value(rules);
141 copy_prop_value(model);
142 copy_prop_value(layout);
143 copy_prop_value(variant);
144 copy_prop_value(options);
145#undef copy_prop_value
146
147 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
148
149 free(reply);
150 return ret;
151}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400152
Daniel Stone62b33b62012-06-22 13:21:35 +0100153static void
154x11_compositor_setup_xkb(struct x11_compositor *c)
155{
156#ifndef HAVE_XCB_XKB
157 weston_log("XCB-XKB not available during build\n");
158 c->has_xkb = 0;
159 c->xkb_event_base = 0;
160 return;
161#else
162 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100163 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100164 xcb_void_cookie_t select;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100165 xcb_xkb_per_client_flags_cookie_t pcf;
166 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100167
168 c->has_xkb = 0;
169 c->xkb_event_base = 0;
170
171 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
172 if (!ext) {
173 weston_log("XKB extension not available on host X11 server\n");
174 return;
175 }
176 c->xkb_event_base = ext->first_event;
177
Daniel Stonee2faa122012-06-22 13:21:39 +0100178 select = xcb_xkb_select_events(c->conn,
179 XCB_XKB_ID_USE_CORE_KBD,
180 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
181 0,
182 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
183 0,
184 0,
185 NULL);
186 error = xcb_request_check(c->conn, select);
187 if (error) {
188 weston_log("error: failed to select for XKB state events\n");
189 return;
190 }
191
Daniel Stone3ee91e12012-06-22 13:21:36 +0100192 pcf = xcb_xkb_per_client_flags(c->conn,
193 XCB_XKB_ID_USE_CORE_KBD,
194 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
195 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
196 0,
197 0,
198 0);
199 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
200 free(pcf_reply);
201 if (error) {
202 weston_log("failed to set XKB per-client flags, not using "
203 "detectable repeat\n");
204 return;
205 }
206
Daniel Stone62b33b62012-06-22 13:21:35 +0100207 c->has_xkb = 1;
208#endif
209}
210
Darxus55973f22010-11-22 21:24:39 -0500211static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300212x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400213{
Daniel Stone576cd152012-06-01 12:14:02 +0100214 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400215
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400216 weston_seat_init(&c->core_seat, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300217
218 if (no_input)
219 return 0;
220
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400221 weston_seat_init_pointer(&c->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100222
Daniel Stone62b33b62012-06-22 13:21:35 +0100223 x11_compositor_setup_xkb(c);
224
Daniel Stone576cd152012-06-01 12:14:02 +0100225 keymap = x11_compositor_get_keymap(c);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400226 weston_seat_init_keyboard(&c->core_seat, keymap);
Daniel Stone576cd152012-06-01 12:14:02 +0100227 if (keymap)
228 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400229
Darxus55973f22010-11-22 21:24:39 -0500230 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400231}
232
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200233static void
234x11_input_destroy(struct x11_compositor *compositor)
235{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400236 weston_seat_release(&compositor->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200237}
238
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400239static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400240x11_compositor_init_egl(struct x11_compositor *c)
241{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400242 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100243 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100244 EGLint config_attribs[] = {
245 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
246 EGL_RED_SIZE, 1,
247 EGL_GREEN_SIZE, 1,
248 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100249 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
250 EGL_NONE
251 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400252
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400253 c->base.egl_display = eglGetDisplay(c->dpy);
254 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200255 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400256 return -1;
257 }
258
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400259 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400261 return -1;
262 }
263
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400264 if (!eglChooseConfig(c->base.egl_display, config_attribs,
265 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200266 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100267 return -1;
268 }
Darxus55973f22010-11-22 21:24:39 -0500269
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400270 return 0;
271}
272
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200273static void
274x11_compositor_fini_egl(struct x11_compositor *compositor)
275{
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400276 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200277 EGL_NO_SURFACE, EGL_NO_SURFACE,
278 EGL_NO_CONTEXT);
279
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400280 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200281 eglReleaseThread();
282}
283
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500284static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500285x11_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400286 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500287{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500288 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400289 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500290
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400291 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500292
293 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500294}
295
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100296static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400297finish_frame_handler(void *data)
298{
299 struct x11_output *output = data;
300 uint32_t msec;
301 struct timeval tv;
302
303 gettimeofday(&tv, NULL);
304 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400306
307 return 1;
308}
309
Matt Roper361d2ad2011-08-29 13:52:23 -0700310static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500311x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700312{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200313 struct x11_output *output = (struct x11_output *)output_base;
314 struct x11_compositor *compositor =
315 (struct x11_compositor *)output->base.compositor;
316
317 wl_list_remove(&output->base.link);
318 wl_event_source_remove(output->finish_frame_timer);
319
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400320 eglDestroySurface(compositor->base.egl_display,
321 output->base.egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200322
323 xcb_destroy_window(compositor->conn, output->window);
324
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500325 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200326
327 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700328}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200329
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400330static void
331x11_output_set_wm_protocols(struct x11_output *output)
332{
333 xcb_atom_t list[1];
334 struct x11_compositor *c =
335 (struct x11_compositor *) output->base.compositor;
336
337 list[0] = c->atom.wm_delete_window;
338 xcb_change_property (c->conn,
339 XCB_PROP_MODE_REPLACE,
340 output->window,
341 c->atom.wm_protocols,
342 XCB_ATOM_ATOM,
343 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500344 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400345 list);
346}
347
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400348static void
349x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
350{
351 xcb_client_message_event_t event;
352 struct x11_compositor *c =
353 (struct x11_compositor *) output->base.compositor;
354 xcb_screen_iterator_t iter;
355
356#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
357#define _NET_WM_STATE_ADD 1 /* add/set property */
358#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
359
360 memset(&event, 0, sizeof event);
361 event.response_type = XCB_CLIENT_MESSAGE;
362 event.format = 32;
363 event.window = output->window;
364 event.type = c->atom.net_wm_state;
365
366 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
367 event.data.data32[1] = state;
368 event.data.data32[2] = 0;
369 event.data.data32[3] = 0;
370 event.data.data32[4] = 0;
371
372 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
373 xcb_send_event(c->conn, 0, iter.data->root,
374 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
375 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
376 (void *) &event);
377}
378
379
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400380struct wm_normal_hints {
381 uint32_t flags;
382 uint32_t pad[4];
383 int32_t min_width, min_height;
384 int32_t max_width, max_height;
385 int32_t width_inc, height_inc;
386 int32_t min_aspect_x, min_aspect_y;
387 int32_t max_aspect_x, max_aspect_y;
388 int32_t base_width, base_height;
389 int32_t win_gravity;
390};
391
392#define WM_NORMAL_HINTS_MIN_SIZE 16
393#define WM_NORMAL_HINTS_MAX_SIZE 32
394
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500395static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400396x11_output_set_icon(struct x11_compositor *c,
397 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500398{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400399 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400400 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400401 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500402
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400403 image = load_image(filename);
404 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500405 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400406 width = pixman_image_get_width(image);
407 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500408 icon = malloc(width * height * 4 + 8);
409 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400410 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500411 return;
412 }
413
414 icon[0] = width;
415 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400416 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500417 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
418 c->atom.net_wm_icon, c->atom.cardinal, 32,
419 width * height + 2, icon);
420 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400421 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500422}
423
Scott Moreau1bad5db2012-08-18 01:04:05 -0600424static struct x11_output *
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700425x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300426 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600427 int no_input, char *configured_name,
428 uint32_t transform)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400429{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600430 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500431 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600432 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400433 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400434 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400435 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400436 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400437 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300438 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400439 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300440 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400441 0
442 };
443
Scott Moreau1bad5db2012-08-18 01:04:05 -0600444 if (configured_name)
445 sprintf(title, "%s - %s", name, configured_name);
446 else
447 strcpy(title, name);
448
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300449 if (!no_input)
450 values[0] |=
451 XCB_EVENT_MASK_KEY_PRESS |
452 XCB_EVENT_MASK_KEY_RELEASE |
453 XCB_EVENT_MASK_BUTTON_PRESS |
454 XCB_EVENT_MASK_BUTTON_RELEASE |
455 XCB_EVENT_MASK_POINTER_MOTION |
456 XCB_EVENT_MASK_ENTER_WINDOW |
457 XCB_EVENT_MASK_LEAVE_WINDOW |
458 XCB_EVENT_MASK_KEYMAP_STATE |
459 XCB_EVENT_MASK_FOCUS_CHANGE;
460
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400461 output = malloc(sizeof *output);
462 if (output == NULL)
Scott Moreau1bad5db2012-08-18 01:04:05 -0600463 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400464
465 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400466
467 output->mode.flags =
468 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
469 output->mode.width = width;
470 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400471 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400472 wl_list_init(&output->base.mode_list);
473 wl_list_insert(&output->base.mode_list, &output->mode.link);
474
475 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400476 output->base.make = "xwayland";
477 output->base.model = "none";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600478 weston_output_init(&output->base, &c->base,
479 x, y, width, height, transform);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400480
481 values[1] = c->null_cursor;
482 output->window = xcb_generate_id(c->conn);
483 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
484 xcb_create_window(c->conn,
485 XCB_COPY_FROM_PARENT,
486 output->window,
487 iter.data->root,
488 0, 0,
489 width, height,
490 0,
491 XCB_WINDOW_CLASS_INPUT_OUTPUT,
492 iter.data->root_visual,
493 mask, values);
494
495 /* Don't resize me. */
496 memset(&normal_hints, 0, sizeof normal_hints);
497 normal_hints.flags =
498 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
499 normal_hints.min_width = width;
500 normal_hints.min_height = height;
501 normal_hints.max_width = width;
502 normal_hints.max_height = height;
503 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
504 c->atom.wm_normal_hints,
505 c->atom.wm_size_hints, 32,
506 sizeof normal_hints / 4,
507 (uint8_t *) &normal_hints);
508
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400509 /* Set window name. Don't bother with non-EWMH WMs. */
510 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
511 c->atom.net_wm_name, c->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600512 strlen(title), title);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500513 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
514 c->atom.wm_class, c->atom.string, 8,
515 sizeof class, class);
516
Kristian Høgsberg86000402012-01-03 23:24:14 -0500517 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500518
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500519 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400520
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400521 x11_output_set_wm_protocols(output);
522
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400523 if (fullscreen)
524 x11_output_change_state(output, 1,
525 c->atom.net_wm_state_fullscreen);
526
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400527 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400528 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100529 output->window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400530 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200531 weston_log("failed to create window surface\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600532 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400533 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400534
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400535 loop = wl_display_get_event_loop(c->base.wl_display);
536 output->finish_frame_timer =
537 wl_event_loop_add_timer(loop, finish_frame_handler, output);
538
Alex Wubd3354b2012-04-17 17:20:49 +0800539 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500540 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700541 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800542 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200543 output->base.set_backlight = NULL;
544 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800545 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100546
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400547 wl_list_insert(c->base.output_list.prev, &output->base.link);
548
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400549 weston_log("x11 output %dx%d, window id %d\n",
550 width, height, output->window);
551
Scott Moreau1bad5db2012-08-18 01:04:05 -0600552 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553}
554
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400555static struct x11_output *
556x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
557{
558 struct x11_output *output;
559
560 wl_list_for_each(output, &c->base.output_list, base.link) {
561 if (output->window == window)
562 return output;
563 }
564
565 return NULL;
566}
567
Daniel Stonee2faa122012-06-22 13:21:39 +0100568static uint32_t
569get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
570{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400571 struct weston_xkb_info *info = &c->core_seat.xkb_info;
Daniel Stonee2faa122012-06-22 13:21:39 +0100572 uint32_t ret = 0;
573
574 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
575 ret |= (1 << info->shift_mod);
576 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
577 ret |= (1 << info->caps_mod);
578 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
579 ret |= (1 << info->ctrl_mod);
580 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
581 ret |= (1 << info->alt_mod);
582 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
583 ret |= (1 << info->mod2_mod);
584 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
585 ret |= (1 << info->mod3_mod);
586 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
587 ret |= (1 << info->super_mod);
588 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
589 ret |= (1 << info->mod5_mod);
590
591 return ret;
592}
593
Daniel Stone154c34c2012-06-22 13:21:40 +0100594#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100595static void
596update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
597{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400598 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stonee2faa122012-06-22 13:21:39 +0100599 get_xkb_mod_mask(c, state->baseMods),
600 get_xkb_mod_mask(c, state->latchedMods),
601 get_xkb_mod_mask(c, state->lockedMods),
602 0,
603 0,
604 state->group);
605
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400606 notify_modifiers(&c->core_seat,
607 wl_display_next_serial(c->base.wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100608}
609#endif
610
Daniel Stone154c34c2012-06-22 13:21:40 +0100611/**
612 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
613 * the best we can do (given that XCB also lacks XI2 support), is to take
614 * the state from the core key events. Unfortunately that only gives us
615 * the effective (i.e. union of depressed/latched/locked) state, and we
616 * need the granularity.
617 *
618 * So we still update the state with every key event we see, but also use
619 * the state field from X11 events as a mask so we don't get any stuck
620 * modifiers.
621 */
622static void
623update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
624{
625 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400626 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +0100627
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400628 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +0100629 keyboard->modifiers.mods_depressed & mask,
630 keyboard->modifiers.mods_latched & mask,
631 keyboard->modifiers.mods_locked & mask,
632 0,
633 0,
634 (x11_mask >> 13) & 3);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400635 notify_modifiers(&c->core_seat,
Daniel Stone154c34c2012-06-22 13:21:40 +0100636 wl_display_next_serial(c->base.wl_display));
637}
638
Tiago Vignattia3a71622011-12-08 17:03:17 +0200639static void
640x11_compositor_deliver_button_event(struct x11_compositor *c,
641 xcb_generic_event_t *event, int state)
642{
643 xcb_button_press_event_t *button_event =
644 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100645 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200646
Daniel Stone154c34c2012-06-22 13:21:40 +0100647 if (!c->has_xkb)
648 update_xkb_state_from_core(c, button_event->state);
649
Tiago Vignattia3a71622011-12-08 17:03:17 +0200650 switch (button_event->detail) {
651 default:
652 button = button_event->detail + BTN_LEFT - 1;
653 break;
654 case 2:
655 button = BTN_MIDDLE;
656 break;
657 case 3:
658 button = BTN_RIGHT;
659 break;
660 case 4:
Scott Moreau210d0792012-03-22 10:47:01 -0600661 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400662 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600663 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100664 WL_POINTER_AXIS_VERTICAL_SCROLL,
665 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600666 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200667 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600668 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400669 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600670 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100671 WL_POINTER_AXIS_VERTICAL_SCROLL,
672 wl_fixed_from_int(-1));
Scott Moreau210d0792012-03-22 10:47:01 -0600673 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200674 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600675 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400676 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600677 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100678 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
679 wl_fixed_from_int(1));
Scott Moreau210d0792012-03-22 10:47:01 -0600680 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200681 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600682 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400683 notify_axis(&c->core_seat,
Scott Moreau210d0792012-03-22 10:47:01 -0600684 weston_compositor_get_time(),
Daniel Stone878f0b72012-05-30 16:31:57 +0100685 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
686 wl_fixed_from_int(-1));
Tiago Vignattia3a71622011-12-08 17:03:17 +0200687 return;
688 }
689
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400690 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100691 weston_compositor_get_time(), button,
692 state ? WL_POINTER_BUTTON_STATE_PRESSED :
693 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200694}
695
Scott Moreau1bad5db2012-08-18 01:04:05 -0600696static void
697x11_output_transform_coordinate(struct x11_output *x11_output,
698 wl_fixed_t *x, wl_fixed_t *y)
699{
700 struct weston_output *output = &x11_output->base;
701 wl_fixed_t tx, ty;
702 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
703 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
704
705 switch(output->transform) {
706 case WL_OUTPUT_TRANSFORM_NORMAL:
707 default:
708 tx = *x;
709 ty = *y;
710 break;
711 case WL_OUTPUT_TRANSFORM_90:
712 tx = *y;
713 ty = height - *x;
714 break;
715 case WL_OUTPUT_TRANSFORM_180:
716 tx = width - *x;
717 ty = height - *y;
718 break;
719 case WL_OUTPUT_TRANSFORM_270:
720 tx = width - *y;
721 ty = *x;
722 break;
723 case WL_OUTPUT_TRANSFORM_FLIPPED:
724 tx = width - *x;
725 ty = *y;
726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
728 tx = width - *y;
729 ty = height - *x;
730 break;
731 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
732 tx = *x;
733 ty = height - *y;
734 break;
735 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
736 tx = *y;
737 ty = *x;
738 break;
739 }
740
741 tx += wl_fixed_from_int(output->x);
742 ty += wl_fixed_from_int(output->y);
743
744 *x = tx;
745 *y = ty;
746}
747
748static void
749x11_compositor_deliver_motion_event(struct x11_compositor *c,
750 xcb_generic_event_t *event)
751{
752 struct x11_output *output;
753 wl_fixed_t x, y;
754 xcb_motion_notify_event_t *motion_notify =
755 (xcb_motion_notify_event_t *) event;
756
757 if (!c->has_xkb)
758 update_xkb_state_from_core(c, motion_notify->state);
759 output = x11_compositor_find_output(c, motion_notify->event);
760 x = wl_fixed_from_int(motion_notify->event_x);
761 y = wl_fixed_from_int(motion_notify->event_y);
762 x11_output_transform_coordinate(output, &x, &y);
763
764 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
765}
766
767static void
768x11_compositor_deliver_enter_event(struct x11_compositor *c,
769 xcb_generic_event_t *event)
770{
771 struct x11_output *output;
772 wl_fixed_t x, y;
773
774 xcb_enter_notify_event_t *enter_notify =
775 (xcb_enter_notify_event_t *) event;
776 if (enter_notify->state >= Button1Mask)
777 return;
778 if (!c->has_xkb)
779 update_xkb_state_from_core(c, enter_notify->state);
780 output = x11_compositor_find_output(c, enter_notify->event);
781 x = wl_fixed_from_int(enter_notify->event_x);
782 y = wl_fixed_from_int(enter_notify->event_y);
783 x11_output_transform_coordinate(output, &x, &y);
784
785 notify_pointer_focus(&c->core_seat, &output->base, x, y);
786}
787
Kristian Høgsbergee724822011-04-21 14:51:44 -0400788static int
789x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400790 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400791{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400792 if (mask & WL_EVENT_READABLE) {
793 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400794 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400795#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400796 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400797#else
798 *event = xcb_poll_for_event(c->conn);
799#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400800 }
801
802 return *event != NULL;
803}
804
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400805static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400806x11_compositor_handle_event(int fd, uint32_t mask, void *data)
807{
808 struct x11_compositor *c = data;
809 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400810 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400811 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500812 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400813 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500814 xcb_keymap_notify_event_t *keymap_notify;
815 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400816 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400817 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500818 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400819 uint32_t i, set;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400820 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400821
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400822 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400823 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400824 while (x11_compositor_next_event(c, &event, mask)) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400825 switch (prev ? prev->response_type & ~0x80 : 0x80) {
826 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100827 /* Suppress key repeat events; this is only used if we
828 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400829 key_release = (xcb_key_press_event_t *) prev;
830 key_press = (xcb_key_press_event_t *) event;
831 if ((event->response_type & ~0x80) == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700832 key_release->time == key_press->time &&
833 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400834 /* Don't deliver the held key release
835 * event or the new key press event. */
836 free(event);
837 free(prev);
838 prev = NULL;
839 continue;
840 } else {
841 /* Deliver the held key release now
842 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400843 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100844 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400845 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500846 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100847 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100848 WL_KEYBOARD_KEY_STATE_RELEASED,
849 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400850 free(prev);
851 prev = NULL;
852 break;
853 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400854
855 case XCB_FOCUS_IN:
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400856 /* assert event is keymap_notify */
857 focus_in = (xcb_focus_in_event_t *) prev;
858 keymap_notify = (xcb_keymap_notify_event_t *) event;
859 c->keys.size = 0;
860 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
861 set = keymap_notify->keys[i >> 3] &
862 (1 << (i & 7));
863 if (set) {
864 k = wl_array_add(&c->keys, sizeof *k);
865 *k = i;
866 }
867 }
868
869 output = x11_compositor_find_output(c, focus_in->event);
Daniel Stoned6da09e2012-06-22 13:21:29 +0100870 /* Unfortunately the state only comes with the enter
871 * event, rather than with the focus event. I'm not
872 * sure of the exact semantics around it and whether
873 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400874 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100875 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400876
877 free(prev);
878 prev = NULL;
879 break;
880
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400881 default:
882 /* No previous event held */
883 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400884 }
885
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400886 switch (event->response_type & ~0x80) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400887 case XCB_KEY_PRESS:
888 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100889 if (!c->has_xkb)
890 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400891 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500892 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100893 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100894 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100895 c->has_xkb ? STATE_UPDATE_NONE :
896 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400897 break;
898 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100899 /* If we don't have XKB, we need to use the lame
900 * autorepeat detection above. */
901 if (!c->has_xkb) {
902 prev = event;
903 break;
904 }
905 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400906 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100907 weston_compositor_get_time(),
908 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100909 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100910 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400911 break;
912 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200913 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400914 break;
915 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200916 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400917 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400918 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600919 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400920 break;
921
922 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400923 expose = (xcb_expose_event_t *) event;
924 output = x11_compositor_find_output(c, expose->window);
925 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400926 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400927
928 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600929 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400930 break;
931
932 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500933 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500934 if (enter_notify->state >= Button1Mask)
935 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100936 if (!c->has_xkb)
937 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700938 output = x11_compositor_find_output(c, enter_notify->event);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400939 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400940 break;
941
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400942 case XCB_CLIENT_MESSAGE:
943 client_message = (xcb_client_message_event_t *) event;
944 atom = client_message->data.data32[0];
945 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500946 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400947 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400948
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500949 case XCB_FOCUS_IN:
950 focus_in = (xcb_focus_in_event_t *) event;
951 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
952 break;
953
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400954 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500955 break;
956
957 case XCB_FOCUS_OUT:
958 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500959 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
960 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500961 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400962 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500963 break;
964
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500965 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400966 break;
967 }
968
Daniel Stonee2faa122012-06-22 13:21:39 +0100969#ifdef HAVE_XCB_XKB
970 if (c->has_xkb &&
971 (event->response_type & ~0x80) == c->xkb_event_base) {
972 xcb_xkb_state_notify_event_t *state =
973 (xcb_xkb_state_notify_event_t *) event;
974 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
975 update_xkb_state(c, state);
976 }
977#endif
978
Bill Spitzakb715cec2012-06-05 17:08:23 -0400979 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400980 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400981 free (event);
982 }
983
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400984 switch (prev ? prev->response_type & ~0x80 : 0x80) {
985 case XCB_KEY_RELEASE:
986 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +0100987 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400988 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500989 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100990 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100991 WL_KEYBOARD_KEY_STATE_RELEASED,
992 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400993 free(prev);
994 prev = NULL;
995 break;
996 default:
997 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500998 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400999
Bill Spitzakb715cec2012-06-05 17:08:23 -04001000 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001001}
1002
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001003#define F(field) offsetof(struct x11_compositor, field)
1004
1005static void
1006x11_compositor_get_resources(struct x11_compositor *c)
1007{
1008 static const struct { const char *name; int offset; } atoms[] = {
1009 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1010 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1011 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1012 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001013 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001014 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001015 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001016 { "_NET_WM_STATE", F(atom.net_wm_state) },
1017 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001018 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001019 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001020 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001021 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001022 };
1023
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001024 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001025 xcb_intern_atom_reply_t *reply;
1026 xcb_pixmap_t pixmap;
1027 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001028 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001029 uint8_t data[] = { 0, 0, 0, 0 };
1030
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001031 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001032 cookies[i] = xcb_intern_atom (c->conn, 0,
1033 strlen(atoms[i].name),
1034 atoms[i].name);
1035
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001036 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001037 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1038 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1039 free(reply);
1040 }
1041
1042 pixmap = xcb_generate_id(c->conn);
1043 gc = xcb_generate_id(c->conn);
1044 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1045 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1046 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1047 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1048 c->null_cursor = xcb_generate_id(c->conn);
1049 xcb_create_cursor (c->conn, c->null_cursor,
1050 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1051 xcb_free_gc(c->conn, gc);
1052 xcb_free_pixmap(c->conn, pixmap);
1053}
1054
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001055static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001056x11_restore(struct weston_compositor *ec)
1057{
1058}
1059
1060static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001061x11_free_configured_output(struct x11_configured_output *output)
1062{
1063 free(output->name);
1064 free(output);
1065}
1066
1067static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001068x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001069{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001070 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001071 struct x11_configured_output *o, *n;
1072
1073 wl_list_for_each_safe(o, n, &configured_output_list, link)
1074 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001075
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001076 wl_event_source_remove(compositor->xcb_source);
1077 x11_input_destroy(compositor);
1078
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001079 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001080
1081 x11_compositor_fini_egl(compositor);
1082
1083 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001084 free(ec);
1085}
1086
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001088x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001089 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001090 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001091 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001092{
1093 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001094 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001095 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001096 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001097 int i, x = 0, output_count = 0;
1098 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001099
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001100 weston_log("initializing x11 backend\n");
1101
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001102 c = malloc(sizeof *c);
1103 if (c == NULL)
1104 return NULL;
1105
1106 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001107
Daniel Stone725c2c32012-06-22 14:04:36 +01001108 if (weston_compositor_init(&c->base, display, argc, argv,
1109 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001110 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001111
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001112 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001113 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001114 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001115
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001116 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001117 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1118
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001119 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001120 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001121
1122 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1123 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001124 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001125
1126 x11_compositor_get_resources(c);
1127
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001128 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001129 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001130 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001131
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001132 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001133 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001134
Daniel Stone22815f92012-06-22 13:21:34 +01001135 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001136 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001137
Scott Moreau248aaec2012-08-03 14:19:52 -06001138 width = option_width ? option_width : 1024;
1139 height = option_height ? option_height : 640;
1140 count = option_count ? option_count : 1;
1141
1142 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001143 output = x11_compositor_create_output(c, x, 0,
1144 option_width ? width :
1145 o->width,
1146 option_height ? height :
1147 o->height,
1148 fullscreen, no_input,
1149 o->name, o->transform);
1150 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001151 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001152
1153 x = pixman_region32_extents(&output->base.region)->x2;
1154
Scott Moreau248aaec2012-08-03 14:19:52 -06001155 output_count++;
1156 if (option_count && output_count >= option_count)
1157 break;
1158 }
1159
1160 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001161 output = x11_compositor_create_output(c, x, 0, width, height,
1162 fullscreen, no_input, NULL,
1163 WL_OUTPUT_TRANSFORM_NORMAL);
1164 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001165 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001166 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001167 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001168
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001169 if (gles2_renderer_init(&c->base) < 0)
1170 goto err_egl;
1171
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001172 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001173 wl_event_loop_add_fd(c->base.input_loop,
1174 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001175 WL_EVENT_READABLE,
1176 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001177 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001178
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001179 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001180
1181err_x11_input:
1182 x11_input_destroy(c);
1183err_egl:
1184 x11_compositor_fini_egl(c);
1185err_xdisplay:
1186 XCloseDisplay(c->dpy);
1187err_free:
1188 free(c);
1189 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001190}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001191
Scott Moreau248aaec2012-08-03 14:19:52 -06001192static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001193x11_output_set_transform(struct x11_configured_output *output)
1194{
1195 if (!output_transform) {
1196 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1197 return;
1198 }
1199
1200 if (!strcmp(output_transform, "normal"))
1201 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1202 else if (!strcmp(output_transform, "90"))
1203 output->transform = WL_OUTPUT_TRANSFORM_90;
1204 else if (!strcmp(output_transform, "180"))
1205 output->transform = WL_OUTPUT_TRANSFORM_180;
1206 else if (!strcmp(output_transform, "270"))
1207 output->transform = WL_OUTPUT_TRANSFORM_270;
1208 else if (!strcmp(output_transform, "flipped"))
1209 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1210 else if (!strcmp(output_transform, "flipped-90"))
1211 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1212 else if (!strcmp(output_transform, "flipped-180"))
1213 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1214 else if (!strcmp(output_transform, "flipped-270"))
1215 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1216 else {
1217 weston_log("Invalid transform \"%s\" for output %s\n",
1218 output_transform, output_name);
1219 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1220 }
1221}
1222
1223static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001224output_section_done(void *data)
1225{
1226 struct x11_configured_output *output;
1227
1228 output = malloc(sizeof *output);
1229
Scott Moreau1bad5db2012-08-18 01:04:05 -06001230 if (!output || !output_name || (output_name[0] != 'X') ||
1231 (!output_mode && !output_transform)) {
1232 if (output_name)
1233 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001234 output_name = NULL;
Scott Moreauca9b09d2012-08-06 01:27:06 -06001235 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001236 }
1237
1238 output->name = output_name;
1239
Scott Moreau1bad5db2012-08-18 01:04:05 -06001240 if (output_mode) {
1241 if (sscanf(output_mode, "%dx%d", &output->width,
1242 &output->height) != 2) {
1243 weston_log("Invalid mode \"%s\" for output %s\n",
1244 output_mode, output_name);
1245 x11_free_configured_output(output);
1246 goto err_free;
1247 }
1248 } else {
1249 output->width = 1024;
1250 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001251 }
1252
Scott Moreau1bad5db2012-08-18 01:04:05 -06001253 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001254
Scott Moreauca9b09d2012-08-06 01:27:06 -06001255 wl_list_insert(configured_output_list.prev, &output->link);
1256
1257err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001258 if (output_mode)
1259 free(output_mode);
1260 if (output_transform)
1261 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001262 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001263 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001264}
1265
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001266WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001267backend_init(struct wl_display *display, int argc, char *argv[],
1268 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001269{
Scott Moreau248aaec2012-08-03 14:19:52 -06001270 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001271 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001272
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001273 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001274 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1275 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001276 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001277 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001278 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001279 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001280
1281 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001282
Scott Moreau248aaec2012-08-03 14:19:52 -06001283 wl_list_init(&configured_output_list);
1284
1285 const struct config_key x11_config_keys[] = {
1286 { "name", CONFIG_KEY_STRING, &output_name },
1287 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001288 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001289 };
1290
1291 const struct config_section config_section[] = {
1292 { "output", x11_config_keys,
1293 ARRAY_LENGTH(x11_config_keys), output_section_done },
1294 };
1295
1296 parse_config_file(config_file, config_section,
1297 ARRAY_LENGTH(config_section), NULL);
1298
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001299 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001300 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001301 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001302 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001303}