blob: 22514c989fd5e139af5083902dc834c42c948094 [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
John Kåre Alsaker143a8982012-10-12 12:25:07 +020028#include <assert.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040029#include <stddef.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040030#include <stdlib.h>
31#include <string.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <errno.h>
35#include <sys/time.h>
Kristian Høgsbergf9112b22010-06-14 12:53:43 -040036#include <linux/input.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037
38#include <xcb/xcb.h>
Daniel Stone62b33b62012-06-22 13:21:35 +010039#ifdef HAVE_XCB_XKB
40#include <xcb/xkb.h>
41#endif
42
Benjamin Franzke3b288af2011-02-20 19:58:42 +010043#include <X11/Xlib.h>
44#include <X11/Xlib-xcb.h>
45
Daniel Stone576cd152012-06-01 12:14:02 +010046#include <xkbcommon/xkbcommon.h>
47
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048#include <GLES2/gl2.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040049#include <EGL/egl.h>
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040051#include "compositor.h"
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040052#include "../shared/config-parser.h"
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040053
Jonas Ådahl62efe202012-09-27 18:40:41 +020054#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
55
Scott Moreau248aaec2012-08-03 14:19:52 -060056static char *output_name;
57static char *output_mode;
Scott Moreau1bad5db2012-08-18 01:04:05 -060058static char *output_transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060059static int option_width;
60static int option_height;
61static int option_count;
62static struct wl_list configured_output_list;
63
64struct x11_configured_output {
65 char *name;
66 int width, height;
Scott Moreau1bad5db2012-08-18 01:04:05 -060067 uint32_t transform;
Scott Moreau248aaec2012-08-03 14:19:52 -060068 struct wl_list link;
69};
70
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071struct x11_compositor {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050072 struct weston_compositor base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040073
Benjamin Franzke3b288af2011-02-20 19:58:42 +010074 Display *dpy;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040075 xcb_connection_t *conn;
76 xcb_screen_t *screen;
77 xcb_cursor_t null_cursor;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -050078 struct wl_array keys;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040079 struct wl_event_source *xcb_source;
Daniel Stone576cd152012-06-01 12:14:02 +010080 struct xkb_keymap *xkb_keymap;
Daniel Stone62b33b62012-06-22 13:21:35 +010081 unsigned int has_xkb;
82 uint8_t xkb_event_base;
Kristian Høgsberg4f92c532012-08-10 10:04:36 -040083
84 /* We could map multi-pointer X to multiple wayland seats, but
85 * for now we only support core X input. */
86 struct weston_seat core_seat;
87
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088 struct {
89 xcb_atom_t wm_protocols;
90 xcb_atom_t wm_normal_hints;
91 xcb_atom_t wm_size_hints;
92 xcb_atom_t wm_delete_window;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050093 xcb_atom_t wm_class;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094 xcb_atom_t net_wm_name;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -050095 xcb_atom_t net_wm_icon;
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -040096 xcb_atom_t net_wm_state;
97 xcb_atom_t net_wm_state_fullscreen;
Kristian Høgsberg24ed6212011-01-26 14:02:31 -050098 xcb_atom_t string;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040099 xcb_atom_t utf8_string;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500100 xcb_atom_t cardinal;
Daniel Stone855028f2012-05-01 20:37:10 +0100101 xcb_atom_t xkb_names;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400102 } atom;
103};
104
105struct x11_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500106 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400107
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400108 xcb_window_t window;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500109 struct weston_mode mode;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400110 struct wl_event_source *finish_frame_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400111};
112
Daniel Stone576cd152012-06-01 12:14:02 +0100113static struct xkb_keymap *
114x11_compositor_get_keymap(struct x11_compositor *c)
115{
116 xcb_get_property_cookie_t cookie;
117 xcb_get_property_reply_t *reply;
118 xcb_generic_error_t *error;
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, &error);
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}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400155
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400156static uint32_t
157get_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
Daniel Stone62b33b62012-06-22 13:21:35 +0100182static void
183x11_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;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100192 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100193 xcb_void_cookie_t select;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100194 xcb_xkb_per_client_flags_cookie_t pcf;
195 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400196 xcb_xkb_get_state_cookie_t state;
197 xcb_xkb_get_state_reply_t *state_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100198
199 c->has_xkb = 0;
200 c->xkb_event_base = 0;
201
202 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
203 if (!ext) {
204 weston_log("XKB extension not available on host X11 server\n");
205 return;
206 }
207 c->xkb_event_base = ext->first_event;
208
Daniel Stonee2faa122012-06-22 13:21:39 +0100209 select = xcb_xkb_select_events(c->conn,
210 XCB_XKB_ID_USE_CORE_KBD,
211 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
212 0,
213 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
214 0,
215 0,
216 NULL);
217 error = xcb_request_check(c->conn, select);
218 if (error) {
219 weston_log("error: failed to select for XKB state events\n");
220 return;
221 }
222
Daniel Stone3ee91e12012-06-22 13:21:36 +0100223 pcf = xcb_xkb_per_client_flags(c->conn,
224 XCB_XKB_ID_USE_CORE_KBD,
225 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
226 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
227 0,
228 0,
229 0);
230 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, &error);
231 free(pcf_reply);
232 if (error) {
233 weston_log("failed to set XKB per-client flags, not using "
234 "detectable repeat\n");
235 return;
236 }
237
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400238 state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
239 state_reply = xcb_xkb_get_state_reply(c->conn, state, &error);
240 if (error) {
241 weston_log("failed to get initial XKB state\n");
242 free(state_reply);
243 return;
244 }
245
246 xkb_state_update_mask(c->core_seat.xkb_state.state,
247 get_xkb_mod_mask(c, state_reply->baseMods),
248 get_xkb_mod_mask(c, state_reply->latchedMods),
249 get_xkb_mod_mask(c, state_reply->lockedMods),
250 0,
251 0,
252 state_reply->group);
253
254 free(state_reply);
255
Daniel Stone62b33b62012-06-22 13:21:35 +0100256 c->has_xkb = 1;
257#endif
258}
259
Darxus55973f22010-11-22 21:24:39 -0500260static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300261x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400262{
Daniel Stone576cd152012-06-01 12:14:02 +0100263 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400264
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400265 weston_seat_init(&c->core_seat, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300266
267 if (no_input)
268 return 0;
269
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400270 weston_seat_init_pointer(&c->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100271
272 keymap = x11_compositor_get_keymap(c);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400273 weston_seat_init_keyboard(&c->core_seat, keymap);
Daniel Stone576cd152012-06-01 12:14:02 +0100274 if (keymap)
275 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400276
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400277 x11_compositor_setup_xkb(c);
278
Darxus55973f22010-11-22 21:24:39 -0500279 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400280}
281
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200282static void
283x11_input_destroy(struct x11_compositor *compositor)
284{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400285 weston_seat_release(&compositor->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200286}
287
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400288static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400289x11_compositor_init_egl(struct x11_compositor *c)
290{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400291 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100292 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100293 EGLint config_attribs[] = {
294 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
295 EGL_RED_SIZE, 1,
296 EGL_GREEN_SIZE, 1,
297 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100298 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
299 EGL_NONE
300 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400301
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400302 c->base.egl_display = eglGetDisplay(c->dpy);
303 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200304 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400305 return -1;
306 }
307
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400308 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400310 return -1;
311 }
312
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400313 if (!eglChooseConfig(c->base.egl_display, config_attribs,
314 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200315 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100316 return -1;
317 }
Darxus55973f22010-11-22 21:24:39 -0500318
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400319 return 0;
320}
321
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200322static void
323x11_compositor_fini_egl(struct x11_compositor *compositor)
324{
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400325 gles2_renderer_destroy(&compositor->base);
326
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400327 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200328 EGL_NO_SURFACE, EGL_NO_SURFACE,
329 EGL_NO_CONTEXT);
330
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400331 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200332 eglReleaseThread();
333}
334
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500335static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500336x11_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400337 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500338{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500339 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400340 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500341
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400342 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500343
344 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500345}
346
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100347static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400348finish_frame_handler(void *data)
349{
350 struct x11_output *output = data;
351 uint32_t msec;
352 struct timeval tv;
353
354 gettimeofday(&tv, NULL);
355 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500356 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400357
358 return 1;
359}
360
Matt Roper361d2ad2011-08-29 13:52:23 -0700361static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500362x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700363{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200364 struct x11_output *output = (struct x11_output *)output_base;
365 struct x11_compositor *compositor =
366 (struct x11_compositor *)output->base.compositor;
367
368 wl_list_remove(&output->base.link);
369 wl_event_source_remove(output->finish_frame_timer);
370
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400371 eglDestroySurface(compositor->base.egl_display,
372 output->base.egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200373
374 xcb_destroy_window(compositor->conn, output->window);
375
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500376 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200377
378 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700379}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200380
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400381static void
382x11_output_set_wm_protocols(struct x11_output *output)
383{
384 xcb_atom_t list[1];
385 struct x11_compositor *c =
386 (struct x11_compositor *) output->base.compositor;
387
388 list[0] = c->atom.wm_delete_window;
389 xcb_change_property (c->conn,
390 XCB_PROP_MODE_REPLACE,
391 output->window,
392 c->atom.wm_protocols,
393 XCB_ATOM_ATOM,
394 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500395 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400396 list);
397}
398
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400399static void
400x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
401{
402 xcb_client_message_event_t event;
403 struct x11_compositor *c =
404 (struct x11_compositor *) output->base.compositor;
405 xcb_screen_iterator_t iter;
406
407#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
408#define _NET_WM_STATE_ADD 1 /* add/set property */
409#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
410
411 memset(&event, 0, sizeof event);
412 event.response_type = XCB_CLIENT_MESSAGE;
413 event.format = 32;
414 event.window = output->window;
415 event.type = c->atom.net_wm_state;
416
417 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
418 event.data.data32[1] = state;
419 event.data.data32[2] = 0;
420 event.data.data32[3] = 0;
421 event.data.data32[4] = 0;
422
423 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
424 xcb_send_event(c->conn, 0, iter.data->root,
425 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
426 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
427 (void *) &event);
428}
429
430
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400431struct wm_normal_hints {
432 uint32_t flags;
433 uint32_t pad[4];
434 int32_t min_width, min_height;
435 int32_t max_width, max_height;
436 int32_t width_inc, height_inc;
437 int32_t min_aspect_x, min_aspect_y;
438 int32_t max_aspect_x, max_aspect_y;
439 int32_t base_width, base_height;
440 int32_t win_gravity;
441};
442
443#define WM_NORMAL_HINTS_MIN_SIZE 16
444#define WM_NORMAL_HINTS_MAX_SIZE 32
445
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500446static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400447x11_output_set_icon(struct x11_compositor *c,
448 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500449{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400450 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400451 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400452 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500453
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400454 image = load_image(filename);
455 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500456 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400457 width = pixman_image_get_width(image);
458 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500459 icon = malloc(width * height * 4 + 8);
460 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400461 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500462 return;
463 }
464
465 icon[0] = width;
466 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400467 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500468 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
469 c->atom.net_wm_icon, c->atom.cardinal, 32,
470 width * height + 2, icon);
471 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400472 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500473}
474
Scott Moreau1bad5db2012-08-18 01:04:05 -0600475static struct x11_output *
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700476x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300477 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600478 int no_input, char *configured_name,
479 uint32_t transform)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400480{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600481 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500482 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600483 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400484 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400485 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400486 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400487 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400488 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300489 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400490 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300491 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400492 0
493 };
494
Scott Moreau1bad5db2012-08-18 01:04:05 -0600495 if (configured_name)
496 sprintf(title, "%s - %s", name, configured_name);
497 else
498 strcpy(title, name);
499
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300500 if (!no_input)
501 values[0] |=
502 XCB_EVENT_MASK_KEY_PRESS |
503 XCB_EVENT_MASK_KEY_RELEASE |
504 XCB_EVENT_MASK_BUTTON_PRESS |
505 XCB_EVENT_MASK_BUTTON_RELEASE |
506 XCB_EVENT_MASK_POINTER_MOTION |
507 XCB_EVENT_MASK_ENTER_WINDOW |
508 XCB_EVENT_MASK_LEAVE_WINDOW |
509 XCB_EVENT_MASK_KEYMAP_STATE |
510 XCB_EVENT_MASK_FOCUS_CHANGE;
511
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400512 output = malloc(sizeof *output);
513 if (output == NULL)
Scott Moreau1bad5db2012-08-18 01:04:05 -0600514 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400515
516 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400517
518 output->mode.flags =
519 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
520 output->mode.width = width;
521 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400522 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400523 wl_list_init(&output->base.mode_list);
524 wl_list_insert(&output->base.mode_list, &output->mode.link);
525
526 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400527 output->base.make = "xwayland";
528 output->base.model = "none";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600529 weston_output_init(&output->base, &c->base,
530 x, y, width, height, transform);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400531
532 values[1] = c->null_cursor;
533 output->window = xcb_generate_id(c->conn);
534 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
535 xcb_create_window(c->conn,
536 XCB_COPY_FROM_PARENT,
537 output->window,
538 iter.data->root,
539 0, 0,
540 width, height,
541 0,
542 XCB_WINDOW_CLASS_INPUT_OUTPUT,
543 iter.data->root_visual,
544 mask, values);
545
546 /* Don't resize me. */
547 memset(&normal_hints, 0, sizeof normal_hints);
548 normal_hints.flags =
549 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
550 normal_hints.min_width = width;
551 normal_hints.min_height = height;
552 normal_hints.max_width = width;
553 normal_hints.max_height = height;
554 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
555 c->atom.wm_normal_hints,
556 c->atom.wm_size_hints, 32,
557 sizeof normal_hints / 4,
558 (uint8_t *) &normal_hints);
559
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400560 /* Set window name. Don't bother with non-EWMH WMs. */
561 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
562 c->atom.net_wm_name, c->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600563 strlen(title), title);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500564 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
565 c->atom.wm_class, c->atom.string, 8,
566 sizeof class, class);
567
Kristian Høgsberg86000402012-01-03 23:24:14 -0500568 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500569
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500570 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400571
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400572 x11_output_set_wm_protocols(output);
573
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400574 if (fullscreen)
575 x11_output_change_state(output, 1,
576 c->atom.net_wm_state_fullscreen);
577
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400578 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400579 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100580 output->window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400581 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200582 weston_log("failed to create window surface\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600583 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400584 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400585
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400586 loop = wl_display_get_event_loop(c->base.wl_display);
587 output->finish_frame_timer =
588 wl_event_loop_add_timer(loop, finish_frame_handler, output);
589
Alex Wubd3354b2012-04-17 17:20:49 +0800590 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500591 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700592 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800593 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200594 output->base.set_backlight = NULL;
595 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800596 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100597
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400598 wl_list_insert(c->base.output_list.prev, &output->base.link);
599
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400600 weston_log("x11 output %dx%d, window id %d\n",
601 width, height, output->window);
602
Scott Moreau1bad5db2012-08-18 01:04:05 -0600603 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400604}
605
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400606static struct x11_output *
607x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
608{
609 struct x11_output *output;
610
611 wl_list_for_each(output, &c->base.output_list, base.link) {
612 if (output->window == window)
613 return output;
614 }
615
616 return NULL;
617}
618
Daniel Stone154c34c2012-06-22 13:21:40 +0100619#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100620static void
621update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
622{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400623 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stonee2faa122012-06-22 13:21:39 +0100624 get_xkb_mod_mask(c, state->baseMods),
625 get_xkb_mod_mask(c, state->latchedMods),
626 get_xkb_mod_mask(c, state->lockedMods),
627 0,
628 0,
629 state->group);
630
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400631 notify_modifiers(&c->core_seat,
632 wl_display_next_serial(c->base.wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100633}
634#endif
635
Daniel Stone154c34c2012-06-22 13:21:40 +0100636/**
637 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
638 * the best we can do (given that XCB also lacks XI2 support), is to take
639 * the state from the core key events. Unfortunately that only gives us
640 * the effective (i.e. union of depressed/latched/locked) state, and we
641 * need the granularity.
642 *
643 * So we still update the state with every key event we see, but also use
644 * the state field from X11 events as a mask so we don't get any stuck
645 * modifiers.
646 */
647static void
648update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
649{
650 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400651 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +0100652
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400653 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +0100654 keyboard->modifiers.mods_depressed & mask,
655 keyboard->modifiers.mods_latched & mask,
656 keyboard->modifiers.mods_locked & mask,
657 0,
658 0,
659 (x11_mask >> 13) & 3);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400660 notify_modifiers(&c->core_seat,
Daniel Stone154c34c2012-06-22 13:21:40 +0100661 wl_display_next_serial(c->base.wl_display));
662}
663
Tiago Vignattia3a71622011-12-08 17:03:17 +0200664static void
665x11_compositor_deliver_button_event(struct x11_compositor *c,
666 xcb_generic_event_t *event, int state)
667{
668 xcb_button_press_event_t *button_event =
669 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100670 uint32_t button;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200671
Daniel Stone154c34c2012-06-22 13:21:40 +0100672 if (!c->has_xkb)
673 update_xkb_state_from_core(c, button_event->state);
674
Tiago Vignattia3a71622011-12-08 17:03:17 +0200675 switch (button_event->detail) {
676 default:
677 button = button_event->detail + BTN_LEFT - 1;
678 break;
679 case 2:
680 button = BTN_MIDDLE;
681 break;
682 case 3:
683 button = BTN_RIGHT;
684 break;
685 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +0200686 /* Axis are measured in pixels, but the xcb events are discrete
687 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -0600688 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400689 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200690 weston_compositor_get_time(),
691 WL_POINTER_AXIS_VERTICAL_SCROLL,
692 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600693 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200694 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600695 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400696 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200697 weston_compositor_get_time(),
698 WL_POINTER_AXIS_VERTICAL_SCROLL,
699 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600700 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200701 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600702 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400703 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200704 weston_compositor_get_time(),
705 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
706 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600707 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200708 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600709 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400710 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200711 weston_compositor_get_time(),
712 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
713 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200714 return;
715 }
716
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400717 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100718 weston_compositor_get_time(), button,
719 state ? WL_POINTER_BUTTON_STATE_PRESSED :
720 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200721}
722
Scott Moreau1bad5db2012-08-18 01:04:05 -0600723static void
724x11_output_transform_coordinate(struct x11_output *x11_output,
725 wl_fixed_t *x, wl_fixed_t *y)
726{
727 struct weston_output *output = &x11_output->base;
728 wl_fixed_t tx, ty;
729 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
730 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
731
732 switch(output->transform) {
733 case WL_OUTPUT_TRANSFORM_NORMAL:
734 default:
735 tx = *x;
736 ty = *y;
737 break;
738 case WL_OUTPUT_TRANSFORM_90:
739 tx = *y;
740 ty = height - *x;
741 break;
742 case WL_OUTPUT_TRANSFORM_180:
743 tx = width - *x;
744 ty = height - *y;
745 break;
746 case WL_OUTPUT_TRANSFORM_270:
747 tx = width - *y;
748 ty = *x;
749 break;
750 case WL_OUTPUT_TRANSFORM_FLIPPED:
751 tx = width - *x;
752 ty = *y;
753 break;
754 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
755 tx = width - *y;
756 ty = height - *x;
757 break;
758 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
759 tx = *x;
760 ty = height - *y;
761 break;
762 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
763 tx = *y;
764 ty = *x;
765 break;
766 }
767
768 tx += wl_fixed_from_int(output->x);
769 ty += wl_fixed_from_int(output->y);
770
771 *x = tx;
772 *y = ty;
773}
774
775static void
776x11_compositor_deliver_motion_event(struct x11_compositor *c,
777 xcb_generic_event_t *event)
778{
779 struct x11_output *output;
780 wl_fixed_t x, y;
781 xcb_motion_notify_event_t *motion_notify =
782 (xcb_motion_notify_event_t *) event;
783
784 if (!c->has_xkb)
785 update_xkb_state_from_core(c, motion_notify->state);
786 output = x11_compositor_find_output(c, motion_notify->event);
787 x = wl_fixed_from_int(motion_notify->event_x);
788 y = wl_fixed_from_int(motion_notify->event_y);
789 x11_output_transform_coordinate(output, &x, &y);
790
791 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
792}
793
794static void
795x11_compositor_deliver_enter_event(struct x11_compositor *c,
796 xcb_generic_event_t *event)
797{
798 struct x11_output *output;
799 wl_fixed_t x, y;
800
801 xcb_enter_notify_event_t *enter_notify =
802 (xcb_enter_notify_event_t *) event;
803 if (enter_notify->state >= Button1Mask)
804 return;
805 if (!c->has_xkb)
806 update_xkb_state_from_core(c, enter_notify->state);
807 output = x11_compositor_find_output(c, enter_notify->event);
808 x = wl_fixed_from_int(enter_notify->event_x);
809 y = wl_fixed_from_int(enter_notify->event_y);
810 x11_output_transform_coordinate(output, &x, &y);
811
812 notify_pointer_focus(&c->core_seat, &output->base, x, y);
813}
814
Kristian Høgsbergee724822011-04-21 14:51:44 -0400815static int
816x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400817 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400818{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400819 if (mask & WL_EVENT_READABLE) {
820 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400821 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400822#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400823 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400824#else
825 *event = xcb_poll_for_event(c->conn);
826#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400827 }
828
829 return *event != NULL;
830}
831
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400832static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400833x11_compositor_handle_event(int fd, uint32_t mask, void *data)
834{
835 struct x11_compositor *c = data;
836 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400837 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400838 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500839 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400840 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500841 xcb_keymap_notify_event_t *keymap_notify;
842 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400843 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400844 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500845 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400846 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200847 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400848 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400849
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400850 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400851 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400852 while (x11_compositor_next_event(c, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200853 response_type = event->response_type & ~0x80;
854
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400855 switch (prev ? prev->response_type & ~0x80 : 0x80) {
856 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100857 /* Suppress key repeat events; this is only used if we
858 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400859 key_release = (xcb_key_press_event_t *) prev;
860 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200861 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700862 key_release->time == key_press->time &&
863 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400864 /* Don't deliver the held key release
865 * event or the new key press event. */
866 free(event);
867 free(prev);
868 prev = NULL;
869 continue;
870 } else {
871 /* Deliver the held key release now
872 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400873 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100874 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400875 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500876 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100877 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100878 WL_KEYBOARD_KEY_STATE_RELEASED,
879 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400880 free(prev);
881 prev = NULL;
882 break;
883 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400884
885 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200886 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400887 keymap_notify = (xcb_keymap_notify_event_t *) event;
888 c->keys.size = 0;
889 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
890 set = keymap_notify->keys[i >> 3] &
891 (1 << (i & 7));
892 if (set) {
893 k = wl_array_add(&c->keys, sizeof *k);
894 *k = i;
895 }
896 }
897
Daniel Stoned6da09e2012-06-22 13:21:29 +0100898 /* Unfortunately the state only comes with the enter
899 * event, rather than with the focus event. I'm not
900 * sure of the exact semantics around it and whether
901 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400902 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100903 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400904
905 free(prev);
906 prev = NULL;
907 break;
908
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400909 default:
910 /* No previous event held */
911 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400912 }
913
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200914 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400915 case XCB_KEY_PRESS:
916 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100917 if (!c->has_xkb)
918 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400919 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500920 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100921 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100922 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100923 c->has_xkb ? STATE_UPDATE_NONE :
924 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400925 break;
926 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100927 /* If we don't have XKB, we need to use the lame
928 * autorepeat detection above. */
929 if (!c->has_xkb) {
930 prev = event;
931 break;
932 }
933 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400934 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100935 weston_compositor_get_time(),
936 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100937 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100938 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400939 break;
940 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200941 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400942 break;
943 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200944 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400945 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400946 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600947 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400948 break;
949
950 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400951 expose = (xcb_expose_event_t *) event;
952 output = x11_compositor_find_output(c, expose->window);
953 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400954 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400955
956 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600957 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400958 break;
959
960 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500961 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500962 if (enter_notify->state >= Button1Mask)
963 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100964 if (!c->has_xkb)
965 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400966 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400967 break;
968
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400969 case XCB_CLIENT_MESSAGE:
970 client_message = (xcb_client_message_event_t *) event;
971 atom = client_message->data.data32[0];
972 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500973 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400974 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400975
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500976 case XCB_FOCUS_IN:
977 focus_in = (xcb_focus_in_event_t *) event;
978 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
979 break;
980
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400981 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500982 break;
983
984 case XCB_FOCUS_OUT:
985 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -0500986 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
987 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500988 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400989 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500990 break;
991
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500992 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400993 break;
994 }
995
Daniel Stonee2faa122012-06-22 13:21:39 +0100996#ifdef HAVE_XCB_XKB
997 if (c->has_xkb &&
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200998 response_type == c->xkb_event_base) {
Daniel Stonee2faa122012-06-22 13:21:39 +0100999 xcb_xkb_state_notify_event_t *state =
1000 (xcb_xkb_state_notify_event_t *) event;
1001 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1002 update_xkb_state(c, state);
1003 }
1004#endif
1005
Bill Spitzakb715cec2012-06-05 17:08:23 -04001006 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001007 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001008 free (event);
1009 }
1010
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001011 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1012 case XCB_KEY_RELEASE:
1013 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +01001014 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001015 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001016 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001017 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001018 WL_KEYBOARD_KEY_STATE_RELEASED,
1019 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001020 free(prev);
1021 prev = NULL;
1022 break;
1023 default:
1024 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001025 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001026
Bill Spitzakb715cec2012-06-05 17:08:23 -04001027 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001028}
1029
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001030#define F(field) offsetof(struct x11_compositor, field)
1031
1032static void
1033x11_compositor_get_resources(struct x11_compositor *c)
1034{
1035 static const struct { const char *name; int offset; } atoms[] = {
1036 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1037 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1038 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1039 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001040 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001041 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001042 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001043 { "_NET_WM_STATE", F(atom.net_wm_state) },
1044 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001045 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001046 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001047 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001048 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001049 };
1050
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001051 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001052 xcb_intern_atom_reply_t *reply;
1053 xcb_pixmap_t pixmap;
1054 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001055 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001056 uint8_t data[] = { 0, 0, 0, 0 };
1057
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001058 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001059 cookies[i] = xcb_intern_atom (c->conn, 0,
1060 strlen(atoms[i].name),
1061 atoms[i].name);
1062
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001063 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001064 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1065 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1066 free(reply);
1067 }
1068
1069 pixmap = xcb_generate_id(c->conn);
1070 gc = xcb_generate_id(c->conn);
1071 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1072 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1073 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1074 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1075 c->null_cursor = xcb_generate_id(c->conn);
1076 xcb_create_cursor (c->conn, c->null_cursor,
1077 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1078 xcb_free_gc(c->conn, gc);
1079 xcb_free_pixmap(c->conn, pixmap);
1080}
1081
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001082static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001083x11_restore(struct weston_compositor *ec)
1084{
1085}
1086
1087static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001088x11_free_configured_output(struct x11_configured_output *output)
1089{
1090 free(output->name);
1091 free(output);
1092}
1093
1094static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001095x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001096{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001097 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001098 struct x11_configured_output *o, *n;
1099
1100 wl_list_for_each_safe(o, n, &configured_output_list, link)
1101 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001102
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001103 wl_event_source_remove(compositor->xcb_source);
1104 x11_input_destroy(compositor);
1105
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001106 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001107
1108 x11_compositor_fini_egl(compositor);
1109
1110 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001111 free(ec);
1112}
1113
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001114static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001115x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001116 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001117 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001118 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001119{
1120 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001121 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001122 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001123 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001124 int i, x = 0, output_count = 0;
1125 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001126
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001127 weston_log("initializing x11 backend\n");
1128
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001129 c = malloc(sizeof *c);
1130 if (c == NULL)
1131 return NULL;
1132
1133 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001134
Daniel Stone725c2c32012-06-22 14:04:36 +01001135 if (weston_compositor_init(&c->base, display, argc, argv,
1136 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001137 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001138
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001139 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001140 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001141 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001142
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001143 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001144 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1145
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001146 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001147 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001148
1149 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1150 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001151 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001152
1153 x11_compositor_get_resources(c);
1154
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001155 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001156 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001157 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001158
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001159 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001160 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001161
Daniel Stone22815f92012-06-22 13:21:34 +01001162 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001163 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001164
Scott Moreau248aaec2012-08-03 14:19:52 -06001165 width = option_width ? option_width : 1024;
1166 height = option_height ? option_height : 640;
1167 count = option_count ? option_count : 1;
1168
1169 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001170 output = x11_compositor_create_output(c, x, 0,
1171 option_width ? width :
1172 o->width,
1173 option_height ? height :
1174 o->height,
1175 fullscreen, no_input,
1176 o->name, o->transform);
1177 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001178 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001179
1180 x = pixman_region32_extents(&output->base.region)->x2;
1181
Scott Moreau248aaec2012-08-03 14:19:52 -06001182 output_count++;
1183 if (option_count && output_count >= option_count)
1184 break;
1185 }
1186
1187 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001188 output = x11_compositor_create_output(c, x, 0, width, height,
1189 fullscreen, no_input, NULL,
1190 WL_OUTPUT_TRANSFORM_NORMAL);
1191 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001192 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001193 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001194 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001195
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001196 if (gles2_renderer_init(&c->base) < 0)
1197 goto err_egl;
1198
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001199 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001200 wl_event_loop_add_fd(c->base.input_loop,
1201 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001202 WL_EVENT_READABLE,
1203 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001204 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001205
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001206 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001207
1208err_x11_input:
1209 x11_input_destroy(c);
1210err_egl:
1211 x11_compositor_fini_egl(c);
1212err_xdisplay:
1213 XCloseDisplay(c->dpy);
1214err_free:
1215 free(c);
1216 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001217}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001218
Scott Moreau248aaec2012-08-03 14:19:52 -06001219static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001220x11_output_set_transform(struct x11_configured_output *output)
1221{
1222 if (!output_transform) {
1223 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1224 return;
1225 }
1226
1227 if (!strcmp(output_transform, "normal"))
1228 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1229 else if (!strcmp(output_transform, "90"))
1230 output->transform = WL_OUTPUT_TRANSFORM_90;
1231 else if (!strcmp(output_transform, "180"))
1232 output->transform = WL_OUTPUT_TRANSFORM_180;
1233 else if (!strcmp(output_transform, "270"))
1234 output->transform = WL_OUTPUT_TRANSFORM_270;
1235 else if (!strcmp(output_transform, "flipped"))
1236 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1237 else if (!strcmp(output_transform, "flipped-90"))
1238 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1239 else if (!strcmp(output_transform, "flipped-180"))
1240 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1241 else if (!strcmp(output_transform, "flipped-270"))
1242 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1243 else {
1244 weston_log("Invalid transform \"%s\" for output %s\n",
1245 output_transform, output_name);
1246 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1247 }
1248}
1249
1250static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001251output_section_done(void *data)
1252{
1253 struct x11_configured_output *output;
1254
1255 output = malloc(sizeof *output);
1256
Scott Moreau1bad5db2012-08-18 01:04:05 -06001257 if (!output || !output_name || (output_name[0] != 'X') ||
1258 (!output_mode && !output_transform)) {
1259 if (output_name)
1260 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001261 output_name = NULL;
John Kåre Alsaker5e48a852012-10-12 12:25:11 +02001262 free(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001263 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001264 }
1265
1266 output->name = output_name;
1267
Scott Moreau1bad5db2012-08-18 01:04:05 -06001268 if (output_mode) {
1269 if (sscanf(output_mode, "%dx%d", &output->width,
1270 &output->height) != 2) {
1271 weston_log("Invalid mode \"%s\" for output %s\n",
1272 output_mode, output_name);
1273 x11_free_configured_output(output);
1274 goto err_free;
1275 }
1276 } else {
1277 output->width = 1024;
1278 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001279 }
1280
Scott Moreau1bad5db2012-08-18 01:04:05 -06001281 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001282
Scott Moreauca9b09d2012-08-06 01:27:06 -06001283 wl_list_insert(configured_output_list.prev, &output->link);
1284
1285err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001286 if (output_mode)
1287 free(output_mode);
1288 if (output_transform)
1289 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001290 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001291 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001292}
1293
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001294WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001295backend_init(struct wl_display *display, int argc, char *argv[],
1296 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001297{
Scott Moreau248aaec2012-08-03 14:19:52 -06001298 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001299 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001300
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001301 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001302 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1303 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001304 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001305 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001306 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001307 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001308
1309 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001310
Scott Moreau248aaec2012-08-03 14:19:52 -06001311 wl_list_init(&configured_output_list);
1312
1313 const struct config_key x11_config_keys[] = {
1314 { "name", CONFIG_KEY_STRING, &output_name },
1315 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001316 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001317 };
1318
1319 const struct config_section config_section[] = {
1320 { "output", x11_config_keys,
1321 ARRAY_LENGTH(x11_config_keys), output_section_done },
1322 };
1323
1324 parse_config_file(config_file, config_section,
1325 ARRAY_LENGTH(config_section), NULL);
1326
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001327 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001328 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001329 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001330 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001331}