blob: c575f25edad98fc6f52feccfdaf120dad66dc64c [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;
Daniel Stone576cd152012-06-01 12:14:02 +0100118 struct xkb_rule_names names;
119 struct xkb_keymap *ret;
120 const char *value_all, *value_part;
121 int length_all, length_part;
122
123 memset(&names, 0, sizeof(names));
124
125 cookie = xcb_get_property(c->conn, 0, c->screen->root,
126 c->atom.xkb_names, c->atom.string, 0, 1024);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200127 reply = xcb_get_property_reply(c->conn, cookie, NULL);
Daniel Stone576cd152012-06-01 12:14:02 +0100128 if (reply == NULL)
129 return NULL;
130
131 value_all = xcb_get_property_value(reply);
132 length_all = xcb_get_property_value_length(reply);
133 value_part = value_all;
134
135#define copy_prop_value(to) \
136 length_part = strlen(value_part); \
137 if (value_part + length_part < (value_all + length_all) && \
138 length_part > 0) \
139 names.to = value_part; \
140 value_part += length_part + 1;
141
142 copy_prop_value(rules);
143 copy_prop_value(model);
144 copy_prop_value(layout);
145 copy_prop_value(variant);
146 copy_prop_value(options);
147#undef copy_prop_value
148
149 ret = xkb_map_new_from_names(c->base.xkb_context, &names, 0);
150
151 free(reply);
152 return ret;
153}
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400154
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400155static uint32_t
156get_xkb_mod_mask(struct x11_compositor *c, uint32_t in)
157{
158 struct weston_xkb_info *info = &c->core_seat.xkb_info;
159 uint32_t ret = 0;
160
161 if ((in & ShiftMask) && info->shift_mod != XKB_MOD_INVALID)
162 ret |= (1 << info->shift_mod);
163 if ((in & LockMask) && info->caps_mod != XKB_MOD_INVALID)
164 ret |= (1 << info->caps_mod);
165 if ((in & ControlMask) && info->ctrl_mod != XKB_MOD_INVALID)
166 ret |= (1 << info->ctrl_mod);
167 if ((in & Mod1Mask) && info->alt_mod != XKB_MOD_INVALID)
168 ret |= (1 << info->alt_mod);
169 if ((in & Mod2Mask) && info->mod2_mod != XKB_MOD_INVALID)
170 ret |= (1 << info->mod2_mod);
171 if ((in & Mod3Mask) && info->mod3_mod != XKB_MOD_INVALID)
172 ret |= (1 << info->mod3_mod);
173 if ((in & Mod4Mask) && info->super_mod != XKB_MOD_INVALID)
174 ret |= (1 << info->super_mod);
175 if ((in & Mod5Mask) && info->mod5_mod != XKB_MOD_INVALID)
176 ret |= (1 << info->mod5_mod);
177
178 return ret;
179}
180
Daniel Stone62b33b62012-06-22 13:21:35 +0100181static void
182x11_compositor_setup_xkb(struct x11_compositor *c)
183{
184#ifndef HAVE_XCB_XKB
185 weston_log("XCB-XKB not available during build\n");
186 c->has_xkb = 0;
187 c->xkb_event_base = 0;
188 return;
189#else
190 const xcb_query_extension_reply_t *ext;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100191 xcb_generic_error_t *error;
Daniel Stonee2faa122012-06-22 13:21:39 +0100192 xcb_void_cookie_t select;
Ran Benita6a39d872012-10-31 20:14:57 +0200193 xcb_xkb_use_extension_cookie_t use_ext;
194 xcb_xkb_use_extension_reply_t *use_ext_reply;
Daniel Stone3ee91e12012-06-22 13:21:36 +0100195 xcb_xkb_per_client_flags_cookie_t pcf;
196 xcb_xkb_per_client_flags_reply_t *pcf_reply;
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400197 xcb_xkb_get_state_cookie_t state;
198 xcb_xkb_get_state_reply_t *state_reply;
Daniel Stone62b33b62012-06-22 13:21:35 +0100199
200 c->has_xkb = 0;
201 c->xkb_event_base = 0;
202
203 ext = xcb_get_extension_data(c->conn, &xcb_xkb_id);
204 if (!ext) {
205 weston_log("XKB extension not available on host X11 server\n");
206 return;
207 }
208 c->xkb_event_base = ext->first_event;
209
Ran Benita424ae012012-10-31 00:13:08 +0200210 select = xcb_xkb_select_events_checked(c->conn,
211 XCB_XKB_ID_USE_CORE_KBD,
212 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
213 0,
214 XCB_XKB_EVENT_TYPE_STATE_NOTIFY,
215 0,
216 0,
217 NULL);
Daniel Stonee2faa122012-06-22 13:21:39 +0100218 error = xcb_request_check(c->conn, select);
219 if (error) {
220 weston_log("error: failed to select for XKB state events\n");
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200221 free(error);
Daniel Stonee2faa122012-06-22 13:21:39 +0100222 return;
223 }
224
Ran Benita6a39d872012-10-31 20:14:57 +0200225 use_ext = xcb_xkb_use_extension(c->conn,
226 XCB_XKB_MAJOR_VERSION,
227 XCB_XKB_MINOR_VERSION);
228 use_ext_reply = xcb_xkb_use_extension_reply(c->conn, use_ext, NULL);
229 if (!use_ext_reply) {
230 weston_log("couldn't start using XKB extension\n");
231 return;
232 }
233
234 if (!use_ext_reply->supported) {
235 weston_log("XKB extension version on the server is too old "
236 "(want %d.%d, has %d.%d)\n",
237 XCB_XKB_MAJOR_VERSION, XCB_XKB_MINOR_VERSION,
238 use_ext_reply->serverMajor, use_ext_reply->serverMinor);
239 free(use_ext_reply);
240 return;
241 }
242 free(use_ext_reply);
243
Daniel Stone3ee91e12012-06-22 13:21:36 +0100244 pcf = xcb_xkb_per_client_flags(c->conn,
245 XCB_XKB_ID_USE_CORE_KBD,
246 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
247 XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
248 0,
249 0,
250 0);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200251 pcf_reply = xcb_xkb_per_client_flags_reply(c->conn, pcf, NULL);
Ran Benita7109cc82012-10-31 20:14:58 +0200252 if (!pcf_reply ||
253 !(pcf_reply->value & XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT)) {
Daniel Stone3ee91e12012-06-22 13:21:36 +0100254 weston_log("failed to set XKB per-client flags, not using "
255 "detectable repeat\n");
Ran Benita7109cc82012-10-31 20:14:58 +0200256 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100257 return;
258 }
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200259 free(pcf_reply);
Daniel Stone3ee91e12012-06-22 13:21:36 +0100260
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400261 state = xcb_xkb_get_state(c->conn, XCB_XKB_ID_USE_CORE_KBD);
Ran Benita7b5e3cd2012-10-31 20:14:56 +0200262 state_reply = xcb_xkb_get_state_reply(c->conn, state, NULL);
263 if (!state_reply) {
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400264 weston_log("failed to get initial XKB state\n");
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400265 return;
266 }
267
268 xkb_state_update_mask(c->core_seat.xkb_state.state,
269 get_xkb_mod_mask(c, state_reply->baseMods),
270 get_xkb_mod_mask(c, state_reply->latchedMods),
271 get_xkb_mod_mask(c, state_reply->lockedMods),
272 0,
273 0,
274 state_reply->group);
275
276 free(state_reply);
277
Daniel Stone62b33b62012-06-22 13:21:35 +0100278 c->has_xkb = 1;
279#endif
280}
281
Darxus55973f22010-11-22 21:24:39 -0500282static int
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300283x11_input_create(struct x11_compositor *c, int no_input)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400284{
Daniel Stone576cd152012-06-01 12:14:02 +0100285 struct xkb_keymap *keymap;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400286
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400287 weston_seat_init(&c->core_seat, &c->base);
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300288
289 if (no_input)
290 return 0;
291
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400292 weston_seat_init_pointer(&c->core_seat);
Daniel Stone576cd152012-06-01 12:14:02 +0100293
294 keymap = x11_compositor_get_keymap(c);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400295 weston_seat_init_keyboard(&c->core_seat, keymap);
Daniel Stone576cd152012-06-01 12:14:02 +0100296 if (keymap)
297 xkb_map_unref(keymap);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400298
Kristian Høgsbergaac86932012-10-29 14:15:40 -0400299 x11_compositor_setup_xkb(c);
300
Darxus55973f22010-11-22 21:24:39 -0500301 return 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400302}
303
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200304static void
305x11_input_destroy(struct x11_compositor *compositor)
306{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400307 weston_seat_release(&compositor->core_seat);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200308}
309
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400310static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400311x11_compositor_init_egl(struct x11_compositor *c)
312{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400313 EGLint major, minor;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100314 EGLint n;
Benjamin Franzke84290d02011-03-02 10:07:59 +0100315 EGLint config_attribs[] = {
316 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
317 EGL_RED_SIZE, 1,
318 EGL_GREEN_SIZE, 1,
319 EGL_BLUE_SIZE, 1,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100320 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
321 EGL_NONE
322 };
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400323
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400324 c->base.egl_display = eglGetDisplay(c->dpy);
325 if (c->base.egl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +0200326 weston_log("failed to create display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400327 return -1;
328 }
329
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400330 if (!eglInitialize(c->base.egl_display, &major, &minor)) {
Martin Minarik6d118362012-06-07 18:01:59 +0200331 weston_log("failed to initialize display\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400332 return -1;
333 }
334
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400335 if (!eglChooseConfig(c->base.egl_display, config_attribs,
336 &c->base.egl_config, 1, &n) || n == 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200337 weston_log("failed to choose config: %d\n", n);
Benjamin Franzke84290d02011-03-02 10:07:59 +0100338 return -1;
339 }
Darxus55973f22010-11-22 21:24:39 -0500340
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400341 return 0;
342}
343
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200344static void
345x11_compositor_fini_egl(struct x11_compositor *compositor)
346{
Kristian Høgsberg3a0de882012-09-06 21:44:24 -0400347 gles2_renderer_destroy(&compositor->base);
348
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400349 eglMakeCurrent(compositor->base.egl_display,
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200350 EGL_NO_SURFACE, EGL_NO_SURFACE,
351 EGL_NO_CONTEXT);
352
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400353 eglTerminate(compositor->base.egl_display);
Pekka Paalanen43c61d82012-01-03 11:58:34 +0200354 eglReleaseThread();
355}
356
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500357static void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500358x11_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400359 pixman_region32_t *damage)
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500360{
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500361 struct x11_output *output = (struct x11_output *)output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400362 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500363
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400364 ec->renderer->repaint_output(output_base, damage);
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500365
366 wl_event_source_timer_update(output->finish_frame_timer, 10);
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500367}
368
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100369static int
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400370finish_frame_handler(void *data)
371{
372 struct x11_output *output = data;
373 uint32_t msec;
374 struct timeval tv;
375
376 gettimeofday(&tv, NULL);
377 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500378 weston_output_finish_frame(&output->base, msec);
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400379
380 return 1;
381}
382
Matt Roper361d2ad2011-08-29 13:52:23 -0700383static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500384x11_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700385{
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200386 struct x11_output *output = (struct x11_output *)output_base;
387 struct x11_compositor *compositor =
388 (struct x11_compositor *)output->base.compositor;
389
390 wl_list_remove(&output->base.link);
391 wl_event_source_remove(output->finish_frame_timer);
392
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400393 eglDestroySurface(compositor->base.egl_display,
394 output->base.egl_surface);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200395
396 xcb_destroy_window(compositor->conn, output->window);
397
Kristian Høgsberg3466bc82012-01-03 11:29:15 -0500398 weston_output_destroy(&output->base);
Pekka Paalanen2de99e22012-01-03 11:51:03 +0200399
400 free(output);
Matt Roper361d2ad2011-08-29 13:52:23 -0700401}
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200402
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400403static void
404x11_output_set_wm_protocols(struct x11_output *output)
405{
406 xcb_atom_t list[1];
407 struct x11_compositor *c =
408 (struct x11_compositor *) output->base.compositor;
409
410 list[0] = c->atom.wm_delete_window;
411 xcb_change_property (c->conn,
412 XCB_PROP_MODE_REPLACE,
413 output->window,
414 c->atom.wm_protocols,
415 XCB_ATOM_ATOM,
416 32,
Kristian Høgsberg09e26922011-12-23 13:33:45 -0500417 ARRAY_LENGTH(list),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400418 list);
419}
420
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400421static void
422x11_output_change_state(struct x11_output *output, int add, xcb_atom_t state)
423{
424 xcb_client_message_event_t event;
425 struct x11_compositor *c =
426 (struct x11_compositor *) output->base.compositor;
427 xcb_screen_iterator_t iter;
428
429#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
430#define _NET_WM_STATE_ADD 1 /* add/set property */
431#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
432
433 memset(&event, 0, sizeof event);
434 event.response_type = XCB_CLIENT_MESSAGE;
435 event.format = 32;
436 event.window = output->window;
437 event.type = c->atom.net_wm_state;
438
439 event.data.data32[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
440 event.data.data32[1] = state;
441 event.data.data32[2] = 0;
442 event.data.data32[3] = 0;
443 event.data.data32[4] = 0;
444
445 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
446 xcb_send_event(c->conn, 0, iter.data->root,
447 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
448 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
449 (void *) &event);
450}
451
452
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400453struct wm_normal_hints {
454 uint32_t flags;
455 uint32_t pad[4];
456 int32_t min_width, min_height;
457 int32_t max_width, max_height;
458 int32_t width_inc, height_inc;
459 int32_t min_aspect_x, min_aspect_y;
460 int32_t max_aspect_x, max_aspect_y;
461 int32_t base_width, base_height;
462 int32_t win_gravity;
463};
464
465#define WM_NORMAL_HINTS_MIN_SIZE 16
466#define WM_NORMAL_HINTS_MAX_SIZE 32
467
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500468static void
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400469x11_output_set_icon(struct x11_compositor *c,
470 struct x11_output *output, const char *filename)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500471{
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400472 uint32_t *icon;
Kristian Høgsbergb41d76c2011-04-23 15:03:15 -0400473 int32_t width, height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400474 pixman_image_t *image;
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500475
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400476 image = load_image(filename);
477 if (!image)
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500478 return;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400479 width = pixman_image_get_width(image);
480 height = pixman_image_get_height(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500481 icon = malloc(width * height * 4 + 8);
482 if (!icon) {
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400483 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500484 return;
485 }
486
487 icon[0] = width;
488 icon[1] = height;
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400489 memcpy(icon + 2, pixman_image_get_data(image), width * height * 4);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500490 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
491 c->atom.net_wm_icon, c->atom.cardinal, 32,
492 width * height + 2, icon);
493 free(icon);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400494 pixman_image_unref(image);
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500495}
496
Scott Moreau1bad5db2012-08-18 01:04:05 -0600497static struct x11_output *
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -0700498x11_compositor_create_output(struct x11_compositor *c, int x, int y,
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300499 int width, int height, int fullscreen,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600500 int no_input, char *configured_name,
501 uint32_t transform)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400502{
Scott Moreau1bad5db2012-08-18 01:04:05 -0600503 static const char name[] = "Weston Compositor";
Kristian Høgsberg86000402012-01-03 23:24:14 -0500504 static const char class[] = "weston-1\0Weston Compositor";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600505 char title[32];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400506 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400507 xcb_screen_iterator_t iter;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400508 struct wm_normal_hints normal_hints;
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400509 struct wl_event_loop *loop;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400510 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300511 uint32_t values[2] = {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400512 XCB_EVENT_MASK_EXPOSURE |
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300513 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400514 0
515 };
516
Scott Moreau1bad5db2012-08-18 01:04:05 -0600517 if (configured_name)
518 sprintf(title, "%s - %s", name, configured_name);
519 else
520 strcpy(title, name);
521
Pekka Paalanen36f155f2012-06-07 15:07:05 +0300522 if (!no_input)
523 values[0] |=
524 XCB_EVENT_MASK_KEY_PRESS |
525 XCB_EVENT_MASK_KEY_RELEASE |
526 XCB_EVENT_MASK_BUTTON_PRESS |
527 XCB_EVENT_MASK_BUTTON_RELEASE |
528 XCB_EVENT_MASK_POINTER_MOTION |
529 XCB_EVENT_MASK_ENTER_WINDOW |
530 XCB_EVENT_MASK_LEAVE_WINDOW |
531 XCB_EVENT_MASK_KEYMAP_STATE |
532 XCB_EVENT_MASK_FOCUS_CHANGE;
533
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400534 output = malloc(sizeof *output);
535 if (output == NULL)
Scott Moreau1bad5db2012-08-18 01:04:05 -0600536 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400537
538 memset(output, 0, sizeof *output);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400539
540 output->mode.flags =
541 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
542 output->mode.width = width;
543 output->mode.height = height;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -0400544 output->mode.refresh = 60000;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400545 wl_list_init(&output->base.mode_list);
546 wl_list_insert(&output->base.mode_list, &output->mode.link);
547
548 output->base.current = &output->mode;
Kristian Høgsberg12481582012-07-02 21:24:57 -0400549 output->base.make = "xwayland";
550 output->base.model = "none";
Scott Moreau1bad5db2012-08-18 01:04:05 -0600551 weston_output_init(&output->base, &c->base,
552 x, y, width, height, transform);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400553
554 values[1] = c->null_cursor;
555 output->window = xcb_generate_id(c->conn);
556 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
557 xcb_create_window(c->conn,
558 XCB_COPY_FROM_PARENT,
559 output->window,
560 iter.data->root,
561 0, 0,
562 width, height,
563 0,
564 XCB_WINDOW_CLASS_INPUT_OUTPUT,
565 iter.data->root_visual,
566 mask, values);
567
568 /* Don't resize me. */
569 memset(&normal_hints, 0, sizeof normal_hints);
570 normal_hints.flags =
571 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
572 normal_hints.min_width = width;
573 normal_hints.min_height = height;
574 normal_hints.max_width = width;
575 normal_hints.max_height = height;
576 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
577 c->atom.wm_normal_hints,
578 c->atom.wm_size_hints, 32,
579 sizeof normal_hints / 4,
580 (uint8_t *) &normal_hints);
581
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400582 /* Set window name. Don't bother with non-EWMH WMs. */
583 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
584 c->atom.net_wm_name, c->atom.utf8_string, 8,
Scott Moreau1bad5db2012-08-18 01:04:05 -0600585 strlen(title), title);
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500586 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
587 c->atom.wm_class, c->atom.string, 8,
588 sizeof class, class);
589
Kristian Høgsberg86000402012-01-03 23:24:14 -0500590 x11_output_set_icon(c, output, DATADIR "/weston/wayland.png");
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -0500591
Kristian Høgsberg24ed6212011-01-26 14:02:31 -0500592 xcb_map_window(c->conn, output->window);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400593
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400594 x11_output_set_wm_protocols(output);
595
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -0400596 if (fullscreen)
597 x11_output_change_state(output, 1,
598 c->atom.net_wm_state_fullscreen);
599
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400600 output->base.egl_surface =
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400601 eglCreateWindowSurface(c->base.egl_display, c->base.egl_config,
Benjamin Franzke84290d02011-03-02 10:07:59 +0100602 output->window, NULL);
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400603 if (!output->base.egl_surface) {
Martin Minarik6d118362012-06-07 18:01:59 +0200604 weston_log("failed to create window surface\n");
Scott Moreau1bad5db2012-08-18 01:04:05 -0600605 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400606 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400607
Kristian Høgsberg06a670f2011-10-29 14:39:13 -0400608 loop = wl_display_get_event_loop(c->base.wl_display);
609 output->finish_frame_timer =
610 wl_event_loop_add_timer(loop, finish_frame_handler, output);
611
Alex Wubd3354b2012-04-17 17:20:49 +0800612 output->base.origin = output->base.current;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500613 output->base.repaint = x11_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -0700614 output->base.destroy = x11_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800615 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200616 output->base.set_backlight = NULL;
617 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800618 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100619
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400620 wl_list_insert(c->base.output_list.prev, &output->base.link);
621
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -0400622 weston_log("x11 output %dx%d, window id %d\n",
623 width, height, output->window);
624
Scott Moreau1bad5db2012-08-18 01:04:05 -0600625 return output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400626}
627
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400628static struct x11_output *
629x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
630{
631 struct x11_output *output;
632
633 wl_list_for_each(output, &c->base.output_list, base.link) {
634 if (output->window == window)
635 return output;
636 }
637
638 return NULL;
639}
640
Daniel Stone154c34c2012-06-22 13:21:40 +0100641#ifdef HAVE_XCB_XKB
Daniel Stonee2faa122012-06-22 13:21:39 +0100642static void
643update_xkb_state(struct x11_compositor *c, xcb_xkb_state_notify_event_t *state)
644{
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400645 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stonee2faa122012-06-22 13:21:39 +0100646 get_xkb_mod_mask(c, state->baseMods),
647 get_xkb_mod_mask(c, state->latchedMods),
648 get_xkb_mod_mask(c, state->lockedMods),
649 0,
650 0,
651 state->group);
652
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400653 notify_modifiers(&c->core_seat,
654 wl_display_next_serial(c->base.wl_display));
Daniel Stonee2faa122012-06-22 13:21:39 +0100655}
656#endif
657
Daniel Stone154c34c2012-06-22 13:21:40 +0100658/**
659 * This is monumentally unpleasant. If we don't have XCB-XKB bindings,
660 * the best we can do (given that XCB also lacks XI2 support), is to take
661 * the state from the core key events. Unfortunately that only gives us
662 * the effective (i.e. union of depressed/latched/locked) state, and we
663 * need the granularity.
664 *
665 * So we still update the state with every key event we see, but also use
666 * the state field from X11 events as a mask so we don't get any stuck
667 * modifiers.
668 */
669static void
670update_xkb_state_from_core(struct x11_compositor *c, uint16_t x11_mask)
671{
672 uint32_t mask = get_xkb_mod_mask(c, x11_mask);
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400673 struct wl_keyboard *keyboard = &c->core_seat.keyboard;
Daniel Stone154c34c2012-06-22 13:21:40 +0100674
Kristian Høgsberg4f92c532012-08-10 10:04:36 -0400675 xkb_state_update_mask(c->core_seat.xkb_state.state,
Daniel Stone154c34c2012-06-22 13:21:40 +0100676 keyboard->modifiers.mods_depressed & mask,
677 keyboard->modifiers.mods_latched & mask,
678 keyboard->modifiers.mods_locked & mask,
679 0,
680 0,
681 (x11_mask >> 13) & 3);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400682 notify_modifiers(&c->core_seat,
Daniel Stone154c34c2012-06-22 13:21:40 +0100683 wl_display_next_serial(c->base.wl_display));
684}
685
Tiago Vignattia3a71622011-12-08 17:03:17 +0200686static void
687x11_compositor_deliver_button_event(struct x11_compositor *c,
688 xcb_generic_event_t *event, int state)
689{
690 xcb_button_press_event_t *button_event =
691 (xcb_button_press_event_t *) event;
Daniel Stone5d663712012-05-04 11:21:55 +0100692 uint32_t button;
Kristian Høgsberg73308622012-10-30 11:04:52 -0400693 struct x11_output *output;
694
695 output = x11_compositor_find_output(c, button_event->event);
696
697 if (state)
698 xcb_grab_pointer(c->conn, 0, output->window,
699 XCB_EVENT_MASK_BUTTON_PRESS |
700 XCB_EVENT_MASK_BUTTON_RELEASE |
701 XCB_EVENT_MASK_POINTER_MOTION |
702 XCB_EVENT_MASK_ENTER_WINDOW |
703 XCB_EVENT_MASK_LEAVE_WINDOW,
704 XCB_GRAB_MODE_ASYNC,
705 XCB_GRAB_MODE_ASYNC,
706 output->window, XCB_CURSOR_NONE,
707 button_event->time);
708 else
709 xcb_ungrab_pointer(c->conn, button_event->time);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200710
Daniel Stone154c34c2012-06-22 13:21:40 +0100711 if (!c->has_xkb)
712 update_xkb_state_from_core(c, button_event->state);
713
Tiago Vignattia3a71622011-12-08 17:03:17 +0200714 switch (button_event->detail) {
715 default:
716 button = button_event->detail + BTN_LEFT - 1;
717 break;
718 case 2:
719 button = BTN_MIDDLE;
720 break;
721 case 3:
722 button = BTN_RIGHT;
723 break;
724 case 4:
Jonas Ådahl62efe202012-09-27 18:40:41 +0200725 /* Axis are measured in pixels, but the xcb events are discrete
726 * steps. Therefore move the axis by some pixels every step. */
Scott Moreau210d0792012-03-22 10:47:01 -0600727 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400728 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200729 weston_compositor_get_time(),
730 WL_POINTER_AXIS_VERTICAL_SCROLL,
731 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600732 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200733 case 5:
Scott Moreau210d0792012-03-22 10:47:01 -0600734 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400735 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200736 weston_compositor_get_time(),
737 WL_POINTER_AXIS_VERTICAL_SCROLL,
738 DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600739 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200740 case 6:
Scott Moreau210d0792012-03-22 10:47:01 -0600741 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400742 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200743 weston_compositor_get_time(),
744 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
745 -DEFAULT_AXIS_STEP_DISTANCE);
Scott Moreau210d0792012-03-22 10:47:01 -0600746 return;
Tiago Vignattia3a71622011-12-08 17:03:17 +0200747 case 7:
Scott Moreau210d0792012-03-22 10:47:01 -0600748 if (state)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400749 notify_axis(&c->core_seat,
Jonas Ådahl62efe202012-09-27 18:40:41 +0200750 weston_compositor_get_time(),
751 WL_POINTER_AXIS_HORIZONTAL_SCROLL,
752 DEFAULT_AXIS_STEP_DISTANCE);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200753 return;
754 }
755
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400756 notify_button(&c->core_seat,
Daniel Stone4dbadb12012-05-30 16:31:51 +0100757 weston_compositor_get_time(), button,
758 state ? WL_POINTER_BUTTON_STATE_PRESSED :
759 WL_POINTER_BUTTON_STATE_RELEASED);
Tiago Vignattia3a71622011-12-08 17:03:17 +0200760}
761
Scott Moreau1bad5db2012-08-18 01:04:05 -0600762static void
763x11_output_transform_coordinate(struct x11_output *x11_output,
764 wl_fixed_t *x, wl_fixed_t *y)
765{
766 struct weston_output *output = &x11_output->base;
767 wl_fixed_t tx, ty;
768 wl_fixed_t width = wl_fixed_from_int(output->width - 1);
769 wl_fixed_t height = wl_fixed_from_int(output->height - 1);
770
771 switch(output->transform) {
772 case WL_OUTPUT_TRANSFORM_NORMAL:
773 default:
774 tx = *x;
775 ty = *y;
776 break;
777 case WL_OUTPUT_TRANSFORM_90:
778 tx = *y;
779 ty = height - *x;
780 break;
781 case WL_OUTPUT_TRANSFORM_180:
782 tx = width - *x;
783 ty = height - *y;
784 break;
785 case WL_OUTPUT_TRANSFORM_270:
786 tx = width - *y;
787 ty = *x;
788 break;
789 case WL_OUTPUT_TRANSFORM_FLIPPED:
790 tx = width - *x;
791 ty = *y;
792 break;
793 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
794 tx = width - *y;
795 ty = height - *x;
796 break;
797 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
798 tx = *x;
799 ty = height - *y;
800 break;
801 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
802 tx = *y;
803 ty = *x;
804 break;
805 }
806
807 tx += wl_fixed_from_int(output->x);
808 ty += wl_fixed_from_int(output->y);
809
810 *x = tx;
811 *y = ty;
812}
813
814static void
815x11_compositor_deliver_motion_event(struct x11_compositor *c,
816 xcb_generic_event_t *event)
817{
818 struct x11_output *output;
819 wl_fixed_t x, y;
820 xcb_motion_notify_event_t *motion_notify =
821 (xcb_motion_notify_event_t *) event;
822
823 if (!c->has_xkb)
824 update_xkb_state_from_core(c, motion_notify->state);
825 output = x11_compositor_find_output(c, motion_notify->event);
826 x = wl_fixed_from_int(motion_notify->event_x);
827 y = wl_fixed_from_int(motion_notify->event_y);
828 x11_output_transform_coordinate(output, &x, &y);
829
830 notify_motion(&c->core_seat, weston_compositor_get_time(), x, y);
831}
832
833static void
834x11_compositor_deliver_enter_event(struct x11_compositor *c,
835 xcb_generic_event_t *event)
836{
837 struct x11_output *output;
838 wl_fixed_t x, y;
839
840 xcb_enter_notify_event_t *enter_notify =
841 (xcb_enter_notify_event_t *) event;
842 if (enter_notify->state >= Button1Mask)
843 return;
844 if (!c->has_xkb)
845 update_xkb_state_from_core(c, enter_notify->state);
846 output = x11_compositor_find_output(c, enter_notify->event);
847 x = wl_fixed_from_int(enter_notify->event_x);
848 y = wl_fixed_from_int(enter_notify->event_y);
849 x11_output_transform_coordinate(output, &x, &y);
850
851 notify_pointer_focus(&c->core_seat, &output->base, x, y);
852}
853
Kristian Høgsbergee724822011-04-21 14:51:44 -0400854static int
855x11_compositor_next_event(struct x11_compositor *c,
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400856 xcb_generic_event_t **event, uint32_t mask)
Kristian Høgsbergee724822011-04-21 14:51:44 -0400857{
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400858 if (mask & WL_EVENT_READABLE) {
859 *event = xcb_poll_for_event(c->conn);
Kristian Høgsbergee724822011-04-21 14:51:44 -0400860 } else {
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400861#ifdef HAVE_XCB_POLL_FOR_QUEUED_EVENT
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400862 *event = xcb_poll_for_queued_event(c->conn);
Kristian Høgsberg82ed0422011-04-25 15:41:59 -0400863#else
864 *event = xcb_poll_for_event(c->conn);
865#endif
Kristian Høgsbergee724822011-04-21 14:51:44 -0400866 }
867
868 return *event != NULL;
869}
870
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400871static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400872x11_compositor_handle_event(int fd, uint32_t mask, void *data)
873{
874 struct x11_compositor *c = data;
875 struct x11_output *output;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400876 xcb_generic_event_t *event, *prev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400877 xcb_client_message_event_t *client_message;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -0500878 xcb_enter_notify_event_t *enter_notify;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400879 xcb_key_press_event_t *key_press, *key_release;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500880 xcb_keymap_notify_event_t *keymap_notify;
881 xcb_focus_in_event_t *focus_in;
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400882 xcb_expose_event_t *expose;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400883 xcb_atom_t atom;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -0500884 uint32_t *k;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -0400885 uint32_t i, set;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200886 uint8_t response_type;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400887 int count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400888
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400889 prev = NULL;
Bill Spitzakb715cec2012-06-05 17:08:23 -0400890 count = 0;
Kristian Høgsberg127d0f02011-04-22 12:18:13 -0400891 while (x11_compositor_next_event(c, &event, mask)) {
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200892 response_type = event->response_type & ~0x80;
893
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400894 switch (prev ? prev->response_type & ~0x80 : 0x80) {
895 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100896 /* Suppress key repeat events; this is only used if we
897 * don't have XCB XKB support. */
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400898 key_release = (xcb_key_press_event_t *) prev;
899 key_press = (xcb_key_press_event_t *) event;
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200900 if (response_type == XCB_KEY_PRESS &&
Dima Ryazanovc2247482011-08-16 17:25:32 -0700901 key_release->time == key_press->time &&
902 key_release->detail == key_press->detail) {
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400903 /* Don't deliver the held key release
904 * event or the new key press event. */
905 free(event);
906 free(prev);
907 prev = NULL;
908 continue;
909 } else {
910 /* Deliver the held key release now
911 * and fall through and handle the new
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400912 * event below. */
Daniel Stone154c34c2012-06-22 13:21:40 +0100913 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400914 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500915 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100916 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100917 WL_KEYBOARD_KEY_STATE_RELEASED,
918 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400919 free(prev);
920 prev = NULL;
921 break;
922 }
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400923
924 case XCB_FOCUS_IN:
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200925 assert(response_type == XCB_KEYMAP_NOTIFY);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400926 keymap_notify = (xcb_keymap_notify_event_t *) event;
927 c->keys.size = 0;
928 for (i = 0; i < ARRAY_LENGTH(keymap_notify->keys) * 8; i++) {
929 set = keymap_notify->keys[i >> 3] &
930 (1 << (i & 7));
931 if (set) {
932 k = wl_array_add(&c->keys, sizeof *k);
933 *k = i;
934 }
935 }
936
Daniel Stoned6da09e2012-06-22 13:21:29 +0100937 /* Unfortunately the state only comes with the enter
938 * event, rather than with the focus event. I'm not
939 * sure of the exact semantics around it and whether
940 * we can ensure that we get both? */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400941 notify_keyboard_focus_in(&c->core_seat, &c->keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +0100942 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg025f7b82011-04-19 12:38:22 -0400943
944 free(prev);
945 prev = NULL;
946 break;
947
Kristian Høgsberg94da7e12011-04-19 09:23:29 -0400948 default:
949 /* No previous event held */
950 break;
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -0400951 }
952
John Kåre Alsaker143a8982012-10-12 12:25:07 +0200953 switch (response_type) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400954 case XCB_KEY_PRESS:
955 key_press = (xcb_key_press_event_t *) event;
Daniel Stone154c34c2012-06-22 13:21:40 +0100956 if (!c->has_xkb)
957 update_xkb_state_from_core(c, key_press->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400958 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500959 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +0100960 key_press->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100961 WL_KEYBOARD_KEY_STATE_PRESSED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100962 c->has_xkb ? STATE_UPDATE_NONE :
963 STATE_UPDATE_AUTOMATIC);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400964 break;
965 case XCB_KEY_RELEASE:
Daniel Stone3ee91e12012-06-22 13:21:36 +0100966 /* If we don't have XKB, we need to use the lame
967 * autorepeat detection above. */
968 if (!c->has_xkb) {
969 prev = event;
970 break;
971 }
972 key_release = (xcb_key_press_event_t *) event;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400973 notify_key(&c->core_seat,
Daniel Stone3ee91e12012-06-22 13:21:36 +0100974 weston_compositor_get_time(),
975 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +0100976 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stonee2faa122012-06-22 13:21:39 +0100977 STATE_UPDATE_NONE);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400978 break;
979 case XCB_BUTTON_PRESS:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200980 x11_compositor_deliver_button_event(c, event, 1);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400981 break;
982 case XCB_BUTTON_RELEASE:
Tiago Vignattia3a71622011-12-08 17:03:17 +0200983 x11_compositor_deliver_button_event(c, event, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400984 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400985 case XCB_MOTION_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600986 x11_compositor_deliver_motion_event(c, event);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400987 break;
988
989 case XCB_EXPOSE:
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400990 expose = (xcb_expose_event_t *) event;
991 output = x11_compositor_find_output(c, expose->window);
992 weston_output_schedule_repaint(&output->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400993 break;
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400994
995 case XCB_ENTER_NOTIFY:
Scott Moreau1bad5db2012-08-18 01:04:05 -0600996 x11_compositor_deliver_enter_event(c, event);
Kristian Høgsberg86e09892010-07-07 09:51:11 -0400997 break;
998
999 case XCB_LEAVE_NOTIFY:
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001000 enter_notify = (xcb_enter_notify_event_t *) event;
Kristian Høgsberg2dfe6262011-02-08 11:59:53 -05001001 if (enter_notify->state >= Button1Mask)
1002 break;
Daniel Stone154c34c2012-06-22 13:21:40 +01001003 if (!c->has_xkb)
1004 update_xkb_state_from_core(c, enter_notify->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001005 notify_pointer_focus(&c->core_seat, NULL, 0, 0);
Kristian Høgsberg86e09892010-07-07 09:51:11 -04001006 break;
1007
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001008 case XCB_CLIENT_MESSAGE:
1009 client_message = (xcb_client_message_event_t *) event;
1010 atom = client_message->data.data32[0];
1011 if (atom == c->atom.wm_delete_window)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05001012 wl_display_terminate(c->base.wl_display);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001013 break;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001014
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001015 case XCB_FOCUS_IN:
1016 focus_in = (xcb_focus_in_event_t *) event;
1017 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED)
1018 break;
1019
Kristian Høgsberg025f7b82011-04-19 12:38:22 -04001020 prev = event;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001021 break;
1022
1023 case XCB_FOCUS_OUT:
1024 focus_in = (xcb_focus_in_event_t *) event;
Kristian Høgsberg42e40ae2011-12-19 14:36:50 -05001025 if (focus_in->mode == XCB_NOTIFY_MODE_WHILE_GRABBED ||
1026 focus_in->mode == XCB_NOTIFY_MODE_UNGRAB)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001027 break;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001028 notify_keyboard_focus_out(&c->core_seat);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001029 break;
1030
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001031 default:
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001032 break;
1033 }
1034
Daniel Stonee2faa122012-06-22 13:21:39 +01001035#ifdef HAVE_XCB_XKB
1036 if (c->has_xkb &&
John Kåre Alsaker143a8982012-10-12 12:25:07 +02001037 response_type == c->xkb_event_base) {
Daniel Stonee2faa122012-06-22 13:21:39 +01001038 xcb_xkb_state_notify_event_t *state =
1039 (xcb_xkb_state_notify_event_t *) event;
1040 if (state->xkbType == XCB_XKB_STATE_NOTIFY)
1041 update_xkb_state(c, state);
1042 }
1043#endif
1044
Bill Spitzakb715cec2012-06-05 17:08:23 -04001045 count++;
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001046 if (prev != event)
Kristian Høgsberg3ddd1482011-04-15 15:48:07 -04001047 free (event);
1048 }
1049
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001050 switch (prev ? prev->response_type & ~0x80 : 0x80) {
1051 case XCB_KEY_RELEASE:
1052 key_release = (xcb_key_press_event_t *) prev;
Daniel Stone154c34c2012-06-22 13:21:40 +01001053 update_xkb_state_from_core(c, key_release->state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001054 notify_key(&c->core_seat,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001055 weston_compositor_get_time(),
Daniel Stonec9785ea2012-05-30 16:31:52 +01001056 key_release->detail - 8,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001057 WL_KEYBOARD_KEY_STATE_RELEASED,
1058 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg94da7e12011-04-19 09:23:29 -04001059 free(prev);
1060 prev = NULL;
1061 break;
1062 default:
1063 break;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001064 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001065
Bill Spitzakb715cec2012-06-05 17:08:23 -04001066 return count;
Kristian Høgsbergee724822011-04-21 14:51:44 -04001067}
1068
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001069#define F(field) offsetof(struct x11_compositor, field)
1070
1071static void
1072x11_compositor_get_resources(struct x11_compositor *c)
1073{
1074 static const struct { const char *name; int offset; } atoms[] = {
1075 { "WM_PROTOCOLS", F(atom.wm_protocols) },
1076 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
1077 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
1078 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001079 { "WM_CLASS", F(atom.wm_class) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001080 { "_NET_WM_NAME", F(atom.net_wm_name) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001081 { "_NET_WM_ICON", F(atom.net_wm_icon) },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001082 { "_NET_WM_STATE", F(atom.net_wm_state) },
1083 { "_NET_WM_STATE_FULLSCREEN", F(atom.net_wm_state_fullscreen) },
Kristian Høgsberg24ed6212011-01-26 14:02:31 -05001084 { "STRING", F(atom.string) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001085 { "UTF8_STRING", F(atom.utf8_string) },
Kristian Høgsbergf58d8ca2011-01-26 14:37:07 -05001086 { "CARDINAL", F(atom.cardinal) },
Daniel Stone855028f2012-05-01 20:37:10 +01001087 { "_XKB_RULES_NAMES", F(atom.xkb_names) },
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001088 };
1089
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001090 xcb_intern_atom_cookie_t cookies[ARRAY_LENGTH(atoms)];
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001091 xcb_intern_atom_reply_t *reply;
1092 xcb_pixmap_t pixmap;
1093 xcb_gc_t gc;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001094 unsigned int i;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001095 uint8_t data[] = { 0, 0, 0, 0 };
1096
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001097 for (i = 0; i < ARRAY_LENGTH(atoms); i++)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001098 cookies[i] = xcb_intern_atom (c->conn, 0,
1099 strlen(atoms[i].name),
1100 atoms[i].name);
1101
Kristian Høgsberg09e26922011-12-23 13:33:45 -05001102 for (i = 0; i < ARRAY_LENGTH(atoms); i++) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001103 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
1104 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
1105 free(reply);
1106 }
1107
1108 pixmap = xcb_generate_id(c->conn);
1109 gc = xcb_generate_id(c->conn);
1110 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
1111 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
1112 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
1113 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
1114 c->null_cursor = xcb_generate_id(c->conn);
1115 xcb_create_cursor (c->conn, c->null_cursor,
1116 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
1117 xcb_free_gc(c->conn, gc);
1118 xcb_free_pixmap(c->conn, pixmap);
1119}
1120
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001121static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001122x11_restore(struct weston_compositor *ec)
1123{
1124}
1125
1126static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001127x11_free_configured_output(struct x11_configured_output *output)
1128{
1129 free(output->name);
1130 free(output);
1131}
1132
1133static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001134x11_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001135{
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001136 struct x11_compositor *compositor = (struct x11_compositor *)ec;
Scott Moreau248aaec2012-08-03 14:19:52 -06001137 struct x11_configured_output *o, *n;
1138
1139 wl_list_for_each_safe(o, n, &configured_output_list, link)
1140 x11_free_configured_output(o);
Matt Roper361d2ad2011-08-29 13:52:23 -07001141
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001142 wl_event_source_remove(compositor->xcb_source);
1143 x11_input_destroy(compositor);
1144
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05001145 weston_compositor_shutdown(ec); /* destroys outputs, too */
Pekka Paalanen43c61d82012-01-03 11:58:34 +02001146
1147 x11_compositor_fini_egl(compositor);
1148
1149 XCloseDisplay(compositor->dpy);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001150 free(ec);
1151}
1152
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001153static struct weston_compositor *
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001154x11_compositor_create(struct wl_display *display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001155 int fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001156 int no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001157 int argc, char *argv[], const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001158{
1159 struct x11_compositor *c;
Scott Moreau248aaec2012-08-03 14:19:52 -06001160 struct x11_configured_output *o;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001161 struct x11_output *output;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001162 xcb_screen_iterator_t s;
Scott Moreau248aaec2012-08-03 14:19:52 -06001163 int i, x = 0, output_count = 0;
1164 int width, height, count;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001165
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001166 weston_log("initializing x11 backend\n");
1167
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001168 c = malloc(sizeof *c);
1169 if (c == NULL)
1170 return NULL;
1171
1172 memset(c, 0, sizeof *c);
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001173
Daniel Stone725c2c32012-06-22 14:04:36 +01001174 if (weston_compositor_init(&c->base, display, argc, argv,
1175 config_file) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001176 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001177
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001178 c->dpy = XOpenDisplay(NULL);
Cyril Brulebois20798292011-04-06 18:05:40 +02001179 if (c->dpy == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001180 goto err_free;
Cyril Brulebois20798292011-04-06 18:05:40 +02001181
Benjamin Franzke3b288af2011-02-20 19:58:42 +01001182 c->conn = XGetXCBConnection(c->dpy);
Benjamin Franzkee997c5f2011-03-25 14:06:37 +01001183 XSetEventQueueOwner(c->dpy, XCBOwnsEventQueue);
1184
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001185 if (xcb_connection_has_error(c->conn))
Martin Olsson11434bb2012-07-08 03:03:44 +02001186 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001187
1188 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
1189 c->screen = s.data;
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001190 wl_array_init(&c->keys);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001191
1192 x11_compositor_get_resources(c);
1193
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001194 c->base.wl_display = display;
Tiago Vignatti997ce642010-11-10 02:42:35 +02001195 if (x11_compositor_init_egl(c) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001196 goto err_xdisplay;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001197
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001198 c->base.destroy = x11_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001199 c->base.restore = x11_restore;
Kristian Høgsberg8525a502011-01-14 16:20:21 -05001200
Daniel Stone22815f92012-06-22 13:21:34 +01001201 if (x11_input_create(c, no_input) < 0)
Martin Olsson11434bb2012-07-08 03:03:44 +02001202 goto err_egl;
Daniel Stone22815f92012-06-22 13:21:34 +01001203
Scott Moreau248aaec2012-08-03 14:19:52 -06001204 width = option_width ? option_width : 1024;
1205 height = option_height ? option_height : 640;
1206 count = option_count ? option_count : 1;
1207
1208 wl_list_for_each(o, &configured_output_list, link) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001209 output = x11_compositor_create_output(c, x, 0,
1210 option_width ? width :
1211 o->width,
1212 option_height ? height :
1213 o->height,
1214 fullscreen, no_input,
1215 o->name, o->transform);
1216 if (output == NULL)
Scott Moreau248aaec2012-08-03 14:19:52 -06001217 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001218
1219 x = pixman_region32_extents(&output->base.region)->x2;
1220
Scott Moreau248aaec2012-08-03 14:19:52 -06001221 output_count++;
1222 if (option_count && output_count >= option_count)
1223 break;
1224 }
1225
1226 for (i = output_count; i < count; i++) {
Scott Moreau1bad5db2012-08-18 01:04:05 -06001227 output = x11_compositor_create_output(c, x, 0, width, height,
1228 fullscreen, no_input, NULL,
1229 WL_OUTPUT_TRANSFORM_NORMAL);
1230 if (output == NULL)
Martin Olsson11434bb2012-07-08 03:03:44 +02001231 goto err_x11_input;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001232 x = pixman_region32_extents(&output->base.region)->x2;
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001233 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001234
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04001235 if (gles2_renderer_init(&c->base) < 0)
1236 goto err_egl;
1237
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001238 c->xcb_source =
Kristian Høgsberg22ba60e2012-03-12 01:18:24 -04001239 wl_event_loop_add_fd(c->base.input_loop,
1240 xcb_get_file_descriptor(c->conn),
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001241 WL_EVENT_READABLE,
1242 x11_compositor_handle_event, c);
Kristian Høgsberg127d0f02011-04-22 12:18:13 -04001243 wl_event_source_check(c->xcb_source);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001244
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001245 return &c->base;
Martin Olsson11434bb2012-07-08 03:03:44 +02001246
1247err_x11_input:
1248 x11_input_destroy(c);
1249err_egl:
1250 x11_compositor_fini_egl(c);
1251err_xdisplay:
1252 XCloseDisplay(c->dpy);
1253err_free:
1254 free(c);
1255 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001256}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001257
Scott Moreau248aaec2012-08-03 14:19:52 -06001258static void
Scott Moreau1bad5db2012-08-18 01:04:05 -06001259x11_output_set_transform(struct x11_configured_output *output)
1260{
1261 if (!output_transform) {
1262 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1263 return;
1264 }
1265
1266 if (!strcmp(output_transform, "normal"))
1267 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1268 else if (!strcmp(output_transform, "90"))
1269 output->transform = WL_OUTPUT_TRANSFORM_90;
1270 else if (!strcmp(output_transform, "180"))
1271 output->transform = WL_OUTPUT_TRANSFORM_180;
1272 else if (!strcmp(output_transform, "270"))
1273 output->transform = WL_OUTPUT_TRANSFORM_270;
1274 else if (!strcmp(output_transform, "flipped"))
1275 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
1276 else if (!strcmp(output_transform, "flipped-90"))
1277 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
1278 else if (!strcmp(output_transform, "flipped-180"))
1279 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
1280 else if (!strcmp(output_transform, "flipped-270"))
1281 output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
1282 else {
1283 weston_log("Invalid transform \"%s\" for output %s\n",
1284 output_transform, output_name);
1285 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1286 }
1287}
1288
1289static void
Scott Moreau248aaec2012-08-03 14:19:52 -06001290output_section_done(void *data)
1291{
1292 struct x11_configured_output *output;
1293
1294 output = malloc(sizeof *output);
1295
Scott Moreau1bad5db2012-08-18 01:04:05 -06001296 if (!output || !output_name || (output_name[0] != 'X') ||
1297 (!output_mode && !output_transform)) {
1298 if (output_name)
1299 free(output_name);
Scott Moreau248aaec2012-08-03 14:19:52 -06001300 output_name = NULL;
John Kåre Alsaker5e48a852012-10-12 12:25:11 +02001301 free(output);
Scott Moreauca9b09d2012-08-06 01:27:06 -06001302 goto err_free;
Scott Moreau248aaec2012-08-03 14:19:52 -06001303 }
1304
1305 output->name = output_name;
1306
Scott Moreau1bad5db2012-08-18 01:04:05 -06001307 if (output_mode) {
1308 if (sscanf(output_mode, "%dx%d", &output->width,
1309 &output->height) != 2) {
1310 weston_log("Invalid mode \"%s\" for output %s\n",
1311 output_mode, output_name);
1312 x11_free_configured_output(output);
1313 goto err_free;
1314 }
1315 } else {
1316 output->width = 1024;
1317 output->height = 640;
Scott Moreau248aaec2012-08-03 14:19:52 -06001318 }
1319
Scott Moreau1bad5db2012-08-18 01:04:05 -06001320 x11_output_set_transform(output);
Scott Moreau248aaec2012-08-03 14:19:52 -06001321
Scott Moreauca9b09d2012-08-06 01:27:06 -06001322 wl_list_insert(configured_output_list.prev, &output->link);
1323
1324err_free:
Scott Moreau1bad5db2012-08-18 01:04:05 -06001325 if (output_mode)
1326 free(output_mode);
1327 if (output_transform)
1328 free(output_transform);
Scott Moreau248aaec2012-08-03 14:19:52 -06001329 output_mode = NULL;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001330 output_transform = NULL;
Scott Moreau248aaec2012-08-03 14:19:52 -06001331}
1332
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001333WL_EXPORT struct weston_compositor *
Daniel Stonec1be8e52012-06-01 11:14:02 -04001334backend_init(struct wl_display *display, int argc, char *argv[],
1335 const char *config_file)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001336{
Scott Moreau248aaec2012-08-03 14:19:52 -06001337 int fullscreen = 0;
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001338 int no_input = 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001339
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001340 const struct weston_option x11_options[] = {
Scott Moreau248aaec2012-08-03 14:19:52 -06001341 { WESTON_OPTION_INTEGER, "width", 0, &option_width },
1342 { WESTON_OPTION_INTEGER, "height", 0, &option_height },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001343 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Scott Moreau248aaec2012-08-03 14:19:52 -06001344 { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001345 { WESTON_OPTION_BOOLEAN, "no-input", 0, &no_input },
Kristian Høgsberg83eeacb2011-06-18 04:20:54 -04001346 };
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04001347
1348 parse_options(x11_options, ARRAY_LENGTH(x11_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001349
Scott Moreau248aaec2012-08-03 14:19:52 -06001350 wl_list_init(&configured_output_list);
1351
1352 const struct config_key x11_config_keys[] = {
1353 { "name", CONFIG_KEY_STRING, &output_name },
1354 { "mode", CONFIG_KEY_STRING, &output_mode },
Scott Moreau1bad5db2012-08-18 01:04:05 -06001355 { "transform", CONFIG_KEY_STRING, &output_transform },
Scott Moreau248aaec2012-08-03 14:19:52 -06001356 };
1357
1358 const struct config_section config_section[] = {
1359 { "output", x11_config_keys,
1360 ARRAY_LENGTH(x11_config_keys), output_section_done },
1361 };
1362
1363 parse_config_file(config_file, config_section,
1364 ARRAY_LENGTH(config_section), NULL);
1365
Kristian Høgsberg1ccd9d22011-07-21 10:22:13 -07001366 return x11_compositor_create(display,
Scott Moreau248aaec2012-08-03 14:19:52 -06001367 fullscreen,
Pekka Paalanen36f155f2012-06-07 15:07:05 +03001368 no_input,
Daniel Stonec1be8e52012-06-01 11:14:02 -04001369 argc, argv, config_file);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001370}