blob: c654aec704f2c63b51c0352d03c38d8aad1ffda5 [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
Ran Benita424ae012012-10-31 00:13:08 +0200209 select = xcb_xkb_select_events_checked(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);
Daniel Stonee2faa122012-06-22 13:21:39 +0100217 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;
Kristian Høgsberg73308622012-10-30 11:04:52 -0400671 struct x11_output *output;
672
673 output = x11_compositor_find_output(c, button_event->event);
674
675 if (state)
676 xcb_grab_pointer(c->conn, 0, output->window,
677 XCB_EVENT_MASK_BUTTON_PRESS |
678 XCB_EVENT_MASK_BUTTON_RELEASE |
679 XCB_EVENT_MASK_POINTER_MOTION |
680 XCB_EVENT_MASK_ENTER_WINDOW |
681 XCB_EVENT_MASK_LEAVE_WINDOW,
682 XCB_GRAB_MODE_ASYNC,
683 XCB_GRAB_MODE_ASYNC,
684 output->window, XCB_CURSOR_NONE,
685 button_event->time);
686 else
687 xcb_ungrab_pointer(c->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200688
Daniel Stone154c34c2012-06-22 13:21:40 +0100689 if (!c->has_xkb)
690 update_xkb_state_from_core(c, button_event->state);
691
Tiago Vignattia3a71622011-12-08 17:03:17 +0200692 switch (button_event->detail) {
693 default:
694 button = button_event->detail + BTN_LEFT - 1;
695 break;
696 case 2:
697 button = BTN_MIDDLE;
698 break;
699 case 3:
700 button = BTN_RIGHT;
701 break;
702 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +0200703 /* Axis are measured in pixels, but the xcb events are discrete
704 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -0600705 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400706 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200707 weston_compositor_get_time(),
708 WL_POINTER_AXIS_VERTICAL_SCROLL,
709 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600710 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200711 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600712 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400713 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200714 weston_compositor_get_time(),
715 WL_POINTER_AXIS_VERTICAL_SCROLL,
716 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600717 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200718 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600719 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400720 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200721 weston_compositor_get_time(),
722 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
723 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600724 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200725 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600726 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400727 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200728 weston_compositor_get_time(),
729 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
730 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200731 return;
732 }
733
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400734 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100735 weston_compositor_get_time(), button,
736 state ? WL_POINTER_BUTTON_STATE_PRESSED :
737 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200738}
739
Scott Moreau1bad5db2012-08-18 01:04:05 -0600740static void
741x11_output_transform_coordinate(struct x11_output *x11_output,
742 wl_fixed_t *x, wl_fixed_t *y)
743{
744 struct weston_output *output = &x11_output->base;
745 wl_fixed_t tx, ty;
746 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
747 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
748
749 switch(output->transform) {
750 case WL_OUTPUT_TRANSFORM_NORMAL:
751 default:
752 tx = *x;
753 ty = *y;
754 break;
755 case WL_OUTPUT_TRANSFORM_90:
756 tx = *y;
757 ty = height - *x;
758 break;
759 case WL_OUTPUT_TRANSFORM_180:
760 tx = width - *x;
761 ty = height - *y;
762 break;
763 case WL_OUTPUT_TRANSFORM_270:
764 tx = width - *y;
765 ty = *x;
766 break;
767 case WL_OUTPUT_TRANSFORM_FLIPPED:
768 tx = width - *x;
769 ty = *y;
770 break;
771 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
772 tx = width - *y;
773 ty = height - *x;
774 break;
775 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
776 tx = *x;
777 ty = height - *y;
778 break;
779 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
780 tx = *y;
781 ty = *x;
782 break;
783 }
784
785 tx += wl_fixed_from_int(output->x);
786 ty += wl_fixed_from_int(output->y);
787
788 *x = tx;
789 *y = ty;
790}
791
792static void
793x11_compositor_deliver_motion_event(struct x11_compositor *c,
794 xcb_generic_event_t *event)
795{
796 struct x11_output *output;
797 wl_fixed_t x, y;
798 xcb_motion_notify_event_t *motion_notify =
799 (xcb_motion_notify_event_t *) event;
800
801 if (!c->has_xkb)
802 update_xkb_state_from_core(c, motion_notify->state);
803 output = x11_compositor_find_output(c, motion_notify->event);
804 x = wl_fixed_from_int(motion_notify->event_x);
805 y = wl_fixed_from_int(motion_notify->event_y);
806 x11_output_transform_coordinate(output, &x, &y);
807
808 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
809}
810
811static void
812x11_compositor_deliver_enter_event(struct x11_compositor *c,
813 xcb_generic_event_t *event)
814{
815 struct x11_output *output;
816 wl_fixed_t x, y;
817
818 xcb_enter_notify_event_t *enter_notify =
819 (xcb_enter_notify_event_t *) event;
820 if (enter_notify->state >= Button1Mask)
821 return;
822 if (!c->has_xkb)
823 update_xkb_state_from_core(c, enter_notify->state);
824 output = x11_compositor_find_output(c, enter_notify->event);
825 x = wl_fixed_from_int(enter_notify->event_x);
826 y = wl_fixed_from_int(enter_notify->event_y);
827 x11_output_transform_coordinate(output, &x, &y);
828
829 notify_pointer_focus(&c->core_seat, &output->base, x, y);
830}
831
Kristian Høgsbergee724822011-04-21 14:51:44 -0400832static int
833x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400834 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400835{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400836 if (mask & WL_EVENT_READABLE) {
837 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400838 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400839#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400840 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400841#else
842 *event = xcb_poll_for_event(c->conn);
843#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400844 }
845
846 return *event != NULL;
847}
848
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400849static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400850x11_compositor_handle_event(int fd, uint32_t mask, void *data)
851{
852 struct x11_compositor *c = data;
853 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400854 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400855 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500856 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400857 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500858 xcb_keymap_notify_event_t *keymap_notify;
859 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400860 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400861 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500862 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400863 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200864 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400865 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400866
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400867 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400868 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400869 while (x11_compositor_next_event(c, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200870 response_type = event->response_type & ~0x80;
871
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400872 switch (prev ? prev->response_type & ~0x80 : 0x80) {
873 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100874 /* Suppress key repeat events; this is only used if we
875 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400876 key_release = (xcb_key_press_event_t *) prev;
877 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200878 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700879 key_release->time == key_press->time &&
880 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400881 /* Don't deliver the held key release
882 * event or the new key press event. */
883 free(event);
884 free(prev);
885 prev = NULL;
886 continue;
887 } else {
888 /* Deliver the held key release now
889 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400890 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100891 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400892 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500893 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100894 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100895 WL_KEYBOARD_KEY_STATE_RELEASED,
896 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400897 free(prev);
898 prev = NULL;
899 break;
900 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400901
902 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200903 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400904 keymap_notify = (xcb_keymap_notify_event_t *) event;
905 c->keys.size = 0;
906 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
907 set = keymap_notify->keys[i >> 3] &
908 (1 << (i & 7));
909 if (set) {
910 k = wl_array_add(&c->keys, sizeof *k);
911 *k = i;
912 }
913 }
914
Daniel Stoned6da09e2012-06-22 13:21:29 +0100915 /* Unfortunately the state only comes with the enter
916 * event, rather than with the focus event. I'm not
917 * sure of the exact semantics around it and whether
918 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400919 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100920 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400921
922 free(prev);
923 prev = NULL;
924 break;
925
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400926 default:
927 /* No previous event held */
928 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400929 }
930
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200931 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400932 case XCB_KEY_PRESS:
933 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100934 if (!c->has_xkb)
935 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400936 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500937 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100938 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100939 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100940 c->has_xkb ? STATE_UPDATE_NONE :
941 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400942 break;
943 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100944 /* If we don't have XKB, we need to use the lame
945 * autorepeat detection above. */
946 if (!c->has_xkb) {
947 prev = event;
948 break;
949 }
950 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400951 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100952 weston_compositor_get_time(),
953 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100954 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100955 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400956 break;
957 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200958 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400959 break;
960 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200961 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400962 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400963 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600964 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400965 break;
966
967 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400968 expose = (xcb_expose_event_t *) event;
969 output = x11_compositor_find_output(c, expose->window);
970 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400971 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400972
973 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600974 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400975 break;
976
977 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500978 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -0500979 if (enter_notify->state >= Button1Mask)
980 break;
Daniel Stone154c34c2012-06-22 13:21:40 +0100981 if (!c->has_xkb)
982 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400983 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400984 break;
985
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400986 case XCB_CLIENT_MESSAGE:
987 client_message = (xcb_client_message_event_t *) event;
988 atom = client_message->data.data32[0];
989 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -0500990 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400991 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400992
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500993 case XCB_FOCUS_IN:
994 focus_in = (xcb_focus_in_event_t *) event;
995 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
996 break;
997
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400998 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500999 break;
1000
1001 case XCB_FOCUS_OUT:
1002 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001003 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1004 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001005 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001006 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001007 break;
1008
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001009 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001010 break;
1011 }
1012
Daniel Stonee2faa122012-06-22 13:21:39 +01001013#ifdef HAVE_XCB_XKB
1014 if (c->has_xkb &&
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001015 response_type == c->xkb_event_base) {
Daniel Stonee2faa122012-06-22 13:21:39 +01001016 xcb_xkb_state_notify_event_t *state =
1017 (xcb_xkb_state_notify_event_t *) event;
1018 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1019 update_xkb_state(c, state);
1020 }
1021#endif
1022
Bill Spitzakb715cec2012-06-05 17:08:23 -04001023 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001024 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001025 free (event);
1026 }
1027
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001028 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1029 case XCB_KEY_RELEASE:
1030 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +01001031 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001032 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001033 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001034 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001035 WL_KEYBOARD_KEY_STATE_RELEASED,
1036 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001037 free(prev);
1038 prev = NULL;
1039 break;
1040 default:
1041 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001042 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001043
Bill Spitzakb715cec2012-06-05 17:08:23 -04001044 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001045}
1046
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001047#define F(field) offsetof(struct x11_compositor, field)
1048
1049static void
1050x11_compositor_get_resources(struct x11_compositor *c)
1051{
1052 static const struct { const char *name; int offset; } atoms[] = {
1053 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1054 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1055 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1056 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001057 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001058 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001059 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001060 { "_NET_WM_STATE", F(atom.net_wm_state) },
1061 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001062 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001063 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001064 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001065 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001066 };
1067
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001068 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001069 xcb_intern_atom_reply_t *reply;
1070 xcb_pixmap_t pixmap;
1071 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001072 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001073 uint8_t data[] = { 0, 0, 0, 0 };
1074
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001075 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001076 cookies[i] = xcb_intern_atom (c->conn, 0,
1077 strlen(atoms[i].name),
1078 atoms[i].name);
1079
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001080 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001081 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1082 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1083 free(reply);
1084 }
1085
1086 pixmap = xcb_generate_id(c->conn);
1087 gc = xcb_generate_id(c->conn);
1088 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1089 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1090 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1091 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1092 c->null_cursor = xcb_generate_id(c->conn);
1093 xcb_create_cursor (c->conn, c->null_cursor,
1094 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1095 xcb_free_gc(c->conn, gc);
1096 xcb_free_pixmap(c->conn, pixmap);
1097}
1098
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001099static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001100x11_restore(struct weston_compositor *ec)
1101{
1102}
1103
1104static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001105x11_free_configured_output(struct x11_configured_output *output)
1106{
1107 free(output->name);
1108 free(output);
1109}
1110
1111static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001112x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001113{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001114 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001115 struct x11_configured_output *o, *n;
1116
1117 wl_list_for_each_safe(o, n, &configured_output_list, link)
1118 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001119
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001120 wl_event_source_remove(compositor->xcb_source);
1121 x11_input_destroy(compositor);
1122
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001123 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001124
1125 x11_compositor_fini_egl(compositor);
1126
1127 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001128 free(ec);
1129}
1130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001132x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001133 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001134 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001135 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001136{
1137 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001138 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001139 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001140 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001141 int i, x = 0, output_count = 0;
1142 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001143
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001144 weston_log("initializing x11 backend\n");
1145
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001146 c = malloc(sizeof *c);
1147 if (c == NULL)
1148 return NULL;
1149
1150 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001151
Daniel Stone725c2c32012-06-22 14:04:36 +01001152 if (weston_compositor_init(&c->base, display, argc, argv,
1153 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001154 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001155
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001156 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001157 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001158 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001159
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001160 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001161 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1162
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001163 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001164 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001165
1166 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1167 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001168 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001169
1170 x11_compositor_get_resources(c);
1171
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001172 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001173 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001174 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001175
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001176 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001177 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001178
Daniel Stone22815f92012-06-22 13:21:34 +01001179 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001180 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001181
Scott Moreau248aaec2012-08-03 14:19:52 -06001182 width = option_width ? option_width : 1024;
1183 height = option_height ? option_height : 640;
1184 count = option_count ? option_count : 1;
1185
1186 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001187 output = x11_compositor_create_output(c, x, 0,
1188 option_width ? width :
1189 o->width,
1190 option_height ? height :
1191 o->height,
1192 fullscreen, no_input,
1193 o->name, o->transform);
1194 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001195 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001196
1197 x = pixman_region32_extents(&output->base.region)->x2;
1198
Scott Moreau248aaec2012-08-03 14:19:52 -06001199 output_count++;
1200 if (option_count && output_count >= option_count)
1201 break;
1202 }
1203
1204 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001205 output = x11_compositor_create_output(c, x, 0, width, height,
1206 fullscreen, no_input, NULL,
1207 WL_OUTPUT_TRANSFORM_NORMAL);
1208 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001209 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001210 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001211 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001212
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001213 if (gles2_renderer_init(&c->base) < 0)
1214 goto err_egl;
1215
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001216 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001217 wl_event_loop_add_fd(c->base.input_loop,
1218 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001219 WL_EVENT_READABLE,
1220 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001221 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001222
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001223 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001224
1225err_x11_input:
1226 x11_input_destroy(c);
1227err_egl:
1228 x11_compositor_fini_egl(c);
1229err_xdisplay:
1230 XCloseDisplay(c->dpy);
1231err_free:
1232 free(c);
1233 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001234}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001235
Scott Moreau248aaec2012-08-03 14:19:52 -06001236static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001237x11_output_set_transform(struct x11_configured_output *output)
1238{
1239 if (!output_transform) {
1240 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1241 return;
1242 }
1243
1244 if (!strcmp(output_transform, "normal"))
1245 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1246 else if (!strcmp(output_transform, "90"))
1247 output->transform = WL_OUTPUT_TRANSFORM_90;
1248 else if (!strcmp(output_transform, "180"))
1249 output->transform = WL_OUTPUT_TRANSFORM_180;
1250 else if (!strcmp(output_transform, "270"))
1251 output->transform = WL_OUTPUT_TRANSFORM_270;
1252 else if (!strcmp(output_transform, "flipped"))
1253 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1254 else if (!strcmp(output_transform, "flipped-90"))
1255 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1256 else if (!strcmp(output_transform, "flipped-180"))
1257 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1258 else if (!strcmp(output_transform, "flipped-270"))
1259 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1260 else {
1261 weston_log("Invalid transform \"%s\" for output %s\n",
1262 output_transform, output_name);
1263 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1264 }
1265}
1266
1267static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001268output_section_done(void *data)
1269{
1270 struct x11_configured_output *output;
1271
1272 output = malloc(sizeof *output);
1273
Scott Moreau1bad5db2012-08-18 01:04:05 -06001274 if (!output || !output_name || (output_name[0] != 'X') ||
1275 (!output_mode && !output_transform)) {
1276 if (output_name)
1277 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001278 output_name = NULL;
John Kåre Alsaker5e48a852012-10-12 12:25:11 +02001279 free(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001280 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001281 }
1282
1283 output->name = output_name;
1284
Scott Moreau1bad5db2012-08-18 01:04:05 -06001285 if (output_mode) {
1286 if (sscanf(output_mode, "%dx%d", &output->width,
1287 &output->height) != 2) {
1288 weston_log("Invalid mode \"%s\" for output %s\n",
1289 output_mode, output_name);
1290 x11_free_configured_output(output);
1291 goto err_free;
1292 }
1293 } else {
1294 output->width = 1024;
1295 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001296 }
1297
Scott Moreau1bad5db2012-08-18 01:04:05 -06001298 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001299
Scott Moreauca9b09d2012-08-06 01:27:06 -06001300 wl_list_insert(configured_output_list.prev, &output->link);
1301
1302err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001303 if (output_mode)
1304 free(output_mode);
1305 if (output_transform)
1306 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001307 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001308 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001309}
1310
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001311WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001312backend_init(struct wl_display *display, int argc, char *argv[],
1313 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001314{
Scott Moreau248aaec2012-08-03 14:19:52 -06001315 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001316 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001317
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001318 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001319 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1320 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001321 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001322 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001323 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001324 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001325
1326 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001327
Scott Moreau248aaec2012-08-03 14:19:52 -06001328 wl_list_init(&configured_output_list);
1329
1330 const struct config_key x11_config_keys[] = {
1331 { "name", CONFIG_KEY_STRING, &output_name },
1332 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001333 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001334 };
1335
1336 const struct config_section config_section[] = {
1337 { "output", x11_config_keys,
1338 ARRAY_LENGTH(x11_config_keys), output_section_done },
1339 };
1340
1341 parse_config_file(config_file, config_section,
1342 ARRAY_LENGTH(config_section), NULL);
1343
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001344 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001345 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001346 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001347 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001348}